blob: af38a215f0e00457cd23d9c7eb64e11d545202a6 [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
Thomas Petazzonidf089aa2017-08-03 10:41:58 +0200688#define MVPP2_MAX_THREADS 8
Thomas Petazzonia7868412017-03-07 16:53:13 +0100689
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
Thomas Petazzonidf089aa2017-08-03 10:41:58 +0200704 /* On PPv2.2, each "software thread" can access the base
705 * register through a separate address space, each 64 KB apart
706 * from each other. Typically, such address spaces will be
707 * used per CPU.
Thomas Petazzonia7868412017-03-07 16:53:13 +0100708 */
Thomas Petazzonidf089aa2017-08-03 10:41:58 +0200709 void __iomem *swth_base[MVPP2_MAX_THREADS];
Marcin Wojtas3f518502014-07-10 16:52:13 -0300710
711 /* Common clocks */
712 struct clk *pp_clk;
713 struct clk *gop_clk;
Thomas Petazzonifceb55d2017-03-07 16:53:18 +0100714 struct clk *mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300715
716 /* List of pointers to port structures */
717 struct mvpp2_port **port_list;
718
719 /* Aggregated TXQs */
720 struct mvpp2_tx_queue *aggr_txqs;
721
722 /* BM pools */
723 struct mvpp2_bm_pool *bm_pools;
724
725 /* PRS shadow table */
726 struct mvpp2_prs_shadow *prs_shadow;
727 /* PRS auxiliary table for double vlan entries control */
728 bool *prs_double_vlans;
729
730 /* Tclk value */
731 u32 tclk;
Thomas Petazzonifaca9242017-03-07 16:53:06 +0100732
733 /* HW version */
734 enum { MVPP21, MVPP22 } hw_version;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +0100735
736 /* Maximum number of RXQs per port */
737 unsigned int max_port_rxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300738};
739
740struct mvpp2_pcpu_stats {
741 struct u64_stats_sync syncp;
742 u64 rx_packets;
743 u64 rx_bytes;
744 u64 tx_packets;
745 u64 tx_bytes;
746};
747
Marcin Wojtasedc660f2015-08-06 19:00:30 +0200748/* Per-CPU port control */
749struct mvpp2_port_pcpu {
750 struct hrtimer tx_done_timer;
751 bool timer_scheduled;
752 /* Tasklet for egress finalization */
753 struct tasklet_struct tx_done_tasklet;
754};
755
Marcin Wojtas3f518502014-07-10 16:52:13 -0300756struct mvpp2_port {
757 u8 id;
758
Thomas Petazzonia7868412017-03-07 16:53:13 +0100759 /* Index of the port from the "group of ports" complex point
760 * of view
761 */
762 int gop_id;
763
Marcin Wojtas3f518502014-07-10 16:52:13 -0300764 int irq;
765
766 struct mvpp2 *priv;
767
768 /* Per-port registers' base address */
769 void __iomem *base;
770
771 struct mvpp2_rx_queue **rxqs;
Thomas Petazzoni09f83972017-08-03 10:41:57 +0200772 unsigned int nrxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300773 struct mvpp2_tx_queue **txqs;
Thomas Petazzoni09f83972017-08-03 10:41:57 +0200774 unsigned int ntxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300775 struct net_device *dev;
776
777 int pkt_size;
778
779 u32 pending_cause_rx;
780 struct napi_struct napi;
781
Marcin Wojtasedc660f2015-08-06 19:00:30 +0200782 /* Per-CPU port control */
783 struct mvpp2_port_pcpu __percpu *pcpu;
784
Marcin Wojtas3f518502014-07-10 16:52:13 -0300785 /* Flags */
786 unsigned long flags;
787
788 u16 tx_ring_size;
789 u16 rx_ring_size;
790 struct mvpp2_pcpu_stats __percpu *stats;
791
Marcin Wojtas3f518502014-07-10 16:52:13 -0300792 phy_interface_t phy_interface;
793 struct device_node *phy_node;
794 unsigned int link;
795 unsigned int duplex;
796 unsigned int speed;
797
798 struct mvpp2_bm_pool *pool_long;
799 struct mvpp2_bm_pool *pool_short;
800
801 /* Index of first port's physical RXQ */
802 u8 first_rxq;
803};
804
805/* The mvpp2_tx_desc and mvpp2_rx_desc structures describe the
806 * layout of the transmit and reception DMA descriptors, and their
807 * layout is therefore defined by the hardware design
808 */
809
810#define MVPP2_TXD_L3_OFF_SHIFT 0
811#define MVPP2_TXD_IP_HLEN_SHIFT 8
812#define MVPP2_TXD_L4_CSUM_FRAG BIT(13)
813#define MVPP2_TXD_L4_CSUM_NOT BIT(14)
814#define MVPP2_TXD_IP_CSUM_DISABLE BIT(15)
815#define MVPP2_TXD_PADDING_DISABLE BIT(23)
816#define MVPP2_TXD_L4_UDP BIT(24)
817#define MVPP2_TXD_L3_IP6 BIT(26)
818#define MVPP2_TXD_L_DESC BIT(28)
819#define MVPP2_TXD_F_DESC BIT(29)
820
821#define MVPP2_RXD_ERR_SUMMARY BIT(15)
822#define MVPP2_RXD_ERR_CODE_MASK (BIT(13) | BIT(14))
823#define MVPP2_RXD_ERR_CRC 0x0
824#define MVPP2_RXD_ERR_OVERRUN BIT(13)
825#define MVPP2_RXD_ERR_RESOURCE (BIT(13) | BIT(14))
826#define MVPP2_RXD_BM_POOL_ID_OFFS 16
827#define MVPP2_RXD_BM_POOL_ID_MASK (BIT(16) | BIT(17) | BIT(18))
828#define MVPP2_RXD_HWF_SYNC BIT(21)
829#define MVPP2_RXD_L4_CSUM_OK BIT(22)
830#define MVPP2_RXD_IP4_HEADER_ERR BIT(24)
831#define MVPP2_RXD_L4_TCP BIT(25)
832#define MVPP2_RXD_L4_UDP BIT(26)
833#define MVPP2_RXD_L3_IP4 BIT(28)
834#define MVPP2_RXD_L3_IP6 BIT(30)
835#define MVPP2_RXD_BUF_HDR BIT(31)
836
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100837/* HW TX descriptor for PPv2.1 */
838struct mvpp21_tx_desc {
Marcin Wojtas3f518502014-07-10 16:52:13 -0300839 u32 command; /* Options used by HW for packet transmitting.*/
840 u8 packet_offset; /* the offset from the buffer beginning */
841 u8 phys_txq; /* destination queue ID */
842 u16 data_size; /* data size of transmitted packet in bytes */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100843 u32 buf_dma_addr; /* physical addr of transmitted buffer */
Marcin Wojtas3f518502014-07-10 16:52:13 -0300844 u32 buf_cookie; /* cookie for access to TX buffer in tx path */
845 u32 reserved1[3]; /* hw_cmd (for future use, BM, PON, PNC) */
846 u32 reserved2; /* reserved (for future use) */
847};
848
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100849/* HW RX descriptor for PPv2.1 */
850struct mvpp21_rx_desc {
Marcin Wojtas3f518502014-07-10 16:52:13 -0300851 u32 status; /* info about received packet */
852 u16 reserved1; /* parser_info (for future use, PnC) */
853 u16 data_size; /* size of received packet in bytes */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100854 u32 buf_dma_addr; /* physical address of the buffer */
Marcin Wojtas3f518502014-07-10 16:52:13 -0300855 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
856 u16 reserved2; /* gem_port_id (for future use, PON) */
857 u16 reserved3; /* csum_l4 (for future use, PnC) */
858 u8 reserved4; /* bm_qset (for future use, BM) */
859 u8 reserved5;
860 u16 reserved6; /* classify_info (for future use, PnC) */
861 u32 reserved7; /* flow_id (for future use, PnC) */
862 u32 reserved8;
863};
864
Thomas Petazzonie7c53592017-03-07 16:53:08 +0100865/* HW TX descriptor for PPv2.2 */
866struct mvpp22_tx_desc {
867 u32 command;
868 u8 packet_offset;
869 u8 phys_txq;
870 u16 data_size;
871 u64 reserved1;
872 u64 buf_dma_addr_ptp;
873 u64 buf_cookie_misc;
874};
875
876/* HW RX descriptor for PPv2.2 */
877struct mvpp22_rx_desc {
878 u32 status;
879 u16 reserved1;
880 u16 data_size;
881 u32 reserved2;
882 u32 reserved3;
883 u64 buf_dma_addr_key_hash;
884 u64 buf_cookie_misc;
885};
886
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100887/* Opaque type used by the driver to manipulate the HW TX and RX
888 * descriptors
889 */
890struct mvpp2_tx_desc {
891 union {
892 struct mvpp21_tx_desc pp21;
Thomas Petazzonie7c53592017-03-07 16:53:08 +0100893 struct mvpp22_tx_desc pp22;
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100894 };
895};
896
897struct mvpp2_rx_desc {
898 union {
899 struct mvpp21_rx_desc pp21;
Thomas Petazzonie7c53592017-03-07 16:53:08 +0100900 struct mvpp22_rx_desc pp22;
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100901 };
902};
903
Thomas Petazzoni83544912016-12-21 11:28:49 +0100904struct mvpp2_txq_pcpu_buf {
905 /* Transmitted SKB */
906 struct sk_buff *skb;
907
908 /* Physical address of transmitted buffer */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100909 dma_addr_t dma;
Thomas Petazzoni83544912016-12-21 11:28:49 +0100910
911 /* Size transmitted */
912 size_t size;
913};
914
Marcin Wojtas3f518502014-07-10 16:52:13 -0300915/* Per-CPU Tx queue control */
916struct mvpp2_txq_pcpu {
917 int cpu;
918
919 /* Number of Tx DMA descriptors in the descriptor ring */
920 int size;
921
922 /* Number of currently used Tx DMA descriptor in the
923 * descriptor ring
924 */
925 int count;
926
927 /* Number of Tx DMA descriptors reserved for each CPU */
928 int reserved_num;
929
Thomas Petazzoni83544912016-12-21 11:28:49 +0100930 /* Infos about transmitted buffers */
931 struct mvpp2_txq_pcpu_buf *buffs;
Marcin Wojtas71ce3912015-08-06 19:00:29 +0200932
Marcin Wojtas3f518502014-07-10 16:52:13 -0300933 /* Index of last TX DMA descriptor that was inserted */
934 int txq_put_index;
935
936 /* Index of the TX DMA descriptor to be cleaned up */
937 int txq_get_index;
938};
939
940struct mvpp2_tx_queue {
941 /* Physical number of this Tx queue */
942 u8 id;
943
944 /* Logical number of this Tx queue */
945 u8 log_id;
946
947 /* Number of Tx DMA descriptors in the descriptor ring */
948 int size;
949
950 /* Number of currently used Tx DMA descriptor in the descriptor ring */
951 int count;
952
953 /* Per-CPU control of physical Tx queues */
954 struct mvpp2_txq_pcpu __percpu *pcpu;
955
Marcin Wojtas3f518502014-07-10 16:52:13 -0300956 u32 done_pkts_coal;
957
958 /* Virtual address of thex Tx DMA descriptors array */
959 struct mvpp2_tx_desc *descs;
960
961 /* DMA address of the Tx DMA descriptors array */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100962 dma_addr_t descs_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300963
964 /* Index of the last Tx DMA descriptor */
965 int last_desc;
966
967 /* Index of the next Tx DMA descriptor to process */
968 int next_desc_to_proc;
969};
970
971struct mvpp2_rx_queue {
972 /* RX queue number, in the range 0-31 for physical RXQs */
973 u8 id;
974
975 /* Num of rx descriptors in the rx descriptor ring */
976 int size;
977
978 u32 pkts_coal;
979 u32 time_coal;
980
981 /* Virtual address of the RX DMA descriptors array */
982 struct mvpp2_rx_desc *descs;
983
984 /* DMA address of the RX DMA descriptors array */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100985 dma_addr_t descs_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300986
987 /* Index of the last RX DMA descriptor */
988 int last_desc;
989
990 /* Index of the next RX DMA descriptor to process */
991 int next_desc_to_proc;
992
993 /* ID of port to which physical RXQ is mapped */
994 int port;
995
996 /* Port's logic RXQ number to which physical RXQ is mapped */
997 int logic_rxq;
998};
999
1000union mvpp2_prs_tcam_entry {
1001 u32 word[MVPP2_PRS_TCAM_WORDS];
1002 u8 byte[MVPP2_PRS_TCAM_WORDS * 4];
1003};
1004
1005union mvpp2_prs_sram_entry {
1006 u32 word[MVPP2_PRS_SRAM_WORDS];
1007 u8 byte[MVPP2_PRS_SRAM_WORDS * 4];
1008};
1009
1010struct mvpp2_prs_entry {
1011 u32 index;
1012 union mvpp2_prs_tcam_entry tcam;
1013 union mvpp2_prs_sram_entry sram;
1014};
1015
1016struct mvpp2_prs_shadow {
1017 bool valid;
1018 bool finish;
1019
1020 /* Lookup ID */
1021 int lu;
1022
1023 /* User defined offset */
1024 int udf;
1025
1026 /* Result info */
1027 u32 ri;
1028 u32 ri_mask;
1029};
1030
1031struct mvpp2_cls_flow_entry {
1032 u32 index;
1033 u32 data[MVPP2_CLS_FLOWS_TBL_DATA_WORDS];
1034};
1035
1036struct mvpp2_cls_lookup_entry {
1037 u32 lkpid;
1038 u32 way;
1039 u32 data;
1040};
1041
1042struct mvpp2_bm_pool {
1043 /* Pool number in the range 0-7 */
1044 int id;
1045 enum mvpp2_bm_type type;
1046
1047 /* Buffer Pointers Pool External (BPPE) size */
1048 int size;
Thomas Petazzonid01524d2017-03-07 16:53:09 +01001049 /* BPPE size in bytes */
1050 int size_bytes;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001051 /* Number of buffers for this pool */
1052 int buf_num;
1053 /* Pool buffer size */
1054 int buf_size;
1055 /* Packet size */
1056 int pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01001057 int frag_size;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001058
1059 /* BPPE virtual base address */
1060 u32 *virt_addr;
Thomas Petazzoni20396132017-03-07 16:53:00 +01001061 /* BPPE DMA base address */
1062 dma_addr_t dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001063
1064 /* Ports using BM pool */
1065 u32 port_map;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001066};
1067
Marcin Wojtas3f518502014-07-10 16:52:13 -03001068#define MVPP2_DRIVER_NAME "mvpp2"
1069#define MVPP2_DRIVER_VERSION "1.0"
1070
1071/* Utility/helper methods */
1072
1073static void mvpp2_write(struct mvpp2 *priv, u32 offset, u32 data)
1074{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001075 writel(data, priv->swth_base[0] + offset);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001076}
1077
1078static u32 mvpp2_read(struct mvpp2 *priv, u32 offset)
1079{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001080 return readl(priv->swth_base[0] + offset);
Thomas Petazzonia7868412017-03-07 16:53:13 +01001081}
1082
1083/* These accessors should be used to access:
1084 *
1085 * - per-CPU registers, where each CPU has its own copy of the
1086 * register.
1087 *
1088 * MVPP2_BM_VIRT_ALLOC_REG
1089 * MVPP2_BM_ADDR_HIGH_ALLOC
1090 * MVPP22_BM_ADDR_HIGH_RLS_REG
1091 * MVPP2_BM_VIRT_RLS_REG
1092 * MVPP2_ISR_RX_TX_CAUSE_REG
1093 * MVPP2_ISR_RX_TX_MASK_REG
1094 * MVPP2_TXQ_NUM_REG
1095 * MVPP2_AGGR_TXQ_UPDATE_REG
1096 * MVPP2_TXQ_RSVD_REQ_REG
1097 * MVPP2_TXQ_RSVD_RSLT_REG
1098 * MVPP2_TXQ_SENT_REG
1099 * MVPP2_RXQ_NUM_REG
1100 *
1101 * - global registers that must be accessed through a specific CPU
1102 * window, because they are related to an access to a per-CPU
1103 * register
1104 *
1105 * MVPP2_BM_PHY_ALLOC_REG (related to MVPP2_BM_VIRT_ALLOC_REG)
1106 * MVPP2_BM_PHY_RLS_REG (related to MVPP2_BM_VIRT_RLS_REG)
1107 * MVPP2_RXQ_THRESH_REG (related to MVPP2_RXQ_NUM_REG)
1108 * MVPP2_RXQ_DESC_ADDR_REG (related to MVPP2_RXQ_NUM_REG)
1109 * MVPP2_RXQ_DESC_SIZE_REG (related to MVPP2_RXQ_NUM_REG)
1110 * MVPP2_RXQ_INDEX_REG (related to MVPP2_RXQ_NUM_REG)
1111 * MVPP2_TXQ_PENDING_REG (related to MVPP2_TXQ_NUM_REG)
1112 * MVPP2_TXQ_DESC_ADDR_REG (related to MVPP2_TXQ_NUM_REG)
1113 * MVPP2_TXQ_DESC_SIZE_REG (related to MVPP2_TXQ_NUM_REG)
1114 * MVPP2_TXQ_INDEX_REG (related to MVPP2_TXQ_NUM_REG)
1115 * MVPP2_TXQ_PENDING_REG (related to MVPP2_TXQ_NUM_REG)
1116 * MVPP2_TXQ_PREF_BUF_REG (related to MVPP2_TXQ_NUM_REG)
1117 * MVPP2_TXQ_PREF_BUF_REG (related to MVPP2_TXQ_NUM_REG)
1118 */
1119static void mvpp2_percpu_write(struct mvpp2 *priv, int cpu,
1120 u32 offset, u32 data)
1121{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001122 writel(data, priv->swth_base[cpu] + offset);
Thomas Petazzonia7868412017-03-07 16:53:13 +01001123}
1124
1125static u32 mvpp2_percpu_read(struct mvpp2 *priv, int cpu,
1126 u32 offset)
1127{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001128 return readl(priv->swth_base[cpu] + offset);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001129}
1130
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001131static dma_addr_t mvpp2_txdesc_dma_addr_get(struct mvpp2_port *port,
1132 struct mvpp2_tx_desc *tx_desc)
1133{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001134 if (port->priv->hw_version == MVPP21)
1135 return tx_desc->pp21.buf_dma_addr;
1136 else
1137 return tx_desc->pp22.buf_dma_addr_ptp & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001138}
1139
1140static void mvpp2_txdesc_dma_addr_set(struct mvpp2_port *port,
1141 struct mvpp2_tx_desc *tx_desc,
1142 dma_addr_t dma_addr)
1143{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001144 if (port->priv->hw_version == MVPP21) {
1145 tx_desc->pp21.buf_dma_addr = dma_addr;
1146 } else {
1147 u64 val = (u64)dma_addr;
1148
1149 tx_desc->pp22.buf_dma_addr_ptp &= ~GENMASK_ULL(40, 0);
1150 tx_desc->pp22.buf_dma_addr_ptp |= val;
1151 }
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001152}
1153
1154static size_t mvpp2_txdesc_size_get(struct mvpp2_port *port,
1155 struct mvpp2_tx_desc *tx_desc)
1156{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001157 if (port->priv->hw_version == MVPP21)
1158 return tx_desc->pp21.data_size;
1159 else
1160 return tx_desc->pp22.data_size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001161}
1162
1163static void mvpp2_txdesc_size_set(struct mvpp2_port *port,
1164 struct mvpp2_tx_desc *tx_desc,
1165 size_t size)
1166{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001167 if (port->priv->hw_version == MVPP21)
1168 tx_desc->pp21.data_size = size;
1169 else
1170 tx_desc->pp22.data_size = size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001171}
1172
1173static void mvpp2_txdesc_txq_set(struct mvpp2_port *port,
1174 struct mvpp2_tx_desc *tx_desc,
1175 unsigned int txq)
1176{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001177 if (port->priv->hw_version == MVPP21)
1178 tx_desc->pp21.phys_txq = txq;
1179 else
1180 tx_desc->pp22.phys_txq = txq;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001181}
1182
1183static void mvpp2_txdesc_cmd_set(struct mvpp2_port *port,
1184 struct mvpp2_tx_desc *tx_desc,
1185 unsigned int command)
1186{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001187 if (port->priv->hw_version == MVPP21)
1188 tx_desc->pp21.command = command;
1189 else
1190 tx_desc->pp22.command = command;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001191}
1192
1193static void mvpp2_txdesc_offset_set(struct mvpp2_port *port,
1194 struct mvpp2_tx_desc *tx_desc,
1195 unsigned int offset)
1196{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001197 if (port->priv->hw_version == MVPP21)
1198 tx_desc->pp21.packet_offset = offset;
1199 else
1200 tx_desc->pp22.packet_offset = offset;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001201}
1202
1203static unsigned int mvpp2_txdesc_offset_get(struct mvpp2_port *port,
1204 struct mvpp2_tx_desc *tx_desc)
1205{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001206 if (port->priv->hw_version == MVPP21)
1207 return tx_desc->pp21.packet_offset;
1208 else
1209 return tx_desc->pp22.packet_offset;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001210}
1211
1212static dma_addr_t mvpp2_rxdesc_dma_addr_get(struct mvpp2_port *port,
1213 struct mvpp2_rx_desc *rx_desc)
1214{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001215 if (port->priv->hw_version == MVPP21)
1216 return rx_desc->pp21.buf_dma_addr;
1217 else
1218 return rx_desc->pp22.buf_dma_addr_key_hash & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001219}
1220
1221static unsigned long mvpp2_rxdesc_cookie_get(struct mvpp2_port *port,
1222 struct mvpp2_rx_desc *rx_desc)
1223{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001224 if (port->priv->hw_version == MVPP21)
1225 return rx_desc->pp21.buf_cookie;
1226 else
1227 return rx_desc->pp22.buf_cookie_misc & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001228}
1229
1230static size_t mvpp2_rxdesc_size_get(struct mvpp2_port *port,
1231 struct mvpp2_rx_desc *rx_desc)
1232{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001233 if (port->priv->hw_version == MVPP21)
1234 return rx_desc->pp21.data_size;
1235 else
1236 return rx_desc->pp22.data_size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001237}
1238
1239static u32 mvpp2_rxdesc_status_get(struct mvpp2_port *port,
1240 struct mvpp2_rx_desc *rx_desc)
1241{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001242 if (port->priv->hw_version == MVPP21)
1243 return rx_desc->pp21.status;
1244 else
1245 return rx_desc->pp22.status;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001246}
1247
Marcin Wojtas3f518502014-07-10 16:52:13 -03001248static void mvpp2_txq_inc_get(struct mvpp2_txq_pcpu *txq_pcpu)
1249{
1250 txq_pcpu->txq_get_index++;
1251 if (txq_pcpu->txq_get_index == txq_pcpu->size)
1252 txq_pcpu->txq_get_index = 0;
1253}
1254
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001255static void mvpp2_txq_inc_put(struct mvpp2_port *port,
1256 struct mvpp2_txq_pcpu *txq_pcpu,
Marcin Wojtas71ce3912015-08-06 19:00:29 +02001257 struct sk_buff *skb,
1258 struct mvpp2_tx_desc *tx_desc)
Marcin Wojtas3f518502014-07-10 16:52:13 -03001259{
Thomas Petazzoni83544912016-12-21 11:28:49 +01001260 struct mvpp2_txq_pcpu_buf *tx_buf =
1261 txq_pcpu->buffs + txq_pcpu->txq_put_index;
1262 tx_buf->skb = skb;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001263 tx_buf->size = mvpp2_txdesc_size_get(port, tx_desc);
1264 tx_buf->dma = mvpp2_txdesc_dma_addr_get(port, tx_desc) +
1265 mvpp2_txdesc_offset_get(port, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001266 txq_pcpu->txq_put_index++;
1267 if (txq_pcpu->txq_put_index == txq_pcpu->size)
1268 txq_pcpu->txq_put_index = 0;
1269}
1270
1271/* Get number of physical egress port */
1272static inline int mvpp2_egress_port(struct mvpp2_port *port)
1273{
1274 return MVPP2_MAX_TCONT + port->id;
1275}
1276
1277/* Get number of physical TXQ */
1278static inline int mvpp2_txq_phys(int port, int txq)
1279{
1280 return (MVPP2_MAX_TCONT + port) * MVPP2_MAX_TXQ + txq;
1281}
1282
1283/* Parser configuration routines */
1284
1285/* Update parser tcam and sram hw entries */
1286static int mvpp2_prs_hw_write(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1287{
1288 int i;
1289
1290 if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1291 return -EINVAL;
1292
1293 /* Clear entry invalidation bit */
1294 pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] &= ~MVPP2_PRS_TCAM_INV_MASK;
1295
1296 /* Write tcam index - indirect access */
1297 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1298 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1299 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), pe->tcam.word[i]);
1300
1301 /* Write sram index - indirect access */
1302 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1303 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1304 mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), pe->sram.word[i]);
1305
1306 return 0;
1307}
1308
1309/* Read tcam entry from hw */
1310static int mvpp2_prs_hw_read(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1311{
1312 int i;
1313
1314 if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1315 return -EINVAL;
1316
1317 /* Write tcam index - indirect access */
1318 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1319
1320 pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] = mvpp2_read(priv,
1321 MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD));
1322 if (pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] & MVPP2_PRS_TCAM_INV_MASK)
1323 return MVPP2_PRS_TCAM_ENTRY_INVALID;
1324
1325 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1326 pe->tcam.word[i] = mvpp2_read(priv, MVPP2_PRS_TCAM_DATA_REG(i));
1327
1328 /* Write sram index - indirect access */
1329 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1330 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1331 pe->sram.word[i] = mvpp2_read(priv, MVPP2_PRS_SRAM_DATA_REG(i));
1332
1333 return 0;
1334}
1335
1336/* Invalidate tcam hw entry */
1337static void mvpp2_prs_hw_inv(struct mvpp2 *priv, int index)
1338{
1339 /* Write index - indirect access */
1340 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
1341 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD),
1342 MVPP2_PRS_TCAM_INV_MASK);
1343}
1344
1345/* Enable shadow table entry and set its lookup ID */
1346static void mvpp2_prs_shadow_set(struct mvpp2 *priv, int index, int lu)
1347{
1348 priv->prs_shadow[index].valid = true;
1349 priv->prs_shadow[index].lu = lu;
1350}
1351
1352/* Update ri fields in shadow table entry */
1353static void mvpp2_prs_shadow_ri_set(struct mvpp2 *priv, int index,
1354 unsigned int ri, unsigned int ri_mask)
1355{
1356 priv->prs_shadow[index].ri_mask = ri_mask;
1357 priv->prs_shadow[index].ri = ri;
1358}
1359
1360/* Update lookup field in tcam sw entry */
1361static void mvpp2_prs_tcam_lu_set(struct mvpp2_prs_entry *pe, unsigned int lu)
1362{
1363 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_LU_BYTE);
1364
1365 pe->tcam.byte[MVPP2_PRS_TCAM_LU_BYTE] = lu;
1366 pe->tcam.byte[enable_off] = MVPP2_PRS_LU_MASK;
1367}
1368
1369/* Update mask for single port in tcam sw entry */
1370static void mvpp2_prs_tcam_port_set(struct mvpp2_prs_entry *pe,
1371 unsigned int port, bool add)
1372{
1373 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1374
1375 if (add)
1376 pe->tcam.byte[enable_off] &= ~(1 << port);
1377 else
1378 pe->tcam.byte[enable_off] |= 1 << port;
1379}
1380
1381/* Update port map in tcam sw entry */
1382static void mvpp2_prs_tcam_port_map_set(struct mvpp2_prs_entry *pe,
1383 unsigned int ports)
1384{
1385 unsigned char port_mask = MVPP2_PRS_PORT_MASK;
1386 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1387
1388 pe->tcam.byte[MVPP2_PRS_TCAM_PORT_BYTE] = 0;
1389 pe->tcam.byte[enable_off] &= ~port_mask;
1390 pe->tcam.byte[enable_off] |= ~ports & MVPP2_PRS_PORT_MASK;
1391}
1392
1393/* Obtain port map from tcam sw entry */
1394static unsigned int mvpp2_prs_tcam_port_map_get(struct mvpp2_prs_entry *pe)
1395{
1396 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1397
1398 return ~(pe->tcam.byte[enable_off]) & MVPP2_PRS_PORT_MASK;
1399}
1400
1401/* Set byte of data and its enable bits in tcam sw entry */
1402static void mvpp2_prs_tcam_data_byte_set(struct mvpp2_prs_entry *pe,
1403 unsigned int offs, unsigned char byte,
1404 unsigned char enable)
1405{
1406 pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)] = byte;
1407 pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)] = enable;
1408}
1409
1410/* Get byte of data and its enable bits from tcam sw entry */
1411static void mvpp2_prs_tcam_data_byte_get(struct mvpp2_prs_entry *pe,
1412 unsigned int offs, unsigned char *byte,
1413 unsigned char *enable)
1414{
1415 *byte = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)];
1416 *enable = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)];
1417}
1418
1419/* Compare tcam data bytes with a pattern */
1420static bool mvpp2_prs_tcam_data_cmp(struct mvpp2_prs_entry *pe, int offs,
1421 u16 data)
1422{
1423 int off = MVPP2_PRS_TCAM_DATA_BYTE(offs);
1424 u16 tcam_data;
1425
1426 tcam_data = (8 << pe->tcam.byte[off + 1]) | pe->tcam.byte[off];
1427 if (tcam_data != data)
1428 return false;
1429 return true;
1430}
1431
1432/* Update ai bits in tcam sw entry */
1433static void mvpp2_prs_tcam_ai_update(struct mvpp2_prs_entry *pe,
1434 unsigned int bits, unsigned int enable)
1435{
1436 int i, ai_idx = MVPP2_PRS_TCAM_AI_BYTE;
1437
1438 for (i = 0; i < MVPP2_PRS_AI_BITS; i++) {
1439
1440 if (!(enable & BIT(i)))
1441 continue;
1442
1443 if (bits & BIT(i))
1444 pe->tcam.byte[ai_idx] |= 1 << i;
1445 else
1446 pe->tcam.byte[ai_idx] &= ~(1 << i);
1447 }
1448
1449 pe->tcam.byte[MVPP2_PRS_TCAM_EN_OFFS(ai_idx)] |= enable;
1450}
1451
1452/* Get ai bits from tcam sw entry */
1453static int mvpp2_prs_tcam_ai_get(struct mvpp2_prs_entry *pe)
1454{
1455 return pe->tcam.byte[MVPP2_PRS_TCAM_AI_BYTE];
1456}
1457
1458/* Set ethertype in tcam sw entry */
1459static void mvpp2_prs_match_etype(struct mvpp2_prs_entry *pe, int offset,
1460 unsigned short ethertype)
1461{
1462 mvpp2_prs_tcam_data_byte_set(pe, offset + 0, ethertype >> 8, 0xff);
1463 mvpp2_prs_tcam_data_byte_set(pe, offset + 1, ethertype & 0xff, 0xff);
1464}
1465
1466/* Set bits in sram sw entry */
1467static void mvpp2_prs_sram_bits_set(struct mvpp2_prs_entry *pe, int bit_num,
1468 int val)
1469{
1470 pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] |= (val << (bit_num % 8));
1471}
1472
1473/* Clear bits in sram sw entry */
1474static void mvpp2_prs_sram_bits_clear(struct mvpp2_prs_entry *pe, int bit_num,
1475 int val)
1476{
1477 pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] &= ~(val << (bit_num % 8));
1478}
1479
1480/* Update ri bits in sram sw entry */
1481static void mvpp2_prs_sram_ri_update(struct mvpp2_prs_entry *pe,
1482 unsigned int bits, unsigned int mask)
1483{
1484 unsigned int i;
1485
1486 for (i = 0; i < MVPP2_PRS_SRAM_RI_CTRL_BITS; i++) {
1487 int ri_off = MVPP2_PRS_SRAM_RI_OFFS;
1488
1489 if (!(mask & BIT(i)))
1490 continue;
1491
1492 if (bits & BIT(i))
1493 mvpp2_prs_sram_bits_set(pe, ri_off + i, 1);
1494 else
1495 mvpp2_prs_sram_bits_clear(pe, ri_off + i, 1);
1496
1497 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_RI_CTRL_OFFS + i, 1);
1498 }
1499}
1500
1501/* Obtain ri bits from sram sw entry */
1502static int mvpp2_prs_sram_ri_get(struct mvpp2_prs_entry *pe)
1503{
1504 return pe->sram.word[MVPP2_PRS_SRAM_RI_WORD];
1505}
1506
1507/* Update ai bits in sram sw entry */
1508static void mvpp2_prs_sram_ai_update(struct mvpp2_prs_entry *pe,
1509 unsigned int bits, unsigned int mask)
1510{
1511 unsigned int i;
1512 int ai_off = MVPP2_PRS_SRAM_AI_OFFS;
1513
1514 for (i = 0; i < MVPP2_PRS_SRAM_AI_CTRL_BITS; i++) {
1515
1516 if (!(mask & BIT(i)))
1517 continue;
1518
1519 if (bits & BIT(i))
1520 mvpp2_prs_sram_bits_set(pe, ai_off + i, 1);
1521 else
1522 mvpp2_prs_sram_bits_clear(pe, ai_off + i, 1);
1523
1524 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_AI_CTRL_OFFS + i, 1);
1525 }
1526}
1527
1528/* Read ai bits from sram sw entry */
1529static int mvpp2_prs_sram_ai_get(struct mvpp2_prs_entry *pe)
1530{
1531 u8 bits;
1532 int ai_off = MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_AI_OFFS);
1533 int ai_en_off = ai_off + 1;
1534 int ai_shift = MVPP2_PRS_SRAM_AI_OFFS % 8;
1535
1536 bits = (pe->sram.byte[ai_off] >> ai_shift) |
1537 (pe->sram.byte[ai_en_off] << (8 - ai_shift));
1538
1539 return bits;
1540}
1541
1542/* In sram sw entry set lookup ID field of the tcam key to be used in the next
1543 * lookup interation
1544 */
1545static void mvpp2_prs_sram_next_lu_set(struct mvpp2_prs_entry *pe,
1546 unsigned int lu)
1547{
1548 int sram_next_off = MVPP2_PRS_SRAM_NEXT_LU_OFFS;
1549
1550 mvpp2_prs_sram_bits_clear(pe, sram_next_off,
1551 MVPP2_PRS_SRAM_NEXT_LU_MASK);
1552 mvpp2_prs_sram_bits_set(pe, sram_next_off, lu);
1553}
1554
1555/* In the sram sw entry set sign and value of the next lookup offset
1556 * and the offset value generated to the classifier
1557 */
1558static void mvpp2_prs_sram_shift_set(struct mvpp2_prs_entry *pe, int shift,
1559 unsigned int op)
1560{
1561 /* Set sign */
1562 if (shift < 0) {
1563 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1564 shift = 0 - shift;
1565 } else {
1566 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1567 }
1568
1569 /* Set value */
1570 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_SHIFT_OFFS)] =
1571 (unsigned char)shift;
1572
1573 /* Reset and set operation */
1574 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS,
1575 MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK);
1576 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS, op);
1577
1578 /* Set base offset as current */
1579 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1580}
1581
1582/* In the sram sw entry set sign and value of the user defined offset
1583 * generated to the classifier
1584 */
1585static void mvpp2_prs_sram_offset_set(struct mvpp2_prs_entry *pe,
1586 unsigned int type, int offset,
1587 unsigned int op)
1588{
1589 /* Set sign */
1590 if (offset < 0) {
1591 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1592 offset = 0 - offset;
1593 } else {
1594 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1595 }
1596
1597 /* Set value */
1598 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_OFFS,
1599 MVPP2_PRS_SRAM_UDF_MASK);
1600 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_OFFS, offset);
1601 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1602 MVPP2_PRS_SRAM_UDF_BITS)] &=
1603 ~(MVPP2_PRS_SRAM_UDF_MASK >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1604 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1605 MVPP2_PRS_SRAM_UDF_BITS)] |=
1606 (offset >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1607
1608 /* Set offset type */
1609 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS,
1610 MVPP2_PRS_SRAM_UDF_TYPE_MASK);
1611 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS, type);
1612
1613 /* Set offset operation */
1614 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS,
1615 MVPP2_PRS_SRAM_OP_SEL_UDF_MASK);
1616 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS, op);
1617
1618 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1619 MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] &=
1620 ~(MVPP2_PRS_SRAM_OP_SEL_UDF_MASK >>
1621 (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1622
1623 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1624 MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] |=
1625 (op >> (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1626
1627 /* Set base offset as current */
1628 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1629}
1630
1631/* Find parser flow entry */
1632static struct mvpp2_prs_entry *mvpp2_prs_flow_find(struct mvpp2 *priv, int flow)
1633{
1634 struct mvpp2_prs_entry *pe;
1635 int tid;
1636
1637 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
1638 if (!pe)
1639 return NULL;
1640 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
1641
1642 /* Go through the all entires with MVPP2_PRS_LU_FLOWS */
1643 for (tid = MVPP2_PRS_TCAM_SRAM_SIZE - 1; tid >= 0; tid--) {
1644 u8 bits;
1645
1646 if (!priv->prs_shadow[tid].valid ||
1647 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_FLOWS)
1648 continue;
1649
1650 pe->index = tid;
1651 mvpp2_prs_hw_read(priv, pe);
1652 bits = mvpp2_prs_sram_ai_get(pe);
1653
1654 /* Sram store classification lookup ID in AI bits [5:0] */
1655 if ((bits & MVPP2_PRS_FLOW_ID_MASK) == flow)
1656 return pe;
1657 }
1658 kfree(pe);
1659
1660 return NULL;
1661}
1662
1663/* Return first free tcam index, seeking from start to end */
1664static int mvpp2_prs_tcam_first_free(struct mvpp2 *priv, unsigned char start,
1665 unsigned char end)
1666{
1667 int tid;
1668
1669 if (start > end)
1670 swap(start, end);
1671
1672 if (end >= MVPP2_PRS_TCAM_SRAM_SIZE)
1673 end = MVPP2_PRS_TCAM_SRAM_SIZE - 1;
1674
1675 for (tid = start; tid <= end; tid++) {
1676 if (!priv->prs_shadow[tid].valid)
1677 return tid;
1678 }
1679
1680 return -EINVAL;
1681}
1682
1683/* Enable/disable dropping all mac da's */
1684static void mvpp2_prs_mac_drop_all_set(struct mvpp2 *priv, int port, bool add)
1685{
1686 struct mvpp2_prs_entry pe;
1687
1688 if (priv->prs_shadow[MVPP2_PE_DROP_ALL].valid) {
1689 /* Entry exist - update port only */
1690 pe.index = MVPP2_PE_DROP_ALL;
1691 mvpp2_prs_hw_read(priv, &pe);
1692 } else {
1693 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001694 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001695 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1696 pe.index = MVPP2_PE_DROP_ALL;
1697
1698 /* Non-promiscuous mode for all ports - DROP unknown packets */
1699 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
1700 MVPP2_PRS_RI_DROP_MASK);
1701
1702 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1703 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1704
1705 /* Update shadow table */
1706 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1707
1708 /* Mask all ports */
1709 mvpp2_prs_tcam_port_map_set(&pe, 0);
1710 }
1711
1712 /* Update port mask */
1713 mvpp2_prs_tcam_port_set(&pe, port, add);
1714
1715 mvpp2_prs_hw_write(priv, &pe);
1716}
1717
1718/* Set port to promiscuous mode */
1719static void mvpp2_prs_mac_promisc_set(struct mvpp2 *priv, int port, bool add)
1720{
1721 struct mvpp2_prs_entry pe;
1722
Joe Perchesdbedd442015-03-06 20:49:12 -08001723 /* Promiscuous mode - Accept unknown packets */
Marcin Wojtas3f518502014-07-10 16:52:13 -03001724
1725 if (priv->prs_shadow[MVPP2_PE_MAC_PROMISCUOUS].valid) {
1726 /* Entry exist - update port only */
1727 pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1728 mvpp2_prs_hw_read(priv, &pe);
1729 } else {
1730 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001731 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001732 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1733 pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1734
1735 /* Continue - set next lookup */
1736 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
1737
1738 /* Set result info bits */
1739 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_UCAST,
1740 MVPP2_PRS_RI_L2_CAST_MASK);
1741
1742 /* Shift to ethertype */
1743 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
1744 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1745
1746 /* Mask all ports */
1747 mvpp2_prs_tcam_port_map_set(&pe, 0);
1748
1749 /* Update shadow table */
1750 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1751 }
1752
1753 /* Update port mask */
1754 mvpp2_prs_tcam_port_set(&pe, port, add);
1755
1756 mvpp2_prs_hw_write(priv, &pe);
1757}
1758
1759/* Accept multicast */
1760static void mvpp2_prs_mac_multi_set(struct mvpp2 *priv, int port, int index,
1761 bool add)
1762{
1763 struct mvpp2_prs_entry pe;
1764 unsigned char da_mc;
1765
1766 /* Ethernet multicast address first byte is
1767 * 0x01 for IPv4 and 0x33 for IPv6
1768 */
1769 da_mc = (index == MVPP2_PE_MAC_MC_ALL) ? 0x01 : 0x33;
1770
1771 if (priv->prs_shadow[index].valid) {
1772 /* Entry exist - update port only */
1773 pe.index = index;
1774 mvpp2_prs_hw_read(priv, &pe);
1775 } else {
1776 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001777 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001778 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1779 pe.index = index;
1780
1781 /* Continue - set next lookup */
1782 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
1783
1784 /* Set result info bits */
1785 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_MCAST,
1786 MVPP2_PRS_RI_L2_CAST_MASK);
1787
1788 /* Update tcam entry data first byte */
1789 mvpp2_prs_tcam_data_byte_set(&pe, 0, da_mc, 0xff);
1790
1791 /* Shift to ethertype */
1792 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
1793 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1794
1795 /* Mask all ports */
1796 mvpp2_prs_tcam_port_map_set(&pe, 0);
1797
1798 /* Update shadow table */
1799 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1800 }
1801
1802 /* Update port mask */
1803 mvpp2_prs_tcam_port_set(&pe, port, add);
1804
1805 mvpp2_prs_hw_write(priv, &pe);
1806}
1807
1808/* Set entry for dsa packets */
1809static void mvpp2_prs_dsa_tag_set(struct mvpp2 *priv, int port, bool add,
1810 bool tagged, bool extend)
1811{
1812 struct mvpp2_prs_entry pe;
1813 int tid, shift;
1814
1815 if (extend) {
1816 tid = tagged ? MVPP2_PE_EDSA_TAGGED : MVPP2_PE_EDSA_UNTAGGED;
1817 shift = 8;
1818 } else {
1819 tid = tagged ? MVPP2_PE_DSA_TAGGED : MVPP2_PE_DSA_UNTAGGED;
1820 shift = 4;
1821 }
1822
1823 if (priv->prs_shadow[tid].valid) {
1824 /* Entry exist - update port only */
1825 pe.index = tid;
1826 mvpp2_prs_hw_read(priv, &pe);
1827 } else {
1828 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001829 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001830 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
1831 pe.index = tid;
1832
1833 /* Shift 4 bytes if DSA tag or 8 bytes in case of EDSA tag*/
1834 mvpp2_prs_sram_shift_set(&pe, shift,
1835 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1836
1837 /* Update shadow table */
1838 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
1839
1840 if (tagged) {
1841 /* Set tagged bit in DSA tag */
1842 mvpp2_prs_tcam_data_byte_set(&pe, 0,
1843 MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
1844 MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
1845 /* Clear all ai bits for next iteration */
1846 mvpp2_prs_sram_ai_update(&pe, 0,
1847 MVPP2_PRS_SRAM_AI_MASK);
1848 /* If packet is tagged continue check vlans */
1849 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
1850 } else {
1851 /* Set result info bits to 'no vlans' */
1852 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
1853 MVPP2_PRS_RI_VLAN_MASK);
1854 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
1855 }
1856
1857 /* Mask all ports */
1858 mvpp2_prs_tcam_port_map_set(&pe, 0);
1859 }
1860
1861 /* Update port mask */
1862 mvpp2_prs_tcam_port_set(&pe, port, add);
1863
1864 mvpp2_prs_hw_write(priv, &pe);
1865}
1866
1867/* Set entry for dsa ethertype */
1868static void mvpp2_prs_dsa_tag_ethertype_set(struct mvpp2 *priv, int port,
1869 bool add, bool tagged, bool extend)
1870{
1871 struct mvpp2_prs_entry pe;
1872 int tid, shift, port_mask;
1873
1874 if (extend) {
1875 tid = tagged ? MVPP2_PE_ETYPE_EDSA_TAGGED :
1876 MVPP2_PE_ETYPE_EDSA_UNTAGGED;
1877 port_mask = 0;
1878 shift = 8;
1879 } else {
1880 tid = tagged ? MVPP2_PE_ETYPE_DSA_TAGGED :
1881 MVPP2_PE_ETYPE_DSA_UNTAGGED;
1882 port_mask = MVPP2_PRS_PORT_MASK;
1883 shift = 4;
1884 }
1885
1886 if (priv->prs_shadow[tid].valid) {
1887 /* Entry exist - update port only */
1888 pe.index = tid;
1889 mvpp2_prs_hw_read(priv, &pe);
1890 } else {
1891 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001892 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001893 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
1894 pe.index = tid;
1895
1896 /* Set ethertype */
1897 mvpp2_prs_match_etype(&pe, 0, ETH_P_EDSA);
1898 mvpp2_prs_match_etype(&pe, 2, 0);
1899
1900 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DSA_MASK,
1901 MVPP2_PRS_RI_DSA_MASK);
1902 /* Shift ethertype + 2 byte reserved + tag*/
1903 mvpp2_prs_sram_shift_set(&pe, 2 + MVPP2_ETH_TYPE_LEN + shift,
1904 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1905
1906 /* Update shadow table */
1907 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
1908
1909 if (tagged) {
1910 /* Set tagged bit in DSA tag */
1911 mvpp2_prs_tcam_data_byte_set(&pe,
1912 MVPP2_ETH_TYPE_LEN + 2 + 3,
1913 MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
1914 MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
1915 /* Clear all ai bits for next iteration */
1916 mvpp2_prs_sram_ai_update(&pe, 0,
1917 MVPP2_PRS_SRAM_AI_MASK);
1918 /* If packet is tagged continue check vlans */
1919 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
1920 } else {
1921 /* Set result info bits to 'no vlans' */
1922 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
1923 MVPP2_PRS_RI_VLAN_MASK);
1924 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
1925 }
1926 /* Mask/unmask all ports, depending on dsa type */
1927 mvpp2_prs_tcam_port_map_set(&pe, port_mask);
1928 }
1929
1930 /* Update port mask */
1931 mvpp2_prs_tcam_port_set(&pe, port, add);
1932
1933 mvpp2_prs_hw_write(priv, &pe);
1934}
1935
1936/* Search for existing single/triple vlan entry */
1937static struct mvpp2_prs_entry *mvpp2_prs_vlan_find(struct mvpp2 *priv,
1938 unsigned short tpid, int ai)
1939{
1940 struct mvpp2_prs_entry *pe;
1941 int tid;
1942
1943 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
1944 if (!pe)
1945 return NULL;
1946 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
1947
1948 /* Go through the all entries with MVPP2_PRS_LU_VLAN */
1949 for (tid = MVPP2_PE_FIRST_FREE_TID;
1950 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
1951 unsigned int ri_bits, ai_bits;
1952 bool match;
1953
1954 if (!priv->prs_shadow[tid].valid ||
1955 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
1956 continue;
1957
1958 pe->index = tid;
1959
1960 mvpp2_prs_hw_read(priv, pe);
1961 match = mvpp2_prs_tcam_data_cmp(pe, 0, swab16(tpid));
1962 if (!match)
1963 continue;
1964
1965 /* Get vlan type */
1966 ri_bits = mvpp2_prs_sram_ri_get(pe);
1967 ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
1968
1969 /* Get current ai value from tcam */
1970 ai_bits = mvpp2_prs_tcam_ai_get(pe);
1971 /* Clear double vlan bit */
1972 ai_bits &= ~MVPP2_PRS_DBL_VLAN_AI_BIT;
1973
1974 if (ai != ai_bits)
1975 continue;
1976
1977 if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
1978 ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
1979 return pe;
1980 }
1981 kfree(pe);
1982
1983 return NULL;
1984}
1985
1986/* Add/update single/triple vlan entry */
1987static int mvpp2_prs_vlan_add(struct mvpp2 *priv, unsigned short tpid, int ai,
1988 unsigned int port_map)
1989{
1990 struct mvpp2_prs_entry *pe;
1991 int tid_aux, tid;
Sudip Mukherjee43737472014-11-01 16:59:34 +05301992 int ret = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001993
1994 pe = mvpp2_prs_vlan_find(priv, tpid, ai);
1995
1996 if (!pe) {
1997 /* Create new tcam entry */
1998 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_LAST_FREE_TID,
1999 MVPP2_PE_FIRST_FREE_TID);
2000 if (tid < 0)
2001 return tid;
2002
2003 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2004 if (!pe)
2005 return -ENOMEM;
2006
2007 /* Get last double vlan tid */
2008 for (tid_aux = MVPP2_PE_LAST_FREE_TID;
2009 tid_aux >= MVPP2_PE_FIRST_FREE_TID; tid_aux--) {
2010 unsigned int ri_bits;
2011
2012 if (!priv->prs_shadow[tid_aux].valid ||
2013 priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
2014 continue;
2015
2016 pe->index = tid_aux;
2017 mvpp2_prs_hw_read(priv, pe);
2018 ri_bits = mvpp2_prs_sram_ri_get(pe);
2019 if ((ri_bits & MVPP2_PRS_RI_VLAN_MASK) ==
2020 MVPP2_PRS_RI_VLAN_DOUBLE)
2021 break;
2022 }
2023
Sudip Mukherjee43737472014-11-01 16:59:34 +05302024 if (tid <= tid_aux) {
2025 ret = -EINVAL;
Markus Elfringf9fd0e32017-04-17 13:50:35 +02002026 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302027 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002028
Markus Elfringbd6aaf52017-04-17 10:40:32 +02002029 memset(pe, 0, sizeof(*pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002030 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2031 pe->index = tid;
2032
2033 mvpp2_prs_match_etype(pe, 0, tpid);
2034
2035 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_L2);
2036 /* Shift 4 bytes - skip 1 vlan tag */
2037 mvpp2_prs_sram_shift_set(pe, MVPP2_VLAN_TAG_LEN,
2038 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2039 /* Clear all ai bits for next iteration */
2040 mvpp2_prs_sram_ai_update(pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2041
2042 if (ai == MVPP2_PRS_SINGLE_VLAN_AI) {
2043 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_SINGLE,
2044 MVPP2_PRS_RI_VLAN_MASK);
2045 } else {
2046 ai |= MVPP2_PRS_DBL_VLAN_AI_BIT;
2047 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_TRIPLE,
2048 MVPP2_PRS_RI_VLAN_MASK);
2049 }
2050 mvpp2_prs_tcam_ai_update(pe, ai, MVPP2_PRS_SRAM_AI_MASK);
2051
2052 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_VLAN);
2053 }
2054 /* Update ports' mask */
2055 mvpp2_prs_tcam_port_map_set(pe, port_map);
2056
2057 mvpp2_prs_hw_write(priv, pe);
Markus Elfringf9fd0e32017-04-17 13:50:35 +02002058free_pe:
Marcin Wojtas3f518502014-07-10 16:52:13 -03002059 kfree(pe);
2060
Sudip Mukherjee43737472014-11-01 16:59:34 +05302061 return ret;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002062}
2063
2064/* Get first free double vlan ai number */
2065static int mvpp2_prs_double_vlan_ai_free_get(struct mvpp2 *priv)
2066{
2067 int i;
2068
2069 for (i = 1; i < MVPP2_PRS_DBL_VLANS_MAX; i++) {
2070 if (!priv->prs_double_vlans[i])
2071 return i;
2072 }
2073
2074 return -EINVAL;
2075}
2076
2077/* Search for existing double vlan entry */
2078static struct mvpp2_prs_entry *mvpp2_prs_double_vlan_find(struct mvpp2 *priv,
2079 unsigned short tpid1,
2080 unsigned short tpid2)
2081{
2082 struct mvpp2_prs_entry *pe;
2083 int tid;
2084
2085 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2086 if (!pe)
2087 return NULL;
2088 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2089
2090 /* Go through the all entries with MVPP2_PRS_LU_VLAN */
2091 for (tid = MVPP2_PE_FIRST_FREE_TID;
2092 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
2093 unsigned int ri_mask;
2094 bool match;
2095
2096 if (!priv->prs_shadow[tid].valid ||
2097 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
2098 continue;
2099
2100 pe->index = tid;
2101 mvpp2_prs_hw_read(priv, pe);
2102
2103 match = mvpp2_prs_tcam_data_cmp(pe, 0, swab16(tpid1))
2104 && mvpp2_prs_tcam_data_cmp(pe, 4, swab16(tpid2));
2105
2106 if (!match)
2107 continue;
2108
2109 ri_mask = mvpp2_prs_sram_ri_get(pe) & MVPP2_PRS_RI_VLAN_MASK;
2110 if (ri_mask == MVPP2_PRS_RI_VLAN_DOUBLE)
2111 return pe;
2112 }
2113 kfree(pe);
2114
2115 return NULL;
2116}
2117
2118/* Add or update double vlan entry */
2119static int mvpp2_prs_double_vlan_add(struct mvpp2 *priv, unsigned short tpid1,
2120 unsigned short tpid2,
2121 unsigned int port_map)
2122{
2123 struct mvpp2_prs_entry *pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302124 int tid_aux, tid, ai, ret = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002125
2126 pe = mvpp2_prs_double_vlan_find(priv, tpid1, tpid2);
2127
2128 if (!pe) {
2129 /* Create new tcam entry */
2130 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2131 MVPP2_PE_LAST_FREE_TID);
2132 if (tid < 0)
2133 return tid;
2134
2135 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2136 if (!pe)
2137 return -ENOMEM;
2138
2139 /* Set ai value for new double vlan entry */
2140 ai = mvpp2_prs_double_vlan_ai_free_get(priv);
Sudip Mukherjee43737472014-11-01 16:59:34 +05302141 if (ai < 0) {
2142 ret = ai;
Markus Elfringc9a7e122017-04-17 13:03:49 +02002143 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302144 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002145
2146 /* Get first single/triple vlan tid */
2147 for (tid_aux = MVPP2_PE_FIRST_FREE_TID;
2148 tid_aux <= MVPP2_PE_LAST_FREE_TID; tid_aux++) {
2149 unsigned int ri_bits;
2150
2151 if (!priv->prs_shadow[tid_aux].valid ||
2152 priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
2153 continue;
2154
2155 pe->index = tid_aux;
2156 mvpp2_prs_hw_read(priv, pe);
2157 ri_bits = mvpp2_prs_sram_ri_get(pe);
2158 ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
2159 if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
2160 ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
2161 break;
2162 }
2163
Sudip Mukherjee43737472014-11-01 16:59:34 +05302164 if (tid >= tid_aux) {
2165 ret = -ERANGE;
Markus Elfringc9a7e122017-04-17 13:03:49 +02002166 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302167 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002168
Markus Elfringbd6aaf52017-04-17 10:40:32 +02002169 memset(pe, 0, sizeof(*pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002170 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2171 pe->index = tid;
2172
2173 priv->prs_double_vlans[ai] = true;
2174
2175 mvpp2_prs_match_etype(pe, 0, tpid1);
2176 mvpp2_prs_match_etype(pe, 4, tpid2);
2177
2178 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_VLAN);
2179 /* Shift 8 bytes - skip 2 vlan tags */
2180 mvpp2_prs_sram_shift_set(pe, 2 * MVPP2_VLAN_TAG_LEN,
2181 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2182 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_DOUBLE,
2183 MVPP2_PRS_RI_VLAN_MASK);
2184 mvpp2_prs_sram_ai_update(pe, ai | MVPP2_PRS_DBL_VLAN_AI_BIT,
2185 MVPP2_PRS_SRAM_AI_MASK);
2186
2187 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_VLAN);
2188 }
2189
2190 /* Update ports' mask */
2191 mvpp2_prs_tcam_port_map_set(pe, port_map);
2192 mvpp2_prs_hw_write(priv, pe);
Markus Elfringc9a7e122017-04-17 13:03:49 +02002193free_pe:
Marcin Wojtas3f518502014-07-10 16:52:13 -03002194 kfree(pe);
Sudip Mukherjee43737472014-11-01 16:59:34 +05302195 return ret;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002196}
2197
2198/* IPv4 header parsing for fragmentation and L4 offset */
2199static int mvpp2_prs_ip4_proto(struct mvpp2 *priv, unsigned short proto,
2200 unsigned int ri, unsigned int ri_mask)
2201{
2202 struct mvpp2_prs_entry pe;
2203 int tid;
2204
2205 if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
2206 (proto != IPPROTO_IGMP))
2207 return -EINVAL;
2208
2209 /* Fragmented packet */
2210 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2211 MVPP2_PE_LAST_FREE_TID);
2212 if (tid < 0)
2213 return tid;
2214
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002215 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002216 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
2217 pe.index = tid;
2218
2219 /* Set next lu to IPv4 */
2220 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2221 mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2222 /* Set L4 offset */
2223 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
2224 sizeof(struct iphdr) - 4,
2225 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2226 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
2227 MVPP2_PRS_IPV4_DIP_AI_BIT);
2228 mvpp2_prs_sram_ri_update(&pe, ri | MVPP2_PRS_RI_IP_FRAG_MASK,
2229 ri_mask | MVPP2_PRS_RI_IP_FRAG_MASK);
2230
2231 mvpp2_prs_tcam_data_byte_set(&pe, 5, proto, MVPP2_PRS_TCAM_PROTO_MASK);
2232 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
2233 /* Unmask all ports */
2234 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2235
2236 /* Update shadow table and hw entry */
2237 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2238 mvpp2_prs_hw_write(priv, &pe);
2239
2240 /* Not fragmented packet */
2241 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2242 MVPP2_PE_LAST_FREE_TID);
2243 if (tid < 0)
2244 return tid;
2245
2246 pe.index = tid;
2247 /* Clear ri before updating */
2248 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2249 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2250 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
2251
2252 mvpp2_prs_tcam_data_byte_set(&pe, 2, 0x00, MVPP2_PRS_TCAM_PROTO_MASK_L);
2253 mvpp2_prs_tcam_data_byte_set(&pe, 3, 0x00, MVPP2_PRS_TCAM_PROTO_MASK);
2254
2255 /* Update shadow table and hw entry */
2256 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2257 mvpp2_prs_hw_write(priv, &pe);
2258
2259 return 0;
2260}
2261
2262/* IPv4 L3 multicast or broadcast */
2263static int mvpp2_prs_ip4_cast(struct mvpp2 *priv, unsigned short l3_cast)
2264{
2265 struct mvpp2_prs_entry pe;
2266 int mask, tid;
2267
2268 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2269 MVPP2_PE_LAST_FREE_TID);
2270 if (tid < 0)
2271 return tid;
2272
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002273 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002274 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
2275 pe.index = tid;
2276
2277 switch (l3_cast) {
2278 case MVPP2_PRS_L3_MULTI_CAST:
2279 mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV4_MC,
2280 MVPP2_PRS_IPV4_MC_MASK);
2281 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
2282 MVPP2_PRS_RI_L3_ADDR_MASK);
2283 break;
2284 case MVPP2_PRS_L3_BROAD_CAST:
2285 mask = MVPP2_PRS_IPV4_BC_MASK;
2286 mvpp2_prs_tcam_data_byte_set(&pe, 0, mask, mask);
2287 mvpp2_prs_tcam_data_byte_set(&pe, 1, mask, mask);
2288 mvpp2_prs_tcam_data_byte_set(&pe, 2, mask, mask);
2289 mvpp2_prs_tcam_data_byte_set(&pe, 3, mask, mask);
2290 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_BCAST,
2291 MVPP2_PRS_RI_L3_ADDR_MASK);
2292 break;
2293 default:
2294 return -EINVAL;
2295 }
2296
2297 /* Finished: go to flowid generation */
2298 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2299 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2300
2301 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
2302 MVPP2_PRS_IPV4_DIP_AI_BIT);
2303 /* Unmask all ports */
2304 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2305
2306 /* Update shadow table and hw entry */
2307 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2308 mvpp2_prs_hw_write(priv, &pe);
2309
2310 return 0;
2311}
2312
2313/* Set entries for protocols over IPv6 */
2314static int mvpp2_prs_ip6_proto(struct mvpp2 *priv, unsigned short proto,
2315 unsigned int ri, unsigned int ri_mask)
2316{
2317 struct mvpp2_prs_entry pe;
2318 int tid;
2319
2320 if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
2321 (proto != IPPROTO_ICMPV6) && (proto != IPPROTO_IPIP))
2322 return -EINVAL;
2323
2324 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2325 MVPP2_PE_LAST_FREE_TID);
2326 if (tid < 0)
2327 return tid;
2328
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002329 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002330 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2331 pe.index = tid;
2332
2333 /* Finished: go to flowid generation */
2334 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2335 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2336 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
2337 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
2338 sizeof(struct ipv6hdr) - 6,
2339 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2340
2341 mvpp2_prs_tcam_data_byte_set(&pe, 0, proto, MVPP2_PRS_TCAM_PROTO_MASK);
2342 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
2343 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2344 /* Unmask all ports */
2345 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2346
2347 /* Write HW */
2348 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
2349 mvpp2_prs_hw_write(priv, &pe);
2350
2351 return 0;
2352}
2353
2354/* IPv6 L3 multicast entry */
2355static int mvpp2_prs_ip6_cast(struct mvpp2 *priv, unsigned short l3_cast)
2356{
2357 struct mvpp2_prs_entry pe;
2358 int tid;
2359
2360 if (l3_cast != MVPP2_PRS_L3_MULTI_CAST)
2361 return -EINVAL;
2362
2363 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2364 MVPP2_PE_LAST_FREE_TID);
2365 if (tid < 0)
2366 return tid;
2367
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002368 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002369 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2370 pe.index = tid;
2371
2372 /* Finished: go to flowid generation */
2373 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2374 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
2375 MVPP2_PRS_RI_L3_ADDR_MASK);
2376 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
2377 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2378 /* Shift back to IPv6 NH */
2379 mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2380
2381 mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV6_MC,
2382 MVPP2_PRS_IPV6_MC_MASK);
2383 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2384 /* Unmask all ports */
2385 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2386
2387 /* Update shadow table and hw entry */
2388 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
2389 mvpp2_prs_hw_write(priv, &pe);
2390
2391 return 0;
2392}
2393
2394/* Parser per-port initialization */
2395static void mvpp2_prs_hw_port_init(struct mvpp2 *priv, int port, int lu_first,
2396 int lu_max, int offset)
2397{
2398 u32 val;
2399
2400 /* Set lookup ID */
2401 val = mvpp2_read(priv, MVPP2_PRS_INIT_LOOKUP_REG);
2402 val &= ~MVPP2_PRS_PORT_LU_MASK(port);
2403 val |= MVPP2_PRS_PORT_LU_VAL(port, lu_first);
2404 mvpp2_write(priv, MVPP2_PRS_INIT_LOOKUP_REG, val);
2405
2406 /* Set maximum number of loops for packet received from port */
2407 val = mvpp2_read(priv, MVPP2_PRS_MAX_LOOP_REG(port));
2408 val &= ~MVPP2_PRS_MAX_LOOP_MASK(port);
2409 val |= MVPP2_PRS_MAX_LOOP_VAL(port, lu_max);
2410 mvpp2_write(priv, MVPP2_PRS_MAX_LOOP_REG(port), val);
2411
2412 /* Set initial offset for packet header extraction for the first
2413 * searching loop
2414 */
2415 val = mvpp2_read(priv, MVPP2_PRS_INIT_OFFS_REG(port));
2416 val &= ~MVPP2_PRS_INIT_OFF_MASK(port);
2417 val |= MVPP2_PRS_INIT_OFF_VAL(port, offset);
2418 mvpp2_write(priv, MVPP2_PRS_INIT_OFFS_REG(port), val);
2419}
2420
2421/* Default flow entries initialization for all ports */
2422static void mvpp2_prs_def_flow_init(struct mvpp2 *priv)
2423{
2424 struct mvpp2_prs_entry pe;
2425 int port;
2426
2427 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002428 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002429 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2430 pe.index = MVPP2_PE_FIRST_DEFAULT_FLOW - port;
2431
2432 /* Mask all ports */
2433 mvpp2_prs_tcam_port_map_set(&pe, 0);
2434
2435 /* Set flow ID*/
2436 mvpp2_prs_sram_ai_update(&pe, port, MVPP2_PRS_FLOW_ID_MASK);
2437 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
2438
2439 /* Update shadow table and hw entry */
2440 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_FLOWS);
2441 mvpp2_prs_hw_write(priv, &pe);
2442 }
2443}
2444
2445/* Set default entry for Marvell Header field */
2446static void mvpp2_prs_mh_init(struct mvpp2 *priv)
2447{
2448 struct mvpp2_prs_entry pe;
2449
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002450 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002451
2452 pe.index = MVPP2_PE_MH_DEFAULT;
2453 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MH);
2454 mvpp2_prs_sram_shift_set(&pe, MVPP2_MH_SIZE,
2455 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2456 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_MAC);
2457
2458 /* Unmask all ports */
2459 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2460
2461 /* Update shadow table and hw entry */
2462 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MH);
2463 mvpp2_prs_hw_write(priv, &pe);
2464}
2465
2466/* Set default entires (place holder) for promiscuous, non-promiscuous and
2467 * multicast MAC addresses
2468 */
2469static void mvpp2_prs_mac_init(struct mvpp2 *priv)
2470{
2471 struct mvpp2_prs_entry pe;
2472
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002473 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002474
2475 /* Non-promiscuous mode for all ports - DROP unknown packets */
2476 pe.index = MVPP2_PE_MAC_NON_PROMISCUOUS;
2477 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
2478
2479 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
2480 MVPP2_PRS_RI_DROP_MASK);
2481 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2482 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2483
2484 /* Unmask all ports */
2485 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2486
2487 /* Update shadow table and hw entry */
2488 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2489 mvpp2_prs_hw_write(priv, &pe);
2490
2491 /* place holders only - no ports */
2492 mvpp2_prs_mac_drop_all_set(priv, 0, false);
2493 mvpp2_prs_mac_promisc_set(priv, 0, false);
2494 mvpp2_prs_mac_multi_set(priv, MVPP2_PE_MAC_MC_ALL, 0, false);
2495 mvpp2_prs_mac_multi_set(priv, MVPP2_PE_MAC_MC_IP6, 0, false);
2496}
2497
2498/* Set default entries for various types of dsa packets */
2499static void mvpp2_prs_dsa_init(struct mvpp2 *priv)
2500{
2501 struct mvpp2_prs_entry pe;
2502
2503 /* None tagged EDSA entry - place holder */
2504 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
2505 MVPP2_PRS_EDSA);
2506
2507 /* Tagged EDSA entry - place holder */
2508 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
2509
2510 /* None tagged DSA entry - place holder */
2511 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
2512 MVPP2_PRS_DSA);
2513
2514 /* Tagged DSA entry - place holder */
2515 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
2516
2517 /* None tagged EDSA ethertype entry - place holder*/
2518 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
2519 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
2520
2521 /* Tagged EDSA ethertype entry - place holder*/
2522 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
2523 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
2524
2525 /* None tagged DSA ethertype entry */
2526 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
2527 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
2528
2529 /* Tagged DSA ethertype entry */
2530 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
2531 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
2532
2533 /* Set default entry, in case DSA or EDSA tag not found */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002534 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002535 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
2536 pe.index = MVPP2_PE_DSA_DEFAULT;
2537 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2538
2539 /* Shift 0 bytes */
2540 mvpp2_prs_sram_shift_set(&pe, 0, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2541 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2542
2543 /* Clear all sram ai bits for next iteration */
2544 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2545
2546 /* Unmask all ports */
2547 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2548
2549 mvpp2_prs_hw_write(priv, &pe);
2550}
2551
2552/* Match basic ethertypes */
2553static int mvpp2_prs_etype_init(struct mvpp2 *priv)
2554{
2555 struct mvpp2_prs_entry pe;
2556 int tid;
2557
2558 /* Ethertype: PPPoE */
2559 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2560 MVPP2_PE_LAST_FREE_TID);
2561 if (tid < 0)
2562 return tid;
2563
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002564 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002565 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2566 pe.index = tid;
2567
2568 mvpp2_prs_match_etype(&pe, 0, ETH_P_PPP_SES);
2569
2570 mvpp2_prs_sram_shift_set(&pe, MVPP2_PPPOE_HDR_SIZE,
2571 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2572 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2573 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_PPPOE_MASK,
2574 MVPP2_PRS_RI_PPPOE_MASK);
2575
2576 /* Update shadow table and hw entry */
2577 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2578 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2579 priv->prs_shadow[pe.index].finish = false;
2580 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_PPPOE_MASK,
2581 MVPP2_PRS_RI_PPPOE_MASK);
2582 mvpp2_prs_hw_write(priv, &pe);
2583
2584 /* Ethertype: ARP */
2585 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2586 MVPP2_PE_LAST_FREE_TID);
2587 if (tid < 0)
2588 return tid;
2589
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002590 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002591 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2592 pe.index = tid;
2593
2594 mvpp2_prs_match_etype(&pe, 0, ETH_P_ARP);
2595
2596 /* Generate flow in the next iteration*/
2597 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2598 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2599 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_ARP,
2600 MVPP2_PRS_RI_L3_PROTO_MASK);
2601 /* Set L3 offset */
2602 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2603 MVPP2_ETH_TYPE_LEN,
2604 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2605
2606 /* Update shadow table and hw entry */
2607 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2608 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2609 priv->prs_shadow[pe.index].finish = true;
2610 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_ARP,
2611 MVPP2_PRS_RI_L3_PROTO_MASK);
2612 mvpp2_prs_hw_write(priv, &pe);
2613
2614 /* Ethertype: LBTD */
2615 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2616 MVPP2_PE_LAST_FREE_TID);
2617 if (tid < 0)
2618 return tid;
2619
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002620 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002621 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2622 pe.index = tid;
2623
2624 mvpp2_prs_match_etype(&pe, 0, MVPP2_IP_LBDT_TYPE);
2625
2626 /* Generate flow in the next iteration*/
2627 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2628 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2629 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2630 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2631 MVPP2_PRS_RI_CPU_CODE_MASK |
2632 MVPP2_PRS_RI_UDF3_MASK);
2633 /* Set L3 offset */
2634 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2635 MVPP2_ETH_TYPE_LEN,
2636 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2637
2638 /* Update shadow table and hw entry */
2639 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2640 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2641 priv->prs_shadow[pe.index].finish = true;
2642 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2643 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2644 MVPP2_PRS_RI_CPU_CODE_MASK |
2645 MVPP2_PRS_RI_UDF3_MASK);
2646 mvpp2_prs_hw_write(priv, &pe);
2647
2648 /* Ethertype: IPv4 without options */
2649 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2650 MVPP2_PE_LAST_FREE_TID);
2651 if (tid < 0)
2652 return tid;
2653
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002654 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002655 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2656 pe.index = tid;
2657
2658 mvpp2_prs_match_etype(&pe, 0, ETH_P_IP);
2659 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2660 MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
2661 MVPP2_PRS_IPV4_HEAD_MASK |
2662 MVPP2_PRS_IPV4_IHL_MASK);
2663
2664 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2665 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
2666 MVPP2_PRS_RI_L3_PROTO_MASK);
2667 /* Skip eth_type + 4 bytes of IP header */
2668 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
2669 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2670 /* Set L3 offset */
2671 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2672 MVPP2_ETH_TYPE_LEN,
2673 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2674
2675 /* Update shadow table and hw entry */
2676 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2677 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2678 priv->prs_shadow[pe.index].finish = false;
2679 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4,
2680 MVPP2_PRS_RI_L3_PROTO_MASK);
2681 mvpp2_prs_hw_write(priv, &pe);
2682
2683 /* Ethertype: IPv4 with options */
2684 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2685 MVPP2_PE_LAST_FREE_TID);
2686 if (tid < 0)
2687 return tid;
2688
2689 pe.index = tid;
2690
2691 /* Clear tcam data before updating */
2692 pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(MVPP2_ETH_TYPE_LEN)] = 0x0;
2693 pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(MVPP2_ETH_TYPE_LEN)] = 0x0;
2694
2695 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2696 MVPP2_PRS_IPV4_HEAD,
2697 MVPP2_PRS_IPV4_HEAD_MASK);
2698
2699 /* Clear ri before updating */
2700 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2701 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2702 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
2703 MVPP2_PRS_RI_L3_PROTO_MASK);
2704
2705 /* Update shadow table and hw entry */
2706 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2707 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2708 priv->prs_shadow[pe.index].finish = false;
2709 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4_OPT,
2710 MVPP2_PRS_RI_L3_PROTO_MASK);
2711 mvpp2_prs_hw_write(priv, &pe);
2712
2713 /* Ethertype: IPv6 without options */
2714 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2715 MVPP2_PE_LAST_FREE_TID);
2716 if (tid < 0)
2717 return tid;
2718
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002719 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002720 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2721 pe.index = tid;
2722
2723 mvpp2_prs_match_etype(&pe, 0, ETH_P_IPV6);
2724
2725 /* Skip DIP of IPV6 header */
2726 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 8 +
2727 MVPP2_MAX_L3_ADDR_SIZE,
2728 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2729 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2730 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
2731 MVPP2_PRS_RI_L3_PROTO_MASK);
2732 /* Set L3 offset */
2733 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2734 MVPP2_ETH_TYPE_LEN,
2735 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2736
2737 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2738 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2739 priv->prs_shadow[pe.index].finish = false;
2740 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP6,
2741 MVPP2_PRS_RI_L3_PROTO_MASK);
2742 mvpp2_prs_hw_write(priv, &pe);
2743
2744 /* Default entry for MVPP2_PRS_LU_L2 - Unknown ethtype */
2745 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2746 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2747 pe.index = MVPP2_PE_ETH_TYPE_UN;
2748
2749 /* Unmask all ports */
2750 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2751
2752 /* Generate flow in the next iteration*/
2753 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2754 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2755 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
2756 MVPP2_PRS_RI_L3_PROTO_MASK);
2757 /* Set L3 offset even it's unknown L3 */
2758 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2759 MVPP2_ETH_TYPE_LEN,
2760 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2761
2762 /* Update shadow table and hw entry */
2763 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2764 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2765 priv->prs_shadow[pe.index].finish = true;
2766 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_UN,
2767 MVPP2_PRS_RI_L3_PROTO_MASK);
2768 mvpp2_prs_hw_write(priv, &pe);
2769
2770 return 0;
2771}
2772
2773/* Configure vlan entries and detect up to 2 successive VLAN tags.
2774 * Possible options:
2775 * 0x8100, 0x88A8
2776 * 0x8100, 0x8100
2777 * 0x8100
2778 * 0x88A8
2779 */
2780static int mvpp2_prs_vlan_init(struct platform_device *pdev, struct mvpp2 *priv)
2781{
2782 struct mvpp2_prs_entry pe;
2783 int err;
2784
2785 priv->prs_double_vlans = devm_kcalloc(&pdev->dev, sizeof(bool),
2786 MVPP2_PRS_DBL_VLANS_MAX,
2787 GFP_KERNEL);
2788 if (!priv->prs_double_vlans)
2789 return -ENOMEM;
2790
2791 /* Double VLAN: 0x8100, 0x88A8 */
2792 err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021AD,
2793 MVPP2_PRS_PORT_MASK);
2794 if (err)
2795 return err;
2796
2797 /* Double VLAN: 0x8100, 0x8100 */
2798 err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021Q,
2799 MVPP2_PRS_PORT_MASK);
2800 if (err)
2801 return err;
2802
2803 /* Single VLAN: 0x88a8 */
2804 err = mvpp2_prs_vlan_add(priv, ETH_P_8021AD, MVPP2_PRS_SINGLE_VLAN_AI,
2805 MVPP2_PRS_PORT_MASK);
2806 if (err)
2807 return err;
2808
2809 /* Single VLAN: 0x8100 */
2810 err = mvpp2_prs_vlan_add(priv, ETH_P_8021Q, MVPP2_PRS_SINGLE_VLAN_AI,
2811 MVPP2_PRS_PORT_MASK);
2812 if (err)
2813 return err;
2814
2815 /* Set default double vlan entry */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002816 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002817 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2818 pe.index = MVPP2_PE_VLAN_DBL;
2819
2820 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2821 /* Clear ai for next iterations */
2822 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2823 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_DOUBLE,
2824 MVPP2_PRS_RI_VLAN_MASK);
2825
2826 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_DBL_VLAN_AI_BIT,
2827 MVPP2_PRS_DBL_VLAN_AI_BIT);
2828 /* Unmask all ports */
2829 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2830
2831 /* Update shadow table and hw entry */
2832 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
2833 mvpp2_prs_hw_write(priv, &pe);
2834
2835 /* Set default vlan none entry */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002836 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002837 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2838 pe.index = MVPP2_PE_VLAN_NONE;
2839
2840 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2841 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
2842 MVPP2_PRS_RI_VLAN_MASK);
2843
2844 /* Unmask all ports */
2845 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2846
2847 /* Update shadow table and hw entry */
2848 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
2849 mvpp2_prs_hw_write(priv, &pe);
2850
2851 return 0;
2852}
2853
2854/* Set entries for PPPoE ethertype */
2855static int mvpp2_prs_pppoe_init(struct mvpp2 *priv)
2856{
2857 struct mvpp2_prs_entry pe;
2858 int tid;
2859
2860 /* IPv4 over PPPoE with options */
2861 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2862 MVPP2_PE_LAST_FREE_TID);
2863 if (tid < 0)
2864 return tid;
2865
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002866 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002867 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2868 pe.index = tid;
2869
2870 mvpp2_prs_match_etype(&pe, 0, PPP_IP);
2871
2872 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2873 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
2874 MVPP2_PRS_RI_L3_PROTO_MASK);
2875 /* Skip eth_type + 4 bytes of IP header */
2876 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
2877 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2878 /* Set L3 offset */
2879 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2880 MVPP2_ETH_TYPE_LEN,
2881 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2882
2883 /* Update shadow table and hw entry */
2884 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
2885 mvpp2_prs_hw_write(priv, &pe);
2886
2887 /* IPv4 over PPPoE without options */
2888 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2889 MVPP2_PE_LAST_FREE_TID);
2890 if (tid < 0)
2891 return tid;
2892
2893 pe.index = tid;
2894
2895 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2896 MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
2897 MVPP2_PRS_IPV4_HEAD_MASK |
2898 MVPP2_PRS_IPV4_IHL_MASK);
2899
2900 /* Clear ri before updating */
2901 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2902 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2903 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
2904 MVPP2_PRS_RI_L3_PROTO_MASK);
2905
2906 /* Update shadow table and hw entry */
2907 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
2908 mvpp2_prs_hw_write(priv, &pe);
2909
2910 /* IPv6 over PPPoE */
2911 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2912 MVPP2_PE_LAST_FREE_TID);
2913 if (tid < 0)
2914 return tid;
2915
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002916 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002917 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2918 pe.index = tid;
2919
2920 mvpp2_prs_match_etype(&pe, 0, PPP_IPV6);
2921
2922 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2923 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
2924 MVPP2_PRS_RI_L3_PROTO_MASK);
2925 /* Skip eth_type + 4 bytes of IPv6 header */
2926 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
2927 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2928 /* Set L3 offset */
2929 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2930 MVPP2_ETH_TYPE_LEN,
2931 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2932
2933 /* Update shadow table and hw entry */
2934 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
2935 mvpp2_prs_hw_write(priv, &pe);
2936
2937 /* Non-IP over PPPoE */
2938 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2939 MVPP2_PE_LAST_FREE_TID);
2940 if (tid < 0)
2941 return tid;
2942
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002943 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002944 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2945 pe.index = tid;
2946
2947 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
2948 MVPP2_PRS_RI_L3_PROTO_MASK);
2949
2950 /* Finished: go to flowid generation */
2951 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2952 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2953 /* Set L3 offset even if it's unknown L3 */
2954 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2955 MVPP2_ETH_TYPE_LEN,
2956 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2957
2958 /* Update shadow table and hw entry */
2959 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
2960 mvpp2_prs_hw_write(priv, &pe);
2961
2962 return 0;
2963}
2964
2965/* Initialize entries for IPv4 */
2966static int mvpp2_prs_ip4_init(struct mvpp2 *priv)
2967{
2968 struct mvpp2_prs_entry pe;
2969 int err;
2970
2971 /* Set entries for TCP, UDP and IGMP over IPv4 */
2972 err = mvpp2_prs_ip4_proto(priv, IPPROTO_TCP, MVPP2_PRS_RI_L4_TCP,
2973 MVPP2_PRS_RI_L4_PROTO_MASK);
2974 if (err)
2975 return err;
2976
2977 err = mvpp2_prs_ip4_proto(priv, IPPROTO_UDP, MVPP2_PRS_RI_L4_UDP,
2978 MVPP2_PRS_RI_L4_PROTO_MASK);
2979 if (err)
2980 return err;
2981
2982 err = mvpp2_prs_ip4_proto(priv, IPPROTO_IGMP,
2983 MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2984 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2985 MVPP2_PRS_RI_CPU_CODE_MASK |
2986 MVPP2_PRS_RI_UDF3_MASK);
2987 if (err)
2988 return err;
2989
2990 /* IPv4 Broadcast */
2991 err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_BROAD_CAST);
2992 if (err)
2993 return err;
2994
2995 /* IPv4 Multicast */
2996 err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
2997 if (err)
2998 return err;
2999
3000 /* Default IPv4 entry for unknown protocols */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003001 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003002 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
3003 pe.index = MVPP2_PE_IP4_PROTO_UN;
3004
3005 /* Set next lu to IPv4 */
3006 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
3007 mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3008 /* Set L4 offset */
3009 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
3010 sizeof(struct iphdr) - 4,
3011 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3012 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
3013 MVPP2_PRS_IPV4_DIP_AI_BIT);
3014 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3015 MVPP2_PRS_RI_L4_PROTO_MASK);
3016
3017 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
3018 /* Unmask all ports */
3019 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3020
3021 /* Update shadow table and hw entry */
3022 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3023 mvpp2_prs_hw_write(priv, &pe);
3024
3025 /* Default IPv4 entry for unicast address */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003026 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003027 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
3028 pe.index = MVPP2_PE_IP4_ADDR_UN;
3029
3030 /* Finished: go to flowid generation */
3031 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3032 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3033 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
3034 MVPP2_PRS_RI_L3_ADDR_MASK);
3035
3036 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
3037 MVPP2_PRS_IPV4_DIP_AI_BIT);
3038 /* Unmask all ports */
3039 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3040
3041 /* Update shadow table and hw entry */
3042 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3043 mvpp2_prs_hw_write(priv, &pe);
3044
3045 return 0;
3046}
3047
3048/* Initialize entries for IPv6 */
3049static int mvpp2_prs_ip6_init(struct mvpp2 *priv)
3050{
3051 struct mvpp2_prs_entry pe;
3052 int tid, err;
3053
3054 /* Set entries for TCP, UDP and ICMP over IPv6 */
3055 err = mvpp2_prs_ip6_proto(priv, IPPROTO_TCP,
3056 MVPP2_PRS_RI_L4_TCP,
3057 MVPP2_PRS_RI_L4_PROTO_MASK);
3058 if (err)
3059 return err;
3060
3061 err = mvpp2_prs_ip6_proto(priv, IPPROTO_UDP,
3062 MVPP2_PRS_RI_L4_UDP,
3063 MVPP2_PRS_RI_L4_PROTO_MASK);
3064 if (err)
3065 return err;
3066
3067 err = mvpp2_prs_ip6_proto(priv, IPPROTO_ICMPV6,
3068 MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
3069 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
3070 MVPP2_PRS_RI_CPU_CODE_MASK |
3071 MVPP2_PRS_RI_UDF3_MASK);
3072 if (err)
3073 return err;
3074
3075 /* IPv4 is the last header. This is similar case as 6-TCP or 17-UDP */
3076 /* Result Info: UDF7=1, DS lite */
3077 err = mvpp2_prs_ip6_proto(priv, IPPROTO_IPIP,
3078 MVPP2_PRS_RI_UDF7_IP6_LITE,
3079 MVPP2_PRS_RI_UDF7_MASK);
3080 if (err)
3081 return err;
3082
3083 /* IPv6 multicast */
3084 err = mvpp2_prs_ip6_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
3085 if (err)
3086 return err;
3087
3088 /* Entry for checking hop limit */
3089 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3090 MVPP2_PE_LAST_FREE_TID);
3091 if (tid < 0)
3092 return tid;
3093
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003094 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003095 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3096 pe.index = tid;
3097
3098 /* Finished: go to flowid generation */
3099 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3100 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3101 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN |
3102 MVPP2_PRS_RI_DROP_MASK,
3103 MVPP2_PRS_RI_L3_PROTO_MASK |
3104 MVPP2_PRS_RI_DROP_MASK);
3105
3106 mvpp2_prs_tcam_data_byte_set(&pe, 1, 0x00, MVPP2_PRS_IPV6_HOP_MASK);
3107 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3108 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3109
3110 /* Update shadow table and hw entry */
3111 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3112 mvpp2_prs_hw_write(priv, &pe);
3113
3114 /* Default IPv6 entry for unknown protocols */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003115 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003116 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3117 pe.index = MVPP2_PE_IP6_PROTO_UN;
3118
3119 /* Finished: go to flowid generation */
3120 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3121 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3122 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3123 MVPP2_PRS_RI_L4_PROTO_MASK);
3124 /* Set L4 offset relatively to our current place */
3125 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
3126 sizeof(struct ipv6hdr) - 4,
3127 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3128
3129 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3130 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3131 /* Unmask all ports */
3132 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3133
3134 /* Update shadow table and hw entry */
3135 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3136 mvpp2_prs_hw_write(priv, &pe);
3137
3138 /* Default IPv6 entry for unknown ext protocols */
3139 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3140 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3141 pe.index = MVPP2_PE_IP6_EXT_PROTO_UN;
3142
3143 /* Finished: go to flowid generation */
3144 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3145 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3146 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3147 MVPP2_PRS_RI_L4_PROTO_MASK);
3148
3149 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_EXT_AI_BIT,
3150 MVPP2_PRS_IPV6_EXT_AI_BIT);
3151 /* Unmask all ports */
3152 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3153
3154 /* Update shadow table and hw entry */
3155 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3156 mvpp2_prs_hw_write(priv, &pe);
3157
3158 /* Default IPv6 entry for unicast address */
3159 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3160 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3161 pe.index = MVPP2_PE_IP6_ADDR_UN;
3162
3163 /* Finished: go to IPv6 again */
3164 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
3165 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
3166 MVPP2_PRS_RI_L3_ADDR_MASK);
3167 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3168 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3169 /* Shift back to IPV6 NH */
3170 mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3171
3172 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3173 /* Unmask all ports */
3174 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3175
3176 /* Update shadow table and hw entry */
3177 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
3178 mvpp2_prs_hw_write(priv, &pe);
3179
3180 return 0;
3181}
3182
3183/* Parser default initialization */
3184static int mvpp2_prs_default_init(struct platform_device *pdev,
3185 struct mvpp2 *priv)
3186{
3187 int err, index, i;
3188
3189 /* Enable tcam table */
3190 mvpp2_write(priv, MVPP2_PRS_TCAM_CTRL_REG, MVPP2_PRS_TCAM_EN_MASK);
3191
3192 /* Clear all tcam and sram entries */
3193 for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++) {
3194 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
3195 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
3196 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), 0);
3197
3198 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, index);
3199 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
3200 mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), 0);
3201 }
3202
3203 /* Invalidate all tcam entries */
3204 for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++)
3205 mvpp2_prs_hw_inv(priv, index);
3206
3207 priv->prs_shadow = devm_kcalloc(&pdev->dev, MVPP2_PRS_TCAM_SRAM_SIZE,
Markus Elfring37df25e2017-04-17 09:12:34 +02003208 sizeof(*priv->prs_shadow),
Marcin Wojtas3f518502014-07-10 16:52:13 -03003209 GFP_KERNEL);
3210 if (!priv->prs_shadow)
3211 return -ENOMEM;
3212
3213 /* Always start from lookup = 0 */
3214 for (index = 0; index < MVPP2_MAX_PORTS; index++)
3215 mvpp2_prs_hw_port_init(priv, index, MVPP2_PRS_LU_MH,
3216 MVPP2_PRS_PORT_LU_MAX, 0);
3217
3218 mvpp2_prs_def_flow_init(priv);
3219
3220 mvpp2_prs_mh_init(priv);
3221
3222 mvpp2_prs_mac_init(priv);
3223
3224 mvpp2_prs_dsa_init(priv);
3225
3226 err = mvpp2_prs_etype_init(priv);
3227 if (err)
3228 return err;
3229
3230 err = mvpp2_prs_vlan_init(pdev, priv);
3231 if (err)
3232 return err;
3233
3234 err = mvpp2_prs_pppoe_init(priv);
3235 if (err)
3236 return err;
3237
3238 err = mvpp2_prs_ip6_init(priv);
3239 if (err)
3240 return err;
3241
3242 err = mvpp2_prs_ip4_init(priv);
3243 if (err)
3244 return err;
3245
3246 return 0;
3247}
3248
3249/* Compare MAC DA with tcam entry data */
3250static bool mvpp2_prs_mac_range_equals(struct mvpp2_prs_entry *pe,
3251 const u8 *da, unsigned char *mask)
3252{
3253 unsigned char tcam_byte, tcam_mask;
3254 int index;
3255
3256 for (index = 0; index < ETH_ALEN; index++) {
3257 mvpp2_prs_tcam_data_byte_get(pe, index, &tcam_byte, &tcam_mask);
3258 if (tcam_mask != mask[index])
3259 return false;
3260
3261 if ((tcam_mask & tcam_byte) != (da[index] & mask[index]))
3262 return false;
3263 }
3264
3265 return true;
3266}
3267
3268/* Find tcam entry with matched pair <MAC DA, port> */
3269static struct mvpp2_prs_entry *
3270mvpp2_prs_mac_da_range_find(struct mvpp2 *priv, int pmap, const u8 *da,
3271 unsigned char *mask, int udf_type)
3272{
3273 struct mvpp2_prs_entry *pe;
3274 int tid;
3275
3276 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
3277 if (!pe)
3278 return NULL;
3279 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
3280
3281 /* Go through the all entires with MVPP2_PRS_LU_MAC */
3282 for (tid = MVPP2_PE_FIRST_FREE_TID;
3283 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
3284 unsigned int entry_pmap;
3285
3286 if (!priv->prs_shadow[tid].valid ||
3287 (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
3288 (priv->prs_shadow[tid].udf != udf_type))
3289 continue;
3290
3291 pe->index = tid;
3292 mvpp2_prs_hw_read(priv, pe);
3293 entry_pmap = mvpp2_prs_tcam_port_map_get(pe);
3294
3295 if (mvpp2_prs_mac_range_equals(pe, da, mask) &&
3296 entry_pmap == pmap)
3297 return pe;
3298 }
3299 kfree(pe);
3300
3301 return NULL;
3302}
3303
3304/* Update parser's mac da entry */
3305static int mvpp2_prs_mac_da_accept(struct mvpp2 *priv, int port,
3306 const u8 *da, bool add)
3307{
3308 struct mvpp2_prs_entry *pe;
3309 unsigned int pmap, len, ri;
3310 unsigned char mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3311 int tid;
3312
3313 /* Scan TCAM and see if entry with this <MAC DA, port> already exist */
3314 pe = mvpp2_prs_mac_da_range_find(priv, (1 << port), da, mask,
3315 MVPP2_PRS_UDF_MAC_DEF);
3316
3317 /* No such entry */
3318 if (!pe) {
3319 if (!add)
3320 return 0;
3321
3322 /* Create new TCAM entry */
3323 /* Find first range mac entry*/
3324 for (tid = MVPP2_PE_FIRST_FREE_TID;
3325 tid <= MVPP2_PE_LAST_FREE_TID; tid++)
3326 if (priv->prs_shadow[tid].valid &&
3327 (priv->prs_shadow[tid].lu == MVPP2_PRS_LU_MAC) &&
3328 (priv->prs_shadow[tid].udf ==
3329 MVPP2_PRS_UDF_MAC_RANGE))
3330 break;
3331
3332 /* Go through the all entries from first to last */
3333 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3334 tid - 1);
3335 if (tid < 0)
3336 return tid;
3337
3338 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
3339 if (!pe)
Amitoj Kaur Chawlac2bb7bc2016-02-04 19:25:26 +05303340 return -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003341 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
3342 pe->index = tid;
3343
3344 /* Mask all ports */
3345 mvpp2_prs_tcam_port_map_set(pe, 0);
3346 }
3347
3348 /* Update port mask */
3349 mvpp2_prs_tcam_port_set(pe, port, add);
3350
3351 /* Invalidate the entry if no ports are left enabled */
3352 pmap = mvpp2_prs_tcam_port_map_get(pe);
3353 if (pmap == 0) {
3354 if (add) {
3355 kfree(pe);
Amitoj Kaur Chawlac2bb7bc2016-02-04 19:25:26 +05303356 return -EINVAL;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003357 }
3358 mvpp2_prs_hw_inv(priv, pe->index);
3359 priv->prs_shadow[pe->index].valid = false;
3360 kfree(pe);
3361 return 0;
3362 }
3363
3364 /* Continue - set next lookup */
3365 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_DSA);
3366
3367 /* Set match on DA */
3368 len = ETH_ALEN;
3369 while (len--)
3370 mvpp2_prs_tcam_data_byte_set(pe, len, da[len], 0xff);
3371
3372 /* Set result info bits */
3373 if (is_broadcast_ether_addr(da))
3374 ri = MVPP2_PRS_RI_L2_BCAST;
3375 else if (is_multicast_ether_addr(da))
3376 ri = MVPP2_PRS_RI_L2_MCAST;
3377 else
3378 ri = MVPP2_PRS_RI_L2_UCAST | MVPP2_PRS_RI_MAC_ME_MASK;
3379
3380 mvpp2_prs_sram_ri_update(pe, ri, MVPP2_PRS_RI_L2_CAST_MASK |
3381 MVPP2_PRS_RI_MAC_ME_MASK);
3382 mvpp2_prs_shadow_ri_set(priv, pe->index, ri, MVPP2_PRS_RI_L2_CAST_MASK |
3383 MVPP2_PRS_RI_MAC_ME_MASK);
3384
3385 /* Shift to ethertype */
3386 mvpp2_prs_sram_shift_set(pe, 2 * ETH_ALEN,
3387 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3388
3389 /* Update shadow table and hw entry */
3390 priv->prs_shadow[pe->index].udf = MVPP2_PRS_UDF_MAC_DEF;
3391 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_MAC);
3392 mvpp2_prs_hw_write(priv, pe);
3393
3394 kfree(pe);
3395
3396 return 0;
3397}
3398
3399static int mvpp2_prs_update_mac_da(struct net_device *dev, const u8 *da)
3400{
3401 struct mvpp2_port *port = netdev_priv(dev);
3402 int err;
3403
3404 /* Remove old parser entry */
3405 err = mvpp2_prs_mac_da_accept(port->priv, port->id, dev->dev_addr,
3406 false);
3407 if (err)
3408 return err;
3409
3410 /* Add new parser entry */
3411 err = mvpp2_prs_mac_da_accept(port->priv, port->id, da, true);
3412 if (err)
3413 return err;
3414
3415 /* Set addr in the device */
3416 ether_addr_copy(dev->dev_addr, da);
3417
3418 return 0;
3419}
3420
3421/* Delete all port's multicast simple (not range) entries */
3422static void mvpp2_prs_mcast_del_all(struct mvpp2 *priv, int port)
3423{
3424 struct mvpp2_prs_entry pe;
3425 int index, tid;
3426
3427 for (tid = MVPP2_PE_FIRST_FREE_TID;
3428 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
3429 unsigned char da[ETH_ALEN], da_mask[ETH_ALEN];
3430
3431 if (!priv->prs_shadow[tid].valid ||
3432 (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
3433 (priv->prs_shadow[tid].udf != MVPP2_PRS_UDF_MAC_DEF))
3434 continue;
3435
3436 /* Only simple mac entries */
3437 pe.index = tid;
3438 mvpp2_prs_hw_read(priv, &pe);
3439
3440 /* Read mac addr from entry */
3441 for (index = 0; index < ETH_ALEN; index++)
3442 mvpp2_prs_tcam_data_byte_get(&pe, index, &da[index],
3443 &da_mask[index]);
3444
3445 if (is_multicast_ether_addr(da) && !is_broadcast_ether_addr(da))
3446 /* Delete this entry */
3447 mvpp2_prs_mac_da_accept(priv, port, da, false);
3448 }
3449}
3450
3451static int mvpp2_prs_tag_mode_set(struct mvpp2 *priv, int port, int type)
3452{
3453 switch (type) {
3454 case MVPP2_TAG_TYPE_EDSA:
3455 /* Add port to EDSA entries */
3456 mvpp2_prs_dsa_tag_set(priv, port, true,
3457 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3458 mvpp2_prs_dsa_tag_set(priv, port, true,
3459 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3460 /* Remove port from DSA entries */
3461 mvpp2_prs_dsa_tag_set(priv, port, false,
3462 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3463 mvpp2_prs_dsa_tag_set(priv, port, false,
3464 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3465 break;
3466
3467 case MVPP2_TAG_TYPE_DSA:
3468 /* Add port to DSA entries */
3469 mvpp2_prs_dsa_tag_set(priv, port, true,
3470 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3471 mvpp2_prs_dsa_tag_set(priv, port, true,
3472 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3473 /* Remove port from EDSA entries */
3474 mvpp2_prs_dsa_tag_set(priv, port, false,
3475 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3476 mvpp2_prs_dsa_tag_set(priv, port, false,
3477 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3478 break;
3479
3480 case MVPP2_TAG_TYPE_MH:
3481 case MVPP2_TAG_TYPE_NONE:
3482 /* Remove port form EDSA and DSA entries */
3483 mvpp2_prs_dsa_tag_set(priv, port, false,
3484 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3485 mvpp2_prs_dsa_tag_set(priv, port, false,
3486 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3487 mvpp2_prs_dsa_tag_set(priv, port, false,
3488 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3489 mvpp2_prs_dsa_tag_set(priv, port, false,
3490 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3491 break;
3492
3493 default:
3494 if ((type < 0) || (type > MVPP2_TAG_TYPE_EDSA))
3495 return -EINVAL;
3496 }
3497
3498 return 0;
3499}
3500
3501/* Set prs flow for the port */
3502static int mvpp2_prs_def_flow(struct mvpp2_port *port)
3503{
3504 struct mvpp2_prs_entry *pe;
3505 int tid;
3506
3507 pe = mvpp2_prs_flow_find(port->priv, port->id);
3508
3509 /* Such entry not exist */
3510 if (!pe) {
3511 /* Go through the all entires from last to first */
3512 tid = mvpp2_prs_tcam_first_free(port->priv,
3513 MVPP2_PE_LAST_FREE_TID,
3514 MVPP2_PE_FIRST_FREE_TID);
3515 if (tid < 0)
3516 return tid;
3517
3518 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
3519 if (!pe)
3520 return -ENOMEM;
3521
3522 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
3523 pe->index = tid;
3524
3525 /* Set flow ID*/
3526 mvpp2_prs_sram_ai_update(pe, port->id, MVPP2_PRS_FLOW_ID_MASK);
3527 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
3528
3529 /* Update shadow table */
3530 mvpp2_prs_shadow_set(port->priv, pe->index, MVPP2_PRS_LU_FLOWS);
3531 }
3532
3533 mvpp2_prs_tcam_port_map_set(pe, (1 << port->id));
3534 mvpp2_prs_hw_write(port->priv, pe);
3535 kfree(pe);
3536
3537 return 0;
3538}
3539
3540/* Classifier configuration routines */
3541
3542/* Update classification flow table registers */
3543static void mvpp2_cls_flow_write(struct mvpp2 *priv,
3544 struct mvpp2_cls_flow_entry *fe)
3545{
3546 mvpp2_write(priv, MVPP2_CLS_FLOW_INDEX_REG, fe->index);
3547 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL0_REG, fe->data[0]);
3548 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL1_REG, fe->data[1]);
3549 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL2_REG, fe->data[2]);
3550}
3551
3552/* Update classification lookup table register */
3553static void mvpp2_cls_lookup_write(struct mvpp2 *priv,
3554 struct mvpp2_cls_lookup_entry *le)
3555{
3556 u32 val;
3557
3558 val = (le->way << MVPP2_CLS_LKP_INDEX_WAY_OFFS) | le->lkpid;
3559 mvpp2_write(priv, MVPP2_CLS_LKP_INDEX_REG, val);
3560 mvpp2_write(priv, MVPP2_CLS_LKP_TBL_REG, le->data);
3561}
3562
3563/* Classifier default initialization */
3564static void mvpp2_cls_init(struct mvpp2 *priv)
3565{
3566 struct mvpp2_cls_lookup_entry le;
3567 struct mvpp2_cls_flow_entry fe;
3568 int index;
3569
3570 /* Enable classifier */
3571 mvpp2_write(priv, MVPP2_CLS_MODE_REG, MVPP2_CLS_MODE_ACTIVE_MASK);
3572
3573 /* Clear classifier flow table */
Arnd Bergmanne8f967c2016-11-24 17:28:12 +01003574 memset(&fe.data, 0, sizeof(fe.data));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003575 for (index = 0; index < MVPP2_CLS_FLOWS_TBL_SIZE; index++) {
3576 fe.index = index;
3577 mvpp2_cls_flow_write(priv, &fe);
3578 }
3579
3580 /* Clear classifier lookup table */
3581 le.data = 0;
3582 for (index = 0; index < MVPP2_CLS_LKP_TBL_SIZE; index++) {
3583 le.lkpid = index;
3584 le.way = 0;
3585 mvpp2_cls_lookup_write(priv, &le);
3586
3587 le.way = 1;
3588 mvpp2_cls_lookup_write(priv, &le);
3589 }
3590}
3591
3592static void mvpp2_cls_port_config(struct mvpp2_port *port)
3593{
3594 struct mvpp2_cls_lookup_entry le;
3595 u32 val;
3596
3597 /* Set way for the port */
3598 val = mvpp2_read(port->priv, MVPP2_CLS_PORT_WAY_REG);
3599 val &= ~MVPP2_CLS_PORT_WAY_MASK(port->id);
3600 mvpp2_write(port->priv, MVPP2_CLS_PORT_WAY_REG, val);
3601
3602 /* Pick the entry to be accessed in lookup ID decoding table
3603 * according to the way and lkpid.
3604 */
3605 le.lkpid = port->id;
3606 le.way = 0;
3607 le.data = 0;
3608
3609 /* Set initial CPU queue for receiving packets */
3610 le.data &= ~MVPP2_CLS_LKP_TBL_RXQ_MASK;
3611 le.data |= port->first_rxq;
3612
3613 /* Disable classification engines */
3614 le.data &= ~MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK;
3615
3616 /* Update lookup ID table entry */
3617 mvpp2_cls_lookup_write(port->priv, &le);
3618}
3619
3620/* Set CPU queue number for oversize packets */
3621static void mvpp2_cls_oversize_rxq_set(struct mvpp2_port *port)
3622{
3623 u32 val;
3624
3625 mvpp2_write(port->priv, MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port->id),
3626 port->first_rxq & MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK);
3627
3628 mvpp2_write(port->priv, MVPP2_CLS_SWFWD_P2HQ_REG(port->id),
3629 (port->first_rxq >> MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS));
3630
3631 val = mvpp2_read(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG);
3632 val |= MVPP2_CLS_SWFWD_PCTRL_MASK(port->id);
3633 mvpp2_write(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG, val);
3634}
3635
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003636static void *mvpp2_frag_alloc(const struct mvpp2_bm_pool *pool)
3637{
3638 if (likely(pool->frag_size <= PAGE_SIZE))
3639 return netdev_alloc_frag(pool->frag_size);
3640 else
3641 return kmalloc(pool->frag_size, GFP_ATOMIC);
3642}
3643
3644static void mvpp2_frag_free(const struct mvpp2_bm_pool *pool, void *data)
3645{
3646 if (likely(pool->frag_size <= PAGE_SIZE))
3647 skb_free_frag(data);
3648 else
3649 kfree(data);
3650}
3651
Marcin Wojtas3f518502014-07-10 16:52:13 -03003652/* Buffer Manager configuration routines */
3653
3654/* Create pool */
3655static int mvpp2_bm_pool_create(struct platform_device *pdev,
3656 struct mvpp2 *priv,
3657 struct mvpp2_bm_pool *bm_pool, int size)
3658{
Marcin Wojtas3f518502014-07-10 16:52:13 -03003659 u32 val;
3660
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003661 /* Number of buffer pointers must be a multiple of 16, as per
3662 * hardware constraints
3663 */
3664 if (!IS_ALIGNED(size, 16))
3665 return -EINVAL;
3666
3667 /* PPv2.1 needs 8 bytes per buffer pointer, PPv2.2 needs 16
3668 * bytes per buffer pointer
3669 */
3670 if (priv->hw_version == MVPP21)
3671 bm_pool->size_bytes = 2 * sizeof(u32) * size;
3672 else
3673 bm_pool->size_bytes = 2 * sizeof(u64) * size;
3674
3675 bm_pool->virt_addr = dma_alloc_coherent(&pdev->dev, bm_pool->size_bytes,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003676 &bm_pool->dma_addr,
Marcin Wojtas3f518502014-07-10 16:52:13 -03003677 GFP_KERNEL);
3678 if (!bm_pool->virt_addr)
3679 return -ENOMEM;
3680
Thomas Petazzonid3158802017-02-21 11:28:13 +01003681 if (!IS_ALIGNED((unsigned long)bm_pool->virt_addr,
3682 MVPP2_BM_POOL_PTR_ALIGN)) {
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003683 dma_free_coherent(&pdev->dev, bm_pool->size_bytes,
3684 bm_pool->virt_addr, bm_pool->dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003685 dev_err(&pdev->dev, "BM pool %d is not %d bytes aligned\n",
3686 bm_pool->id, MVPP2_BM_POOL_PTR_ALIGN);
3687 return -ENOMEM;
3688 }
3689
3690 mvpp2_write(priv, MVPP2_BM_POOL_BASE_REG(bm_pool->id),
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003691 lower_32_bits(bm_pool->dma_addr));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003692 mvpp2_write(priv, MVPP2_BM_POOL_SIZE_REG(bm_pool->id), size);
3693
3694 val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
3695 val |= MVPP2_BM_START_MASK;
3696 mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
3697
3698 bm_pool->type = MVPP2_BM_FREE;
3699 bm_pool->size = size;
3700 bm_pool->pkt_size = 0;
3701 bm_pool->buf_num = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003702
3703 return 0;
3704}
3705
3706/* Set pool buffer size */
3707static void mvpp2_bm_pool_bufsize_set(struct mvpp2 *priv,
3708 struct mvpp2_bm_pool *bm_pool,
3709 int buf_size)
3710{
3711 u32 val;
3712
3713 bm_pool->buf_size = buf_size;
3714
3715 val = ALIGN(buf_size, 1 << MVPP2_POOL_BUF_SIZE_OFFSET);
3716 mvpp2_write(priv, MVPP2_POOL_BUF_SIZE_REG(bm_pool->id), val);
3717}
3718
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003719static void mvpp2_bm_bufs_get_addrs(struct device *dev, struct mvpp2 *priv,
3720 struct mvpp2_bm_pool *bm_pool,
3721 dma_addr_t *dma_addr,
3722 phys_addr_t *phys_addr)
3723{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02003724 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01003725
3726 *dma_addr = mvpp2_percpu_read(priv, cpu,
3727 MVPP2_BM_PHY_ALLOC_REG(bm_pool->id));
3728 *phys_addr = mvpp2_percpu_read(priv, cpu, MVPP2_BM_VIRT_ALLOC_REG);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003729
3730 if (priv->hw_version == MVPP22) {
3731 u32 val;
3732 u32 dma_addr_highbits, phys_addr_highbits;
3733
Thomas Petazzonia7868412017-03-07 16:53:13 +01003734 val = mvpp2_percpu_read(priv, cpu, MVPP22_BM_ADDR_HIGH_ALLOC);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003735 dma_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_PHYS_MASK);
3736 phys_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_VIRT_MASK) >>
3737 MVPP22_BM_ADDR_HIGH_VIRT_SHIFT;
3738
3739 if (sizeof(dma_addr_t) == 8)
3740 *dma_addr |= (u64)dma_addr_highbits << 32;
3741
3742 if (sizeof(phys_addr_t) == 8)
3743 *phys_addr |= (u64)phys_addr_highbits << 32;
3744 }
Thomas Petazzonia704bb52017-06-10 23:18:22 +02003745
3746 put_cpu();
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003747}
3748
Ezequiel Garcia7861f122014-07-21 13:48:14 -03003749/* Free all buffers from the pool */
Marcin Wojtas4229d502015-12-03 15:20:50 +01003750static void mvpp2_bm_bufs_free(struct device *dev, struct mvpp2 *priv,
3751 struct mvpp2_bm_pool *bm_pool)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003752{
3753 int i;
3754
Ezequiel Garcia7861f122014-07-21 13:48:14 -03003755 for (i = 0; i < bm_pool->buf_num; i++) {
Thomas Petazzoni20396132017-03-07 16:53:00 +01003756 dma_addr_t buf_dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003757 phys_addr_t buf_phys_addr;
3758 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003759
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003760 mvpp2_bm_bufs_get_addrs(dev, priv, bm_pool,
3761 &buf_dma_addr, &buf_phys_addr);
Marcin Wojtas4229d502015-12-03 15:20:50 +01003762
Thomas Petazzoni20396132017-03-07 16:53:00 +01003763 dma_unmap_single(dev, buf_dma_addr,
Marcin Wojtas4229d502015-12-03 15:20:50 +01003764 bm_pool->buf_size, DMA_FROM_DEVICE);
3765
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003766 data = (void *)phys_to_virt(buf_phys_addr);
3767 if (!data)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003768 break;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003769
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003770 mvpp2_frag_free(bm_pool, data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003771 }
3772
3773 /* Update BM driver with number of buffers removed from pool */
3774 bm_pool->buf_num -= i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003775}
3776
3777/* Cleanup pool */
3778static int mvpp2_bm_pool_destroy(struct platform_device *pdev,
3779 struct mvpp2 *priv,
3780 struct mvpp2_bm_pool *bm_pool)
3781{
Marcin Wojtas3f518502014-07-10 16:52:13 -03003782 u32 val;
3783
Marcin Wojtas4229d502015-12-03 15:20:50 +01003784 mvpp2_bm_bufs_free(&pdev->dev, priv, bm_pool);
Ezequiel Garciad74c96c2014-07-21 13:48:13 -03003785 if (bm_pool->buf_num) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03003786 WARN(1, "cannot free all buffers in pool %d\n", bm_pool->id);
3787 return 0;
3788 }
3789
3790 val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
3791 val |= MVPP2_BM_STOP_MASK;
3792 mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
3793
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003794 dma_free_coherent(&pdev->dev, bm_pool->size_bytes,
Marcin Wojtas3f518502014-07-10 16:52:13 -03003795 bm_pool->virt_addr,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003796 bm_pool->dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003797 return 0;
3798}
3799
3800static int mvpp2_bm_pools_init(struct platform_device *pdev,
3801 struct mvpp2 *priv)
3802{
3803 int i, err, size;
3804 struct mvpp2_bm_pool *bm_pool;
3805
3806 /* Create all pools with maximum size */
3807 size = MVPP2_BM_POOL_SIZE_MAX;
3808 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
3809 bm_pool = &priv->bm_pools[i];
3810 bm_pool->id = i;
3811 err = mvpp2_bm_pool_create(pdev, priv, bm_pool, size);
3812 if (err)
3813 goto err_unroll_pools;
3814 mvpp2_bm_pool_bufsize_set(priv, bm_pool, 0);
3815 }
3816 return 0;
3817
3818err_unroll_pools:
3819 dev_err(&pdev->dev, "failed to create BM pool %d, size %d\n", i, size);
3820 for (i = i - 1; i >= 0; i--)
3821 mvpp2_bm_pool_destroy(pdev, priv, &priv->bm_pools[i]);
3822 return err;
3823}
3824
3825static int mvpp2_bm_init(struct platform_device *pdev, struct mvpp2 *priv)
3826{
3827 int i, err;
3828
3829 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
3830 /* Mask BM all interrupts */
3831 mvpp2_write(priv, MVPP2_BM_INTR_MASK_REG(i), 0);
3832 /* Clear BM cause register */
3833 mvpp2_write(priv, MVPP2_BM_INTR_CAUSE_REG(i), 0);
3834 }
3835
3836 /* Allocate and initialize BM pools */
3837 priv->bm_pools = devm_kcalloc(&pdev->dev, MVPP2_BM_POOLS_NUM,
Markus Elfring81f915e2017-04-17 09:06:33 +02003838 sizeof(*priv->bm_pools), GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003839 if (!priv->bm_pools)
3840 return -ENOMEM;
3841
3842 err = mvpp2_bm_pools_init(pdev, priv);
3843 if (err < 0)
3844 return err;
3845 return 0;
3846}
3847
3848/* Attach long pool to rxq */
3849static void mvpp2_rxq_long_pool_set(struct mvpp2_port *port,
3850 int lrxq, int long_pool)
3851{
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003852 u32 val, mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003853 int prxq;
3854
3855 /* Get queue physical ID */
3856 prxq = port->rxqs[lrxq]->id;
3857
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003858 if (port->priv->hw_version == MVPP21)
3859 mask = MVPP21_RXQ_POOL_LONG_MASK;
3860 else
3861 mask = MVPP22_RXQ_POOL_LONG_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003862
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003863 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
3864 val &= ~mask;
3865 val |= (long_pool << MVPP2_RXQ_POOL_LONG_OFFS) & mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003866 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
3867}
3868
3869/* Attach short pool to rxq */
3870static void mvpp2_rxq_short_pool_set(struct mvpp2_port *port,
3871 int lrxq, int short_pool)
3872{
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003873 u32 val, mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003874 int prxq;
3875
3876 /* Get queue physical ID */
3877 prxq = port->rxqs[lrxq]->id;
3878
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003879 if (port->priv->hw_version == MVPP21)
3880 mask = MVPP21_RXQ_POOL_SHORT_MASK;
3881 else
3882 mask = MVPP22_RXQ_POOL_SHORT_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003883
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003884 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
3885 val &= ~mask;
3886 val |= (short_pool << MVPP2_RXQ_POOL_SHORT_OFFS) & mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003887 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
3888}
3889
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003890static void *mvpp2_buf_alloc(struct mvpp2_port *port,
3891 struct mvpp2_bm_pool *bm_pool,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003892 dma_addr_t *buf_dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003893 phys_addr_t *buf_phys_addr,
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003894 gfp_t gfp_mask)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003895{
Thomas Petazzoni20396132017-03-07 16:53:00 +01003896 dma_addr_t dma_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003897 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003898
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003899 data = mvpp2_frag_alloc(bm_pool);
3900 if (!data)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003901 return NULL;
3902
Thomas Petazzoni20396132017-03-07 16:53:00 +01003903 dma_addr = dma_map_single(port->dev->dev.parent, data,
3904 MVPP2_RX_BUF_SIZE(bm_pool->pkt_size),
3905 DMA_FROM_DEVICE);
3906 if (unlikely(dma_mapping_error(port->dev->dev.parent, dma_addr))) {
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003907 mvpp2_frag_free(bm_pool, data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003908 return NULL;
3909 }
Thomas Petazzoni20396132017-03-07 16:53:00 +01003910 *buf_dma_addr = dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003911 *buf_phys_addr = virt_to_phys(data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003912
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003913 return data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003914}
3915
Marcin Wojtas3f518502014-07-10 16:52:13 -03003916/* Release buffer to BM */
3917static inline void mvpp2_bm_pool_put(struct mvpp2_port *port, int pool,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003918 dma_addr_t buf_dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003919 phys_addr_t buf_phys_addr)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003920{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02003921 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01003922
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003923 if (port->priv->hw_version == MVPP22) {
3924 u32 val = 0;
3925
3926 if (sizeof(dma_addr_t) == 8)
3927 val |= upper_32_bits(buf_dma_addr) &
3928 MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK;
3929
3930 if (sizeof(phys_addr_t) == 8)
3931 val |= (upper_32_bits(buf_phys_addr)
3932 << MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT) &
3933 MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK;
3934
Thomas Petazzonia7868412017-03-07 16:53:13 +01003935 mvpp2_percpu_write(port->priv, cpu,
3936 MVPP22_BM_ADDR_HIGH_RLS_REG, val);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003937 }
3938
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003939 /* MVPP2_BM_VIRT_RLS_REG is not interpreted by HW, and simply
3940 * returned in the "cookie" field of the RX
3941 * descriptor. Instead of storing the virtual address, we
3942 * store the physical address
3943 */
Thomas Petazzonia7868412017-03-07 16:53:13 +01003944 mvpp2_percpu_write(port->priv, cpu,
3945 MVPP2_BM_VIRT_RLS_REG, buf_phys_addr);
3946 mvpp2_percpu_write(port->priv, cpu,
3947 MVPP2_BM_PHY_RLS_REG(pool), buf_dma_addr);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02003948
3949 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03003950}
3951
Marcin Wojtas3f518502014-07-10 16:52:13 -03003952/* Allocate buffers for the pool */
3953static int mvpp2_bm_bufs_add(struct mvpp2_port *port,
3954 struct mvpp2_bm_pool *bm_pool, int buf_num)
3955{
Marcin Wojtas3f518502014-07-10 16:52:13 -03003956 int i, buf_size, total_size;
Thomas Petazzoni20396132017-03-07 16:53:00 +01003957 dma_addr_t dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003958 phys_addr_t phys_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003959 void *buf;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003960
3961 buf_size = MVPP2_RX_BUF_SIZE(bm_pool->pkt_size);
3962 total_size = MVPP2_RX_TOTAL_SIZE(buf_size);
3963
3964 if (buf_num < 0 ||
3965 (buf_num + bm_pool->buf_num > bm_pool->size)) {
3966 netdev_err(port->dev,
3967 "cannot allocate %d buffers for pool %d\n",
3968 buf_num, bm_pool->id);
3969 return 0;
3970 }
3971
Marcin Wojtas3f518502014-07-10 16:52:13 -03003972 for (i = 0; i < buf_num; i++) {
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003973 buf = mvpp2_buf_alloc(port, bm_pool, &dma_addr,
3974 &phys_addr, GFP_KERNEL);
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003975 if (!buf)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003976 break;
3977
Thomas Petazzoni20396132017-03-07 16:53:00 +01003978 mvpp2_bm_pool_put(port, bm_pool->id, dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003979 phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003980 }
3981
3982 /* Update BM driver with number of buffers added to pool */
3983 bm_pool->buf_num += i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003984
3985 netdev_dbg(port->dev,
3986 "%s pool %d: pkt_size=%4d, buf_size=%4d, total_size=%4d\n",
3987 bm_pool->type == MVPP2_BM_SWF_SHORT ? "short" : " long",
3988 bm_pool->id, bm_pool->pkt_size, buf_size, total_size);
3989
3990 netdev_dbg(port->dev,
3991 "%s pool %d: %d of %d buffers added\n",
3992 bm_pool->type == MVPP2_BM_SWF_SHORT ? "short" : " long",
3993 bm_pool->id, i, buf_num);
3994 return i;
3995}
3996
3997/* Notify the driver that BM pool is being used as specific type and return the
3998 * pool pointer on success
3999 */
4000static struct mvpp2_bm_pool *
4001mvpp2_bm_pool_use(struct mvpp2_port *port, int pool, enum mvpp2_bm_type type,
4002 int pkt_size)
4003{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004004 struct mvpp2_bm_pool *new_pool = &port->priv->bm_pools[pool];
4005 int num;
4006
4007 if (new_pool->type != MVPP2_BM_FREE && new_pool->type != type) {
4008 netdev_err(port->dev, "mixing pool types is forbidden\n");
4009 return NULL;
4010 }
4011
Marcin Wojtas3f518502014-07-10 16:52:13 -03004012 if (new_pool->type == MVPP2_BM_FREE)
4013 new_pool->type = type;
4014
4015 /* Allocate buffers in case BM pool is used as long pool, but packet
4016 * size doesn't match MTU or BM pool hasn't being used yet
4017 */
4018 if (((type == MVPP2_BM_SWF_LONG) && (pkt_size > new_pool->pkt_size)) ||
4019 (new_pool->pkt_size == 0)) {
4020 int pkts_num;
4021
4022 /* Set default buffer number or free all the buffers in case
4023 * the pool is not empty
4024 */
4025 pkts_num = new_pool->buf_num;
4026 if (pkts_num == 0)
4027 pkts_num = type == MVPP2_BM_SWF_LONG ?
4028 MVPP2_BM_LONG_BUF_NUM :
4029 MVPP2_BM_SHORT_BUF_NUM;
4030 else
Marcin Wojtas4229d502015-12-03 15:20:50 +01004031 mvpp2_bm_bufs_free(port->dev->dev.parent,
4032 port->priv, new_pool);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004033
4034 new_pool->pkt_size = pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004035 new_pool->frag_size =
4036 SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
4037 MVPP2_SKB_SHINFO_SIZE;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004038
4039 /* Allocate buffers for this pool */
4040 num = mvpp2_bm_bufs_add(port, new_pool, pkts_num);
4041 if (num != pkts_num) {
4042 WARN(1, "pool %d: %d of %d allocated\n",
4043 new_pool->id, num, pkts_num);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004044 return NULL;
4045 }
4046 }
4047
4048 mvpp2_bm_pool_bufsize_set(port->priv, new_pool,
4049 MVPP2_RX_BUF_SIZE(new_pool->pkt_size));
4050
Marcin Wojtas3f518502014-07-10 16:52:13 -03004051 return new_pool;
4052}
4053
4054/* Initialize pools for swf */
4055static int mvpp2_swf_bm_pool_init(struct mvpp2_port *port)
4056{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004057 int rxq;
4058
4059 if (!port->pool_long) {
4060 port->pool_long =
4061 mvpp2_bm_pool_use(port, MVPP2_BM_SWF_LONG_POOL(port->id),
4062 MVPP2_BM_SWF_LONG,
4063 port->pkt_size);
4064 if (!port->pool_long)
4065 return -ENOMEM;
4066
Marcin Wojtas3f518502014-07-10 16:52:13 -03004067 port->pool_long->port_map |= (1 << port->id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004068
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004069 for (rxq = 0; rxq < port->nrxqs; rxq++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004070 mvpp2_rxq_long_pool_set(port, rxq, port->pool_long->id);
4071 }
4072
4073 if (!port->pool_short) {
4074 port->pool_short =
4075 mvpp2_bm_pool_use(port, MVPP2_BM_SWF_SHORT_POOL,
4076 MVPP2_BM_SWF_SHORT,
4077 MVPP2_BM_SHORT_PKT_SIZE);
4078 if (!port->pool_short)
4079 return -ENOMEM;
4080
Marcin Wojtas3f518502014-07-10 16:52:13 -03004081 port->pool_short->port_map |= (1 << port->id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004082
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004083 for (rxq = 0; rxq < port->nrxqs; rxq++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004084 mvpp2_rxq_short_pool_set(port, rxq,
4085 port->pool_short->id);
4086 }
4087
4088 return 0;
4089}
4090
4091static int mvpp2_bm_update_mtu(struct net_device *dev, int mtu)
4092{
4093 struct mvpp2_port *port = netdev_priv(dev);
4094 struct mvpp2_bm_pool *port_pool = port->pool_long;
4095 int num, pkts_num = port_pool->buf_num;
4096 int pkt_size = MVPP2_RX_PKT_SIZE(mtu);
4097
4098 /* Update BM pool with new buffer size */
Marcin Wojtas4229d502015-12-03 15:20:50 +01004099 mvpp2_bm_bufs_free(dev->dev.parent, port->priv, port_pool);
Ezequiel Garciad74c96c2014-07-21 13:48:13 -03004100 if (port_pool->buf_num) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004101 WARN(1, "cannot free all buffers in pool %d\n", port_pool->id);
4102 return -EIO;
4103 }
4104
4105 port_pool->pkt_size = pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004106 port_pool->frag_size = SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
4107 MVPP2_SKB_SHINFO_SIZE;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004108 num = mvpp2_bm_bufs_add(port, port_pool, pkts_num);
4109 if (num != pkts_num) {
4110 WARN(1, "pool %d: %d of %d allocated\n",
4111 port_pool->id, num, pkts_num);
4112 return -EIO;
4113 }
4114
4115 mvpp2_bm_pool_bufsize_set(port->priv, port_pool,
4116 MVPP2_RX_BUF_SIZE(port_pool->pkt_size));
4117 dev->mtu = mtu;
4118 netdev_update_features(dev);
4119 return 0;
4120}
4121
4122static inline void mvpp2_interrupts_enable(struct mvpp2_port *port)
4123{
4124 int cpu, cpu_mask = 0;
4125
4126 for_each_present_cpu(cpu)
4127 cpu_mask |= 1 << cpu;
4128 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
4129 MVPP2_ISR_ENABLE_INTERRUPT(cpu_mask));
4130}
4131
4132static inline void mvpp2_interrupts_disable(struct mvpp2_port *port)
4133{
4134 int cpu, cpu_mask = 0;
4135
4136 for_each_present_cpu(cpu)
4137 cpu_mask |= 1 << cpu;
4138 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
4139 MVPP2_ISR_DISABLE_INTERRUPT(cpu_mask));
4140}
4141
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004142/* Mask the current CPU's Rx/Tx interrupts
4143 * Called by on_each_cpu(), guaranteed to run with migration disabled,
4144 * using smp_processor_id() is OK.
4145 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004146static void mvpp2_interrupts_mask(void *arg)
4147{
4148 struct mvpp2_port *port = arg;
4149
Thomas Petazzonia7868412017-03-07 16:53:13 +01004150 mvpp2_percpu_write(port->priv, smp_processor_id(),
4151 MVPP2_ISR_RX_TX_MASK_REG(port->id), 0);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004152}
4153
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004154/* Unmask the current CPU's Rx/Tx interrupts.
4155 * Called by on_each_cpu(), guaranteed to run with migration disabled,
4156 * using smp_processor_id() is OK.
4157 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004158static void mvpp2_interrupts_unmask(void *arg)
4159{
4160 struct mvpp2_port *port = arg;
4161
Thomas Petazzonia7868412017-03-07 16:53:13 +01004162 mvpp2_percpu_write(port->priv, smp_processor_id(),
4163 MVPP2_ISR_RX_TX_MASK_REG(port->id),
4164 (MVPP2_CAUSE_MISC_SUM_MASK |
4165 MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004166}
4167
4168/* Port configuration routines */
4169
Thomas Petazzoni26975822017-03-07 16:53:14 +01004170static void mvpp22_port_mii_set(struct mvpp2_port *port)
4171{
4172 u32 val;
4173
Thomas Petazzoni26975822017-03-07 16:53:14 +01004174 /* Only GOP port 0 has an XLG MAC */
4175 if (port->gop_id == 0) {
4176 val = readl(port->base + MVPP22_XLG_CTRL3_REG);
4177 val &= ~MVPP22_XLG_CTRL3_MACMODESELECT_MASK;
Antoine Ténart725757a2017-06-12 16:01:39 +02004178
4179 if (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
4180 port->phy_interface == PHY_INTERFACE_MODE_10GKR)
4181 val |= MVPP22_XLG_CTRL3_MACMODESELECT_10G;
4182 else
4183 val |= MVPP22_XLG_CTRL3_MACMODESELECT_GMAC;
4184
Thomas Petazzoni26975822017-03-07 16:53:14 +01004185 writel(val, port->base + MVPP22_XLG_CTRL3_REG);
4186 }
4187
4188 val = readl(port->base + MVPP22_GMAC_CTRL_4_REG);
4189 if (port->phy_interface == PHY_INTERFACE_MODE_RGMII)
4190 val |= MVPP22_CTRL4_EXT_PIN_GMII_SEL;
4191 else
4192 val &= ~MVPP22_CTRL4_EXT_PIN_GMII_SEL;
4193 val &= ~MVPP22_CTRL4_DP_CLK_SEL;
4194 val |= MVPP22_CTRL4_SYNC_BYPASS;
4195 val |= MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
4196 writel(val, port->base + MVPP22_GMAC_CTRL_4_REG);
4197}
4198
Marcin Wojtas3f518502014-07-10 16:52:13 -03004199static void mvpp2_port_mii_set(struct mvpp2_port *port)
4200{
Marcin Wojtas08a23752014-07-21 13:48:12 -03004201 u32 val;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004202
Thomas Petazzoni26975822017-03-07 16:53:14 +01004203 if (port->priv->hw_version == MVPP22)
4204 mvpp22_port_mii_set(port);
4205
Marcin Wojtas08a23752014-07-21 13:48:12 -03004206 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004207
Marcin Wojtas08a23752014-07-21 13:48:12 -03004208 switch (port->phy_interface) {
4209 case PHY_INTERFACE_MODE_SGMII:
4210 val |= MVPP2_GMAC_INBAND_AN_MASK;
4211 break;
4212 case PHY_INTERFACE_MODE_RGMII:
4213 val |= MVPP2_GMAC_PORT_RGMII_MASK;
4214 default:
4215 val &= ~MVPP2_GMAC_PCS_ENABLE_MASK;
4216 }
4217
4218 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4219}
4220
4221static void mvpp2_port_fc_adv_enable(struct mvpp2_port *port)
4222{
4223 u32 val;
4224
4225 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4226 val |= MVPP2_GMAC_FC_ADV_EN;
4227 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004228}
4229
4230static void mvpp2_port_enable(struct mvpp2_port *port)
4231{
4232 u32 val;
4233
Antoine Ténart725757a2017-06-12 16:01:39 +02004234 /* Only GOP port 0 has an XLG MAC */
4235 if (port->gop_id == 0 &&
4236 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
4237 port->phy_interface == PHY_INTERFACE_MODE_10GKR)) {
4238 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
4239 val |= MVPP22_XLG_CTRL0_PORT_EN |
4240 MVPP22_XLG_CTRL0_MAC_RESET_DIS;
4241 val &= ~MVPP22_XLG_CTRL0_MIB_CNT_DIS;
4242 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
4243 } else {
4244 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4245 val |= MVPP2_GMAC_PORT_EN_MASK;
4246 val |= MVPP2_GMAC_MIB_CNTR_EN_MASK;
4247 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4248 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004249}
4250
4251static void mvpp2_port_disable(struct mvpp2_port *port)
4252{
4253 u32 val;
4254
Antoine Ténart725757a2017-06-12 16:01:39 +02004255 /* Only GOP port 0 has an XLG MAC */
4256 if (port->gop_id == 0 &&
4257 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
4258 port->phy_interface == PHY_INTERFACE_MODE_10GKR)) {
4259 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
4260 val &= ~(MVPP22_XLG_CTRL0_PORT_EN |
4261 MVPP22_XLG_CTRL0_MAC_RESET_DIS);
4262 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
4263 } else {
4264 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4265 val &= ~(MVPP2_GMAC_PORT_EN_MASK);
4266 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4267 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004268}
4269
4270/* Set IEEE 802.3x Flow Control Xon Packet Transmission Mode */
4271static void mvpp2_port_periodic_xon_disable(struct mvpp2_port *port)
4272{
4273 u32 val;
4274
4275 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG) &
4276 ~MVPP2_GMAC_PERIODIC_XON_EN_MASK;
4277 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
4278}
4279
4280/* Configure loopback port */
4281static void mvpp2_port_loopback_set(struct mvpp2_port *port)
4282{
4283 u32 val;
4284
4285 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
4286
4287 if (port->speed == 1000)
4288 val |= MVPP2_GMAC_GMII_LB_EN_MASK;
4289 else
4290 val &= ~MVPP2_GMAC_GMII_LB_EN_MASK;
4291
4292 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
4293 val |= MVPP2_GMAC_PCS_LB_EN_MASK;
4294 else
4295 val &= ~MVPP2_GMAC_PCS_LB_EN_MASK;
4296
4297 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
4298}
4299
4300static void mvpp2_port_reset(struct mvpp2_port *port)
4301{
4302 u32 val;
4303
4304 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
4305 ~MVPP2_GMAC_PORT_RESET_MASK;
4306 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4307
4308 while (readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
4309 MVPP2_GMAC_PORT_RESET_MASK)
4310 continue;
4311}
4312
4313/* Change maximum receive size of the port */
4314static inline void mvpp2_gmac_max_rx_size_set(struct mvpp2_port *port)
4315{
4316 u32 val;
4317
4318 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4319 val &= ~MVPP2_GMAC_MAX_RX_SIZE_MASK;
4320 val |= (((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
4321 MVPP2_GMAC_MAX_RX_SIZE_OFFS);
4322 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4323}
4324
4325/* Set defaults to the MVPP2 port */
4326static void mvpp2_defaults_set(struct mvpp2_port *port)
4327{
4328 int tx_port_num, val, queue, ptxq, lrxq;
4329
Thomas Petazzoni3d9017d2017-03-07 16:53:11 +01004330 if (port->priv->hw_version == MVPP21) {
4331 /* Configure port to loopback if needed */
4332 if (port->flags & MVPP2_F_LOOPBACK)
4333 mvpp2_port_loopback_set(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004334
Thomas Petazzoni3d9017d2017-03-07 16:53:11 +01004335 /* Update TX FIFO MIN Threshold */
4336 val = readl(port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
4337 val &= ~MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK;
4338 /* Min. TX threshold must be less than minimal packet length */
4339 val |= MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(64 - 4 - 2);
4340 writel(val, port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
4341 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004342
4343 /* Disable Legacy WRR, Disable EJP, Release from reset */
4344 tx_port_num = mvpp2_egress_port(port);
4345 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG,
4346 tx_port_num);
4347 mvpp2_write(port->priv, MVPP2_TXP_SCHED_CMD_1_REG, 0);
4348
4349 /* Close bandwidth for all queues */
4350 for (queue = 0; queue < MVPP2_MAX_TXQ; queue++) {
4351 ptxq = mvpp2_txq_phys(port->id, queue);
4352 mvpp2_write(port->priv,
4353 MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(ptxq), 0);
4354 }
4355
4356 /* Set refill period to 1 usec, refill tokens
4357 * and bucket size to maximum
4358 */
4359 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PERIOD_REG,
4360 port->priv->tclk / USEC_PER_SEC);
4361 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_REFILL_REG);
4362 val &= ~MVPP2_TXP_REFILL_PERIOD_ALL_MASK;
4363 val |= MVPP2_TXP_REFILL_PERIOD_MASK(1);
4364 val |= MVPP2_TXP_REFILL_TOKENS_ALL_MASK;
4365 mvpp2_write(port->priv, MVPP2_TXP_SCHED_REFILL_REG, val);
4366 val = MVPP2_TXP_TOKEN_SIZE_MAX;
4367 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
4368
4369 /* Set MaximumLowLatencyPacketSize value to 256 */
4370 mvpp2_write(port->priv, MVPP2_RX_CTRL_REG(port->id),
4371 MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK |
4372 MVPP2_RX_LOW_LATENCY_PKT_SIZE(256));
4373
4374 /* Enable Rx cache snoop */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004375 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004376 queue = port->rxqs[lrxq]->id;
4377 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
4378 val |= MVPP2_SNOOP_PKT_SIZE_MASK |
4379 MVPP2_SNOOP_BUF_HDR_MASK;
4380 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
4381 }
4382
4383 /* At default, mask all interrupts to all present cpus */
4384 mvpp2_interrupts_disable(port);
4385}
4386
4387/* Enable/disable receiving packets */
4388static void mvpp2_ingress_enable(struct mvpp2_port *port)
4389{
4390 u32 val;
4391 int lrxq, queue;
4392
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004393 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004394 queue = port->rxqs[lrxq]->id;
4395 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
4396 val &= ~MVPP2_RXQ_DISABLE_MASK;
4397 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
4398 }
4399}
4400
4401static void mvpp2_ingress_disable(struct mvpp2_port *port)
4402{
4403 u32 val;
4404 int lrxq, queue;
4405
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004406 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004407 queue = port->rxqs[lrxq]->id;
4408 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
4409 val |= MVPP2_RXQ_DISABLE_MASK;
4410 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
4411 }
4412}
4413
4414/* Enable transmit via physical egress queue
4415 * - HW starts take descriptors from DRAM
4416 */
4417static void mvpp2_egress_enable(struct mvpp2_port *port)
4418{
4419 u32 qmap;
4420 int queue;
4421 int tx_port_num = mvpp2_egress_port(port);
4422
4423 /* Enable all initialized TXs. */
4424 qmap = 0;
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004425 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004426 struct mvpp2_tx_queue *txq = port->txqs[queue];
4427
Markus Elfringdbbb2f02017-04-17 14:07:52 +02004428 if (txq->descs)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004429 qmap |= (1 << queue);
4430 }
4431
4432 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
4433 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG, qmap);
4434}
4435
4436/* Disable transmit via physical egress queue
4437 * - HW doesn't take descriptors from DRAM
4438 */
4439static void mvpp2_egress_disable(struct mvpp2_port *port)
4440{
4441 u32 reg_data;
4442 int delay;
4443 int tx_port_num = mvpp2_egress_port(port);
4444
4445 /* Issue stop command for active channels only */
4446 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
4447 reg_data = (mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG)) &
4448 MVPP2_TXP_SCHED_ENQ_MASK;
4449 if (reg_data != 0)
4450 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG,
4451 (reg_data << MVPP2_TXP_SCHED_DISQ_OFFSET));
4452
4453 /* Wait for all Tx activity to terminate. */
4454 delay = 0;
4455 do {
4456 if (delay >= MVPP2_TX_DISABLE_TIMEOUT_MSEC) {
4457 netdev_warn(port->dev,
4458 "Tx stop timed out, status=0x%08x\n",
4459 reg_data);
4460 break;
4461 }
4462 mdelay(1);
4463 delay++;
4464
4465 /* Check port TX Command register that all
4466 * Tx queues are stopped
4467 */
4468 reg_data = mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG);
4469 } while (reg_data & MVPP2_TXP_SCHED_ENQ_MASK);
4470}
4471
4472/* Rx descriptors helper methods */
4473
4474/* Get number of Rx descriptors occupied by received packets */
4475static inline int
4476mvpp2_rxq_received(struct mvpp2_port *port, int rxq_id)
4477{
4478 u32 val = mvpp2_read(port->priv, MVPP2_RXQ_STATUS_REG(rxq_id));
4479
4480 return val & MVPP2_RXQ_OCCUPIED_MASK;
4481}
4482
4483/* Update Rx queue status with the number of occupied and available
4484 * Rx descriptor slots.
4485 */
4486static inline void
4487mvpp2_rxq_status_update(struct mvpp2_port *port, int rxq_id,
4488 int used_count, int free_count)
4489{
4490 /* Decrement the number of used descriptors and increment count
4491 * increment the number of free descriptors.
4492 */
4493 u32 val = used_count | (free_count << MVPP2_RXQ_NUM_NEW_OFFSET);
4494
4495 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_UPDATE_REG(rxq_id), val);
4496}
4497
4498/* Get pointer to next RX descriptor to be processed by SW */
4499static inline struct mvpp2_rx_desc *
4500mvpp2_rxq_next_desc_get(struct mvpp2_rx_queue *rxq)
4501{
4502 int rx_desc = rxq->next_desc_to_proc;
4503
4504 rxq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(rxq, rx_desc);
4505 prefetch(rxq->descs + rxq->next_desc_to_proc);
4506 return rxq->descs + rx_desc;
4507}
4508
4509/* Set rx queue offset */
4510static void mvpp2_rxq_offset_set(struct mvpp2_port *port,
4511 int prxq, int offset)
4512{
4513 u32 val;
4514
4515 /* Convert offset from bytes to units of 32 bytes */
4516 offset = offset >> 5;
4517
4518 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
4519 val &= ~MVPP2_RXQ_PACKET_OFFSET_MASK;
4520
4521 /* Offset is in */
4522 val |= ((offset << MVPP2_RXQ_PACKET_OFFSET_OFFS) &
4523 MVPP2_RXQ_PACKET_OFFSET_MASK);
4524
4525 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
4526}
4527
Marcin Wojtas3f518502014-07-10 16:52:13 -03004528/* Tx descriptors helper methods */
4529
Marcin Wojtas3f518502014-07-10 16:52:13 -03004530/* Get pointer to next Tx descriptor to be processed (send) by HW */
4531static struct mvpp2_tx_desc *
4532mvpp2_txq_next_desc_get(struct mvpp2_tx_queue *txq)
4533{
4534 int tx_desc = txq->next_desc_to_proc;
4535
4536 txq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(txq, tx_desc);
4537 return txq->descs + tx_desc;
4538}
4539
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004540/* Update HW with number of aggregated Tx descriptors to be sent
4541 *
4542 * Called only from mvpp2_tx(), so migration is disabled, using
4543 * smp_processor_id() is OK.
4544 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004545static void mvpp2_aggr_txq_pend_desc_add(struct mvpp2_port *port, int pending)
4546{
4547 /* aggregated access - relevant TXQ number is written in TX desc */
Thomas Petazzonia7868412017-03-07 16:53:13 +01004548 mvpp2_percpu_write(port->priv, smp_processor_id(),
4549 MVPP2_AGGR_TXQ_UPDATE_REG, pending);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004550}
4551
4552
4553/* Check if there are enough free descriptors in aggregated txq.
4554 * If not, update the number of occupied descriptors and repeat the check.
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004555 *
4556 * Called only from mvpp2_tx(), so migration is disabled, using
4557 * smp_processor_id() is OK.
Marcin Wojtas3f518502014-07-10 16:52:13 -03004558 */
4559static int mvpp2_aggr_desc_num_check(struct mvpp2 *priv,
4560 struct mvpp2_tx_queue *aggr_txq, int num)
4561{
4562 if ((aggr_txq->count + num) > aggr_txq->size) {
4563 /* Update number of occupied aggregated Tx descriptors */
4564 int cpu = smp_processor_id();
4565 u32 val = mvpp2_read(priv, MVPP2_AGGR_TXQ_STATUS_REG(cpu));
4566
4567 aggr_txq->count = val & MVPP2_AGGR_TXQ_PENDING_MASK;
4568 }
4569
4570 if ((aggr_txq->count + num) > aggr_txq->size)
4571 return -ENOMEM;
4572
4573 return 0;
4574}
4575
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004576/* Reserved Tx descriptors allocation request
4577 *
4578 * Called only from mvpp2_txq_reserved_desc_num_proc(), itself called
4579 * only by mvpp2_tx(), so migration is disabled, using
4580 * smp_processor_id() is OK.
4581 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004582static int mvpp2_txq_alloc_reserved_desc(struct mvpp2 *priv,
4583 struct mvpp2_tx_queue *txq, int num)
4584{
4585 u32 val;
Thomas Petazzonia7868412017-03-07 16:53:13 +01004586 int cpu = smp_processor_id();
Marcin Wojtas3f518502014-07-10 16:52:13 -03004587
4588 val = (txq->id << MVPP2_TXQ_RSVD_REQ_Q_OFFSET) | num;
Thomas Petazzonia7868412017-03-07 16:53:13 +01004589 mvpp2_percpu_write(priv, cpu, MVPP2_TXQ_RSVD_REQ_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004590
Thomas Petazzonia7868412017-03-07 16:53:13 +01004591 val = mvpp2_percpu_read(priv, cpu, MVPP2_TXQ_RSVD_RSLT_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004592
4593 return val & MVPP2_TXQ_RSVD_RSLT_MASK;
4594}
4595
4596/* Check if there are enough reserved descriptors for transmission.
4597 * If not, request chunk of reserved descriptors and check again.
4598 */
4599static int mvpp2_txq_reserved_desc_num_proc(struct mvpp2 *priv,
4600 struct mvpp2_tx_queue *txq,
4601 struct mvpp2_txq_pcpu *txq_pcpu,
4602 int num)
4603{
4604 int req, cpu, desc_count;
4605
4606 if (txq_pcpu->reserved_num >= num)
4607 return 0;
4608
4609 /* Not enough descriptors reserved! Update the reserved descriptor
4610 * count and check again.
4611 */
4612
4613 desc_count = 0;
4614 /* Compute total of used descriptors */
4615 for_each_present_cpu(cpu) {
4616 struct mvpp2_txq_pcpu *txq_pcpu_aux;
4617
4618 txq_pcpu_aux = per_cpu_ptr(txq->pcpu, cpu);
4619 desc_count += txq_pcpu_aux->count;
4620 desc_count += txq_pcpu_aux->reserved_num;
4621 }
4622
4623 req = max(MVPP2_CPU_DESC_CHUNK, num - txq_pcpu->reserved_num);
4624 desc_count += req;
4625
4626 if (desc_count >
4627 (txq->size - (num_present_cpus() * MVPP2_CPU_DESC_CHUNK)))
4628 return -ENOMEM;
4629
4630 txq_pcpu->reserved_num += mvpp2_txq_alloc_reserved_desc(priv, txq, req);
4631
4632 /* OK, the descriptor cound has been updated: check again. */
4633 if (txq_pcpu->reserved_num < num)
4634 return -ENOMEM;
4635 return 0;
4636}
4637
4638/* Release the last allocated Tx descriptor. Useful to handle DMA
4639 * mapping failures in the Tx path.
4640 */
4641static void mvpp2_txq_desc_put(struct mvpp2_tx_queue *txq)
4642{
4643 if (txq->next_desc_to_proc == 0)
4644 txq->next_desc_to_proc = txq->last_desc - 1;
4645 else
4646 txq->next_desc_to_proc--;
4647}
4648
4649/* Set Tx descriptors fields relevant for CSUM calculation */
4650static u32 mvpp2_txq_desc_csum(int l3_offs, int l3_proto,
4651 int ip_hdr_len, int l4_proto)
4652{
4653 u32 command;
4654
4655 /* fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
4656 * G_L4_chk, L4_type required only for checksum calculation
4657 */
4658 command = (l3_offs << MVPP2_TXD_L3_OFF_SHIFT);
4659 command |= (ip_hdr_len << MVPP2_TXD_IP_HLEN_SHIFT);
4660 command |= MVPP2_TXD_IP_CSUM_DISABLE;
4661
4662 if (l3_proto == swab16(ETH_P_IP)) {
4663 command &= ~MVPP2_TXD_IP_CSUM_DISABLE; /* enable IPv4 csum */
4664 command &= ~MVPP2_TXD_L3_IP6; /* enable IPv4 */
4665 } else {
4666 command |= MVPP2_TXD_L3_IP6; /* enable IPv6 */
4667 }
4668
4669 if (l4_proto == IPPROTO_TCP) {
4670 command &= ~MVPP2_TXD_L4_UDP; /* enable TCP */
4671 command &= ~MVPP2_TXD_L4_CSUM_FRAG; /* generate L4 csum */
4672 } else if (l4_proto == IPPROTO_UDP) {
4673 command |= MVPP2_TXD_L4_UDP; /* enable UDP */
4674 command &= ~MVPP2_TXD_L4_CSUM_FRAG; /* generate L4 csum */
4675 } else {
4676 command |= MVPP2_TXD_L4_CSUM_NOT;
4677 }
4678
4679 return command;
4680}
4681
4682/* Get number of sent descriptors and decrement counter.
4683 * The number of sent descriptors is returned.
4684 * Per-CPU access
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004685 *
4686 * Called only from mvpp2_txq_done(), called from mvpp2_tx()
4687 * (migration disabled) and from the TX completion tasklet (migration
4688 * disabled) so using smp_processor_id() is OK.
Marcin Wojtas3f518502014-07-10 16:52:13 -03004689 */
4690static inline int mvpp2_txq_sent_desc_proc(struct mvpp2_port *port,
4691 struct mvpp2_tx_queue *txq)
4692{
4693 u32 val;
4694
4695 /* Reading status reg resets transmitted descriptor counter */
Thomas Petazzonia7868412017-03-07 16:53:13 +01004696 val = mvpp2_percpu_read(port->priv, smp_processor_id(),
4697 MVPP2_TXQ_SENT_REG(txq->id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004698
4699 return (val & MVPP2_TRANSMITTED_COUNT_MASK) >>
4700 MVPP2_TRANSMITTED_COUNT_OFFSET;
4701}
4702
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004703/* Called through on_each_cpu(), so runs on all CPUs, with migration
4704 * disabled, therefore using smp_processor_id() is OK.
4705 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004706static void mvpp2_txq_sent_counter_clear(void *arg)
4707{
4708 struct mvpp2_port *port = arg;
4709 int queue;
4710
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004711 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004712 int id = port->txqs[queue]->id;
4713
Thomas Petazzonia7868412017-03-07 16:53:13 +01004714 mvpp2_percpu_read(port->priv, smp_processor_id(),
4715 MVPP2_TXQ_SENT_REG(id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004716 }
4717}
4718
4719/* Set max sizes for Tx queues */
4720static void mvpp2_txp_max_tx_size_set(struct mvpp2_port *port)
4721{
4722 u32 val, size, mtu;
4723 int txq, tx_port_num;
4724
4725 mtu = port->pkt_size * 8;
4726 if (mtu > MVPP2_TXP_MTU_MAX)
4727 mtu = MVPP2_TXP_MTU_MAX;
4728
4729 /* WA for wrong Token bucket update: Set MTU value = 3*real MTU value */
4730 mtu = 3 * mtu;
4731
4732 /* Indirect access to registers */
4733 tx_port_num = mvpp2_egress_port(port);
4734 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
4735
4736 /* Set MTU */
4737 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_MTU_REG);
4738 val &= ~MVPP2_TXP_MTU_MAX;
4739 val |= mtu;
4740 mvpp2_write(port->priv, MVPP2_TXP_SCHED_MTU_REG, val);
4741
4742 /* TXP token size and all TXQs token size must be larger that MTU */
4743 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG);
4744 size = val & MVPP2_TXP_TOKEN_SIZE_MAX;
4745 if (size < mtu) {
4746 size = mtu;
4747 val &= ~MVPP2_TXP_TOKEN_SIZE_MAX;
4748 val |= size;
4749 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
4750 }
4751
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004752 for (txq = 0; txq < port->ntxqs; txq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004753 val = mvpp2_read(port->priv,
4754 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq));
4755 size = val & MVPP2_TXQ_TOKEN_SIZE_MAX;
4756
4757 if (size < mtu) {
4758 size = mtu;
4759 val &= ~MVPP2_TXQ_TOKEN_SIZE_MAX;
4760 val |= size;
4761 mvpp2_write(port->priv,
4762 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq),
4763 val);
4764 }
4765 }
4766}
4767
4768/* Set the number of packets that will be received before Rx interrupt
4769 * will be generated by HW.
4770 */
4771static void mvpp2_rx_pkts_coal_set(struct mvpp2_port *port,
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01004772 struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004773{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004774 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01004775
Thomas Petazzonif8b0d5f2017-02-21 11:28:03 +01004776 if (rxq->pkts_coal > MVPP2_OCCUPIED_THRESH_MASK)
4777 rxq->pkts_coal = MVPP2_OCCUPIED_THRESH_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004778
Thomas Petazzonia7868412017-03-07 16:53:13 +01004779 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
4780 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_THRESH_REG,
4781 rxq->pkts_coal);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004782
4783 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03004784}
4785
Thomas Petazzoniab426762017-02-21 11:28:04 +01004786static u32 mvpp2_usec_to_cycles(u32 usec, unsigned long clk_hz)
4787{
4788 u64 tmp = (u64)clk_hz * usec;
4789
4790 do_div(tmp, USEC_PER_SEC);
4791
4792 return tmp > U32_MAX ? U32_MAX : tmp;
4793}
4794
4795static u32 mvpp2_cycles_to_usec(u32 cycles, unsigned long clk_hz)
4796{
4797 u64 tmp = (u64)cycles * USEC_PER_SEC;
4798
4799 do_div(tmp, clk_hz);
4800
4801 return tmp > U32_MAX ? U32_MAX : tmp;
4802}
4803
Marcin Wojtas3f518502014-07-10 16:52:13 -03004804/* Set the time delay in usec before Rx interrupt */
4805static void mvpp2_rx_time_coal_set(struct mvpp2_port *port,
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01004806 struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004807{
Thomas Petazzoniab426762017-02-21 11:28:04 +01004808 unsigned long freq = port->priv->tclk;
4809 u32 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004810
Thomas Petazzoniab426762017-02-21 11:28:04 +01004811 if (val > MVPP2_MAX_ISR_RX_THRESHOLD) {
4812 rxq->time_coal =
4813 mvpp2_cycles_to_usec(MVPP2_MAX_ISR_RX_THRESHOLD, freq);
4814
4815 /* re-evaluate to get actual register value */
4816 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
4817 }
4818
Marcin Wojtas3f518502014-07-10 16:52:13 -03004819 mvpp2_write(port->priv, MVPP2_ISR_RX_THRESHOLD_REG(rxq->id), val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004820}
4821
Marcin Wojtas3f518502014-07-10 16:52:13 -03004822/* Free Tx queue skbuffs */
4823static void mvpp2_txq_bufs_free(struct mvpp2_port *port,
4824 struct mvpp2_tx_queue *txq,
4825 struct mvpp2_txq_pcpu *txq_pcpu, int num)
4826{
4827 int i;
4828
4829 for (i = 0; i < num; i++) {
Thomas Petazzoni83544912016-12-21 11:28:49 +01004830 struct mvpp2_txq_pcpu_buf *tx_buf =
4831 txq_pcpu->buffs + txq_pcpu->txq_get_index;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004832
Thomas Petazzoni20396132017-03-07 16:53:00 +01004833 dma_unmap_single(port->dev->dev.parent, tx_buf->dma,
Thomas Petazzoni83544912016-12-21 11:28:49 +01004834 tx_buf->size, DMA_TO_DEVICE);
Thomas Petazzoni36fb7432017-02-21 11:28:05 +01004835 if (tx_buf->skb)
4836 dev_kfree_skb_any(tx_buf->skb);
4837
4838 mvpp2_txq_inc_get(txq_pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004839 }
4840}
4841
4842static inline struct mvpp2_rx_queue *mvpp2_get_rx_queue(struct mvpp2_port *port,
4843 u32 cause)
4844{
4845 int queue = fls(cause) - 1;
4846
4847 return port->rxqs[queue];
4848}
4849
4850static inline struct mvpp2_tx_queue *mvpp2_get_tx_queue(struct mvpp2_port *port,
4851 u32 cause)
4852{
Marcin Wojtasedc660f2015-08-06 19:00:30 +02004853 int queue = fls(cause) - 1;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004854
4855 return port->txqs[queue];
4856}
4857
4858/* Handle end of transmission */
4859static void mvpp2_txq_done(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
4860 struct mvpp2_txq_pcpu *txq_pcpu)
4861{
4862 struct netdev_queue *nq = netdev_get_tx_queue(port->dev, txq->log_id);
4863 int tx_done;
4864
4865 if (txq_pcpu->cpu != smp_processor_id())
4866 netdev_err(port->dev, "wrong cpu on the end of Tx processing\n");
4867
4868 tx_done = mvpp2_txq_sent_desc_proc(port, txq);
4869 if (!tx_done)
4870 return;
4871 mvpp2_txq_bufs_free(port, txq, txq_pcpu, tx_done);
4872
4873 txq_pcpu->count -= tx_done;
4874
4875 if (netif_tx_queue_stopped(nq))
4876 if (txq_pcpu->size - txq_pcpu->count >= MAX_SKB_FRAGS + 1)
4877 netif_tx_wake_queue(nq);
4878}
4879
Marcin Wojtasedc660f2015-08-06 19:00:30 +02004880static unsigned int mvpp2_tx_done(struct mvpp2_port *port, u32 cause)
4881{
4882 struct mvpp2_tx_queue *txq;
4883 struct mvpp2_txq_pcpu *txq_pcpu;
4884 unsigned int tx_todo = 0;
4885
4886 while (cause) {
4887 txq = mvpp2_get_tx_queue(port, cause);
4888 if (!txq)
4889 break;
4890
4891 txq_pcpu = this_cpu_ptr(txq->pcpu);
4892
4893 if (txq_pcpu->count) {
4894 mvpp2_txq_done(port, txq, txq_pcpu);
4895 tx_todo += txq_pcpu->count;
4896 }
4897
4898 cause &= ~(1 << txq->log_id);
4899 }
4900 return tx_todo;
4901}
4902
Marcin Wojtas3f518502014-07-10 16:52:13 -03004903/* Rx/Tx queue initialization/cleanup methods */
4904
4905/* Allocate and initialize descriptors for aggr TXQ */
4906static int mvpp2_aggr_txq_init(struct platform_device *pdev,
4907 struct mvpp2_tx_queue *aggr_txq,
4908 int desc_num, int cpu,
4909 struct mvpp2 *priv)
4910{
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01004911 u32 txq_dma;
4912
Marcin Wojtas3f518502014-07-10 16:52:13 -03004913 /* Allocate memory for TX descriptors */
4914 aggr_txq->descs = dma_alloc_coherent(&pdev->dev,
4915 desc_num * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004916 &aggr_txq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004917 if (!aggr_txq->descs)
4918 return -ENOMEM;
4919
Marcin Wojtas3f518502014-07-10 16:52:13 -03004920 aggr_txq->last_desc = aggr_txq->size - 1;
4921
4922 /* Aggr TXQ no reset WA */
4923 aggr_txq->next_desc_to_proc = mvpp2_read(priv,
4924 MVPP2_AGGR_TXQ_INDEX_REG(cpu));
4925
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01004926 /* Set Tx descriptors queue starting address indirect
4927 * access
4928 */
4929 if (priv->hw_version == MVPP21)
4930 txq_dma = aggr_txq->descs_dma;
4931 else
4932 txq_dma = aggr_txq->descs_dma >>
4933 MVPP22_AGGR_TXQ_DESC_ADDR_OFFS;
4934
4935 mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu), txq_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004936 mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu), desc_num);
4937
4938 return 0;
4939}
4940
4941/* Create a specified Rx queue */
4942static int mvpp2_rxq_init(struct mvpp2_port *port,
4943 struct mvpp2_rx_queue *rxq)
4944
4945{
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01004946 u32 rxq_dma;
Thomas Petazzonia7868412017-03-07 16:53:13 +01004947 int cpu;
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01004948
Marcin Wojtas3f518502014-07-10 16:52:13 -03004949 rxq->size = port->rx_ring_size;
4950
4951 /* Allocate memory for RX descriptors */
4952 rxq->descs = dma_alloc_coherent(port->dev->dev.parent,
4953 rxq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004954 &rxq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004955 if (!rxq->descs)
4956 return -ENOMEM;
4957
Marcin Wojtas3f518502014-07-10 16:52:13 -03004958 rxq->last_desc = rxq->size - 1;
4959
4960 /* Zero occupied and non-occupied counters - direct access */
4961 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
4962
4963 /* Set Rx descriptors queue starting address - indirect access */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004964 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01004965 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01004966 if (port->priv->hw_version == MVPP21)
4967 rxq_dma = rxq->descs_dma;
4968 else
4969 rxq_dma = rxq->descs_dma >> MVPP22_DESC_ADDR_OFFS;
Thomas Petazzonia7868412017-03-07 16:53:13 +01004970 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_ADDR_REG, rxq_dma);
4971 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_SIZE_REG, rxq->size);
4972 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_INDEX_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004973 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03004974
4975 /* Set Offset */
4976 mvpp2_rxq_offset_set(port, rxq->id, NET_SKB_PAD);
4977
4978 /* Set coalescing pkts and time */
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01004979 mvpp2_rx_pkts_coal_set(port, rxq);
4980 mvpp2_rx_time_coal_set(port, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004981
4982 /* Add number of descriptors ready for receiving packets */
4983 mvpp2_rxq_status_update(port, rxq->id, 0, rxq->size);
4984
4985 return 0;
4986}
4987
4988/* Push packets received by the RXQ to BM pool */
4989static void mvpp2_rxq_drop_pkts(struct mvpp2_port *port,
4990 struct mvpp2_rx_queue *rxq)
4991{
4992 int rx_received, i;
4993
4994 rx_received = mvpp2_rxq_received(port, rxq->id);
4995 if (!rx_received)
4996 return;
4997
4998 for (i = 0; i < rx_received; i++) {
4999 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005000 u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
5001 int pool;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005002
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005003 pool = (status & MVPP2_RXD_BM_POOL_ID_MASK) >>
5004 MVPP2_RXD_BM_POOL_ID_OFFS;
5005
Thomas Petazzoni7d7627b2017-06-22 14:23:20 +02005006 mvpp2_bm_pool_put(port, pool,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005007 mvpp2_rxdesc_dma_addr_get(port, rx_desc),
5008 mvpp2_rxdesc_cookie_get(port, rx_desc));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005009 }
5010 mvpp2_rxq_status_update(port, rxq->id, rx_received, rx_received);
5011}
5012
5013/* Cleanup Rx queue */
5014static void mvpp2_rxq_deinit(struct mvpp2_port *port,
5015 struct mvpp2_rx_queue *rxq)
5016{
Thomas Petazzonia7868412017-03-07 16:53:13 +01005017 int cpu;
5018
Marcin Wojtas3f518502014-07-10 16:52:13 -03005019 mvpp2_rxq_drop_pkts(port, rxq);
5020
5021 if (rxq->descs)
5022 dma_free_coherent(port->dev->dev.parent,
5023 rxq->size * MVPP2_DESC_ALIGNED_SIZE,
5024 rxq->descs,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005025 rxq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005026
5027 rxq->descs = NULL;
5028 rxq->last_desc = 0;
5029 rxq->next_desc_to_proc = 0;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005030 rxq->descs_dma = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005031
5032 /* Clear Rx descriptors queue starting address and size;
5033 * free descriptor number
5034 */
5035 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005036 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005037 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
5038 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_ADDR_REG, 0);
5039 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_SIZE_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005040 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005041}
5042
5043/* Create and initialize a Tx queue */
5044static int mvpp2_txq_init(struct mvpp2_port *port,
5045 struct mvpp2_tx_queue *txq)
5046{
5047 u32 val;
5048 int cpu, desc, desc_per_txq, tx_port_num;
5049 struct mvpp2_txq_pcpu *txq_pcpu;
5050
5051 txq->size = port->tx_ring_size;
5052
5053 /* Allocate memory for Tx descriptors */
5054 txq->descs = dma_alloc_coherent(port->dev->dev.parent,
5055 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005056 &txq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005057 if (!txq->descs)
5058 return -ENOMEM;
5059
Marcin Wojtas3f518502014-07-10 16:52:13 -03005060 txq->last_desc = txq->size - 1;
5061
5062 /* Set Tx descriptors queue starting address - indirect access */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005063 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005064 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5065 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_ADDR_REG,
5066 txq->descs_dma);
5067 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_SIZE_REG,
5068 txq->size & MVPP2_TXQ_DESC_SIZE_MASK);
5069 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_INDEX_REG, 0);
5070 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_RSVD_CLR_REG,
5071 txq->id << MVPP2_TXQ_RSVD_CLR_OFFSET);
5072 val = mvpp2_percpu_read(port->priv, cpu, MVPP2_TXQ_PENDING_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005073 val &= ~MVPP2_TXQ_PENDING_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005074 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PENDING_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005075
5076 /* Calculate base address in prefetch buffer. We reserve 16 descriptors
5077 * for each existing TXQ.
5078 * TCONTS for PON port must be continuous from 0 to MVPP2_MAX_TCONT
5079 * GBE ports assumed to be continious from 0 to MVPP2_MAX_PORTS
5080 */
5081 desc_per_txq = 16;
5082 desc = (port->id * MVPP2_MAX_TXQ * desc_per_txq) +
5083 (txq->log_id * desc_per_txq);
5084
Thomas Petazzonia7868412017-03-07 16:53:13 +01005085 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG,
5086 MVPP2_PREF_BUF_PTR(desc) | MVPP2_PREF_BUF_SIZE_16 |
5087 MVPP2_PREF_BUF_THRESH(desc_per_txq / 2));
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005088 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005089
5090 /* WRR / EJP configuration - indirect access */
5091 tx_port_num = mvpp2_egress_port(port);
5092 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
5093
5094 val = mvpp2_read(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id));
5095 val &= ~MVPP2_TXQ_REFILL_PERIOD_ALL_MASK;
5096 val |= MVPP2_TXQ_REFILL_PERIOD_MASK(1);
5097 val |= MVPP2_TXQ_REFILL_TOKENS_ALL_MASK;
5098 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id), val);
5099
5100 val = MVPP2_TXQ_TOKEN_SIZE_MAX;
5101 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq->log_id),
5102 val);
5103
5104 for_each_present_cpu(cpu) {
5105 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
5106 txq_pcpu->size = txq->size;
Markus Elfring02c91ec2017-04-17 08:09:07 +02005107 txq_pcpu->buffs = kmalloc_array(txq_pcpu->size,
5108 sizeof(*txq_pcpu->buffs),
5109 GFP_KERNEL);
Thomas Petazzoni83544912016-12-21 11:28:49 +01005110 if (!txq_pcpu->buffs)
Markus Elfring20b1e162017-04-17 12:58:33 +02005111 goto cleanup;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005112
5113 txq_pcpu->count = 0;
5114 txq_pcpu->reserved_num = 0;
5115 txq_pcpu->txq_put_index = 0;
5116 txq_pcpu->txq_get_index = 0;
5117 }
5118
5119 return 0;
Markus Elfring20b1e162017-04-17 12:58:33 +02005120cleanup:
Marcin Wojtas71ce3912015-08-06 19:00:29 +02005121 for_each_present_cpu(cpu) {
5122 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Thomas Petazzoni83544912016-12-21 11:28:49 +01005123 kfree(txq_pcpu->buffs);
Marcin Wojtas71ce3912015-08-06 19:00:29 +02005124 }
5125
5126 dma_free_coherent(port->dev->dev.parent,
5127 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005128 txq->descs, txq->descs_dma);
Marcin Wojtas71ce3912015-08-06 19:00:29 +02005129
5130 return -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005131}
5132
5133/* Free allocated TXQ resources */
5134static void mvpp2_txq_deinit(struct mvpp2_port *port,
5135 struct mvpp2_tx_queue *txq)
5136{
5137 struct mvpp2_txq_pcpu *txq_pcpu;
5138 int cpu;
5139
5140 for_each_present_cpu(cpu) {
5141 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Thomas Petazzoni83544912016-12-21 11:28:49 +01005142 kfree(txq_pcpu->buffs);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005143 }
5144
5145 if (txq->descs)
5146 dma_free_coherent(port->dev->dev.parent,
5147 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005148 txq->descs, txq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005149
5150 txq->descs = NULL;
5151 txq->last_desc = 0;
5152 txq->next_desc_to_proc = 0;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005153 txq->descs_dma = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005154
5155 /* Set minimum bandwidth for disabled TXQs */
5156 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(txq->id), 0);
5157
5158 /* Set Tx descriptors queue starting address and size */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005159 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005160 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5161 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_ADDR_REG, 0);
5162 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_SIZE_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005163 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005164}
5165
5166/* Cleanup Tx ports */
5167static void mvpp2_txq_clean(struct mvpp2_port *port, struct mvpp2_tx_queue *txq)
5168{
5169 struct mvpp2_txq_pcpu *txq_pcpu;
5170 int delay, pending, cpu;
5171 u32 val;
5172
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005173 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005174 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5175 val = mvpp2_percpu_read(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005176 val |= MVPP2_TXQ_DRAIN_EN_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005177 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005178
5179 /* The napi queue has been stopped so wait for all packets
5180 * to be transmitted.
5181 */
5182 delay = 0;
5183 do {
5184 if (delay >= MVPP2_TX_PENDING_TIMEOUT_MSEC) {
5185 netdev_warn(port->dev,
5186 "port %d: cleaning queue %d timed out\n",
5187 port->id, txq->log_id);
5188 break;
5189 }
5190 mdelay(1);
5191 delay++;
5192
Thomas Petazzonia7868412017-03-07 16:53:13 +01005193 pending = mvpp2_percpu_read(port->priv, cpu,
5194 MVPP2_TXQ_PENDING_REG);
5195 pending &= MVPP2_TXQ_PENDING_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005196 } while (pending);
5197
5198 val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005199 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG, val);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005200 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005201
5202 for_each_present_cpu(cpu) {
5203 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
5204
5205 /* Release all packets */
5206 mvpp2_txq_bufs_free(port, txq, txq_pcpu, txq_pcpu->count);
5207
5208 /* Reset queue */
5209 txq_pcpu->count = 0;
5210 txq_pcpu->txq_put_index = 0;
5211 txq_pcpu->txq_get_index = 0;
5212 }
5213}
5214
5215/* Cleanup all Tx queues */
5216static void mvpp2_cleanup_txqs(struct mvpp2_port *port)
5217{
5218 struct mvpp2_tx_queue *txq;
5219 int queue;
5220 u32 val;
5221
5222 val = mvpp2_read(port->priv, MVPP2_TX_PORT_FLUSH_REG);
5223
5224 /* Reset Tx ports and delete Tx queues */
5225 val |= MVPP2_TX_PORT_FLUSH_MASK(port->id);
5226 mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
5227
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005228 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005229 txq = port->txqs[queue];
5230 mvpp2_txq_clean(port, txq);
5231 mvpp2_txq_deinit(port, txq);
5232 }
5233
5234 on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
5235
5236 val &= ~MVPP2_TX_PORT_FLUSH_MASK(port->id);
5237 mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
5238}
5239
5240/* Cleanup all Rx queues */
5241static void mvpp2_cleanup_rxqs(struct mvpp2_port *port)
5242{
5243 int queue;
5244
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005245 for (queue = 0; queue < port->nrxqs; queue++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005246 mvpp2_rxq_deinit(port, port->rxqs[queue]);
5247}
5248
5249/* Init all Rx queues for port */
5250static int mvpp2_setup_rxqs(struct mvpp2_port *port)
5251{
5252 int queue, err;
5253
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005254 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005255 err = mvpp2_rxq_init(port, port->rxqs[queue]);
5256 if (err)
5257 goto err_cleanup;
5258 }
5259 return 0;
5260
5261err_cleanup:
5262 mvpp2_cleanup_rxqs(port);
5263 return err;
5264}
5265
5266/* Init all tx queues for port */
5267static int mvpp2_setup_txqs(struct mvpp2_port *port)
5268{
5269 struct mvpp2_tx_queue *txq;
5270 int queue, err;
5271
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005272 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005273 txq = port->txqs[queue];
5274 err = mvpp2_txq_init(port, txq);
5275 if (err)
5276 goto err_cleanup;
5277 }
5278
Marcin Wojtas3f518502014-07-10 16:52:13 -03005279 on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
5280 return 0;
5281
5282err_cleanup:
5283 mvpp2_cleanup_txqs(port);
5284 return err;
5285}
5286
5287/* The callback for per-port interrupt */
5288static irqreturn_t mvpp2_isr(int irq, void *dev_id)
5289{
5290 struct mvpp2_port *port = (struct mvpp2_port *)dev_id;
5291
5292 mvpp2_interrupts_disable(port);
5293
5294 napi_schedule(&port->napi);
5295
5296 return IRQ_HANDLED;
5297}
5298
5299/* Adjust link */
5300static void mvpp2_link_event(struct net_device *dev)
5301{
5302 struct mvpp2_port *port = netdev_priv(dev);
Philippe Reynes8e072692016-06-28 00:08:11 +02005303 struct phy_device *phydev = dev->phydev;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005304 int status_change = 0;
5305 u32 val;
5306
5307 if (phydev->link) {
5308 if ((port->speed != phydev->speed) ||
5309 (port->duplex != phydev->duplex)) {
5310 u32 val;
5311
5312 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5313 val &= ~(MVPP2_GMAC_CONFIG_MII_SPEED |
5314 MVPP2_GMAC_CONFIG_GMII_SPEED |
5315 MVPP2_GMAC_CONFIG_FULL_DUPLEX |
5316 MVPP2_GMAC_AN_SPEED_EN |
5317 MVPP2_GMAC_AN_DUPLEX_EN);
5318
5319 if (phydev->duplex)
5320 val |= MVPP2_GMAC_CONFIG_FULL_DUPLEX;
5321
5322 if (phydev->speed == SPEED_1000)
5323 val |= MVPP2_GMAC_CONFIG_GMII_SPEED;
Thomas Petazzoni2add5112014-07-27 23:21:35 +02005324 else if (phydev->speed == SPEED_100)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005325 val |= MVPP2_GMAC_CONFIG_MII_SPEED;
5326
5327 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5328
5329 port->duplex = phydev->duplex;
5330 port->speed = phydev->speed;
5331 }
5332 }
5333
5334 if (phydev->link != port->link) {
5335 if (!phydev->link) {
5336 port->duplex = -1;
5337 port->speed = 0;
5338 }
5339
5340 port->link = phydev->link;
5341 status_change = 1;
5342 }
5343
5344 if (status_change) {
5345 if (phydev->link) {
5346 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5347 val |= (MVPP2_GMAC_FORCE_LINK_PASS |
5348 MVPP2_GMAC_FORCE_LINK_DOWN);
5349 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5350 mvpp2_egress_enable(port);
5351 mvpp2_ingress_enable(port);
5352 } else {
5353 mvpp2_ingress_disable(port);
5354 mvpp2_egress_disable(port);
5355 }
5356 phy_print_status(phydev);
5357 }
5358}
5359
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005360static void mvpp2_timer_set(struct mvpp2_port_pcpu *port_pcpu)
5361{
5362 ktime_t interval;
5363
5364 if (!port_pcpu->timer_scheduled) {
5365 port_pcpu->timer_scheduled = true;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01005366 interval = MVPP2_TXDONE_HRTIMER_PERIOD_NS;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005367 hrtimer_start(&port_pcpu->tx_done_timer, interval,
5368 HRTIMER_MODE_REL_PINNED);
5369 }
5370}
5371
5372static void mvpp2_tx_proc_cb(unsigned long data)
5373{
5374 struct net_device *dev = (struct net_device *)data;
5375 struct mvpp2_port *port = netdev_priv(dev);
5376 struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
5377 unsigned int tx_todo, cause;
5378
5379 if (!netif_running(dev))
5380 return;
5381 port_pcpu->timer_scheduled = false;
5382
5383 /* Process all the Tx queues */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005384 cause = (1 << port->ntxqs) - 1;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005385 tx_todo = mvpp2_tx_done(port, cause);
5386
5387 /* Set the timer in case not all the packets were processed */
5388 if (tx_todo)
5389 mvpp2_timer_set(port_pcpu);
5390}
5391
5392static enum hrtimer_restart mvpp2_hr_timer_cb(struct hrtimer *timer)
5393{
5394 struct mvpp2_port_pcpu *port_pcpu = container_of(timer,
5395 struct mvpp2_port_pcpu,
5396 tx_done_timer);
5397
5398 tasklet_schedule(&port_pcpu->tx_done_tasklet);
5399
5400 return HRTIMER_NORESTART;
5401}
5402
Marcin Wojtas3f518502014-07-10 16:52:13 -03005403/* Main RX/TX processing routines */
5404
5405/* Display more error info */
5406static void mvpp2_rx_error(struct mvpp2_port *port,
5407 struct mvpp2_rx_desc *rx_desc)
5408{
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005409 u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
5410 size_t sz = mvpp2_rxdesc_size_get(port, rx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005411
5412 switch (status & MVPP2_RXD_ERR_CODE_MASK) {
5413 case MVPP2_RXD_ERR_CRC:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005414 netdev_err(port->dev, "bad rx status %08x (crc error), size=%zu\n",
5415 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005416 break;
5417 case MVPP2_RXD_ERR_OVERRUN:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005418 netdev_err(port->dev, "bad rx status %08x (overrun error), size=%zu\n",
5419 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005420 break;
5421 case MVPP2_RXD_ERR_RESOURCE:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005422 netdev_err(port->dev, "bad rx status %08x (resource error), size=%zu\n",
5423 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005424 break;
5425 }
5426}
5427
5428/* Handle RX checksum offload */
5429static void mvpp2_rx_csum(struct mvpp2_port *port, u32 status,
5430 struct sk_buff *skb)
5431{
5432 if (((status & MVPP2_RXD_L3_IP4) &&
5433 !(status & MVPP2_RXD_IP4_HEADER_ERR)) ||
5434 (status & MVPP2_RXD_L3_IP6))
5435 if (((status & MVPP2_RXD_L4_UDP) ||
5436 (status & MVPP2_RXD_L4_TCP)) &&
5437 (status & MVPP2_RXD_L4_CSUM_OK)) {
5438 skb->csum = 0;
5439 skb->ip_summed = CHECKSUM_UNNECESSARY;
5440 return;
5441 }
5442
5443 skb->ip_summed = CHECKSUM_NONE;
5444}
5445
5446/* Reuse skb if possible, or allocate a new skb and add it to BM pool */
5447static int mvpp2_rx_refill(struct mvpp2_port *port,
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005448 struct mvpp2_bm_pool *bm_pool, int pool)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005449{
Thomas Petazzoni20396132017-03-07 16:53:00 +01005450 dma_addr_t dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01005451 phys_addr_t phys_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005452 void *buf;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005453
Marcin Wojtas3f518502014-07-10 16:52:13 -03005454 /* No recycle or too many buffers are in use, so allocate a new skb */
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01005455 buf = mvpp2_buf_alloc(port, bm_pool, &dma_addr, &phys_addr,
5456 GFP_ATOMIC);
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005457 if (!buf)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005458 return -ENOMEM;
5459
Thomas Petazzoni7d7627b2017-06-22 14:23:20 +02005460 mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
Thomas Petazzoni7ef7e1d2017-02-21 11:28:07 +01005461
Marcin Wojtas3f518502014-07-10 16:52:13 -03005462 return 0;
5463}
5464
5465/* Handle tx checksum */
5466static u32 mvpp2_skb_tx_csum(struct mvpp2_port *port, struct sk_buff *skb)
5467{
5468 if (skb->ip_summed == CHECKSUM_PARTIAL) {
5469 int ip_hdr_len = 0;
5470 u8 l4_proto;
5471
5472 if (skb->protocol == htons(ETH_P_IP)) {
5473 struct iphdr *ip4h = ip_hdr(skb);
5474
5475 /* Calculate IPv4 checksum and L4 checksum */
5476 ip_hdr_len = ip4h->ihl;
5477 l4_proto = ip4h->protocol;
5478 } else if (skb->protocol == htons(ETH_P_IPV6)) {
5479 struct ipv6hdr *ip6h = ipv6_hdr(skb);
5480
5481 /* Read l4_protocol from one of IPv6 extra headers */
5482 if (skb_network_header_len(skb) > 0)
5483 ip_hdr_len = (skb_network_header_len(skb) >> 2);
5484 l4_proto = ip6h->nexthdr;
5485 } else {
5486 return MVPP2_TXD_L4_CSUM_NOT;
5487 }
5488
5489 return mvpp2_txq_desc_csum(skb_network_offset(skb),
5490 skb->protocol, ip_hdr_len, l4_proto);
5491 }
5492
5493 return MVPP2_TXD_L4_CSUM_NOT | MVPP2_TXD_IP_CSUM_DISABLE;
5494}
5495
Marcin Wojtas3f518502014-07-10 16:52:13 -03005496/* Main rx processing */
5497static int mvpp2_rx(struct mvpp2_port *port, int rx_todo,
5498 struct mvpp2_rx_queue *rxq)
5499{
5500 struct net_device *dev = port->dev;
Marcin Wojtasb5015852015-12-03 15:20:51 +01005501 int rx_received;
5502 int rx_done = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005503 u32 rcvd_pkts = 0;
5504 u32 rcvd_bytes = 0;
5505
5506 /* Get number of received packets and clamp the to-do */
5507 rx_received = mvpp2_rxq_received(port, rxq->id);
5508 if (rx_todo > rx_received)
5509 rx_todo = rx_received;
5510
Marcin Wojtasb5015852015-12-03 15:20:51 +01005511 while (rx_done < rx_todo) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005512 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
5513 struct mvpp2_bm_pool *bm_pool;
5514 struct sk_buff *skb;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005515 unsigned int frag_size;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005516 dma_addr_t dma_addr;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005517 phys_addr_t phys_addr;
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005518 u32 rx_status;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005519 int pool, rx_bytes, err;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005520 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005521
Marcin Wojtasb5015852015-12-03 15:20:51 +01005522 rx_done++;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005523 rx_status = mvpp2_rxdesc_status_get(port, rx_desc);
5524 rx_bytes = mvpp2_rxdesc_size_get(port, rx_desc);
5525 rx_bytes -= MVPP2_MH_SIZE;
5526 dma_addr = mvpp2_rxdesc_dma_addr_get(port, rx_desc);
5527 phys_addr = mvpp2_rxdesc_cookie_get(port, rx_desc);
5528 data = (void *)phys_to_virt(phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005529
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005530 pool = (rx_status & MVPP2_RXD_BM_POOL_ID_MASK) >>
5531 MVPP2_RXD_BM_POOL_ID_OFFS;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005532 bm_pool = &port->priv->bm_pools[pool];
Marcin Wojtas3f518502014-07-10 16:52:13 -03005533
5534 /* In case of an error, release the requested buffer pointer
5535 * to the Buffer Manager. This request process is controlled
5536 * by the hardware, and the information about the buffer is
5537 * comprised by the RX descriptor.
5538 */
5539 if (rx_status & MVPP2_RXD_ERR_SUMMARY) {
Markus Elfring8a524882017-04-17 10:52:02 +02005540err_drop_frame:
Marcin Wojtas3f518502014-07-10 16:52:13 -03005541 dev->stats.rx_errors++;
5542 mvpp2_rx_error(port, rx_desc);
Marcin Wojtasb5015852015-12-03 15:20:51 +01005543 /* Return the buffer to the pool */
Thomas Petazzoni7d7627b2017-06-22 14:23:20 +02005544 mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005545 continue;
5546 }
5547
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005548 if (bm_pool->frag_size > PAGE_SIZE)
5549 frag_size = 0;
5550 else
5551 frag_size = bm_pool->frag_size;
5552
5553 skb = build_skb(data, frag_size);
5554 if (!skb) {
5555 netdev_warn(port->dev, "skb build failed\n");
5556 goto err_drop_frame;
5557 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03005558
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005559 err = mvpp2_rx_refill(port, bm_pool, pool);
Marcin Wojtasb5015852015-12-03 15:20:51 +01005560 if (err) {
5561 netdev_err(port->dev, "failed to refill BM pools\n");
5562 goto err_drop_frame;
5563 }
5564
Thomas Petazzoni20396132017-03-07 16:53:00 +01005565 dma_unmap_single(dev->dev.parent, dma_addr,
Marcin Wojtas4229d502015-12-03 15:20:50 +01005566 bm_pool->buf_size, DMA_FROM_DEVICE);
5567
Marcin Wojtas3f518502014-07-10 16:52:13 -03005568 rcvd_pkts++;
5569 rcvd_bytes += rx_bytes;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005570
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005571 skb_reserve(skb, MVPP2_MH_SIZE + NET_SKB_PAD);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005572 skb_put(skb, rx_bytes);
5573 skb->protocol = eth_type_trans(skb, dev);
5574 mvpp2_rx_csum(port, rx_status, skb);
5575
5576 napi_gro_receive(&port->napi, skb);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005577 }
5578
5579 if (rcvd_pkts) {
5580 struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
5581
5582 u64_stats_update_begin(&stats->syncp);
5583 stats->rx_packets += rcvd_pkts;
5584 stats->rx_bytes += rcvd_bytes;
5585 u64_stats_update_end(&stats->syncp);
5586 }
5587
5588 /* Update Rx queue management counters */
5589 wmb();
Marcin Wojtasb5015852015-12-03 15:20:51 +01005590 mvpp2_rxq_status_update(port, rxq->id, rx_done, rx_done);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005591
5592 return rx_todo;
5593}
5594
5595static inline void
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005596tx_desc_unmap_put(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
Marcin Wojtas3f518502014-07-10 16:52:13 -03005597 struct mvpp2_tx_desc *desc)
5598{
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005599 dma_addr_t buf_dma_addr =
5600 mvpp2_txdesc_dma_addr_get(port, desc);
5601 size_t buf_sz =
5602 mvpp2_txdesc_size_get(port, desc);
5603 dma_unmap_single(port->dev->dev.parent, buf_dma_addr,
5604 buf_sz, DMA_TO_DEVICE);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005605 mvpp2_txq_desc_put(txq);
5606}
5607
5608/* Handle tx fragmentation processing */
5609static int mvpp2_tx_frag_process(struct mvpp2_port *port, struct sk_buff *skb,
5610 struct mvpp2_tx_queue *aggr_txq,
5611 struct mvpp2_tx_queue *txq)
5612{
5613 struct mvpp2_txq_pcpu *txq_pcpu = this_cpu_ptr(txq->pcpu);
5614 struct mvpp2_tx_desc *tx_desc;
5615 int i;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005616 dma_addr_t buf_dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005617
5618 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
5619 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5620 void *addr = page_address(frag->page.p) + frag->page_offset;
5621
5622 tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005623 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
5624 mvpp2_txdesc_size_set(port, tx_desc, frag->size);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005625
Thomas Petazzoni20396132017-03-07 16:53:00 +01005626 buf_dma_addr = dma_map_single(port->dev->dev.parent, addr,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005627 frag->size,
5628 DMA_TO_DEVICE);
Thomas Petazzoni20396132017-03-07 16:53:00 +01005629 if (dma_mapping_error(port->dev->dev.parent, buf_dma_addr)) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005630 mvpp2_txq_desc_put(txq);
Markus Elfring32bae632017-04-17 11:36:34 +02005631 goto cleanup;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005632 }
5633
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005634 mvpp2_txdesc_offset_set(port, tx_desc,
5635 buf_dma_addr & MVPP2_TX_DESC_ALIGN);
5636 mvpp2_txdesc_dma_addr_set(port, tx_desc,
5637 buf_dma_addr & ~MVPP2_TX_DESC_ALIGN);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005638
5639 if (i == (skb_shinfo(skb)->nr_frags - 1)) {
5640 /* Last descriptor */
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005641 mvpp2_txdesc_cmd_set(port, tx_desc,
5642 MVPP2_TXD_L_DESC);
5643 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005644 } else {
5645 /* Descriptor in the middle: Not First, Not Last */
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005646 mvpp2_txdesc_cmd_set(port, tx_desc, 0);
5647 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005648 }
5649 }
5650
5651 return 0;
Markus Elfring32bae632017-04-17 11:36:34 +02005652cleanup:
Marcin Wojtas3f518502014-07-10 16:52:13 -03005653 /* Release all descriptors that were used to map fragments of
5654 * this packet, as well as the corresponding DMA mappings
5655 */
5656 for (i = i - 1; i >= 0; i--) {
5657 tx_desc = txq->descs + i;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005658 tx_desc_unmap_put(port, txq, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005659 }
5660
5661 return -ENOMEM;
5662}
5663
5664/* Main tx processing */
5665static int mvpp2_tx(struct sk_buff *skb, struct net_device *dev)
5666{
5667 struct mvpp2_port *port = netdev_priv(dev);
5668 struct mvpp2_tx_queue *txq, *aggr_txq;
5669 struct mvpp2_txq_pcpu *txq_pcpu;
5670 struct mvpp2_tx_desc *tx_desc;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005671 dma_addr_t buf_dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005672 int frags = 0;
5673 u16 txq_id;
5674 u32 tx_cmd;
5675
5676 txq_id = skb_get_queue_mapping(skb);
5677 txq = port->txqs[txq_id];
5678 txq_pcpu = this_cpu_ptr(txq->pcpu);
5679 aggr_txq = &port->priv->aggr_txqs[smp_processor_id()];
5680
5681 frags = skb_shinfo(skb)->nr_frags + 1;
5682
5683 /* Check number of available descriptors */
5684 if (mvpp2_aggr_desc_num_check(port->priv, aggr_txq, frags) ||
5685 mvpp2_txq_reserved_desc_num_proc(port->priv, txq,
5686 txq_pcpu, frags)) {
5687 frags = 0;
5688 goto out;
5689 }
5690
5691 /* Get a descriptor for the first part of the packet */
5692 tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005693 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
5694 mvpp2_txdesc_size_set(port, tx_desc, skb_headlen(skb));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005695
Thomas Petazzoni20396132017-03-07 16:53:00 +01005696 buf_dma_addr = dma_map_single(dev->dev.parent, skb->data,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005697 skb_headlen(skb), DMA_TO_DEVICE);
Thomas Petazzoni20396132017-03-07 16:53:00 +01005698 if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005699 mvpp2_txq_desc_put(txq);
5700 frags = 0;
5701 goto out;
5702 }
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005703
5704 mvpp2_txdesc_offset_set(port, tx_desc,
5705 buf_dma_addr & MVPP2_TX_DESC_ALIGN);
5706 mvpp2_txdesc_dma_addr_set(port, tx_desc,
5707 buf_dma_addr & ~MVPP2_TX_DESC_ALIGN);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005708
5709 tx_cmd = mvpp2_skb_tx_csum(port, skb);
5710
5711 if (frags == 1) {
5712 /* First and Last descriptor */
5713 tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_L_DESC;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005714 mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
5715 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005716 } else {
5717 /* First but not Last */
5718 tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_PADDING_DISABLE;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005719 mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
5720 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005721
5722 /* Continue with other skb fragments */
5723 if (mvpp2_tx_frag_process(port, skb, aggr_txq, txq)) {
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005724 tx_desc_unmap_put(port, txq, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005725 frags = 0;
5726 goto out;
5727 }
5728 }
5729
5730 txq_pcpu->reserved_num -= frags;
5731 txq_pcpu->count += frags;
5732 aggr_txq->count += frags;
5733
5734 /* Enable transmit */
5735 wmb();
5736 mvpp2_aggr_txq_pend_desc_add(port, frags);
5737
5738 if (txq_pcpu->size - txq_pcpu->count < MAX_SKB_FRAGS + 1) {
5739 struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
5740
5741 netif_tx_stop_queue(nq);
5742 }
5743out:
5744 if (frags > 0) {
5745 struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
5746
5747 u64_stats_update_begin(&stats->syncp);
5748 stats->tx_packets++;
5749 stats->tx_bytes += skb->len;
5750 u64_stats_update_end(&stats->syncp);
5751 } else {
5752 dev->stats.tx_dropped++;
5753 dev_kfree_skb_any(skb);
5754 }
5755
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005756 /* Finalize TX processing */
5757 if (txq_pcpu->count >= txq->done_pkts_coal)
5758 mvpp2_txq_done(port, txq, txq_pcpu);
5759
5760 /* Set the timer in case not all frags were processed */
5761 if (txq_pcpu->count <= frags && txq_pcpu->count > 0) {
5762 struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
5763
5764 mvpp2_timer_set(port_pcpu);
5765 }
5766
Marcin Wojtas3f518502014-07-10 16:52:13 -03005767 return NETDEV_TX_OK;
5768}
5769
5770static inline void mvpp2_cause_error(struct net_device *dev, int cause)
5771{
5772 if (cause & MVPP2_CAUSE_FCS_ERR_MASK)
5773 netdev_err(dev, "FCS error\n");
5774 if (cause & MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK)
5775 netdev_err(dev, "rx fifo overrun error\n");
5776 if (cause & MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK)
5777 netdev_err(dev, "tx fifo underrun error\n");
5778}
5779
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005780static int mvpp2_poll(struct napi_struct *napi, int budget)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005781{
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005782 u32 cause_rx_tx, cause_rx, cause_misc;
5783 int rx_done = 0;
5784 struct mvpp2_port *port = netdev_priv(napi->dev);
Thomas Petazzonia7868412017-03-07 16:53:13 +01005785 int cpu = smp_processor_id();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005786
5787 /* Rx/Tx cause register
5788 *
5789 * Bits 0-15: each bit indicates received packets on the Rx queue
5790 * (bit 0 is for Rx queue 0).
5791 *
5792 * Bits 16-23: each bit indicates transmitted packets on the Tx queue
5793 * (bit 16 is for Tx queue 0).
5794 *
5795 * Each CPU has its own Rx/Tx cause register
5796 */
Thomas Petazzonia7868412017-03-07 16:53:13 +01005797 cause_rx_tx = mvpp2_percpu_read(port->priv, cpu,
5798 MVPP2_ISR_RX_TX_CAUSE_REG(port->id));
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005799 cause_rx_tx &= ~MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005800 cause_misc = cause_rx_tx & MVPP2_CAUSE_MISC_SUM_MASK;
5801
5802 if (cause_misc) {
5803 mvpp2_cause_error(port->dev, cause_misc);
5804
5805 /* Clear the cause register */
5806 mvpp2_write(port->priv, MVPP2_ISR_MISC_CAUSE_REG, 0);
Thomas Petazzonia7868412017-03-07 16:53:13 +01005807 mvpp2_percpu_write(port->priv, cpu,
5808 MVPP2_ISR_RX_TX_CAUSE_REG(port->id),
5809 cause_rx_tx & ~MVPP2_CAUSE_MISC_SUM_MASK);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005810 }
5811
Marcin Wojtas3f518502014-07-10 16:52:13 -03005812 cause_rx = cause_rx_tx & MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
5813
5814 /* Process RX packets */
5815 cause_rx |= port->pending_cause_rx;
5816 while (cause_rx && budget > 0) {
5817 int count;
5818 struct mvpp2_rx_queue *rxq;
5819
5820 rxq = mvpp2_get_rx_queue(port, cause_rx);
5821 if (!rxq)
5822 break;
5823
5824 count = mvpp2_rx(port, budget, rxq);
5825 rx_done += count;
5826 budget -= count;
5827 if (budget > 0) {
5828 /* Clear the bit associated to this Rx queue
5829 * so that next iteration will continue from
5830 * the next Rx queue.
5831 */
5832 cause_rx &= ~(1 << rxq->logic_rxq);
5833 }
5834 }
5835
5836 if (budget > 0) {
5837 cause_rx = 0;
Eric Dumazet6ad20162017-01-30 08:22:01 -08005838 napi_complete_done(napi, rx_done);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005839
5840 mvpp2_interrupts_enable(port);
5841 }
5842 port->pending_cause_rx = cause_rx;
5843 return rx_done;
5844}
5845
5846/* Set hw internals when starting port */
5847static void mvpp2_start_dev(struct mvpp2_port *port)
5848{
Philippe Reynes8e072692016-06-28 00:08:11 +02005849 struct net_device *ndev = port->dev;
5850
Marcin Wojtas3f518502014-07-10 16:52:13 -03005851 mvpp2_gmac_max_rx_size_set(port);
5852 mvpp2_txp_max_tx_size_set(port);
5853
5854 napi_enable(&port->napi);
5855
5856 /* Enable interrupts on all CPUs */
5857 mvpp2_interrupts_enable(port);
5858
5859 mvpp2_port_enable(port);
Philippe Reynes8e072692016-06-28 00:08:11 +02005860 phy_start(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005861 netif_tx_start_all_queues(port->dev);
5862}
5863
5864/* Set hw internals when stopping port */
5865static void mvpp2_stop_dev(struct mvpp2_port *port)
5866{
Philippe Reynes8e072692016-06-28 00:08:11 +02005867 struct net_device *ndev = port->dev;
5868
Marcin Wojtas3f518502014-07-10 16:52:13 -03005869 /* Stop new packets from arriving to RXQs */
5870 mvpp2_ingress_disable(port);
5871
5872 mdelay(10);
5873
5874 /* Disable interrupts on all CPUs */
5875 mvpp2_interrupts_disable(port);
5876
5877 napi_disable(&port->napi);
5878
5879 netif_carrier_off(port->dev);
5880 netif_tx_stop_all_queues(port->dev);
5881
5882 mvpp2_egress_disable(port);
5883 mvpp2_port_disable(port);
Philippe Reynes8e072692016-06-28 00:08:11 +02005884 phy_stop(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005885}
5886
Marcin Wojtas3f518502014-07-10 16:52:13 -03005887static int mvpp2_check_ringparam_valid(struct net_device *dev,
5888 struct ethtool_ringparam *ring)
5889{
5890 u16 new_rx_pending = ring->rx_pending;
5891 u16 new_tx_pending = ring->tx_pending;
5892
5893 if (ring->rx_pending == 0 || ring->tx_pending == 0)
5894 return -EINVAL;
5895
5896 if (ring->rx_pending > MVPP2_MAX_RXD)
5897 new_rx_pending = MVPP2_MAX_RXD;
5898 else if (!IS_ALIGNED(ring->rx_pending, 16))
5899 new_rx_pending = ALIGN(ring->rx_pending, 16);
5900
5901 if (ring->tx_pending > MVPP2_MAX_TXD)
5902 new_tx_pending = MVPP2_MAX_TXD;
5903 else if (!IS_ALIGNED(ring->tx_pending, 32))
5904 new_tx_pending = ALIGN(ring->tx_pending, 32);
5905
5906 if (ring->rx_pending != new_rx_pending) {
5907 netdev_info(dev, "illegal Rx ring size value %d, round to %d\n",
5908 ring->rx_pending, new_rx_pending);
5909 ring->rx_pending = new_rx_pending;
5910 }
5911
5912 if (ring->tx_pending != new_tx_pending) {
5913 netdev_info(dev, "illegal Tx ring size value %d, round to %d\n",
5914 ring->tx_pending, new_tx_pending);
5915 ring->tx_pending = new_tx_pending;
5916 }
5917
5918 return 0;
5919}
5920
Thomas Petazzoni26975822017-03-07 16:53:14 +01005921static void mvpp21_get_mac_address(struct mvpp2_port *port, unsigned char *addr)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005922{
5923 u32 mac_addr_l, mac_addr_m, mac_addr_h;
5924
5925 mac_addr_l = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
5926 mac_addr_m = readl(port->priv->lms_base + MVPP2_SRC_ADDR_MIDDLE);
5927 mac_addr_h = readl(port->priv->lms_base + MVPP2_SRC_ADDR_HIGH);
5928 addr[0] = (mac_addr_h >> 24) & 0xFF;
5929 addr[1] = (mac_addr_h >> 16) & 0xFF;
5930 addr[2] = (mac_addr_h >> 8) & 0xFF;
5931 addr[3] = mac_addr_h & 0xFF;
5932 addr[4] = mac_addr_m & 0xFF;
5933 addr[5] = (mac_addr_l >> MVPP2_GMAC_SA_LOW_OFFS) & 0xFF;
5934}
5935
5936static int mvpp2_phy_connect(struct mvpp2_port *port)
5937{
5938 struct phy_device *phy_dev;
5939
5940 phy_dev = of_phy_connect(port->dev, port->phy_node, mvpp2_link_event, 0,
5941 port->phy_interface);
5942 if (!phy_dev) {
5943 netdev_err(port->dev, "cannot connect to phy\n");
5944 return -ENODEV;
5945 }
5946 phy_dev->supported &= PHY_GBIT_FEATURES;
5947 phy_dev->advertising = phy_dev->supported;
5948
Marcin Wojtas3f518502014-07-10 16:52:13 -03005949 port->link = 0;
5950 port->duplex = 0;
5951 port->speed = 0;
5952
5953 return 0;
5954}
5955
5956static void mvpp2_phy_disconnect(struct mvpp2_port *port)
5957{
Philippe Reynes8e072692016-06-28 00:08:11 +02005958 struct net_device *ndev = port->dev;
5959
5960 phy_disconnect(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005961}
5962
5963static int mvpp2_open(struct net_device *dev)
5964{
5965 struct mvpp2_port *port = netdev_priv(dev);
5966 unsigned char mac_bcast[ETH_ALEN] = {
5967 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
5968 int err;
5969
5970 err = mvpp2_prs_mac_da_accept(port->priv, port->id, mac_bcast, true);
5971 if (err) {
5972 netdev_err(dev, "mvpp2_prs_mac_da_accept BC failed\n");
5973 return err;
5974 }
5975 err = mvpp2_prs_mac_da_accept(port->priv, port->id,
5976 dev->dev_addr, true);
5977 if (err) {
5978 netdev_err(dev, "mvpp2_prs_mac_da_accept MC failed\n");
5979 return err;
5980 }
5981 err = mvpp2_prs_tag_mode_set(port->priv, port->id, MVPP2_TAG_TYPE_MH);
5982 if (err) {
5983 netdev_err(dev, "mvpp2_prs_tag_mode_set failed\n");
5984 return err;
5985 }
5986 err = mvpp2_prs_def_flow(port);
5987 if (err) {
5988 netdev_err(dev, "mvpp2_prs_def_flow failed\n");
5989 return err;
5990 }
5991
5992 /* Allocate the Rx/Tx queues */
5993 err = mvpp2_setup_rxqs(port);
5994 if (err) {
5995 netdev_err(port->dev, "cannot allocate Rx queues\n");
5996 return err;
5997 }
5998
5999 err = mvpp2_setup_txqs(port);
6000 if (err) {
6001 netdev_err(port->dev, "cannot allocate Tx queues\n");
6002 goto err_cleanup_rxqs;
6003 }
6004
6005 err = request_irq(port->irq, mvpp2_isr, 0, dev->name, port);
6006 if (err) {
6007 netdev_err(port->dev, "cannot request IRQ %d\n", port->irq);
6008 goto err_cleanup_txqs;
6009 }
6010
6011 /* In default link is down */
6012 netif_carrier_off(port->dev);
6013
6014 err = mvpp2_phy_connect(port);
6015 if (err < 0)
6016 goto err_free_irq;
6017
6018 /* Unmask interrupts on all CPUs */
6019 on_each_cpu(mvpp2_interrupts_unmask, port, 1);
6020
6021 mvpp2_start_dev(port);
6022
6023 return 0;
6024
6025err_free_irq:
6026 free_irq(port->irq, port);
6027err_cleanup_txqs:
6028 mvpp2_cleanup_txqs(port);
6029err_cleanup_rxqs:
6030 mvpp2_cleanup_rxqs(port);
6031 return err;
6032}
6033
6034static int mvpp2_stop(struct net_device *dev)
6035{
6036 struct mvpp2_port *port = netdev_priv(dev);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006037 struct mvpp2_port_pcpu *port_pcpu;
6038 int cpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006039
6040 mvpp2_stop_dev(port);
6041 mvpp2_phy_disconnect(port);
6042
6043 /* Mask interrupts on all CPUs */
6044 on_each_cpu(mvpp2_interrupts_mask, port, 1);
6045
6046 free_irq(port->irq, port);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006047 for_each_present_cpu(cpu) {
6048 port_pcpu = per_cpu_ptr(port->pcpu, cpu);
6049
6050 hrtimer_cancel(&port_pcpu->tx_done_timer);
6051 port_pcpu->timer_scheduled = false;
6052 tasklet_kill(&port_pcpu->tx_done_tasklet);
6053 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006054 mvpp2_cleanup_rxqs(port);
6055 mvpp2_cleanup_txqs(port);
6056
6057 return 0;
6058}
6059
6060static void mvpp2_set_rx_mode(struct net_device *dev)
6061{
6062 struct mvpp2_port *port = netdev_priv(dev);
6063 struct mvpp2 *priv = port->priv;
6064 struct netdev_hw_addr *ha;
6065 int id = port->id;
6066 bool allmulti = dev->flags & IFF_ALLMULTI;
6067
6068 mvpp2_prs_mac_promisc_set(priv, id, dev->flags & IFF_PROMISC);
6069 mvpp2_prs_mac_multi_set(priv, id, MVPP2_PE_MAC_MC_ALL, allmulti);
6070 mvpp2_prs_mac_multi_set(priv, id, MVPP2_PE_MAC_MC_IP6, allmulti);
6071
6072 /* Remove all port->id's mcast enries */
6073 mvpp2_prs_mcast_del_all(priv, id);
6074
6075 if (allmulti && !netdev_mc_empty(dev)) {
6076 netdev_for_each_mc_addr(ha, dev)
6077 mvpp2_prs_mac_da_accept(priv, id, ha->addr, true);
6078 }
6079}
6080
6081static int mvpp2_set_mac_address(struct net_device *dev, void *p)
6082{
6083 struct mvpp2_port *port = netdev_priv(dev);
6084 const struct sockaddr *addr = p;
6085 int err;
6086
6087 if (!is_valid_ether_addr(addr->sa_data)) {
6088 err = -EADDRNOTAVAIL;
Markus Elfringc1175542017-04-17 11:10:47 +02006089 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006090 }
6091
6092 if (!netif_running(dev)) {
6093 err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
6094 if (!err)
6095 return 0;
6096 /* Reconfigure parser to accept the original MAC address */
6097 err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
6098 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02006099 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006100 }
6101
6102 mvpp2_stop_dev(port);
6103
6104 err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
6105 if (!err)
6106 goto out_start;
6107
6108 /* Reconfigure parser accept the original MAC address */
6109 err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
6110 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02006111 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006112out_start:
6113 mvpp2_start_dev(port);
6114 mvpp2_egress_enable(port);
6115 mvpp2_ingress_enable(port);
6116 return 0;
Markus Elfringc1175542017-04-17 11:10:47 +02006117log_error:
Markus Elfringdfd42402017-04-17 11:20:41 +02006118 netdev_err(dev, "failed to change MAC address\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03006119 return err;
6120}
6121
6122static int mvpp2_change_mtu(struct net_device *dev, int mtu)
6123{
6124 struct mvpp2_port *port = netdev_priv(dev);
6125 int err;
6126
Jarod Wilson57779872016-10-17 15:54:06 -04006127 if (!IS_ALIGNED(MVPP2_RX_PKT_SIZE(mtu), 8)) {
6128 netdev_info(dev, "illegal MTU value %d, round to %d\n", mtu,
6129 ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8));
6130 mtu = ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006131 }
6132
6133 if (!netif_running(dev)) {
6134 err = mvpp2_bm_update_mtu(dev, mtu);
6135 if (!err) {
6136 port->pkt_size = MVPP2_RX_PKT_SIZE(mtu);
6137 return 0;
6138 }
6139
6140 /* Reconfigure BM to the original MTU */
6141 err = mvpp2_bm_update_mtu(dev, dev->mtu);
6142 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02006143 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006144 }
6145
6146 mvpp2_stop_dev(port);
6147
6148 err = mvpp2_bm_update_mtu(dev, mtu);
6149 if (!err) {
6150 port->pkt_size = MVPP2_RX_PKT_SIZE(mtu);
6151 goto out_start;
6152 }
6153
6154 /* Reconfigure BM to the original MTU */
6155 err = mvpp2_bm_update_mtu(dev, dev->mtu);
6156 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02006157 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006158
6159out_start:
6160 mvpp2_start_dev(port);
6161 mvpp2_egress_enable(port);
6162 mvpp2_ingress_enable(port);
6163
6164 return 0;
Markus Elfringc1175542017-04-17 11:10:47 +02006165log_error:
Markus Elfringdfd42402017-04-17 11:20:41 +02006166 netdev_err(dev, "failed to change MTU\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03006167 return err;
6168}
6169
stephen hemmingerbc1f4472017-01-06 19:12:52 -08006170static void
Marcin Wojtas3f518502014-07-10 16:52:13 -03006171mvpp2_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
6172{
6173 struct mvpp2_port *port = netdev_priv(dev);
6174 unsigned int start;
6175 int cpu;
6176
6177 for_each_possible_cpu(cpu) {
6178 struct mvpp2_pcpu_stats *cpu_stats;
6179 u64 rx_packets;
6180 u64 rx_bytes;
6181 u64 tx_packets;
6182 u64 tx_bytes;
6183
6184 cpu_stats = per_cpu_ptr(port->stats, cpu);
6185 do {
6186 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
6187 rx_packets = cpu_stats->rx_packets;
6188 rx_bytes = cpu_stats->rx_bytes;
6189 tx_packets = cpu_stats->tx_packets;
6190 tx_bytes = cpu_stats->tx_bytes;
6191 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
6192
6193 stats->rx_packets += rx_packets;
6194 stats->rx_bytes += rx_bytes;
6195 stats->tx_packets += tx_packets;
6196 stats->tx_bytes += tx_bytes;
6197 }
6198
6199 stats->rx_errors = dev->stats.rx_errors;
6200 stats->rx_dropped = dev->stats.rx_dropped;
6201 stats->tx_dropped = dev->stats.tx_dropped;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006202}
6203
Thomas Petazzonibd695a52014-07-27 23:21:36 +02006204static int mvpp2_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
6205{
Thomas Petazzonibd695a52014-07-27 23:21:36 +02006206 int ret;
6207
Philippe Reynes8e072692016-06-28 00:08:11 +02006208 if (!dev->phydev)
Thomas Petazzonibd695a52014-07-27 23:21:36 +02006209 return -ENOTSUPP;
6210
Philippe Reynes8e072692016-06-28 00:08:11 +02006211 ret = phy_mii_ioctl(dev->phydev, ifr, cmd);
Thomas Petazzonibd695a52014-07-27 23:21:36 +02006212 if (!ret)
6213 mvpp2_link_event(dev);
6214
6215 return ret;
6216}
6217
Marcin Wojtas3f518502014-07-10 16:52:13 -03006218/* Ethtool methods */
6219
Marcin Wojtas3f518502014-07-10 16:52:13 -03006220/* Set interrupt coalescing for ethtools */
6221static int mvpp2_ethtool_set_coalesce(struct net_device *dev,
6222 struct ethtool_coalesce *c)
6223{
6224 struct mvpp2_port *port = netdev_priv(dev);
6225 int queue;
6226
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006227 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006228 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
6229
6230 rxq->time_coal = c->rx_coalesce_usecs;
6231 rxq->pkts_coal = c->rx_max_coalesced_frames;
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01006232 mvpp2_rx_pkts_coal_set(port, rxq);
6233 mvpp2_rx_time_coal_set(port, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006234 }
6235
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006236 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006237 struct mvpp2_tx_queue *txq = port->txqs[queue];
6238
6239 txq->done_pkts_coal = c->tx_max_coalesced_frames;
6240 }
6241
Marcin Wojtas3f518502014-07-10 16:52:13 -03006242 return 0;
6243}
6244
6245/* get coalescing for ethtools */
6246static int mvpp2_ethtool_get_coalesce(struct net_device *dev,
6247 struct ethtool_coalesce *c)
6248{
6249 struct mvpp2_port *port = netdev_priv(dev);
6250
6251 c->rx_coalesce_usecs = port->rxqs[0]->time_coal;
6252 c->rx_max_coalesced_frames = port->rxqs[0]->pkts_coal;
6253 c->tx_max_coalesced_frames = port->txqs[0]->done_pkts_coal;
6254 return 0;
6255}
6256
6257static void mvpp2_ethtool_get_drvinfo(struct net_device *dev,
6258 struct ethtool_drvinfo *drvinfo)
6259{
6260 strlcpy(drvinfo->driver, MVPP2_DRIVER_NAME,
6261 sizeof(drvinfo->driver));
6262 strlcpy(drvinfo->version, MVPP2_DRIVER_VERSION,
6263 sizeof(drvinfo->version));
6264 strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
6265 sizeof(drvinfo->bus_info));
6266}
6267
6268static void mvpp2_ethtool_get_ringparam(struct net_device *dev,
6269 struct ethtool_ringparam *ring)
6270{
6271 struct mvpp2_port *port = netdev_priv(dev);
6272
6273 ring->rx_max_pending = MVPP2_MAX_RXD;
6274 ring->tx_max_pending = MVPP2_MAX_TXD;
6275 ring->rx_pending = port->rx_ring_size;
6276 ring->tx_pending = port->tx_ring_size;
6277}
6278
6279static int mvpp2_ethtool_set_ringparam(struct net_device *dev,
6280 struct ethtool_ringparam *ring)
6281{
6282 struct mvpp2_port *port = netdev_priv(dev);
6283 u16 prev_rx_ring_size = port->rx_ring_size;
6284 u16 prev_tx_ring_size = port->tx_ring_size;
6285 int err;
6286
6287 err = mvpp2_check_ringparam_valid(dev, ring);
6288 if (err)
6289 return err;
6290
6291 if (!netif_running(dev)) {
6292 port->rx_ring_size = ring->rx_pending;
6293 port->tx_ring_size = ring->tx_pending;
6294 return 0;
6295 }
6296
6297 /* The interface is running, so we have to force a
6298 * reallocation of the queues
6299 */
6300 mvpp2_stop_dev(port);
6301 mvpp2_cleanup_rxqs(port);
6302 mvpp2_cleanup_txqs(port);
6303
6304 port->rx_ring_size = ring->rx_pending;
6305 port->tx_ring_size = ring->tx_pending;
6306
6307 err = mvpp2_setup_rxqs(port);
6308 if (err) {
6309 /* Reallocate Rx queues with the original ring size */
6310 port->rx_ring_size = prev_rx_ring_size;
6311 ring->rx_pending = prev_rx_ring_size;
6312 err = mvpp2_setup_rxqs(port);
6313 if (err)
6314 goto err_out;
6315 }
6316 err = mvpp2_setup_txqs(port);
6317 if (err) {
6318 /* Reallocate Tx queues with the original ring size */
6319 port->tx_ring_size = prev_tx_ring_size;
6320 ring->tx_pending = prev_tx_ring_size;
6321 err = mvpp2_setup_txqs(port);
6322 if (err)
6323 goto err_clean_rxqs;
6324 }
6325
6326 mvpp2_start_dev(port);
6327 mvpp2_egress_enable(port);
6328 mvpp2_ingress_enable(port);
6329
6330 return 0;
6331
6332err_clean_rxqs:
6333 mvpp2_cleanup_rxqs(port);
6334err_out:
Markus Elfringdfd42402017-04-17 11:20:41 +02006335 netdev_err(dev, "failed to change ring parameters");
Marcin Wojtas3f518502014-07-10 16:52:13 -03006336 return err;
6337}
6338
6339/* Device ops */
6340
6341static const struct net_device_ops mvpp2_netdev_ops = {
6342 .ndo_open = mvpp2_open,
6343 .ndo_stop = mvpp2_stop,
6344 .ndo_start_xmit = mvpp2_tx,
6345 .ndo_set_rx_mode = mvpp2_set_rx_mode,
6346 .ndo_set_mac_address = mvpp2_set_mac_address,
6347 .ndo_change_mtu = mvpp2_change_mtu,
6348 .ndo_get_stats64 = mvpp2_get_stats64,
Thomas Petazzonibd695a52014-07-27 23:21:36 +02006349 .ndo_do_ioctl = mvpp2_ioctl,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006350};
6351
6352static const struct ethtool_ops mvpp2_eth_tool_ops = {
Florian Fainelli00606c42016-11-15 11:19:48 -08006353 .nway_reset = phy_ethtool_nway_reset,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006354 .get_link = ethtool_op_get_link,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006355 .set_coalesce = mvpp2_ethtool_set_coalesce,
6356 .get_coalesce = mvpp2_ethtool_get_coalesce,
6357 .get_drvinfo = mvpp2_ethtool_get_drvinfo,
6358 .get_ringparam = mvpp2_ethtool_get_ringparam,
6359 .set_ringparam = mvpp2_ethtool_set_ringparam,
Philippe Reynesfb773e92016-06-28 00:08:12 +02006360 .get_link_ksettings = phy_ethtool_get_link_ksettings,
6361 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006362};
6363
Marcin Wojtas3f518502014-07-10 16:52:13 -03006364/* Initialize port HW */
6365static int mvpp2_port_init(struct mvpp2_port *port)
6366{
6367 struct device *dev = port->dev->dev.parent;
6368 struct mvpp2 *priv = port->priv;
6369 struct mvpp2_txq_pcpu *txq_pcpu;
6370 int queue, cpu, err;
6371
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006372 /* Checks for hardware constraints */
6373 if (port->first_rxq + port->nrxqs >
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01006374 MVPP2_MAX_PORTS * priv->max_port_rxqs)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006375 return -EINVAL;
6376
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006377 if (port->nrxqs % 4 || (port->nrxqs > priv->max_port_rxqs) ||
6378 (port->ntxqs > MVPP2_MAX_TXQ))
6379 return -EINVAL;
6380
Marcin Wojtas3f518502014-07-10 16:52:13 -03006381 /* Disable port */
6382 mvpp2_egress_disable(port);
6383 mvpp2_port_disable(port);
6384
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006385 port->txqs = devm_kcalloc(dev, port->ntxqs, sizeof(*port->txqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03006386 GFP_KERNEL);
6387 if (!port->txqs)
6388 return -ENOMEM;
6389
6390 /* Associate physical Tx queues to this port and initialize.
6391 * The mapping is predefined.
6392 */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006393 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006394 int queue_phy_id = mvpp2_txq_phys(port->id, queue);
6395 struct mvpp2_tx_queue *txq;
6396
6397 txq = devm_kzalloc(dev, sizeof(*txq), GFP_KERNEL);
Christophe Jaillet177c8d12017-02-19 10:19:57 +01006398 if (!txq) {
6399 err = -ENOMEM;
6400 goto err_free_percpu;
6401 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006402
6403 txq->pcpu = alloc_percpu(struct mvpp2_txq_pcpu);
6404 if (!txq->pcpu) {
6405 err = -ENOMEM;
6406 goto err_free_percpu;
6407 }
6408
6409 txq->id = queue_phy_id;
6410 txq->log_id = queue;
6411 txq->done_pkts_coal = MVPP2_TXDONE_COAL_PKTS_THRESH;
6412 for_each_present_cpu(cpu) {
6413 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
6414 txq_pcpu->cpu = cpu;
6415 }
6416
6417 port->txqs[queue] = txq;
6418 }
6419
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006420 port->rxqs = devm_kcalloc(dev, port->nrxqs, sizeof(*port->rxqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03006421 GFP_KERNEL);
6422 if (!port->rxqs) {
6423 err = -ENOMEM;
6424 goto err_free_percpu;
6425 }
6426
6427 /* Allocate and initialize Rx queue for this port */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006428 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006429 struct mvpp2_rx_queue *rxq;
6430
6431 /* Map physical Rx queue to port's logical Rx queue */
6432 rxq = devm_kzalloc(dev, sizeof(*rxq), GFP_KERNEL);
Jisheng Zhangd82b0c22016-03-31 17:01:23 +08006433 if (!rxq) {
6434 err = -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006435 goto err_free_percpu;
Jisheng Zhangd82b0c22016-03-31 17:01:23 +08006436 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006437 /* Map this Rx queue to a physical queue */
6438 rxq->id = port->first_rxq + queue;
6439 rxq->port = port->id;
6440 rxq->logic_rxq = queue;
6441
6442 port->rxqs[queue] = rxq;
6443 }
6444
6445 /* Configure Rx queue group interrupt for this port */
Thomas Petazzonia73fef12017-03-07 16:53:16 +01006446 if (priv->hw_version == MVPP21) {
6447 mvpp2_write(priv, MVPP21_ISR_RXQ_GROUP_REG(port->id),
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006448 port->nrxqs);
Thomas Petazzonia73fef12017-03-07 16:53:16 +01006449 } else {
6450 u32 val;
6451
6452 val = (port->id << MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_OFFSET);
6453 mvpp2_write(priv, MVPP22_ISR_RXQ_GROUP_INDEX_REG, val);
6454
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006455 val = (port->nrxqs << MVPP22_ISR_RXQ_SUB_GROUP_SIZE_OFFSET);
Thomas Petazzonia73fef12017-03-07 16:53:16 +01006456 mvpp2_write(priv, MVPP22_ISR_RXQ_SUB_GROUP_CONFIG_REG, val);
6457 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006458
6459 /* Create Rx descriptor rings */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006460 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006461 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
6462
6463 rxq->size = port->rx_ring_size;
6464 rxq->pkts_coal = MVPP2_RX_COAL_PKTS;
6465 rxq->time_coal = MVPP2_RX_COAL_USEC;
6466 }
6467
6468 mvpp2_ingress_disable(port);
6469
6470 /* Port default configuration */
6471 mvpp2_defaults_set(port);
6472
6473 /* Port's classifier configuration */
6474 mvpp2_cls_oversize_rxq_set(port);
6475 mvpp2_cls_port_config(port);
6476
6477 /* Provide an initial Rx packet size */
6478 port->pkt_size = MVPP2_RX_PKT_SIZE(port->dev->mtu);
6479
6480 /* Initialize pools for swf */
6481 err = mvpp2_swf_bm_pool_init(port);
6482 if (err)
6483 goto err_free_percpu;
6484
6485 return 0;
6486
6487err_free_percpu:
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006488 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006489 if (!port->txqs[queue])
6490 continue;
6491 free_percpu(port->txqs[queue]->pcpu);
6492 }
6493 return err;
6494}
6495
6496/* Ports initialization */
6497static int mvpp2_port_probe(struct platform_device *pdev,
6498 struct device_node *port_node,
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01006499 struct mvpp2 *priv)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006500{
6501 struct device_node *phy_node;
6502 struct mvpp2_port *port;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006503 struct mvpp2_port_pcpu *port_pcpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006504 struct net_device *dev;
6505 struct resource *res;
6506 const char *dt_mac_addr;
6507 const char *mac_from;
6508 char hw_mac_addr[ETH_ALEN];
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006509 unsigned int ntxqs, nrxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006510 u32 id;
6511 int features;
6512 int phy_mode;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006513 int err, i, cpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006514
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006515 ntxqs = MVPP2_MAX_TXQ;
6516 nrxqs = MVPP2_DEFAULT_RXQ;
6517
6518 dev = alloc_etherdev_mqs(sizeof(*port), ntxqs, nrxqs);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006519 if (!dev)
6520 return -ENOMEM;
6521
6522 phy_node = of_parse_phandle(port_node, "phy", 0);
6523 if (!phy_node) {
6524 dev_err(&pdev->dev, "missing phy\n");
6525 err = -ENODEV;
6526 goto err_free_netdev;
6527 }
6528
6529 phy_mode = of_get_phy_mode(port_node);
6530 if (phy_mode < 0) {
6531 dev_err(&pdev->dev, "incorrect phy mode\n");
6532 err = phy_mode;
6533 goto err_free_netdev;
6534 }
6535
6536 if (of_property_read_u32(port_node, "port-id", &id)) {
6537 err = -EINVAL;
6538 dev_err(&pdev->dev, "missing port-id value\n");
6539 goto err_free_netdev;
6540 }
6541
6542 dev->tx_queue_len = MVPP2_MAX_TXD;
6543 dev->watchdog_timeo = 5 * HZ;
6544 dev->netdev_ops = &mvpp2_netdev_ops;
6545 dev->ethtool_ops = &mvpp2_eth_tool_ops;
6546
6547 port = netdev_priv(dev);
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006548 port->ntxqs = ntxqs;
6549 port->nrxqs = nrxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006550
6551 port->irq = irq_of_parse_and_map(port_node, 0);
6552 if (port->irq <= 0) {
6553 err = -EINVAL;
6554 goto err_free_netdev;
6555 }
6556
6557 if (of_property_read_bool(port_node, "marvell,loopback"))
6558 port->flags |= MVPP2_F_LOOPBACK;
6559
6560 port->priv = priv;
6561 port->id = id;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01006562 if (priv->hw_version == MVPP21)
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006563 port->first_rxq = port->id * port->nrxqs;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01006564 else
6565 port->first_rxq = port->id * priv->max_port_rxqs;
6566
Marcin Wojtas3f518502014-07-10 16:52:13 -03006567 port->phy_node = phy_node;
6568 port->phy_interface = phy_mode;
6569
Thomas Petazzonia7868412017-03-07 16:53:13 +01006570 if (priv->hw_version == MVPP21) {
6571 res = platform_get_resource(pdev, IORESOURCE_MEM, 2 + id);
6572 port->base = devm_ioremap_resource(&pdev->dev, res);
6573 if (IS_ERR(port->base)) {
6574 err = PTR_ERR(port->base);
6575 goto err_free_irq;
6576 }
6577 } else {
6578 if (of_property_read_u32(port_node, "gop-port-id",
6579 &port->gop_id)) {
6580 err = -EINVAL;
6581 dev_err(&pdev->dev, "missing gop-port-id value\n");
6582 goto err_free_irq;
6583 }
6584
6585 port->base = priv->iface_base + MVPP22_GMAC_BASE(port->gop_id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006586 }
6587
6588 /* Alloc per-cpu stats */
6589 port->stats = netdev_alloc_pcpu_stats(struct mvpp2_pcpu_stats);
6590 if (!port->stats) {
6591 err = -ENOMEM;
6592 goto err_free_irq;
6593 }
6594
6595 dt_mac_addr = of_get_mac_address(port_node);
6596 if (dt_mac_addr && is_valid_ether_addr(dt_mac_addr)) {
6597 mac_from = "device tree";
6598 ether_addr_copy(dev->dev_addr, dt_mac_addr);
6599 } else {
Thomas Petazzoni26975822017-03-07 16:53:14 +01006600 if (priv->hw_version == MVPP21)
6601 mvpp21_get_mac_address(port, hw_mac_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006602 if (is_valid_ether_addr(hw_mac_addr)) {
6603 mac_from = "hardware";
6604 ether_addr_copy(dev->dev_addr, hw_mac_addr);
6605 } else {
6606 mac_from = "random";
6607 eth_hw_addr_random(dev);
6608 }
6609 }
6610
6611 port->tx_ring_size = MVPP2_MAX_TXD;
6612 port->rx_ring_size = MVPP2_MAX_RXD;
6613 port->dev = dev;
6614 SET_NETDEV_DEV(dev, &pdev->dev);
6615
6616 err = mvpp2_port_init(port);
6617 if (err < 0) {
6618 dev_err(&pdev->dev, "failed to init port %d\n", id);
6619 goto err_free_stats;
6620 }
Thomas Petazzoni26975822017-03-07 16:53:14 +01006621
6622 mvpp2_port_mii_set(port);
6623 mvpp2_port_periodic_xon_disable(port);
6624
6625 if (priv->hw_version == MVPP21)
6626 mvpp2_port_fc_adv_enable(port);
6627
6628 mvpp2_port_reset(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006629
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006630 port->pcpu = alloc_percpu(struct mvpp2_port_pcpu);
6631 if (!port->pcpu) {
6632 err = -ENOMEM;
6633 goto err_free_txq_pcpu;
6634 }
6635
6636 for_each_present_cpu(cpu) {
6637 port_pcpu = per_cpu_ptr(port->pcpu, cpu);
6638
6639 hrtimer_init(&port_pcpu->tx_done_timer, CLOCK_MONOTONIC,
6640 HRTIMER_MODE_REL_PINNED);
6641 port_pcpu->tx_done_timer.function = mvpp2_hr_timer_cb;
6642 port_pcpu->timer_scheduled = false;
6643
6644 tasklet_init(&port_pcpu->tx_done_tasklet, mvpp2_tx_proc_cb,
6645 (unsigned long)dev);
6646 }
6647
Marcin Wojtas3f518502014-07-10 16:52:13 -03006648 netif_napi_add(dev, &port->napi, mvpp2_poll, NAPI_POLL_WEIGHT);
6649 features = NETIF_F_SG | NETIF_F_IP_CSUM;
6650 dev->features = features | NETIF_F_RXCSUM;
6651 dev->hw_features |= features | NETIF_F_RXCSUM | NETIF_F_GRO;
6652 dev->vlan_features |= features;
6653
Jarod Wilson57779872016-10-17 15:54:06 -04006654 /* MTU range: 68 - 9676 */
6655 dev->min_mtu = ETH_MIN_MTU;
6656 /* 9676 == 9700 - 20 and rounding to 8 */
6657 dev->max_mtu = 9676;
6658
Marcin Wojtas3f518502014-07-10 16:52:13 -03006659 err = register_netdev(dev);
6660 if (err < 0) {
6661 dev_err(&pdev->dev, "failed to register netdev\n");
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006662 goto err_free_port_pcpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006663 }
6664 netdev_info(dev, "Using %s mac address %pM\n", mac_from, dev->dev_addr);
6665
Marcin Wojtas3f518502014-07-10 16:52:13 -03006666 priv->port_list[id] = port;
6667 return 0;
6668
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006669err_free_port_pcpu:
6670 free_percpu(port->pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006671err_free_txq_pcpu:
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006672 for (i = 0; i < port->ntxqs; i++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006673 free_percpu(port->txqs[i]->pcpu);
6674err_free_stats:
6675 free_percpu(port->stats);
6676err_free_irq:
6677 irq_dispose_mapping(port->irq);
6678err_free_netdev:
Peter Chenccb80392016-08-01 15:02:37 +08006679 of_node_put(phy_node);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006680 free_netdev(dev);
6681 return err;
6682}
6683
6684/* Ports removal routine */
6685static void mvpp2_port_remove(struct mvpp2_port *port)
6686{
6687 int i;
6688
6689 unregister_netdev(port->dev);
Peter Chenccb80392016-08-01 15:02:37 +08006690 of_node_put(port->phy_node);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006691 free_percpu(port->pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006692 free_percpu(port->stats);
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006693 for (i = 0; i < port->ntxqs; i++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006694 free_percpu(port->txqs[i]->pcpu);
6695 irq_dispose_mapping(port->irq);
6696 free_netdev(port->dev);
6697}
6698
6699/* Initialize decoding windows */
6700static void mvpp2_conf_mbus_windows(const struct mbus_dram_target_info *dram,
6701 struct mvpp2 *priv)
6702{
6703 u32 win_enable;
6704 int i;
6705
6706 for (i = 0; i < 6; i++) {
6707 mvpp2_write(priv, MVPP2_WIN_BASE(i), 0);
6708 mvpp2_write(priv, MVPP2_WIN_SIZE(i), 0);
6709
6710 if (i < 4)
6711 mvpp2_write(priv, MVPP2_WIN_REMAP(i), 0);
6712 }
6713
6714 win_enable = 0;
6715
6716 for (i = 0; i < dram->num_cs; i++) {
6717 const struct mbus_dram_window *cs = dram->cs + i;
6718
6719 mvpp2_write(priv, MVPP2_WIN_BASE(i),
6720 (cs->base & 0xffff0000) | (cs->mbus_attr << 8) |
6721 dram->mbus_dram_target_id);
6722
6723 mvpp2_write(priv, MVPP2_WIN_SIZE(i),
6724 (cs->size - 1) & 0xffff0000);
6725
6726 win_enable |= (1 << i);
6727 }
6728
6729 mvpp2_write(priv, MVPP2_BASE_ADDR_ENABLE, win_enable);
6730}
6731
6732/* Initialize Rx FIFO's */
6733static void mvpp2_rx_fifo_init(struct mvpp2 *priv)
6734{
6735 int port;
6736
6737 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
6738 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
6739 MVPP2_RX_FIFO_PORT_DATA_SIZE);
6740 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
6741 MVPP2_RX_FIFO_PORT_ATTR_SIZE);
6742 }
6743
6744 mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
6745 MVPP2_RX_FIFO_PORT_MIN_PKT);
6746 mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
6747}
6748
Thomas Petazzoni6763ce32017-03-07 16:53:15 +01006749static void mvpp2_axi_init(struct mvpp2 *priv)
6750{
6751 u32 val, rdval, wrval;
6752
6753 mvpp2_write(priv, MVPP22_BM_ADDR_HIGH_RLS_REG, 0x0);
6754
6755 /* AXI Bridge Configuration */
6756
6757 rdval = MVPP22_AXI_CODE_CACHE_RD_CACHE
6758 << MVPP22_AXI_ATTR_CACHE_OFFS;
6759 rdval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
6760 << MVPP22_AXI_ATTR_DOMAIN_OFFS;
6761
6762 wrval = MVPP22_AXI_CODE_CACHE_WR_CACHE
6763 << MVPP22_AXI_ATTR_CACHE_OFFS;
6764 wrval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
6765 << MVPP22_AXI_ATTR_DOMAIN_OFFS;
6766
6767 /* BM */
6768 mvpp2_write(priv, MVPP22_AXI_BM_WR_ATTR_REG, wrval);
6769 mvpp2_write(priv, MVPP22_AXI_BM_RD_ATTR_REG, rdval);
6770
6771 /* Descriptors */
6772 mvpp2_write(priv, MVPP22_AXI_AGGRQ_DESCR_RD_ATTR_REG, rdval);
6773 mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_WR_ATTR_REG, wrval);
6774 mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_RD_ATTR_REG, rdval);
6775 mvpp2_write(priv, MVPP22_AXI_RXQ_DESCR_WR_ATTR_REG, wrval);
6776
6777 /* Buffer Data */
6778 mvpp2_write(priv, MVPP22_AXI_TX_DATA_RD_ATTR_REG, rdval);
6779 mvpp2_write(priv, MVPP22_AXI_RX_DATA_WR_ATTR_REG, wrval);
6780
6781 val = MVPP22_AXI_CODE_CACHE_NON_CACHE
6782 << MVPP22_AXI_CODE_CACHE_OFFS;
6783 val |= MVPP22_AXI_CODE_DOMAIN_SYSTEM
6784 << MVPP22_AXI_CODE_DOMAIN_OFFS;
6785 mvpp2_write(priv, MVPP22_AXI_RD_NORMAL_CODE_REG, val);
6786 mvpp2_write(priv, MVPP22_AXI_WR_NORMAL_CODE_REG, val);
6787
6788 val = MVPP22_AXI_CODE_CACHE_RD_CACHE
6789 << MVPP22_AXI_CODE_CACHE_OFFS;
6790 val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
6791 << MVPP22_AXI_CODE_DOMAIN_OFFS;
6792
6793 mvpp2_write(priv, MVPP22_AXI_RD_SNOOP_CODE_REG, val);
6794
6795 val = MVPP22_AXI_CODE_CACHE_WR_CACHE
6796 << MVPP22_AXI_CODE_CACHE_OFFS;
6797 val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
6798 << MVPP22_AXI_CODE_DOMAIN_OFFS;
6799
6800 mvpp2_write(priv, MVPP22_AXI_WR_SNOOP_CODE_REG, val);
6801}
6802
Marcin Wojtas3f518502014-07-10 16:52:13 -03006803/* Initialize network controller common part HW */
6804static int mvpp2_init(struct platform_device *pdev, struct mvpp2 *priv)
6805{
6806 const struct mbus_dram_target_info *dram_target_info;
6807 int err, i;
Marcin Wojtas08a23752014-07-21 13:48:12 -03006808 u32 val;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006809
Marcin Wojtas3f518502014-07-10 16:52:13 -03006810 /* MBUS windows configuration */
6811 dram_target_info = mv_mbus_dram_info();
6812 if (dram_target_info)
6813 mvpp2_conf_mbus_windows(dram_target_info, priv);
6814
Thomas Petazzoni6763ce32017-03-07 16:53:15 +01006815 if (priv->hw_version == MVPP22)
6816 mvpp2_axi_init(priv);
6817
Marcin Wojtas08a23752014-07-21 13:48:12 -03006818 /* Disable HW PHY polling */
Thomas Petazzoni26975822017-03-07 16:53:14 +01006819 if (priv->hw_version == MVPP21) {
6820 val = readl(priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
6821 val |= MVPP2_PHY_AN_STOP_SMI0_MASK;
6822 writel(val, priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
6823 } else {
6824 val = readl(priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
6825 val &= ~MVPP22_SMI_POLLING_EN;
6826 writel(val, priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
6827 }
Marcin Wojtas08a23752014-07-21 13:48:12 -03006828
Marcin Wojtas3f518502014-07-10 16:52:13 -03006829 /* Allocate and initialize aggregated TXQs */
6830 priv->aggr_txqs = devm_kcalloc(&pdev->dev, num_present_cpus(),
Markus Elfringd7ce3ce2017-04-17 08:48:23 +02006831 sizeof(*priv->aggr_txqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03006832 GFP_KERNEL);
6833 if (!priv->aggr_txqs)
6834 return -ENOMEM;
6835
6836 for_each_present_cpu(i) {
6837 priv->aggr_txqs[i].id = i;
6838 priv->aggr_txqs[i].size = MVPP2_AGGR_TXQ_SIZE;
6839 err = mvpp2_aggr_txq_init(pdev, &priv->aggr_txqs[i],
6840 MVPP2_AGGR_TXQ_SIZE, i, priv);
6841 if (err < 0)
6842 return err;
6843 }
6844
6845 /* Rx Fifo Init */
6846 mvpp2_rx_fifo_init(priv);
6847
Thomas Petazzoni26975822017-03-07 16:53:14 +01006848 if (priv->hw_version == MVPP21)
6849 writel(MVPP2_EXT_GLOBAL_CTRL_DEFAULT,
6850 priv->lms_base + MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006851
6852 /* Allow cache snoop when transmiting packets */
6853 mvpp2_write(priv, MVPP2_TX_SNOOP_REG, 0x1);
6854
6855 /* Buffer Manager initialization */
6856 err = mvpp2_bm_init(pdev, priv);
6857 if (err < 0)
6858 return err;
6859
6860 /* Parser default initialization */
6861 err = mvpp2_prs_default_init(pdev, priv);
6862 if (err < 0)
6863 return err;
6864
6865 /* Classifier default initialization */
6866 mvpp2_cls_init(priv);
6867
6868 return 0;
6869}
6870
6871static int mvpp2_probe(struct platform_device *pdev)
6872{
6873 struct device_node *dn = pdev->dev.of_node;
6874 struct device_node *port_node;
6875 struct mvpp2 *priv;
6876 struct resource *res;
Thomas Petazzonia7868412017-03-07 16:53:13 +01006877 void __iomem *base;
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02006878 int port_count, i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006879 int err;
6880
Markus Elfring0b92e592017-04-17 08:38:32 +02006881 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006882 if (!priv)
6883 return -ENOMEM;
6884
Thomas Petazzonifaca9242017-03-07 16:53:06 +01006885 priv->hw_version =
6886 (unsigned long)of_device_get_match_data(&pdev->dev);
6887
Marcin Wojtas3f518502014-07-10 16:52:13 -03006888 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thomas Petazzonia7868412017-03-07 16:53:13 +01006889 base = devm_ioremap_resource(&pdev->dev, res);
6890 if (IS_ERR(base))
6891 return PTR_ERR(base);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006892
Thomas Petazzonia7868412017-03-07 16:53:13 +01006893 if (priv->hw_version == MVPP21) {
6894 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
6895 priv->lms_base = devm_ioremap_resource(&pdev->dev, res);
6896 if (IS_ERR(priv->lms_base))
6897 return PTR_ERR(priv->lms_base);
6898 } else {
6899 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
6900 priv->iface_base = devm_ioremap_resource(&pdev->dev, res);
6901 if (IS_ERR(priv->iface_base))
6902 return PTR_ERR(priv->iface_base);
6903 }
6904
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02006905 for (i = 0; i < MVPP2_MAX_THREADS; i++) {
Thomas Petazzonia7868412017-03-07 16:53:13 +01006906 u32 addr_space_sz;
6907
6908 addr_space_sz = (priv->hw_version == MVPP21 ?
6909 MVPP21_ADDR_SPACE_SZ : MVPP22_ADDR_SPACE_SZ);
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02006910 priv->swth_base[i] = base + i * addr_space_sz;
Thomas Petazzonia7868412017-03-07 16:53:13 +01006911 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006912
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01006913 if (priv->hw_version == MVPP21)
6914 priv->max_port_rxqs = 8;
6915 else
6916 priv->max_port_rxqs = 32;
6917
Marcin Wojtas3f518502014-07-10 16:52:13 -03006918 priv->pp_clk = devm_clk_get(&pdev->dev, "pp_clk");
6919 if (IS_ERR(priv->pp_clk))
6920 return PTR_ERR(priv->pp_clk);
6921 err = clk_prepare_enable(priv->pp_clk);
6922 if (err < 0)
6923 return err;
6924
6925 priv->gop_clk = devm_clk_get(&pdev->dev, "gop_clk");
6926 if (IS_ERR(priv->gop_clk)) {
6927 err = PTR_ERR(priv->gop_clk);
6928 goto err_pp_clk;
6929 }
6930 err = clk_prepare_enable(priv->gop_clk);
6931 if (err < 0)
6932 goto err_pp_clk;
6933
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01006934 if (priv->hw_version == MVPP22) {
6935 priv->mg_clk = devm_clk_get(&pdev->dev, "mg_clk");
6936 if (IS_ERR(priv->mg_clk)) {
6937 err = PTR_ERR(priv->mg_clk);
6938 goto err_gop_clk;
6939 }
6940
6941 err = clk_prepare_enable(priv->mg_clk);
6942 if (err < 0)
6943 goto err_gop_clk;
6944 }
6945
Marcin Wojtas3f518502014-07-10 16:52:13 -03006946 /* Get system's tclk rate */
6947 priv->tclk = clk_get_rate(priv->pp_clk);
6948
Thomas Petazzoni2067e0a2017-03-07 16:53:19 +01006949 if (priv->hw_version == MVPP22) {
6950 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(40));
6951 if (err)
6952 goto err_mg_clk;
6953 /* Sadly, the BM pools all share the same register to
6954 * store the high 32 bits of their address. So they
6955 * must all have the same high 32 bits, which forces
6956 * us to restrict coherent memory to DMA_BIT_MASK(32).
6957 */
6958 err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
6959 if (err)
6960 goto err_mg_clk;
6961 }
6962
Marcin Wojtas3f518502014-07-10 16:52:13 -03006963 /* Initialize network controller */
6964 err = mvpp2_init(pdev, priv);
6965 if (err < 0) {
6966 dev_err(&pdev->dev, "failed to initialize controller\n");
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01006967 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006968 }
6969
6970 port_count = of_get_available_child_count(dn);
6971 if (port_count == 0) {
6972 dev_err(&pdev->dev, "no ports enabled\n");
Wei Yongjun575a1932014-07-20 22:02:43 +08006973 err = -ENODEV;
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01006974 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006975 }
6976
6977 priv->port_list = devm_kcalloc(&pdev->dev, port_count,
Markus Elfring0b92e592017-04-17 08:38:32 +02006978 sizeof(*priv->port_list),
6979 GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006980 if (!priv->port_list) {
6981 err = -ENOMEM;
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01006982 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006983 }
6984
6985 /* Initialize ports */
Marcin Wojtas3f518502014-07-10 16:52:13 -03006986 for_each_available_child_of_node(dn, port_node) {
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01006987 err = mvpp2_port_probe(pdev, port_node, priv);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006988 if (err < 0)
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01006989 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006990 }
6991
6992 platform_set_drvdata(pdev, priv);
6993 return 0;
6994
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01006995err_mg_clk:
6996 if (priv->hw_version == MVPP22)
6997 clk_disable_unprepare(priv->mg_clk);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006998err_gop_clk:
6999 clk_disable_unprepare(priv->gop_clk);
7000err_pp_clk:
7001 clk_disable_unprepare(priv->pp_clk);
7002 return err;
7003}
7004
7005static int mvpp2_remove(struct platform_device *pdev)
7006{
7007 struct mvpp2 *priv = platform_get_drvdata(pdev);
7008 struct device_node *dn = pdev->dev.of_node;
7009 struct device_node *port_node;
7010 int i = 0;
7011
7012 for_each_available_child_of_node(dn, port_node) {
7013 if (priv->port_list[i])
7014 mvpp2_port_remove(priv->port_list[i]);
7015 i++;
7016 }
7017
7018 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
7019 struct mvpp2_bm_pool *bm_pool = &priv->bm_pools[i];
7020
7021 mvpp2_bm_pool_destroy(pdev, priv, bm_pool);
7022 }
7023
7024 for_each_present_cpu(i) {
7025 struct mvpp2_tx_queue *aggr_txq = &priv->aggr_txqs[i];
7026
7027 dma_free_coherent(&pdev->dev,
7028 MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
7029 aggr_txq->descs,
Thomas Petazzoni20396132017-03-07 16:53:00 +01007030 aggr_txq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007031 }
7032
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01007033 clk_disable_unprepare(priv->mg_clk);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007034 clk_disable_unprepare(priv->pp_clk);
7035 clk_disable_unprepare(priv->gop_clk);
7036
7037 return 0;
7038}
7039
7040static const struct of_device_id mvpp2_match[] = {
Thomas Petazzonifaca9242017-03-07 16:53:06 +01007041 {
7042 .compatible = "marvell,armada-375-pp2",
7043 .data = (void *)MVPP21,
7044 },
Thomas Petazzonifc5e1552017-03-07 16:53:20 +01007045 {
7046 .compatible = "marvell,armada-7k-pp22",
7047 .data = (void *)MVPP22,
7048 },
Marcin Wojtas3f518502014-07-10 16:52:13 -03007049 { }
7050};
7051MODULE_DEVICE_TABLE(of, mvpp2_match);
7052
7053static struct platform_driver mvpp2_driver = {
7054 .probe = mvpp2_probe,
7055 .remove = mvpp2_remove,
7056 .driver = {
7057 .name = MVPP2_DRIVER_NAME,
7058 .of_match_table = mvpp2_match,
7059 },
7060};
7061
7062module_platform_driver(mvpp2_driver);
7063
7064MODULE_DESCRIPTION("Marvell PPv2 Ethernet Driver - www.marvell.com");
7065MODULE_AUTHOR("Marcin Wojtas <mw@semihalf.com>");
Ezequiel Garciac6340992014-07-14 10:34:47 -03007066MODULE_LICENSE("GPL v2");