blob: 21f47d2809959801c2055053279926e4a42965ab [file] [log] [blame]
Marcin Wojtas3f518502014-07-10 16:52:13 -03001/*
2 * Driver for Marvell PPv2 network controller for Armada 375 SoC.
3 *
4 * Copyright (C) 2014 Marvell
5 *
6 * Marcin Wojtas <mw@semihalf.com>
7 *
8 * This file is licensed under the terms of the GNU General Public
9 * License version 2. This program is licensed "as is" without any
10 * warranty of any kind, whether express or implied.
11 */
12
13#include <linux/kernel.h>
14#include <linux/netdevice.h>
15#include <linux/etherdevice.h>
16#include <linux/platform_device.h>
17#include <linux/skbuff.h>
18#include <linux/inetdevice.h>
19#include <linux/mbus.h>
20#include <linux/module.h>
21#include <linux/interrupt.h>
22#include <linux/cpumask.h>
23#include <linux/of.h>
24#include <linux/of_irq.h>
25#include <linux/of_mdio.h>
26#include <linux/of_net.h>
27#include <linux/of_address.h>
Thomas Petazzonifaca9242017-03-07 16:53:06 +010028#include <linux/of_device.h>
Marcin Wojtas3f518502014-07-10 16:52:13 -030029#include <linux/phy.h>
30#include <linux/clk.h>
Marcin Wojtasedc660f2015-08-06 19:00:30 +020031#include <linux/hrtimer.h>
32#include <linux/ktime.h>
Marcin Wojtas3f518502014-07-10 16:52:13 -030033#include <uapi/linux/ppp_defs.h>
34#include <net/ip.h>
35#include <net/ipv6.h>
36
37/* RX Fifo Registers */
38#define MVPP2_RX_DATA_FIFO_SIZE_REG(port) (0x00 + 4 * (port))
39#define MVPP2_RX_ATTR_FIFO_SIZE_REG(port) (0x20 + 4 * (port))
40#define MVPP2_RX_MIN_PKT_SIZE_REG 0x60
41#define MVPP2_RX_FIFO_INIT_REG 0x64
42
43/* RX DMA Top Registers */
44#define MVPP2_RX_CTRL_REG(port) (0x140 + 4 * (port))
45#define MVPP2_RX_LOW_LATENCY_PKT_SIZE(s) (((s) & 0xfff) << 16)
46#define MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK BIT(31)
47#define MVPP2_POOL_BUF_SIZE_REG(pool) (0x180 + 4 * (pool))
48#define MVPP2_POOL_BUF_SIZE_OFFSET 5
49#define MVPP2_RXQ_CONFIG_REG(rxq) (0x800 + 4 * (rxq))
50#define MVPP2_SNOOP_PKT_SIZE_MASK 0x1ff
51#define MVPP2_SNOOP_BUF_HDR_MASK BIT(9)
52#define MVPP2_RXQ_POOL_SHORT_OFFS 20
Thomas Petazzoni5eac8922017-03-07 16:53:10 +010053#define MVPP21_RXQ_POOL_SHORT_MASK 0x700000
54#define MVPP22_RXQ_POOL_SHORT_MASK 0xf00000
Marcin Wojtas3f518502014-07-10 16:52:13 -030055#define MVPP2_RXQ_POOL_LONG_OFFS 24
Thomas Petazzoni5eac8922017-03-07 16:53:10 +010056#define MVPP21_RXQ_POOL_LONG_MASK 0x7000000
57#define MVPP22_RXQ_POOL_LONG_MASK 0xf000000
Marcin Wojtas3f518502014-07-10 16:52:13 -030058#define MVPP2_RXQ_PACKET_OFFSET_OFFS 28
59#define MVPP2_RXQ_PACKET_OFFSET_MASK 0x70000000
60#define MVPP2_RXQ_DISABLE_MASK BIT(31)
61
62/* Parser Registers */
63#define MVPP2_PRS_INIT_LOOKUP_REG 0x1000
64#define MVPP2_PRS_PORT_LU_MAX 0xf
65#define MVPP2_PRS_PORT_LU_MASK(port) (0xff << ((port) * 4))
66#define MVPP2_PRS_PORT_LU_VAL(port, val) ((val) << ((port) * 4))
67#define MVPP2_PRS_INIT_OFFS_REG(port) (0x1004 + ((port) & 4))
68#define MVPP2_PRS_INIT_OFF_MASK(port) (0x3f << (((port) % 4) * 8))
69#define MVPP2_PRS_INIT_OFF_VAL(port, val) ((val) << (((port) % 4) * 8))
70#define MVPP2_PRS_MAX_LOOP_REG(port) (0x100c + ((port) & 4))
71#define MVPP2_PRS_MAX_LOOP_MASK(port) (0xff << (((port) % 4) * 8))
72#define MVPP2_PRS_MAX_LOOP_VAL(port, val) ((val) << (((port) % 4) * 8))
73#define MVPP2_PRS_TCAM_IDX_REG 0x1100
74#define MVPP2_PRS_TCAM_DATA_REG(idx) (0x1104 + (idx) * 4)
75#define MVPP2_PRS_TCAM_INV_MASK BIT(31)
76#define MVPP2_PRS_SRAM_IDX_REG 0x1200
77#define MVPP2_PRS_SRAM_DATA_REG(idx) (0x1204 + (idx) * 4)
78#define MVPP2_PRS_TCAM_CTRL_REG 0x1230
79#define MVPP2_PRS_TCAM_EN_MASK BIT(0)
80
81/* Classifier Registers */
82#define MVPP2_CLS_MODE_REG 0x1800
83#define MVPP2_CLS_MODE_ACTIVE_MASK BIT(0)
84#define MVPP2_CLS_PORT_WAY_REG 0x1810
85#define MVPP2_CLS_PORT_WAY_MASK(port) (1 << (port))
86#define MVPP2_CLS_LKP_INDEX_REG 0x1814
87#define MVPP2_CLS_LKP_INDEX_WAY_OFFS 6
88#define MVPP2_CLS_LKP_TBL_REG 0x1818
89#define MVPP2_CLS_LKP_TBL_RXQ_MASK 0xff
90#define MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK BIT(25)
91#define MVPP2_CLS_FLOW_INDEX_REG 0x1820
92#define MVPP2_CLS_FLOW_TBL0_REG 0x1824
93#define MVPP2_CLS_FLOW_TBL1_REG 0x1828
94#define MVPP2_CLS_FLOW_TBL2_REG 0x182c
95#define MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port) (0x1980 + ((port) * 4))
96#define MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS 3
97#define MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK 0x7
98#define MVPP2_CLS_SWFWD_P2HQ_REG(port) (0x19b0 + ((port) * 4))
99#define MVPP2_CLS_SWFWD_PCTRL_REG 0x19d0
100#define MVPP2_CLS_SWFWD_PCTRL_MASK(port) (1 << (port))
101
102/* Descriptor Manager Top Registers */
103#define MVPP2_RXQ_NUM_REG 0x2040
104#define MVPP2_RXQ_DESC_ADDR_REG 0x2044
Thomas Petazzonib02f31f2017-03-07 16:53:12 +0100105#define MVPP22_DESC_ADDR_OFFS 8
Marcin Wojtas3f518502014-07-10 16:52:13 -0300106#define MVPP2_RXQ_DESC_SIZE_REG 0x2048
107#define MVPP2_RXQ_DESC_SIZE_MASK 0x3ff0
108#define MVPP2_RXQ_STATUS_UPDATE_REG(rxq) (0x3000 + 4 * (rxq))
109#define MVPP2_RXQ_NUM_PROCESSED_OFFSET 0
110#define MVPP2_RXQ_NUM_NEW_OFFSET 16
111#define MVPP2_RXQ_STATUS_REG(rxq) (0x3400 + 4 * (rxq))
112#define MVPP2_RXQ_OCCUPIED_MASK 0x3fff
113#define MVPP2_RXQ_NON_OCCUPIED_OFFSET 16
114#define MVPP2_RXQ_NON_OCCUPIED_MASK 0x3fff0000
115#define MVPP2_RXQ_THRESH_REG 0x204c
116#define MVPP2_OCCUPIED_THRESH_OFFSET 0
117#define MVPP2_OCCUPIED_THRESH_MASK 0x3fff
118#define MVPP2_RXQ_INDEX_REG 0x2050
119#define MVPP2_TXQ_NUM_REG 0x2080
120#define MVPP2_TXQ_DESC_ADDR_REG 0x2084
121#define MVPP2_TXQ_DESC_SIZE_REG 0x2088
122#define MVPP2_TXQ_DESC_SIZE_MASK 0x3ff0
123#define MVPP2_AGGR_TXQ_UPDATE_REG 0x2090
Marcin Wojtas3f518502014-07-10 16:52:13 -0300124#define MVPP2_TXQ_INDEX_REG 0x2098
125#define MVPP2_TXQ_PREF_BUF_REG 0x209c
126#define MVPP2_PREF_BUF_PTR(desc) ((desc) & 0xfff)
127#define MVPP2_PREF_BUF_SIZE_4 (BIT(12) | BIT(13))
128#define MVPP2_PREF_BUF_SIZE_16 (BIT(12) | BIT(14))
129#define MVPP2_PREF_BUF_THRESH(val) ((val) << 17)
130#define MVPP2_TXQ_DRAIN_EN_MASK BIT(31)
131#define MVPP2_TXQ_PENDING_REG 0x20a0
132#define MVPP2_TXQ_PENDING_MASK 0x3fff
133#define MVPP2_TXQ_INT_STATUS_REG 0x20a4
134#define MVPP2_TXQ_SENT_REG(txq) (0x3c00 + 4 * (txq))
135#define MVPP2_TRANSMITTED_COUNT_OFFSET 16
136#define MVPP2_TRANSMITTED_COUNT_MASK 0x3fff0000
137#define MVPP2_TXQ_RSVD_REQ_REG 0x20b0
138#define MVPP2_TXQ_RSVD_REQ_Q_OFFSET 16
139#define MVPP2_TXQ_RSVD_RSLT_REG 0x20b4
140#define MVPP2_TXQ_RSVD_RSLT_MASK 0x3fff
141#define MVPP2_TXQ_RSVD_CLR_REG 0x20b8
142#define MVPP2_TXQ_RSVD_CLR_OFFSET 16
143#define MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu) (0x2100 + 4 * (cpu))
Thomas Petazzonib02f31f2017-03-07 16:53:12 +0100144#define MVPP22_AGGR_TXQ_DESC_ADDR_OFFS 8
Marcin Wojtas3f518502014-07-10 16:52:13 -0300145#define MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu) (0x2140 + 4 * (cpu))
146#define MVPP2_AGGR_TXQ_DESC_SIZE_MASK 0x3ff0
147#define MVPP2_AGGR_TXQ_STATUS_REG(cpu) (0x2180 + 4 * (cpu))
148#define MVPP2_AGGR_TXQ_PENDING_MASK 0x3fff
149#define MVPP2_AGGR_TXQ_INDEX_REG(cpu) (0x21c0 + 4 * (cpu))
150
151/* MBUS bridge registers */
152#define MVPP2_WIN_BASE(w) (0x4000 + ((w) << 2))
153#define MVPP2_WIN_SIZE(w) (0x4020 + ((w) << 2))
154#define MVPP2_WIN_REMAP(w) (0x4040 + ((w) << 2))
155#define MVPP2_BASE_ADDR_ENABLE 0x4060
156
Thomas Petazzoni6763ce32017-03-07 16:53:15 +0100157/* AXI Bridge Registers */
158#define MVPP22_AXI_BM_WR_ATTR_REG 0x4100
159#define MVPP22_AXI_BM_RD_ATTR_REG 0x4104
160#define MVPP22_AXI_AGGRQ_DESCR_RD_ATTR_REG 0x4110
161#define MVPP22_AXI_TXQ_DESCR_WR_ATTR_REG 0x4114
162#define MVPP22_AXI_TXQ_DESCR_RD_ATTR_REG 0x4118
163#define MVPP22_AXI_RXQ_DESCR_WR_ATTR_REG 0x411c
164#define MVPP22_AXI_RX_DATA_WR_ATTR_REG 0x4120
165#define MVPP22_AXI_TX_DATA_RD_ATTR_REG 0x4130
166#define MVPP22_AXI_RD_NORMAL_CODE_REG 0x4150
167#define MVPP22_AXI_RD_SNOOP_CODE_REG 0x4154
168#define MVPP22_AXI_WR_NORMAL_CODE_REG 0x4160
169#define MVPP22_AXI_WR_SNOOP_CODE_REG 0x4164
170
171/* Values for AXI Bridge registers */
172#define MVPP22_AXI_ATTR_CACHE_OFFS 0
173#define MVPP22_AXI_ATTR_DOMAIN_OFFS 12
174
175#define MVPP22_AXI_CODE_CACHE_OFFS 0
176#define MVPP22_AXI_CODE_DOMAIN_OFFS 4
177
178#define MVPP22_AXI_CODE_CACHE_NON_CACHE 0x3
179#define MVPP22_AXI_CODE_CACHE_WR_CACHE 0x7
180#define MVPP22_AXI_CODE_CACHE_RD_CACHE 0xb
181
182#define MVPP22_AXI_CODE_DOMAIN_OUTER_DOM 2
183#define MVPP22_AXI_CODE_DOMAIN_SYSTEM 3
184
Marcin Wojtas3f518502014-07-10 16:52:13 -0300185/* Interrupt Cause and Mask registers */
186#define MVPP2_ISR_RX_THRESHOLD_REG(rxq) (0x5200 + 4 * (rxq))
Thomas Petazzoniab426762017-02-21 11:28:04 +0100187#define MVPP2_MAX_ISR_RX_THRESHOLD 0xfffff0
Thomas Petazzonia73fef12017-03-07 16:53:16 +0100188#define MVPP21_ISR_RXQ_GROUP_REG(rxq) (0x5400 + 4 * (rxq))
189
190#define MVPP22_ISR_RXQ_GROUP_INDEX_REG 0x5400
191#define MVPP22_ISR_RXQ_GROUP_INDEX_SUBGROUP_MASK 0xf
192#define MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_MASK 0x380
193#define MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_OFFSET 7
194
195#define MVPP22_ISR_RXQ_GROUP_INDEX_SUBGROUP_MASK 0xf
196#define MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_MASK 0x380
197
198#define MVPP22_ISR_RXQ_SUB_GROUP_CONFIG_REG 0x5404
199#define MVPP22_ISR_RXQ_SUB_GROUP_STARTQ_MASK 0x1f
200#define MVPP22_ISR_RXQ_SUB_GROUP_SIZE_MASK 0xf00
201#define MVPP22_ISR_RXQ_SUB_GROUP_SIZE_OFFSET 8
202
Marcin Wojtas3f518502014-07-10 16:52:13 -0300203#define MVPP2_ISR_ENABLE_REG(port) (0x5420 + 4 * (port))
204#define MVPP2_ISR_ENABLE_INTERRUPT(mask) ((mask) & 0xffff)
205#define MVPP2_ISR_DISABLE_INTERRUPT(mask) (((mask) << 16) & 0xffff0000)
206#define MVPP2_ISR_RX_TX_CAUSE_REG(port) (0x5480 + 4 * (port))
207#define MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK 0xffff
208#define MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK 0xff0000
209#define MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK BIT(24)
210#define MVPP2_CAUSE_FCS_ERR_MASK BIT(25)
211#define MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK BIT(26)
212#define MVPP2_CAUSE_TX_EXCEPTION_SUM_MASK BIT(29)
213#define MVPP2_CAUSE_RX_EXCEPTION_SUM_MASK BIT(30)
214#define MVPP2_CAUSE_MISC_SUM_MASK BIT(31)
215#define MVPP2_ISR_RX_TX_MASK_REG(port) (0x54a0 + 4 * (port))
216#define MVPP2_ISR_PON_RX_TX_MASK_REG 0x54bc
217#define MVPP2_PON_CAUSE_RXQ_OCCUP_DESC_ALL_MASK 0xffff
218#define MVPP2_PON_CAUSE_TXP_OCCUP_DESC_ALL_MASK 0x3fc00000
219#define MVPP2_PON_CAUSE_MISC_SUM_MASK BIT(31)
220#define MVPP2_ISR_MISC_CAUSE_REG 0x55b0
221
222/* Buffer Manager registers */
223#define MVPP2_BM_POOL_BASE_REG(pool) (0x6000 + ((pool) * 4))
224#define MVPP2_BM_POOL_BASE_ADDR_MASK 0xfffff80
225#define MVPP2_BM_POOL_SIZE_REG(pool) (0x6040 + ((pool) * 4))
226#define MVPP2_BM_POOL_SIZE_MASK 0xfff0
227#define MVPP2_BM_POOL_READ_PTR_REG(pool) (0x6080 + ((pool) * 4))
228#define MVPP2_BM_POOL_GET_READ_PTR_MASK 0xfff0
229#define MVPP2_BM_POOL_PTRS_NUM_REG(pool) (0x60c0 + ((pool) * 4))
230#define MVPP2_BM_POOL_PTRS_NUM_MASK 0xfff0
231#define MVPP2_BM_BPPI_READ_PTR_REG(pool) (0x6100 + ((pool) * 4))
232#define MVPP2_BM_BPPI_PTRS_NUM_REG(pool) (0x6140 + ((pool) * 4))
233#define MVPP2_BM_BPPI_PTR_NUM_MASK 0x7ff
234#define MVPP2_BM_BPPI_PREFETCH_FULL_MASK BIT(16)
235#define MVPP2_BM_POOL_CTRL_REG(pool) (0x6200 + ((pool) * 4))
236#define MVPP2_BM_START_MASK BIT(0)
237#define MVPP2_BM_STOP_MASK BIT(1)
238#define MVPP2_BM_STATE_MASK BIT(4)
239#define MVPP2_BM_LOW_THRESH_OFFS 8
240#define MVPP2_BM_LOW_THRESH_MASK 0x7f00
241#define MVPP2_BM_LOW_THRESH_VALUE(val) ((val) << \
242 MVPP2_BM_LOW_THRESH_OFFS)
243#define MVPP2_BM_HIGH_THRESH_OFFS 16
244#define MVPP2_BM_HIGH_THRESH_MASK 0x7f0000
245#define MVPP2_BM_HIGH_THRESH_VALUE(val) ((val) << \
246 MVPP2_BM_HIGH_THRESH_OFFS)
247#define MVPP2_BM_INTR_CAUSE_REG(pool) (0x6240 + ((pool) * 4))
248#define MVPP2_BM_RELEASED_DELAY_MASK BIT(0)
249#define MVPP2_BM_ALLOC_FAILED_MASK BIT(1)
250#define MVPP2_BM_BPPE_EMPTY_MASK BIT(2)
251#define MVPP2_BM_BPPE_FULL_MASK BIT(3)
252#define MVPP2_BM_AVAILABLE_BP_LOW_MASK BIT(4)
253#define MVPP2_BM_INTR_MASK_REG(pool) (0x6280 + ((pool) * 4))
254#define MVPP2_BM_PHY_ALLOC_REG(pool) (0x6400 + ((pool) * 4))
255#define MVPP2_BM_PHY_ALLOC_GRNTD_MASK BIT(0)
256#define MVPP2_BM_VIRT_ALLOC_REG 0x6440
Thomas Petazzonid01524d2017-03-07 16:53:09 +0100257#define MVPP22_BM_ADDR_HIGH_ALLOC 0x6444
258#define MVPP22_BM_ADDR_HIGH_PHYS_MASK 0xff
259#define MVPP22_BM_ADDR_HIGH_VIRT_MASK 0xff00
260#define MVPP22_BM_ADDR_HIGH_VIRT_SHIFT 8
Marcin Wojtas3f518502014-07-10 16:52:13 -0300261#define MVPP2_BM_PHY_RLS_REG(pool) (0x6480 + ((pool) * 4))
262#define MVPP2_BM_PHY_RLS_MC_BUFF_MASK BIT(0)
263#define MVPP2_BM_PHY_RLS_PRIO_EN_MASK BIT(1)
264#define MVPP2_BM_PHY_RLS_GRNTD_MASK BIT(2)
265#define MVPP2_BM_VIRT_RLS_REG 0x64c0
Thomas Petazzonid01524d2017-03-07 16:53:09 +0100266#define MVPP22_BM_ADDR_HIGH_RLS_REG 0x64c4
267#define MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK 0xff
268#define MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK 0xff00
269#define MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT 8
Marcin Wojtas3f518502014-07-10 16:52:13 -0300270
271/* TX Scheduler registers */
272#define MVPP2_TXP_SCHED_PORT_INDEX_REG 0x8000
273#define MVPP2_TXP_SCHED_Q_CMD_REG 0x8004
274#define MVPP2_TXP_SCHED_ENQ_MASK 0xff
275#define MVPP2_TXP_SCHED_DISQ_OFFSET 8
276#define MVPP2_TXP_SCHED_CMD_1_REG 0x8010
277#define MVPP2_TXP_SCHED_PERIOD_REG 0x8018
278#define MVPP2_TXP_SCHED_MTU_REG 0x801c
279#define MVPP2_TXP_MTU_MAX 0x7FFFF
280#define MVPP2_TXP_SCHED_REFILL_REG 0x8020
281#define MVPP2_TXP_REFILL_TOKENS_ALL_MASK 0x7ffff
282#define MVPP2_TXP_REFILL_PERIOD_ALL_MASK 0x3ff00000
283#define MVPP2_TXP_REFILL_PERIOD_MASK(v) ((v) << 20)
284#define MVPP2_TXP_SCHED_TOKEN_SIZE_REG 0x8024
285#define MVPP2_TXP_TOKEN_SIZE_MAX 0xffffffff
286#define MVPP2_TXQ_SCHED_REFILL_REG(q) (0x8040 + ((q) << 2))
287#define MVPP2_TXQ_REFILL_TOKENS_ALL_MASK 0x7ffff
288#define MVPP2_TXQ_REFILL_PERIOD_ALL_MASK 0x3ff00000
289#define MVPP2_TXQ_REFILL_PERIOD_MASK(v) ((v) << 20)
290#define MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(q) (0x8060 + ((q) << 2))
291#define MVPP2_TXQ_TOKEN_SIZE_MAX 0x7fffffff
292#define MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(q) (0x8080 + ((q) << 2))
293#define MVPP2_TXQ_TOKEN_CNTR_MAX 0xffffffff
294
295/* TX general registers */
296#define MVPP2_TX_SNOOP_REG 0x8800
297#define MVPP2_TX_PORT_FLUSH_REG 0x8810
298#define MVPP2_TX_PORT_FLUSH_MASK(port) (1 << (port))
299
300/* LMS registers */
301#define MVPP2_SRC_ADDR_MIDDLE 0x24
302#define MVPP2_SRC_ADDR_HIGH 0x28
Marcin Wojtas08a23752014-07-21 13:48:12 -0300303#define MVPP2_PHY_AN_CFG0_REG 0x34
304#define MVPP2_PHY_AN_STOP_SMI0_MASK BIT(7)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300305#define MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG 0x305c
Thomas Petazzoni31d76772017-02-21 11:28:10 +0100306#define MVPP2_EXT_GLOBAL_CTRL_DEFAULT 0x27
Marcin Wojtas3f518502014-07-10 16:52:13 -0300307
308/* Per-port registers */
309#define MVPP2_GMAC_CTRL_0_REG 0x0
310#define MVPP2_GMAC_PORT_EN_MASK BIT(0)
311#define MVPP2_GMAC_MAX_RX_SIZE_OFFS 2
312#define MVPP2_GMAC_MAX_RX_SIZE_MASK 0x7ffc
313#define MVPP2_GMAC_MIB_CNTR_EN_MASK BIT(15)
314#define MVPP2_GMAC_CTRL_1_REG 0x4
Marcin Wojtasb5c0a802014-07-21 13:48:11 -0300315#define MVPP2_GMAC_PERIODIC_XON_EN_MASK BIT(1)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300316#define MVPP2_GMAC_GMII_LB_EN_MASK BIT(5)
317#define MVPP2_GMAC_PCS_LB_EN_BIT 6
318#define MVPP2_GMAC_PCS_LB_EN_MASK BIT(6)
319#define MVPP2_GMAC_SA_LOW_OFFS 7
320#define MVPP2_GMAC_CTRL_2_REG 0x8
321#define MVPP2_GMAC_INBAND_AN_MASK BIT(0)
322#define MVPP2_GMAC_PCS_ENABLE_MASK BIT(3)
323#define MVPP2_GMAC_PORT_RGMII_MASK BIT(4)
324#define MVPP2_GMAC_PORT_RESET_MASK BIT(6)
325#define MVPP2_GMAC_AUTONEG_CONFIG 0xc
326#define MVPP2_GMAC_FORCE_LINK_DOWN BIT(0)
327#define MVPP2_GMAC_FORCE_LINK_PASS BIT(1)
328#define MVPP2_GMAC_CONFIG_MII_SPEED BIT(5)
329#define MVPP2_GMAC_CONFIG_GMII_SPEED BIT(6)
330#define MVPP2_GMAC_AN_SPEED_EN BIT(7)
Marcin Wojtas08a23752014-07-21 13:48:12 -0300331#define MVPP2_GMAC_FC_ADV_EN BIT(9)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300332#define MVPP2_GMAC_CONFIG_FULL_DUPLEX BIT(12)
333#define MVPP2_GMAC_AN_DUPLEX_EN BIT(13)
334#define MVPP2_GMAC_PORT_FIFO_CFG_1_REG 0x1c
335#define MVPP2_GMAC_TX_FIFO_MIN_TH_OFFS 6
336#define MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK 0x1fc0
337#define MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(v) (((v) << 6) & \
338 MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK)
Thomas Petazzoni26975822017-03-07 16:53:14 +0100339#define MVPP22_GMAC_CTRL_4_REG 0x90
340#define MVPP22_CTRL4_EXT_PIN_GMII_SEL BIT(0)
341#define MVPP22_CTRL4_DP_CLK_SEL BIT(5)
342#define MVPP22_CTRL4_SYNC_BYPASS BIT(6)
343#define MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE BIT(7)
344
345/* Per-port XGMAC registers. PPv2.2 only, only for GOP port 0,
346 * relative to port->base.
347 */
348#define MVPP22_XLG_CTRL3_REG 0x11c
349#define MVPP22_XLG_CTRL3_MACMODESELECT_MASK (7 << 13)
350#define MVPP22_XLG_CTRL3_MACMODESELECT_GMAC (0 << 13)
351
352/* SMI registers. PPv2.2 only, relative to priv->iface_base. */
353#define MVPP22_SMI_MISC_CFG_REG 0x1204
354#define MVPP22_SMI_POLLING_EN BIT(10)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300355
Thomas Petazzonia7868412017-03-07 16:53:13 +0100356#define MVPP22_GMAC_BASE(port) (0x7000 + (port) * 0x1000 + 0xe00)
357
Marcin Wojtas3f518502014-07-10 16:52:13 -0300358#define MVPP2_CAUSE_TXQ_SENT_DESC_ALL_MASK 0xff
359
360/* Descriptor ring Macros */
361#define MVPP2_QUEUE_NEXT_DESC(q, index) \
362 (((index) < (q)->last_desc) ? ((index) + 1) : 0)
363
364/* Various constants */
365
366/* Coalescing */
367#define MVPP2_TXDONE_COAL_PKTS_THRESH 15
Marcin Wojtasedc660f2015-08-06 19:00:30 +0200368#define MVPP2_TXDONE_HRTIMER_PERIOD_NS 1000000UL
Marcin Wojtas3f518502014-07-10 16:52:13 -0300369#define MVPP2_RX_COAL_PKTS 32
370#define MVPP2_RX_COAL_USEC 100
371
372/* The two bytes Marvell header. Either contains a special value used
373 * by Marvell switches when a specific hardware mode is enabled (not
374 * supported by this driver) or is filled automatically by zeroes on
375 * the RX side. Those two bytes being at the front of the Ethernet
376 * header, they allow to have the IP header aligned on a 4 bytes
377 * boundary automatically: the hardware skips those two bytes on its
378 * own.
379 */
380#define MVPP2_MH_SIZE 2
381#define MVPP2_ETH_TYPE_LEN 2
382#define MVPP2_PPPOE_HDR_SIZE 8
383#define MVPP2_VLAN_TAG_LEN 4
384
385/* Lbtd 802.3 type */
386#define MVPP2_IP_LBDT_TYPE 0xfffa
387
Marcin Wojtas3f518502014-07-10 16:52:13 -0300388#define MVPP2_TX_CSUM_MAX_SIZE 9800
389
390/* Timeout constants */
391#define MVPP2_TX_DISABLE_TIMEOUT_MSEC 1000
392#define MVPP2_TX_PENDING_TIMEOUT_MSEC 1000
393
394#define MVPP2_TX_MTU_MAX 0x7ffff
395
396/* Maximum number of T-CONTs of PON port */
397#define MVPP2_MAX_TCONT 16
398
399/* Maximum number of supported ports */
400#define MVPP2_MAX_PORTS 4
401
402/* Maximum number of TXQs used by single port */
403#define MVPP2_MAX_TXQ 8
404
405/* Maximum number of RXQs used by single port */
406#define MVPP2_MAX_RXQ 8
407
408/* Dfault number of RXQs in use */
409#define MVPP2_DEFAULT_RXQ 4
410
411/* Total number of RXQs available to all ports */
412#define MVPP2_RXQ_TOTAL_NUM (MVPP2_MAX_PORTS * MVPP2_MAX_RXQ)
413
414/* Max number of Rx descriptors */
415#define MVPP2_MAX_RXD 128
416
417/* Max number of Tx descriptors */
418#define MVPP2_MAX_TXD 1024
419
420/* Amount of Tx descriptors that can be reserved at once by CPU */
421#define MVPP2_CPU_DESC_CHUNK 64
422
423/* Max number of Tx descriptors in each aggregated queue */
424#define MVPP2_AGGR_TXQ_SIZE 256
425
426/* Descriptor aligned size */
427#define MVPP2_DESC_ALIGNED_SIZE 32
428
429/* Descriptor alignment mask */
430#define MVPP2_TX_DESC_ALIGN (MVPP2_DESC_ALIGNED_SIZE - 1)
431
432/* RX FIFO constants */
433#define MVPP2_RX_FIFO_PORT_DATA_SIZE 0x2000
434#define MVPP2_RX_FIFO_PORT_ATTR_SIZE 0x80
435#define MVPP2_RX_FIFO_PORT_MIN_PKT 0x80
436
437/* RX buffer constants */
438#define MVPP2_SKB_SHINFO_SIZE \
439 SKB_DATA_ALIGN(sizeof(struct skb_shared_info))
440
441#define MVPP2_RX_PKT_SIZE(mtu) \
442 ALIGN((mtu) + MVPP2_MH_SIZE + MVPP2_VLAN_TAG_LEN + \
Jisheng Zhang4a0a12d2016-04-01 17:11:05 +0800443 ETH_HLEN + ETH_FCS_LEN, cache_line_size())
Marcin Wojtas3f518502014-07-10 16:52:13 -0300444
445#define MVPP2_RX_BUF_SIZE(pkt_size) ((pkt_size) + NET_SKB_PAD)
446#define MVPP2_RX_TOTAL_SIZE(buf_size) ((buf_size) + MVPP2_SKB_SHINFO_SIZE)
447#define MVPP2_RX_MAX_PKT_SIZE(total_size) \
448 ((total_size) - NET_SKB_PAD - MVPP2_SKB_SHINFO_SIZE)
449
450#define MVPP2_BIT_TO_BYTE(bit) ((bit) / 8)
451
452/* IPv6 max L3 address size */
453#define MVPP2_MAX_L3_ADDR_SIZE 16
454
455/* Port flags */
456#define MVPP2_F_LOOPBACK BIT(0)
457
458/* Marvell tag types */
459enum mvpp2_tag_type {
460 MVPP2_TAG_TYPE_NONE = 0,
461 MVPP2_TAG_TYPE_MH = 1,
462 MVPP2_TAG_TYPE_DSA = 2,
463 MVPP2_TAG_TYPE_EDSA = 3,
464 MVPP2_TAG_TYPE_VLAN = 4,
465 MVPP2_TAG_TYPE_LAST = 5
466};
467
468/* Parser constants */
469#define MVPP2_PRS_TCAM_SRAM_SIZE 256
470#define MVPP2_PRS_TCAM_WORDS 6
471#define MVPP2_PRS_SRAM_WORDS 4
472#define MVPP2_PRS_FLOW_ID_SIZE 64
473#define MVPP2_PRS_FLOW_ID_MASK 0x3f
474#define MVPP2_PRS_TCAM_ENTRY_INVALID 1
475#define MVPP2_PRS_TCAM_DSA_TAGGED_BIT BIT(5)
476#define MVPP2_PRS_IPV4_HEAD 0x40
477#define MVPP2_PRS_IPV4_HEAD_MASK 0xf0
478#define MVPP2_PRS_IPV4_MC 0xe0
479#define MVPP2_PRS_IPV4_MC_MASK 0xf0
480#define MVPP2_PRS_IPV4_BC_MASK 0xff
481#define MVPP2_PRS_IPV4_IHL 0x5
482#define MVPP2_PRS_IPV4_IHL_MASK 0xf
483#define MVPP2_PRS_IPV6_MC 0xff
484#define MVPP2_PRS_IPV6_MC_MASK 0xff
485#define MVPP2_PRS_IPV6_HOP_MASK 0xff
486#define MVPP2_PRS_TCAM_PROTO_MASK 0xff
487#define MVPP2_PRS_TCAM_PROTO_MASK_L 0x3f
488#define MVPP2_PRS_DBL_VLANS_MAX 100
489
490/* Tcam structure:
491 * - lookup ID - 4 bits
492 * - port ID - 1 byte
493 * - additional information - 1 byte
494 * - header data - 8 bytes
495 * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(5)->(0).
496 */
497#define MVPP2_PRS_AI_BITS 8
498#define MVPP2_PRS_PORT_MASK 0xff
499#define MVPP2_PRS_LU_MASK 0xf
500#define MVPP2_PRS_TCAM_DATA_BYTE(offs) \
501 (((offs) - ((offs) % 2)) * 2 + ((offs) % 2))
502#define MVPP2_PRS_TCAM_DATA_BYTE_EN(offs) \
503 (((offs) * 2) - ((offs) % 2) + 2)
504#define MVPP2_PRS_TCAM_AI_BYTE 16
505#define MVPP2_PRS_TCAM_PORT_BYTE 17
506#define MVPP2_PRS_TCAM_LU_BYTE 20
507#define MVPP2_PRS_TCAM_EN_OFFS(offs) ((offs) + 2)
508#define MVPP2_PRS_TCAM_INV_WORD 5
509/* Tcam entries ID */
510#define MVPP2_PE_DROP_ALL 0
511#define MVPP2_PE_FIRST_FREE_TID 1
512#define MVPP2_PE_LAST_FREE_TID (MVPP2_PRS_TCAM_SRAM_SIZE - 31)
513#define MVPP2_PE_IP6_EXT_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 30)
514#define MVPP2_PE_MAC_MC_IP6 (MVPP2_PRS_TCAM_SRAM_SIZE - 29)
515#define MVPP2_PE_IP6_ADDR_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 28)
516#define MVPP2_PE_IP4_ADDR_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 27)
517#define MVPP2_PE_LAST_DEFAULT_FLOW (MVPP2_PRS_TCAM_SRAM_SIZE - 26)
518#define MVPP2_PE_FIRST_DEFAULT_FLOW (MVPP2_PRS_TCAM_SRAM_SIZE - 19)
519#define MVPP2_PE_EDSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 18)
520#define MVPP2_PE_EDSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 17)
521#define MVPP2_PE_DSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 16)
522#define MVPP2_PE_DSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 15)
523#define MVPP2_PE_ETYPE_EDSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 14)
524#define MVPP2_PE_ETYPE_EDSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 13)
525#define MVPP2_PE_ETYPE_DSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 12)
526#define MVPP2_PE_ETYPE_DSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 11)
527#define MVPP2_PE_MH_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 10)
528#define MVPP2_PE_DSA_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 9)
529#define MVPP2_PE_IP6_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 8)
530#define MVPP2_PE_IP4_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 7)
531#define MVPP2_PE_ETH_TYPE_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 6)
532#define MVPP2_PE_VLAN_DBL (MVPP2_PRS_TCAM_SRAM_SIZE - 5)
533#define MVPP2_PE_VLAN_NONE (MVPP2_PRS_TCAM_SRAM_SIZE - 4)
534#define MVPP2_PE_MAC_MC_ALL (MVPP2_PRS_TCAM_SRAM_SIZE - 3)
535#define MVPP2_PE_MAC_PROMISCUOUS (MVPP2_PRS_TCAM_SRAM_SIZE - 2)
536#define MVPP2_PE_MAC_NON_PROMISCUOUS (MVPP2_PRS_TCAM_SRAM_SIZE - 1)
537
538/* Sram structure
539 * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(3)->(0).
540 */
541#define MVPP2_PRS_SRAM_RI_OFFS 0
542#define MVPP2_PRS_SRAM_RI_WORD 0
543#define MVPP2_PRS_SRAM_RI_CTRL_OFFS 32
544#define MVPP2_PRS_SRAM_RI_CTRL_WORD 1
545#define MVPP2_PRS_SRAM_RI_CTRL_BITS 32
546#define MVPP2_PRS_SRAM_SHIFT_OFFS 64
547#define MVPP2_PRS_SRAM_SHIFT_SIGN_BIT 72
548#define MVPP2_PRS_SRAM_UDF_OFFS 73
549#define MVPP2_PRS_SRAM_UDF_BITS 8
550#define MVPP2_PRS_SRAM_UDF_MASK 0xff
551#define MVPP2_PRS_SRAM_UDF_SIGN_BIT 81
552#define MVPP2_PRS_SRAM_UDF_TYPE_OFFS 82
553#define MVPP2_PRS_SRAM_UDF_TYPE_MASK 0x7
554#define MVPP2_PRS_SRAM_UDF_TYPE_L3 1
555#define MVPP2_PRS_SRAM_UDF_TYPE_L4 4
556#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS 85
557#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK 0x3
558#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD 1
559#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP4_ADD 2
560#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP6_ADD 3
561#define MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS 87
562#define MVPP2_PRS_SRAM_OP_SEL_UDF_BITS 2
563#define MVPP2_PRS_SRAM_OP_SEL_UDF_MASK 0x3
564#define MVPP2_PRS_SRAM_OP_SEL_UDF_ADD 0
565#define MVPP2_PRS_SRAM_OP_SEL_UDF_IP4_ADD 2
566#define MVPP2_PRS_SRAM_OP_SEL_UDF_IP6_ADD 3
567#define MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS 89
568#define MVPP2_PRS_SRAM_AI_OFFS 90
569#define MVPP2_PRS_SRAM_AI_CTRL_OFFS 98
570#define MVPP2_PRS_SRAM_AI_CTRL_BITS 8
571#define MVPP2_PRS_SRAM_AI_MASK 0xff
572#define MVPP2_PRS_SRAM_NEXT_LU_OFFS 106
573#define MVPP2_PRS_SRAM_NEXT_LU_MASK 0xf
574#define MVPP2_PRS_SRAM_LU_DONE_BIT 110
575#define MVPP2_PRS_SRAM_LU_GEN_BIT 111
576
577/* Sram result info bits assignment */
578#define MVPP2_PRS_RI_MAC_ME_MASK 0x1
579#define MVPP2_PRS_RI_DSA_MASK 0x2
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100580#define MVPP2_PRS_RI_VLAN_MASK (BIT(2) | BIT(3))
581#define MVPP2_PRS_RI_VLAN_NONE 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300582#define MVPP2_PRS_RI_VLAN_SINGLE BIT(2)
583#define MVPP2_PRS_RI_VLAN_DOUBLE BIT(3)
584#define MVPP2_PRS_RI_VLAN_TRIPLE (BIT(2) | BIT(3))
585#define MVPP2_PRS_RI_CPU_CODE_MASK 0x70
586#define MVPP2_PRS_RI_CPU_CODE_RX_SPEC BIT(4)
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100587#define MVPP2_PRS_RI_L2_CAST_MASK (BIT(9) | BIT(10))
588#define MVPP2_PRS_RI_L2_UCAST 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300589#define MVPP2_PRS_RI_L2_MCAST BIT(9)
590#define MVPP2_PRS_RI_L2_BCAST BIT(10)
591#define MVPP2_PRS_RI_PPPOE_MASK 0x800
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100592#define MVPP2_PRS_RI_L3_PROTO_MASK (BIT(12) | BIT(13) | BIT(14))
593#define MVPP2_PRS_RI_L3_UN 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300594#define MVPP2_PRS_RI_L3_IP4 BIT(12)
595#define MVPP2_PRS_RI_L3_IP4_OPT BIT(13)
596#define MVPP2_PRS_RI_L3_IP4_OTHER (BIT(12) | BIT(13))
597#define MVPP2_PRS_RI_L3_IP6 BIT(14)
598#define MVPP2_PRS_RI_L3_IP6_EXT (BIT(12) | BIT(14))
599#define MVPP2_PRS_RI_L3_ARP (BIT(13) | BIT(14))
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100600#define MVPP2_PRS_RI_L3_ADDR_MASK (BIT(15) | BIT(16))
601#define MVPP2_PRS_RI_L3_UCAST 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300602#define MVPP2_PRS_RI_L3_MCAST BIT(15)
603#define MVPP2_PRS_RI_L3_BCAST (BIT(15) | BIT(16))
604#define MVPP2_PRS_RI_IP_FRAG_MASK 0x20000
605#define MVPP2_PRS_RI_UDF3_MASK 0x300000
606#define MVPP2_PRS_RI_UDF3_RX_SPECIAL BIT(21)
607#define MVPP2_PRS_RI_L4_PROTO_MASK 0x1c00000
608#define MVPP2_PRS_RI_L4_TCP BIT(22)
609#define MVPP2_PRS_RI_L4_UDP BIT(23)
610#define MVPP2_PRS_RI_L4_OTHER (BIT(22) | BIT(23))
611#define MVPP2_PRS_RI_UDF7_MASK 0x60000000
612#define MVPP2_PRS_RI_UDF7_IP6_LITE BIT(29)
613#define MVPP2_PRS_RI_DROP_MASK 0x80000000
614
615/* Sram additional info bits assignment */
616#define MVPP2_PRS_IPV4_DIP_AI_BIT BIT(0)
617#define MVPP2_PRS_IPV6_NO_EXT_AI_BIT BIT(0)
618#define MVPP2_PRS_IPV6_EXT_AI_BIT BIT(1)
619#define MVPP2_PRS_IPV6_EXT_AH_AI_BIT BIT(2)
620#define MVPP2_PRS_IPV6_EXT_AH_LEN_AI_BIT BIT(3)
621#define MVPP2_PRS_IPV6_EXT_AH_L4_AI_BIT BIT(4)
622#define MVPP2_PRS_SINGLE_VLAN_AI 0
623#define MVPP2_PRS_DBL_VLAN_AI_BIT BIT(7)
624
625/* DSA/EDSA type */
626#define MVPP2_PRS_TAGGED true
627#define MVPP2_PRS_UNTAGGED false
628#define MVPP2_PRS_EDSA true
629#define MVPP2_PRS_DSA false
630
631/* MAC entries, shadow udf */
632enum mvpp2_prs_udf {
633 MVPP2_PRS_UDF_MAC_DEF,
634 MVPP2_PRS_UDF_MAC_RANGE,
635 MVPP2_PRS_UDF_L2_DEF,
636 MVPP2_PRS_UDF_L2_DEF_COPY,
637 MVPP2_PRS_UDF_L2_USER,
638};
639
640/* Lookup ID */
641enum mvpp2_prs_lookup {
642 MVPP2_PRS_LU_MH,
643 MVPP2_PRS_LU_MAC,
644 MVPP2_PRS_LU_DSA,
645 MVPP2_PRS_LU_VLAN,
646 MVPP2_PRS_LU_L2,
647 MVPP2_PRS_LU_PPPOE,
648 MVPP2_PRS_LU_IP4,
649 MVPP2_PRS_LU_IP6,
650 MVPP2_PRS_LU_FLOWS,
651 MVPP2_PRS_LU_LAST,
652};
653
654/* L3 cast enum */
655enum mvpp2_prs_l3_cast {
656 MVPP2_PRS_L3_UNI_CAST,
657 MVPP2_PRS_L3_MULTI_CAST,
658 MVPP2_PRS_L3_BROAD_CAST
659};
660
661/* Classifier constants */
662#define MVPP2_CLS_FLOWS_TBL_SIZE 512
663#define MVPP2_CLS_FLOWS_TBL_DATA_WORDS 3
664#define MVPP2_CLS_LKP_TBL_SIZE 64
665
666/* BM constants */
667#define MVPP2_BM_POOLS_NUM 8
668#define MVPP2_BM_LONG_BUF_NUM 1024
669#define MVPP2_BM_SHORT_BUF_NUM 2048
670#define MVPP2_BM_POOL_SIZE_MAX (16*1024 - MVPP2_BM_POOL_PTR_ALIGN/4)
671#define MVPP2_BM_POOL_PTR_ALIGN 128
672#define MVPP2_BM_SWF_LONG_POOL(port) ((port > 2) ? 2 : port)
673#define MVPP2_BM_SWF_SHORT_POOL 3
674
675/* BM cookie (32 bits) definition */
676#define MVPP2_BM_COOKIE_POOL_OFFS 8
677#define MVPP2_BM_COOKIE_CPU_OFFS 24
678
679/* BM short pool packet size
680 * These value assure that for SWF the total number
681 * of bytes allocated for each buffer will be 512
682 */
683#define MVPP2_BM_SHORT_PKT_SIZE MVPP2_RX_MAX_PKT_SIZE(512)
684
Thomas Petazzonia7868412017-03-07 16:53:13 +0100685#define MVPP21_ADDR_SPACE_SZ 0
686#define MVPP22_ADDR_SPACE_SZ SZ_64K
687
688#define MVPP2_MAX_CPUS 4
689
Marcin Wojtas3f518502014-07-10 16:52:13 -0300690enum mvpp2_bm_type {
691 MVPP2_BM_FREE,
692 MVPP2_BM_SWF_LONG,
693 MVPP2_BM_SWF_SHORT
694};
695
696/* Definitions */
697
698/* Shared Packet Processor resources */
699struct mvpp2 {
700 /* Shared registers' base addresses */
Marcin Wojtas3f518502014-07-10 16:52:13 -0300701 void __iomem *lms_base;
Thomas Petazzonia7868412017-03-07 16:53:13 +0100702 void __iomem *iface_base;
703
704 /* On PPv2.2, each CPU can access the base register through a
705 * separate address space, each 64 KB apart from each
706 * other.
707 */
708 void __iomem *cpu_base[MVPP2_MAX_CPUS];
Marcin Wojtas3f518502014-07-10 16:52:13 -0300709
710 /* Common clocks */
711 struct clk *pp_clk;
712 struct clk *gop_clk;
713
714 /* List of pointers to port structures */
715 struct mvpp2_port **port_list;
716
717 /* Aggregated TXQs */
718 struct mvpp2_tx_queue *aggr_txqs;
719
720 /* BM pools */
721 struct mvpp2_bm_pool *bm_pools;
722
723 /* PRS shadow table */
724 struct mvpp2_prs_shadow *prs_shadow;
725 /* PRS auxiliary table for double vlan entries control */
726 bool *prs_double_vlans;
727
728 /* Tclk value */
729 u32 tclk;
Thomas Petazzonifaca9242017-03-07 16:53:06 +0100730
731 /* HW version */
732 enum { MVPP21, MVPP22 } hw_version;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300733};
734
735struct mvpp2_pcpu_stats {
736 struct u64_stats_sync syncp;
737 u64 rx_packets;
738 u64 rx_bytes;
739 u64 tx_packets;
740 u64 tx_bytes;
741};
742
Marcin Wojtasedc660f2015-08-06 19:00:30 +0200743/* Per-CPU port control */
744struct mvpp2_port_pcpu {
745 struct hrtimer tx_done_timer;
746 bool timer_scheduled;
747 /* Tasklet for egress finalization */
748 struct tasklet_struct tx_done_tasklet;
749};
750
Marcin Wojtas3f518502014-07-10 16:52:13 -0300751struct mvpp2_port {
752 u8 id;
753
Thomas Petazzonia7868412017-03-07 16:53:13 +0100754 /* Index of the port from the "group of ports" complex point
755 * of view
756 */
757 int gop_id;
758
Marcin Wojtas3f518502014-07-10 16:52:13 -0300759 int irq;
760
761 struct mvpp2 *priv;
762
763 /* Per-port registers' base address */
764 void __iomem *base;
765
766 struct mvpp2_rx_queue **rxqs;
767 struct mvpp2_tx_queue **txqs;
768 struct net_device *dev;
769
770 int pkt_size;
771
772 u32 pending_cause_rx;
773 struct napi_struct napi;
774
Marcin Wojtasedc660f2015-08-06 19:00:30 +0200775 /* Per-CPU port control */
776 struct mvpp2_port_pcpu __percpu *pcpu;
777
Marcin Wojtas3f518502014-07-10 16:52:13 -0300778 /* Flags */
779 unsigned long flags;
780
781 u16 tx_ring_size;
782 u16 rx_ring_size;
783 struct mvpp2_pcpu_stats __percpu *stats;
784
Marcin Wojtas3f518502014-07-10 16:52:13 -0300785 phy_interface_t phy_interface;
786 struct device_node *phy_node;
787 unsigned int link;
788 unsigned int duplex;
789 unsigned int speed;
790
791 struct mvpp2_bm_pool *pool_long;
792 struct mvpp2_bm_pool *pool_short;
793
794 /* Index of first port's physical RXQ */
795 u8 first_rxq;
796};
797
798/* The mvpp2_tx_desc and mvpp2_rx_desc structures describe the
799 * layout of the transmit and reception DMA descriptors, and their
800 * layout is therefore defined by the hardware design
801 */
802
803#define MVPP2_TXD_L3_OFF_SHIFT 0
804#define MVPP2_TXD_IP_HLEN_SHIFT 8
805#define MVPP2_TXD_L4_CSUM_FRAG BIT(13)
806#define MVPP2_TXD_L4_CSUM_NOT BIT(14)
807#define MVPP2_TXD_IP_CSUM_DISABLE BIT(15)
808#define MVPP2_TXD_PADDING_DISABLE BIT(23)
809#define MVPP2_TXD_L4_UDP BIT(24)
810#define MVPP2_TXD_L3_IP6 BIT(26)
811#define MVPP2_TXD_L_DESC BIT(28)
812#define MVPP2_TXD_F_DESC BIT(29)
813
814#define MVPP2_RXD_ERR_SUMMARY BIT(15)
815#define MVPP2_RXD_ERR_CODE_MASK (BIT(13) | BIT(14))
816#define MVPP2_RXD_ERR_CRC 0x0
817#define MVPP2_RXD_ERR_OVERRUN BIT(13)
818#define MVPP2_RXD_ERR_RESOURCE (BIT(13) | BIT(14))
819#define MVPP2_RXD_BM_POOL_ID_OFFS 16
820#define MVPP2_RXD_BM_POOL_ID_MASK (BIT(16) | BIT(17) | BIT(18))
821#define MVPP2_RXD_HWF_SYNC BIT(21)
822#define MVPP2_RXD_L4_CSUM_OK BIT(22)
823#define MVPP2_RXD_IP4_HEADER_ERR BIT(24)
824#define MVPP2_RXD_L4_TCP BIT(25)
825#define MVPP2_RXD_L4_UDP BIT(26)
826#define MVPP2_RXD_L3_IP4 BIT(28)
827#define MVPP2_RXD_L3_IP6 BIT(30)
828#define MVPP2_RXD_BUF_HDR BIT(31)
829
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100830/* HW TX descriptor for PPv2.1 */
831struct mvpp21_tx_desc {
Marcin Wojtas3f518502014-07-10 16:52:13 -0300832 u32 command; /* Options used by HW for packet transmitting.*/
833 u8 packet_offset; /* the offset from the buffer beginning */
834 u8 phys_txq; /* destination queue ID */
835 u16 data_size; /* data size of transmitted packet in bytes */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100836 u32 buf_dma_addr; /* physical addr of transmitted buffer */
Marcin Wojtas3f518502014-07-10 16:52:13 -0300837 u32 buf_cookie; /* cookie for access to TX buffer in tx path */
838 u32 reserved1[3]; /* hw_cmd (for future use, BM, PON, PNC) */
839 u32 reserved2; /* reserved (for future use) */
840};
841
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100842/* HW RX descriptor for PPv2.1 */
843struct mvpp21_rx_desc {
Marcin Wojtas3f518502014-07-10 16:52:13 -0300844 u32 status; /* info about received packet */
845 u16 reserved1; /* parser_info (for future use, PnC) */
846 u16 data_size; /* size of received packet in bytes */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100847 u32 buf_dma_addr; /* physical address of the buffer */
Marcin Wojtas3f518502014-07-10 16:52:13 -0300848 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
849 u16 reserved2; /* gem_port_id (for future use, PON) */
850 u16 reserved3; /* csum_l4 (for future use, PnC) */
851 u8 reserved4; /* bm_qset (for future use, BM) */
852 u8 reserved5;
853 u16 reserved6; /* classify_info (for future use, PnC) */
854 u32 reserved7; /* flow_id (for future use, PnC) */
855 u32 reserved8;
856};
857
Thomas Petazzonie7c53592017-03-07 16:53:08 +0100858/* HW TX descriptor for PPv2.2 */
859struct mvpp22_tx_desc {
860 u32 command;
861 u8 packet_offset;
862 u8 phys_txq;
863 u16 data_size;
864 u64 reserved1;
865 u64 buf_dma_addr_ptp;
866 u64 buf_cookie_misc;
867};
868
869/* HW RX descriptor for PPv2.2 */
870struct mvpp22_rx_desc {
871 u32 status;
872 u16 reserved1;
873 u16 data_size;
874 u32 reserved2;
875 u32 reserved3;
876 u64 buf_dma_addr_key_hash;
877 u64 buf_cookie_misc;
878};
879
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100880/* Opaque type used by the driver to manipulate the HW TX and RX
881 * descriptors
882 */
883struct mvpp2_tx_desc {
884 union {
885 struct mvpp21_tx_desc pp21;
Thomas Petazzonie7c53592017-03-07 16:53:08 +0100886 struct mvpp22_tx_desc pp22;
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100887 };
888};
889
890struct mvpp2_rx_desc {
891 union {
892 struct mvpp21_rx_desc pp21;
Thomas Petazzonie7c53592017-03-07 16:53:08 +0100893 struct mvpp22_rx_desc pp22;
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100894 };
895};
896
Thomas Petazzoni83544912016-12-21 11:28:49 +0100897struct mvpp2_txq_pcpu_buf {
898 /* Transmitted SKB */
899 struct sk_buff *skb;
900
901 /* Physical address of transmitted buffer */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100902 dma_addr_t dma;
Thomas Petazzoni83544912016-12-21 11:28:49 +0100903
904 /* Size transmitted */
905 size_t size;
906};
907
Marcin Wojtas3f518502014-07-10 16:52:13 -0300908/* Per-CPU Tx queue control */
909struct mvpp2_txq_pcpu {
910 int cpu;
911
912 /* Number of Tx DMA descriptors in the descriptor ring */
913 int size;
914
915 /* Number of currently used Tx DMA descriptor in the
916 * descriptor ring
917 */
918 int count;
919
920 /* Number of Tx DMA descriptors reserved for each CPU */
921 int reserved_num;
922
Thomas Petazzoni83544912016-12-21 11:28:49 +0100923 /* Infos about transmitted buffers */
924 struct mvpp2_txq_pcpu_buf *buffs;
Marcin Wojtas71ce3912015-08-06 19:00:29 +0200925
Marcin Wojtas3f518502014-07-10 16:52:13 -0300926 /* Index of last TX DMA descriptor that was inserted */
927 int txq_put_index;
928
929 /* Index of the TX DMA descriptor to be cleaned up */
930 int txq_get_index;
931};
932
933struct mvpp2_tx_queue {
934 /* Physical number of this Tx queue */
935 u8 id;
936
937 /* Logical number of this Tx queue */
938 u8 log_id;
939
940 /* Number of Tx DMA descriptors in the descriptor ring */
941 int size;
942
943 /* Number of currently used Tx DMA descriptor in the descriptor ring */
944 int count;
945
946 /* Per-CPU control of physical Tx queues */
947 struct mvpp2_txq_pcpu __percpu *pcpu;
948
Marcin Wojtas3f518502014-07-10 16:52:13 -0300949 u32 done_pkts_coal;
950
951 /* Virtual address of thex Tx DMA descriptors array */
952 struct mvpp2_tx_desc *descs;
953
954 /* DMA address of the Tx DMA descriptors array */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100955 dma_addr_t descs_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300956
957 /* Index of the last Tx DMA descriptor */
958 int last_desc;
959
960 /* Index of the next Tx DMA descriptor to process */
961 int next_desc_to_proc;
962};
963
964struct mvpp2_rx_queue {
965 /* RX queue number, in the range 0-31 for physical RXQs */
966 u8 id;
967
968 /* Num of rx descriptors in the rx descriptor ring */
969 int size;
970
971 u32 pkts_coal;
972 u32 time_coal;
973
974 /* Virtual address of the RX DMA descriptors array */
975 struct mvpp2_rx_desc *descs;
976
977 /* DMA address of the RX DMA descriptors array */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100978 dma_addr_t descs_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300979
980 /* Index of the last RX DMA descriptor */
981 int last_desc;
982
983 /* Index of the next RX DMA descriptor to process */
984 int next_desc_to_proc;
985
986 /* ID of port to which physical RXQ is mapped */
987 int port;
988
989 /* Port's logic RXQ number to which physical RXQ is mapped */
990 int logic_rxq;
991};
992
993union mvpp2_prs_tcam_entry {
994 u32 word[MVPP2_PRS_TCAM_WORDS];
995 u8 byte[MVPP2_PRS_TCAM_WORDS * 4];
996};
997
998union mvpp2_prs_sram_entry {
999 u32 word[MVPP2_PRS_SRAM_WORDS];
1000 u8 byte[MVPP2_PRS_SRAM_WORDS * 4];
1001};
1002
1003struct mvpp2_prs_entry {
1004 u32 index;
1005 union mvpp2_prs_tcam_entry tcam;
1006 union mvpp2_prs_sram_entry sram;
1007};
1008
1009struct mvpp2_prs_shadow {
1010 bool valid;
1011 bool finish;
1012
1013 /* Lookup ID */
1014 int lu;
1015
1016 /* User defined offset */
1017 int udf;
1018
1019 /* Result info */
1020 u32 ri;
1021 u32 ri_mask;
1022};
1023
1024struct mvpp2_cls_flow_entry {
1025 u32 index;
1026 u32 data[MVPP2_CLS_FLOWS_TBL_DATA_WORDS];
1027};
1028
1029struct mvpp2_cls_lookup_entry {
1030 u32 lkpid;
1031 u32 way;
1032 u32 data;
1033};
1034
1035struct mvpp2_bm_pool {
1036 /* Pool number in the range 0-7 */
1037 int id;
1038 enum mvpp2_bm_type type;
1039
1040 /* Buffer Pointers Pool External (BPPE) size */
1041 int size;
Thomas Petazzonid01524d2017-03-07 16:53:09 +01001042 /* BPPE size in bytes */
1043 int size_bytes;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001044 /* Number of buffers for this pool */
1045 int buf_num;
1046 /* Pool buffer size */
1047 int buf_size;
1048 /* Packet size */
1049 int pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01001050 int frag_size;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001051
1052 /* BPPE virtual base address */
1053 u32 *virt_addr;
Thomas Petazzoni20396132017-03-07 16:53:00 +01001054 /* BPPE DMA base address */
1055 dma_addr_t dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001056
1057 /* Ports using BM pool */
1058 u32 port_map;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001059};
1060
Marcin Wojtas3f518502014-07-10 16:52:13 -03001061/* Static declaractions */
1062
1063/* Number of RXQs used by single port */
1064static int rxq_number = MVPP2_DEFAULT_RXQ;
1065/* Number of TXQs used by single port */
1066static int txq_number = MVPP2_MAX_TXQ;
1067
1068#define MVPP2_DRIVER_NAME "mvpp2"
1069#define MVPP2_DRIVER_VERSION "1.0"
1070
1071/* Utility/helper methods */
1072
1073static void mvpp2_write(struct mvpp2 *priv, u32 offset, u32 data)
1074{
Thomas Petazzonia7868412017-03-07 16:53:13 +01001075 writel(data, priv->cpu_base[0] + offset);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001076}
1077
1078static u32 mvpp2_read(struct mvpp2 *priv, u32 offset)
1079{
Thomas Petazzonia7868412017-03-07 16:53:13 +01001080 return readl(priv->cpu_base[0] + offset);
1081}
1082
1083/* These accessors should be used to access:
1084 *
1085 * - per-CPU registers, where each CPU has its own copy of the
1086 * register.
1087 *
1088 * MVPP2_BM_VIRT_ALLOC_REG
1089 * MVPP2_BM_ADDR_HIGH_ALLOC
1090 * MVPP22_BM_ADDR_HIGH_RLS_REG
1091 * MVPP2_BM_VIRT_RLS_REG
1092 * MVPP2_ISR_RX_TX_CAUSE_REG
1093 * MVPP2_ISR_RX_TX_MASK_REG
1094 * MVPP2_TXQ_NUM_REG
1095 * MVPP2_AGGR_TXQ_UPDATE_REG
1096 * MVPP2_TXQ_RSVD_REQ_REG
1097 * MVPP2_TXQ_RSVD_RSLT_REG
1098 * MVPP2_TXQ_SENT_REG
1099 * MVPP2_RXQ_NUM_REG
1100 *
1101 * - global registers that must be accessed through a specific CPU
1102 * window, because they are related to an access to a per-CPU
1103 * register
1104 *
1105 * MVPP2_BM_PHY_ALLOC_REG (related to MVPP2_BM_VIRT_ALLOC_REG)
1106 * MVPP2_BM_PHY_RLS_REG (related to MVPP2_BM_VIRT_RLS_REG)
1107 * MVPP2_RXQ_THRESH_REG (related to MVPP2_RXQ_NUM_REG)
1108 * MVPP2_RXQ_DESC_ADDR_REG (related to MVPP2_RXQ_NUM_REG)
1109 * MVPP2_RXQ_DESC_SIZE_REG (related to MVPP2_RXQ_NUM_REG)
1110 * MVPP2_RXQ_INDEX_REG (related to MVPP2_RXQ_NUM_REG)
1111 * MVPP2_TXQ_PENDING_REG (related to MVPP2_TXQ_NUM_REG)
1112 * MVPP2_TXQ_DESC_ADDR_REG (related to MVPP2_TXQ_NUM_REG)
1113 * MVPP2_TXQ_DESC_SIZE_REG (related to MVPP2_TXQ_NUM_REG)
1114 * MVPP2_TXQ_INDEX_REG (related to MVPP2_TXQ_NUM_REG)
1115 * MVPP2_TXQ_PENDING_REG (related to MVPP2_TXQ_NUM_REG)
1116 * MVPP2_TXQ_PREF_BUF_REG (related to MVPP2_TXQ_NUM_REG)
1117 * MVPP2_TXQ_PREF_BUF_REG (related to MVPP2_TXQ_NUM_REG)
1118 */
1119static void mvpp2_percpu_write(struct mvpp2 *priv, int cpu,
1120 u32 offset, u32 data)
1121{
1122 writel(data, priv->cpu_base[cpu] + offset);
1123}
1124
1125static u32 mvpp2_percpu_read(struct mvpp2 *priv, int cpu,
1126 u32 offset)
1127{
1128 return readl(priv->cpu_base[cpu] + offset);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001129}
1130
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001131static dma_addr_t mvpp2_txdesc_dma_addr_get(struct mvpp2_port *port,
1132 struct mvpp2_tx_desc *tx_desc)
1133{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001134 if (port->priv->hw_version == MVPP21)
1135 return tx_desc->pp21.buf_dma_addr;
1136 else
1137 return tx_desc->pp22.buf_dma_addr_ptp & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001138}
1139
1140static void mvpp2_txdesc_dma_addr_set(struct mvpp2_port *port,
1141 struct mvpp2_tx_desc *tx_desc,
1142 dma_addr_t dma_addr)
1143{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001144 if (port->priv->hw_version == MVPP21) {
1145 tx_desc->pp21.buf_dma_addr = dma_addr;
1146 } else {
1147 u64 val = (u64)dma_addr;
1148
1149 tx_desc->pp22.buf_dma_addr_ptp &= ~GENMASK_ULL(40, 0);
1150 tx_desc->pp22.buf_dma_addr_ptp |= val;
1151 }
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001152}
1153
1154static size_t mvpp2_txdesc_size_get(struct mvpp2_port *port,
1155 struct mvpp2_tx_desc *tx_desc)
1156{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001157 if (port->priv->hw_version == MVPP21)
1158 return tx_desc->pp21.data_size;
1159 else
1160 return tx_desc->pp22.data_size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001161}
1162
1163static void mvpp2_txdesc_size_set(struct mvpp2_port *port,
1164 struct mvpp2_tx_desc *tx_desc,
1165 size_t size)
1166{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001167 if (port->priv->hw_version == MVPP21)
1168 tx_desc->pp21.data_size = size;
1169 else
1170 tx_desc->pp22.data_size = size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001171}
1172
1173static void mvpp2_txdesc_txq_set(struct mvpp2_port *port,
1174 struct mvpp2_tx_desc *tx_desc,
1175 unsigned int txq)
1176{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001177 if (port->priv->hw_version == MVPP21)
1178 tx_desc->pp21.phys_txq = txq;
1179 else
1180 tx_desc->pp22.phys_txq = txq;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001181}
1182
1183static void mvpp2_txdesc_cmd_set(struct mvpp2_port *port,
1184 struct mvpp2_tx_desc *tx_desc,
1185 unsigned int command)
1186{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001187 if (port->priv->hw_version == MVPP21)
1188 tx_desc->pp21.command = command;
1189 else
1190 tx_desc->pp22.command = command;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001191}
1192
1193static void mvpp2_txdesc_offset_set(struct mvpp2_port *port,
1194 struct mvpp2_tx_desc *tx_desc,
1195 unsigned int offset)
1196{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001197 if (port->priv->hw_version == MVPP21)
1198 tx_desc->pp21.packet_offset = offset;
1199 else
1200 tx_desc->pp22.packet_offset = offset;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001201}
1202
1203static unsigned int mvpp2_txdesc_offset_get(struct mvpp2_port *port,
1204 struct mvpp2_tx_desc *tx_desc)
1205{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001206 if (port->priv->hw_version == MVPP21)
1207 return tx_desc->pp21.packet_offset;
1208 else
1209 return tx_desc->pp22.packet_offset;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001210}
1211
1212static dma_addr_t mvpp2_rxdesc_dma_addr_get(struct mvpp2_port *port,
1213 struct mvpp2_rx_desc *rx_desc)
1214{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001215 if (port->priv->hw_version == MVPP21)
1216 return rx_desc->pp21.buf_dma_addr;
1217 else
1218 return rx_desc->pp22.buf_dma_addr_key_hash & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001219}
1220
1221static unsigned long mvpp2_rxdesc_cookie_get(struct mvpp2_port *port,
1222 struct mvpp2_rx_desc *rx_desc)
1223{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001224 if (port->priv->hw_version == MVPP21)
1225 return rx_desc->pp21.buf_cookie;
1226 else
1227 return rx_desc->pp22.buf_cookie_misc & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001228}
1229
1230static size_t mvpp2_rxdesc_size_get(struct mvpp2_port *port,
1231 struct mvpp2_rx_desc *rx_desc)
1232{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001233 if (port->priv->hw_version == MVPP21)
1234 return rx_desc->pp21.data_size;
1235 else
1236 return rx_desc->pp22.data_size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001237}
1238
1239static u32 mvpp2_rxdesc_status_get(struct mvpp2_port *port,
1240 struct mvpp2_rx_desc *rx_desc)
1241{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001242 if (port->priv->hw_version == MVPP21)
1243 return rx_desc->pp21.status;
1244 else
1245 return rx_desc->pp22.status;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001246}
1247
Marcin Wojtas3f518502014-07-10 16:52:13 -03001248static void mvpp2_txq_inc_get(struct mvpp2_txq_pcpu *txq_pcpu)
1249{
1250 txq_pcpu->txq_get_index++;
1251 if (txq_pcpu->txq_get_index == txq_pcpu->size)
1252 txq_pcpu->txq_get_index = 0;
1253}
1254
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001255static void mvpp2_txq_inc_put(struct mvpp2_port *port,
1256 struct mvpp2_txq_pcpu *txq_pcpu,
Marcin Wojtas71ce3912015-08-06 19:00:29 +02001257 struct sk_buff *skb,
1258 struct mvpp2_tx_desc *tx_desc)
Marcin Wojtas3f518502014-07-10 16:52:13 -03001259{
Thomas Petazzoni83544912016-12-21 11:28:49 +01001260 struct mvpp2_txq_pcpu_buf *tx_buf =
1261 txq_pcpu->buffs + txq_pcpu->txq_put_index;
1262 tx_buf->skb = skb;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001263 tx_buf->size = mvpp2_txdesc_size_get(port, tx_desc);
1264 tx_buf->dma = mvpp2_txdesc_dma_addr_get(port, tx_desc) +
1265 mvpp2_txdesc_offset_get(port, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001266 txq_pcpu->txq_put_index++;
1267 if (txq_pcpu->txq_put_index == txq_pcpu->size)
1268 txq_pcpu->txq_put_index = 0;
1269}
1270
1271/* Get number of physical egress port */
1272static inline int mvpp2_egress_port(struct mvpp2_port *port)
1273{
1274 return MVPP2_MAX_TCONT + port->id;
1275}
1276
1277/* Get number of physical TXQ */
1278static inline int mvpp2_txq_phys(int port, int txq)
1279{
1280 return (MVPP2_MAX_TCONT + port) * MVPP2_MAX_TXQ + txq;
1281}
1282
1283/* Parser configuration routines */
1284
1285/* Update parser tcam and sram hw entries */
1286static int mvpp2_prs_hw_write(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1287{
1288 int i;
1289
1290 if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1291 return -EINVAL;
1292
1293 /* Clear entry invalidation bit */
1294 pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] &= ~MVPP2_PRS_TCAM_INV_MASK;
1295
1296 /* Write tcam index - indirect access */
1297 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1298 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1299 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), pe->tcam.word[i]);
1300
1301 /* Write sram index - indirect access */
1302 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1303 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1304 mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), pe->sram.word[i]);
1305
1306 return 0;
1307}
1308
1309/* Read tcam entry from hw */
1310static int mvpp2_prs_hw_read(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1311{
1312 int i;
1313
1314 if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1315 return -EINVAL;
1316
1317 /* Write tcam index - indirect access */
1318 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1319
1320 pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] = mvpp2_read(priv,
1321 MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD));
1322 if (pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] & MVPP2_PRS_TCAM_INV_MASK)
1323 return MVPP2_PRS_TCAM_ENTRY_INVALID;
1324
1325 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1326 pe->tcam.word[i] = mvpp2_read(priv, MVPP2_PRS_TCAM_DATA_REG(i));
1327
1328 /* Write sram index - indirect access */
1329 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1330 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1331 pe->sram.word[i] = mvpp2_read(priv, MVPP2_PRS_SRAM_DATA_REG(i));
1332
1333 return 0;
1334}
1335
1336/* Invalidate tcam hw entry */
1337static void mvpp2_prs_hw_inv(struct mvpp2 *priv, int index)
1338{
1339 /* Write index - indirect access */
1340 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
1341 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD),
1342 MVPP2_PRS_TCAM_INV_MASK);
1343}
1344
1345/* Enable shadow table entry and set its lookup ID */
1346static void mvpp2_prs_shadow_set(struct mvpp2 *priv, int index, int lu)
1347{
1348 priv->prs_shadow[index].valid = true;
1349 priv->prs_shadow[index].lu = lu;
1350}
1351
1352/* Update ri fields in shadow table entry */
1353static void mvpp2_prs_shadow_ri_set(struct mvpp2 *priv, int index,
1354 unsigned int ri, unsigned int ri_mask)
1355{
1356 priv->prs_shadow[index].ri_mask = ri_mask;
1357 priv->prs_shadow[index].ri = ri;
1358}
1359
1360/* Update lookup field in tcam sw entry */
1361static void mvpp2_prs_tcam_lu_set(struct mvpp2_prs_entry *pe, unsigned int lu)
1362{
1363 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_LU_BYTE);
1364
1365 pe->tcam.byte[MVPP2_PRS_TCAM_LU_BYTE] = lu;
1366 pe->tcam.byte[enable_off] = MVPP2_PRS_LU_MASK;
1367}
1368
1369/* Update mask for single port in tcam sw entry */
1370static void mvpp2_prs_tcam_port_set(struct mvpp2_prs_entry *pe,
1371 unsigned int port, bool add)
1372{
1373 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1374
1375 if (add)
1376 pe->tcam.byte[enable_off] &= ~(1 << port);
1377 else
1378 pe->tcam.byte[enable_off] |= 1 << port;
1379}
1380
1381/* Update port map in tcam sw entry */
1382static void mvpp2_prs_tcam_port_map_set(struct mvpp2_prs_entry *pe,
1383 unsigned int ports)
1384{
1385 unsigned char port_mask = MVPP2_PRS_PORT_MASK;
1386 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1387
1388 pe->tcam.byte[MVPP2_PRS_TCAM_PORT_BYTE] = 0;
1389 pe->tcam.byte[enable_off] &= ~port_mask;
1390 pe->tcam.byte[enable_off] |= ~ports & MVPP2_PRS_PORT_MASK;
1391}
1392
1393/* Obtain port map from tcam sw entry */
1394static unsigned int mvpp2_prs_tcam_port_map_get(struct mvpp2_prs_entry *pe)
1395{
1396 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1397
1398 return ~(pe->tcam.byte[enable_off]) & MVPP2_PRS_PORT_MASK;
1399}
1400
1401/* Set byte of data and its enable bits in tcam sw entry */
1402static void mvpp2_prs_tcam_data_byte_set(struct mvpp2_prs_entry *pe,
1403 unsigned int offs, unsigned char byte,
1404 unsigned char enable)
1405{
1406 pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)] = byte;
1407 pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)] = enable;
1408}
1409
1410/* Get byte of data and its enable bits from tcam sw entry */
1411static void mvpp2_prs_tcam_data_byte_get(struct mvpp2_prs_entry *pe,
1412 unsigned int offs, unsigned char *byte,
1413 unsigned char *enable)
1414{
1415 *byte = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)];
1416 *enable = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)];
1417}
1418
1419/* Compare tcam data bytes with a pattern */
1420static bool mvpp2_prs_tcam_data_cmp(struct mvpp2_prs_entry *pe, int offs,
1421 u16 data)
1422{
1423 int off = MVPP2_PRS_TCAM_DATA_BYTE(offs);
1424 u16 tcam_data;
1425
1426 tcam_data = (8 << pe->tcam.byte[off + 1]) | pe->tcam.byte[off];
1427 if (tcam_data != data)
1428 return false;
1429 return true;
1430}
1431
1432/* Update ai bits in tcam sw entry */
1433static void mvpp2_prs_tcam_ai_update(struct mvpp2_prs_entry *pe,
1434 unsigned int bits, unsigned int enable)
1435{
1436 int i, ai_idx = MVPP2_PRS_TCAM_AI_BYTE;
1437
1438 for (i = 0; i < MVPP2_PRS_AI_BITS; i++) {
1439
1440 if (!(enable & BIT(i)))
1441 continue;
1442
1443 if (bits & BIT(i))
1444 pe->tcam.byte[ai_idx] |= 1 << i;
1445 else
1446 pe->tcam.byte[ai_idx] &= ~(1 << i);
1447 }
1448
1449 pe->tcam.byte[MVPP2_PRS_TCAM_EN_OFFS(ai_idx)] |= enable;
1450}
1451
1452/* Get ai bits from tcam sw entry */
1453static int mvpp2_prs_tcam_ai_get(struct mvpp2_prs_entry *pe)
1454{
1455 return pe->tcam.byte[MVPP2_PRS_TCAM_AI_BYTE];
1456}
1457
1458/* Set ethertype in tcam sw entry */
1459static void mvpp2_prs_match_etype(struct mvpp2_prs_entry *pe, int offset,
1460 unsigned short ethertype)
1461{
1462 mvpp2_prs_tcam_data_byte_set(pe, offset + 0, ethertype >> 8, 0xff);
1463 mvpp2_prs_tcam_data_byte_set(pe, offset + 1, ethertype & 0xff, 0xff);
1464}
1465
1466/* Set bits in sram sw entry */
1467static void mvpp2_prs_sram_bits_set(struct mvpp2_prs_entry *pe, int bit_num,
1468 int val)
1469{
1470 pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] |= (val << (bit_num % 8));
1471}
1472
1473/* Clear bits in sram sw entry */
1474static void mvpp2_prs_sram_bits_clear(struct mvpp2_prs_entry *pe, int bit_num,
1475 int val)
1476{
1477 pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] &= ~(val << (bit_num % 8));
1478}
1479
1480/* Update ri bits in sram sw entry */
1481static void mvpp2_prs_sram_ri_update(struct mvpp2_prs_entry *pe,
1482 unsigned int bits, unsigned int mask)
1483{
1484 unsigned int i;
1485
1486 for (i = 0; i < MVPP2_PRS_SRAM_RI_CTRL_BITS; i++) {
1487 int ri_off = MVPP2_PRS_SRAM_RI_OFFS;
1488
1489 if (!(mask & BIT(i)))
1490 continue;
1491
1492 if (bits & BIT(i))
1493 mvpp2_prs_sram_bits_set(pe, ri_off + i, 1);
1494 else
1495 mvpp2_prs_sram_bits_clear(pe, ri_off + i, 1);
1496
1497 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_RI_CTRL_OFFS + i, 1);
1498 }
1499}
1500
1501/* Obtain ri bits from sram sw entry */
1502static int mvpp2_prs_sram_ri_get(struct mvpp2_prs_entry *pe)
1503{
1504 return pe->sram.word[MVPP2_PRS_SRAM_RI_WORD];
1505}
1506
1507/* Update ai bits in sram sw entry */
1508static void mvpp2_prs_sram_ai_update(struct mvpp2_prs_entry *pe,
1509 unsigned int bits, unsigned int mask)
1510{
1511 unsigned int i;
1512 int ai_off = MVPP2_PRS_SRAM_AI_OFFS;
1513
1514 for (i = 0; i < MVPP2_PRS_SRAM_AI_CTRL_BITS; i++) {
1515
1516 if (!(mask & BIT(i)))
1517 continue;
1518
1519 if (bits & BIT(i))
1520 mvpp2_prs_sram_bits_set(pe, ai_off + i, 1);
1521 else
1522 mvpp2_prs_sram_bits_clear(pe, ai_off + i, 1);
1523
1524 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_AI_CTRL_OFFS + i, 1);
1525 }
1526}
1527
1528/* Read ai bits from sram sw entry */
1529static int mvpp2_prs_sram_ai_get(struct mvpp2_prs_entry *pe)
1530{
1531 u8 bits;
1532 int ai_off = MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_AI_OFFS);
1533 int ai_en_off = ai_off + 1;
1534 int ai_shift = MVPP2_PRS_SRAM_AI_OFFS % 8;
1535
1536 bits = (pe->sram.byte[ai_off] >> ai_shift) |
1537 (pe->sram.byte[ai_en_off] << (8 - ai_shift));
1538
1539 return bits;
1540}
1541
1542/* In sram sw entry set lookup ID field of the tcam key to be used in the next
1543 * lookup interation
1544 */
1545static void mvpp2_prs_sram_next_lu_set(struct mvpp2_prs_entry *pe,
1546 unsigned int lu)
1547{
1548 int sram_next_off = MVPP2_PRS_SRAM_NEXT_LU_OFFS;
1549
1550 mvpp2_prs_sram_bits_clear(pe, sram_next_off,
1551 MVPP2_PRS_SRAM_NEXT_LU_MASK);
1552 mvpp2_prs_sram_bits_set(pe, sram_next_off, lu);
1553}
1554
1555/* In the sram sw entry set sign and value of the next lookup offset
1556 * and the offset value generated to the classifier
1557 */
1558static void mvpp2_prs_sram_shift_set(struct mvpp2_prs_entry *pe, int shift,
1559 unsigned int op)
1560{
1561 /* Set sign */
1562 if (shift < 0) {
1563 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1564 shift = 0 - shift;
1565 } else {
1566 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1567 }
1568
1569 /* Set value */
1570 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_SHIFT_OFFS)] =
1571 (unsigned char)shift;
1572
1573 /* Reset and set operation */
1574 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS,
1575 MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK);
1576 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS, op);
1577
1578 /* Set base offset as current */
1579 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1580}
1581
1582/* In the sram sw entry set sign and value of the user defined offset
1583 * generated to the classifier
1584 */
1585static void mvpp2_prs_sram_offset_set(struct mvpp2_prs_entry *pe,
1586 unsigned int type, int offset,
1587 unsigned int op)
1588{
1589 /* Set sign */
1590 if (offset < 0) {
1591 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1592 offset = 0 - offset;
1593 } else {
1594 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1595 }
1596
1597 /* Set value */
1598 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_OFFS,
1599 MVPP2_PRS_SRAM_UDF_MASK);
1600 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_OFFS, offset);
1601 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1602 MVPP2_PRS_SRAM_UDF_BITS)] &=
1603 ~(MVPP2_PRS_SRAM_UDF_MASK >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1604 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1605 MVPP2_PRS_SRAM_UDF_BITS)] |=
1606 (offset >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1607
1608 /* Set offset type */
1609 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS,
1610 MVPP2_PRS_SRAM_UDF_TYPE_MASK);
1611 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS, type);
1612
1613 /* Set offset operation */
1614 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS,
1615 MVPP2_PRS_SRAM_OP_SEL_UDF_MASK);
1616 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS, op);
1617
1618 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1619 MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] &=
1620 ~(MVPP2_PRS_SRAM_OP_SEL_UDF_MASK >>
1621 (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1622
1623 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1624 MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] |=
1625 (op >> (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1626
1627 /* Set base offset as current */
1628 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1629}
1630
1631/* Find parser flow entry */
1632static struct mvpp2_prs_entry *mvpp2_prs_flow_find(struct mvpp2 *priv, int flow)
1633{
1634 struct mvpp2_prs_entry *pe;
1635 int tid;
1636
1637 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
1638 if (!pe)
1639 return NULL;
1640 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
1641
1642 /* Go through the all entires with MVPP2_PRS_LU_FLOWS */
1643 for (tid = MVPP2_PRS_TCAM_SRAM_SIZE - 1; tid >= 0; tid--) {
1644 u8 bits;
1645
1646 if (!priv->prs_shadow[tid].valid ||
1647 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_FLOWS)
1648 continue;
1649
1650 pe->index = tid;
1651 mvpp2_prs_hw_read(priv, pe);
1652 bits = mvpp2_prs_sram_ai_get(pe);
1653
1654 /* Sram store classification lookup ID in AI bits [5:0] */
1655 if ((bits & MVPP2_PRS_FLOW_ID_MASK) == flow)
1656 return pe;
1657 }
1658 kfree(pe);
1659
1660 return NULL;
1661}
1662
1663/* Return first free tcam index, seeking from start to end */
1664static int mvpp2_prs_tcam_first_free(struct mvpp2 *priv, unsigned char start,
1665 unsigned char end)
1666{
1667 int tid;
1668
1669 if (start > end)
1670 swap(start, end);
1671
1672 if (end >= MVPP2_PRS_TCAM_SRAM_SIZE)
1673 end = MVPP2_PRS_TCAM_SRAM_SIZE - 1;
1674
1675 for (tid = start; tid <= end; tid++) {
1676 if (!priv->prs_shadow[tid].valid)
1677 return tid;
1678 }
1679
1680 return -EINVAL;
1681}
1682
1683/* Enable/disable dropping all mac da's */
1684static void mvpp2_prs_mac_drop_all_set(struct mvpp2 *priv, int port, bool add)
1685{
1686 struct mvpp2_prs_entry pe;
1687
1688 if (priv->prs_shadow[MVPP2_PE_DROP_ALL].valid) {
1689 /* Entry exist - update port only */
1690 pe.index = MVPP2_PE_DROP_ALL;
1691 mvpp2_prs_hw_read(priv, &pe);
1692 } else {
1693 /* Entry doesn't exist - create new */
1694 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1695 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1696 pe.index = MVPP2_PE_DROP_ALL;
1697
1698 /* Non-promiscuous mode for all ports - DROP unknown packets */
1699 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
1700 MVPP2_PRS_RI_DROP_MASK);
1701
1702 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1703 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1704
1705 /* Update shadow table */
1706 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1707
1708 /* Mask all ports */
1709 mvpp2_prs_tcam_port_map_set(&pe, 0);
1710 }
1711
1712 /* Update port mask */
1713 mvpp2_prs_tcam_port_set(&pe, port, add);
1714
1715 mvpp2_prs_hw_write(priv, &pe);
1716}
1717
1718/* Set port to promiscuous mode */
1719static void mvpp2_prs_mac_promisc_set(struct mvpp2 *priv, int port, bool add)
1720{
1721 struct mvpp2_prs_entry pe;
1722
Joe Perchesdbedd442015-03-06 20:49:12 -08001723 /* Promiscuous mode - Accept unknown packets */
Marcin Wojtas3f518502014-07-10 16:52:13 -03001724
1725 if (priv->prs_shadow[MVPP2_PE_MAC_PROMISCUOUS].valid) {
1726 /* Entry exist - update port only */
1727 pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1728 mvpp2_prs_hw_read(priv, &pe);
1729 } else {
1730 /* Entry doesn't exist - create new */
1731 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1732 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1733 pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1734
1735 /* Continue - set next lookup */
1736 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
1737
1738 /* Set result info bits */
1739 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_UCAST,
1740 MVPP2_PRS_RI_L2_CAST_MASK);
1741
1742 /* Shift to ethertype */
1743 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
1744 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1745
1746 /* Mask all ports */
1747 mvpp2_prs_tcam_port_map_set(&pe, 0);
1748
1749 /* Update shadow table */
1750 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1751 }
1752
1753 /* Update port mask */
1754 mvpp2_prs_tcam_port_set(&pe, port, add);
1755
1756 mvpp2_prs_hw_write(priv, &pe);
1757}
1758
1759/* Accept multicast */
1760static void mvpp2_prs_mac_multi_set(struct mvpp2 *priv, int port, int index,
1761 bool add)
1762{
1763 struct mvpp2_prs_entry pe;
1764 unsigned char da_mc;
1765
1766 /* Ethernet multicast address first byte is
1767 * 0x01 for IPv4 and 0x33 for IPv6
1768 */
1769 da_mc = (index == MVPP2_PE_MAC_MC_ALL) ? 0x01 : 0x33;
1770
1771 if (priv->prs_shadow[index].valid) {
1772 /* Entry exist - update port only */
1773 pe.index = index;
1774 mvpp2_prs_hw_read(priv, &pe);
1775 } else {
1776 /* Entry doesn't exist - create new */
1777 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1778 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1779 pe.index = index;
1780
1781 /* Continue - set next lookup */
1782 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
1783
1784 /* Set result info bits */
1785 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_MCAST,
1786 MVPP2_PRS_RI_L2_CAST_MASK);
1787
1788 /* Update tcam entry data first byte */
1789 mvpp2_prs_tcam_data_byte_set(&pe, 0, da_mc, 0xff);
1790
1791 /* Shift to ethertype */
1792 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
1793 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1794
1795 /* Mask all ports */
1796 mvpp2_prs_tcam_port_map_set(&pe, 0);
1797
1798 /* Update shadow table */
1799 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1800 }
1801
1802 /* Update port mask */
1803 mvpp2_prs_tcam_port_set(&pe, port, add);
1804
1805 mvpp2_prs_hw_write(priv, &pe);
1806}
1807
1808/* Set entry for dsa packets */
1809static void mvpp2_prs_dsa_tag_set(struct mvpp2 *priv, int port, bool add,
1810 bool tagged, bool extend)
1811{
1812 struct mvpp2_prs_entry pe;
1813 int tid, shift;
1814
1815 if (extend) {
1816 tid = tagged ? MVPP2_PE_EDSA_TAGGED : MVPP2_PE_EDSA_UNTAGGED;
1817 shift = 8;
1818 } else {
1819 tid = tagged ? MVPP2_PE_DSA_TAGGED : MVPP2_PE_DSA_UNTAGGED;
1820 shift = 4;
1821 }
1822
1823 if (priv->prs_shadow[tid].valid) {
1824 /* Entry exist - update port only */
1825 pe.index = tid;
1826 mvpp2_prs_hw_read(priv, &pe);
1827 } else {
1828 /* Entry doesn't exist - create new */
1829 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1830 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
1831 pe.index = tid;
1832
1833 /* Shift 4 bytes if DSA tag or 8 bytes in case of EDSA tag*/
1834 mvpp2_prs_sram_shift_set(&pe, shift,
1835 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1836
1837 /* Update shadow table */
1838 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
1839
1840 if (tagged) {
1841 /* Set tagged bit in DSA tag */
1842 mvpp2_prs_tcam_data_byte_set(&pe, 0,
1843 MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
1844 MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
1845 /* Clear all ai bits for next iteration */
1846 mvpp2_prs_sram_ai_update(&pe, 0,
1847 MVPP2_PRS_SRAM_AI_MASK);
1848 /* If packet is tagged continue check vlans */
1849 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
1850 } else {
1851 /* Set result info bits to 'no vlans' */
1852 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
1853 MVPP2_PRS_RI_VLAN_MASK);
1854 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
1855 }
1856
1857 /* Mask all ports */
1858 mvpp2_prs_tcam_port_map_set(&pe, 0);
1859 }
1860
1861 /* Update port mask */
1862 mvpp2_prs_tcam_port_set(&pe, port, add);
1863
1864 mvpp2_prs_hw_write(priv, &pe);
1865}
1866
1867/* Set entry for dsa ethertype */
1868static void mvpp2_prs_dsa_tag_ethertype_set(struct mvpp2 *priv, int port,
1869 bool add, bool tagged, bool extend)
1870{
1871 struct mvpp2_prs_entry pe;
1872 int tid, shift, port_mask;
1873
1874 if (extend) {
1875 tid = tagged ? MVPP2_PE_ETYPE_EDSA_TAGGED :
1876 MVPP2_PE_ETYPE_EDSA_UNTAGGED;
1877 port_mask = 0;
1878 shift = 8;
1879 } else {
1880 tid = tagged ? MVPP2_PE_ETYPE_DSA_TAGGED :
1881 MVPP2_PE_ETYPE_DSA_UNTAGGED;
1882 port_mask = MVPP2_PRS_PORT_MASK;
1883 shift = 4;
1884 }
1885
1886 if (priv->prs_shadow[tid].valid) {
1887 /* Entry exist - update port only */
1888 pe.index = tid;
1889 mvpp2_prs_hw_read(priv, &pe);
1890 } else {
1891 /* Entry doesn't exist - create new */
1892 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1893 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
1894 pe.index = tid;
1895
1896 /* Set ethertype */
1897 mvpp2_prs_match_etype(&pe, 0, ETH_P_EDSA);
1898 mvpp2_prs_match_etype(&pe, 2, 0);
1899
1900 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DSA_MASK,
1901 MVPP2_PRS_RI_DSA_MASK);
1902 /* Shift ethertype + 2 byte reserved + tag*/
1903 mvpp2_prs_sram_shift_set(&pe, 2 + MVPP2_ETH_TYPE_LEN + shift,
1904 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1905
1906 /* Update shadow table */
1907 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
1908
1909 if (tagged) {
1910 /* Set tagged bit in DSA tag */
1911 mvpp2_prs_tcam_data_byte_set(&pe,
1912 MVPP2_ETH_TYPE_LEN + 2 + 3,
1913 MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
1914 MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
1915 /* Clear all ai bits for next iteration */
1916 mvpp2_prs_sram_ai_update(&pe, 0,
1917 MVPP2_PRS_SRAM_AI_MASK);
1918 /* If packet is tagged continue check vlans */
1919 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
1920 } else {
1921 /* Set result info bits to 'no vlans' */
1922 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
1923 MVPP2_PRS_RI_VLAN_MASK);
1924 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
1925 }
1926 /* Mask/unmask all ports, depending on dsa type */
1927 mvpp2_prs_tcam_port_map_set(&pe, port_mask);
1928 }
1929
1930 /* Update port mask */
1931 mvpp2_prs_tcam_port_set(&pe, port, add);
1932
1933 mvpp2_prs_hw_write(priv, &pe);
1934}
1935
1936/* Search for existing single/triple vlan entry */
1937static struct mvpp2_prs_entry *mvpp2_prs_vlan_find(struct mvpp2 *priv,
1938 unsigned short tpid, int ai)
1939{
1940 struct mvpp2_prs_entry *pe;
1941 int tid;
1942
1943 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
1944 if (!pe)
1945 return NULL;
1946 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
1947
1948 /* Go through the all entries with MVPP2_PRS_LU_VLAN */
1949 for (tid = MVPP2_PE_FIRST_FREE_TID;
1950 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
1951 unsigned int ri_bits, ai_bits;
1952 bool match;
1953
1954 if (!priv->prs_shadow[tid].valid ||
1955 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
1956 continue;
1957
1958 pe->index = tid;
1959
1960 mvpp2_prs_hw_read(priv, pe);
1961 match = mvpp2_prs_tcam_data_cmp(pe, 0, swab16(tpid));
1962 if (!match)
1963 continue;
1964
1965 /* Get vlan type */
1966 ri_bits = mvpp2_prs_sram_ri_get(pe);
1967 ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
1968
1969 /* Get current ai value from tcam */
1970 ai_bits = mvpp2_prs_tcam_ai_get(pe);
1971 /* Clear double vlan bit */
1972 ai_bits &= ~MVPP2_PRS_DBL_VLAN_AI_BIT;
1973
1974 if (ai != ai_bits)
1975 continue;
1976
1977 if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
1978 ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
1979 return pe;
1980 }
1981 kfree(pe);
1982
1983 return NULL;
1984}
1985
1986/* Add/update single/triple vlan entry */
1987static int mvpp2_prs_vlan_add(struct mvpp2 *priv, unsigned short tpid, int ai,
1988 unsigned int port_map)
1989{
1990 struct mvpp2_prs_entry *pe;
1991 int tid_aux, tid;
Sudip Mukherjee43737472014-11-01 16:59:34 +05301992 int ret = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001993
1994 pe = mvpp2_prs_vlan_find(priv, tpid, ai);
1995
1996 if (!pe) {
1997 /* Create new tcam entry */
1998 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_LAST_FREE_TID,
1999 MVPP2_PE_FIRST_FREE_TID);
2000 if (tid < 0)
2001 return tid;
2002
2003 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2004 if (!pe)
2005 return -ENOMEM;
2006
2007 /* Get last double vlan tid */
2008 for (tid_aux = MVPP2_PE_LAST_FREE_TID;
2009 tid_aux >= MVPP2_PE_FIRST_FREE_TID; tid_aux--) {
2010 unsigned int ri_bits;
2011
2012 if (!priv->prs_shadow[tid_aux].valid ||
2013 priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
2014 continue;
2015
2016 pe->index = tid_aux;
2017 mvpp2_prs_hw_read(priv, pe);
2018 ri_bits = mvpp2_prs_sram_ri_get(pe);
2019 if ((ri_bits & MVPP2_PRS_RI_VLAN_MASK) ==
2020 MVPP2_PRS_RI_VLAN_DOUBLE)
2021 break;
2022 }
2023
Sudip Mukherjee43737472014-11-01 16:59:34 +05302024 if (tid <= tid_aux) {
2025 ret = -EINVAL;
2026 goto error;
2027 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002028
2029 memset(pe, 0 , sizeof(struct mvpp2_prs_entry));
2030 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2031 pe->index = tid;
2032
2033 mvpp2_prs_match_etype(pe, 0, tpid);
2034
2035 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_L2);
2036 /* Shift 4 bytes - skip 1 vlan tag */
2037 mvpp2_prs_sram_shift_set(pe, MVPP2_VLAN_TAG_LEN,
2038 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2039 /* Clear all ai bits for next iteration */
2040 mvpp2_prs_sram_ai_update(pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2041
2042 if (ai == MVPP2_PRS_SINGLE_VLAN_AI) {
2043 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_SINGLE,
2044 MVPP2_PRS_RI_VLAN_MASK);
2045 } else {
2046 ai |= MVPP2_PRS_DBL_VLAN_AI_BIT;
2047 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_TRIPLE,
2048 MVPP2_PRS_RI_VLAN_MASK);
2049 }
2050 mvpp2_prs_tcam_ai_update(pe, ai, MVPP2_PRS_SRAM_AI_MASK);
2051
2052 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_VLAN);
2053 }
2054 /* Update ports' mask */
2055 mvpp2_prs_tcam_port_map_set(pe, port_map);
2056
2057 mvpp2_prs_hw_write(priv, pe);
2058
Sudip Mukherjee43737472014-11-01 16:59:34 +05302059error:
Marcin Wojtas3f518502014-07-10 16:52:13 -03002060 kfree(pe);
2061
Sudip Mukherjee43737472014-11-01 16:59:34 +05302062 return ret;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002063}
2064
2065/* Get first free double vlan ai number */
2066static int mvpp2_prs_double_vlan_ai_free_get(struct mvpp2 *priv)
2067{
2068 int i;
2069
2070 for (i = 1; i < MVPP2_PRS_DBL_VLANS_MAX; i++) {
2071 if (!priv->prs_double_vlans[i])
2072 return i;
2073 }
2074
2075 return -EINVAL;
2076}
2077
2078/* Search for existing double vlan entry */
2079static struct mvpp2_prs_entry *mvpp2_prs_double_vlan_find(struct mvpp2 *priv,
2080 unsigned short tpid1,
2081 unsigned short tpid2)
2082{
2083 struct mvpp2_prs_entry *pe;
2084 int tid;
2085
2086 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2087 if (!pe)
2088 return NULL;
2089 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2090
2091 /* Go through the all entries with MVPP2_PRS_LU_VLAN */
2092 for (tid = MVPP2_PE_FIRST_FREE_TID;
2093 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
2094 unsigned int ri_mask;
2095 bool match;
2096
2097 if (!priv->prs_shadow[tid].valid ||
2098 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
2099 continue;
2100
2101 pe->index = tid;
2102 mvpp2_prs_hw_read(priv, pe);
2103
2104 match = mvpp2_prs_tcam_data_cmp(pe, 0, swab16(tpid1))
2105 && mvpp2_prs_tcam_data_cmp(pe, 4, swab16(tpid2));
2106
2107 if (!match)
2108 continue;
2109
2110 ri_mask = mvpp2_prs_sram_ri_get(pe) & MVPP2_PRS_RI_VLAN_MASK;
2111 if (ri_mask == MVPP2_PRS_RI_VLAN_DOUBLE)
2112 return pe;
2113 }
2114 kfree(pe);
2115
2116 return NULL;
2117}
2118
2119/* Add or update double vlan entry */
2120static int mvpp2_prs_double_vlan_add(struct mvpp2 *priv, unsigned short tpid1,
2121 unsigned short tpid2,
2122 unsigned int port_map)
2123{
2124 struct mvpp2_prs_entry *pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302125 int tid_aux, tid, ai, ret = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002126
2127 pe = mvpp2_prs_double_vlan_find(priv, tpid1, tpid2);
2128
2129 if (!pe) {
2130 /* Create new tcam entry */
2131 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2132 MVPP2_PE_LAST_FREE_TID);
2133 if (tid < 0)
2134 return tid;
2135
2136 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2137 if (!pe)
2138 return -ENOMEM;
2139
2140 /* Set ai value for new double vlan entry */
2141 ai = mvpp2_prs_double_vlan_ai_free_get(priv);
Sudip Mukherjee43737472014-11-01 16:59:34 +05302142 if (ai < 0) {
2143 ret = ai;
2144 goto error;
2145 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002146
2147 /* Get first single/triple vlan tid */
2148 for (tid_aux = MVPP2_PE_FIRST_FREE_TID;
2149 tid_aux <= MVPP2_PE_LAST_FREE_TID; tid_aux++) {
2150 unsigned int ri_bits;
2151
2152 if (!priv->prs_shadow[tid_aux].valid ||
2153 priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
2154 continue;
2155
2156 pe->index = tid_aux;
2157 mvpp2_prs_hw_read(priv, pe);
2158 ri_bits = mvpp2_prs_sram_ri_get(pe);
2159 ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
2160 if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
2161 ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
2162 break;
2163 }
2164
Sudip Mukherjee43737472014-11-01 16:59:34 +05302165 if (tid >= tid_aux) {
2166 ret = -ERANGE;
2167 goto error;
2168 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002169
2170 memset(pe, 0, sizeof(struct mvpp2_prs_entry));
2171 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2172 pe->index = tid;
2173
2174 priv->prs_double_vlans[ai] = true;
2175
2176 mvpp2_prs_match_etype(pe, 0, tpid1);
2177 mvpp2_prs_match_etype(pe, 4, tpid2);
2178
2179 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_VLAN);
2180 /* Shift 8 bytes - skip 2 vlan tags */
2181 mvpp2_prs_sram_shift_set(pe, 2 * MVPP2_VLAN_TAG_LEN,
2182 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2183 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_DOUBLE,
2184 MVPP2_PRS_RI_VLAN_MASK);
2185 mvpp2_prs_sram_ai_update(pe, ai | MVPP2_PRS_DBL_VLAN_AI_BIT,
2186 MVPP2_PRS_SRAM_AI_MASK);
2187
2188 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_VLAN);
2189 }
2190
2191 /* Update ports' mask */
2192 mvpp2_prs_tcam_port_map_set(pe, port_map);
2193 mvpp2_prs_hw_write(priv, pe);
2194
Sudip Mukherjee43737472014-11-01 16:59:34 +05302195error:
Marcin Wojtas3f518502014-07-10 16:52:13 -03002196 kfree(pe);
Sudip Mukherjee43737472014-11-01 16:59:34 +05302197 return ret;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002198}
2199
2200/* IPv4 header parsing for fragmentation and L4 offset */
2201static int mvpp2_prs_ip4_proto(struct mvpp2 *priv, unsigned short proto,
2202 unsigned int ri, unsigned int ri_mask)
2203{
2204 struct mvpp2_prs_entry pe;
2205 int tid;
2206
2207 if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
2208 (proto != IPPROTO_IGMP))
2209 return -EINVAL;
2210
2211 /* Fragmented packet */
2212 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2213 MVPP2_PE_LAST_FREE_TID);
2214 if (tid < 0)
2215 return tid;
2216
2217 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2218 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
2219 pe.index = tid;
2220
2221 /* Set next lu to IPv4 */
2222 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2223 mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2224 /* Set L4 offset */
2225 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
2226 sizeof(struct iphdr) - 4,
2227 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2228 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
2229 MVPP2_PRS_IPV4_DIP_AI_BIT);
2230 mvpp2_prs_sram_ri_update(&pe, ri | MVPP2_PRS_RI_IP_FRAG_MASK,
2231 ri_mask | MVPP2_PRS_RI_IP_FRAG_MASK);
2232
2233 mvpp2_prs_tcam_data_byte_set(&pe, 5, proto, MVPP2_PRS_TCAM_PROTO_MASK);
2234 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
2235 /* Unmask all ports */
2236 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2237
2238 /* Update shadow table and hw entry */
2239 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2240 mvpp2_prs_hw_write(priv, &pe);
2241
2242 /* Not fragmented packet */
2243 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2244 MVPP2_PE_LAST_FREE_TID);
2245 if (tid < 0)
2246 return tid;
2247
2248 pe.index = tid;
2249 /* Clear ri before updating */
2250 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2251 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2252 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
2253
2254 mvpp2_prs_tcam_data_byte_set(&pe, 2, 0x00, MVPP2_PRS_TCAM_PROTO_MASK_L);
2255 mvpp2_prs_tcam_data_byte_set(&pe, 3, 0x00, MVPP2_PRS_TCAM_PROTO_MASK);
2256
2257 /* Update shadow table and hw entry */
2258 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2259 mvpp2_prs_hw_write(priv, &pe);
2260
2261 return 0;
2262}
2263
2264/* IPv4 L3 multicast or broadcast */
2265static int mvpp2_prs_ip4_cast(struct mvpp2 *priv, unsigned short l3_cast)
2266{
2267 struct mvpp2_prs_entry pe;
2268 int mask, tid;
2269
2270 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2271 MVPP2_PE_LAST_FREE_TID);
2272 if (tid < 0)
2273 return tid;
2274
2275 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2276 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
2277 pe.index = tid;
2278
2279 switch (l3_cast) {
2280 case MVPP2_PRS_L3_MULTI_CAST:
2281 mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV4_MC,
2282 MVPP2_PRS_IPV4_MC_MASK);
2283 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
2284 MVPP2_PRS_RI_L3_ADDR_MASK);
2285 break;
2286 case MVPP2_PRS_L3_BROAD_CAST:
2287 mask = MVPP2_PRS_IPV4_BC_MASK;
2288 mvpp2_prs_tcam_data_byte_set(&pe, 0, mask, mask);
2289 mvpp2_prs_tcam_data_byte_set(&pe, 1, mask, mask);
2290 mvpp2_prs_tcam_data_byte_set(&pe, 2, mask, mask);
2291 mvpp2_prs_tcam_data_byte_set(&pe, 3, mask, mask);
2292 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_BCAST,
2293 MVPP2_PRS_RI_L3_ADDR_MASK);
2294 break;
2295 default:
2296 return -EINVAL;
2297 }
2298
2299 /* Finished: go to flowid generation */
2300 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2301 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2302
2303 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
2304 MVPP2_PRS_IPV4_DIP_AI_BIT);
2305 /* Unmask all ports */
2306 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2307
2308 /* Update shadow table and hw entry */
2309 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2310 mvpp2_prs_hw_write(priv, &pe);
2311
2312 return 0;
2313}
2314
2315/* Set entries for protocols over IPv6 */
2316static int mvpp2_prs_ip6_proto(struct mvpp2 *priv, unsigned short proto,
2317 unsigned int ri, unsigned int ri_mask)
2318{
2319 struct mvpp2_prs_entry pe;
2320 int tid;
2321
2322 if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
2323 (proto != IPPROTO_ICMPV6) && (proto != IPPROTO_IPIP))
2324 return -EINVAL;
2325
2326 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2327 MVPP2_PE_LAST_FREE_TID);
2328 if (tid < 0)
2329 return tid;
2330
2331 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2332 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2333 pe.index = tid;
2334
2335 /* Finished: go to flowid generation */
2336 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2337 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2338 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
2339 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
2340 sizeof(struct ipv6hdr) - 6,
2341 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2342
2343 mvpp2_prs_tcam_data_byte_set(&pe, 0, proto, MVPP2_PRS_TCAM_PROTO_MASK);
2344 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
2345 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2346 /* Unmask all ports */
2347 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2348
2349 /* Write HW */
2350 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
2351 mvpp2_prs_hw_write(priv, &pe);
2352
2353 return 0;
2354}
2355
2356/* IPv6 L3 multicast entry */
2357static int mvpp2_prs_ip6_cast(struct mvpp2 *priv, unsigned short l3_cast)
2358{
2359 struct mvpp2_prs_entry pe;
2360 int tid;
2361
2362 if (l3_cast != MVPP2_PRS_L3_MULTI_CAST)
2363 return -EINVAL;
2364
2365 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2366 MVPP2_PE_LAST_FREE_TID);
2367 if (tid < 0)
2368 return tid;
2369
2370 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2371 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2372 pe.index = tid;
2373
2374 /* Finished: go to flowid generation */
2375 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2376 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
2377 MVPP2_PRS_RI_L3_ADDR_MASK);
2378 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
2379 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2380 /* Shift back to IPv6 NH */
2381 mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2382
2383 mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV6_MC,
2384 MVPP2_PRS_IPV6_MC_MASK);
2385 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2386 /* Unmask all ports */
2387 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2388
2389 /* Update shadow table and hw entry */
2390 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
2391 mvpp2_prs_hw_write(priv, &pe);
2392
2393 return 0;
2394}
2395
2396/* Parser per-port initialization */
2397static void mvpp2_prs_hw_port_init(struct mvpp2 *priv, int port, int lu_first,
2398 int lu_max, int offset)
2399{
2400 u32 val;
2401
2402 /* Set lookup ID */
2403 val = mvpp2_read(priv, MVPP2_PRS_INIT_LOOKUP_REG);
2404 val &= ~MVPP2_PRS_PORT_LU_MASK(port);
2405 val |= MVPP2_PRS_PORT_LU_VAL(port, lu_first);
2406 mvpp2_write(priv, MVPP2_PRS_INIT_LOOKUP_REG, val);
2407
2408 /* Set maximum number of loops for packet received from port */
2409 val = mvpp2_read(priv, MVPP2_PRS_MAX_LOOP_REG(port));
2410 val &= ~MVPP2_PRS_MAX_LOOP_MASK(port);
2411 val |= MVPP2_PRS_MAX_LOOP_VAL(port, lu_max);
2412 mvpp2_write(priv, MVPP2_PRS_MAX_LOOP_REG(port), val);
2413
2414 /* Set initial offset for packet header extraction for the first
2415 * searching loop
2416 */
2417 val = mvpp2_read(priv, MVPP2_PRS_INIT_OFFS_REG(port));
2418 val &= ~MVPP2_PRS_INIT_OFF_MASK(port);
2419 val |= MVPP2_PRS_INIT_OFF_VAL(port, offset);
2420 mvpp2_write(priv, MVPP2_PRS_INIT_OFFS_REG(port), val);
2421}
2422
2423/* Default flow entries initialization for all ports */
2424static void mvpp2_prs_def_flow_init(struct mvpp2 *priv)
2425{
2426 struct mvpp2_prs_entry pe;
2427 int port;
2428
2429 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
2430 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2431 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2432 pe.index = MVPP2_PE_FIRST_DEFAULT_FLOW - port;
2433
2434 /* Mask all ports */
2435 mvpp2_prs_tcam_port_map_set(&pe, 0);
2436
2437 /* Set flow ID*/
2438 mvpp2_prs_sram_ai_update(&pe, port, MVPP2_PRS_FLOW_ID_MASK);
2439 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
2440
2441 /* Update shadow table and hw entry */
2442 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_FLOWS);
2443 mvpp2_prs_hw_write(priv, &pe);
2444 }
2445}
2446
2447/* Set default entry for Marvell Header field */
2448static void mvpp2_prs_mh_init(struct mvpp2 *priv)
2449{
2450 struct mvpp2_prs_entry pe;
2451
2452 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2453
2454 pe.index = MVPP2_PE_MH_DEFAULT;
2455 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MH);
2456 mvpp2_prs_sram_shift_set(&pe, MVPP2_MH_SIZE,
2457 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2458 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_MAC);
2459
2460 /* Unmask all ports */
2461 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2462
2463 /* Update shadow table and hw entry */
2464 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MH);
2465 mvpp2_prs_hw_write(priv, &pe);
2466}
2467
2468/* Set default entires (place holder) for promiscuous, non-promiscuous and
2469 * multicast MAC addresses
2470 */
2471static void mvpp2_prs_mac_init(struct mvpp2 *priv)
2472{
2473 struct mvpp2_prs_entry pe;
2474
2475 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2476
2477 /* Non-promiscuous mode for all ports - DROP unknown packets */
2478 pe.index = MVPP2_PE_MAC_NON_PROMISCUOUS;
2479 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
2480
2481 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
2482 MVPP2_PRS_RI_DROP_MASK);
2483 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2484 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2485
2486 /* Unmask all ports */
2487 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2488
2489 /* Update shadow table and hw entry */
2490 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2491 mvpp2_prs_hw_write(priv, &pe);
2492
2493 /* place holders only - no ports */
2494 mvpp2_prs_mac_drop_all_set(priv, 0, false);
2495 mvpp2_prs_mac_promisc_set(priv, 0, false);
2496 mvpp2_prs_mac_multi_set(priv, MVPP2_PE_MAC_MC_ALL, 0, false);
2497 mvpp2_prs_mac_multi_set(priv, MVPP2_PE_MAC_MC_IP6, 0, false);
2498}
2499
2500/* Set default entries for various types of dsa packets */
2501static void mvpp2_prs_dsa_init(struct mvpp2 *priv)
2502{
2503 struct mvpp2_prs_entry pe;
2504
2505 /* None tagged EDSA entry - place holder */
2506 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
2507 MVPP2_PRS_EDSA);
2508
2509 /* Tagged EDSA entry - place holder */
2510 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
2511
2512 /* None tagged DSA entry - place holder */
2513 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
2514 MVPP2_PRS_DSA);
2515
2516 /* Tagged DSA entry - place holder */
2517 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
2518
2519 /* None tagged EDSA ethertype entry - place holder*/
2520 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
2521 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
2522
2523 /* Tagged EDSA ethertype entry - place holder*/
2524 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
2525 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
2526
2527 /* None tagged DSA ethertype entry */
2528 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
2529 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
2530
2531 /* Tagged DSA ethertype entry */
2532 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
2533 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
2534
2535 /* Set default entry, in case DSA or EDSA tag not found */
2536 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2537 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
2538 pe.index = MVPP2_PE_DSA_DEFAULT;
2539 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2540
2541 /* Shift 0 bytes */
2542 mvpp2_prs_sram_shift_set(&pe, 0, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2543 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2544
2545 /* Clear all sram ai bits for next iteration */
2546 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2547
2548 /* Unmask all ports */
2549 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2550
2551 mvpp2_prs_hw_write(priv, &pe);
2552}
2553
2554/* Match basic ethertypes */
2555static int mvpp2_prs_etype_init(struct mvpp2 *priv)
2556{
2557 struct mvpp2_prs_entry pe;
2558 int tid;
2559
2560 /* Ethertype: PPPoE */
2561 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2562 MVPP2_PE_LAST_FREE_TID);
2563 if (tid < 0)
2564 return tid;
2565
2566 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2567 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2568 pe.index = tid;
2569
2570 mvpp2_prs_match_etype(&pe, 0, ETH_P_PPP_SES);
2571
2572 mvpp2_prs_sram_shift_set(&pe, MVPP2_PPPOE_HDR_SIZE,
2573 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2574 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2575 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_PPPOE_MASK,
2576 MVPP2_PRS_RI_PPPOE_MASK);
2577
2578 /* Update shadow table and hw entry */
2579 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2580 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2581 priv->prs_shadow[pe.index].finish = false;
2582 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_PPPOE_MASK,
2583 MVPP2_PRS_RI_PPPOE_MASK);
2584 mvpp2_prs_hw_write(priv, &pe);
2585
2586 /* Ethertype: ARP */
2587 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2588 MVPP2_PE_LAST_FREE_TID);
2589 if (tid < 0)
2590 return tid;
2591
2592 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2593 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2594 pe.index = tid;
2595
2596 mvpp2_prs_match_etype(&pe, 0, ETH_P_ARP);
2597
2598 /* Generate flow in the next iteration*/
2599 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2600 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2601 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_ARP,
2602 MVPP2_PRS_RI_L3_PROTO_MASK);
2603 /* Set L3 offset */
2604 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2605 MVPP2_ETH_TYPE_LEN,
2606 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2607
2608 /* Update shadow table and hw entry */
2609 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2610 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2611 priv->prs_shadow[pe.index].finish = true;
2612 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_ARP,
2613 MVPP2_PRS_RI_L3_PROTO_MASK);
2614 mvpp2_prs_hw_write(priv, &pe);
2615
2616 /* Ethertype: LBTD */
2617 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2618 MVPP2_PE_LAST_FREE_TID);
2619 if (tid < 0)
2620 return tid;
2621
2622 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2623 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2624 pe.index = tid;
2625
2626 mvpp2_prs_match_etype(&pe, 0, MVPP2_IP_LBDT_TYPE);
2627
2628 /* Generate flow in the next iteration*/
2629 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2630 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2631 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2632 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2633 MVPP2_PRS_RI_CPU_CODE_MASK |
2634 MVPP2_PRS_RI_UDF3_MASK);
2635 /* Set L3 offset */
2636 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2637 MVPP2_ETH_TYPE_LEN,
2638 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2639
2640 /* Update shadow table and hw entry */
2641 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2642 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2643 priv->prs_shadow[pe.index].finish = true;
2644 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2645 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2646 MVPP2_PRS_RI_CPU_CODE_MASK |
2647 MVPP2_PRS_RI_UDF3_MASK);
2648 mvpp2_prs_hw_write(priv, &pe);
2649
2650 /* Ethertype: IPv4 without options */
2651 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2652 MVPP2_PE_LAST_FREE_TID);
2653 if (tid < 0)
2654 return tid;
2655
2656 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2657 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2658 pe.index = tid;
2659
2660 mvpp2_prs_match_etype(&pe, 0, ETH_P_IP);
2661 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2662 MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
2663 MVPP2_PRS_IPV4_HEAD_MASK |
2664 MVPP2_PRS_IPV4_IHL_MASK);
2665
2666 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2667 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
2668 MVPP2_PRS_RI_L3_PROTO_MASK);
2669 /* Skip eth_type + 4 bytes of IP header */
2670 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
2671 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2672 /* Set L3 offset */
2673 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2674 MVPP2_ETH_TYPE_LEN,
2675 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2676
2677 /* Update shadow table and hw entry */
2678 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2679 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2680 priv->prs_shadow[pe.index].finish = false;
2681 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4,
2682 MVPP2_PRS_RI_L3_PROTO_MASK);
2683 mvpp2_prs_hw_write(priv, &pe);
2684
2685 /* Ethertype: IPv4 with options */
2686 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2687 MVPP2_PE_LAST_FREE_TID);
2688 if (tid < 0)
2689 return tid;
2690
2691 pe.index = tid;
2692
2693 /* Clear tcam data before updating */
2694 pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(MVPP2_ETH_TYPE_LEN)] = 0x0;
2695 pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(MVPP2_ETH_TYPE_LEN)] = 0x0;
2696
2697 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2698 MVPP2_PRS_IPV4_HEAD,
2699 MVPP2_PRS_IPV4_HEAD_MASK);
2700
2701 /* Clear ri before updating */
2702 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2703 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2704 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
2705 MVPP2_PRS_RI_L3_PROTO_MASK);
2706
2707 /* Update shadow table and hw entry */
2708 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2709 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2710 priv->prs_shadow[pe.index].finish = false;
2711 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4_OPT,
2712 MVPP2_PRS_RI_L3_PROTO_MASK);
2713 mvpp2_prs_hw_write(priv, &pe);
2714
2715 /* Ethertype: IPv6 without options */
2716 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2717 MVPP2_PE_LAST_FREE_TID);
2718 if (tid < 0)
2719 return tid;
2720
2721 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2722 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2723 pe.index = tid;
2724
2725 mvpp2_prs_match_etype(&pe, 0, ETH_P_IPV6);
2726
2727 /* Skip DIP of IPV6 header */
2728 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 8 +
2729 MVPP2_MAX_L3_ADDR_SIZE,
2730 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2731 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2732 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
2733 MVPP2_PRS_RI_L3_PROTO_MASK);
2734 /* Set L3 offset */
2735 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2736 MVPP2_ETH_TYPE_LEN,
2737 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2738
2739 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2740 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2741 priv->prs_shadow[pe.index].finish = false;
2742 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP6,
2743 MVPP2_PRS_RI_L3_PROTO_MASK);
2744 mvpp2_prs_hw_write(priv, &pe);
2745
2746 /* Default entry for MVPP2_PRS_LU_L2 - Unknown ethtype */
2747 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2748 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2749 pe.index = MVPP2_PE_ETH_TYPE_UN;
2750
2751 /* Unmask all ports */
2752 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2753
2754 /* Generate flow in the next iteration*/
2755 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2756 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2757 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
2758 MVPP2_PRS_RI_L3_PROTO_MASK);
2759 /* Set L3 offset even it's unknown L3 */
2760 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2761 MVPP2_ETH_TYPE_LEN,
2762 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2763
2764 /* Update shadow table and hw entry */
2765 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2766 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2767 priv->prs_shadow[pe.index].finish = true;
2768 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_UN,
2769 MVPP2_PRS_RI_L3_PROTO_MASK);
2770 mvpp2_prs_hw_write(priv, &pe);
2771
2772 return 0;
2773}
2774
2775/* Configure vlan entries and detect up to 2 successive VLAN tags.
2776 * Possible options:
2777 * 0x8100, 0x88A8
2778 * 0x8100, 0x8100
2779 * 0x8100
2780 * 0x88A8
2781 */
2782static int mvpp2_prs_vlan_init(struct platform_device *pdev, struct mvpp2 *priv)
2783{
2784 struct mvpp2_prs_entry pe;
2785 int err;
2786
2787 priv->prs_double_vlans = devm_kcalloc(&pdev->dev, sizeof(bool),
2788 MVPP2_PRS_DBL_VLANS_MAX,
2789 GFP_KERNEL);
2790 if (!priv->prs_double_vlans)
2791 return -ENOMEM;
2792
2793 /* Double VLAN: 0x8100, 0x88A8 */
2794 err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021AD,
2795 MVPP2_PRS_PORT_MASK);
2796 if (err)
2797 return err;
2798
2799 /* Double VLAN: 0x8100, 0x8100 */
2800 err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021Q,
2801 MVPP2_PRS_PORT_MASK);
2802 if (err)
2803 return err;
2804
2805 /* Single VLAN: 0x88a8 */
2806 err = mvpp2_prs_vlan_add(priv, ETH_P_8021AD, MVPP2_PRS_SINGLE_VLAN_AI,
2807 MVPP2_PRS_PORT_MASK);
2808 if (err)
2809 return err;
2810
2811 /* Single VLAN: 0x8100 */
2812 err = mvpp2_prs_vlan_add(priv, ETH_P_8021Q, MVPP2_PRS_SINGLE_VLAN_AI,
2813 MVPP2_PRS_PORT_MASK);
2814 if (err)
2815 return err;
2816
2817 /* Set default double vlan entry */
2818 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2819 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2820 pe.index = MVPP2_PE_VLAN_DBL;
2821
2822 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2823 /* Clear ai for next iterations */
2824 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2825 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_DOUBLE,
2826 MVPP2_PRS_RI_VLAN_MASK);
2827
2828 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_DBL_VLAN_AI_BIT,
2829 MVPP2_PRS_DBL_VLAN_AI_BIT);
2830 /* Unmask all ports */
2831 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2832
2833 /* Update shadow table and hw entry */
2834 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
2835 mvpp2_prs_hw_write(priv, &pe);
2836
2837 /* Set default vlan none entry */
2838 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2839 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2840 pe.index = MVPP2_PE_VLAN_NONE;
2841
2842 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2843 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
2844 MVPP2_PRS_RI_VLAN_MASK);
2845
2846 /* Unmask all ports */
2847 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2848
2849 /* Update shadow table and hw entry */
2850 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
2851 mvpp2_prs_hw_write(priv, &pe);
2852
2853 return 0;
2854}
2855
2856/* Set entries for PPPoE ethertype */
2857static int mvpp2_prs_pppoe_init(struct mvpp2 *priv)
2858{
2859 struct mvpp2_prs_entry pe;
2860 int tid;
2861
2862 /* IPv4 over PPPoE with options */
2863 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2864 MVPP2_PE_LAST_FREE_TID);
2865 if (tid < 0)
2866 return tid;
2867
2868 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2869 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2870 pe.index = tid;
2871
2872 mvpp2_prs_match_etype(&pe, 0, PPP_IP);
2873
2874 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2875 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
2876 MVPP2_PRS_RI_L3_PROTO_MASK);
2877 /* Skip eth_type + 4 bytes of IP header */
2878 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
2879 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2880 /* Set L3 offset */
2881 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2882 MVPP2_ETH_TYPE_LEN,
2883 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2884
2885 /* Update shadow table and hw entry */
2886 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
2887 mvpp2_prs_hw_write(priv, &pe);
2888
2889 /* IPv4 over PPPoE without options */
2890 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2891 MVPP2_PE_LAST_FREE_TID);
2892 if (tid < 0)
2893 return tid;
2894
2895 pe.index = tid;
2896
2897 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2898 MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
2899 MVPP2_PRS_IPV4_HEAD_MASK |
2900 MVPP2_PRS_IPV4_IHL_MASK);
2901
2902 /* Clear ri before updating */
2903 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2904 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2905 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
2906 MVPP2_PRS_RI_L3_PROTO_MASK);
2907
2908 /* Update shadow table and hw entry */
2909 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
2910 mvpp2_prs_hw_write(priv, &pe);
2911
2912 /* IPv6 over PPPoE */
2913 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2914 MVPP2_PE_LAST_FREE_TID);
2915 if (tid < 0)
2916 return tid;
2917
2918 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2919 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2920 pe.index = tid;
2921
2922 mvpp2_prs_match_etype(&pe, 0, PPP_IPV6);
2923
2924 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2925 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
2926 MVPP2_PRS_RI_L3_PROTO_MASK);
2927 /* Skip eth_type + 4 bytes of IPv6 header */
2928 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
2929 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2930 /* Set L3 offset */
2931 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2932 MVPP2_ETH_TYPE_LEN,
2933 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2934
2935 /* Update shadow table and hw entry */
2936 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
2937 mvpp2_prs_hw_write(priv, &pe);
2938
2939 /* Non-IP over PPPoE */
2940 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2941 MVPP2_PE_LAST_FREE_TID);
2942 if (tid < 0)
2943 return tid;
2944
2945 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2946 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2947 pe.index = tid;
2948
2949 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
2950 MVPP2_PRS_RI_L3_PROTO_MASK);
2951
2952 /* Finished: go to flowid generation */
2953 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2954 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2955 /* Set L3 offset even if it's unknown L3 */
2956 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2957 MVPP2_ETH_TYPE_LEN,
2958 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2959
2960 /* Update shadow table and hw entry */
2961 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
2962 mvpp2_prs_hw_write(priv, &pe);
2963
2964 return 0;
2965}
2966
2967/* Initialize entries for IPv4 */
2968static int mvpp2_prs_ip4_init(struct mvpp2 *priv)
2969{
2970 struct mvpp2_prs_entry pe;
2971 int err;
2972
2973 /* Set entries for TCP, UDP and IGMP over IPv4 */
2974 err = mvpp2_prs_ip4_proto(priv, IPPROTO_TCP, MVPP2_PRS_RI_L4_TCP,
2975 MVPP2_PRS_RI_L4_PROTO_MASK);
2976 if (err)
2977 return err;
2978
2979 err = mvpp2_prs_ip4_proto(priv, IPPROTO_UDP, MVPP2_PRS_RI_L4_UDP,
2980 MVPP2_PRS_RI_L4_PROTO_MASK);
2981 if (err)
2982 return err;
2983
2984 err = mvpp2_prs_ip4_proto(priv, IPPROTO_IGMP,
2985 MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2986 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2987 MVPP2_PRS_RI_CPU_CODE_MASK |
2988 MVPP2_PRS_RI_UDF3_MASK);
2989 if (err)
2990 return err;
2991
2992 /* IPv4 Broadcast */
2993 err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_BROAD_CAST);
2994 if (err)
2995 return err;
2996
2997 /* IPv4 Multicast */
2998 err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
2999 if (err)
3000 return err;
3001
3002 /* Default IPv4 entry for unknown protocols */
3003 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3004 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
3005 pe.index = MVPP2_PE_IP4_PROTO_UN;
3006
3007 /* Set next lu to IPv4 */
3008 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
3009 mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3010 /* Set L4 offset */
3011 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
3012 sizeof(struct iphdr) - 4,
3013 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3014 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
3015 MVPP2_PRS_IPV4_DIP_AI_BIT);
3016 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3017 MVPP2_PRS_RI_L4_PROTO_MASK);
3018
3019 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
3020 /* Unmask all ports */
3021 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3022
3023 /* Update shadow table and hw entry */
3024 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3025 mvpp2_prs_hw_write(priv, &pe);
3026
3027 /* Default IPv4 entry for unicast address */
3028 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3029 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
3030 pe.index = MVPP2_PE_IP4_ADDR_UN;
3031
3032 /* Finished: go to flowid generation */
3033 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3034 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3035 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
3036 MVPP2_PRS_RI_L3_ADDR_MASK);
3037
3038 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
3039 MVPP2_PRS_IPV4_DIP_AI_BIT);
3040 /* Unmask all ports */
3041 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3042
3043 /* Update shadow table and hw entry */
3044 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3045 mvpp2_prs_hw_write(priv, &pe);
3046
3047 return 0;
3048}
3049
3050/* Initialize entries for IPv6 */
3051static int mvpp2_prs_ip6_init(struct mvpp2 *priv)
3052{
3053 struct mvpp2_prs_entry pe;
3054 int tid, err;
3055
3056 /* Set entries for TCP, UDP and ICMP over IPv6 */
3057 err = mvpp2_prs_ip6_proto(priv, IPPROTO_TCP,
3058 MVPP2_PRS_RI_L4_TCP,
3059 MVPP2_PRS_RI_L4_PROTO_MASK);
3060 if (err)
3061 return err;
3062
3063 err = mvpp2_prs_ip6_proto(priv, IPPROTO_UDP,
3064 MVPP2_PRS_RI_L4_UDP,
3065 MVPP2_PRS_RI_L4_PROTO_MASK);
3066 if (err)
3067 return err;
3068
3069 err = mvpp2_prs_ip6_proto(priv, IPPROTO_ICMPV6,
3070 MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
3071 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
3072 MVPP2_PRS_RI_CPU_CODE_MASK |
3073 MVPP2_PRS_RI_UDF3_MASK);
3074 if (err)
3075 return err;
3076
3077 /* IPv4 is the last header. This is similar case as 6-TCP or 17-UDP */
3078 /* Result Info: UDF7=1, DS lite */
3079 err = mvpp2_prs_ip6_proto(priv, IPPROTO_IPIP,
3080 MVPP2_PRS_RI_UDF7_IP6_LITE,
3081 MVPP2_PRS_RI_UDF7_MASK);
3082 if (err)
3083 return err;
3084
3085 /* IPv6 multicast */
3086 err = mvpp2_prs_ip6_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
3087 if (err)
3088 return err;
3089
3090 /* Entry for checking hop limit */
3091 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3092 MVPP2_PE_LAST_FREE_TID);
3093 if (tid < 0)
3094 return tid;
3095
3096 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3097 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3098 pe.index = tid;
3099
3100 /* Finished: go to flowid generation */
3101 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3102 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3103 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN |
3104 MVPP2_PRS_RI_DROP_MASK,
3105 MVPP2_PRS_RI_L3_PROTO_MASK |
3106 MVPP2_PRS_RI_DROP_MASK);
3107
3108 mvpp2_prs_tcam_data_byte_set(&pe, 1, 0x00, MVPP2_PRS_IPV6_HOP_MASK);
3109 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3110 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3111
3112 /* Update shadow table and hw entry */
3113 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3114 mvpp2_prs_hw_write(priv, &pe);
3115
3116 /* Default IPv6 entry for unknown protocols */
3117 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3118 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3119 pe.index = MVPP2_PE_IP6_PROTO_UN;
3120
3121 /* Finished: go to flowid generation */
3122 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3123 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3124 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3125 MVPP2_PRS_RI_L4_PROTO_MASK);
3126 /* Set L4 offset relatively to our current place */
3127 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
3128 sizeof(struct ipv6hdr) - 4,
3129 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3130
3131 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3132 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3133 /* Unmask all ports */
3134 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3135
3136 /* Update shadow table and hw entry */
3137 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3138 mvpp2_prs_hw_write(priv, &pe);
3139
3140 /* Default IPv6 entry for unknown ext protocols */
3141 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3142 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3143 pe.index = MVPP2_PE_IP6_EXT_PROTO_UN;
3144
3145 /* Finished: go to flowid generation */
3146 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3147 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3148 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3149 MVPP2_PRS_RI_L4_PROTO_MASK);
3150
3151 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_EXT_AI_BIT,
3152 MVPP2_PRS_IPV6_EXT_AI_BIT);
3153 /* Unmask all ports */
3154 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3155
3156 /* Update shadow table and hw entry */
3157 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3158 mvpp2_prs_hw_write(priv, &pe);
3159
3160 /* Default IPv6 entry for unicast address */
3161 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3162 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3163 pe.index = MVPP2_PE_IP6_ADDR_UN;
3164
3165 /* Finished: go to IPv6 again */
3166 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
3167 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
3168 MVPP2_PRS_RI_L3_ADDR_MASK);
3169 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3170 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3171 /* Shift back to IPV6 NH */
3172 mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3173
3174 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3175 /* Unmask all ports */
3176 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3177
3178 /* Update shadow table and hw entry */
3179 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
3180 mvpp2_prs_hw_write(priv, &pe);
3181
3182 return 0;
3183}
3184
3185/* Parser default initialization */
3186static int mvpp2_prs_default_init(struct platform_device *pdev,
3187 struct mvpp2 *priv)
3188{
3189 int err, index, i;
3190
3191 /* Enable tcam table */
3192 mvpp2_write(priv, MVPP2_PRS_TCAM_CTRL_REG, MVPP2_PRS_TCAM_EN_MASK);
3193
3194 /* Clear all tcam and sram entries */
3195 for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++) {
3196 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
3197 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
3198 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), 0);
3199
3200 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, index);
3201 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
3202 mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), 0);
3203 }
3204
3205 /* Invalidate all tcam entries */
3206 for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++)
3207 mvpp2_prs_hw_inv(priv, index);
3208
3209 priv->prs_shadow = devm_kcalloc(&pdev->dev, MVPP2_PRS_TCAM_SRAM_SIZE,
3210 sizeof(struct mvpp2_prs_shadow),
3211 GFP_KERNEL);
3212 if (!priv->prs_shadow)
3213 return -ENOMEM;
3214
3215 /* Always start from lookup = 0 */
3216 for (index = 0; index < MVPP2_MAX_PORTS; index++)
3217 mvpp2_prs_hw_port_init(priv, index, MVPP2_PRS_LU_MH,
3218 MVPP2_PRS_PORT_LU_MAX, 0);
3219
3220 mvpp2_prs_def_flow_init(priv);
3221
3222 mvpp2_prs_mh_init(priv);
3223
3224 mvpp2_prs_mac_init(priv);
3225
3226 mvpp2_prs_dsa_init(priv);
3227
3228 err = mvpp2_prs_etype_init(priv);
3229 if (err)
3230 return err;
3231
3232 err = mvpp2_prs_vlan_init(pdev, priv);
3233 if (err)
3234 return err;
3235
3236 err = mvpp2_prs_pppoe_init(priv);
3237 if (err)
3238 return err;
3239
3240 err = mvpp2_prs_ip6_init(priv);
3241 if (err)
3242 return err;
3243
3244 err = mvpp2_prs_ip4_init(priv);
3245 if (err)
3246 return err;
3247
3248 return 0;
3249}
3250
3251/* Compare MAC DA with tcam entry data */
3252static bool mvpp2_prs_mac_range_equals(struct mvpp2_prs_entry *pe,
3253 const u8 *da, unsigned char *mask)
3254{
3255 unsigned char tcam_byte, tcam_mask;
3256 int index;
3257
3258 for (index = 0; index < ETH_ALEN; index++) {
3259 mvpp2_prs_tcam_data_byte_get(pe, index, &tcam_byte, &tcam_mask);
3260 if (tcam_mask != mask[index])
3261 return false;
3262
3263 if ((tcam_mask & tcam_byte) != (da[index] & mask[index]))
3264 return false;
3265 }
3266
3267 return true;
3268}
3269
3270/* Find tcam entry with matched pair <MAC DA, port> */
3271static struct mvpp2_prs_entry *
3272mvpp2_prs_mac_da_range_find(struct mvpp2 *priv, int pmap, const u8 *da,
3273 unsigned char *mask, int udf_type)
3274{
3275 struct mvpp2_prs_entry *pe;
3276 int tid;
3277
3278 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
3279 if (!pe)
3280 return NULL;
3281 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
3282
3283 /* Go through the all entires with MVPP2_PRS_LU_MAC */
3284 for (tid = MVPP2_PE_FIRST_FREE_TID;
3285 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
3286 unsigned int entry_pmap;
3287
3288 if (!priv->prs_shadow[tid].valid ||
3289 (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
3290 (priv->prs_shadow[tid].udf != udf_type))
3291 continue;
3292
3293 pe->index = tid;
3294 mvpp2_prs_hw_read(priv, pe);
3295 entry_pmap = mvpp2_prs_tcam_port_map_get(pe);
3296
3297 if (mvpp2_prs_mac_range_equals(pe, da, mask) &&
3298 entry_pmap == pmap)
3299 return pe;
3300 }
3301 kfree(pe);
3302
3303 return NULL;
3304}
3305
3306/* Update parser's mac da entry */
3307static int mvpp2_prs_mac_da_accept(struct mvpp2 *priv, int port,
3308 const u8 *da, bool add)
3309{
3310 struct mvpp2_prs_entry *pe;
3311 unsigned int pmap, len, ri;
3312 unsigned char mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3313 int tid;
3314
3315 /* Scan TCAM and see if entry with this <MAC DA, port> already exist */
3316 pe = mvpp2_prs_mac_da_range_find(priv, (1 << port), da, mask,
3317 MVPP2_PRS_UDF_MAC_DEF);
3318
3319 /* No such entry */
3320 if (!pe) {
3321 if (!add)
3322 return 0;
3323
3324 /* Create new TCAM entry */
3325 /* Find first range mac entry*/
3326 for (tid = MVPP2_PE_FIRST_FREE_TID;
3327 tid <= MVPP2_PE_LAST_FREE_TID; tid++)
3328 if (priv->prs_shadow[tid].valid &&
3329 (priv->prs_shadow[tid].lu == MVPP2_PRS_LU_MAC) &&
3330 (priv->prs_shadow[tid].udf ==
3331 MVPP2_PRS_UDF_MAC_RANGE))
3332 break;
3333
3334 /* Go through the all entries from first to last */
3335 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3336 tid - 1);
3337 if (tid < 0)
3338 return tid;
3339
3340 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
3341 if (!pe)
Amitoj Kaur Chawlac2bb7bc2016-02-04 19:25:26 +05303342 return -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003343 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
3344 pe->index = tid;
3345
3346 /* Mask all ports */
3347 mvpp2_prs_tcam_port_map_set(pe, 0);
3348 }
3349
3350 /* Update port mask */
3351 mvpp2_prs_tcam_port_set(pe, port, add);
3352
3353 /* Invalidate the entry if no ports are left enabled */
3354 pmap = mvpp2_prs_tcam_port_map_get(pe);
3355 if (pmap == 0) {
3356 if (add) {
3357 kfree(pe);
Amitoj Kaur Chawlac2bb7bc2016-02-04 19:25:26 +05303358 return -EINVAL;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003359 }
3360 mvpp2_prs_hw_inv(priv, pe->index);
3361 priv->prs_shadow[pe->index].valid = false;
3362 kfree(pe);
3363 return 0;
3364 }
3365
3366 /* Continue - set next lookup */
3367 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_DSA);
3368
3369 /* Set match on DA */
3370 len = ETH_ALEN;
3371 while (len--)
3372 mvpp2_prs_tcam_data_byte_set(pe, len, da[len], 0xff);
3373
3374 /* Set result info bits */
3375 if (is_broadcast_ether_addr(da))
3376 ri = MVPP2_PRS_RI_L2_BCAST;
3377 else if (is_multicast_ether_addr(da))
3378 ri = MVPP2_PRS_RI_L2_MCAST;
3379 else
3380 ri = MVPP2_PRS_RI_L2_UCAST | MVPP2_PRS_RI_MAC_ME_MASK;
3381
3382 mvpp2_prs_sram_ri_update(pe, ri, MVPP2_PRS_RI_L2_CAST_MASK |
3383 MVPP2_PRS_RI_MAC_ME_MASK);
3384 mvpp2_prs_shadow_ri_set(priv, pe->index, ri, MVPP2_PRS_RI_L2_CAST_MASK |
3385 MVPP2_PRS_RI_MAC_ME_MASK);
3386
3387 /* Shift to ethertype */
3388 mvpp2_prs_sram_shift_set(pe, 2 * ETH_ALEN,
3389 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3390
3391 /* Update shadow table and hw entry */
3392 priv->prs_shadow[pe->index].udf = MVPP2_PRS_UDF_MAC_DEF;
3393 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_MAC);
3394 mvpp2_prs_hw_write(priv, pe);
3395
3396 kfree(pe);
3397
3398 return 0;
3399}
3400
3401static int mvpp2_prs_update_mac_da(struct net_device *dev, const u8 *da)
3402{
3403 struct mvpp2_port *port = netdev_priv(dev);
3404 int err;
3405
3406 /* Remove old parser entry */
3407 err = mvpp2_prs_mac_da_accept(port->priv, port->id, dev->dev_addr,
3408 false);
3409 if (err)
3410 return err;
3411
3412 /* Add new parser entry */
3413 err = mvpp2_prs_mac_da_accept(port->priv, port->id, da, true);
3414 if (err)
3415 return err;
3416
3417 /* Set addr in the device */
3418 ether_addr_copy(dev->dev_addr, da);
3419
3420 return 0;
3421}
3422
3423/* Delete all port's multicast simple (not range) entries */
3424static void mvpp2_prs_mcast_del_all(struct mvpp2 *priv, int port)
3425{
3426 struct mvpp2_prs_entry pe;
3427 int index, tid;
3428
3429 for (tid = MVPP2_PE_FIRST_FREE_TID;
3430 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
3431 unsigned char da[ETH_ALEN], da_mask[ETH_ALEN];
3432
3433 if (!priv->prs_shadow[tid].valid ||
3434 (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
3435 (priv->prs_shadow[tid].udf != MVPP2_PRS_UDF_MAC_DEF))
3436 continue;
3437
3438 /* Only simple mac entries */
3439 pe.index = tid;
3440 mvpp2_prs_hw_read(priv, &pe);
3441
3442 /* Read mac addr from entry */
3443 for (index = 0; index < ETH_ALEN; index++)
3444 mvpp2_prs_tcam_data_byte_get(&pe, index, &da[index],
3445 &da_mask[index]);
3446
3447 if (is_multicast_ether_addr(da) && !is_broadcast_ether_addr(da))
3448 /* Delete this entry */
3449 mvpp2_prs_mac_da_accept(priv, port, da, false);
3450 }
3451}
3452
3453static int mvpp2_prs_tag_mode_set(struct mvpp2 *priv, int port, int type)
3454{
3455 switch (type) {
3456 case MVPP2_TAG_TYPE_EDSA:
3457 /* Add port to EDSA entries */
3458 mvpp2_prs_dsa_tag_set(priv, port, true,
3459 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3460 mvpp2_prs_dsa_tag_set(priv, port, true,
3461 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3462 /* Remove port from DSA entries */
3463 mvpp2_prs_dsa_tag_set(priv, port, false,
3464 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3465 mvpp2_prs_dsa_tag_set(priv, port, false,
3466 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3467 break;
3468
3469 case MVPP2_TAG_TYPE_DSA:
3470 /* Add port to DSA entries */
3471 mvpp2_prs_dsa_tag_set(priv, port, true,
3472 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3473 mvpp2_prs_dsa_tag_set(priv, port, true,
3474 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3475 /* Remove port from EDSA entries */
3476 mvpp2_prs_dsa_tag_set(priv, port, false,
3477 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3478 mvpp2_prs_dsa_tag_set(priv, port, false,
3479 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3480 break;
3481
3482 case MVPP2_TAG_TYPE_MH:
3483 case MVPP2_TAG_TYPE_NONE:
3484 /* Remove port form EDSA and DSA entries */
3485 mvpp2_prs_dsa_tag_set(priv, port, false,
3486 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3487 mvpp2_prs_dsa_tag_set(priv, port, false,
3488 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3489 mvpp2_prs_dsa_tag_set(priv, port, false,
3490 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3491 mvpp2_prs_dsa_tag_set(priv, port, false,
3492 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3493 break;
3494
3495 default:
3496 if ((type < 0) || (type > MVPP2_TAG_TYPE_EDSA))
3497 return -EINVAL;
3498 }
3499
3500 return 0;
3501}
3502
3503/* Set prs flow for the port */
3504static int mvpp2_prs_def_flow(struct mvpp2_port *port)
3505{
3506 struct mvpp2_prs_entry *pe;
3507 int tid;
3508
3509 pe = mvpp2_prs_flow_find(port->priv, port->id);
3510
3511 /* Such entry not exist */
3512 if (!pe) {
3513 /* Go through the all entires from last to first */
3514 tid = mvpp2_prs_tcam_first_free(port->priv,
3515 MVPP2_PE_LAST_FREE_TID,
3516 MVPP2_PE_FIRST_FREE_TID);
3517 if (tid < 0)
3518 return tid;
3519
3520 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
3521 if (!pe)
3522 return -ENOMEM;
3523
3524 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
3525 pe->index = tid;
3526
3527 /* Set flow ID*/
3528 mvpp2_prs_sram_ai_update(pe, port->id, MVPP2_PRS_FLOW_ID_MASK);
3529 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
3530
3531 /* Update shadow table */
3532 mvpp2_prs_shadow_set(port->priv, pe->index, MVPP2_PRS_LU_FLOWS);
3533 }
3534
3535 mvpp2_prs_tcam_port_map_set(pe, (1 << port->id));
3536 mvpp2_prs_hw_write(port->priv, pe);
3537 kfree(pe);
3538
3539 return 0;
3540}
3541
3542/* Classifier configuration routines */
3543
3544/* Update classification flow table registers */
3545static void mvpp2_cls_flow_write(struct mvpp2 *priv,
3546 struct mvpp2_cls_flow_entry *fe)
3547{
3548 mvpp2_write(priv, MVPP2_CLS_FLOW_INDEX_REG, fe->index);
3549 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL0_REG, fe->data[0]);
3550 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL1_REG, fe->data[1]);
3551 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL2_REG, fe->data[2]);
3552}
3553
3554/* Update classification lookup table register */
3555static void mvpp2_cls_lookup_write(struct mvpp2 *priv,
3556 struct mvpp2_cls_lookup_entry *le)
3557{
3558 u32 val;
3559
3560 val = (le->way << MVPP2_CLS_LKP_INDEX_WAY_OFFS) | le->lkpid;
3561 mvpp2_write(priv, MVPP2_CLS_LKP_INDEX_REG, val);
3562 mvpp2_write(priv, MVPP2_CLS_LKP_TBL_REG, le->data);
3563}
3564
3565/* Classifier default initialization */
3566static void mvpp2_cls_init(struct mvpp2 *priv)
3567{
3568 struct mvpp2_cls_lookup_entry le;
3569 struct mvpp2_cls_flow_entry fe;
3570 int index;
3571
3572 /* Enable classifier */
3573 mvpp2_write(priv, MVPP2_CLS_MODE_REG, MVPP2_CLS_MODE_ACTIVE_MASK);
3574
3575 /* Clear classifier flow table */
Arnd Bergmanne8f967c2016-11-24 17:28:12 +01003576 memset(&fe.data, 0, sizeof(fe.data));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003577 for (index = 0; index < MVPP2_CLS_FLOWS_TBL_SIZE; index++) {
3578 fe.index = index;
3579 mvpp2_cls_flow_write(priv, &fe);
3580 }
3581
3582 /* Clear classifier lookup table */
3583 le.data = 0;
3584 for (index = 0; index < MVPP2_CLS_LKP_TBL_SIZE; index++) {
3585 le.lkpid = index;
3586 le.way = 0;
3587 mvpp2_cls_lookup_write(priv, &le);
3588
3589 le.way = 1;
3590 mvpp2_cls_lookup_write(priv, &le);
3591 }
3592}
3593
3594static void mvpp2_cls_port_config(struct mvpp2_port *port)
3595{
3596 struct mvpp2_cls_lookup_entry le;
3597 u32 val;
3598
3599 /* Set way for the port */
3600 val = mvpp2_read(port->priv, MVPP2_CLS_PORT_WAY_REG);
3601 val &= ~MVPP2_CLS_PORT_WAY_MASK(port->id);
3602 mvpp2_write(port->priv, MVPP2_CLS_PORT_WAY_REG, val);
3603
3604 /* Pick the entry to be accessed in lookup ID decoding table
3605 * according to the way and lkpid.
3606 */
3607 le.lkpid = port->id;
3608 le.way = 0;
3609 le.data = 0;
3610
3611 /* Set initial CPU queue for receiving packets */
3612 le.data &= ~MVPP2_CLS_LKP_TBL_RXQ_MASK;
3613 le.data |= port->first_rxq;
3614
3615 /* Disable classification engines */
3616 le.data &= ~MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK;
3617
3618 /* Update lookup ID table entry */
3619 mvpp2_cls_lookup_write(port->priv, &le);
3620}
3621
3622/* Set CPU queue number for oversize packets */
3623static void mvpp2_cls_oversize_rxq_set(struct mvpp2_port *port)
3624{
3625 u32 val;
3626
3627 mvpp2_write(port->priv, MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port->id),
3628 port->first_rxq & MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK);
3629
3630 mvpp2_write(port->priv, MVPP2_CLS_SWFWD_P2HQ_REG(port->id),
3631 (port->first_rxq >> MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS));
3632
3633 val = mvpp2_read(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG);
3634 val |= MVPP2_CLS_SWFWD_PCTRL_MASK(port->id);
3635 mvpp2_write(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG, val);
3636}
3637
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003638static void *mvpp2_frag_alloc(const struct mvpp2_bm_pool *pool)
3639{
3640 if (likely(pool->frag_size <= PAGE_SIZE))
3641 return netdev_alloc_frag(pool->frag_size);
3642 else
3643 return kmalloc(pool->frag_size, GFP_ATOMIC);
3644}
3645
3646static void mvpp2_frag_free(const struct mvpp2_bm_pool *pool, void *data)
3647{
3648 if (likely(pool->frag_size <= PAGE_SIZE))
3649 skb_free_frag(data);
3650 else
3651 kfree(data);
3652}
3653
Marcin Wojtas3f518502014-07-10 16:52:13 -03003654/* Buffer Manager configuration routines */
3655
3656/* Create pool */
3657static int mvpp2_bm_pool_create(struct platform_device *pdev,
3658 struct mvpp2 *priv,
3659 struct mvpp2_bm_pool *bm_pool, int size)
3660{
Marcin Wojtas3f518502014-07-10 16:52:13 -03003661 u32 val;
3662
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003663 /* Number of buffer pointers must be a multiple of 16, as per
3664 * hardware constraints
3665 */
3666 if (!IS_ALIGNED(size, 16))
3667 return -EINVAL;
3668
3669 /* PPv2.1 needs 8 bytes per buffer pointer, PPv2.2 needs 16
3670 * bytes per buffer pointer
3671 */
3672 if (priv->hw_version == MVPP21)
3673 bm_pool->size_bytes = 2 * sizeof(u32) * size;
3674 else
3675 bm_pool->size_bytes = 2 * sizeof(u64) * size;
3676
3677 bm_pool->virt_addr = dma_alloc_coherent(&pdev->dev, bm_pool->size_bytes,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003678 &bm_pool->dma_addr,
Marcin Wojtas3f518502014-07-10 16:52:13 -03003679 GFP_KERNEL);
3680 if (!bm_pool->virt_addr)
3681 return -ENOMEM;
3682
Thomas Petazzonid3158802017-02-21 11:28:13 +01003683 if (!IS_ALIGNED((unsigned long)bm_pool->virt_addr,
3684 MVPP2_BM_POOL_PTR_ALIGN)) {
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003685 dma_free_coherent(&pdev->dev, bm_pool->size_bytes,
3686 bm_pool->virt_addr, bm_pool->dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003687 dev_err(&pdev->dev, "BM pool %d is not %d bytes aligned\n",
3688 bm_pool->id, MVPP2_BM_POOL_PTR_ALIGN);
3689 return -ENOMEM;
3690 }
3691
3692 mvpp2_write(priv, MVPP2_BM_POOL_BASE_REG(bm_pool->id),
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003693 lower_32_bits(bm_pool->dma_addr));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003694 mvpp2_write(priv, MVPP2_BM_POOL_SIZE_REG(bm_pool->id), size);
3695
3696 val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
3697 val |= MVPP2_BM_START_MASK;
3698 mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
3699
3700 bm_pool->type = MVPP2_BM_FREE;
3701 bm_pool->size = size;
3702 bm_pool->pkt_size = 0;
3703 bm_pool->buf_num = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003704
3705 return 0;
3706}
3707
3708/* Set pool buffer size */
3709static void mvpp2_bm_pool_bufsize_set(struct mvpp2 *priv,
3710 struct mvpp2_bm_pool *bm_pool,
3711 int buf_size)
3712{
3713 u32 val;
3714
3715 bm_pool->buf_size = buf_size;
3716
3717 val = ALIGN(buf_size, 1 << MVPP2_POOL_BUF_SIZE_OFFSET);
3718 mvpp2_write(priv, MVPP2_POOL_BUF_SIZE_REG(bm_pool->id), val);
3719}
3720
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003721static void mvpp2_bm_bufs_get_addrs(struct device *dev, struct mvpp2 *priv,
3722 struct mvpp2_bm_pool *bm_pool,
3723 dma_addr_t *dma_addr,
3724 phys_addr_t *phys_addr)
3725{
Thomas Petazzonia7868412017-03-07 16:53:13 +01003726 int cpu = smp_processor_id();
3727
3728 *dma_addr = mvpp2_percpu_read(priv, cpu,
3729 MVPP2_BM_PHY_ALLOC_REG(bm_pool->id));
3730 *phys_addr = mvpp2_percpu_read(priv, cpu, MVPP2_BM_VIRT_ALLOC_REG);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003731
3732 if (priv->hw_version == MVPP22) {
3733 u32 val;
3734 u32 dma_addr_highbits, phys_addr_highbits;
3735
Thomas Petazzonia7868412017-03-07 16:53:13 +01003736 val = mvpp2_percpu_read(priv, cpu, MVPP22_BM_ADDR_HIGH_ALLOC);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003737 dma_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_PHYS_MASK);
3738 phys_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_VIRT_MASK) >>
3739 MVPP22_BM_ADDR_HIGH_VIRT_SHIFT;
3740
3741 if (sizeof(dma_addr_t) == 8)
3742 *dma_addr |= (u64)dma_addr_highbits << 32;
3743
3744 if (sizeof(phys_addr_t) == 8)
3745 *phys_addr |= (u64)phys_addr_highbits << 32;
3746 }
3747}
3748
Ezequiel Garcia7861f122014-07-21 13:48:14 -03003749/* Free all buffers from the pool */
Marcin Wojtas4229d502015-12-03 15:20:50 +01003750static void mvpp2_bm_bufs_free(struct device *dev, struct mvpp2 *priv,
3751 struct mvpp2_bm_pool *bm_pool)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003752{
3753 int i;
3754
Ezequiel Garcia7861f122014-07-21 13:48:14 -03003755 for (i = 0; i < bm_pool->buf_num; i++) {
Thomas Petazzoni20396132017-03-07 16:53:00 +01003756 dma_addr_t buf_dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003757 phys_addr_t buf_phys_addr;
3758 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003759
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003760 mvpp2_bm_bufs_get_addrs(dev, priv, bm_pool,
3761 &buf_dma_addr, &buf_phys_addr);
Marcin Wojtas4229d502015-12-03 15:20:50 +01003762
Thomas Petazzoni20396132017-03-07 16:53:00 +01003763 dma_unmap_single(dev, buf_dma_addr,
Marcin Wojtas4229d502015-12-03 15:20:50 +01003764 bm_pool->buf_size, DMA_FROM_DEVICE);
3765
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003766 data = (void *)phys_to_virt(buf_phys_addr);
3767 if (!data)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003768 break;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003769
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003770 mvpp2_frag_free(bm_pool, data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003771 }
3772
3773 /* Update BM driver with number of buffers removed from pool */
3774 bm_pool->buf_num -= i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003775}
3776
3777/* Cleanup pool */
3778static int mvpp2_bm_pool_destroy(struct platform_device *pdev,
3779 struct mvpp2 *priv,
3780 struct mvpp2_bm_pool *bm_pool)
3781{
Marcin Wojtas3f518502014-07-10 16:52:13 -03003782 u32 val;
3783
Marcin Wojtas4229d502015-12-03 15:20:50 +01003784 mvpp2_bm_bufs_free(&pdev->dev, priv, bm_pool);
Ezequiel Garciad74c96c2014-07-21 13:48:13 -03003785 if (bm_pool->buf_num) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03003786 WARN(1, "cannot free all buffers in pool %d\n", bm_pool->id);
3787 return 0;
3788 }
3789
3790 val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
3791 val |= MVPP2_BM_STOP_MASK;
3792 mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
3793
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003794 dma_free_coherent(&pdev->dev, bm_pool->size_bytes,
Marcin Wojtas3f518502014-07-10 16:52:13 -03003795 bm_pool->virt_addr,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003796 bm_pool->dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003797 return 0;
3798}
3799
3800static int mvpp2_bm_pools_init(struct platform_device *pdev,
3801 struct mvpp2 *priv)
3802{
3803 int i, err, size;
3804 struct mvpp2_bm_pool *bm_pool;
3805
3806 /* Create all pools with maximum size */
3807 size = MVPP2_BM_POOL_SIZE_MAX;
3808 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
3809 bm_pool = &priv->bm_pools[i];
3810 bm_pool->id = i;
3811 err = mvpp2_bm_pool_create(pdev, priv, bm_pool, size);
3812 if (err)
3813 goto err_unroll_pools;
3814 mvpp2_bm_pool_bufsize_set(priv, bm_pool, 0);
3815 }
3816 return 0;
3817
3818err_unroll_pools:
3819 dev_err(&pdev->dev, "failed to create BM pool %d, size %d\n", i, size);
3820 for (i = i - 1; i >= 0; i--)
3821 mvpp2_bm_pool_destroy(pdev, priv, &priv->bm_pools[i]);
3822 return err;
3823}
3824
3825static int mvpp2_bm_init(struct platform_device *pdev, struct mvpp2 *priv)
3826{
3827 int i, err;
3828
3829 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
3830 /* Mask BM all interrupts */
3831 mvpp2_write(priv, MVPP2_BM_INTR_MASK_REG(i), 0);
3832 /* Clear BM cause register */
3833 mvpp2_write(priv, MVPP2_BM_INTR_CAUSE_REG(i), 0);
3834 }
3835
3836 /* Allocate and initialize BM pools */
3837 priv->bm_pools = devm_kcalloc(&pdev->dev, MVPP2_BM_POOLS_NUM,
3838 sizeof(struct mvpp2_bm_pool), GFP_KERNEL);
3839 if (!priv->bm_pools)
3840 return -ENOMEM;
3841
3842 err = mvpp2_bm_pools_init(pdev, priv);
3843 if (err < 0)
3844 return err;
3845 return 0;
3846}
3847
3848/* Attach long pool to rxq */
3849static void mvpp2_rxq_long_pool_set(struct mvpp2_port *port,
3850 int lrxq, int long_pool)
3851{
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003852 u32 val, mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003853 int prxq;
3854
3855 /* Get queue physical ID */
3856 prxq = port->rxqs[lrxq]->id;
3857
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003858 if (port->priv->hw_version == MVPP21)
3859 mask = MVPP21_RXQ_POOL_LONG_MASK;
3860 else
3861 mask = MVPP22_RXQ_POOL_LONG_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003862
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003863 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
3864 val &= ~mask;
3865 val |= (long_pool << MVPP2_RXQ_POOL_LONG_OFFS) & mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003866 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
3867}
3868
3869/* Attach short pool to rxq */
3870static void mvpp2_rxq_short_pool_set(struct mvpp2_port *port,
3871 int lrxq, int short_pool)
3872{
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003873 u32 val, mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003874 int prxq;
3875
3876 /* Get queue physical ID */
3877 prxq = port->rxqs[lrxq]->id;
3878
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003879 if (port->priv->hw_version == MVPP21)
3880 mask = MVPP21_RXQ_POOL_SHORT_MASK;
3881 else
3882 mask = MVPP22_RXQ_POOL_SHORT_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003883
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003884 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
3885 val &= ~mask;
3886 val |= (short_pool << MVPP2_RXQ_POOL_SHORT_OFFS) & mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003887 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
3888}
3889
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003890static void *mvpp2_buf_alloc(struct mvpp2_port *port,
3891 struct mvpp2_bm_pool *bm_pool,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003892 dma_addr_t *buf_dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003893 phys_addr_t *buf_phys_addr,
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003894 gfp_t gfp_mask)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003895{
Thomas Petazzoni20396132017-03-07 16:53:00 +01003896 dma_addr_t dma_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003897 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003898
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003899 data = mvpp2_frag_alloc(bm_pool);
3900 if (!data)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003901 return NULL;
3902
Thomas Petazzoni20396132017-03-07 16:53:00 +01003903 dma_addr = dma_map_single(port->dev->dev.parent, data,
3904 MVPP2_RX_BUF_SIZE(bm_pool->pkt_size),
3905 DMA_FROM_DEVICE);
3906 if (unlikely(dma_mapping_error(port->dev->dev.parent, dma_addr))) {
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003907 mvpp2_frag_free(bm_pool, data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003908 return NULL;
3909 }
Thomas Petazzoni20396132017-03-07 16:53:00 +01003910 *buf_dma_addr = dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003911 *buf_phys_addr = virt_to_phys(data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003912
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003913 return data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003914}
3915
3916/* Set pool number in a BM cookie */
3917static inline u32 mvpp2_bm_cookie_pool_set(u32 cookie, int pool)
3918{
3919 u32 bm;
3920
3921 bm = cookie & ~(0xFF << MVPP2_BM_COOKIE_POOL_OFFS);
3922 bm |= ((pool & 0xFF) << MVPP2_BM_COOKIE_POOL_OFFS);
3923
3924 return bm;
3925}
3926
3927/* Get pool number from a BM cookie */
Thomas Petazzonid3158802017-02-21 11:28:13 +01003928static inline int mvpp2_bm_cookie_pool_get(unsigned long cookie)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003929{
3930 return (cookie >> MVPP2_BM_COOKIE_POOL_OFFS) & 0xFF;
3931}
3932
3933/* Release buffer to BM */
3934static inline void mvpp2_bm_pool_put(struct mvpp2_port *port, int pool,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003935 dma_addr_t buf_dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003936 phys_addr_t buf_phys_addr)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003937{
Thomas Petazzonia7868412017-03-07 16:53:13 +01003938 int cpu = smp_processor_id();
3939
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003940 if (port->priv->hw_version == MVPP22) {
3941 u32 val = 0;
3942
3943 if (sizeof(dma_addr_t) == 8)
3944 val |= upper_32_bits(buf_dma_addr) &
3945 MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK;
3946
3947 if (sizeof(phys_addr_t) == 8)
3948 val |= (upper_32_bits(buf_phys_addr)
3949 << MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT) &
3950 MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK;
3951
Thomas Petazzonia7868412017-03-07 16:53:13 +01003952 mvpp2_percpu_write(port->priv, cpu,
3953 MVPP22_BM_ADDR_HIGH_RLS_REG, val);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003954 }
3955
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003956 /* MVPP2_BM_VIRT_RLS_REG is not interpreted by HW, and simply
3957 * returned in the "cookie" field of the RX
3958 * descriptor. Instead of storing the virtual address, we
3959 * store the physical address
3960 */
Thomas Petazzonia7868412017-03-07 16:53:13 +01003961 mvpp2_percpu_write(port->priv, cpu,
3962 MVPP2_BM_VIRT_RLS_REG, buf_phys_addr);
3963 mvpp2_percpu_write(port->priv, cpu,
3964 MVPP2_BM_PHY_RLS_REG(pool), buf_dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003965}
3966
Marcin Wojtas3f518502014-07-10 16:52:13 -03003967/* Refill BM pool */
3968static void mvpp2_pool_refill(struct mvpp2_port *port, u32 bm,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003969 dma_addr_t dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003970 phys_addr_t phys_addr)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003971{
3972 int pool = mvpp2_bm_cookie_pool_get(bm);
3973
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003974 mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003975}
3976
3977/* Allocate buffers for the pool */
3978static int mvpp2_bm_bufs_add(struct mvpp2_port *port,
3979 struct mvpp2_bm_pool *bm_pool, int buf_num)
3980{
Marcin Wojtas3f518502014-07-10 16:52:13 -03003981 int i, buf_size, total_size;
Thomas Petazzoni20396132017-03-07 16:53:00 +01003982 dma_addr_t dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003983 phys_addr_t phys_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003984 void *buf;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003985
3986 buf_size = MVPP2_RX_BUF_SIZE(bm_pool->pkt_size);
3987 total_size = MVPP2_RX_TOTAL_SIZE(buf_size);
3988
3989 if (buf_num < 0 ||
3990 (buf_num + bm_pool->buf_num > bm_pool->size)) {
3991 netdev_err(port->dev,
3992 "cannot allocate %d buffers for pool %d\n",
3993 buf_num, bm_pool->id);
3994 return 0;
3995 }
3996
Marcin Wojtas3f518502014-07-10 16:52:13 -03003997 for (i = 0; i < buf_num; i++) {
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003998 buf = mvpp2_buf_alloc(port, bm_pool, &dma_addr,
3999 &phys_addr, GFP_KERNEL);
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004000 if (!buf)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004001 break;
4002
Thomas Petazzoni20396132017-03-07 16:53:00 +01004003 mvpp2_bm_pool_put(port, bm_pool->id, dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004004 phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004005 }
4006
4007 /* Update BM driver with number of buffers added to pool */
4008 bm_pool->buf_num += i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004009
4010 netdev_dbg(port->dev,
4011 "%s pool %d: pkt_size=%4d, buf_size=%4d, total_size=%4d\n",
4012 bm_pool->type == MVPP2_BM_SWF_SHORT ? "short" : " long",
4013 bm_pool->id, bm_pool->pkt_size, buf_size, total_size);
4014
4015 netdev_dbg(port->dev,
4016 "%s pool %d: %d of %d buffers added\n",
4017 bm_pool->type == MVPP2_BM_SWF_SHORT ? "short" : " long",
4018 bm_pool->id, i, buf_num);
4019 return i;
4020}
4021
4022/* Notify the driver that BM pool is being used as specific type and return the
4023 * pool pointer on success
4024 */
4025static struct mvpp2_bm_pool *
4026mvpp2_bm_pool_use(struct mvpp2_port *port, int pool, enum mvpp2_bm_type type,
4027 int pkt_size)
4028{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004029 struct mvpp2_bm_pool *new_pool = &port->priv->bm_pools[pool];
4030 int num;
4031
4032 if (new_pool->type != MVPP2_BM_FREE && new_pool->type != type) {
4033 netdev_err(port->dev, "mixing pool types is forbidden\n");
4034 return NULL;
4035 }
4036
Marcin Wojtas3f518502014-07-10 16:52:13 -03004037 if (new_pool->type == MVPP2_BM_FREE)
4038 new_pool->type = type;
4039
4040 /* Allocate buffers in case BM pool is used as long pool, but packet
4041 * size doesn't match MTU or BM pool hasn't being used yet
4042 */
4043 if (((type == MVPP2_BM_SWF_LONG) && (pkt_size > new_pool->pkt_size)) ||
4044 (new_pool->pkt_size == 0)) {
4045 int pkts_num;
4046
4047 /* Set default buffer number or free all the buffers in case
4048 * the pool is not empty
4049 */
4050 pkts_num = new_pool->buf_num;
4051 if (pkts_num == 0)
4052 pkts_num = type == MVPP2_BM_SWF_LONG ?
4053 MVPP2_BM_LONG_BUF_NUM :
4054 MVPP2_BM_SHORT_BUF_NUM;
4055 else
Marcin Wojtas4229d502015-12-03 15:20:50 +01004056 mvpp2_bm_bufs_free(port->dev->dev.parent,
4057 port->priv, new_pool);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004058
4059 new_pool->pkt_size = pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004060 new_pool->frag_size =
4061 SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
4062 MVPP2_SKB_SHINFO_SIZE;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004063
4064 /* Allocate buffers for this pool */
4065 num = mvpp2_bm_bufs_add(port, new_pool, pkts_num);
4066 if (num != pkts_num) {
4067 WARN(1, "pool %d: %d of %d allocated\n",
4068 new_pool->id, num, pkts_num);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004069 return NULL;
4070 }
4071 }
4072
4073 mvpp2_bm_pool_bufsize_set(port->priv, new_pool,
4074 MVPP2_RX_BUF_SIZE(new_pool->pkt_size));
4075
Marcin Wojtas3f518502014-07-10 16:52:13 -03004076 return new_pool;
4077}
4078
4079/* Initialize pools for swf */
4080static int mvpp2_swf_bm_pool_init(struct mvpp2_port *port)
4081{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004082 int rxq;
4083
4084 if (!port->pool_long) {
4085 port->pool_long =
4086 mvpp2_bm_pool_use(port, MVPP2_BM_SWF_LONG_POOL(port->id),
4087 MVPP2_BM_SWF_LONG,
4088 port->pkt_size);
4089 if (!port->pool_long)
4090 return -ENOMEM;
4091
Marcin Wojtas3f518502014-07-10 16:52:13 -03004092 port->pool_long->port_map |= (1 << port->id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004093
4094 for (rxq = 0; rxq < rxq_number; rxq++)
4095 mvpp2_rxq_long_pool_set(port, rxq, port->pool_long->id);
4096 }
4097
4098 if (!port->pool_short) {
4099 port->pool_short =
4100 mvpp2_bm_pool_use(port, MVPP2_BM_SWF_SHORT_POOL,
4101 MVPP2_BM_SWF_SHORT,
4102 MVPP2_BM_SHORT_PKT_SIZE);
4103 if (!port->pool_short)
4104 return -ENOMEM;
4105
Marcin Wojtas3f518502014-07-10 16:52:13 -03004106 port->pool_short->port_map |= (1 << port->id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004107
4108 for (rxq = 0; rxq < rxq_number; rxq++)
4109 mvpp2_rxq_short_pool_set(port, rxq,
4110 port->pool_short->id);
4111 }
4112
4113 return 0;
4114}
4115
4116static int mvpp2_bm_update_mtu(struct net_device *dev, int mtu)
4117{
4118 struct mvpp2_port *port = netdev_priv(dev);
4119 struct mvpp2_bm_pool *port_pool = port->pool_long;
4120 int num, pkts_num = port_pool->buf_num;
4121 int pkt_size = MVPP2_RX_PKT_SIZE(mtu);
4122
4123 /* Update BM pool with new buffer size */
Marcin Wojtas4229d502015-12-03 15:20:50 +01004124 mvpp2_bm_bufs_free(dev->dev.parent, port->priv, port_pool);
Ezequiel Garciad74c96c2014-07-21 13:48:13 -03004125 if (port_pool->buf_num) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004126 WARN(1, "cannot free all buffers in pool %d\n", port_pool->id);
4127 return -EIO;
4128 }
4129
4130 port_pool->pkt_size = pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004131 port_pool->frag_size = SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
4132 MVPP2_SKB_SHINFO_SIZE;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004133 num = mvpp2_bm_bufs_add(port, port_pool, pkts_num);
4134 if (num != pkts_num) {
4135 WARN(1, "pool %d: %d of %d allocated\n",
4136 port_pool->id, num, pkts_num);
4137 return -EIO;
4138 }
4139
4140 mvpp2_bm_pool_bufsize_set(port->priv, port_pool,
4141 MVPP2_RX_BUF_SIZE(port_pool->pkt_size));
4142 dev->mtu = mtu;
4143 netdev_update_features(dev);
4144 return 0;
4145}
4146
4147static inline void mvpp2_interrupts_enable(struct mvpp2_port *port)
4148{
4149 int cpu, cpu_mask = 0;
4150
4151 for_each_present_cpu(cpu)
4152 cpu_mask |= 1 << cpu;
4153 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
4154 MVPP2_ISR_ENABLE_INTERRUPT(cpu_mask));
4155}
4156
4157static inline void mvpp2_interrupts_disable(struct mvpp2_port *port)
4158{
4159 int cpu, cpu_mask = 0;
4160
4161 for_each_present_cpu(cpu)
4162 cpu_mask |= 1 << cpu;
4163 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
4164 MVPP2_ISR_DISABLE_INTERRUPT(cpu_mask));
4165}
4166
4167/* Mask the current CPU's Rx/Tx interrupts */
4168static void mvpp2_interrupts_mask(void *arg)
4169{
4170 struct mvpp2_port *port = arg;
4171
Thomas Petazzonia7868412017-03-07 16:53:13 +01004172 mvpp2_percpu_write(port->priv, smp_processor_id(),
4173 MVPP2_ISR_RX_TX_MASK_REG(port->id), 0);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004174}
4175
4176/* Unmask the current CPU's Rx/Tx interrupts */
4177static void mvpp2_interrupts_unmask(void *arg)
4178{
4179 struct mvpp2_port *port = arg;
4180
Thomas Petazzonia7868412017-03-07 16:53:13 +01004181 mvpp2_percpu_write(port->priv, smp_processor_id(),
4182 MVPP2_ISR_RX_TX_MASK_REG(port->id),
4183 (MVPP2_CAUSE_MISC_SUM_MASK |
4184 MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004185}
4186
4187/* Port configuration routines */
4188
Thomas Petazzoni26975822017-03-07 16:53:14 +01004189static void mvpp22_port_mii_set(struct mvpp2_port *port)
4190{
4191 u32 val;
4192
4193 return;
4194
4195 /* Only GOP port 0 has an XLG MAC */
4196 if (port->gop_id == 0) {
4197 val = readl(port->base + MVPP22_XLG_CTRL3_REG);
4198 val &= ~MVPP22_XLG_CTRL3_MACMODESELECT_MASK;
4199 val |= MVPP22_XLG_CTRL3_MACMODESELECT_GMAC;
4200 writel(val, port->base + MVPP22_XLG_CTRL3_REG);
4201 }
4202
4203 val = readl(port->base + MVPP22_GMAC_CTRL_4_REG);
4204 if (port->phy_interface == PHY_INTERFACE_MODE_RGMII)
4205 val |= MVPP22_CTRL4_EXT_PIN_GMII_SEL;
4206 else
4207 val &= ~MVPP22_CTRL4_EXT_PIN_GMII_SEL;
4208 val &= ~MVPP22_CTRL4_DP_CLK_SEL;
4209 val |= MVPP22_CTRL4_SYNC_BYPASS;
4210 val |= MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
4211 writel(val, port->base + MVPP22_GMAC_CTRL_4_REG);
4212}
4213
Marcin Wojtas3f518502014-07-10 16:52:13 -03004214static void mvpp2_port_mii_set(struct mvpp2_port *port)
4215{
Marcin Wojtas08a23752014-07-21 13:48:12 -03004216 u32 val;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004217
Thomas Petazzoni26975822017-03-07 16:53:14 +01004218 if (port->priv->hw_version == MVPP22)
4219 mvpp22_port_mii_set(port);
4220
Marcin Wojtas08a23752014-07-21 13:48:12 -03004221 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004222
Marcin Wojtas08a23752014-07-21 13:48:12 -03004223 switch (port->phy_interface) {
4224 case PHY_INTERFACE_MODE_SGMII:
4225 val |= MVPP2_GMAC_INBAND_AN_MASK;
4226 break;
4227 case PHY_INTERFACE_MODE_RGMII:
4228 val |= MVPP2_GMAC_PORT_RGMII_MASK;
4229 default:
4230 val &= ~MVPP2_GMAC_PCS_ENABLE_MASK;
4231 }
4232
4233 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4234}
4235
4236static void mvpp2_port_fc_adv_enable(struct mvpp2_port *port)
4237{
4238 u32 val;
4239
4240 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4241 val |= MVPP2_GMAC_FC_ADV_EN;
4242 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004243}
4244
4245static void mvpp2_port_enable(struct mvpp2_port *port)
4246{
4247 u32 val;
4248
4249 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4250 val |= MVPP2_GMAC_PORT_EN_MASK;
4251 val |= MVPP2_GMAC_MIB_CNTR_EN_MASK;
4252 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4253}
4254
4255static void mvpp2_port_disable(struct mvpp2_port *port)
4256{
4257 u32 val;
4258
4259 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4260 val &= ~(MVPP2_GMAC_PORT_EN_MASK);
4261 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4262}
4263
4264/* Set IEEE 802.3x Flow Control Xon Packet Transmission Mode */
4265static void mvpp2_port_periodic_xon_disable(struct mvpp2_port *port)
4266{
4267 u32 val;
4268
4269 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG) &
4270 ~MVPP2_GMAC_PERIODIC_XON_EN_MASK;
4271 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
4272}
4273
4274/* Configure loopback port */
4275static void mvpp2_port_loopback_set(struct mvpp2_port *port)
4276{
4277 u32 val;
4278
4279 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
4280
4281 if (port->speed == 1000)
4282 val |= MVPP2_GMAC_GMII_LB_EN_MASK;
4283 else
4284 val &= ~MVPP2_GMAC_GMII_LB_EN_MASK;
4285
4286 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
4287 val |= MVPP2_GMAC_PCS_LB_EN_MASK;
4288 else
4289 val &= ~MVPP2_GMAC_PCS_LB_EN_MASK;
4290
4291 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
4292}
4293
4294static void mvpp2_port_reset(struct mvpp2_port *port)
4295{
4296 u32 val;
4297
4298 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
4299 ~MVPP2_GMAC_PORT_RESET_MASK;
4300 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4301
4302 while (readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
4303 MVPP2_GMAC_PORT_RESET_MASK)
4304 continue;
4305}
4306
4307/* Change maximum receive size of the port */
4308static inline void mvpp2_gmac_max_rx_size_set(struct mvpp2_port *port)
4309{
4310 u32 val;
4311
4312 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4313 val &= ~MVPP2_GMAC_MAX_RX_SIZE_MASK;
4314 val |= (((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
4315 MVPP2_GMAC_MAX_RX_SIZE_OFFS);
4316 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4317}
4318
4319/* Set defaults to the MVPP2 port */
4320static void mvpp2_defaults_set(struct mvpp2_port *port)
4321{
4322 int tx_port_num, val, queue, ptxq, lrxq;
4323
Thomas Petazzoni3d9017d2017-03-07 16:53:11 +01004324 if (port->priv->hw_version == MVPP21) {
4325 /* Configure port to loopback if needed */
4326 if (port->flags & MVPP2_F_LOOPBACK)
4327 mvpp2_port_loopback_set(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004328
Thomas Petazzoni3d9017d2017-03-07 16:53:11 +01004329 /* Update TX FIFO MIN Threshold */
4330 val = readl(port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
4331 val &= ~MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK;
4332 /* Min. TX threshold must be less than minimal packet length */
4333 val |= MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(64 - 4 - 2);
4334 writel(val, port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
4335 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004336
4337 /* Disable Legacy WRR, Disable EJP, Release from reset */
4338 tx_port_num = mvpp2_egress_port(port);
4339 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG,
4340 tx_port_num);
4341 mvpp2_write(port->priv, MVPP2_TXP_SCHED_CMD_1_REG, 0);
4342
4343 /* Close bandwidth for all queues */
4344 for (queue = 0; queue < MVPP2_MAX_TXQ; queue++) {
4345 ptxq = mvpp2_txq_phys(port->id, queue);
4346 mvpp2_write(port->priv,
4347 MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(ptxq), 0);
4348 }
4349
4350 /* Set refill period to 1 usec, refill tokens
4351 * and bucket size to maximum
4352 */
4353 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PERIOD_REG,
4354 port->priv->tclk / USEC_PER_SEC);
4355 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_REFILL_REG);
4356 val &= ~MVPP2_TXP_REFILL_PERIOD_ALL_MASK;
4357 val |= MVPP2_TXP_REFILL_PERIOD_MASK(1);
4358 val |= MVPP2_TXP_REFILL_TOKENS_ALL_MASK;
4359 mvpp2_write(port->priv, MVPP2_TXP_SCHED_REFILL_REG, val);
4360 val = MVPP2_TXP_TOKEN_SIZE_MAX;
4361 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
4362
4363 /* Set MaximumLowLatencyPacketSize value to 256 */
4364 mvpp2_write(port->priv, MVPP2_RX_CTRL_REG(port->id),
4365 MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK |
4366 MVPP2_RX_LOW_LATENCY_PKT_SIZE(256));
4367
4368 /* Enable Rx cache snoop */
4369 for (lrxq = 0; lrxq < rxq_number; lrxq++) {
4370 queue = port->rxqs[lrxq]->id;
4371 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
4372 val |= MVPP2_SNOOP_PKT_SIZE_MASK |
4373 MVPP2_SNOOP_BUF_HDR_MASK;
4374 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
4375 }
4376
4377 /* At default, mask all interrupts to all present cpus */
4378 mvpp2_interrupts_disable(port);
4379}
4380
4381/* Enable/disable receiving packets */
4382static void mvpp2_ingress_enable(struct mvpp2_port *port)
4383{
4384 u32 val;
4385 int lrxq, queue;
4386
4387 for (lrxq = 0; lrxq < rxq_number; lrxq++) {
4388 queue = port->rxqs[lrxq]->id;
4389 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
4390 val &= ~MVPP2_RXQ_DISABLE_MASK;
4391 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
4392 }
4393}
4394
4395static void mvpp2_ingress_disable(struct mvpp2_port *port)
4396{
4397 u32 val;
4398 int lrxq, queue;
4399
4400 for (lrxq = 0; lrxq < rxq_number; lrxq++) {
4401 queue = port->rxqs[lrxq]->id;
4402 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
4403 val |= MVPP2_RXQ_DISABLE_MASK;
4404 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
4405 }
4406}
4407
4408/* Enable transmit via physical egress queue
4409 * - HW starts take descriptors from DRAM
4410 */
4411static void mvpp2_egress_enable(struct mvpp2_port *port)
4412{
4413 u32 qmap;
4414 int queue;
4415 int tx_port_num = mvpp2_egress_port(port);
4416
4417 /* Enable all initialized TXs. */
4418 qmap = 0;
4419 for (queue = 0; queue < txq_number; queue++) {
4420 struct mvpp2_tx_queue *txq = port->txqs[queue];
4421
4422 if (txq->descs != NULL)
4423 qmap |= (1 << queue);
4424 }
4425
4426 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
4427 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG, qmap);
4428}
4429
4430/* Disable transmit via physical egress queue
4431 * - HW doesn't take descriptors from DRAM
4432 */
4433static void mvpp2_egress_disable(struct mvpp2_port *port)
4434{
4435 u32 reg_data;
4436 int delay;
4437 int tx_port_num = mvpp2_egress_port(port);
4438
4439 /* Issue stop command for active channels only */
4440 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
4441 reg_data = (mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG)) &
4442 MVPP2_TXP_SCHED_ENQ_MASK;
4443 if (reg_data != 0)
4444 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG,
4445 (reg_data << MVPP2_TXP_SCHED_DISQ_OFFSET));
4446
4447 /* Wait for all Tx activity to terminate. */
4448 delay = 0;
4449 do {
4450 if (delay >= MVPP2_TX_DISABLE_TIMEOUT_MSEC) {
4451 netdev_warn(port->dev,
4452 "Tx stop timed out, status=0x%08x\n",
4453 reg_data);
4454 break;
4455 }
4456 mdelay(1);
4457 delay++;
4458
4459 /* Check port TX Command register that all
4460 * Tx queues are stopped
4461 */
4462 reg_data = mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG);
4463 } while (reg_data & MVPP2_TXP_SCHED_ENQ_MASK);
4464}
4465
4466/* Rx descriptors helper methods */
4467
4468/* Get number of Rx descriptors occupied by received packets */
4469static inline int
4470mvpp2_rxq_received(struct mvpp2_port *port, int rxq_id)
4471{
4472 u32 val = mvpp2_read(port->priv, MVPP2_RXQ_STATUS_REG(rxq_id));
4473
4474 return val & MVPP2_RXQ_OCCUPIED_MASK;
4475}
4476
4477/* Update Rx queue status with the number of occupied and available
4478 * Rx descriptor slots.
4479 */
4480static inline void
4481mvpp2_rxq_status_update(struct mvpp2_port *port, int rxq_id,
4482 int used_count, int free_count)
4483{
4484 /* Decrement the number of used descriptors and increment count
4485 * increment the number of free descriptors.
4486 */
4487 u32 val = used_count | (free_count << MVPP2_RXQ_NUM_NEW_OFFSET);
4488
4489 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_UPDATE_REG(rxq_id), val);
4490}
4491
4492/* Get pointer to next RX descriptor to be processed by SW */
4493static inline struct mvpp2_rx_desc *
4494mvpp2_rxq_next_desc_get(struct mvpp2_rx_queue *rxq)
4495{
4496 int rx_desc = rxq->next_desc_to_proc;
4497
4498 rxq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(rxq, rx_desc);
4499 prefetch(rxq->descs + rxq->next_desc_to_proc);
4500 return rxq->descs + rx_desc;
4501}
4502
4503/* Set rx queue offset */
4504static void mvpp2_rxq_offset_set(struct mvpp2_port *port,
4505 int prxq, int offset)
4506{
4507 u32 val;
4508
4509 /* Convert offset from bytes to units of 32 bytes */
4510 offset = offset >> 5;
4511
4512 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
4513 val &= ~MVPP2_RXQ_PACKET_OFFSET_MASK;
4514
4515 /* Offset is in */
4516 val |= ((offset << MVPP2_RXQ_PACKET_OFFSET_OFFS) &
4517 MVPP2_RXQ_PACKET_OFFSET_MASK);
4518
4519 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
4520}
4521
4522/* Obtain BM cookie information from descriptor */
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01004523static u32 mvpp2_bm_cookie_build(struct mvpp2_port *port,
4524 struct mvpp2_rx_desc *rx_desc)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004525{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004526 int cpu = smp_processor_id();
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01004527 int pool;
4528
4529 pool = (mvpp2_rxdesc_status_get(port, rx_desc) &
4530 MVPP2_RXD_BM_POOL_ID_MASK) >>
4531 MVPP2_RXD_BM_POOL_ID_OFFS;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004532
4533 return ((pool & 0xFF) << MVPP2_BM_COOKIE_POOL_OFFS) |
4534 ((cpu & 0xFF) << MVPP2_BM_COOKIE_CPU_OFFS);
4535}
4536
4537/* Tx descriptors helper methods */
4538
Marcin Wojtas3f518502014-07-10 16:52:13 -03004539/* Get pointer to next Tx descriptor to be processed (send) by HW */
4540static struct mvpp2_tx_desc *
4541mvpp2_txq_next_desc_get(struct mvpp2_tx_queue *txq)
4542{
4543 int tx_desc = txq->next_desc_to_proc;
4544
4545 txq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(txq, tx_desc);
4546 return txq->descs + tx_desc;
4547}
4548
4549/* Update HW with number of aggregated Tx descriptors to be sent */
4550static void mvpp2_aggr_txq_pend_desc_add(struct mvpp2_port *port, int pending)
4551{
4552 /* aggregated access - relevant TXQ number is written in TX desc */
Thomas Petazzonia7868412017-03-07 16:53:13 +01004553 mvpp2_percpu_write(port->priv, smp_processor_id(),
4554 MVPP2_AGGR_TXQ_UPDATE_REG, pending);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004555}
4556
4557
4558/* Check if there are enough free descriptors in aggregated txq.
4559 * If not, update the number of occupied descriptors and repeat the check.
4560 */
4561static int mvpp2_aggr_desc_num_check(struct mvpp2 *priv,
4562 struct mvpp2_tx_queue *aggr_txq, int num)
4563{
4564 if ((aggr_txq->count + num) > aggr_txq->size) {
4565 /* Update number of occupied aggregated Tx descriptors */
4566 int cpu = smp_processor_id();
4567 u32 val = mvpp2_read(priv, MVPP2_AGGR_TXQ_STATUS_REG(cpu));
4568
4569 aggr_txq->count = val & MVPP2_AGGR_TXQ_PENDING_MASK;
4570 }
4571
4572 if ((aggr_txq->count + num) > aggr_txq->size)
4573 return -ENOMEM;
4574
4575 return 0;
4576}
4577
4578/* Reserved Tx descriptors allocation request */
4579static int mvpp2_txq_alloc_reserved_desc(struct mvpp2 *priv,
4580 struct mvpp2_tx_queue *txq, int num)
4581{
4582 u32 val;
Thomas Petazzonia7868412017-03-07 16:53:13 +01004583 int cpu = smp_processor_id();
Marcin Wojtas3f518502014-07-10 16:52:13 -03004584
4585 val = (txq->id << MVPP2_TXQ_RSVD_REQ_Q_OFFSET) | num;
Thomas Petazzonia7868412017-03-07 16:53:13 +01004586 mvpp2_percpu_write(priv, cpu, MVPP2_TXQ_RSVD_REQ_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004587
Thomas Petazzonia7868412017-03-07 16:53:13 +01004588 val = mvpp2_percpu_read(priv, cpu, MVPP2_TXQ_RSVD_RSLT_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004589
4590 return val & MVPP2_TXQ_RSVD_RSLT_MASK;
4591}
4592
4593/* Check if there are enough reserved descriptors for transmission.
4594 * If not, request chunk of reserved descriptors and check again.
4595 */
4596static int mvpp2_txq_reserved_desc_num_proc(struct mvpp2 *priv,
4597 struct mvpp2_tx_queue *txq,
4598 struct mvpp2_txq_pcpu *txq_pcpu,
4599 int num)
4600{
4601 int req, cpu, desc_count;
4602
4603 if (txq_pcpu->reserved_num >= num)
4604 return 0;
4605
4606 /* Not enough descriptors reserved! Update the reserved descriptor
4607 * count and check again.
4608 */
4609
4610 desc_count = 0;
4611 /* Compute total of used descriptors */
4612 for_each_present_cpu(cpu) {
4613 struct mvpp2_txq_pcpu *txq_pcpu_aux;
4614
4615 txq_pcpu_aux = per_cpu_ptr(txq->pcpu, cpu);
4616 desc_count += txq_pcpu_aux->count;
4617 desc_count += txq_pcpu_aux->reserved_num;
4618 }
4619
4620 req = max(MVPP2_CPU_DESC_CHUNK, num - txq_pcpu->reserved_num);
4621 desc_count += req;
4622
4623 if (desc_count >
4624 (txq->size - (num_present_cpus() * MVPP2_CPU_DESC_CHUNK)))
4625 return -ENOMEM;
4626
4627 txq_pcpu->reserved_num += mvpp2_txq_alloc_reserved_desc(priv, txq, req);
4628
4629 /* OK, the descriptor cound has been updated: check again. */
4630 if (txq_pcpu->reserved_num < num)
4631 return -ENOMEM;
4632 return 0;
4633}
4634
4635/* Release the last allocated Tx descriptor. Useful to handle DMA
4636 * mapping failures in the Tx path.
4637 */
4638static void mvpp2_txq_desc_put(struct mvpp2_tx_queue *txq)
4639{
4640 if (txq->next_desc_to_proc == 0)
4641 txq->next_desc_to_proc = txq->last_desc - 1;
4642 else
4643 txq->next_desc_to_proc--;
4644}
4645
4646/* Set Tx descriptors fields relevant for CSUM calculation */
4647static u32 mvpp2_txq_desc_csum(int l3_offs, int l3_proto,
4648 int ip_hdr_len, int l4_proto)
4649{
4650 u32 command;
4651
4652 /* fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
4653 * G_L4_chk, L4_type required only for checksum calculation
4654 */
4655 command = (l3_offs << MVPP2_TXD_L3_OFF_SHIFT);
4656 command |= (ip_hdr_len << MVPP2_TXD_IP_HLEN_SHIFT);
4657 command |= MVPP2_TXD_IP_CSUM_DISABLE;
4658
4659 if (l3_proto == swab16(ETH_P_IP)) {
4660 command &= ~MVPP2_TXD_IP_CSUM_DISABLE; /* enable IPv4 csum */
4661 command &= ~MVPP2_TXD_L3_IP6; /* enable IPv4 */
4662 } else {
4663 command |= MVPP2_TXD_L3_IP6; /* enable IPv6 */
4664 }
4665
4666 if (l4_proto == IPPROTO_TCP) {
4667 command &= ~MVPP2_TXD_L4_UDP; /* enable TCP */
4668 command &= ~MVPP2_TXD_L4_CSUM_FRAG; /* generate L4 csum */
4669 } else if (l4_proto == IPPROTO_UDP) {
4670 command |= MVPP2_TXD_L4_UDP; /* enable UDP */
4671 command &= ~MVPP2_TXD_L4_CSUM_FRAG; /* generate L4 csum */
4672 } else {
4673 command |= MVPP2_TXD_L4_CSUM_NOT;
4674 }
4675
4676 return command;
4677}
4678
4679/* Get number of sent descriptors and decrement counter.
4680 * The number of sent descriptors is returned.
4681 * Per-CPU access
4682 */
4683static inline int mvpp2_txq_sent_desc_proc(struct mvpp2_port *port,
4684 struct mvpp2_tx_queue *txq)
4685{
4686 u32 val;
4687
4688 /* Reading status reg resets transmitted descriptor counter */
Thomas Petazzonia7868412017-03-07 16:53:13 +01004689 val = mvpp2_percpu_read(port->priv, smp_processor_id(),
4690 MVPP2_TXQ_SENT_REG(txq->id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004691
4692 return (val & MVPP2_TRANSMITTED_COUNT_MASK) >>
4693 MVPP2_TRANSMITTED_COUNT_OFFSET;
4694}
4695
4696static void mvpp2_txq_sent_counter_clear(void *arg)
4697{
4698 struct mvpp2_port *port = arg;
4699 int queue;
4700
4701 for (queue = 0; queue < txq_number; queue++) {
4702 int id = port->txqs[queue]->id;
4703
Thomas Petazzonia7868412017-03-07 16:53:13 +01004704 mvpp2_percpu_read(port->priv, smp_processor_id(),
4705 MVPP2_TXQ_SENT_REG(id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004706 }
4707}
4708
4709/* Set max sizes for Tx queues */
4710static void mvpp2_txp_max_tx_size_set(struct mvpp2_port *port)
4711{
4712 u32 val, size, mtu;
4713 int txq, tx_port_num;
4714
4715 mtu = port->pkt_size * 8;
4716 if (mtu > MVPP2_TXP_MTU_MAX)
4717 mtu = MVPP2_TXP_MTU_MAX;
4718
4719 /* WA for wrong Token bucket update: Set MTU value = 3*real MTU value */
4720 mtu = 3 * mtu;
4721
4722 /* Indirect access to registers */
4723 tx_port_num = mvpp2_egress_port(port);
4724 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
4725
4726 /* Set MTU */
4727 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_MTU_REG);
4728 val &= ~MVPP2_TXP_MTU_MAX;
4729 val |= mtu;
4730 mvpp2_write(port->priv, MVPP2_TXP_SCHED_MTU_REG, val);
4731
4732 /* TXP token size and all TXQs token size must be larger that MTU */
4733 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG);
4734 size = val & MVPP2_TXP_TOKEN_SIZE_MAX;
4735 if (size < mtu) {
4736 size = mtu;
4737 val &= ~MVPP2_TXP_TOKEN_SIZE_MAX;
4738 val |= size;
4739 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
4740 }
4741
4742 for (txq = 0; txq < txq_number; txq++) {
4743 val = mvpp2_read(port->priv,
4744 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq));
4745 size = val & MVPP2_TXQ_TOKEN_SIZE_MAX;
4746
4747 if (size < mtu) {
4748 size = mtu;
4749 val &= ~MVPP2_TXQ_TOKEN_SIZE_MAX;
4750 val |= size;
4751 mvpp2_write(port->priv,
4752 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq),
4753 val);
4754 }
4755 }
4756}
4757
4758/* Set the number of packets that will be received before Rx interrupt
4759 * will be generated by HW.
4760 */
4761static void mvpp2_rx_pkts_coal_set(struct mvpp2_port *port,
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01004762 struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004763{
Thomas Petazzonia7868412017-03-07 16:53:13 +01004764 int cpu = smp_processor_id();
4765
Thomas Petazzonif8b0d5f2017-02-21 11:28:03 +01004766 if (rxq->pkts_coal > MVPP2_OCCUPIED_THRESH_MASK)
4767 rxq->pkts_coal = MVPP2_OCCUPIED_THRESH_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004768
Thomas Petazzonia7868412017-03-07 16:53:13 +01004769 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
4770 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_THRESH_REG,
4771 rxq->pkts_coal);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004772}
4773
Thomas Petazzoniab426762017-02-21 11:28:04 +01004774static u32 mvpp2_usec_to_cycles(u32 usec, unsigned long clk_hz)
4775{
4776 u64 tmp = (u64)clk_hz * usec;
4777
4778 do_div(tmp, USEC_PER_SEC);
4779
4780 return tmp > U32_MAX ? U32_MAX : tmp;
4781}
4782
4783static u32 mvpp2_cycles_to_usec(u32 cycles, unsigned long clk_hz)
4784{
4785 u64 tmp = (u64)cycles * USEC_PER_SEC;
4786
4787 do_div(tmp, clk_hz);
4788
4789 return tmp > U32_MAX ? U32_MAX : tmp;
4790}
4791
Marcin Wojtas3f518502014-07-10 16:52:13 -03004792/* Set the time delay in usec before Rx interrupt */
4793static void mvpp2_rx_time_coal_set(struct mvpp2_port *port,
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01004794 struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004795{
Thomas Petazzoniab426762017-02-21 11:28:04 +01004796 unsigned long freq = port->priv->tclk;
4797 u32 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004798
Thomas Petazzoniab426762017-02-21 11:28:04 +01004799 if (val > MVPP2_MAX_ISR_RX_THRESHOLD) {
4800 rxq->time_coal =
4801 mvpp2_cycles_to_usec(MVPP2_MAX_ISR_RX_THRESHOLD, freq);
4802
4803 /* re-evaluate to get actual register value */
4804 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
4805 }
4806
Marcin Wojtas3f518502014-07-10 16:52:13 -03004807 mvpp2_write(port->priv, MVPP2_ISR_RX_THRESHOLD_REG(rxq->id), val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004808}
4809
Marcin Wojtas3f518502014-07-10 16:52:13 -03004810/* Free Tx queue skbuffs */
4811static void mvpp2_txq_bufs_free(struct mvpp2_port *port,
4812 struct mvpp2_tx_queue *txq,
4813 struct mvpp2_txq_pcpu *txq_pcpu, int num)
4814{
4815 int i;
4816
4817 for (i = 0; i < num; i++) {
Thomas Petazzoni83544912016-12-21 11:28:49 +01004818 struct mvpp2_txq_pcpu_buf *tx_buf =
4819 txq_pcpu->buffs + txq_pcpu->txq_get_index;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004820
Thomas Petazzoni20396132017-03-07 16:53:00 +01004821 dma_unmap_single(port->dev->dev.parent, tx_buf->dma,
Thomas Petazzoni83544912016-12-21 11:28:49 +01004822 tx_buf->size, DMA_TO_DEVICE);
Thomas Petazzoni36fb7432017-02-21 11:28:05 +01004823 if (tx_buf->skb)
4824 dev_kfree_skb_any(tx_buf->skb);
4825
4826 mvpp2_txq_inc_get(txq_pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004827 }
4828}
4829
4830static inline struct mvpp2_rx_queue *mvpp2_get_rx_queue(struct mvpp2_port *port,
4831 u32 cause)
4832{
4833 int queue = fls(cause) - 1;
4834
4835 return port->rxqs[queue];
4836}
4837
4838static inline struct mvpp2_tx_queue *mvpp2_get_tx_queue(struct mvpp2_port *port,
4839 u32 cause)
4840{
Marcin Wojtasedc660f2015-08-06 19:00:30 +02004841 int queue = fls(cause) - 1;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004842
4843 return port->txqs[queue];
4844}
4845
4846/* Handle end of transmission */
4847static void mvpp2_txq_done(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
4848 struct mvpp2_txq_pcpu *txq_pcpu)
4849{
4850 struct netdev_queue *nq = netdev_get_tx_queue(port->dev, txq->log_id);
4851 int tx_done;
4852
4853 if (txq_pcpu->cpu != smp_processor_id())
4854 netdev_err(port->dev, "wrong cpu on the end of Tx processing\n");
4855
4856 tx_done = mvpp2_txq_sent_desc_proc(port, txq);
4857 if (!tx_done)
4858 return;
4859 mvpp2_txq_bufs_free(port, txq, txq_pcpu, tx_done);
4860
4861 txq_pcpu->count -= tx_done;
4862
4863 if (netif_tx_queue_stopped(nq))
4864 if (txq_pcpu->size - txq_pcpu->count >= MAX_SKB_FRAGS + 1)
4865 netif_tx_wake_queue(nq);
4866}
4867
Marcin Wojtasedc660f2015-08-06 19:00:30 +02004868static unsigned int mvpp2_tx_done(struct mvpp2_port *port, u32 cause)
4869{
4870 struct mvpp2_tx_queue *txq;
4871 struct mvpp2_txq_pcpu *txq_pcpu;
4872 unsigned int tx_todo = 0;
4873
4874 while (cause) {
4875 txq = mvpp2_get_tx_queue(port, cause);
4876 if (!txq)
4877 break;
4878
4879 txq_pcpu = this_cpu_ptr(txq->pcpu);
4880
4881 if (txq_pcpu->count) {
4882 mvpp2_txq_done(port, txq, txq_pcpu);
4883 tx_todo += txq_pcpu->count;
4884 }
4885
4886 cause &= ~(1 << txq->log_id);
4887 }
4888 return tx_todo;
4889}
4890
Marcin Wojtas3f518502014-07-10 16:52:13 -03004891/* Rx/Tx queue initialization/cleanup methods */
4892
4893/* Allocate and initialize descriptors for aggr TXQ */
4894static int mvpp2_aggr_txq_init(struct platform_device *pdev,
4895 struct mvpp2_tx_queue *aggr_txq,
4896 int desc_num, int cpu,
4897 struct mvpp2 *priv)
4898{
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01004899 u32 txq_dma;
4900
Marcin Wojtas3f518502014-07-10 16:52:13 -03004901 /* Allocate memory for TX descriptors */
4902 aggr_txq->descs = dma_alloc_coherent(&pdev->dev,
4903 desc_num * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004904 &aggr_txq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004905 if (!aggr_txq->descs)
4906 return -ENOMEM;
4907
Marcin Wojtas3f518502014-07-10 16:52:13 -03004908 aggr_txq->last_desc = aggr_txq->size - 1;
4909
4910 /* Aggr TXQ no reset WA */
4911 aggr_txq->next_desc_to_proc = mvpp2_read(priv,
4912 MVPP2_AGGR_TXQ_INDEX_REG(cpu));
4913
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01004914 /* Set Tx descriptors queue starting address indirect
4915 * access
4916 */
4917 if (priv->hw_version == MVPP21)
4918 txq_dma = aggr_txq->descs_dma;
4919 else
4920 txq_dma = aggr_txq->descs_dma >>
4921 MVPP22_AGGR_TXQ_DESC_ADDR_OFFS;
4922
4923 mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu), txq_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004924 mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu), desc_num);
4925
4926 return 0;
4927}
4928
4929/* Create a specified Rx queue */
4930static int mvpp2_rxq_init(struct mvpp2_port *port,
4931 struct mvpp2_rx_queue *rxq)
4932
4933{
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01004934 u32 rxq_dma;
Thomas Petazzonia7868412017-03-07 16:53:13 +01004935 int cpu;
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01004936
Marcin Wojtas3f518502014-07-10 16:52:13 -03004937 rxq->size = port->rx_ring_size;
4938
4939 /* Allocate memory for RX descriptors */
4940 rxq->descs = dma_alloc_coherent(port->dev->dev.parent,
4941 rxq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004942 &rxq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004943 if (!rxq->descs)
4944 return -ENOMEM;
4945
Marcin Wojtas3f518502014-07-10 16:52:13 -03004946 rxq->last_desc = rxq->size - 1;
4947
4948 /* Zero occupied and non-occupied counters - direct access */
4949 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
4950
4951 /* Set Rx descriptors queue starting address - indirect access */
Thomas Petazzonia7868412017-03-07 16:53:13 +01004952 cpu = smp_processor_id();
4953 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01004954 if (port->priv->hw_version == MVPP21)
4955 rxq_dma = rxq->descs_dma;
4956 else
4957 rxq_dma = rxq->descs_dma >> MVPP22_DESC_ADDR_OFFS;
Thomas Petazzonia7868412017-03-07 16:53:13 +01004958 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_ADDR_REG, rxq_dma);
4959 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_SIZE_REG, rxq->size);
4960 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_INDEX_REG, 0);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004961
4962 /* Set Offset */
4963 mvpp2_rxq_offset_set(port, rxq->id, NET_SKB_PAD);
4964
4965 /* Set coalescing pkts and time */
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01004966 mvpp2_rx_pkts_coal_set(port, rxq);
4967 mvpp2_rx_time_coal_set(port, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004968
4969 /* Add number of descriptors ready for receiving packets */
4970 mvpp2_rxq_status_update(port, rxq->id, 0, rxq->size);
4971
4972 return 0;
4973}
4974
4975/* Push packets received by the RXQ to BM pool */
4976static void mvpp2_rxq_drop_pkts(struct mvpp2_port *port,
4977 struct mvpp2_rx_queue *rxq)
4978{
4979 int rx_received, i;
4980
4981 rx_received = mvpp2_rxq_received(port, rxq->id);
4982 if (!rx_received)
4983 return;
4984
4985 for (i = 0; i < rx_received; i++) {
4986 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01004987 u32 bm = mvpp2_bm_cookie_build(port, rx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004988
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01004989 mvpp2_pool_refill(port, bm,
4990 mvpp2_rxdesc_dma_addr_get(port, rx_desc),
4991 mvpp2_rxdesc_cookie_get(port, rx_desc));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004992 }
4993 mvpp2_rxq_status_update(port, rxq->id, rx_received, rx_received);
4994}
4995
4996/* Cleanup Rx queue */
4997static void mvpp2_rxq_deinit(struct mvpp2_port *port,
4998 struct mvpp2_rx_queue *rxq)
4999{
Thomas Petazzonia7868412017-03-07 16:53:13 +01005000 int cpu;
5001
Marcin Wojtas3f518502014-07-10 16:52:13 -03005002 mvpp2_rxq_drop_pkts(port, rxq);
5003
5004 if (rxq->descs)
5005 dma_free_coherent(port->dev->dev.parent,
5006 rxq->size * MVPP2_DESC_ALIGNED_SIZE,
5007 rxq->descs,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005008 rxq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005009
5010 rxq->descs = NULL;
5011 rxq->last_desc = 0;
5012 rxq->next_desc_to_proc = 0;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005013 rxq->descs_dma = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005014
5015 /* Clear Rx descriptors queue starting address and size;
5016 * free descriptor number
5017 */
5018 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
Thomas Petazzonia7868412017-03-07 16:53:13 +01005019 cpu = smp_processor_id();
5020 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
5021 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_ADDR_REG, 0);
5022 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_SIZE_REG, 0);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005023}
5024
5025/* Create and initialize a Tx queue */
5026static int mvpp2_txq_init(struct mvpp2_port *port,
5027 struct mvpp2_tx_queue *txq)
5028{
5029 u32 val;
5030 int cpu, desc, desc_per_txq, tx_port_num;
5031 struct mvpp2_txq_pcpu *txq_pcpu;
5032
5033 txq->size = port->tx_ring_size;
5034
5035 /* Allocate memory for Tx descriptors */
5036 txq->descs = dma_alloc_coherent(port->dev->dev.parent,
5037 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005038 &txq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005039 if (!txq->descs)
5040 return -ENOMEM;
5041
Marcin Wojtas3f518502014-07-10 16:52:13 -03005042 txq->last_desc = txq->size - 1;
5043
5044 /* Set Tx descriptors queue starting address - indirect access */
Thomas Petazzonia7868412017-03-07 16:53:13 +01005045 cpu = smp_processor_id();
5046 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5047 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_ADDR_REG,
5048 txq->descs_dma);
5049 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_SIZE_REG,
5050 txq->size & MVPP2_TXQ_DESC_SIZE_MASK);
5051 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_INDEX_REG, 0);
5052 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_RSVD_CLR_REG,
5053 txq->id << MVPP2_TXQ_RSVD_CLR_OFFSET);
5054 val = mvpp2_percpu_read(port->priv, cpu, MVPP2_TXQ_PENDING_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005055 val &= ~MVPP2_TXQ_PENDING_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005056 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PENDING_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005057
5058 /* Calculate base address in prefetch buffer. We reserve 16 descriptors
5059 * for each existing TXQ.
5060 * TCONTS for PON port must be continuous from 0 to MVPP2_MAX_TCONT
5061 * GBE ports assumed to be continious from 0 to MVPP2_MAX_PORTS
5062 */
5063 desc_per_txq = 16;
5064 desc = (port->id * MVPP2_MAX_TXQ * desc_per_txq) +
5065 (txq->log_id * desc_per_txq);
5066
Thomas Petazzonia7868412017-03-07 16:53:13 +01005067 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG,
5068 MVPP2_PREF_BUF_PTR(desc) | MVPP2_PREF_BUF_SIZE_16 |
5069 MVPP2_PREF_BUF_THRESH(desc_per_txq / 2));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005070
5071 /* WRR / EJP configuration - indirect access */
5072 tx_port_num = mvpp2_egress_port(port);
5073 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
5074
5075 val = mvpp2_read(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id));
5076 val &= ~MVPP2_TXQ_REFILL_PERIOD_ALL_MASK;
5077 val |= MVPP2_TXQ_REFILL_PERIOD_MASK(1);
5078 val |= MVPP2_TXQ_REFILL_TOKENS_ALL_MASK;
5079 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id), val);
5080
5081 val = MVPP2_TXQ_TOKEN_SIZE_MAX;
5082 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq->log_id),
5083 val);
5084
5085 for_each_present_cpu(cpu) {
5086 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
5087 txq_pcpu->size = txq->size;
Thomas Petazzoni83544912016-12-21 11:28:49 +01005088 txq_pcpu->buffs = kmalloc(txq_pcpu->size *
5089 sizeof(struct mvpp2_txq_pcpu_buf),
5090 GFP_KERNEL);
5091 if (!txq_pcpu->buffs)
Marcin Wojtas71ce3912015-08-06 19:00:29 +02005092 goto error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005093
5094 txq_pcpu->count = 0;
5095 txq_pcpu->reserved_num = 0;
5096 txq_pcpu->txq_put_index = 0;
5097 txq_pcpu->txq_get_index = 0;
5098 }
5099
5100 return 0;
Marcin Wojtas71ce3912015-08-06 19:00:29 +02005101
5102error:
5103 for_each_present_cpu(cpu) {
5104 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Thomas Petazzoni83544912016-12-21 11:28:49 +01005105 kfree(txq_pcpu->buffs);
Marcin Wojtas71ce3912015-08-06 19:00:29 +02005106 }
5107
5108 dma_free_coherent(port->dev->dev.parent,
5109 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005110 txq->descs, txq->descs_dma);
Marcin Wojtas71ce3912015-08-06 19:00:29 +02005111
5112 return -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005113}
5114
5115/* Free allocated TXQ resources */
5116static void mvpp2_txq_deinit(struct mvpp2_port *port,
5117 struct mvpp2_tx_queue *txq)
5118{
5119 struct mvpp2_txq_pcpu *txq_pcpu;
5120 int cpu;
5121
5122 for_each_present_cpu(cpu) {
5123 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Thomas Petazzoni83544912016-12-21 11:28:49 +01005124 kfree(txq_pcpu->buffs);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005125 }
5126
5127 if (txq->descs)
5128 dma_free_coherent(port->dev->dev.parent,
5129 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005130 txq->descs, txq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005131
5132 txq->descs = NULL;
5133 txq->last_desc = 0;
5134 txq->next_desc_to_proc = 0;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005135 txq->descs_dma = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005136
5137 /* Set minimum bandwidth for disabled TXQs */
5138 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(txq->id), 0);
5139
5140 /* Set Tx descriptors queue starting address and size */
Thomas Petazzonia7868412017-03-07 16:53:13 +01005141 cpu = smp_processor_id();
5142 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5143 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_ADDR_REG, 0);
5144 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_SIZE_REG, 0);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005145}
5146
5147/* Cleanup Tx ports */
5148static void mvpp2_txq_clean(struct mvpp2_port *port, struct mvpp2_tx_queue *txq)
5149{
5150 struct mvpp2_txq_pcpu *txq_pcpu;
5151 int delay, pending, cpu;
5152 u32 val;
5153
Thomas Petazzonia7868412017-03-07 16:53:13 +01005154 cpu = smp_processor_id();
5155 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5156 val = mvpp2_percpu_read(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005157 val |= MVPP2_TXQ_DRAIN_EN_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005158 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005159
5160 /* The napi queue has been stopped so wait for all packets
5161 * to be transmitted.
5162 */
5163 delay = 0;
5164 do {
5165 if (delay >= MVPP2_TX_PENDING_TIMEOUT_MSEC) {
5166 netdev_warn(port->dev,
5167 "port %d: cleaning queue %d timed out\n",
5168 port->id, txq->log_id);
5169 break;
5170 }
5171 mdelay(1);
5172 delay++;
5173
Thomas Petazzonia7868412017-03-07 16:53:13 +01005174 pending = mvpp2_percpu_read(port->priv, cpu,
5175 MVPP2_TXQ_PENDING_REG);
5176 pending &= MVPP2_TXQ_PENDING_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005177 } while (pending);
5178
5179 val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005180 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005181
5182 for_each_present_cpu(cpu) {
5183 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
5184
5185 /* Release all packets */
5186 mvpp2_txq_bufs_free(port, txq, txq_pcpu, txq_pcpu->count);
5187
5188 /* Reset queue */
5189 txq_pcpu->count = 0;
5190 txq_pcpu->txq_put_index = 0;
5191 txq_pcpu->txq_get_index = 0;
5192 }
5193}
5194
5195/* Cleanup all Tx queues */
5196static void mvpp2_cleanup_txqs(struct mvpp2_port *port)
5197{
5198 struct mvpp2_tx_queue *txq;
5199 int queue;
5200 u32 val;
5201
5202 val = mvpp2_read(port->priv, MVPP2_TX_PORT_FLUSH_REG);
5203
5204 /* Reset Tx ports and delete Tx queues */
5205 val |= MVPP2_TX_PORT_FLUSH_MASK(port->id);
5206 mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
5207
5208 for (queue = 0; queue < txq_number; queue++) {
5209 txq = port->txqs[queue];
5210 mvpp2_txq_clean(port, txq);
5211 mvpp2_txq_deinit(port, txq);
5212 }
5213
5214 on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
5215
5216 val &= ~MVPP2_TX_PORT_FLUSH_MASK(port->id);
5217 mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
5218}
5219
5220/* Cleanup all Rx queues */
5221static void mvpp2_cleanup_rxqs(struct mvpp2_port *port)
5222{
5223 int queue;
5224
5225 for (queue = 0; queue < rxq_number; queue++)
5226 mvpp2_rxq_deinit(port, port->rxqs[queue]);
5227}
5228
5229/* Init all Rx queues for port */
5230static int mvpp2_setup_rxqs(struct mvpp2_port *port)
5231{
5232 int queue, err;
5233
5234 for (queue = 0; queue < rxq_number; queue++) {
5235 err = mvpp2_rxq_init(port, port->rxqs[queue]);
5236 if (err)
5237 goto err_cleanup;
5238 }
5239 return 0;
5240
5241err_cleanup:
5242 mvpp2_cleanup_rxqs(port);
5243 return err;
5244}
5245
5246/* Init all tx queues for port */
5247static int mvpp2_setup_txqs(struct mvpp2_port *port)
5248{
5249 struct mvpp2_tx_queue *txq;
5250 int queue, err;
5251
5252 for (queue = 0; queue < txq_number; queue++) {
5253 txq = port->txqs[queue];
5254 err = mvpp2_txq_init(port, txq);
5255 if (err)
5256 goto err_cleanup;
5257 }
5258
Marcin Wojtas3f518502014-07-10 16:52:13 -03005259 on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
5260 return 0;
5261
5262err_cleanup:
5263 mvpp2_cleanup_txqs(port);
5264 return err;
5265}
5266
5267/* The callback for per-port interrupt */
5268static irqreturn_t mvpp2_isr(int irq, void *dev_id)
5269{
5270 struct mvpp2_port *port = (struct mvpp2_port *)dev_id;
5271
5272 mvpp2_interrupts_disable(port);
5273
5274 napi_schedule(&port->napi);
5275
5276 return IRQ_HANDLED;
5277}
5278
5279/* Adjust link */
5280static void mvpp2_link_event(struct net_device *dev)
5281{
5282 struct mvpp2_port *port = netdev_priv(dev);
Philippe Reynes8e072692016-06-28 00:08:11 +02005283 struct phy_device *phydev = dev->phydev;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005284 int status_change = 0;
5285 u32 val;
5286
5287 if (phydev->link) {
5288 if ((port->speed != phydev->speed) ||
5289 (port->duplex != phydev->duplex)) {
5290 u32 val;
5291
5292 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5293 val &= ~(MVPP2_GMAC_CONFIG_MII_SPEED |
5294 MVPP2_GMAC_CONFIG_GMII_SPEED |
5295 MVPP2_GMAC_CONFIG_FULL_DUPLEX |
5296 MVPP2_GMAC_AN_SPEED_EN |
5297 MVPP2_GMAC_AN_DUPLEX_EN);
5298
5299 if (phydev->duplex)
5300 val |= MVPP2_GMAC_CONFIG_FULL_DUPLEX;
5301
5302 if (phydev->speed == SPEED_1000)
5303 val |= MVPP2_GMAC_CONFIG_GMII_SPEED;
Thomas Petazzoni2add5112014-07-27 23:21:35 +02005304 else if (phydev->speed == SPEED_100)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005305 val |= MVPP2_GMAC_CONFIG_MII_SPEED;
5306
5307 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5308
5309 port->duplex = phydev->duplex;
5310 port->speed = phydev->speed;
5311 }
5312 }
5313
5314 if (phydev->link != port->link) {
5315 if (!phydev->link) {
5316 port->duplex = -1;
5317 port->speed = 0;
5318 }
5319
5320 port->link = phydev->link;
5321 status_change = 1;
5322 }
5323
5324 if (status_change) {
5325 if (phydev->link) {
5326 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5327 val |= (MVPP2_GMAC_FORCE_LINK_PASS |
5328 MVPP2_GMAC_FORCE_LINK_DOWN);
5329 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5330 mvpp2_egress_enable(port);
5331 mvpp2_ingress_enable(port);
5332 } else {
5333 mvpp2_ingress_disable(port);
5334 mvpp2_egress_disable(port);
5335 }
5336 phy_print_status(phydev);
5337 }
5338}
5339
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005340static void mvpp2_timer_set(struct mvpp2_port_pcpu *port_pcpu)
5341{
5342 ktime_t interval;
5343
5344 if (!port_pcpu->timer_scheduled) {
5345 port_pcpu->timer_scheduled = true;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01005346 interval = MVPP2_TXDONE_HRTIMER_PERIOD_NS;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005347 hrtimer_start(&port_pcpu->tx_done_timer, interval,
5348 HRTIMER_MODE_REL_PINNED);
5349 }
5350}
5351
5352static void mvpp2_tx_proc_cb(unsigned long data)
5353{
5354 struct net_device *dev = (struct net_device *)data;
5355 struct mvpp2_port *port = netdev_priv(dev);
5356 struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
5357 unsigned int tx_todo, cause;
5358
5359 if (!netif_running(dev))
5360 return;
5361 port_pcpu->timer_scheduled = false;
5362
5363 /* Process all the Tx queues */
5364 cause = (1 << txq_number) - 1;
5365 tx_todo = mvpp2_tx_done(port, cause);
5366
5367 /* Set the timer in case not all the packets were processed */
5368 if (tx_todo)
5369 mvpp2_timer_set(port_pcpu);
5370}
5371
5372static enum hrtimer_restart mvpp2_hr_timer_cb(struct hrtimer *timer)
5373{
5374 struct mvpp2_port_pcpu *port_pcpu = container_of(timer,
5375 struct mvpp2_port_pcpu,
5376 tx_done_timer);
5377
5378 tasklet_schedule(&port_pcpu->tx_done_tasklet);
5379
5380 return HRTIMER_NORESTART;
5381}
5382
Marcin Wojtas3f518502014-07-10 16:52:13 -03005383/* Main RX/TX processing routines */
5384
5385/* Display more error info */
5386static void mvpp2_rx_error(struct mvpp2_port *port,
5387 struct mvpp2_rx_desc *rx_desc)
5388{
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005389 u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
5390 size_t sz = mvpp2_rxdesc_size_get(port, rx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005391
5392 switch (status & MVPP2_RXD_ERR_CODE_MASK) {
5393 case MVPP2_RXD_ERR_CRC:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005394 netdev_err(port->dev, "bad rx status %08x (crc error), size=%zu\n",
5395 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005396 break;
5397 case MVPP2_RXD_ERR_OVERRUN:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005398 netdev_err(port->dev, "bad rx status %08x (overrun error), size=%zu\n",
5399 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005400 break;
5401 case MVPP2_RXD_ERR_RESOURCE:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005402 netdev_err(port->dev, "bad rx status %08x (resource error), size=%zu\n",
5403 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005404 break;
5405 }
5406}
5407
5408/* Handle RX checksum offload */
5409static void mvpp2_rx_csum(struct mvpp2_port *port, u32 status,
5410 struct sk_buff *skb)
5411{
5412 if (((status & MVPP2_RXD_L3_IP4) &&
5413 !(status & MVPP2_RXD_IP4_HEADER_ERR)) ||
5414 (status & MVPP2_RXD_L3_IP6))
5415 if (((status & MVPP2_RXD_L4_UDP) ||
5416 (status & MVPP2_RXD_L4_TCP)) &&
5417 (status & MVPP2_RXD_L4_CSUM_OK)) {
5418 skb->csum = 0;
5419 skb->ip_summed = CHECKSUM_UNNECESSARY;
5420 return;
5421 }
5422
5423 skb->ip_summed = CHECKSUM_NONE;
5424}
5425
5426/* Reuse skb if possible, or allocate a new skb and add it to BM pool */
5427static int mvpp2_rx_refill(struct mvpp2_port *port,
Thomas Petazzoni7ef7e1d2017-02-21 11:28:07 +01005428 struct mvpp2_bm_pool *bm_pool, u32 bm)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005429{
Thomas Petazzoni20396132017-03-07 16:53:00 +01005430 dma_addr_t dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01005431 phys_addr_t phys_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005432 void *buf;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005433
Marcin Wojtas3f518502014-07-10 16:52:13 -03005434 /* No recycle or too many buffers are in use, so allocate a new skb */
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01005435 buf = mvpp2_buf_alloc(port, bm_pool, &dma_addr, &phys_addr,
5436 GFP_ATOMIC);
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005437 if (!buf)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005438 return -ENOMEM;
5439
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01005440 mvpp2_pool_refill(port, bm, dma_addr, phys_addr);
Thomas Petazzoni7ef7e1d2017-02-21 11:28:07 +01005441
Marcin Wojtas3f518502014-07-10 16:52:13 -03005442 return 0;
5443}
5444
5445/* Handle tx checksum */
5446static u32 mvpp2_skb_tx_csum(struct mvpp2_port *port, struct sk_buff *skb)
5447{
5448 if (skb->ip_summed == CHECKSUM_PARTIAL) {
5449 int ip_hdr_len = 0;
5450 u8 l4_proto;
5451
5452 if (skb->protocol == htons(ETH_P_IP)) {
5453 struct iphdr *ip4h = ip_hdr(skb);
5454
5455 /* Calculate IPv4 checksum and L4 checksum */
5456 ip_hdr_len = ip4h->ihl;
5457 l4_proto = ip4h->protocol;
5458 } else if (skb->protocol == htons(ETH_P_IPV6)) {
5459 struct ipv6hdr *ip6h = ipv6_hdr(skb);
5460
5461 /* Read l4_protocol from one of IPv6 extra headers */
5462 if (skb_network_header_len(skb) > 0)
5463 ip_hdr_len = (skb_network_header_len(skb) >> 2);
5464 l4_proto = ip6h->nexthdr;
5465 } else {
5466 return MVPP2_TXD_L4_CSUM_NOT;
5467 }
5468
5469 return mvpp2_txq_desc_csum(skb_network_offset(skb),
5470 skb->protocol, ip_hdr_len, l4_proto);
5471 }
5472
5473 return MVPP2_TXD_L4_CSUM_NOT | MVPP2_TXD_IP_CSUM_DISABLE;
5474}
5475
Marcin Wojtas3f518502014-07-10 16:52:13 -03005476/* Main rx processing */
5477static int mvpp2_rx(struct mvpp2_port *port, int rx_todo,
5478 struct mvpp2_rx_queue *rxq)
5479{
5480 struct net_device *dev = port->dev;
Marcin Wojtasb5015852015-12-03 15:20:51 +01005481 int rx_received;
5482 int rx_done = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005483 u32 rcvd_pkts = 0;
5484 u32 rcvd_bytes = 0;
5485
5486 /* Get number of received packets and clamp the to-do */
5487 rx_received = mvpp2_rxq_received(port, rxq->id);
5488 if (rx_todo > rx_received)
5489 rx_todo = rx_received;
5490
Marcin Wojtasb5015852015-12-03 15:20:51 +01005491 while (rx_done < rx_todo) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005492 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
5493 struct mvpp2_bm_pool *bm_pool;
5494 struct sk_buff *skb;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005495 unsigned int frag_size;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005496 dma_addr_t dma_addr;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005497 phys_addr_t phys_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005498 u32 bm, rx_status;
5499 int pool, rx_bytes, err;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005500 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005501
Marcin Wojtasb5015852015-12-03 15:20:51 +01005502 rx_done++;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005503 rx_status = mvpp2_rxdesc_status_get(port, rx_desc);
5504 rx_bytes = mvpp2_rxdesc_size_get(port, rx_desc);
5505 rx_bytes -= MVPP2_MH_SIZE;
5506 dma_addr = mvpp2_rxdesc_dma_addr_get(port, rx_desc);
5507 phys_addr = mvpp2_rxdesc_cookie_get(port, rx_desc);
5508 data = (void *)phys_to_virt(phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005509
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005510 bm = mvpp2_bm_cookie_build(port, rx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005511 pool = mvpp2_bm_cookie_pool_get(bm);
5512 bm_pool = &port->priv->bm_pools[pool];
Marcin Wojtas3f518502014-07-10 16:52:13 -03005513
5514 /* In case of an error, release the requested buffer pointer
5515 * to the Buffer Manager. This request process is controlled
5516 * by the hardware, and the information about the buffer is
5517 * comprised by the RX descriptor.
5518 */
5519 if (rx_status & MVPP2_RXD_ERR_SUMMARY) {
Marcin Wojtasb5015852015-12-03 15:20:51 +01005520 err_drop_frame:
Marcin Wojtas3f518502014-07-10 16:52:13 -03005521 dev->stats.rx_errors++;
5522 mvpp2_rx_error(port, rx_desc);
Marcin Wojtasb5015852015-12-03 15:20:51 +01005523 /* Return the buffer to the pool */
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005524 mvpp2_pool_refill(port, bm, dma_addr, phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005525 continue;
5526 }
5527
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005528 if (bm_pool->frag_size > PAGE_SIZE)
5529 frag_size = 0;
5530 else
5531 frag_size = bm_pool->frag_size;
5532
5533 skb = build_skb(data, frag_size);
5534 if (!skb) {
5535 netdev_warn(port->dev, "skb build failed\n");
5536 goto err_drop_frame;
5537 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03005538
Thomas Petazzoni7ef7e1d2017-02-21 11:28:07 +01005539 err = mvpp2_rx_refill(port, bm_pool, bm);
Marcin Wojtasb5015852015-12-03 15:20:51 +01005540 if (err) {
5541 netdev_err(port->dev, "failed to refill BM pools\n");
5542 goto err_drop_frame;
5543 }
5544
Thomas Petazzoni20396132017-03-07 16:53:00 +01005545 dma_unmap_single(dev->dev.parent, dma_addr,
Marcin Wojtas4229d502015-12-03 15:20:50 +01005546 bm_pool->buf_size, DMA_FROM_DEVICE);
5547
Marcin Wojtas3f518502014-07-10 16:52:13 -03005548 rcvd_pkts++;
5549 rcvd_bytes += rx_bytes;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005550
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005551 skb_reserve(skb, MVPP2_MH_SIZE + NET_SKB_PAD);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005552 skb_put(skb, rx_bytes);
5553 skb->protocol = eth_type_trans(skb, dev);
5554 mvpp2_rx_csum(port, rx_status, skb);
5555
5556 napi_gro_receive(&port->napi, skb);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005557 }
5558
5559 if (rcvd_pkts) {
5560 struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
5561
5562 u64_stats_update_begin(&stats->syncp);
5563 stats->rx_packets += rcvd_pkts;
5564 stats->rx_bytes += rcvd_bytes;
5565 u64_stats_update_end(&stats->syncp);
5566 }
5567
5568 /* Update Rx queue management counters */
5569 wmb();
Marcin Wojtasb5015852015-12-03 15:20:51 +01005570 mvpp2_rxq_status_update(port, rxq->id, rx_done, rx_done);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005571
5572 return rx_todo;
5573}
5574
5575static inline void
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005576tx_desc_unmap_put(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
Marcin Wojtas3f518502014-07-10 16:52:13 -03005577 struct mvpp2_tx_desc *desc)
5578{
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005579 dma_addr_t buf_dma_addr =
5580 mvpp2_txdesc_dma_addr_get(port, desc);
5581 size_t buf_sz =
5582 mvpp2_txdesc_size_get(port, desc);
5583 dma_unmap_single(port->dev->dev.parent, buf_dma_addr,
5584 buf_sz, DMA_TO_DEVICE);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005585 mvpp2_txq_desc_put(txq);
5586}
5587
5588/* Handle tx fragmentation processing */
5589static int mvpp2_tx_frag_process(struct mvpp2_port *port, struct sk_buff *skb,
5590 struct mvpp2_tx_queue *aggr_txq,
5591 struct mvpp2_tx_queue *txq)
5592{
5593 struct mvpp2_txq_pcpu *txq_pcpu = this_cpu_ptr(txq->pcpu);
5594 struct mvpp2_tx_desc *tx_desc;
5595 int i;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005596 dma_addr_t buf_dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005597
5598 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
5599 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5600 void *addr = page_address(frag->page.p) + frag->page_offset;
5601
5602 tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005603 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
5604 mvpp2_txdesc_size_set(port, tx_desc, frag->size);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005605
Thomas Petazzoni20396132017-03-07 16:53:00 +01005606 buf_dma_addr = dma_map_single(port->dev->dev.parent, addr,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005607 frag->size,
5608 DMA_TO_DEVICE);
Thomas Petazzoni20396132017-03-07 16:53:00 +01005609 if (dma_mapping_error(port->dev->dev.parent, buf_dma_addr)) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005610 mvpp2_txq_desc_put(txq);
5611 goto error;
5612 }
5613
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005614 mvpp2_txdesc_offset_set(port, tx_desc,
5615 buf_dma_addr & MVPP2_TX_DESC_ALIGN);
5616 mvpp2_txdesc_dma_addr_set(port, tx_desc,
5617 buf_dma_addr & ~MVPP2_TX_DESC_ALIGN);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005618
5619 if (i == (skb_shinfo(skb)->nr_frags - 1)) {
5620 /* Last descriptor */
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005621 mvpp2_txdesc_cmd_set(port, tx_desc,
5622 MVPP2_TXD_L_DESC);
5623 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005624 } else {
5625 /* Descriptor in the middle: Not First, Not Last */
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005626 mvpp2_txdesc_cmd_set(port, tx_desc, 0);
5627 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005628 }
5629 }
5630
5631 return 0;
5632
5633error:
5634 /* Release all descriptors that were used to map fragments of
5635 * this packet, as well as the corresponding DMA mappings
5636 */
5637 for (i = i - 1; i >= 0; i--) {
5638 tx_desc = txq->descs + i;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005639 tx_desc_unmap_put(port, txq, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005640 }
5641
5642 return -ENOMEM;
5643}
5644
5645/* Main tx processing */
5646static int mvpp2_tx(struct sk_buff *skb, struct net_device *dev)
5647{
5648 struct mvpp2_port *port = netdev_priv(dev);
5649 struct mvpp2_tx_queue *txq, *aggr_txq;
5650 struct mvpp2_txq_pcpu *txq_pcpu;
5651 struct mvpp2_tx_desc *tx_desc;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005652 dma_addr_t buf_dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005653 int frags = 0;
5654 u16 txq_id;
5655 u32 tx_cmd;
5656
5657 txq_id = skb_get_queue_mapping(skb);
5658 txq = port->txqs[txq_id];
5659 txq_pcpu = this_cpu_ptr(txq->pcpu);
5660 aggr_txq = &port->priv->aggr_txqs[smp_processor_id()];
5661
5662 frags = skb_shinfo(skb)->nr_frags + 1;
5663
5664 /* Check number of available descriptors */
5665 if (mvpp2_aggr_desc_num_check(port->priv, aggr_txq, frags) ||
5666 mvpp2_txq_reserved_desc_num_proc(port->priv, txq,
5667 txq_pcpu, frags)) {
5668 frags = 0;
5669 goto out;
5670 }
5671
5672 /* Get a descriptor for the first part of the packet */
5673 tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005674 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
5675 mvpp2_txdesc_size_set(port, tx_desc, skb_headlen(skb));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005676
Thomas Petazzoni20396132017-03-07 16:53:00 +01005677 buf_dma_addr = dma_map_single(dev->dev.parent, skb->data,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005678 skb_headlen(skb), DMA_TO_DEVICE);
Thomas Petazzoni20396132017-03-07 16:53:00 +01005679 if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005680 mvpp2_txq_desc_put(txq);
5681 frags = 0;
5682 goto out;
5683 }
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005684
5685 mvpp2_txdesc_offset_set(port, tx_desc,
5686 buf_dma_addr & MVPP2_TX_DESC_ALIGN);
5687 mvpp2_txdesc_dma_addr_set(port, tx_desc,
5688 buf_dma_addr & ~MVPP2_TX_DESC_ALIGN);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005689
5690 tx_cmd = mvpp2_skb_tx_csum(port, skb);
5691
5692 if (frags == 1) {
5693 /* First and Last descriptor */
5694 tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_L_DESC;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005695 mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
5696 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005697 } else {
5698 /* First but not Last */
5699 tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_PADDING_DISABLE;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005700 mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
5701 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005702
5703 /* Continue with other skb fragments */
5704 if (mvpp2_tx_frag_process(port, skb, aggr_txq, txq)) {
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005705 tx_desc_unmap_put(port, txq, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005706 frags = 0;
5707 goto out;
5708 }
5709 }
5710
5711 txq_pcpu->reserved_num -= frags;
5712 txq_pcpu->count += frags;
5713 aggr_txq->count += frags;
5714
5715 /* Enable transmit */
5716 wmb();
5717 mvpp2_aggr_txq_pend_desc_add(port, frags);
5718
5719 if (txq_pcpu->size - txq_pcpu->count < MAX_SKB_FRAGS + 1) {
5720 struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
5721
5722 netif_tx_stop_queue(nq);
5723 }
5724out:
5725 if (frags > 0) {
5726 struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
5727
5728 u64_stats_update_begin(&stats->syncp);
5729 stats->tx_packets++;
5730 stats->tx_bytes += skb->len;
5731 u64_stats_update_end(&stats->syncp);
5732 } else {
5733 dev->stats.tx_dropped++;
5734 dev_kfree_skb_any(skb);
5735 }
5736
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005737 /* Finalize TX processing */
5738 if (txq_pcpu->count >= txq->done_pkts_coal)
5739 mvpp2_txq_done(port, txq, txq_pcpu);
5740
5741 /* Set the timer in case not all frags were processed */
5742 if (txq_pcpu->count <= frags && txq_pcpu->count > 0) {
5743 struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
5744
5745 mvpp2_timer_set(port_pcpu);
5746 }
5747
Marcin Wojtas3f518502014-07-10 16:52:13 -03005748 return NETDEV_TX_OK;
5749}
5750
5751static inline void mvpp2_cause_error(struct net_device *dev, int cause)
5752{
5753 if (cause & MVPP2_CAUSE_FCS_ERR_MASK)
5754 netdev_err(dev, "FCS error\n");
5755 if (cause & MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK)
5756 netdev_err(dev, "rx fifo overrun error\n");
5757 if (cause & MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK)
5758 netdev_err(dev, "tx fifo underrun error\n");
5759}
5760
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005761static int mvpp2_poll(struct napi_struct *napi, int budget)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005762{
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005763 u32 cause_rx_tx, cause_rx, cause_misc;
5764 int rx_done = 0;
5765 struct mvpp2_port *port = netdev_priv(napi->dev);
Thomas Petazzonia7868412017-03-07 16:53:13 +01005766 int cpu = smp_processor_id();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005767
5768 /* Rx/Tx cause register
5769 *
5770 * Bits 0-15: each bit indicates received packets on the Rx queue
5771 * (bit 0 is for Rx queue 0).
5772 *
5773 * Bits 16-23: each bit indicates transmitted packets on the Tx queue
5774 * (bit 16 is for Tx queue 0).
5775 *
5776 * Each CPU has its own Rx/Tx cause register
5777 */
Thomas Petazzonia7868412017-03-07 16:53:13 +01005778 cause_rx_tx = mvpp2_percpu_read(port->priv, cpu,
5779 MVPP2_ISR_RX_TX_CAUSE_REG(port->id));
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005780 cause_rx_tx &= ~MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005781 cause_misc = cause_rx_tx & MVPP2_CAUSE_MISC_SUM_MASK;
5782
5783 if (cause_misc) {
5784 mvpp2_cause_error(port->dev, cause_misc);
5785
5786 /* Clear the cause register */
5787 mvpp2_write(port->priv, MVPP2_ISR_MISC_CAUSE_REG, 0);
Thomas Petazzonia7868412017-03-07 16:53:13 +01005788 mvpp2_percpu_write(port->priv, cpu,
5789 MVPP2_ISR_RX_TX_CAUSE_REG(port->id),
5790 cause_rx_tx & ~MVPP2_CAUSE_MISC_SUM_MASK);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005791 }
5792
Marcin Wojtas3f518502014-07-10 16:52:13 -03005793 cause_rx = cause_rx_tx & MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
5794
5795 /* Process RX packets */
5796 cause_rx |= port->pending_cause_rx;
5797 while (cause_rx && budget > 0) {
5798 int count;
5799 struct mvpp2_rx_queue *rxq;
5800
5801 rxq = mvpp2_get_rx_queue(port, cause_rx);
5802 if (!rxq)
5803 break;
5804
5805 count = mvpp2_rx(port, budget, rxq);
5806 rx_done += count;
5807 budget -= count;
5808 if (budget > 0) {
5809 /* Clear the bit associated to this Rx queue
5810 * so that next iteration will continue from
5811 * the next Rx queue.
5812 */
5813 cause_rx &= ~(1 << rxq->logic_rxq);
5814 }
5815 }
5816
5817 if (budget > 0) {
5818 cause_rx = 0;
Eric Dumazet6ad20162017-01-30 08:22:01 -08005819 napi_complete_done(napi, rx_done);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005820
5821 mvpp2_interrupts_enable(port);
5822 }
5823 port->pending_cause_rx = cause_rx;
5824 return rx_done;
5825}
5826
5827/* Set hw internals when starting port */
5828static void mvpp2_start_dev(struct mvpp2_port *port)
5829{
Philippe Reynes8e072692016-06-28 00:08:11 +02005830 struct net_device *ndev = port->dev;
5831
Marcin Wojtas3f518502014-07-10 16:52:13 -03005832 mvpp2_gmac_max_rx_size_set(port);
5833 mvpp2_txp_max_tx_size_set(port);
5834
5835 napi_enable(&port->napi);
5836
5837 /* Enable interrupts on all CPUs */
5838 mvpp2_interrupts_enable(port);
5839
5840 mvpp2_port_enable(port);
Philippe Reynes8e072692016-06-28 00:08:11 +02005841 phy_start(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005842 netif_tx_start_all_queues(port->dev);
5843}
5844
5845/* Set hw internals when stopping port */
5846static void mvpp2_stop_dev(struct mvpp2_port *port)
5847{
Philippe Reynes8e072692016-06-28 00:08:11 +02005848 struct net_device *ndev = port->dev;
5849
Marcin Wojtas3f518502014-07-10 16:52:13 -03005850 /* Stop new packets from arriving to RXQs */
5851 mvpp2_ingress_disable(port);
5852
5853 mdelay(10);
5854
5855 /* Disable interrupts on all CPUs */
5856 mvpp2_interrupts_disable(port);
5857
5858 napi_disable(&port->napi);
5859
5860 netif_carrier_off(port->dev);
5861 netif_tx_stop_all_queues(port->dev);
5862
5863 mvpp2_egress_disable(port);
5864 mvpp2_port_disable(port);
Philippe Reynes8e072692016-06-28 00:08:11 +02005865 phy_stop(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005866}
5867
Marcin Wojtas3f518502014-07-10 16:52:13 -03005868static int mvpp2_check_ringparam_valid(struct net_device *dev,
5869 struct ethtool_ringparam *ring)
5870{
5871 u16 new_rx_pending = ring->rx_pending;
5872 u16 new_tx_pending = ring->tx_pending;
5873
5874 if (ring->rx_pending == 0 || ring->tx_pending == 0)
5875 return -EINVAL;
5876
5877 if (ring->rx_pending > MVPP2_MAX_RXD)
5878 new_rx_pending = MVPP2_MAX_RXD;
5879 else if (!IS_ALIGNED(ring->rx_pending, 16))
5880 new_rx_pending = ALIGN(ring->rx_pending, 16);
5881
5882 if (ring->tx_pending > MVPP2_MAX_TXD)
5883 new_tx_pending = MVPP2_MAX_TXD;
5884 else if (!IS_ALIGNED(ring->tx_pending, 32))
5885 new_tx_pending = ALIGN(ring->tx_pending, 32);
5886
5887 if (ring->rx_pending != new_rx_pending) {
5888 netdev_info(dev, "illegal Rx ring size value %d, round to %d\n",
5889 ring->rx_pending, new_rx_pending);
5890 ring->rx_pending = new_rx_pending;
5891 }
5892
5893 if (ring->tx_pending != new_tx_pending) {
5894 netdev_info(dev, "illegal Tx ring size value %d, round to %d\n",
5895 ring->tx_pending, new_tx_pending);
5896 ring->tx_pending = new_tx_pending;
5897 }
5898
5899 return 0;
5900}
5901
Thomas Petazzoni26975822017-03-07 16:53:14 +01005902static void mvpp21_get_mac_address(struct mvpp2_port *port, unsigned char *addr)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005903{
5904 u32 mac_addr_l, mac_addr_m, mac_addr_h;
5905
5906 mac_addr_l = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
5907 mac_addr_m = readl(port->priv->lms_base + MVPP2_SRC_ADDR_MIDDLE);
5908 mac_addr_h = readl(port->priv->lms_base + MVPP2_SRC_ADDR_HIGH);
5909 addr[0] = (mac_addr_h >> 24) & 0xFF;
5910 addr[1] = (mac_addr_h >> 16) & 0xFF;
5911 addr[2] = (mac_addr_h >> 8) & 0xFF;
5912 addr[3] = mac_addr_h & 0xFF;
5913 addr[4] = mac_addr_m & 0xFF;
5914 addr[5] = (mac_addr_l >> MVPP2_GMAC_SA_LOW_OFFS) & 0xFF;
5915}
5916
5917static int mvpp2_phy_connect(struct mvpp2_port *port)
5918{
5919 struct phy_device *phy_dev;
5920
5921 phy_dev = of_phy_connect(port->dev, port->phy_node, mvpp2_link_event, 0,
5922 port->phy_interface);
5923 if (!phy_dev) {
5924 netdev_err(port->dev, "cannot connect to phy\n");
5925 return -ENODEV;
5926 }
5927 phy_dev->supported &= PHY_GBIT_FEATURES;
5928 phy_dev->advertising = phy_dev->supported;
5929
Marcin Wojtas3f518502014-07-10 16:52:13 -03005930 port->link = 0;
5931 port->duplex = 0;
5932 port->speed = 0;
5933
5934 return 0;
5935}
5936
5937static void mvpp2_phy_disconnect(struct mvpp2_port *port)
5938{
Philippe Reynes8e072692016-06-28 00:08:11 +02005939 struct net_device *ndev = port->dev;
5940
5941 phy_disconnect(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005942}
5943
5944static int mvpp2_open(struct net_device *dev)
5945{
5946 struct mvpp2_port *port = netdev_priv(dev);
5947 unsigned char mac_bcast[ETH_ALEN] = {
5948 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
5949 int err;
5950
5951 err = mvpp2_prs_mac_da_accept(port->priv, port->id, mac_bcast, true);
5952 if (err) {
5953 netdev_err(dev, "mvpp2_prs_mac_da_accept BC failed\n");
5954 return err;
5955 }
5956 err = mvpp2_prs_mac_da_accept(port->priv, port->id,
5957 dev->dev_addr, true);
5958 if (err) {
5959 netdev_err(dev, "mvpp2_prs_mac_da_accept MC failed\n");
5960 return err;
5961 }
5962 err = mvpp2_prs_tag_mode_set(port->priv, port->id, MVPP2_TAG_TYPE_MH);
5963 if (err) {
5964 netdev_err(dev, "mvpp2_prs_tag_mode_set failed\n");
5965 return err;
5966 }
5967 err = mvpp2_prs_def_flow(port);
5968 if (err) {
5969 netdev_err(dev, "mvpp2_prs_def_flow failed\n");
5970 return err;
5971 }
5972
5973 /* Allocate the Rx/Tx queues */
5974 err = mvpp2_setup_rxqs(port);
5975 if (err) {
5976 netdev_err(port->dev, "cannot allocate Rx queues\n");
5977 return err;
5978 }
5979
5980 err = mvpp2_setup_txqs(port);
5981 if (err) {
5982 netdev_err(port->dev, "cannot allocate Tx queues\n");
5983 goto err_cleanup_rxqs;
5984 }
5985
5986 err = request_irq(port->irq, mvpp2_isr, 0, dev->name, port);
5987 if (err) {
5988 netdev_err(port->dev, "cannot request IRQ %d\n", port->irq);
5989 goto err_cleanup_txqs;
5990 }
5991
5992 /* In default link is down */
5993 netif_carrier_off(port->dev);
5994
5995 err = mvpp2_phy_connect(port);
5996 if (err < 0)
5997 goto err_free_irq;
5998
5999 /* Unmask interrupts on all CPUs */
6000 on_each_cpu(mvpp2_interrupts_unmask, port, 1);
6001
6002 mvpp2_start_dev(port);
6003
6004 return 0;
6005
6006err_free_irq:
6007 free_irq(port->irq, port);
6008err_cleanup_txqs:
6009 mvpp2_cleanup_txqs(port);
6010err_cleanup_rxqs:
6011 mvpp2_cleanup_rxqs(port);
6012 return err;
6013}
6014
6015static int mvpp2_stop(struct net_device *dev)
6016{
6017 struct mvpp2_port *port = netdev_priv(dev);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006018 struct mvpp2_port_pcpu *port_pcpu;
6019 int cpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006020
6021 mvpp2_stop_dev(port);
6022 mvpp2_phy_disconnect(port);
6023
6024 /* Mask interrupts on all CPUs */
6025 on_each_cpu(mvpp2_interrupts_mask, port, 1);
6026
6027 free_irq(port->irq, port);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006028 for_each_present_cpu(cpu) {
6029 port_pcpu = per_cpu_ptr(port->pcpu, cpu);
6030
6031 hrtimer_cancel(&port_pcpu->tx_done_timer);
6032 port_pcpu->timer_scheduled = false;
6033 tasklet_kill(&port_pcpu->tx_done_tasklet);
6034 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006035 mvpp2_cleanup_rxqs(port);
6036 mvpp2_cleanup_txqs(port);
6037
6038 return 0;
6039}
6040
6041static void mvpp2_set_rx_mode(struct net_device *dev)
6042{
6043 struct mvpp2_port *port = netdev_priv(dev);
6044 struct mvpp2 *priv = port->priv;
6045 struct netdev_hw_addr *ha;
6046 int id = port->id;
6047 bool allmulti = dev->flags & IFF_ALLMULTI;
6048
6049 mvpp2_prs_mac_promisc_set(priv, id, dev->flags & IFF_PROMISC);
6050 mvpp2_prs_mac_multi_set(priv, id, MVPP2_PE_MAC_MC_ALL, allmulti);
6051 mvpp2_prs_mac_multi_set(priv, id, MVPP2_PE_MAC_MC_IP6, allmulti);
6052
6053 /* Remove all port->id's mcast enries */
6054 mvpp2_prs_mcast_del_all(priv, id);
6055
6056 if (allmulti && !netdev_mc_empty(dev)) {
6057 netdev_for_each_mc_addr(ha, dev)
6058 mvpp2_prs_mac_da_accept(priv, id, ha->addr, true);
6059 }
6060}
6061
6062static int mvpp2_set_mac_address(struct net_device *dev, void *p)
6063{
6064 struct mvpp2_port *port = netdev_priv(dev);
6065 const struct sockaddr *addr = p;
6066 int err;
6067
6068 if (!is_valid_ether_addr(addr->sa_data)) {
6069 err = -EADDRNOTAVAIL;
6070 goto error;
6071 }
6072
6073 if (!netif_running(dev)) {
6074 err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
6075 if (!err)
6076 return 0;
6077 /* Reconfigure parser to accept the original MAC address */
6078 err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
6079 if (err)
6080 goto error;
6081 }
6082
6083 mvpp2_stop_dev(port);
6084
6085 err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
6086 if (!err)
6087 goto out_start;
6088
6089 /* Reconfigure parser accept the original MAC address */
6090 err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
6091 if (err)
6092 goto error;
6093out_start:
6094 mvpp2_start_dev(port);
6095 mvpp2_egress_enable(port);
6096 mvpp2_ingress_enable(port);
6097 return 0;
6098
6099error:
6100 netdev_err(dev, "fail to change MAC address\n");
6101 return err;
6102}
6103
6104static int mvpp2_change_mtu(struct net_device *dev, int mtu)
6105{
6106 struct mvpp2_port *port = netdev_priv(dev);
6107 int err;
6108
Jarod Wilson57779872016-10-17 15:54:06 -04006109 if (!IS_ALIGNED(MVPP2_RX_PKT_SIZE(mtu), 8)) {
6110 netdev_info(dev, "illegal MTU value %d, round to %d\n", mtu,
6111 ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8));
6112 mtu = ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006113 }
6114
6115 if (!netif_running(dev)) {
6116 err = mvpp2_bm_update_mtu(dev, mtu);
6117 if (!err) {
6118 port->pkt_size = MVPP2_RX_PKT_SIZE(mtu);
6119 return 0;
6120 }
6121
6122 /* Reconfigure BM to the original MTU */
6123 err = mvpp2_bm_update_mtu(dev, dev->mtu);
6124 if (err)
6125 goto error;
6126 }
6127
6128 mvpp2_stop_dev(port);
6129
6130 err = mvpp2_bm_update_mtu(dev, mtu);
6131 if (!err) {
6132 port->pkt_size = MVPP2_RX_PKT_SIZE(mtu);
6133 goto out_start;
6134 }
6135
6136 /* Reconfigure BM to the original MTU */
6137 err = mvpp2_bm_update_mtu(dev, dev->mtu);
6138 if (err)
6139 goto error;
6140
6141out_start:
6142 mvpp2_start_dev(port);
6143 mvpp2_egress_enable(port);
6144 mvpp2_ingress_enable(port);
6145
6146 return 0;
6147
6148error:
6149 netdev_err(dev, "fail to change MTU\n");
6150 return err;
6151}
6152
stephen hemmingerbc1f4472017-01-06 19:12:52 -08006153static void
Marcin Wojtas3f518502014-07-10 16:52:13 -03006154mvpp2_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
6155{
6156 struct mvpp2_port *port = netdev_priv(dev);
6157 unsigned int start;
6158 int cpu;
6159
6160 for_each_possible_cpu(cpu) {
6161 struct mvpp2_pcpu_stats *cpu_stats;
6162 u64 rx_packets;
6163 u64 rx_bytes;
6164 u64 tx_packets;
6165 u64 tx_bytes;
6166
6167 cpu_stats = per_cpu_ptr(port->stats, cpu);
6168 do {
6169 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
6170 rx_packets = cpu_stats->rx_packets;
6171 rx_bytes = cpu_stats->rx_bytes;
6172 tx_packets = cpu_stats->tx_packets;
6173 tx_bytes = cpu_stats->tx_bytes;
6174 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
6175
6176 stats->rx_packets += rx_packets;
6177 stats->rx_bytes += rx_bytes;
6178 stats->tx_packets += tx_packets;
6179 stats->tx_bytes += tx_bytes;
6180 }
6181
6182 stats->rx_errors = dev->stats.rx_errors;
6183 stats->rx_dropped = dev->stats.rx_dropped;
6184 stats->tx_dropped = dev->stats.tx_dropped;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006185}
6186
Thomas Petazzonibd695a52014-07-27 23:21:36 +02006187static int mvpp2_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
6188{
Thomas Petazzonibd695a52014-07-27 23:21:36 +02006189 int ret;
6190
Philippe Reynes8e072692016-06-28 00:08:11 +02006191 if (!dev->phydev)
Thomas Petazzonibd695a52014-07-27 23:21:36 +02006192 return -ENOTSUPP;
6193
Philippe Reynes8e072692016-06-28 00:08:11 +02006194 ret = phy_mii_ioctl(dev->phydev, ifr, cmd);
Thomas Petazzonibd695a52014-07-27 23:21:36 +02006195 if (!ret)
6196 mvpp2_link_event(dev);
6197
6198 return ret;
6199}
6200
Marcin Wojtas3f518502014-07-10 16:52:13 -03006201/* Ethtool methods */
6202
Marcin Wojtas3f518502014-07-10 16:52:13 -03006203/* Set interrupt coalescing for ethtools */
6204static int mvpp2_ethtool_set_coalesce(struct net_device *dev,
6205 struct ethtool_coalesce *c)
6206{
6207 struct mvpp2_port *port = netdev_priv(dev);
6208 int queue;
6209
6210 for (queue = 0; queue < rxq_number; queue++) {
6211 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
6212
6213 rxq->time_coal = c->rx_coalesce_usecs;
6214 rxq->pkts_coal = c->rx_max_coalesced_frames;
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01006215 mvpp2_rx_pkts_coal_set(port, rxq);
6216 mvpp2_rx_time_coal_set(port, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006217 }
6218
6219 for (queue = 0; queue < txq_number; queue++) {
6220 struct mvpp2_tx_queue *txq = port->txqs[queue];
6221
6222 txq->done_pkts_coal = c->tx_max_coalesced_frames;
6223 }
6224
Marcin Wojtas3f518502014-07-10 16:52:13 -03006225 return 0;
6226}
6227
6228/* get coalescing for ethtools */
6229static int mvpp2_ethtool_get_coalesce(struct net_device *dev,
6230 struct ethtool_coalesce *c)
6231{
6232 struct mvpp2_port *port = netdev_priv(dev);
6233
6234 c->rx_coalesce_usecs = port->rxqs[0]->time_coal;
6235 c->rx_max_coalesced_frames = port->rxqs[0]->pkts_coal;
6236 c->tx_max_coalesced_frames = port->txqs[0]->done_pkts_coal;
6237 return 0;
6238}
6239
6240static void mvpp2_ethtool_get_drvinfo(struct net_device *dev,
6241 struct ethtool_drvinfo *drvinfo)
6242{
6243 strlcpy(drvinfo->driver, MVPP2_DRIVER_NAME,
6244 sizeof(drvinfo->driver));
6245 strlcpy(drvinfo->version, MVPP2_DRIVER_VERSION,
6246 sizeof(drvinfo->version));
6247 strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
6248 sizeof(drvinfo->bus_info));
6249}
6250
6251static void mvpp2_ethtool_get_ringparam(struct net_device *dev,
6252 struct ethtool_ringparam *ring)
6253{
6254 struct mvpp2_port *port = netdev_priv(dev);
6255
6256 ring->rx_max_pending = MVPP2_MAX_RXD;
6257 ring->tx_max_pending = MVPP2_MAX_TXD;
6258 ring->rx_pending = port->rx_ring_size;
6259 ring->tx_pending = port->tx_ring_size;
6260}
6261
6262static int mvpp2_ethtool_set_ringparam(struct net_device *dev,
6263 struct ethtool_ringparam *ring)
6264{
6265 struct mvpp2_port *port = netdev_priv(dev);
6266 u16 prev_rx_ring_size = port->rx_ring_size;
6267 u16 prev_tx_ring_size = port->tx_ring_size;
6268 int err;
6269
6270 err = mvpp2_check_ringparam_valid(dev, ring);
6271 if (err)
6272 return err;
6273
6274 if (!netif_running(dev)) {
6275 port->rx_ring_size = ring->rx_pending;
6276 port->tx_ring_size = ring->tx_pending;
6277 return 0;
6278 }
6279
6280 /* The interface is running, so we have to force a
6281 * reallocation of the queues
6282 */
6283 mvpp2_stop_dev(port);
6284 mvpp2_cleanup_rxqs(port);
6285 mvpp2_cleanup_txqs(port);
6286
6287 port->rx_ring_size = ring->rx_pending;
6288 port->tx_ring_size = ring->tx_pending;
6289
6290 err = mvpp2_setup_rxqs(port);
6291 if (err) {
6292 /* Reallocate Rx queues with the original ring size */
6293 port->rx_ring_size = prev_rx_ring_size;
6294 ring->rx_pending = prev_rx_ring_size;
6295 err = mvpp2_setup_rxqs(port);
6296 if (err)
6297 goto err_out;
6298 }
6299 err = mvpp2_setup_txqs(port);
6300 if (err) {
6301 /* Reallocate Tx queues with the original ring size */
6302 port->tx_ring_size = prev_tx_ring_size;
6303 ring->tx_pending = prev_tx_ring_size;
6304 err = mvpp2_setup_txqs(port);
6305 if (err)
6306 goto err_clean_rxqs;
6307 }
6308
6309 mvpp2_start_dev(port);
6310 mvpp2_egress_enable(port);
6311 mvpp2_ingress_enable(port);
6312
6313 return 0;
6314
6315err_clean_rxqs:
6316 mvpp2_cleanup_rxqs(port);
6317err_out:
6318 netdev_err(dev, "fail to change ring parameters");
6319 return err;
6320}
6321
6322/* Device ops */
6323
6324static const struct net_device_ops mvpp2_netdev_ops = {
6325 .ndo_open = mvpp2_open,
6326 .ndo_stop = mvpp2_stop,
6327 .ndo_start_xmit = mvpp2_tx,
6328 .ndo_set_rx_mode = mvpp2_set_rx_mode,
6329 .ndo_set_mac_address = mvpp2_set_mac_address,
6330 .ndo_change_mtu = mvpp2_change_mtu,
6331 .ndo_get_stats64 = mvpp2_get_stats64,
Thomas Petazzonibd695a52014-07-27 23:21:36 +02006332 .ndo_do_ioctl = mvpp2_ioctl,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006333};
6334
6335static const struct ethtool_ops mvpp2_eth_tool_ops = {
Florian Fainelli00606c42016-11-15 11:19:48 -08006336 .nway_reset = phy_ethtool_nway_reset,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006337 .get_link = ethtool_op_get_link,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006338 .set_coalesce = mvpp2_ethtool_set_coalesce,
6339 .get_coalesce = mvpp2_ethtool_get_coalesce,
6340 .get_drvinfo = mvpp2_ethtool_get_drvinfo,
6341 .get_ringparam = mvpp2_ethtool_get_ringparam,
6342 .set_ringparam = mvpp2_ethtool_set_ringparam,
Philippe Reynesfb773e92016-06-28 00:08:12 +02006343 .get_link_ksettings = phy_ethtool_get_link_ksettings,
6344 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006345};
6346
Marcin Wojtas3f518502014-07-10 16:52:13 -03006347/* Initialize port HW */
6348static int mvpp2_port_init(struct mvpp2_port *port)
6349{
6350 struct device *dev = port->dev->dev.parent;
6351 struct mvpp2 *priv = port->priv;
6352 struct mvpp2_txq_pcpu *txq_pcpu;
6353 int queue, cpu, err;
6354
6355 if (port->first_rxq + rxq_number > MVPP2_RXQ_TOTAL_NUM)
6356 return -EINVAL;
6357
6358 /* Disable port */
6359 mvpp2_egress_disable(port);
6360 mvpp2_port_disable(port);
6361
6362 port->txqs = devm_kcalloc(dev, txq_number, sizeof(*port->txqs),
6363 GFP_KERNEL);
6364 if (!port->txqs)
6365 return -ENOMEM;
6366
6367 /* Associate physical Tx queues to this port and initialize.
6368 * The mapping is predefined.
6369 */
6370 for (queue = 0; queue < txq_number; queue++) {
6371 int queue_phy_id = mvpp2_txq_phys(port->id, queue);
6372 struct mvpp2_tx_queue *txq;
6373
6374 txq = devm_kzalloc(dev, sizeof(*txq), GFP_KERNEL);
Christophe Jaillet177c8d12017-02-19 10:19:57 +01006375 if (!txq) {
6376 err = -ENOMEM;
6377 goto err_free_percpu;
6378 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006379
6380 txq->pcpu = alloc_percpu(struct mvpp2_txq_pcpu);
6381 if (!txq->pcpu) {
6382 err = -ENOMEM;
6383 goto err_free_percpu;
6384 }
6385
6386 txq->id = queue_phy_id;
6387 txq->log_id = queue;
6388 txq->done_pkts_coal = MVPP2_TXDONE_COAL_PKTS_THRESH;
6389 for_each_present_cpu(cpu) {
6390 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
6391 txq_pcpu->cpu = cpu;
6392 }
6393
6394 port->txqs[queue] = txq;
6395 }
6396
6397 port->rxqs = devm_kcalloc(dev, rxq_number, sizeof(*port->rxqs),
6398 GFP_KERNEL);
6399 if (!port->rxqs) {
6400 err = -ENOMEM;
6401 goto err_free_percpu;
6402 }
6403
6404 /* Allocate and initialize Rx queue for this port */
6405 for (queue = 0; queue < rxq_number; queue++) {
6406 struct mvpp2_rx_queue *rxq;
6407
6408 /* Map physical Rx queue to port's logical Rx queue */
6409 rxq = devm_kzalloc(dev, sizeof(*rxq), GFP_KERNEL);
Jisheng Zhangd82b0c22016-03-31 17:01:23 +08006410 if (!rxq) {
6411 err = -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006412 goto err_free_percpu;
Jisheng Zhangd82b0c22016-03-31 17:01:23 +08006413 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006414 /* Map this Rx queue to a physical queue */
6415 rxq->id = port->first_rxq + queue;
6416 rxq->port = port->id;
6417 rxq->logic_rxq = queue;
6418
6419 port->rxqs[queue] = rxq;
6420 }
6421
6422 /* Configure Rx queue group interrupt for this port */
Thomas Petazzonia73fef12017-03-07 16:53:16 +01006423 if (priv->hw_version == MVPP21) {
6424 mvpp2_write(priv, MVPP21_ISR_RXQ_GROUP_REG(port->id),
6425 rxq_number);
6426 } else {
6427 u32 val;
6428
6429 val = (port->id << MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_OFFSET);
6430 mvpp2_write(priv, MVPP22_ISR_RXQ_GROUP_INDEX_REG, val);
6431
6432 val = (rxq_number << MVPP22_ISR_RXQ_SUB_GROUP_SIZE_OFFSET);
6433 mvpp2_write(priv, MVPP22_ISR_RXQ_SUB_GROUP_CONFIG_REG, val);
6434 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006435
6436 /* Create Rx descriptor rings */
6437 for (queue = 0; queue < rxq_number; queue++) {
6438 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
6439
6440 rxq->size = port->rx_ring_size;
6441 rxq->pkts_coal = MVPP2_RX_COAL_PKTS;
6442 rxq->time_coal = MVPP2_RX_COAL_USEC;
6443 }
6444
6445 mvpp2_ingress_disable(port);
6446
6447 /* Port default configuration */
6448 mvpp2_defaults_set(port);
6449
6450 /* Port's classifier configuration */
6451 mvpp2_cls_oversize_rxq_set(port);
6452 mvpp2_cls_port_config(port);
6453
6454 /* Provide an initial Rx packet size */
6455 port->pkt_size = MVPP2_RX_PKT_SIZE(port->dev->mtu);
6456
6457 /* Initialize pools for swf */
6458 err = mvpp2_swf_bm_pool_init(port);
6459 if (err)
6460 goto err_free_percpu;
6461
6462 return 0;
6463
6464err_free_percpu:
6465 for (queue = 0; queue < txq_number; queue++) {
6466 if (!port->txqs[queue])
6467 continue;
6468 free_percpu(port->txqs[queue]->pcpu);
6469 }
6470 return err;
6471}
6472
6473/* Ports initialization */
6474static int mvpp2_port_probe(struct platform_device *pdev,
6475 struct device_node *port_node,
6476 struct mvpp2 *priv,
6477 int *next_first_rxq)
6478{
6479 struct device_node *phy_node;
6480 struct mvpp2_port *port;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006481 struct mvpp2_port_pcpu *port_pcpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006482 struct net_device *dev;
6483 struct resource *res;
6484 const char *dt_mac_addr;
6485 const char *mac_from;
6486 char hw_mac_addr[ETH_ALEN];
6487 u32 id;
6488 int features;
6489 int phy_mode;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006490 int err, i, cpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006491
6492 dev = alloc_etherdev_mqs(sizeof(struct mvpp2_port), txq_number,
6493 rxq_number);
6494 if (!dev)
6495 return -ENOMEM;
6496
6497 phy_node = of_parse_phandle(port_node, "phy", 0);
6498 if (!phy_node) {
6499 dev_err(&pdev->dev, "missing phy\n");
6500 err = -ENODEV;
6501 goto err_free_netdev;
6502 }
6503
6504 phy_mode = of_get_phy_mode(port_node);
6505 if (phy_mode < 0) {
6506 dev_err(&pdev->dev, "incorrect phy mode\n");
6507 err = phy_mode;
6508 goto err_free_netdev;
6509 }
6510
6511 if (of_property_read_u32(port_node, "port-id", &id)) {
6512 err = -EINVAL;
6513 dev_err(&pdev->dev, "missing port-id value\n");
6514 goto err_free_netdev;
6515 }
6516
6517 dev->tx_queue_len = MVPP2_MAX_TXD;
6518 dev->watchdog_timeo = 5 * HZ;
6519 dev->netdev_ops = &mvpp2_netdev_ops;
6520 dev->ethtool_ops = &mvpp2_eth_tool_ops;
6521
6522 port = netdev_priv(dev);
6523
6524 port->irq = irq_of_parse_and_map(port_node, 0);
6525 if (port->irq <= 0) {
6526 err = -EINVAL;
6527 goto err_free_netdev;
6528 }
6529
6530 if (of_property_read_bool(port_node, "marvell,loopback"))
6531 port->flags |= MVPP2_F_LOOPBACK;
6532
6533 port->priv = priv;
6534 port->id = id;
6535 port->first_rxq = *next_first_rxq;
6536 port->phy_node = phy_node;
6537 port->phy_interface = phy_mode;
6538
Thomas Petazzonia7868412017-03-07 16:53:13 +01006539 if (priv->hw_version == MVPP21) {
6540 res = platform_get_resource(pdev, IORESOURCE_MEM, 2 + id);
6541 port->base = devm_ioremap_resource(&pdev->dev, res);
6542 if (IS_ERR(port->base)) {
6543 err = PTR_ERR(port->base);
6544 goto err_free_irq;
6545 }
6546 } else {
6547 if (of_property_read_u32(port_node, "gop-port-id",
6548 &port->gop_id)) {
6549 err = -EINVAL;
6550 dev_err(&pdev->dev, "missing gop-port-id value\n");
6551 goto err_free_irq;
6552 }
6553
6554 port->base = priv->iface_base + MVPP22_GMAC_BASE(port->gop_id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006555 }
6556
6557 /* Alloc per-cpu stats */
6558 port->stats = netdev_alloc_pcpu_stats(struct mvpp2_pcpu_stats);
6559 if (!port->stats) {
6560 err = -ENOMEM;
6561 goto err_free_irq;
6562 }
6563
6564 dt_mac_addr = of_get_mac_address(port_node);
6565 if (dt_mac_addr && is_valid_ether_addr(dt_mac_addr)) {
6566 mac_from = "device tree";
6567 ether_addr_copy(dev->dev_addr, dt_mac_addr);
6568 } else {
Thomas Petazzoni26975822017-03-07 16:53:14 +01006569 if (priv->hw_version == MVPP21)
6570 mvpp21_get_mac_address(port, hw_mac_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006571 if (is_valid_ether_addr(hw_mac_addr)) {
6572 mac_from = "hardware";
6573 ether_addr_copy(dev->dev_addr, hw_mac_addr);
6574 } else {
6575 mac_from = "random";
6576 eth_hw_addr_random(dev);
6577 }
6578 }
6579
6580 port->tx_ring_size = MVPP2_MAX_TXD;
6581 port->rx_ring_size = MVPP2_MAX_RXD;
6582 port->dev = dev;
6583 SET_NETDEV_DEV(dev, &pdev->dev);
6584
6585 err = mvpp2_port_init(port);
6586 if (err < 0) {
6587 dev_err(&pdev->dev, "failed to init port %d\n", id);
6588 goto err_free_stats;
6589 }
Thomas Petazzoni26975822017-03-07 16:53:14 +01006590
6591 mvpp2_port_mii_set(port);
6592 mvpp2_port_periodic_xon_disable(port);
6593
6594 if (priv->hw_version == MVPP21)
6595 mvpp2_port_fc_adv_enable(port);
6596
6597 mvpp2_port_reset(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006598
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006599 port->pcpu = alloc_percpu(struct mvpp2_port_pcpu);
6600 if (!port->pcpu) {
6601 err = -ENOMEM;
6602 goto err_free_txq_pcpu;
6603 }
6604
6605 for_each_present_cpu(cpu) {
6606 port_pcpu = per_cpu_ptr(port->pcpu, cpu);
6607
6608 hrtimer_init(&port_pcpu->tx_done_timer, CLOCK_MONOTONIC,
6609 HRTIMER_MODE_REL_PINNED);
6610 port_pcpu->tx_done_timer.function = mvpp2_hr_timer_cb;
6611 port_pcpu->timer_scheduled = false;
6612
6613 tasklet_init(&port_pcpu->tx_done_tasklet, mvpp2_tx_proc_cb,
6614 (unsigned long)dev);
6615 }
6616
Marcin Wojtas3f518502014-07-10 16:52:13 -03006617 netif_napi_add(dev, &port->napi, mvpp2_poll, NAPI_POLL_WEIGHT);
6618 features = NETIF_F_SG | NETIF_F_IP_CSUM;
6619 dev->features = features | NETIF_F_RXCSUM;
6620 dev->hw_features |= features | NETIF_F_RXCSUM | NETIF_F_GRO;
6621 dev->vlan_features |= features;
6622
Jarod Wilson57779872016-10-17 15:54:06 -04006623 /* MTU range: 68 - 9676 */
6624 dev->min_mtu = ETH_MIN_MTU;
6625 /* 9676 == 9700 - 20 and rounding to 8 */
6626 dev->max_mtu = 9676;
6627
Marcin Wojtas3f518502014-07-10 16:52:13 -03006628 err = register_netdev(dev);
6629 if (err < 0) {
6630 dev_err(&pdev->dev, "failed to register netdev\n");
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006631 goto err_free_port_pcpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006632 }
6633 netdev_info(dev, "Using %s mac address %pM\n", mac_from, dev->dev_addr);
6634
6635 /* Increment the first Rx queue number to be used by the next port */
6636 *next_first_rxq += rxq_number;
6637 priv->port_list[id] = port;
6638 return 0;
6639
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006640err_free_port_pcpu:
6641 free_percpu(port->pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006642err_free_txq_pcpu:
6643 for (i = 0; i < txq_number; i++)
6644 free_percpu(port->txqs[i]->pcpu);
6645err_free_stats:
6646 free_percpu(port->stats);
6647err_free_irq:
6648 irq_dispose_mapping(port->irq);
6649err_free_netdev:
Peter Chenccb80392016-08-01 15:02:37 +08006650 of_node_put(phy_node);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006651 free_netdev(dev);
6652 return err;
6653}
6654
6655/* Ports removal routine */
6656static void mvpp2_port_remove(struct mvpp2_port *port)
6657{
6658 int i;
6659
6660 unregister_netdev(port->dev);
Peter Chenccb80392016-08-01 15:02:37 +08006661 of_node_put(port->phy_node);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006662 free_percpu(port->pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006663 free_percpu(port->stats);
6664 for (i = 0; i < txq_number; i++)
6665 free_percpu(port->txqs[i]->pcpu);
6666 irq_dispose_mapping(port->irq);
6667 free_netdev(port->dev);
6668}
6669
6670/* Initialize decoding windows */
6671static void mvpp2_conf_mbus_windows(const struct mbus_dram_target_info *dram,
6672 struct mvpp2 *priv)
6673{
6674 u32 win_enable;
6675 int i;
6676
6677 for (i = 0; i < 6; i++) {
6678 mvpp2_write(priv, MVPP2_WIN_BASE(i), 0);
6679 mvpp2_write(priv, MVPP2_WIN_SIZE(i), 0);
6680
6681 if (i < 4)
6682 mvpp2_write(priv, MVPP2_WIN_REMAP(i), 0);
6683 }
6684
6685 win_enable = 0;
6686
6687 for (i = 0; i < dram->num_cs; i++) {
6688 const struct mbus_dram_window *cs = dram->cs + i;
6689
6690 mvpp2_write(priv, MVPP2_WIN_BASE(i),
6691 (cs->base & 0xffff0000) | (cs->mbus_attr << 8) |
6692 dram->mbus_dram_target_id);
6693
6694 mvpp2_write(priv, MVPP2_WIN_SIZE(i),
6695 (cs->size - 1) & 0xffff0000);
6696
6697 win_enable |= (1 << i);
6698 }
6699
6700 mvpp2_write(priv, MVPP2_BASE_ADDR_ENABLE, win_enable);
6701}
6702
6703/* Initialize Rx FIFO's */
6704static void mvpp2_rx_fifo_init(struct mvpp2 *priv)
6705{
6706 int port;
6707
6708 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
6709 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
6710 MVPP2_RX_FIFO_PORT_DATA_SIZE);
6711 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
6712 MVPP2_RX_FIFO_PORT_ATTR_SIZE);
6713 }
6714
6715 mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
6716 MVPP2_RX_FIFO_PORT_MIN_PKT);
6717 mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
6718}
6719
Thomas Petazzoni6763ce32017-03-07 16:53:15 +01006720static void mvpp2_axi_init(struct mvpp2 *priv)
6721{
6722 u32 val, rdval, wrval;
6723
6724 mvpp2_write(priv, MVPP22_BM_ADDR_HIGH_RLS_REG, 0x0);
6725
6726 /* AXI Bridge Configuration */
6727
6728 rdval = MVPP22_AXI_CODE_CACHE_RD_CACHE
6729 << MVPP22_AXI_ATTR_CACHE_OFFS;
6730 rdval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
6731 << MVPP22_AXI_ATTR_DOMAIN_OFFS;
6732
6733 wrval = MVPP22_AXI_CODE_CACHE_WR_CACHE
6734 << MVPP22_AXI_ATTR_CACHE_OFFS;
6735 wrval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
6736 << MVPP22_AXI_ATTR_DOMAIN_OFFS;
6737
6738 /* BM */
6739 mvpp2_write(priv, MVPP22_AXI_BM_WR_ATTR_REG, wrval);
6740 mvpp2_write(priv, MVPP22_AXI_BM_RD_ATTR_REG, rdval);
6741
6742 /* Descriptors */
6743 mvpp2_write(priv, MVPP22_AXI_AGGRQ_DESCR_RD_ATTR_REG, rdval);
6744 mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_WR_ATTR_REG, wrval);
6745 mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_RD_ATTR_REG, rdval);
6746 mvpp2_write(priv, MVPP22_AXI_RXQ_DESCR_WR_ATTR_REG, wrval);
6747
6748 /* Buffer Data */
6749 mvpp2_write(priv, MVPP22_AXI_TX_DATA_RD_ATTR_REG, rdval);
6750 mvpp2_write(priv, MVPP22_AXI_RX_DATA_WR_ATTR_REG, wrval);
6751
6752 val = MVPP22_AXI_CODE_CACHE_NON_CACHE
6753 << MVPP22_AXI_CODE_CACHE_OFFS;
6754 val |= MVPP22_AXI_CODE_DOMAIN_SYSTEM
6755 << MVPP22_AXI_CODE_DOMAIN_OFFS;
6756 mvpp2_write(priv, MVPP22_AXI_RD_NORMAL_CODE_REG, val);
6757 mvpp2_write(priv, MVPP22_AXI_WR_NORMAL_CODE_REG, val);
6758
6759 val = MVPP22_AXI_CODE_CACHE_RD_CACHE
6760 << MVPP22_AXI_CODE_CACHE_OFFS;
6761 val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
6762 << MVPP22_AXI_CODE_DOMAIN_OFFS;
6763
6764 mvpp2_write(priv, MVPP22_AXI_RD_SNOOP_CODE_REG, val);
6765
6766 val = MVPP22_AXI_CODE_CACHE_WR_CACHE
6767 << MVPP22_AXI_CODE_CACHE_OFFS;
6768 val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
6769 << MVPP22_AXI_CODE_DOMAIN_OFFS;
6770
6771 mvpp2_write(priv, MVPP22_AXI_WR_SNOOP_CODE_REG, val);
6772}
6773
Marcin Wojtas3f518502014-07-10 16:52:13 -03006774/* Initialize network controller common part HW */
6775static int mvpp2_init(struct platform_device *pdev, struct mvpp2 *priv)
6776{
6777 const struct mbus_dram_target_info *dram_target_info;
6778 int err, i;
Marcin Wojtas08a23752014-07-21 13:48:12 -03006779 u32 val;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006780
6781 /* Checks for hardware constraints */
6782 if (rxq_number % 4 || (rxq_number > MVPP2_MAX_RXQ) ||
6783 (txq_number > MVPP2_MAX_TXQ)) {
6784 dev_err(&pdev->dev, "invalid queue size parameter\n");
6785 return -EINVAL;
6786 }
6787
6788 /* MBUS windows configuration */
6789 dram_target_info = mv_mbus_dram_info();
6790 if (dram_target_info)
6791 mvpp2_conf_mbus_windows(dram_target_info, priv);
6792
Thomas Petazzoni6763ce32017-03-07 16:53:15 +01006793 if (priv->hw_version == MVPP22)
6794 mvpp2_axi_init(priv);
6795
Marcin Wojtas08a23752014-07-21 13:48:12 -03006796 /* Disable HW PHY polling */
Thomas Petazzoni26975822017-03-07 16:53:14 +01006797 if (priv->hw_version == MVPP21) {
6798 val = readl(priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
6799 val |= MVPP2_PHY_AN_STOP_SMI0_MASK;
6800 writel(val, priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
6801 } else {
6802 val = readl(priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
6803 val &= ~MVPP22_SMI_POLLING_EN;
6804 writel(val, priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
6805 }
Marcin Wojtas08a23752014-07-21 13:48:12 -03006806
Marcin Wojtas3f518502014-07-10 16:52:13 -03006807 /* Allocate and initialize aggregated TXQs */
6808 priv->aggr_txqs = devm_kcalloc(&pdev->dev, num_present_cpus(),
6809 sizeof(struct mvpp2_tx_queue),
6810 GFP_KERNEL);
6811 if (!priv->aggr_txqs)
6812 return -ENOMEM;
6813
6814 for_each_present_cpu(i) {
6815 priv->aggr_txqs[i].id = i;
6816 priv->aggr_txqs[i].size = MVPP2_AGGR_TXQ_SIZE;
6817 err = mvpp2_aggr_txq_init(pdev, &priv->aggr_txqs[i],
6818 MVPP2_AGGR_TXQ_SIZE, i, priv);
6819 if (err < 0)
6820 return err;
6821 }
6822
6823 /* Rx Fifo Init */
6824 mvpp2_rx_fifo_init(priv);
6825
6826 /* Reset Rx queue group interrupt configuration */
Thomas Petazzonia73fef12017-03-07 16:53:16 +01006827 for (i = 0; i < MVPP2_MAX_PORTS; i++) {
6828 if (priv->hw_version == MVPP21) {
6829 mvpp2_write(priv, MVPP21_ISR_RXQ_GROUP_REG(i),
6830 rxq_number);
6831 continue;
6832 } else {
6833 u32 val;
6834
6835 val = (i << MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_OFFSET);
6836 mvpp2_write(priv, MVPP22_ISR_RXQ_GROUP_INDEX_REG, val);
6837
6838 val = (rxq_number << MVPP22_ISR_RXQ_SUB_GROUP_SIZE_OFFSET);
6839 mvpp2_write(priv, MVPP22_ISR_RXQ_SUB_GROUP_CONFIG_REG, val);
6840 }
6841 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006842
Thomas Petazzoni26975822017-03-07 16:53:14 +01006843 if (priv->hw_version == MVPP21)
6844 writel(MVPP2_EXT_GLOBAL_CTRL_DEFAULT,
6845 priv->lms_base + MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006846
6847 /* Allow cache snoop when transmiting packets */
6848 mvpp2_write(priv, MVPP2_TX_SNOOP_REG, 0x1);
6849
6850 /* Buffer Manager initialization */
6851 err = mvpp2_bm_init(pdev, priv);
6852 if (err < 0)
6853 return err;
6854
6855 /* Parser default initialization */
6856 err = mvpp2_prs_default_init(pdev, priv);
6857 if (err < 0)
6858 return err;
6859
6860 /* Classifier default initialization */
6861 mvpp2_cls_init(priv);
6862
6863 return 0;
6864}
6865
6866static int mvpp2_probe(struct platform_device *pdev)
6867{
6868 struct device_node *dn = pdev->dev.of_node;
6869 struct device_node *port_node;
6870 struct mvpp2 *priv;
6871 struct resource *res;
Thomas Petazzonia7868412017-03-07 16:53:13 +01006872 void __iomem *base;
6873 int port_count, first_rxq, cpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006874 int err;
6875
6876 priv = devm_kzalloc(&pdev->dev, sizeof(struct mvpp2), GFP_KERNEL);
6877 if (!priv)
6878 return -ENOMEM;
6879
Thomas Petazzonifaca9242017-03-07 16:53:06 +01006880 priv->hw_version =
6881 (unsigned long)of_device_get_match_data(&pdev->dev);
6882
Marcin Wojtas3f518502014-07-10 16:52:13 -03006883 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thomas Petazzonia7868412017-03-07 16:53:13 +01006884 base = devm_ioremap_resource(&pdev->dev, res);
6885 if (IS_ERR(base))
6886 return PTR_ERR(base);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006887
Thomas Petazzonia7868412017-03-07 16:53:13 +01006888 if (priv->hw_version == MVPP21) {
6889 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
6890 priv->lms_base = devm_ioremap_resource(&pdev->dev, res);
6891 if (IS_ERR(priv->lms_base))
6892 return PTR_ERR(priv->lms_base);
6893 } else {
6894 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
6895 priv->iface_base = devm_ioremap_resource(&pdev->dev, res);
6896 if (IS_ERR(priv->iface_base))
6897 return PTR_ERR(priv->iface_base);
6898 }
6899
6900 for_each_present_cpu(cpu) {
6901 u32 addr_space_sz;
6902
6903 addr_space_sz = (priv->hw_version == MVPP21 ?
6904 MVPP21_ADDR_SPACE_SZ : MVPP22_ADDR_SPACE_SZ);
6905 priv->cpu_base[cpu] = base + cpu * addr_space_sz;
6906 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006907
6908 priv->pp_clk = devm_clk_get(&pdev->dev, "pp_clk");
6909 if (IS_ERR(priv->pp_clk))
6910 return PTR_ERR(priv->pp_clk);
6911 err = clk_prepare_enable(priv->pp_clk);
6912 if (err < 0)
6913 return err;
6914
6915 priv->gop_clk = devm_clk_get(&pdev->dev, "gop_clk");
6916 if (IS_ERR(priv->gop_clk)) {
6917 err = PTR_ERR(priv->gop_clk);
6918 goto err_pp_clk;
6919 }
6920 err = clk_prepare_enable(priv->gop_clk);
6921 if (err < 0)
6922 goto err_pp_clk;
6923
6924 /* Get system's tclk rate */
6925 priv->tclk = clk_get_rate(priv->pp_clk);
6926
6927 /* Initialize network controller */
6928 err = mvpp2_init(pdev, priv);
6929 if (err < 0) {
6930 dev_err(&pdev->dev, "failed to initialize controller\n");
6931 goto err_gop_clk;
6932 }
6933
6934 port_count = of_get_available_child_count(dn);
6935 if (port_count == 0) {
6936 dev_err(&pdev->dev, "no ports enabled\n");
Wei Yongjun575a1932014-07-20 22:02:43 +08006937 err = -ENODEV;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006938 goto err_gop_clk;
6939 }
6940
6941 priv->port_list = devm_kcalloc(&pdev->dev, port_count,
6942 sizeof(struct mvpp2_port *),
6943 GFP_KERNEL);
6944 if (!priv->port_list) {
6945 err = -ENOMEM;
6946 goto err_gop_clk;
6947 }
6948
6949 /* Initialize ports */
6950 first_rxq = 0;
6951 for_each_available_child_of_node(dn, port_node) {
6952 err = mvpp2_port_probe(pdev, port_node, priv, &first_rxq);
6953 if (err < 0)
6954 goto err_gop_clk;
6955 }
6956
6957 platform_set_drvdata(pdev, priv);
6958 return 0;
6959
6960err_gop_clk:
6961 clk_disable_unprepare(priv->gop_clk);
6962err_pp_clk:
6963 clk_disable_unprepare(priv->pp_clk);
6964 return err;
6965}
6966
6967static int mvpp2_remove(struct platform_device *pdev)
6968{
6969 struct mvpp2 *priv = platform_get_drvdata(pdev);
6970 struct device_node *dn = pdev->dev.of_node;
6971 struct device_node *port_node;
6972 int i = 0;
6973
6974 for_each_available_child_of_node(dn, port_node) {
6975 if (priv->port_list[i])
6976 mvpp2_port_remove(priv->port_list[i]);
6977 i++;
6978 }
6979
6980 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
6981 struct mvpp2_bm_pool *bm_pool = &priv->bm_pools[i];
6982
6983 mvpp2_bm_pool_destroy(pdev, priv, bm_pool);
6984 }
6985
6986 for_each_present_cpu(i) {
6987 struct mvpp2_tx_queue *aggr_txq = &priv->aggr_txqs[i];
6988
6989 dma_free_coherent(&pdev->dev,
6990 MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
6991 aggr_txq->descs,
Thomas Petazzoni20396132017-03-07 16:53:00 +01006992 aggr_txq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006993 }
6994
6995 clk_disable_unprepare(priv->pp_clk);
6996 clk_disable_unprepare(priv->gop_clk);
6997
6998 return 0;
6999}
7000
7001static const struct of_device_id mvpp2_match[] = {
Thomas Petazzonifaca9242017-03-07 16:53:06 +01007002 {
7003 .compatible = "marvell,armada-375-pp2",
7004 .data = (void *)MVPP21,
7005 },
Marcin Wojtas3f518502014-07-10 16:52:13 -03007006 { }
7007};
7008MODULE_DEVICE_TABLE(of, mvpp2_match);
7009
7010static struct platform_driver mvpp2_driver = {
7011 .probe = mvpp2_probe,
7012 .remove = mvpp2_remove,
7013 .driver = {
7014 .name = MVPP2_DRIVER_NAME,
7015 .of_match_table = mvpp2_match,
7016 },
7017};
7018
7019module_platform_driver(mvpp2_driver);
7020
7021MODULE_DESCRIPTION("Marvell PPv2 Ethernet Driver - www.marvell.com");
7022MODULE_AUTHOR("Marcin Wojtas <mw@semihalf.com>");
Ezequiel Garciac6340992014-07-14 10:34:47 -03007023MODULE_LICENSE("GPL v2");