blob: aa72109290c3ca3f9bf291b049278bfd0f3b6377 [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>
Antoine Ténartf84bf382017-08-22 19:08:27 +020021#include <linux/mfd/syscon.h>
Marcin Wojtas3f518502014-07-10 16:52:13 -030022#include <linux/interrupt.h>
23#include <linux/cpumask.h>
24#include <linux/of.h>
25#include <linux/of_irq.h>
26#include <linux/of_mdio.h>
27#include <linux/of_net.h>
28#include <linux/of_address.h>
Thomas Petazzonifaca9242017-03-07 16:53:06 +010029#include <linux/of_device.h>
Marcin Wojtas3f518502014-07-10 16:52:13 -030030#include <linux/phy.h>
Antoine Tenart542897d2017-08-30 10:29:15 +020031#include <linux/phy/phy.h>
Marcin Wojtas3f518502014-07-10 16:52:13 -030032#include <linux/clk.h>
Marcin Wojtasedc660f2015-08-06 19:00:30 +020033#include <linux/hrtimer.h>
34#include <linux/ktime.h>
Antoine Ténartf84bf382017-08-22 19:08:27 +020035#include <linux/regmap.h>
Marcin Wojtas3f518502014-07-10 16:52:13 -030036#include <uapi/linux/ppp_defs.h>
37#include <net/ip.h>
38#include <net/ipv6.h>
Antoine Ténart186cd4d2017-08-23 09:46:56 +020039#include <net/tso.h>
Marcin Wojtas3f518502014-07-10 16:52:13 -030040
Antoine Tenart7c10f972017-10-30 11:23:29 +010041/* Fifo Registers */
Marcin Wojtas3f518502014-07-10 16:52:13 -030042#define MVPP2_RX_DATA_FIFO_SIZE_REG(port) (0x00 + 4 * (port))
43#define MVPP2_RX_ATTR_FIFO_SIZE_REG(port) (0x20 + 4 * (port))
44#define MVPP2_RX_MIN_PKT_SIZE_REG 0x60
45#define MVPP2_RX_FIFO_INIT_REG 0x64
Antoine Tenart7c10f972017-10-30 11:23:29 +010046#define MVPP22_TX_FIFO_SIZE_REG(port) (0x8860 + 4 * (port))
Marcin Wojtas3f518502014-07-10 16:52:13 -030047
48/* RX DMA Top Registers */
49#define MVPP2_RX_CTRL_REG(port) (0x140 + 4 * (port))
50#define MVPP2_RX_LOW_LATENCY_PKT_SIZE(s) (((s) & 0xfff) << 16)
51#define MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK BIT(31)
52#define MVPP2_POOL_BUF_SIZE_REG(pool) (0x180 + 4 * (pool))
53#define MVPP2_POOL_BUF_SIZE_OFFSET 5
54#define MVPP2_RXQ_CONFIG_REG(rxq) (0x800 + 4 * (rxq))
55#define MVPP2_SNOOP_PKT_SIZE_MASK 0x1ff
56#define MVPP2_SNOOP_BUF_HDR_MASK BIT(9)
57#define MVPP2_RXQ_POOL_SHORT_OFFS 20
Thomas Petazzoni5eac8922017-03-07 16:53:10 +010058#define MVPP21_RXQ_POOL_SHORT_MASK 0x700000
59#define MVPP22_RXQ_POOL_SHORT_MASK 0xf00000
Marcin Wojtas3f518502014-07-10 16:52:13 -030060#define MVPP2_RXQ_POOL_LONG_OFFS 24
Thomas Petazzoni5eac8922017-03-07 16:53:10 +010061#define MVPP21_RXQ_POOL_LONG_MASK 0x7000000
62#define MVPP22_RXQ_POOL_LONG_MASK 0xf000000
Marcin Wojtas3f518502014-07-10 16:52:13 -030063#define MVPP2_RXQ_PACKET_OFFSET_OFFS 28
64#define MVPP2_RXQ_PACKET_OFFSET_MASK 0x70000000
65#define MVPP2_RXQ_DISABLE_MASK BIT(31)
66
67/* Parser Registers */
68#define MVPP2_PRS_INIT_LOOKUP_REG 0x1000
69#define MVPP2_PRS_PORT_LU_MAX 0xf
70#define MVPP2_PRS_PORT_LU_MASK(port) (0xff << ((port) * 4))
71#define MVPP2_PRS_PORT_LU_VAL(port, val) ((val) << ((port) * 4))
72#define MVPP2_PRS_INIT_OFFS_REG(port) (0x1004 + ((port) & 4))
73#define MVPP2_PRS_INIT_OFF_MASK(port) (0x3f << (((port) % 4) * 8))
74#define MVPP2_PRS_INIT_OFF_VAL(port, val) ((val) << (((port) % 4) * 8))
75#define MVPP2_PRS_MAX_LOOP_REG(port) (0x100c + ((port) & 4))
76#define MVPP2_PRS_MAX_LOOP_MASK(port) (0xff << (((port) % 4) * 8))
77#define MVPP2_PRS_MAX_LOOP_VAL(port, val) ((val) << (((port) % 4) * 8))
78#define MVPP2_PRS_TCAM_IDX_REG 0x1100
79#define MVPP2_PRS_TCAM_DATA_REG(idx) (0x1104 + (idx) * 4)
80#define MVPP2_PRS_TCAM_INV_MASK BIT(31)
81#define MVPP2_PRS_SRAM_IDX_REG 0x1200
82#define MVPP2_PRS_SRAM_DATA_REG(idx) (0x1204 + (idx) * 4)
83#define MVPP2_PRS_TCAM_CTRL_REG 0x1230
84#define MVPP2_PRS_TCAM_EN_MASK BIT(0)
85
Antoine Tenart1d7d15d2017-10-30 11:23:30 +010086/* RSS Registers */
87#define MVPP22_RSS_INDEX 0x1500
Antoine Tenart8a7b7412017-12-08 10:24:20 +010088#define MVPP22_RSS_INDEX_TABLE_ENTRY(idx) (idx)
Antoine Tenart1d7d15d2017-10-30 11:23:30 +010089#define MVPP22_RSS_INDEX_TABLE(idx) ((idx) << 8)
90#define MVPP22_RSS_INDEX_QUEUE(idx) ((idx) << 16)
91#define MVPP22_RSS_TABLE_ENTRY 0x1508
92#define MVPP22_RSS_TABLE 0x1510
93#define MVPP22_RSS_TABLE_POINTER(p) (p)
94#define MVPP22_RSS_WIDTH 0x150c
95
Marcin Wojtas3f518502014-07-10 16:52:13 -030096/* Classifier Registers */
97#define MVPP2_CLS_MODE_REG 0x1800
98#define MVPP2_CLS_MODE_ACTIVE_MASK BIT(0)
99#define MVPP2_CLS_PORT_WAY_REG 0x1810
100#define MVPP2_CLS_PORT_WAY_MASK(port) (1 << (port))
101#define MVPP2_CLS_LKP_INDEX_REG 0x1814
102#define MVPP2_CLS_LKP_INDEX_WAY_OFFS 6
103#define MVPP2_CLS_LKP_TBL_REG 0x1818
104#define MVPP2_CLS_LKP_TBL_RXQ_MASK 0xff
105#define MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK BIT(25)
106#define MVPP2_CLS_FLOW_INDEX_REG 0x1820
107#define MVPP2_CLS_FLOW_TBL0_REG 0x1824
108#define MVPP2_CLS_FLOW_TBL1_REG 0x1828
109#define MVPP2_CLS_FLOW_TBL2_REG 0x182c
110#define MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port) (0x1980 + ((port) * 4))
111#define MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS 3
112#define MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK 0x7
113#define MVPP2_CLS_SWFWD_P2HQ_REG(port) (0x19b0 + ((port) * 4))
114#define MVPP2_CLS_SWFWD_PCTRL_REG 0x19d0
115#define MVPP2_CLS_SWFWD_PCTRL_MASK(port) (1 << (port))
116
117/* Descriptor Manager Top Registers */
118#define MVPP2_RXQ_NUM_REG 0x2040
119#define MVPP2_RXQ_DESC_ADDR_REG 0x2044
Thomas Petazzonib02f31f2017-03-07 16:53:12 +0100120#define MVPP22_DESC_ADDR_OFFS 8
Marcin Wojtas3f518502014-07-10 16:52:13 -0300121#define MVPP2_RXQ_DESC_SIZE_REG 0x2048
122#define MVPP2_RXQ_DESC_SIZE_MASK 0x3ff0
123#define MVPP2_RXQ_STATUS_UPDATE_REG(rxq) (0x3000 + 4 * (rxq))
124#define MVPP2_RXQ_NUM_PROCESSED_OFFSET 0
125#define MVPP2_RXQ_NUM_NEW_OFFSET 16
126#define MVPP2_RXQ_STATUS_REG(rxq) (0x3400 + 4 * (rxq))
127#define MVPP2_RXQ_OCCUPIED_MASK 0x3fff
128#define MVPP2_RXQ_NON_OCCUPIED_OFFSET 16
129#define MVPP2_RXQ_NON_OCCUPIED_MASK 0x3fff0000
130#define MVPP2_RXQ_THRESH_REG 0x204c
131#define MVPP2_OCCUPIED_THRESH_OFFSET 0
132#define MVPP2_OCCUPIED_THRESH_MASK 0x3fff
133#define MVPP2_RXQ_INDEX_REG 0x2050
134#define MVPP2_TXQ_NUM_REG 0x2080
135#define MVPP2_TXQ_DESC_ADDR_REG 0x2084
136#define MVPP2_TXQ_DESC_SIZE_REG 0x2088
137#define MVPP2_TXQ_DESC_SIZE_MASK 0x3ff0
Thomas Petazzoni213f4282017-08-03 10:42:00 +0200138#define MVPP2_TXQ_THRESH_REG 0x2094
139#define MVPP2_TXQ_THRESH_OFFSET 16
140#define MVPP2_TXQ_THRESH_MASK 0x3fff
Marcin Wojtas3f518502014-07-10 16:52:13 -0300141#define MVPP2_AGGR_TXQ_UPDATE_REG 0x2090
Marcin Wojtas3f518502014-07-10 16:52:13 -0300142#define MVPP2_TXQ_INDEX_REG 0x2098
143#define MVPP2_TXQ_PREF_BUF_REG 0x209c
144#define MVPP2_PREF_BUF_PTR(desc) ((desc) & 0xfff)
145#define MVPP2_PREF_BUF_SIZE_4 (BIT(12) | BIT(13))
146#define MVPP2_PREF_BUF_SIZE_16 (BIT(12) | BIT(14))
147#define MVPP2_PREF_BUF_THRESH(val) ((val) << 17)
148#define MVPP2_TXQ_DRAIN_EN_MASK BIT(31)
149#define MVPP2_TXQ_PENDING_REG 0x20a0
150#define MVPP2_TXQ_PENDING_MASK 0x3fff
151#define MVPP2_TXQ_INT_STATUS_REG 0x20a4
152#define MVPP2_TXQ_SENT_REG(txq) (0x3c00 + 4 * (txq))
153#define MVPP2_TRANSMITTED_COUNT_OFFSET 16
154#define MVPP2_TRANSMITTED_COUNT_MASK 0x3fff0000
155#define MVPP2_TXQ_RSVD_REQ_REG 0x20b0
156#define MVPP2_TXQ_RSVD_REQ_Q_OFFSET 16
157#define MVPP2_TXQ_RSVD_RSLT_REG 0x20b4
158#define MVPP2_TXQ_RSVD_RSLT_MASK 0x3fff
159#define MVPP2_TXQ_RSVD_CLR_REG 0x20b8
160#define MVPP2_TXQ_RSVD_CLR_OFFSET 16
161#define MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu) (0x2100 + 4 * (cpu))
Thomas Petazzonib02f31f2017-03-07 16:53:12 +0100162#define MVPP22_AGGR_TXQ_DESC_ADDR_OFFS 8
Marcin Wojtas3f518502014-07-10 16:52:13 -0300163#define MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu) (0x2140 + 4 * (cpu))
164#define MVPP2_AGGR_TXQ_DESC_SIZE_MASK 0x3ff0
165#define MVPP2_AGGR_TXQ_STATUS_REG(cpu) (0x2180 + 4 * (cpu))
166#define MVPP2_AGGR_TXQ_PENDING_MASK 0x3fff
167#define MVPP2_AGGR_TXQ_INDEX_REG(cpu) (0x21c0 + 4 * (cpu))
168
169/* MBUS bridge registers */
170#define MVPP2_WIN_BASE(w) (0x4000 + ((w) << 2))
171#define MVPP2_WIN_SIZE(w) (0x4020 + ((w) << 2))
172#define MVPP2_WIN_REMAP(w) (0x4040 + ((w) << 2))
173#define MVPP2_BASE_ADDR_ENABLE 0x4060
174
Thomas Petazzoni6763ce32017-03-07 16:53:15 +0100175/* AXI Bridge Registers */
176#define MVPP22_AXI_BM_WR_ATTR_REG 0x4100
177#define MVPP22_AXI_BM_RD_ATTR_REG 0x4104
178#define MVPP22_AXI_AGGRQ_DESCR_RD_ATTR_REG 0x4110
179#define MVPP22_AXI_TXQ_DESCR_WR_ATTR_REG 0x4114
180#define MVPP22_AXI_TXQ_DESCR_RD_ATTR_REG 0x4118
181#define MVPP22_AXI_RXQ_DESCR_WR_ATTR_REG 0x411c
182#define MVPP22_AXI_RX_DATA_WR_ATTR_REG 0x4120
183#define MVPP22_AXI_TX_DATA_RD_ATTR_REG 0x4130
184#define MVPP22_AXI_RD_NORMAL_CODE_REG 0x4150
185#define MVPP22_AXI_RD_SNOOP_CODE_REG 0x4154
186#define MVPP22_AXI_WR_NORMAL_CODE_REG 0x4160
187#define MVPP22_AXI_WR_SNOOP_CODE_REG 0x4164
188
189/* Values for AXI Bridge registers */
190#define MVPP22_AXI_ATTR_CACHE_OFFS 0
191#define MVPP22_AXI_ATTR_DOMAIN_OFFS 12
192
193#define MVPP22_AXI_CODE_CACHE_OFFS 0
194#define MVPP22_AXI_CODE_DOMAIN_OFFS 4
195
196#define MVPP22_AXI_CODE_CACHE_NON_CACHE 0x3
197#define MVPP22_AXI_CODE_CACHE_WR_CACHE 0x7
198#define MVPP22_AXI_CODE_CACHE_RD_CACHE 0xb
199
200#define MVPP22_AXI_CODE_DOMAIN_OUTER_DOM 2
201#define MVPP22_AXI_CODE_DOMAIN_SYSTEM 3
202
Marcin Wojtas3f518502014-07-10 16:52:13 -0300203/* Interrupt Cause and Mask registers */
Thomas Petazzoni213f4282017-08-03 10:42:00 +0200204#define MVPP2_ISR_TX_THRESHOLD_REG(port) (0x5140 + 4 * (port))
205#define MVPP2_MAX_ISR_TX_THRESHOLD 0xfffff0
206
Marcin Wojtas3f518502014-07-10 16:52:13 -0300207#define MVPP2_ISR_RX_THRESHOLD_REG(rxq) (0x5200 + 4 * (rxq))
Thomas Petazzoniab426762017-02-21 11:28:04 +0100208#define MVPP2_MAX_ISR_RX_THRESHOLD 0xfffff0
Thomas Petazzonieb1e93a2017-08-03 10:41:55 +0200209#define MVPP21_ISR_RXQ_GROUP_REG(port) (0x5400 + 4 * (port))
Thomas Petazzonia73fef12017-03-07 16:53:16 +0100210
Antoine Ténart81b66302017-08-22 19:08:21 +0200211#define MVPP22_ISR_RXQ_GROUP_INDEX_REG 0x5400
Thomas Petazzonia73fef12017-03-07 16:53:16 +0100212#define MVPP22_ISR_RXQ_GROUP_INDEX_SUBGROUP_MASK 0xf
Antoine Ténart81b66302017-08-22 19:08:21 +0200213#define MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_MASK 0x380
214#define MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_OFFSET 7
Thomas Petazzonia73fef12017-03-07 16:53:16 +0100215
216#define MVPP22_ISR_RXQ_GROUP_INDEX_SUBGROUP_MASK 0xf
Antoine Ténart81b66302017-08-22 19:08:21 +0200217#define MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_MASK 0x380
Thomas Petazzonia73fef12017-03-07 16:53:16 +0100218
Antoine Ténart81b66302017-08-22 19:08:21 +0200219#define MVPP22_ISR_RXQ_SUB_GROUP_CONFIG_REG 0x5404
220#define MVPP22_ISR_RXQ_SUB_GROUP_STARTQ_MASK 0x1f
221#define MVPP22_ISR_RXQ_SUB_GROUP_SIZE_MASK 0xf00
222#define MVPP22_ISR_RXQ_SUB_GROUP_SIZE_OFFSET 8
Thomas Petazzonia73fef12017-03-07 16:53:16 +0100223
Marcin Wojtas3f518502014-07-10 16:52:13 -0300224#define MVPP2_ISR_ENABLE_REG(port) (0x5420 + 4 * (port))
225#define MVPP2_ISR_ENABLE_INTERRUPT(mask) ((mask) & 0xffff)
226#define MVPP2_ISR_DISABLE_INTERRUPT(mask) (((mask) << 16) & 0xffff0000)
227#define MVPP2_ISR_RX_TX_CAUSE_REG(port) (0x5480 + 4 * (port))
228#define MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK 0xffff
229#define MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK 0xff0000
Thomas Petazzoni213f4282017-08-03 10:42:00 +0200230#define MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_OFFSET 16
Marcin Wojtas3f518502014-07-10 16:52:13 -0300231#define MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK BIT(24)
232#define MVPP2_CAUSE_FCS_ERR_MASK BIT(25)
233#define MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK BIT(26)
234#define MVPP2_CAUSE_TX_EXCEPTION_SUM_MASK BIT(29)
235#define MVPP2_CAUSE_RX_EXCEPTION_SUM_MASK BIT(30)
236#define MVPP2_CAUSE_MISC_SUM_MASK BIT(31)
237#define MVPP2_ISR_RX_TX_MASK_REG(port) (0x54a0 + 4 * (port))
238#define MVPP2_ISR_PON_RX_TX_MASK_REG 0x54bc
239#define MVPP2_PON_CAUSE_RXQ_OCCUP_DESC_ALL_MASK 0xffff
240#define MVPP2_PON_CAUSE_TXP_OCCUP_DESC_ALL_MASK 0x3fc00000
241#define MVPP2_PON_CAUSE_MISC_SUM_MASK BIT(31)
242#define MVPP2_ISR_MISC_CAUSE_REG 0x55b0
243
244/* Buffer Manager registers */
245#define MVPP2_BM_POOL_BASE_REG(pool) (0x6000 + ((pool) * 4))
246#define MVPP2_BM_POOL_BASE_ADDR_MASK 0xfffff80
247#define MVPP2_BM_POOL_SIZE_REG(pool) (0x6040 + ((pool) * 4))
248#define MVPP2_BM_POOL_SIZE_MASK 0xfff0
249#define MVPP2_BM_POOL_READ_PTR_REG(pool) (0x6080 + ((pool) * 4))
250#define MVPP2_BM_POOL_GET_READ_PTR_MASK 0xfff0
251#define MVPP2_BM_POOL_PTRS_NUM_REG(pool) (0x60c0 + ((pool) * 4))
252#define MVPP2_BM_POOL_PTRS_NUM_MASK 0xfff0
253#define MVPP2_BM_BPPI_READ_PTR_REG(pool) (0x6100 + ((pool) * 4))
254#define MVPP2_BM_BPPI_PTRS_NUM_REG(pool) (0x6140 + ((pool) * 4))
255#define MVPP2_BM_BPPI_PTR_NUM_MASK 0x7ff
256#define MVPP2_BM_BPPI_PREFETCH_FULL_MASK BIT(16)
257#define MVPP2_BM_POOL_CTRL_REG(pool) (0x6200 + ((pool) * 4))
258#define MVPP2_BM_START_MASK BIT(0)
259#define MVPP2_BM_STOP_MASK BIT(1)
260#define MVPP2_BM_STATE_MASK BIT(4)
261#define MVPP2_BM_LOW_THRESH_OFFS 8
262#define MVPP2_BM_LOW_THRESH_MASK 0x7f00
263#define MVPP2_BM_LOW_THRESH_VALUE(val) ((val) << \
264 MVPP2_BM_LOW_THRESH_OFFS)
265#define MVPP2_BM_HIGH_THRESH_OFFS 16
266#define MVPP2_BM_HIGH_THRESH_MASK 0x7f0000
267#define MVPP2_BM_HIGH_THRESH_VALUE(val) ((val) << \
268 MVPP2_BM_HIGH_THRESH_OFFS)
269#define MVPP2_BM_INTR_CAUSE_REG(pool) (0x6240 + ((pool) * 4))
270#define MVPP2_BM_RELEASED_DELAY_MASK BIT(0)
271#define MVPP2_BM_ALLOC_FAILED_MASK BIT(1)
272#define MVPP2_BM_BPPE_EMPTY_MASK BIT(2)
273#define MVPP2_BM_BPPE_FULL_MASK BIT(3)
274#define MVPP2_BM_AVAILABLE_BP_LOW_MASK BIT(4)
275#define MVPP2_BM_INTR_MASK_REG(pool) (0x6280 + ((pool) * 4))
276#define MVPP2_BM_PHY_ALLOC_REG(pool) (0x6400 + ((pool) * 4))
277#define MVPP2_BM_PHY_ALLOC_GRNTD_MASK BIT(0)
278#define MVPP2_BM_VIRT_ALLOC_REG 0x6440
Thomas Petazzonid01524d2017-03-07 16:53:09 +0100279#define MVPP22_BM_ADDR_HIGH_ALLOC 0x6444
280#define MVPP22_BM_ADDR_HIGH_PHYS_MASK 0xff
281#define MVPP22_BM_ADDR_HIGH_VIRT_MASK 0xff00
282#define MVPP22_BM_ADDR_HIGH_VIRT_SHIFT 8
Marcin Wojtas3f518502014-07-10 16:52:13 -0300283#define MVPP2_BM_PHY_RLS_REG(pool) (0x6480 + ((pool) * 4))
284#define MVPP2_BM_PHY_RLS_MC_BUFF_MASK BIT(0)
285#define MVPP2_BM_PHY_RLS_PRIO_EN_MASK BIT(1)
286#define MVPP2_BM_PHY_RLS_GRNTD_MASK BIT(2)
287#define MVPP2_BM_VIRT_RLS_REG 0x64c0
Thomas Petazzonid01524d2017-03-07 16:53:09 +0100288#define MVPP22_BM_ADDR_HIGH_RLS_REG 0x64c4
289#define MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK 0xff
Antoine Ténart81b66302017-08-22 19:08:21 +0200290#define MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK 0xff00
Thomas Petazzonid01524d2017-03-07 16:53:09 +0100291#define MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT 8
Marcin Wojtas3f518502014-07-10 16:52:13 -0300292
293/* TX Scheduler registers */
294#define MVPP2_TXP_SCHED_PORT_INDEX_REG 0x8000
295#define MVPP2_TXP_SCHED_Q_CMD_REG 0x8004
296#define MVPP2_TXP_SCHED_ENQ_MASK 0xff
297#define MVPP2_TXP_SCHED_DISQ_OFFSET 8
298#define MVPP2_TXP_SCHED_CMD_1_REG 0x8010
299#define MVPP2_TXP_SCHED_PERIOD_REG 0x8018
300#define MVPP2_TXP_SCHED_MTU_REG 0x801c
301#define MVPP2_TXP_MTU_MAX 0x7FFFF
302#define MVPP2_TXP_SCHED_REFILL_REG 0x8020
303#define MVPP2_TXP_REFILL_TOKENS_ALL_MASK 0x7ffff
304#define MVPP2_TXP_REFILL_PERIOD_ALL_MASK 0x3ff00000
305#define MVPP2_TXP_REFILL_PERIOD_MASK(v) ((v) << 20)
306#define MVPP2_TXP_SCHED_TOKEN_SIZE_REG 0x8024
307#define MVPP2_TXP_TOKEN_SIZE_MAX 0xffffffff
308#define MVPP2_TXQ_SCHED_REFILL_REG(q) (0x8040 + ((q) << 2))
309#define MVPP2_TXQ_REFILL_TOKENS_ALL_MASK 0x7ffff
310#define MVPP2_TXQ_REFILL_PERIOD_ALL_MASK 0x3ff00000
311#define MVPP2_TXQ_REFILL_PERIOD_MASK(v) ((v) << 20)
312#define MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(q) (0x8060 + ((q) << 2))
313#define MVPP2_TXQ_TOKEN_SIZE_MAX 0x7fffffff
314#define MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(q) (0x8080 + ((q) << 2))
315#define MVPP2_TXQ_TOKEN_CNTR_MAX 0xffffffff
316
317/* TX general registers */
318#define MVPP2_TX_SNOOP_REG 0x8800
319#define MVPP2_TX_PORT_FLUSH_REG 0x8810
320#define MVPP2_TX_PORT_FLUSH_MASK(port) (1 << (port))
321
322/* LMS registers */
323#define MVPP2_SRC_ADDR_MIDDLE 0x24
324#define MVPP2_SRC_ADDR_HIGH 0x28
Marcin Wojtas08a23752014-07-21 13:48:12 -0300325#define MVPP2_PHY_AN_CFG0_REG 0x34
326#define MVPP2_PHY_AN_STOP_SMI0_MASK BIT(7)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300327#define MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG 0x305c
Thomas Petazzoni31d76772017-02-21 11:28:10 +0100328#define MVPP2_EXT_GLOBAL_CTRL_DEFAULT 0x27
Marcin Wojtas3f518502014-07-10 16:52:13 -0300329
330/* Per-port registers */
331#define MVPP2_GMAC_CTRL_0_REG 0x0
Antoine Ténart81b66302017-08-22 19:08:21 +0200332#define MVPP2_GMAC_PORT_EN_MASK BIT(0)
Antoine Ténart39193572017-08-22 19:08:24 +0200333#define MVPP2_GMAC_PORT_TYPE_MASK BIT(1)
Antoine Ténart81b66302017-08-22 19:08:21 +0200334#define MVPP2_GMAC_MAX_RX_SIZE_OFFS 2
335#define MVPP2_GMAC_MAX_RX_SIZE_MASK 0x7ffc
336#define MVPP2_GMAC_MIB_CNTR_EN_MASK BIT(15)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300337#define MVPP2_GMAC_CTRL_1_REG 0x4
Antoine Ténart81b66302017-08-22 19:08:21 +0200338#define MVPP2_GMAC_PERIODIC_XON_EN_MASK BIT(1)
339#define MVPP2_GMAC_GMII_LB_EN_MASK BIT(5)
340#define MVPP2_GMAC_PCS_LB_EN_BIT 6
341#define MVPP2_GMAC_PCS_LB_EN_MASK BIT(6)
342#define MVPP2_GMAC_SA_LOW_OFFS 7
Marcin Wojtas3f518502014-07-10 16:52:13 -0300343#define MVPP2_GMAC_CTRL_2_REG 0x8
Antoine Ténart81b66302017-08-22 19:08:21 +0200344#define MVPP2_GMAC_INBAND_AN_MASK BIT(0)
Antoine Ténart39193572017-08-22 19:08:24 +0200345#define MVPP2_GMAC_FLOW_CTRL_MASK GENMASK(2, 1)
Antoine Ténart81b66302017-08-22 19:08:21 +0200346#define MVPP2_GMAC_PCS_ENABLE_MASK BIT(3)
Antoine Tenartc7dfc8c2017-09-25 14:59:48 +0200347#define MVPP2_GMAC_INTERNAL_CLK_MASK BIT(4)
Antoine Ténart39193572017-08-22 19:08:24 +0200348#define MVPP2_GMAC_DISABLE_PADDING BIT(5)
Antoine Ténart81b66302017-08-22 19:08:21 +0200349#define MVPP2_GMAC_PORT_RESET_MASK BIT(6)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300350#define MVPP2_GMAC_AUTONEG_CONFIG 0xc
Antoine Ténart81b66302017-08-22 19:08:21 +0200351#define MVPP2_GMAC_FORCE_LINK_DOWN BIT(0)
352#define MVPP2_GMAC_FORCE_LINK_PASS BIT(1)
Antoine Ténart39193572017-08-22 19:08:24 +0200353#define MVPP2_GMAC_IN_BAND_AUTONEG BIT(2)
354#define MVPP2_GMAC_IN_BAND_AUTONEG_BYPASS BIT(3)
Antoine Ténart81b66302017-08-22 19:08:21 +0200355#define MVPP2_GMAC_CONFIG_MII_SPEED BIT(5)
356#define MVPP2_GMAC_CONFIG_GMII_SPEED BIT(6)
357#define MVPP2_GMAC_AN_SPEED_EN BIT(7)
358#define MVPP2_GMAC_FC_ADV_EN BIT(9)
Antoine Ténart39193572017-08-22 19:08:24 +0200359#define MVPP2_GMAC_FLOW_CTRL_AUTONEG BIT(11)
Antoine Ténart81b66302017-08-22 19:08:21 +0200360#define MVPP2_GMAC_CONFIG_FULL_DUPLEX BIT(12)
361#define MVPP2_GMAC_AN_DUPLEX_EN BIT(13)
Antoine Tenartfd3651b2017-09-01 11:04:54 +0200362#define MVPP2_GMAC_STATUS0 0x10
363#define MVPP2_GMAC_STATUS0_LINK_UP BIT(0)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300364#define MVPP2_GMAC_PORT_FIFO_CFG_1_REG 0x1c
Antoine Ténart81b66302017-08-22 19:08:21 +0200365#define MVPP2_GMAC_TX_FIFO_MIN_TH_OFFS 6
366#define MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK 0x1fc0
367#define MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(v) (((v) << 6) & \
Marcin Wojtas3f518502014-07-10 16:52:13 -0300368 MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK)
Antoine Tenartfd3651b2017-09-01 11:04:54 +0200369#define MVPP22_GMAC_INT_STAT 0x20
370#define MVPP22_GMAC_INT_STAT_LINK BIT(1)
371#define MVPP22_GMAC_INT_MASK 0x24
372#define MVPP22_GMAC_INT_MASK_LINK_STAT BIT(1)
Thomas Petazzoni26975822017-03-07 16:53:14 +0100373#define MVPP22_GMAC_CTRL_4_REG 0x90
Antoine Ténart81b66302017-08-22 19:08:21 +0200374#define MVPP22_CTRL4_EXT_PIN_GMII_SEL BIT(0)
375#define MVPP22_CTRL4_DP_CLK_SEL BIT(5)
Antoine Ténart1068ec72017-08-22 19:08:22 +0200376#define MVPP22_CTRL4_SYNC_BYPASS_DIS BIT(6)
Antoine Ténart81b66302017-08-22 19:08:21 +0200377#define MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE BIT(7)
Antoine Tenartfd3651b2017-09-01 11:04:54 +0200378#define MVPP22_GMAC_INT_SUM_MASK 0xa4
379#define MVPP22_GMAC_INT_SUM_MASK_LINK_STAT BIT(1)
Thomas Petazzoni26975822017-03-07 16:53:14 +0100380
381/* Per-port XGMAC registers. PPv2.2 only, only for GOP port 0,
382 * relative to port->base.
383 */
Antoine Ténart725757a2017-06-12 16:01:39 +0200384#define MVPP22_XLG_CTRL0_REG 0x100
Antoine Ténart81b66302017-08-22 19:08:21 +0200385#define MVPP22_XLG_CTRL0_PORT_EN BIT(0)
386#define MVPP22_XLG_CTRL0_MAC_RESET_DIS BIT(1)
Antoine Ténart77321952017-08-22 19:08:25 +0200387#define MVPP22_XLG_CTRL0_RX_FLOW_CTRL_EN BIT(7)
Antoine Ténart81b66302017-08-22 19:08:21 +0200388#define MVPP22_XLG_CTRL0_MIB_CNT_DIS BIT(14)
Stefan Chulski76eb1b12017-08-22 19:08:26 +0200389#define MVPP22_XLG_CTRL1_REG 0x104
Antoine Ténartec15ecd2017-08-25 15:24:46 +0200390#define MVPP22_XLG_CTRL1_FRAMESIZELIMIT_OFFS 0
Stefan Chulski76eb1b12017-08-22 19:08:26 +0200391#define MVPP22_XLG_CTRL1_FRAMESIZELIMIT_MASK 0x1fff
Antoine Tenartfd3651b2017-09-01 11:04:54 +0200392#define MVPP22_XLG_STATUS 0x10c
393#define MVPP22_XLG_STATUS_LINK_UP BIT(0)
394#define MVPP22_XLG_INT_STAT 0x114
395#define MVPP22_XLG_INT_STAT_LINK BIT(1)
396#define MVPP22_XLG_INT_MASK 0x118
397#define MVPP22_XLG_INT_MASK_LINK BIT(1)
Thomas Petazzoni26975822017-03-07 16:53:14 +0100398#define MVPP22_XLG_CTRL3_REG 0x11c
Antoine Ténart81b66302017-08-22 19:08:21 +0200399#define MVPP22_XLG_CTRL3_MACMODESELECT_MASK (7 << 13)
400#define MVPP22_XLG_CTRL3_MACMODESELECT_GMAC (0 << 13)
401#define MVPP22_XLG_CTRL3_MACMODESELECT_10G (1 << 13)
Antoine Tenartfd3651b2017-09-01 11:04:54 +0200402#define MVPP22_XLG_EXT_INT_MASK 0x15c
403#define MVPP22_XLG_EXT_INT_MASK_XLG BIT(1)
404#define MVPP22_XLG_EXT_INT_MASK_GIG BIT(2)
Antoine Ténart77321952017-08-22 19:08:25 +0200405#define MVPP22_XLG_CTRL4_REG 0x184
406#define MVPP22_XLG_CTRL4_FWD_FC BIT(5)
407#define MVPP22_XLG_CTRL4_FWD_PFC BIT(6)
408#define MVPP22_XLG_CTRL4_MACMODSELECT_GMAC BIT(12)
409
Thomas Petazzoni26975822017-03-07 16:53:14 +0100410/* SMI registers. PPv2.2 only, relative to priv->iface_base. */
411#define MVPP22_SMI_MISC_CFG_REG 0x1204
Antoine Ténart81b66302017-08-22 19:08:21 +0200412#define MVPP22_SMI_POLLING_EN BIT(10)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300413
Thomas Petazzonia7868412017-03-07 16:53:13 +0100414#define MVPP22_GMAC_BASE(port) (0x7000 + (port) * 0x1000 + 0xe00)
415
Marcin Wojtas3f518502014-07-10 16:52:13 -0300416#define MVPP2_CAUSE_TXQ_SENT_DESC_ALL_MASK 0xff
417
418/* Descriptor ring Macros */
419#define MVPP2_QUEUE_NEXT_DESC(q, index) \
420 (((index) < (q)->last_desc) ? ((index) + 1) : 0)
421
Antoine Ténartf84bf382017-08-22 19:08:27 +0200422/* XPCS registers. PPv2.2 only */
423#define MVPP22_MPCS_BASE(port) (0x7000 + (port) * 0x1000)
424#define MVPP22_MPCS_CTRL 0x14
425#define MVPP22_MPCS_CTRL_FWD_ERR_CONN BIT(10)
426#define MVPP22_MPCS_CLK_RESET 0x14c
427#define MAC_CLK_RESET_SD_TX BIT(0)
428#define MAC_CLK_RESET_SD_RX BIT(1)
429#define MAC_CLK_RESET_MAC BIT(2)
430#define MVPP22_MPCS_CLK_RESET_DIV_RATIO(n) ((n) << 4)
431#define MVPP22_MPCS_CLK_RESET_DIV_SET BIT(11)
432
433/* XPCS registers. PPv2.2 only */
434#define MVPP22_XPCS_BASE(port) (0x7400 + (port) * 0x1000)
435#define MVPP22_XPCS_CFG0 0x0
436#define MVPP22_XPCS_CFG0_PCS_MODE(n) ((n) << 3)
437#define MVPP22_XPCS_CFG0_ACTIVE_LANE(n) ((n) << 5)
438
439/* System controller registers. Accessed through a regmap. */
440#define GENCONF_SOFT_RESET1 0x1108
441#define GENCONF_SOFT_RESET1_GOP BIT(6)
442#define GENCONF_PORT_CTRL0 0x1110
443#define GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT BIT(1)
444#define GENCONF_PORT_CTRL0_RX_DATA_SAMPLE BIT(29)
445#define GENCONF_PORT_CTRL0_CLK_DIV_PHASE_CLR BIT(31)
446#define GENCONF_PORT_CTRL1 0x1114
447#define GENCONF_PORT_CTRL1_EN(p) BIT(p)
448#define GENCONF_PORT_CTRL1_RESET(p) (BIT(p) << 28)
449#define GENCONF_CTRL0 0x1120
450#define GENCONF_CTRL0_PORT0_RGMII BIT(0)
451#define GENCONF_CTRL0_PORT1_RGMII_MII BIT(1)
452#define GENCONF_CTRL0_PORT1_RGMII BIT(2)
453
Marcin Wojtas3f518502014-07-10 16:52:13 -0300454/* Various constants */
455
456/* Coalescing */
457#define MVPP2_TXDONE_COAL_PKTS_THRESH 15
Marcin Wojtasedc660f2015-08-06 19:00:30 +0200458#define MVPP2_TXDONE_HRTIMER_PERIOD_NS 1000000UL
Thomas Petazzoni213f4282017-08-03 10:42:00 +0200459#define MVPP2_TXDONE_COAL_USEC 1000
Marcin Wojtas3f518502014-07-10 16:52:13 -0300460#define MVPP2_RX_COAL_PKTS 32
461#define MVPP2_RX_COAL_USEC 100
462
463/* The two bytes Marvell header. Either contains a special value used
464 * by Marvell switches when a specific hardware mode is enabled (not
465 * supported by this driver) or is filled automatically by zeroes on
466 * the RX side. Those two bytes being at the front of the Ethernet
467 * header, they allow to have the IP header aligned on a 4 bytes
468 * boundary automatically: the hardware skips those two bytes on its
469 * own.
470 */
471#define MVPP2_MH_SIZE 2
472#define MVPP2_ETH_TYPE_LEN 2
473#define MVPP2_PPPOE_HDR_SIZE 8
474#define MVPP2_VLAN_TAG_LEN 4
475
476/* Lbtd 802.3 type */
477#define MVPP2_IP_LBDT_TYPE 0xfffa
478
Marcin Wojtas3f518502014-07-10 16:52:13 -0300479#define MVPP2_TX_CSUM_MAX_SIZE 9800
480
481/* Timeout constants */
482#define MVPP2_TX_DISABLE_TIMEOUT_MSEC 1000
483#define MVPP2_TX_PENDING_TIMEOUT_MSEC 1000
484
485#define MVPP2_TX_MTU_MAX 0x7ffff
486
487/* Maximum number of T-CONTs of PON port */
488#define MVPP2_MAX_TCONT 16
489
490/* Maximum number of supported ports */
491#define MVPP2_MAX_PORTS 4
492
493/* Maximum number of TXQs used by single port */
494#define MVPP2_MAX_TXQ 8
495
Antoine Tenart1d17db02017-10-30 11:23:31 +0100496/* MVPP2_MAX_TSO_SEGS is the maximum number of fragments to allow in the GSO
497 * skb. As we need a maxium of two descriptors per fragments (1 header, 1 data),
498 * multiply this value by two to count the maximum number of skb descs needed.
499 */
500#define MVPP2_MAX_TSO_SEGS 300
501#define MVPP2_MAX_SKB_DESCS (MVPP2_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
502
Marcin Wojtas3f518502014-07-10 16:52:13 -0300503/* Dfault number of RXQs in use */
504#define MVPP2_DEFAULT_RXQ 4
505
Marcin Wojtas3f518502014-07-10 16:52:13 -0300506/* Max number of Rx descriptors */
Yan Markman7cf87e42017-12-11 09:13:26 +0100507#define MVPP2_MAX_RXD_MAX 1024
508#define MVPP2_MAX_RXD_DFLT 128
Marcin Wojtas3f518502014-07-10 16:52:13 -0300509
510/* Max number of Tx descriptors */
Yan Markman7cf87e42017-12-11 09:13:26 +0100511#define MVPP2_MAX_TXD_MAX 2048
512#define MVPP2_MAX_TXD_DFLT 1024
Marcin Wojtas3f518502014-07-10 16:52:13 -0300513
514/* Amount of Tx descriptors that can be reserved at once by CPU */
515#define MVPP2_CPU_DESC_CHUNK 64
516
517/* Max number of Tx descriptors in each aggregated queue */
518#define MVPP2_AGGR_TXQ_SIZE 256
519
520/* Descriptor aligned size */
521#define MVPP2_DESC_ALIGNED_SIZE 32
522
523/* Descriptor alignment mask */
524#define MVPP2_TX_DESC_ALIGN (MVPP2_DESC_ALIGNED_SIZE - 1)
525
526/* RX FIFO constants */
Antoine Tenart2d1d7df2017-10-30 11:23:28 +0100527#define MVPP2_RX_FIFO_PORT_DATA_SIZE_32KB 0x8000
528#define MVPP2_RX_FIFO_PORT_DATA_SIZE_8KB 0x2000
529#define MVPP2_RX_FIFO_PORT_DATA_SIZE_4KB 0x1000
530#define MVPP2_RX_FIFO_PORT_ATTR_SIZE_32KB 0x200
531#define MVPP2_RX_FIFO_PORT_ATTR_SIZE_8KB 0x80
532#define MVPP2_RX_FIFO_PORT_ATTR_SIZE_4KB 0x40
533#define MVPP2_RX_FIFO_PORT_MIN_PKT 0x80
Marcin Wojtas3f518502014-07-10 16:52:13 -0300534
Antoine Tenart7c10f972017-10-30 11:23:29 +0100535/* TX FIFO constants */
536#define MVPP22_TX_FIFO_DATA_SIZE_10KB 0xa
537#define MVPP22_TX_FIFO_DATA_SIZE_3KB 0x3
538
Marcin Wojtas3f518502014-07-10 16:52:13 -0300539/* RX buffer constants */
540#define MVPP2_SKB_SHINFO_SIZE \
541 SKB_DATA_ALIGN(sizeof(struct skb_shared_info))
542
543#define MVPP2_RX_PKT_SIZE(mtu) \
544 ALIGN((mtu) + MVPP2_MH_SIZE + MVPP2_VLAN_TAG_LEN + \
Jisheng Zhang4a0a12d2016-04-01 17:11:05 +0800545 ETH_HLEN + ETH_FCS_LEN, cache_line_size())
Marcin Wojtas3f518502014-07-10 16:52:13 -0300546
547#define MVPP2_RX_BUF_SIZE(pkt_size) ((pkt_size) + NET_SKB_PAD)
548#define MVPP2_RX_TOTAL_SIZE(buf_size) ((buf_size) + MVPP2_SKB_SHINFO_SIZE)
549#define MVPP2_RX_MAX_PKT_SIZE(total_size) \
550 ((total_size) - NET_SKB_PAD - MVPP2_SKB_SHINFO_SIZE)
551
552#define MVPP2_BIT_TO_BYTE(bit) ((bit) / 8)
553
554/* IPv6 max L3 address size */
555#define MVPP2_MAX_L3_ADDR_SIZE 16
556
557/* Port flags */
558#define MVPP2_F_LOOPBACK BIT(0)
559
560/* Marvell tag types */
561enum mvpp2_tag_type {
562 MVPP2_TAG_TYPE_NONE = 0,
563 MVPP2_TAG_TYPE_MH = 1,
564 MVPP2_TAG_TYPE_DSA = 2,
565 MVPP2_TAG_TYPE_EDSA = 3,
566 MVPP2_TAG_TYPE_VLAN = 4,
567 MVPP2_TAG_TYPE_LAST = 5
568};
569
570/* Parser constants */
571#define MVPP2_PRS_TCAM_SRAM_SIZE 256
572#define MVPP2_PRS_TCAM_WORDS 6
573#define MVPP2_PRS_SRAM_WORDS 4
574#define MVPP2_PRS_FLOW_ID_SIZE 64
575#define MVPP2_PRS_FLOW_ID_MASK 0x3f
576#define MVPP2_PRS_TCAM_ENTRY_INVALID 1
577#define MVPP2_PRS_TCAM_DSA_TAGGED_BIT BIT(5)
578#define MVPP2_PRS_IPV4_HEAD 0x40
579#define MVPP2_PRS_IPV4_HEAD_MASK 0xf0
580#define MVPP2_PRS_IPV4_MC 0xe0
581#define MVPP2_PRS_IPV4_MC_MASK 0xf0
582#define MVPP2_PRS_IPV4_BC_MASK 0xff
583#define MVPP2_PRS_IPV4_IHL 0x5
584#define MVPP2_PRS_IPV4_IHL_MASK 0xf
585#define MVPP2_PRS_IPV6_MC 0xff
586#define MVPP2_PRS_IPV6_MC_MASK 0xff
587#define MVPP2_PRS_IPV6_HOP_MASK 0xff
588#define MVPP2_PRS_TCAM_PROTO_MASK 0xff
589#define MVPP2_PRS_TCAM_PROTO_MASK_L 0x3f
590#define MVPP2_PRS_DBL_VLANS_MAX 100
591
592/* Tcam structure:
593 * - lookup ID - 4 bits
594 * - port ID - 1 byte
595 * - additional information - 1 byte
596 * - header data - 8 bytes
597 * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(5)->(0).
598 */
599#define MVPP2_PRS_AI_BITS 8
600#define MVPP2_PRS_PORT_MASK 0xff
601#define MVPP2_PRS_LU_MASK 0xf
602#define MVPP2_PRS_TCAM_DATA_BYTE(offs) \
603 (((offs) - ((offs) % 2)) * 2 + ((offs) % 2))
604#define MVPP2_PRS_TCAM_DATA_BYTE_EN(offs) \
605 (((offs) * 2) - ((offs) % 2) + 2)
606#define MVPP2_PRS_TCAM_AI_BYTE 16
607#define MVPP2_PRS_TCAM_PORT_BYTE 17
608#define MVPP2_PRS_TCAM_LU_BYTE 20
609#define MVPP2_PRS_TCAM_EN_OFFS(offs) ((offs) + 2)
610#define MVPP2_PRS_TCAM_INV_WORD 5
611/* Tcam entries ID */
612#define MVPP2_PE_DROP_ALL 0
613#define MVPP2_PE_FIRST_FREE_TID 1
614#define MVPP2_PE_LAST_FREE_TID (MVPP2_PRS_TCAM_SRAM_SIZE - 31)
615#define MVPP2_PE_IP6_EXT_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 30)
616#define MVPP2_PE_MAC_MC_IP6 (MVPP2_PRS_TCAM_SRAM_SIZE - 29)
617#define MVPP2_PE_IP6_ADDR_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 28)
618#define MVPP2_PE_IP4_ADDR_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 27)
619#define MVPP2_PE_LAST_DEFAULT_FLOW (MVPP2_PRS_TCAM_SRAM_SIZE - 26)
620#define MVPP2_PE_FIRST_DEFAULT_FLOW (MVPP2_PRS_TCAM_SRAM_SIZE - 19)
621#define MVPP2_PE_EDSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 18)
622#define MVPP2_PE_EDSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 17)
623#define MVPP2_PE_DSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 16)
624#define MVPP2_PE_DSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 15)
625#define MVPP2_PE_ETYPE_EDSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 14)
626#define MVPP2_PE_ETYPE_EDSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 13)
627#define MVPP2_PE_ETYPE_DSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 12)
628#define MVPP2_PE_ETYPE_DSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 11)
629#define MVPP2_PE_MH_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 10)
630#define MVPP2_PE_DSA_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 9)
631#define MVPP2_PE_IP6_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 8)
632#define MVPP2_PE_IP4_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 7)
633#define MVPP2_PE_ETH_TYPE_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 6)
634#define MVPP2_PE_VLAN_DBL (MVPP2_PRS_TCAM_SRAM_SIZE - 5)
635#define MVPP2_PE_VLAN_NONE (MVPP2_PRS_TCAM_SRAM_SIZE - 4)
636#define MVPP2_PE_MAC_MC_ALL (MVPP2_PRS_TCAM_SRAM_SIZE - 3)
637#define MVPP2_PE_MAC_PROMISCUOUS (MVPP2_PRS_TCAM_SRAM_SIZE - 2)
638#define MVPP2_PE_MAC_NON_PROMISCUOUS (MVPP2_PRS_TCAM_SRAM_SIZE - 1)
639
640/* Sram structure
641 * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(3)->(0).
642 */
643#define MVPP2_PRS_SRAM_RI_OFFS 0
644#define MVPP2_PRS_SRAM_RI_WORD 0
645#define MVPP2_PRS_SRAM_RI_CTRL_OFFS 32
646#define MVPP2_PRS_SRAM_RI_CTRL_WORD 1
647#define MVPP2_PRS_SRAM_RI_CTRL_BITS 32
648#define MVPP2_PRS_SRAM_SHIFT_OFFS 64
649#define MVPP2_PRS_SRAM_SHIFT_SIGN_BIT 72
650#define MVPP2_PRS_SRAM_UDF_OFFS 73
651#define MVPP2_PRS_SRAM_UDF_BITS 8
652#define MVPP2_PRS_SRAM_UDF_MASK 0xff
653#define MVPP2_PRS_SRAM_UDF_SIGN_BIT 81
654#define MVPP2_PRS_SRAM_UDF_TYPE_OFFS 82
655#define MVPP2_PRS_SRAM_UDF_TYPE_MASK 0x7
656#define MVPP2_PRS_SRAM_UDF_TYPE_L3 1
657#define MVPP2_PRS_SRAM_UDF_TYPE_L4 4
658#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS 85
659#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK 0x3
660#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD 1
661#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP4_ADD 2
662#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP6_ADD 3
663#define MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS 87
664#define MVPP2_PRS_SRAM_OP_SEL_UDF_BITS 2
665#define MVPP2_PRS_SRAM_OP_SEL_UDF_MASK 0x3
666#define MVPP2_PRS_SRAM_OP_SEL_UDF_ADD 0
667#define MVPP2_PRS_SRAM_OP_SEL_UDF_IP4_ADD 2
668#define MVPP2_PRS_SRAM_OP_SEL_UDF_IP6_ADD 3
669#define MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS 89
670#define MVPP2_PRS_SRAM_AI_OFFS 90
671#define MVPP2_PRS_SRAM_AI_CTRL_OFFS 98
672#define MVPP2_PRS_SRAM_AI_CTRL_BITS 8
673#define MVPP2_PRS_SRAM_AI_MASK 0xff
674#define MVPP2_PRS_SRAM_NEXT_LU_OFFS 106
675#define MVPP2_PRS_SRAM_NEXT_LU_MASK 0xf
676#define MVPP2_PRS_SRAM_LU_DONE_BIT 110
677#define MVPP2_PRS_SRAM_LU_GEN_BIT 111
678
679/* Sram result info bits assignment */
680#define MVPP2_PRS_RI_MAC_ME_MASK 0x1
681#define MVPP2_PRS_RI_DSA_MASK 0x2
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100682#define MVPP2_PRS_RI_VLAN_MASK (BIT(2) | BIT(3))
683#define MVPP2_PRS_RI_VLAN_NONE 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300684#define MVPP2_PRS_RI_VLAN_SINGLE BIT(2)
685#define MVPP2_PRS_RI_VLAN_DOUBLE BIT(3)
686#define MVPP2_PRS_RI_VLAN_TRIPLE (BIT(2) | BIT(3))
687#define MVPP2_PRS_RI_CPU_CODE_MASK 0x70
688#define MVPP2_PRS_RI_CPU_CODE_RX_SPEC BIT(4)
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100689#define MVPP2_PRS_RI_L2_CAST_MASK (BIT(9) | BIT(10))
690#define MVPP2_PRS_RI_L2_UCAST 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300691#define MVPP2_PRS_RI_L2_MCAST BIT(9)
692#define MVPP2_PRS_RI_L2_BCAST BIT(10)
693#define MVPP2_PRS_RI_PPPOE_MASK 0x800
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100694#define MVPP2_PRS_RI_L3_PROTO_MASK (BIT(12) | BIT(13) | BIT(14))
695#define MVPP2_PRS_RI_L3_UN 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300696#define MVPP2_PRS_RI_L3_IP4 BIT(12)
697#define MVPP2_PRS_RI_L3_IP4_OPT BIT(13)
698#define MVPP2_PRS_RI_L3_IP4_OTHER (BIT(12) | BIT(13))
699#define MVPP2_PRS_RI_L3_IP6 BIT(14)
700#define MVPP2_PRS_RI_L3_IP6_EXT (BIT(12) | BIT(14))
701#define MVPP2_PRS_RI_L3_ARP (BIT(13) | BIT(14))
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100702#define MVPP2_PRS_RI_L3_ADDR_MASK (BIT(15) | BIT(16))
703#define MVPP2_PRS_RI_L3_UCAST 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300704#define MVPP2_PRS_RI_L3_MCAST BIT(15)
705#define MVPP2_PRS_RI_L3_BCAST (BIT(15) | BIT(16))
706#define MVPP2_PRS_RI_IP_FRAG_MASK 0x20000
Stefan Chulskiaff3da32017-09-25 14:59:46 +0200707#define MVPP2_PRS_RI_IP_FRAG_TRUE BIT(17)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300708#define MVPP2_PRS_RI_UDF3_MASK 0x300000
709#define MVPP2_PRS_RI_UDF3_RX_SPECIAL BIT(21)
710#define MVPP2_PRS_RI_L4_PROTO_MASK 0x1c00000
711#define MVPP2_PRS_RI_L4_TCP BIT(22)
712#define MVPP2_PRS_RI_L4_UDP BIT(23)
713#define MVPP2_PRS_RI_L4_OTHER (BIT(22) | BIT(23))
714#define MVPP2_PRS_RI_UDF7_MASK 0x60000000
715#define MVPP2_PRS_RI_UDF7_IP6_LITE BIT(29)
716#define MVPP2_PRS_RI_DROP_MASK 0x80000000
717
718/* Sram additional info bits assignment */
719#define MVPP2_PRS_IPV4_DIP_AI_BIT BIT(0)
720#define MVPP2_PRS_IPV6_NO_EXT_AI_BIT BIT(0)
721#define MVPP2_PRS_IPV6_EXT_AI_BIT BIT(1)
722#define MVPP2_PRS_IPV6_EXT_AH_AI_BIT BIT(2)
723#define MVPP2_PRS_IPV6_EXT_AH_LEN_AI_BIT BIT(3)
724#define MVPP2_PRS_IPV6_EXT_AH_L4_AI_BIT BIT(4)
725#define MVPP2_PRS_SINGLE_VLAN_AI 0
726#define MVPP2_PRS_DBL_VLAN_AI_BIT BIT(7)
727
728/* DSA/EDSA type */
729#define MVPP2_PRS_TAGGED true
730#define MVPP2_PRS_UNTAGGED false
731#define MVPP2_PRS_EDSA true
732#define MVPP2_PRS_DSA false
733
734/* MAC entries, shadow udf */
735enum mvpp2_prs_udf {
736 MVPP2_PRS_UDF_MAC_DEF,
737 MVPP2_PRS_UDF_MAC_RANGE,
738 MVPP2_PRS_UDF_L2_DEF,
739 MVPP2_PRS_UDF_L2_DEF_COPY,
740 MVPP2_PRS_UDF_L2_USER,
741};
742
743/* Lookup ID */
744enum mvpp2_prs_lookup {
745 MVPP2_PRS_LU_MH,
746 MVPP2_PRS_LU_MAC,
747 MVPP2_PRS_LU_DSA,
748 MVPP2_PRS_LU_VLAN,
749 MVPP2_PRS_LU_L2,
750 MVPP2_PRS_LU_PPPOE,
751 MVPP2_PRS_LU_IP4,
752 MVPP2_PRS_LU_IP6,
753 MVPP2_PRS_LU_FLOWS,
754 MVPP2_PRS_LU_LAST,
755};
756
757/* L3 cast enum */
758enum mvpp2_prs_l3_cast {
759 MVPP2_PRS_L3_UNI_CAST,
760 MVPP2_PRS_L3_MULTI_CAST,
761 MVPP2_PRS_L3_BROAD_CAST
762};
763
764/* Classifier constants */
765#define MVPP2_CLS_FLOWS_TBL_SIZE 512
766#define MVPP2_CLS_FLOWS_TBL_DATA_WORDS 3
767#define MVPP2_CLS_LKP_TBL_SIZE 64
Antoine Tenart1d7d15d2017-10-30 11:23:30 +0100768#define MVPP2_CLS_RX_QUEUES 256
769
770/* RSS constants */
771#define MVPP22_RSS_TABLE_ENTRIES 32
Marcin Wojtas3f518502014-07-10 16:52:13 -0300772
773/* BM constants */
774#define MVPP2_BM_POOLS_NUM 8
775#define MVPP2_BM_LONG_BUF_NUM 1024
776#define MVPP2_BM_SHORT_BUF_NUM 2048
777#define MVPP2_BM_POOL_SIZE_MAX (16*1024 - MVPP2_BM_POOL_PTR_ALIGN/4)
778#define MVPP2_BM_POOL_PTR_ALIGN 128
779#define MVPP2_BM_SWF_LONG_POOL(port) ((port > 2) ? 2 : port)
780#define MVPP2_BM_SWF_SHORT_POOL 3
781
782/* BM cookie (32 bits) definition */
783#define MVPP2_BM_COOKIE_POOL_OFFS 8
784#define MVPP2_BM_COOKIE_CPU_OFFS 24
785
786/* BM short pool packet size
787 * These value assure that for SWF the total number
788 * of bytes allocated for each buffer will be 512
789 */
790#define MVPP2_BM_SHORT_PKT_SIZE MVPP2_RX_MAX_PKT_SIZE(512)
791
Thomas Petazzonia7868412017-03-07 16:53:13 +0100792#define MVPP21_ADDR_SPACE_SZ 0
793#define MVPP22_ADDR_SPACE_SZ SZ_64K
794
Thomas Petazzonidf089aa2017-08-03 10:41:58 +0200795#define MVPP2_MAX_THREADS 8
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +0200796#define MVPP2_MAX_QVECS MVPP2_MAX_THREADS
Thomas Petazzonia7868412017-03-07 16:53:13 +0100797
Marcin Wojtas3f518502014-07-10 16:52:13 -0300798enum mvpp2_bm_type {
799 MVPP2_BM_FREE,
800 MVPP2_BM_SWF_LONG,
801 MVPP2_BM_SWF_SHORT
802};
803
Miquel Raynal118d6292017-11-06 22:56:53 +0100804/* GMAC MIB Counters register definitions */
805#define MVPP21_MIB_COUNTERS_OFFSET 0x1000
806#define MVPP21_MIB_COUNTERS_PORT_SZ 0x400
807#define MVPP22_MIB_COUNTERS_OFFSET 0x0
808#define MVPP22_MIB_COUNTERS_PORT_SZ 0x100
809
810#define MVPP2_MIB_GOOD_OCTETS_RCVD 0x0
811#define MVPP2_MIB_BAD_OCTETS_RCVD 0x8
812#define MVPP2_MIB_CRC_ERRORS_SENT 0xc
813#define MVPP2_MIB_UNICAST_FRAMES_RCVD 0x10
814#define MVPP2_MIB_BROADCAST_FRAMES_RCVD 0x18
815#define MVPP2_MIB_MULTICAST_FRAMES_RCVD 0x1c
816#define MVPP2_MIB_FRAMES_64_OCTETS 0x20
817#define MVPP2_MIB_FRAMES_65_TO_127_OCTETS 0x24
818#define MVPP2_MIB_FRAMES_128_TO_255_OCTETS 0x28
819#define MVPP2_MIB_FRAMES_256_TO_511_OCTETS 0x2c
820#define MVPP2_MIB_FRAMES_512_TO_1023_OCTETS 0x30
821#define MVPP2_MIB_FRAMES_1024_TO_MAX_OCTETS 0x34
822#define MVPP2_MIB_GOOD_OCTETS_SENT 0x38
823#define MVPP2_MIB_UNICAST_FRAMES_SENT 0x40
824#define MVPP2_MIB_MULTICAST_FRAMES_SENT 0x48
825#define MVPP2_MIB_BROADCAST_FRAMES_SENT 0x4c
826#define MVPP2_MIB_FC_SENT 0x54
827#define MVPP2_MIB_FC_RCVD 0x58
828#define MVPP2_MIB_RX_FIFO_OVERRUN 0x5c
829#define MVPP2_MIB_UNDERSIZE_RCVD 0x60
830#define MVPP2_MIB_FRAGMENTS_RCVD 0x64
831#define MVPP2_MIB_OVERSIZE_RCVD 0x68
832#define MVPP2_MIB_JABBER_RCVD 0x6c
833#define MVPP2_MIB_MAC_RCV_ERROR 0x70
834#define MVPP2_MIB_BAD_CRC_EVENT 0x74
835#define MVPP2_MIB_COLLISION 0x78
836#define MVPP2_MIB_LATE_COLLISION 0x7c
837
838#define MVPP2_MIB_COUNTERS_STATS_DELAY (1 * HZ)
839
Marcin Wojtas3f518502014-07-10 16:52:13 -0300840/* Definitions */
841
842/* Shared Packet Processor resources */
843struct mvpp2 {
844 /* Shared registers' base addresses */
Marcin Wojtas3f518502014-07-10 16:52:13 -0300845 void __iomem *lms_base;
Thomas Petazzonia7868412017-03-07 16:53:13 +0100846 void __iomem *iface_base;
847
Thomas Petazzonidf089aa2017-08-03 10:41:58 +0200848 /* On PPv2.2, each "software thread" can access the base
849 * register through a separate address space, each 64 KB apart
850 * from each other. Typically, such address spaces will be
851 * used per CPU.
Thomas Petazzonia7868412017-03-07 16:53:13 +0100852 */
Thomas Petazzonidf089aa2017-08-03 10:41:58 +0200853 void __iomem *swth_base[MVPP2_MAX_THREADS];
Marcin Wojtas3f518502014-07-10 16:52:13 -0300854
Antoine Ténartf84bf382017-08-22 19:08:27 +0200855 /* On PPv2.2, some port control registers are located into the system
856 * controller space. These registers are accessible through a regmap.
857 */
858 struct regmap *sysctrl_base;
859
Marcin Wojtas3f518502014-07-10 16:52:13 -0300860 /* Common clocks */
861 struct clk *pp_clk;
862 struct clk *gop_clk;
Thomas Petazzonifceb55d2017-03-07 16:53:18 +0100863 struct clk *mg_clk;
Gregory CLEMENT4792ea02017-09-29 14:27:39 +0200864 struct clk *axi_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300865
866 /* List of pointers to port structures */
Miquel Raynal118d6292017-11-06 22:56:53 +0100867 int port_count;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300868 struct mvpp2_port **port_list;
869
870 /* Aggregated TXQs */
871 struct mvpp2_tx_queue *aggr_txqs;
872
873 /* BM pools */
874 struct mvpp2_bm_pool *bm_pools;
875
876 /* PRS shadow table */
877 struct mvpp2_prs_shadow *prs_shadow;
878 /* PRS auxiliary table for double vlan entries control */
879 bool *prs_double_vlans;
880
881 /* Tclk value */
882 u32 tclk;
Thomas Petazzonifaca9242017-03-07 16:53:06 +0100883
884 /* HW version */
885 enum { MVPP21, MVPP22 } hw_version;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +0100886
887 /* Maximum number of RXQs per port */
888 unsigned int max_port_rxqs;
Miquel Raynal118d6292017-11-06 22:56:53 +0100889
Miquel Raynale5c500e2017-11-08 08:59:40 +0100890 /* Workqueue to gather hardware statistics */
Miquel Raynal118d6292017-11-06 22:56:53 +0100891 char queue_name[30];
892 struct workqueue_struct *stats_queue;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300893};
894
895struct mvpp2_pcpu_stats {
896 struct u64_stats_sync syncp;
897 u64 rx_packets;
898 u64 rx_bytes;
899 u64 tx_packets;
900 u64 tx_bytes;
901};
902
Marcin Wojtasedc660f2015-08-06 19:00:30 +0200903/* Per-CPU port control */
904struct mvpp2_port_pcpu {
905 struct hrtimer tx_done_timer;
906 bool timer_scheduled;
907 /* Tasklet for egress finalization */
908 struct tasklet_struct tx_done_tasklet;
909};
910
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +0200911struct mvpp2_queue_vector {
912 int irq;
913 struct napi_struct napi;
914 enum { MVPP2_QUEUE_VECTOR_SHARED, MVPP2_QUEUE_VECTOR_PRIVATE } type;
915 int sw_thread_id;
916 u16 sw_thread_mask;
917 int first_rxq;
918 int nrxqs;
919 u32 pending_cause_rx;
920 struct mvpp2_port *port;
921};
922
Marcin Wojtas3f518502014-07-10 16:52:13 -0300923struct mvpp2_port {
924 u8 id;
925
Thomas Petazzonia7868412017-03-07 16:53:13 +0100926 /* Index of the port from the "group of ports" complex point
927 * of view
928 */
929 int gop_id;
930
Antoine Tenartfd3651b2017-09-01 11:04:54 +0200931 int link_irq;
932
Marcin Wojtas3f518502014-07-10 16:52:13 -0300933 struct mvpp2 *priv;
934
935 /* Per-port registers' base address */
936 void __iomem *base;
Miquel Raynal118d6292017-11-06 22:56:53 +0100937 void __iomem *stats_base;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300938
939 struct mvpp2_rx_queue **rxqs;
Thomas Petazzoni09f83972017-08-03 10:41:57 +0200940 unsigned int nrxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300941 struct mvpp2_tx_queue **txqs;
Thomas Petazzoni09f83972017-08-03 10:41:57 +0200942 unsigned int ntxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300943 struct net_device *dev;
944
945 int pkt_size;
946
Marcin Wojtasedc660f2015-08-06 19:00:30 +0200947 /* Per-CPU port control */
948 struct mvpp2_port_pcpu __percpu *pcpu;
949
Marcin Wojtas3f518502014-07-10 16:52:13 -0300950 /* Flags */
951 unsigned long flags;
952
953 u16 tx_ring_size;
954 u16 rx_ring_size;
955 struct mvpp2_pcpu_stats __percpu *stats;
Miquel Raynal118d6292017-11-06 22:56:53 +0100956 u64 *ethtool_stats;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300957
Miquel Raynale5c500e2017-11-08 08:59:40 +0100958 /* Per-port work and its lock to gather hardware statistics */
959 struct mutex gather_stats_lock;
960 struct delayed_work stats_work;
961
Marcin Wojtas3f518502014-07-10 16:52:13 -0300962 phy_interface_t phy_interface;
963 struct device_node *phy_node;
Antoine Tenart542897d2017-08-30 10:29:15 +0200964 struct phy *comphy;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300965 unsigned int link;
966 unsigned int duplex;
967 unsigned int speed;
968
969 struct mvpp2_bm_pool *pool_long;
970 struct mvpp2_bm_pool *pool_short;
971
972 /* Index of first port's physical RXQ */
973 u8 first_rxq;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +0200974
975 struct mvpp2_queue_vector qvecs[MVPP2_MAX_QVECS];
976 unsigned int nqvecs;
Thomas Petazzoni213f4282017-08-03 10:42:00 +0200977 bool has_tx_irqs;
978
979 u32 tx_time_coal;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300980};
981
982/* The mvpp2_tx_desc and mvpp2_rx_desc structures describe the
983 * layout of the transmit and reception DMA descriptors, and their
984 * layout is therefore defined by the hardware design
985 */
986
987#define MVPP2_TXD_L3_OFF_SHIFT 0
988#define MVPP2_TXD_IP_HLEN_SHIFT 8
989#define MVPP2_TXD_L4_CSUM_FRAG BIT(13)
990#define MVPP2_TXD_L4_CSUM_NOT BIT(14)
991#define MVPP2_TXD_IP_CSUM_DISABLE BIT(15)
992#define MVPP2_TXD_PADDING_DISABLE BIT(23)
993#define MVPP2_TXD_L4_UDP BIT(24)
994#define MVPP2_TXD_L3_IP6 BIT(26)
995#define MVPP2_TXD_L_DESC BIT(28)
996#define MVPP2_TXD_F_DESC BIT(29)
997
998#define MVPP2_RXD_ERR_SUMMARY BIT(15)
999#define MVPP2_RXD_ERR_CODE_MASK (BIT(13) | BIT(14))
1000#define MVPP2_RXD_ERR_CRC 0x0
1001#define MVPP2_RXD_ERR_OVERRUN BIT(13)
1002#define MVPP2_RXD_ERR_RESOURCE (BIT(13) | BIT(14))
1003#define MVPP2_RXD_BM_POOL_ID_OFFS 16
1004#define MVPP2_RXD_BM_POOL_ID_MASK (BIT(16) | BIT(17) | BIT(18))
1005#define MVPP2_RXD_HWF_SYNC BIT(21)
1006#define MVPP2_RXD_L4_CSUM_OK BIT(22)
1007#define MVPP2_RXD_IP4_HEADER_ERR BIT(24)
1008#define MVPP2_RXD_L4_TCP BIT(25)
1009#define MVPP2_RXD_L4_UDP BIT(26)
1010#define MVPP2_RXD_L3_IP4 BIT(28)
1011#define MVPP2_RXD_L3_IP6 BIT(30)
1012#define MVPP2_RXD_BUF_HDR BIT(31)
1013
Thomas Petazzoni054f6372017-03-07 16:53:07 +01001014/* HW TX descriptor for PPv2.1 */
1015struct mvpp21_tx_desc {
Marcin Wojtas3f518502014-07-10 16:52:13 -03001016 u32 command; /* Options used by HW for packet transmitting.*/
1017 u8 packet_offset; /* the offset from the buffer beginning */
1018 u8 phys_txq; /* destination queue ID */
1019 u16 data_size; /* data size of transmitted packet in bytes */
Thomas Petazzoni20396132017-03-07 16:53:00 +01001020 u32 buf_dma_addr; /* physical addr of transmitted buffer */
Marcin Wojtas3f518502014-07-10 16:52:13 -03001021 u32 buf_cookie; /* cookie for access to TX buffer in tx path */
1022 u32 reserved1[3]; /* hw_cmd (for future use, BM, PON, PNC) */
1023 u32 reserved2; /* reserved (for future use) */
1024};
1025
Thomas Petazzoni054f6372017-03-07 16:53:07 +01001026/* HW RX descriptor for PPv2.1 */
1027struct mvpp21_rx_desc {
Marcin Wojtas3f518502014-07-10 16:52:13 -03001028 u32 status; /* info about received packet */
1029 u16 reserved1; /* parser_info (for future use, PnC) */
1030 u16 data_size; /* size of received packet in bytes */
Thomas Petazzoni20396132017-03-07 16:53:00 +01001031 u32 buf_dma_addr; /* physical address of the buffer */
Marcin Wojtas3f518502014-07-10 16:52:13 -03001032 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
1033 u16 reserved2; /* gem_port_id (for future use, PON) */
1034 u16 reserved3; /* csum_l4 (for future use, PnC) */
1035 u8 reserved4; /* bm_qset (for future use, BM) */
1036 u8 reserved5;
1037 u16 reserved6; /* classify_info (for future use, PnC) */
1038 u32 reserved7; /* flow_id (for future use, PnC) */
1039 u32 reserved8;
1040};
1041
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001042/* HW TX descriptor for PPv2.2 */
1043struct mvpp22_tx_desc {
1044 u32 command;
1045 u8 packet_offset;
1046 u8 phys_txq;
1047 u16 data_size;
1048 u64 reserved1;
1049 u64 buf_dma_addr_ptp;
1050 u64 buf_cookie_misc;
1051};
1052
1053/* HW RX descriptor for PPv2.2 */
1054struct mvpp22_rx_desc {
1055 u32 status;
1056 u16 reserved1;
1057 u16 data_size;
1058 u32 reserved2;
1059 u32 reserved3;
1060 u64 buf_dma_addr_key_hash;
1061 u64 buf_cookie_misc;
1062};
1063
Thomas Petazzoni054f6372017-03-07 16:53:07 +01001064/* Opaque type used by the driver to manipulate the HW TX and RX
1065 * descriptors
1066 */
1067struct mvpp2_tx_desc {
1068 union {
1069 struct mvpp21_tx_desc pp21;
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001070 struct mvpp22_tx_desc pp22;
Thomas Petazzoni054f6372017-03-07 16:53:07 +01001071 };
1072};
1073
1074struct mvpp2_rx_desc {
1075 union {
1076 struct mvpp21_rx_desc pp21;
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001077 struct mvpp22_rx_desc pp22;
Thomas Petazzoni054f6372017-03-07 16:53:07 +01001078 };
1079};
1080
Thomas Petazzoni83544912016-12-21 11:28:49 +01001081struct mvpp2_txq_pcpu_buf {
1082 /* Transmitted SKB */
1083 struct sk_buff *skb;
1084
1085 /* Physical address of transmitted buffer */
Thomas Petazzoni20396132017-03-07 16:53:00 +01001086 dma_addr_t dma;
Thomas Petazzoni83544912016-12-21 11:28:49 +01001087
1088 /* Size transmitted */
1089 size_t size;
1090};
1091
Marcin Wojtas3f518502014-07-10 16:52:13 -03001092/* Per-CPU Tx queue control */
1093struct mvpp2_txq_pcpu {
1094 int cpu;
1095
1096 /* Number of Tx DMA descriptors in the descriptor ring */
1097 int size;
1098
1099 /* Number of currently used Tx DMA descriptor in the
1100 * descriptor ring
1101 */
1102 int count;
1103
Antoine Tenart1d17db02017-10-30 11:23:31 +01001104 int wake_threshold;
1105 int stop_threshold;
1106
Marcin Wojtas3f518502014-07-10 16:52:13 -03001107 /* Number of Tx DMA descriptors reserved for each CPU */
1108 int reserved_num;
1109
Thomas Petazzoni83544912016-12-21 11:28:49 +01001110 /* Infos about transmitted buffers */
1111 struct mvpp2_txq_pcpu_buf *buffs;
Marcin Wojtas71ce3912015-08-06 19:00:29 +02001112
Marcin Wojtas3f518502014-07-10 16:52:13 -03001113 /* Index of last TX DMA descriptor that was inserted */
1114 int txq_put_index;
1115
1116 /* Index of the TX DMA descriptor to be cleaned up */
1117 int txq_get_index;
Antoine Ténart186cd4d2017-08-23 09:46:56 +02001118
1119 /* DMA buffer for TSO headers */
1120 char *tso_headers;
1121 dma_addr_t tso_headers_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001122};
1123
1124struct mvpp2_tx_queue {
1125 /* Physical number of this Tx queue */
1126 u8 id;
1127
1128 /* Logical number of this Tx queue */
1129 u8 log_id;
1130
1131 /* Number of Tx DMA descriptors in the descriptor ring */
1132 int size;
1133
1134 /* Number of currently used Tx DMA descriptor in the descriptor ring */
1135 int count;
1136
1137 /* Per-CPU control of physical Tx queues */
1138 struct mvpp2_txq_pcpu __percpu *pcpu;
1139
Marcin Wojtas3f518502014-07-10 16:52:13 -03001140 u32 done_pkts_coal;
1141
1142 /* Virtual address of thex Tx DMA descriptors array */
1143 struct mvpp2_tx_desc *descs;
1144
1145 /* DMA address of the Tx DMA descriptors array */
Thomas Petazzoni20396132017-03-07 16:53:00 +01001146 dma_addr_t descs_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001147
1148 /* Index of the last Tx DMA descriptor */
1149 int last_desc;
1150
1151 /* Index of the next Tx DMA descriptor to process */
1152 int next_desc_to_proc;
1153};
1154
1155struct mvpp2_rx_queue {
1156 /* RX queue number, in the range 0-31 for physical RXQs */
1157 u8 id;
1158
1159 /* Num of rx descriptors in the rx descriptor ring */
1160 int size;
1161
1162 u32 pkts_coal;
1163 u32 time_coal;
1164
1165 /* Virtual address of the RX DMA descriptors array */
1166 struct mvpp2_rx_desc *descs;
1167
1168 /* DMA address of the RX DMA descriptors array */
Thomas Petazzoni20396132017-03-07 16:53:00 +01001169 dma_addr_t descs_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001170
1171 /* Index of the last RX DMA descriptor */
1172 int last_desc;
1173
1174 /* Index of the next RX DMA descriptor to process */
1175 int next_desc_to_proc;
1176
1177 /* ID of port to which physical RXQ is mapped */
1178 int port;
1179
1180 /* Port's logic RXQ number to which physical RXQ is mapped */
1181 int logic_rxq;
1182};
1183
1184union mvpp2_prs_tcam_entry {
1185 u32 word[MVPP2_PRS_TCAM_WORDS];
1186 u8 byte[MVPP2_PRS_TCAM_WORDS * 4];
1187};
1188
1189union mvpp2_prs_sram_entry {
1190 u32 word[MVPP2_PRS_SRAM_WORDS];
1191 u8 byte[MVPP2_PRS_SRAM_WORDS * 4];
1192};
1193
1194struct mvpp2_prs_entry {
1195 u32 index;
1196 union mvpp2_prs_tcam_entry tcam;
1197 union mvpp2_prs_sram_entry sram;
1198};
1199
1200struct mvpp2_prs_shadow {
1201 bool valid;
1202 bool finish;
1203
1204 /* Lookup ID */
1205 int lu;
1206
1207 /* User defined offset */
1208 int udf;
1209
1210 /* Result info */
1211 u32 ri;
1212 u32 ri_mask;
1213};
1214
1215struct mvpp2_cls_flow_entry {
1216 u32 index;
1217 u32 data[MVPP2_CLS_FLOWS_TBL_DATA_WORDS];
1218};
1219
1220struct mvpp2_cls_lookup_entry {
1221 u32 lkpid;
1222 u32 way;
1223 u32 data;
1224};
1225
1226struct mvpp2_bm_pool {
1227 /* Pool number in the range 0-7 */
1228 int id;
1229 enum mvpp2_bm_type type;
1230
1231 /* Buffer Pointers Pool External (BPPE) size */
1232 int size;
Thomas Petazzonid01524d2017-03-07 16:53:09 +01001233 /* BPPE size in bytes */
1234 int size_bytes;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001235 /* Number of buffers for this pool */
1236 int buf_num;
1237 /* Pool buffer size */
1238 int buf_size;
1239 /* Packet size */
1240 int pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01001241 int frag_size;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001242
1243 /* BPPE virtual base address */
1244 u32 *virt_addr;
Thomas Petazzoni20396132017-03-07 16:53:00 +01001245 /* BPPE DMA base address */
1246 dma_addr_t dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001247
1248 /* Ports using BM pool */
1249 u32 port_map;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001250};
1251
Antoine Tenart20920262017-10-23 15:24:30 +02001252#define IS_TSO_HEADER(txq_pcpu, addr) \
1253 ((addr) >= (txq_pcpu)->tso_headers_dma && \
1254 (addr) < (txq_pcpu)->tso_headers_dma + \
1255 (txq_pcpu)->size * TSO_HEADER_SIZE)
1256
Thomas Petazzoni213f4282017-08-03 10:42:00 +02001257/* Queue modes */
1258#define MVPP2_QDIST_SINGLE_MODE 0
1259#define MVPP2_QDIST_MULTI_MODE 1
1260
1261static int queue_mode = MVPP2_QDIST_SINGLE_MODE;
1262
1263module_param(queue_mode, int, 0444);
1264MODULE_PARM_DESC(queue_mode, "Set queue_mode (single=0, multi=1)");
1265
Marcin Wojtas3f518502014-07-10 16:52:13 -03001266#define MVPP2_DRIVER_NAME "mvpp2"
1267#define MVPP2_DRIVER_VERSION "1.0"
1268
1269/* Utility/helper methods */
1270
1271static void mvpp2_write(struct mvpp2 *priv, u32 offset, u32 data)
1272{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001273 writel(data, priv->swth_base[0] + offset);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001274}
1275
1276static u32 mvpp2_read(struct mvpp2 *priv, u32 offset)
1277{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001278 return readl(priv->swth_base[0] + offset);
Thomas Petazzonia7868412017-03-07 16:53:13 +01001279}
1280
1281/* These accessors should be used to access:
1282 *
1283 * - per-CPU registers, where each CPU has its own copy of the
1284 * register.
1285 *
1286 * MVPP2_BM_VIRT_ALLOC_REG
1287 * MVPP2_BM_ADDR_HIGH_ALLOC
1288 * MVPP22_BM_ADDR_HIGH_RLS_REG
1289 * MVPP2_BM_VIRT_RLS_REG
1290 * MVPP2_ISR_RX_TX_CAUSE_REG
1291 * MVPP2_ISR_RX_TX_MASK_REG
1292 * MVPP2_TXQ_NUM_REG
1293 * MVPP2_AGGR_TXQ_UPDATE_REG
1294 * MVPP2_TXQ_RSVD_REQ_REG
1295 * MVPP2_TXQ_RSVD_RSLT_REG
1296 * MVPP2_TXQ_SENT_REG
1297 * MVPP2_RXQ_NUM_REG
1298 *
1299 * - global registers that must be accessed through a specific CPU
1300 * window, because they are related to an access to a per-CPU
1301 * register
1302 *
1303 * MVPP2_BM_PHY_ALLOC_REG (related to MVPP2_BM_VIRT_ALLOC_REG)
1304 * MVPP2_BM_PHY_RLS_REG (related to MVPP2_BM_VIRT_RLS_REG)
1305 * MVPP2_RXQ_THRESH_REG (related to MVPP2_RXQ_NUM_REG)
1306 * MVPP2_RXQ_DESC_ADDR_REG (related to MVPP2_RXQ_NUM_REG)
1307 * MVPP2_RXQ_DESC_SIZE_REG (related to MVPP2_RXQ_NUM_REG)
1308 * MVPP2_RXQ_INDEX_REG (related to MVPP2_RXQ_NUM_REG)
1309 * MVPP2_TXQ_PENDING_REG (related to MVPP2_TXQ_NUM_REG)
1310 * MVPP2_TXQ_DESC_ADDR_REG (related to MVPP2_TXQ_NUM_REG)
1311 * MVPP2_TXQ_DESC_SIZE_REG (related to MVPP2_TXQ_NUM_REG)
1312 * MVPP2_TXQ_INDEX_REG (related to MVPP2_TXQ_NUM_REG)
1313 * MVPP2_TXQ_PENDING_REG (related to MVPP2_TXQ_NUM_REG)
1314 * MVPP2_TXQ_PREF_BUF_REG (related to MVPP2_TXQ_NUM_REG)
1315 * MVPP2_TXQ_PREF_BUF_REG (related to MVPP2_TXQ_NUM_REG)
1316 */
1317static void mvpp2_percpu_write(struct mvpp2 *priv, int cpu,
1318 u32 offset, u32 data)
1319{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001320 writel(data, priv->swth_base[cpu] + offset);
Thomas Petazzonia7868412017-03-07 16:53:13 +01001321}
1322
1323static u32 mvpp2_percpu_read(struct mvpp2 *priv, int cpu,
1324 u32 offset)
1325{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001326 return readl(priv->swth_base[cpu] + offset);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001327}
1328
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001329static dma_addr_t mvpp2_txdesc_dma_addr_get(struct mvpp2_port *port,
1330 struct mvpp2_tx_desc *tx_desc)
1331{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001332 if (port->priv->hw_version == MVPP21)
1333 return tx_desc->pp21.buf_dma_addr;
1334 else
1335 return tx_desc->pp22.buf_dma_addr_ptp & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001336}
1337
1338static void mvpp2_txdesc_dma_addr_set(struct mvpp2_port *port,
1339 struct mvpp2_tx_desc *tx_desc,
1340 dma_addr_t dma_addr)
1341{
Antoine Tenart6eb5d372017-10-30 11:23:33 +01001342 dma_addr_t addr, offset;
1343
1344 addr = dma_addr & ~MVPP2_TX_DESC_ALIGN;
1345 offset = dma_addr & MVPP2_TX_DESC_ALIGN;
1346
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001347 if (port->priv->hw_version == MVPP21) {
Antoine Tenart6eb5d372017-10-30 11:23:33 +01001348 tx_desc->pp21.buf_dma_addr = addr;
1349 tx_desc->pp21.packet_offset = offset;
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001350 } else {
Antoine Tenart6eb5d372017-10-30 11:23:33 +01001351 u64 val = (u64)addr;
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001352
1353 tx_desc->pp22.buf_dma_addr_ptp &= ~GENMASK_ULL(40, 0);
1354 tx_desc->pp22.buf_dma_addr_ptp |= val;
Antoine Tenart6eb5d372017-10-30 11:23:33 +01001355 tx_desc->pp22.packet_offset = offset;
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001356 }
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001357}
1358
1359static size_t mvpp2_txdesc_size_get(struct mvpp2_port *port,
1360 struct mvpp2_tx_desc *tx_desc)
1361{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001362 if (port->priv->hw_version == MVPP21)
1363 return tx_desc->pp21.data_size;
1364 else
1365 return tx_desc->pp22.data_size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001366}
1367
1368static void mvpp2_txdesc_size_set(struct mvpp2_port *port,
1369 struct mvpp2_tx_desc *tx_desc,
1370 size_t size)
1371{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001372 if (port->priv->hw_version == MVPP21)
1373 tx_desc->pp21.data_size = size;
1374 else
1375 tx_desc->pp22.data_size = size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001376}
1377
1378static void mvpp2_txdesc_txq_set(struct mvpp2_port *port,
1379 struct mvpp2_tx_desc *tx_desc,
1380 unsigned int txq)
1381{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001382 if (port->priv->hw_version == MVPP21)
1383 tx_desc->pp21.phys_txq = txq;
1384 else
1385 tx_desc->pp22.phys_txq = txq;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001386}
1387
1388static void mvpp2_txdesc_cmd_set(struct mvpp2_port *port,
1389 struct mvpp2_tx_desc *tx_desc,
1390 unsigned int command)
1391{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001392 if (port->priv->hw_version == MVPP21)
1393 tx_desc->pp21.command = command;
1394 else
1395 tx_desc->pp22.command = command;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001396}
1397
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001398static unsigned int mvpp2_txdesc_offset_get(struct mvpp2_port *port,
1399 struct mvpp2_tx_desc *tx_desc)
1400{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001401 if (port->priv->hw_version == MVPP21)
1402 return tx_desc->pp21.packet_offset;
1403 else
1404 return tx_desc->pp22.packet_offset;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001405}
1406
1407static dma_addr_t mvpp2_rxdesc_dma_addr_get(struct mvpp2_port *port,
1408 struct mvpp2_rx_desc *rx_desc)
1409{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001410 if (port->priv->hw_version == MVPP21)
1411 return rx_desc->pp21.buf_dma_addr;
1412 else
1413 return rx_desc->pp22.buf_dma_addr_key_hash & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001414}
1415
1416static unsigned long mvpp2_rxdesc_cookie_get(struct mvpp2_port *port,
1417 struct mvpp2_rx_desc *rx_desc)
1418{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001419 if (port->priv->hw_version == MVPP21)
1420 return rx_desc->pp21.buf_cookie;
1421 else
1422 return rx_desc->pp22.buf_cookie_misc & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001423}
1424
1425static size_t mvpp2_rxdesc_size_get(struct mvpp2_port *port,
1426 struct mvpp2_rx_desc *rx_desc)
1427{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001428 if (port->priv->hw_version == MVPP21)
1429 return rx_desc->pp21.data_size;
1430 else
1431 return rx_desc->pp22.data_size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001432}
1433
1434static u32 mvpp2_rxdesc_status_get(struct mvpp2_port *port,
1435 struct mvpp2_rx_desc *rx_desc)
1436{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001437 if (port->priv->hw_version == MVPP21)
1438 return rx_desc->pp21.status;
1439 else
1440 return rx_desc->pp22.status;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001441}
1442
Marcin Wojtas3f518502014-07-10 16:52:13 -03001443static void mvpp2_txq_inc_get(struct mvpp2_txq_pcpu *txq_pcpu)
1444{
1445 txq_pcpu->txq_get_index++;
1446 if (txq_pcpu->txq_get_index == txq_pcpu->size)
1447 txq_pcpu->txq_get_index = 0;
1448}
1449
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001450static void mvpp2_txq_inc_put(struct mvpp2_port *port,
1451 struct mvpp2_txq_pcpu *txq_pcpu,
Marcin Wojtas71ce3912015-08-06 19:00:29 +02001452 struct sk_buff *skb,
1453 struct mvpp2_tx_desc *tx_desc)
Marcin Wojtas3f518502014-07-10 16:52:13 -03001454{
Thomas Petazzoni83544912016-12-21 11:28:49 +01001455 struct mvpp2_txq_pcpu_buf *tx_buf =
1456 txq_pcpu->buffs + txq_pcpu->txq_put_index;
1457 tx_buf->skb = skb;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001458 tx_buf->size = mvpp2_txdesc_size_get(port, tx_desc);
1459 tx_buf->dma = mvpp2_txdesc_dma_addr_get(port, tx_desc) +
1460 mvpp2_txdesc_offset_get(port, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001461 txq_pcpu->txq_put_index++;
1462 if (txq_pcpu->txq_put_index == txq_pcpu->size)
1463 txq_pcpu->txq_put_index = 0;
1464}
1465
1466/* Get number of physical egress port */
1467static inline int mvpp2_egress_port(struct mvpp2_port *port)
1468{
1469 return MVPP2_MAX_TCONT + port->id;
1470}
1471
1472/* Get number of physical TXQ */
1473static inline int mvpp2_txq_phys(int port, int txq)
1474{
1475 return (MVPP2_MAX_TCONT + port) * MVPP2_MAX_TXQ + txq;
1476}
1477
1478/* Parser configuration routines */
1479
1480/* Update parser tcam and sram hw entries */
1481static int mvpp2_prs_hw_write(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1482{
1483 int i;
1484
1485 if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1486 return -EINVAL;
1487
1488 /* Clear entry invalidation bit */
1489 pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] &= ~MVPP2_PRS_TCAM_INV_MASK;
1490
1491 /* Write tcam index - indirect access */
1492 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1493 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1494 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), pe->tcam.word[i]);
1495
1496 /* Write sram index - indirect access */
1497 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1498 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1499 mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), pe->sram.word[i]);
1500
1501 return 0;
1502}
1503
1504/* Read tcam entry from hw */
1505static int mvpp2_prs_hw_read(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1506{
1507 int i;
1508
1509 if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1510 return -EINVAL;
1511
1512 /* Write tcam index - indirect access */
1513 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1514
1515 pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] = mvpp2_read(priv,
1516 MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD));
1517 if (pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] & MVPP2_PRS_TCAM_INV_MASK)
1518 return MVPP2_PRS_TCAM_ENTRY_INVALID;
1519
1520 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1521 pe->tcam.word[i] = mvpp2_read(priv, MVPP2_PRS_TCAM_DATA_REG(i));
1522
1523 /* Write sram index - indirect access */
1524 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1525 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1526 pe->sram.word[i] = mvpp2_read(priv, MVPP2_PRS_SRAM_DATA_REG(i));
1527
1528 return 0;
1529}
1530
1531/* Invalidate tcam hw entry */
1532static void mvpp2_prs_hw_inv(struct mvpp2 *priv, int index)
1533{
1534 /* Write index - indirect access */
1535 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
1536 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD),
1537 MVPP2_PRS_TCAM_INV_MASK);
1538}
1539
1540/* Enable shadow table entry and set its lookup ID */
1541static void mvpp2_prs_shadow_set(struct mvpp2 *priv, int index, int lu)
1542{
1543 priv->prs_shadow[index].valid = true;
1544 priv->prs_shadow[index].lu = lu;
1545}
1546
1547/* Update ri fields in shadow table entry */
1548static void mvpp2_prs_shadow_ri_set(struct mvpp2 *priv, int index,
1549 unsigned int ri, unsigned int ri_mask)
1550{
1551 priv->prs_shadow[index].ri_mask = ri_mask;
1552 priv->prs_shadow[index].ri = ri;
1553}
1554
1555/* Update lookup field in tcam sw entry */
1556static void mvpp2_prs_tcam_lu_set(struct mvpp2_prs_entry *pe, unsigned int lu)
1557{
1558 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_LU_BYTE);
1559
1560 pe->tcam.byte[MVPP2_PRS_TCAM_LU_BYTE] = lu;
1561 pe->tcam.byte[enable_off] = MVPP2_PRS_LU_MASK;
1562}
1563
1564/* Update mask for single port in tcam sw entry */
1565static void mvpp2_prs_tcam_port_set(struct mvpp2_prs_entry *pe,
1566 unsigned int port, bool add)
1567{
1568 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1569
1570 if (add)
1571 pe->tcam.byte[enable_off] &= ~(1 << port);
1572 else
1573 pe->tcam.byte[enable_off] |= 1 << port;
1574}
1575
1576/* Update port map in tcam sw entry */
1577static void mvpp2_prs_tcam_port_map_set(struct mvpp2_prs_entry *pe,
1578 unsigned int ports)
1579{
1580 unsigned char port_mask = MVPP2_PRS_PORT_MASK;
1581 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1582
1583 pe->tcam.byte[MVPP2_PRS_TCAM_PORT_BYTE] = 0;
1584 pe->tcam.byte[enable_off] &= ~port_mask;
1585 pe->tcam.byte[enable_off] |= ~ports & MVPP2_PRS_PORT_MASK;
1586}
1587
1588/* Obtain port map from tcam sw entry */
1589static unsigned int mvpp2_prs_tcam_port_map_get(struct mvpp2_prs_entry *pe)
1590{
1591 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1592
1593 return ~(pe->tcam.byte[enable_off]) & MVPP2_PRS_PORT_MASK;
1594}
1595
1596/* Set byte of data and its enable bits in tcam sw entry */
1597static void mvpp2_prs_tcam_data_byte_set(struct mvpp2_prs_entry *pe,
1598 unsigned int offs, unsigned char byte,
1599 unsigned char enable)
1600{
1601 pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)] = byte;
1602 pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)] = enable;
1603}
1604
1605/* Get byte of data and its enable bits from tcam sw entry */
1606static void mvpp2_prs_tcam_data_byte_get(struct mvpp2_prs_entry *pe,
1607 unsigned int offs, unsigned char *byte,
1608 unsigned char *enable)
1609{
1610 *byte = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)];
1611 *enable = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)];
1612}
1613
1614/* Compare tcam data bytes with a pattern */
1615static bool mvpp2_prs_tcam_data_cmp(struct mvpp2_prs_entry *pe, int offs,
1616 u16 data)
1617{
1618 int off = MVPP2_PRS_TCAM_DATA_BYTE(offs);
1619 u16 tcam_data;
1620
Antoine Tenartef4816f2017-10-24 11:41:26 +02001621 tcam_data = (pe->tcam.byte[off + 1] << 8) | pe->tcam.byte[off];
Marcin Wojtas3f518502014-07-10 16:52:13 -03001622 if (tcam_data != data)
1623 return false;
1624 return true;
1625}
1626
1627/* Update ai bits in tcam sw entry */
1628static void mvpp2_prs_tcam_ai_update(struct mvpp2_prs_entry *pe,
1629 unsigned int bits, unsigned int enable)
1630{
1631 int i, ai_idx = MVPP2_PRS_TCAM_AI_BYTE;
1632
1633 for (i = 0; i < MVPP2_PRS_AI_BITS; i++) {
1634
1635 if (!(enable & BIT(i)))
1636 continue;
1637
1638 if (bits & BIT(i))
1639 pe->tcam.byte[ai_idx] |= 1 << i;
1640 else
1641 pe->tcam.byte[ai_idx] &= ~(1 << i);
1642 }
1643
1644 pe->tcam.byte[MVPP2_PRS_TCAM_EN_OFFS(ai_idx)] |= enable;
1645}
1646
1647/* Get ai bits from tcam sw entry */
1648static int mvpp2_prs_tcam_ai_get(struct mvpp2_prs_entry *pe)
1649{
1650 return pe->tcam.byte[MVPP2_PRS_TCAM_AI_BYTE];
1651}
1652
1653/* Set ethertype in tcam sw entry */
1654static void mvpp2_prs_match_etype(struct mvpp2_prs_entry *pe, int offset,
1655 unsigned short ethertype)
1656{
1657 mvpp2_prs_tcam_data_byte_set(pe, offset + 0, ethertype >> 8, 0xff);
1658 mvpp2_prs_tcam_data_byte_set(pe, offset + 1, ethertype & 0xff, 0xff);
1659}
1660
1661/* Set bits in sram sw entry */
1662static void mvpp2_prs_sram_bits_set(struct mvpp2_prs_entry *pe, int bit_num,
1663 int val)
1664{
1665 pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] |= (val << (bit_num % 8));
1666}
1667
1668/* Clear bits in sram sw entry */
1669static void mvpp2_prs_sram_bits_clear(struct mvpp2_prs_entry *pe, int bit_num,
1670 int val)
1671{
1672 pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] &= ~(val << (bit_num % 8));
1673}
1674
1675/* Update ri bits in sram sw entry */
1676static void mvpp2_prs_sram_ri_update(struct mvpp2_prs_entry *pe,
1677 unsigned int bits, unsigned int mask)
1678{
1679 unsigned int i;
1680
1681 for (i = 0; i < MVPP2_PRS_SRAM_RI_CTRL_BITS; i++) {
1682 int ri_off = MVPP2_PRS_SRAM_RI_OFFS;
1683
1684 if (!(mask & BIT(i)))
1685 continue;
1686
1687 if (bits & BIT(i))
1688 mvpp2_prs_sram_bits_set(pe, ri_off + i, 1);
1689 else
1690 mvpp2_prs_sram_bits_clear(pe, ri_off + i, 1);
1691
1692 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_RI_CTRL_OFFS + i, 1);
1693 }
1694}
1695
1696/* Obtain ri bits from sram sw entry */
1697static int mvpp2_prs_sram_ri_get(struct mvpp2_prs_entry *pe)
1698{
1699 return pe->sram.word[MVPP2_PRS_SRAM_RI_WORD];
1700}
1701
1702/* Update ai bits in sram sw entry */
1703static void mvpp2_prs_sram_ai_update(struct mvpp2_prs_entry *pe,
1704 unsigned int bits, unsigned int mask)
1705{
1706 unsigned int i;
1707 int ai_off = MVPP2_PRS_SRAM_AI_OFFS;
1708
1709 for (i = 0; i < MVPP2_PRS_SRAM_AI_CTRL_BITS; i++) {
1710
1711 if (!(mask & BIT(i)))
1712 continue;
1713
1714 if (bits & BIT(i))
1715 mvpp2_prs_sram_bits_set(pe, ai_off + i, 1);
1716 else
1717 mvpp2_prs_sram_bits_clear(pe, ai_off + i, 1);
1718
1719 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_AI_CTRL_OFFS + i, 1);
1720 }
1721}
1722
1723/* Read ai bits from sram sw entry */
1724static int mvpp2_prs_sram_ai_get(struct mvpp2_prs_entry *pe)
1725{
1726 u8 bits;
1727 int ai_off = MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_AI_OFFS);
1728 int ai_en_off = ai_off + 1;
1729 int ai_shift = MVPP2_PRS_SRAM_AI_OFFS % 8;
1730
1731 bits = (pe->sram.byte[ai_off] >> ai_shift) |
1732 (pe->sram.byte[ai_en_off] << (8 - ai_shift));
1733
1734 return bits;
1735}
1736
1737/* In sram sw entry set lookup ID field of the tcam key to be used in the next
1738 * lookup interation
1739 */
1740static void mvpp2_prs_sram_next_lu_set(struct mvpp2_prs_entry *pe,
1741 unsigned int lu)
1742{
1743 int sram_next_off = MVPP2_PRS_SRAM_NEXT_LU_OFFS;
1744
1745 mvpp2_prs_sram_bits_clear(pe, sram_next_off,
1746 MVPP2_PRS_SRAM_NEXT_LU_MASK);
1747 mvpp2_prs_sram_bits_set(pe, sram_next_off, lu);
1748}
1749
1750/* In the sram sw entry set sign and value of the next lookup offset
1751 * and the offset value generated to the classifier
1752 */
1753static void mvpp2_prs_sram_shift_set(struct mvpp2_prs_entry *pe, int shift,
1754 unsigned int op)
1755{
1756 /* Set sign */
1757 if (shift < 0) {
1758 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1759 shift = 0 - shift;
1760 } else {
1761 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1762 }
1763
1764 /* Set value */
1765 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_SHIFT_OFFS)] =
1766 (unsigned char)shift;
1767
1768 /* Reset and set operation */
1769 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS,
1770 MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK);
1771 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS, op);
1772
1773 /* Set base offset as current */
1774 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1775}
1776
1777/* In the sram sw entry set sign and value of the user defined offset
1778 * generated to the classifier
1779 */
1780static void mvpp2_prs_sram_offset_set(struct mvpp2_prs_entry *pe,
1781 unsigned int type, int offset,
1782 unsigned int op)
1783{
1784 /* Set sign */
1785 if (offset < 0) {
1786 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1787 offset = 0 - offset;
1788 } else {
1789 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1790 }
1791
1792 /* Set value */
1793 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_OFFS,
1794 MVPP2_PRS_SRAM_UDF_MASK);
1795 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_OFFS, offset);
1796 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1797 MVPP2_PRS_SRAM_UDF_BITS)] &=
1798 ~(MVPP2_PRS_SRAM_UDF_MASK >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1799 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1800 MVPP2_PRS_SRAM_UDF_BITS)] |=
1801 (offset >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1802
1803 /* Set offset type */
1804 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS,
1805 MVPP2_PRS_SRAM_UDF_TYPE_MASK);
1806 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS, type);
1807
1808 /* Set offset operation */
1809 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS,
1810 MVPP2_PRS_SRAM_OP_SEL_UDF_MASK);
1811 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS, op);
1812
1813 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1814 MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] &=
1815 ~(MVPP2_PRS_SRAM_OP_SEL_UDF_MASK >>
1816 (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1817
1818 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1819 MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] |=
1820 (op >> (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1821
1822 /* Set base offset as current */
1823 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1824}
1825
1826/* Find parser flow entry */
1827static struct mvpp2_prs_entry *mvpp2_prs_flow_find(struct mvpp2 *priv, int flow)
1828{
1829 struct mvpp2_prs_entry *pe;
1830 int tid;
1831
1832 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
1833 if (!pe)
1834 return NULL;
1835 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
1836
1837 /* Go through the all entires with MVPP2_PRS_LU_FLOWS */
1838 for (tid = MVPP2_PRS_TCAM_SRAM_SIZE - 1; tid >= 0; tid--) {
1839 u8 bits;
1840
1841 if (!priv->prs_shadow[tid].valid ||
1842 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_FLOWS)
1843 continue;
1844
1845 pe->index = tid;
1846 mvpp2_prs_hw_read(priv, pe);
1847 bits = mvpp2_prs_sram_ai_get(pe);
1848
1849 /* Sram store classification lookup ID in AI bits [5:0] */
1850 if ((bits & MVPP2_PRS_FLOW_ID_MASK) == flow)
1851 return pe;
1852 }
1853 kfree(pe);
1854
1855 return NULL;
1856}
1857
1858/* Return first free tcam index, seeking from start to end */
1859static int mvpp2_prs_tcam_first_free(struct mvpp2 *priv, unsigned char start,
1860 unsigned char end)
1861{
1862 int tid;
1863
1864 if (start > end)
1865 swap(start, end);
1866
1867 if (end >= MVPP2_PRS_TCAM_SRAM_SIZE)
1868 end = MVPP2_PRS_TCAM_SRAM_SIZE - 1;
1869
1870 for (tid = start; tid <= end; tid++) {
1871 if (!priv->prs_shadow[tid].valid)
1872 return tid;
1873 }
1874
1875 return -EINVAL;
1876}
1877
1878/* Enable/disable dropping all mac da's */
1879static void mvpp2_prs_mac_drop_all_set(struct mvpp2 *priv, int port, bool add)
1880{
1881 struct mvpp2_prs_entry pe;
1882
1883 if (priv->prs_shadow[MVPP2_PE_DROP_ALL].valid) {
1884 /* Entry exist - update port only */
1885 pe.index = MVPP2_PE_DROP_ALL;
1886 mvpp2_prs_hw_read(priv, &pe);
1887 } else {
1888 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001889 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001890 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1891 pe.index = MVPP2_PE_DROP_ALL;
1892
1893 /* Non-promiscuous mode for all ports - DROP unknown packets */
1894 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
1895 MVPP2_PRS_RI_DROP_MASK);
1896
1897 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1898 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1899
1900 /* Update shadow table */
1901 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1902
1903 /* Mask all ports */
1904 mvpp2_prs_tcam_port_map_set(&pe, 0);
1905 }
1906
1907 /* Update port mask */
1908 mvpp2_prs_tcam_port_set(&pe, port, add);
1909
1910 mvpp2_prs_hw_write(priv, &pe);
1911}
1912
1913/* Set port to promiscuous mode */
1914static void mvpp2_prs_mac_promisc_set(struct mvpp2 *priv, int port, bool add)
1915{
1916 struct mvpp2_prs_entry pe;
1917
Joe Perchesdbedd442015-03-06 20:49:12 -08001918 /* Promiscuous mode - Accept unknown packets */
Marcin Wojtas3f518502014-07-10 16:52:13 -03001919
1920 if (priv->prs_shadow[MVPP2_PE_MAC_PROMISCUOUS].valid) {
1921 /* Entry exist - update port only */
1922 pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1923 mvpp2_prs_hw_read(priv, &pe);
1924 } else {
1925 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001926 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001927 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1928 pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1929
1930 /* Continue - set next lookup */
1931 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
1932
1933 /* Set result info bits */
1934 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_UCAST,
1935 MVPP2_PRS_RI_L2_CAST_MASK);
1936
1937 /* Shift to ethertype */
1938 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
1939 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1940
1941 /* Mask all ports */
1942 mvpp2_prs_tcam_port_map_set(&pe, 0);
1943
1944 /* Update shadow table */
1945 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1946 }
1947
1948 /* Update port mask */
1949 mvpp2_prs_tcam_port_set(&pe, port, add);
1950
1951 mvpp2_prs_hw_write(priv, &pe);
1952}
1953
1954/* Accept multicast */
1955static void mvpp2_prs_mac_multi_set(struct mvpp2 *priv, int port, int index,
1956 bool add)
1957{
1958 struct mvpp2_prs_entry pe;
1959 unsigned char da_mc;
1960
1961 /* Ethernet multicast address first byte is
1962 * 0x01 for IPv4 and 0x33 for IPv6
1963 */
1964 da_mc = (index == MVPP2_PE_MAC_MC_ALL) ? 0x01 : 0x33;
1965
1966 if (priv->prs_shadow[index].valid) {
1967 /* Entry exist - update port only */
1968 pe.index = index;
1969 mvpp2_prs_hw_read(priv, &pe);
1970 } else {
1971 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001972 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001973 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1974 pe.index = index;
1975
1976 /* Continue - set next lookup */
1977 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
1978
1979 /* Set result info bits */
1980 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_MCAST,
1981 MVPP2_PRS_RI_L2_CAST_MASK);
1982
1983 /* Update tcam entry data first byte */
1984 mvpp2_prs_tcam_data_byte_set(&pe, 0, da_mc, 0xff);
1985
1986 /* Shift to ethertype */
1987 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
1988 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1989
1990 /* Mask all ports */
1991 mvpp2_prs_tcam_port_map_set(&pe, 0);
1992
1993 /* Update shadow table */
1994 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1995 }
1996
1997 /* Update port mask */
1998 mvpp2_prs_tcam_port_set(&pe, port, add);
1999
2000 mvpp2_prs_hw_write(priv, &pe);
2001}
2002
2003/* Set entry for dsa packets */
2004static void mvpp2_prs_dsa_tag_set(struct mvpp2 *priv, int port, bool add,
2005 bool tagged, bool extend)
2006{
2007 struct mvpp2_prs_entry pe;
2008 int tid, shift;
2009
2010 if (extend) {
2011 tid = tagged ? MVPP2_PE_EDSA_TAGGED : MVPP2_PE_EDSA_UNTAGGED;
2012 shift = 8;
2013 } else {
2014 tid = tagged ? MVPP2_PE_DSA_TAGGED : MVPP2_PE_DSA_UNTAGGED;
2015 shift = 4;
2016 }
2017
2018 if (priv->prs_shadow[tid].valid) {
2019 /* Entry exist - update port only */
2020 pe.index = tid;
2021 mvpp2_prs_hw_read(priv, &pe);
2022 } else {
2023 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002024 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002025 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
2026 pe.index = tid;
2027
2028 /* Shift 4 bytes if DSA tag or 8 bytes in case of EDSA tag*/
2029 mvpp2_prs_sram_shift_set(&pe, shift,
2030 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2031
2032 /* Update shadow table */
2033 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
2034
2035 if (tagged) {
2036 /* Set tagged bit in DSA tag */
2037 mvpp2_prs_tcam_data_byte_set(&pe, 0,
2038 MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
2039 MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
2040 /* Clear all ai bits for next iteration */
2041 mvpp2_prs_sram_ai_update(&pe, 0,
2042 MVPP2_PRS_SRAM_AI_MASK);
2043 /* If packet is tagged continue check vlans */
2044 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2045 } else {
2046 /* Set result info bits to 'no vlans' */
2047 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
2048 MVPP2_PRS_RI_VLAN_MASK);
2049 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2050 }
2051
2052 /* Mask all ports */
2053 mvpp2_prs_tcam_port_map_set(&pe, 0);
2054 }
2055
2056 /* Update port mask */
2057 mvpp2_prs_tcam_port_set(&pe, port, add);
2058
2059 mvpp2_prs_hw_write(priv, &pe);
2060}
2061
2062/* Set entry for dsa ethertype */
2063static void mvpp2_prs_dsa_tag_ethertype_set(struct mvpp2 *priv, int port,
2064 bool add, bool tagged, bool extend)
2065{
2066 struct mvpp2_prs_entry pe;
2067 int tid, shift, port_mask;
2068
2069 if (extend) {
2070 tid = tagged ? MVPP2_PE_ETYPE_EDSA_TAGGED :
2071 MVPP2_PE_ETYPE_EDSA_UNTAGGED;
2072 port_mask = 0;
2073 shift = 8;
2074 } else {
2075 tid = tagged ? MVPP2_PE_ETYPE_DSA_TAGGED :
2076 MVPP2_PE_ETYPE_DSA_UNTAGGED;
2077 port_mask = MVPP2_PRS_PORT_MASK;
2078 shift = 4;
2079 }
2080
2081 if (priv->prs_shadow[tid].valid) {
2082 /* Entry exist - update port only */
2083 pe.index = tid;
2084 mvpp2_prs_hw_read(priv, &pe);
2085 } else {
2086 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002087 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002088 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
2089 pe.index = tid;
2090
2091 /* Set ethertype */
2092 mvpp2_prs_match_etype(&pe, 0, ETH_P_EDSA);
2093 mvpp2_prs_match_etype(&pe, 2, 0);
2094
2095 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DSA_MASK,
2096 MVPP2_PRS_RI_DSA_MASK);
2097 /* Shift ethertype + 2 byte reserved + tag*/
2098 mvpp2_prs_sram_shift_set(&pe, 2 + MVPP2_ETH_TYPE_LEN + shift,
2099 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2100
2101 /* Update shadow table */
2102 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
2103
2104 if (tagged) {
2105 /* Set tagged bit in DSA tag */
2106 mvpp2_prs_tcam_data_byte_set(&pe,
2107 MVPP2_ETH_TYPE_LEN + 2 + 3,
2108 MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
2109 MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
2110 /* Clear all ai bits for next iteration */
2111 mvpp2_prs_sram_ai_update(&pe, 0,
2112 MVPP2_PRS_SRAM_AI_MASK);
2113 /* If packet is tagged continue check vlans */
2114 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2115 } else {
2116 /* Set result info bits to 'no vlans' */
2117 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
2118 MVPP2_PRS_RI_VLAN_MASK);
2119 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2120 }
2121 /* Mask/unmask all ports, depending on dsa type */
2122 mvpp2_prs_tcam_port_map_set(&pe, port_mask);
2123 }
2124
2125 /* Update port mask */
2126 mvpp2_prs_tcam_port_set(&pe, port, add);
2127
2128 mvpp2_prs_hw_write(priv, &pe);
2129}
2130
2131/* Search for existing single/triple vlan entry */
2132static struct mvpp2_prs_entry *mvpp2_prs_vlan_find(struct mvpp2 *priv,
2133 unsigned short tpid, int ai)
2134{
2135 struct mvpp2_prs_entry *pe;
2136 int tid;
2137
2138 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2139 if (!pe)
2140 return NULL;
2141 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2142
2143 /* Go through the all entries with MVPP2_PRS_LU_VLAN */
2144 for (tid = MVPP2_PE_FIRST_FREE_TID;
2145 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
2146 unsigned int ri_bits, ai_bits;
2147 bool match;
2148
2149 if (!priv->prs_shadow[tid].valid ||
2150 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
2151 continue;
2152
2153 pe->index = tid;
2154
2155 mvpp2_prs_hw_read(priv, pe);
2156 match = mvpp2_prs_tcam_data_cmp(pe, 0, swab16(tpid));
2157 if (!match)
2158 continue;
2159
2160 /* Get vlan type */
2161 ri_bits = mvpp2_prs_sram_ri_get(pe);
2162 ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
2163
2164 /* Get current ai value from tcam */
2165 ai_bits = mvpp2_prs_tcam_ai_get(pe);
2166 /* Clear double vlan bit */
2167 ai_bits &= ~MVPP2_PRS_DBL_VLAN_AI_BIT;
2168
2169 if (ai != ai_bits)
2170 continue;
2171
2172 if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
2173 ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
2174 return pe;
2175 }
2176 kfree(pe);
2177
2178 return NULL;
2179}
2180
2181/* Add/update single/triple vlan entry */
2182static int mvpp2_prs_vlan_add(struct mvpp2 *priv, unsigned short tpid, int ai,
2183 unsigned int port_map)
2184{
2185 struct mvpp2_prs_entry *pe;
2186 int tid_aux, tid;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302187 int ret = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002188
2189 pe = mvpp2_prs_vlan_find(priv, tpid, ai);
2190
2191 if (!pe) {
2192 /* Create new tcam entry */
2193 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_LAST_FREE_TID,
2194 MVPP2_PE_FIRST_FREE_TID);
2195 if (tid < 0)
2196 return tid;
2197
2198 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2199 if (!pe)
2200 return -ENOMEM;
2201
2202 /* Get last double vlan tid */
2203 for (tid_aux = MVPP2_PE_LAST_FREE_TID;
2204 tid_aux >= MVPP2_PE_FIRST_FREE_TID; tid_aux--) {
2205 unsigned int ri_bits;
2206
2207 if (!priv->prs_shadow[tid_aux].valid ||
2208 priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
2209 continue;
2210
2211 pe->index = tid_aux;
2212 mvpp2_prs_hw_read(priv, pe);
2213 ri_bits = mvpp2_prs_sram_ri_get(pe);
2214 if ((ri_bits & MVPP2_PRS_RI_VLAN_MASK) ==
2215 MVPP2_PRS_RI_VLAN_DOUBLE)
2216 break;
2217 }
2218
Sudip Mukherjee43737472014-11-01 16:59:34 +05302219 if (tid <= tid_aux) {
2220 ret = -EINVAL;
Markus Elfringf9fd0e32017-04-17 13:50:35 +02002221 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302222 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002223
Markus Elfringbd6aaf52017-04-17 10:40:32 +02002224 memset(pe, 0, sizeof(*pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002225 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2226 pe->index = tid;
2227
2228 mvpp2_prs_match_etype(pe, 0, tpid);
2229
2230 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_L2);
2231 /* Shift 4 bytes - skip 1 vlan tag */
2232 mvpp2_prs_sram_shift_set(pe, MVPP2_VLAN_TAG_LEN,
2233 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2234 /* Clear all ai bits for next iteration */
2235 mvpp2_prs_sram_ai_update(pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2236
2237 if (ai == MVPP2_PRS_SINGLE_VLAN_AI) {
2238 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_SINGLE,
2239 MVPP2_PRS_RI_VLAN_MASK);
2240 } else {
2241 ai |= MVPP2_PRS_DBL_VLAN_AI_BIT;
2242 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_TRIPLE,
2243 MVPP2_PRS_RI_VLAN_MASK);
2244 }
2245 mvpp2_prs_tcam_ai_update(pe, ai, MVPP2_PRS_SRAM_AI_MASK);
2246
2247 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_VLAN);
2248 }
2249 /* Update ports' mask */
2250 mvpp2_prs_tcam_port_map_set(pe, port_map);
2251
2252 mvpp2_prs_hw_write(priv, pe);
Markus Elfringf9fd0e32017-04-17 13:50:35 +02002253free_pe:
Marcin Wojtas3f518502014-07-10 16:52:13 -03002254 kfree(pe);
2255
Sudip Mukherjee43737472014-11-01 16:59:34 +05302256 return ret;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002257}
2258
2259/* Get first free double vlan ai number */
2260static int mvpp2_prs_double_vlan_ai_free_get(struct mvpp2 *priv)
2261{
2262 int i;
2263
2264 for (i = 1; i < MVPP2_PRS_DBL_VLANS_MAX; i++) {
2265 if (!priv->prs_double_vlans[i])
2266 return i;
2267 }
2268
2269 return -EINVAL;
2270}
2271
2272/* Search for existing double vlan entry */
2273static struct mvpp2_prs_entry *mvpp2_prs_double_vlan_find(struct mvpp2 *priv,
2274 unsigned short tpid1,
2275 unsigned short tpid2)
2276{
2277 struct mvpp2_prs_entry *pe;
2278 int tid;
2279
2280 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2281 if (!pe)
2282 return NULL;
2283 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2284
2285 /* Go through the all entries with MVPP2_PRS_LU_VLAN */
2286 for (tid = MVPP2_PE_FIRST_FREE_TID;
2287 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
2288 unsigned int ri_mask;
2289 bool match;
2290
2291 if (!priv->prs_shadow[tid].valid ||
2292 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
2293 continue;
2294
2295 pe->index = tid;
2296 mvpp2_prs_hw_read(priv, pe);
2297
2298 match = mvpp2_prs_tcam_data_cmp(pe, 0, swab16(tpid1))
2299 && mvpp2_prs_tcam_data_cmp(pe, 4, swab16(tpid2));
2300
2301 if (!match)
2302 continue;
2303
2304 ri_mask = mvpp2_prs_sram_ri_get(pe) & MVPP2_PRS_RI_VLAN_MASK;
2305 if (ri_mask == MVPP2_PRS_RI_VLAN_DOUBLE)
2306 return pe;
2307 }
2308 kfree(pe);
2309
2310 return NULL;
2311}
2312
2313/* Add or update double vlan entry */
2314static int mvpp2_prs_double_vlan_add(struct mvpp2 *priv, unsigned short tpid1,
2315 unsigned short tpid2,
2316 unsigned int port_map)
2317{
2318 struct mvpp2_prs_entry *pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302319 int tid_aux, tid, ai, ret = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002320
2321 pe = mvpp2_prs_double_vlan_find(priv, tpid1, tpid2);
2322
2323 if (!pe) {
2324 /* Create new tcam entry */
2325 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2326 MVPP2_PE_LAST_FREE_TID);
2327 if (tid < 0)
2328 return tid;
2329
2330 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2331 if (!pe)
2332 return -ENOMEM;
2333
2334 /* Set ai value for new double vlan entry */
2335 ai = mvpp2_prs_double_vlan_ai_free_get(priv);
Sudip Mukherjee43737472014-11-01 16:59:34 +05302336 if (ai < 0) {
2337 ret = ai;
Markus Elfringc9a7e122017-04-17 13:03:49 +02002338 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302339 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002340
2341 /* Get first single/triple vlan tid */
2342 for (tid_aux = MVPP2_PE_FIRST_FREE_TID;
2343 tid_aux <= MVPP2_PE_LAST_FREE_TID; tid_aux++) {
2344 unsigned int ri_bits;
2345
2346 if (!priv->prs_shadow[tid_aux].valid ||
2347 priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
2348 continue;
2349
2350 pe->index = tid_aux;
2351 mvpp2_prs_hw_read(priv, pe);
2352 ri_bits = mvpp2_prs_sram_ri_get(pe);
2353 ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
2354 if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
2355 ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
2356 break;
2357 }
2358
Sudip Mukherjee43737472014-11-01 16:59:34 +05302359 if (tid >= tid_aux) {
2360 ret = -ERANGE;
Markus Elfringc9a7e122017-04-17 13:03:49 +02002361 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302362 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002363
Markus Elfringbd6aaf52017-04-17 10:40:32 +02002364 memset(pe, 0, sizeof(*pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002365 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2366 pe->index = tid;
2367
2368 priv->prs_double_vlans[ai] = true;
2369
2370 mvpp2_prs_match_etype(pe, 0, tpid1);
2371 mvpp2_prs_match_etype(pe, 4, tpid2);
2372
2373 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_VLAN);
2374 /* Shift 8 bytes - skip 2 vlan tags */
2375 mvpp2_prs_sram_shift_set(pe, 2 * MVPP2_VLAN_TAG_LEN,
2376 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2377 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_DOUBLE,
2378 MVPP2_PRS_RI_VLAN_MASK);
2379 mvpp2_prs_sram_ai_update(pe, ai | MVPP2_PRS_DBL_VLAN_AI_BIT,
2380 MVPP2_PRS_SRAM_AI_MASK);
2381
2382 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_VLAN);
2383 }
2384
2385 /* Update ports' mask */
2386 mvpp2_prs_tcam_port_map_set(pe, port_map);
2387 mvpp2_prs_hw_write(priv, pe);
Markus Elfringc9a7e122017-04-17 13:03:49 +02002388free_pe:
Marcin Wojtas3f518502014-07-10 16:52:13 -03002389 kfree(pe);
Sudip Mukherjee43737472014-11-01 16:59:34 +05302390 return ret;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002391}
2392
2393/* IPv4 header parsing for fragmentation and L4 offset */
2394static int mvpp2_prs_ip4_proto(struct mvpp2 *priv, unsigned short proto,
2395 unsigned int ri, unsigned int ri_mask)
2396{
2397 struct mvpp2_prs_entry pe;
2398 int tid;
2399
2400 if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
2401 (proto != IPPROTO_IGMP))
2402 return -EINVAL;
2403
Stefan Chulskiaff3da32017-09-25 14:59:46 +02002404 /* Not fragmented packet */
Marcin Wojtas3f518502014-07-10 16:52:13 -03002405 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2406 MVPP2_PE_LAST_FREE_TID);
2407 if (tid < 0)
2408 return tid;
2409
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002410 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002411 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
2412 pe.index = tid;
2413
2414 /* Set next lu to IPv4 */
2415 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2416 mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2417 /* Set L4 offset */
2418 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
2419 sizeof(struct iphdr) - 4,
2420 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2421 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
2422 MVPP2_PRS_IPV4_DIP_AI_BIT);
Stefan Chulskiaff3da32017-09-25 14:59:46 +02002423 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask | MVPP2_PRS_RI_IP_FRAG_MASK);
2424
2425 mvpp2_prs_tcam_data_byte_set(&pe, 2, 0x00,
2426 MVPP2_PRS_TCAM_PROTO_MASK_L);
2427 mvpp2_prs_tcam_data_byte_set(&pe, 3, 0x00,
2428 MVPP2_PRS_TCAM_PROTO_MASK);
Marcin Wojtas3f518502014-07-10 16:52:13 -03002429
2430 mvpp2_prs_tcam_data_byte_set(&pe, 5, proto, MVPP2_PRS_TCAM_PROTO_MASK);
2431 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
2432 /* Unmask all ports */
2433 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2434
2435 /* Update shadow table and hw entry */
2436 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2437 mvpp2_prs_hw_write(priv, &pe);
2438
Stefan Chulskiaff3da32017-09-25 14:59:46 +02002439 /* Fragmented packet */
Marcin Wojtas3f518502014-07-10 16:52:13 -03002440 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2441 MVPP2_PE_LAST_FREE_TID);
2442 if (tid < 0)
2443 return tid;
2444
2445 pe.index = tid;
2446 /* Clear ri before updating */
2447 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2448 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2449 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
2450
Stefan Chulskiaff3da32017-09-25 14:59:46 +02002451 mvpp2_prs_sram_ri_update(&pe, ri | MVPP2_PRS_RI_IP_FRAG_TRUE,
2452 ri_mask | MVPP2_PRS_RI_IP_FRAG_MASK);
2453
2454 mvpp2_prs_tcam_data_byte_set(&pe, 2, 0x00, 0x0);
2455 mvpp2_prs_tcam_data_byte_set(&pe, 3, 0x00, 0x0);
Marcin Wojtas3f518502014-07-10 16:52:13 -03002456
2457 /* Update shadow table and hw entry */
2458 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2459 mvpp2_prs_hw_write(priv, &pe);
2460
2461 return 0;
2462}
2463
2464/* IPv4 L3 multicast or broadcast */
2465static int mvpp2_prs_ip4_cast(struct mvpp2 *priv, unsigned short l3_cast)
2466{
2467 struct mvpp2_prs_entry pe;
2468 int mask, tid;
2469
2470 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2471 MVPP2_PE_LAST_FREE_TID);
2472 if (tid < 0)
2473 return tid;
2474
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002475 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002476 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
2477 pe.index = tid;
2478
2479 switch (l3_cast) {
2480 case MVPP2_PRS_L3_MULTI_CAST:
2481 mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV4_MC,
2482 MVPP2_PRS_IPV4_MC_MASK);
2483 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
2484 MVPP2_PRS_RI_L3_ADDR_MASK);
2485 break;
2486 case MVPP2_PRS_L3_BROAD_CAST:
2487 mask = MVPP2_PRS_IPV4_BC_MASK;
2488 mvpp2_prs_tcam_data_byte_set(&pe, 0, mask, mask);
2489 mvpp2_prs_tcam_data_byte_set(&pe, 1, mask, mask);
2490 mvpp2_prs_tcam_data_byte_set(&pe, 2, mask, mask);
2491 mvpp2_prs_tcam_data_byte_set(&pe, 3, mask, mask);
2492 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_BCAST,
2493 MVPP2_PRS_RI_L3_ADDR_MASK);
2494 break;
2495 default:
2496 return -EINVAL;
2497 }
2498
2499 /* Finished: go to flowid generation */
2500 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2501 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2502
2503 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
2504 MVPP2_PRS_IPV4_DIP_AI_BIT);
2505 /* Unmask all ports */
2506 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2507
2508 /* Update shadow table and hw entry */
2509 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2510 mvpp2_prs_hw_write(priv, &pe);
2511
2512 return 0;
2513}
2514
2515/* Set entries for protocols over IPv6 */
2516static int mvpp2_prs_ip6_proto(struct mvpp2 *priv, unsigned short proto,
2517 unsigned int ri, unsigned int ri_mask)
2518{
2519 struct mvpp2_prs_entry pe;
2520 int tid;
2521
2522 if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
2523 (proto != IPPROTO_ICMPV6) && (proto != IPPROTO_IPIP))
2524 return -EINVAL;
2525
2526 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2527 MVPP2_PE_LAST_FREE_TID);
2528 if (tid < 0)
2529 return tid;
2530
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002531 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002532 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2533 pe.index = tid;
2534
2535 /* Finished: go to flowid generation */
2536 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2537 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2538 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
2539 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
2540 sizeof(struct ipv6hdr) - 6,
2541 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2542
2543 mvpp2_prs_tcam_data_byte_set(&pe, 0, proto, MVPP2_PRS_TCAM_PROTO_MASK);
2544 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
2545 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2546 /* Unmask all ports */
2547 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2548
2549 /* Write HW */
2550 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
2551 mvpp2_prs_hw_write(priv, &pe);
2552
2553 return 0;
2554}
2555
2556/* IPv6 L3 multicast entry */
2557static int mvpp2_prs_ip6_cast(struct mvpp2 *priv, unsigned short l3_cast)
2558{
2559 struct mvpp2_prs_entry pe;
2560 int tid;
2561
2562 if (l3_cast != MVPP2_PRS_L3_MULTI_CAST)
2563 return -EINVAL;
2564
2565 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2566 MVPP2_PE_LAST_FREE_TID);
2567 if (tid < 0)
2568 return tid;
2569
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002570 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002571 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2572 pe.index = tid;
2573
2574 /* Finished: go to flowid generation */
2575 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2576 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
2577 MVPP2_PRS_RI_L3_ADDR_MASK);
2578 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
2579 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2580 /* Shift back to IPv6 NH */
2581 mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2582
2583 mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV6_MC,
2584 MVPP2_PRS_IPV6_MC_MASK);
2585 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2586 /* Unmask all ports */
2587 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2588
2589 /* Update shadow table and hw entry */
2590 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
2591 mvpp2_prs_hw_write(priv, &pe);
2592
2593 return 0;
2594}
2595
2596/* Parser per-port initialization */
2597static void mvpp2_prs_hw_port_init(struct mvpp2 *priv, int port, int lu_first,
2598 int lu_max, int offset)
2599{
2600 u32 val;
2601
2602 /* Set lookup ID */
2603 val = mvpp2_read(priv, MVPP2_PRS_INIT_LOOKUP_REG);
2604 val &= ~MVPP2_PRS_PORT_LU_MASK(port);
2605 val |= MVPP2_PRS_PORT_LU_VAL(port, lu_first);
2606 mvpp2_write(priv, MVPP2_PRS_INIT_LOOKUP_REG, val);
2607
2608 /* Set maximum number of loops for packet received from port */
2609 val = mvpp2_read(priv, MVPP2_PRS_MAX_LOOP_REG(port));
2610 val &= ~MVPP2_PRS_MAX_LOOP_MASK(port);
2611 val |= MVPP2_PRS_MAX_LOOP_VAL(port, lu_max);
2612 mvpp2_write(priv, MVPP2_PRS_MAX_LOOP_REG(port), val);
2613
2614 /* Set initial offset for packet header extraction for the first
2615 * searching loop
2616 */
2617 val = mvpp2_read(priv, MVPP2_PRS_INIT_OFFS_REG(port));
2618 val &= ~MVPP2_PRS_INIT_OFF_MASK(port);
2619 val |= MVPP2_PRS_INIT_OFF_VAL(port, offset);
2620 mvpp2_write(priv, MVPP2_PRS_INIT_OFFS_REG(port), val);
2621}
2622
2623/* Default flow entries initialization for all ports */
2624static void mvpp2_prs_def_flow_init(struct mvpp2 *priv)
2625{
2626 struct mvpp2_prs_entry pe;
2627 int port;
2628
2629 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002630 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002631 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2632 pe.index = MVPP2_PE_FIRST_DEFAULT_FLOW - port;
2633
2634 /* Mask all ports */
2635 mvpp2_prs_tcam_port_map_set(&pe, 0);
2636
2637 /* Set flow ID*/
2638 mvpp2_prs_sram_ai_update(&pe, port, MVPP2_PRS_FLOW_ID_MASK);
2639 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
2640
2641 /* Update shadow table and hw entry */
2642 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_FLOWS);
2643 mvpp2_prs_hw_write(priv, &pe);
2644 }
2645}
2646
2647/* Set default entry for Marvell Header field */
2648static void mvpp2_prs_mh_init(struct mvpp2 *priv)
2649{
2650 struct mvpp2_prs_entry pe;
2651
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002652 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002653
2654 pe.index = MVPP2_PE_MH_DEFAULT;
2655 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MH);
2656 mvpp2_prs_sram_shift_set(&pe, MVPP2_MH_SIZE,
2657 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2658 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_MAC);
2659
2660 /* Unmask all ports */
2661 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2662
2663 /* Update shadow table and hw entry */
2664 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MH);
2665 mvpp2_prs_hw_write(priv, &pe);
2666}
2667
2668/* Set default entires (place holder) for promiscuous, non-promiscuous and
2669 * multicast MAC addresses
2670 */
2671static void mvpp2_prs_mac_init(struct mvpp2 *priv)
2672{
2673 struct mvpp2_prs_entry pe;
2674
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002675 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002676
2677 /* Non-promiscuous mode for all ports - DROP unknown packets */
2678 pe.index = MVPP2_PE_MAC_NON_PROMISCUOUS;
2679 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
2680
2681 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
2682 MVPP2_PRS_RI_DROP_MASK);
2683 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2684 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2685
2686 /* Unmask all ports */
2687 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2688
2689 /* Update shadow table and hw entry */
2690 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2691 mvpp2_prs_hw_write(priv, &pe);
2692
2693 /* place holders only - no ports */
2694 mvpp2_prs_mac_drop_all_set(priv, 0, false);
2695 mvpp2_prs_mac_promisc_set(priv, 0, false);
Antoine Tenart20746d72017-10-24 11:41:27 +02002696 mvpp2_prs_mac_multi_set(priv, 0, MVPP2_PE_MAC_MC_ALL, false);
2697 mvpp2_prs_mac_multi_set(priv, 0, MVPP2_PE_MAC_MC_IP6, false);
Marcin Wojtas3f518502014-07-10 16:52:13 -03002698}
2699
2700/* Set default entries for various types of dsa packets */
2701static void mvpp2_prs_dsa_init(struct mvpp2 *priv)
2702{
2703 struct mvpp2_prs_entry pe;
2704
2705 /* None tagged EDSA entry - place holder */
2706 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
2707 MVPP2_PRS_EDSA);
2708
2709 /* Tagged EDSA entry - place holder */
2710 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
2711
2712 /* None tagged DSA entry - place holder */
2713 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
2714 MVPP2_PRS_DSA);
2715
2716 /* Tagged DSA entry - place holder */
2717 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
2718
2719 /* None tagged EDSA ethertype entry - place holder*/
2720 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
2721 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
2722
2723 /* Tagged EDSA ethertype entry - place holder*/
2724 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
2725 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
2726
2727 /* None tagged DSA ethertype entry */
2728 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
2729 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
2730
2731 /* Tagged DSA ethertype entry */
2732 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
2733 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
2734
2735 /* Set default entry, in case DSA or EDSA tag not found */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002736 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002737 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
2738 pe.index = MVPP2_PE_DSA_DEFAULT;
2739 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2740
2741 /* Shift 0 bytes */
2742 mvpp2_prs_sram_shift_set(&pe, 0, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2743 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2744
2745 /* Clear all sram ai bits for next iteration */
2746 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2747
2748 /* Unmask all ports */
2749 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2750
2751 mvpp2_prs_hw_write(priv, &pe);
2752}
2753
2754/* Match basic ethertypes */
2755static int mvpp2_prs_etype_init(struct mvpp2 *priv)
2756{
2757 struct mvpp2_prs_entry pe;
2758 int tid;
2759
2760 /* Ethertype: PPPoE */
2761 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2762 MVPP2_PE_LAST_FREE_TID);
2763 if (tid < 0)
2764 return tid;
2765
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002766 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002767 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2768 pe.index = tid;
2769
2770 mvpp2_prs_match_etype(&pe, 0, ETH_P_PPP_SES);
2771
2772 mvpp2_prs_sram_shift_set(&pe, MVPP2_PPPOE_HDR_SIZE,
2773 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2774 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2775 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_PPPOE_MASK,
2776 MVPP2_PRS_RI_PPPOE_MASK);
2777
2778 /* Update shadow table and hw entry */
2779 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2780 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2781 priv->prs_shadow[pe.index].finish = false;
2782 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_PPPOE_MASK,
2783 MVPP2_PRS_RI_PPPOE_MASK);
2784 mvpp2_prs_hw_write(priv, &pe);
2785
2786 /* Ethertype: ARP */
2787 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2788 MVPP2_PE_LAST_FREE_TID);
2789 if (tid < 0)
2790 return tid;
2791
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002792 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002793 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2794 pe.index = tid;
2795
2796 mvpp2_prs_match_etype(&pe, 0, ETH_P_ARP);
2797
2798 /* Generate flow in the next iteration*/
2799 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2800 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2801 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_ARP,
2802 MVPP2_PRS_RI_L3_PROTO_MASK);
2803 /* Set L3 offset */
2804 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2805 MVPP2_ETH_TYPE_LEN,
2806 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2807
2808 /* Update shadow table and hw entry */
2809 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2810 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2811 priv->prs_shadow[pe.index].finish = true;
2812 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_ARP,
2813 MVPP2_PRS_RI_L3_PROTO_MASK);
2814 mvpp2_prs_hw_write(priv, &pe);
2815
2816 /* Ethertype: LBTD */
2817 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2818 MVPP2_PE_LAST_FREE_TID);
2819 if (tid < 0)
2820 return tid;
2821
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002822 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002823 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2824 pe.index = tid;
2825
2826 mvpp2_prs_match_etype(&pe, 0, MVPP2_IP_LBDT_TYPE);
2827
2828 /* Generate flow in the next iteration*/
2829 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2830 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2831 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2832 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2833 MVPP2_PRS_RI_CPU_CODE_MASK |
2834 MVPP2_PRS_RI_UDF3_MASK);
2835 /* Set L3 offset */
2836 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2837 MVPP2_ETH_TYPE_LEN,
2838 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2839
2840 /* Update shadow table and hw entry */
2841 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2842 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2843 priv->prs_shadow[pe.index].finish = true;
2844 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2845 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2846 MVPP2_PRS_RI_CPU_CODE_MASK |
2847 MVPP2_PRS_RI_UDF3_MASK);
2848 mvpp2_prs_hw_write(priv, &pe);
2849
2850 /* Ethertype: IPv4 without options */
2851 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2852 MVPP2_PE_LAST_FREE_TID);
2853 if (tid < 0)
2854 return tid;
2855
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002856 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002857 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2858 pe.index = tid;
2859
2860 mvpp2_prs_match_etype(&pe, 0, ETH_P_IP);
2861 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2862 MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
2863 MVPP2_PRS_IPV4_HEAD_MASK |
2864 MVPP2_PRS_IPV4_IHL_MASK);
2865
2866 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2867 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
2868 MVPP2_PRS_RI_L3_PROTO_MASK);
2869 /* Skip eth_type + 4 bytes of IP header */
2870 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
2871 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2872 /* Set L3 offset */
2873 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2874 MVPP2_ETH_TYPE_LEN,
2875 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2876
2877 /* Update shadow table and hw entry */
2878 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2879 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2880 priv->prs_shadow[pe.index].finish = false;
2881 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4,
2882 MVPP2_PRS_RI_L3_PROTO_MASK);
2883 mvpp2_prs_hw_write(priv, &pe);
2884
2885 /* Ethertype: IPv4 with options */
2886 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2887 MVPP2_PE_LAST_FREE_TID);
2888 if (tid < 0)
2889 return tid;
2890
2891 pe.index = tid;
2892
2893 /* Clear tcam data before updating */
2894 pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(MVPP2_ETH_TYPE_LEN)] = 0x0;
2895 pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(MVPP2_ETH_TYPE_LEN)] = 0x0;
2896
2897 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2898 MVPP2_PRS_IPV4_HEAD,
2899 MVPP2_PRS_IPV4_HEAD_MASK);
2900
2901 /* Clear ri before updating */
2902 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2903 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2904 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
2905 MVPP2_PRS_RI_L3_PROTO_MASK);
2906
2907 /* Update shadow table and hw entry */
2908 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2909 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2910 priv->prs_shadow[pe.index].finish = false;
2911 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4_OPT,
2912 MVPP2_PRS_RI_L3_PROTO_MASK);
2913 mvpp2_prs_hw_write(priv, &pe);
2914
2915 /* Ethertype: IPv6 without options */
2916 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2917 MVPP2_PE_LAST_FREE_TID);
2918 if (tid < 0)
2919 return tid;
2920
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002921 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002922 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2923 pe.index = tid;
2924
2925 mvpp2_prs_match_etype(&pe, 0, ETH_P_IPV6);
2926
2927 /* Skip DIP of IPV6 header */
2928 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 8 +
2929 MVPP2_MAX_L3_ADDR_SIZE,
2930 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2931 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2932 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
2933 MVPP2_PRS_RI_L3_PROTO_MASK);
2934 /* Set L3 offset */
2935 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2936 MVPP2_ETH_TYPE_LEN,
2937 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2938
2939 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2940 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2941 priv->prs_shadow[pe.index].finish = false;
2942 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP6,
2943 MVPP2_PRS_RI_L3_PROTO_MASK);
2944 mvpp2_prs_hw_write(priv, &pe);
2945
2946 /* Default entry for MVPP2_PRS_LU_L2 - Unknown ethtype */
2947 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2948 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2949 pe.index = MVPP2_PE_ETH_TYPE_UN;
2950
2951 /* Unmask all ports */
2952 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2953
2954 /* Generate flow in the next iteration*/
2955 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2956 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2957 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
2958 MVPP2_PRS_RI_L3_PROTO_MASK);
2959 /* Set L3 offset even it's unknown L3 */
2960 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2961 MVPP2_ETH_TYPE_LEN,
2962 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2963
2964 /* Update shadow table and hw entry */
2965 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2966 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2967 priv->prs_shadow[pe.index].finish = true;
2968 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_UN,
2969 MVPP2_PRS_RI_L3_PROTO_MASK);
2970 mvpp2_prs_hw_write(priv, &pe);
2971
2972 return 0;
2973}
2974
2975/* Configure vlan entries and detect up to 2 successive VLAN tags.
2976 * Possible options:
2977 * 0x8100, 0x88A8
2978 * 0x8100, 0x8100
2979 * 0x8100
2980 * 0x88A8
2981 */
2982static int mvpp2_prs_vlan_init(struct platform_device *pdev, struct mvpp2 *priv)
2983{
2984 struct mvpp2_prs_entry pe;
2985 int err;
2986
2987 priv->prs_double_vlans = devm_kcalloc(&pdev->dev, sizeof(bool),
2988 MVPP2_PRS_DBL_VLANS_MAX,
2989 GFP_KERNEL);
2990 if (!priv->prs_double_vlans)
2991 return -ENOMEM;
2992
2993 /* Double VLAN: 0x8100, 0x88A8 */
2994 err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021AD,
2995 MVPP2_PRS_PORT_MASK);
2996 if (err)
2997 return err;
2998
2999 /* Double VLAN: 0x8100, 0x8100 */
3000 err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021Q,
3001 MVPP2_PRS_PORT_MASK);
3002 if (err)
3003 return err;
3004
3005 /* Single VLAN: 0x88a8 */
3006 err = mvpp2_prs_vlan_add(priv, ETH_P_8021AD, MVPP2_PRS_SINGLE_VLAN_AI,
3007 MVPP2_PRS_PORT_MASK);
3008 if (err)
3009 return err;
3010
3011 /* Single VLAN: 0x8100 */
3012 err = mvpp2_prs_vlan_add(priv, ETH_P_8021Q, MVPP2_PRS_SINGLE_VLAN_AI,
3013 MVPP2_PRS_PORT_MASK);
3014 if (err)
3015 return err;
3016
3017 /* Set default double vlan entry */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003018 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003019 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
3020 pe.index = MVPP2_PE_VLAN_DBL;
3021
3022 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
3023 /* Clear ai for next iterations */
3024 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
3025 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_DOUBLE,
3026 MVPP2_PRS_RI_VLAN_MASK);
3027
3028 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_DBL_VLAN_AI_BIT,
3029 MVPP2_PRS_DBL_VLAN_AI_BIT);
3030 /* Unmask all ports */
3031 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3032
3033 /* Update shadow table and hw entry */
3034 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
3035 mvpp2_prs_hw_write(priv, &pe);
3036
3037 /* Set default vlan none entry */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003038 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003039 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
3040 pe.index = MVPP2_PE_VLAN_NONE;
3041
3042 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
3043 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
3044 MVPP2_PRS_RI_VLAN_MASK);
3045
3046 /* Unmask all ports */
3047 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3048
3049 /* Update shadow table and hw entry */
3050 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
3051 mvpp2_prs_hw_write(priv, &pe);
3052
3053 return 0;
3054}
3055
3056/* Set entries for PPPoE ethertype */
3057static int mvpp2_prs_pppoe_init(struct mvpp2 *priv)
3058{
3059 struct mvpp2_prs_entry pe;
3060 int tid;
3061
3062 /* IPv4 over PPPoE with options */
3063 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3064 MVPP2_PE_LAST_FREE_TID);
3065 if (tid < 0)
3066 return tid;
3067
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003068 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003069 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
3070 pe.index = tid;
3071
3072 mvpp2_prs_match_etype(&pe, 0, PPP_IP);
3073
3074 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
3075 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
3076 MVPP2_PRS_RI_L3_PROTO_MASK);
3077 /* Skip eth_type + 4 bytes of IP header */
3078 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
3079 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3080 /* Set L3 offset */
3081 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
3082 MVPP2_ETH_TYPE_LEN,
3083 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3084
3085 /* Update shadow table and hw entry */
3086 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3087 mvpp2_prs_hw_write(priv, &pe);
3088
3089 /* IPv4 over PPPoE without options */
3090 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3091 MVPP2_PE_LAST_FREE_TID);
3092 if (tid < 0)
3093 return tid;
3094
3095 pe.index = tid;
3096
3097 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
3098 MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
3099 MVPP2_PRS_IPV4_HEAD_MASK |
3100 MVPP2_PRS_IPV4_IHL_MASK);
3101
3102 /* Clear ri before updating */
3103 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
3104 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
3105 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
3106 MVPP2_PRS_RI_L3_PROTO_MASK);
3107
3108 /* Update shadow table and hw entry */
3109 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3110 mvpp2_prs_hw_write(priv, &pe);
3111
3112 /* IPv6 over PPPoE */
3113 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3114 MVPP2_PE_LAST_FREE_TID);
3115 if (tid < 0)
3116 return tid;
3117
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003118 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003119 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
3120 pe.index = tid;
3121
3122 mvpp2_prs_match_etype(&pe, 0, PPP_IPV6);
3123
3124 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
3125 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
3126 MVPP2_PRS_RI_L3_PROTO_MASK);
3127 /* Skip eth_type + 4 bytes of IPv6 header */
3128 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
3129 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3130 /* Set L3 offset */
3131 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
3132 MVPP2_ETH_TYPE_LEN,
3133 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3134
3135 /* Update shadow table and hw entry */
3136 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3137 mvpp2_prs_hw_write(priv, &pe);
3138
3139 /* Non-IP over PPPoE */
3140 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3141 MVPP2_PE_LAST_FREE_TID);
3142 if (tid < 0)
3143 return tid;
3144
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003145 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003146 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
3147 pe.index = tid;
3148
3149 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
3150 MVPP2_PRS_RI_L3_PROTO_MASK);
3151
3152 /* Finished: go to flowid generation */
3153 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3154 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3155 /* Set L3 offset even if it's unknown L3 */
3156 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
3157 MVPP2_ETH_TYPE_LEN,
3158 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3159
3160 /* Update shadow table and hw entry */
3161 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3162 mvpp2_prs_hw_write(priv, &pe);
3163
3164 return 0;
3165}
3166
3167/* Initialize entries for IPv4 */
3168static int mvpp2_prs_ip4_init(struct mvpp2 *priv)
3169{
3170 struct mvpp2_prs_entry pe;
3171 int err;
3172
3173 /* Set entries for TCP, UDP and IGMP over IPv4 */
3174 err = mvpp2_prs_ip4_proto(priv, IPPROTO_TCP, MVPP2_PRS_RI_L4_TCP,
3175 MVPP2_PRS_RI_L4_PROTO_MASK);
3176 if (err)
3177 return err;
3178
3179 err = mvpp2_prs_ip4_proto(priv, IPPROTO_UDP, MVPP2_PRS_RI_L4_UDP,
3180 MVPP2_PRS_RI_L4_PROTO_MASK);
3181 if (err)
3182 return err;
3183
3184 err = mvpp2_prs_ip4_proto(priv, IPPROTO_IGMP,
3185 MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
3186 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
3187 MVPP2_PRS_RI_CPU_CODE_MASK |
3188 MVPP2_PRS_RI_UDF3_MASK);
3189 if (err)
3190 return err;
3191
3192 /* IPv4 Broadcast */
3193 err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_BROAD_CAST);
3194 if (err)
3195 return err;
3196
3197 /* IPv4 Multicast */
3198 err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
3199 if (err)
3200 return err;
3201
3202 /* Default IPv4 entry for unknown protocols */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003203 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003204 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
3205 pe.index = MVPP2_PE_IP4_PROTO_UN;
3206
3207 /* Set next lu to IPv4 */
3208 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
3209 mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3210 /* Set L4 offset */
3211 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
3212 sizeof(struct iphdr) - 4,
3213 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3214 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
3215 MVPP2_PRS_IPV4_DIP_AI_BIT);
3216 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3217 MVPP2_PRS_RI_L4_PROTO_MASK);
3218
3219 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
3220 /* Unmask all ports */
3221 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3222
3223 /* Update shadow table and hw entry */
3224 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3225 mvpp2_prs_hw_write(priv, &pe);
3226
3227 /* Default IPv4 entry for unicast address */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003228 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003229 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
3230 pe.index = MVPP2_PE_IP4_ADDR_UN;
3231
3232 /* Finished: go to flowid generation */
3233 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3234 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3235 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
3236 MVPP2_PRS_RI_L3_ADDR_MASK);
3237
3238 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
3239 MVPP2_PRS_IPV4_DIP_AI_BIT);
3240 /* Unmask all ports */
3241 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3242
3243 /* Update shadow table and hw entry */
3244 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3245 mvpp2_prs_hw_write(priv, &pe);
3246
3247 return 0;
3248}
3249
3250/* Initialize entries for IPv6 */
3251static int mvpp2_prs_ip6_init(struct mvpp2 *priv)
3252{
3253 struct mvpp2_prs_entry pe;
3254 int tid, err;
3255
3256 /* Set entries for TCP, UDP and ICMP over IPv6 */
3257 err = mvpp2_prs_ip6_proto(priv, IPPROTO_TCP,
3258 MVPP2_PRS_RI_L4_TCP,
3259 MVPP2_PRS_RI_L4_PROTO_MASK);
3260 if (err)
3261 return err;
3262
3263 err = mvpp2_prs_ip6_proto(priv, IPPROTO_UDP,
3264 MVPP2_PRS_RI_L4_UDP,
3265 MVPP2_PRS_RI_L4_PROTO_MASK);
3266 if (err)
3267 return err;
3268
3269 err = mvpp2_prs_ip6_proto(priv, IPPROTO_ICMPV6,
3270 MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
3271 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
3272 MVPP2_PRS_RI_CPU_CODE_MASK |
3273 MVPP2_PRS_RI_UDF3_MASK);
3274 if (err)
3275 return err;
3276
3277 /* IPv4 is the last header. This is similar case as 6-TCP or 17-UDP */
3278 /* Result Info: UDF7=1, DS lite */
3279 err = mvpp2_prs_ip6_proto(priv, IPPROTO_IPIP,
3280 MVPP2_PRS_RI_UDF7_IP6_LITE,
3281 MVPP2_PRS_RI_UDF7_MASK);
3282 if (err)
3283 return err;
3284
3285 /* IPv6 multicast */
3286 err = mvpp2_prs_ip6_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
3287 if (err)
3288 return err;
3289
3290 /* Entry for checking hop limit */
3291 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3292 MVPP2_PE_LAST_FREE_TID);
3293 if (tid < 0)
3294 return tid;
3295
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003296 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003297 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3298 pe.index = tid;
3299
3300 /* Finished: go to flowid generation */
3301 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3302 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3303 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN |
3304 MVPP2_PRS_RI_DROP_MASK,
3305 MVPP2_PRS_RI_L3_PROTO_MASK |
3306 MVPP2_PRS_RI_DROP_MASK);
3307
3308 mvpp2_prs_tcam_data_byte_set(&pe, 1, 0x00, MVPP2_PRS_IPV6_HOP_MASK);
3309 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3310 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3311
3312 /* Update shadow table and hw entry */
3313 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3314 mvpp2_prs_hw_write(priv, &pe);
3315
3316 /* Default IPv6 entry for unknown protocols */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003317 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003318 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3319 pe.index = MVPP2_PE_IP6_PROTO_UN;
3320
3321 /* Finished: go to flowid generation */
3322 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3323 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3324 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3325 MVPP2_PRS_RI_L4_PROTO_MASK);
3326 /* Set L4 offset relatively to our current place */
3327 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
3328 sizeof(struct ipv6hdr) - 4,
3329 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3330
3331 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3332 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3333 /* Unmask all ports */
3334 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3335
3336 /* Update shadow table and hw entry */
3337 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3338 mvpp2_prs_hw_write(priv, &pe);
3339
3340 /* Default IPv6 entry for unknown ext protocols */
3341 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3342 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3343 pe.index = MVPP2_PE_IP6_EXT_PROTO_UN;
3344
3345 /* Finished: go to flowid generation */
3346 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3347 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3348 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3349 MVPP2_PRS_RI_L4_PROTO_MASK);
3350
3351 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_EXT_AI_BIT,
3352 MVPP2_PRS_IPV6_EXT_AI_BIT);
3353 /* Unmask all ports */
3354 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3355
3356 /* Update shadow table and hw entry */
3357 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3358 mvpp2_prs_hw_write(priv, &pe);
3359
3360 /* Default IPv6 entry for unicast address */
3361 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3362 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3363 pe.index = MVPP2_PE_IP6_ADDR_UN;
3364
3365 /* Finished: go to IPv6 again */
3366 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
3367 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
3368 MVPP2_PRS_RI_L3_ADDR_MASK);
3369 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3370 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3371 /* Shift back to IPV6 NH */
3372 mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3373
3374 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3375 /* Unmask all ports */
3376 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3377
3378 /* Update shadow table and hw entry */
3379 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
3380 mvpp2_prs_hw_write(priv, &pe);
3381
3382 return 0;
3383}
3384
3385/* Parser default initialization */
3386static int mvpp2_prs_default_init(struct platform_device *pdev,
3387 struct mvpp2 *priv)
3388{
3389 int err, index, i;
3390
3391 /* Enable tcam table */
3392 mvpp2_write(priv, MVPP2_PRS_TCAM_CTRL_REG, MVPP2_PRS_TCAM_EN_MASK);
3393
3394 /* Clear all tcam and sram entries */
3395 for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++) {
3396 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
3397 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
3398 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), 0);
3399
3400 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, index);
3401 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
3402 mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), 0);
3403 }
3404
3405 /* Invalidate all tcam entries */
3406 for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++)
3407 mvpp2_prs_hw_inv(priv, index);
3408
3409 priv->prs_shadow = devm_kcalloc(&pdev->dev, MVPP2_PRS_TCAM_SRAM_SIZE,
Markus Elfring37df25e2017-04-17 09:12:34 +02003410 sizeof(*priv->prs_shadow),
Marcin Wojtas3f518502014-07-10 16:52:13 -03003411 GFP_KERNEL);
3412 if (!priv->prs_shadow)
3413 return -ENOMEM;
3414
3415 /* Always start from lookup = 0 */
3416 for (index = 0; index < MVPP2_MAX_PORTS; index++)
3417 mvpp2_prs_hw_port_init(priv, index, MVPP2_PRS_LU_MH,
3418 MVPP2_PRS_PORT_LU_MAX, 0);
3419
3420 mvpp2_prs_def_flow_init(priv);
3421
3422 mvpp2_prs_mh_init(priv);
3423
3424 mvpp2_prs_mac_init(priv);
3425
3426 mvpp2_prs_dsa_init(priv);
3427
3428 err = mvpp2_prs_etype_init(priv);
3429 if (err)
3430 return err;
3431
3432 err = mvpp2_prs_vlan_init(pdev, priv);
3433 if (err)
3434 return err;
3435
3436 err = mvpp2_prs_pppoe_init(priv);
3437 if (err)
3438 return err;
3439
3440 err = mvpp2_prs_ip6_init(priv);
3441 if (err)
3442 return err;
3443
3444 err = mvpp2_prs_ip4_init(priv);
3445 if (err)
3446 return err;
3447
3448 return 0;
3449}
3450
3451/* Compare MAC DA with tcam entry data */
3452static bool mvpp2_prs_mac_range_equals(struct mvpp2_prs_entry *pe,
3453 const u8 *da, unsigned char *mask)
3454{
3455 unsigned char tcam_byte, tcam_mask;
3456 int index;
3457
3458 for (index = 0; index < ETH_ALEN; index++) {
3459 mvpp2_prs_tcam_data_byte_get(pe, index, &tcam_byte, &tcam_mask);
3460 if (tcam_mask != mask[index])
3461 return false;
3462
3463 if ((tcam_mask & tcam_byte) != (da[index] & mask[index]))
3464 return false;
3465 }
3466
3467 return true;
3468}
3469
3470/* Find tcam entry with matched pair <MAC DA, port> */
3471static struct mvpp2_prs_entry *
3472mvpp2_prs_mac_da_range_find(struct mvpp2 *priv, int pmap, const u8 *da,
3473 unsigned char *mask, int udf_type)
3474{
3475 struct mvpp2_prs_entry *pe;
3476 int tid;
3477
Antoine Tenart239dd4e2017-10-24 11:41:28 +02003478 pe = kzalloc(sizeof(*pe), GFP_ATOMIC);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003479 if (!pe)
3480 return NULL;
3481 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
3482
3483 /* Go through the all entires with MVPP2_PRS_LU_MAC */
3484 for (tid = MVPP2_PE_FIRST_FREE_TID;
3485 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
3486 unsigned int entry_pmap;
3487
3488 if (!priv->prs_shadow[tid].valid ||
3489 (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
3490 (priv->prs_shadow[tid].udf != udf_type))
3491 continue;
3492
3493 pe->index = tid;
3494 mvpp2_prs_hw_read(priv, pe);
3495 entry_pmap = mvpp2_prs_tcam_port_map_get(pe);
3496
3497 if (mvpp2_prs_mac_range_equals(pe, da, mask) &&
3498 entry_pmap == pmap)
3499 return pe;
3500 }
3501 kfree(pe);
3502
3503 return NULL;
3504}
3505
3506/* Update parser's mac da entry */
3507static int mvpp2_prs_mac_da_accept(struct mvpp2 *priv, int port,
3508 const u8 *da, bool add)
3509{
3510 struct mvpp2_prs_entry *pe;
3511 unsigned int pmap, len, ri;
3512 unsigned char mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3513 int tid;
3514
3515 /* Scan TCAM and see if entry with this <MAC DA, port> already exist */
3516 pe = mvpp2_prs_mac_da_range_find(priv, (1 << port), da, mask,
3517 MVPP2_PRS_UDF_MAC_DEF);
3518
3519 /* No such entry */
3520 if (!pe) {
3521 if (!add)
3522 return 0;
3523
3524 /* Create new TCAM entry */
3525 /* Find first range mac entry*/
3526 for (tid = MVPP2_PE_FIRST_FREE_TID;
3527 tid <= MVPP2_PE_LAST_FREE_TID; tid++)
3528 if (priv->prs_shadow[tid].valid &&
3529 (priv->prs_shadow[tid].lu == MVPP2_PRS_LU_MAC) &&
3530 (priv->prs_shadow[tid].udf ==
3531 MVPP2_PRS_UDF_MAC_RANGE))
3532 break;
3533
3534 /* Go through the all entries from first to last */
3535 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3536 tid - 1);
3537 if (tid < 0)
3538 return tid;
3539
Antoine Tenart239dd4e2017-10-24 11:41:28 +02003540 pe = kzalloc(sizeof(*pe), GFP_ATOMIC);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003541 if (!pe)
Amitoj Kaur Chawlac2bb7bc2016-02-04 19:25:26 +05303542 return -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003543 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
3544 pe->index = tid;
3545
3546 /* Mask all ports */
3547 mvpp2_prs_tcam_port_map_set(pe, 0);
3548 }
3549
3550 /* Update port mask */
3551 mvpp2_prs_tcam_port_set(pe, port, add);
3552
3553 /* Invalidate the entry if no ports are left enabled */
3554 pmap = mvpp2_prs_tcam_port_map_get(pe);
3555 if (pmap == 0) {
3556 if (add) {
3557 kfree(pe);
Amitoj Kaur Chawlac2bb7bc2016-02-04 19:25:26 +05303558 return -EINVAL;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003559 }
3560 mvpp2_prs_hw_inv(priv, pe->index);
3561 priv->prs_shadow[pe->index].valid = false;
3562 kfree(pe);
3563 return 0;
3564 }
3565
3566 /* Continue - set next lookup */
3567 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_DSA);
3568
3569 /* Set match on DA */
3570 len = ETH_ALEN;
3571 while (len--)
3572 mvpp2_prs_tcam_data_byte_set(pe, len, da[len], 0xff);
3573
3574 /* Set result info bits */
3575 if (is_broadcast_ether_addr(da))
3576 ri = MVPP2_PRS_RI_L2_BCAST;
3577 else if (is_multicast_ether_addr(da))
3578 ri = MVPP2_PRS_RI_L2_MCAST;
3579 else
3580 ri = MVPP2_PRS_RI_L2_UCAST | MVPP2_PRS_RI_MAC_ME_MASK;
3581
3582 mvpp2_prs_sram_ri_update(pe, ri, MVPP2_PRS_RI_L2_CAST_MASK |
3583 MVPP2_PRS_RI_MAC_ME_MASK);
3584 mvpp2_prs_shadow_ri_set(priv, pe->index, ri, MVPP2_PRS_RI_L2_CAST_MASK |
3585 MVPP2_PRS_RI_MAC_ME_MASK);
3586
3587 /* Shift to ethertype */
3588 mvpp2_prs_sram_shift_set(pe, 2 * ETH_ALEN,
3589 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3590
3591 /* Update shadow table and hw entry */
3592 priv->prs_shadow[pe->index].udf = MVPP2_PRS_UDF_MAC_DEF;
3593 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_MAC);
3594 mvpp2_prs_hw_write(priv, pe);
3595
3596 kfree(pe);
3597
3598 return 0;
3599}
3600
3601static int mvpp2_prs_update_mac_da(struct net_device *dev, const u8 *da)
3602{
3603 struct mvpp2_port *port = netdev_priv(dev);
3604 int err;
3605
3606 /* Remove old parser entry */
3607 err = mvpp2_prs_mac_da_accept(port->priv, port->id, dev->dev_addr,
3608 false);
3609 if (err)
3610 return err;
3611
3612 /* Add new parser entry */
3613 err = mvpp2_prs_mac_da_accept(port->priv, port->id, da, true);
3614 if (err)
3615 return err;
3616
3617 /* Set addr in the device */
3618 ether_addr_copy(dev->dev_addr, da);
3619
3620 return 0;
3621}
3622
3623/* Delete all port's multicast simple (not range) entries */
3624static void mvpp2_prs_mcast_del_all(struct mvpp2 *priv, int port)
3625{
3626 struct mvpp2_prs_entry pe;
3627 int index, tid;
3628
3629 for (tid = MVPP2_PE_FIRST_FREE_TID;
3630 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
3631 unsigned char da[ETH_ALEN], da_mask[ETH_ALEN];
3632
3633 if (!priv->prs_shadow[tid].valid ||
3634 (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
3635 (priv->prs_shadow[tid].udf != MVPP2_PRS_UDF_MAC_DEF))
3636 continue;
3637
3638 /* Only simple mac entries */
3639 pe.index = tid;
3640 mvpp2_prs_hw_read(priv, &pe);
3641
3642 /* Read mac addr from entry */
3643 for (index = 0; index < ETH_ALEN; index++)
3644 mvpp2_prs_tcam_data_byte_get(&pe, index, &da[index],
3645 &da_mask[index]);
3646
3647 if (is_multicast_ether_addr(da) && !is_broadcast_ether_addr(da))
3648 /* Delete this entry */
3649 mvpp2_prs_mac_da_accept(priv, port, da, false);
3650 }
3651}
3652
3653static int mvpp2_prs_tag_mode_set(struct mvpp2 *priv, int port, int type)
3654{
3655 switch (type) {
3656 case MVPP2_TAG_TYPE_EDSA:
3657 /* Add port to EDSA entries */
3658 mvpp2_prs_dsa_tag_set(priv, port, true,
3659 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3660 mvpp2_prs_dsa_tag_set(priv, port, true,
3661 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3662 /* Remove port from DSA entries */
3663 mvpp2_prs_dsa_tag_set(priv, port, false,
3664 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3665 mvpp2_prs_dsa_tag_set(priv, port, false,
3666 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3667 break;
3668
3669 case MVPP2_TAG_TYPE_DSA:
3670 /* Add port to DSA entries */
3671 mvpp2_prs_dsa_tag_set(priv, port, true,
3672 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3673 mvpp2_prs_dsa_tag_set(priv, port, true,
3674 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3675 /* Remove port from EDSA entries */
3676 mvpp2_prs_dsa_tag_set(priv, port, false,
3677 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3678 mvpp2_prs_dsa_tag_set(priv, port, false,
3679 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3680 break;
3681
3682 case MVPP2_TAG_TYPE_MH:
3683 case MVPP2_TAG_TYPE_NONE:
3684 /* Remove port form EDSA and DSA entries */
3685 mvpp2_prs_dsa_tag_set(priv, port, false,
3686 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3687 mvpp2_prs_dsa_tag_set(priv, port, false,
3688 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3689 mvpp2_prs_dsa_tag_set(priv, port, false,
3690 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3691 mvpp2_prs_dsa_tag_set(priv, port, false,
3692 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3693 break;
3694
3695 default:
3696 if ((type < 0) || (type > MVPP2_TAG_TYPE_EDSA))
3697 return -EINVAL;
3698 }
3699
3700 return 0;
3701}
3702
3703/* Set prs flow for the port */
3704static int mvpp2_prs_def_flow(struct mvpp2_port *port)
3705{
3706 struct mvpp2_prs_entry *pe;
3707 int tid;
3708
3709 pe = mvpp2_prs_flow_find(port->priv, port->id);
3710
3711 /* Such entry not exist */
3712 if (!pe) {
3713 /* Go through the all entires from last to first */
3714 tid = mvpp2_prs_tcam_first_free(port->priv,
3715 MVPP2_PE_LAST_FREE_TID,
3716 MVPP2_PE_FIRST_FREE_TID);
3717 if (tid < 0)
3718 return tid;
3719
3720 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
3721 if (!pe)
3722 return -ENOMEM;
3723
3724 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
3725 pe->index = tid;
3726
3727 /* Set flow ID*/
3728 mvpp2_prs_sram_ai_update(pe, port->id, MVPP2_PRS_FLOW_ID_MASK);
3729 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
3730
3731 /* Update shadow table */
3732 mvpp2_prs_shadow_set(port->priv, pe->index, MVPP2_PRS_LU_FLOWS);
3733 }
3734
3735 mvpp2_prs_tcam_port_map_set(pe, (1 << port->id));
3736 mvpp2_prs_hw_write(port->priv, pe);
3737 kfree(pe);
3738
3739 return 0;
3740}
3741
3742/* Classifier configuration routines */
3743
3744/* Update classification flow table registers */
3745static void mvpp2_cls_flow_write(struct mvpp2 *priv,
3746 struct mvpp2_cls_flow_entry *fe)
3747{
3748 mvpp2_write(priv, MVPP2_CLS_FLOW_INDEX_REG, fe->index);
3749 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL0_REG, fe->data[0]);
3750 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL1_REG, fe->data[1]);
3751 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL2_REG, fe->data[2]);
3752}
3753
3754/* Update classification lookup table register */
3755static void mvpp2_cls_lookup_write(struct mvpp2 *priv,
3756 struct mvpp2_cls_lookup_entry *le)
3757{
3758 u32 val;
3759
3760 val = (le->way << MVPP2_CLS_LKP_INDEX_WAY_OFFS) | le->lkpid;
3761 mvpp2_write(priv, MVPP2_CLS_LKP_INDEX_REG, val);
3762 mvpp2_write(priv, MVPP2_CLS_LKP_TBL_REG, le->data);
3763}
3764
3765/* Classifier default initialization */
3766static void mvpp2_cls_init(struct mvpp2 *priv)
3767{
3768 struct mvpp2_cls_lookup_entry le;
3769 struct mvpp2_cls_flow_entry fe;
3770 int index;
3771
3772 /* Enable classifier */
3773 mvpp2_write(priv, MVPP2_CLS_MODE_REG, MVPP2_CLS_MODE_ACTIVE_MASK);
3774
3775 /* Clear classifier flow table */
Arnd Bergmanne8f967c2016-11-24 17:28:12 +01003776 memset(&fe.data, 0, sizeof(fe.data));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003777 for (index = 0; index < MVPP2_CLS_FLOWS_TBL_SIZE; index++) {
3778 fe.index = index;
3779 mvpp2_cls_flow_write(priv, &fe);
3780 }
3781
3782 /* Clear classifier lookup table */
3783 le.data = 0;
3784 for (index = 0; index < MVPP2_CLS_LKP_TBL_SIZE; index++) {
3785 le.lkpid = index;
3786 le.way = 0;
3787 mvpp2_cls_lookup_write(priv, &le);
3788
3789 le.way = 1;
3790 mvpp2_cls_lookup_write(priv, &le);
3791 }
3792}
3793
3794static void mvpp2_cls_port_config(struct mvpp2_port *port)
3795{
3796 struct mvpp2_cls_lookup_entry le;
3797 u32 val;
3798
3799 /* Set way for the port */
3800 val = mvpp2_read(port->priv, MVPP2_CLS_PORT_WAY_REG);
3801 val &= ~MVPP2_CLS_PORT_WAY_MASK(port->id);
3802 mvpp2_write(port->priv, MVPP2_CLS_PORT_WAY_REG, val);
3803
3804 /* Pick the entry to be accessed in lookup ID decoding table
3805 * according to the way and lkpid.
3806 */
3807 le.lkpid = port->id;
3808 le.way = 0;
3809 le.data = 0;
3810
3811 /* Set initial CPU queue for receiving packets */
3812 le.data &= ~MVPP2_CLS_LKP_TBL_RXQ_MASK;
3813 le.data |= port->first_rxq;
3814
3815 /* Disable classification engines */
3816 le.data &= ~MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK;
3817
3818 /* Update lookup ID table entry */
3819 mvpp2_cls_lookup_write(port->priv, &le);
3820}
3821
3822/* Set CPU queue number for oversize packets */
3823static void mvpp2_cls_oversize_rxq_set(struct mvpp2_port *port)
3824{
3825 u32 val;
3826
3827 mvpp2_write(port->priv, MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port->id),
3828 port->first_rxq & MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK);
3829
3830 mvpp2_write(port->priv, MVPP2_CLS_SWFWD_P2HQ_REG(port->id),
3831 (port->first_rxq >> MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS));
3832
3833 val = mvpp2_read(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG);
3834 val |= MVPP2_CLS_SWFWD_PCTRL_MASK(port->id);
3835 mvpp2_write(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG, val);
3836}
3837
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003838static void *mvpp2_frag_alloc(const struct mvpp2_bm_pool *pool)
3839{
3840 if (likely(pool->frag_size <= PAGE_SIZE))
3841 return netdev_alloc_frag(pool->frag_size);
3842 else
3843 return kmalloc(pool->frag_size, GFP_ATOMIC);
3844}
3845
3846static void mvpp2_frag_free(const struct mvpp2_bm_pool *pool, void *data)
3847{
3848 if (likely(pool->frag_size <= PAGE_SIZE))
3849 skb_free_frag(data);
3850 else
3851 kfree(data);
3852}
3853
Marcin Wojtas3f518502014-07-10 16:52:13 -03003854/* Buffer Manager configuration routines */
3855
3856/* Create pool */
3857static int mvpp2_bm_pool_create(struct platform_device *pdev,
3858 struct mvpp2 *priv,
3859 struct mvpp2_bm_pool *bm_pool, int size)
3860{
Marcin Wojtas3f518502014-07-10 16:52:13 -03003861 u32 val;
3862
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003863 /* Number of buffer pointers must be a multiple of 16, as per
3864 * hardware constraints
3865 */
3866 if (!IS_ALIGNED(size, 16))
3867 return -EINVAL;
3868
3869 /* PPv2.1 needs 8 bytes per buffer pointer, PPv2.2 needs 16
3870 * bytes per buffer pointer
3871 */
3872 if (priv->hw_version == MVPP21)
3873 bm_pool->size_bytes = 2 * sizeof(u32) * size;
3874 else
3875 bm_pool->size_bytes = 2 * sizeof(u64) * size;
3876
3877 bm_pool->virt_addr = dma_alloc_coherent(&pdev->dev, bm_pool->size_bytes,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003878 &bm_pool->dma_addr,
Marcin Wojtas3f518502014-07-10 16:52:13 -03003879 GFP_KERNEL);
3880 if (!bm_pool->virt_addr)
3881 return -ENOMEM;
3882
Thomas Petazzonid3158802017-02-21 11:28:13 +01003883 if (!IS_ALIGNED((unsigned long)bm_pool->virt_addr,
3884 MVPP2_BM_POOL_PTR_ALIGN)) {
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003885 dma_free_coherent(&pdev->dev, bm_pool->size_bytes,
3886 bm_pool->virt_addr, bm_pool->dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003887 dev_err(&pdev->dev, "BM pool %d is not %d bytes aligned\n",
3888 bm_pool->id, MVPP2_BM_POOL_PTR_ALIGN);
3889 return -ENOMEM;
3890 }
3891
3892 mvpp2_write(priv, MVPP2_BM_POOL_BASE_REG(bm_pool->id),
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003893 lower_32_bits(bm_pool->dma_addr));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003894 mvpp2_write(priv, MVPP2_BM_POOL_SIZE_REG(bm_pool->id), size);
3895
3896 val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
3897 val |= MVPP2_BM_START_MASK;
3898 mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
3899
3900 bm_pool->type = MVPP2_BM_FREE;
3901 bm_pool->size = size;
3902 bm_pool->pkt_size = 0;
3903 bm_pool->buf_num = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003904
3905 return 0;
3906}
3907
3908/* Set pool buffer size */
3909static void mvpp2_bm_pool_bufsize_set(struct mvpp2 *priv,
3910 struct mvpp2_bm_pool *bm_pool,
3911 int buf_size)
3912{
3913 u32 val;
3914
3915 bm_pool->buf_size = buf_size;
3916
3917 val = ALIGN(buf_size, 1 << MVPP2_POOL_BUF_SIZE_OFFSET);
3918 mvpp2_write(priv, MVPP2_POOL_BUF_SIZE_REG(bm_pool->id), val);
3919}
3920
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003921static void mvpp2_bm_bufs_get_addrs(struct device *dev, struct mvpp2 *priv,
3922 struct mvpp2_bm_pool *bm_pool,
3923 dma_addr_t *dma_addr,
3924 phys_addr_t *phys_addr)
3925{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02003926 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01003927
3928 *dma_addr = mvpp2_percpu_read(priv, cpu,
3929 MVPP2_BM_PHY_ALLOC_REG(bm_pool->id));
3930 *phys_addr = mvpp2_percpu_read(priv, cpu, MVPP2_BM_VIRT_ALLOC_REG);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003931
3932 if (priv->hw_version == MVPP22) {
3933 u32 val;
3934 u32 dma_addr_highbits, phys_addr_highbits;
3935
Thomas Petazzonia7868412017-03-07 16:53:13 +01003936 val = mvpp2_percpu_read(priv, cpu, MVPP22_BM_ADDR_HIGH_ALLOC);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003937 dma_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_PHYS_MASK);
3938 phys_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_VIRT_MASK) >>
3939 MVPP22_BM_ADDR_HIGH_VIRT_SHIFT;
3940
3941 if (sizeof(dma_addr_t) == 8)
3942 *dma_addr |= (u64)dma_addr_highbits << 32;
3943
3944 if (sizeof(phys_addr_t) == 8)
3945 *phys_addr |= (u64)phys_addr_highbits << 32;
3946 }
Thomas Petazzonia704bb52017-06-10 23:18:22 +02003947
3948 put_cpu();
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003949}
3950
Ezequiel Garcia7861f122014-07-21 13:48:14 -03003951/* Free all buffers from the pool */
Marcin Wojtas4229d502015-12-03 15:20:50 +01003952static void mvpp2_bm_bufs_free(struct device *dev, struct mvpp2 *priv,
3953 struct mvpp2_bm_pool *bm_pool)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003954{
3955 int i;
3956
Ezequiel Garcia7861f122014-07-21 13:48:14 -03003957 for (i = 0; i < bm_pool->buf_num; i++) {
Thomas Petazzoni20396132017-03-07 16:53:00 +01003958 dma_addr_t buf_dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003959 phys_addr_t buf_phys_addr;
3960 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003961
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003962 mvpp2_bm_bufs_get_addrs(dev, priv, bm_pool,
3963 &buf_dma_addr, &buf_phys_addr);
Marcin Wojtas4229d502015-12-03 15:20:50 +01003964
Thomas Petazzoni20396132017-03-07 16:53:00 +01003965 dma_unmap_single(dev, buf_dma_addr,
Marcin Wojtas4229d502015-12-03 15:20:50 +01003966 bm_pool->buf_size, DMA_FROM_DEVICE);
3967
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003968 data = (void *)phys_to_virt(buf_phys_addr);
3969 if (!data)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003970 break;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003971
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003972 mvpp2_frag_free(bm_pool, data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003973 }
3974
3975 /* Update BM driver with number of buffers removed from pool */
3976 bm_pool->buf_num -= i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003977}
3978
3979/* Cleanup pool */
3980static int mvpp2_bm_pool_destroy(struct platform_device *pdev,
3981 struct mvpp2 *priv,
3982 struct mvpp2_bm_pool *bm_pool)
3983{
Marcin Wojtas3f518502014-07-10 16:52:13 -03003984 u32 val;
3985
Marcin Wojtas4229d502015-12-03 15:20:50 +01003986 mvpp2_bm_bufs_free(&pdev->dev, priv, bm_pool);
Ezequiel Garciad74c96c2014-07-21 13:48:13 -03003987 if (bm_pool->buf_num) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03003988 WARN(1, "cannot free all buffers in pool %d\n", bm_pool->id);
3989 return 0;
3990 }
3991
3992 val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
3993 val |= MVPP2_BM_STOP_MASK;
3994 mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
3995
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003996 dma_free_coherent(&pdev->dev, bm_pool->size_bytes,
Marcin Wojtas3f518502014-07-10 16:52:13 -03003997 bm_pool->virt_addr,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003998 bm_pool->dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003999 return 0;
4000}
4001
4002static int mvpp2_bm_pools_init(struct platform_device *pdev,
4003 struct mvpp2 *priv)
4004{
4005 int i, err, size;
4006 struct mvpp2_bm_pool *bm_pool;
4007
4008 /* Create all pools with maximum size */
4009 size = MVPP2_BM_POOL_SIZE_MAX;
4010 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
4011 bm_pool = &priv->bm_pools[i];
4012 bm_pool->id = i;
4013 err = mvpp2_bm_pool_create(pdev, priv, bm_pool, size);
4014 if (err)
4015 goto err_unroll_pools;
4016 mvpp2_bm_pool_bufsize_set(priv, bm_pool, 0);
4017 }
4018 return 0;
4019
4020err_unroll_pools:
4021 dev_err(&pdev->dev, "failed to create BM pool %d, size %d\n", i, size);
4022 for (i = i - 1; i >= 0; i--)
4023 mvpp2_bm_pool_destroy(pdev, priv, &priv->bm_pools[i]);
4024 return err;
4025}
4026
4027static int mvpp2_bm_init(struct platform_device *pdev, struct mvpp2 *priv)
4028{
4029 int i, err;
4030
4031 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
4032 /* Mask BM all interrupts */
4033 mvpp2_write(priv, MVPP2_BM_INTR_MASK_REG(i), 0);
4034 /* Clear BM cause register */
4035 mvpp2_write(priv, MVPP2_BM_INTR_CAUSE_REG(i), 0);
4036 }
4037
4038 /* Allocate and initialize BM pools */
4039 priv->bm_pools = devm_kcalloc(&pdev->dev, MVPP2_BM_POOLS_NUM,
Markus Elfring81f915e2017-04-17 09:06:33 +02004040 sizeof(*priv->bm_pools), GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004041 if (!priv->bm_pools)
4042 return -ENOMEM;
4043
4044 err = mvpp2_bm_pools_init(pdev, priv);
4045 if (err < 0)
4046 return err;
4047 return 0;
4048}
4049
4050/* Attach long pool to rxq */
4051static void mvpp2_rxq_long_pool_set(struct mvpp2_port *port,
4052 int lrxq, int long_pool)
4053{
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004054 u32 val, mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004055 int prxq;
4056
4057 /* Get queue physical ID */
4058 prxq = port->rxqs[lrxq]->id;
4059
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004060 if (port->priv->hw_version == MVPP21)
4061 mask = MVPP21_RXQ_POOL_LONG_MASK;
4062 else
4063 mask = MVPP22_RXQ_POOL_LONG_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004064
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004065 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
4066 val &= ~mask;
4067 val |= (long_pool << MVPP2_RXQ_POOL_LONG_OFFS) & mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004068 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
4069}
4070
4071/* Attach short pool to rxq */
4072static void mvpp2_rxq_short_pool_set(struct mvpp2_port *port,
4073 int lrxq, int short_pool)
4074{
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004075 u32 val, mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004076 int prxq;
4077
4078 /* Get queue physical ID */
4079 prxq = port->rxqs[lrxq]->id;
4080
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004081 if (port->priv->hw_version == MVPP21)
4082 mask = MVPP21_RXQ_POOL_SHORT_MASK;
4083 else
4084 mask = MVPP22_RXQ_POOL_SHORT_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004085
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004086 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
4087 val &= ~mask;
4088 val |= (short_pool << MVPP2_RXQ_POOL_SHORT_OFFS) & mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004089 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
4090}
4091
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004092static void *mvpp2_buf_alloc(struct mvpp2_port *port,
4093 struct mvpp2_bm_pool *bm_pool,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004094 dma_addr_t *buf_dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004095 phys_addr_t *buf_phys_addr,
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004096 gfp_t gfp_mask)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004097{
Thomas Petazzoni20396132017-03-07 16:53:00 +01004098 dma_addr_t dma_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004099 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004100
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004101 data = mvpp2_frag_alloc(bm_pool);
4102 if (!data)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004103 return NULL;
4104
Thomas Petazzoni20396132017-03-07 16:53:00 +01004105 dma_addr = dma_map_single(port->dev->dev.parent, data,
4106 MVPP2_RX_BUF_SIZE(bm_pool->pkt_size),
4107 DMA_FROM_DEVICE);
4108 if (unlikely(dma_mapping_error(port->dev->dev.parent, dma_addr))) {
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004109 mvpp2_frag_free(bm_pool, data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004110 return NULL;
4111 }
Thomas Petazzoni20396132017-03-07 16:53:00 +01004112 *buf_dma_addr = dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004113 *buf_phys_addr = virt_to_phys(data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004114
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004115 return data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004116}
4117
Marcin Wojtas3f518502014-07-10 16:52:13 -03004118/* Release buffer to BM */
4119static inline void mvpp2_bm_pool_put(struct mvpp2_port *port, int pool,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004120 dma_addr_t buf_dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004121 phys_addr_t buf_phys_addr)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004122{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004123 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01004124
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004125 if (port->priv->hw_version == MVPP22) {
4126 u32 val = 0;
4127
4128 if (sizeof(dma_addr_t) == 8)
4129 val |= upper_32_bits(buf_dma_addr) &
4130 MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK;
4131
4132 if (sizeof(phys_addr_t) == 8)
4133 val |= (upper_32_bits(buf_phys_addr)
4134 << MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT) &
4135 MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK;
4136
Thomas Petazzonia7868412017-03-07 16:53:13 +01004137 mvpp2_percpu_write(port->priv, cpu,
4138 MVPP22_BM_ADDR_HIGH_RLS_REG, val);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004139 }
4140
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004141 /* MVPP2_BM_VIRT_RLS_REG is not interpreted by HW, and simply
4142 * returned in the "cookie" field of the RX
4143 * descriptor. Instead of storing the virtual address, we
4144 * store the physical address
4145 */
Thomas Petazzonia7868412017-03-07 16:53:13 +01004146 mvpp2_percpu_write(port->priv, cpu,
4147 MVPP2_BM_VIRT_RLS_REG, buf_phys_addr);
4148 mvpp2_percpu_write(port->priv, cpu,
4149 MVPP2_BM_PHY_RLS_REG(pool), buf_dma_addr);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004150
4151 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03004152}
4153
Marcin Wojtas3f518502014-07-10 16:52:13 -03004154/* Allocate buffers for the pool */
4155static int mvpp2_bm_bufs_add(struct mvpp2_port *port,
4156 struct mvpp2_bm_pool *bm_pool, int buf_num)
4157{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004158 int i, buf_size, total_size;
Thomas Petazzoni20396132017-03-07 16:53:00 +01004159 dma_addr_t dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004160 phys_addr_t phys_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004161 void *buf;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004162
4163 buf_size = MVPP2_RX_BUF_SIZE(bm_pool->pkt_size);
4164 total_size = MVPP2_RX_TOTAL_SIZE(buf_size);
4165
4166 if (buf_num < 0 ||
4167 (buf_num + bm_pool->buf_num > bm_pool->size)) {
4168 netdev_err(port->dev,
4169 "cannot allocate %d buffers for pool %d\n",
4170 buf_num, bm_pool->id);
4171 return 0;
4172 }
4173
Marcin Wojtas3f518502014-07-10 16:52:13 -03004174 for (i = 0; i < buf_num; i++) {
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004175 buf = mvpp2_buf_alloc(port, bm_pool, &dma_addr,
4176 &phys_addr, GFP_KERNEL);
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004177 if (!buf)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004178 break;
4179
Thomas Petazzoni20396132017-03-07 16:53:00 +01004180 mvpp2_bm_pool_put(port, bm_pool->id, dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004181 phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004182 }
4183
4184 /* Update BM driver with number of buffers added to pool */
4185 bm_pool->buf_num += i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004186
4187 netdev_dbg(port->dev,
4188 "%s pool %d: pkt_size=%4d, buf_size=%4d, total_size=%4d\n",
4189 bm_pool->type == MVPP2_BM_SWF_SHORT ? "short" : " long",
4190 bm_pool->id, bm_pool->pkt_size, buf_size, total_size);
4191
4192 netdev_dbg(port->dev,
4193 "%s pool %d: %d of %d buffers added\n",
4194 bm_pool->type == MVPP2_BM_SWF_SHORT ? "short" : " long",
4195 bm_pool->id, i, buf_num);
4196 return i;
4197}
4198
4199/* Notify the driver that BM pool is being used as specific type and return the
4200 * pool pointer on success
4201 */
4202static struct mvpp2_bm_pool *
4203mvpp2_bm_pool_use(struct mvpp2_port *port, int pool, enum mvpp2_bm_type type,
4204 int pkt_size)
4205{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004206 struct mvpp2_bm_pool *new_pool = &port->priv->bm_pools[pool];
4207 int num;
4208
4209 if (new_pool->type != MVPP2_BM_FREE && new_pool->type != type) {
4210 netdev_err(port->dev, "mixing pool types is forbidden\n");
4211 return NULL;
4212 }
4213
Marcin Wojtas3f518502014-07-10 16:52:13 -03004214 if (new_pool->type == MVPP2_BM_FREE)
4215 new_pool->type = type;
4216
4217 /* Allocate buffers in case BM pool is used as long pool, but packet
4218 * size doesn't match MTU or BM pool hasn't being used yet
4219 */
4220 if (((type == MVPP2_BM_SWF_LONG) && (pkt_size > new_pool->pkt_size)) ||
4221 (new_pool->pkt_size == 0)) {
4222 int pkts_num;
4223
4224 /* Set default buffer number or free all the buffers in case
4225 * the pool is not empty
4226 */
4227 pkts_num = new_pool->buf_num;
4228 if (pkts_num == 0)
4229 pkts_num = type == MVPP2_BM_SWF_LONG ?
4230 MVPP2_BM_LONG_BUF_NUM :
4231 MVPP2_BM_SHORT_BUF_NUM;
4232 else
Marcin Wojtas4229d502015-12-03 15:20:50 +01004233 mvpp2_bm_bufs_free(port->dev->dev.parent,
4234 port->priv, new_pool);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004235
4236 new_pool->pkt_size = pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004237 new_pool->frag_size =
4238 SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
4239 MVPP2_SKB_SHINFO_SIZE;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004240
4241 /* Allocate buffers for this pool */
4242 num = mvpp2_bm_bufs_add(port, new_pool, pkts_num);
4243 if (num != pkts_num) {
4244 WARN(1, "pool %d: %d of %d allocated\n",
4245 new_pool->id, num, pkts_num);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004246 return NULL;
4247 }
4248 }
4249
4250 mvpp2_bm_pool_bufsize_set(port->priv, new_pool,
4251 MVPP2_RX_BUF_SIZE(new_pool->pkt_size));
4252
Marcin Wojtas3f518502014-07-10 16:52:13 -03004253 return new_pool;
4254}
4255
4256/* Initialize pools for swf */
4257static int mvpp2_swf_bm_pool_init(struct mvpp2_port *port)
4258{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004259 int rxq;
4260
4261 if (!port->pool_long) {
4262 port->pool_long =
4263 mvpp2_bm_pool_use(port, MVPP2_BM_SWF_LONG_POOL(port->id),
4264 MVPP2_BM_SWF_LONG,
4265 port->pkt_size);
4266 if (!port->pool_long)
4267 return -ENOMEM;
4268
Marcin Wojtas3f518502014-07-10 16:52:13 -03004269 port->pool_long->port_map |= (1 << port->id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004270
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004271 for (rxq = 0; rxq < port->nrxqs; rxq++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004272 mvpp2_rxq_long_pool_set(port, rxq, port->pool_long->id);
4273 }
4274
4275 if (!port->pool_short) {
4276 port->pool_short =
4277 mvpp2_bm_pool_use(port, MVPP2_BM_SWF_SHORT_POOL,
4278 MVPP2_BM_SWF_SHORT,
4279 MVPP2_BM_SHORT_PKT_SIZE);
4280 if (!port->pool_short)
4281 return -ENOMEM;
4282
Marcin Wojtas3f518502014-07-10 16:52:13 -03004283 port->pool_short->port_map |= (1 << port->id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004284
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004285 for (rxq = 0; rxq < port->nrxqs; rxq++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004286 mvpp2_rxq_short_pool_set(port, rxq,
4287 port->pool_short->id);
4288 }
4289
4290 return 0;
4291}
4292
4293static int mvpp2_bm_update_mtu(struct net_device *dev, int mtu)
4294{
4295 struct mvpp2_port *port = netdev_priv(dev);
4296 struct mvpp2_bm_pool *port_pool = port->pool_long;
4297 int num, pkts_num = port_pool->buf_num;
4298 int pkt_size = MVPP2_RX_PKT_SIZE(mtu);
4299
4300 /* Update BM pool with new buffer size */
Marcin Wojtas4229d502015-12-03 15:20:50 +01004301 mvpp2_bm_bufs_free(dev->dev.parent, port->priv, port_pool);
Ezequiel Garciad74c96c2014-07-21 13:48:13 -03004302 if (port_pool->buf_num) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004303 WARN(1, "cannot free all buffers in pool %d\n", port_pool->id);
4304 return -EIO;
4305 }
4306
4307 port_pool->pkt_size = pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004308 port_pool->frag_size = SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
4309 MVPP2_SKB_SHINFO_SIZE;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004310 num = mvpp2_bm_bufs_add(port, port_pool, pkts_num);
4311 if (num != pkts_num) {
4312 WARN(1, "pool %d: %d of %d allocated\n",
4313 port_pool->id, num, pkts_num);
4314 return -EIO;
4315 }
4316
4317 mvpp2_bm_pool_bufsize_set(port->priv, port_pool,
4318 MVPP2_RX_BUF_SIZE(port_pool->pkt_size));
4319 dev->mtu = mtu;
4320 netdev_update_features(dev);
4321 return 0;
4322}
4323
4324static inline void mvpp2_interrupts_enable(struct mvpp2_port *port)
4325{
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004326 int i, sw_thread_mask = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004327
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004328 for (i = 0; i < port->nqvecs; i++)
4329 sw_thread_mask |= port->qvecs[i].sw_thread_mask;
4330
Marcin Wojtas3f518502014-07-10 16:52:13 -03004331 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004332 MVPP2_ISR_ENABLE_INTERRUPT(sw_thread_mask));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004333}
4334
4335static inline void mvpp2_interrupts_disable(struct mvpp2_port *port)
4336{
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004337 int i, sw_thread_mask = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004338
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004339 for (i = 0; i < port->nqvecs; i++)
4340 sw_thread_mask |= port->qvecs[i].sw_thread_mask;
4341
Marcin Wojtas3f518502014-07-10 16:52:13 -03004342 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004343 MVPP2_ISR_DISABLE_INTERRUPT(sw_thread_mask));
4344}
4345
4346static inline void mvpp2_qvec_interrupt_enable(struct mvpp2_queue_vector *qvec)
4347{
4348 struct mvpp2_port *port = qvec->port;
4349
4350 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
4351 MVPP2_ISR_ENABLE_INTERRUPT(qvec->sw_thread_mask));
4352}
4353
4354static inline void mvpp2_qvec_interrupt_disable(struct mvpp2_queue_vector *qvec)
4355{
4356 struct mvpp2_port *port = qvec->port;
4357
4358 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
4359 MVPP2_ISR_DISABLE_INTERRUPT(qvec->sw_thread_mask));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004360}
4361
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004362/* Mask the current CPU's Rx/Tx interrupts
4363 * Called by on_each_cpu(), guaranteed to run with migration disabled,
4364 * using smp_processor_id() is OK.
4365 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004366static void mvpp2_interrupts_mask(void *arg)
4367{
4368 struct mvpp2_port *port = arg;
4369
Thomas Petazzonia7868412017-03-07 16:53:13 +01004370 mvpp2_percpu_write(port->priv, smp_processor_id(),
4371 MVPP2_ISR_RX_TX_MASK_REG(port->id), 0);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004372}
4373
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004374/* Unmask the current CPU's Rx/Tx interrupts.
4375 * Called by on_each_cpu(), guaranteed to run with migration disabled,
4376 * using smp_processor_id() is OK.
4377 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004378static void mvpp2_interrupts_unmask(void *arg)
4379{
4380 struct mvpp2_port *port = arg;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02004381 u32 val;
4382
4383 val = MVPP2_CAUSE_MISC_SUM_MASK |
4384 MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
4385 if (port->has_tx_irqs)
4386 val |= MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004387
Thomas Petazzonia7868412017-03-07 16:53:13 +01004388 mvpp2_percpu_write(port->priv, smp_processor_id(),
Thomas Petazzoni213f4282017-08-03 10:42:00 +02004389 MVPP2_ISR_RX_TX_MASK_REG(port->id), val);
4390}
4391
4392static void
4393mvpp2_shared_interrupt_mask_unmask(struct mvpp2_port *port, bool mask)
4394{
4395 u32 val;
4396 int i;
4397
4398 if (port->priv->hw_version != MVPP22)
4399 return;
4400
4401 if (mask)
4402 val = 0;
4403 else
4404 val = MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
4405
4406 for (i = 0; i < port->nqvecs; i++) {
4407 struct mvpp2_queue_vector *v = port->qvecs + i;
4408
4409 if (v->type != MVPP2_QUEUE_VECTOR_SHARED)
4410 continue;
4411
4412 mvpp2_percpu_write(port->priv, v->sw_thread_id,
4413 MVPP2_ISR_RX_TX_MASK_REG(port->id), val);
4414 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004415}
4416
4417/* Port configuration routines */
4418
Antoine Ténartf84bf382017-08-22 19:08:27 +02004419static void mvpp22_gop_init_rgmii(struct mvpp2_port *port)
4420{
4421 struct mvpp2 *priv = port->priv;
4422 u32 val;
4423
4424 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
4425 val |= GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT;
4426 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
4427
4428 regmap_read(priv->sysctrl_base, GENCONF_CTRL0, &val);
4429 if (port->gop_id == 2)
4430 val |= GENCONF_CTRL0_PORT0_RGMII | GENCONF_CTRL0_PORT1_RGMII;
4431 else if (port->gop_id == 3)
4432 val |= GENCONF_CTRL0_PORT1_RGMII_MII;
4433 regmap_write(priv->sysctrl_base, GENCONF_CTRL0, val);
4434}
4435
4436static void mvpp22_gop_init_sgmii(struct mvpp2_port *port)
4437{
4438 struct mvpp2 *priv = port->priv;
4439 u32 val;
4440
4441 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
4442 val |= GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT |
4443 GENCONF_PORT_CTRL0_RX_DATA_SAMPLE;
4444 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
4445
4446 if (port->gop_id > 1) {
4447 regmap_read(priv->sysctrl_base, GENCONF_CTRL0, &val);
4448 if (port->gop_id == 2)
4449 val &= ~GENCONF_CTRL0_PORT0_RGMII;
4450 else if (port->gop_id == 3)
4451 val &= ~GENCONF_CTRL0_PORT1_RGMII_MII;
4452 regmap_write(priv->sysctrl_base, GENCONF_CTRL0, val);
4453 }
4454}
4455
4456static void mvpp22_gop_init_10gkr(struct mvpp2_port *port)
4457{
4458 struct mvpp2 *priv = port->priv;
4459 void __iomem *mpcs = priv->iface_base + MVPP22_MPCS_BASE(port->gop_id);
4460 void __iomem *xpcs = priv->iface_base + MVPP22_XPCS_BASE(port->gop_id);
4461 u32 val;
4462
4463 /* XPCS */
4464 val = readl(xpcs + MVPP22_XPCS_CFG0);
4465 val &= ~(MVPP22_XPCS_CFG0_PCS_MODE(0x3) |
4466 MVPP22_XPCS_CFG0_ACTIVE_LANE(0x3));
4467 val |= MVPP22_XPCS_CFG0_ACTIVE_LANE(2);
4468 writel(val, xpcs + MVPP22_XPCS_CFG0);
4469
4470 /* MPCS */
4471 val = readl(mpcs + MVPP22_MPCS_CTRL);
4472 val &= ~MVPP22_MPCS_CTRL_FWD_ERR_CONN;
4473 writel(val, mpcs + MVPP22_MPCS_CTRL);
4474
4475 val = readl(mpcs + MVPP22_MPCS_CLK_RESET);
4476 val &= ~(MVPP22_MPCS_CLK_RESET_DIV_RATIO(0x7) | MAC_CLK_RESET_MAC |
4477 MAC_CLK_RESET_SD_RX | MAC_CLK_RESET_SD_TX);
4478 val |= MVPP22_MPCS_CLK_RESET_DIV_RATIO(1);
4479 writel(val, mpcs + MVPP22_MPCS_CLK_RESET);
4480
4481 val &= ~MVPP22_MPCS_CLK_RESET_DIV_SET;
4482 val |= MAC_CLK_RESET_MAC | MAC_CLK_RESET_SD_RX | MAC_CLK_RESET_SD_TX;
4483 writel(val, mpcs + MVPP22_MPCS_CLK_RESET);
4484}
4485
4486static int mvpp22_gop_init(struct mvpp2_port *port)
4487{
4488 struct mvpp2 *priv = port->priv;
4489 u32 val;
4490
4491 if (!priv->sysctrl_base)
4492 return 0;
4493
4494 switch (port->phy_interface) {
4495 case PHY_INTERFACE_MODE_RGMII:
4496 case PHY_INTERFACE_MODE_RGMII_ID:
4497 case PHY_INTERFACE_MODE_RGMII_RXID:
4498 case PHY_INTERFACE_MODE_RGMII_TXID:
4499 if (port->gop_id == 0)
4500 goto invalid_conf;
4501 mvpp22_gop_init_rgmii(port);
4502 break;
4503 case PHY_INTERFACE_MODE_SGMII:
4504 mvpp22_gop_init_sgmii(port);
4505 break;
4506 case PHY_INTERFACE_MODE_10GKR:
4507 if (port->gop_id != 0)
4508 goto invalid_conf;
4509 mvpp22_gop_init_10gkr(port);
4510 break;
4511 default:
4512 goto unsupported_conf;
4513 }
4514
4515 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL1, &val);
4516 val |= GENCONF_PORT_CTRL1_RESET(port->gop_id) |
4517 GENCONF_PORT_CTRL1_EN(port->gop_id);
4518 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL1, val);
4519
4520 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
4521 val |= GENCONF_PORT_CTRL0_CLK_DIV_PHASE_CLR;
4522 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
4523
4524 regmap_read(priv->sysctrl_base, GENCONF_SOFT_RESET1, &val);
4525 val |= GENCONF_SOFT_RESET1_GOP;
4526 regmap_write(priv->sysctrl_base, GENCONF_SOFT_RESET1, val);
4527
4528unsupported_conf:
4529 return 0;
4530
4531invalid_conf:
4532 netdev_err(port->dev, "Invalid port configuration\n");
4533 return -EINVAL;
4534}
4535
Antoine Tenartfd3651b2017-09-01 11:04:54 +02004536static void mvpp22_gop_unmask_irq(struct mvpp2_port *port)
4537{
4538 u32 val;
4539
4540 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
4541 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4542 /* Enable the GMAC link status irq for this port */
4543 val = readl(port->base + MVPP22_GMAC_INT_SUM_MASK);
4544 val |= MVPP22_GMAC_INT_SUM_MASK_LINK_STAT;
4545 writel(val, port->base + MVPP22_GMAC_INT_SUM_MASK);
4546 }
4547
4548 if (port->gop_id == 0) {
4549 /* Enable the XLG/GIG irqs for this port */
4550 val = readl(port->base + MVPP22_XLG_EXT_INT_MASK);
4551 if (port->phy_interface == PHY_INTERFACE_MODE_10GKR)
4552 val |= MVPP22_XLG_EXT_INT_MASK_XLG;
4553 else
4554 val |= MVPP22_XLG_EXT_INT_MASK_GIG;
4555 writel(val, port->base + MVPP22_XLG_EXT_INT_MASK);
4556 }
4557}
4558
4559static void mvpp22_gop_mask_irq(struct mvpp2_port *port)
4560{
4561 u32 val;
4562
4563 if (port->gop_id == 0) {
4564 val = readl(port->base + MVPP22_XLG_EXT_INT_MASK);
4565 val &= ~(MVPP22_XLG_EXT_INT_MASK_XLG |
4566 MVPP22_XLG_EXT_INT_MASK_GIG);
4567 writel(val, port->base + MVPP22_XLG_EXT_INT_MASK);
4568 }
4569
4570 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
4571 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4572 val = readl(port->base + MVPP22_GMAC_INT_SUM_MASK);
4573 val &= ~MVPP22_GMAC_INT_SUM_MASK_LINK_STAT;
4574 writel(val, port->base + MVPP22_GMAC_INT_SUM_MASK);
4575 }
4576}
4577
4578static void mvpp22_gop_setup_irq(struct mvpp2_port *port)
4579{
4580 u32 val;
4581
4582 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
4583 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4584 val = readl(port->base + MVPP22_GMAC_INT_MASK);
4585 val |= MVPP22_GMAC_INT_MASK_LINK_STAT;
4586 writel(val, port->base + MVPP22_GMAC_INT_MASK);
4587 }
4588
4589 if (port->gop_id == 0) {
4590 val = readl(port->base + MVPP22_XLG_INT_MASK);
4591 val |= MVPP22_XLG_INT_MASK_LINK;
4592 writel(val, port->base + MVPP22_XLG_INT_MASK);
4593 }
4594
4595 mvpp22_gop_unmask_irq(port);
4596}
4597
Antoine Tenart542897d2017-08-30 10:29:15 +02004598static int mvpp22_comphy_init(struct mvpp2_port *port)
4599{
4600 enum phy_mode mode;
4601 int ret;
4602
4603 if (!port->comphy)
4604 return 0;
4605
4606 switch (port->phy_interface) {
4607 case PHY_INTERFACE_MODE_SGMII:
4608 mode = PHY_MODE_SGMII;
4609 break;
4610 case PHY_INTERFACE_MODE_10GKR:
4611 mode = PHY_MODE_10GKR;
4612 break;
4613 default:
4614 return -EINVAL;
4615 }
4616
4617 ret = phy_set_mode(port->comphy, mode);
4618 if (ret)
4619 return ret;
4620
4621 return phy_power_on(port->comphy);
4622}
4623
Antoine Ténart39193572017-08-22 19:08:24 +02004624static void mvpp2_port_mii_gmac_configure_mode(struct mvpp2_port *port)
4625{
4626 u32 val;
4627
4628 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4629 val = readl(port->base + MVPP22_GMAC_CTRL_4_REG);
4630 val |= MVPP22_CTRL4_SYNC_BYPASS_DIS | MVPP22_CTRL4_DP_CLK_SEL |
4631 MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
4632 val &= ~MVPP22_CTRL4_EXT_PIN_GMII_SEL;
4633 writel(val, port->base + MVPP22_GMAC_CTRL_4_REG);
Antoine Tenart1df22702017-09-01 11:04:52 +02004634 } else if (phy_interface_mode_is_rgmii(port->phy_interface)) {
Antoine Ténart39193572017-08-22 19:08:24 +02004635 val = readl(port->base + MVPP22_GMAC_CTRL_4_REG);
4636 val |= MVPP22_CTRL4_EXT_PIN_GMII_SEL |
4637 MVPP22_CTRL4_SYNC_BYPASS_DIS |
4638 MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
4639 val &= ~MVPP22_CTRL4_DP_CLK_SEL;
4640 writel(val, port->base + MVPP22_GMAC_CTRL_4_REG);
Antoine Ténart39193572017-08-22 19:08:24 +02004641 }
4642
4643 /* The port is connected to a copper PHY */
4644 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4645 val &= ~MVPP2_GMAC_PORT_TYPE_MASK;
4646 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4647
4648 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4649 val |= MVPP2_GMAC_IN_BAND_AUTONEG_BYPASS |
4650 MVPP2_GMAC_AN_SPEED_EN | MVPP2_GMAC_FLOW_CTRL_AUTONEG |
4651 MVPP2_GMAC_AN_DUPLEX_EN;
4652 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
4653 val |= MVPP2_GMAC_IN_BAND_AUTONEG;
4654 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4655}
4656
4657static void mvpp2_port_mii_gmac_configure(struct mvpp2_port *port)
4658{
4659 u32 val;
4660
4661 /* Force link down */
4662 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4663 val &= ~MVPP2_GMAC_FORCE_LINK_PASS;
4664 val |= MVPP2_GMAC_FORCE_LINK_DOWN;
4665 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4666
4667 /* Set the GMAC in a reset state */
4668 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4669 val |= MVPP2_GMAC_PORT_RESET_MASK;
4670 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4671
4672 /* Configure the PCS and in-band AN */
4673 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4674 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4675 val |= MVPP2_GMAC_INBAND_AN_MASK | MVPP2_GMAC_PCS_ENABLE_MASK;
Antoine Tenart1df22702017-09-01 11:04:52 +02004676 } else if (phy_interface_mode_is_rgmii(port->phy_interface)) {
Antoine Ténart39193572017-08-22 19:08:24 +02004677 val &= ~MVPP2_GMAC_PCS_ENABLE_MASK;
Antoine Ténart39193572017-08-22 19:08:24 +02004678 }
4679 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4680
4681 mvpp2_port_mii_gmac_configure_mode(port);
4682
4683 /* Unset the GMAC reset state */
4684 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4685 val &= ~MVPP2_GMAC_PORT_RESET_MASK;
4686 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4687
4688 /* Stop forcing link down */
4689 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4690 val &= ~MVPP2_GMAC_FORCE_LINK_DOWN;
4691 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4692}
4693
Antoine Ténart77321952017-08-22 19:08:25 +02004694static void mvpp2_port_mii_xlg_configure(struct mvpp2_port *port)
4695{
4696 u32 val;
4697
4698 if (port->gop_id != 0)
4699 return;
4700
4701 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
4702 val |= MVPP22_XLG_CTRL0_RX_FLOW_CTRL_EN;
4703 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
4704
4705 val = readl(port->base + MVPP22_XLG_CTRL4_REG);
4706 val &= ~MVPP22_XLG_CTRL4_MACMODSELECT_GMAC;
4707 val |= MVPP22_XLG_CTRL4_FWD_FC | MVPP22_XLG_CTRL4_FWD_PFC;
4708 writel(val, port->base + MVPP22_XLG_CTRL4_REG);
4709}
4710
Thomas Petazzoni26975822017-03-07 16:53:14 +01004711static void mvpp22_port_mii_set(struct mvpp2_port *port)
4712{
4713 u32 val;
4714
Thomas Petazzoni26975822017-03-07 16:53:14 +01004715 /* Only GOP port 0 has an XLG MAC */
4716 if (port->gop_id == 0) {
4717 val = readl(port->base + MVPP22_XLG_CTRL3_REG);
4718 val &= ~MVPP22_XLG_CTRL3_MACMODESELECT_MASK;
Antoine Ténart725757a2017-06-12 16:01:39 +02004719
4720 if (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
4721 port->phy_interface == PHY_INTERFACE_MODE_10GKR)
4722 val |= MVPP22_XLG_CTRL3_MACMODESELECT_10G;
4723 else
4724 val |= MVPP22_XLG_CTRL3_MACMODESELECT_GMAC;
4725
Thomas Petazzoni26975822017-03-07 16:53:14 +01004726 writel(val, port->base + MVPP22_XLG_CTRL3_REG);
4727 }
Thomas Petazzoni26975822017-03-07 16:53:14 +01004728}
4729
Marcin Wojtas3f518502014-07-10 16:52:13 -03004730static void mvpp2_port_mii_set(struct mvpp2_port *port)
4731{
Thomas Petazzoni26975822017-03-07 16:53:14 +01004732 if (port->priv->hw_version == MVPP22)
4733 mvpp22_port_mii_set(port);
4734
Antoine Tenart1df22702017-09-01 11:04:52 +02004735 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
Antoine Ténart39193572017-08-22 19:08:24 +02004736 port->phy_interface == PHY_INTERFACE_MODE_SGMII)
4737 mvpp2_port_mii_gmac_configure(port);
Antoine Ténart77321952017-08-22 19:08:25 +02004738 else if (port->phy_interface == PHY_INTERFACE_MODE_10GKR)
4739 mvpp2_port_mii_xlg_configure(port);
Marcin Wojtas08a23752014-07-21 13:48:12 -03004740}
4741
4742static void mvpp2_port_fc_adv_enable(struct mvpp2_port *port)
4743{
4744 u32 val;
4745
4746 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4747 val |= MVPP2_GMAC_FC_ADV_EN;
4748 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004749}
4750
4751static void mvpp2_port_enable(struct mvpp2_port *port)
4752{
4753 u32 val;
4754
Antoine Ténart725757a2017-06-12 16:01:39 +02004755 /* Only GOP port 0 has an XLG MAC */
4756 if (port->gop_id == 0 &&
4757 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
4758 port->phy_interface == PHY_INTERFACE_MODE_10GKR)) {
4759 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
4760 val |= MVPP22_XLG_CTRL0_PORT_EN |
4761 MVPP22_XLG_CTRL0_MAC_RESET_DIS;
4762 val &= ~MVPP22_XLG_CTRL0_MIB_CNT_DIS;
4763 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
4764 } else {
4765 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4766 val |= MVPP2_GMAC_PORT_EN_MASK;
4767 val |= MVPP2_GMAC_MIB_CNTR_EN_MASK;
4768 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4769 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004770}
4771
4772static void mvpp2_port_disable(struct mvpp2_port *port)
4773{
4774 u32 val;
4775
Antoine Ténart725757a2017-06-12 16:01:39 +02004776 /* Only GOP port 0 has an XLG MAC */
4777 if (port->gop_id == 0 &&
4778 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
4779 port->phy_interface == PHY_INTERFACE_MODE_10GKR)) {
4780 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
4781 val &= ~(MVPP22_XLG_CTRL0_PORT_EN |
4782 MVPP22_XLG_CTRL0_MAC_RESET_DIS);
4783 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
4784 } else {
4785 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4786 val &= ~(MVPP2_GMAC_PORT_EN_MASK);
4787 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4788 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004789}
4790
4791/* Set IEEE 802.3x Flow Control Xon Packet Transmission Mode */
4792static void mvpp2_port_periodic_xon_disable(struct mvpp2_port *port)
4793{
4794 u32 val;
4795
4796 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG) &
4797 ~MVPP2_GMAC_PERIODIC_XON_EN_MASK;
4798 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
4799}
4800
4801/* Configure loopback port */
4802static void mvpp2_port_loopback_set(struct mvpp2_port *port)
4803{
4804 u32 val;
4805
4806 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
4807
4808 if (port->speed == 1000)
4809 val |= MVPP2_GMAC_GMII_LB_EN_MASK;
4810 else
4811 val &= ~MVPP2_GMAC_GMII_LB_EN_MASK;
4812
4813 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
4814 val |= MVPP2_GMAC_PCS_LB_EN_MASK;
4815 else
4816 val &= ~MVPP2_GMAC_PCS_LB_EN_MASK;
4817
4818 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
4819}
4820
Miquel Raynal118d6292017-11-06 22:56:53 +01004821struct mvpp2_ethtool_counter {
4822 unsigned int offset;
4823 const char string[ETH_GSTRING_LEN];
4824 bool reg_is_64b;
4825};
4826
4827static u64 mvpp2_read_count(struct mvpp2_port *port,
4828 const struct mvpp2_ethtool_counter *counter)
4829{
4830 u64 val;
4831
4832 val = readl(port->stats_base + counter->offset);
4833 if (counter->reg_is_64b)
4834 val += (u64)readl(port->stats_base + counter->offset + 4) << 32;
4835
4836 return val;
4837}
4838
4839/* Due to the fact that software statistics and hardware statistics are, by
4840 * design, incremented at different moments in the chain of packet processing,
4841 * it is very likely that incoming packets could have been dropped after being
4842 * counted by hardware but before reaching software statistics (most probably
4843 * multicast packets), and in the oppposite way, during transmission, FCS bytes
4844 * are added in between as well as TSO skb will be split and header bytes added.
4845 * Hence, statistics gathered from userspace with ifconfig (software) and
4846 * ethtool (hardware) cannot be compared.
4847 */
4848static const struct mvpp2_ethtool_counter mvpp2_ethtool_regs[] = {
4849 { MVPP2_MIB_GOOD_OCTETS_RCVD, "good_octets_received", true },
4850 { MVPP2_MIB_BAD_OCTETS_RCVD, "bad_octets_received" },
4851 { MVPP2_MIB_CRC_ERRORS_SENT, "crc_errors_sent" },
4852 { MVPP2_MIB_UNICAST_FRAMES_RCVD, "unicast_frames_received" },
4853 { MVPP2_MIB_BROADCAST_FRAMES_RCVD, "broadcast_frames_received" },
4854 { MVPP2_MIB_MULTICAST_FRAMES_RCVD, "multicast_frames_received" },
4855 { MVPP2_MIB_FRAMES_64_OCTETS, "frames_64_octets" },
4856 { MVPP2_MIB_FRAMES_65_TO_127_OCTETS, "frames_65_to_127_octet" },
4857 { MVPP2_MIB_FRAMES_128_TO_255_OCTETS, "frames_128_to_255_octet" },
4858 { MVPP2_MIB_FRAMES_256_TO_511_OCTETS, "frames_256_to_511_octet" },
4859 { MVPP2_MIB_FRAMES_512_TO_1023_OCTETS, "frames_512_to_1023_octet" },
4860 { MVPP2_MIB_FRAMES_1024_TO_MAX_OCTETS, "frames_1024_to_max_octet" },
4861 { MVPP2_MIB_GOOD_OCTETS_SENT, "good_octets_sent", true },
4862 { MVPP2_MIB_UNICAST_FRAMES_SENT, "unicast_frames_sent" },
4863 { MVPP2_MIB_MULTICAST_FRAMES_SENT, "multicast_frames_sent" },
4864 { MVPP2_MIB_BROADCAST_FRAMES_SENT, "broadcast_frames_sent" },
4865 { MVPP2_MIB_FC_SENT, "fc_sent" },
4866 { MVPP2_MIB_FC_RCVD, "fc_received" },
4867 { MVPP2_MIB_RX_FIFO_OVERRUN, "rx_fifo_overrun" },
4868 { MVPP2_MIB_UNDERSIZE_RCVD, "undersize_received" },
4869 { MVPP2_MIB_FRAGMENTS_RCVD, "fragments_received" },
4870 { MVPP2_MIB_OVERSIZE_RCVD, "oversize_received" },
4871 { MVPP2_MIB_JABBER_RCVD, "jabber_received" },
4872 { MVPP2_MIB_MAC_RCV_ERROR, "mac_receive_error" },
4873 { MVPP2_MIB_BAD_CRC_EVENT, "bad_crc_event" },
4874 { MVPP2_MIB_COLLISION, "collision" },
4875 { MVPP2_MIB_LATE_COLLISION, "late_collision" },
4876};
4877
4878static void mvpp2_ethtool_get_strings(struct net_device *netdev, u32 sset,
4879 u8 *data)
4880{
4881 if (sset == ETH_SS_STATS) {
4882 int i;
4883
4884 for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_regs); i++)
4885 memcpy(data + i * ETH_GSTRING_LEN,
4886 &mvpp2_ethtool_regs[i].string, ETH_GSTRING_LEN);
4887 }
4888}
4889
4890static void mvpp2_gather_hw_statistics(struct work_struct *work)
4891{
4892 struct delayed_work *del_work = to_delayed_work(work);
Miquel Raynale5c500e2017-11-08 08:59:40 +01004893 struct mvpp2_port *port = container_of(del_work, struct mvpp2_port,
4894 stats_work);
Miquel Raynal118d6292017-11-06 22:56:53 +01004895 u64 *pstats;
Miquel Raynale5c500e2017-11-08 08:59:40 +01004896 int i;
Miquel Raynal118d6292017-11-06 22:56:53 +01004897
Miquel Raynale5c500e2017-11-08 08:59:40 +01004898 mutex_lock(&port->gather_stats_lock);
Miquel Raynal118d6292017-11-06 22:56:53 +01004899
Miquel Raynale5c500e2017-11-08 08:59:40 +01004900 pstats = port->ethtool_stats;
4901 for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_regs); i++)
4902 *pstats++ += mvpp2_read_count(port, &mvpp2_ethtool_regs[i]);
Miquel Raynal118d6292017-11-06 22:56:53 +01004903
4904 /* No need to read again the counters right after this function if it
4905 * was called asynchronously by the user (ie. use of ethtool).
4906 */
Miquel Raynale5c500e2017-11-08 08:59:40 +01004907 cancel_delayed_work(&port->stats_work);
4908 queue_delayed_work(port->priv->stats_queue, &port->stats_work,
Miquel Raynal118d6292017-11-06 22:56:53 +01004909 MVPP2_MIB_COUNTERS_STATS_DELAY);
4910
Miquel Raynale5c500e2017-11-08 08:59:40 +01004911 mutex_unlock(&port->gather_stats_lock);
Miquel Raynal118d6292017-11-06 22:56:53 +01004912}
4913
4914static void mvpp2_ethtool_get_stats(struct net_device *dev,
4915 struct ethtool_stats *stats, u64 *data)
4916{
4917 struct mvpp2_port *port = netdev_priv(dev);
4918
Miquel Raynale5c500e2017-11-08 08:59:40 +01004919 /* Update statistics for the given port, then take the lock to avoid
4920 * concurrent accesses on the ethtool_stats structure during its copy.
4921 */
4922 mvpp2_gather_hw_statistics(&port->stats_work.work);
Miquel Raynal118d6292017-11-06 22:56:53 +01004923
Miquel Raynale5c500e2017-11-08 08:59:40 +01004924 mutex_lock(&port->gather_stats_lock);
Miquel Raynal118d6292017-11-06 22:56:53 +01004925 memcpy(data, port->ethtool_stats,
4926 sizeof(u64) * ARRAY_SIZE(mvpp2_ethtool_regs));
Miquel Raynale5c500e2017-11-08 08:59:40 +01004927 mutex_unlock(&port->gather_stats_lock);
Miquel Raynal118d6292017-11-06 22:56:53 +01004928}
4929
4930static int mvpp2_ethtool_get_sset_count(struct net_device *dev, int sset)
4931{
4932 if (sset == ETH_SS_STATS)
4933 return ARRAY_SIZE(mvpp2_ethtool_regs);
4934
4935 return -EOPNOTSUPP;
4936}
4937
Marcin Wojtas3f518502014-07-10 16:52:13 -03004938static void mvpp2_port_reset(struct mvpp2_port *port)
4939{
4940 u32 val;
Miquel Raynal118d6292017-11-06 22:56:53 +01004941 unsigned int i;
4942
4943 /* Read the GOP statistics to reset the hardware counters */
4944 for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_regs); i++)
4945 mvpp2_read_count(port, &mvpp2_ethtool_regs[i]);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004946
4947 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
4948 ~MVPP2_GMAC_PORT_RESET_MASK;
4949 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4950
4951 while (readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
4952 MVPP2_GMAC_PORT_RESET_MASK)
4953 continue;
4954}
4955
4956/* Change maximum receive size of the port */
4957static inline void mvpp2_gmac_max_rx_size_set(struct mvpp2_port *port)
4958{
4959 u32 val;
4960
4961 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4962 val &= ~MVPP2_GMAC_MAX_RX_SIZE_MASK;
4963 val |= (((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
4964 MVPP2_GMAC_MAX_RX_SIZE_OFFS);
4965 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4966}
4967
Stefan Chulski76eb1b12017-08-22 19:08:26 +02004968/* Change maximum receive size of the port */
4969static inline void mvpp2_xlg_max_rx_size_set(struct mvpp2_port *port)
4970{
4971 u32 val;
4972
4973 val = readl(port->base + MVPP22_XLG_CTRL1_REG);
4974 val &= ~MVPP22_XLG_CTRL1_FRAMESIZELIMIT_MASK;
4975 val |= ((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
Antoine Ténartec15ecd2017-08-25 15:24:46 +02004976 MVPP22_XLG_CTRL1_FRAMESIZELIMIT_OFFS;
Stefan Chulski76eb1b12017-08-22 19:08:26 +02004977 writel(val, port->base + MVPP22_XLG_CTRL1_REG);
4978}
4979
Marcin Wojtas3f518502014-07-10 16:52:13 -03004980/* Set defaults to the MVPP2 port */
4981static void mvpp2_defaults_set(struct mvpp2_port *port)
4982{
4983 int tx_port_num, val, queue, ptxq, lrxq;
4984
Thomas Petazzoni3d9017d2017-03-07 16:53:11 +01004985 if (port->priv->hw_version == MVPP21) {
4986 /* Configure port to loopback if needed */
4987 if (port->flags & MVPP2_F_LOOPBACK)
4988 mvpp2_port_loopback_set(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004989
Thomas Petazzoni3d9017d2017-03-07 16:53:11 +01004990 /* Update TX FIFO MIN Threshold */
4991 val = readl(port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
4992 val &= ~MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK;
4993 /* Min. TX threshold must be less than minimal packet length */
4994 val |= MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(64 - 4 - 2);
4995 writel(val, port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
4996 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004997
4998 /* Disable Legacy WRR, Disable EJP, Release from reset */
4999 tx_port_num = mvpp2_egress_port(port);
5000 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG,
5001 tx_port_num);
5002 mvpp2_write(port->priv, MVPP2_TXP_SCHED_CMD_1_REG, 0);
5003
5004 /* Close bandwidth for all queues */
5005 for (queue = 0; queue < MVPP2_MAX_TXQ; queue++) {
5006 ptxq = mvpp2_txq_phys(port->id, queue);
5007 mvpp2_write(port->priv,
5008 MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(ptxq), 0);
5009 }
5010
5011 /* Set refill period to 1 usec, refill tokens
5012 * and bucket size to maximum
5013 */
5014 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PERIOD_REG,
5015 port->priv->tclk / USEC_PER_SEC);
5016 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_REFILL_REG);
5017 val &= ~MVPP2_TXP_REFILL_PERIOD_ALL_MASK;
5018 val |= MVPP2_TXP_REFILL_PERIOD_MASK(1);
5019 val |= MVPP2_TXP_REFILL_TOKENS_ALL_MASK;
5020 mvpp2_write(port->priv, MVPP2_TXP_SCHED_REFILL_REG, val);
5021 val = MVPP2_TXP_TOKEN_SIZE_MAX;
5022 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
5023
5024 /* Set MaximumLowLatencyPacketSize value to 256 */
5025 mvpp2_write(port->priv, MVPP2_RX_CTRL_REG(port->id),
5026 MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK |
5027 MVPP2_RX_LOW_LATENCY_PKT_SIZE(256));
5028
5029 /* Enable Rx cache snoop */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005030 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005031 queue = port->rxqs[lrxq]->id;
5032 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
5033 val |= MVPP2_SNOOP_PKT_SIZE_MASK |
5034 MVPP2_SNOOP_BUF_HDR_MASK;
5035 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
5036 }
5037
5038 /* At default, mask all interrupts to all present cpus */
5039 mvpp2_interrupts_disable(port);
5040}
5041
5042/* Enable/disable receiving packets */
5043static void mvpp2_ingress_enable(struct mvpp2_port *port)
5044{
5045 u32 val;
5046 int lrxq, queue;
5047
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005048 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005049 queue = port->rxqs[lrxq]->id;
5050 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
5051 val &= ~MVPP2_RXQ_DISABLE_MASK;
5052 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
5053 }
5054}
5055
5056static void mvpp2_ingress_disable(struct mvpp2_port *port)
5057{
5058 u32 val;
5059 int lrxq, queue;
5060
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005061 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005062 queue = port->rxqs[lrxq]->id;
5063 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
5064 val |= MVPP2_RXQ_DISABLE_MASK;
5065 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
5066 }
5067}
5068
5069/* Enable transmit via physical egress queue
5070 * - HW starts take descriptors from DRAM
5071 */
5072static void mvpp2_egress_enable(struct mvpp2_port *port)
5073{
5074 u32 qmap;
5075 int queue;
5076 int tx_port_num = mvpp2_egress_port(port);
5077
5078 /* Enable all initialized TXs. */
5079 qmap = 0;
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005080 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005081 struct mvpp2_tx_queue *txq = port->txqs[queue];
5082
Markus Elfringdbbb2f02017-04-17 14:07:52 +02005083 if (txq->descs)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005084 qmap |= (1 << queue);
5085 }
5086
5087 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
5088 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG, qmap);
5089}
5090
5091/* Disable transmit via physical egress queue
5092 * - HW doesn't take descriptors from DRAM
5093 */
5094static void mvpp2_egress_disable(struct mvpp2_port *port)
5095{
5096 u32 reg_data;
5097 int delay;
5098 int tx_port_num = mvpp2_egress_port(port);
5099
5100 /* Issue stop command for active channels only */
5101 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
5102 reg_data = (mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG)) &
5103 MVPP2_TXP_SCHED_ENQ_MASK;
5104 if (reg_data != 0)
5105 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG,
5106 (reg_data << MVPP2_TXP_SCHED_DISQ_OFFSET));
5107
5108 /* Wait for all Tx activity to terminate. */
5109 delay = 0;
5110 do {
5111 if (delay >= MVPP2_TX_DISABLE_TIMEOUT_MSEC) {
5112 netdev_warn(port->dev,
5113 "Tx stop timed out, status=0x%08x\n",
5114 reg_data);
5115 break;
5116 }
5117 mdelay(1);
5118 delay++;
5119
5120 /* Check port TX Command register that all
5121 * Tx queues are stopped
5122 */
5123 reg_data = mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG);
5124 } while (reg_data & MVPP2_TXP_SCHED_ENQ_MASK);
5125}
5126
5127/* Rx descriptors helper methods */
5128
5129/* Get number of Rx descriptors occupied by received packets */
5130static inline int
5131mvpp2_rxq_received(struct mvpp2_port *port, int rxq_id)
5132{
5133 u32 val = mvpp2_read(port->priv, MVPP2_RXQ_STATUS_REG(rxq_id));
5134
5135 return val & MVPP2_RXQ_OCCUPIED_MASK;
5136}
5137
5138/* Update Rx queue status with the number of occupied and available
5139 * Rx descriptor slots.
5140 */
5141static inline void
5142mvpp2_rxq_status_update(struct mvpp2_port *port, int rxq_id,
5143 int used_count, int free_count)
5144{
5145 /* Decrement the number of used descriptors and increment count
5146 * increment the number of free descriptors.
5147 */
5148 u32 val = used_count | (free_count << MVPP2_RXQ_NUM_NEW_OFFSET);
5149
5150 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_UPDATE_REG(rxq_id), val);
5151}
5152
5153/* Get pointer to next RX descriptor to be processed by SW */
5154static inline struct mvpp2_rx_desc *
5155mvpp2_rxq_next_desc_get(struct mvpp2_rx_queue *rxq)
5156{
5157 int rx_desc = rxq->next_desc_to_proc;
5158
5159 rxq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(rxq, rx_desc);
5160 prefetch(rxq->descs + rxq->next_desc_to_proc);
5161 return rxq->descs + rx_desc;
5162}
5163
5164/* Set rx queue offset */
5165static void mvpp2_rxq_offset_set(struct mvpp2_port *port,
5166 int prxq, int offset)
5167{
5168 u32 val;
5169
5170 /* Convert offset from bytes to units of 32 bytes */
5171 offset = offset >> 5;
5172
5173 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
5174 val &= ~MVPP2_RXQ_PACKET_OFFSET_MASK;
5175
5176 /* Offset is in */
5177 val |= ((offset << MVPP2_RXQ_PACKET_OFFSET_OFFS) &
5178 MVPP2_RXQ_PACKET_OFFSET_MASK);
5179
5180 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
5181}
5182
Marcin Wojtas3f518502014-07-10 16:52:13 -03005183/* Tx descriptors helper methods */
5184
Marcin Wojtas3f518502014-07-10 16:52:13 -03005185/* Get pointer to next Tx descriptor to be processed (send) by HW */
5186static struct mvpp2_tx_desc *
5187mvpp2_txq_next_desc_get(struct mvpp2_tx_queue *txq)
5188{
5189 int tx_desc = txq->next_desc_to_proc;
5190
5191 txq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(txq, tx_desc);
5192 return txq->descs + tx_desc;
5193}
5194
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005195/* Update HW with number of aggregated Tx descriptors to be sent
5196 *
5197 * Called only from mvpp2_tx(), so migration is disabled, using
5198 * smp_processor_id() is OK.
5199 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03005200static void mvpp2_aggr_txq_pend_desc_add(struct mvpp2_port *port, int pending)
5201{
5202 /* aggregated access - relevant TXQ number is written in TX desc */
Thomas Petazzonia7868412017-03-07 16:53:13 +01005203 mvpp2_percpu_write(port->priv, smp_processor_id(),
5204 MVPP2_AGGR_TXQ_UPDATE_REG, pending);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005205}
5206
5207
5208/* Check if there are enough free descriptors in aggregated txq.
5209 * If not, update the number of occupied descriptors and repeat the check.
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005210 *
5211 * Called only from mvpp2_tx(), so migration is disabled, using
5212 * smp_processor_id() is OK.
Marcin Wojtas3f518502014-07-10 16:52:13 -03005213 */
5214static int mvpp2_aggr_desc_num_check(struct mvpp2 *priv,
5215 struct mvpp2_tx_queue *aggr_txq, int num)
5216{
Antoine Tenart02856a32017-10-30 11:23:32 +01005217 if ((aggr_txq->count + num) > MVPP2_AGGR_TXQ_SIZE) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005218 /* Update number of occupied aggregated Tx descriptors */
5219 int cpu = smp_processor_id();
5220 u32 val = mvpp2_read(priv, MVPP2_AGGR_TXQ_STATUS_REG(cpu));
5221
5222 aggr_txq->count = val & MVPP2_AGGR_TXQ_PENDING_MASK;
5223 }
5224
Antoine Tenart02856a32017-10-30 11:23:32 +01005225 if ((aggr_txq->count + num) > MVPP2_AGGR_TXQ_SIZE)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005226 return -ENOMEM;
5227
5228 return 0;
5229}
5230
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005231/* Reserved Tx descriptors allocation request
5232 *
5233 * Called only from mvpp2_txq_reserved_desc_num_proc(), itself called
5234 * only by mvpp2_tx(), so migration is disabled, using
5235 * smp_processor_id() is OK.
5236 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03005237static int mvpp2_txq_alloc_reserved_desc(struct mvpp2 *priv,
5238 struct mvpp2_tx_queue *txq, int num)
5239{
5240 u32 val;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005241 int cpu = smp_processor_id();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005242
5243 val = (txq->id << MVPP2_TXQ_RSVD_REQ_Q_OFFSET) | num;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005244 mvpp2_percpu_write(priv, cpu, MVPP2_TXQ_RSVD_REQ_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005245
Thomas Petazzonia7868412017-03-07 16:53:13 +01005246 val = mvpp2_percpu_read(priv, cpu, MVPP2_TXQ_RSVD_RSLT_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005247
5248 return val & MVPP2_TXQ_RSVD_RSLT_MASK;
5249}
5250
5251/* Check if there are enough reserved descriptors for transmission.
5252 * If not, request chunk of reserved descriptors and check again.
5253 */
5254static int mvpp2_txq_reserved_desc_num_proc(struct mvpp2 *priv,
5255 struct mvpp2_tx_queue *txq,
5256 struct mvpp2_txq_pcpu *txq_pcpu,
5257 int num)
5258{
5259 int req, cpu, desc_count;
5260
5261 if (txq_pcpu->reserved_num >= num)
5262 return 0;
5263
5264 /* Not enough descriptors reserved! Update the reserved descriptor
5265 * count and check again.
5266 */
5267
5268 desc_count = 0;
5269 /* Compute total of used descriptors */
5270 for_each_present_cpu(cpu) {
5271 struct mvpp2_txq_pcpu *txq_pcpu_aux;
5272
5273 txq_pcpu_aux = per_cpu_ptr(txq->pcpu, cpu);
5274 desc_count += txq_pcpu_aux->count;
5275 desc_count += txq_pcpu_aux->reserved_num;
5276 }
5277
5278 req = max(MVPP2_CPU_DESC_CHUNK, num - txq_pcpu->reserved_num);
5279 desc_count += req;
5280
5281 if (desc_count >
5282 (txq->size - (num_present_cpus() * MVPP2_CPU_DESC_CHUNK)))
5283 return -ENOMEM;
5284
5285 txq_pcpu->reserved_num += mvpp2_txq_alloc_reserved_desc(priv, txq, req);
5286
5287 /* OK, the descriptor cound has been updated: check again. */
5288 if (txq_pcpu->reserved_num < num)
5289 return -ENOMEM;
5290 return 0;
5291}
5292
5293/* Release the last allocated Tx descriptor. Useful to handle DMA
5294 * mapping failures in the Tx path.
5295 */
5296static void mvpp2_txq_desc_put(struct mvpp2_tx_queue *txq)
5297{
5298 if (txq->next_desc_to_proc == 0)
5299 txq->next_desc_to_proc = txq->last_desc - 1;
5300 else
5301 txq->next_desc_to_proc--;
5302}
5303
5304/* Set Tx descriptors fields relevant for CSUM calculation */
5305static u32 mvpp2_txq_desc_csum(int l3_offs, int l3_proto,
5306 int ip_hdr_len, int l4_proto)
5307{
5308 u32 command;
5309
5310 /* fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
5311 * G_L4_chk, L4_type required only for checksum calculation
5312 */
5313 command = (l3_offs << MVPP2_TXD_L3_OFF_SHIFT);
5314 command |= (ip_hdr_len << MVPP2_TXD_IP_HLEN_SHIFT);
5315 command |= MVPP2_TXD_IP_CSUM_DISABLE;
5316
5317 if (l3_proto == swab16(ETH_P_IP)) {
5318 command &= ~MVPP2_TXD_IP_CSUM_DISABLE; /* enable IPv4 csum */
5319 command &= ~MVPP2_TXD_L3_IP6; /* enable IPv4 */
5320 } else {
5321 command |= MVPP2_TXD_L3_IP6; /* enable IPv6 */
5322 }
5323
5324 if (l4_proto == IPPROTO_TCP) {
5325 command &= ~MVPP2_TXD_L4_UDP; /* enable TCP */
5326 command &= ~MVPP2_TXD_L4_CSUM_FRAG; /* generate L4 csum */
5327 } else if (l4_proto == IPPROTO_UDP) {
5328 command |= MVPP2_TXD_L4_UDP; /* enable UDP */
5329 command &= ~MVPP2_TXD_L4_CSUM_FRAG; /* generate L4 csum */
5330 } else {
5331 command |= MVPP2_TXD_L4_CSUM_NOT;
5332 }
5333
5334 return command;
5335}
5336
5337/* Get number of sent descriptors and decrement counter.
5338 * The number of sent descriptors is returned.
5339 * Per-CPU access
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005340 *
5341 * Called only from mvpp2_txq_done(), called from mvpp2_tx()
5342 * (migration disabled) and from the TX completion tasklet (migration
5343 * disabled) so using smp_processor_id() is OK.
Marcin Wojtas3f518502014-07-10 16:52:13 -03005344 */
5345static inline int mvpp2_txq_sent_desc_proc(struct mvpp2_port *port,
5346 struct mvpp2_tx_queue *txq)
5347{
5348 u32 val;
5349
5350 /* Reading status reg resets transmitted descriptor counter */
Thomas Petazzonia7868412017-03-07 16:53:13 +01005351 val = mvpp2_percpu_read(port->priv, smp_processor_id(),
5352 MVPP2_TXQ_SENT_REG(txq->id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005353
5354 return (val & MVPP2_TRANSMITTED_COUNT_MASK) >>
5355 MVPP2_TRANSMITTED_COUNT_OFFSET;
5356}
5357
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005358/* Called through on_each_cpu(), so runs on all CPUs, with migration
5359 * disabled, therefore using smp_processor_id() is OK.
5360 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03005361static void mvpp2_txq_sent_counter_clear(void *arg)
5362{
5363 struct mvpp2_port *port = arg;
5364 int queue;
5365
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005366 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005367 int id = port->txqs[queue]->id;
5368
Thomas Petazzonia7868412017-03-07 16:53:13 +01005369 mvpp2_percpu_read(port->priv, smp_processor_id(),
5370 MVPP2_TXQ_SENT_REG(id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005371 }
5372}
5373
5374/* Set max sizes for Tx queues */
5375static void mvpp2_txp_max_tx_size_set(struct mvpp2_port *port)
5376{
5377 u32 val, size, mtu;
5378 int txq, tx_port_num;
5379
5380 mtu = port->pkt_size * 8;
5381 if (mtu > MVPP2_TXP_MTU_MAX)
5382 mtu = MVPP2_TXP_MTU_MAX;
5383
5384 /* WA for wrong Token bucket update: Set MTU value = 3*real MTU value */
5385 mtu = 3 * mtu;
5386
5387 /* Indirect access to registers */
5388 tx_port_num = mvpp2_egress_port(port);
5389 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
5390
5391 /* Set MTU */
5392 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_MTU_REG);
5393 val &= ~MVPP2_TXP_MTU_MAX;
5394 val |= mtu;
5395 mvpp2_write(port->priv, MVPP2_TXP_SCHED_MTU_REG, val);
5396
5397 /* TXP token size and all TXQs token size must be larger that MTU */
5398 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG);
5399 size = val & MVPP2_TXP_TOKEN_SIZE_MAX;
5400 if (size < mtu) {
5401 size = mtu;
5402 val &= ~MVPP2_TXP_TOKEN_SIZE_MAX;
5403 val |= size;
5404 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
5405 }
5406
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005407 for (txq = 0; txq < port->ntxqs; txq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005408 val = mvpp2_read(port->priv,
5409 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq));
5410 size = val & MVPP2_TXQ_TOKEN_SIZE_MAX;
5411
5412 if (size < mtu) {
5413 size = mtu;
5414 val &= ~MVPP2_TXQ_TOKEN_SIZE_MAX;
5415 val |= size;
5416 mvpp2_write(port->priv,
5417 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq),
5418 val);
5419 }
5420 }
5421}
5422
5423/* Set the number of packets that will be received before Rx interrupt
5424 * will be generated by HW.
5425 */
5426static void mvpp2_rx_pkts_coal_set(struct mvpp2_port *port,
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01005427 struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005428{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005429 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005430
Thomas Petazzonif8b0d5f2017-02-21 11:28:03 +01005431 if (rxq->pkts_coal > MVPP2_OCCUPIED_THRESH_MASK)
5432 rxq->pkts_coal = MVPP2_OCCUPIED_THRESH_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005433
Thomas Petazzonia7868412017-03-07 16:53:13 +01005434 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
5435 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_THRESH_REG,
5436 rxq->pkts_coal);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005437
5438 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005439}
5440
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005441/* For some reason in the LSP this is done on each CPU. Why ? */
5442static void mvpp2_tx_pkts_coal_set(struct mvpp2_port *port,
5443 struct mvpp2_tx_queue *txq)
5444{
5445 int cpu = get_cpu();
5446 u32 val;
5447
5448 if (txq->done_pkts_coal > MVPP2_TXQ_THRESH_MASK)
5449 txq->done_pkts_coal = MVPP2_TXQ_THRESH_MASK;
5450
5451 val = (txq->done_pkts_coal << MVPP2_TXQ_THRESH_OFFSET);
5452 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5453 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_THRESH_REG, val);
5454
5455 put_cpu();
5456}
5457
Thomas Petazzoniab426762017-02-21 11:28:04 +01005458static u32 mvpp2_usec_to_cycles(u32 usec, unsigned long clk_hz)
5459{
5460 u64 tmp = (u64)clk_hz * usec;
5461
5462 do_div(tmp, USEC_PER_SEC);
5463
5464 return tmp > U32_MAX ? U32_MAX : tmp;
5465}
5466
5467static u32 mvpp2_cycles_to_usec(u32 cycles, unsigned long clk_hz)
5468{
5469 u64 tmp = (u64)cycles * USEC_PER_SEC;
5470
5471 do_div(tmp, clk_hz);
5472
5473 return tmp > U32_MAX ? U32_MAX : tmp;
5474}
5475
Marcin Wojtas3f518502014-07-10 16:52:13 -03005476/* Set the time delay in usec before Rx interrupt */
5477static void mvpp2_rx_time_coal_set(struct mvpp2_port *port,
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01005478 struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005479{
Thomas Petazzoniab426762017-02-21 11:28:04 +01005480 unsigned long freq = port->priv->tclk;
5481 u32 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005482
Thomas Petazzoniab426762017-02-21 11:28:04 +01005483 if (val > MVPP2_MAX_ISR_RX_THRESHOLD) {
5484 rxq->time_coal =
5485 mvpp2_cycles_to_usec(MVPP2_MAX_ISR_RX_THRESHOLD, freq);
5486
5487 /* re-evaluate to get actual register value */
5488 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
5489 }
5490
Marcin Wojtas3f518502014-07-10 16:52:13 -03005491 mvpp2_write(port->priv, MVPP2_ISR_RX_THRESHOLD_REG(rxq->id), val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005492}
5493
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005494static void mvpp2_tx_time_coal_set(struct mvpp2_port *port)
5495{
5496 unsigned long freq = port->priv->tclk;
5497 u32 val = mvpp2_usec_to_cycles(port->tx_time_coal, freq);
5498
5499 if (val > MVPP2_MAX_ISR_TX_THRESHOLD) {
5500 port->tx_time_coal =
5501 mvpp2_cycles_to_usec(MVPP2_MAX_ISR_TX_THRESHOLD, freq);
5502
5503 /* re-evaluate to get actual register value */
5504 val = mvpp2_usec_to_cycles(port->tx_time_coal, freq);
5505 }
5506
5507 mvpp2_write(port->priv, MVPP2_ISR_TX_THRESHOLD_REG(port->id), val);
5508}
5509
Marcin Wojtas3f518502014-07-10 16:52:13 -03005510/* Free Tx queue skbuffs */
5511static void mvpp2_txq_bufs_free(struct mvpp2_port *port,
5512 struct mvpp2_tx_queue *txq,
5513 struct mvpp2_txq_pcpu *txq_pcpu, int num)
5514{
5515 int i;
5516
5517 for (i = 0; i < num; i++) {
Thomas Petazzoni83544912016-12-21 11:28:49 +01005518 struct mvpp2_txq_pcpu_buf *tx_buf =
5519 txq_pcpu->buffs + txq_pcpu->txq_get_index;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005520
Antoine Tenart20920262017-10-23 15:24:30 +02005521 if (!IS_TSO_HEADER(txq_pcpu, tx_buf->dma))
5522 dma_unmap_single(port->dev->dev.parent, tx_buf->dma,
5523 tx_buf->size, DMA_TO_DEVICE);
Thomas Petazzoni36fb7432017-02-21 11:28:05 +01005524 if (tx_buf->skb)
5525 dev_kfree_skb_any(tx_buf->skb);
5526
5527 mvpp2_txq_inc_get(txq_pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005528 }
5529}
5530
5531static inline struct mvpp2_rx_queue *mvpp2_get_rx_queue(struct mvpp2_port *port,
5532 u32 cause)
5533{
5534 int queue = fls(cause) - 1;
5535
5536 return port->rxqs[queue];
5537}
5538
5539static inline struct mvpp2_tx_queue *mvpp2_get_tx_queue(struct mvpp2_port *port,
5540 u32 cause)
5541{
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005542 int queue = fls(cause) - 1;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005543
5544 return port->txqs[queue];
5545}
5546
5547/* Handle end of transmission */
5548static void mvpp2_txq_done(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
5549 struct mvpp2_txq_pcpu *txq_pcpu)
5550{
5551 struct netdev_queue *nq = netdev_get_tx_queue(port->dev, txq->log_id);
5552 int tx_done;
5553
5554 if (txq_pcpu->cpu != smp_processor_id())
5555 netdev_err(port->dev, "wrong cpu on the end of Tx processing\n");
5556
5557 tx_done = mvpp2_txq_sent_desc_proc(port, txq);
5558 if (!tx_done)
5559 return;
5560 mvpp2_txq_bufs_free(port, txq, txq_pcpu, tx_done);
5561
5562 txq_pcpu->count -= tx_done;
5563
5564 if (netif_tx_queue_stopped(nq))
Antoine Tenart1d17db02017-10-30 11:23:31 +01005565 if (txq_pcpu->count <= txq_pcpu->wake_threshold)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005566 netif_tx_wake_queue(nq);
5567}
5568
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005569static unsigned int mvpp2_tx_done(struct mvpp2_port *port, u32 cause,
5570 int cpu)
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005571{
5572 struct mvpp2_tx_queue *txq;
5573 struct mvpp2_txq_pcpu *txq_pcpu;
5574 unsigned int tx_todo = 0;
5575
5576 while (cause) {
5577 txq = mvpp2_get_tx_queue(port, cause);
5578 if (!txq)
5579 break;
5580
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005581 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005582
5583 if (txq_pcpu->count) {
5584 mvpp2_txq_done(port, txq, txq_pcpu);
5585 tx_todo += txq_pcpu->count;
5586 }
5587
5588 cause &= ~(1 << txq->log_id);
5589 }
5590 return tx_todo;
5591}
5592
Marcin Wojtas3f518502014-07-10 16:52:13 -03005593/* Rx/Tx queue initialization/cleanup methods */
5594
5595/* Allocate and initialize descriptors for aggr TXQ */
5596static int mvpp2_aggr_txq_init(struct platform_device *pdev,
Antoine Ténart85affd72017-08-23 09:46:55 +02005597 struct mvpp2_tx_queue *aggr_txq, int cpu,
Marcin Wojtas3f518502014-07-10 16:52:13 -03005598 struct mvpp2 *priv)
5599{
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005600 u32 txq_dma;
5601
Marcin Wojtas3f518502014-07-10 16:52:13 -03005602 /* Allocate memory for TX descriptors */
Yan Markmana154f8e2017-11-30 10:49:46 +01005603 aggr_txq->descs = dma_zalloc_coherent(&pdev->dev,
Antoine Ténart85affd72017-08-23 09:46:55 +02005604 MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005605 &aggr_txq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005606 if (!aggr_txq->descs)
5607 return -ENOMEM;
5608
Antoine Tenart02856a32017-10-30 11:23:32 +01005609 aggr_txq->last_desc = MVPP2_AGGR_TXQ_SIZE - 1;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005610
5611 /* Aggr TXQ no reset WA */
5612 aggr_txq->next_desc_to_proc = mvpp2_read(priv,
5613 MVPP2_AGGR_TXQ_INDEX_REG(cpu));
5614
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005615 /* Set Tx descriptors queue starting address indirect
5616 * access
5617 */
5618 if (priv->hw_version == MVPP21)
5619 txq_dma = aggr_txq->descs_dma;
5620 else
5621 txq_dma = aggr_txq->descs_dma >>
5622 MVPP22_AGGR_TXQ_DESC_ADDR_OFFS;
5623
5624 mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu), txq_dma);
Antoine Ténart85affd72017-08-23 09:46:55 +02005625 mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu),
5626 MVPP2_AGGR_TXQ_SIZE);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005627
5628 return 0;
5629}
5630
5631/* Create a specified Rx queue */
5632static int mvpp2_rxq_init(struct mvpp2_port *port,
5633 struct mvpp2_rx_queue *rxq)
5634
5635{
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005636 u32 rxq_dma;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005637 int cpu;
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005638
Marcin Wojtas3f518502014-07-10 16:52:13 -03005639 rxq->size = port->rx_ring_size;
5640
5641 /* Allocate memory for RX descriptors */
5642 rxq->descs = dma_alloc_coherent(port->dev->dev.parent,
5643 rxq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005644 &rxq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005645 if (!rxq->descs)
5646 return -ENOMEM;
5647
Marcin Wojtas3f518502014-07-10 16:52:13 -03005648 rxq->last_desc = rxq->size - 1;
5649
5650 /* Zero occupied and non-occupied counters - direct access */
5651 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
5652
5653 /* Set Rx descriptors queue starting address - indirect access */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005654 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005655 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005656 if (port->priv->hw_version == MVPP21)
5657 rxq_dma = rxq->descs_dma;
5658 else
5659 rxq_dma = rxq->descs_dma >> MVPP22_DESC_ADDR_OFFS;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005660 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_ADDR_REG, rxq_dma);
5661 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_SIZE_REG, rxq->size);
5662 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_INDEX_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005663 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005664
5665 /* Set Offset */
5666 mvpp2_rxq_offset_set(port, rxq->id, NET_SKB_PAD);
5667
5668 /* Set coalescing pkts and time */
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01005669 mvpp2_rx_pkts_coal_set(port, rxq);
5670 mvpp2_rx_time_coal_set(port, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005671
5672 /* Add number of descriptors ready for receiving packets */
5673 mvpp2_rxq_status_update(port, rxq->id, 0, rxq->size);
5674
5675 return 0;
5676}
5677
5678/* Push packets received by the RXQ to BM pool */
5679static void mvpp2_rxq_drop_pkts(struct mvpp2_port *port,
5680 struct mvpp2_rx_queue *rxq)
5681{
5682 int rx_received, i;
5683
5684 rx_received = mvpp2_rxq_received(port, rxq->id);
5685 if (!rx_received)
5686 return;
5687
5688 for (i = 0; i < rx_received; i++) {
5689 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005690 u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
5691 int pool;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005692
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005693 pool = (status & MVPP2_RXD_BM_POOL_ID_MASK) >>
5694 MVPP2_RXD_BM_POOL_ID_OFFS;
5695
Thomas Petazzoni7d7627b2017-06-22 14:23:20 +02005696 mvpp2_bm_pool_put(port, pool,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005697 mvpp2_rxdesc_dma_addr_get(port, rx_desc),
5698 mvpp2_rxdesc_cookie_get(port, rx_desc));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005699 }
5700 mvpp2_rxq_status_update(port, rxq->id, rx_received, rx_received);
5701}
5702
5703/* Cleanup Rx queue */
5704static void mvpp2_rxq_deinit(struct mvpp2_port *port,
5705 struct mvpp2_rx_queue *rxq)
5706{
Thomas Petazzonia7868412017-03-07 16:53:13 +01005707 int cpu;
5708
Marcin Wojtas3f518502014-07-10 16:52:13 -03005709 mvpp2_rxq_drop_pkts(port, rxq);
5710
5711 if (rxq->descs)
5712 dma_free_coherent(port->dev->dev.parent,
5713 rxq->size * MVPP2_DESC_ALIGNED_SIZE,
5714 rxq->descs,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005715 rxq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005716
5717 rxq->descs = NULL;
5718 rxq->last_desc = 0;
5719 rxq->next_desc_to_proc = 0;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005720 rxq->descs_dma = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005721
5722 /* Clear Rx descriptors queue starting address and size;
5723 * free descriptor number
5724 */
5725 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005726 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005727 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
5728 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_ADDR_REG, 0);
5729 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_SIZE_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005730 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005731}
5732
5733/* Create and initialize a Tx queue */
5734static int mvpp2_txq_init(struct mvpp2_port *port,
5735 struct mvpp2_tx_queue *txq)
5736{
5737 u32 val;
5738 int cpu, desc, desc_per_txq, tx_port_num;
5739 struct mvpp2_txq_pcpu *txq_pcpu;
5740
5741 txq->size = port->tx_ring_size;
5742
5743 /* Allocate memory for Tx descriptors */
5744 txq->descs = dma_alloc_coherent(port->dev->dev.parent,
5745 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005746 &txq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005747 if (!txq->descs)
5748 return -ENOMEM;
5749
Marcin Wojtas3f518502014-07-10 16:52:13 -03005750 txq->last_desc = txq->size - 1;
5751
5752 /* Set Tx descriptors queue starting address - indirect access */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005753 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005754 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5755 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_ADDR_REG,
5756 txq->descs_dma);
5757 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_SIZE_REG,
5758 txq->size & MVPP2_TXQ_DESC_SIZE_MASK);
5759 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_INDEX_REG, 0);
5760 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_RSVD_CLR_REG,
5761 txq->id << MVPP2_TXQ_RSVD_CLR_OFFSET);
5762 val = mvpp2_percpu_read(port->priv, cpu, MVPP2_TXQ_PENDING_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005763 val &= ~MVPP2_TXQ_PENDING_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005764 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PENDING_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005765
5766 /* Calculate base address in prefetch buffer. We reserve 16 descriptors
5767 * for each existing TXQ.
5768 * TCONTS for PON port must be continuous from 0 to MVPP2_MAX_TCONT
5769 * GBE ports assumed to be continious from 0 to MVPP2_MAX_PORTS
5770 */
5771 desc_per_txq = 16;
5772 desc = (port->id * MVPP2_MAX_TXQ * desc_per_txq) +
5773 (txq->log_id * desc_per_txq);
5774
Thomas Petazzonia7868412017-03-07 16:53:13 +01005775 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG,
5776 MVPP2_PREF_BUF_PTR(desc) | MVPP2_PREF_BUF_SIZE_16 |
5777 MVPP2_PREF_BUF_THRESH(desc_per_txq / 2));
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005778 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005779
5780 /* WRR / EJP configuration - indirect access */
5781 tx_port_num = mvpp2_egress_port(port);
5782 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
5783
5784 val = mvpp2_read(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id));
5785 val &= ~MVPP2_TXQ_REFILL_PERIOD_ALL_MASK;
5786 val |= MVPP2_TXQ_REFILL_PERIOD_MASK(1);
5787 val |= MVPP2_TXQ_REFILL_TOKENS_ALL_MASK;
5788 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id), val);
5789
5790 val = MVPP2_TXQ_TOKEN_SIZE_MAX;
5791 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq->log_id),
5792 val);
5793
5794 for_each_present_cpu(cpu) {
5795 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
5796 txq_pcpu->size = txq->size;
Markus Elfring02c91ec2017-04-17 08:09:07 +02005797 txq_pcpu->buffs = kmalloc_array(txq_pcpu->size,
5798 sizeof(*txq_pcpu->buffs),
5799 GFP_KERNEL);
Thomas Petazzoni83544912016-12-21 11:28:49 +01005800 if (!txq_pcpu->buffs)
Antoine Tenartba2d8d82017-11-28 14:19:48 +01005801 return -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005802
5803 txq_pcpu->count = 0;
5804 txq_pcpu->reserved_num = 0;
5805 txq_pcpu->txq_put_index = 0;
5806 txq_pcpu->txq_get_index = 0;
Antoine Tenartb70d4a52017-12-11 09:13:25 +01005807 txq_pcpu->tso_headers = NULL;
Antoine Ténart186cd4d2017-08-23 09:46:56 +02005808
Antoine Tenart1d17db02017-10-30 11:23:31 +01005809 txq_pcpu->stop_threshold = txq->size - MVPP2_MAX_SKB_DESCS;
5810 txq_pcpu->wake_threshold = txq_pcpu->stop_threshold / 2;
5811
Antoine Ténart186cd4d2017-08-23 09:46:56 +02005812 txq_pcpu->tso_headers =
5813 dma_alloc_coherent(port->dev->dev.parent,
Yan Markman822eaf72017-10-23 15:24:29 +02005814 txq_pcpu->size * TSO_HEADER_SIZE,
Antoine Ténart186cd4d2017-08-23 09:46:56 +02005815 &txq_pcpu->tso_headers_dma,
5816 GFP_KERNEL);
5817 if (!txq_pcpu->tso_headers)
Antoine Tenartba2d8d82017-11-28 14:19:48 +01005818 return -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005819 }
5820
5821 return 0;
5822}
5823
5824/* Free allocated TXQ resources */
5825static void mvpp2_txq_deinit(struct mvpp2_port *port,
5826 struct mvpp2_tx_queue *txq)
5827{
5828 struct mvpp2_txq_pcpu *txq_pcpu;
5829 int cpu;
5830
5831 for_each_present_cpu(cpu) {
5832 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Thomas Petazzoni83544912016-12-21 11:28:49 +01005833 kfree(txq_pcpu->buffs);
Antoine Ténart186cd4d2017-08-23 09:46:56 +02005834
Antoine Tenartb70d4a52017-12-11 09:13:25 +01005835 if (txq_pcpu->tso_headers)
5836 dma_free_coherent(port->dev->dev.parent,
5837 txq_pcpu->size * TSO_HEADER_SIZE,
5838 txq_pcpu->tso_headers,
5839 txq_pcpu->tso_headers_dma);
5840
5841 txq_pcpu->tso_headers = NULL;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005842 }
5843
5844 if (txq->descs)
5845 dma_free_coherent(port->dev->dev.parent,
5846 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005847 txq->descs, txq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005848
5849 txq->descs = NULL;
5850 txq->last_desc = 0;
5851 txq->next_desc_to_proc = 0;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005852 txq->descs_dma = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005853
5854 /* Set minimum bandwidth for disabled TXQs */
5855 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(txq->id), 0);
5856
5857 /* Set Tx descriptors queue starting address and size */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005858 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005859 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5860 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_ADDR_REG, 0);
5861 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_SIZE_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005862 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005863}
5864
5865/* Cleanup Tx ports */
5866static void mvpp2_txq_clean(struct mvpp2_port *port, struct mvpp2_tx_queue *txq)
5867{
5868 struct mvpp2_txq_pcpu *txq_pcpu;
5869 int delay, pending, cpu;
5870 u32 val;
5871
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005872 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005873 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5874 val = mvpp2_percpu_read(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005875 val |= MVPP2_TXQ_DRAIN_EN_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005876 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005877
5878 /* The napi queue has been stopped so wait for all packets
5879 * to be transmitted.
5880 */
5881 delay = 0;
5882 do {
5883 if (delay >= MVPP2_TX_PENDING_TIMEOUT_MSEC) {
5884 netdev_warn(port->dev,
5885 "port %d: cleaning queue %d timed out\n",
5886 port->id, txq->log_id);
5887 break;
5888 }
5889 mdelay(1);
5890 delay++;
5891
Thomas Petazzonia7868412017-03-07 16:53:13 +01005892 pending = mvpp2_percpu_read(port->priv, cpu,
5893 MVPP2_TXQ_PENDING_REG);
5894 pending &= MVPP2_TXQ_PENDING_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005895 } while (pending);
5896
5897 val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005898 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG, val);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005899 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005900
5901 for_each_present_cpu(cpu) {
5902 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
5903
5904 /* Release all packets */
5905 mvpp2_txq_bufs_free(port, txq, txq_pcpu, txq_pcpu->count);
5906
5907 /* Reset queue */
5908 txq_pcpu->count = 0;
5909 txq_pcpu->txq_put_index = 0;
5910 txq_pcpu->txq_get_index = 0;
5911 }
5912}
5913
5914/* Cleanup all Tx queues */
5915static void mvpp2_cleanup_txqs(struct mvpp2_port *port)
5916{
5917 struct mvpp2_tx_queue *txq;
5918 int queue;
5919 u32 val;
5920
5921 val = mvpp2_read(port->priv, MVPP2_TX_PORT_FLUSH_REG);
5922
5923 /* Reset Tx ports and delete Tx queues */
5924 val |= MVPP2_TX_PORT_FLUSH_MASK(port->id);
5925 mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
5926
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005927 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005928 txq = port->txqs[queue];
5929 mvpp2_txq_clean(port, txq);
5930 mvpp2_txq_deinit(port, txq);
5931 }
5932
5933 on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
5934
5935 val &= ~MVPP2_TX_PORT_FLUSH_MASK(port->id);
5936 mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
5937}
5938
5939/* Cleanup all Rx queues */
5940static void mvpp2_cleanup_rxqs(struct mvpp2_port *port)
5941{
5942 int queue;
5943
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005944 for (queue = 0; queue < port->nrxqs; queue++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005945 mvpp2_rxq_deinit(port, port->rxqs[queue]);
5946}
5947
5948/* Init all Rx queues for port */
5949static int mvpp2_setup_rxqs(struct mvpp2_port *port)
5950{
5951 int queue, err;
5952
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005953 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005954 err = mvpp2_rxq_init(port, port->rxqs[queue]);
5955 if (err)
5956 goto err_cleanup;
5957 }
5958 return 0;
5959
5960err_cleanup:
5961 mvpp2_cleanup_rxqs(port);
5962 return err;
5963}
5964
5965/* Init all tx queues for port */
5966static int mvpp2_setup_txqs(struct mvpp2_port *port)
5967{
5968 struct mvpp2_tx_queue *txq;
5969 int queue, err;
5970
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005971 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005972 txq = port->txqs[queue];
5973 err = mvpp2_txq_init(port, txq);
5974 if (err)
5975 goto err_cleanup;
5976 }
5977
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005978 if (port->has_tx_irqs) {
5979 mvpp2_tx_time_coal_set(port);
5980 for (queue = 0; queue < port->ntxqs; queue++) {
5981 txq = port->txqs[queue];
5982 mvpp2_tx_pkts_coal_set(port, txq);
5983 }
5984 }
5985
Marcin Wojtas3f518502014-07-10 16:52:13 -03005986 on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
5987 return 0;
5988
5989err_cleanup:
5990 mvpp2_cleanup_txqs(port);
5991 return err;
5992}
5993
5994/* The callback for per-port interrupt */
5995static irqreturn_t mvpp2_isr(int irq, void *dev_id)
5996{
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02005997 struct mvpp2_queue_vector *qv = dev_id;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005998
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02005999 mvpp2_qvec_interrupt_disable(qv);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006000
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006001 napi_schedule(&qv->napi);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006002
6003 return IRQ_HANDLED;
6004}
6005
Antoine Tenartfd3651b2017-09-01 11:04:54 +02006006/* Per-port interrupt for link status changes */
6007static irqreturn_t mvpp2_link_status_isr(int irq, void *dev_id)
6008{
6009 struct mvpp2_port *port = (struct mvpp2_port *)dev_id;
6010 struct net_device *dev = port->dev;
6011 bool event = false, link = false;
6012 u32 val;
6013
6014 mvpp22_gop_mask_irq(port);
6015
6016 if (port->gop_id == 0 &&
6017 port->phy_interface == PHY_INTERFACE_MODE_10GKR) {
6018 val = readl(port->base + MVPP22_XLG_INT_STAT);
6019 if (val & MVPP22_XLG_INT_STAT_LINK) {
6020 event = true;
6021 val = readl(port->base + MVPP22_XLG_STATUS);
6022 if (val & MVPP22_XLG_STATUS_LINK_UP)
6023 link = true;
6024 }
6025 } else if (phy_interface_mode_is_rgmii(port->phy_interface) ||
6026 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
6027 val = readl(port->base + MVPP22_GMAC_INT_STAT);
6028 if (val & MVPP22_GMAC_INT_STAT_LINK) {
6029 event = true;
6030 val = readl(port->base + MVPP2_GMAC_STATUS0);
6031 if (val & MVPP2_GMAC_STATUS0_LINK_UP)
6032 link = true;
6033 }
6034 }
6035
6036 if (!netif_running(dev) || !event)
6037 goto handled;
6038
6039 if (link) {
6040 mvpp2_interrupts_enable(port);
6041
6042 mvpp2_egress_enable(port);
6043 mvpp2_ingress_enable(port);
6044 netif_carrier_on(dev);
6045 netif_tx_wake_all_queues(dev);
6046 } else {
6047 netif_tx_stop_all_queues(dev);
6048 netif_carrier_off(dev);
6049 mvpp2_ingress_disable(port);
6050 mvpp2_egress_disable(port);
6051
6052 mvpp2_interrupts_disable(port);
6053 }
6054
6055handled:
6056 mvpp22_gop_unmask_irq(port);
6057 return IRQ_HANDLED;
6058}
6059
Antoine Tenart65a2c092017-08-30 10:29:18 +02006060static void mvpp2_gmac_set_autoneg(struct mvpp2_port *port,
6061 struct phy_device *phydev)
6062{
6063 u32 val;
6064
6065 if (port->phy_interface != PHY_INTERFACE_MODE_RGMII &&
6066 port->phy_interface != PHY_INTERFACE_MODE_RGMII_ID &&
6067 port->phy_interface != PHY_INTERFACE_MODE_RGMII_RXID &&
6068 port->phy_interface != PHY_INTERFACE_MODE_RGMII_TXID &&
6069 port->phy_interface != PHY_INTERFACE_MODE_SGMII)
6070 return;
6071
6072 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
6073 val &= ~(MVPP2_GMAC_CONFIG_MII_SPEED |
6074 MVPP2_GMAC_CONFIG_GMII_SPEED |
6075 MVPP2_GMAC_CONFIG_FULL_DUPLEX |
6076 MVPP2_GMAC_AN_SPEED_EN |
6077 MVPP2_GMAC_AN_DUPLEX_EN);
6078
6079 if (phydev->duplex)
6080 val |= MVPP2_GMAC_CONFIG_FULL_DUPLEX;
6081
6082 if (phydev->speed == SPEED_1000)
6083 val |= MVPP2_GMAC_CONFIG_GMII_SPEED;
6084 else if (phydev->speed == SPEED_100)
6085 val |= MVPP2_GMAC_CONFIG_MII_SPEED;
6086
6087 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
Antoine Tenart65a2c092017-08-30 10:29:18 +02006088}
6089
Marcin Wojtas3f518502014-07-10 16:52:13 -03006090/* Adjust link */
6091static void mvpp2_link_event(struct net_device *dev)
6092{
6093 struct mvpp2_port *port = netdev_priv(dev);
Philippe Reynes8e072692016-06-28 00:08:11 +02006094 struct phy_device *phydev = dev->phydev;
Antoine Tenart89273bc2017-08-30 10:29:19 +02006095 bool link_reconfigured = false;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006096 u32 val;
6097
6098 if (phydev->link) {
Antoine Tenart89273bc2017-08-30 10:29:19 +02006099 if (port->phy_interface != phydev->interface && port->comphy) {
6100 /* disable current port for reconfiguration */
6101 mvpp2_interrupts_disable(port);
6102 netif_carrier_off(port->dev);
6103 mvpp2_port_disable(port);
6104 phy_power_off(port->comphy);
6105
6106 /* comphy reconfiguration */
6107 port->phy_interface = phydev->interface;
6108 mvpp22_comphy_init(port);
6109
6110 /* gop/mac reconfiguration */
6111 mvpp22_gop_init(port);
6112 mvpp2_port_mii_set(port);
6113
6114 link_reconfigured = true;
6115 }
6116
Marcin Wojtas3f518502014-07-10 16:52:13 -03006117 if ((port->speed != phydev->speed) ||
6118 (port->duplex != phydev->duplex)) {
Antoine Tenart65a2c092017-08-30 10:29:18 +02006119 mvpp2_gmac_set_autoneg(port, phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006120
6121 port->duplex = phydev->duplex;
6122 port->speed = phydev->speed;
6123 }
6124 }
6125
Antoine Tenart89273bc2017-08-30 10:29:19 +02006126 if (phydev->link != port->link || link_reconfigured) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006127 port->link = phydev->link;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006128
Marcin Wojtas3f518502014-07-10 16:52:13 -03006129 if (phydev->link) {
Antoine Tenart65a2c092017-08-30 10:29:18 +02006130 if (port->phy_interface == PHY_INTERFACE_MODE_RGMII ||
6131 port->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
6132 port->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID ||
6133 port->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID ||
6134 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
6135 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
6136 val |= (MVPP2_GMAC_FORCE_LINK_PASS |
6137 MVPP2_GMAC_FORCE_LINK_DOWN);
6138 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
6139 }
Antoine Tenartf55744a2017-08-30 10:29:17 +02006140
6141 mvpp2_interrupts_enable(port);
6142 mvpp2_port_enable(port);
6143
Marcin Wojtas3f518502014-07-10 16:52:13 -03006144 mvpp2_egress_enable(port);
6145 mvpp2_ingress_enable(port);
Antoine Tenartf55744a2017-08-30 10:29:17 +02006146 netif_carrier_on(dev);
6147 netif_tx_wake_all_queues(dev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006148 } else {
Antoine Tenart968b2112017-08-30 10:29:16 +02006149 port->duplex = -1;
6150 port->speed = 0;
6151
Antoine Tenartf55744a2017-08-30 10:29:17 +02006152 netif_tx_stop_all_queues(dev);
6153 netif_carrier_off(dev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006154 mvpp2_ingress_disable(port);
6155 mvpp2_egress_disable(port);
Antoine Tenartf55744a2017-08-30 10:29:17 +02006156
6157 mvpp2_port_disable(port);
6158 mvpp2_interrupts_disable(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006159 }
Antoine Tenart968b2112017-08-30 10:29:16 +02006160
Marcin Wojtas3f518502014-07-10 16:52:13 -03006161 phy_print_status(phydev);
6162 }
6163}
6164
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006165static void mvpp2_timer_set(struct mvpp2_port_pcpu *port_pcpu)
6166{
6167 ktime_t interval;
6168
6169 if (!port_pcpu->timer_scheduled) {
6170 port_pcpu->timer_scheduled = true;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01006171 interval = MVPP2_TXDONE_HRTIMER_PERIOD_NS;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006172 hrtimer_start(&port_pcpu->tx_done_timer, interval,
6173 HRTIMER_MODE_REL_PINNED);
6174 }
6175}
6176
6177static void mvpp2_tx_proc_cb(unsigned long data)
6178{
6179 struct net_device *dev = (struct net_device *)data;
6180 struct mvpp2_port *port = netdev_priv(dev);
6181 struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
6182 unsigned int tx_todo, cause;
6183
6184 if (!netif_running(dev))
6185 return;
6186 port_pcpu->timer_scheduled = false;
6187
6188 /* Process all the Tx queues */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006189 cause = (1 << port->ntxqs) - 1;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006190 tx_todo = mvpp2_tx_done(port, cause, smp_processor_id());
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006191
6192 /* Set the timer in case not all the packets were processed */
6193 if (tx_todo)
6194 mvpp2_timer_set(port_pcpu);
6195}
6196
6197static enum hrtimer_restart mvpp2_hr_timer_cb(struct hrtimer *timer)
6198{
6199 struct mvpp2_port_pcpu *port_pcpu = container_of(timer,
6200 struct mvpp2_port_pcpu,
6201 tx_done_timer);
6202
6203 tasklet_schedule(&port_pcpu->tx_done_tasklet);
6204
6205 return HRTIMER_NORESTART;
6206}
6207
Marcin Wojtas3f518502014-07-10 16:52:13 -03006208/* Main RX/TX processing routines */
6209
6210/* Display more error info */
6211static void mvpp2_rx_error(struct mvpp2_port *port,
6212 struct mvpp2_rx_desc *rx_desc)
6213{
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006214 u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
6215 size_t sz = mvpp2_rxdesc_size_get(port, rx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006216
6217 switch (status & MVPP2_RXD_ERR_CODE_MASK) {
6218 case MVPP2_RXD_ERR_CRC:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006219 netdev_err(port->dev, "bad rx status %08x (crc error), size=%zu\n",
6220 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006221 break;
6222 case MVPP2_RXD_ERR_OVERRUN:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006223 netdev_err(port->dev, "bad rx status %08x (overrun error), size=%zu\n",
6224 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006225 break;
6226 case MVPP2_RXD_ERR_RESOURCE:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006227 netdev_err(port->dev, "bad rx status %08x (resource error), size=%zu\n",
6228 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006229 break;
6230 }
6231}
6232
6233/* Handle RX checksum offload */
6234static void mvpp2_rx_csum(struct mvpp2_port *port, u32 status,
6235 struct sk_buff *skb)
6236{
6237 if (((status & MVPP2_RXD_L3_IP4) &&
6238 !(status & MVPP2_RXD_IP4_HEADER_ERR)) ||
6239 (status & MVPP2_RXD_L3_IP6))
6240 if (((status & MVPP2_RXD_L4_UDP) ||
6241 (status & MVPP2_RXD_L4_TCP)) &&
6242 (status & MVPP2_RXD_L4_CSUM_OK)) {
6243 skb->csum = 0;
6244 skb->ip_summed = CHECKSUM_UNNECESSARY;
6245 return;
6246 }
6247
6248 skb->ip_summed = CHECKSUM_NONE;
6249}
6250
6251/* Reuse skb if possible, or allocate a new skb and add it to BM pool */
6252static int mvpp2_rx_refill(struct mvpp2_port *port,
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006253 struct mvpp2_bm_pool *bm_pool, int pool)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006254{
Thomas Petazzoni20396132017-03-07 16:53:00 +01006255 dma_addr_t dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01006256 phys_addr_t phys_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006257 void *buf;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006258
Marcin Wojtas3f518502014-07-10 16:52:13 -03006259 /* No recycle or too many buffers are in use, so allocate a new skb */
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01006260 buf = mvpp2_buf_alloc(port, bm_pool, &dma_addr, &phys_addr,
6261 GFP_ATOMIC);
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006262 if (!buf)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006263 return -ENOMEM;
6264
Thomas Petazzoni7d7627b2017-06-22 14:23:20 +02006265 mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
Thomas Petazzoni7ef7e1d2017-02-21 11:28:07 +01006266
Marcin Wojtas3f518502014-07-10 16:52:13 -03006267 return 0;
6268}
6269
6270/* Handle tx checksum */
6271static u32 mvpp2_skb_tx_csum(struct mvpp2_port *port, struct sk_buff *skb)
6272{
6273 if (skb->ip_summed == CHECKSUM_PARTIAL) {
6274 int ip_hdr_len = 0;
6275 u8 l4_proto;
6276
6277 if (skb->protocol == htons(ETH_P_IP)) {
6278 struct iphdr *ip4h = ip_hdr(skb);
6279
6280 /* Calculate IPv4 checksum and L4 checksum */
6281 ip_hdr_len = ip4h->ihl;
6282 l4_proto = ip4h->protocol;
6283 } else if (skb->protocol == htons(ETH_P_IPV6)) {
6284 struct ipv6hdr *ip6h = ipv6_hdr(skb);
6285
6286 /* Read l4_protocol from one of IPv6 extra headers */
6287 if (skb_network_header_len(skb) > 0)
6288 ip_hdr_len = (skb_network_header_len(skb) >> 2);
6289 l4_proto = ip6h->nexthdr;
6290 } else {
6291 return MVPP2_TXD_L4_CSUM_NOT;
6292 }
6293
6294 return mvpp2_txq_desc_csum(skb_network_offset(skb),
6295 skb->protocol, ip_hdr_len, l4_proto);
6296 }
6297
6298 return MVPP2_TXD_L4_CSUM_NOT | MVPP2_TXD_IP_CSUM_DISABLE;
6299}
6300
Marcin Wojtas3f518502014-07-10 16:52:13 -03006301/* Main rx processing */
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006302static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi,
6303 int rx_todo, struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006304{
6305 struct net_device *dev = port->dev;
Marcin Wojtasb5015852015-12-03 15:20:51 +01006306 int rx_received;
6307 int rx_done = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006308 u32 rcvd_pkts = 0;
6309 u32 rcvd_bytes = 0;
6310
6311 /* Get number of received packets and clamp the to-do */
6312 rx_received = mvpp2_rxq_received(port, rxq->id);
6313 if (rx_todo > rx_received)
6314 rx_todo = rx_received;
6315
Marcin Wojtasb5015852015-12-03 15:20:51 +01006316 while (rx_done < rx_todo) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006317 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
6318 struct mvpp2_bm_pool *bm_pool;
6319 struct sk_buff *skb;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006320 unsigned int frag_size;
Thomas Petazzoni20396132017-03-07 16:53:00 +01006321 dma_addr_t dma_addr;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006322 phys_addr_t phys_addr;
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006323 u32 rx_status;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006324 int pool, rx_bytes, err;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006325 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006326
Marcin Wojtasb5015852015-12-03 15:20:51 +01006327 rx_done++;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006328 rx_status = mvpp2_rxdesc_status_get(port, rx_desc);
6329 rx_bytes = mvpp2_rxdesc_size_get(port, rx_desc);
6330 rx_bytes -= MVPP2_MH_SIZE;
6331 dma_addr = mvpp2_rxdesc_dma_addr_get(port, rx_desc);
6332 phys_addr = mvpp2_rxdesc_cookie_get(port, rx_desc);
6333 data = (void *)phys_to_virt(phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006334
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006335 pool = (rx_status & MVPP2_RXD_BM_POOL_ID_MASK) >>
6336 MVPP2_RXD_BM_POOL_ID_OFFS;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006337 bm_pool = &port->priv->bm_pools[pool];
Marcin Wojtas3f518502014-07-10 16:52:13 -03006338
6339 /* In case of an error, release the requested buffer pointer
6340 * to the Buffer Manager. This request process is controlled
6341 * by the hardware, and the information about the buffer is
6342 * comprised by the RX descriptor.
6343 */
6344 if (rx_status & MVPP2_RXD_ERR_SUMMARY) {
Markus Elfring8a524882017-04-17 10:52:02 +02006345err_drop_frame:
Marcin Wojtas3f518502014-07-10 16:52:13 -03006346 dev->stats.rx_errors++;
6347 mvpp2_rx_error(port, rx_desc);
Marcin Wojtasb5015852015-12-03 15:20:51 +01006348 /* Return the buffer to the pool */
Thomas Petazzoni7d7627b2017-06-22 14:23:20 +02006349 mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006350 continue;
6351 }
6352
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006353 if (bm_pool->frag_size > PAGE_SIZE)
6354 frag_size = 0;
6355 else
6356 frag_size = bm_pool->frag_size;
6357
6358 skb = build_skb(data, frag_size);
6359 if (!skb) {
6360 netdev_warn(port->dev, "skb build failed\n");
6361 goto err_drop_frame;
6362 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006363
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006364 err = mvpp2_rx_refill(port, bm_pool, pool);
Marcin Wojtasb5015852015-12-03 15:20:51 +01006365 if (err) {
6366 netdev_err(port->dev, "failed to refill BM pools\n");
6367 goto err_drop_frame;
6368 }
6369
Thomas Petazzoni20396132017-03-07 16:53:00 +01006370 dma_unmap_single(dev->dev.parent, dma_addr,
Marcin Wojtas4229d502015-12-03 15:20:50 +01006371 bm_pool->buf_size, DMA_FROM_DEVICE);
6372
Marcin Wojtas3f518502014-07-10 16:52:13 -03006373 rcvd_pkts++;
6374 rcvd_bytes += rx_bytes;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006375
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006376 skb_reserve(skb, MVPP2_MH_SIZE + NET_SKB_PAD);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006377 skb_put(skb, rx_bytes);
6378 skb->protocol = eth_type_trans(skb, dev);
6379 mvpp2_rx_csum(port, rx_status, skb);
6380
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006381 napi_gro_receive(napi, skb);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006382 }
6383
6384 if (rcvd_pkts) {
6385 struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
6386
6387 u64_stats_update_begin(&stats->syncp);
6388 stats->rx_packets += rcvd_pkts;
6389 stats->rx_bytes += rcvd_bytes;
6390 u64_stats_update_end(&stats->syncp);
6391 }
6392
6393 /* Update Rx queue management counters */
6394 wmb();
Marcin Wojtasb5015852015-12-03 15:20:51 +01006395 mvpp2_rxq_status_update(port, rxq->id, rx_done, rx_done);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006396
6397 return rx_todo;
6398}
6399
6400static inline void
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006401tx_desc_unmap_put(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006402 struct mvpp2_tx_desc *desc)
6403{
Antoine Tenart20920262017-10-23 15:24:30 +02006404 struct mvpp2_txq_pcpu *txq_pcpu = this_cpu_ptr(txq->pcpu);
6405
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006406 dma_addr_t buf_dma_addr =
6407 mvpp2_txdesc_dma_addr_get(port, desc);
6408 size_t buf_sz =
6409 mvpp2_txdesc_size_get(port, desc);
Antoine Tenart20920262017-10-23 15:24:30 +02006410 if (!IS_TSO_HEADER(txq_pcpu, buf_dma_addr))
6411 dma_unmap_single(port->dev->dev.parent, buf_dma_addr,
6412 buf_sz, DMA_TO_DEVICE);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006413 mvpp2_txq_desc_put(txq);
6414}
6415
6416/* Handle tx fragmentation processing */
6417static int mvpp2_tx_frag_process(struct mvpp2_port *port, struct sk_buff *skb,
6418 struct mvpp2_tx_queue *aggr_txq,
6419 struct mvpp2_tx_queue *txq)
6420{
6421 struct mvpp2_txq_pcpu *txq_pcpu = this_cpu_ptr(txq->pcpu);
6422 struct mvpp2_tx_desc *tx_desc;
6423 int i;
Thomas Petazzoni20396132017-03-07 16:53:00 +01006424 dma_addr_t buf_dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006425
6426 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
6427 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6428 void *addr = page_address(frag->page.p) + frag->page_offset;
6429
6430 tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006431 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6432 mvpp2_txdesc_size_set(port, tx_desc, frag->size);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006433
Thomas Petazzoni20396132017-03-07 16:53:00 +01006434 buf_dma_addr = dma_map_single(port->dev->dev.parent, addr,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006435 frag->size,
6436 DMA_TO_DEVICE);
Thomas Petazzoni20396132017-03-07 16:53:00 +01006437 if (dma_mapping_error(port->dev->dev.parent, buf_dma_addr)) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006438 mvpp2_txq_desc_put(txq);
Markus Elfring32bae632017-04-17 11:36:34 +02006439 goto cleanup;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006440 }
6441
Antoine Tenart6eb5d372017-10-30 11:23:33 +01006442 mvpp2_txdesc_dma_addr_set(port, tx_desc, buf_dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006443
6444 if (i == (skb_shinfo(skb)->nr_frags - 1)) {
6445 /* Last descriptor */
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006446 mvpp2_txdesc_cmd_set(port, tx_desc,
6447 MVPP2_TXD_L_DESC);
6448 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006449 } else {
6450 /* Descriptor in the middle: Not First, Not Last */
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006451 mvpp2_txdesc_cmd_set(port, tx_desc, 0);
6452 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006453 }
6454 }
6455
6456 return 0;
Markus Elfring32bae632017-04-17 11:36:34 +02006457cleanup:
Marcin Wojtas3f518502014-07-10 16:52:13 -03006458 /* Release all descriptors that were used to map fragments of
6459 * this packet, as well as the corresponding DMA mappings
6460 */
6461 for (i = i - 1; i >= 0; i--) {
6462 tx_desc = txq->descs + i;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006463 tx_desc_unmap_put(port, txq, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006464 }
6465
6466 return -ENOMEM;
6467}
6468
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006469static inline void mvpp2_tso_put_hdr(struct sk_buff *skb,
6470 struct net_device *dev,
6471 struct mvpp2_tx_queue *txq,
6472 struct mvpp2_tx_queue *aggr_txq,
6473 struct mvpp2_txq_pcpu *txq_pcpu,
6474 int hdr_sz)
6475{
6476 struct mvpp2_port *port = netdev_priv(dev);
6477 struct mvpp2_tx_desc *tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
6478 dma_addr_t addr;
6479
6480 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6481 mvpp2_txdesc_size_set(port, tx_desc, hdr_sz);
6482
6483 addr = txq_pcpu->tso_headers_dma +
6484 txq_pcpu->txq_put_index * TSO_HEADER_SIZE;
Antoine Tenart6eb5d372017-10-30 11:23:33 +01006485 mvpp2_txdesc_dma_addr_set(port, tx_desc, addr);
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006486
6487 mvpp2_txdesc_cmd_set(port, tx_desc, mvpp2_skb_tx_csum(port, skb) |
6488 MVPP2_TXD_F_DESC |
6489 MVPP2_TXD_PADDING_DISABLE);
6490 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
6491}
6492
6493static inline int mvpp2_tso_put_data(struct sk_buff *skb,
6494 struct net_device *dev, struct tso_t *tso,
6495 struct mvpp2_tx_queue *txq,
6496 struct mvpp2_tx_queue *aggr_txq,
6497 struct mvpp2_txq_pcpu *txq_pcpu,
6498 int sz, bool left, bool last)
6499{
6500 struct mvpp2_port *port = netdev_priv(dev);
6501 struct mvpp2_tx_desc *tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
6502 dma_addr_t buf_dma_addr;
6503
6504 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6505 mvpp2_txdesc_size_set(port, tx_desc, sz);
6506
6507 buf_dma_addr = dma_map_single(dev->dev.parent, tso->data, sz,
6508 DMA_TO_DEVICE);
6509 if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
6510 mvpp2_txq_desc_put(txq);
6511 return -ENOMEM;
6512 }
6513
Antoine Tenart6eb5d372017-10-30 11:23:33 +01006514 mvpp2_txdesc_dma_addr_set(port, tx_desc, buf_dma_addr);
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006515
6516 if (!left) {
6517 mvpp2_txdesc_cmd_set(port, tx_desc, MVPP2_TXD_L_DESC);
6518 if (last) {
6519 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
6520 return 0;
6521 }
6522 } else {
6523 mvpp2_txdesc_cmd_set(port, tx_desc, 0);
6524 }
6525
6526 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
6527 return 0;
6528}
6529
6530static int mvpp2_tx_tso(struct sk_buff *skb, struct net_device *dev,
6531 struct mvpp2_tx_queue *txq,
6532 struct mvpp2_tx_queue *aggr_txq,
6533 struct mvpp2_txq_pcpu *txq_pcpu)
6534{
6535 struct mvpp2_port *port = netdev_priv(dev);
6536 struct tso_t tso;
6537 int hdr_sz = skb_transport_offset(skb) + tcp_hdrlen(skb);
6538 int i, len, descs = 0;
6539
6540 /* Check number of available descriptors */
6541 if (mvpp2_aggr_desc_num_check(port->priv, aggr_txq,
6542 tso_count_descs(skb)) ||
6543 mvpp2_txq_reserved_desc_num_proc(port->priv, txq, txq_pcpu,
6544 tso_count_descs(skb)))
6545 return 0;
6546
6547 tso_start(skb, &tso);
6548 len = skb->len - hdr_sz;
6549 while (len > 0) {
6550 int left = min_t(int, skb_shinfo(skb)->gso_size, len);
6551 char *hdr = txq_pcpu->tso_headers +
6552 txq_pcpu->txq_put_index * TSO_HEADER_SIZE;
6553
6554 len -= left;
6555 descs++;
6556
6557 tso_build_hdr(skb, hdr, &tso, left, len == 0);
6558 mvpp2_tso_put_hdr(skb, dev, txq, aggr_txq, txq_pcpu, hdr_sz);
6559
6560 while (left > 0) {
6561 int sz = min_t(int, tso.size, left);
6562 left -= sz;
6563 descs++;
6564
6565 if (mvpp2_tso_put_data(skb, dev, &tso, txq, aggr_txq,
6566 txq_pcpu, sz, left, len == 0))
6567 goto release;
6568 tso_build_data(skb, &tso, sz);
6569 }
6570 }
6571
6572 return descs;
6573
6574release:
6575 for (i = descs - 1; i >= 0; i--) {
6576 struct mvpp2_tx_desc *tx_desc = txq->descs + i;
6577 tx_desc_unmap_put(port, txq, tx_desc);
6578 }
6579 return 0;
6580}
6581
Marcin Wojtas3f518502014-07-10 16:52:13 -03006582/* Main tx processing */
6583static int mvpp2_tx(struct sk_buff *skb, struct net_device *dev)
6584{
6585 struct mvpp2_port *port = netdev_priv(dev);
6586 struct mvpp2_tx_queue *txq, *aggr_txq;
6587 struct mvpp2_txq_pcpu *txq_pcpu;
6588 struct mvpp2_tx_desc *tx_desc;
Thomas Petazzoni20396132017-03-07 16:53:00 +01006589 dma_addr_t buf_dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006590 int frags = 0;
6591 u16 txq_id;
6592 u32 tx_cmd;
6593
6594 txq_id = skb_get_queue_mapping(skb);
6595 txq = port->txqs[txq_id];
6596 txq_pcpu = this_cpu_ptr(txq->pcpu);
6597 aggr_txq = &port->priv->aggr_txqs[smp_processor_id()];
6598
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006599 if (skb_is_gso(skb)) {
6600 frags = mvpp2_tx_tso(skb, dev, txq, aggr_txq, txq_pcpu);
6601 goto out;
6602 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006603 frags = skb_shinfo(skb)->nr_frags + 1;
6604
6605 /* Check number of available descriptors */
6606 if (mvpp2_aggr_desc_num_check(port->priv, aggr_txq, frags) ||
6607 mvpp2_txq_reserved_desc_num_proc(port->priv, txq,
6608 txq_pcpu, frags)) {
6609 frags = 0;
6610 goto out;
6611 }
6612
6613 /* Get a descriptor for the first part of the packet */
6614 tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006615 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6616 mvpp2_txdesc_size_set(port, tx_desc, skb_headlen(skb));
Marcin Wojtas3f518502014-07-10 16:52:13 -03006617
Thomas Petazzoni20396132017-03-07 16:53:00 +01006618 buf_dma_addr = dma_map_single(dev->dev.parent, skb->data,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006619 skb_headlen(skb), DMA_TO_DEVICE);
Thomas Petazzoni20396132017-03-07 16:53:00 +01006620 if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006621 mvpp2_txq_desc_put(txq);
6622 frags = 0;
6623 goto out;
6624 }
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006625
Antoine Tenart6eb5d372017-10-30 11:23:33 +01006626 mvpp2_txdesc_dma_addr_set(port, tx_desc, buf_dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006627
6628 tx_cmd = mvpp2_skb_tx_csum(port, skb);
6629
6630 if (frags == 1) {
6631 /* First and Last descriptor */
6632 tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_L_DESC;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006633 mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
6634 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006635 } else {
6636 /* First but not Last */
6637 tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_PADDING_DISABLE;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006638 mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
6639 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006640
6641 /* Continue with other skb fragments */
6642 if (mvpp2_tx_frag_process(port, skb, aggr_txq, txq)) {
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006643 tx_desc_unmap_put(port, txq, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006644 frags = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006645 }
6646 }
6647
Marcin Wojtas3f518502014-07-10 16:52:13 -03006648out:
6649 if (frags > 0) {
6650 struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006651 struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
6652
6653 txq_pcpu->reserved_num -= frags;
6654 txq_pcpu->count += frags;
6655 aggr_txq->count += frags;
6656
6657 /* Enable transmit */
6658 wmb();
6659 mvpp2_aggr_txq_pend_desc_add(port, frags);
6660
Antoine Tenart1d17db02017-10-30 11:23:31 +01006661 if (txq_pcpu->count >= txq_pcpu->stop_threshold)
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006662 netif_tx_stop_queue(nq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006663
6664 u64_stats_update_begin(&stats->syncp);
6665 stats->tx_packets++;
6666 stats->tx_bytes += skb->len;
6667 u64_stats_update_end(&stats->syncp);
6668 } else {
6669 dev->stats.tx_dropped++;
6670 dev_kfree_skb_any(skb);
6671 }
6672
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006673 /* Finalize TX processing */
Antoine Tenart082297e2017-10-23 15:24:31 +02006674 if (!port->has_tx_irqs && txq_pcpu->count >= txq->done_pkts_coal)
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006675 mvpp2_txq_done(port, txq, txq_pcpu);
6676
6677 /* Set the timer in case not all frags were processed */
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006678 if (!port->has_tx_irqs && txq_pcpu->count <= frags &&
6679 txq_pcpu->count > 0) {
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006680 struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
6681
6682 mvpp2_timer_set(port_pcpu);
6683 }
6684
Marcin Wojtas3f518502014-07-10 16:52:13 -03006685 return NETDEV_TX_OK;
6686}
6687
6688static inline void mvpp2_cause_error(struct net_device *dev, int cause)
6689{
6690 if (cause & MVPP2_CAUSE_FCS_ERR_MASK)
6691 netdev_err(dev, "FCS error\n");
6692 if (cause & MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK)
6693 netdev_err(dev, "rx fifo overrun error\n");
6694 if (cause & MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK)
6695 netdev_err(dev, "tx fifo underrun error\n");
6696}
6697
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006698static int mvpp2_poll(struct napi_struct *napi, int budget)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006699{
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006700 u32 cause_rx_tx, cause_rx, cause_tx, cause_misc;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006701 int rx_done = 0;
6702 struct mvpp2_port *port = netdev_priv(napi->dev);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006703 struct mvpp2_queue_vector *qv;
Thomas Petazzonia7868412017-03-07 16:53:13 +01006704 int cpu = smp_processor_id();
Marcin Wojtas3f518502014-07-10 16:52:13 -03006705
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006706 qv = container_of(napi, struct mvpp2_queue_vector, napi);
6707
Marcin Wojtas3f518502014-07-10 16:52:13 -03006708 /* Rx/Tx cause register
6709 *
6710 * Bits 0-15: each bit indicates received packets on the Rx queue
6711 * (bit 0 is for Rx queue 0).
6712 *
6713 * Bits 16-23: each bit indicates transmitted packets on the Tx queue
6714 * (bit 16 is for Tx queue 0).
6715 *
6716 * Each CPU has its own Rx/Tx cause register
6717 */
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006718 cause_rx_tx = mvpp2_percpu_read(port->priv, qv->sw_thread_id,
Thomas Petazzonia7868412017-03-07 16:53:13 +01006719 MVPP2_ISR_RX_TX_CAUSE_REG(port->id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03006720
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006721 cause_misc = cause_rx_tx & MVPP2_CAUSE_MISC_SUM_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006722 if (cause_misc) {
6723 mvpp2_cause_error(port->dev, cause_misc);
6724
6725 /* Clear the cause register */
6726 mvpp2_write(port->priv, MVPP2_ISR_MISC_CAUSE_REG, 0);
Thomas Petazzonia7868412017-03-07 16:53:13 +01006727 mvpp2_percpu_write(port->priv, cpu,
6728 MVPP2_ISR_RX_TX_CAUSE_REG(port->id),
6729 cause_rx_tx & ~MVPP2_CAUSE_MISC_SUM_MASK);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006730 }
6731
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006732 cause_tx = cause_rx_tx & MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
6733 if (cause_tx) {
6734 cause_tx >>= MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_OFFSET;
6735 mvpp2_tx_done(port, cause_tx, qv->sw_thread_id);
6736 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006737
6738 /* Process RX packets */
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006739 cause_rx = cause_rx_tx & MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
6740 cause_rx <<= qv->first_rxq;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006741 cause_rx |= qv->pending_cause_rx;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006742 while (cause_rx && budget > 0) {
6743 int count;
6744 struct mvpp2_rx_queue *rxq;
6745
6746 rxq = mvpp2_get_rx_queue(port, cause_rx);
6747 if (!rxq)
6748 break;
6749
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006750 count = mvpp2_rx(port, napi, budget, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006751 rx_done += count;
6752 budget -= count;
6753 if (budget > 0) {
6754 /* Clear the bit associated to this Rx queue
6755 * so that next iteration will continue from
6756 * the next Rx queue.
6757 */
6758 cause_rx &= ~(1 << rxq->logic_rxq);
6759 }
6760 }
6761
6762 if (budget > 0) {
6763 cause_rx = 0;
Eric Dumazet6ad20162017-01-30 08:22:01 -08006764 napi_complete_done(napi, rx_done);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006765
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006766 mvpp2_qvec_interrupt_enable(qv);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006767 }
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006768 qv->pending_cause_rx = cause_rx;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006769 return rx_done;
6770}
6771
6772/* Set hw internals when starting port */
6773static void mvpp2_start_dev(struct mvpp2_port *port)
6774{
Philippe Reynes8e072692016-06-28 00:08:11 +02006775 struct net_device *ndev = port->dev;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006776 int i;
Philippe Reynes8e072692016-06-28 00:08:11 +02006777
Stefan Chulski76eb1b12017-08-22 19:08:26 +02006778 if (port->gop_id == 0 &&
6779 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
6780 port->phy_interface == PHY_INTERFACE_MODE_10GKR))
6781 mvpp2_xlg_max_rx_size_set(port);
6782 else
6783 mvpp2_gmac_max_rx_size_set(port);
6784
Marcin Wojtas3f518502014-07-10 16:52:13 -03006785 mvpp2_txp_max_tx_size_set(port);
6786
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006787 for (i = 0; i < port->nqvecs; i++)
6788 napi_enable(&port->qvecs[i].napi);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006789
6790 /* Enable interrupts on all CPUs */
6791 mvpp2_interrupts_enable(port);
6792
Antoine Tenart542897d2017-08-30 10:29:15 +02006793 if (port->priv->hw_version == MVPP22) {
6794 mvpp22_comphy_init(port);
Antoine Ténartf84bf382017-08-22 19:08:27 +02006795 mvpp22_gop_init(port);
Antoine Tenart542897d2017-08-30 10:29:15 +02006796 }
Antoine Ténartf84bf382017-08-22 19:08:27 +02006797
Antoine Ténart2055d622017-08-22 19:08:23 +02006798 mvpp2_port_mii_set(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006799 mvpp2_port_enable(port);
Antoine Tenart5997c862017-09-01 11:04:53 +02006800 if (ndev->phydev)
6801 phy_start(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006802 netif_tx_start_all_queues(port->dev);
6803}
6804
6805/* Set hw internals when stopping port */
6806static void mvpp2_stop_dev(struct mvpp2_port *port)
6807{
Philippe Reynes8e072692016-06-28 00:08:11 +02006808 struct net_device *ndev = port->dev;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006809 int i;
Philippe Reynes8e072692016-06-28 00:08:11 +02006810
Marcin Wojtas3f518502014-07-10 16:52:13 -03006811 /* Stop new packets from arriving to RXQs */
6812 mvpp2_ingress_disable(port);
6813
6814 mdelay(10);
6815
6816 /* Disable interrupts on all CPUs */
6817 mvpp2_interrupts_disable(port);
6818
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006819 for (i = 0; i < port->nqvecs; i++)
6820 napi_disable(&port->qvecs[i].napi);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006821
6822 netif_carrier_off(port->dev);
6823 netif_tx_stop_all_queues(port->dev);
6824
6825 mvpp2_egress_disable(port);
6826 mvpp2_port_disable(port);
Antoine Tenart5997c862017-09-01 11:04:53 +02006827 if (ndev->phydev)
6828 phy_stop(ndev->phydev);
Antoine Tenart542897d2017-08-30 10:29:15 +02006829 phy_power_off(port->comphy);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006830}
6831
Marcin Wojtas3f518502014-07-10 16:52:13 -03006832static int mvpp2_check_ringparam_valid(struct net_device *dev,
6833 struct ethtool_ringparam *ring)
6834{
6835 u16 new_rx_pending = ring->rx_pending;
6836 u16 new_tx_pending = ring->tx_pending;
6837
6838 if (ring->rx_pending == 0 || ring->tx_pending == 0)
6839 return -EINVAL;
6840
Yan Markman7cf87e42017-12-11 09:13:26 +01006841 if (ring->rx_pending > MVPP2_MAX_RXD_MAX)
6842 new_rx_pending = MVPP2_MAX_RXD_MAX;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006843 else if (!IS_ALIGNED(ring->rx_pending, 16))
6844 new_rx_pending = ALIGN(ring->rx_pending, 16);
6845
Yan Markman7cf87e42017-12-11 09:13:26 +01006846 if (ring->tx_pending > MVPP2_MAX_TXD_MAX)
6847 new_tx_pending = MVPP2_MAX_TXD_MAX;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006848 else if (!IS_ALIGNED(ring->tx_pending, 32))
6849 new_tx_pending = ALIGN(ring->tx_pending, 32);
6850
Antoine Tenart76e583c2017-11-28 14:19:51 +01006851 /* The Tx ring size cannot be smaller than the minimum number of
6852 * descriptors needed for TSO.
6853 */
6854 if (new_tx_pending < MVPP2_MAX_SKB_DESCS)
6855 new_tx_pending = ALIGN(MVPP2_MAX_SKB_DESCS, 32);
6856
Marcin Wojtas3f518502014-07-10 16:52:13 -03006857 if (ring->rx_pending != new_rx_pending) {
6858 netdev_info(dev, "illegal Rx ring size value %d, round to %d\n",
6859 ring->rx_pending, new_rx_pending);
6860 ring->rx_pending = new_rx_pending;
6861 }
6862
6863 if (ring->tx_pending != new_tx_pending) {
6864 netdev_info(dev, "illegal Tx ring size value %d, round to %d\n",
6865 ring->tx_pending, new_tx_pending);
6866 ring->tx_pending = new_tx_pending;
6867 }
6868
6869 return 0;
6870}
6871
Thomas Petazzoni26975822017-03-07 16:53:14 +01006872static void mvpp21_get_mac_address(struct mvpp2_port *port, unsigned char *addr)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006873{
6874 u32 mac_addr_l, mac_addr_m, mac_addr_h;
6875
6876 mac_addr_l = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
6877 mac_addr_m = readl(port->priv->lms_base + MVPP2_SRC_ADDR_MIDDLE);
6878 mac_addr_h = readl(port->priv->lms_base + MVPP2_SRC_ADDR_HIGH);
6879 addr[0] = (mac_addr_h >> 24) & 0xFF;
6880 addr[1] = (mac_addr_h >> 16) & 0xFF;
6881 addr[2] = (mac_addr_h >> 8) & 0xFF;
6882 addr[3] = mac_addr_h & 0xFF;
6883 addr[4] = mac_addr_m & 0xFF;
6884 addr[5] = (mac_addr_l >> MVPP2_GMAC_SA_LOW_OFFS) & 0xFF;
6885}
6886
6887static int mvpp2_phy_connect(struct mvpp2_port *port)
6888{
6889 struct phy_device *phy_dev;
6890
Antoine Tenart5997c862017-09-01 11:04:53 +02006891 /* No PHY is attached */
6892 if (!port->phy_node)
6893 return 0;
6894
Marcin Wojtas3f518502014-07-10 16:52:13 -03006895 phy_dev = of_phy_connect(port->dev, port->phy_node, mvpp2_link_event, 0,
6896 port->phy_interface);
6897 if (!phy_dev) {
6898 netdev_err(port->dev, "cannot connect to phy\n");
6899 return -ENODEV;
6900 }
6901 phy_dev->supported &= PHY_GBIT_FEATURES;
6902 phy_dev->advertising = phy_dev->supported;
6903
Marcin Wojtas3f518502014-07-10 16:52:13 -03006904 port->link = 0;
6905 port->duplex = 0;
6906 port->speed = 0;
6907
6908 return 0;
6909}
6910
6911static void mvpp2_phy_disconnect(struct mvpp2_port *port)
6912{
Philippe Reynes8e072692016-06-28 00:08:11 +02006913 struct net_device *ndev = port->dev;
6914
Antoine Tenart5997c862017-09-01 11:04:53 +02006915 if (!ndev->phydev)
6916 return;
6917
Philippe Reynes8e072692016-06-28 00:08:11 +02006918 phy_disconnect(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006919}
6920
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006921static int mvpp2_irqs_init(struct mvpp2_port *port)
6922{
6923 int err, i;
6924
6925 for (i = 0; i < port->nqvecs; i++) {
6926 struct mvpp2_queue_vector *qv = port->qvecs + i;
6927
Marc Zyngier13c249a2017-11-04 12:33:47 +00006928 if (qv->type == MVPP2_QUEUE_VECTOR_PRIVATE)
6929 irq_set_status_flags(qv->irq, IRQ_NO_BALANCING);
6930
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006931 err = request_irq(qv->irq, mvpp2_isr, 0, port->dev->name, qv);
6932 if (err)
6933 goto err;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006934
6935 if (qv->type == MVPP2_QUEUE_VECTOR_PRIVATE)
6936 irq_set_affinity_hint(qv->irq,
6937 cpumask_of(qv->sw_thread_id));
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006938 }
6939
6940 return 0;
6941err:
6942 for (i = 0; i < port->nqvecs; i++) {
6943 struct mvpp2_queue_vector *qv = port->qvecs + i;
6944
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006945 irq_set_affinity_hint(qv->irq, NULL);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006946 free_irq(qv->irq, qv);
6947 }
6948
6949 return err;
6950}
6951
6952static void mvpp2_irqs_deinit(struct mvpp2_port *port)
6953{
6954 int i;
6955
6956 for (i = 0; i < port->nqvecs; i++) {
6957 struct mvpp2_queue_vector *qv = port->qvecs + i;
6958
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006959 irq_set_affinity_hint(qv->irq, NULL);
Marc Zyngier13c249a2017-11-04 12:33:47 +00006960 irq_clear_status_flags(qv->irq, IRQ_NO_BALANCING);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006961 free_irq(qv->irq, qv);
6962 }
6963}
6964
Antoine Tenart1d7d15d2017-10-30 11:23:30 +01006965static void mvpp22_init_rss(struct mvpp2_port *port)
6966{
6967 struct mvpp2 *priv = port->priv;
6968 int i;
6969
6970 /* Set the table width: replace the whole classifier Rx queue number
6971 * with the ones configured in RSS table entries.
6972 */
6973 mvpp2_write(priv, MVPP22_RSS_INDEX, MVPP22_RSS_INDEX_TABLE(0));
6974 mvpp2_write(priv, MVPP22_RSS_WIDTH, 8);
6975
6976 /* Loop through the classifier Rx Queues and map them to a RSS table.
6977 * Map them all to the first table (0) by default.
6978 */
6979 for (i = 0; i < MVPP2_CLS_RX_QUEUES; i++) {
6980 mvpp2_write(priv, MVPP22_RSS_INDEX, MVPP22_RSS_INDEX_QUEUE(i));
6981 mvpp2_write(priv, MVPP22_RSS_TABLE,
6982 MVPP22_RSS_TABLE_POINTER(0));
6983 }
6984
6985 /* Configure the first table to evenly distribute the packets across
6986 * real Rx Queues. The table entries map a hash to an port Rx Queue.
6987 */
6988 for (i = 0; i < MVPP22_RSS_TABLE_ENTRIES; i++) {
6989 u32 sel = MVPP22_RSS_INDEX_TABLE(0) |
6990 MVPP22_RSS_INDEX_TABLE_ENTRY(i);
6991 mvpp2_write(priv, MVPP22_RSS_INDEX, sel);
6992
6993 mvpp2_write(priv, MVPP22_RSS_TABLE_ENTRY, i % port->nrxqs);
6994 }
6995
6996}
6997
Marcin Wojtas3f518502014-07-10 16:52:13 -03006998static int mvpp2_open(struct net_device *dev)
6999{
7000 struct mvpp2_port *port = netdev_priv(dev);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007001 struct mvpp2 *priv = port->priv;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007002 unsigned char mac_bcast[ETH_ALEN] = {
7003 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7004 int err;
7005
7006 err = mvpp2_prs_mac_da_accept(port->priv, port->id, mac_bcast, true);
7007 if (err) {
7008 netdev_err(dev, "mvpp2_prs_mac_da_accept BC failed\n");
7009 return err;
7010 }
7011 err = mvpp2_prs_mac_da_accept(port->priv, port->id,
7012 dev->dev_addr, true);
7013 if (err) {
7014 netdev_err(dev, "mvpp2_prs_mac_da_accept MC failed\n");
7015 return err;
7016 }
7017 err = mvpp2_prs_tag_mode_set(port->priv, port->id, MVPP2_TAG_TYPE_MH);
7018 if (err) {
7019 netdev_err(dev, "mvpp2_prs_tag_mode_set failed\n");
7020 return err;
7021 }
7022 err = mvpp2_prs_def_flow(port);
7023 if (err) {
7024 netdev_err(dev, "mvpp2_prs_def_flow failed\n");
7025 return err;
7026 }
7027
7028 /* Allocate the Rx/Tx queues */
7029 err = mvpp2_setup_rxqs(port);
7030 if (err) {
7031 netdev_err(port->dev, "cannot allocate Rx queues\n");
7032 return err;
7033 }
7034
7035 err = mvpp2_setup_txqs(port);
7036 if (err) {
7037 netdev_err(port->dev, "cannot allocate Tx queues\n");
7038 goto err_cleanup_rxqs;
7039 }
7040
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007041 err = mvpp2_irqs_init(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007042 if (err) {
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007043 netdev_err(port->dev, "cannot init IRQs\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03007044 goto err_cleanup_txqs;
7045 }
7046
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007047 if (priv->hw_version == MVPP22 && !port->phy_node && port->link_irq) {
7048 err = request_irq(port->link_irq, mvpp2_link_status_isr, 0,
7049 dev->name, port);
7050 if (err) {
7051 netdev_err(port->dev, "cannot request link IRQ %d\n",
7052 port->link_irq);
7053 goto err_free_irq;
7054 }
7055
7056 mvpp22_gop_setup_irq(port);
7057 }
7058
Marcin Wojtas3f518502014-07-10 16:52:13 -03007059 /* In default link is down */
7060 netif_carrier_off(port->dev);
7061
7062 err = mvpp2_phy_connect(port);
7063 if (err < 0)
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007064 goto err_free_link_irq;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007065
7066 /* Unmask interrupts on all CPUs */
7067 on_each_cpu(mvpp2_interrupts_unmask, port, 1);
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007068 mvpp2_shared_interrupt_mask_unmask(port, false);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007069
7070 mvpp2_start_dev(port);
7071
Antoine Tenart1d7d15d2017-10-30 11:23:30 +01007072 if (priv->hw_version == MVPP22)
7073 mvpp22_init_rss(port);
7074
Miquel Raynal118d6292017-11-06 22:56:53 +01007075 /* Start hardware statistics gathering */
Miquel Raynale5c500e2017-11-08 08:59:40 +01007076 queue_delayed_work(priv->stats_queue, &port->stats_work,
Miquel Raynal118d6292017-11-06 22:56:53 +01007077 MVPP2_MIB_COUNTERS_STATS_DELAY);
7078
Marcin Wojtas3f518502014-07-10 16:52:13 -03007079 return 0;
7080
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007081err_free_link_irq:
7082 if (priv->hw_version == MVPP22 && !port->phy_node && port->link_irq)
7083 free_irq(port->link_irq, port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007084err_free_irq:
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007085 mvpp2_irqs_deinit(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007086err_cleanup_txqs:
7087 mvpp2_cleanup_txqs(port);
7088err_cleanup_rxqs:
7089 mvpp2_cleanup_rxqs(port);
7090 return err;
7091}
7092
7093static int mvpp2_stop(struct net_device *dev)
7094{
7095 struct mvpp2_port *port = netdev_priv(dev);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007096 struct mvpp2_port_pcpu *port_pcpu;
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007097 struct mvpp2 *priv = port->priv;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007098 int cpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007099
7100 mvpp2_stop_dev(port);
7101 mvpp2_phy_disconnect(port);
7102
7103 /* Mask interrupts on all CPUs */
7104 on_each_cpu(mvpp2_interrupts_mask, port, 1);
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007105 mvpp2_shared_interrupt_mask_unmask(port, true);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007106
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007107 if (priv->hw_version == MVPP22 && !port->phy_node && port->link_irq)
7108 free_irq(port->link_irq, port);
7109
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007110 mvpp2_irqs_deinit(port);
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007111 if (!port->has_tx_irqs) {
7112 for_each_present_cpu(cpu) {
7113 port_pcpu = per_cpu_ptr(port->pcpu, cpu);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007114
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007115 hrtimer_cancel(&port_pcpu->tx_done_timer);
7116 port_pcpu->timer_scheduled = false;
7117 tasklet_kill(&port_pcpu->tx_done_tasklet);
7118 }
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007119 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03007120 mvpp2_cleanup_rxqs(port);
7121 mvpp2_cleanup_txqs(port);
7122
Miquel Raynale5c500e2017-11-08 08:59:40 +01007123 cancel_delayed_work_sync(&port->stats_work);
Miquel Raynal118d6292017-11-06 22:56:53 +01007124
Marcin Wojtas3f518502014-07-10 16:52:13 -03007125 return 0;
7126}
7127
7128static void mvpp2_set_rx_mode(struct net_device *dev)
7129{
7130 struct mvpp2_port *port = netdev_priv(dev);
7131 struct mvpp2 *priv = port->priv;
7132 struct netdev_hw_addr *ha;
7133 int id = port->id;
7134 bool allmulti = dev->flags & IFF_ALLMULTI;
7135
7136 mvpp2_prs_mac_promisc_set(priv, id, dev->flags & IFF_PROMISC);
7137 mvpp2_prs_mac_multi_set(priv, id, MVPP2_PE_MAC_MC_ALL, allmulti);
7138 mvpp2_prs_mac_multi_set(priv, id, MVPP2_PE_MAC_MC_IP6, allmulti);
7139
7140 /* Remove all port->id's mcast enries */
7141 mvpp2_prs_mcast_del_all(priv, id);
7142
7143 if (allmulti && !netdev_mc_empty(dev)) {
7144 netdev_for_each_mc_addr(ha, dev)
7145 mvpp2_prs_mac_da_accept(priv, id, ha->addr, true);
7146 }
7147}
7148
7149static int mvpp2_set_mac_address(struct net_device *dev, void *p)
7150{
7151 struct mvpp2_port *port = netdev_priv(dev);
7152 const struct sockaddr *addr = p;
7153 int err;
7154
7155 if (!is_valid_ether_addr(addr->sa_data)) {
7156 err = -EADDRNOTAVAIL;
Markus Elfringc1175542017-04-17 11:10:47 +02007157 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007158 }
7159
7160 if (!netif_running(dev)) {
7161 err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
7162 if (!err)
7163 return 0;
7164 /* Reconfigure parser to accept the original MAC address */
7165 err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
7166 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02007167 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007168 }
7169
7170 mvpp2_stop_dev(port);
7171
7172 err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
7173 if (!err)
7174 goto out_start;
7175
7176 /* Reconfigure parser accept the original MAC address */
7177 err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
7178 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02007179 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007180out_start:
7181 mvpp2_start_dev(port);
7182 mvpp2_egress_enable(port);
7183 mvpp2_ingress_enable(port);
7184 return 0;
Markus Elfringc1175542017-04-17 11:10:47 +02007185log_error:
Markus Elfringdfd42402017-04-17 11:20:41 +02007186 netdev_err(dev, "failed to change MAC address\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03007187 return err;
7188}
7189
7190static int mvpp2_change_mtu(struct net_device *dev, int mtu)
7191{
7192 struct mvpp2_port *port = netdev_priv(dev);
7193 int err;
7194
Jarod Wilson57779872016-10-17 15:54:06 -04007195 if (!IS_ALIGNED(MVPP2_RX_PKT_SIZE(mtu), 8)) {
7196 netdev_info(dev, "illegal MTU value %d, round to %d\n", mtu,
7197 ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8));
7198 mtu = ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007199 }
7200
7201 if (!netif_running(dev)) {
7202 err = mvpp2_bm_update_mtu(dev, mtu);
7203 if (!err) {
7204 port->pkt_size = MVPP2_RX_PKT_SIZE(mtu);
7205 return 0;
7206 }
7207
7208 /* Reconfigure BM to the original MTU */
7209 err = mvpp2_bm_update_mtu(dev, dev->mtu);
7210 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02007211 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007212 }
7213
7214 mvpp2_stop_dev(port);
7215
7216 err = mvpp2_bm_update_mtu(dev, mtu);
7217 if (!err) {
7218 port->pkt_size = MVPP2_RX_PKT_SIZE(mtu);
7219 goto out_start;
7220 }
7221
7222 /* Reconfigure BM to the original MTU */
7223 err = mvpp2_bm_update_mtu(dev, dev->mtu);
7224 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02007225 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007226
7227out_start:
7228 mvpp2_start_dev(port);
7229 mvpp2_egress_enable(port);
7230 mvpp2_ingress_enable(port);
7231
7232 return 0;
Markus Elfringc1175542017-04-17 11:10:47 +02007233log_error:
Markus Elfringdfd42402017-04-17 11:20:41 +02007234 netdev_err(dev, "failed to change MTU\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03007235 return err;
7236}
7237
stephen hemmingerbc1f4472017-01-06 19:12:52 -08007238static void
Marcin Wojtas3f518502014-07-10 16:52:13 -03007239mvpp2_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
7240{
7241 struct mvpp2_port *port = netdev_priv(dev);
7242 unsigned int start;
7243 int cpu;
7244
7245 for_each_possible_cpu(cpu) {
7246 struct mvpp2_pcpu_stats *cpu_stats;
7247 u64 rx_packets;
7248 u64 rx_bytes;
7249 u64 tx_packets;
7250 u64 tx_bytes;
7251
7252 cpu_stats = per_cpu_ptr(port->stats, cpu);
7253 do {
7254 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
7255 rx_packets = cpu_stats->rx_packets;
7256 rx_bytes = cpu_stats->rx_bytes;
7257 tx_packets = cpu_stats->tx_packets;
7258 tx_bytes = cpu_stats->tx_bytes;
7259 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
7260
7261 stats->rx_packets += rx_packets;
7262 stats->rx_bytes += rx_bytes;
7263 stats->tx_packets += tx_packets;
7264 stats->tx_bytes += tx_bytes;
7265 }
7266
7267 stats->rx_errors = dev->stats.rx_errors;
7268 stats->rx_dropped = dev->stats.rx_dropped;
7269 stats->tx_dropped = dev->stats.tx_dropped;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007270}
7271
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007272static int mvpp2_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
7273{
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007274 int ret;
7275
Philippe Reynes8e072692016-06-28 00:08:11 +02007276 if (!dev->phydev)
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007277 return -ENOTSUPP;
7278
Philippe Reynes8e072692016-06-28 00:08:11 +02007279 ret = phy_mii_ioctl(dev->phydev, ifr, cmd);
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007280 if (!ret)
7281 mvpp2_link_event(dev);
7282
7283 return ret;
7284}
7285
Marcin Wojtas3f518502014-07-10 16:52:13 -03007286/* Ethtool methods */
7287
Marcin Wojtas3f518502014-07-10 16:52:13 -03007288/* Set interrupt coalescing for ethtools */
7289static int mvpp2_ethtool_set_coalesce(struct net_device *dev,
7290 struct ethtool_coalesce *c)
7291{
7292 struct mvpp2_port *port = netdev_priv(dev);
7293 int queue;
7294
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007295 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007296 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
7297
7298 rxq->time_coal = c->rx_coalesce_usecs;
7299 rxq->pkts_coal = c->rx_max_coalesced_frames;
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01007300 mvpp2_rx_pkts_coal_set(port, rxq);
7301 mvpp2_rx_time_coal_set(port, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007302 }
7303
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007304 if (port->has_tx_irqs) {
7305 port->tx_time_coal = c->tx_coalesce_usecs;
7306 mvpp2_tx_time_coal_set(port);
7307 }
7308
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007309 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007310 struct mvpp2_tx_queue *txq = port->txqs[queue];
7311
7312 txq->done_pkts_coal = c->tx_max_coalesced_frames;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007313
7314 if (port->has_tx_irqs)
7315 mvpp2_tx_pkts_coal_set(port, txq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007316 }
7317
Marcin Wojtas3f518502014-07-10 16:52:13 -03007318 return 0;
7319}
7320
7321/* get coalescing for ethtools */
7322static int mvpp2_ethtool_get_coalesce(struct net_device *dev,
7323 struct ethtool_coalesce *c)
7324{
7325 struct mvpp2_port *port = netdev_priv(dev);
7326
Antoine Tenart385c2842017-12-11 09:13:27 +01007327 c->rx_coalesce_usecs = port->rxqs[0]->time_coal;
7328 c->rx_max_coalesced_frames = port->rxqs[0]->pkts_coal;
7329 c->tx_max_coalesced_frames = port->txqs[0]->done_pkts_coal;
Antoine Tenart24b28cc2017-12-11 09:13:28 +01007330 c->tx_coalesce_usecs = port->tx_time_coal;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007331 return 0;
7332}
7333
7334static void mvpp2_ethtool_get_drvinfo(struct net_device *dev,
7335 struct ethtool_drvinfo *drvinfo)
7336{
7337 strlcpy(drvinfo->driver, MVPP2_DRIVER_NAME,
7338 sizeof(drvinfo->driver));
7339 strlcpy(drvinfo->version, MVPP2_DRIVER_VERSION,
7340 sizeof(drvinfo->version));
7341 strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
7342 sizeof(drvinfo->bus_info));
7343}
7344
7345static void mvpp2_ethtool_get_ringparam(struct net_device *dev,
7346 struct ethtool_ringparam *ring)
7347{
7348 struct mvpp2_port *port = netdev_priv(dev);
7349
Yan Markman7cf87e42017-12-11 09:13:26 +01007350 ring->rx_max_pending = MVPP2_MAX_RXD_MAX;
7351 ring->tx_max_pending = MVPP2_MAX_TXD_MAX;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007352 ring->rx_pending = port->rx_ring_size;
7353 ring->tx_pending = port->tx_ring_size;
7354}
7355
7356static int mvpp2_ethtool_set_ringparam(struct net_device *dev,
7357 struct ethtool_ringparam *ring)
7358{
7359 struct mvpp2_port *port = netdev_priv(dev);
7360 u16 prev_rx_ring_size = port->rx_ring_size;
7361 u16 prev_tx_ring_size = port->tx_ring_size;
7362 int err;
7363
7364 err = mvpp2_check_ringparam_valid(dev, ring);
7365 if (err)
7366 return err;
7367
7368 if (!netif_running(dev)) {
7369 port->rx_ring_size = ring->rx_pending;
7370 port->tx_ring_size = ring->tx_pending;
7371 return 0;
7372 }
7373
7374 /* The interface is running, so we have to force a
7375 * reallocation of the queues
7376 */
7377 mvpp2_stop_dev(port);
7378 mvpp2_cleanup_rxqs(port);
7379 mvpp2_cleanup_txqs(port);
7380
7381 port->rx_ring_size = ring->rx_pending;
7382 port->tx_ring_size = ring->tx_pending;
7383
7384 err = mvpp2_setup_rxqs(port);
7385 if (err) {
7386 /* Reallocate Rx queues with the original ring size */
7387 port->rx_ring_size = prev_rx_ring_size;
7388 ring->rx_pending = prev_rx_ring_size;
7389 err = mvpp2_setup_rxqs(port);
7390 if (err)
7391 goto err_out;
7392 }
7393 err = mvpp2_setup_txqs(port);
7394 if (err) {
7395 /* Reallocate Tx queues with the original ring size */
7396 port->tx_ring_size = prev_tx_ring_size;
7397 ring->tx_pending = prev_tx_ring_size;
7398 err = mvpp2_setup_txqs(port);
7399 if (err)
7400 goto err_clean_rxqs;
7401 }
7402
7403 mvpp2_start_dev(port);
7404 mvpp2_egress_enable(port);
7405 mvpp2_ingress_enable(port);
7406
7407 return 0;
7408
7409err_clean_rxqs:
7410 mvpp2_cleanup_rxqs(port);
7411err_out:
Markus Elfringdfd42402017-04-17 11:20:41 +02007412 netdev_err(dev, "failed to change ring parameters");
Marcin Wojtas3f518502014-07-10 16:52:13 -03007413 return err;
7414}
7415
7416/* Device ops */
7417
7418static const struct net_device_ops mvpp2_netdev_ops = {
7419 .ndo_open = mvpp2_open,
7420 .ndo_stop = mvpp2_stop,
7421 .ndo_start_xmit = mvpp2_tx,
7422 .ndo_set_rx_mode = mvpp2_set_rx_mode,
7423 .ndo_set_mac_address = mvpp2_set_mac_address,
7424 .ndo_change_mtu = mvpp2_change_mtu,
7425 .ndo_get_stats64 = mvpp2_get_stats64,
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007426 .ndo_do_ioctl = mvpp2_ioctl,
Marcin Wojtas3f518502014-07-10 16:52:13 -03007427};
7428
7429static const struct ethtool_ops mvpp2_eth_tool_ops = {
Florian Fainelli00606c42016-11-15 11:19:48 -08007430 .nway_reset = phy_ethtool_nway_reset,
Marcin Wojtas3f518502014-07-10 16:52:13 -03007431 .get_link = ethtool_op_get_link,
Marcin Wojtas3f518502014-07-10 16:52:13 -03007432 .set_coalesce = mvpp2_ethtool_set_coalesce,
7433 .get_coalesce = mvpp2_ethtool_get_coalesce,
7434 .get_drvinfo = mvpp2_ethtool_get_drvinfo,
7435 .get_ringparam = mvpp2_ethtool_get_ringparam,
7436 .set_ringparam = mvpp2_ethtool_set_ringparam,
Miquel Raynal118d6292017-11-06 22:56:53 +01007437 .get_strings = mvpp2_ethtool_get_strings,
7438 .get_ethtool_stats = mvpp2_ethtool_get_stats,
7439 .get_sset_count = mvpp2_ethtool_get_sset_count,
Philippe Reynesfb773e92016-06-28 00:08:12 +02007440 .get_link_ksettings = phy_ethtool_get_link_ksettings,
7441 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Marcin Wojtas3f518502014-07-10 16:52:13 -03007442};
7443
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007444/* Used for PPv2.1, or PPv2.2 with the old Device Tree binding that
7445 * had a single IRQ defined per-port.
7446 */
7447static int mvpp2_simple_queue_vectors_init(struct mvpp2_port *port,
7448 struct device_node *port_node)
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007449{
7450 struct mvpp2_queue_vector *v = &port->qvecs[0];
7451
7452 v->first_rxq = 0;
7453 v->nrxqs = port->nrxqs;
7454 v->type = MVPP2_QUEUE_VECTOR_SHARED;
7455 v->sw_thread_id = 0;
7456 v->sw_thread_mask = *cpumask_bits(cpu_online_mask);
7457 v->port = port;
7458 v->irq = irq_of_parse_and_map(port_node, 0);
7459 if (v->irq <= 0)
7460 return -EINVAL;
7461 netif_napi_add(port->dev, &v->napi, mvpp2_poll,
7462 NAPI_POLL_WEIGHT);
7463
7464 port->nqvecs = 1;
7465
7466 return 0;
7467}
7468
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007469static int mvpp2_multi_queue_vectors_init(struct mvpp2_port *port,
7470 struct device_node *port_node)
7471{
7472 struct mvpp2_queue_vector *v;
7473 int i, ret;
7474
7475 port->nqvecs = num_possible_cpus();
7476 if (queue_mode == MVPP2_QDIST_SINGLE_MODE)
7477 port->nqvecs += 1;
7478
7479 for (i = 0; i < port->nqvecs; i++) {
7480 char irqname[16];
7481
7482 v = port->qvecs + i;
7483
7484 v->port = port;
7485 v->type = MVPP2_QUEUE_VECTOR_PRIVATE;
7486 v->sw_thread_id = i;
7487 v->sw_thread_mask = BIT(i);
7488
7489 snprintf(irqname, sizeof(irqname), "tx-cpu%d", i);
7490
7491 if (queue_mode == MVPP2_QDIST_MULTI_MODE) {
7492 v->first_rxq = i * MVPP2_DEFAULT_RXQ;
7493 v->nrxqs = MVPP2_DEFAULT_RXQ;
7494 } else if (queue_mode == MVPP2_QDIST_SINGLE_MODE &&
7495 i == (port->nqvecs - 1)) {
7496 v->first_rxq = 0;
7497 v->nrxqs = port->nrxqs;
7498 v->type = MVPP2_QUEUE_VECTOR_SHARED;
7499 strncpy(irqname, "rx-shared", sizeof(irqname));
7500 }
7501
7502 v->irq = of_irq_get_byname(port_node, irqname);
7503 if (v->irq <= 0) {
7504 ret = -EINVAL;
7505 goto err;
7506 }
7507
7508 netif_napi_add(port->dev, &v->napi, mvpp2_poll,
7509 NAPI_POLL_WEIGHT);
7510 }
7511
7512 return 0;
7513
7514err:
7515 for (i = 0; i < port->nqvecs; i++)
7516 irq_dispose_mapping(port->qvecs[i].irq);
7517 return ret;
7518}
7519
7520static int mvpp2_queue_vectors_init(struct mvpp2_port *port,
7521 struct device_node *port_node)
7522{
7523 if (port->has_tx_irqs)
7524 return mvpp2_multi_queue_vectors_init(port, port_node);
7525 else
7526 return mvpp2_simple_queue_vectors_init(port, port_node);
7527}
7528
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007529static void mvpp2_queue_vectors_deinit(struct mvpp2_port *port)
7530{
7531 int i;
7532
7533 for (i = 0; i < port->nqvecs; i++)
7534 irq_dispose_mapping(port->qvecs[i].irq);
7535}
7536
7537/* Configure Rx queue group interrupt for this port */
7538static void mvpp2_rx_irqs_setup(struct mvpp2_port *port)
7539{
7540 struct mvpp2 *priv = port->priv;
7541 u32 val;
7542 int i;
7543
7544 if (priv->hw_version == MVPP21) {
7545 mvpp2_write(priv, MVPP21_ISR_RXQ_GROUP_REG(port->id),
7546 port->nrxqs);
7547 return;
7548 }
7549
7550 /* Handle the more complicated PPv2.2 case */
7551 for (i = 0; i < port->nqvecs; i++) {
7552 struct mvpp2_queue_vector *qv = port->qvecs + i;
7553
7554 if (!qv->nrxqs)
7555 continue;
7556
7557 val = qv->sw_thread_id;
7558 val |= port->id << MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_OFFSET;
7559 mvpp2_write(priv, MVPP22_ISR_RXQ_GROUP_INDEX_REG, val);
7560
7561 val = qv->first_rxq;
7562 val |= qv->nrxqs << MVPP22_ISR_RXQ_SUB_GROUP_SIZE_OFFSET;
7563 mvpp2_write(priv, MVPP22_ISR_RXQ_SUB_GROUP_CONFIG_REG, val);
7564 }
7565}
7566
Marcin Wojtas3f518502014-07-10 16:52:13 -03007567/* Initialize port HW */
7568static int mvpp2_port_init(struct mvpp2_port *port)
7569{
7570 struct device *dev = port->dev->dev.parent;
7571 struct mvpp2 *priv = port->priv;
7572 struct mvpp2_txq_pcpu *txq_pcpu;
7573 int queue, cpu, err;
7574
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007575 /* Checks for hardware constraints */
7576 if (port->first_rxq + port->nrxqs >
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01007577 MVPP2_MAX_PORTS * priv->max_port_rxqs)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007578 return -EINVAL;
7579
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007580 if (port->nrxqs % 4 || (port->nrxqs > priv->max_port_rxqs) ||
7581 (port->ntxqs > MVPP2_MAX_TXQ))
7582 return -EINVAL;
7583
Marcin Wojtas3f518502014-07-10 16:52:13 -03007584 /* Disable port */
7585 mvpp2_egress_disable(port);
7586 mvpp2_port_disable(port);
7587
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007588 port->tx_time_coal = MVPP2_TXDONE_COAL_USEC;
7589
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007590 port->txqs = devm_kcalloc(dev, port->ntxqs, sizeof(*port->txqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03007591 GFP_KERNEL);
7592 if (!port->txqs)
7593 return -ENOMEM;
7594
7595 /* Associate physical Tx queues to this port and initialize.
7596 * The mapping is predefined.
7597 */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007598 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007599 int queue_phy_id = mvpp2_txq_phys(port->id, queue);
7600 struct mvpp2_tx_queue *txq;
7601
7602 txq = devm_kzalloc(dev, sizeof(*txq), GFP_KERNEL);
Christophe Jaillet177c8d12017-02-19 10:19:57 +01007603 if (!txq) {
7604 err = -ENOMEM;
7605 goto err_free_percpu;
7606 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03007607
7608 txq->pcpu = alloc_percpu(struct mvpp2_txq_pcpu);
7609 if (!txq->pcpu) {
7610 err = -ENOMEM;
7611 goto err_free_percpu;
7612 }
7613
7614 txq->id = queue_phy_id;
7615 txq->log_id = queue;
7616 txq->done_pkts_coal = MVPP2_TXDONE_COAL_PKTS_THRESH;
7617 for_each_present_cpu(cpu) {
7618 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
7619 txq_pcpu->cpu = cpu;
7620 }
7621
7622 port->txqs[queue] = txq;
7623 }
7624
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007625 port->rxqs = devm_kcalloc(dev, port->nrxqs, sizeof(*port->rxqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03007626 GFP_KERNEL);
7627 if (!port->rxqs) {
7628 err = -ENOMEM;
7629 goto err_free_percpu;
7630 }
7631
7632 /* Allocate and initialize Rx queue for this port */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007633 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007634 struct mvpp2_rx_queue *rxq;
7635
7636 /* Map physical Rx queue to port's logical Rx queue */
7637 rxq = devm_kzalloc(dev, sizeof(*rxq), GFP_KERNEL);
Jisheng Zhangd82b0c22016-03-31 17:01:23 +08007638 if (!rxq) {
7639 err = -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007640 goto err_free_percpu;
Jisheng Zhangd82b0c22016-03-31 17:01:23 +08007641 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03007642 /* Map this Rx queue to a physical queue */
7643 rxq->id = port->first_rxq + queue;
7644 rxq->port = port->id;
7645 rxq->logic_rxq = queue;
7646
7647 port->rxqs[queue] = rxq;
7648 }
7649
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007650 mvpp2_rx_irqs_setup(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007651
7652 /* Create Rx descriptor rings */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007653 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007654 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
7655
7656 rxq->size = port->rx_ring_size;
7657 rxq->pkts_coal = MVPP2_RX_COAL_PKTS;
7658 rxq->time_coal = MVPP2_RX_COAL_USEC;
7659 }
7660
7661 mvpp2_ingress_disable(port);
7662
7663 /* Port default configuration */
7664 mvpp2_defaults_set(port);
7665
7666 /* Port's classifier configuration */
7667 mvpp2_cls_oversize_rxq_set(port);
7668 mvpp2_cls_port_config(port);
7669
7670 /* Provide an initial Rx packet size */
7671 port->pkt_size = MVPP2_RX_PKT_SIZE(port->dev->mtu);
7672
7673 /* Initialize pools for swf */
7674 err = mvpp2_swf_bm_pool_init(port);
7675 if (err)
7676 goto err_free_percpu;
7677
7678 return 0;
7679
7680err_free_percpu:
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007681 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007682 if (!port->txqs[queue])
7683 continue;
7684 free_percpu(port->txqs[queue]->pcpu);
7685 }
7686 return err;
7687}
7688
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007689/* Checks if the port DT description has the TX interrupts
7690 * described. On PPv2.1, there are no such interrupts. On PPv2.2,
7691 * there are available, but we need to keep support for old DTs.
7692 */
7693static bool mvpp2_port_has_tx_irqs(struct mvpp2 *priv,
7694 struct device_node *port_node)
7695{
7696 char *irqs[5] = { "rx-shared", "tx-cpu0", "tx-cpu1",
7697 "tx-cpu2", "tx-cpu3" };
7698 int ret, i;
7699
7700 if (priv->hw_version == MVPP21)
7701 return false;
7702
7703 for (i = 0; i < 5; i++) {
7704 ret = of_property_match_string(port_node, "interrupt-names",
7705 irqs[i]);
7706 if (ret < 0)
7707 return false;
7708 }
7709
7710 return true;
7711}
7712
Antoine Tenart3ba8c812017-09-02 11:06:47 +02007713static void mvpp2_port_copy_mac_addr(struct net_device *dev, struct mvpp2 *priv,
7714 struct device_node *port_node,
7715 char **mac_from)
7716{
7717 struct mvpp2_port *port = netdev_priv(dev);
7718 char hw_mac_addr[ETH_ALEN] = {0};
7719 const char *dt_mac_addr;
7720
7721 dt_mac_addr = of_get_mac_address(port_node);
7722 if (dt_mac_addr && is_valid_ether_addr(dt_mac_addr)) {
7723 *mac_from = "device tree";
7724 ether_addr_copy(dev->dev_addr, dt_mac_addr);
Antoine Tenart688cbaf2017-09-02 11:06:49 +02007725 return;
Antoine Tenart3ba8c812017-09-02 11:06:47 +02007726 }
Antoine Tenart688cbaf2017-09-02 11:06:49 +02007727
7728 if (priv->hw_version == MVPP21) {
7729 mvpp21_get_mac_address(port, hw_mac_addr);
7730 if (is_valid_ether_addr(hw_mac_addr)) {
7731 *mac_from = "hardware";
7732 ether_addr_copy(dev->dev_addr, hw_mac_addr);
7733 return;
7734 }
7735 }
7736
7737 *mac_from = "random";
7738 eth_hw_addr_random(dev);
Antoine Tenart3ba8c812017-09-02 11:06:47 +02007739}
7740
Marcin Wojtas3f518502014-07-10 16:52:13 -03007741/* Ports initialization */
7742static int mvpp2_port_probe(struct platform_device *pdev,
7743 struct device_node *port_node,
Yan Markman6bf69a12017-09-25 14:59:47 +02007744 struct mvpp2 *priv, int index)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007745{
7746 struct device_node *phy_node;
Antoine Tenart542897d2017-08-30 10:29:15 +02007747 struct phy *comphy;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007748 struct mvpp2_port *port;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007749 struct mvpp2_port_pcpu *port_pcpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007750 struct net_device *dev;
7751 struct resource *res;
Antoine Tenart3ba8c812017-09-02 11:06:47 +02007752 char *mac_from = "";
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007753 unsigned int ntxqs, nrxqs;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007754 bool has_tx_irqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007755 u32 id;
7756 int features;
7757 int phy_mode;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007758 int err, i, cpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007759
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007760 has_tx_irqs = mvpp2_port_has_tx_irqs(priv, port_node);
7761
7762 if (!has_tx_irqs)
7763 queue_mode = MVPP2_QDIST_SINGLE_MODE;
7764
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007765 ntxqs = MVPP2_MAX_TXQ;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007766 if (priv->hw_version == MVPP22 && queue_mode == MVPP2_QDIST_MULTI_MODE)
7767 nrxqs = MVPP2_DEFAULT_RXQ * num_possible_cpus();
7768 else
7769 nrxqs = MVPP2_DEFAULT_RXQ;
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007770
7771 dev = alloc_etherdev_mqs(sizeof(*port), ntxqs, nrxqs);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007772 if (!dev)
7773 return -ENOMEM;
7774
7775 phy_node = of_parse_phandle(port_node, "phy", 0);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007776 phy_mode = of_get_phy_mode(port_node);
7777 if (phy_mode < 0) {
7778 dev_err(&pdev->dev, "incorrect phy mode\n");
7779 err = phy_mode;
7780 goto err_free_netdev;
7781 }
7782
Antoine Tenart542897d2017-08-30 10:29:15 +02007783 comphy = devm_of_phy_get(&pdev->dev, port_node, NULL);
7784 if (IS_ERR(comphy)) {
7785 if (PTR_ERR(comphy) == -EPROBE_DEFER) {
7786 err = -EPROBE_DEFER;
7787 goto err_free_netdev;
7788 }
7789 comphy = NULL;
7790 }
7791
Marcin Wojtas3f518502014-07-10 16:52:13 -03007792 if (of_property_read_u32(port_node, "port-id", &id)) {
7793 err = -EINVAL;
7794 dev_err(&pdev->dev, "missing port-id value\n");
7795 goto err_free_netdev;
7796 }
7797
Yan Markman7cf87e42017-12-11 09:13:26 +01007798 dev->tx_queue_len = MVPP2_MAX_TXD_MAX;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007799 dev->watchdog_timeo = 5 * HZ;
7800 dev->netdev_ops = &mvpp2_netdev_ops;
7801 dev->ethtool_ops = &mvpp2_eth_tool_ops;
7802
7803 port = netdev_priv(dev);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007804 port->dev = dev;
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007805 port->ntxqs = ntxqs;
7806 port->nrxqs = nrxqs;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007807 port->priv = priv;
7808 port->has_tx_irqs = has_tx_irqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007809
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007810 err = mvpp2_queue_vectors_init(port, port_node);
7811 if (err)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007812 goto err_free_netdev;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007813
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007814 port->link_irq = of_irq_get_byname(port_node, "link");
7815 if (port->link_irq == -EPROBE_DEFER) {
7816 err = -EPROBE_DEFER;
7817 goto err_deinit_qvecs;
7818 }
7819 if (port->link_irq <= 0)
7820 /* the link irq is optional */
7821 port->link_irq = 0;
7822
Marcin Wojtas3f518502014-07-10 16:52:13 -03007823 if (of_property_read_bool(port_node, "marvell,loopback"))
7824 port->flags |= MVPP2_F_LOOPBACK;
7825
Marcin Wojtas3f518502014-07-10 16:52:13 -03007826 port->id = id;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01007827 if (priv->hw_version == MVPP21)
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007828 port->first_rxq = port->id * port->nrxqs;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01007829 else
7830 port->first_rxq = port->id * priv->max_port_rxqs;
7831
Marcin Wojtas3f518502014-07-10 16:52:13 -03007832 port->phy_node = phy_node;
7833 port->phy_interface = phy_mode;
Antoine Tenart542897d2017-08-30 10:29:15 +02007834 port->comphy = comphy;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007835
Thomas Petazzonia7868412017-03-07 16:53:13 +01007836 if (priv->hw_version == MVPP21) {
7837 res = platform_get_resource(pdev, IORESOURCE_MEM, 2 + id);
7838 port->base = devm_ioremap_resource(&pdev->dev, res);
7839 if (IS_ERR(port->base)) {
7840 err = PTR_ERR(port->base);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007841 goto err_free_irq;
Thomas Petazzonia7868412017-03-07 16:53:13 +01007842 }
Miquel Raynal118d6292017-11-06 22:56:53 +01007843
7844 port->stats_base = port->priv->lms_base +
7845 MVPP21_MIB_COUNTERS_OFFSET +
7846 port->gop_id * MVPP21_MIB_COUNTERS_PORT_SZ;
Thomas Petazzonia7868412017-03-07 16:53:13 +01007847 } else {
7848 if (of_property_read_u32(port_node, "gop-port-id",
7849 &port->gop_id)) {
7850 err = -EINVAL;
7851 dev_err(&pdev->dev, "missing gop-port-id value\n");
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007852 goto err_deinit_qvecs;
Thomas Petazzonia7868412017-03-07 16:53:13 +01007853 }
7854
7855 port->base = priv->iface_base + MVPP22_GMAC_BASE(port->gop_id);
Miquel Raynal118d6292017-11-06 22:56:53 +01007856 port->stats_base = port->priv->iface_base +
7857 MVPP22_MIB_COUNTERS_OFFSET +
7858 port->gop_id * MVPP22_MIB_COUNTERS_PORT_SZ;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007859 }
7860
Miquel Raynal118d6292017-11-06 22:56:53 +01007861 /* Alloc per-cpu and ethtool stats */
Marcin Wojtas3f518502014-07-10 16:52:13 -03007862 port->stats = netdev_alloc_pcpu_stats(struct mvpp2_pcpu_stats);
7863 if (!port->stats) {
7864 err = -ENOMEM;
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007865 goto err_free_irq;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007866 }
7867
Miquel Raynal118d6292017-11-06 22:56:53 +01007868 port->ethtool_stats = devm_kcalloc(&pdev->dev,
7869 ARRAY_SIZE(mvpp2_ethtool_regs),
7870 sizeof(u64), GFP_KERNEL);
7871 if (!port->ethtool_stats) {
7872 err = -ENOMEM;
7873 goto err_free_stats;
7874 }
7875
Miquel Raynale5c500e2017-11-08 08:59:40 +01007876 mutex_init(&port->gather_stats_lock);
7877 INIT_DELAYED_WORK(&port->stats_work, mvpp2_gather_hw_statistics);
7878
Antoine Tenart3ba8c812017-09-02 11:06:47 +02007879 mvpp2_port_copy_mac_addr(dev, priv, port_node, &mac_from);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007880
Yan Markman7cf87e42017-12-11 09:13:26 +01007881 port->tx_ring_size = MVPP2_MAX_TXD_DFLT;
7882 port->rx_ring_size = MVPP2_MAX_RXD_DFLT;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007883 SET_NETDEV_DEV(dev, &pdev->dev);
7884
7885 err = mvpp2_port_init(port);
7886 if (err < 0) {
7887 dev_err(&pdev->dev, "failed to init port %d\n", id);
7888 goto err_free_stats;
7889 }
Thomas Petazzoni26975822017-03-07 16:53:14 +01007890
Thomas Petazzoni26975822017-03-07 16:53:14 +01007891 mvpp2_port_periodic_xon_disable(port);
7892
7893 if (priv->hw_version == MVPP21)
7894 mvpp2_port_fc_adv_enable(port);
7895
7896 mvpp2_port_reset(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007897
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007898 port->pcpu = alloc_percpu(struct mvpp2_port_pcpu);
7899 if (!port->pcpu) {
7900 err = -ENOMEM;
7901 goto err_free_txq_pcpu;
7902 }
7903
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007904 if (!port->has_tx_irqs) {
7905 for_each_present_cpu(cpu) {
7906 port_pcpu = per_cpu_ptr(port->pcpu, cpu);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007907
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007908 hrtimer_init(&port_pcpu->tx_done_timer, CLOCK_MONOTONIC,
7909 HRTIMER_MODE_REL_PINNED);
7910 port_pcpu->tx_done_timer.function = mvpp2_hr_timer_cb;
7911 port_pcpu->timer_scheduled = false;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007912
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007913 tasklet_init(&port_pcpu->tx_done_tasklet,
7914 mvpp2_tx_proc_cb,
7915 (unsigned long)dev);
7916 }
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007917 }
7918
Antoine Ténart186cd4d2017-08-23 09:46:56 +02007919 features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007920 dev->features = features | NETIF_F_RXCSUM;
7921 dev->hw_features |= features | NETIF_F_RXCSUM | NETIF_F_GRO;
7922 dev->vlan_features |= features;
Antoine Tenart1d17db02017-10-30 11:23:31 +01007923 dev->gso_max_segs = MVPP2_MAX_TSO_SEGS;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007924
Jarod Wilson57779872016-10-17 15:54:06 -04007925 /* MTU range: 68 - 9676 */
7926 dev->min_mtu = ETH_MIN_MTU;
7927 /* 9676 == 9700 - 20 and rounding to 8 */
7928 dev->max_mtu = 9676;
7929
Marcin Wojtas3f518502014-07-10 16:52:13 -03007930 err = register_netdev(dev);
7931 if (err < 0) {
7932 dev_err(&pdev->dev, "failed to register netdev\n");
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007933 goto err_free_port_pcpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007934 }
7935 netdev_info(dev, "Using %s mac address %pM\n", mac_from, dev->dev_addr);
7936
Yan Markman6bf69a12017-09-25 14:59:47 +02007937 priv->port_list[index] = port;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007938 return 0;
7939
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007940err_free_port_pcpu:
7941 free_percpu(port->pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007942err_free_txq_pcpu:
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007943 for (i = 0; i < port->ntxqs; i++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007944 free_percpu(port->txqs[i]->pcpu);
7945err_free_stats:
7946 free_percpu(port->stats);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007947err_free_irq:
7948 if (port->link_irq)
7949 irq_dispose_mapping(port->link_irq);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007950err_deinit_qvecs:
7951 mvpp2_queue_vectors_deinit(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007952err_free_netdev:
Peter Chenccb80392016-08-01 15:02:37 +08007953 of_node_put(phy_node);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007954 free_netdev(dev);
7955 return err;
7956}
7957
7958/* Ports removal routine */
7959static void mvpp2_port_remove(struct mvpp2_port *port)
7960{
7961 int i;
7962
7963 unregister_netdev(port->dev);
Peter Chenccb80392016-08-01 15:02:37 +08007964 of_node_put(port->phy_node);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007965 free_percpu(port->pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007966 free_percpu(port->stats);
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007967 for (i = 0; i < port->ntxqs; i++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007968 free_percpu(port->txqs[i]->pcpu);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007969 mvpp2_queue_vectors_deinit(port);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007970 if (port->link_irq)
7971 irq_dispose_mapping(port->link_irq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007972 free_netdev(port->dev);
7973}
7974
7975/* Initialize decoding windows */
7976static void mvpp2_conf_mbus_windows(const struct mbus_dram_target_info *dram,
7977 struct mvpp2 *priv)
7978{
7979 u32 win_enable;
7980 int i;
7981
7982 for (i = 0; i < 6; i++) {
7983 mvpp2_write(priv, MVPP2_WIN_BASE(i), 0);
7984 mvpp2_write(priv, MVPP2_WIN_SIZE(i), 0);
7985
7986 if (i < 4)
7987 mvpp2_write(priv, MVPP2_WIN_REMAP(i), 0);
7988 }
7989
7990 win_enable = 0;
7991
7992 for (i = 0; i < dram->num_cs; i++) {
7993 const struct mbus_dram_window *cs = dram->cs + i;
7994
7995 mvpp2_write(priv, MVPP2_WIN_BASE(i),
7996 (cs->base & 0xffff0000) | (cs->mbus_attr << 8) |
7997 dram->mbus_dram_target_id);
7998
7999 mvpp2_write(priv, MVPP2_WIN_SIZE(i),
8000 (cs->size - 1) & 0xffff0000);
8001
8002 win_enable |= (1 << i);
8003 }
8004
8005 mvpp2_write(priv, MVPP2_BASE_ADDR_ENABLE, win_enable);
8006}
8007
8008/* Initialize Rx FIFO's */
8009static void mvpp2_rx_fifo_init(struct mvpp2 *priv)
8010{
8011 int port;
8012
8013 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
8014 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
Antoine Tenart2d1d7df2017-10-30 11:23:28 +01008015 MVPP2_RX_FIFO_PORT_DATA_SIZE_4KB);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008016 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
Antoine Tenart2d1d7df2017-10-30 11:23:28 +01008017 MVPP2_RX_FIFO_PORT_ATTR_SIZE_4KB);
8018 }
8019
8020 mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
8021 MVPP2_RX_FIFO_PORT_MIN_PKT);
8022 mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
8023}
8024
8025static void mvpp22_rx_fifo_init(struct mvpp2 *priv)
8026{
8027 int port;
8028
8029 /* The FIFO size parameters are set depending on the maximum speed a
8030 * given port can handle:
8031 * - Port 0: 10Gbps
8032 * - Port 1: 2.5Gbps
8033 * - Ports 2 and 3: 1Gbps
8034 */
8035
8036 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(0),
8037 MVPP2_RX_FIFO_PORT_DATA_SIZE_32KB);
8038 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(0),
8039 MVPP2_RX_FIFO_PORT_ATTR_SIZE_32KB);
8040
8041 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(1),
8042 MVPP2_RX_FIFO_PORT_DATA_SIZE_8KB);
8043 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(1),
8044 MVPP2_RX_FIFO_PORT_ATTR_SIZE_8KB);
8045
8046 for (port = 2; port < MVPP2_MAX_PORTS; port++) {
8047 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
8048 MVPP2_RX_FIFO_PORT_DATA_SIZE_4KB);
8049 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
8050 MVPP2_RX_FIFO_PORT_ATTR_SIZE_4KB);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008051 }
8052
8053 mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
8054 MVPP2_RX_FIFO_PORT_MIN_PKT);
8055 mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
8056}
8057
Antoine Tenart7c10f972017-10-30 11:23:29 +01008058/* Initialize Tx FIFO's */
8059static void mvpp22_tx_fifo_init(struct mvpp2 *priv)
8060{
8061 int port;
8062
8063 for (port = 0; port < MVPP2_MAX_PORTS; port++)
8064 mvpp2_write(priv, MVPP22_TX_FIFO_SIZE_REG(port),
8065 MVPP22_TX_FIFO_DATA_SIZE_3KB);
8066}
8067
Thomas Petazzoni6763ce32017-03-07 16:53:15 +01008068static void mvpp2_axi_init(struct mvpp2 *priv)
8069{
8070 u32 val, rdval, wrval;
8071
8072 mvpp2_write(priv, MVPP22_BM_ADDR_HIGH_RLS_REG, 0x0);
8073
8074 /* AXI Bridge Configuration */
8075
8076 rdval = MVPP22_AXI_CODE_CACHE_RD_CACHE
8077 << MVPP22_AXI_ATTR_CACHE_OFFS;
8078 rdval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
8079 << MVPP22_AXI_ATTR_DOMAIN_OFFS;
8080
8081 wrval = MVPP22_AXI_CODE_CACHE_WR_CACHE
8082 << MVPP22_AXI_ATTR_CACHE_OFFS;
8083 wrval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
8084 << MVPP22_AXI_ATTR_DOMAIN_OFFS;
8085
8086 /* BM */
8087 mvpp2_write(priv, MVPP22_AXI_BM_WR_ATTR_REG, wrval);
8088 mvpp2_write(priv, MVPP22_AXI_BM_RD_ATTR_REG, rdval);
8089
8090 /* Descriptors */
8091 mvpp2_write(priv, MVPP22_AXI_AGGRQ_DESCR_RD_ATTR_REG, rdval);
8092 mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_WR_ATTR_REG, wrval);
8093 mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_RD_ATTR_REG, rdval);
8094 mvpp2_write(priv, MVPP22_AXI_RXQ_DESCR_WR_ATTR_REG, wrval);
8095
8096 /* Buffer Data */
8097 mvpp2_write(priv, MVPP22_AXI_TX_DATA_RD_ATTR_REG, rdval);
8098 mvpp2_write(priv, MVPP22_AXI_RX_DATA_WR_ATTR_REG, wrval);
8099
8100 val = MVPP22_AXI_CODE_CACHE_NON_CACHE
8101 << MVPP22_AXI_CODE_CACHE_OFFS;
8102 val |= MVPP22_AXI_CODE_DOMAIN_SYSTEM
8103 << MVPP22_AXI_CODE_DOMAIN_OFFS;
8104 mvpp2_write(priv, MVPP22_AXI_RD_NORMAL_CODE_REG, val);
8105 mvpp2_write(priv, MVPP22_AXI_WR_NORMAL_CODE_REG, val);
8106
8107 val = MVPP22_AXI_CODE_CACHE_RD_CACHE
8108 << MVPP22_AXI_CODE_CACHE_OFFS;
8109 val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
8110 << MVPP22_AXI_CODE_DOMAIN_OFFS;
8111
8112 mvpp2_write(priv, MVPP22_AXI_RD_SNOOP_CODE_REG, val);
8113
8114 val = MVPP22_AXI_CODE_CACHE_WR_CACHE
8115 << MVPP22_AXI_CODE_CACHE_OFFS;
8116 val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
8117 << MVPP22_AXI_CODE_DOMAIN_OFFS;
8118
8119 mvpp2_write(priv, MVPP22_AXI_WR_SNOOP_CODE_REG, val);
8120}
8121
Marcin Wojtas3f518502014-07-10 16:52:13 -03008122/* Initialize network controller common part HW */
8123static int mvpp2_init(struct platform_device *pdev, struct mvpp2 *priv)
8124{
8125 const struct mbus_dram_target_info *dram_target_info;
8126 int err, i;
Marcin Wojtas08a23752014-07-21 13:48:12 -03008127 u32 val;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008128
Marcin Wojtas3f518502014-07-10 16:52:13 -03008129 /* MBUS windows configuration */
8130 dram_target_info = mv_mbus_dram_info();
8131 if (dram_target_info)
8132 mvpp2_conf_mbus_windows(dram_target_info, priv);
8133
Thomas Petazzoni6763ce32017-03-07 16:53:15 +01008134 if (priv->hw_version == MVPP22)
8135 mvpp2_axi_init(priv);
8136
Marcin Wojtas08a23752014-07-21 13:48:12 -03008137 /* Disable HW PHY polling */
Thomas Petazzoni26975822017-03-07 16:53:14 +01008138 if (priv->hw_version == MVPP21) {
8139 val = readl(priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
8140 val |= MVPP2_PHY_AN_STOP_SMI0_MASK;
8141 writel(val, priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
8142 } else {
8143 val = readl(priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
8144 val &= ~MVPP22_SMI_POLLING_EN;
8145 writel(val, priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
8146 }
Marcin Wojtas08a23752014-07-21 13:48:12 -03008147
Marcin Wojtas3f518502014-07-10 16:52:13 -03008148 /* Allocate and initialize aggregated TXQs */
8149 priv->aggr_txqs = devm_kcalloc(&pdev->dev, num_present_cpus(),
Markus Elfringd7ce3ce2017-04-17 08:48:23 +02008150 sizeof(*priv->aggr_txqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03008151 GFP_KERNEL);
8152 if (!priv->aggr_txqs)
8153 return -ENOMEM;
8154
8155 for_each_present_cpu(i) {
8156 priv->aggr_txqs[i].id = i;
8157 priv->aggr_txqs[i].size = MVPP2_AGGR_TXQ_SIZE;
Antoine Ténart85affd72017-08-23 09:46:55 +02008158 err = mvpp2_aggr_txq_init(pdev, &priv->aggr_txqs[i], i, priv);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008159 if (err < 0)
8160 return err;
8161 }
8162
Antoine Tenart7c10f972017-10-30 11:23:29 +01008163 /* Fifo Init */
8164 if (priv->hw_version == MVPP21) {
Antoine Tenart2d1d7df2017-10-30 11:23:28 +01008165 mvpp2_rx_fifo_init(priv);
Antoine Tenart7c10f972017-10-30 11:23:29 +01008166 } else {
Antoine Tenart2d1d7df2017-10-30 11:23:28 +01008167 mvpp22_rx_fifo_init(priv);
Antoine Tenart7c10f972017-10-30 11:23:29 +01008168 mvpp22_tx_fifo_init(priv);
8169 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03008170
Thomas Petazzoni26975822017-03-07 16:53:14 +01008171 if (priv->hw_version == MVPP21)
8172 writel(MVPP2_EXT_GLOBAL_CTRL_DEFAULT,
8173 priv->lms_base + MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008174
8175 /* Allow cache snoop when transmiting packets */
8176 mvpp2_write(priv, MVPP2_TX_SNOOP_REG, 0x1);
8177
8178 /* Buffer Manager initialization */
8179 err = mvpp2_bm_init(pdev, priv);
8180 if (err < 0)
8181 return err;
8182
8183 /* Parser default initialization */
8184 err = mvpp2_prs_default_init(pdev, priv);
8185 if (err < 0)
8186 return err;
8187
8188 /* Classifier default initialization */
8189 mvpp2_cls_init(priv);
8190
8191 return 0;
8192}
8193
8194static int mvpp2_probe(struct platform_device *pdev)
8195{
8196 struct device_node *dn = pdev->dev.of_node;
8197 struct device_node *port_node;
8198 struct mvpp2 *priv;
8199 struct resource *res;
Thomas Petazzonia7868412017-03-07 16:53:13 +01008200 void __iomem *base;
Miquel Raynal118d6292017-11-06 22:56:53 +01008201 int i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008202 int err;
8203
Markus Elfring0b92e592017-04-17 08:38:32 +02008204 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008205 if (!priv)
8206 return -ENOMEM;
8207
Thomas Petazzonifaca9242017-03-07 16:53:06 +01008208 priv->hw_version =
8209 (unsigned long)of_device_get_match_data(&pdev->dev);
8210
Marcin Wojtas3f518502014-07-10 16:52:13 -03008211 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thomas Petazzonia7868412017-03-07 16:53:13 +01008212 base = devm_ioremap_resource(&pdev->dev, res);
8213 if (IS_ERR(base))
8214 return PTR_ERR(base);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008215
Thomas Petazzonia7868412017-03-07 16:53:13 +01008216 if (priv->hw_version == MVPP21) {
8217 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
8218 priv->lms_base = devm_ioremap_resource(&pdev->dev, res);
8219 if (IS_ERR(priv->lms_base))
8220 return PTR_ERR(priv->lms_base);
8221 } else {
8222 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
8223 priv->iface_base = devm_ioremap_resource(&pdev->dev, res);
8224 if (IS_ERR(priv->iface_base))
8225 return PTR_ERR(priv->iface_base);
Antoine Ténartf84bf382017-08-22 19:08:27 +02008226
8227 priv->sysctrl_base =
8228 syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
8229 "marvell,system-controller");
8230 if (IS_ERR(priv->sysctrl_base))
8231 /* The system controller regmap is optional for dt
8232 * compatibility reasons. When not provided, the
8233 * configuration of the GoP relies on the
8234 * firmware/bootloader.
8235 */
8236 priv->sysctrl_base = NULL;
Thomas Petazzonia7868412017-03-07 16:53:13 +01008237 }
8238
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02008239 for (i = 0; i < MVPP2_MAX_THREADS; i++) {
Thomas Petazzonia7868412017-03-07 16:53:13 +01008240 u32 addr_space_sz;
8241
8242 addr_space_sz = (priv->hw_version == MVPP21 ?
8243 MVPP21_ADDR_SPACE_SZ : MVPP22_ADDR_SPACE_SZ);
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02008244 priv->swth_base[i] = base + i * addr_space_sz;
Thomas Petazzonia7868412017-03-07 16:53:13 +01008245 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03008246
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01008247 if (priv->hw_version == MVPP21)
8248 priv->max_port_rxqs = 8;
8249 else
8250 priv->max_port_rxqs = 32;
8251
Marcin Wojtas3f518502014-07-10 16:52:13 -03008252 priv->pp_clk = devm_clk_get(&pdev->dev, "pp_clk");
8253 if (IS_ERR(priv->pp_clk))
8254 return PTR_ERR(priv->pp_clk);
8255 err = clk_prepare_enable(priv->pp_clk);
8256 if (err < 0)
8257 return err;
8258
8259 priv->gop_clk = devm_clk_get(&pdev->dev, "gop_clk");
8260 if (IS_ERR(priv->gop_clk)) {
8261 err = PTR_ERR(priv->gop_clk);
8262 goto err_pp_clk;
8263 }
8264 err = clk_prepare_enable(priv->gop_clk);
8265 if (err < 0)
8266 goto err_pp_clk;
8267
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008268 if (priv->hw_version == MVPP22) {
8269 priv->mg_clk = devm_clk_get(&pdev->dev, "mg_clk");
8270 if (IS_ERR(priv->mg_clk)) {
8271 err = PTR_ERR(priv->mg_clk);
8272 goto err_gop_clk;
8273 }
8274
8275 err = clk_prepare_enable(priv->mg_clk);
8276 if (err < 0)
8277 goto err_gop_clk;
Gregory CLEMENT4792ea02017-09-29 14:27:39 +02008278
8279 priv->axi_clk = devm_clk_get(&pdev->dev, "axi_clk");
8280 if (IS_ERR(priv->axi_clk)) {
8281 err = PTR_ERR(priv->axi_clk);
8282 if (err == -EPROBE_DEFER)
8283 goto err_gop_clk;
8284 priv->axi_clk = NULL;
8285 } else {
8286 err = clk_prepare_enable(priv->axi_clk);
8287 if (err < 0)
8288 goto err_gop_clk;
8289 }
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008290 }
8291
Marcin Wojtas3f518502014-07-10 16:52:13 -03008292 /* Get system's tclk rate */
8293 priv->tclk = clk_get_rate(priv->pp_clk);
8294
Thomas Petazzoni2067e0a2017-03-07 16:53:19 +01008295 if (priv->hw_version == MVPP22) {
8296 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(40));
8297 if (err)
8298 goto err_mg_clk;
8299 /* Sadly, the BM pools all share the same register to
8300 * store the high 32 bits of their address. So they
8301 * must all have the same high 32 bits, which forces
8302 * us to restrict coherent memory to DMA_BIT_MASK(32).
8303 */
8304 err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
8305 if (err)
8306 goto err_mg_clk;
8307 }
8308
Marcin Wojtas3f518502014-07-10 16:52:13 -03008309 /* Initialize network controller */
8310 err = mvpp2_init(pdev, priv);
8311 if (err < 0) {
8312 dev_err(&pdev->dev, "failed to initialize controller\n");
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008313 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008314 }
8315
Miquel Raynal118d6292017-11-06 22:56:53 +01008316 priv->port_count = of_get_available_child_count(dn);
8317 if (priv->port_count == 0) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03008318 dev_err(&pdev->dev, "no ports enabled\n");
Wei Yongjun575a1932014-07-20 22:02:43 +08008319 err = -ENODEV;
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008320 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008321 }
8322
Miquel Raynal118d6292017-11-06 22:56:53 +01008323 priv->port_list = devm_kcalloc(&pdev->dev, priv->port_count,
Markus Elfring0b92e592017-04-17 08:38:32 +02008324 sizeof(*priv->port_list),
8325 GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008326 if (!priv->port_list) {
8327 err = -ENOMEM;
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008328 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008329 }
8330
8331 /* Initialize ports */
Yan Markman6bf69a12017-09-25 14:59:47 +02008332 i = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008333 for_each_available_child_of_node(dn, port_node) {
Yan Markman6bf69a12017-09-25 14:59:47 +02008334 err = mvpp2_port_probe(pdev, port_node, priv, i);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008335 if (err < 0)
Antoine Tenart26146b02017-11-28 14:19:49 +01008336 goto err_port_probe;
Yan Markman6bf69a12017-09-25 14:59:47 +02008337 i++;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008338 }
8339
Miquel Raynal118d6292017-11-06 22:56:53 +01008340 /* Statistics must be gathered regularly because some of them (like
8341 * packets counters) are 32-bit registers and could overflow quite
8342 * quickly. For instance, a 10Gb link used at full bandwidth with the
8343 * smallest packets (64B) will overflow a 32-bit counter in less than
8344 * 30 seconds. Then, use a workqueue to fill 64-bit counters.
8345 */
Miquel Raynal118d6292017-11-06 22:56:53 +01008346 snprintf(priv->queue_name, sizeof(priv->queue_name),
8347 "stats-wq-%s%s", netdev_name(priv->port_list[0]->dev),
8348 priv->port_count > 1 ? "+" : "");
8349 priv->stats_queue = create_singlethread_workqueue(priv->queue_name);
8350 if (!priv->stats_queue) {
8351 err = -ENOMEM;
Antoine Tenart26146b02017-11-28 14:19:49 +01008352 goto err_port_probe;
Miquel Raynal118d6292017-11-06 22:56:53 +01008353 }
8354
Marcin Wojtas3f518502014-07-10 16:52:13 -03008355 platform_set_drvdata(pdev, priv);
8356 return 0;
8357
Antoine Tenart26146b02017-11-28 14:19:49 +01008358err_port_probe:
8359 i = 0;
8360 for_each_available_child_of_node(dn, port_node) {
8361 if (priv->port_list[i])
8362 mvpp2_port_remove(priv->port_list[i]);
8363 i++;
8364 }
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008365err_mg_clk:
Gregory CLEMENT4792ea02017-09-29 14:27:39 +02008366 clk_disable_unprepare(priv->axi_clk);
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008367 if (priv->hw_version == MVPP22)
8368 clk_disable_unprepare(priv->mg_clk);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008369err_gop_clk:
8370 clk_disable_unprepare(priv->gop_clk);
8371err_pp_clk:
8372 clk_disable_unprepare(priv->pp_clk);
8373 return err;
8374}
8375
8376static int mvpp2_remove(struct platform_device *pdev)
8377{
8378 struct mvpp2 *priv = platform_get_drvdata(pdev);
8379 struct device_node *dn = pdev->dev.of_node;
8380 struct device_node *port_node;
8381 int i = 0;
8382
Miquel Raynale5c500e2017-11-08 08:59:40 +01008383 flush_workqueue(priv->stats_queue);
Miquel Raynal118d6292017-11-06 22:56:53 +01008384 destroy_workqueue(priv->stats_queue);
Miquel Raynal118d6292017-11-06 22:56:53 +01008385
Marcin Wojtas3f518502014-07-10 16:52:13 -03008386 for_each_available_child_of_node(dn, port_node) {
Miquel Raynale5c500e2017-11-08 08:59:40 +01008387 if (priv->port_list[i]) {
8388 mutex_destroy(&priv->port_list[i]->gather_stats_lock);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008389 mvpp2_port_remove(priv->port_list[i]);
Miquel Raynale5c500e2017-11-08 08:59:40 +01008390 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03008391 i++;
8392 }
8393
8394 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
8395 struct mvpp2_bm_pool *bm_pool = &priv->bm_pools[i];
8396
8397 mvpp2_bm_pool_destroy(pdev, priv, bm_pool);
8398 }
8399
8400 for_each_present_cpu(i) {
8401 struct mvpp2_tx_queue *aggr_txq = &priv->aggr_txqs[i];
8402
8403 dma_free_coherent(&pdev->dev,
8404 MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
8405 aggr_txq->descs,
Thomas Petazzoni20396132017-03-07 16:53:00 +01008406 aggr_txq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008407 }
8408
Gregory CLEMENT4792ea02017-09-29 14:27:39 +02008409 clk_disable_unprepare(priv->axi_clk);
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008410 clk_disable_unprepare(priv->mg_clk);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008411 clk_disable_unprepare(priv->pp_clk);
8412 clk_disable_unprepare(priv->gop_clk);
8413
8414 return 0;
8415}
8416
8417static const struct of_device_id mvpp2_match[] = {
Thomas Petazzonifaca9242017-03-07 16:53:06 +01008418 {
8419 .compatible = "marvell,armada-375-pp2",
8420 .data = (void *)MVPP21,
8421 },
Thomas Petazzonifc5e1552017-03-07 16:53:20 +01008422 {
8423 .compatible = "marvell,armada-7k-pp22",
8424 .data = (void *)MVPP22,
8425 },
Marcin Wojtas3f518502014-07-10 16:52:13 -03008426 { }
8427};
8428MODULE_DEVICE_TABLE(of, mvpp2_match);
8429
8430static struct platform_driver mvpp2_driver = {
8431 .probe = mvpp2_probe,
8432 .remove = mvpp2_remove,
8433 .driver = {
8434 .name = MVPP2_DRIVER_NAME,
8435 .of_match_table = mvpp2_match,
8436 },
8437};
8438
8439module_platform_driver(mvpp2_driver);
8440
8441MODULE_DESCRIPTION("Marvell PPv2 Ethernet Driver - www.marvell.com");
8442MODULE_AUTHOR("Marcin Wojtas <mw@semihalf.com>");
Ezequiel Garciac6340992014-07-14 10:34:47 -03008443MODULE_LICENSE("GPL v2");