blob: 1bf327271cee34bab6720c70df028b71b5a2513e [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 Petazzoni591f4cf2017-08-03 10:41:59 +0200689#define MVPP2_MAX_QVECS MVPP2_MAX_THREADS
Thomas Petazzonia7868412017-03-07 16:53:13 +0100690
Marcin Wojtas3f518502014-07-10 16:52:13 -0300691enum mvpp2_bm_type {
692 MVPP2_BM_FREE,
693 MVPP2_BM_SWF_LONG,
694 MVPP2_BM_SWF_SHORT
695};
696
697/* Definitions */
698
699/* Shared Packet Processor resources */
700struct mvpp2 {
701 /* Shared registers' base addresses */
Marcin Wojtas3f518502014-07-10 16:52:13 -0300702 void __iomem *lms_base;
Thomas Petazzonia7868412017-03-07 16:53:13 +0100703 void __iomem *iface_base;
704
Thomas Petazzonidf089aa2017-08-03 10:41:58 +0200705 /* On PPv2.2, each "software thread" can access the base
706 * register through a separate address space, each 64 KB apart
707 * from each other. Typically, such address spaces will be
708 * used per CPU.
Thomas Petazzonia7868412017-03-07 16:53:13 +0100709 */
Thomas Petazzonidf089aa2017-08-03 10:41:58 +0200710 void __iomem *swth_base[MVPP2_MAX_THREADS];
Marcin Wojtas3f518502014-07-10 16:52:13 -0300711
712 /* Common clocks */
713 struct clk *pp_clk;
714 struct clk *gop_clk;
Thomas Petazzonifceb55d2017-03-07 16:53:18 +0100715 struct clk *mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300716
717 /* List of pointers to port structures */
718 struct mvpp2_port **port_list;
719
720 /* Aggregated TXQs */
721 struct mvpp2_tx_queue *aggr_txqs;
722
723 /* BM pools */
724 struct mvpp2_bm_pool *bm_pools;
725
726 /* PRS shadow table */
727 struct mvpp2_prs_shadow *prs_shadow;
728 /* PRS auxiliary table for double vlan entries control */
729 bool *prs_double_vlans;
730
731 /* Tclk value */
732 u32 tclk;
Thomas Petazzonifaca9242017-03-07 16:53:06 +0100733
734 /* HW version */
735 enum { MVPP21, MVPP22 } hw_version;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +0100736
737 /* Maximum number of RXQs per port */
738 unsigned int max_port_rxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300739};
740
741struct mvpp2_pcpu_stats {
742 struct u64_stats_sync syncp;
743 u64 rx_packets;
744 u64 rx_bytes;
745 u64 tx_packets;
746 u64 tx_bytes;
747};
748
Marcin Wojtasedc660f2015-08-06 19:00:30 +0200749/* Per-CPU port control */
750struct mvpp2_port_pcpu {
751 struct hrtimer tx_done_timer;
752 bool timer_scheduled;
753 /* Tasklet for egress finalization */
754 struct tasklet_struct tx_done_tasklet;
755};
756
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +0200757struct mvpp2_queue_vector {
758 int irq;
759 struct napi_struct napi;
760 enum { MVPP2_QUEUE_VECTOR_SHARED, MVPP2_QUEUE_VECTOR_PRIVATE } type;
761 int sw_thread_id;
762 u16 sw_thread_mask;
763 int first_rxq;
764 int nrxqs;
765 u32 pending_cause_rx;
766 struct mvpp2_port *port;
767};
768
Marcin Wojtas3f518502014-07-10 16:52:13 -0300769struct mvpp2_port {
770 u8 id;
771
Thomas Petazzonia7868412017-03-07 16:53:13 +0100772 /* Index of the port from the "group of ports" complex point
773 * of view
774 */
775 int gop_id;
776
Marcin Wojtas3f518502014-07-10 16:52:13 -0300777 struct mvpp2 *priv;
778
779 /* Per-port registers' base address */
780 void __iomem *base;
781
782 struct mvpp2_rx_queue **rxqs;
Thomas Petazzoni09f83972017-08-03 10:41:57 +0200783 unsigned int nrxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300784 struct mvpp2_tx_queue **txqs;
Thomas Petazzoni09f83972017-08-03 10:41:57 +0200785 unsigned int ntxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300786 struct net_device *dev;
787
788 int pkt_size;
789
Marcin Wojtasedc660f2015-08-06 19:00:30 +0200790 /* Per-CPU port control */
791 struct mvpp2_port_pcpu __percpu *pcpu;
792
Marcin Wojtas3f518502014-07-10 16:52:13 -0300793 /* Flags */
794 unsigned long flags;
795
796 u16 tx_ring_size;
797 u16 rx_ring_size;
798 struct mvpp2_pcpu_stats __percpu *stats;
799
Marcin Wojtas3f518502014-07-10 16:52:13 -0300800 phy_interface_t phy_interface;
801 struct device_node *phy_node;
802 unsigned int link;
803 unsigned int duplex;
804 unsigned int speed;
805
806 struct mvpp2_bm_pool *pool_long;
807 struct mvpp2_bm_pool *pool_short;
808
809 /* Index of first port's physical RXQ */
810 u8 first_rxq;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +0200811
812 struct mvpp2_queue_vector qvecs[MVPP2_MAX_QVECS];
813 unsigned int nqvecs;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300814};
815
816/* The mvpp2_tx_desc and mvpp2_rx_desc structures describe the
817 * layout of the transmit and reception DMA descriptors, and their
818 * layout is therefore defined by the hardware design
819 */
820
821#define MVPP2_TXD_L3_OFF_SHIFT 0
822#define MVPP2_TXD_IP_HLEN_SHIFT 8
823#define MVPP2_TXD_L4_CSUM_FRAG BIT(13)
824#define MVPP2_TXD_L4_CSUM_NOT BIT(14)
825#define MVPP2_TXD_IP_CSUM_DISABLE BIT(15)
826#define MVPP2_TXD_PADDING_DISABLE BIT(23)
827#define MVPP2_TXD_L4_UDP BIT(24)
828#define MVPP2_TXD_L3_IP6 BIT(26)
829#define MVPP2_TXD_L_DESC BIT(28)
830#define MVPP2_TXD_F_DESC BIT(29)
831
832#define MVPP2_RXD_ERR_SUMMARY BIT(15)
833#define MVPP2_RXD_ERR_CODE_MASK (BIT(13) | BIT(14))
834#define MVPP2_RXD_ERR_CRC 0x0
835#define MVPP2_RXD_ERR_OVERRUN BIT(13)
836#define MVPP2_RXD_ERR_RESOURCE (BIT(13) | BIT(14))
837#define MVPP2_RXD_BM_POOL_ID_OFFS 16
838#define MVPP2_RXD_BM_POOL_ID_MASK (BIT(16) | BIT(17) | BIT(18))
839#define MVPP2_RXD_HWF_SYNC BIT(21)
840#define MVPP2_RXD_L4_CSUM_OK BIT(22)
841#define MVPP2_RXD_IP4_HEADER_ERR BIT(24)
842#define MVPP2_RXD_L4_TCP BIT(25)
843#define MVPP2_RXD_L4_UDP BIT(26)
844#define MVPP2_RXD_L3_IP4 BIT(28)
845#define MVPP2_RXD_L3_IP6 BIT(30)
846#define MVPP2_RXD_BUF_HDR BIT(31)
847
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100848/* HW TX descriptor for PPv2.1 */
849struct mvpp21_tx_desc {
Marcin Wojtas3f518502014-07-10 16:52:13 -0300850 u32 command; /* Options used by HW for packet transmitting.*/
851 u8 packet_offset; /* the offset from the buffer beginning */
852 u8 phys_txq; /* destination queue ID */
853 u16 data_size; /* data size of transmitted packet in bytes */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100854 u32 buf_dma_addr; /* physical addr of transmitted buffer */
Marcin Wojtas3f518502014-07-10 16:52:13 -0300855 u32 buf_cookie; /* cookie for access to TX buffer in tx path */
856 u32 reserved1[3]; /* hw_cmd (for future use, BM, PON, PNC) */
857 u32 reserved2; /* reserved (for future use) */
858};
859
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100860/* HW RX descriptor for PPv2.1 */
861struct mvpp21_rx_desc {
Marcin Wojtas3f518502014-07-10 16:52:13 -0300862 u32 status; /* info about received packet */
863 u16 reserved1; /* parser_info (for future use, PnC) */
864 u16 data_size; /* size of received packet in bytes */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100865 u32 buf_dma_addr; /* physical address of the buffer */
Marcin Wojtas3f518502014-07-10 16:52:13 -0300866 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
867 u16 reserved2; /* gem_port_id (for future use, PON) */
868 u16 reserved3; /* csum_l4 (for future use, PnC) */
869 u8 reserved4; /* bm_qset (for future use, BM) */
870 u8 reserved5;
871 u16 reserved6; /* classify_info (for future use, PnC) */
872 u32 reserved7; /* flow_id (for future use, PnC) */
873 u32 reserved8;
874};
875
Thomas Petazzonie7c53592017-03-07 16:53:08 +0100876/* HW TX descriptor for PPv2.2 */
877struct mvpp22_tx_desc {
878 u32 command;
879 u8 packet_offset;
880 u8 phys_txq;
881 u16 data_size;
882 u64 reserved1;
883 u64 buf_dma_addr_ptp;
884 u64 buf_cookie_misc;
885};
886
887/* HW RX descriptor for PPv2.2 */
888struct mvpp22_rx_desc {
889 u32 status;
890 u16 reserved1;
891 u16 data_size;
892 u32 reserved2;
893 u32 reserved3;
894 u64 buf_dma_addr_key_hash;
895 u64 buf_cookie_misc;
896};
897
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100898/* Opaque type used by the driver to manipulate the HW TX and RX
899 * descriptors
900 */
901struct mvpp2_tx_desc {
902 union {
903 struct mvpp21_tx_desc pp21;
Thomas Petazzonie7c53592017-03-07 16:53:08 +0100904 struct mvpp22_tx_desc pp22;
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100905 };
906};
907
908struct mvpp2_rx_desc {
909 union {
910 struct mvpp21_rx_desc pp21;
Thomas Petazzonie7c53592017-03-07 16:53:08 +0100911 struct mvpp22_rx_desc pp22;
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100912 };
913};
914
Thomas Petazzoni83544912016-12-21 11:28:49 +0100915struct mvpp2_txq_pcpu_buf {
916 /* Transmitted SKB */
917 struct sk_buff *skb;
918
919 /* Physical address of transmitted buffer */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100920 dma_addr_t dma;
Thomas Petazzoni83544912016-12-21 11:28:49 +0100921
922 /* Size transmitted */
923 size_t size;
924};
925
Marcin Wojtas3f518502014-07-10 16:52:13 -0300926/* Per-CPU Tx queue control */
927struct mvpp2_txq_pcpu {
928 int cpu;
929
930 /* Number of Tx DMA descriptors in the descriptor ring */
931 int size;
932
933 /* Number of currently used Tx DMA descriptor in the
934 * descriptor ring
935 */
936 int count;
937
938 /* Number of Tx DMA descriptors reserved for each CPU */
939 int reserved_num;
940
Thomas Petazzoni83544912016-12-21 11:28:49 +0100941 /* Infos about transmitted buffers */
942 struct mvpp2_txq_pcpu_buf *buffs;
Marcin Wojtas71ce3912015-08-06 19:00:29 +0200943
Marcin Wojtas3f518502014-07-10 16:52:13 -0300944 /* Index of last TX DMA descriptor that was inserted */
945 int txq_put_index;
946
947 /* Index of the TX DMA descriptor to be cleaned up */
948 int txq_get_index;
949};
950
951struct mvpp2_tx_queue {
952 /* Physical number of this Tx queue */
953 u8 id;
954
955 /* Logical number of this Tx queue */
956 u8 log_id;
957
958 /* Number of Tx DMA descriptors in the descriptor ring */
959 int size;
960
961 /* Number of currently used Tx DMA descriptor in the descriptor ring */
962 int count;
963
964 /* Per-CPU control of physical Tx queues */
965 struct mvpp2_txq_pcpu __percpu *pcpu;
966
Marcin Wojtas3f518502014-07-10 16:52:13 -0300967 u32 done_pkts_coal;
968
969 /* Virtual address of thex Tx DMA descriptors array */
970 struct mvpp2_tx_desc *descs;
971
972 /* DMA address of the Tx DMA descriptors array */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100973 dma_addr_t descs_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300974
975 /* Index of the last Tx DMA descriptor */
976 int last_desc;
977
978 /* Index of the next Tx DMA descriptor to process */
979 int next_desc_to_proc;
980};
981
982struct mvpp2_rx_queue {
983 /* RX queue number, in the range 0-31 for physical RXQs */
984 u8 id;
985
986 /* Num of rx descriptors in the rx descriptor ring */
987 int size;
988
989 u32 pkts_coal;
990 u32 time_coal;
991
992 /* Virtual address of the RX DMA descriptors array */
993 struct mvpp2_rx_desc *descs;
994
995 /* DMA address of the RX DMA descriptors array */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100996 dma_addr_t descs_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300997
998 /* Index of the last RX DMA descriptor */
999 int last_desc;
1000
1001 /* Index of the next RX DMA descriptor to process */
1002 int next_desc_to_proc;
1003
1004 /* ID of port to which physical RXQ is mapped */
1005 int port;
1006
1007 /* Port's logic RXQ number to which physical RXQ is mapped */
1008 int logic_rxq;
1009};
1010
1011union mvpp2_prs_tcam_entry {
1012 u32 word[MVPP2_PRS_TCAM_WORDS];
1013 u8 byte[MVPP2_PRS_TCAM_WORDS * 4];
1014};
1015
1016union mvpp2_prs_sram_entry {
1017 u32 word[MVPP2_PRS_SRAM_WORDS];
1018 u8 byte[MVPP2_PRS_SRAM_WORDS * 4];
1019};
1020
1021struct mvpp2_prs_entry {
1022 u32 index;
1023 union mvpp2_prs_tcam_entry tcam;
1024 union mvpp2_prs_sram_entry sram;
1025};
1026
1027struct mvpp2_prs_shadow {
1028 bool valid;
1029 bool finish;
1030
1031 /* Lookup ID */
1032 int lu;
1033
1034 /* User defined offset */
1035 int udf;
1036
1037 /* Result info */
1038 u32 ri;
1039 u32 ri_mask;
1040};
1041
1042struct mvpp2_cls_flow_entry {
1043 u32 index;
1044 u32 data[MVPP2_CLS_FLOWS_TBL_DATA_WORDS];
1045};
1046
1047struct mvpp2_cls_lookup_entry {
1048 u32 lkpid;
1049 u32 way;
1050 u32 data;
1051};
1052
1053struct mvpp2_bm_pool {
1054 /* Pool number in the range 0-7 */
1055 int id;
1056 enum mvpp2_bm_type type;
1057
1058 /* Buffer Pointers Pool External (BPPE) size */
1059 int size;
Thomas Petazzonid01524d2017-03-07 16:53:09 +01001060 /* BPPE size in bytes */
1061 int size_bytes;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001062 /* Number of buffers for this pool */
1063 int buf_num;
1064 /* Pool buffer size */
1065 int buf_size;
1066 /* Packet size */
1067 int pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01001068 int frag_size;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001069
1070 /* BPPE virtual base address */
1071 u32 *virt_addr;
Thomas Petazzoni20396132017-03-07 16:53:00 +01001072 /* BPPE DMA base address */
1073 dma_addr_t dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001074
1075 /* Ports using BM pool */
1076 u32 port_map;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001077};
1078
Marcin Wojtas3f518502014-07-10 16:52:13 -03001079#define MVPP2_DRIVER_NAME "mvpp2"
1080#define MVPP2_DRIVER_VERSION "1.0"
1081
1082/* Utility/helper methods */
1083
1084static void mvpp2_write(struct mvpp2 *priv, u32 offset, u32 data)
1085{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001086 writel(data, priv->swth_base[0] + offset);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001087}
1088
1089static u32 mvpp2_read(struct mvpp2 *priv, u32 offset)
1090{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001091 return readl(priv->swth_base[0] + offset);
Thomas Petazzonia7868412017-03-07 16:53:13 +01001092}
1093
1094/* These accessors should be used to access:
1095 *
1096 * - per-CPU registers, where each CPU has its own copy of the
1097 * register.
1098 *
1099 * MVPP2_BM_VIRT_ALLOC_REG
1100 * MVPP2_BM_ADDR_HIGH_ALLOC
1101 * MVPP22_BM_ADDR_HIGH_RLS_REG
1102 * MVPP2_BM_VIRT_RLS_REG
1103 * MVPP2_ISR_RX_TX_CAUSE_REG
1104 * MVPP2_ISR_RX_TX_MASK_REG
1105 * MVPP2_TXQ_NUM_REG
1106 * MVPP2_AGGR_TXQ_UPDATE_REG
1107 * MVPP2_TXQ_RSVD_REQ_REG
1108 * MVPP2_TXQ_RSVD_RSLT_REG
1109 * MVPP2_TXQ_SENT_REG
1110 * MVPP2_RXQ_NUM_REG
1111 *
1112 * - global registers that must be accessed through a specific CPU
1113 * window, because they are related to an access to a per-CPU
1114 * register
1115 *
1116 * MVPP2_BM_PHY_ALLOC_REG (related to MVPP2_BM_VIRT_ALLOC_REG)
1117 * MVPP2_BM_PHY_RLS_REG (related to MVPP2_BM_VIRT_RLS_REG)
1118 * MVPP2_RXQ_THRESH_REG (related to MVPP2_RXQ_NUM_REG)
1119 * MVPP2_RXQ_DESC_ADDR_REG (related to MVPP2_RXQ_NUM_REG)
1120 * MVPP2_RXQ_DESC_SIZE_REG (related to MVPP2_RXQ_NUM_REG)
1121 * MVPP2_RXQ_INDEX_REG (related to MVPP2_RXQ_NUM_REG)
1122 * MVPP2_TXQ_PENDING_REG (related to MVPP2_TXQ_NUM_REG)
1123 * MVPP2_TXQ_DESC_ADDR_REG (related to MVPP2_TXQ_NUM_REG)
1124 * MVPP2_TXQ_DESC_SIZE_REG (related to MVPP2_TXQ_NUM_REG)
1125 * MVPP2_TXQ_INDEX_REG (related to MVPP2_TXQ_NUM_REG)
1126 * MVPP2_TXQ_PENDING_REG (related to MVPP2_TXQ_NUM_REG)
1127 * MVPP2_TXQ_PREF_BUF_REG (related to MVPP2_TXQ_NUM_REG)
1128 * MVPP2_TXQ_PREF_BUF_REG (related to MVPP2_TXQ_NUM_REG)
1129 */
1130static void mvpp2_percpu_write(struct mvpp2 *priv, int cpu,
1131 u32 offset, u32 data)
1132{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001133 writel(data, priv->swth_base[cpu] + offset);
Thomas Petazzonia7868412017-03-07 16:53:13 +01001134}
1135
1136static u32 mvpp2_percpu_read(struct mvpp2 *priv, int cpu,
1137 u32 offset)
1138{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001139 return readl(priv->swth_base[cpu] + offset);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001140}
1141
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001142static dma_addr_t mvpp2_txdesc_dma_addr_get(struct mvpp2_port *port,
1143 struct mvpp2_tx_desc *tx_desc)
1144{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001145 if (port->priv->hw_version == MVPP21)
1146 return tx_desc->pp21.buf_dma_addr;
1147 else
1148 return tx_desc->pp22.buf_dma_addr_ptp & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001149}
1150
1151static void mvpp2_txdesc_dma_addr_set(struct mvpp2_port *port,
1152 struct mvpp2_tx_desc *tx_desc,
1153 dma_addr_t dma_addr)
1154{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001155 if (port->priv->hw_version == MVPP21) {
1156 tx_desc->pp21.buf_dma_addr = dma_addr;
1157 } else {
1158 u64 val = (u64)dma_addr;
1159
1160 tx_desc->pp22.buf_dma_addr_ptp &= ~GENMASK_ULL(40, 0);
1161 tx_desc->pp22.buf_dma_addr_ptp |= val;
1162 }
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001163}
1164
1165static size_t mvpp2_txdesc_size_get(struct mvpp2_port *port,
1166 struct mvpp2_tx_desc *tx_desc)
1167{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001168 if (port->priv->hw_version == MVPP21)
1169 return tx_desc->pp21.data_size;
1170 else
1171 return tx_desc->pp22.data_size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001172}
1173
1174static void mvpp2_txdesc_size_set(struct mvpp2_port *port,
1175 struct mvpp2_tx_desc *tx_desc,
1176 size_t size)
1177{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001178 if (port->priv->hw_version == MVPP21)
1179 tx_desc->pp21.data_size = size;
1180 else
1181 tx_desc->pp22.data_size = size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001182}
1183
1184static void mvpp2_txdesc_txq_set(struct mvpp2_port *port,
1185 struct mvpp2_tx_desc *tx_desc,
1186 unsigned int txq)
1187{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001188 if (port->priv->hw_version == MVPP21)
1189 tx_desc->pp21.phys_txq = txq;
1190 else
1191 tx_desc->pp22.phys_txq = txq;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001192}
1193
1194static void mvpp2_txdesc_cmd_set(struct mvpp2_port *port,
1195 struct mvpp2_tx_desc *tx_desc,
1196 unsigned int command)
1197{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001198 if (port->priv->hw_version == MVPP21)
1199 tx_desc->pp21.command = command;
1200 else
1201 tx_desc->pp22.command = command;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001202}
1203
1204static void mvpp2_txdesc_offset_set(struct mvpp2_port *port,
1205 struct mvpp2_tx_desc *tx_desc,
1206 unsigned int offset)
1207{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001208 if (port->priv->hw_version == MVPP21)
1209 tx_desc->pp21.packet_offset = offset;
1210 else
1211 tx_desc->pp22.packet_offset = offset;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001212}
1213
1214static unsigned int mvpp2_txdesc_offset_get(struct mvpp2_port *port,
1215 struct mvpp2_tx_desc *tx_desc)
1216{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001217 if (port->priv->hw_version == MVPP21)
1218 return tx_desc->pp21.packet_offset;
1219 else
1220 return tx_desc->pp22.packet_offset;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001221}
1222
1223static dma_addr_t mvpp2_rxdesc_dma_addr_get(struct mvpp2_port *port,
1224 struct mvpp2_rx_desc *rx_desc)
1225{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001226 if (port->priv->hw_version == MVPP21)
1227 return rx_desc->pp21.buf_dma_addr;
1228 else
1229 return rx_desc->pp22.buf_dma_addr_key_hash & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001230}
1231
1232static unsigned long mvpp2_rxdesc_cookie_get(struct mvpp2_port *port,
1233 struct mvpp2_rx_desc *rx_desc)
1234{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001235 if (port->priv->hw_version == MVPP21)
1236 return rx_desc->pp21.buf_cookie;
1237 else
1238 return rx_desc->pp22.buf_cookie_misc & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001239}
1240
1241static size_t mvpp2_rxdesc_size_get(struct mvpp2_port *port,
1242 struct mvpp2_rx_desc *rx_desc)
1243{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001244 if (port->priv->hw_version == MVPP21)
1245 return rx_desc->pp21.data_size;
1246 else
1247 return rx_desc->pp22.data_size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001248}
1249
1250static u32 mvpp2_rxdesc_status_get(struct mvpp2_port *port,
1251 struct mvpp2_rx_desc *rx_desc)
1252{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001253 if (port->priv->hw_version == MVPP21)
1254 return rx_desc->pp21.status;
1255 else
1256 return rx_desc->pp22.status;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001257}
1258
Marcin Wojtas3f518502014-07-10 16:52:13 -03001259static void mvpp2_txq_inc_get(struct mvpp2_txq_pcpu *txq_pcpu)
1260{
1261 txq_pcpu->txq_get_index++;
1262 if (txq_pcpu->txq_get_index == txq_pcpu->size)
1263 txq_pcpu->txq_get_index = 0;
1264}
1265
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001266static void mvpp2_txq_inc_put(struct mvpp2_port *port,
1267 struct mvpp2_txq_pcpu *txq_pcpu,
Marcin Wojtas71ce3912015-08-06 19:00:29 +02001268 struct sk_buff *skb,
1269 struct mvpp2_tx_desc *tx_desc)
Marcin Wojtas3f518502014-07-10 16:52:13 -03001270{
Thomas Petazzoni83544912016-12-21 11:28:49 +01001271 struct mvpp2_txq_pcpu_buf *tx_buf =
1272 txq_pcpu->buffs + txq_pcpu->txq_put_index;
1273 tx_buf->skb = skb;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001274 tx_buf->size = mvpp2_txdesc_size_get(port, tx_desc);
1275 tx_buf->dma = mvpp2_txdesc_dma_addr_get(port, tx_desc) +
1276 mvpp2_txdesc_offset_get(port, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001277 txq_pcpu->txq_put_index++;
1278 if (txq_pcpu->txq_put_index == txq_pcpu->size)
1279 txq_pcpu->txq_put_index = 0;
1280}
1281
1282/* Get number of physical egress port */
1283static inline int mvpp2_egress_port(struct mvpp2_port *port)
1284{
1285 return MVPP2_MAX_TCONT + port->id;
1286}
1287
1288/* Get number of physical TXQ */
1289static inline int mvpp2_txq_phys(int port, int txq)
1290{
1291 return (MVPP2_MAX_TCONT + port) * MVPP2_MAX_TXQ + txq;
1292}
1293
1294/* Parser configuration routines */
1295
1296/* Update parser tcam and sram hw entries */
1297static int mvpp2_prs_hw_write(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1298{
1299 int i;
1300
1301 if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1302 return -EINVAL;
1303
1304 /* Clear entry invalidation bit */
1305 pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] &= ~MVPP2_PRS_TCAM_INV_MASK;
1306
1307 /* Write tcam index - indirect access */
1308 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1309 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1310 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), pe->tcam.word[i]);
1311
1312 /* Write sram index - indirect access */
1313 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1314 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1315 mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), pe->sram.word[i]);
1316
1317 return 0;
1318}
1319
1320/* Read tcam entry from hw */
1321static int mvpp2_prs_hw_read(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1322{
1323 int i;
1324
1325 if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1326 return -EINVAL;
1327
1328 /* Write tcam index - indirect access */
1329 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1330
1331 pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] = mvpp2_read(priv,
1332 MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD));
1333 if (pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] & MVPP2_PRS_TCAM_INV_MASK)
1334 return MVPP2_PRS_TCAM_ENTRY_INVALID;
1335
1336 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1337 pe->tcam.word[i] = mvpp2_read(priv, MVPP2_PRS_TCAM_DATA_REG(i));
1338
1339 /* Write sram index - indirect access */
1340 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1341 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1342 pe->sram.word[i] = mvpp2_read(priv, MVPP2_PRS_SRAM_DATA_REG(i));
1343
1344 return 0;
1345}
1346
1347/* Invalidate tcam hw entry */
1348static void mvpp2_prs_hw_inv(struct mvpp2 *priv, int index)
1349{
1350 /* Write index - indirect access */
1351 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
1352 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD),
1353 MVPP2_PRS_TCAM_INV_MASK);
1354}
1355
1356/* Enable shadow table entry and set its lookup ID */
1357static void mvpp2_prs_shadow_set(struct mvpp2 *priv, int index, int lu)
1358{
1359 priv->prs_shadow[index].valid = true;
1360 priv->prs_shadow[index].lu = lu;
1361}
1362
1363/* Update ri fields in shadow table entry */
1364static void mvpp2_prs_shadow_ri_set(struct mvpp2 *priv, int index,
1365 unsigned int ri, unsigned int ri_mask)
1366{
1367 priv->prs_shadow[index].ri_mask = ri_mask;
1368 priv->prs_shadow[index].ri = ri;
1369}
1370
1371/* Update lookup field in tcam sw entry */
1372static void mvpp2_prs_tcam_lu_set(struct mvpp2_prs_entry *pe, unsigned int lu)
1373{
1374 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_LU_BYTE);
1375
1376 pe->tcam.byte[MVPP2_PRS_TCAM_LU_BYTE] = lu;
1377 pe->tcam.byte[enable_off] = MVPP2_PRS_LU_MASK;
1378}
1379
1380/* Update mask for single port in tcam sw entry */
1381static void mvpp2_prs_tcam_port_set(struct mvpp2_prs_entry *pe,
1382 unsigned int port, bool add)
1383{
1384 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1385
1386 if (add)
1387 pe->tcam.byte[enable_off] &= ~(1 << port);
1388 else
1389 pe->tcam.byte[enable_off] |= 1 << port;
1390}
1391
1392/* Update port map in tcam sw entry */
1393static void mvpp2_prs_tcam_port_map_set(struct mvpp2_prs_entry *pe,
1394 unsigned int ports)
1395{
1396 unsigned char port_mask = MVPP2_PRS_PORT_MASK;
1397 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1398
1399 pe->tcam.byte[MVPP2_PRS_TCAM_PORT_BYTE] = 0;
1400 pe->tcam.byte[enable_off] &= ~port_mask;
1401 pe->tcam.byte[enable_off] |= ~ports & MVPP2_PRS_PORT_MASK;
1402}
1403
1404/* Obtain port map from tcam sw entry */
1405static unsigned int mvpp2_prs_tcam_port_map_get(struct mvpp2_prs_entry *pe)
1406{
1407 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1408
1409 return ~(pe->tcam.byte[enable_off]) & MVPP2_PRS_PORT_MASK;
1410}
1411
1412/* Set byte of data and its enable bits in tcam sw entry */
1413static void mvpp2_prs_tcam_data_byte_set(struct mvpp2_prs_entry *pe,
1414 unsigned int offs, unsigned char byte,
1415 unsigned char enable)
1416{
1417 pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)] = byte;
1418 pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)] = enable;
1419}
1420
1421/* Get byte of data and its enable bits from tcam sw entry */
1422static void mvpp2_prs_tcam_data_byte_get(struct mvpp2_prs_entry *pe,
1423 unsigned int offs, unsigned char *byte,
1424 unsigned char *enable)
1425{
1426 *byte = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)];
1427 *enable = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)];
1428}
1429
1430/* Compare tcam data bytes with a pattern */
1431static bool mvpp2_prs_tcam_data_cmp(struct mvpp2_prs_entry *pe, int offs,
1432 u16 data)
1433{
1434 int off = MVPP2_PRS_TCAM_DATA_BYTE(offs);
1435 u16 tcam_data;
1436
1437 tcam_data = (8 << pe->tcam.byte[off + 1]) | pe->tcam.byte[off];
1438 if (tcam_data != data)
1439 return false;
1440 return true;
1441}
1442
1443/* Update ai bits in tcam sw entry */
1444static void mvpp2_prs_tcam_ai_update(struct mvpp2_prs_entry *pe,
1445 unsigned int bits, unsigned int enable)
1446{
1447 int i, ai_idx = MVPP2_PRS_TCAM_AI_BYTE;
1448
1449 for (i = 0; i < MVPP2_PRS_AI_BITS; i++) {
1450
1451 if (!(enable & BIT(i)))
1452 continue;
1453
1454 if (bits & BIT(i))
1455 pe->tcam.byte[ai_idx] |= 1 << i;
1456 else
1457 pe->tcam.byte[ai_idx] &= ~(1 << i);
1458 }
1459
1460 pe->tcam.byte[MVPP2_PRS_TCAM_EN_OFFS(ai_idx)] |= enable;
1461}
1462
1463/* Get ai bits from tcam sw entry */
1464static int mvpp2_prs_tcam_ai_get(struct mvpp2_prs_entry *pe)
1465{
1466 return pe->tcam.byte[MVPP2_PRS_TCAM_AI_BYTE];
1467}
1468
1469/* Set ethertype in tcam sw entry */
1470static void mvpp2_prs_match_etype(struct mvpp2_prs_entry *pe, int offset,
1471 unsigned short ethertype)
1472{
1473 mvpp2_prs_tcam_data_byte_set(pe, offset + 0, ethertype >> 8, 0xff);
1474 mvpp2_prs_tcam_data_byte_set(pe, offset + 1, ethertype & 0xff, 0xff);
1475}
1476
1477/* Set bits in sram sw entry */
1478static void mvpp2_prs_sram_bits_set(struct mvpp2_prs_entry *pe, int bit_num,
1479 int val)
1480{
1481 pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] |= (val << (bit_num % 8));
1482}
1483
1484/* Clear bits in sram sw entry */
1485static void mvpp2_prs_sram_bits_clear(struct mvpp2_prs_entry *pe, int bit_num,
1486 int val)
1487{
1488 pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] &= ~(val << (bit_num % 8));
1489}
1490
1491/* Update ri bits in sram sw entry */
1492static void mvpp2_prs_sram_ri_update(struct mvpp2_prs_entry *pe,
1493 unsigned int bits, unsigned int mask)
1494{
1495 unsigned int i;
1496
1497 for (i = 0; i < MVPP2_PRS_SRAM_RI_CTRL_BITS; i++) {
1498 int ri_off = MVPP2_PRS_SRAM_RI_OFFS;
1499
1500 if (!(mask & BIT(i)))
1501 continue;
1502
1503 if (bits & BIT(i))
1504 mvpp2_prs_sram_bits_set(pe, ri_off + i, 1);
1505 else
1506 mvpp2_prs_sram_bits_clear(pe, ri_off + i, 1);
1507
1508 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_RI_CTRL_OFFS + i, 1);
1509 }
1510}
1511
1512/* Obtain ri bits from sram sw entry */
1513static int mvpp2_prs_sram_ri_get(struct mvpp2_prs_entry *pe)
1514{
1515 return pe->sram.word[MVPP2_PRS_SRAM_RI_WORD];
1516}
1517
1518/* Update ai bits in sram sw entry */
1519static void mvpp2_prs_sram_ai_update(struct mvpp2_prs_entry *pe,
1520 unsigned int bits, unsigned int mask)
1521{
1522 unsigned int i;
1523 int ai_off = MVPP2_PRS_SRAM_AI_OFFS;
1524
1525 for (i = 0; i < MVPP2_PRS_SRAM_AI_CTRL_BITS; i++) {
1526
1527 if (!(mask & BIT(i)))
1528 continue;
1529
1530 if (bits & BIT(i))
1531 mvpp2_prs_sram_bits_set(pe, ai_off + i, 1);
1532 else
1533 mvpp2_prs_sram_bits_clear(pe, ai_off + i, 1);
1534
1535 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_AI_CTRL_OFFS + i, 1);
1536 }
1537}
1538
1539/* Read ai bits from sram sw entry */
1540static int mvpp2_prs_sram_ai_get(struct mvpp2_prs_entry *pe)
1541{
1542 u8 bits;
1543 int ai_off = MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_AI_OFFS);
1544 int ai_en_off = ai_off + 1;
1545 int ai_shift = MVPP2_PRS_SRAM_AI_OFFS % 8;
1546
1547 bits = (pe->sram.byte[ai_off] >> ai_shift) |
1548 (pe->sram.byte[ai_en_off] << (8 - ai_shift));
1549
1550 return bits;
1551}
1552
1553/* In sram sw entry set lookup ID field of the tcam key to be used in the next
1554 * lookup interation
1555 */
1556static void mvpp2_prs_sram_next_lu_set(struct mvpp2_prs_entry *pe,
1557 unsigned int lu)
1558{
1559 int sram_next_off = MVPP2_PRS_SRAM_NEXT_LU_OFFS;
1560
1561 mvpp2_prs_sram_bits_clear(pe, sram_next_off,
1562 MVPP2_PRS_SRAM_NEXT_LU_MASK);
1563 mvpp2_prs_sram_bits_set(pe, sram_next_off, lu);
1564}
1565
1566/* In the sram sw entry set sign and value of the next lookup offset
1567 * and the offset value generated to the classifier
1568 */
1569static void mvpp2_prs_sram_shift_set(struct mvpp2_prs_entry *pe, int shift,
1570 unsigned int op)
1571{
1572 /* Set sign */
1573 if (shift < 0) {
1574 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1575 shift = 0 - shift;
1576 } else {
1577 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1578 }
1579
1580 /* Set value */
1581 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_SHIFT_OFFS)] =
1582 (unsigned char)shift;
1583
1584 /* Reset and set operation */
1585 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS,
1586 MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK);
1587 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS, op);
1588
1589 /* Set base offset as current */
1590 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1591}
1592
1593/* In the sram sw entry set sign and value of the user defined offset
1594 * generated to the classifier
1595 */
1596static void mvpp2_prs_sram_offset_set(struct mvpp2_prs_entry *pe,
1597 unsigned int type, int offset,
1598 unsigned int op)
1599{
1600 /* Set sign */
1601 if (offset < 0) {
1602 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1603 offset = 0 - offset;
1604 } else {
1605 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1606 }
1607
1608 /* Set value */
1609 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_OFFS,
1610 MVPP2_PRS_SRAM_UDF_MASK);
1611 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_OFFS, offset);
1612 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1613 MVPP2_PRS_SRAM_UDF_BITS)] &=
1614 ~(MVPP2_PRS_SRAM_UDF_MASK >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1615 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1616 MVPP2_PRS_SRAM_UDF_BITS)] |=
1617 (offset >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1618
1619 /* Set offset type */
1620 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS,
1621 MVPP2_PRS_SRAM_UDF_TYPE_MASK);
1622 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS, type);
1623
1624 /* Set offset operation */
1625 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS,
1626 MVPP2_PRS_SRAM_OP_SEL_UDF_MASK);
1627 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS, op);
1628
1629 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1630 MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] &=
1631 ~(MVPP2_PRS_SRAM_OP_SEL_UDF_MASK >>
1632 (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1633
1634 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1635 MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] |=
1636 (op >> (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1637
1638 /* Set base offset as current */
1639 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1640}
1641
1642/* Find parser flow entry */
1643static struct mvpp2_prs_entry *mvpp2_prs_flow_find(struct mvpp2 *priv, int flow)
1644{
1645 struct mvpp2_prs_entry *pe;
1646 int tid;
1647
1648 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
1649 if (!pe)
1650 return NULL;
1651 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
1652
1653 /* Go through the all entires with MVPP2_PRS_LU_FLOWS */
1654 for (tid = MVPP2_PRS_TCAM_SRAM_SIZE - 1; tid >= 0; tid--) {
1655 u8 bits;
1656
1657 if (!priv->prs_shadow[tid].valid ||
1658 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_FLOWS)
1659 continue;
1660
1661 pe->index = tid;
1662 mvpp2_prs_hw_read(priv, pe);
1663 bits = mvpp2_prs_sram_ai_get(pe);
1664
1665 /* Sram store classification lookup ID in AI bits [5:0] */
1666 if ((bits & MVPP2_PRS_FLOW_ID_MASK) == flow)
1667 return pe;
1668 }
1669 kfree(pe);
1670
1671 return NULL;
1672}
1673
1674/* Return first free tcam index, seeking from start to end */
1675static int mvpp2_prs_tcam_first_free(struct mvpp2 *priv, unsigned char start,
1676 unsigned char end)
1677{
1678 int tid;
1679
1680 if (start > end)
1681 swap(start, end);
1682
1683 if (end >= MVPP2_PRS_TCAM_SRAM_SIZE)
1684 end = MVPP2_PRS_TCAM_SRAM_SIZE - 1;
1685
1686 for (tid = start; tid <= end; tid++) {
1687 if (!priv->prs_shadow[tid].valid)
1688 return tid;
1689 }
1690
1691 return -EINVAL;
1692}
1693
1694/* Enable/disable dropping all mac da's */
1695static void mvpp2_prs_mac_drop_all_set(struct mvpp2 *priv, int port, bool add)
1696{
1697 struct mvpp2_prs_entry pe;
1698
1699 if (priv->prs_shadow[MVPP2_PE_DROP_ALL].valid) {
1700 /* Entry exist - update port only */
1701 pe.index = MVPP2_PE_DROP_ALL;
1702 mvpp2_prs_hw_read(priv, &pe);
1703 } else {
1704 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001705 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001706 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1707 pe.index = MVPP2_PE_DROP_ALL;
1708
1709 /* Non-promiscuous mode for all ports - DROP unknown packets */
1710 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
1711 MVPP2_PRS_RI_DROP_MASK);
1712
1713 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1714 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1715
1716 /* Update shadow table */
1717 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1718
1719 /* Mask all ports */
1720 mvpp2_prs_tcam_port_map_set(&pe, 0);
1721 }
1722
1723 /* Update port mask */
1724 mvpp2_prs_tcam_port_set(&pe, port, add);
1725
1726 mvpp2_prs_hw_write(priv, &pe);
1727}
1728
1729/* Set port to promiscuous mode */
1730static void mvpp2_prs_mac_promisc_set(struct mvpp2 *priv, int port, bool add)
1731{
1732 struct mvpp2_prs_entry pe;
1733
Joe Perchesdbedd442015-03-06 20:49:12 -08001734 /* Promiscuous mode - Accept unknown packets */
Marcin Wojtas3f518502014-07-10 16:52:13 -03001735
1736 if (priv->prs_shadow[MVPP2_PE_MAC_PROMISCUOUS].valid) {
1737 /* Entry exist - update port only */
1738 pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1739 mvpp2_prs_hw_read(priv, &pe);
1740 } else {
1741 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001742 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001743 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1744 pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1745
1746 /* Continue - set next lookup */
1747 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
1748
1749 /* Set result info bits */
1750 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_UCAST,
1751 MVPP2_PRS_RI_L2_CAST_MASK);
1752
1753 /* Shift to ethertype */
1754 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
1755 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1756
1757 /* Mask all ports */
1758 mvpp2_prs_tcam_port_map_set(&pe, 0);
1759
1760 /* Update shadow table */
1761 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1762 }
1763
1764 /* Update port mask */
1765 mvpp2_prs_tcam_port_set(&pe, port, add);
1766
1767 mvpp2_prs_hw_write(priv, &pe);
1768}
1769
1770/* Accept multicast */
1771static void mvpp2_prs_mac_multi_set(struct mvpp2 *priv, int port, int index,
1772 bool add)
1773{
1774 struct mvpp2_prs_entry pe;
1775 unsigned char da_mc;
1776
1777 /* Ethernet multicast address first byte is
1778 * 0x01 for IPv4 and 0x33 for IPv6
1779 */
1780 da_mc = (index == MVPP2_PE_MAC_MC_ALL) ? 0x01 : 0x33;
1781
1782 if (priv->prs_shadow[index].valid) {
1783 /* Entry exist - update port only */
1784 pe.index = index;
1785 mvpp2_prs_hw_read(priv, &pe);
1786 } else {
1787 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001788 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001789 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1790 pe.index = index;
1791
1792 /* Continue - set next lookup */
1793 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
1794
1795 /* Set result info bits */
1796 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_MCAST,
1797 MVPP2_PRS_RI_L2_CAST_MASK);
1798
1799 /* Update tcam entry data first byte */
1800 mvpp2_prs_tcam_data_byte_set(&pe, 0, da_mc, 0xff);
1801
1802 /* Shift to ethertype */
1803 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
1804 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1805
1806 /* Mask all ports */
1807 mvpp2_prs_tcam_port_map_set(&pe, 0);
1808
1809 /* Update shadow table */
1810 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1811 }
1812
1813 /* Update port mask */
1814 mvpp2_prs_tcam_port_set(&pe, port, add);
1815
1816 mvpp2_prs_hw_write(priv, &pe);
1817}
1818
1819/* Set entry for dsa packets */
1820static void mvpp2_prs_dsa_tag_set(struct mvpp2 *priv, int port, bool add,
1821 bool tagged, bool extend)
1822{
1823 struct mvpp2_prs_entry pe;
1824 int tid, shift;
1825
1826 if (extend) {
1827 tid = tagged ? MVPP2_PE_EDSA_TAGGED : MVPP2_PE_EDSA_UNTAGGED;
1828 shift = 8;
1829 } else {
1830 tid = tagged ? MVPP2_PE_DSA_TAGGED : MVPP2_PE_DSA_UNTAGGED;
1831 shift = 4;
1832 }
1833
1834 if (priv->prs_shadow[tid].valid) {
1835 /* Entry exist - update port only */
1836 pe.index = tid;
1837 mvpp2_prs_hw_read(priv, &pe);
1838 } else {
1839 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001840 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001841 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
1842 pe.index = tid;
1843
1844 /* Shift 4 bytes if DSA tag or 8 bytes in case of EDSA tag*/
1845 mvpp2_prs_sram_shift_set(&pe, shift,
1846 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1847
1848 /* Update shadow table */
1849 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
1850
1851 if (tagged) {
1852 /* Set tagged bit in DSA tag */
1853 mvpp2_prs_tcam_data_byte_set(&pe, 0,
1854 MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
1855 MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
1856 /* Clear all ai bits for next iteration */
1857 mvpp2_prs_sram_ai_update(&pe, 0,
1858 MVPP2_PRS_SRAM_AI_MASK);
1859 /* If packet is tagged continue check vlans */
1860 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
1861 } else {
1862 /* Set result info bits to 'no vlans' */
1863 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
1864 MVPP2_PRS_RI_VLAN_MASK);
1865 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
1866 }
1867
1868 /* Mask all ports */
1869 mvpp2_prs_tcam_port_map_set(&pe, 0);
1870 }
1871
1872 /* Update port mask */
1873 mvpp2_prs_tcam_port_set(&pe, port, add);
1874
1875 mvpp2_prs_hw_write(priv, &pe);
1876}
1877
1878/* Set entry for dsa ethertype */
1879static void mvpp2_prs_dsa_tag_ethertype_set(struct mvpp2 *priv, int port,
1880 bool add, bool tagged, bool extend)
1881{
1882 struct mvpp2_prs_entry pe;
1883 int tid, shift, port_mask;
1884
1885 if (extend) {
1886 tid = tagged ? MVPP2_PE_ETYPE_EDSA_TAGGED :
1887 MVPP2_PE_ETYPE_EDSA_UNTAGGED;
1888 port_mask = 0;
1889 shift = 8;
1890 } else {
1891 tid = tagged ? MVPP2_PE_ETYPE_DSA_TAGGED :
1892 MVPP2_PE_ETYPE_DSA_UNTAGGED;
1893 port_mask = MVPP2_PRS_PORT_MASK;
1894 shift = 4;
1895 }
1896
1897 if (priv->prs_shadow[tid].valid) {
1898 /* Entry exist - update port only */
1899 pe.index = tid;
1900 mvpp2_prs_hw_read(priv, &pe);
1901 } else {
1902 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001903 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001904 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
1905 pe.index = tid;
1906
1907 /* Set ethertype */
1908 mvpp2_prs_match_etype(&pe, 0, ETH_P_EDSA);
1909 mvpp2_prs_match_etype(&pe, 2, 0);
1910
1911 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DSA_MASK,
1912 MVPP2_PRS_RI_DSA_MASK);
1913 /* Shift ethertype + 2 byte reserved + tag*/
1914 mvpp2_prs_sram_shift_set(&pe, 2 + MVPP2_ETH_TYPE_LEN + shift,
1915 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1916
1917 /* Update shadow table */
1918 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
1919
1920 if (tagged) {
1921 /* Set tagged bit in DSA tag */
1922 mvpp2_prs_tcam_data_byte_set(&pe,
1923 MVPP2_ETH_TYPE_LEN + 2 + 3,
1924 MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
1925 MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
1926 /* Clear all ai bits for next iteration */
1927 mvpp2_prs_sram_ai_update(&pe, 0,
1928 MVPP2_PRS_SRAM_AI_MASK);
1929 /* If packet is tagged continue check vlans */
1930 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
1931 } else {
1932 /* Set result info bits to 'no vlans' */
1933 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
1934 MVPP2_PRS_RI_VLAN_MASK);
1935 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
1936 }
1937 /* Mask/unmask all ports, depending on dsa type */
1938 mvpp2_prs_tcam_port_map_set(&pe, port_mask);
1939 }
1940
1941 /* Update port mask */
1942 mvpp2_prs_tcam_port_set(&pe, port, add);
1943
1944 mvpp2_prs_hw_write(priv, &pe);
1945}
1946
1947/* Search for existing single/triple vlan entry */
1948static struct mvpp2_prs_entry *mvpp2_prs_vlan_find(struct mvpp2 *priv,
1949 unsigned short tpid, int ai)
1950{
1951 struct mvpp2_prs_entry *pe;
1952 int tid;
1953
1954 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
1955 if (!pe)
1956 return NULL;
1957 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
1958
1959 /* Go through the all entries with MVPP2_PRS_LU_VLAN */
1960 for (tid = MVPP2_PE_FIRST_FREE_TID;
1961 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
1962 unsigned int ri_bits, ai_bits;
1963 bool match;
1964
1965 if (!priv->prs_shadow[tid].valid ||
1966 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
1967 continue;
1968
1969 pe->index = tid;
1970
1971 mvpp2_prs_hw_read(priv, pe);
1972 match = mvpp2_prs_tcam_data_cmp(pe, 0, swab16(tpid));
1973 if (!match)
1974 continue;
1975
1976 /* Get vlan type */
1977 ri_bits = mvpp2_prs_sram_ri_get(pe);
1978 ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
1979
1980 /* Get current ai value from tcam */
1981 ai_bits = mvpp2_prs_tcam_ai_get(pe);
1982 /* Clear double vlan bit */
1983 ai_bits &= ~MVPP2_PRS_DBL_VLAN_AI_BIT;
1984
1985 if (ai != ai_bits)
1986 continue;
1987
1988 if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
1989 ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
1990 return pe;
1991 }
1992 kfree(pe);
1993
1994 return NULL;
1995}
1996
1997/* Add/update single/triple vlan entry */
1998static int mvpp2_prs_vlan_add(struct mvpp2 *priv, unsigned short tpid, int ai,
1999 unsigned int port_map)
2000{
2001 struct mvpp2_prs_entry *pe;
2002 int tid_aux, tid;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302003 int ret = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002004
2005 pe = mvpp2_prs_vlan_find(priv, tpid, ai);
2006
2007 if (!pe) {
2008 /* Create new tcam entry */
2009 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_LAST_FREE_TID,
2010 MVPP2_PE_FIRST_FREE_TID);
2011 if (tid < 0)
2012 return tid;
2013
2014 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2015 if (!pe)
2016 return -ENOMEM;
2017
2018 /* Get last double vlan tid */
2019 for (tid_aux = MVPP2_PE_LAST_FREE_TID;
2020 tid_aux >= MVPP2_PE_FIRST_FREE_TID; tid_aux--) {
2021 unsigned int ri_bits;
2022
2023 if (!priv->prs_shadow[tid_aux].valid ||
2024 priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
2025 continue;
2026
2027 pe->index = tid_aux;
2028 mvpp2_prs_hw_read(priv, pe);
2029 ri_bits = mvpp2_prs_sram_ri_get(pe);
2030 if ((ri_bits & MVPP2_PRS_RI_VLAN_MASK) ==
2031 MVPP2_PRS_RI_VLAN_DOUBLE)
2032 break;
2033 }
2034
Sudip Mukherjee43737472014-11-01 16:59:34 +05302035 if (tid <= tid_aux) {
2036 ret = -EINVAL;
Markus Elfringf9fd0e32017-04-17 13:50:35 +02002037 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302038 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002039
Markus Elfringbd6aaf52017-04-17 10:40:32 +02002040 memset(pe, 0, sizeof(*pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002041 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2042 pe->index = tid;
2043
2044 mvpp2_prs_match_etype(pe, 0, tpid);
2045
2046 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_L2);
2047 /* Shift 4 bytes - skip 1 vlan tag */
2048 mvpp2_prs_sram_shift_set(pe, MVPP2_VLAN_TAG_LEN,
2049 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2050 /* Clear all ai bits for next iteration */
2051 mvpp2_prs_sram_ai_update(pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2052
2053 if (ai == MVPP2_PRS_SINGLE_VLAN_AI) {
2054 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_SINGLE,
2055 MVPP2_PRS_RI_VLAN_MASK);
2056 } else {
2057 ai |= MVPP2_PRS_DBL_VLAN_AI_BIT;
2058 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_TRIPLE,
2059 MVPP2_PRS_RI_VLAN_MASK);
2060 }
2061 mvpp2_prs_tcam_ai_update(pe, ai, MVPP2_PRS_SRAM_AI_MASK);
2062
2063 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_VLAN);
2064 }
2065 /* Update ports' mask */
2066 mvpp2_prs_tcam_port_map_set(pe, port_map);
2067
2068 mvpp2_prs_hw_write(priv, pe);
Markus Elfringf9fd0e32017-04-17 13:50:35 +02002069free_pe:
Marcin Wojtas3f518502014-07-10 16:52:13 -03002070 kfree(pe);
2071
Sudip Mukherjee43737472014-11-01 16:59:34 +05302072 return ret;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002073}
2074
2075/* Get first free double vlan ai number */
2076static int mvpp2_prs_double_vlan_ai_free_get(struct mvpp2 *priv)
2077{
2078 int i;
2079
2080 for (i = 1; i < MVPP2_PRS_DBL_VLANS_MAX; i++) {
2081 if (!priv->prs_double_vlans[i])
2082 return i;
2083 }
2084
2085 return -EINVAL;
2086}
2087
2088/* Search for existing double vlan entry */
2089static struct mvpp2_prs_entry *mvpp2_prs_double_vlan_find(struct mvpp2 *priv,
2090 unsigned short tpid1,
2091 unsigned short tpid2)
2092{
2093 struct mvpp2_prs_entry *pe;
2094 int tid;
2095
2096 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2097 if (!pe)
2098 return NULL;
2099 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2100
2101 /* Go through the all entries with MVPP2_PRS_LU_VLAN */
2102 for (tid = MVPP2_PE_FIRST_FREE_TID;
2103 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
2104 unsigned int ri_mask;
2105 bool match;
2106
2107 if (!priv->prs_shadow[tid].valid ||
2108 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
2109 continue;
2110
2111 pe->index = tid;
2112 mvpp2_prs_hw_read(priv, pe);
2113
2114 match = mvpp2_prs_tcam_data_cmp(pe, 0, swab16(tpid1))
2115 && mvpp2_prs_tcam_data_cmp(pe, 4, swab16(tpid2));
2116
2117 if (!match)
2118 continue;
2119
2120 ri_mask = mvpp2_prs_sram_ri_get(pe) & MVPP2_PRS_RI_VLAN_MASK;
2121 if (ri_mask == MVPP2_PRS_RI_VLAN_DOUBLE)
2122 return pe;
2123 }
2124 kfree(pe);
2125
2126 return NULL;
2127}
2128
2129/* Add or update double vlan entry */
2130static int mvpp2_prs_double_vlan_add(struct mvpp2 *priv, unsigned short tpid1,
2131 unsigned short tpid2,
2132 unsigned int port_map)
2133{
2134 struct mvpp2_prs_entry *pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302135 int tid_aux, tid, ai, ret = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002136
2137 pe = mvpp2_prs_double_vlan_find(priv, tpid1, tpid2);
2138
2139 if (!pe) {
2140 /* Create new tcam entry */
2141 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2142 MVPP2_PE_LAST_FREE_TID);
2143 if (tid < 0)
2144 return tid;
2145
2146 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2147 if (!pe)
2148 return -ENOMEM;
2149
2150 /* Set ai value for new double vlan entry */
2151 ai = mvpp2_prs_double_vlan_ai_free_get(priv);
Sudip Mukherjee43737472014-11-01 16:59:34 +05302152 if (ai < 0) {
2153 ret = ai;
Markus Elfringc9a7e122017-04-17 13:03:49 +02002154 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302155 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002156
2157 /* Get first single/triple vlan tid */
2158 for (tid_aux = MVPP2_PE_FIRST_FREE_TID;
2159 tid_aux <= MVPP2_PE_LAST_FREE_TID; tid_aux++) {
2160 unsigned int ri_bits;
2161
2162 if (!priv->prs_shadow[tid_aux].valid ||
2163 priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
2164 continue;
2165
2166 pe->index = tid_aux;
2167 mvpp2_prs_hw_read(priv, pe);
2168 ri_bits = mvpp2_prs_sram_ri_get(pe);
2169 ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
2170 if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
2171 ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
2172 break;
2173 }
2174
Sudip Mukherjee43737472014-11-01 16:59:34 +05302175 if (tid >= tid_aux) {
2176 ret = -ERANGE;
Markus Elfringc9a7e122017-04-17 13:03:49 +02002177 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302178 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002179
Markus Elfringbd6aaf52017-04-17 10:40:32 +02002180 memset(pe, 0, sizeof(*pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002181 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2182 pe->index = tid;
2183
2184 priv->prs_double_vlans[ai] = true;
2185
2186 mvpp2_prs_match_etype(pe, 0, tpid1);
2187 mvpp2_prs_match_etype(pe, 4, tpid2);
2188
2189 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_VLAN);
2190 /* Shift 8 bytes - skip 2 vlan tags */
2191 mvpp2_prs_sram_shift_set(pe, 2 * MVPP2_VLAN_TAG_LEN,
2192 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2193 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_DOUBLE,
2194 MVPP2_PRS_RI_VLAN_MASK);
2195 mvpp2_prs_sram_ai_update(pe, ai | MVPP2_PRS_DBL_VLAN_AI_BIT,
2196 MVPP2_PRS_SRAM_AI_MASK);
2197
2198 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_VLAN);
2199 }
2200
2201 /* Update ports' mask */
2202 mvpp2_prs_tcam_port_map_set(pe, port_map);
2203 mvpp2_prs_hw_write(priv, pe);
Markus Elfringc9a7e122017-04-17 13:03:49 +02002204free_pe:
Marcin Wojtas3f518502014-07-10 16:52:13 -03002205 kfree(pe);
Sudip Mukherjee43737472014-11-01 16:59:34 +05302206 return ret;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002207}
2208
2209/* IPv4 header parsing for fragmentation and L4 offset */
2210static int mvpp2_prs_ip4_proto(struct mvpp2 *priv, unsigned short proto,
2211 unsigned int ri, unsigned int ri_mask)
2212{
2213 struct mvpp2_prs_entry pe;
2214 int tid;
2215
2216 if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
2217 (proto != IPPROTO_IGMP))
2218 return -EINVAL;
2219
2220 /* Fragmented packet */
2221 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2222 MVPP2_PE_LAST_FREE_TID);
2223 if (tid < 0)
2224 return tid;
2225
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002226 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002227 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
2228 pe.index = tid;
2229
2230 /* Set next lu to IPv4 */
2231 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2232 mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2233 /* Set L4 offset */
2234 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
2235 sizeof(struct iphdr) - 4,
2236 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2237 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
2238 MVPP2_PRS_IPV4_DIP_AI_BIT);
2239 mvpp2_prs_sram_ri_update(&pe, ri | MVPP2_PRS_RI_IP_FRAG_MASK,
2240 ri_mask | MVPP2_PRS_RI_IP_FRAG_MASK);
2241
2242 mvpp2_prs_tcam_data_byte_set(&pe, 5, proto, MVPP2_PRS_TCAM_PROTO_MASK);
2243 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
2244 /* Unmask all ports */
2245 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2246
2247 /* Update shadow table and hw entry */
2248 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2249 mvpp2_prs_hw_write(priv, &pe);
2250
2251 /* Not fragmented packet */
2252 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2253 MVPP2_PE_LAST_FREE_TID);
2254 if (tid < 0)
2255 return tid;
2256
2257 pe.index = tid;
2258 /* Clear ri before updating */
2259 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2260 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2261 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
2262
2263 mvpp2_prs_tcam_data_byte_set(&pe, 2, 0x00, MVPP2_PRS_TCAM_PROTO_MASK_L);
2264 mvpp2_prs_tcam_data_byte_set(&pe, 3, 0x00, MVPP2_PRS_TCAM_PROTO_MASK);
2265
2266 /* Update shadow table and hw entry */
2267 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2268 mvpp2_prs_hw_write(priv, &pe);
2269
2270 return 0;
2271}
2272
2273/* IPv4 L3 multicast or broadcast */
2274static int mvpp2_prs_ip4_cast(struct mvpp2 *priv, unsigned short l3_cast)
2275{
2276 struct mvpp2_prs_entry pe;
2277 int mask, tid;
2278
2279 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2280 MVPP2_PE_LAST_FREE_TID);
2281 if (tid < 0)
2282 return tid;
2283
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002284 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002285 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
2286 pe.index = tid;
2287
2288 switch (l3_cast) {
2289 case MVPP2_PRS_L3_MULTI_CAST:
2290 mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV4_MC,
2291 MVPP2_PRS_IPV4_MC_MASK);
2292 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
2293 MVPP2_PRS_RI_L3_ADDR_MASK);
2294 break;
2295 case MVPP2_PRS_L3_BROAD_CAST:
2296 mask = MVPP2_PRS_IPV4_BC_MASK;
2297 mvpp2_prs_tcam_data_byte_set(&pe, 0, mask, mask);
2298 mvpp2_prs_tcam_data_byte_set(&pe, 1, mask, mask);
2299 mvpp2_prs_tcam_data_byte_set(&pe, 2, mask, mask);
2300 mvpp2_prs_tcam_data_byte_set(&pe, 3, mask, mask);
2301 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_BCAST,
2302 MVPP2_PRS_RI_L3_ADDR_MASK);
2303 break;
2304 default:
2305 return -EINVAL;
2306 }
2307
2308 /* Finished: go to flowid generation */
2309 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2310 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2311
2312 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
2313 MVPP2_PRS_IPV4_DIP_AI_BIT);
2314 /* Unmask all ports */
2315 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2316
2317 /* Update shadow table and hw entry */
2318 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2319 mvpp2_prs_hw_write(priv, &pe);
2320
2321 return 0;
2322}
2323
2324/* Set entries for protocols over IPv6 */
2325static int mvpp2_prs_ip6_proto(struct mvpp2 *priv, unsigned short proto,
2326 unsigned int ri, unsigned int ri_mask)
2327{
2328 struct mvpp2_prs_entry pe;
2329 int tid;
2330
2331 if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
2332 (proto != IPPROTO_ICMPV6) && (proto != IPPROTO_IPIP))
2333 return -EINVAL;
2334
2335 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2336 MVPP2_PE_LAST_FREE_TID);
2337 if (tid < 0)
2338 return tid;
2339
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002340 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002341 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2342 pe.index = tid;
2343
2344 /* Finished: go to flowid generation */
2345 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2346 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2347 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
2348 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
2349 sizeof(struct ipv6hdr) - 6,
2350 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2351
2352 mvpp2_prs_tcam_data_byte_set(&pe, 0, proto, MVPP2_PRS_TCAM_PROTO_MASK);
2353 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
2354 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2355 /* Unmask all ports */
2356 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2357
2358 /* Write HW */
2359 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
2360 mvpp2_prs_hw_write(priv, &pe);
2361
2362 return 0;
2363}
2364
2365/* IPv6 L3 multicast entry */
2366static int mvpp2_prs_ip6_cast(struct mvpp2 *priv, unsigned short l3_cast)
2367{
2368 struct mvpp2_prs_entry pe;
2369 int tid;
2370
2371 if (l3_cast != MVPP2_PRS_L3_MULTI_CAST)
2372 return -EINVAL;
2373
2374 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2375 MVPP2_PE_LAST_FREE_TID);
2376 if (tid < 0)
2377 return tid;
2378
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002379 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002380 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2381 pe.index = tid;
2382
2383 /* Finished: go to flowid generation */
2384 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2385 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
2386 MVPP2_PRS_RI_L3_ADDR_MASK);
2387 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
2388 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2389 /* Shift back to IPv6 NH */
2390 mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2391
2392 mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV6_MC,
2393 MVPP2_PRS_IPV6_MC_MASK);
2394 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2395 /* Unmask all ports */
2396 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2397
2398 /* Update shadow table and hw entry */
2399 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
2400 mvpp2_prs_hw_write(priv, &pe);
2401
2402 return 0;
2403}
2404
2405/* Parser per-port initialization */
2406static void mvpp2_prs_hw_port_init(struct mvpp2 *priv, int port, int lu_first,
2407 int lu_max, int offset)
2408{
2409 u32 val;
2410
2411 /* Set lookup ID */
2412 val = mvpp2_read(priv, MVPP2_PRS_INIT_LOOKUP_REG);
2413 val &= ~MVPP2_PRS_PORT_LU_MASK(port);
2414 val |= MVPP2_PRS_PORT_LU_VAL(port, lu_first);
2415 mvpp2_write(priv, MVPP2_PRS_INIT_LOOKUP_REG, val);
2416
2417 /* Set maximum number of loops for packet received from port */
2418 val = mvpp2_read(priv, MVPP2_PRS_MAX_LOOP_REG(port));
2419 val &= ~MVPP2_PRS_MAX_LOOP_MASK(port);
2420 val |= MVPP2_PRS_MAX_LOOP_VAL(port, lu_max);
2421 mvpp2_write(priv, MVPP2_PRS_MAX_LOOP_REG(port), val);
2422
2423 /* Set initial offset for packet header extraction for the first
2424 * searching loop
2425 */
2426 val = mvpp2_read(priv, MVPP2_PRS_INIT_OFFS_REG(port));
2427 val &= ~MVPP2_PRS_INIT_OFF_MASK(port);
2428 val |= MVPP2_PRS_INIT_OFF_VAL(port, offset);
2429 mvpp2_write(priv, MVPP2_PRS_INIT_OFFS_REG(port), val);
2430}
2431
2432/* Default flow entries initialization for all ports */
2433static void mvpp2_prs_def_flow_init(struct mvpp2 *priv)
2434{
2435 struct mvpp2_prs_entry pe;
2436 int port;
2437
2438 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002439 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002440 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2441 pe.index = MVPP2_PE_FIRST_DEFAULT_FLOW - port;
2442
2443 /* Mask all ports */
2444 mvpp2_prs_tcam_port_map_set(&pe, 0);
2445
2446 /* Set flow ID*/
2447 mvpp2_prs_sram_ai_update(&pe, port, MVPP2_PRS_FLOW_ID_MASK);
2448 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
2449
2450 /* Update shadow table and hw entry */
2451 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_FLOWS);
2452 mvpp2_prs_hw_write(priv, &pe);
2453 }
2454}
2455
2456/* Set default entry for Marvell Header field */
2457static void mvpp2_prs_mh_init(struct mvpp2 *priv)
2458{
2459 struct mvpp2_prs_entry pe;
2460
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002461 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002462
2463 pe.index = MVPP2_PE_MH_DEFAULT;
2464 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MH);
2465 mvpp2_prs_sram_shift_set(&pe, MVPP2_MH_SIZE,
2466 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2467 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_MAC);
2468
2469 /* Unmask all ports */
2470 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2471
2472 /* Update shadow table and hw entry */
2473 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MH);
2474 mvpp2_prs_hw_write(priv, &pe);
2475}
2476
2477/* Set default entires (place holder) for promiscuous, non-promiscuous and
2478 * multicast MAC addresses
2479 */
2480static void mvpp2_prs_mac_init(struct mvpp2 *priv)
2481{
2482 struct mvpp2_prs_entry pe;
2483
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002484 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002485
2486 /* Non-promiscuous mode for all ports - DROP unknown packets */
2487 pe.index = MVPP2_PE_MAC_NON_PROMISCUOUS;
2488 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
2489
2490 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
2491 MVPP2_PRS_RI_DROP_MASK);
2492 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2493 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2494
2495 /* Unmask all ports */
2496 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2497
2498 /* Update shadow table and hw entry */
2499 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2500 mvpp2_prs_hw_write(priv, &pe);
2501
2502 /* place holders only - no ports */
2503 mvpp2_prs_mac_drop_all_set(priv, 0, false);
2504 mvpp2_prs_mac_promisc_set(priv, 0, false);
2505 mvpp2_prs_mac_multi_set(priv, MVPP2_PE_MAC_MC_ALL, 0, false);
2506 mvpp2_prs_mac_multi_set(priv, MVPP2_PE_MAC_MC_IP6, 0, false);
2507}
2508
2509/* Set default entries for various types of dsa packets */
2510static void mvpp2_prs_dsa_init(struct mvpp2 *priv)
2511{
2512 struct mvpp2_prs_entry pe;
2513
2514 /* None tagged EDSA entry - place holder */
2515 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
2516 MVPP2_PRS_EDSA);
2517
2518 /* Tagged EDSA entry - place holder */
2519 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
2520
2521 /* None tagged DSA entry - place holder */
2522 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
2523 MVPP2_PRS_DSA);
2524
2525 /* Tagged DSA entry - place holder */
2526 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
2527
2528 /* None tagged EDSA ethertype entry - place holder*/
2529 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
2530 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
2531
2532 /* Tagged EDSA ethertype entry - place holder*/
2533 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
2534 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
2535
2536 /* None tagged DSA ethertype entry */
2537 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
2538 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
2539
2540 /* Tagged DSA ethertype entry */
2541 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
2542 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
2543
2544 /* Set default entry, in case DSA or EDSA tag not found */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002545 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002546 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
2547 pe.index = MVPP2_PE_DSA_DEFAULT;
2548 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2549
2550 /* Shift 0 bytes */
2551 mvpp2_prs_sram_shift_set(&pe, 0, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2552 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2553
2554 /* Clear all sram ai bits for next iteration */
2555 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2556
2557 /* Unmask all ports */
2558 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2559
2560 mvpp2_prs_hw_write(priv, &pe);
2561}
2562
2563/* Match basic ethertypes */
2564static int mvpp2_prs_etype_init(struct mvpp2 *priv)
2565{
2566 struct mvpp2_prs_entry pe;
2567 int tid;
2568
2569 /* Ethertype: PPPoE */
2570 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2571 MVPP2_PE_LAST_FREE_TID);
2572 if (tid < 0)
2573 return tid;
2574
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002575 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002576 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2577 pe.index = tid;
2578
2579 mvpp2_prs_match_etype(&pe, 0, ETH_P_PPP_SES);
2580
2581 mvpp2_prs_sram_shift_set(&pe, MVPP2_PPPOE_HDR_SIZE,
2582 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2583 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2584 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_PPPOE_MASK,
2585 MVPP2_PRS_RI_PPPOE_MASK);
2586
2587 /* Update shadow table and hw entry */
2588 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2589 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2590 priv->prs_shadow[pe.index].finish = false;
2591 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_PPPOE_MASK,
2592 MVPP2_PRS_RI_PPPOE_MASK);
2593 mvpp2_prs_hw_write(priv, &pe);
2594
2595 /* Ethertype: ARP */
2596 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2597 MVPP2_PE_LAST_FREE_TID);
2598 if (tid < 0)
2599 return tid;
2600
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002601 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002602 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2603 pe.index = tid;
2604
2605 mvpp2_prs_match_etype(&pe, 0, ETH_P_ARP);
2606
2607 /* Generate flow in the next iteration*/
2608 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2609 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2610 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_ARP,
2611 MVPP2_PRS_RI_L3_PROTO_MASK);
2612 /* Set L3 offset */
2613 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2614 MVPP2_ETH_TYPE_LEN,
2615 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2616
2617 /* Update shadow table and hw entry */
2618 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2619 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2620 priv->prs_shadow[pe.index].finish = true;
2621 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_ARP,
2622 MVPP2_PRS_RI_L3_PROTO_MASK);
2623 mvpp2_prs_hw_write(priv, &pe);
2624
2625 /* Ethertype: LBTD */
2626 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2627 MVPP2_PE_LAST_FREE_TID);
2628 if (tid < 0)
2629 return tid;
2630
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002631 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002632 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2633 pe.index = tid;
2634
2635 mvpp2_prs_match_etype(&pe, 0, MVPP2_IP_LBDT_TYPE);
2636
2637 /* Generate flow in the next iteration*/
2638 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2639 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2640 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2641 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2642 MVPP2_PRS_RI_CPU_CODE_MASK |
2643 MVPP2_PRS_RI_UDF3_MASK);
2644 /* Set L3 offset */
2645 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2646 MVPP2_ETH_TYPE_LEN,
2647 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2648
2649 /* Update shadow table and hw entry */
2650 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2651 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2652 priv->prs_shadow[pe.index].finish = true;
2653 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2654 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2655 MVPP2_PRS_RI_CPU_CODE_MASK |
2656 MVPP2_PRS_RI_UDF3_MASK);
2657 mvpp2_prs_hw_write(priv, &pe);
2658
2659 /* Ethertype: IPv4 without options */
2660 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2661 MVPP2_PE_LAST_FREE_TID);
2662 if (tid < 0)
2663 return tid;
2664
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002665 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002666 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2667 pe.index = tid;
2668
2669 mvpp2_prs_match_etype(&pe, 0, ETH_P_IP);
2670 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2671 MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
2672 MVPP2_PRS_IPV4_HEAD_MASK |
2673 MVPP2_PRS_IPV4_IHL_MASK);
2674
2675 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2676 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
2677 MVPP2_PRS_RI_L3_PROTO_MASK);
2678 /* Skip eth_type + 4 bytes of IP header */
2679 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
2680 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2681 /* Set L3 offset */
2682 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2683 MVPP2_ETH_TYPE_LEN,
2684 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2685
2686 /* Update shadow table and hw entry */
2687 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2688 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2689 priv->prs_shadow[pe.index].finish = false;
2690 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4,
2691 MVPP2_PRS_RI_L3_PROTO_MASK);
2692 mvpp2_prs_hw_write(priv, &pe);
2693
2694 /* Ethertype: IPv4 with options */
2695 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2696 MVPP2_PE_LAST_FREE_TID);
2697 if (tid < 0)
2698 return tid;
2699
2700 pe.index = tid;
2701
2702 /* Clear tcam data before updating */
2703 pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(MVPP2_ETH_TYPE_LEN)] = 0x0;
2704 pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(MVPP2_ETH_TYPE_LEN)] = 0x0;
2705
2706 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2707 MVPP2_PRS_IPV4_HEAD,
2708 MVPP2_PRS_IPV4_HEAD_MASK);
2709
2710 /* Clear ri before updating */
2711 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2712 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2713 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
2714 MVPP2_PRS_RI_L3_PROTO_MASK);
2715
2716 /* Update shadow table and hw entry */
2717 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2718 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2719 priv->prs_shadow[pe.index].finish = false;
2720 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4_OPT,
2721 MVPP2_PRS_RI_L3_PROTO_MASK);
2722 mvpp2_prs_hw_write(priv, &pe);
2723
2724 /* Ethertype: IPv6 without options */
2725 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2726 MVPP2_PE_LAST_FREE_TID);
2727 if (tid < 0)
2728 return tid;
2729
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002730 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002731 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2732 pe.index = tid;
2733
2734 mvpp2_prs_match_etype(&pe, 0, ETH_P_IPV6);
2735
2736 /* Skip DIP of IPV6 header */
2737 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 8 +
2738 MVPP2_MAX_L3_ADDR_SIZE,
2739 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2740 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2741 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
2742 MVPP2_PRS_RI_L3_PROTO_MASK);
2743 /* Set L3 offset */
2744 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2745 MVPP2_ETH_TYPE_LEN,
2746 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2747
2748 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2749 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2750 priv->prs_shadow[pe.index].finish = false;
2751 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP6,
2752 MVPP2_PRS_RI_L3_PROTO_MASK);
2753 mvpp2_prs_hw_write(priv, &pe);
2754
2755 /* Default entry for MVPP2_PRS_LU_L2 - Unknown ethtype */
2756 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2757 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2758 pe.index = MVPP2_PE_ETH_TYPE_UN;
2759
2760 /* Unmask all ports */
2761 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2762
2763 /* Generate flow in the next iteration*/
2764 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2765 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2766 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
2767 MVPP2_PRS_RI_L3_PROTO_MASK);
2768 /* Set L3 offset even it's unknown L3 */
2769 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2770 MVPP2_ETH_TYPE_LEN,
2771 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2772
2773 /* Update shadow table and hw entry */
2774 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2775 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2776 priv->prs_shadow[pe.index].finish = true;
2777 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_UN,
2778 MVPP2_PRS_RI_L3_PROTO_MASK);
2779 mvpp2_prs_hw_write(priv, &pe);
2780
2781 return 0;
2782}
2783
2784/* Configure vlan entries and detect up to 2 successive VLAN tags.
2785 * Possible options:
2786 * 0x8100, 0x88A8
2787 * 0x8100, 0x8100
2788 * 0x8100
2789 * 0x88A8
2790 */
2791static int mvpp2_prs_vlan_init(struct platform_device *pdev, struct mvpp2 *priv)
2792{
2793 struct mvpp2_prs_entry pe;
2794 int err;
2795
2796 priv->prs_double_vlans = devm_kcalloc(&pdev->dev, sizeof(bool),
2797 MVPP2_PRS_DBL_VLANS_MAX,
2798 GFP_KERNEL);
2799 if (!priv->prs_double_vlans)
2800 return -ENOMEM;
2801
2802 /* Double VLAN: 0x8100, 0x88A8 */
2803 err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021AD,
2804 MVPP2_PRS_PORT_MASK);
2805 if (err)
2806 return err;
2807
2808 /* Double VLAN: 0x8100, 0x8100 */
2809 err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021Q,
2810 MVPP2_PRS_PORT_MASK);
2811 if (err)
2812 return err;
2813
2814 /* Single VLAN: 0x88a8 */
2815 err = mvpp2_prs_vlan_add(priv, ETH_P_8021AD, MVPP2_PRS_SINGLE_VLAN_AI,
2816 MVPP2_PRS_PORT_MASK);
2817 if (err)
2818 return err;
2819
2820 /* Single VLAN: 0x8100 */
2821 err = mvpp2_prs_vlan_add(priv, ETH_P_8021Q, MVPP2_PRS_SINGLE_VLAN_AI,
2822 MVPP2_PRS_PORT_MASK);
2823 if (err)
2824 return err;
2825
2826 /* Set default double vlan entry */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002827 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002828 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2829 pe.index = MVPP2_PE_VLAN_DBL;
2830
2831 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2832 /* Clear ai for next iterations */
2833 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2834 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_DOUBLE,
2835 MVPP2_PRS_RI_VLAN_MASK);
2836
2837 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_DBL_VLAN_AI_BIT,
2838 MVPP2_PRS_DBL_VLAN_AI_BIT);
2839 /* Unmask all ports */
2840 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2841
2842 /* Update shadow table and hw entry */
2843 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
2844 mvpp2_prs_hw_write(priv, &pe);
2845
2846 /* Set default vlan none entry */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002847 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002848 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2849 pe.index = MVPP2_PE_VLAN_NONE;
2850
2851 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2852 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
2853 MVPP2_PRS_RI_VLAN_MASK);
2854
2855 /* Unmask all ports */
2856 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2857
2858 /* Update shadow table and hw entry */
2859 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
2860 mvpp2_prs_hw_write(priv, &pe);
2861
2862 return 0;
2863}
2864
2865/* Set entries for PPPoE ethertype */
2866static int mvpp2_prs_pppoe_init(struct mvpp2 *priv)
2867{
2868 struct mvpp2_prs_entry pe;
2869 int tid;
2870
2871 /* IPv4 over PPPoE with options */
2872 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2873 MVPP2_PE_LAST_FREE_TID);
2874 if (tid < 0)
2875 return tid;
2876
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002877 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002878 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2879 pe.index = tid;
2880
2881 mvpp2_prs_match_etype(&pe, 0, PPP_IP);
2882
2883 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2884 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
2885 MVPP2_PRS_RI_L3_PROTO_MASK);
2886 /* Skip eth_type + 4 bytes of IP header */
2887 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
2888 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2889 /* Set L3 offset */
2890 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2891 MVPP2_ETH_TYPE_LEN,
2892 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2893
2894 /* Update shadow table and hw entry */
2895 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
2896 mvpp2_prs_hw_write(priv, &pe);
2897
2898 /* IPv4 over PPPoE without options */
2899 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2900 MVPP2_PE_LAST_FREE_TID);
2901 if (tid < 0)
2902 return tid;
2903
2904 pe.index = tid;
2905
2906 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2907 MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
2908 MVPP2_PRS_IPV4_HEAD_MASK |
2909 MVPP2_PRS_IPV4_IHL_MASK);
2910
2911 /* Clear ri before updating */
2912 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2913 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2914 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
2915 MVPP2_PRS_RI_L3_PROTO_MASK);
2916
2917 /* Update shadow table and hw entry */
2918 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
2919 mvpp2_prs_hw_write(priv, &pe);
2920
2921 /* IPv6 over PPPoE */
2922 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2923 MVPP2_PE_LAST_FREE_TID);
2924 if (tid < 0)
2925 return tid;
2926
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002927 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002928 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2929 pe.index = tid;
2930
2931 mvpp2_prs_match_etype(&pe, 0, PPP_IPV6);
2932
2933 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2934 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
2935 MVPP2_PRS_RI_L3_PROTO_MASK);
2936 /* Skip eth_type + 4 bytes of IPv6 header */
2937 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
2938 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2939 /* Set L3 offset */
2940 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2941 MVPP2_ETH_TYPE_LEN,
2942 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2943
2944 /* Update shadow table and hw entry */
2945 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
2946 mvpp2_prs_hw_write(priv, &pe);
2947
2948 /* Non-IP over PPPoE */
2949 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2950 MVPP2_PE_LAST_FREE_TID);
2951 if (tid < 0)
2952 return tid;
2953
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002954 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002955 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2956 pe.index = tid;
2957
2958 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
2959 MVPP2_PRS_RI_L3_PROTO_MASK);
2960
2961 /* Finished: go to flowid generation */
2962 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2963 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2964 /* Set L3 offset even if it's unknown L3 */
2965 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2966 MVPP2_ETH_TYPE_LEN,
2967 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2968
2969 /* Update shadow table and hw entry */
2970 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
2971 mvpp2_prs_hw_write(priv, &pe);
2972
2973 return 0;
2974}
2975
2976/* Initialize entries for IPv4 */
2977static int mvpp2_prs_ip4_init(struct mvpp2 *priv)
2978{
2979 struct mvpp2_prs_entry pe;
2980 int err;
2981
2982 /* Set entries for TCP, UDP and IGMP over IPv4 */
2983 err = mvpp2_prs_ip4_proto(priv, IPPROTO_TCP, MVPP2_PRS_RI_L4_TCP,
2984 MVPP2_PRS_RI_L4_PROTO_MASK);
2985 if (err)
2986 return err;
2987
2988 err = mvpp2_prs_ip4_proto(priv, IPPROTO_UDP, MVPP2_PRS_RI_L4_UDP,
2989 MVPP2_PRS_RI_L4_PROTO_MASK);
2990 if (err)
2991 return err;
2992
2993 err = mvpp2_prs_ip4_proto(priv, IPPROTO_IGMP,
2994 MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2995 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2996 MVPP2_PRS_RI_CPU_CODE_MASK |
2997 MVPP2_PRS_RI_UDF3_MASK);
2998 if (err)
2999 return err;
3000
3001 /* IPv4 Broadcast */
3002 err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_BROAD_CAST);
3003 if (err)
3004 return err;
3005
3006 /* IPv4 Multicast */
3007 err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
3008 if (err)
3009 return err;
3010
3011 /* Default IPv4 entry for unknown protocols */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003012 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003013 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
3014 pe.index = MVPP2_PE_IP4_PROTO_UN;
3015
3016 /* Set next lu to IPv4 */
3017 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
3018 mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3019 /* Set L4 offset */
3020 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
3021 sizeof(struct iphdr) - 4,
3022 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3023 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
3024 MVPP2_PRS_IPV4_DIP_AI_BIT);
3025 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3026 MVPP2_PRS_RI_L4_PROTO_MASK);
3027
3028 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
3029 /* Unmask all ports */
3030 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3031
3032 /* Update shadow table and hw entry */
3033 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3034 mvpp2_prs_hw_write(priv, &pe);
3035
3036 /* Default IPv4 entry for unicast address */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003037 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003038 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
3039 pe.index = MVPP2_PE_IP4_ADDR_UN;
3040
3041 /* Finished: go to flowid generation */
3042 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3043 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3044 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
3045 MVPP2_PRS_RI_L3_ADDR_MASK);
3046
3047 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
3048 MVPP2_PRS_IPV4_DIP_AI_BIT);
3049 /* Unmask all ports */
3050 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3051
3052 /* Update shadow table and hw entry */
3053 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3054 mvpp2_prs_hw_write(priv, &pe);
3055
3056 return 0;
3057}
3058
3059/* Initialize entries for IPv6 */
3060static int mvpp2_prs_ip6_init(struct mvpp2 *priv)
3061{
3062 struct mvpp2_prs_entry pe;
3063 int tid, err;
3064
3065 /* Set entries for TCP, UDP and ICMP over IPv6 */
3066 err = mvpp2_prs_ip6_proto(priv, IPPROTO_TCP,
3067 MVPP2_PRS_RI_L4_TCP,
3068 MVPP2_PRS_RI_L4_PROTO_MASK);
3069 if (err)
3070 return err;
3071
3072 err = mvpp2_prs_ip6_proto(priv, IPPROTO_UDP,
3073 MVPP2_PRS_RI_L4_UDP,
3074 MVPP2_PRS_RI_L4_PROTO_MASK);
3075 if (err)
3076 return err;
3077
3078 err = mvpp2_prs_ip6_proto(priv, IPPROTO_ICMPV6,
3079 MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
3080 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
3081 MVPP2_PRS_RI_CPU_CODE_MASK |
3082 MVPP2_PRS_RI_UDF3_MASK);
3083 if (err)
3084 return err;
3085
3086 /* IPv4 is the last header. This is similar case as 6-TCP or 17-UDP */
3087 /* Result Info: UDF7=1, DS lite */
3088 err = mvpp2_prs_ip6_proto(priv, IPPROTO_IPIP,
3089 MVPP2_PRS_RI_UDF7_IP6_LITE,
3090 MVPP2_PRS_RI_UDF7_MASK);
3091 if (err)
3092 return err;
3093
3094 /* IPv6 multicast */
3095 err = mvpp2_prs_ip6_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
3096 if (err)
3097 return err;
3098
3099 /* Entry for checking hop limit */
3100 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3101 MVPP2_PE_LAST_FREE_TID);
3102 if (tid < 0)
3103 return tid;
3104
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003105 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003106 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3107 pe.index = tid;
3108
3109 /* Finished: go to flowid generation */
3110 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3111 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3112 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN |
3113 MVPP2_PRS_RI_DROP_MASK,
3114 MVPP2_PRS_RI_L3_PROTO_MASK |
3115 MVPP2_PRS_RI_DROP_MASK);
3116
3117 mvpp2_prs_tcam_data_byte_set(&pe, 1, 0x00, MVPP2_PRS_IPV6_HOP_MASK);
3118 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3119 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3120
3121 /* Update shadow table and hw entry */
3122 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3123 mvpp2_prs_hw_write(priv, &pe);
3124
3125 /* Default IPv6 entry for unknown protocols */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003126 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003127 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3128 pe.index = MVPP2_PE_IP6_PROTO_UN;
3129
3130 /* Finished: go to flowid generation */
3131 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3132 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3133 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3134 MVPP2_PRS_RI_L4_PROTO_MASK);
3135 /* Set L4 offset relatively to our current place */
3136 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
3137 sizeof(struct ipv6hdr) - 4,
3138 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3139
3140 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3141 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3142 /* Unmask all ports */
3143 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3144
3145 /* Update shadow table and hw entry */
3146 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3147 mvpp2_prs_hw_write(priv, &pe);
3148
3149 /* Default IPv6 entry for unknown ext protocols */
3150 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3151 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3152 pe.index = MVPP2_PE_IP6_EXT_PROTO_UN;
3153
3154 /* Finished: go to flowid generation */
3155 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3156 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3157 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3158 MVPP2_PRS_RI_L4_PROTO_MASK);
3159
3160 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_EXT_AI_BIT,
3161 MVPP2_PRS_IPV6_EXT_AI_BIT);
3162 /* Unmask all ports */
3163 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3164
3165 /* Update shadow table and hw entry */
3166 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3167 mvpp2_prs_hw_write(priv, &pe);
3168
3169 /* Default IPv6 entry for unicast address */
3170 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3171 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3172 pe.index = MVPP2_PE_IP6_ADDR_UN;
3173
3174 /* Finished: go to IPv6 again */
3175 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
3176 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
3177 MVPP2_PRS_RI_L3_ADDR_MASK);
3178 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3179 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3180 /* Shift back to IPV6 NH */
3181 mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3182
3183 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3184 /* Unmask all ports */
3185 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3186
3187 /* Update shadow table and hw entry */
3188 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
3189 mvpp2_prs_hw_write(priv, &pe);
3190
3191 return 0;
3192}
3193
3194/* Parser default initialization */
3195static int mvpp2_prs_default_init(struct platform_device *pdev,
3196 struct mvpp2 *priv)
3197{
3198 int err, index, i;
3199
3200 /* Enable tcam table */
3201 mvpp2_write(priv, MVPP2_PRS_TCAM_CTRL_REG, MVPP2_PRS_TCAM_EN_MASK);
3202
3203 /* Clear all tcam and sram entries */
3204 for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++) {
3205 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
3206 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
3207 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), 0);
3208
3209 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, index);
3210 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
3211 mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), 0);
3212 }
3213
3214 /* Invalidate all tcam entries */
3215 for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++)
3216 mvpp2_prs_hw_inv(priv, index);
3217
3218 priv->prs_shadow = devm_kcalloc(&pdev->dev, MVPP2_PRS_TCAM_SRAM_SIZE,
Markus Elfring37df25e2017-04-17 09:12:34 +02003219 sizeof(*priv->prs_shadow),
Marcin Wojtas3f518502014-07-10 16:52:13 -03003220 GFP_KERNEL);
3221 if (!priv->prs_shadow)
3222 return -ENOMEM;
3223
3224 /* Always start from lookup = 0 */
3225 for (index = 0; index < MVPP2_MAX_PORTS; index++)
3226 mvpp2_prs_hw_port_init(priv, index, MVPP2_PRS_LU_MH,
3227 MVPP2_PRS_PORT_LU_MAX, 0);
3228
3229 mvpp2_prs_def_flow_init(priv);
3230
3231 mvpp2_prs_mh_init(priv);
3232
3233 mvpp2_prs_mac_init(priv);
3234
3235 mvpp2_prs_dsa_init(priv);
3236
3237 err = mvpp2_prs_etype_init(priv);
3238 if (err)
3239 return err;
3240
3241 err = mvpp2_prs_vlan_init(pdev, priv);
3242 if (err)
3243 return err;
3244
3245 err = mvpp2_prs_pppoe_init(priv);
3246 if (err)
3247 return err;
3248
3249 err = mvpp2_prs_ip6_init(priv);
3250 if (err)
3251 return err;
3252
3253 err = mvpp2_prs_ip4_init(priv);
3254 if (err)
3255 return err;
3256
3257 return 0;
3258}
3259
3260/* Compare MAC DA with tcam entry data */
3261static bool mvpp2_prs_mac_range_equals(struct mvpp2_prs_entry *pe,
3262 const u8 *da, unsigned char *mask)
3263{
3264 unsigned char tcam_byte, tcam_mask;
3265 int index;
3266
3267 for (index = 0; index < ETH_ALEN; index++) {
3268 mvpp2_prs_tcam_data_byte_get(pe, index, &tcam_byte, &tcam_mask);
3269 if (tcam_mask != mask[index])
3270 return false;
3271
3272 if ((tcam_mask & tcam_byte) != (da[index] & mask[index]))
3273 return false;
3274 }
3275
3276 return true;
3277}
3278
3279/* Find tcam entry with matched pair <MAC DA, port> */
3280static struct mvpp2_prs_entry *
3281mvpp2_prs_mac_da_range_find(struct mvpp2 *priv, int pmap, const u8 *da,
3282 unsigned char *mask, int udf_type)
3283{
3284 struct mvpp2_prs_entry *pe;
3285 int tid;
3286
3287 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
3288 if (!pe)
3289 return NULL;
3290 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
3291
3292 /* Go through the all entires with MVPP2_PRS_LU_MAC */
3293 for (tid = MVPP2_PE_FIRST_FREE_TID;
3294 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
3295 unsigned int entry_pmap;
3296
3297 if (!priv->prs_shadow[tid].valid ||
3298 (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
3299 (priv->prs_shadow[tid].udf != udf_type))
3300 continue;
3301
3302 pe->index = tid;
3303 mvpp2_prs_hw_read(priv, pe);
3304 entry_pmap = mvpp2_prs_tcam_port_map_get(pe);
3305
3306 if (mvpp2_prs_mac_range_equals(pe, da, mask) &&
3307 entry_pmap == pmap)
3308 return pe;
3309 }
3310 kfree(pe);
3311
3312 return NULL;
3313}
3314
3315/* Update parser's mac da entry */
3316static int mvpp2_prs_mac_da_accept(struct mvpp2 *priv, int port,
3317 const u8 *da, bool add)
3318{
3319 struct mvpp2_prs_entry *pe;
3320 unsigned int pmap, len, ri;
3321 unsigned char mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3322 int tid;
3323
3324 /* Scan TCAM and see if entry with this <MAC DA, port> already exist */
3325 pe = mvpp2_prs_mac_da_range_find(priv, (1 << port), da, mask,
3326 MVPP2_PRS_UDF_MAC_DEF);
3327
3328 /* No such entry */
3329 if (!pe) {
3330 if (!add)
3331 return 0;
3332
3333 /* Create new TCAM entry */
3334 /* Find first range mac entry*/
3335 for (tid = MVPP2_PE_FIRST_FREE_TID;
3336 tid <= MVPP2_PE_LAST_FREE_TID; tid++)
3337 if (priv->prs_shadow[tid].valid &&
3338 (priv->prs_shadow[tid].lu == MVPP2_PRS_LU_MAC) &&
3339 (priv->prs_shadow[tid].udf ==
3340 MVPP2_PRS_UDF_MAC_RANGE))
3341 break;
3342
3343 /* Go through the all entries from first to last */
3344 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3345 tid - 1);
3346 if (tid < 0)
3347 return tid;
3348
3349 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
3350 if (!pe)
Amitoj Kaur Chawlac2bb7bc2016-02-04 19:25:26 +05303351 return -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003352 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
3353 pe->index = tid;
3354
3355 /* Mask all ports */
3356 mvpp2_prs_tcam_port_map_set(pe, 0);
3357 }
3358
3359 /* Update port mask */
3360 mvpp2_prs_tcam_port_set(pe, port, add);
3361
3362 /* Invalidate the entry if no ports are left enabled */
3363 pmap = mvpp2_prs_tcam_port_map_get(pe);
3364 if (pmap == 0) {
3365 if (add) {
3366 kfree(pe);
Amitoj Kaur Chawlac2bb7bc2016-02-04 19:25:26 +05303367 return -EINVAL;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003368 }
3369 mvpp2_prs_hw_inv(priv, pe->index);
3370 priv->prs_shadow[pe->index].valid = false;
3371 kfree(pe);
3372 return 0;
3373 }
3374
3375 /* Continue - set next lookup */
3376 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_DSA);
3377
3378 /* Set match on DA */
3379 len = ETH_ALEN;
3380 while (len--)
3381 mvpp2_prs_tcam_data_byte_set(pe, len, da[len], 0xff);
3382
3383 /* Set result info bits */
3384 if (is_broadcast_ether_addr(da))
3385 ri = MVPP2_PRS_RI_L2_BCAST;
3386 else if (is_multicast_ether_addr(da))
3387 ri = MVPP2_PRS_RI_L2_MCAST;
3388 else
3389 ri = MVPP2_PRS_RI_L2_UCAST | MVPP2_PRS_RI_MAC_ME_MASK;
3390
3391 mvpp2_prs_sram_ri_update(pe, ri, MVPP2_PRS_RI_L2_CAST_MASK |
3392 MVPP2_PRS_RI_MAC_ME_MASK);
3393 mvpp2_prs_shadow_ri_set(priv, pe->index, ri, MVPP2_PRS_RI_L2_CAST_MASK |
3394 MVPP2_PRS_RI_MAC_ME_MASK);
3395
3396 /* Shift to ethertype */
3397 mvpp2_prs_sram_shift_set(pe, 2 * ETH_ALEN,
3398 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3399
3400 /* Update shadow table and hw entry */
3401 priv->prs_shadow[pe->index].udf = MVPP2_PRS_UDF_MAC_DEF;
3402 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_MAC);
3403 mvpp2_prs_hw_write(priv, pe);
3404
3405 kfree(pe);
3406
3407 return 0;
3408}
3409
3410static int mvpp2_prs_update_mac_da(struct net_device *dev, const u8 *da)
3411{
3412 struct mvpp2_port *port = netdev_priv(dev);
3413 int err;
3414
3415 /* Remove old parser entry */
3416 err = mvpp2_prs_mac_da_accept(port->priv, port->id, dev->dev_addr,
3417 false);
3418 if (err)
3419 return err;
3420
3421 /* Add new parser entry */
3422 err = mvpp2_prs_mac_da_accept(port->priv, port->id, da, true);
3423 if (err)
3424 return err;
3425
3426 /* Set addr in the device */
3427 ether_addr_copy(dev->dev_addr, da);
3428
3429 return 0;
3430}
3431
3432/* Delete all port's multicast simple (not range) entries */
3433static void mvpp2_prs_mcast_del_all(struct mvpp2 *priv, int port)
3434{
3435 struct mvpp2_prs_entry pe;
3436 int index, tid;
3437
3438 for (tid = MVPP2_PE_FIRST_FREE_TID;
3439 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
3440 unsigned char da[ETH_ALEN], da_mask[ETH_ALEN];
3441
3442 if (!priv->prs_shadow[tid].valid ||
3443 (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
3444 (priv->prs_shadow[tid].udf != MVPP2_PRS_UDF_MAC_DEF))
3445 continue;
3446
3447 /* Only simple mac entries */
3448 pe.index = tid;
3449 mvpp2_prs_hw_read(priv, &pe);
3450
3451 /* Read mac addr from entry */
3452 for (index = 0; index < ETH_ALEN; index++)
3453 mvpp2_prs_tcam_data_byte_get(&pe, index, &da[index],
3454 &da_mask[index]);
3455
3456 if (is_multicast_ether_addr(da) && !is_broadcast_ether_addr(da))
3457 /* Delete this entry */
3458 mvpp2_prs_mac_da_accept(priv, port, da, false);
3459 }
3460}
3461
3462static int mvpp2_prs_tag_mode_set(struct mvpp2 *priv, int port, int type)
3463{
3464 switch (type) {
3465 case MVPP2_TAG_TYPE_EDSA:
3466 /* Add port to EDSA entries */
3467 mvpp2_prs_dsa_tag_set(priv, port, true,
3468 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3469 mvpp2_prs_dsa_tag_set(priv, port, true,
3470 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3471 /* Remove port from DSA entries */
3472 mvpp2_prs_dsa_tag_set(priv, port, false,
3473 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3474 mvpp2_prs_dsa_tag_set(priv, port, false,
3475 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3476 break;
3477
3478 case MVPP2_TAG_TYPE_DSA:
3479 /* Add port to DSA entries */
3480 mvpp2_prs_dsa_tag_set(priv, port, true,
3481 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3482 mvpp2_prs_dsa_tag_set(priv, port, true,
3483 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3484 /* Remove port from EDSA entries */
3485 mvpp2_prs_dsa_tag_set(priv, port, false,
3486 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3487 mvpp2_prs_dsa_tag_set(priv, port, false,
3488 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3489 break;
3490
3491 case MVPP2_TAG_TYPE_MH:
3492 case MVPP2_TAG_TYPE_NONE:
3493 /* Remove port form EDSA and DSA entries */
3494 mvpp2_prs_dsa_tag_set(priv, port, false,
3495 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3496 mvpp2_prs_dsa_tag_set(priv, port, false,
3497 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3498 mvpp2_prs_dsa_tag_set(priv, port, false,
3499 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3500 mvpp2_prs_dsa_tag_set(priv, port, false,
3501 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3502 break;
3503
3504 default:
3505 if ((type < 0) || (type > MVPP2_TAG_TYPE_EDSA))
3506 return -EINVAL;
3507 }
3508
3509 return 0;
3510}
3511
3512/* Set prs flow for the port */
3513static int mvpp2_prs_def_flow(struct mvpp2_port *port)
3514{
3515 struct mvpp2_prs_entry *pe;
3516 int tid;
3517
3518 pe = mvpp2_prs_flow_find(port->priv, port->id);
3519
3520 /* Such entry not exist */
3521 if (!pe) {
3522 /* Go through the all entires from last to first */
3523 tid = mvpp2_prs_tcam_first_free(port->priv,
3524 MVPP2_PE_LAST_FREE_TID,
3525 MVPP2_PE_FIRST_FREE_TID);
3526 if (tid < 0)
3527 return tid;
3528
3529 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
3530 if (!pe)
3531 return -ENOMEM;
3532
3533 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
3534 pe->index = tid;
3535
3536 /* Set flow ID*/
3537 mvpp2_prs_sram_ai_update(pe, port->id, MVPP2_PRS_FLOW_ID_MASK);
3538 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
3539
3540 /* Update shadow table */
3541 mvpp2_prs_shadow_set(port->priv, pe->index, MVPP2_PRS_LU_FLOWS);
3542 }
3543
3544 mvpp2_prs_tcam_port_map_set(pe, (1 << port->id));
3545 mvpp2_prs_hw_write(port->priv, pe);
3546 kfree(pe);
3547
3548 return 0;
3549}
3550
3551/* Classifier configuration routines */
3552
3553/* Update classification flow table registers */
3554static void mvpp2_cls_flow_write(struct mvpp2 *priv,
3555 struct mvpp2_cls_flow_entry *fe)
3556{
3557 mvpp2_write(priv, MVPP2_CLS_FLOW_INDEX_REG, fe->index);
3558 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL0_REG, fe->data[0]);
3559 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL1_REG, fe->data[1]);
3560 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL2_REG, fe->data[2]);
3561}
3562
3563/* Update classification lookup table register */
3564static void mvpp2_cls_lookup_write(struct mvpp2 *priv,
3565 struct mvpp2_cls_lookup_entry *le)
3566{
3567 u32 val;
3568
3569 val = (le->way << MVPP2_CLS_LKP_INDEX_WAY_OFFS) | le->lkpid;
3570 mvpp2_write(priv, MVPP2_CLS_LKP_INDEX_REG, val);
3571 mvpp2_write(priv, MVPP2_CLS_LKP_TBL_REG, le->data);
3572}
3573
3574/* Classifier default initialization */
3575static void mvpp2_cls_init(struct mvpp2 *priv)
3576{
3577 struct mvpp2_cls_lookup_entry le;
3578 struct mvpp2_cls_flow_entry fe;
3579 int index;
3580
3581 /* Enable classifier */
3582 mvpp2_write(priv, MVPP2_CLS_MODE_REG, MVPP2_CLS_MODE_ACTIVE_MASK);
3583
3584 /* Clear classifier flow table */
Arnd Bergmanne8f967c2016-11-24 17:28:12 +01003585 memset(&fe.data, 0, sizeof(fe.data));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003586 for (index = 0; index < MVPP2_CLS_FLOWS_TBL_SIZE; index++) {
3587 fe.index = index;
3588 mvpp2_cls_flow_write(priv, &fe);
3589 }
3590
3591 /* Clear classifier lookup table */
3592 le.data = 0;
3593 for (index = 0; index < MVPP2_CLS_LKP_TBL_SIZE; index++) {
3594 le.lkpid = index;
3595 le.way = 0;
3596 mvpp2_cls_lookup_write(priv, &le);
3597
3598 le.way = 1;
3599 mvpp2_cls_lookup_write(priv, &le);
3600 }
3601}
3602
3603static void mvpp2_cls_port_config(struct mvpp2_port *port)
3604{
3605 struct mvpp2_cls_lookup_entry le;
3606 u32 val;
3607
3608 /* Set way for the port */
3609 val = mvpp2_read(port->priv, MVPP2_CLS_PORT_WAY_REG);
3610 val &= ~MVPP2_CLS_PORT_WAY_MASK(port->id);
3611 mvpp2_write(port->priv, MVPP2_CLS_PORT_WAY_REG, val);
3612
3613 /* Pick the entry to be accessed in lookup ID decoding table
3614 * according to the way and lkpid.
3615 */
3616 le.lkpid = port->id;
3617 le.way = 0;
3618 le.data = 0;
3619
3620 /* Set initial CPU queue for receiving packets */
3621 le.data &= ~MVPP2_CLS_LKP_TBL_RXQ_MASK;
3622 le.data |= port->first_rxq;
3623
3624 /* Disable classification engines */
3625 le.data &= ~MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK;
3626
3627 /* Update lookup ID table entry */
3628 mvpp2_cls_lookup_write(port->priv, &le);
3629}
3630
3631/* Set CPU queue number for oversize packets */
3632static void mvpp2_cls_oversize_rxq_set(struct mvpp2_port *port)
3633{
3634 u32 val;
3635
3636 mvpp2_write(port->priv, MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port->id),
3637 port->first_rxq & MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK);
3638
3639 mvpp2_write(port->priv, MVPP2_CLS_SWFWD_P2HQ_REG(port->id),
3640 (port->first_rxq >> MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS));
3641
3642 val = mvpp2_read(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG);
3643 val |= MVPP2_CLS_SWFWD_PCTRL_MASK(port->id);
3644 mvpp2_write(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG, val);
3645}
3646
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003647static void *mvpp2_frag_alloc(const struct mvpp2_bm_pool *pool)
3648{
3649 if (likely(pool->frag_size <= PAGE_SIZE))
3650 return netdev_alloc_frag(pool->frag_size);
3651 else
3652 return kmalloc(pool->frag_size, GFP_ATOMIC);
3653}
3654
3655static void mvpp2_frag_free(const struct mvpp2_bm_pool *pool, void *data)
3656{
3657 if (likely(pool->frag_size <= PAGE_SIZE))
3658 skb_free_frag(data);
3659 else
3660 kfree(data);
3661}
3662
Marcin Wojtas3f518502014-07-10 16:52:13 -03003663/* Buffer Manager configuration routines */
3664
3665/* Create pool */
3666static int mvpp2_bm_pool_create(struct platform_device *pdev,
3667 struct mvpp2 *priv,
3668 struct mvpp2_bm_pool *bm_pool, int size)
3669{
Marcin Wojtas3f518502014-07-10 16:52:13 -03003670 u32 val;
3671
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003672 /* Number of buffer pointers must be a multiple of 16, as per
3673 * hardware constraints
3674 */
3675 if (!IS_ALIGNED(size, 16))
3676 return -EINVAL;
3677
3678 /* PPv2.1 needs 8 bytes per buffer pointer, PPv2.2 needs 16
3679 * bytes per buffer pointer
3680 */
3681 if (priv->hw_version == MVPP21)
3682 bm_pool->size_bytes = 2 * sizeof(u32) * size;
3683 else
3684 bm_pool->size_bytes = 2 * sizeof(u64) * size;
3685
3686 bm_pool->virt_addr = dma_alloc_coherent(&pdev->dev, bm_pool->size_bytes,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003687 &bm_pool->dma_addr,
Marcin Wojtas3f518502014-07-10 16:52:13 -03003688 GFP_KERNEL);
3689 if (!bm_pool->virt_addr)
3690 return -ENOMEM;
3691
Thomas Petazzonid3158802017-02-21 11:28:13 +01003692 if (!IS_ALIGNED((unsigned long)bm_pool->virt_addr,
3693 MVPP2_BM_POOL_PTR_ALIGN)) {
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003694 dma_free_coherent(&pdev->dev, bm_pool->size_bytes,
3695 bm_pool->virt_addr, bm_pool->dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003696 dev_err(&pdev->dev, "BM pool %d is not %d bytes aligned\n",
3697 bm_pool->id, MVPP2_BM_POOL_PTR_ALIGN);
3698 return -ENOMEM;
3699 }
3700
3701 mvpp2_write(priv, MVPP2_BM_POOL_BASE_REG(bm_pool->id),
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003702 lower_32_bits(bm_pool->dma_addr));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003703 mvpp2_write(priv, MVPP2_BM_POOL_SIZE_REG(bm_pool->id), size);
3704
3705 val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
3706 val |= MVPP2_BM_START_MASK;
3707 mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
3708
3709 bm_pool->type = MVPP2_BM_FREE;
3710 bm_pool->size = size;
3711 bm_pool->pkt_size = 0;
3712 bm_pool->buf_num = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003713
3714 return 0;
3715}
3716
3717/* Set pool buffer size */
3718static void mvpp2_bm_pool_bufsize_set(struct mvpp2 *priv,
3719 struct mvpp2_bm_pool *bm_pool,
3720 int buf_size)
3721{
3722 u32 val;
3723
3724 bm_pool->buf_size = buf_size;
3725
3726 val = ALIGN(buf_size, 1 << MVPP2_POOL_BUF_SIZE_OFFSET);
3727 mvpp2_write(priv, MVPP2_POOL_BUF_SIZE_REG(bm_pool->id), val);
3728}
3729
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003730static void mvpp2_bm_bufs_get_addrs(struct device *dev, struct mvpp2 *priv,
3731 struct mvpp2_bm_pool *bm_pool,
3732 dma_addr_t *dma_addr,
3733 phys_addr_t *phys_addr)
3734{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02003735 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01003736
3737 *dma_addr = mvpp2_percpu_read(priv, cpu,
3738 MVPP2_BM_PHY_ALLOC_REG(bm_pool->id));
3739 *phys_addr = mvpp2_percpu_read(priv, cpu, MVPP2_BM_VIRT_ALLOC_REG);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003740
3741 if (priv->hw_version == MVPP22) {
3742 u32 val;
3743 u32 dma_addr_highbits, phys_addr_highbits;
3744
Thomas Petazzonia7868412017-03-07 16:53:13 +01003745 val = mvpp2_percpu_read(priv, cpu, MVPP22_BM_ADDR_HIGH_ALLOC);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003746 dma_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_PHYS_MASK);
3747 phys_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_VIRT_MASK) >>
3748 MVPP22_BM_ADDR_HIGH_VIRT_SHIFT;
3749
3750 if (sizeof(dma_addr_t) == 8)
3751 *dma_addr |= (u64)dma_addr_highbits << 32;
3752
3753 if (sizeof(phys_addr_t) == 8)
3754 *phys_addr |= (u64)phys_addr_highbits << 32;
3755 }
Thomas Petazzonia704bb52017-06-10 23:18:22 +02003756
3757 put_cpu();
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003758}
3759
Ezequiel Garcia7861f122014-07-21 13:48:14 -03003760/* Free all buffers from the pool */
Marcin Wojtas4229d502015-12-03 15:20:50 +01003761static void mvpp2_bm_bufs_free(struct device *dev, struct mvpp2 *priv,
3762 struct mvpp2_bm_pool *bm_pool)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003763{
3764 int i;
3765
Ezequiel Garcia7861f122014-07-21 13:48:14 -03003766 for (i = 0; i < bm_pool->buf_num; i++) {
Thomas Petazzoni20396132017-03-07 16:53:00 +01003767 dma_addr_t buf_dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003768 phys_addr_t buf_phys_addr;
3769 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003770
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003771 mvpp2_bm_bufs_get_addrs(dev, priv, bm_pool,
3772 &buf_dma_addr, &buf_phys_addr);
Marcin Wojtas4229d502015-12-03 15:20:50 +01003773
Thomas Petazzoni20396132017-03-07 16:53:00 +01003774 dma_unmap_single(dev, buf_dma_addr,
Marcin Wojtas4229d502015-12-03 15:20:50 +01003775 bm_pool->buf_size, DMA_FROM_DEVICE);
3776
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003777 data = (void *)phys_to_virt(buf_phys_addr);
3778 if (!data)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003779 break;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003780
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003781 mvpp2_frag_free(bm_pool, data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003782 }
3783
3784 /* Update BM driver with number of buffers removed from pool */
3785 bm_pool->buf_num -= i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003786}
3787
3788/* Cleanup pool */
3789static int mvpp2_bm_pool_destroy(struct platform_device *pdev,
3790 struct mvpp2 *priv,
3791 struct mvpp2_bm_pool *bm_pool)
3792{
Marcin Wojtas3f518502014-07-10 16:52:13 -03003793 u32 val;
3794
Marcin Wojtas4229d502015-12-03 15:20:50 +01003795 mvpp2_bm_bufs_free(&pdev->dev, priv, bm_pool);
Ezequiel Garciad74c96c2014-07-21 13:48:13 -03003796 if (bm_pool->buf_num) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03003797 WARN(1, "cannot free all buffers in pool %d\n", bm_pool->id);
3798 return 0;
3799 }
3800
3801 val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
3802 val |= MVPP2_BM_STOP_MASK;
3803 mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
3804
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003805 dma_free_coherent(&pdev->dev, bm_pool->size_bytes,
Marcin Wojtas3f518502014-07-10 16:52:13 -03003806 bm_pool->virt_addr,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003807 bm_pool->dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003808 return 0;
3809}
3810
3811static int mvpp2_bm_pools_init(struct platform_device *pdev,
3812 struct mvpp2 *priv)
3813{
3814 int i, err, size;
3815 struct mvpp2_bm_pool *bm_pool;
3816
3817 /* Create all pools with maximum size */
3818 size = MVPP2_BM_POOL_SIZE_MAX;
3819 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
3820 bm_pool = &priv->bm_pools[i];
3821 bm_pool->id = i;
3822 err = mvpp2_bm_pool_create(pdev, priv, bm_pool, size);
3823 if (err)
3824 goto err_unroll_pools;
3825 mvpp2_bm_pool_bufsize_set(priv, bm_pool, 0);
3826 }
3827 return 0;
3828
3829err_unroll_pools:
3830 dev_err(&pdev->dev, "failed to create BM pool %d, size %d\n", i, size);
3831 for (i = i - 1; i >= 0; i--)
3832 mvpp2_bm_pool_destroy(pdev, priv, &priv->bm_pools[i]);
3833 return err;
3834}
3835
3836static int mvpp2_bm_init(struct platform_device *pdev, struct mvpp2 *priv)
3837{
3838 int i, err;
3839
3840 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
3841 /* Mask BM all interrupts */
3842 mvpp2_write(priv, MVPP2_BM_INTR_MASK_REG(i), 0);
3843 /* Clear BM cause register */
3844 mvpp2_write(priv, MVPP2_BM_INTR_CAUSE_REG(i), 0);
3845 }
3846
3847 /* Allocate and initialize BM pools */
3848 priv->bm_pools = devm_kcalloc(&pdev->dev, MVPP2_BM_POOLS_NUM,
Markus Elfring81f915e2017-04-17 09:06:33 +02003849 sizeof(*priv->bm_pools), GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003850 if (!priv->bm_pools)
3851 return -ENOMEM;
3852
3853 err = mvpp2_bm_pools_init(pdev, priv);
3854 if (err < 0)
3855 return err;
3856 return 0;
3857}
3858
3859/* Attach long pool to rxq */
3860static void mvpp2_rxq_long_pool_set(struct mvpp2_port *port,
3861 int lrxq, int long_pool)
3862{
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003863 u32 val, mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003864 int prxq;
3865
3866 /* Get queue physical ID */
3867 prxq = port->rxqs[lrxq]->id;
3868
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003869 if (port->priv->hw_version == MVPP21)
3870 mask = MVPP21_RXQ_POOL_LONG_MASK;
3871 else
3872 mask = MVPP22_RXQ_POOL_LONG_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003873
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003874 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
3875 val &= ~mask;
3876 val |= (long_pool << MVPP2_RXQ_POOL_LONG_OFFS) & mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003877 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
3878}
3879
3880/* Attach short pool to rxq */
3881static void mvpp2_rxq_short_pool_set(struct mvpp2_port *port,
3882 int lrxq, int short_pool)
3883{
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003884 u32 val, mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003885 int prxq;
3886
3887 /* Get queue physical ID */
3888 prxq = port->rxqs[lrxq]->id;
3889
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003890 if (port->priv->hw_version == MVPP21)
3891 mask = MVPP21_RXQ_POOL_SHORT_MASK;
3892 else
3893 mask = MVPP22_RXQ_POOL_SHORT_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003894
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003895 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
3896 val &= ~mask;
3897 val |= (short_pool << MVPP2_RXQ_POOL_SHORT_OFFS) & mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003898 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
3899}
3900
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003901static void *mvpp2_buf_alloc(struct mvpp2_port *port,
3902 struct mvpp2_bm_pool *bm_pool,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003903 dma_addr_t *buf_dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003904 phys_addr_t *buf_phys_addr,
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003905 gfp_t gfp_mask)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003906{
Thomas Petazzoni20396132017-03-07 16:53:00 +01003907 dma_addr_t dma_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003908 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003909
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003910 data = mvpp2_frag_alloc(bm_pool);
3911 if (!data)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003912 return NULL;
3913
Thomas Petazzoni20396132017-03-07 16:53:00 +01003914 dma_addr = dma_map_single(port->dev->dev.parent, data,
3915 MVPP2_RX_BUF_SIZE(bm_pool->pkt_size),
3916 DMA_FROM_DEVICE);
3917 if (unlikely(dma_mapping_error(port->dev->dev.parent, dma_addr))) {
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003918 mvpp2_frag_free(bm_pool, data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003919 return NULL;
3920 }
Thomas Petazzoni20396132017-03-07 16:53:00 +01003921 *buf_dma_addr = dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003922 *buf_phys_addr = virt_to_phys(data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003923
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003924 return data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003925}
3926
Marcin Wojtas3f518502014-07-10 16:52:13 -03003927/* Release buffer to BM */
3928static inline void mvpp2_bm_pool_put(struct mvpp2_port *port, int pool,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003929 dma_addr_t buf_dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003930 phys_addr_t buf_phys_addr)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003931{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02003932 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01003933
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003934 if (port->priv->hw_version == MVPP22) {
3935 u32 val = 0;
3936
3937 if (sizeof(dma_addr_t) == 8)
3938 val |= upper_32_bits(buf_dma_addr) &
3939 MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK;
3940
3941 if (sizeof(phys_addr_t) == 8)
3942 val |= (upper_32_bits(buf_phys_addr)
3943 << MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT) &
3944 MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK;
3945
Thomas Petazzonia7868412017-03-07 16:53:13 +01003946 mvpp2_percpu_write(port->priv, cpu,
3947 MVPP22_BM_ADDR_HIGH_RLS_REG, val);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003948 }
3949
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003950 /* MVPP2_BM_VIRT_RLS_REG is not interpreted by HW, and simply
3951 * returned in the "cookie" field of the RX
3952 * descriptor. Instead of storing the virtual address, we
3953 * store the physical address
3954 */
Thomas Petazzonia7868412017-03-07 16:53:13 +01003955 mvpp2_percpu_write(port->priv, cpu,
3956 MVPP2_BM_VIRT_RLS_REG, buf_phys_addr);
3957 mvpp2_percpu_write(port->priv, cpu,
3958 MVPP2_BM_PHY_RLS_REG(pool), buf_dma_addr);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02003959
3960 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03003961}
3962
Marcin Wojtas3f518502014-07-10 16:52:13 -03003963/* Allocate buffers for the pool */
3964static int mvpp2_bm_bufs_add(struct mvpp2_port *port,
3965 struct mvpp2_bm_pool *bm_pool, int buf_num)
3966{
Marcin Wojtas3f518502014-07-10 16:52:13 -03003967 int i, buf_size, total_size;
Thomas Petazzoni20396132017-03-07 16:53:00 +01003968 dma_addr_t dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003969 phys_addr_t phys_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003970 void *buf;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003971
3972 buf_size = MVPP2_RX_BUF_SIZE(bm_pool->pkt_size);
3973 total_size = MVPP2_RX_TOTAL_SIZE(buf_size);
3974
3975 if (buf_num < 0 ||
3976 (buf_num + bm_pool->buf_num > bm_pool->size)) {
3977 netdev_err(port->dev,
3978 "cannot allocate %d buffers for pool %d\n",
3979 buf_num, bm_pool->id);
3980 return 0;
3981 }
3982
Marcin Wojtas3f518502014-07-10 16:52:13 -03003983 for (i = 0; i < buf_num; i++) {
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003984 buf = mvpp2_buf_alloc(port, bm_pool, &dma_addr,
3985 &phys_addr, GFP_KERNEL);
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003986 if (!buf)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003987 break;
3988
Thomas Petazzoni20396132017-03-07 16:53:00 +01003989 mvpp2_bm_pool_put(port, bm_pool->id, dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003990 phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003991 }
3992
3993 /* Update BM driver with number of buffers added to pool */
3994 bm_pool->buf_num += i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003995
3996 netdev_dbg(port->dev,
3997 "%s pool %d: pkt_size=%4d, buf_size=%4d, total_size=%4d\n",
3998 bm_pool->type == MVPP2_BM_SWF_SHORT ? "short" : " long",
3999 bm_pool->id, bm_pool->pkt_size, buf_size, total_size);
4000
4001 netdev_dbg(port->dev,
4002 "%s pool %d: %d of %d buffers added\n",
4003 bm_pool->type == MVPP2_BM_SWF_SHORT ? "short" : " long",
4004 bm_pool->id, i, buf_num);
4005 return i;
4006}
4007
4008/* Notify the driver that BM pool is being used as specific type and return the
4009 * pool pointer on success
4010 */
4011static struct mvpp2_bm_pool *
4012mvpp2_bm_pool_use(struct mvpp2_port *port, int pool, enum mvpp2_bm_type type,
4013 int pkt_size)
4014{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004015 struct mvpp2_bm_pool *new_pool = &port->priv->bm_pools[pool];
4016 int num;
4017
4018 if (new_pool->type != MVPP2_BM_FREE && new_pool->type != type) {
4019 netdev_err(port->dev, "mixing pool types is forbidden\n");
4020 return NULL;
4021 }
4022
Marcin Wojtas3f518502014-07-10 16:52:13 -03004023 if (new_pool->type == MVPP2_BM_FREE)
4024 new_pool->type = type;
4025
4026 /* Allocate buffers in case BM pool is used as long pool, but packet
4027 * size doesn't match MTU or BM pool hasn't being used yet
4028 */
4029 if (((type == MVPP2_BM_SWF_LONG) && (pkt_size > new_pool->pkt_size)) ||
4030 (new_pool->pkt_size == 0)) {
4031 int pkts_num;
4032
4033 /* Set default buffer number or free all the buffers in case
4034 * the pool is not empty
4035 */
4036 pkts_num = new_pool->buf_num;
4037 if (pkts_num == 0)
4038 pkts_num = type == MVPP2_BM_SWF_LONG ?
4039 MVPP2_BM_LONG_BUF_NUM :
4040 MVPP2_BM_SHORT_BUF_NUM;
4041 else
Marcin Wojtas4229d502015-12-03 15:20:50 +01004042 mvpp2_bm_bufs_free(port->dev->dev.parent,
4043 port->priv, new_pool);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004044
4045 new_pool->pkt_size = pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004046 new_pool->frag_size =
4047 SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
4048 MVPP2_SKB_SHINFO_SIZE;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004049
4050 /* Allocate buffers for this pool */
4051 num = mvpp2_bm_bufs_add(port, new_pool, pkts_num);
4052 if (num != pkts_num) {
4053 WARN(1, "pool %d: %d of %d allocated\n",
4054 new_pool->id, num, pkts_num);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004055 return NULL;
4056 }
4057 }
4058
4059 mvpp2_bm_pool_bufsize_set(port->priv, new_pool,
4060 MVPP2_RX_BUF_SIZE(new_pool->pkt_size));
4061
Marcin Wojtas3f518502014-07-10 16:52:13 -03004062 return new_pool;
4063}
4064
4065/* Initialize pools for swf */
4066static int mvpp2_swf_bm_pool_init(struct mvpp2_port *port)
4067{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004068 int rxq;
4069
4070 if (!port->pool_long) {
4071 port->pool_long =
4072 mvpp2_bm_pool_use(port, MVPP2_BM_SWF_LONG_POOL(port->id),
4073 MVPP2_BM_SWF_LONG,
4074 port->pkt_size);
4075 if (!port->pool_long)
4076 return -ENOMEM;
4077
Marcin Wojtas3f518502014-07-10 16:52:13 -03004078 port->pool_long->port_map |= (1 << port->id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004079
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004080 for (rxq = 0; rxq < port->nrxqs; rxq++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004081 mvpp2_rxq_long_pool_set(port, rxq, port->pool_long->id);
4082 }
4083
4084 if (!port->pool_short) {
4085 port->pool_short =
4086 mvpp2_bm_pool_use(port, MVPP2_BM_SWF_SHORT_POOL,
4087 MVPP2_BM_SWF_SHORT,
4088 MVPP2_BM_SHORT_PKT_SIZE);
4089 if (!port->pool_short)
4090 return -ENOMEM;
4091
Marcin Wojtas3f518502014-07-10 16:52:13 -03004092 port->pool_short->port_map |= (1 << port->id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004093
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004094 for (rxq = 0; rxq < port->nrxqs; rxq++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004095 mvpp2_rxq_short_pool_set(port, rxq,
4096 port->pool_short->id);
4097 }
4098
4099 return 0;
4100}
4101
4102static int mvpp2_bm_update_mtu(struct net_device *dev, int mtu)
4103{
4104 struct mvpp2_port *port = netdev_priv(dev);
4105 struct mvpp2_bm_pool *port_pool = port->pool_long;
4106 int num, pkts_num = port_pool->buf_num;
4107 int pkt_size = MVPP2_RX_PKT_SIZE(mtu);
4108
4109 /* Update BM pool with new buffer size */
Marcin Wojtas4229d502015-12-03 15:20:50 +01004110 mvpp2_bm_bufs_free(dev->dev.parent, port->priv, port_pool);
Ezequiel Garciad74c96c2014-07-21 13:48:13 -03004111 if (port_pool->buf_num) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004112 WARN(1, "cannot free all buffers in pool %d\n", port_pool->id);
4113 return -EIO;
4114 }
4115
4116 port_pool->pkt_size = pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004117 port_pool->frag_size = SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
4118 MVPP2_SKB_SHINFO_SIZE;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004119 num = mvpp2_bm_bufs_add(port, port_pool, pkts_num);
4120 if (num != pkts_num) {
4121 WARN(1, "pool %d: %d of %d allocated\n",
4122 port_pool->id, num, pkts_num);
4123 return -EIO;
4124 }
4125
4126 mvpp2_bm_pool_bufsize_set(port->priv, port_pool,
4127 MVPP2_RX_BUF_SIZE(port_pool->pkt_size));
4128 dev->mtu = mtu;
4129 netdev_update_features(dev);
4130 return 0;
4131}
4132
4133static inline void mvpp2_interrupts_enable(struct mvpp2_port *port)
4134{
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004135 int i, sw_thread_mask = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004136
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004137 for (i = 0; i < port->nqvecs; i++)
4138 sw_thread_mask |= port->qvecs[i].sw_thread_mask;
4139
Marcin Wojtas3f518502014-07-10 16:52:13 -03004140 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004141 MVPP2_ISR_ENABLE_INTERRUPT(sw_thread_mask));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004142}
4143
4144static inline void mvpp2_interrupts_disable(struct mvpp2_port *port)
4145{
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004146 int i, sw_thread_mask = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004147
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004148 for (i = 0; i < port->nqvecs; i++)
4149 sw_thread_mask |= port->qvecs[i].sw_thread_mask;
4150
Marcin Wojtas3f518502014-07-10 16:52:13 -03004151 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004152 MVPP2_ISR_DISABLE_INTERRUPT(sw_thread_mask));
4153}
4154
4155static inline void mvpp2_qvec_interrupt_enable(struct mvpp2_queue_vector *qvec)
4156{
4157 struct mvpp2_port *port = qvec->port;
4158
4159 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
4160 MVPP2_ISR_ENABLE_INTERRUPT(qvec->sw_thread_mask));
4161}
4162
4163static inline void mvpp2_qvec_interrupt_disable(struct mvpp2_queue_vector *qvec)
4164{
4165 struct mvpp2_port *port = qvec->port;
4166
4167 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
4168 MVPP2_ISR_DISABLE_INTERRUPT(qvec->sw_thread_mask));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004169}
4170
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004171/* Mask the current CPU's Rx/Tx interrupts
4172 * Called by on_each_cpu(), guaranteed to run with migration disabled,
4173 * using smp_processor_id() is OK.
4174 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004175static void mvpp2_interrupts_mask(void *arg)
4176{
4177 struct mvpp2_port *port = arg;
4178
Thomas Petazzonia7868412017-03-07 16:53:13 +01004179 mvpp2_percpu_write(port->priv, smp_processor_id(),
4180 MVPP2_ISR_RX_TX_MASK_REG(port->id), 0);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004181}
4182
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004183/* Unmask the current CPU's Rx/Tx interrupts.
4184 * Called by on_each_cpu(), guaranteed to run with migration disabled,
4185 * using smp_processor_id() is OK.
4186 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004187static void mvpp2_interrupts_unmask(void *arg)
4188{
4189 struct mvpp2_port *port = arg;
4190
Thomas Petazzonia7868412017-03-07 16:53:13 +01004191 mvpp2_percpu_write(port->priv, smp_processor_id(),
4192 MVPP2_ISR_RX_TX_MASK_REG(port->id),
4193 (MVPP2_CAUSE_MISC_SUM_MASK |
4194 MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004195}
4196
4197/* Port configuration routines */
4198
Thomas Petazzoni26975822017-03-07 16:53:14 +01004199static void mvpp22_port_mii_set(struct mvpp2_port *port)
4200{
4201 u32 val;
4202
Thomas Petazzoni26975822017-03-07 16:53:14 +01004203 /* Only GOP port 0 has an XLG MAC */
4204 if (port->gop_id == 0) {
4205 val = readl(port->base + MVPP22_XLG_CTRL3_REG);
4206 val &= ~MVPP22_XLG_CTRL3_MACMODESELECT_MASK;
Antoine Ténart725757a2017-06-12 16:01:39 +02004207
4208 if (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
4209 port->phy_interface == PHY_INTERFACE_MODE_10GKR)
4210 val |= MVPP22_XLG_CTRL3_MACMODESELECT_10G;
4211 else
4212 val |= MVPP22_XLG_CTRL3_MACMODESELECT_GMAC;
4213
Thomas Petazzoni26975822017-03-07 16:53:14 +01004214 writel(val, port->base + MVPP22_XLG_CTRL3_REG);
4215 }
4216
4217 val = readl(port->base + MVPP22_GMAC_CTRL_4_REG);
4218 if (port->phy_interface == PHY_INTERFACE_MODE_RGMII)
4219 val |= MVPP22_CTRL4_EXT_PIN_GMII_SEL;
4220 else
4221 val &= ~MVPP22_CTRL4_EXT_PIN_GMII_SEL;
4222 val &= ~MVPP22_CTRL4_DP_CLK_SEL;
4223 val |= MVPP22_CTRL4_SYNC_BYPASS;
4224 val |= MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
4225 writel(val, port->base + MVPP22_GMAC_CTRL_4_REG);
4226}
4227
Marcin Wojtas3f518502014-07-10 16:52:13 -03004228static void mvpp2_port_mii_set(struct mvpp2_port *port)
4229{
Marcin Wojtas08a23752014-07-21 13:48:12 -03004230 u32 val;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004231
Thomas Petazzoni26975822017-03-07 16:53:14 +01004232 if (port->priv->hw_version == MVPP22)
4233 mvpp22_port_mii_set(port);
4234
Marcin Wojtas08a23752014-07-21 13:48:12 -03004235 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004236
Marcin Wojtas08a23752014-07-21 13:48:12 -03004237 switch (port->phy_interface) {
4238 case PHY_INTERFACE_MODE_SGMII:
4239 val |= MVPP2_GMAC_INBAND_AN_MASK;
4240 break;
4241 case PHY_INTERFACE_MODE_RGMII:
4242 val |= MVPP2_GMAC_PORT_RGMII_MASK;
4243 default:
4244 val &= ~MVPP2_GMAC_PCS_ENABLE_MASK;
4245 }
4246
4247 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4248}
4249
4250static void mvpp2_port_fc_adv_enable(struct mvpp2_port *port)
4251{
4252 u32 val;
4253
4254 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4255 val |= MVPP2_GMAC_FC_ADV_EN;
4256 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004257}
4258
4259static void mvpp2_port_enable(struct mvpp2_port *port)
4260{
4261 u32 val;
4262
Antoine Ténart725757a2017-06-12 16:01:39 +02004263 /* Only GOP port 0 has an XLG MAC */
4264 if (port->gop_id == 0 &&
4265 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
4266 port->phy_interface == PHY_INTERFACE_MODE_10GKR)) {
4267 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
4268 val |= MVPP22_XLG_CTRL0_PORT_EN |
4269 MVPP22_XLG_CTRL0_MAC_RESET_DIS;
4270 val &= ~MVPP22_XLG_CTRL0_MIB_CNT_DIS;
4271 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
4272 } else {
4273 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4274 val |= MVPP2_GMAC_PORT_EN_MASK;
4275 val |= MVPP2_GMAC_MIB_CNTR_EN_MASK;
4276 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4277 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004278}
4279
4280static void mvpp2_port_disable(struct mvpp2_port *port)
4281{
4282 u32 val;
4283
Antoine Ténart725757a2017-06-12 16:01:39 +02004284 /* Only GOP port 0 has an XLG MAC */
4285 if (port->gop_id == 0 &&
4286 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
4287 port->phy_interface == PHY_INTERFACE_MODE_10GKR)) {
4288 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
4289 val &= ~(MVPP22_XLG_CTRL0_PORT_EN |
4290 MVPP22_XLG_CTRL0_MAC_RESET_DIS);
4291 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
4292 } else {
4293 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4294 val &= ~(MVPP2_GMAC_PORT_EN_MASK);
4295 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4296 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004297}
4298
4299/* Set IEEE 802.3x Flow Control Xon Packet Transmission Mode */
4300static void mvpp2_port_periodic_xon_disable(struct mvpp2_port *port)
4301{
4302 u32 val;
4303
4304 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG) &
4305 ~MVPP2_GMAC_PERIODIC_XON_EN_MASK;
4306 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
4307}
4308
4309/* Configure loopback port */
4310static void mvpp2_port_loopback_set(struct mvpp2_port *port)
4311{
4312 u32 val;
4313
4314 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
4315
4316 if (port->speed == 1000)
4317 val |= MVPP2_GMAC_GMII_LB_EN_MASK;
4318 else
4319 val &= ~MVPP2_GMAC_GMII_LB_EN_MASK;
4320
4321 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
4322 val |= MVPP2_GMAC_PCS_LB_EN_MASK;
4323 else
4324 val &= ~MVPP2_GMAC_PCS_LB_EN_MASK;
4325
4326 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
4327}
4328
4329static void mvpp2_port_reset(struct mvpp2_port *port)
4330{
4331 u32 val;
4332
4333 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
4334 ~MVPP2_GMAC_PORT_RESET_MASK;
4335 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4336
4337 while (readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
4338 MVPP2_GMAC_PORT_RESET_MASK)
4339 continue;
4340}
4341
4342/* Change maximum receive size of the port */
4343static inline void mvpp2_gmac_max_rx_size_set(struct mvpp2_port *port)
4344{
4345 u32 val;
4346
4347 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4348 val &= ~MVPP2_GMAC_MAX_RX_SIZE_MASK;
4349 val |= (((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
4350 MVPP2_GMAC_MAX_RX_SIZE_OFFS);
4351 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4352}
4353
4354/* Set defaults to the MVPP2 port */
4355static void mvpp2_defaults_set(struct mvpp2_port *port)
4356{
4357 int tx_port_num, val, queue, ptxq, lrxq;
4358
Thomas Petazzoni3d9017d2017-03-07 16:53:11 +01004359 if (port->priv->hw_version == MVPP21) {
4360 /* Configure port to loopback if needed */
4361 if (port->flags & MVPP2_F_LOOPBACK)
4362 mvpp2_port_loopback_set(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004363
Thomas Petazzoni3d9017d2017-03-07 16:53:11 +01004364 /* Update TX FIFO MIN Threshold */
4365 val = readl(port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
4366 val &= ~MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK;
4367 /* Min. TX threshold must be less than minimal packet length */
4368 val |= MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(64 - 4 - 2);
4369 writel(val, port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
4370 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004371
4372 /* Disable Legacy WRR, Disable EJP, Release from reset */
4373 tx_port_num = mvpp2_egress_port(port);
4374 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG,
4375 tx_port_num);
4376 mvpp2_write(port->priv, MVPP2_TXP_SCHED_CMD_1_REG, 0);
4377
4378 /* Close bandwidth for all queues */
4379 for (queue = 0; queue < MVPP2_MAX_TXQ; queue++) {
4380 ptxq = mvpp2_txq_phys(port->id, queue);
4381 mvpp2_write(port->priv,
4382 MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(ptxq), 0);
4383 }
4384
4385 /* Set refill period to 1 usec, refill tokens
4386 * and bucket size to maximum
4387 */
4388 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PERIOD_REG,
4389 port->priv->tclk / USEC_PER_SEC);
4390 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_REFILL_REG);
4391 val &= ~MVPP2_TXP_REFILL_PERIOD_ALL_MASK;
4392 val |= MVPP2_TXP_REFILL_PERIOD_MASK(1);
4393 val |= MVPP2_TXP_REFILL_TOKENS_ALL_MASK;
4394 mvpp2_write(port->priv, MVPP2_TXP_SCHED_REFILL_REG, val);
4395 val = MVPP2_TXP_TOKEN_SIZE_MAX;
4396 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
4397
4398 /* Set MaximumLowLatencyPacketSize value to 256 */
4399 mvpp2_write(port->priv, MVPP2_RX_CTRL_REG(port->id),
4400 MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK |
4401 MVPP2_RX_LOW_LATENCY_PKT_SIZE(256));
4402
4403 /* Enable Rx cache snoop */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004404 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004405 queue = port->rxqs[lrxq]->id;
4406 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
4407 val |= MVPP2_SNOOP_PKT_SIZE_MASK |
4408 MVPP2_SNOOP_BUF_HDR_MASK;
4409 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
4410 }
4411
4412 /* At default, mask all interrupts to all present cpus */
4413 mvpp2_interrupts_disable(port);
4414}
4415
4416/* Enable/disable receiving packets */
4417static void mvpp2_ingress_enable(struct mvpp2_port *port)
4418{
4419 u32 val;
4420 int lrxq, queue;
4421
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004422 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004423 queue = port->rxqs[lrxq]->id;
4424 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
4425 val &= ~MVPP2_RXQ_DISABLE_MASK;
4426 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
4427 }
4428}
4429
4430static void mvpp2_ingress_disable(struct mvpp2_port *port)
4431{
4432 u32 val;
4433 int lrxq, queue;
4434
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004435 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004436 queue = port->rxqs[lrxq]->id;
4437 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
4438 val |= MVPP2_RXQ_DISABLE_MASK;
4439 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
4440 }
4441}
4442
4443/* Enable transmit via physical egress queue
4444 * - HW starts take descriptors from DRAM
4445 */
4446static void mvpp2_egress_enable(struct mvpp2_port *port)
4447{
4448 u32 qmap;
4449 int queue;
4450 int tx_port_num = mvpp2_egress_port(port);
4451
4452 /* Enable all initialized TXs. */
4453 qmap = 0;
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004454 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004455 struct mvpp2_tx_queue *txq = port->txqs[queue];
4456
Markus Elfringdbbb2f02017-04-17 14:07:52 +02004457 if (txq->descs)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004458 qmap |= (1 << queue);
4459 }
4460
4461 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
4462 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG, qmap);
4463}
4464
4465/* Disable transmit via physical egress queue
4466 * - HW doesn't take descriptors from DRAM
4467 */
4468static void mvpp2_egress_disable(struct mvpp2_port *port)
4469{
4470 u32 reg_data;
4471 int delay;
4472 int tx_port_num = mvpp2_egress_port(port);
4473
4474 /* Issue stop command for active channels only */
4475 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
4476 reg_data = (mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG)) &
4477 MVPP2_TXP_SCHED_ENQ_MASK;
4478 if (reg_data != 0)
4479 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG,
4480 (reg_data << MVPP2_TXP_SCHED_DISQ_OFFSET));
4481
4482 /* Wait for all Tx activity to terminate. */
4483 delay = 0;
4484 do {
4485 if (delay >= MVPP2_TX_DISABLE_TIMEOUT_MSEC) {
4486 netdev_warn(port->dev,
4487 "Tx stop timed out, status=0x%08x\n",
4488 reg_data);
4489 break;
4490 }
4491 mdelay(1);
4492 delay++;
4493
4494 /* Check port TX Command register that all
4495 * Tx queues are stopped
4496 */
4497 reg_data = mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG);
4498 } while (reg_data & MVPP2_TXP_SCHED_ENQ_MASK);
4499}
4500
4501/* Rx descriptors helper methods */
4502
4503/* Get number of Rx descriptors occupied by received packets */
4504static inline int
4505mvpp2_rxq_received(struct mvpp2_port *port, int rxq_id)
4506{
4507 u32 val = mvpp2_read(port->priv, MVPP2_RXQ_STATUS_REG(rxq_id));
4508
4509 return val & MVPP2_RXQ_OCCUPIED_MASK;
4510}
4511
4512/* Update Rx queue status with the number of occupied and available
4513 * Rx descriptor slots.
4514 */
4515static inline void
4516mvpp2_rxq_status_update(struct mvpp2_port *port, int rxq_id,
4517 int used_count, int free_count)
4518{
4519 /* Decrement the number of used descriptors and increment count
4520 * increment the number of free descriptors.
4521 */
4522 u32 val = used_count | (free_count << MVPP2_RXQ_NUM_NEW_OFFSET);
4523
4524 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_UPDATE_REG(rxq_id), val);
4525}
4526
4527/* Get pointer to next RX descriptor to be processed by SW */
4528static inline struct mvpp2_rx_desc *
4529mvpp2_rxq_next_desc_get(struct mvpp2_rx_queue *rxq)
4530{
4531 int rx_desc = rxq->next_desc_to_proc;
4532
4533 rxq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(rxq, rx_desc);
4534 prefetch(rxq->descs + rxq->next_desc_to_proc);
4535 return rxq->descs + rx_desc;
4536}
4537
4538/* Set rx queue offset */
4539static void mvpp2_rxq_offset_set(struct mvpp2_port *port,
4540 int prxq, int offset)
4541{
4542 u32 val;
4543
4544 /* Convert offset from bytes to units of 32 bytes */
4545 offset = offset >> 5;
4546
4547 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
4548 val &= ~MVPP2_RXQ_PACKET_OFFSET_MASK;
4549
4550 /* Offset is in */
4551 val |= ((offset << MVPP2_RXQ_PACKET_OFFSET_OFFS) &
4552 MVPP2_RXQ_PACKET_OFFSET_MASK);
4553
4554 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
4555}
4556
Marcin Wojtas3f518502014-07-10 16:52:13 -03004557/* Tx descriptors helper methods */
4558
Marcin Wojtas3f518502014-07-10 16:52:13 -03004559/* Get pointer to next Tx descriptor to be processed (send) by HW */
4560static struct mvpp2_tx_desc *
4561mvpp2_txq_next_desc_get(struct mvpp2_tx_queue *txq)
4562{
4563 int tx_desc = txq->next_desc_to_proc;
4564
4565 txq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(txq, tx_desc);
4566 return txq->descs + tx_desc;
4567}
4568
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004569/* Update HW with number of aggregated Tx descriptors to be sent
4570 *
4571 * Called only from mvpp2_tx(), so migration is disabled, using
4572 * smp_processor_id() is OK.
4573 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004574static void mvpp2_aggr_txq_pend_desc_add(struct mvpp2_port *port, int pending)
4575{
4576 /* aggregated access - relevant TXQ number is written in TX desc */
Thomas Petazzonia7868412017-03-07 16:53:13 +01004577 mvpp2_percpu_write(port->priv, smp_processor_id(),
4578 MVPP2_AGGR_TXQ_UPDATE_REG, pending);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004579}
4580
4581
4582/* Check if there are enough free descriptors in aggregated txq.
4583 * If not, update the number of occupied descriptors and repeat the check.
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004584 *
4585 * Called only from mvpp2_tx(), so migration is disabled, using
4586 * smp_processor_id() is OK.
Marcin Wojtas3f518502014-07-10 16:52:13 -03004587 */
4588static int mvpp2_aggr_desc_num_check(struct mvpp2 *priv,
4589 struct mvpp2_tx_queue *aggr_txq, int num)
4590{
4591 if ((aggr_txq->count + num) > aggr_txq->size) {
4592 /* Update number of occupied aggregated Tx descriptors */
4593 int cpu = smp_processor_id();
4594 u32 val = mvpp2_read(priv, MVPP2_AGGR_TXQ_STATUS_REG(cpu));
4595
4596 aggr_txq->count = val & MVPP2_AGGR_TXQ_PENDING_MASK;
4597 }
4598
4599 if ((aggr_txq->count + num) > aggr_txq->size)
4600 return -ENOMEM;
4601
4602 return 0;
4603}
4604
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004605/* Reserved Tx descriptors allocation request
4606 *
4607 * Called only from mvpp2_txq_reserved_desc_num_proc(), itself called
4608 * only by mvpp2_tx(), so migration is disabled, using
4609 * smp_processor_id() is OK.
4610 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004611static int mvpp2_txq_alloc_reserved_desc(struct mvpp2 *priv,
4612 struct mvpp2_tx_queue *txq, int num)
4613{
4614 u32 val;
Thomas Petazzonia7868412017-03-07 16:53:13 +01004615 int cpu = smp_processor_id();
Marcin Wojtas3f518502014-07-10 16:52:13 -03004616
4617 val = (txq->id << MVPP2_TXQ_RSVD_REQ_Q_OFFSET) | num;
Thomas Petazzonia7868412017-03-07 16:53:13 +01004618 mvpp2_percpu_write(priv, cpu, MVPP2_TXQ_RSVD_REQ_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004619
Thomas Petazzonia7868412017-03-07 16:53:13 +01004620 val = mvpp2_percpu_read(priv, cpu, MVPP2_TXQ_RSVD_RSLT_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004621
4622 return val & MVPP2_TXQ_RSVD_RSLT_MASK;
4623}
4624
4625/* Check if there are enough reserved descriptors for transmission.
4626 * If not, request chunk of reserved descriptors and check again.
4627 */
4628static int mvpp2_txq_reserved_desc_num_proc(struct mvpp2 *priv,
4629 struct mvpp2_tx_queue *txq,
4630 struct mvpp2_txq_pcpu *txq_pcpu,
4631 int num)
4632{
4633 int req, cpu, desc_count;
4634
4635 if (txq_pcpu->reserved_num >= num)
4636 return 0;
4637
4638 /* Not enough descriptors reserved! Update the reserved descriptor
4639 * count and check again.
4640 */
4641
4642 desc_count = 0;
4643 /* Compute total of used descriptors */
4644 for_each_present_cpu(cpu) {
4645 struct mvpp2_txq_pcpu *txq_pcpu_aux;
4646
4647 txq_pcpu_aux = per_cpu_ptr(txq->pcpu, cpu);
4648 desc_count += txq_pcpu_aux->count;
4649 desc_count += txq_pcpu_aux->reserved_num;
4650 }
4651
4652 req = max(MVPP2_CPU_DESC_CHUNK, num - txq_pcpu->reserved_num);
4653 desc_count += req;
4654
4655 if (desc_count >
4656 (txq->size - (num_present_cpus() * MVPP2_CPU_DESC_CHUNK)))
4657 return -ENOMEM;
4658
4659 txq_pcpu->reserved_num += mvpp2_txq_alloc_reserved_desc(priv, txq, req);
4660
4661 /* OK, the descriptor cound has been updated: check again. */
4662 if (txq_pcpu->reserved_num < num)
4663 return -ENOMEM;
4664 return 0;
4665}
4666
4667/* Release the last allocated Tx descriptor. Useful to handle DMA
4668 * mapping failures in the Tx path.
4669 */
4670static void mvpp2_txq_desc_put(struct mvpp2_tx_queue *txq)
4671{
4672 if (txq->next_desc_to_proc == 0)
4673 txq->next_desc_to_proc = txq->last_desc - 1;
4674 else
4675 txq->next_desc_to_proc--;
4676}
4677
4678/* Set Tx descriptors fields relevant for CSUM calculation */
4679static u32 mvpp2_txq_desc_csum(int l3_offs, int l3_proto,
4680 int ip_hdr_len, int l4_proto)
4681{
4682 u32 command;
4683
4684 /* fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
4685 * G_L4_chk, L4_type required only for checksum calculation
4686 */
4687 command = (l3_offs << MVPP2_TXD_L3_OFF_SHIFT);
4688 command |= (ip_hdr_len << MVPP2_TXD_IP_HLEN_SHIFT);
4689 command |= MVPP2_TXD_IP_CSUM_DISABLE;
4690
4691 if (l3_proto == swab16(ETH_P_IP)) {
4692 command &= ~MVPP2_TXD_IP_CSUM_DISABLE; /* enable IPv4 csum */
4693 command &= ~MVPP2_TXD_L3_IP6; /* enable IPv4 */
4694 } else {
4695 command |= MVPP2_TXD_L3_IP6; /* enable IPv6 */
4696 }
4697
4698 if (l4_proto == IPPROTO_TCP) {
4699 command &= ~MVPP2_TXD_L4_UDP; /* enable TCP */
4700 command &= ~MVPP2_TXD_L4_CSUM_FRAG; /* generate L4 csum */
4701 } else if (l4_proto == IPPROTO_UDP) {
4702 command |= MVPP2_TXD_L4_UDP; /* enable UDP */
4703 command &= ~MVPP2_TXD_L4_CSUM_FRAG; /* generate L4 csum */
4704 } else {
4705 command |= MVPP2_TXD_L4_CSUM_NOT;
4706 }
4707
4708 return command;
4709}
4710
4711/* Get number of sent descriptors and decrement counter.
4712 * The number of sent descriptors is returned.
4713 * Per-CPU access
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004714 *
4715 * Called only from mvpp2_txq_done(), called from mvpp2_tx()
4716 * (migration disabled) and from the TX completion tasklet (migration
4717 * disabled) so using smp_processor_id() is OK.
Marcin Wojtas3f518502014-07-10 16:52:13 -03004718 */
4719static inline int mvpp2_txq_sent_desc_proc(struct mvpp2_port *port,
4720 struct mvpp2_tx_queue *txq)
4721{
4722 u32 val;
4723
4724 /* Reading status reg resets transmitted descriptor counter */
Thomas Petazzonia7868412017-03-07 16:53:13 +01004725 val = mvpp2_percpu_read(port->priv, smp_processor_id(),
4726 MVPP2_TXQ_SENT_REG(txq->id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004727
4728 return (val & MVPP2_TRANSMITTED_COUNT_MASK) >>
4729 MVPP2_TRANSMITTED_COUNT_OFFSET;
4730}
4731
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004732/* Called through on_each_cpu(), so runs on all CPUs, with migration
4733 * disabled, therefore using smp_processor_id() is OK.
4734 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004735static void mvpp2_txq_sent_counter_clear(void *arg)
4736{
4737 struct mvpp2_port *port = arg;
4738 int queue;
4739
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004740 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004741 int id = port->txqs[queue]->id;
4742
Thomas Petazzonia7868412017-03-07 16:53:13 +01004743 mvpp2_percpu_read(port->priv, smp_processor_id(),
4744 MVPP2_TXQ_SENT_REG(id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004745 }
4746}
4747
4748/* Set max sizes for Tx queues */
4749static void mvpp2_txp_max_tx_size_set(struct mvpp2_port *port)
4750{
4751 u32 val, size, mtu;
4752 int txq, tx_port_num;
4753
4754 mtu = port->pkt_size * 8;
4755 if (mtu > MVPP2_TXP_MTU_MAX)
4756 mtu = MVPP2_TXP_MTU_MAX;
4757
4758 /* WA for wrong Token bucket update: Set MTU value = 3*real MTU value */
4759 mtu = 3 * mtu;
4760
4761 /* Indirect access to registers */
4762 tx_port_num = mvpp2_egress_port(port);
4763 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
4764
4765 /* Set MTU */
4766 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_MTU_REG);
4767 val &= ~MVPP2_TXP_MTU_MAX;
4768 val |= mtu;
4769 mvpp2_write(port->priv, MVPP2_TXP_SCHED_MTU_REG, val);
4770
4771 /* TXP token size and all TXQs token size must be larger that MTU */
4772 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG);
4773 size = val & MVPP2_TXP_TOKEN_SIZE_MAX;
4774 if (size < mtu) {
4775 size = mtu;
4776 val &= ~MVPP2_TXP_TOKEN_SIZE_MAX;
4777 val |= size;
4778 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
4779 }
4780
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004781 for (txq = 0; txq < port->ntxqs; txq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004782 val = mvpp2_read(port->priv,
4783 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq));
4784 size = val & MVPP2_TXQ_TOKEN_SIZE_MAX;
4785
4786 if (size < mtu) {
4787 size = mtu;
4788 val &= ~MVPP2_TXQ_TOKEN_SIZE_MAX;
4789 val |= size;
4790 mvpp2_write(port->priv,
4791 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq),
4792 val);
4793 }
4794 }
4795}
4796
4797/* Set the number of packets that will be received before Rx interrupt
4798 * will be generated by HW.
4799 */
4800static void mvpp2_rx_pkts_coal_set(struct mvpp2_port *port,
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01004801 struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004802{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004803 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01004804
Thomas Petazzonif8b0d5f2017-02-21 11:28:03 +01004805 if (rxq->pkts_coal > MVPP2_OCCUPIED_THRESH_MASK)
4806 rxq->pkts_coal = MVPP2_OCCUPIED_THRESH_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004807
Thomas Petazzonia7868412017-03-07 16:53:13 +01004808 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
4809 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_THRESH_REG,
4810 rxq->pkts_coal);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004811
4812 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03004813}
4814
Thomas Petazzoniab426762017-02-21 11:28:04 +01004815static u32 mvpp2_usec_to_cycles(u32 usec, unsigned long clk_hz)
4816{
4817 u64 tmp = (u64)clk_hz * usec;
4818
4819 do_div(tmp, USEC_PER_SEC);
4820
4821 return tmp > U32_MAX ? U32_MAX : tmp;
4822}
4823
4824static u32 mvpp2_cycles_to_usec(u32 cycles, unsigned long clk_hz)
4825{
4826 u64 tmp = (u64)cycles * USEC_PER_SEC;
4827
4828 do_div(tmp, clk_hz);
4829
4830 return tmp > U32_MAX ? U32_MAX : tmp;
4831}
4832
Marcin Wojtas3f518502014-07-10 16:52:13 -03004833/* Set the time delay in usec before Rx interrupt */
4834static void mvpp2_rx_time_coal_set(struct mvpp2_port *port,
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01004835 struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004836{
Thomas Petazzoniab426762017-02-21 11:28:04 +01004837 unsigned long freq = port->priv->tclk;
4838 u32 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004839
Thomas Petazzoniab426762017-02-21 11:28:04 +01004840 if (val > MVPP2_MAX_ISR_RX_THRESHOLD) {
4841 rxq->time_coal =
4842 mvpp2_cycles_to_usec(MVPP2_MAX_ISR_RX_THRESHOLD, freq);
4843
4844 /* re-evaluate to get actual register value */
4845 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
4846 }
4847
Marcin Wojtas3f518502014-07-10 16:52:13 -03004848 mvpp2_write(port->priv, MVPP2_ISR_RX_THRESHOLD_REG(rxq->id), val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004849}
4850
Marcin Wojtas3f518502014-07-10 16:52:13 -03004851/* Free Tx queue skbuffs */
4852static void mvpp2_txq_bufs_free(struct mvpp2_port *port,
4853 struct mvpp2_tx_queue *txq,
4854 struct mvpp2_txq_pcpu *txq_pcpu, int num)
4855{
4856 int i;
4857
4858 for (i = 0; i < num; i++) {
Thomas Petazzoni83544912016-12-21 11:28:49 +01004859 struct mvpp2_txq_pcpu_buf *tx_buf =
4860 txq_pcpu->buffs + txq_pcpu->txq_get_index;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004861
Thomas Petazzoni20396132017-03-07 16:53:00 +01004862 dma_unmap_single(port->dev->dev.parent, tx_buf->dma,
Thomas Petazzoni83544912016-12-21 11:28:49 +01004863 tx_buf->size, DMA_TO_DEVICE);
Thomas Petazzoni36fb7432017-02-21 11:28:05 +01004864 if (tx_buf->skb)
4865 dev_kfree_skb_any(tx_buf->skb);
4866
4867 mvpp2_txq_inc_get(txq_pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004868 }
4869}
4870
4871static inline struct mvpp2_rx_queue *mvpp2_get_rx_queue(struct mvpp2_port *port,
4872 u32 cause)
4873{
4874 int queue = fls(cause) - 1;
4875
4876 return port->rxqs[queue];
4877}
4878
4879static inline struct mvpp2_tx_queue *mvpp2_get_tx_queue(struct mvpp2_port *port,
4880 u32 cause)
4881{
Marcin Wojtasedc660f2015-08-06 19:00:30 +02004882 int queue = fls(cause) - 1;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004883
4884 return port->txqs[queue];
4885}
4886
4887/* Handle end of transmission */
4888static void mvpp2_txq_done(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
4889 struct mvpp2_txq_pcpu *txq_pcpu)
4890{
4891 struct netdev_queue *nq = netdev_get_tx_queue(port->dev, txq->log_id);
4892 int tx_done;
4893
4894 if (txq_pcpu->cpu != smp_processor_id())
4895 netdev_err(port->dev, "wrong cpu on the end of Tx processing\n");
4896
4897 tx_done = mvpp2_txq_sent_desc_proc(port, txq);
4898 if (!tx_done)
4899 return;
4900 mvpp2_txq_bufs_free(port, txq, txq_pcpu, tx_done);
4901
4902 txq_pcpu->count -= tx_done;
4903
4904 if (netif_tx_queue_stopped(nq))
4905 if (txq_pcpu->size - txq_pcpu->count >= MAX_SKB_FRAGS + 1)
4906 netif_tx_wake_queue(nq);
4907}
4908
Marcin Wojtasedc660f2015-08-06 19:00:30 +02004909static unsigned int mvpp2_tx_done(struct mvpp2_port *port, u32 cause)
4910{
4911 struct mvpp2_tx_queue *txq;
4912 struct mvpp2_txq_pcpu *txq_pcpu;
4913 unsigned int tx_todo = 0;
4914
4915 while (cause) {
4916 txq = mvpp2_get_tx_queue(port, cause);
4917 if (!txq)
4918 break;
4919
4920 txq_pcpu = this_cpu_ptr(txq->pcpu);
4921
4922 if (txq_pcpu->count) {
4923 mvpp2_txq_done(port, txq, txq_pcpu);
4924 tx_todo += txq_pcpu->count;
4925 }
4926
4927 cause &= ~(1 << txq->log_id);
4928 }
4929 return tx_todo;
4930}
4931
Marcin Wojtas3f518502014-07-10 16:52:13 -03004932/* Rx/Tx queue initialization/cleanup methods */
4933
4934/* Allocate and initialize descriptors for aggr TXQ */
4935static int mvpp2_aggr_txq_init(struct platform_device *pdev,
4936 struct mvpp2_tx_queue *aggr_txq,
4937 int desc_num, int cpu,
4938 struct mvpp2 *priv)
4939{
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01004940 u32 txq_dma;
4941
Marcin Wojtas3f518502014-07-10 16:52:13 -03004942 /* Allocate memory for TX descriptors */
4943 aggr_txq->descs = dma_alloc_coherent(&pdev->dev,
4944 desc_num * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004945 &aggr_txq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004946 if (!aggr_txq->descs)
4947 return -ENOMEM;
4948
Marcin Wojtas3f518502014-07-10 16:52:13 -03004949 aggr_txq->last_desc = aggr_txq->size - 1;
4950
4951 /* Aggr TXQ no reset WA */
4952 aggr_txq->next_desc_to_proc = mvpp2_read(priv,
4953 MVPP2_AGGR_TXQ_INDEX_REG(cpu));
4954
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01004955 /* Set Tx descriptors queue starting address indirect
4956 * access
4957 */
4958 if (priv->hw_version == MVPP21)
4959 txq_dma = aggr_txq->descs_dma;
4960 else
4961 txq_dma = aggr_txq->descs_dma >>
4962 MVPP22_AGGR_TXQ_DESC_ADDR_OFFS;
4963
4964 mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu), txq_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004965 mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu), desc_num);
4966
4967 return 0;
4968}
4969
4970/* Create a specified Rx queue */
4971static int mvpp2_rxq_init(struct mvpp2_port *port,
4972 struct mvpp2_rx_queue *rxq)
4973
4974{
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01004975 u32 rxq_dma;
Thomas Petazzonia7868412017-03-07 16:53:13 +01004976 int cpu;
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01004977
Marcin Wojtas3f518502014-07-10 16:52:13 -03004978 rxq->size = port->rx_ring_size;
4979
4980 /* Allocate memory for RX descriptors */
4981 rxq->descs = dma_alloc_coherent(port->dev->dev.parent,
4982 rxq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004983 &rxq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004984 if (!rxq->descs)
4985 return -ENOMEM;
4986
Marcin Wojtas3f518502014-07-10 16:52:13 -03004987 rxq->last_desc = rxq->size - 1;
4988
4989 /* Zero occupied and non-occupied counters - direct access */
4990 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
4991
4992 /* Set Rx descriptors queue starting address - indirect access */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004993 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01004994 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01004995 if (port->priv->hw_version == MVPP21)
4996 rxq_dma = rxq->descs_dma;
4997 else
4998 rxq_dma = rxq->descs_dma >> MVPP22_DESC_ADDR_OFFS;
Thomas Petazzonia7868412017-03-07 16:53:13 +01004999 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_ADDR_REG, rxq_dma);
5000 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_SIZE_REG, rxq->size);
5001 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_INDEX_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005002 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005003
5004 /* Set Offset */
5005 mvpp2_rxq_offset_set(port, rxq->id, NET_SKB_PAD);
5006
5007 /* Set coalescing pkts and time */
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01005008 mvpp2_rx_pkts_coal_set(port, rxq);
5009 mvpp2_rx_time_coal_set(port, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005010
5011 /* Add number of descriptors ready for receiving packets */
5012 mvpp2_rxq_status_update(port, rxq->id, 0, rxq->size);
5013
5014 return 0;
5015}
5016
5017/* Push packets received by the RXQ to BM pool */
5018static void mvpp2_rxq_drop_pkts(struct mvpp2_port *port,
5019 struct mvpp2_rx_queue *rxq)
5020{
5021 int rx_received, i;
5022
5023 rx_received = mvpp2_rxq_received(port, rxq->id);
5024 if (!rx_received)
5025 return;
5026
5027 for (i = 0; i < rx_received; i++) {
5028 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005029 u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
5030 int pool;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005031
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005032 pool = (status & MVPP2_RXD_BM_POOL_ID_MASK) >>
5033 MVPP2_RXD_BM_POOL_ID_OFFS;
5034
Thomas Petazzoni7d7627b2017-06-22 14:23:20 +02005035 mvpp2_bm_pool_put(port, pool,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005036 mvpp2_rxdesc_dma_addr_get(port, rx_desc),
5037 mvpp2_rxdesc_cookie_get(port, rx_desc));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005038 }
5039 mvpp2_rxq_status_update(port, rxq->id, rx_received, rx_received);
5040}
5041
5042/* Cleanup Rx queue */
5043static void mvpp2_rxq_deinit(struct mvpp2_port *port,
5044 struct mvpp2_rx_queue *rxq)
5045{
Thomas Petazzonia7868412017-03-07 16:53:13 +01005046 int cpu;
5047
Marcin Wojtas3f518502014-07-10 16:52:13 -03005048 mvpp2_rxq_drop_pkts(port, rxq);
5049
5050 if (rxq->descs)
5051 dma_free_coherent(port->dev->dev.parent,
5052 rxq->size * MVPP2_DESC_ALIGNED_SIZE,
5053 rxq->descs,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005054 rxq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005055
5056 rxq->descs = NULL;
5057 rxq->last_desc = 0;
5058 rxq->next_desc_to_proc = 0;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005059 rxq->descs_dma = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005060
5061 /* Clear Rx descriptors queue starting address and size;
5062 * free descriptor number
5063 */
5064 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005065 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005066 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
5067 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_ADDR_REG, 0);
5068 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_SIZE_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005069 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005070}
5071
5072/* Create and initialize a Tx queue */
5073static int mvpp2_txq_init(struct mvpp2_port *port,
5074 struct mvpp2_tx_queue *txq)
5075{
5076 u32 val;
5077 int cpu, desc, desc_per_txq, tx_port_num;
5078 struct mvpp2_txq_pcpu *txq_pcpu;
5079
5080 txq->size = port->tx_ring_size;
5081
5082 /* Allocate memory for Tx descriptors */
5083 txq->descs = dma_alloc_coherent(port->dev->dev.parent,
5084 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005085 &txq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005086 if (!txq->descs)
5087 return -ENOMEM;
5088
Marcin Wojtas3f518502014-07-10 16:52:13 -03005089 txq->last_desc = txq->size - 1;
5090
5091 /* Set Tx descriptors queue starting address - indirect access */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005092 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005093 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5094 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_ADDR_REG,
5095 txq->descs_dma);
5096 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_SIZE_REG,
5097 txq->size & MVPP2_TXQ_DESC_SIZE_MASK);
5098 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_INDEX_REG, 0);
5099 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_RSVD_CLR_REG,
5100 txq->id << MVPP2_TXQ_RSVD_CLR_OFFSET);
5101 val = mvpp2_percpu_read(port->priv, cpu, MVPP2_TXQ_PENDING_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005102 val &= ~MVPP2_TXQ_PENDING_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005103 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PENDING_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005104
5105 /* Calculate base address in prefetch buffer. We reserve 16 descriptors
5106 * for each existing TXQ.
5107 * TCONTS for PON port must be continuous from 0 to MVPP2_MAX_TCONT
5108 * GBE ports assumed to be continious from 0 to MVPP2_MAX_PORTS
5109 */
5110 desc_per_txq = 16;
5111 desc = (port->id * MVPP2_MAX_TXQ * desc_per_txq) +
5112 (txq->log_id * desc_per_txq);
5113
Thomas Petazzonia7868412017-03-07 16:53:13 +01005114 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG,
5115 MVPP2_PREF_BUF_PTR(desc) | MVPP2_PREF_BUF_SIZE_16 |
5116 MVPP2_PREF_BUF_THRESH(desc_per_txq / 2));
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005117 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005118
5119 /* WRR / EJP configuration - indirect access */
5120 tx_port_num = mvpp2_egress_port(port);
5121 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
5122
5123 val = mvpp2_read(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id));
5124 val &= ~MVPP2_TXQ_REFILL_PERIOD_ALL_MASK;
5125 val |= MVPP2_TXQ_REFILL_PERIOD_MASK(1);
5126 val |= MVPP2_TXQ_REFILL_TOKENS_ALL_MASK;
5127 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id), val);
5128
5129 val = MVPP2_TXQ_TOKEN_SIZE_MAX;
5130 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq->log_id),
5131 val);
5132
5133 for_each_present_cpu(cpu) {
5134 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
5135 txq_pcpu->size = txq->size;
Markus Elfring02c91ec2017-04-17 08:09:07 +02005136 txq_pcpu->buffs = kmalloc_array(txq_pcpu->size,
5137 sizeof(*txq_pcpu->buffs),
5138 GFP_KERNEL);
Thomas Petazzoni83544912016-12-21 11:28:49 +01005139 if (!txq_pcpu->buffs)
Markus Elfring20b1e162017-04-17 12:58:33 +02005140 goto cleanup;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005141
5142 txq_pcpu->count = 0;
5143 txq_pcpu->reserved_num = 0;
5144 txq_pcpu->txq_put_index = 0;
5145 txq_pcpu->txq_get_index = 0;
5146 }
5147
5148 return 0;
Markus Elfring20b1e162017-04-17 12:58:33 +02005149cleanup:
Marcin Wojtas71ce3912015-08-06 19:00:29 +02005150 for_each_present_cpu(cpu) {
5151 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Thomas Petazzoni83544912016-12-21 11:28:49 +01005152 kfree(txq_pcpu->buffs);
Marcin Wojtas71ce3912015-08-06 19:00:29 +02005153 }
5154
5155 dma_free_coherent(port->dev->dev.parent,
5156 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005157 txq->descs, txq->descs_dma);
Marcin Wojtas71ce3912015-08-06 19:00:29 +02005158
5159 return -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005160}
5161
5162/* Free allocated TXQ resources */
5163static void mvpp2_txq_deinit(struct mvpp2_port *port,
5164 struct mvpp2_tx_queue *txq)
5165{
5166 struct mvpp2_txq_pcpu *txq_pcpu;
5167 int cpu;
5168
5169 for_each_present_cpu(cpu) {
5170 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Thomas Petazzoni83544912016-12-21 11:28:49 +01005171 kfree(txq_pcpu->buffs);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005172 }
5173
5174 if (txq->descs)
5175 dma_free_coherent(port->dev->dev.parent,
5176 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005177 txq->descs, txq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005178
5179 txq->descs = NULL;
5180 txq->last_desc = 0;
5181 txq->next_desc_to_proc = 0;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005182 txq->descs_dma = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005183
5184 /* Set minimum bandwidth for disabled TXQs */
5185 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(txq->id), 0);
5186
5187 /* Set Tx descriptors queue starting address and size */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005188 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005189 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5190 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_ADDR_REG, 0);
5191 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_SIZE_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005192 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005193}
5194
5195/* Cleanup Tx ports */
5196static void mvpp2_txq_clean(struct mvpp2_port *port, struct mvpp2_tx_queue *txq)
5197{
5198 struct mvpp2_txq_pcpu *txq_pcpu;
5199 int delay, pending, cpu;
5200 u32 val;
5201
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005202 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005203 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5204 val = mvpp2_percpu_read(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005205 val |= MVPP2_TXQ_DRAIN_EN_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005206 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005207
5208 /* The napi queue has been stopped so wait for all packets
5209 * to be transmitted.
5210 */
5211 delay = 0;
5212 do {
5213 if (delay >= MVPP2_TX_PENDING_TIMEOUT_MSEC) {
5214 netdev_warn(port->dev,
5215 "port %d: cleaning queue %d timed out\n",
5216 port->id, txq->log_id);
5217 break;
5218 }
5219 mdelay(1);
5220 delay++;
5221
Thomas Petazzonia7868412017-03-07 16:53:13 +01005222 pending = mvpp2_percpu_read(port->priv, cpu,
5223 MVPP2_TXQ_PENDING_REG);
5224 pending &= MVPP2_TXQ_PENDING_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005225 } while (pending);
5226
5227 val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005228 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG, val);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005229 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005230
5231 for_each_present_cpu(cpu) {
5232 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
5233
5234 /* Release all packets */
5235 mvpp2_txq_bufs_free(port, txq, txq_pcpu, txq_pcpu->count);
5236
5237 /* Reset queue */
5238 txq_pcpu->count = 0;
5239 txq_pcpu->txq_put_index = 0;
5240 txq_pcpu->txq_get_index = 0;
5241 }
5242}
5243
5244/* Cleanup all Tx queues */
5245static void mvpp2_cleanup_txqs(struct mvpp2_port *port)
5246{
5247 struct mvpp2_tx_queue *txq;
5248 int queue;
5249 u32 val;
5250
5251 val = mvpp2_read(port->priv, MVPP2_TX_PORT_FLUSH_REG);
5252
5253 /* Reset Tx ports and delete Tx queues */
5254 val |= MVPP2_TX_PORT_FLUSH_MASK(port->id);
5255 mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
5256
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005257 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005258 txq = port->txqs[queue];
5259 mvpp2_txq_clean(port, txq);
5260 mvpp2_txq_deinit(port, txq);
5261 }
5262
5263 on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
5264
5265 val &= ~MVPP2_TX_PORT_FLUSH_MASK(port->id);
5266 mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
5267}
5268
5269/* Cleanup all Rx queues */
5270static void mvpp2_cleanup_rxqs(struct mvpp2_port *port)
5271{
5272 int queue;
5273
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005274 for (queue = 0; queue < port->nrxqs; queue++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005275 mvpp2_rxq_deinit(port, port->rxqs[queue]);
5276}
5277
5278/* Init all Rx queues for port */
5279static int mvpp2_setup_rxqs(struct mvpp2_port *port)
5280{
5281 int queue, err;
5282
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005283 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005284 err = mvpp2_rxq_init(port, port->rxqs[queue]);
5285 if (err)
5286 goto err_cleanup;
5287 }
5288 return 0;
5289
5290err_cleanup:
5291 mvpp2_cleanup_rxqs(port);
5292 return err;
5293}
5294
5295/* Init all tx queues for port */
5296static int mvpp2_setup_txqs(struct mvpp2_port *port)
5297{
5298 struct mvpp2_tx_queue *txq;
5299 int queue, err;
5300
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005301 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005302 txq = port->txqs[queue];
5303 err = mvpp2_txq_init(port, txq);
5304 if (err)
5305 goto err_cleanup;
5306 }
5307
Marcin Wojtas3f518502014-07-10 16:52:13 -03005308 on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
5309 return 0;
5310
5311err_cleanup:
5312 mvpp2_cleanup_txqs(port);
5313 return err;
5314}
5315
5316/* The callback for per-port interrupt */
5317static irqreturn_t mvpp2_isr(int irq, void *dev_id)
5318{
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02005319 struct mvpp2_queue_vector *qv = dev_id;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005320
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02005321 mvpp2_qvec_interrupt_disable(qv);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005322
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02005323 napi_schedule(&qv->napi);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005324
5325 return IRQ_HANDLED;
5326}
5327
5328/* Adjust link */
5329static void mvpp2_link_event(struct net_device *dev)
5330{
5331 struct mvpp2_port *port = netdev_priv(dev);
Philippe Reynes8e072692016-06-28 00:08:11 +02005332 struct phy_device *phydev = dev->phydev;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005333 int status_change = 0;
5334 u32 val;
5335
5336 if (phydev->link) {
5337 if ((port->speed != phydev->speed) ||
5338 (port->duplex != phydev->duplex)) {
5339 u32 val;
5340
5341 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5342 val &= ~(MVPP2_GMAC_CONFIG_MII_SPEED |
5343 MVPP2_GMAC_CONFIG_GMII_SPEED |
5344 MVPP2_GMAC_CONFIG_FULL_DUPLEX |
5345 MVPP2_GMAC_AN_SPEED_EN |
5346 MVPP2_GMAC_AN_DUPLEX_EN);
5347
5348 if (phydev->duplex)
5349 val |= MVPP2_GMAC_CONFIG_FULL_DUPLEX;
5350
5351 if (phydev->speed == SPEED_1000)
5352 val |= MVPP2_GMAC_CONFIG_GMII_SPEED;
Thomas Petazzoni2add5112014-07-27 23:21:35 +02005353 else if (phydev->speed == SPEED_100)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005354 val |= MVPP2_GMAC_CONFIG_MII_SPEED;
5355
5356 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5357
5358 port->duplex = phydev->duplex;
5359 port->speed = phydev->speed;
5360 }
5361 }
5362
5363 if (phydev->link != port->link) {
5364 if (!phydev->link) {
5365 port->duplex = -1;
5366 port->speed = 0;
5367 }
5368
5369 port->link = phydev->link;
5370 status_change = 1;
5371 }
5372
5373 if (status_change) {
5374 if (phydev->link) {
5375 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5376 val |= (MVPP2_GMAC_FORCE_LINK_PASS |
5377 MVPP2_GMAC_FORCE_LINK_DOWN);
5378 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5379 mvpp2_egress_enable(port);
5380 mvpp2_ingress_enable(port);
5381 } else {
5382 mvpp2_ingress_disable(port);
5383 mvpp2_egress_disable(port);
5384 }
5385 phy_print_status(phydev);
5386 }
5387}
5388
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005389static void mvpp2_timer_set(struct mvpp2_port_pcpu *port_pcpu)
5390{
5391 ktime_t interval;
5392
5393 if (!port_pcpu->timer_scheduled) {
5394 port_pcpu->timer_scheduled = true;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01005395 interval = MVPP2_TXDONE_HRTIMER_PERIOD_NS;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005396 hrtimer_start(&port_pcpu->tx_done_timer, interval,
5397 HRTIMER_MODE_REL_PINNED);
5398 }
5399}
5400
5401static void mvpp2_tx_proc_cb(unsigned long data)
5402{
5403 struct net_device *dev = (struct net_device *)data;
5404 struct mvpp2_port *port = netdev_priv(dev);
5405 struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
5406 unsigned int tx_todo, cause;
5407
5408 if (!netif_running(dev))
5409 return;
5410 port_pcpu->timer_scheduled = false;
5411
5412 /* Process all the Tx queues */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005413 cause = (1 << port->ntxqs) - 1;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005414 tx_todo = mvpp2_tx_done(port, cause);
5415
5416 /* Set the timer in case not all the packets were processed */
5417 if (tx_todo)
5418 mvpp2_timer_set(port_pcpu);
5419}
5420
5421static enum hrtimer_restart mvpp2_hr_timer_cb(struct hrtimer *timer)
5422{
5423 struct mvpp2_port_pcpu *port_pcpu = container_of(timer,
5424 struct mvpp2_port_pcpu,
5425 tx_done_timer);
5426
5427 tasklet_schedule(&port_pcpu->tx_done_tasklet);
5428
5429 return HRTIMER_NORESTART;
5430}
5431
Marcin Wojtas3f518502014-07-10 16:52:13 -03005432/* Main RX/TX processing routines */
5433
5434/* Display more error info */
5435static void mvpp2_rx_error(struct mvpp2_port *port,
5436 struct mvpp2_rx_desc *rx_desc)
5437{
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005438 u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
5439 size_t sz = mvpp2_rxdesc_size_get(port, rx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005440
5441 switch (status & MVPP2_RXD_ERR_CODE_MASK) {
5442 case MVPP2_RXD_ERR_CRC:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005443 netdev_err(port->dev, "bad rx status %08x (crc error), size=%zu\n",
5444 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005445 break;
5446 case MVPP2_RXD_ERR_OVERRUN:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005447 netdev_err(port->dev, "bad rx status %08x (overrun error), size=%zu\n",
5448 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005449 break;
5450 case MVPP2_RXD_ERR_RESOURCE:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005451 netdev_err(port->dev, "bad rx status %08x (resource error), size=%zu\n",
5452 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005453 break;
5454 }
5455}
5456
5457/* Handle RX checksum offload */
5458static void mvpp2_rx_csum(struct mvpp2_port *port, u32 status,
5459 struct sk_buff *skb)
5460{
5461 if (((status & MVPP2_RXD_L3_IP4) &&
5462 !(status & MVPP2_RXD_IP4_HEADER_ERR)) ||
5463 (status & MVPP2_RXD_L3_IP6))
5464 if (((status & MVPP2_RXD_L4_UDP) ||
5465 (status & MVPP2_RXD_L4_TCP)) &&
5466 (status & MVPP2_RXD_L4_CSUM_OK)) {
5467 skb->csum = 0;
5468 skb->ip_summed = CHECKSUM_UNNECESSARY;
5469 return;
5470 }
5471
5472 skb->ip_summed = CHECKSUM_NONE;
5473}
5474
5475/* Reuse skb if possible, or allocate a new skb and add it to BM pool */
5476static int mvpp2_rx_refill(struct mvpp2_port *port,
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005477 struct mvpp2_bm_pool *bm_pool, int pool)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005478{
Thomas Petazzoni20396132017-03-07 16:53:00 +01005479 dma_addr_t dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01005480 phys_addr_t phys_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005481 void *buf;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005482
Marcin Wojtas3f518502014-07-10 16:52:13 -03005483 /* No recycle or too many buffers are in use, so allocate a new skb */
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01005484 buf = mvpp2_buf_alloc(port, bm_pool, &dma_addr, &phys_addr,
5485 GFP_ATOMIC);
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005486 if (!buf)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005487 return -ENOMEM;
5488
Thomas Petazzoni7d7627b2017-06-22 14:23:20 +02005489 mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
Thomas Petazzoni7ef7e1d2017-02-21 11:28:07 +01005490
Marcin Wojtas3f518502014-07-10 16:52:13 -03005491 return 0;
5492}
5493
5494/* Handle tx checksum */
5495static u32 mvpp2_skb_tx_csum(struct mvpp2_port *port, struct sk_buff *skb)
5496{
5497 if (skb->ip_summed == CHECKSUM_PARTIAL) {
5498 int ip_hdr_len = 0;
5499 u8 l4_proto;
5500
5501 if (skb->protocol == htons(ETH_P_IP)) {
5502 struct iphdr *ip4h = ip_hdr(skb);
5503
5504 /* Calculate IPv4 checksum and L4 checksum */
5505 ip_hdr_len = ip4h->ihl;
5506 l4_proto = ip4h->protocol;
5507 } else if (skb->protocol == htons(ETH_P_IPV6)) {
5508 struct ipv6hdr *ip6h = ipv6_hdr(skb);
5509
5510 /* Read l4_protocol from one of IPv6 extra headers */
5511 if (skb_network_header_len(skb) > 0)
5512 ip_hdr_len = (skb_network_header_len(skb) >> 2);
5513 l4_proto = ip6h->nexthdr;
5514 } else {
5515 return MVPP2_TXD_L4_CSUM_NOT;
5516 }
5517
5518 return mvpp2_txq_desc_csum(skb_network_offset(skb),
5519 skb->protocol, ip_hdr_len, l4_proto);
5520 }
5521
5522 return MVPP2_TXD_L4_CSUM_NOT | MVPP2_TXD_IP_CSUM_DISABLE;
5523}
5524
Marcin Wojtas3f518502014-07-10 16:52:13 -03005525/* Main rx processing */
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02005526static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi,
5527 int rx_todo, struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005528{
5529 struct net_device *dev = port->dev;
Marcin Wojtasb5015852015-12-03 15:20:51 +01005530 int rx_received;
5531 int rx_done = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005532 u32 rcvd_pkts = 0;
5533 u32 rcvd_bytes = 0;
5534
5535 /* Get number of received packets and clamp the to-do */
5536 rx_received = mvpp2_rxq_received(port, rxq->id);
5537 if (rx_todo > rx_received)
5538 rx_todo = rx_received;
5539
Marcin Wojtasb5015852015-12-03 15:20:51 +01005540 while (rx_done < rx_todo) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005541 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
5542 struct mvpp2_bm_pool *bm_pool;
5543 struct sk_buff *skb;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005544 unsigned int frag_size;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005545 dma_addr_t dma_addr;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005546 phys_addr_t phys_addr;
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005547 u32 rx_status;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005548 int pool, rx_bytes, err;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005549 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005550
Marcin Wojtasb5015852015-12-03 15:20:51 +01005551 rx_done++;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005552 rx_status = mvpp2_rxdesc_status_get(port, rx_desc);
5553 rx_bytes = mvpp2_rxdesc_size_get(port, rx_desc);
5554 rx_bytes -= MVPP2_MH_SIZE;
5555 dma_addr = mvpp2_rxdesc_dma_addr_get(port, rx_desc);
5556 phys_addr = mvpp2_rxdesc_cookie_get(port, rx_desc);
5557 data = (void *)phys_to_virt(phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005558
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005559 pool = (rx_status & MVPP2_RXD_BM_POOL_ID_MASK) >>
5560 MVPP2_RXD_BM_POOL_ID_OFFS;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005561 bm_pool = &port->priv->bm_pools[pool];
Marcin Wojtas3f518502014-07-10 16:52:13 -03005562
5563 /* In case of an error, release the requested buffer pointer
5564 * to the Buffer Manager. This request process is controlled
5565 * by the hardware, and the information about the buffer is
5566 * comprised by the RX descriptor.
5567 */
5568 if (rx_status & MVPP2_RXD_ERR_SUMMARY) {
Markus Elfring8a524882017-04-17 10:52:02 +02005569err_drop_frame:
Marcin Wojtas3f518502014-07-10 16:52:13 -03005570 dev->stats.rx_errors++;
5571 mvpp2_rx_error(port, rx_desc);
Marcin Wojtasb5015852015-12-03 15:20:51 +01005572 /* Return the buffer to the pool */
Thomas Petazzoni7d7627b2017-06-22 14:23:20 +02005573 mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005574 continue;
5575 }
5576
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005577 if (bm_pool->frag_size > PAGE_SIZE)
5578 frag_size = 0;
5579 else
5580 frag_size = bm_pool->frag_size;
5581
5582 skb = build_skb(data, frag_size);
5583 if (!skb) {
5584 netdev_warn(port->dev, "skb build failed\n");
5585 goto err_drop_frame;
5586 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03005587
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005588 err = mvpp2_rx_refill(port, bm_pool, pool);
Marcin Wojtasb5015852015-12-03 15:20:51 +01005589 if (err) {
5590 netdev_err(port->dev, "failed to refill BM pools\n");
5591 goto err_drop_frame;
5592 }
5593
Thomas Petazzoni20396132017-03-07 16:53:00 +01005594 dma_unmap_single(dev->dev.parent, dma_addr,
Marcin Wojtas4229d502015-12-03 15:20:50 +01005595 bm_pool->buf_size, DMA_FROM_DEVICE);
5596
Marcin Wojtas3f518502014-07-10 16:52:13 -03005597 rcvd_pkts++;
5598 rcvd_bytes += rx_bytes;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005599
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005600 skb_reserve(skb, MVPP2_MH_SIZE + NET_SKB_PAD);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005601 skb_put(skb, rx_bytes);
5602 skb->protocol = eth_type_trans(skb, dev);
5603 mvpp2_rx_csum(port, rx_status, skb);
5604
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02005605 napi_gro_receive(napi, skb);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005606 }
5607
5608 if (rcvd_pkts) {
5609 struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
5610
5611 u64_stats_update_begin(&stats->syncp);
5612 stats->rx_packets += rcvd_pkts;
5613 stats->rx_bytes += rcvd_bytes;
5614 u64_stats_update_end(&stats->syncp);
5615 }
5616
5617 /* Update Rx queue management counters */
5618 wmb();
Marcin Wojtasb5015852015-12-03 15:20:51 +01005619 mvpp2_rxq_status_update(port, rxq->id, rx_done, rx_done);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005620
5621 return rx_todo;
5622}
5623
5624static inline void
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005625tx_desc_unmap_put(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
Marcin Wojtas3f518502014-07-10 16:52:13 -03005626 struct mvpp2_tx_desc *desc)
5627{
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005628 dma_addr_t buf_dma_addr =
5629 mvpp2_txdesc_dma_addr_get(port, desc);
5630 size_t buf_sz =
5631 mvpp2_txdesc_size_get(port, desc);
5632 dma_unmap_single(port->dev->dev.parent, buf_dma_addr,
5633 buf_sz, DMA_TO_DEVICE);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005634 mvpp2_txq_desc_put(txq);
5635}
5636
5637/* Handle tx fragmentation processing */
5638static int mvpp2_tx_frag_process(struct mvpp2_port *port, struct sk_buff *skb,
5639 struct mvpp2_tx_queue *aggr_txq,
5640 struct mvpp2_tx_queue *txq)
5641{
5642 struct mvpp2_txq_pcpu *txq_pcpu = this_cpu_ptr(txq->pcpu);
5643 struct mvpp2_tx_desc *tx_desc;
5644 int i;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005645 dma_addr_t buf_dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005646
5647 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
5648 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5649 void *addr = page_address(frag->page.p) + frag->page_offset;
5650
5651 tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005652 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
5653 mvpp2_txdesc_size_set(port, tx_desc, frag->size);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005654
Thomas Petazzoni20396132017-03-07 16:53:00 +01005655 buf_dma_addr = dma_map_single(port->dev->dev.parent, addr,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005656 frag->size,
5657 DMA_TO_DEVICE);
Thomas Petazzoni20396132017-03-07 16:53:00 +01005658 if (dma_mapping_error(port->dev->dev.parent, buf_dma_addr)) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005659 mvpp2_txq_desc_put(txq);
Markus Elfring32bae632017-04-17 11:36:34 +02005660 goto cleanup;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005661 }
5662
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005663 mvpp2_txdesc_offset_set(port, tx_desc,
5664 buf_dma_addr & MVPP2_TX_DESC_ALIGN);
5665 mvpp2_txdesc_dma_addr_set(port, tx_desc,
5666 buf_dma_addr & ~MVPP2_TX_DESC_ALIGN);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005667
5668 if (i == (skb_shinfo(skb)->nr_frags - 1)) {
5669 /* Last descriptor */
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005670 mvpp2_txdesc_cmd_set(port, tx_desc,
5671 MVPP2_TXD_L_DESC);
5672 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005673 } else {
5674 /* Descriptor in the middle: Not First, Not Last */
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005675 mvpp2_txdesc_cmd_set(port, tx_desc, 0);
5676 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005677 }
5678 }
5679
5680 return 0;
Markus Elfring32bae632017-04-17 11:36:34 +02005681cleanup:
Marcin Wojtas3f518502014-07-10 16:52:13 -03005682 /* Release all descriptors that were used to map fragments of
5683 * this packet, as well as the corresponding DMA mappings
5684 */
5685 for (i = i - 1; i >= 0; i--) {
5686 tx_desc = txq->descs + i;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005687 tx_desc_unmap_put(port, txq, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005688 }
5689
5690 return -ENOMEM;
5691}
5692
5693/* Main tx processing */
5694static int mvpp2_tx(struct sk_buff *skb, struct net_device *dev)
5695{
5696 struct mvpp2_port *port = netdev_priv(dev);
5697 struct mvpp2_tx_queue *txq, *aggr_txq;
5698 struct mvpp2_txq_pcpu *txq_pcpu;
5699 struct mvpp2_tx_desc *tx_desc;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005700 dma_addr_t buf_dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005701 int frags = 0;
5702 u16 txq_id;
5703 u32 tx_cmd;
5704
5705 txq_id = skb_get_queue_mapping(skb);
5706 txq = port->txqs[txq_id];
5707 txq_pcpu = this_cpu_ptr(txq->pcpu);
5708 aggr_txq = &port->priv->aggr_txqs[smp_processor_id()];
5709
5710 frags = skb_shinfo(skb)->nr_frags + 1;
5711
5712 /* Check number of available descriptors */
5713 if (mvpp2_aggr_desc_num_check(port->priv, aggr_txq, frags) ||
5714 mvpp2_txq_reserved_desc_num_proc(port->priv, txq,
5715 txq_pcpu, frags)) {
5716 frags = 0;
5717 goto out;
5718 }
5719
5720 /* Get a descriptor for the first part of the packet */
5721 tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005722 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
5723 mvpp2_txdesc_size_set(port, tx_desc, skb_headlen(skb));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005724
Thomas Petazzoni20396132017-03-07 16:53:00 +01005725 buf_dma_addr = dma_map_single(dev->dev.parent, skb->data,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005726 skb_headlen(skb), DMA_TO_DEVICE);
Thomas Petazzoni20396132017-03-07 16:53:00 +01005727 if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005728 mvpp2_txq_desc_put(txq);
5729 frags = 0;
5730 goto out;
5731 }
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005732
5733 mvpp2_txdesc_offset_set(port, tx_desc,
5734 buf_dma_addr & MVPP2_TX_DESC_ALIGN);
5735 mvpp2_txdesc_dma_addr_set(port, tx_desc,
5736 buf_dma_addr & ~MVPP2_TX_DESC_ALIGN);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005737
5738 tx_cmd = mvpp2_skb_tx_csum(port, skb);
5739
5740 if (frags == 1) {
5741 /* First and Last descriptor */
5742 tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_L_DESC;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005743 mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
5744 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005745 } else {
5746 /* First but not Last */
5747 tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_PADDING_DISABLE;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005748 mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
5749 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005750
5751 /* Continue with other skb fragments */
5752 if (mvpp2_tx_frag_process(port, skb, aggr_txq, txq)) {
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005753 tx_desc_unmap_put(port, txq, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005754 frags = 0;
5755 goto out;
5756 }
5757 }
5758
5759 txq_pcpu->reserved_num -= frags;
5760 txq_pcpu->count += frags;
5761 aggr_txq->count += frags;
5762
5763 /* Enable transmit */
5764 wmb();
5765 mvpp2_aggr_txq_pend_desc_add(port, frags);
5766
5767 if (txq_pcpu->size - txq_pcpu->count < MAX_SKB_FRAGS + 1) {
5768 struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
5769
5770 netif_tx_stop_queue(nq);
5771 }
5772out:
5773 if (frags > 0) {
5774 struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
5775
5776 u64_stats_update_begin(&stats->syncp);
5777 stats->tx_packets++;
5778 stats->tx_bytes += skb->len;
5779 u64_stats_update_end(&stats->syncp);
5780 } else {
5781 dev->stats.tx_dropped++;
5782 dev_kfree_skb_any(skb);
5783 }
5784
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005785 /* Finalize TX processing */
5786 if (txq_pcpu->count >= txq->done_pkts_coal)
5787 mvpp2_txq_done(port, txq, txq_pcpu);
5788
5789 /* Set the timer in case not all frags were processed */
5790 if (txq_pcpu->count <= frags && txq_pcpu->count > 0) {
5791 struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
5792
5793 mvpp2_timer_set(port_pcpu);
5794 }
5795
Marcin Wojtas3f518502014-07-10 16:52:13 -03005796 return NETDEV_TX_OK;
5797}
5798
5799static inline void mvpp2_cause_error(struct net_device *dev, int cause)
5800{
5801 if (cause & MVPP2_CAUSE_FCS_ERR_MASK)
5802 netdev_err(dev, "FCS error\n");
5803 if (cause & MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK)
5804 netdev_err(dev, "rx fifo overrun error\n");
5805 if (cause & MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK)
5806 netdev_err(dev, "tx fifo underrun error\n");
5807}
5808
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005809static int mvpp2_poll(struct napi_struct *napi, int budget)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005810{
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005811 u32 cause_rx_tx, cause_rx, cause_misc;
5812 int rx_done = 0;
5813 struct mvpp2_port *port = netdev_priv(napi->dev);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02005814 struct mvpp2_queue_vector *qv;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005815 int cpu = smp_processor_id();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005816
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02005817 qv = container_of(napi, struct mvpp2_queue_vector, napi);
5818
Marcin Wojtas3f518502014-07-10 16:52:13 -03005819 /* Rx/Tx cause register
5820 *
5821 * Bits 0-15: each bit indicates received packets on the Rx queue
5822 * (bit 0 is for Rx queue 0).
5823 *
5824 * Bits 16-23: each bit indicates transmitted packets on the Tx queue
5825 * (bit 16 is for Tx queue 0).
5826 *
5827 * Each CPU has its own Rx/Tx cause register
5828 */
Thomas Petazzonia7868412017-03-07 16:53:13 +01005829 cause_rx_tx = mvpp2_percpu_read(port->priv, cpu,
5830 MVPP2_ISR_RX_TX_CAUSE_REG(port->id));
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005831 cause_rx_tx &= ~MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005832 cause_misc = cause_rx_tx & MVPP2_CAUSE_MISC_SUM_MASK;
5833
5834 if (cause_misc) {
5835 mvpp2_cause_error(port->dev, cause_misc);
5836
5837 /* Clear the cause register */
5838 mvpp2_write(port->priv, MVPP2_ISR_MISC_CAUSE_REG, 0);
Thomas Petazzonia7868412017-03-07 16:53:13 +01005839 mvpp2_percpu_write(port->priv, cpu,
5840 MVPP2_ISR_RX_TX_CAUSE_REG(port->id),
5841 cause_rx_tx & ~MVPP2_CAUSE_MISC_SUM_MASK);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005842 }
5843
Marcin Wojtas3f518502014-07-10 16:52:13 -03005844 cause_rx = cause_rx_tx & MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
5845
5846 /* Process RX packets */
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02005847 cause_rx |= qv->pending_cause_rx;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005848 while (cause_rx && budget > 0) {
5849 int count;
5850 struct mvpp2_rx_queue *rxq;
5851
5852 rxq = mvpp2_get_rx_queue(port, cause_rx);
5853 if (!rxq)
5854 break;
5855
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02005856 count = mvpp2_rx(port, napi, budget, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005857 rx_done += count;
5858 budget -= count;
5859 if (budget > 0) {
5860 /* Clear the bit associated to this Rx queue
5861 * so that next iteration will continue from
5862 * the next Rx queue.
5863 */
5864 cause_rx &= ~(1 << rxq->logic_rxq);
5865 }
5866 }
5867
5868 if (budget > 0) {
5869 cause_rx = 0;
Eric Dumazet6ad20162017-01-30 08:22:01 -08005870 napi_complete_done(napi, rx_done);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005871
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02005872 mvpp2_qvec_interrupt_enable(qv);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005873 }
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02005874 qv->pending_cause_rx = cause_rx;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005875 return rx_done;
5876}
5877
5878/* Set hw internals when starting port */
5879static void mvpp2_start_dev(struct mvpp2_port *port)
5880{
Philippe Reynes8e072692016-06-28 00:08:11 +02005881 struct net_device *ndev = port->dev;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02005882 int i;
Philippe Reynes8e072692016-06-28 00:08:11 +02005883
Marcin Wojtas3f518502014-07-10 16:52:13 -03005884 mvpp2_gmac_max_rx_size_set(port);
5885 mvpp2_txp_max_tx_size_set(port);
5886
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02005887 for (i = 0; i < port->nqvecs; i++)
5888 napi_enable(&port->qvecs[i].napi);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005889
5890 /* Enable interrupts on all CPUs */
5891 mvpp2_interrupts_enable(port);
5892
5893 mvpp2_port_enable(port);
Philippe Reynes8e072692016-06-28 00:08:11 +02005894 phy_start(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005895 netif_tx_start_all_queues(port->dev);
5896}
5897
5898/* Set hw internals when stopping port */
5899static void mvpp2_stop_dev(struct mvpp2_port *port)
5900{
Philippe Reynes8e072692016-06-28 00:08:11 +02005901 struct net_device *ndev = port->dev;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02005902 int i;
Philippe Reynes8e072692016-06-28 00:08:11 +02005903
Marcin Wojtas3f518502014-07-10 16:52:13 -03005904 /* Stop new packets from arriving to RXQs */
5905 mvpp2_ingress_disable(port);
5906
5907 mdelay(10);
5908
5909 /* Disable interrupts on all CPUs */
5910 mvpp2_interrupts_disable(port);
5911
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02005912 for (i = 0; i < port->nqvecs; i++)
5913 napi_disable(&port->qvecs[i].napi);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005914
5915 netif_carrier_off(port->dev);
5916 netif_tx_stop_all_queues(port->dev);
5917
5918 mvpp2_egress_disable(port);
5919 mvpp2_port_disable(port);
Philippe Reynes8e072692016-06-28 00:08:11 +02005920 phy_stop(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005921}
5922
Marcin Wojtas3f518502014-07-10 16:52:13 -03005923static int mvpp2_check_ringparam_valid(struct net_device *dev,
5924 struct ethtool_ringparam *ring)
5925{
5926 u16 new_rx_pending = ring->rx_pending;
5927 u16 new_tx_pending = ring->tx_pending;
5928
5929 if (ring->rx_pending == 0 || ring->tx_pending == 0)
5930 return -EINVAL;
5931
5932 if (ring->rx_pending > MVPP2_MAX_RXD)
5933 new_rx_pending = MVPP2_MAX_RXD;
5934 else if (!IS_ALIGNED(ring->rx_pending, 16))
5935 new_rx_pending = ALIGN(ring->rx_pending, 16);
5936
5937 if (ring->tx_pending > MVPP2_MAX_TXD)
5938 new_tx_pending = MVPP2_MAX_TXD;
5939 else if (!IS_ALIGNED(ring->tx_pending, 32))
5940 new_tx_pending = ALIGN(ring->tx_pending, 32);
5941
5942 if (ring->rx_pending != new_rx_pending) {
5943 netdev_info(dev, "illegal Rx ring size value %d, round to %d\n",
5944 ring->rx_pending, new_rx_pending);
5945 ring->rx_pending = new_rx_pending;
5946 }
5947
5948 if (ring->tx_pending != new_tx_pending) {
5949 netdev_info(dev, "illegal Tx ring size value %d, round to %d\n",
5950 ring->tx_pending, new_tx_pending);
5951 ring->tx_pending = new_tx_pending;
5952 }
5953
5954 return 0;
5955}
5956
Thomas Petazzoni26975822017-03-07 16:53:14 +01005957static void mvpp21_get_mac_address(struct mvpp2_port *port, unsigned char *addr)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005958{
5959 u32 mac_addr_l, mac_addr_m, mac_addr_h;
5960
5961 mac_addr_l = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
5962 mac_addr_m = readl(port->priv->lms_base + MVPP2_SRC_ADDR_MIDDLE);
5963 mac_addr_h = readl(port->priv->lms_base + MVPP2_SRC_ADDR_HIGH);
5964 addr[0] = (mac_addr_h >> 24) & 0xFF;
5965 addr[1] = (mac_addr_h >> 16) & 0xFF;
5966 addr[2] = (mac_addr_h >> 8) & 0xFF;
5967 addr[3] = mac_addr_h & 0xFF;
5968 addr[4] = mac_addr_m & 0xFF;
5969 addr[5] = (mac_addr_l >> MVPP2_GMAC_SA_LOW_OFFS) & 0xFF;
5970}
5971
5972static int mvpp2_phy_connect(struct mvpp2_port *port)
5973{
5974 struct phy_device *phy_dev;
5975
5976 phy_dev = of_phy_connect(port->dev, port->phy_node, mvpp2_link_event, 0,
5977 port->phy_interface);
5978 if (!phy_dev) {
5979 netdev_err(port->dev, "cannot connect to phy\n");
5980 return -ENODEV;
5981 }
5982 phy_dev->supported &= PHY_GBIT_FEATURES;
5983 phy_dev->advertising = phy_dev->supported;
5984
Marcin Wojtas3f518502014-07-10 16:52:13 -03005985 port->link = 0;
5986 port->duplex = 0;
5987 port->speed = 0;
5988
5989 return 0;
5990}
5991
5992static void mvpp2_phy_disconnect(struct mvpp2_port *port)
5993{
Philippe Reynes8e072692016-06-28 00:08:11 +02005994 struct net_device *ndev = port->dev;
5995
5996 phy_disconnect(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005997}
5998
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02005999static int mvpp2_irqs_init(struct mvpp2_port *port)
6000{
6001 int err, i;
6002
6003 for (i = 0; i < port->nqvecs; i++) {
6004 struct mvpp2_queue_vector *qv = port->qvecs + i;
6005
6006 err = request_irq(qv->irq, mvpp2_isr, 0, port->dev->name, qv);
6007 if (err)
6008 goto err;
6009 }
6010
6011 return 0;
6012err:
6013 for (i = 0; i < port->nqvecs; i++) {
6014 struct mvpp2_queue_vector *qv = port->qvecs + i;
6015
6016 free_irq(qv->irq, qv);
6017 }
6018
6019 return err;
6020}
6021
6022static void mvpp2_irqs_deinit(struct mvpp2_port *port)
6023{
6024 int i;
6025
6026 for (i = 0; i < port->nqvecs; i++) {
6027 struct mvpp2_queue_vector *qv = port->qvecs + i;
6028
6029 free_irq(qv->irq, qv);
6030 }
6031}
6032
Marcin Wojtas3f518502014-07-10 16:52:13 -03006033static int mvpp2_open(struct net_device *dev)
6034{
6035 struct mvpp2_port *port = netdev_priv(dev);
6036 unsigned char mac_bcast[ETH_ALEN] = {
6037 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
6038 int err;
6039
6040 err = mvpp2_prs_mac_da_accept(port->priv, port->id, mac_bcast, true);
6041 if (err) {
6042 netdev_err(dev, "mvpp2_prs_mac_da_accept BC failed\n");
6043 return err;
6044 }
6045 err = mvpp2_prs_mac_da_accept(port->priv, port->id,
6046 dev->dev_addr, true);
6047 if (err) {
6048 netdev_err(dev, "mvpp2_prs_mac_da_accept MC failed\n");
6049 return err;
6050 }
6051 err = mvpp2_prs_tag_mode_set(port->priv, port->id, MVPP2_TAG_TYPE_MH);
6052 if (err) {
6053 netdev_err(dev, "mvpp2_prs_tag_mode_set failed\n");
6054 return err;
6055 }
6056 err = mvpp2_prs_def_flow(port);
6057 if (err) {
6058 netdev_err(dev, "mvpp2_prs_def_flow failed\n");
6059 return err;
6060 }
6061
6062 /* Allocate the Rx/Tx queues */
6063 err = mvpp2_setup_rxqs(port);
6064 if (err) {
6065 netdev_err(port->dev, "cannot allocate Rx queues\n");
6066 return err;
6067 }
6068
6069 err = mvpp2_setup_txqs(port);
6070 if (err) {
6071 netdev_err(port->dev, "cannot allocate Tx queues\n");
6072 goto err_cleanup_rxqs;
6073 }
6074
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006075 err = mvpp2_irqs_init(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006076 if (err) {
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006077 netdev_err(port->dev, "cannot init IRQs\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03006078 goto err_cleanup_txqs;
6079 }
6080
6081 /* In default link is down */
6082 netif_carrier_off(port->dev);
6083
6084 err = mvpp2_phy_connect(port);
6085 if (err < 0)
6086 goto err_free_irq;
6087
6088 /* Unmask interrupts on all CPUs */
6089 on_each_cpu(mvpp2_interrupts_unmask, port, 1);
6090
6091 mvpp2_start_dev(port);
6092
6093 return 0;
6094
6095err_free_irq:
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006096 mvpp2_irqs_deinit(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006097err_cleanup_txqs:
6098 mvpp2_cleanup_txqs(port);
6099err_cleanup_rxqs:
6100 mvpp2_cleanup_rxqs(port);
6101 return err;
6102}
6103
6104static int mvpp2_stop(struct net_device *dev)
6105{
6106 struct mvpp2_port *port = netdev_priv(dev);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006107 struct mvpp2_port_pcpu *port_pcpu;
6108 int cpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006109
6110 mvpp2_stop_dev(port);
6111 mvpp2_phy_disconnect(port);
6112
6113 /* Mask interrupts on all CPUs */
6114 on_each_cpu(mvpp2_interrupts_mask, port, 1);
6115
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006116 mvpp2_irqs_deinit(port);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006117 for_each_present_cpu(cpu) {
6118 port_pcpu = per_cpu_ptr(port->pcpu, cpu);
6119
6120 hrtimer_cancel(&port_pcpu->tx_done_timer);
6121 port_pcpu->timer_scheduled = false;
6122 tasklet_kill(&port_pcpu->tx_done_tasklet);
6123 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006124 mvpp2_cleanup_rxqs(port);
6125 mvpp2_cleanup_txqs(port);
6126
6127 return 0;
6128}
6129
6130static void mvpp2_set_rx_mode(struct net_device *dev)
6131{
6132 struct mvpp2_port *port = netdev_priv(dev);
6133 struct mvpp2 *priv = port->priv;
6134 struct netdev_hw_addr *ha;
6135 int id = port->id;
6136 bool allmulti = dev->flags & IFF_ALLMULTI;
6137
6138 mvpp2_prs_mac_promisc_set(priv, id, dev->flags & IFF_PROMISC);
6139 mvpp2_prs_mac_multi_set(priv, id, MVPP2_PE_MAC_MC_ALL, allmulti);
6140 mvpp2_prs_mac_multi_set(priv, id, MVPP2_PE_MAC_MC_IP6, allmulti);
6141
6142 /* Remove all port->id's mcast enries */
6143 mvpp2_prs_mcast_del_all(priv, id);
6144
6145 if (allmulti && !netdev_mc_empty(dev)) {
6146 netdev_for_each_mc_addr(ha, dev)
6147 mvpp2_prs_mac_da_accept(priv, id, ha->addr, true);
6148 }
6149}
6150
6151static int mvpp2_set_mac_address(struct net_device *dev, void *p)
6152{
6153 struct mvpp2_port *port = netdev_priv(dev);
6154 const struct sockaddr *addr = p;
6155 int err;
6156
6157 if (!is_valid_ether_addr(addr->sa_data)) {
6158 err = -EADDRNOTAVAIL;
Markus Elfringc1175542017-04-17 11:10:47 +02006159 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006160 }
6161
6162 if (!netif_running(dev)) {
6163 err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
6164 if (!err)
6165 return 0;
6166 /* Reconfigure parser to accept the original MAC address */
6167 err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
6168 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02006169 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006170 }
6171
6172 mvpp2_stop_dev(port);
6173
6174 err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
6175 if (!err)
6176 goto out_start;
6177
6178 /* Reconfigure parser accept the original MAC address */
6179 err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
6180 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02006181 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006182out_start:
6183 mvpp2_start_dev(port);
6184 mvpp2_egress_enable(port);
6185 mvpp2_ingress_enable(port);
6186 return 0;
Markus Elfringc1175542017-04-17 11:10:47 +02006187log_error:
Markus Elfringdfd42402017-04-17 11:20:41 +02006188 netdev_err(dev, "failed to change MAC address\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03006189 return err;
6190}
6191
6192static int mvpp2_change_mtu(struct net_device *dev, int mtu)
6193{
6194 struct mvpp2_port *port = netdev_priv(dev);
6195 int err;
6196
Jarod Wilson57779872016-10-17 15:54:06 -04006197 if (!IS_ALIGNED(MVPP2_RX_PKT_SIZE(mtu), 8)) {
6198 netdev_info(dev, "illegal MTU value %d, round to %d\n", mtu,
6199 ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8));
6200 mtu = ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006201 }
6202
6203 if (!netif_running(dev)) {
6204 err = mvpp2_bm_update_mtu(dev, mtu);
6205 if (!err) {
6206 port->pkt_size = MVPP2_RX_PKT_SIZE(mtu);
6207 return 0;
6208 }
6209
6210 /* Reconfigure BM to the original MTU */
6211 err = mvpp2_bm_update_mtu(dev, dev->mtu);
6212 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02006213 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006214 }
6215
6216 mvpp2_stop_dev(port);
6217
6218 err = mvpp2_bm_update_mtu(dev, mtu);
6219 if (!err) {
6220 port->pkt_size = MVPP2_RX_PKT_SIZE(mtu);
6221 goto out_start;
6222 }
6223
6224 /* Reconfigure BM to the original MTU */
6225 err = mvpp2_bm_update_mtu(dev, dev->mtu);
6226 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02006227 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006228
6229out_start:
6230 mvpp2_start_dev(port);
6231 mvpp2_egress_enable(port);
6232 mvpp2_ingress_enable(port);
6233
6234 return 0;
Markus Elfringc1175542017-04-17 11:10:47 +02006235log_error:
Markus Elfringdfd42402017-04-17 11:20:41 +02006236 netdev_err(dev, "failed to change MTU\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03006237 return err;
6238}
6239
stephen hemmingerbc1f4472017-01-06 19:12:52 -08006240static void
Marcin Wojtas3f518502014-07-10 16:52:13 -03006241mvpp2_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
6242{
6243 struct mvpp2_port *port = netdev_priv(dev);
6244 unsigned int start;
6245 int cpu;
6246
6247 for_each_possible_cpu(cpu) {
6248 struct mvpp2_pcpu_stats *cpu_stats;
6249 u64 rx_packets;
6250 u64 rx_bytes;
6251 u64 tx_packets;
6252 u64 tx_bytes;
6253
6254 cpu_stats = per_cpu_ptr(port->stats, cpu);
6255 do {
6256 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
6257 rx_packets = cpu_stats->rx_packets;
6258 rx_bytes = cpu_stats->rx_bytes;
6259 tx_packets = cpu_stats->tx_packets;
6260 tx_bytes = cpu_stats->tx_bytes;
6261 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
6262
6263 stats->rx_packets += rx_packets;
6264 stats->rx_bytes += rx_bytes;
6265 stats->tx_packets += tx_packets;
6266 stats->tx_bytes += tx_bytes;
6267 }
6268
6269 stats->rx_errors = dev->stats.rx_errors;
6270 stats->rx_dropped = dev->stats.rx_dropped;
6271 stats->tx_dropped = dev->stats.tx_dropped;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006272}
6273
Thomas Petazzonibd695a52014-07-27 23:21:36 +02006274static int mvpp2_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
6275{
Thomas Petazzonibd695a52014-07-27 23:21:36 +02006276 int ret;
6277
Philippe Reynes8e072692016-06-28 00:08:11 +02006278 if (!dev->phydev)
Thomas Petazzonibd695a52014-07-27 23:21:36 +02006279 return -ENOTSUPP;
6280
Philippe Reynes8e072692016-06-28 00:08:11 +02006281 ret = phy_mii_ioctl(dev->phydev, ifr, cmd);
Thomas Petazzonibd695a52014-07-27 23:21:36 +02006282 if (!ret)
6283 mvpp2_link_event(dev);
6284
6285 return ret;
6286}
6287
Marcin Wojtas3f518502014-07-10 16:52:13 -03006288/* Ethtool methods */
6289
Marcin Wojtas3f518502014-07-10 16:52:13 -03006290/* Set interrupt coalescing for ethtools */
6291static int mvpp2_ethtool_set_coalesce(struct net_device *dev,
6292 struct ethtool_coalesce *c)
6293{
6294 struct mvpp2_port *port = netdev_priv(dev);
6295 int queue;
6296
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006297 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006298 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
6299
6300 rxq->time_coal = c->rx_coalesce_usecs;
6301 rxq->pkts_coal = c->rx_max_coalesced_frames;
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01006302 mvpp2_rx_pkts_coal_set(port, rxq);
6303 mvpp2_rx_time_coal_set(port, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006304 }
6305
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006306 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006307 struct mvpp2_tx_queue *txq = port->txqs[queue];
6308
6309 txq->done_pkts_coal = c->tx_max_coalesced_frames;
6310 }
6311
Marcin Wojtas3f518502014-07-10 16:52:13 -03006312 return 0;
6313}
6314
6315/* get coalescing for ethtools */
6316static int mvpp2_ethtool_get_coalesce(struct net_device *dev,
6317 struct ethtool_coalesce *c)
6318{
6319 struct mvpp2_port *port = netdev_priv(dev);
6320
6321 c->rx_coalesce_usecs = port->rxqs[0]->time_coal;
6322 c->rx_max_coalesced_frames = port->rxqs[0]->pkts_coal;
6323 c->tx_max_coalesced_frames = port->txqs[0]->done_pkts_coal;
6324 return 0;
6325}
6326
6327static void mvpp2_ethtool_get_drvinfo(struct net_device *dev,
6328 struct ethtool_drvinfo *drvinfo)
6329{
6330 strlcpy(drvinfo->driver, MVPP2_DRIVER_NAME,
6331 sizeof(drvinfo->driver));
6332 strlcpy(drvinfo->version, MVPP2_DRIVER_VERSION,
6333 sizeof(drvinfo->version));
6334 strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
6335 sizeof(drvinfo->bus_info));
6336}
6337
6338static void mvpp2_ethtool_get_ringparam(struct net_device *dev,
6339 struct ethtool_ringparam *ring)
6340{
6341 struct mvpp2_port *port = netdev_priv(dev);
6342
6343 ring->rx_max_pending = MVPP2_MAX_RXD;
6344 ring->tx_max_pending = MVPP2_MAX_TXD;
6345 ring->rx_pending = port->rx_ring_size;
6346 ring->tx_pending = port->tx_ring_size;
6347}
6348
6349static int mvpp2_ethtool_set_ringparam(struct net_device *dev,
6350 struct ethtool_ringparam *ring)
6351{
6352 struct mvpp2_port *port = netdev_priv(dev);
6353 u16 prev_rx_ring_size = port->rx_ring_size;
6354 u16 prev_tx_ring_size = port->tx_ring_size;
6355 int err;
6356
6357 err = mvpp2_check_ringparam_valid(dev, ring);
6358 if (err)
6359 return err;
6360
6361 if (!netif_running(dev)) {
6362 port->rx_ring_size = ring->rx_pending;
6363 port->tx_ring_size = ring->tx_pending;
6364 return 0;
6365 }
6366
6367 /* The interface is running, so we have to force a
6368 * reallocation of the queues
6369 */
6370 mvpp2_stop_dev(port);
6371 mvpp2_cleanup_rxqs(port);
6372 mvpp2_cleanup_txqs(port);
6373
6374 port->rx_ring_size = ring->rx_pending;
6375 port->tx_ring_size = ring->tx_pending;
6376
6377 err = mvpp2_setup_rxqs(port);
6378 if (err) {
6379 /* Reallocate Rx queues with the original ring size */
6380 port->rx_ring_size = prev_rx_ring_size;
6381 ring->rx_pending = prev_rx_ring_size;
6382 err = mvpp2_setup_rxqs(port);
6383 if (err)
6384 goto err_out;
6385 }
6386 err = mvpp2_setup_txqs(port);
6387 if (err) {
6388 /* Reallocate Tx queues with the original ring size */
6389 port->tx_ring_size = prev_tx_ring_size;
6390 ring->tx_pending = prev_tx_ring_size;
6391 err = mvpp2_setup_txqs(port);
6392 if (err)
6393 goto err_clean_rxqs;
6394 }
6395
6396 mvpp2_start_dev(port);
6397 mvpp2_egress_enable(port);
6398 mvpp2_ingress_enable(port);
6399
6400 return 0;
6401
6402err_clean_rxqs:
6403 mvpp2_cleanup_rxqs(port);
6404err_out:
Markus Elfringdfd42402017-04-17 11:20:41 +02006405 netdev_err(dev, "failed to change ring parameters");
Marcin Wojtas3f518502014-07-10 16:52:13 -03006406 return err;
6407}
6408
6409/* Device ops */
6410
6411static const struct net_device_ops mvpp2_netdev_ops = {
6412 .ndo_open = mvpp2_open,
6413 .ndo_stop = mvpp2_stop,
6414 .ndo_start_xmit = mvpp2_tx,
6415 .ndo_set_rx_mode = mvpp2_set_rx_mode,
6416 .ndo_set_mac_address = mvpp2_set_mac_address,
6417 .ndo_change_mtu = mvpp2_change_mtu,
6418 .ndo_get_stats64 = mvpp2_get_stats64,
Thomas Petazzonibd695a52014-07-27 23:21:36 +02006419 .ndo_do_ioctl = mvpp2_ioctl,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006420};
6421
6422static const struct ethtool_ops mvpp2_eth_tool_ops = {
Florian Fainelli00606c42016-11-15 11:19:48 -08006423 .nway_reset = phy_ethtool_nway_reset,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006424 .get_link = ethtool_op_get_link,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006425 .set_coalesce = mvpp2_ethtool_set_coalesce,
6426 .get_coalesce = mvpp2_ethtool_get_coalesce,
6427 .get_drvinfo = mvpp2_ethtool_get_drvinfo,
6428 .get_ringparam = mvpp2_ethtool_get_ringparam,
6429 .set_ringparam = mvpp2_ethtool_set_ringparam,
Philippe Reynesfb773e92016-06-28 00:08:12 +02006430 .get_link_ksettings = phy_ethtool_get_link_ksettings,
6431 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006432};
6433
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006434static int mvpp2_queue_vectors_init(struct mvpp2_port *port,
6435 struct device_node *port_node)
6436{
6437 struct mvpp2_queue_vector *v = &port->qvecs[0];
6438
6439 v->first_rxq = 0;
6440 v->nrxqs = port->nrxqs;
6441 v->type = MVPP2_QUEUE_VECTOR_SHARED;
6442 v->sw_thread_id = 0;
6443 v->sw_thread_mask = *cpumask_bits(cpu_online_mask);
6444 v->port = port;
6445 v->irq = irq_of_parse_and_map(port_node, 0);
6446 if (v->irq <= 0)
6447 return -EINVAL;
6448 netif_napi_add(port->dev, &v->napi, mvpp2_poll,
6449 NAPI_POLL_WEIGHT);
6450
6451 port->nqvecs = 1;
6452
6453 return 0;
6454}
6455
6456static void mvpp2_queue_vectors_deinit(struct mvpp2_port *port)
6457{
6458 int i;
6459
6460 for (i = 0; i < port->nqvecs; i++)
6461 irq_dispose_mapping(port->qvecs[i].irq);
6462}
6463
6464/* Configure Rx queue group interrupt for this port */
6465static void mvpp2_rx_irqs_setup(struct mvpp2_port *port)
6466{
6467 struct mvpp2 *priv = port->priv;
6468 u32 val;
6469 int i;
6470
6471 if (priv->hw_version == MVPP21) {
6472 mvpp2_write(priv, MVPP21_ISR_RXQ_GROUP_REG(port->id),
6473 port->nrxqs);
6474 return;
6475 }
6476
6477 /* Handle the more complicated PPv2.2 case */
6478 for (i = 0; i < port->nqvecs; i++) {
6479 struct mvpp2_queue_vector *qv = port->qvecs + i;
6480
6481 if (!qv->nrxqs)
6482 continue;
6483
6484 val = qv->sw_thread_id;
6485 val |= port->id << MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_OFFSET;
6486 mvpp2_write(priv, MVPP22_ISR_RXQ_GROUP_INDEX_REG, val);
6487
6488 val = qv->first_rxq;
6489 val |= qv->nrxqs << MVPP22_ISR_RXQ_SUB_GROUP_SIZE_OFFSET;
6490 mvpp2_write(priv, MVPP22_ISR_RXQ_SUB_GROUP_CONFIG_REG, val);
6491 }
6492}
6493
Marcin Wojtas3f518502014-07-10 16:52:13 -03006494/* Initialize port HW */
6495static int mvpp2_port_init(struct mvpp2_port *port)
6496{
6497 struct device *dev = port->dev->dev.parent;
6498 struct mvpp2 *priv = port->priv;
6499 struct mvpp2_txq_pcpu *txq_pcpu;
6500 int queue, cpu, err;
6501
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006502 /* Checks for hardware constraints */
6503 if (port->first_rxq + port->nrxqs >
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01006504 MVPP2_MAX_PORTS * priv->max_port_rxqs)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006505 return -EINVAL;
6506
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006507 if (port->nrxqs % 4 || (port->nrxqs > priv->max_port_rxqs) ||
6508 (port->ntxqs > MVPP2_MAX_TXQ))
6509 return -EINVAL;
6510
Marcin Wojtas3f518502014-07-10 16:52:13 -03006511 /* Disable port */
6512 mvpp2_egress_disable(port);
6513 mvpp2_port_disable(port);
6514
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006515 port->txqs = devm_kcalloc(dev, port->ntxqs, sizeof(*port->txqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03006516 GFP_KERNEL);
6517 if (!port->txqs)
6518 return -ENOMEM;
6519
6520 /* Associate physical Tx queues to this port and initialize.
6521 * The mapping is predefined.
6522 */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006523 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006524 int queue_phy_id = mvpp2_txq_phys(port->id, queue);
6525 struct mvpp2_tx_queue *txq;
6526
6527 txq = devm_kzalloc(dev, sizeof(*txq), GFP_KERNEL);
Christophe Jaillet177c8d12017-02-19 10:19:57 +01006528 if (!txq) {
6529 err = -ENOMEM;
6530 goto err_free_percpu;
6531 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006532
6533 txq->pcpu = alloc_percpu(struct mvpp2_txq_pcpu);
6534 if (!txq->pcpu) {
6535 err = -ENOMEM;
6536 goto err_free_percpu;
6537 }
6538
6539 txq->id = queue_phy_id;
6540 txq->log_id = queue;
6541 txq->done_pkts_coal = MVPP2_TXDONE_COAL_PKTS_THRESH;
6542 for_each_present_cpu(cpu) {
6543 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
6544 txq_pcpu->cpu = cpu;
6545 }
6546
6547 port->txqs[queue] = txq;
6548 }
6549
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006550 port->rxqs = devm_kcalloc(dev, port->nrxqs, sizeof(*port->rxqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03006551 GFP_KERNEL);
6552 if (!port->rxqs) {
6553 err = -ENOMEM;
6554 goto err_free_percpu;
6555 }
6556
6557 /* Allocate and initialize Rx queue for this port */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006558 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006559 struct mvpp2_rx_queue *rxq;
6560
6561 /* Map physical Rx queue to port's logical Rx queue */
6562 rxq = devm_kzalloc(dev, sizeof(*rxq), GFP_KERNEL);
Jisheng Zhangd82b0c22016-03-31 17:01:23 +08006563 if (!rxq) {
6564 err = -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006565 goto err_free_percpu;
Jisheng Zhangd82b0c22016-03-31 17:01:23 +08006566 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006567 /* Map this Rx queue to a physical queue */
6568 rxq->id = port->first_rxq + queue;
6569 rxq->port = port->id;
6570 rxq->logic_rxq = queue;
6571
6572 port->rxqs[queue] = rxq;
6573 }
6574
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006575 mvpp2_rx_irqs_setup(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006576
6577 /* Create Rx descriptor rings */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006578 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006579 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
6580
6581 rxq->size = port->rx_ring_size;
6582 rxq->pkts_coal = MVPP2_RX_COAL_PKTS;
6583 rxq->time_coal = MVPP2_RX_COAL_USEC;
6584 }
6585
6586 mvpp2_ingress_disable(port);
6587
6588 /* Port default configuration */
6589 mvpp2_defaults_set(port);
6590
6591 /* Port's classifier configuration */
6592 mvpp2_cls_oversize_rxq_set(port);
6593 mvpp2_cls_port_config(port);
6594
6595 /* Provide an initial Rx packet size */
6596 port->pkt_size = MVPP2_RX_PKT_SIZE(port->dev->mtu);
6597
6598 /* Initialize pools for swf */
6599 err = mvpp2_swf_bm_pool_init(port);
6600 if (err)
6601 goto err_free_percpu;
6602
6603 return 0;
6604
6605err_free_percpu:
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006606 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006607 if (!port->txqs[queue])
6608 continue;
6609 free_percpu(port->txqs[queue]->pcpu);
6610 }
6611 return err;
6612}
6613
6614/* Ports initialization */
6615static int mvpp2_port_probe(struct platform_device *pdev,
6616 struct device_node *port_node,
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01006617 struct mvpp2 *priv)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006618{
6619 struct device_node *phy_node;
6620 struct mvpp2_port *port;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006621 struct mvpp2_port_pcpu *port_pcpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006622 struct net_device *dev;
6623 struct resource *res;
6624 const char *dt_mac_addr;
6625 const char *mac_from;
6626 char hw_mac_addr[ETH_ALEN];
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006627 unsigned int ntxqs, nrxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006628 u32 id;
6629 int features;
6630 int phy_mode;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006631 int err, i, cpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006632
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006633 ntxqs = MVPP2_MAX_TXQ;
6634 nrxqs = MVPP2_DEFAULT_RXQ;
6635
6636 dev = alloc_etherdev_mqs(sizeof(*port), ntxqs, nrxqs);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006637 if (!dev)
6638 return -ENOMEM;
6639
6640 phy_node = of_parse_phandle(port_node, "phy", 0);
6641 if (!phy_node) {
6642 dev_err(&pdev->dev, "missing phy\n");
6643 err = -ENODEV;
6644 goto err_free_netdev;
6645 }
6646
6647 phy_mode = of_get_phy_mode(port_node);
6648 if (phy_mode < 0) {
6649 dev_err(&pdev->dev, "incorrect phy mode\n");
6650 err = phy_mode;
6651 goto err_free_netdev;
6652 }
6653
6654 if (of_property_read_u32(port_node, "port-id", &id)) {
6655 err = -EINVAL;
6656 dev_err(&pdev->dev, "missing port-id value\n");
6657 goto err_free_netdev;
6658 }
6659
6660 dev->tx_queue_len = MVPP2_MAX_TXD;
6661 dev->watchdog_timeo = 5 * HZ;
6662 dev->netdev_ops = &mvpp2_netdev_ops;
6663 dev->ethtool_ops = &mvpp2_eth_tool_ops;
6664
6665 port = netdev_priv(dev);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006666 port->dev = dev;
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006667 port->ntxqs = ntxqs;
6668 port->nrxqs = nrxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006669
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006670 err = mvpp2_queue_vectors_init(port, port_node);
6671 if (err)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006672 goto err_free_netdev;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006673
6674 if (of_property_read_bool(port_node, "marvell,loopback"))
6675 port->flags |= MVPP2_F_LOOPBACK;
6676
6677 port->priv = priv;
6678 port->id = id;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01006679 if (priv->hw_version == MVPP21)
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006680 port->first_rxq = port->id * port->nrxqs;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01006681 else
6682 port->first_rxq = port->id * priv->max_port_rxqs;
6683
Marcin Wojtas3f518502014-07-10 16:52:13 -03006684 port->phy_node = phy_node;
6685 port->phy_interface = phy_mode;
6686
Thomas Petazzonia7868412017-03-07 16:53:13 +01006687 if (priv->hw_version == MVPP21) {
6688 res = platform_get_resource(pdev, IORESOURCE_MEM, 2 + id);
6689 port->base = devm_ioremap_resource(&pdev->dev, res);
6690 if (IS_ERR(port->base)) {
6691 err = PTR_ERR(port->base);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006692 goto err_deinit_qvecs;
Thomas Petazzonia7868412017-03-07 16:53:13 +01006693 }
6694 } else {
6695 if (of_property_read_u32(port_node, "gop-port-id",
6696 &port->gop_id)) {
6697 err = -EINVAL;
6698 dev_err(&pdev->dev, "missing gop-port-id value\n");
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006699 goto err_deinit_qvecs;
Thomas Petazzonia7868412017-03-07 16:53:13 +01006700 }
6701
6702 port->base = priv->iface_base + MVPP22_GMAC_BASE(port->gop_id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006703 }
6704
6705 /* Alloc per-cpu stats */
6706 port->stats = netdev_alloc_pcpu_stats(struct mvpp2_pcpu_stats);
6707 if (!port->stats) {
6708 err = -ENOMEM;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006709 goto err_deinit_qvecs;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006710 }
6711
6712 dt_mac_addr = of_get_mac_address(port_node);
6713 if (dt_mac_addr && is_valid_ether_addr(dt_mac_addr)) {
6714 mac_from = "device tree";
6715 ether_addr_copy(dev->dev_addr, dt_mac_addr);
6716 } else {
Thomas Petazzoni26975822017-03-07 16:53:14 +01006717 if (priv->hw_version == MVPP21)
6718 mvpp21_get_mac_address(port, hw_mac_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006719 if (is_valid_ether_addr(hw_mac_addr)) {
6720 mac_from = "hardware";
6721 ether_addr_copy(dev->dev_addr, hw_mac_addr);
6722 } else {
6723 mac_from = "random";
6724 eth_hw_addr_random(dev);
6725 }
6726 }
6727
6728 port->tx_ring_size = MVPP2_MAX_TXD;
6729 port->rx_ring_size = MVPP2_MAX_RXD;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006730 SET_NETDEV_DEV(dev, &pdev->dev);
6731
6732 err = mvpp2_port_init(port);
6733 if (err < 0) {
6734 dev_err(&pdev->dev, "failed to init port %d\n", id);
6735 goto err_free_stats;
6736 }
Thomas Petazzoni26975822017-03-07 16:53:14 +01006737
6738 mvpp2_port_mii_set(port);
6739 mvpp2_port_periodic_xon_disable(port);
6740
6741 if (priv->hw_version == MVPP21)
6742 mvpp2_port_fc_adv_enable(port);
6743
6744 mvpp2_port_reset(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006745
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006746 port->pcpu = alloc_percpu(struct mvpp2_port_pcpu);
6747 if (!port->pcpu) {
6748 err = -ENOMEM;
6749 goto err_free_txq_pcpu;
6750 }
6751
6752 for_each_present_cpu(cpu) {
6753 port_pcpu = per_cpu_ptr(port->pcpu, cpu);
6754
6755 hrtimer_init(&port_pcpu->tx_done_timer, CLOCK_MONOTONIC,
6756 HRTIMER_MODE_REL_PINNED);
6757 port_pcpu->tx_done_timer.function = mvpp2_hr_timer_cb;
6758 port_pcpu->timer_scheduled = false;
6759
6760 tasklet_init(&port_pcpu->tx_done_tasklet, mvpp2_tx_proc_cb,
6761 (unsigned long)dev);
6762 }
6763
Marcin Wojtas3f518502014-07-10 16:52:13 -03006764 features = NETIF_F_SG | NETIF_F_IP_CSUM;
6765 dev->features = features | NETIF_F_RXCSUM;
6766 dev->hw_features |= features | NETIF_F_RXCSUM | NETIF_F_GRO;
6767 dev->vlan_features |= features;
6768
Jarod Wilson57779872016-10-17 15:54:06 -04006769 /* MTU range: 68 - 9676 */
6770 dev->min_mtu = ETH_MIN_MTU;
6771 /* 9676 == 9700 - 20 and rounding to 8 */
6772 dev->max_mtu = 9676;
6773
Marcin Wojtas3f518502014-07-10 16:52:13 -03006774 err = register_netdev(dev);
6775 if (err < 0) {
6776 dev_err(&pdev->dev, "failed to register netdev\n");
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006777 goto err_free_port_pcpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006778 }
6779 netdev_info(dev, "Using %s mac address %pM\n", mac_from, dev->dev_addr);
6780
Marcin Wojtas3f518502014-07-10 16:52:13 -03006781 priv->port_list[id] = port;
6782 return 0;
6783
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006784err_free_port_pcpu:
6785 free_percpu(port->pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006786err_free_txq_pcpu:
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006787 for (i = 0; i < port->ntxqs; i++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006788 free_percpu(port->txqs[i]->pcpu);
6789err_free_stats:
6790 free_percpu(port->stats);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006791err_deinit_qvecs:
6792 mvpp2_queue_vectors_deinit(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006793err_free_netdev:
Peter Chenccb80392016-08-01 15:02:37 +08006794 of_node_put(phy_node);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006795 free_netdev(dev);
6796 return err;
6797}
6798
6799/* Ports removal routine */
6800static void mvpp2_port_remove(struct mvpp2_port *port)
6801{
6802 int i;
6803
6804 unregister_netdev(port->dev);
Peter Chenccb80392016-08-01 15:02:37 +08006805 of_node_put(port->phy_node);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006806 free_percpu(port->pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006807 free_percpu(port->stats);
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006808 for (i = 0; i < port->ntxqs; i++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006809 free_percpu(port->txqs[i]->pcpu);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006810 mvpp2_queue_vectors_deinit(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006811 free_netdev(port->dev);
6812}
6813
6814/* Initialize decoding windows */
6815static void mvpp2_conf_mbus_windows(const struct mbus_dram_target_info *dram,
6816 struct mvpp2 *priv)
6817{
6818 u32 win_enable;
6819 int i;
6820
6821 for (i = 0; i < 6; i++) {
6822 mvpp2_write(priv, MVPP2_WIN_BASE(i), 0);
6823 mvpp2_write(priv, MVPP2_WIN_SIZE(i), 0);
6824
6825 if (i < 4)
6826 mvpp2_write(priv, MVPP2_WIN_REMAP(i), 0);
6827 }
6828
6829 win_enable = 0;
6830
6831 for (i = 0; i < dram->num_cs; i++) {
6832 const struct mbus_dram_window *cs = dram->cs + i;
6833
6834 mvpp2_write(priv, MVPP2_WIN_BASE(i),
6835 (cs->base & 0xffff0000) | (cs->mbus_attr << 8) |
6836 dram->mbus_dram_target_id);
6837
6838 mvpp2_write(priv, MVPP2_WIN_SIZE(i),
6839 (cs->size - 1) & 0xffff0000);
6840
6841 win_enable |= (1 << i);
6842 }
6843
6844 mvpp2_write(priv, MVPP2_BASE_ADDR_ENABLE, win_enable);
6845}
6846
6847/* Initialize Rx FIFO's */
6848static void mvpp2_rx_fifo_init(struct mvpp2 *priv)
6849{
6850 int port;
6851
6852 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
6853 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
6854 MVPP2_RX_FIFO_PORT_DATA_SIZE);
6855 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
6856 MVPP2_RX_FIFO_PORT_ATTR_SIZE);
6857 }
6858
6859 mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
6860 MVPP2_RX_FIFO_PORT_MIN_PKT);
6861 mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
6862}
6863
Thomas Petazzoni6763ce32017-03-07 16:53:15 +01006864static void mvpp2_axi_init(struct mvpp2 *priv)
6865{
6866 u32 val, rdval, wrval;
6867
6868 mvpp2_write(priv, MVPP22_BM_ADDR_HIGH_RLS_REG, 0x0);
6869
6870 /* AXI Bridge Configuration */
6871
6872 rdval = MVPP22_AXI_CODE_CACHE_RD_CACHE
6873 << MVPP22_AXI_ATTR_CACHE_OFFS;
6874 rdval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
6875 << MVPP22_AXI_ATTR_DOMAIN_OFFS;
6876
6877 wrval = MVPP22_AXI_CODE_CACHE_WR_CACHE
6878 << MVPP22_AXI_ATTR_CACHE_OFFS;
6879 wrval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
6880 << MVPP22_AXI_ATTR_DOMAIN_OFFS;
6881
6882 /* BM */
6883 mvpp2_write(priv, MVPP22_AXI_BM_WR_ATTR_REG, wrval);
6884 mvpp2_write(priv, MVPP22_AXI_BM_RD_ATTR_REG, rdval);
6885
6886 /* Descriptors */
6887 mvpp2_write(priv, MVPP22_AXI_AGGRQ_DESCR_RD_ATTR_REG, rdval);
6888 mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_WR_ATTR_REG, wrval);
6889 mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_RD_ATTR_REG, rdval);
6890 mvpp2_write(priv, MVPP22_AXI_RXQ_DESCR_WR_ATTR_REG, wrval);
6891
6892 /* Buffer Data */
6893 mvpp2_write(priv, MVPP22_AXI_TX_DATA_RD_ATTR_REG, rdval);
6894 mvpp2_write(priv, MVPP22_AXI_RX_DATA_WR_ATTR_REG, wrval);
6895
6896 val = MVPP22_AXI_CODE_CACHE_NON_CACHE
6897 << MVPP22_AXI_CODE_CACHE_OFFS;
6898 val |= MVPP22_AXI_CODE_DOMAIN_SYSTEM
6899 << MVPP22_AXI_CODE_DOMAIN_OFFS;
6900 mvpp2_write(priv, MVPP22_AXI_RD_NORMAL_CODE_REG, val);
6901 mvpp2_write(priv, MVPP22_AXI_WR_NORMAL_CODE_REG, val);
6902
6903 val = MVPP22_AXI_CODE_CACHE_RD_CACHE
6904 << MVPP22_AXI_CODE_CACHE_OFFS;
6905 val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
6906 << MVPP22_AXI_CODE_DOMAIN_OFFS;
6907
6908 mvpp2_write(priv, MVPP22_AXI_RD_SNOOP_CODE_REG, val);
6909
6910 val = MVPP22_AXI_CODE_CACHE_WR_CACHE
6911 << MVPP22_AXI_CODE_CACHE_OFFS;
6912 val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
6913 << MVPP22_AXI_CODE_DOMAIN_OFFS;
6914
6915 mvpp2_write(priv, MVPP22_AXI_WR_SNOOP_CODE_REG, val);
6916}
6917
Marcin Wojtas3f518502014-07-10 16:52:13 -03006918/* Initialize network controller common part HW */
6919static int mvpp2_init(struct platform_device *pdev, struct mvpp2 *priv)
6920{
6921 const struct mbus_dram_target_info *dram_target_info;
6922 int err, i;
Marcin Wojtas08a23752014-07-21 13:48:12 -03006923 u32 val;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006924
Marcin Wojtas3f518502014-07-10 16:52:13 -03006925 /* MBUS windows configuration */
6926 dram_target_info = mv_mbus_dram_info();
6927 if (dram_target_info)
6928 mvpp2_conf_mbus_windows(dram_target_info, priv);
6929
Thomas Petazzoni6763ce32017-03-07 16:53:15 +01006930 if (priv->hw_version == MVPP22)
6931 mvpp2_axi_init(priv);
6932
Marcin Wojtas08a23752014-07-21 13:48:12 -03006933 /* Disable HW PHY polling */
Thomas Petazzoni26975822017-03-07 16:53:14 +01006934 if (priv->hw_version == MVPP21) {
6935 val = readl(priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
6936 val |= MVPP2_PHY_AN_STOP_SMI0_MASK;
6937 writel(val, priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
6938 } else {
6939 val = readl(priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
6940 val &= ~MVPP22_SMI_POLLING_EN;
6941 writel(val, priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
6942 }
Marcin Wojtas08a23752014-07-21 13:48:12 -03006943
Marcin Wojtas3f518502014-07-10 16:52:13 -03006944 /* Allocate and initialize aggregated TXQs */
6945 priv->aggr_txqs = devm_kcalloc(&pdev->dev, num_present_cpus(),
Markus Elfringd7ce3ce2017-04-17 08:48:23 +02006946 sizeof(*priv->aggr_txqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03006947 GFP_KERNEL);
6948 if (!priv->aggr_txqs)
6949 return -ENOMEM;
6950
6951 for_each_present_cpu(i) {
6952 priv->aggr_txqs[i].id = i;
6953 priv->aggr_txqs[i].size = MVPP2_AGGR_TXQ_SIZE;
6954 err = mvpp2_aggr_txq_init(pdev, &priv->aggr_txqs[i],
6955 MVPP2_AGGR_TXQ_SIZE, i, priv);
6956 if (err < 0)
6957 return err;
6958 }
6959
6960 /* Rx Fifo Init */
6961 mvpp2_rx_fifo_init(priv);
6962
Thomas Petazzoni26975822017-03-07 16:53:14 +01006963 if (priv->hw_version == MVPP21)
6964 writel(MVPP2_EXT_GLOBAL_CTRL_DEFAULT,
6965 priv->lms_base + MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006966
6967 /* Allow cache snoop when transmiting packets */
6968 mvpp2_write(priv, MVPP2_TX_SNOOP_REG, 0x1);
6969
6970 /* Buffer Manager initialization */
6971 err = mvpp2_bm_init(pdev, priv);
6972 if (err < 0)
6973 return err;
6974
6975 /* Parser default initialization */
6976 err = mvpp2_prs_default_init(pdev, priv);
6977 if (err < 0)
6978 return err;
6979
6980 /* Classifier default initialization */
6981 mvpp2_cls_init(priv);
6982
6983 return 0;
6984}
6985
6986static int mvpp2_probe(struct platform_device *pdev)
6987{
6988 struct device_node *dn = pdev->dev.of_node;
6989 struct device_node *port_node;
6990 struct mvpp2 *priv;
6991 struct resource *res;
Thomas Petazzonia7868412017-03-07 16:53:13 +01006992 void __iomem *base;
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02006993 int port_count, i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006994 int err;
6995
Markus Elfring0b92e592017-04-17 08:38:32 +02006996 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006997 if (!priv)
6998 return -ENOMEM;
6999
Thomas Petazzonifaca9242017-03-07 16:53:06 +01007000 priv->hw_version =
7001 (unsigned long)of_device_get_match_data(&pdev->dev);
7002
Marcin Wojtas3f518502014-07-10 16:52:13 -03007003 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thomas Petazzonia7868412017-03-07 16:53:13 +01007004 base = devm_ioremap_resource(&pdev->dev, res);
7005 if (IS_ERR(base))
7006 return PTR_ERR(base);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007007
Thomas Petazzonia7868412017-03-07 16:53:13 +01007008 if (priv->hw_version == MVPP21) {
7009 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
7010 priv->lms_base = devm_ioremap_resource(&pdev->dev, res);
7011 if (IS_ERR(priv->lms_base))
7012 return PTR_ERR(priv->lms_base);
7013 } else {
7014 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
7015 priv->iface_base = devm_ioremap_resource(&pdev->dev, res);
7016 if (IS_ERR(priv->iface_base))
7017 return PTR_ERR(priv->iface_base);
7018 }
7019
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02007020 for (i = 0; i < MVPP2_MAX_THREADS; i++) {
Thomas Petazzonia7868412017-03-07 16:53:13 +01007021 u32 addr_space_sz;
7022
7023 addr_space_sz = (priv->hw_version == MVPP21 ?
7024 MVPP21_ADDR_SPACE_SZ : MVPP22_ADDR_SPACE_SZ);
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02007025 priv->swth_base[i] = base + i * addr_space_sz;
Thomas Petazzonia7868412017-03-07 16:53:13 +01007026 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03007027
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01007028 if (priv->hw_version == MVPP21)
7029 priv->max_port_rxqs = 8;
7030 else
7031 priv->max_port_rxqs = 32;
7032
Marcin Wojtas3f518502014-07-10 16:52:13 -03007033 priv->pp_clk = devm_clk_get(&pdev->dev, "pp_clk");
7034 if (IS_ERR(priv->pp_clk))
7035 return PTR_ERR(priv->pp_clk);
7036 err = clk_prepare_enable(priv->pp_clk);
7037 if (err < 0)
7038 return err;
7039
7040 priv->gop_clk = devm_clk_get(&pdev->dev, "gop_clk");
7041 if (IS_ERR(priv->gop_clk)) {
7042 err = PTR_ERR(priv->gop_clk);
7043 goto err_pp_clk;
7044 }
7045 err = clk_prepare_enable(priv->gop_clk);
7046 if (err < 0)
7047 goto err_pp_clk;
7048
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01007049 if (priv->hw_version == MVPP22) {
7050 priv->mg_clk = devm_clk_get(&pdev->dev, "mg_clk");
7051 if (IS_ERR(priv->mg_clk)) {
7052 err = PTR_ERR(priv->mg_clk);
7053 goto err_gop_clk;
7054 }
7055
7056 err = clk_prepare_enable(priv->mg_clk);
7057 if (err < 0)
7058 goto err_gop_clk;
7059 }
7060
Marcin Wojtas3f518502014-07-10 16:52:13 -03007061 /* Get system's tclk rate */
7062 priv->tclk = clk_get_rate(priv->pp_clk);
7063
Thomas Petazzoni2067e0a2017-03-07 16:53:19 +01007064 if (priv->hw_version == MVPP22) {
7065 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(40));
7066 if (err)
7067 goto err_mg_clk;
7068 /* Sadly, the BM pools all share the same register to
7069 * store the high 32 bits of their address. So they
7070 * must all have the same high 32 bits, which forces
7071 * us to restrict coherent memory to DMA_BIT_MASK(32).
7072 */
7073 err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
7074 if (err)
7075 goto err_mg_clk;
7076 }
7077
Marcin Wojtas3f518502014-07-10 16:52:13 -03007078 /* Initialize network controller */
7079 err = mvpp2_init(pdev, priv);
7080 if (err < 0) {
7081 dev_err(&pdev->dev, "failed to initialize controller\n");
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01007082 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007083 }
7084
7085 port_count = of_get_available_child_count(dn);
7086 if (port_count == 0) {
7087 dev_err(&pdev->dev, "no ports enabled\n");
Wei Yongjun575a1932014-07-20 22:02:43 +08007088 err = -ENODEV;
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01007089 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007090 }
7091
7092 priv->port_list = devm_kcalloc(&pdev->dev, port_count,
Markus Elfring0b92e592017-04-17 08:38:32 +02007093 sizeof(*priv->port_list),
7094 GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007095 if (!priv->port_list) {
7096 err = -ENOMEM;
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01007097 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007098 }
7099
7100 /* Initialize ports */
Marcin Wojtas3f518502014-07-10 16:52:13 -03007101 for_each_available_child_of_node(dn, port_node) {
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01007102 err = mvpp2_port_probe(pdev, port_node, priv);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007103 if (err < 0)
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01007104 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007105 }
7106
7107 platform_set_drvdata(pdev, priv);
7108 return 0;
7109
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01007110err_mg_clk:
7111 if (priv->hw_version == MVPP22)
7112 clk_disable_unprepare(priv->mg_clk);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007113err_gop_clk:
7114 clk_disable_unprepare(priv->gop_clk);
7115err_pp_clk:
7116 clk_disable_unprepare(priv->pp_clk);
7117 return err;
7118}
7119
7120static int mvpp2_remove(struct platform_device *pdev)
7121{
7122 struct mvpp2 *priv = platform_get_drvdata(pdev);
7123 struct device_node *dn = pdev->dev.of_node;
7124 struct device_node *port_node;
7125 int i = 0;
7126
7127 for_each_available_child_of_node(dn, port_node) {
7128 if (priv->port_list[i])
7129 mvpp2_port_remove(priv->port_list[i]);
7130 i++;
7131 }
7132
7133 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
7134 struct mvpp2_bm_pool *bm_pool = &priv->bm_pools[i];
7135
7136 mvpp2_bm_pool_destroy(pdev, priv, bm_pool);
7137 }
7138
7139 for_each_present_cpu(i) {
7140 struct mvpp2_tx_queue *aggr_txq = &priv->aggr_txqs[i];
7141
7142 dma_free_coherent(&pdev->dev,
7143 MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
7144 aggr_txq->descs,
Thomas Petazzoni20396132017-03-07 16:53:00 +01007145 aggr_txq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007146 }
7147
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01007148 clk_disable_unprepare(priv->mg_clk);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007149 clk_disable_unprepare(priv->pp_clk);
7150 clk_disable_unprepare(priv->gop_clk);
7151
7152 return 0;
7153}
7154
7155static const struct of_device_id mvpp2_match[] = {
Thomas Petazzonifaca9242017-03-07 16:53:06 +01007156 {
7157 .compatible = "marvell,armada-375-pp2",
7158 .data = (void *)MVPP21,
7159 },
Thomas Petazzonifc5e1552017-03-07 16:53:20 +01007160 {
7161 .compatible = "marvell,armada-7k-pp22",
7162 .data = (void *)MVPP22,
7163 },
Marcin Wojtas3f518502014-07-10 16:52:13 -03007164 { }
7165};
7166MODULE_DEVICE_TABLE(of, mvpp2_match);
7167
7168static struct platform_driver mvpp2_driver = {
7169 .probe = mvpp2_probe,
7170 .remove = mvpp2_remove,
7171 .driver = {
7172 .name = MVPP2_DRIVER_NAME,
7173 .of_match_table = mvpp2_match,
7174 },
7175};
7176
7177module_platform_driver(mvpp2_driver);
7178
7179MODULE_DESCRIPTION("Marvell PPv2 Ethernet Driver - www.marvell.com");
7180MODULE_AUTHOR("Marcin Wojtas <mw@semihalf.com>");
Ezequiel Garciac6340992014-07-14 10:34:47 -03007181MODULE_LICENSE("GPL v2");