blob: 6c20e811f9732ee7b1e432827070e36ede6eb688 [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
88#define MVPP22_RSS_INDEX_TABLE_ENTRY(idx) ((idx) << 8)
89#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 */
507#define MVPP2_MAX_RXD 128
508
509/* Max number of Tx descriptors */
510#define MVPP2_MAX_TXD 1024
511
512/* Amount of Tx descriptors that can be reserved at once by CPU */
513#define MVPP2_CPU_DESC_CHUNK 64
514
515/* Max number of Tx descriptors in each aggregated queue */
516#define MVPP2_AGGR_TXQ_SIZE 256
517
518/* Descriptor aligned size */
519#define MVPP2_DESC_ALIGNED_SIZE 32
520
521/* Descriptor alignment mask */
522#define MVPP2_TX_DESC_ALIGN (MVPP2_DESC_ALIGNED_SIZE - 1)
523
524/* RX FIFO constants */
Antoine Tenart2d1d7df2017-10-30 11:23:28 +0100525#define MVPP2_RX_FIFO_PORT_DATA_SIZE_32KB 0x8000
526#define MVPP2_RX_FIFO_PORT_DATA_SIZE_8KB 0x2000
527#define MVPP2_RX_FIFO_PORT_DATA_SIZE_4KB 0x1000
528#define MVPP2_RX_FIFO_PORT_ATTR_SIZE_32KB 0x200
529#define MVPP2_RX_FIFO_PORT_ATTR_SIZE_8KB 0x80
530#define MVPP2_RX_FIFO_PORT_ATTR_SIZE_4KB 0x40
531#define MVPP2_RX_FIFO_PORT_MIN_PKT 0x80
Marcin Wojtas3f518502014-07-10 16:52:13 -0300532
Antoine Tenart7c10f972017-10-30 11:23:29 +0100533/* TX FIFO constants */
534#define MVPP22_TX_FIFO_DATA_SIZE_10KB 0xa
535#define MVPP22_TX_FIFO_DATA_SIZE_3KB 0x3
536
Marcin Wojtas3f518502014-07-10 16:52:13 -0300537/* RX buffer constants */
538#define MVPP2_SKB_SHINFO_SIZE \
539 SKB_DATA_ALIGN(sizeof(struct skb_shared_info))
540
541#define MVPP2_RX_PKT_SIZE(mtu) \
542 ALIGN((mtu) + MVPP2_MH_SIZE + MVPP2_VLAN_TAG_LEN + \
Jisheng Zhang4a0a12d2016-04-01 17:11:05 +0800543 ETH_HLEN + ETH_FCS_LEN, cache_line_size())
Marcin Wojtas3f518502014-07-10 16:52:13 -0300544
545#define MVPP2_RX_BUF_SIZE(pkt_size) ((pkt_size) + NET_SKB_PAD)
546#define MVPP2_RX_TOTAL_SIZE(buf_size) ((buf_size) + MVPP2_SKB_SHINFO_SIZE)
547#define MVPP2_RX_MAX_PKT_SIZE(total_size) \
548 ((total_size) - NET_SKB_PAD - MVPP2_SKB_SHINFO_SIZE)
549
550#define MVPP2_BIT_TO_BYTE(bit) ((bit) / 8)
551
552/* IPv6 max L3 address size */
553#define MVPP2_MAX_L3_ADDR_SIZE 16
554
555/* Port flags */
556#define MVPP2_F_LOOPBACK BIT(0)
557
558/* Marvell tag types */
559enum mvpp2_tag_type {
560 MVPP2_TAG_TYPE_NONE = 0,
561 MVPP2_TAG_TYPE_MH = 1,
562 MVPP2_TAG_TYPE_DSA = 2,
563 MVPP2_TAG_TYPE_EDSA = 3,
564 MVPP2_TAG_TYPE_VLAN = 4,
565 MVPP2_TAG_TYPE_LAST = 5
566};
567
568/* Parser constants */
569#define MVPP2_PRS_TCAM_SRAM_SIZE 256
570#define MVPP2_PRS_TCAM_WORDS 6
571#define MVPP2_PRS_SRAM_WORDS 4
572#define MVPP2_PRS_FLOW_ID_SIZE 64
573#define MVPP2_PRS_FLOW_ID_MASK 0x3f
574#define MVPP2_PRS_TCAM_ENTRY_INVALID 1
575#define MVPP2_PRS_TCAM_DSA_TAGGED_BIT BIT(5)
576#define MVPP2_PRS_IPV4_HEAD 0x40
577#define MVPP2_PRS_IPV4_HEAD_MASK 0xf0
578#define MVPP2_PRS_IPV4_MC 0xe0
579#define MVPP2_PRS_IPV4_MC_MASK 0xf0
580#define MVPP2_PRS_IPV4_BC_MASK 0xff
581#define MVPP2_PRS_IPV4_IHL 0x5
582#define MVPP2_PRS_IPV4_IHL_MASK 0xf
583#define MVPP2_PRS_IPV6_MC 0xff
584#define MVPP2_PRS_IPV6_MC_MASK 0xff
585#define MVPP2_PRS_IPV6_HOP_MASK 0xff
586#define MVPP2_PRS_TCAM_PROTO_MASK 0xff
587#define MVPP2_PRS_TCAM_PROTO_MASK_L 0x3f
588#define MVPP2_PRS_DBL_VLANS_MAX 100
589
590/* Tcam structure:
591 * - lookup ID - 4 bits
592 * - port ID - 1 byte
593 * - additional information - 1 byte
594 * - header data - 8 bytes
595 * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(5)->(0).
596 */
597#define MVPP2_PRS_AI_BITS 8
598#define MVPP2_PRS_PORT_MASK 0xff
599#define MVPP2_PRS_LU_MASK 0xf
600#define MVPP2_PRS_TCAM_DATA_BYTE(offs) \
601 (((offs) - ((offs) % 2)) * 2 + ((offs) % 2))
602#define MVPP2_PRS_TCAM_DATA_BYTE_EN(offs) \
603 (((offs) * 2) - ((offs) % 2) + 2)
604#define MVPP2_PRS_TCAM_AI_BYTE 16
605#define MVPP2_PRS_TCAM_PORT_BYTE 17
606#define MVPP2_PRS_TCAM_LU_BYTE 20
607#define MVPP2_PRS_TCAM_EN_OFFS(offs) ((offs) + 2)
608#define MVPP2_PRS_TCAM_INV_WORD 5
609/* Tcam entries ID */
610#define MVPP2_PE_DROP_ALL 0
611#define MVPP2_PE_FIRST_FREE_TID 1
612#define MVPP2_PE_LAST_FREE_TID (MVPP2_PRS_TCAM_SRAM_SIZE - 31)
613#define MVPP2_PE_IP6_EXT_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 30)
614#define MVPP2_PE_MAC_MC_IP6 (MVPP2_PRS_TCAM_SRAM_SIZE - 29)
615#define MVPP2_PE_IP6_ADDR_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 28)
616#define MVPP2_PE_IP4_ADDR_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 27)
617#define MVPP2_PE_LAST_DEFAULT_FLOW (MVPP2_PRS_TCAM_SRAM_SIZE - 26)
618#define MVPP2_PE_FIRST_DEFAULT_FLOW (MVPP2_PRS_TCAM_SRAM_SIZE - 19)
619#define MVPP2_PE_EDSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 18)
620#define MVPP2_PE_EDSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 17)
621#define MVPP2_PE_DSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 16)
622#define MVPP2_PE_DSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 15)
623#define MVPP2_PE_ETYPE_EDSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 14)
624#define MVPP2_PE_ETYPE_EDSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 13)
625#define MVPP2_PE_ETYPE_DSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 12)
626#define MVPP2_PE_ETYPE_DSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 11)
627#define MVPP2_PE_MH_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 10)
628#define MVPP2_PE_DSA_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 9)
629#define MVPP2_PE_IP6_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 8)
630#define MVPP2_PE_IP4_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 7)
631#define MVPP2_PE_ETH_TYPE_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 6)
632#define MVPP2_PE_VLAN_DBL (MVPP2_PRS_TCAM_SRAM_SIZE - 5)
633#define MVPP2_PE_VLAN_NONE (MVPP2_PRS_TCAM_SRAM_SIZE - 4)
634#define MVPP2_PE_MAC_MC_ALL (MVPP2_PRS_TCAM_SRAM_SIZE - 3)
635#define MVPP2_PE_MAC_PROMISCUOUS (MVPP2_PRS_TCAM_SRAM_SIZE - 2)
636#define MVPP2_PE_MAC_NON_PROMISCUOUS (MVPP2_PRS_TCAM_SRAM_SIZE - 1)
637
638/* Sram structure
639 * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(3)->(0).
640 */
641#define MVPP2_PRS_SRAM_RI_OFFS 0
642#define MVPP2_PRS_SRAM_RI_WORD 0
643#define MVPP2_PRS_SRAM_RI_CTRL_OFFS 32
644#define MVPP2_PRS_SRAM_RI_CTRL_WORD 1
645#define MVPP2_PRS_SRAM_RI_CTRL_BITS 32
646#define MVPP2_PRS_SRAM_SHIFT_OFFS 64
647#define MVPP2_PRS_SRAM_SHIFT_SIGN_BIT 72
648#define MVPP2_PRS_SRAM_UDF_OFFS 73
649#define MVPP2_PRS_SRAM_UDF_BITS 8
650#define MVPP2_PRS_SRAM_UDF_MASK 0xff
651#define MVPP2_PRS_SRAM_UDF_SIGN_BIT 81
652#define MVPP2_PRS_SRAM_UDF_TYPE_OFFS 82
653#define MVPP2_PRS_SRAM_UDF_TYPE_MASK 0x7
654#define MVPP2_PRS_SRAM_UDF_TYPE_L3 1
655#define MVPP2_PRS_SRAM_UDF_TYPE_L4 4
656#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS 85
657#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK 0x3
658#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD 1
659#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP4_ADD 2
660#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP6_ADD 3
661#define MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS 87
662#define MVPP2_PRS_SRAM_OP_SEL_UDF_BITS 2
663#define MVPP2_PRS_SRAM_OP_SEL_UDF_MASK 0x3
664#define MVPP2_PRS_SRAM_OP_SEL_UDF_ADD 0
665#define MVPP2_PRS_SRAM_OP_SEL_UDF_IP4_ADD 2
666#define MVPP2_PRS_SRAM_OP_SEL_UDF_IP6_ADD 3
667#define MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS 89
668#define MVPP2_PRS_SRAM_AI_OFFS 90
669#define MVPP2_PRS_SRAM_AI_CTRL_OFFS 98
670#define MVPP2_PRS_SRAM_AI_CTRL_BITS 8
671#define MVPP2_PRS_SRAM_AI_MASK 0xff
672#define MVPP2_PRS_SRAM_NEXT_LU_OFFS 106
673#define MVPP2_PRS_SRAM_NEXT_LU_MASK 0xf
674#define MVPP2_PRS_SRAM_LU_DONE_BIT 110
675#define MVPP2_PRS_SRAM_LU_GEN_BIT 111
676
677/* Sram result info bits assignment */
678#define MVPP2_PRS_RI_MAC_ME_MASK 0x1
679#define MVPP2_PRS_RI_DSA_MASK 0x2
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100680#define MVPP2_PRS_RI_VLAN_MASK (BIT(2) | BIT(3))
681#define MVPP2_PRS_RI_VLAN_NONE 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300682#define MVPP2_PRS_RI_VLAN_SINGLE BIT(2)
683#define MVPP2_PRS_RI_VLAN_DOUBLE BIT(3)
684#define MVPP2_PRS_RI_VLAN_TRIPLE (BIT(2) | BIT(3))
685#define MVPP2_PRS_RI_CPU_CODE_MASK 0x70
686#define MVPP2_PRS_RI_CPU_CODE_RX_SPEC BIT(4)
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100687#define MVPP2_PRS_RI_L2_CAST_MASK (BIT(9) | BIT(10))
688#define MVPP2_PRS_RI_L2_UCAST 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300689#define MVPP2_PRS_RI_L2_MCAST BIT(9)
690#define MVPP2_PRS_RI_L2_BCAST BIT(10)
691#define MVPP2_PRS_RI_PPPOE_MASK 0x800
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100692#define MVPP2_PRS_RI_L3_PROTO_MASK (BIT(12) | BIT(13) | BIT(14))
693#define MVPP2_PRS_RI_L3_UN 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300694#define MVPP2_PRS_RI_L3_IP4 BIT(12)
695#define MVPP2_PRS_RI_L3_IP4_OPT BIT(13)
696#define MVPP2_PRS_RI_L3_IP4_OTHER (BIT(12) | BIT(13))
697#define MVPP2_PRS_RI_L3_IP6 BIT(14)
698#define MVPP2_PRS_RI_L3_IP6_EXT (BIT(12) | BIT(14))
699#define MVPP2_PRS_RI_L3_ARP (BIT(13) | BIT(14))
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100700#define MVPP2_PRS_RI_L3_ADDR_MASK (BIT(15) | BIT(16))
701#define MVPP2_PRS_RI_L3_UCAST 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300702#define MVPP2_PRS_RI_L3_MCAST BIT(15)
703#define MVPP2_PRS_RI_L3_BCAST (BIT(15) | BIT(16))
704#define MVPP2_PRS_RI_IP_FRAG_MASK 0x20000
Stefan Chulskiaff3da32017-09-25 14:59:46 +0200705#define MVPP2_PRS_RI_IP_FRAG_TRUE BIT(17)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300706#define MVPP2_PRS_RI_UDF3_MASK 0x300000
707#define MVPP2_PRS_RI_UDF3_RX_SPECIAL BIT(21)
708#define MVPP2_PRS_RI_L4_PROTO_MASK 0x1c00000
709#define MVPP2_PRS_RI_L4_TCP BIT(22)
710#define MVPP2_PRS_RI_L4_UDP BIT(23)
711#define MVPP2_PRS_RI_L4_OTHER (BIT(22) | BIT(23))
712#define MVPP2_PRS_RI_UDF7_MASK 0x60000000
713#define MVPP2_PRS_RI_UDF7_IP6_LITE BIT(29)
714#define MVPP2_PRS_RI_DROP_MASK 0x80000000
715
716/* Sram additional info bits assignment */
717#define MVPP2_PRS_IPV4_DIP_AI_BIT BIT(0)
718#define MVPP2_PRS_IPV6_NO_EXT_AI_BIT BIT(0)
719#define MVPP2_PRS_IPV6_EXT_AI_BIT BIT(1)
720#define MVPP2_PRS_IPV6_EXT_AH_AI_BIT BIT(2)
721#define MVPP2_PRS_IPV6_EXT_AH_LEN_AI_BIT BIT(3)
722#define MVPP2_PRS_IPV6_EXT_AH_L4_AI_BIT BIT(4)
723#define MVPP2_PRS_SINGLE_VLAN_AI 0
724#define MVPP2_PRS_DBL_VLAN_AI_BIT BIT(7)
725
726/* DSA/EDSA type */
727#define MVPP2_PRS_TAGGED true
728#define MVPP2_PRS_UNTAGGED false
729#define MVPP2_PRS_EDSA true
730#define MVPP2_PRS_DSA false
731
732/* MAC entries, shadow udf */
733enum mvpp2_prs_udf {
734 MVPP2_PRS_UDF_MAC_DEF,
735 MVPP2_PRS_UDF_MAC_RANGE,
736 MVPP2_PRS_UDF_L2_DEF,
737 MVPP2_PRS_UDF_L2_DEF_COPY,
738 MVPP2_PRS_UDF_L2_USER,
739};
740
741/* Lookup ID */
742enum mvpp2_prs_lookup {
743 MVPP2_PRS_LU_MH,
744 MVPP2_PRS_LU_MAC,
745 MVPP2_PRS_LU_DSA,
746 MVPP2_PRS_LU_VLAN,
747 MVPP2_PRS_LU_L2,
748 MVPP2_PRS_LU_PPPOE,
749 MVPP2_PRS_LU_IP4,
750 MVPP2_PRS_LU_IP6,
751 MVPP2_PRS_LU_FLOWS,
752 MVPP2_PRS_LU_LAST,
753};
754
755/* L3 cast enum */
756enum mvpp2_prs_l3_cast {
757 MVPP2_PRS_L3_UNI_CAST,
758 MVPP2_PRS_L3_MULTI_CAST,
759 MVPP2_PRS_L3_BROAD_CAST
760};
761
762/* Classifier constants */
763#define MVPP2_CLS_FLOWS_TBL_SIZE 512
764#define MVPP2_CLS_FLOWS_TBL_DATA_WORDS 3
765#define MVPP2_CLS_LKP_TBL_SIZE 64
Antoine Tenart1d7d15d2017-10-30 11:23:30 +0100766#define MVPP2_CLS_RX_QUEUES 256
767
768/* RSS constants */
769#define MVPP22_RSS_TABLE_ENTRIES 32
Marcin Wojtas3f518502014-07-10 16:52:13 -0300770
771/* BM constants */
772#define MVPP2_BM_POOLS_NUM 8
773#define MVPP2_BM_LONG_BUF_NUM 1024
774#define MVPP2_BM_SHORT_BUF_NUM 2048
775#define MVPP2_BM_POOL_SIZE_MAX (16*1024 - MVPP2_BM_POOL_PTR_ALIGN/4)
776#define MVPP2_BM_POOL_PTR_ALIGN 128
777#define MVPP2_BM_SWF_LONG_POOL(port) ((port > 2) ? 2 : port)
778#define MVPP2_BM_SWF_SHORT_POOL 3
779
780/* BM cookie (32 bits) definition */
781#define MVPP2_BM_COOKIE_POOL_OFFS 8
782#define MVPP2_BM_COOKIE_CPU_OFFS 24
783
784/* BM short pool packet size
785 * These value assure that for SWF the total number
786 * of bytes allocated for each buffer will be 512
787 */
788#define MVPP2_BM_SHORT_PKT_SIZE MVPP2_RX_MAX_PKT_SIZE(512)
789
Thomas Petazzonia7868412017-03-07 16:53:13 +0100790#define MVPP21_ADDR_SPACE_SZ 0
791#define MVPP22_ADDR_SPACE_SZ SZ_64K
792
Thomas Petazzonidf089aa2017-08-03 10:41:58 +0200793#define MVPP2_MAX_THREADS 8
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +0200794#define MVPP2_MAX_QVECS MVPP2_MAX_THREADS
Thomas Petazzonia7868412017-03-07 16:53:13 +0100795
Marcin Wojtas3f518502014-07-10 16:52:13 -0300796enum mvpp2_bm_type {
797 MVPP2_BM_FREE,
798 MVPP2_BM_SWF_LONG,
799 MVPP2_BM_SWF_SHORT
800};
801
Miquel Raynal118d6292017-11-06 22:56:53 +0100802/* GMAC MIB Counters register definitions */
803#define MVPP21_MIB_COUNTERS_OFFSET 0x1000
804#define MVPP21_MIB_COUNTERS_PORT_SZ 0x400
805#define MVPP22_MIB_COUNTERS_OFFSET 0x0
806#define MVPP22_MIB_COUNTERS_PORT_SZ 0x100
807
808#define MVPP2_MIB_GOOD_OCTETS_RCVD 0x0
809#define MVPP2_MIB_BAD_OCTETS_RCVD 0x8
810#define MVPP2_MIB_CRC_ERRORS_SENT 0xc
811#define MVPP2_MIB_UNICAST_FRAMES_RCVD 0x10
812#define MVPP2_MIB_BROADCAST_FRAMES_RCVD 0x18
813#define MVPP2_MIB_MULTICAST_FRAMES_RCVD 0x1c
814#define MVPP2_MIB_FRAMES_64_OCTETS 0x20
815#define MVPP2_MIB_FRAMES_65_TO_127_OCTETS 0x24
816#define MVPP2_MIB_FRAMES_128_TO_255_OCTETS 0x28
817#define MVPP2_MIB_FRAMES_256_TO_511_OCTETS 0x2c
818#define MVPP2_MIB_FRAMES_512_TO_1023_OCTETS 0x30
819#define MVPP2_MIB_FRAMES_1024_TO_MAX_OCTETS 0x34
820#define MVPP2_MIB_GOOD_OCTETS_SENT 0x38
821#define MVPP2_MIB_UNICAST_FRAMES_SENT 0x40
822#define MVPP2_MIB_MULTICAST_FRAMES_SENT 0x48
823#define MVPP2_MIB_BROADCAST_FRAMES_SENT 0x4c
824#define MVPP2_MIB_FC_SENT 0x54
825#define MVPP2_MIB_FC_RCVD 0x58
826#define MVPP2_MIB_RX_FIFO_OVERRUN 0x5c
827#define MVPP2_MIB_UNDERSIZE_RCVD 0x60
828#define MVPP2_MIB_FRAGMENTS_RCVD 0x64
829#define MVPP2_MIB_OVERSIZE_RCVD 0x68
830#define MVPP2_MIB_JABBER_RCVD 0x6c
831#define MVPP2_MIB_MAC_RCV_ERROR 0x70
832#define MVPP2_MIB_BAD_CRC_EVENT 0x74
833#define MVPP2_MIB_COLLISION 0x78
834#define MVPP2_MIB_LATE_COLLISION 0x7c
835
836#define MVPP2_MIB_COUNTERS_STATS_DELAY (1 * HZ)
837
Marcin Wojtas3f518502014-07-10 16:52:13 -0300838/* Definitions */
839
840/* Shared Packet Processor resources */
841struct mvpp2 {
842 /* Shared registers' base addresses */
Marcin Wojtas3f518502014-07-10 16:52:13 -0300843 void __iomem *lms_base;
Thomas Petazzonia7868412017-03-07 16:53:13 +0100844 void __iomem *iface_base;
845
Thomas Petazzonidf089aa2017-08-03 10:41:58 +0200846 /* On PPv2.2, each "software thread" can access the base
847 * register through a separate address space, each 64 KB apart
848 * from each other. Typically, such address spaces will be
849 * used per CPU.
Thomas Petazzonia7868412017-03-07 16:53:13 +0100850 */
Thomas Petazzonidf089aa2017-08-03 10:41:58 +0200851 void __iomem *swth_base[MVPP2_MAX_THREADS];
Marcin Wojtas3f518502014-07-10 16:52:13 -0300852
Antoine Ténartf84bf382017-08-22 19:08:27 +0200853 /* On PPv2.2, some port control registers are located into the system
854 * controller space. These registers are accessible through a regmap.
855 */
856 struct regmap *sysctrl_base;
857
Marcin Wojtas3f518502014-07-10 16:52:13 -0300858 /* Common clocks */
859 struct clk *pp_clk;
860 struct clk *gop_clk;
Thomas Petazzonifceb55d2017-03-07 16:53:18 +0100861 struct clk *mg_clk;
Gregory CLEMENT4792ea02017-09-29 14:27:39 +0200862 struct clk *axi_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300863
864 /* List of pointers to port structures */
Miquel Raynal118d6292017-11-06 22:56:53 +0100865 int port_count;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300866 struct mvpp2_port **port_list;
867
868 /* Aggregated TXQs */
869 struct mvpp2_tx_queue *aggr_txqs;
870
871 /* BM pools */
872 struct mvpp2_bm_pool *bm_pools;
873
874 /* PRS shadow table */
875 struct mvpp2_prs_shadow *prs_shadow;
876 /* PRS auxiliary table for double vlan entries control */
877 bool *prs_double_vlans;
878
879 /* Tclk value */
880 u32 tclk;
Thomas Petazzonifaca9242017-03-07 16:53:06 +0100881
882 /* HW version */
883 enum { MVPP21, MVPP22 } hw_version;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +0100884
885 /* Maximum number of RXQs per port */
886 unsigned int max_port_rxqs;
Miquel Raynal118d6292017-11-06 22:56:53 +0100887
Miquel Raynale5c500e2017-11-08 08:59:40 +0100888 /* Workqueue to gather hardware statistics */
Miquel Raynal118d6292017-11-06 22:56:53 +0100889 char queue_name[30];
890 struct workqueue_struct *stats_queue;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300891};
892
893struct mvpp2_pcpu_stats {
894 struct u64_stats_sync syncp;
895 u64 rx_packets;
896 u64 rx_bytes;
897 u64 tx_packets;
898 u64 tx_bytes;
899};
900
Marcin Wojtasedc660f2015-08-06 19:00:30 +0200901/* Per-CPU port control */
902struct mvpp2_port_pcpu {
903 struct hrtimer tx_done_timer;
904 bool timer_scheduled;
905 /* Tasklet for egress finalization */
906 struct tasklet_struct tx_done_tasklet;
907};
908
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +0200909struct mvpp2_queue_vector {
910 int irq;
911 struct napi_struct napi;
912 enum { MVPP2_QUEUE_VECTOR_SHARED, MVPP2_QUEUE_VECTOR_PRIVATE } type;
913 int sw_thread_id;
914 u16 sw_thread_mask;
915 int first_rxq;
916 int nrxqs;
917 u32 pending_cause_rx;
918 struct mvpp2_port *port;
919};
920
Marcin Wojtas3f518502014-07-10 16:52:13 -0300921struct mvpp2_port {
922 u8 id;
923
Thomas Petazzonia7868412017-03-07 16:53:13 +0100924 /* Index of the port from the "group of ports" complex point
925 * of view
926 */
927 int gop_id;
928
Antoine Tenartfd3651b2017-09-01 11:04:54 +0200929 int link_irq;
930
Marcin Wojtas3f518502014-07-10 16:52:13 -0300931 struct mvpp2 *priv;
932
933 /* Per-port registers' base address */
934 void __iomem *base;
Miquel Raynal118d6292017-11-06 22:56:53 +0100935 void __iomem *stats_base;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300936
937 struct mvpp2_rx_queue **rxqs;
Thomas Petazzoni09f83972017-08-03 10:41:57 +0200938 unsigned int nrxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300939 struct mvpp2_tx_queue **txqs;
Thomas Petazzoni09f83972017-08-03 10:41:57 +0200940 unsigned int ntxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300941 struct net_device *dev;
942
943 int pkt_size;
944
Marcin Wojtasedc660f2015-08-06 19:00:30 +0200945 /* Per-CPU port control */
946 struct mvpp2_port_pcpu __percpu *pcpu;
947
Marcin Wojtas3f518502014-07-10 16:52:13 -0300948 /* Flags */
949 unsigned long flags;
950
951 u16 tx_ring_size;
952 u16 rx_ring_size;
953 struct mvpp2_pcpu_stats __percpu *stats;
Miquel Raynal118d6292017-11-06 22:56:53 +0100954 u64 *ethtool_stats;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300955
Miquel Raynale5c500e2017-11-08 08:59:40 +0100956 /* Per-port work and its lock to gather hardware statistics */
957 struct mutex gather_stats_lock;
958 struct delayed_work stats_work;
959
Marcin Wojtas3f518502014-07-10 16:52:13 -0300960 phy_interface_t phy_interface;
961 struct device_node *phy_node;
Antoine Tenart542897d2017-08-30 10:29:15 +0200962 struct phy *comphy;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300963 unsigned int link;
964 unsigned int duplex;
965 unsigned int speed;
966
967 struct mvpp2_bm_pool *pool_long;
968 struct mvpp2_bm_pool *pool_short;
969
970 /* Index of first port's physical RXQ */
971 u8 first_rxq;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +0200972
973 struct mvpp2_queue_vector qvecs[MVPP2_MAX_QVECS];
974 unsigned int nqvecs;
Thomas Petazzoni213f4282017-08-03 10:42:00 +0200975 bool has_tx_irqs;
976
977 u32 tx_time_coal;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300978};
979
980/* The mvpp2_tx_desc and mvpp2_rx_desc structures describe the
981 * layout of the transmit and reception DMA descriptors, and their
982 * layout is therefore defined by the hardware design
983 */
984
985#define MVPP2_TXD_L3_OFF_SHIFT 0
986#define MVPP2_TXD_IP_HLEN_SHIFT 8
987#define MVPP2_TXD_L4_CSUM_FRAG BIT(13)
988#define MVPP2_TXD_L4_CSUM_NOT BIT(14)
989#define MVPP2_TXD_IP_CSUM_DISABLE BIT(15)
990#define MVPP2_TXD_PADDING_DISABLE BIT(23)
991#define MVPP2_TXD_L4_UDP BIT(24)
992#define MVPP2_TXD_L3_IP6 BIT(26)
993#define MVPP2_TXD_L_DESC BIT(28)
994#define MVPP2_TXD_F_DESC BIT(29)
995
996#define MVPP2_RXD_ERR_SUMMARY BIT(15)
997#define MVPP2_RXD_ERR_CODE_MASK (BIT(13) | BIT(14))
998#define MVPP2_RXD_ERR_CRC 0x0
999#define MVPP2_RXD_ERR_OVERRUN BIT(13)
1000#define MVPP2_RXD_ERR_RESOURCE (BIT(13) | BIT(14))
1001#define MVPP2_RXD_BM_POOL_ID_OFFS 16
1002#define MVPP2_RXD_BM_POOL_ID_MASK (BIT(16) | BIT(17) | BIT(18))
1003#define MVPP2_RXD_HWF_SYNC BIT(21)
1004#define MVPP2_RXD_L4_CSUM_OK BIT(22)
1005#define MVPP2_RXD_IP4_HEADER_ERR BIT(24)
1006#define MVPP2_RXD_L4_TCP BIT(25)
1007#define MVPP2_RXD_L4_UDP BIT(26)
1008#define MVPP2_RXD_L3_IP4 BIT(28)
1009#define MVPP2_RXD_L3_IP6 BIT(30)
1010#define MVPP2_RXD_BUF_HDR BIT(31)
1011
Thomas Petazzoni054f6372017-03-07 16:53:07 +01001012/* HW TX descriptor for PPv2.1 */
1013struct mvpp21_tx_desc {
Marcin Wojtas3f518502014-07-10 16:52:13 -03001014 u32 command; /* Options used by HW for packet transmitting.*/
1015 u8 packet_offset; /* the offset from the buffer beginning */
1016 u8 phys_txq; /* destination queue ID */
1017 u16 data_size; /* data size of transmitted packet in bytes */
Thomas Petazzoni20396132017-03-07 16:53:00 +01001018 u32 buf_dma_addr; /* physical addr of transmitted buffer */
Marcin Wojtas3f518502014-07-10 16:52:13 -03001019 u32 buf_cookie; /* cookie for access to TX buffer in tx path */
1020 u32 reserved1[3]; /* hw_cmd (for future use, BM, PON, PNC) */
1021 u32 reserved2; /* reserved (for future use) */
1022};
1023
Thomas Petazzoni054f6372017-03-07 16:53:07 +01001024/* HW RX descriptor for PPv2.1 */
1025struct mvpp21_rx_desc {
Marcin Wojtas3f518502014-07-10 16:52:13 -03001026 u32 status; /* info about received packet */
1027 u16 reserved1; /* parser_info (for future use, PnC) */
1028 u16 data_size; /* size of received packet in bytes */
Thomas Petazzoni20396132017-03-07 16:53:00 +01001029 u32 buf_dma_addr; /* physical address of the buffer */
Marcin Wojtas3f518502014-07-10 16:52:13 -03001030 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
1031 u16 reserved2; /* gem_port_id (for future use, PON) */
1032 u16 reserved3; /* csum_l4 (for future use, PnC) */
1033 u8 reserved4; /* bm_qset (for future use, BM) */
1034 u8 reserved5;
1035 u16 reserved6; /* classify_info (for future use, PnC) */
1036 u32 reserved7; /* flow_id (for future use, PnC) */
1037 u32 reserved8;
1038};
1039
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001040/* HW TX descriptor for PPv2.2 */
1041struct mvpp22_tx_desc {
1042 u32 command;
1043 u8 packet_offset;
1044 u8 phys_txq;
1045 u16 data_size;
1046 u64 reserved1;
1047 u64 buf_dma_addr_ptp;
1048 u64 buf_cookie_misc;
1049};
1050
1051/* HW RX descriptor for PPv2.2 */
1052struct mvpp22_rx_desc {
1053 u32 status;
1054 u16 reserved1;
1055 u16 data_size;
1056 u32 reserved2;
1057 u32 reserved3;
1058 u64 buf_dma_addr_key_hash;
1059 u64 buf_cookie_misc;
1060};
1061
Thomas Petazzoni054f6372017-03-07 16:53:07 +01001062/* Opaque type used by the driver to manipulate the HW TX and RX
1063 * descriptors
1064 */
1065struct mvpp2_tx_desc {
1066 union {
1067 struct mvpp21_tx_desc pp21;
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001068 struct mvpp22_tx_desc pp22;
Thomas Petazzoni054f6372017-03-07 16:53:07 +01001069 };
1070};
1071
1072struct mvpp2_rx_desc {
1073 union {
1074 struct mvpp21_rx_desc pp21;
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001075 struct mvpp22_rx_desc pp22;
Thomas Petazzoni054f6372017-03-07 16:53:07 +01001076 };
1077};
1078
Thomas Petazzoni83544912016-12-21 11:28:49 +01001079struct mvpp2_txq_pcpu_buf {
1080 /* Transmitted SKB */
1081 struct sk_buff *skb;
1082
1083 /* Physical address of transmitted buffer */
Thomas Petazzoni20396132017-03-07 16:53:00 +01001084 dma_addr_t dma;
Thomas Petazzoni83544912016-12-21 11:28:49 +01001085
1086 /* Size transmitted */
1087 size_t size;
1088};
1089
Marcin Wojtas3f518502014-07-10 16:52:13 -03001090/* Per-CPU Tx queue control */
1091struct mvpp2_txq_pcpu {
1092 int cpu;
1093
1094 /* Number of Tx DMA descriptors in the descriptor ring */
1095 int size;
1096
1097 /* Number of currently used Tx DMA descriptor in the
1098 * descriptor ring
1099 */
1100 int count;
1101
Antoine Tenart1d17db02017-10-30 11:23:31 +01001102 int wake_threshold;
1103 int stop_threshold;
1104
Marcin Wojtas3f518502014-07-10 16:52:13 -03001105 /* Number of Tx DMA descriptors reserved for each CPU */
1106 int reserved_num;
1107
Thomas Petazzoni83544912016-12-21 11:28:49 +01001108 /* Infos about transmitted buffers */
1109 struct mvpp2_txq_pcpu_buf *buffs;
Marcin Wojtas71ce3912015-08-06 19:00:29 +02001110
Marcin Wojtas3f518502014-07-10 16:52:13 -03001111 /* Index of last TX DMA descriptor that was inserted */
1112 int txq_put_index;
1113
1114 /* Index of the TX DMA descriptor to be cleaned up */
1115 int txq_get_index;
Antoine Ténart186cd4d2017-08-23 09:46:56 +02001116
1117 /* DMA buffer for TSO headers */
1118 char *tso_headers;
1119 dma_addr_t tso_headers_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001120};
1121
1122struct mvpp2_tx_queue {
1123 /* Physical number of this Tx queue */
1124 u8 id;
1125
1126 /* Logical number of this Tx queue */
1127 u8 log_id;
1128
1129 /* Number of Tx DMA descriptors in the descriptor ring */
1130 int size;
1131
1132 /* Number of currently used Tx DMA descriptor in the descriptor ring */
1133 int count;
1134
1135 /* Per-CPU control of physical Tx queues */
1136 struct mvpp2_txq_pcpu __percpu *pcpu;
1137
Marcin Wojtas3f518502014-07-10 16:52:13 -03001138 u32 done_pkts_coal;
1139
1140 /* Virtual address of thex Tx DMA descriptors array */
1141 struct mvpp2_tx_desc *descs;
1142
1143 /* DMA address of the Tx DMA descriptors array */
Thomas Petazzoni20396132017-03-07 16:53:00 +01001144 dma_addr_t descs_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001145
1146 /* Index of the last Tx DMA descriptor */
1147 int last_desc;
1148
1149 /* Index of the next Tx DMA descriptor to process */
1150 int next_desc_to_proc;
1151};
1152
1153struct mvpp2_rx_queue {
1154 /* RX queue number, in the range 0-31 for physical RXQs */
1155 u8 id;
1156
1157 /* Num of rx descriptors in the rx descriptor ring */
1158 int size;
1159
1160 u32 pkts_coal;
1161 u32 time_coal;
1162
1163 /* Virtual address of the RX DMA descriptors array */
1164 struct mvpp2_rx_desc *descs;
1165
1166 /* DMA address of the RX DMA descriptors array */
Thomas Petazzoni20396132017-03-07 16:53:00 +01001167 dma_addr_t descs_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001168
1169 /* Index of the last RX DMA descriptor */
1170 int last_desc;
1171
1172 /* Index of the next RX DMA descriptor to process */
1173 int next_desc_to_proc;
1174
1175 /* ID of port to which physical RXQ is mapped */
1176 int port;
1177
1178 /* Port's logic RXQ number to which physical RXQ is mapped */
1179 int logic_rxq;
1180};
1181
1182union mvpp2_prs_tcam_entry {
1183 u32 word[MVPP2_PRS_TCAM_WORDS];
1184 u8 byte[MVPP2_PRS_TCAM_WORDS * 4];
1185};
1186
1187union mvpp2_prs_sram_entry {
1188 u32 word[MVPP2_PRS_SRAM_WORDS];
1189 u8 byte[MVPP2_PRS_SRAM_WORDS * 4];
1190};
1191
1192struct mvpp2_prs_entry {
1193 u32 index;
1194 union mvpp2_prs_tcam_entry tcam;
1195 union mvpp2_prs_sram_entry sram;
1196};
1197
1198struct mvpp2_prs_shadow {
1199 bool valid;
1200 bool finish;
1201
1202 /* Lookup ID */
1203 int lu;
1204
1205 /* User defined offset */
1206 int udf;
1207
1208 /* Result info */
1209 u32 ri;
1210 u32 ri_mask;
1211};
1212
1213struct mvpp2_cls_flow_entry {
1214 u32 index;
1215 u32 data[MVPP2_CLS_FLOWS_TBL_DATA_WORDS];
1216};
1217
1218struct mvpp2_cls_lookup_entry {
1219 u32 lkpid;
1220 u32 way;
1221 u32 data;
1222};
1223
1224struct mvpp2_bm_pool {
1225 /* Pool number in the range 0-7 */
1226 int id;
1227 enum mvpp2_bm_type type;
1228
1229 /* Buffer Pointers Pool External (BPPE) size */
1230 int size;
Thomas Petazzonid01524d2017-03-07 16:53:09 +01001231 /* BPPE size in bytes */
1232 int size_bytes;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001233 /* Number of buffers for this pool */
1234 int buf_num;
1235 /* Pool buffer size */
1236 int buf_size;
1237 /* Packet size */
1238 int pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01001239 int frag_size;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001240
1241 /* BPPE virtual base address */
1242 u32 *virt_addr;
Thomas Petazzoni20396132017-03-07 16:53:00 +01001243 /* BPPE DMA base address */
1244 dma_addr_t dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001245
1246 /* Ports using BM pool */
1247 u32 port_map;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001248};
1249
Antoine Tenart20920262017-10-23 15:24:30 +02001250#define IS_TSO_HEADER(txq_pcpu, addr) \
1251 ((addr) >= (txq_pcpu)->tso_headers_dma && \
1252 (addr) < (txq_pcpu)->tso_headers_dma + \
1253 (txq_pcpu)->size * TSO_HEADER_SIZE)
1254
Thomas Petazzoni213f4282017-08-03 10:42:00 +02001255/* Queue modes */
1256#define MVPP2_QDIST_SINGLE_MODE 0
1257#define MVPP2_QDIST_MULTI_MODE 1
1258
1259static int queue_mode = MVPP2_QDIST_SINGLE_MODE;
1260
1261module_param(queue_mode, int, 0444);
1262MODULE_PARM_DESC(queue_mode, "Set queue_mode (single=0, multi=1)");
1263
Marcin Wojtas3f518502014-07-10 16:52:13 -03001264#define MVPP2_DRIVER_NAME "mvpp2"
1265#define MVPP2_DRIVER_VERSION "1.0"
1266
1267/* Utility/helper methods */
1268
1269static void mvpp2_write(struct mvpp2 *priv, u32 offset, u32 data)
1270{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001271 writel(data, priv->swth_base[0] + offset);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001272}
1273
1274static u32 mvpp2_read(struct mvpp2 *priv, u32 offset)
1275{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001276 return readl(priv->swth_base[0] + offset);
Thomas Petazzonia7868412017-03-07 16:53:13 +01001277}
1278
1279/* These accessors should be used to access:
1280 *
1281 * - per-CPU registers, where each CPU has its own copy of the
1282 * register.
1283 *
1284 * MVPP2_BM_VIRT_ALLOC_REG
1285 * MVPP2_BM_ADDR_HIGH_ALLOC
1286 * MVPP22_BM_ADDR_HIGH_RLS_REG
1287 * MVPP2_BM_VIRT_RLS_REG
1288 * MVPP2_ISR_RX_TX_CAUSE_REG
1289 * MVPP2_ISR_RX_TX_MASK_REG
1290 * MVPP2_TXQ_NUM_REG
1291 * MVPP2_AGGR_TXQ_UPDATE_REG
1292 * MVPP2_TXQ_RSVD_REQ_REG
1293 * MVPP2_TXQ_RSVD_RSLT_REG
1294 * MVPP2_TXQ_SENT_REG
1295 * MVPP2_RXQ_NUM_REG
1296 *
1297 * - global registers that must be accessed through a specific CPU
1298 * window, because they are related to an access to a per-CPU
1299 * register
1300 *
1301 * MVPP2_BM_PHY_ALLOC_REG (related to MVPP2_BM_VIRT_ALLOC_REG)
1302 * MVPP2_BM_PHY_RLS_REG (related to MVPP2_BM_VIRT_RLS_REG)
1303 * MVPP2_RXQ_THRESH_REG (related to MVPP2_RXQ_NUM_REG)
1304 * MVPP2_RXQ_DESC_ADDR_REG (related to MVPP2_RXQ_NUM_REG)
1305 * MVPP2_RXQ_DESC_SIZE_REG (related to MVPP2_RXQ_NUM_REG)
1306 * MVPP2_RXQ_INDEX_REG (related to MVPP2_RXQ_NUM_REG)
1307 * MVPP2_TXQ_PENDING_REG (related to MVPP2_TXQ_NUM_REG)
1308 * MVPP2_TXQ_DESC_ADDR_REG (related to MVPP2_TXQ_NUM_REG)
1309 * MVPP2_TXQ_DESC_SIZE_REG (related to MVPP2_TXQ_NUM_REG)
1310 * MVPP2_TXQ_INDEX_REG (related to MVPP2_TXQ_NUM_REG)
1311 * MVPP2_TXQ_PENDING_REG (related to MVPP2_TXQ_NUM_REG)
1312 * MVPP2_TXQ_PREF_BUF_REG (related to MVPP2_TXQ_NUM_REG)
1313 * MVPP2_TXQ_PREF_BUF_REG (related to MVPP2_TXQ_NUM_REG)
1314 */
1315static void mvpp2_percpu_write(struct mvpp2 *priv, int cpu,
1316 u32 offset, u32 data)
1317{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001318 writel(data, priv->swth_base[cpu] + offset);
Thomas Petazzonia7868412017-03-07 16:53:13 +01001319}
1320
1321static u32 mvpp2_percpu_read(struct mvpp2 *priv, int cpu,
1322 u32 offset)
1323{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001324 return readl(priv->swth_base[cpu] + offset);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001325}
1326
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001327static dma_addr_t mvpp2_txdesc_dma_addr_get(struct mvpp2_port *port,
1328 struct mvpp2_tx_desc *tx_desc)
1329{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001330 if (port->priv->hw_version == MVPP21)
1331 return tx_desc->pp21.buf_dma_addr;
1332 else
1333 return tx_desc->pp22.buf_dma_addr_ptp & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001334}
1335
1336static void mvpp2_txdesc_dma_addr_set(struct mvpp2_port *port,
1337 struct mvpp2_tx_desc *tx_desc,
1338 dma_addr_t dma_addr)
1339{
Antoine Tenart6eb5d372017-10-30 11:23:33 +01001340 dma_addr_t addr, offset;
1341
1342 addr = dma_addr & ~MVPP2_TX_DESC_ALIGN;
1343 offset = dma_addr & MVPP2_TX_DESC_ALIGN;
1344
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001345 if (port->priv->hw_version == MVPP21) {
Antoine Tenart6eb5d372017-10-30 11:23:33 +01001346 tx_desc->pp21.buf_dma_addr = addr;
1347 tx_desc->pp21.packet_offset = offset;
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001348 } else {
Antoine Tenart6eb5d372017-10-30 11:23:33 +01001349 u64 val = (u64)addr;
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001350
1351 tx_desc->pp22.buf_dma_addr_ptp &= ~GENMASK_ULL(40, 0);
1352 tx_desc->pp22.buf_dma_addr_ptp |= val;
Antoine Tenart6eb5d372017-10-30 11:23:33 +01001353 tx_desc->pp22.packet_offset = offset;
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001354 }
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001355}
1356
1357static size_t mvpp2_txdesc_size_get(struct mvpp2_port *port,
1358 struct mvpp2_tx_desc *tx_desc)
1359{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001360 if (port->priv->hw_version == MVPP21)
1361 return tx_desc->pp21.data_size;
1362 else
1363 return tx_desc->pp22.data_size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001364}
1365
1366static void mvpp2_txdesc_size_set(struct mvpp2_port *port,
1367 struct mvpp2_tx_desc *tx_desc,
1368 size_t size)
1369{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001370 if (port->priv->hw_version == MVPP21)
1371 tx_desc->pp21.data_size = size;
1372 else
1373 tx_desc->pp22.data_size = size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001374}
1375
1376static void mvpp2_txdesc_txq_set(struct mvpp2_port *port,
1377 struct mvpp2_tx_desc *tx_desc,
1378 unsigned int txq)
1379{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001380 if (port->priv->hw_version == MVPP21)
1381 tx_desc->pp21.phys_txq = txq;
1382 else
1383 tx_desc->pp22.phys_txq = txq;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001384}
1385
1386static void mvpp2_txdesc_cmd_set(struct mvpp2_port *port,
1387 struct mvpp2_tx_desc *tx_desc,
1388 unsigned int command)
1389{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001390 if (port->priv->hw_version == MVPP21)
1391 tx_desc->pp21.command = command;
1392 else
1393 tx_desc->pp22.command = command;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001394}
1395
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001396static unsigned int mvpp2_txdesc_offset_get(struct mvpp2_port *port,
1397 struct mvpp2_tx_desc *tx_desc)
1398{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001399 if (port->priv->hw_version == MVPP21)
1400 return tx_desc->pp21.packet_offset;
1401 else
1402 return tx_desc->pp22.packet_offset;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001403}
1404
1405static dma_addr_t mvpp2_rxdesc_dma_addr_get(struct mvpp2_port *port,
1406 struct mvpp2_rx_desc *rx_desc)
1407{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001408 if (port->priv->hw_version == MVPP21)
1409 return rx_desc->pp21.buf_dma_addr;
1410 else
1411 return rx_desc->pp22.buf_dma_addr_key_hash & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001412}
1413
1414static unsigned long mvpp2_rxdesc_cookie_get(struct mvpp2_port *port,
1415 struct mvpp2_rx_desc *rx_desc)
1416{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001417 if (port->priv->hw_version == MVPP21)
1418 return rx_desc->pp21.buf_cookie;
1419 else
1420 return rx_desc->pp22.buf_cookie_misc & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001421}
1422
1423static size_t mvpp2_rxdesc_size_get(struct mvpp2_port *port,
1424 struct mvpp2_rx_desc *rx_desc)
1425{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001426 if (port->priv->hw_version == MVPP21)
1427 return rx_desc->pp21.data_size;
1428 else
1429 return rx_desc->pp22.data_size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001430}
1431
1432static u32 mvpp2_rxdesc_status_get(struct mvpp2_port *port,
1433 struct mvpp2_rx_desc *rx_desc)
1434{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001435 if (port->priv->hw_version == MVPP21)
1436 return rx_desc->pp21.status;
1437 else
1438 return rx_desc->pp22.status;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001439}
1440
Marcin Wojtas3f518502014-07-10 16:52:13 -03001441static void mvpp2_txq_inc_get(struct mvpp2_txq_pcpu *txq_pcpu)
1442{
1443 txq_pcpu->txq_get_index++;
1444 if (txq_pcpu->txq_get_index == txq_pcpu->size)
1445 txq_pcpu->txq_get_index = 0;
1446}
1447
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001448static void mvpp2_txq_inc_put(struct mvpp2_port *port,
1449 struct mvpp2_txq_pcpu *txq_pcpu,
Marcin Wojtas71ce3912015-08-06 19:00:29 +02001450 struct sk_buff *skb,
1451 struct mvpp2_tx_desc *tx_desc)
Marcin Wojtas3f518502014-07-10 16:52:13 -03001452{
Thomas Petazzoni83544912016-12-21 11:28:49 +01001453 struct mvpp2_txq_pcpu_buf *tx_buf =
1454 txq_pcpu->buffs + txq_pcpu->txq_put_index;
1455 tx_buf->skb = skb;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001456 tx_buf->size = mvpp2_txdesc_size_get(port, tx_desc);
1457 tx_buf->dma = mvpp2_txdesc_dma_addr_get(port, tx_desc) +
1458 mvpp2_txdesc_offset_get(port, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001459 txq_pcpu->txq_put_index++;
1460 if (txq_pcpu->txq_put_index == txq_pcpu->size)
1461 txq_pcpu->txq_put_index = 0;
1462}
1463
1464/* Get number of physical egress port */
1465static inline int mvpp2_egress_port(struct mvpp2_port *port)
1466{
1467 return MVPP2_MAX_TCONT + port->id;
1468}
1469
1470/* Get number of physical TXQ */
1471static inline int mvpp2_txq_phys(int port, int txq)
1472{
1473 return (MVPP2_MAX_TCONT + port) * MVPP2_MAX_TXQ + txq;
1474}
1475
1476/* Parser configuration routines */
1477
1478/* Update parser tcam and sram hw entries */
1479static int mvpp2_prs_hw_write(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1480{
1481 int i;
1482
1483 if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1484 return -EINVAL;
1485
1486 /* Clear entry invalidation bit */
1487 pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] &= ~MVPP2_PRS_TCAM_INV_MASK;
1488
1489 /* Write tcam index - indirect access */
1490 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1491 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1492 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), pe->tcam.word[i]);
1493
1494 /* Write sram index - indirect access */
1495 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1496 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1497 mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), pe->sram.word[i]);
1498
1499 return 0;
1500}
1501
1502/* Read tcam entry from hw */
1503static int mvpp2_prs_hw_read(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1504{
1505 int i;
1506
1507 if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1508 return -EINVAL;
1509
1510 /* Write tcam index - indirect access */
1511 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1512
1513 pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] = mvpp2_read(priv,
1514 MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD));
1515 if (pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] & MVPP2_PRS_TCAM_INV_MASK)
1516 return MVPP2_PRS_TCAM_ENTRY_INVALID;
1517
1518 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1519 pe->tcam.word[i] = mvpp2_read(priv, MVPP2_PRS_TCAM_DATA_REG(i));
1520
1521 /* Write sram index - indirect access */
1522 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1523 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1524 pe->sram.word[i] = mvpp2_read(priv, MVPP2_PRS_SRAM_DATA_REG(i));
1525
1526 return 0;
1527}
1528
1529/* Invalidate tcam hw entry */
1530static void mvpp2_prs_hw_inv(struct mvpp2 *priv, int index)
1531{
1532 /* Write index - indirect access */
1533 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
1534 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD),
1535 MVPP2_PRS_TCAM_INV_MASK);
1536}
1537
1538/* Enable shadow table entry and set its lookup ID */
1539static void mvpp2_prs_shadow_set(struct mvpp2 *priv, int index, int lu)
1540{
1541 priv->prs_shadow[index].valid = true;
1542 priv->prs_shadow[index].lu = lu;
1543}
1544
1545/* Update ri fields in shadow table entry */
1546static void mvpp2_prs_shadow_ri_set(struct mvpp2 *priv, int index,
1547 unsigned int ri, unsigned int ri_mask)
1548{
1549 priv->prs_shadow[index].ri_mask = ri_mask;
1550 priv->prs_shadow[index].ri = ri;
1551}
1552
1553/* Update lookup field in tcam sw entry */
1554static void mvpp2_prs_tcam_lu_set(struct mvpp2_prs_entry *pe, unsigned int lu)
1555{
1556 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_LU_BYTE);
1557
1558 pe->tcam.byte[MVPP2_PRS_TCAM_LU_BYTE] = lu;
1559 pe->tcam.byte[enable_off] = MVPP2_PRS_LU_MASK;
1560}
1561
1562/* Update mask for single port in tcam sw entry */
1563static void mvpp2_prs_tcam_port_set(struct mvpp2_prs_entry *pe,
1564 unsigned int port, bool add)
1565{
1566 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1567
1568 if (add)
1569 pe->tcam.byte[enable_off] &= ~(1 << port);
1570 else
1571 pe->tcam.byte[enable_off] |= 1 << port;
1572}
1573
1574/* Update port map in tcam sw entry */
1575static void mvpp2_prs_tcam_port_map_set(struct mvpp2_prs_entry *pe,
1576 unsigned int ports)
1577{
1578 unsigned char port_mask = MVPP2_PRS_PORT_MASK;
1579 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1580
1581 pe->tcam.byte[MVPP2_PRS_TCAM_PORT_BYTE] = 0;
1582 pe->tcam.byte[enable_off] &= ~port_mask;
1583 pe->tcam.byte[enable_off] |= ~ports & MVPP2_PRS_PORT_MASK;
1584}
1585
1586/* Obtain port map from tcam sw entry */
1587static unsigned int mvpp2_prs_tcam_port_map_get(struct mvpp2_prs_entry *pe)
1588{
1589 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1590
1591 return ~(pe->tcam.byte[enable_off]) & MVPP2_PRS_PORT_MASK;
1592}
1593
1594/* Set byte of data and its enable bits in tcam sw entry */
1595static void mvpp2_prs_tcam_data_byte_set(struct mvpp2_prs_entry *pe,
1596 unsigned int offs, unsigned char byte,
1597 unsigned char enable)
1598{
1599 pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)] = byte;
1600 pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)] = enable;
1601}
1602
1603/* Get byte of data and its enable bits from tcam sw entry */
1604static void mvpp2_prs_tcam_data_byte_get(struct mvpp2_prs_entry *pe,
1605 unsigned int offs, unsigned char *byte,
1606 unsigned char *enable)
1607{
1608 *byte = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)];
1609 *enable = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)];
1610}
1611
1612/* Compare tcam data bytes with a pattern */
1613static bool mvpp2_prs_tcam_data_cmp(struct mvpp2_prs_entry *pe, int offs,
1614 u16 data)
1615{
1616 int off = MVPP2_PRS_TCAM_DATA_BYTE(offs);
1617 u16 tcam_data;
1618
Antoine Tenartef4816f2017-10-24 11:41:26 +02001619 tcam_data = (pe->tcam.byte[off + 1] << 8) | pe->tcam.byte[off];
Marcin Wojtas3f518502014-07-10 16:52:13 -03001620 if (tcam_data != data)
1621 return false;
1622 return true;
1623}
1624
1625/* Update ai bits in tcam sw entry */
1626static void mvpp2_prs_tcam_ai_update(struct mvpp2_prs_entry *pe,
1627 unsigned int bits, unsigned int enable)
1628{
1629 int i, ai_idx = MVPP2_PRS_TCAM_AI_BYTE;
1630
1631 for (i = 0; i < MVPP2_PRS_AI_BITS; i++) {
1632
1633 if (!(enable & BIT(i)))
1634 continue;
1635
1636 if (bits & BIT(i))
1637 pe->tcam.byte[ai_idx] |= 1 << i;
1638 else
1639 pe->tcam.byte[ai_idx] &= ~(1 << i);
1640 }
1641
1642 pe->tcam.byte[MVPP2_PRS_TCAM_EN_OFFS(ai_idx)] |= enable;
1643}
1644
1645/* Get ai bits from tcam sw entry */
1646static int mvpp2_prs_tcam_ai_get(struct mvpp2_prs_entry *pe)
1647{
1648 return pe->tcam.byte[MVPP2_PRS_TCAM_AI_BYTE];
1649}
1650
1651/* Set ethertype in tcam sw entry */
1652static void mvpp2_prs_match_etype(struct mvpp2_prs_entry *pe, int offset,
1653 unsigned short ethertype)
1654{
1655 mvpp2_prs_tcam_data_byte_set(pe, offset + 0, ethertype >> 8, 0xff);
1656 mvpp2_prs_tcam_data_byte_set(pe, offset + 1, ethertype & 0xff, 0xff);
1657}
1658
1659/* Set bits in sram sw entry */
1660static void mvpp2_prs_sram_bits_set(struct mvpp2_prs_entry *pe, int bit_num,
1661 int val)
1662{
1663 pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] |= (val << (bit_num % 8));
1664}
1665
1666/* Clear bits in sram sw entry */
1667static void mvpp2_prs_sram_bits_clear(struct mvpp2_prs_entry *pe, int bit_num,
1668 int val)
1669{
1670 pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] &= ~(val << (bit_num % 8));
1671}
1672
1673/* Update ri bits in sram sw entry */
1674static void mvpp2_prs_sram_ri_update(struct mvpp2_prs_entry *pe,
1675 unsigned int bits, unsigned int mask)
1676{
1677 unsigned int i;
1678
1679 for (i = 0; i < MVPP2_PRS_SRAM_RI_CTRL_BITS; i++) {
1680 int ri_off = MVPP2_PRS_SRAM_RI_OFFS;
1681
1682 if (!(mask & BIT(i)))
1683 continue;
1684
1685 if (bits & BIT(i))
1686 mvpp2_prs_sram_bits_set(pe, ri_off + i, 1);
1687 else
1688 mvpp2_prs_sram_bits_clear(pe, ri_off + i, 1);
1689
1690 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_RI_CTRL_OFFS + i, 1);
1691 }
1692}
1693
1694/* Obtain ri bits from sram sw entry */
1695static int mvpp2_prs_sram_ri_get(struct mvpp2_prs_entry *pe)
1696{
1697 return pe->sram.word[MVPP2_PRS_SRAM_RI_WORD];
1698}
1699
1700/* Update ai bits in sram sw entry */
1701static void mvpp2_prs_sram_ai_update(struct mvpp2_prs_entry *pe,
1702 unsigned int bits, unsigned int mask)
1703{
1704 unsigned int i;
1705 int ai_off = MVPP2_PRS_SRAM_AI_OFFS;
1706
1707 for (i = 0; i < MVPP2_PRS_SRAM_AI_CTRL_BITS; i++) {
1708
1709 if (!(mask & BIT(i)))
1710 continue;
1711
1712 if (bits & BIT(i))
1713 mvpp2_prs_sram_bits_set(pe, ai_off + i, 1);
1714 else
1715 mvpp2_prs_sram_bits_clear(pe, ai_off + i, 1);
1716
1717 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_AI_CTRL_OFFS + i, 1);
1718 }
1719}
1720
1721/* Read ai bits from sram sw entry */
1722static int mvpp2_prs_sram_ai_get(struct mvpp2_prs_entry *pe)
1723{
1724 u8 bits;
1725 int ai_off = MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_AI_OFFS);
1726 int ai_en_off = ai_off + 1;
1727 int ai_shift = MVPP2_PRS_SRAM_AI_OFFS % 8;
1728
1729 bits = (pe->sram.byte[ai_off] >> ai_shift) |
1730 (pe->sram.byte[ai_en_off] << (8 - ai_shift));
1731
1732 return bits;
1733}
1734
1735/* In sram sw entry set lookup ID field of the tcam key to be used in the next
1736 * lookup interation
1737 */
1738static void mvpp2_prs_sram_next_lu_set(struct mvpp2_prs_entry *pe,
1739 unsigned int lu)
1740{
1741 int sram_next_off = MVPP2_PRS_SRAM_NEXT_LU_OFFS;
1742
1743 mvpp2_prs_sram_bits_clear(pe, sram_next_off,
1744 MVPP2_PRS_SRAM_NEXT_LU_MASK);
1745 mvpp2_prs_sram_bits_set(pe, sram_next_off, lu);
1746}
1747
1748/* In the sram sw entry set sign and value of the next lookup offset
1749 * and the offset value generated to the classifier
1750 */
1751static void mvpp2_prs_sram_shift_set(struct mvpp2_prs_entry *pe, int shift,
1752 unsigned int op)
1753{
1754 /* Set sign */
1755 if (shift < 0) {
1756 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1757 shift = 0 - shift;
1758 } else {
1759 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1760 }
1761
1762 /* Set value */
1763 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_SHIFT_OFFS)] =
1764 (unsigned char)shift;
1765
1766 /* Reset and set operation */
1767 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS,
1768 MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK);
1769 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS, op);
1770
1771 /* Set base offset as current */
1772 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1773}
1774
1775/* In the sram sw entry set sign and value of the user defined offset
1776 * generated to the classifier
1777 */
1778static void mvpp2_prs_sram_offset_set(struct mvpp2_prs_entry *pe,
1779 unsigned int type, int offset,
1780 unsigned int op)
1781{
1782 /* Set sign */
1783 if (offset < 0) {
1784 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1785 offset = 0 - offset;
1786 } else {
1787 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1788 }
1789
1790 /* Set value */
1791 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_OFFS,
1792 MVPP2_PRS_SRAM_UDF_MASK);
1793 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_OFFS, offset);
1794 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1795 MVPP2_PRS_SRAM_UDF_BITS)] &=
1796 ~(MVPP2_PRS_SRAM_UDF_MASK >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1797 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1798 MVPP2_PRS_SRAM_UDF_BITS)] |=
1799 (offset >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1800
1801 /* Set offset type */
1802 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS,
1803 MVPP2_PRS_SRAM_UDF_TYPE_MASK);
1804 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS, type);
1805
1806 /* Set offset operation */
1807 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS,
1808 MVPP2_PRS_SRAM_OP_SEL_UDF_MASK);
1809 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS, op);
1810
1811 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1812 MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] &=
1813 ~(MVPP2_PRS_SRAM_OP_SEL_UDF_MASK >>
1814 (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1815
1816 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1817 MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] |=
1818 (op >> (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1819
1820 /* Set base offset as current */
1821 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1822}
1823
1824/* Find parser flow entry */
1825static struct mvpp2_prs_entry *mvpp2_prs_flow_find(struct mvpp2 *priv, int flow)
1826{
1827 struct mvpp2_prs_entry *pe;
1828 int tid;
1829
1830 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
1831 if (!pe)
1832 return NULL;
1833 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
1834
1835 /* Go through the all entires with MVPP2_PRS_LU_FLOWS */
1836 for (tid = MVPP2_PRS_TCAM_SRAM_SIZE - 1; tid >= 0; tid--) {
1837 u8 bits;
1838
1839 if (!priv->prs_shadow[tid].valid ||
1840 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_FLOWS)
1841 continue;
1842
1843 pe->index = tid;
1844 mvpp2_prs_hw_read(priv, pe);
1845 bits = mvpp2_prs_sram_ai_get(pe);
1846
1847 /* Sram store classification lookup ID in AI bits [5:0] */
1848 if ((bits & MVPP2_PRS_FLOW_ID_MASK) == flow)
1849 return pe;
1850 }
1851 kfree(pe);
1852
1853 return NULL;
1854}
1855
1856/* Return first free tcam index, seeking from start to end */
1857static int mvpp2_prs_tcam_first_free(struct mvpp2 *priv, unsigned char start,
1858 unsigned char end)
1859{
1860 int tid;
1861
1862 if (start > end)
1863 swap(start, end);
1864
1865 if (end >= MVPP2_PRS_TCAM_SRAM_SIZE)
1866 end = MVPP2_PRS_TCAM_SRAM_SIZE - 1;
1867
1868 for (tid = start; tid <= end; tid++) {
1869 if (!priv->prs_shadow[tid].valid)
1870 return tid;
1871 }
1872
1873 return -EINVAL;
1874}
1875
1876/* Enable/disable dropping all mac da's */
1877static void mvpp2_prs_mac_drop_all_set(struct mvpp2 *priv, int port, bool add)
1878{
1879 struct mvpp2_prs_entry pe;
1880
1881 if (priv->prs_shadow[MVPP2_PE_DROP_ALL].valid) {
1882 /* Entry exist - update port only */
1883 pe.index = MVPP2_PE_DROP_ALL;
1884 mvpp2_prs_hw_read(priv, &pe);
1885 } else {
1886 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001887 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001888 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1889 pe.index = MVPP2_PE_DROP_ALL;
1890
1891 /* Non-promiscuous mode for all ports - DROP unknown packets */
1892 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
1893 MVPP2_PRS_RI_DROP_MASK);
1894
1895 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1896 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1897
1898 /* Update shadow table */
1899 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1900
1901 /* Mask all ports */
1902 mvpp2_prs_tcam_port_map_set(&pe, 0);
1903 }
1904
1905 /* Update port mask */
1906 mvpp2_prs_tcam_port_set(&pe, port, add);
1907
1908 mvpp2_prs_hw_write(priv, &pe);
1909}
1910
1911/* Set port to promiscuous mode */
1912static void mvpp2_prs_mac_promisc_set(struct mvpp2 *priv, int port, bool add)
1913{
1914 struct mvpp2_prs_entry pe;
1915
Joe Perchesdbedd442015-03-06 20:49:12 -08001916 /* Promiscuous mode - Accept unknown packets */
Marcin Wojtas3f518502014-07-10 16:52:13 -03001917
1918 if (priv->prs_shadow[MVPP2_PE_MAC_PROMISCUOUS].valid) {
1919 /* Entry exist - update port only */
1920 pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1921 mvpp2_prs_hw_read(priv, &pe);
1922 } else {
1923 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001924 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001925 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1926 pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1927
1928 /* Continue - set next lookup */
1929 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
1930
1931 /* Set result info bits */
1932 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_UCAST,
1933 MVPP2_PRS_RI_L2_CAST_MASK);
1934
1935 /* Shift to ethertype */
1936 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
1937 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1938
1939 /* Mask all ports */
1940 mvpp2_prs_tcam_port_map_set(&pe, 0);
1941
1942 /* Update shadow table */
1943 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1944 }
1945
1946 /* Update port mask */
1947 mvpp2_prs_tcam_port_set(&pe, port, add);
1948
1949 mvpp2_prs_hw_write(priv, &pe);
1950}
1951
1952/* Accept multicast */
1953static void mvpp2_prs_mac_multi_set(struct mvpp2 *priv, int port, int index,
1954 bool add)
1955{
1956 struct mvpp2_prs_entry pe;
1957 unsigned char da_mc;
1958
1959 /* Ethernet multicast address first byte is
1960 * 0x01 for IPv4 and 0x33 for IPv6
1961 */
1962 da_mc = (index == MVPP2_PE_MAC_MC_ALL) ? 0x01 : 0x33;
1963
1964 if (priv->prs_shadow[index].valid) {
1965 /* Entry exist - update port only */
1966 pe.index = index;
1967 mvpp2_prs_hw_read(priv, &pe);
1968 } else {
1969 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001970 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001971 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1972 pe.index = index;
1973
1974 /* Continue - set next lookup */
1975 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
1976
1977 /* Set result info bits */
1978 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_MCAST,
1979 MVPP2_PRS_RI_L2_CAST_MASK);
1980
1981 /* Update tcam entry data first byte */
1982 mvpp2_prs_tcam_data_byte_set(&pe, 0, da_mc, 0xff);
1983
1984 /* Shift to ethertype */
1985 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
1986 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1987
1988 /* Mask all ports */
1989 mvpp2_prs_tcam_port_map_set(&pe, 0);
1990
1991 /* Update shadow table */
1992 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1993 }
1994
1995 /* Update port mask */
1996 mvpp2_prs_tcam_port_set(&pe, port, add);
1997
1998 mvpp2_prs_hw_write(priv, &pe);
1999}
2000
2001/* Set entry for dsa packets */
2002static void mvpp2_prs_dsa_tag_set(struct mvpp2 *priv, int port, bool add,
2003 bool tagged, bool extend)
2004{
2005 struct mvpp2_prs_entry pe;
2006 int tid, shift;
2007
2008 if (extend) {
2009 tid = tagged ? MVPP2_PE_EDSA_TAGGED : MVPP2_PE_EDSA_UNTAGGED;
2010 shift = 8;
2011 } else {
2012 tid = tagged ? MVPP2_PE_DSA_TAGGED : MVPP2_PE_DSA_UNTAGGED;
2013 shift = 4;
2014 }
2015
2016 if (priv->prs_shadow[tid].valid) {
2017 /* Entry exist - update port only */
2018 pe.index = tid;
2019 mvpp2_prs_hw_read(priv, &pe);
2020 } else {
2021 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002022 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002023 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
2024 pe.index = tid;
2025
2026 /* Shift 4 bytes if DSA tag or 8 bytes in case of EDSA tag*/
2027 mvpp2_prs_sram_shift_set(&pe, shift,
2028 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2029
2030 /* Update shadow table */
2031 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
2032
2033 if (tagged) {
2034 /* Set tagged bit in DSA tag */
2035 mvpp2_prs_tcam_data_byte_set(&pe, 0,
2036 MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
2037 MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
2038 /* Clear all ai bits for next iteration */
2039 mvpp2_prs_sram_ai_update(&pe, 0,
2040 MVPP2_PRS_SRAM_AI_MASK);
2041 /* If packet is tagged continue check vlans */
2042 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2043 } else {
2044 /* Set result info bits to 'no vlans' */
2045 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
2046 MVPP2_PRS_RI_VLAN_MASK);
2047 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2048 }
2049
2050 /* Mask all ports */
2051 mvpp2_prs_tcam_port_map_set(&pe, 0);
2052 }
2053
2054 /* Update port mask */
2055 mvpp2_prs_tcam_port_set(&pe, port, add);
2056
2057 mvpp2_prs_hw_write(priv, &pe);
2058}
2059
2060/* Set entry for dsa ethertype */
2061static void mvpp2_prs_dsa_tag_ethertype_set(struct mvpp2 *priv, int port,
2062 bool add, bool tagged, bool extend)
2063{
2064 struct mvpp2_prs_entry pe;
2065 int tid, shift, port_mask;
2066
2067 if (extend) {
2068 tid = tagged ? MVPP2_PE_ETYPE_EDSA_TAGGED :
2069 MVPP2_PE_ETYPE_EDSA_UNTAGGED;
2070 port_mask = 0;
2071 shift = 8;
2072 } else {
2073 tid = tagged ? MVPP2_PE_ETYPE_DSA_TAGGED :
2074 MVPP2_PE_ETYPE_DSA_UNTAGGED;
2075 port_mask = MVPP2_PRS_PORT_MASK;
2076 shift = 4;
2077 }
2078
2079 if (priv->prs_shadow[tid].valid) {
2080 /* Entry exist - update port only */
2081 pe.index = tid;
2082 mvpp2_prs_hw_read(priv, &pe);
2083 } else {
2084 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002085 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002086 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
2087 pe.index = tid;
2088
2089 /* Set ethertype */
2090 mvpp2_prs_match_etype(&pe, 0, ETH_P_EDSA);
2091 mvpp2_prs_match_etype(&pe, 2, 0);
2092
2093 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DSA_MASK,
2094 MVPP2_PRS_RI_DSA_MASK);
2095 /* Shift ethertype + 2 byte reserved + tag*/
2096 mvpp2_prs_sram_shift_set(&pe, 2 + MVPP2_ETH_TYPE_LEN + shift,
2097 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2098
2099 /* Update shadow table */
2100 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
2101
2102 if (tagged) {
2103 /* Set tagged bit in DSA tag */
2104 mvpp2_prs_tcam_data_byte_set(&pe,
2105 MVPP2_ETH_TYPE_LEN + 2 + 3,
2106 MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
2107 MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
2108 /* Clear all ai bits for next iteration */
2109 mvpp2_prs_sram_ai_update(&pe, 0,
2110 MVPP2_PRS_SRAM_AI_MASK);
2111 /* If packet is tagged continue check vlans */
2112 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2113 } else {
2114 /* Set result info bits to 'no vlans' */
2115 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
2116 MVPP2_PRS_RI_VLAN_MASK);
2117 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2118 }
2119 /* Mask/unmask all ports, depending on dsa type */
2120 mvpp2_prs_tcam_port_map_set(&pe, port_mask);
2121 }
2122
2123 /* Update port mask */
2124 mvpp2_prs_tcam_port_set(&pe, port, add);
2125
2126 mvpp2_prs_hw_write(priv, &pe);
2127}
2128
2129/* Search for existing single/triple vlan entry */
2130static struct mvpp2_prs_entry *mvpp2_prs_vlan_find(struct mvpp2 *priv,
2131 unsigned short tpid, int ai)
2132{
2133 struct mvpp2_prs_entry *pe;
2134 int tid;
2135
2136 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2137 if (!pe)
2138 return NULL;
2139 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2140
2141 /* Go through the all entries with MVPP2_PRS_LU_VLAN */
2142 for (tid = MVPP2_PE_FIRST_FREE_TID;
2143 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
2144 unsigned int ri_bits, ai_bits;
2145 bool match;
2146
2147 if (!priv->prs_shadow[tid].valid ||
2148 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
2149 continue;
2150
2151 pe->index = tid;
2152
2153 mvpp2_prs_hw_read(priv, pe);
2154 match = mvpp2_prs_tcam_data_cmp(pe, 0, swab16(tpid));
2155 if (!match)
2156 continue;
2157
2158 /* Get vlan type */
2159 ri_bits = mvpp2_prs_sram_ri_get(pe);
2160 ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
2161
2162 /* Get current ai value from tcam */
2163 ai_bits = mvpp2_prs_tcam_ai_get(pe);
2164 /* Clear double vlan bit */
2165 ai_bits &= ~MVPP2_PRS_DBL_VLAN_AI_BIT;
2166
2167 if (ai != ai_bits)
2168 continue;
2169
2170 if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
2171 ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
2172 return pe;
2173 }
2174 kfree(pe);
2175
2176 return NULL;
2177}
2178
2179/* Add/update single/triple vlan entry */
2180static int mvpp2_prs_vlan_add(struct mvpp2 *priv, unsigned short tpid, int ai,
2181 unsigned int port_map)
2182{
2183 struct mvpp2_prs_entry *pe;
2184 int tid_aux, tid;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302185 int ret = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002186
2187 pe = mvpp2_prs_vlan_find(priv, tpid, ai);
2188
2189 if (!pe) {
2190 /* Create new tcam entry */
2191 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_LAST_FREE_TID,
2192 MVPP2_PE_FIRST_FREE_TID);
2193 if (tid < 0)
2194 return tid;
2195
2196 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2197 if (!pe)
2198 return -ENOMEM;
2199
2200 /* Get last double vlan tid */
2201 for (tid_aux = MVPP2_PE_LAST_FREE_TID;
2202 tid_aux >= MVPP2_PE_FIRST_FREE_TID; tid_aux--) {
2203 unsigned int ri_bits;
2204
2205 if (!priv->prs_shadow[tid_aux].valid ||
2206 priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
2207 continue;
2208
2209 pe->index = tid_aux;
2210 mvpp2_prs_hw_read(priv, pe);
2211 ri_bits = mvpp2_prs_sram_ri_get(pe);
2212 if ((ri_bits & MVPP2_PRS_RI_VLAN_MASK) ==
2213 MVPP2_PRS_RI_VLAN_DOUBLE)
2214 break;
2215 }
2216
Sudip Mukherjee43737472014-11-01 16:59:34 +05302217 if (tid <= tid_aux) {
2218 ret = -EINVAL;
Markus Elfringf9fd0e32017-04-17 13:50:35 +02002219 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302220 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002221
Markus Elfringbd6aaf52017-04-17 10:40:32 +02002222 memset(pe, 0, sizeof(*pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002223 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2224 pe->index = tid;
2225
2226 mvpp2_prs_match_etype(pe, 0, tpid);
2227
2228 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_L2);
2229 /* Shift 4 bytes - skip 1 vlan tag */
2230 mvpp2_prs_sram_shift_set(pe, MVPP2_VLAN_TAG_LEN,
2231 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2232 /* Clear all ai bits for next iteration */
2233 mvpp2_prs_sram_ai_update(pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2234
2235 if (ai == MVPP2_PRS_SINGLE_VLAN_AI) {
2236 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_SINGLE,
2237 MVPP2_PRS_RI_VLAN_MASK);
2238 } else {
2239 ai |= MVPP2_PRS_DBL_VLAN_AI_BIT;
2240 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_TRIPLE,
2241 MVPP2_PRS_RI_VLAN_MASK);
2242 }
2243 mvpp2_prs_tcam_ai_update(pe, ai, MVPP2_PRS_SRAM_AI_MASK);
2244
2245 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_VLAN);
2246 }
2247 /* Update ports' mask */
2248 mvpp2_prs_tcam_port_map_set(pe, port_map);
2249
2250 mvpp2_prs_hw_write(priv, pe);
Markus Elfringf9fd0e32017-04-17 13:50:35 +02002251free_pe:
Marcin Wojtas3f518502014-07-10 16:52:13 -03002252 kfree(pe);
2253
Sudip Mukherjee43737472014-11-01 16:59:34 +05302254 return ret;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002255}
2256
2257/* Get first free double vlan ai number */
2258static int mvpp2_prs_double_vlan_ai_free_get(struct mvpp2 *priv)
2259{
2260 int i;
2261
2262 for (i = 1; i < MVPP2_PRS_DBL_VLANS_MAX; i++) {
2263 if (!priv->prs_double_vlans[i])
2264 return i;
2265 }
2266
2267 return -EINVAL;
2268}
2269
2270/* Search for existing double vlan entry */
2271static struct mvpp2_prs_entry *mvpp2_prs_double_vlan_find(struct mvpp2 *priv,
2272 unsigned short tpid1,
2273 unsigned short tpid2)
2274{
2275 struct mvpp2_prs_entry *pe;
2276 int tid;
2277
2278 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2279 if (!pe)
2280 return NULL;
2281 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2282
2283 /* Go through the all entries with MVPP2_PRS_LU_VLAN */
2284 for (tid = MVPP2_PE_FIRST_FREE_TID;
2285 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
2286 unsigned int ri_mask;
2287 bool match;
2288
2289 if (!priv->prs_shadow[tid].valid ||
2290 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
2291 continue;
2292
2293 pe->index = tid;
2294 mvpp2_prs_hw_read(priv, pe);
2295
2296 match = mvpp2_prs_tcam_data_cmp(pe, 0, swab16(tpid1))
2297 && mvpp2_prs_tcam_data_cmp(pe, 4, swab16(tpid2));
2298
2299 if (!match)
2300 continue;
2301
2302 ri_mask = mvpp2_prs_sram_ri_get(pe) & MVPP2_PRS_RI_VLAN_MASK;
2303 if (ri_mask == MVPP2_PRS_RI_VLAN_DOUBLE)
2304 return pe;
2305 }
2306 kfree(pe);
2307
2308 return NULL;
2309}
2310
2311/* Add or update double vlan entry */
2312static int mvpp2_prs_double_vlan_add(struct mvpp2 *priv, unsigned short tpid1,
2313 unsigned short tpid2,
2314 unsigned int port_map)
2315{
2316 struct mvpp2_prs_entry *pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302317 int tid_aux, tid, ai, ret = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002318
2319 pe = mvpp2_prs_double_vlan_find(priv, tpid1, tpid2);
2320
2321 if (!pe) {
2322 /* Create new tcam entry */
2323 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2324 MVPP2_PE_LAST_FREE_TID);
2325 if (tid < 0)
2326 return tid;
2327
2328 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2329 if (!pe)
2330 return -ENOMEM;
2331
2332 /* Set ai value for new double vlan entry */
2333 ai = mvpp2_prs_double_vlan_ai_free_get(priv);
Sudip Mukherjee43737472014-11-01 16:59:34 +05302334 if (ai < 0) {
2335 ret = ai;
Markus Elfringc9a7e122017-04-17 13:03:49 +02002336 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302337 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002338
2339 /* Get first single/triple vlan tid */
2340 for (tid_aux = MVPP2_PE_FIRST_FREE_TID;
2341 tid_aux <= MVPP2_PE_LAST_FREE_TID; tid_aux++) {
2342 unsigned int ri_bits;
2343
2344 if (!priv->prs_shadow[tid_aux].valid ||
2345 priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
2346 continue;
2347
2348 pe->index = tid_aux;
2349 mvpp2_prs_hw_read(priv, pe);
2350 ri_bits = mvpp2_prs_sram_ri_get(pe);
2351 ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
2352 if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
2353 ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
2354 break;
2355 }
2356
Sudip Mukherjee43737472014-11-01 16:59:34 +05302357 if (tid >= tid_aux) {
2358 ret = -ERANGE;
Markus Elfringc9a7e122017-04-17 13:03:49 +02002359 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302360 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002361
Markus Elfringbd6aaf52017-04-17 10:40:32 +02002362 memset(pe, 0, sizeof(*pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002363 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2364 pe->index = tid;
2365
2366 priv->prs_double_vlans[ai] = true;
2367
2368 mvpp2_prs_match_etype(pe, 0, tpid1);
2369 mvpp2_prs_match_etype(pe, 4, tpid2);
2370
2371 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_VLAN);
2372 /* Shift 8 bytes - skip 2 vlan tags */
2373 mvpp2_prs_sram_shift_set(pe, 2 * MVPP2_VLAN_TAG_LEN,
2374 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2375 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_DOUBLE,
2376 MVPP2_PRS_RI_VLAN_MASK);
2377 mvpp2_prs_sram_ai_update(pe, ai | MVPP2_PRS_DBL_VLAN_AI_BIT,
2378 MVPP2_PRS_SRAM_AI_MASK);
2379
2380 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_VLAN);
2381 }
2382
2383 /* Update ports' mask */
2384 mvpp2_prs_tcam_port_map_set(pe, port_map);
2385 mvpp2_prs_hw_write(priv, pe);
Markus Elfringc9a7e122017-04-17 13:03:49 +02002386free_pe:
Marcin Wojtas3f518502014-07-10 16:52:13 -03002387 kfree(pe);
Sudip Mukherjee43737472014-11-01 16:59:34 +05302388 return ret;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002389}
2390
2391/* IPv4 header parsing for fragmentation and L4 offset */
2392static int mvpp2_prs_ip4_proto(struct mvpp2 *priv, unsigned short proto,
2393 unsigned int ri, unsigned int ri_mask)
2394{
2395 struct mvpp2_prs_entry pe;
2396 int tid;
2397
2398 if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
2399 (proto != IPPROTO_IGMP))
2400 return -EINVAL;
2401
Stefan Chulskiaff3da32017-09-25 14:59:46 +02002402 /* Not fragmented packet */
Marcin Wojtas3f518502014-07-10 16:52:13 -03002403 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2404 MVPP2_PE_LAST_FREE_TID);
2405 if (tid < 0)
2406 return tid;
2407
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002408 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002409 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
2410 pe.index = tid;
2411
2412 /* Set next lu to IPv4 */
2413 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2414 mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2415 /* Set L4 offset */
2416 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
2417 sizeof(struct iphdr) - 4,
2418 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2419 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
2420 MVPP2_PRS_IPV4_DIP_AI_BIT);
Stefan Chulskiaff3da32017-09-25 14:59:46 +02002421 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask | MVPP2_PRS_RI_IP_FRAG_MASK);
2422
2423 mvpp2_prs_tcam_data_byte_set(&pe, 2, 0x00,
2424 MVPP2_PRS_TCAM_PROTO_MASK_L);
2425 mvpp2_prs_tcam_data_byte_set(&pe, 3, 0x00,
2426 MVPP2_PRS_TCAM_PROTO_MASK);
Marcin Wojtas3f518502014-07-10 16:52:13 -03002427
2428 mvpp2_prs_tcam_data_byte_set(&pe, 5, proto, MVPP2_PRS_TCAM_PROTO_MASK);
2429 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
2430 /* Unmask all ports */
2431 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2432
2433 /* Update shadow table and hw entry */
2434 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2435 mvpp2_prs_hw_write(priv, &pe);
2436
Stefan Chulskiaff3da32017-09-25 14:59:46 +02002437 /* Fragmented packet */
Marcin Wojtas3f518502014-07-10 16:52:13 -03002438 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2439 MVPP2_PE_LAST_FREE_TID);
2440 if (tid < 0)
2441 return tid;
2442
2443 pe.index = tid;
2444 /* Clear ri before updating */
2445 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2446 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2447 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
2448
Stefan Chulskiaff3da32017-09-25 14:59:46 +02002449 mvpp2_prs_sram_ri_update(&pe, ri | MVPP2_PRS_RI_IP_FRAG_TRUE,
2450 ri_mask | MVPP2_PRS_RI_IP_FRAG_MASK);
2451
2452 mvpp2_prs_tcam_data_byte_set(&pe, 2, 0x00, 0x0);
2453 mvpp2_prs_tcam_data_byte_set(&pe, 3, 0x00, 0x0);
Marcin Wojtas3f518502014-07-10 16:52:13 -03002454
2455 /* Update shadow table and hw entry */
2456 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2457 mvpp2_prs_hw_write(priv, &pe);
2458
2459 return 0;
2460}
2461
2462/* IPv4 L3 multicast or broadcast */
2463static int mvpp2_prs_ip4_cast(struct mvpp2 *priv, unsigned short l3_cast)
2464{
2465 struct mvpp2_prs_entry pe;
2466 int mask, tid;
2467
2468 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2469 MVPP2_PE_LAST_FREE_TID);
2470 if (tid < 0)
2471 return tid;
2472
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002473 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002474 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
2475 pe.index = tid;
2476
2477 switch (l3_cast) {
2478 case MVPP2_PRS_L3_MULTI_CAST:
2479 mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV4_MC,
2480 MVPP2_PRS_IPV4_MC_MASK);
2481 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
2482 MVPP2_PRS_RI_L3_ADDR_MASK);
2483 break;
2484 case MVPP2_PRS_L3_BROAD_CAST:
2485 mask = MVPP2_PRS_IPV4_BC_MASK;
2486 mvpp2_prs_tcam_data_byte_set(&pe, 0, mask, mask);
2487 mvpp2_prs_tcam_data_byte_set(&pe, 1, mask, mask);
2488 mvpp2_prs_tcam_data_byte_set(&pe, 2, mask, mask);
2489 mvpp2_prs_tcam_data_byte_set(&pe, 3, mask, mask);
2490 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_BCAST,
2491 MVPP2_PRS_RI_L3_ADDR_MASK);
2492 break;
2493 default:
2494 return -EINVAL;
2495 }
2496
2497 /* Finished: go to flowid generation */
2498 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2499 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2500
2501 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
2502 MVPP2_PRS_IPV4_DIP_AI_BIT);
2503 /* Unmask all ports */
2504 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2505
2506 /* Update shadow table and hw entry */
2507 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2508 mvpp2_prs_hw_write(priv, &pe);
2509
2510 return 0;
2511}
2512
2513/* Set entries for protocols over IPv6 */
2514static int mvpp2_prs_ip6_proto(struct mvpp2 *priv, unsigned short proto,
2515 unsigned int ri, unsigned int ri_mask)
2516{
2517 struct mvpp2_prs_entry pe;
2518 int tid;
2519
2520 if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
2521 (proto != IPPROTO_ICMPV6) && (proto != IPPROTO_IPIP))
2522 return -EINVAL;
2523
2524 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2525 MVPP2_PE_LAST_FREE_TID);
2526 if (tid < 0)
2527 return tid;
2528
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002529 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002530 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2531 pe.index = tid;
2532
2533 /* Finished: go to flowid generation */
2534 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2535 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2536 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
2537 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
2538 sizeof(struct ipv6hdr) - 6,
2539 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2540
2541 mvpp2_prs_tcam_data_byte_set(&pe, 0, proto, MVPP2_PRS_TCAM_PROTO_MASK);
2542 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
2543 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2544 /* Unmask all ports */
2545 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2546
2547 /* Write HW */
2548 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
2549 mvpp2_prs_hw_write(priv, &pe);
2550
2551 return 0;
2552}
2553
2554/* IPv6 L3 multicast entry */
2555static int mvpp2_prs_ip6_cast(struct mvpp2 *priv, unsigned short l3_cast)
2556{
2557 struct mvpp2_prs_entry pe;
2558 int tid;
2559
2560 if (l3_cast != MVPP2_PRS_L3_MULTI_CAST)
2561 return -EINVAL;
2562
2563 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2564 MVPP2_PE_LAST_FREE_TID);
2565 if (tid < 0)
2566 return tid;
2567
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002568 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002569 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2570 pe.index = tid;
2571
2572 /* Finished: go to flowid generation */
2573 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2574 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
2575 MVPP2_PRS_RI_L3_ADDR_MASK);
2576 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
2577 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2578 /* Shift back to IPv6 NH */
2579 mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2580
2581 mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV6_MC,
2582 MVPP2_PRS_IPV6_MC_MASK);
2583 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2584 /* Unmask all ports */
2585 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2586
2587 /* Update shadow table and hw entry */
2588 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
2589 mvpp2_prs_hw_write(priv, &pe);
2590
2591 return 0;
2592}
2593
2594/* Parser per-port initialization */
2595static void mvpp2_prs_hw_port_init(struct mvpp2 *priv, int port, int lu_first,
2596 int lu_max, int offset)
2597{
2598 u32 val;
2599
2600 /* Set lookup ID */
2601 val = mvpp2_read(priv, MVPP2_PRS_INIT_LOOKUP_REG);
2602 val &= ~MVPP2_PRS_PORT_LU_MASK(port);
2603 val |= MVPP2_PRS_PORT_LU_VAL(port, lu_first);
2604 mvpp2_write(priv, MVPP2_PRS_INIT_LOOKUP_REG, val);
2605
2606 /* Set maximum number of loops for packet received from port */
2607 val = mvpp2_read(priv, MVPP2_PRS_MAX_LOOP_REG(port));
2608 val &= ~MVPP2_PRS_MAX_LOOP_MASK(port);
2609 val |= MVPP2_PRS_MAX_LOOP_VAL(port, lu_max);
2610 mvpp2_write(priv, MVPP2_PRS_MAX_LOOP_REG(port), val);
2611
2612 /* Set initial offset for packet header extraction for the first
2613 * searching loop
2614 */
2615 val = mvpp2_read(priv, MVPP2_PRS_INIT_OFFS_REG(port));
2616 val &= ~MVPP2_PRS_INIT_OFF_MASK(port);
2617 val |= MVPP2_PRS_INIT_OFF_VAL(port, offset);
2618 mvpp2_write(priv, MVPP2_PRS_INIT_OFFS_REG(port), val);
2619}
2620
2621/* Default flow entries initialization for all ports */
2622static void mvpp2_prs_def_flow_init(struct mvpp2 *priv)
2623{
2624 struct mvpp2_prs_entry pe;
2625 int port;
2626
2627 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002628 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002629 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2630 pe.index = MVPP2_PE_FIRST_DEFAULT_FLOW - port;
2631
2632 /* Mask all ports */
2633 mvpp2_prs_tcam_port_map_set(&pe, 0);
2634
2635 /* Set flow ID*/
2636 mvpp2_prs_sram_ai_update(&pe, port, MVPP2_PRS_FLOW_ID_MASK);
2637 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
2638
2639 /* Update shadow table and hw entry */
2640 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_FLOWS);
2641 mvpp2_prs_hw_write(priv, &pe);
2642 }
2643}
2644
2645/* Set default entry for Marvell Header field */
2646static void mvpp2_prs_mh_init(struct mvpp2 *priv)
2647{
2648 struct mvpp2_prs_entry pe;
2649
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002650 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002651
2652 pe.index = MVPP2_PE_MH_DEFAULT;
2653 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MH);
2654 mvpp2_prs_sram_shift_set(&pe, MVPP2_MH_SIZE,
2655 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2656 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_MAC);
2657
2658 /* Unmask all ports */
2659 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2660
2661 /* Update shadow table and hw entry */
2662 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MH);
2663 mvpp2_prs_hw_write(priv, &pe);
2664}
2665
2666/* Set default entires (place holder) for promiscuous, non-promiscuous and
2667 * multicast MAC addresses
2668 */
2669static void mvpp2_prs_mac_init(struct mvpp2 *priv)
2670{
2671 struct mvpp2_prs_entry pe;
2672
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002673 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002674
2675 /* Non-promiscuous mode for all ports - DROP unknown packets */
2676 pe.index = MVPP2_PE_MAC_NON_PROMISCUOUS;
2677 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
2678
2679 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
2680 MVPP2_PRS_RI_DROP_MASK);
2681 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2682 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2683
2684 /* Unmask all ports */
2685 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2686
2687 /* Update shadow table and hw entry */
2688 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2689 mvpp2_prs_hw_write(priv, &pe);
2690
2691 /* place holders only - no ports */
2692 mvpp2_prs_mac_drop_all_set(priv, 0, false);
2693 mvpp2_prs_mac_promisc_set(priv, 0, false);
Antoine Tenart20746d72017-10-24 11:41:27 +02002694 mvpp2_prs_mac_multi_set(priv, 0, MVPP2_PE_MAC_MC_ALL, false);
2695 mvpp2_prs_mac_multi_set(priv, 0, MVPP2_PE_MAC_MC_IP6, false);
Marcin Wojtas3f518502014-07-10 16:52:13 -03002696}
2697
2698/* Set default entries for various types of dsa packets */
2699static void mvpp2_prs_dsa_init(struct mvpp2 *priv)
2700{
2701 struct mvpp2_prs_entry pe;
2702
2703 /* None tagged EDSA entry - place holder */
2704 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
2705 MVPP2_PRS_EDSA);
2706
2707 /* Tagged EDSA entry - place holder */
2708 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
2709
2710 /* None tagged DSA entry - place holder */
2711 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
2712 MVPP2_PRS_DSA);
2713
2714 /* Tagged DSA entry - place holder */
2715 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
2716
2717 /* None tagged EDSA ethertype entry - place holder*/
2718 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
2719 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
2720
2721 /* Tagged EDSA ethertype entry - place holder*/
2722 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
2723 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
2724
2725 /* None tagged DSA ethertype entry */
2726 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
2727 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
2728
2729 /* Tagged DSA ethertype entry */
2730 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
2731 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
2732
2733 /* Set default entry, in case DSA or EDSA tag not found */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002734 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002735 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
2736 pe.index = MVPP2_PE_DSA_DEFAULT;
2737 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2738
2739 /* Shift 0 bytes */
2740 mvpp2_prs_sram_shift_set(&pe, 0, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2741 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2742
2743 /* Clear all sram ai bits for next iteration */
2744 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2745
2746 /* Unmask all ports */
2747 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2748
2749 mvpp2_prs_hw_write(priv, &pe);
2750}
2751
2752/* Match basic ethertypes */
2753static int mvpp2_prs_etype_init(struct mvpp2 *priv)
2754{
2755 struct mvpp2_prs_entry pe;
2756 int tid;
2757
2758 /* Ethertype: PPPoE */
2759 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2760 MVPP2_PE_LAST_FREE_TID);
2761 if (tid < 0)
2762 return tid;
2763
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002764 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002765 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2766 pe.index = tid;
2767
2768 mvpp2_prs_match_etype(&pe, 0, ETH_P_PPP_SES);
2769
2770 mvpp2_prs_sram_shift_set(&pe, MVPP2_PPPOE_HDR_SIZE,
2771 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2772 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2773 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_PPPOE_MASK,
2774 MVPP2_PRS_RI_PPPOE_MASK);
2775
2776 /* Update shadow table and hw entry */
2777 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2778 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2779 priv->prs_shadow[pe.index].finish = false;
2780 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_PPPOE_MASK,
2781 MVPP2_PRS_RI_PPPOE_MASK);
2782 mvpp2_prs_hw_write(priv, &pe);
2783
2784 /* Ethertype: ARP */
2785 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2786 MVPP2_PE_LAST_FREE_TID);
2787 if (tid < 0)
2788 return tid;
2789
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002790 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002791 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2792 pe.index = tid;
2793
2794 mvpp2_prs_match_etype(&pe, 0, ETH_P_ARP);
2795
2796 /* Generate flow in the next iteration*/
2797 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2798 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2799 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_ARP,
2800 MVPP2_PRS_RI_L3_PROTO_MASK);
2801 /* Set L3 offset */
2802 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2803 MVPP2_ETH_TYPE_LEN,
2804 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2805
2806 /* Update shadow table and hw entry */
2807 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2808 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2809 priv->prs_shadow[pe.index].finish = true;
2810 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_ARP,
2811 MVPP2_PRS_RI_L3_PROTO_MASK);
2812 mvpp2_prs_hw_write(priv, &pe);
2813
2814 /* Ethertype: LBTD */
2815 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2816 MVPP2_PE_LAST_FREE_TID);
2817 if (tid < 0)
2818 return tid;
2819
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002820 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002821 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2822 pe.index = tid;
2823
2824 mvpp2_prs_match_etype(&pe, 0, MVPP2_IP_LBDT_TYPE);
2825
2826 /* Generate flow in the next iteration*/
2827 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2828 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2829 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2830 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2831 MVPP2_PRS_RI_CPU_CODE_MASK |
2832 MVPP2_PRS_RI_UDF3_MASK);
2833 /* Set L3 offset */
2834 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2835 MVPP2_ETH_TYPE_LEN,
2836 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2837
2838 /* Update shadow table and hw entry */
2839 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2840 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2841 priv->prs_shadow[pe.index].finish = true;
2842 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2843 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2844 MVPP2_PRS_RI_CPU_CODE_MASK |
2845 MVPP2_PRS_RI_UDF3_MASK);
2846 mvpp2_prs_hw_write(priv, &pe);
2847
2848 /* Ethertype: IPv4 without options */
2849 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2850 MVPP2_PE_LAST_FREE_TID);
2851 if (tid < 0)
2852 return tid;
2853
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002854 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002855 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2856 pe.index = tid;
2857
2858 mvpp2_prs_match_etype(&pe, 0, ETH_P_IP);
2859 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2860 MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
2861 MVPP2_PRS_IPV4_HEAD_MASK |
2862 MVPP2_PRS_IPV4_IHL_MASK);
2863
2864 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2865 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
2866 MVPP2_PRS_RI_L3_PROTO_MASK);
2867 /* Skip eth_type + 4 bytes of IP header */
2868 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
2869 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2870 /* Set L3 offset */
2871 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2872 MVPP2_ETH_TYPE_LEN,
2873 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2874
2875 /* Update shadow table and hw entry */
2876 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2877 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2878 priv->prs_shadow[pe.index].finish = false;
2879 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4,
2880 MVPP2_PRS_RI_L3_PROTO_MASK);
2881 mvpp2_prs_hw_write(priv, &pe);
2882
2883 /* Ethertype: IPv4 with options */
2884 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2885 MVPP2_PE_LAST_FREE_TID);
2886 if (tid < 0)
2887 return tid;
2888
2889 pe.index = tid;
2890
2891 /* Clear tcam data before updating */
2892 pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(MVPP2_ETH_TYPE_LEN)] = 0x0;
2893 pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(MVPP2_ETH_TYPE_LEN)] = 0x0;
2894
2895 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2896 MVPP2_PRS_IPV4_HEAD,
2897 MVPP2_PRS_IPV4_HEAD_MASK);
2898
2899 /* Clear ri before updating */
2900 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2901 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2902 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
2903 MVPP2_PRS_RI_L3_PROTO_MASK);
2904
2905 /* Update shadow table and hw entry */
2906 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2907 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2908 priv->prs_shadow[pe.index].finish = false;
2909 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4_OPT,
2910 MVPP2_PRS_RI_L3_PROTO_MASK);
2911 mvpp2_prs_hw_write(priv, &pe);
2912
2913 /* Ethertype: IPv6 without options */
2914 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2915 MVPP2_PE_LAST_FREE_TID);
2916 if (tid < 0)
2917 return tid;
2918
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002919 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002920 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2921 pe.index = tid;
2922
2923 mvpp2_prs_match_etype(&pe, 0, ETH_P_IPV6);
2924
2925 /* Skip DIP of IPV6 header */
2926 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 8 +
2927 MVPP2_MAX_L3_ADDR_SIZE,
2928 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2929 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2930 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
2931 MVPP2_PRS_RI_L3_PROTO_MASK);
2932 /* Set L3 offset */
2933 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2934 MVPP2_ETH_TYPE_LEN,
2935 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2936
2937 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2938 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2939 priv->prs_shadow[pe.index].finish = false;
2940 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP6,
2941 MVPP2_PRS_RI_L3_PROTO_MASK);
2942 mvpp2_prs_hw_write(priv, &pe);
2943
2944 /* Default entry for MVPP2_PRS_LU_L2 - Unknown ethtype */
2945 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2946 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2947 pe.index = MVPP2_PE_ETH_TYPE_UN;
2948
2949 /* Unmask all ports */
2950 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2951
2952 /* Generate flow in the next iteration*/
2953 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2954 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2955 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
2956 MVPP2_PRS_RI_L3_PROTO_MASK);
2957 /* Set L3 offset even it's unknown L3 */
2958 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2959 MVPP2_ETH_TYPE_LEN,
2960 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2961
2962 /* Update shadow table and hw entry */
2963 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2964 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2965 priv->prs_shadow[pe.index].finish = true;
2966 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_UN,
2967 MVPP2_PRS_RI_L3_PROTO_MASK);
2968 mvpp2_prs_hw_write(priv, &pe);
2969
2970 return 0;
2971}
2972
2973/* Configure vlan entries and detect up to 2 successive VLAN tags.
2974 * Possible options:
2975 * 0x8100, 0x88A8
2976 * 0x8100, 0x8100
2977 * 0x8100
2978 * 0x88A8
2979 */
2980static int mvpp2_prs_vlan_init(struct platform_device *pdev, struct mvpp2 *priv)
2981{
2982 struct mvpp2_prs_entry pe;
2983 int err;
2984
2985 priv->prs_double_vlans = devm_kcalloc(&pdev->dev, sizeof(bool),
2986 MVPP2_PRS_DBL_VLANS_MAX,
2987 GFP_KERNEL);
2988 if (!priv->prs_double_vlans)
2989 return -ENOMEM;
2990
2991 /* Double VLAN: 0x8100, 0x88A8 */
2992 err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021AD,
2993 MVPP2_PRS_PORT_MASK);
2994 if (err)
2995 return err;
2996
2997 /* Double VLAN: 0x8100, 0x8100 */
2998 err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021Q,
2999 MVPP2_PRS_PORT_MASK);
3000 if (err)
3001 return err;
3002
3003 /* Single VLAN: 0x88a8 */
3004 err = mvpp2_prs_vlan_add(priv, ETH_P_8021AD, MVPP2_PRS_SINGLE_VLAN_AI,
3005 MVPP2_PRS_PORT_MASK);
3006 if (err)
3007 return err;
3008
3009 /* Single VLAN: 0x8100 */
3010 err = mvpp2_prs_vlan_add(priv, ETH_P_8021Q, MVPP2_PRS_SINGLE_VLAN_AI,
3011 MVPP2_PRS_PORT_MASK);
3012 if (err)
3013 return err;
3014
3015 /* Set default double vlan entry */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003016 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003017 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
3018 pe.index = MVPP2_PE_VLAN_DBL;
3019
3020 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
3021 /* Clear ai for next iterations */
3022 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
3023 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_DOUBLE,
3024 MVPP2_PRS_RI_VLAN_MASK);
3025
3026 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_DBL_VLAN_AI_BIT,
3027 MVPP2_PRS_DBL_VLAN_AI_BIT);
3028 /* Unmask all ports */
3029 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3030
3031 /* Update shadow table and hw entry */
3032 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
3033 mvpp2_prs_hw_write(priv, &pe);
3034
3035 /* Set default vlan none entry */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003036 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003037 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
3038 pe.index = MVPP2_PE_VLAN_NONE;
3039
3040 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
3041 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
3042 MVPP2_PRS_RI_VLAN_MASK);
3043
3044 /* Unmask all ports */
3045 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3046
3047 /* Update shadow table and hw entry */
3048 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
3049 mvpp2_prs_hw_write(priv, &pe);
3050
3051 return 0;
3052}
3053
3054/* Set entries for PPPoE ethertype */
3055static int mvpp2_prs_pppoe_init(struct mvpp2 *priv)
3056{
3057 struct mvpp2_prs_entry pe;
3058 int tid;
3059
3060 /* IPv4 over PPPoE with options */
3061 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3062 MVPP2_PE_LAST_FREE_TID);
3063 if (tid < 0)
3064 return tid;
3065
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003066 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003067 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
3068 pe.index = tid;
3069
3070 mvpp2_prs_match_etype(&pe, 0, PPP_IP);
3071
3072 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
3073 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
3074 MVPP2_PRS_RI_L3_PROTO_MASK);
3075 /* Skip eth_type + 4 bytes of IP header */
3076 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
3077 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3078 /* Set L3 offset */
3079 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
3080 MVPP2_ETH_TYPE_LEN,
3081 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3082
3083 /* Update shadow table and hw entry */
3084 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3085 mvpp2_prs_hw_write(priv, &pe);
3086
3087 /* IPv4 over PPPoE without options */
3088 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3089 MVPP2_PE_LAST_FREE_TID);
3090 if (tid < 0)
3091 return tid;
3092
3093 pe.index = tid;
3094
3095 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
3096 MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
3097 MVPP2_PRS_IPV4_HEAD_MASK |
3098 MVPP2_PRS_IPV4_IHL_MASK);
3099
3100 /* Clear ri before updating */
3101 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
3102 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
3103 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
3104 MVPP2_PRS_RI_L3_PROTO_MASK);
3105
3106 /* Update shadow table and hw entry */
3107 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3108 mvpp2_prs_hw_write(priv, &pe);
3109
3110 /* IPv6 over PPPoE */
3111 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3112 MVPP2_PE_LAST_FREE_TID);
3113 if (tid < 0)
3114 return tid;
3115
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003116 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003117 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
3118 pe.index = tid;
3119
3120 mvpp2_prs_match_etype(&pe, 0, PPP_IPV6);
3121
3122 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
3123 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
3124 MVPP2_PRS_RI_L3_PROTO_MASK);
3125 /* Skip eth_type + 4 bytes of IPv6 header */
3126 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
3127 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3128 /* Set L3 offset */
3129 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
3130 MVPP2_ETH_TYPE_LEN,
3131 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3132
3133 /* Update shadow table and hw entry */
3134 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3135 mvpp2_prs_hw_write(priv, &pe);
3136
3137 /* Non-IP over PPPoE */
3138 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3139 MVPP2_PE_LAST_FREE_TID);
3140 if (tid < 0)
3141 return tid;
3142
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003143 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003144 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
3145 pe.index = tid;
3146
3147 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
3148 MVPP2_PRS_RI_L3_PROTO_MASK);
3149
3150 /* Finished: go to flowid generation */
3151 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3152 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3153 /* Set L3 offset even if it's unknown L3 */
3154 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
3155 MVPP2_ETH_TYPE_LEN,
3156 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3157
3158 /* Update shadow table and hw entry */
3159 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3160 mvpp2_prs_hw_write(priv, &pe);
3161
3162 return 0;
3163}
3164
3165/* Initialize entries for IPv4 */
3166static int mvpp2_prs_ip4_init(struct mvpp2 *priv)
3167{
3168 struct mvpp2_prs_entry pe;
3169 int err;
3170
3171 /* Set entries for TCP, UDP and IGMP over IPv4 */
3172 err = mvpp2_prs_ip4_proto(priv, IPPROTO_TCP, MVPP2_PRS_RI_L4_TCP,
3173 MVPP2_PRS_RI_L4_PROTO_MASK);
3174 if (err)
3175 return err;
3176
3177 err = mvpp2_prs_ip4_proto(priv, IPPROTO_UDP, MVPP2_PRS_RI_L4_UDP,
3178 MVPP2_PRS_RI_L4_PROTO_MASK);
3179 if (err)
3180 return err;
3181
3182 err = mvpp2_prs_ip4_proto(priv, IPPROTO_IGMP,
3183 MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
3184 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
3185 MVPP2_PRS_RI_CPU_CODE_MASK |
3186 MVPP2_PRS_RI_UDF3_MASK);
3187 if (err)
3188 return err;
3189
3190 /* IPv4 Broadcast */
3191 err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_BROAD_CAST);
3192 if (err)
3193 return err;
3194
3195 /* IPv4 Multicast */
3196 err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
3197 if (err)
3198 return err;
3199
3200 /* Default IPv4 entry for unknown protocols */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003201 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003202 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
3203 pe.index = MVPP2_PE_IP4_PROTO_UN;
3204
3205 /* Set next lu to IPv4 */
3206 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
3207 mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3208 /* Set L4 offset */
3209 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
3210 sizeof(struct iphdr) - 4,
3211 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3212 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
3213 MVPP2_PRS_IPV4_DIP_AI_BIT);
3214 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3215 MVPP2_PRS_RI_L4_PROTO_MASK);
3216
3217 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
3218 /* Unmask all ports */
3219 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3220
3221 /* Update shadow table and hw entry */
3222 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3223 mvpp2_prs_hw_write(priv, &pe);
3224
3225 /* Default IPv4 entry for unicast address */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003226 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003227 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
3228 pe.index = MVPP2_PE_IP4_ADDR_UN;
3229
3230 /* Finished: go to flowid generation */
3231 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3232 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3233 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
3234 MVPP2_PRS_RI_L3_ADDR_MASK);
3235
3236 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
3237 MVPP2_PRS_IPV4_DIP_AI_BIT);
3238 /* Unmask all ports */
3239 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3240
3241 /* Update shadow table and hw entry */
3242 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3243 mvpp2_prs_hw_write(priv, &pe);
3244
3245 return 0;
3246}
3247
3248/* Initialize entries for IPv6 */
3249static int mvpp2_prs_ip6_init(struct mvpp2 *priv)
3250{
3251 struct mvpp2_prs_entry pe;
3252 int tid, err;
3253
3254 /* Set entries for TCP, UDP and ICMP over IPv6 */
3255 err = mvpp2_prs_ip6_proto(priv, IPPROTO_TCP,
3256 MVPP2_PRS_RI_L4_TCP,
3257 MVPP2_PRS_RI_L4_PROTO_MASK);
3258 if (err)
3259 return err;
3260
3261 err = mvpp2_prs_ip6_proto(priv, IPPROTO_UDP,
3262 MVPP2_PRS_RI_L4_UDP,
3263 MVPP2_PRS_RI_L4_PROTO_MASK);
3264 if (err)
3265 return err;
3266
3267 err = mvpp2_prs_ip6_proto(priv, IPPROTO_ICMPV6,
3268 MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
3269 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
3270 MVPP2_PRS_RI_CPU_CODE_MASK |
3271 MVPP2_PRS_RI_UDF3_MASK);
3272 if (err)
3273 return err;
3274
3275 /* IPv4 is the last header. This is similar case as 6-TCP or 17-UDP */
3276 /* Result Info: UDF7=1, DS lite */
3277 err = mvpp2_prs_ip6_proto(priv, IPPROTO_IPIP,
3278 MVPP2_PRS_RI_UDF7_IP6_LITE,
3279 MVPP2_PRS_RI_UDF7_MASK);
3280 if (err)
3281 return err;
3282
3283 /* IPv6 multicast */
3284 err = mvpp2_prs_ip6_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
3285 if (err)
3286 return err;
3287
3288 /* Entry for checking hop limit */
3289 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3290 MVPP2_PE_LAST_FREE_TID);
3291 if (tid < 0)
3292 return tid;
3293
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003294 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003295 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3296 pe.index = tid;
3297
3298 /* Finished: go to flowid generation */
3299 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3300 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3301 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN |
3302 MVPP2_PRS_RI_DROP_MASK,
3303 MVPP2_PRS_RI_L3_PROTO_MASK |
3304 MVPP2_PRS_RI_DROP_MASK);
3305
3306 mvpp2_prs_tcam_data_byte_set(&pe, 1, 0x00, MVPP2_PRS_IPV6_HOP_MASK);
3307 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3308 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3309
3310 /* Update shadow table and hw entry */
3311 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3312 mvpp2_prs_hw_write(priv, &pe);
3313
3314 /* Default IPv6 entry for unknown protocols */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003315 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003316 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3317 pe.index = MVPP2_PE_IP6_PROTO_UN;
3318
3319 /* Finished: go to flowid generation */
3320 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3321 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3322 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3323 MVPP2_PRS_RI_L4_PROTO_MASK);
3324 /* Set L4 offset relatively to our current place */
3325 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
3326 sizeof(struct ipv6hdr) - 4,
3327 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3328
3329 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3330 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3331 /* Unmask all ports */
3332 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3333
3334 /* Update shadow table and hw entry */
3335 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3336 mvpp2_prs_hw_write(priv, &pe);
3337
3338 /* Default IPv6 entry for unknown ext protocols */
3339 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3340 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3341 pe.index = MVPP2_PE_IP6_EXT_PROTO_UN;
3342
3343 /* Finished: go to flowid generation */
3344 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3345 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3346 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3347 MVPP2_PRS_RI_L4_PROTO_MASK);
3348
3349 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_EXT_AI_BIT,
3350 MVPP2_PRS_IPV6_EXT_AI_BIT);
3351 /* Unmask all ports */
3352 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3353
3354 /* Update shadow table and hw entry */
3355 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3356 mvpp2_prs_hw_write(priv, &pe);
3357
3358 /* Default IPv6 entry for unicast address */
3359 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3360 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3361 pe.index = MVPP2_PE_IP6_ADDR_UN;
3362
3363 /* Finished: go to IPv6 again */
3364 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
3365 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
3366 MVPP2_PRS_RI_L3_ADDR_MASK);
3367 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3368 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3369 /* Shift back to IPV6 NH */
3370 mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3371
3372 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3373 /* Unmask all ports */
3374 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3375
3376 /* Update shadow table and hw entry */
3377 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
3378 mvpp2_prs_hw_write(priv, &pe);
3379
3380 return 0;
3381}
3382
3383/* Parser default initialization */
3384static int mvpp2_prs_default_init(struct platform_device *pdev,
3385 struct mvpp2 *priv)
3386{
3387 int err, index, i;
3388
3389 /* Enable tcam table */
3390 mvpp2_write(priv, MVPP2_PRS_TCAM_CTRL_REG, MVPP2_PRS_TCAM_EN_MASK);
3391
3392 /* Clear all tcam and sram entries */
3393 for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++) {
3394 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
3395 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
3396 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), 0);
3397
3398 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, index);
3399 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
3400 mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), 0);
3401 }
3402
3403 /* Invalidate all tcam entries */
3404 for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++)
3405 mvpp2_prs_hw_inv(priv, index);
3406
3407 priv->prs_shadow = devm_kcalloc(&pdev->dev, MVPP2_PRS_TCAM_SRAM_SIZE,
Markus Elfring37df25e2017-04-17 09:12:34 +02003408 sizeof(*priv->prs_shadow),
Marcin Wojtas3f518502014-07-10 16:52:13 -03003409 GFP_KERNEL);
3410 if (!priv->prs_shadow)
3411 return -ENOMEM;
3412
3413 /* Always start from lookup = 0 */
3414 for (index = 0; index < MVPP2_MAX_PORTS; index++)
3415 mvpp2_prs_hw_port_init(priv, index, MVPP2_PRS_LU_MH,
3416 MVPP2_PRS_PORT_LU_MAX, 0);
3417
3418 mvpp2_prs_def_flow_init(priv);
3419
3420 mvpp2_prs_mh_init(priv);
3421
3422 mvpp2_prs_mac_init(priv);
3423
3424 mvpp2_prs_dsa_init(priv);
3425
3426 err = mvpp2_prs_etype_init(priv);
3427 if (err)
3428 return err;
3429
3430 err = mvpp2_prs_vlan_init(pdev, priv);
3431 if (err)
3432 return err;
3433
3434 err = mvpp2_prs_pppoe_init(priv);
3435 if (err)
3436 return err;
3437
3438 err = mvpp2_prs_ip6_init(priv);
3439 if (err)
3440 return err;
3441
3442 err = mvpp2_prs_ip4_init(priv);
3443 if (err)
3444 return err;
3445
3446 return 0;
3447}
3448
3449/* Compare MAC DA with tcam entry data */
3450static bool mvpp2_prs_mac_range_equals(struct mvpp2_prs_entry *pe,
3451 const u8 *da, unsigned char *mask)
3452{
3453 unsigned char tcam_byte, tcam_mask;
3454 int index;
3455
3456 for (index = 0; index < ETH_ALEN; index++) {
3457 mvpp2_prs_tcam_data_byte_get(pe, index, &tcam_byte, &tcam_mask);
3458 if (tcam_mask != mask[index])
3459 return false;
3460
3461 if ((tcam_mask & tcam_byte) != (da[index] & mask[index]))
3462 return false;
3463 }
3464
3465 return true;
3466}
3467
3468/* Find tcam entry with matched pair <MAC DA, port> */
3469static struct mvpp2_prs_entry *
3470mvpp2_prs_mac_da_range_find(struct mvpp2 *priv, int pmap, const u8 *da,
3471 unsigned char *mask, int udf_type)
3472{
3473 struct mvpp2_prs_entry *pe;
3474 int tid;
3475
Antoine Tenart239dd4e2017-10-24 11:41:28 +02003476 pe = kzalloc(sizeof(*pe), GFP_ATOMIC);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003477 if (!pe)
3478 return NULL;
3479 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
3480
3481 /* Go through the all entires with MVPP2_PRS_LU_MAC */
3482 for (tid = MVPP2_PE_FIRST_FREE_TID;
3483 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
3484 unsigned int entry_pmap;
3485
3486 if (!priv->prs_shadow[tid].valid ||
3487 (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
3488 (priv->prs_shadow[tid].udf != udf_type))
3489 continue;
3490
3491 pe->index = tid;
3492 mvpp2_prs_hw_read(priv, pe);
3493 entry_pmap = mvpp2_prs_tcam_port_map_get(pe);
3494
3495 if (mvpp2_prs_mac_range_equals(pe, da, mask) &&
3496 entry_pmap == pmap)
3497 return pe;
3498 }
3499 kfree(pe);
3500
3501 return NULL;
3502}
3503
3504/* Update parser's mac da entry */
3505static int mvpp2_prs_mac_da_accept(struct mvpp2 *priv, int port,
3506 const u8 *da, bool add)
3507{
3508 struct mvpp2_prs_entry *pe;
3509 unsigned int pmap, len, ri;
3510 unsigned char mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3511 int tid;
3512
3513 /* Scan TCAM and see if entry with this <MAC DA, port> already exist */
3514 pe = mvpp2_prs_mac_da_range_find(priv, (1 << port), da, mask,
3515 MVPP2_PRS_UDF_MAC_DEF);
3516
3517 /* No such entry */
3518 if (!pe) {
3519 if (!add)
3520 return 0;
3521
3522 /* Create new TCAM entry */
3523 /* Find first range mac entry*/
3524 for (tid = MVPP2_PE_FIRST_FREE_TID;
3525 tid <= MVPP2_PE_LAST_FREE_TID; tid++)
3526 if (priv->prs_shadow[tid].valid &&
3527 (priv->prs_shadow[tid].lu == MVPP2_PRS_LU_MAC) &&
3528 (priv->prs_shadow[tid].udf ==
3529 MVPP2_PRS_UDF_MAC_RANGE))
3530 break;
3531
3532 /* Go through the all entries from first to last */
3533 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3534 tid - 1);
3535 if (tid < 0)
3536 return tid;
3537
Antoine Tenart239dd4e2017-10-24 11:41:28 +02003538 pe = kzalloc(sizeof(*pe), GFP_ATOMIC);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003539 if (!pe)
Amitoj Kaur Chawlac2bb7bc2016-02-04 19:25:26 +05303540 return -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003541 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
3542 pe->index = tid;
3543
3544 /* Mask all ports */
3545 mvpp2_prs_tcam_port_map_set(pe, 0);
3546 }
3547
3548 /* Update port mask */
3549 mvpp2_prs_tcam_port_set(pe, port, add);
3550
3551 /* Invalidate the entry if no ports are left enabled */
3552 pmap = mvpp2_prs_tcam_port_map_get(pe);
3553 if (pmap == 0) {
3554 if (add) {
3555 kfree(pe);
Amitoj Kaur Chawlac2bb7bc2016-02-04 19:25:26 +05303556 return -EINVAL;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003557 }
3558 mvpp2_prs_hw_inv(priv, pe->index);
3559 priv->prs_shadow[pe->index].valid = false;
3560 kfree(pe);
3561 return 0;
3562 }
3563
3564 /* Continue - set next lookup */
3565 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_DSA);
3566
3567 /* Set match on DA */
3568 len = ETH_ALEN;
3569 while (len--)
3570 mvpp2_prs_tcam_data_byte_set(pe, len, da[len], 0xff);
3571
3572 /* Set result info bits */
3573 if (is_broadcast_ether_addr(da))
3574 ri = MVPP2_PRS_RI_L2_BCAST;
3575 else if (is_multicast_ether_addr(da))
3576 ri = MVPP2_PRS_RI_L2_MCAST;
3577 else
3578 ri = MVPP2_PRS_RI_L2_UCAST | MVPP2_PRS_RI_MAC_ME_MASK;
3579
3580 mvpp2_prs_sram_ri_update(pe, ri, MVPP2_PRS_RI_L2_CAST_MASK |
3581 MVPP2_PRS_RI_MAC_ME_MASK);
3582 mvpp2_prs_shadow_ri_set(priv, pe->index, ri, MVPP2_PRS_RI_L2_CAST_MASK |
3583 MVPP2_PRS_RI_MAC_ME_MASK);
3584
3585 /* Shift to ethertype */
3586 mvpp2_prs_sram_shift_set(pe, 2 * ETH_ALEN,
3587 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3588
3589 /* Update shadow table and hw entry */
3590 priv->prs_shadow[pe->index].udf = MVPP2_PRS_UDF_MAC_DEF;
3591 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_MAC);
3592 mvpp2_prs_hw_write(priv, pe);
3593
3594 kfree(pe);
3595
3596 return 0;
3597}
3598
3599static int mvpp2_prs_update_mac_da(struct net_device *dev, const u8 *da)
3600{
3601 struct mvpp2_port *port = netdev_priv(dev);
3602 int err;
3603
3604 /* Remove old parser entry */
3605 err = mvpp2_prs_mac_da_accept(port->priv, port->id, dev->dev_addr,
3606 false);
3607 if (err)
3608 return err;
3609
3610 /* Add new parser entry */
3611 err = mvpp2_prs_mac_da_accept(port->priv, port->id, da, true);
3612 if (err)
3613 return err;
3614
3615 /* Set addr in the device */
3616 ether_addr_copy(dev->dev_addr, da);
3617
3618 return 0;
3619}
3620
3621/* Delete all port's multicast simple (not range) entries */
3622static void mvpp2_prs_mcast_del_all(struct mvpp2 *priv, int port)
3623{
3624 struct mvpp2_prs_entry pe;
3625 int index, tid;
3626
3627 for (tid = MVPP2_PE_FIRST_FREE_TID;
3628 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
3629 unsigned char da[ETH_ALEN], da_mask[ETH_ALEN];
3630
3631 if (!priv->prs_shadow[tid].valid ||
3632 (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
3633 (priv->prs_shadow[tid].udf != MVPP2_PRS_UDF_MAC_DEF))
3634 continue;
3635
3636 /* Only simple mac entries */
3637 pe.index = tid;
3638 mvpp2_prs_hw_read(priv, &pe);
3639
3640 /* Read mac addr from entry */
3641 for (index = 0; index < ETH_ALEN; index++)
3642 mvpp2_prs_tcam_data_byte_get(&pe, index, &da[index],
3643 &da_mask[index]);
3644
3645 if (is_multicast_ether_addr(da) && !is_broadcast_ether_addr(da))
3646 /* Delete this entry */
3647 mvpp2_prs_mac_da_accept(priv, port, da, false);
3648 }
3649}
3650
3651static int mvpp2_prs_tag_mode_set(struct mvpp2 *priv, int port, int type)
3652{
3653 switch (type) {
3654 case MVPP2_TAG_TYPE_EDSA:
3655 /* Add port to EDSA entries */
3656 mvpp2_prs_dsa_tag_set(priv, port, true,
3657 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3658 mvpp2_prs_dsa_tag_set(priv, port, true,
3659 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3660 /* Remove port from DSA entries */
3661 mvpp2_prs_dsa_tag_set(priv, port, false,
3662 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3663 mvpp2_prs_dsa_tag_set(priv, port, false,
3664 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3665 break;
3666
3667 case MVPP2_TAG_TYPE_DSA:
3668 /* Add port to DSA entries */
3669 mvpp2_prs_dsa_tag_set(priv, port, true,
3670 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3671 mvpp2_prs_dsa_tag_set(priv, port, true,
3672 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3673 /* Remove port from EDSA entries */
3674 mvpp2_prs_dsa_tag_set(priv, port, false,
3675 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3676 mvpp2_prs_dsa_tag_set(priv, port, false,
3677 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3678 break;
3679
3680 case MVPP2_TAG_TYPE_MH:
3681 case MVPP2_TAG_TYPE_NONE:
3682 /* Remove port form EDSA and DSA entries */
3683 mvpp2_prs_dsa_tag_set(priv, port, false,
3684 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3685 mvpp2_prs_dsa_tag_set(priv, port, false,
3686 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3687 mvpp2_prs_dsa_tag_set(priv, port, false,
3688 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3689 mvpp2_prs_dsa_tag_set(priv, port, false,
3690 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3691 break;
3692
3693 default:
3694 if ((type < 0) || (type > MVPP2_TAG_TYPE_EDSA))
3695 return -EINVAL;
3696 }
3697
3698 return 0;
3699}
3700
3701/* Set prs flow for the port */
3702static int mvpp2_prs_def_flow(struct mvpp2_port *port)
3703{
3704 struct mvpp2_prs_entry *pe;
3705 int tid;
3706
3707 pe = mvpp2_prs_flow_find(port->priv, port->id);
3708
3709 /* Such entry not exist */
3710 if (!pe) {
3711 /* Go through the all entires from last to first */
3712 tid = mvpp2_prs_tcam_first_free(port->priv,
3713 MVPP2_PE_LAST_FREE_TID,
3714 MVPP2_PE_FIRST_FREE_TID);
3715 if (tid < 0)
3716 return tid;
3717
3718 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
3719 if (!pe)
3720 return -ENOMEM;
3721
3722 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
3723 pe->index = tid;
3724
3725 /* Set flow ID*/
3726 mvpp2_prs_sram_ai_update(pe, port->id, MVPP2_PRS_FLOW_ID_MASK);
3727 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
3728
3729 /* Update shadow table */
3730 mvpp2_prs_shadow_set(port->priv, pe->index, MVPP2_PRS_LU_FLOWS);
3731 }
3732
3733 mvpp2_prs_tcam_port_map_set(pe, (1 << port->id));
3734 mvpp2_prs_hw_write(port->priv, pe);
3735 kfree(pe);
3736
3737 return 0;
3738}
3739
3740/* Classifier configuration routines */
3741
3742/* Update classification flow table registers */
3743static void mvpp2_cls_flow_write(struct mvpp2 *priv,
3744 struct mvpp2_cls_flow_entry *fe)
3745{
3746 mvpp2_write(priv, MVPP2_CLS_FLOW_INDEX_REG, fe->index);
3747 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL0_REG, fe->data[0]);
3748 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL1_REG, fe->data[1]);
3749 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL2_REG, fe->data[2]);
3750}
3751
3752/* Update classification lookup table register */
3753static void mvpp2_cls_lookup_write(struct mvpp2 *priv,
3754 struct mvpp2_cls_lookup_entry *le)
3755{
3756 u32 val;
3757
3758 val = (le->way << MVPP2_CLS_LKP_INDEX_WAY_OFFS) | le->lkpid;
3759 mvpp2_write(priv, MVPP2_CLS_LKP_INDEX_REG, val);
3760 mvpp2_write(priv, MVPP2_CLS_LKP_TBL_REG, le->data);
3761}
3762
3763/* Classifier default initialization */
3764static void mvpp2_cls_init(struct mvpp2 *priv)
3765{
3766 struct mvpp2_cls_lookup_entry le;
3767 struct mvpp2_cls_flow_entry fe;
3768 int index;
3769
3770 /* Enable classifier */
3771 mvpp2_write(priv, MVPP2_CLS_MODE_REG, MVPP2_CLS_MODE_ACTIVE_MASK);
3772
3773 /* Clear classifier flow table */
Arnd Bergmanne8f967c2016-11-24 17:28:12 +01003774 memset(&fe.data, 0, sizeof(fe.data));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003775 for (index = 0; index < MVPP2_CLS_FLOWS_TBL_SIZE; index++) {
3776 fe.index = index;
3777 mvpp2_cls_flow_write(priv, &fe);
3778 }
3779
3780 /* Clear classifier lookup table */
3781 le.data = 0;
3782 for (index = 0; index < MVPP2_CLS_LKP_TBL_SIZE; index++) {
3783 le.lkpid = index;
3784 le.way = 0;
3785 mvpp2_cls_lookup_write(priv, &le);
3786
3787 le.way = 1;
3788 mvpp2_cls_lookup_write(priv, &le);
3789 }
3790}
3791
3792static void mvpp2_cls_port_config(struct mvpp2_port *port)
3793{
3794 struct mvpp2_cls_lookup_entry le;
3795 u32 val;
3796
3797 /* Set way for the port */
3798 val = mvpp2_read(port->priv, MVPP2_CLS_PORT_WAY_REG);
3799 val &= ~MVPP2_CLS_PORT_WAY_MASK(port->id);
3800 mvpp2_write(port->priv, MVPP2_CLS_PORT_WAY_REG, val);
3801
3802 /* Pick the entry to be accessed in lookup ID decoding table
3803 * according to the way and lkpid.
3804 */
3805 le.lkpid = port->id;
3806 le.way = 0;
3807 le.data = 0;
3808
3809 /* Set initial CPU queue for receiving packets */
3810 le.data &= ~MVPP2_CLS_LKP_TBL_RXQ_MASK;
3811 le.data |= port->first_rxq;
3812
3813 /* Disable classification engines */
3814 le.data &= ~MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK;
3815
3816 /* Update lookup ID table entry */
3817 mvpp2_cls_lookup_write(port->priv, &le);
3818}
3819
3820/* Set CPU queue number for oversize packets */
3821static void mvpp2_cls_oversize_rxq_set(struct mvpp2_port *port)
3822{
3823 u32 val;
3824
3825 mvpp2_write(port->priv, MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port->id),
3826 port->first_rxq & MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK);
3827
3828 mvpp2_write(port->priv, MVPP2_CLS_SWFWD_P2HQ_REG(port->id),
3829 (port->first_rxq >> MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS));
3830
3831 val = mvpp2_read(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG);
3832 val |= MVPP2_CLS_SWFWD_PCTRL_MASK(port->id);
3833 mvpp2_write(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG, val);
3834}
3835
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003836static void *mvpp2_frag_alloc(const struct mvpp2_bm_pool *pool)
3837{
3838 if (likely(pool->frag_size <= PAGE_SIZE))
3839 return netdev_alloc_frag(pool->frag_size);
3840 else
3841 return kmalloc(pool->frag_size, GFP_ATOMIC);
3842}
3843
3844static void mvpp2_frag_free(const struct mvpp2_bm_pool *pool, void *data)
3845{
3846 if (likely(pool->frag_size <= PAGE_SIZE))
3847 skb_free_frag(data);
3848 else
3849 kfree(data);
3850}
3851
Marcin Wojtas3f518502014-07-10 16:52:13 -03003852/* Buffer Manager configuration routines */
3853
3854/* Create pool */
3855static int mvpp2_bm_pool_create(struct platform_device *pdev,
3856 struct mvpp2 *priv,
3857 struct mvpp2_bm_pool *bm_pool, int size)
3858{
Marcin Wojtas3f518502014-07-10 16:52:13 -03003859 u32 val;
3860
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003861 /* Number of buffer pointers must be a multiple of 16, as per
3862 * hardware constraints
3863 */
3864 if (!IS_ALIGNED(size, 16))
3865 return -EINVAL;
3866
3867 /* PPv2.1 needs 8 bytes per buffer pointer, PPv2.2 needs 16
3868 * bytes per buffer pointer
3869 */
3870 if (priv->hw_version == MVPP21)
3871 bm_pool->size_bytes = 2 * sizeof(u32) * size;
3872 else
3873 bm_pool->size_bytes = 2 * sizeof(u64) * size;
3874
3875 bm_pool->virt_addr = dma_alloc_coherent(&pdev->dev, bm_pool->size_bytes,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003876 &bm_pool->dma_addr,
Marcin Wojtas3f518502014-07-10 16:52:13 -03003877 GFP_KERNEL);
3878 if (!bm_pool->virt_addr)
3879 return -ENOMEM;
3880
Thomas Petazzonid3158802017-02-21 11:28:13 +01003881 if (!IS_ALIGNED((unsigned long)bm_pool->virt_addr,
3882 MVPP2_BM_POOL_PTR_ALIGN)) {
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003883 dma_free_coherent(&pdev->dev, bm_pool->size_bytes,
3884 bm_pool->virt_addr, bm_pool->dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003885 dev_err(&pdev->dev, "BM pool %d is not %d bytes aligned\n",
3886 bm_pool->id, MVPP2_BM_POOL_PTR_ALIGN);
3887 return -ENOMEM;
3888 }
3889
3890 mvpp2_write(priv, MVPP2_BM_POOL_BASE_REG(bm_pool->id),
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003891 lower_32_bits(bm_pool->dma_addr));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003892 mvpp2_write(priv, MVPP2_BM_POOL_SIZE_REG(bm_pool->id), size);
3893
3894 val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
3895 val |= MVPP2_BM_START_MASK;
3896 mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
3897
3898 bm_pool->type = MVPP2_BM_FREE;
3899 bm_pool->size = size;
3900 bm_pool->pkt_size = 0;
3901 bm_pool->buf_num = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003902
3903 return 0;
3904}
3905
3906/* Set pool buffer size */
3907static void mvpp2_bm_pool_bufsize_set(struct mvpp2 *priv,
3908 struct mvpp2_bm_pool *bm_pool,
3909 int buf_size)
3910{
3911 u32 val;
3912
3913 bm_pool->buf_size = buf_size;
3914
3915 val = ALIGN(buf_size, 1 << MVPP2_POOL_BUF_SIZE_OFFSET);
3916 mvpp2_write(priv, MVPP2_POOL_BUF_SIZE_REG(bm_pool->id), val);
3917}
3918
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003919static void mvpp2_bm_bufs_get_addrs(struct device *dev, struct mvpp2 *priv,
3920 struct mvpp2_bm_pool *bm_pool,
3921 dma_addr_t *dma_addr,
3922 phys_addr_t *phys_addr)
3923{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02003924 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01003925
3926 *dma_addr = mvpp2_percpu_read(priv, cpu,
3927 MVPP2_BM_PHY_ALLOC_REG(bm_pool->id));
3928 *phys_addr = mvpp2_percpu_read(priv, cpu, MVPP2_BM_VIRT_ALLOC_REG);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003929
3930 if (priv->hw_version == MVPP22) {
3931 u32 val;
3932 u32 dma_addr_highbits, phys_addr_highbits;
3933
Thomas Petazzonia7868412017-03-07 16:53:13 +01003934 val = mvpp2_percpu_read(priv, cpu, MVPP22_BM_ADDR_HIGH_ALLOC);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003935 dma_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_PHYS_MASK);
3936 phys_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_VIRT_MASK) >>
3937 MVPP22_BM_ADDR_HIGH_VIRT_SHIFT;
3938
3939 if (sizeof(dma_addr_t) == 8)
3940 *dma_addr |= (u64)dma_addr_highbits << 32;
3941
3942 if (sizeof(phys_addr_t) == 8)
3943 *phys_addr |= (u64)phys_addr_highbits << 32;
3944 }
Thomas Petazzonia704bb52017-06-10 23:18:22 +02003945
3946 put_cpu();
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003947}
3948
Ezequiel Garcia7861f122014-07-21 13:48:14 -03003949/* Free all buffers from the pool */
Marcin Wojtas4229d502015-12-03 15:20:50 +01003950static void mvpp2_bm_bufs_free(struct device *dev, struct mvpp2 *priv,
3951 struct mvpp2_bm_pool *bm_pool)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003952{
3953 int i;
3954
Ezequiel Garcia7861f122014-07-21 13:48:14 -03003955 for (i = 0; i < bm_pool->buf_num; i++) {
Thomas Petazzoni20396132017-03-07 16:53:00 +01003956 dma_addr_t buf_dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003957 phys_addr_t buf_phys_addr;
3958 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003959
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003960 mvpp2_bm_bufs_get_addrs(dev, priv, bm_pool,
3961 &buf_dma_addr, &buf_phys_addr);
Marcin Wojtas4229d502015-12-03 15:20:50 +01003962
Thomas Petazzoni20396132017-03-07 16:53:00 +01003963 dma_unmap_single(dev, buf_dma_addr,
Marcin Wojtas4229d502015-12-03 15:20:50 +01003964 bm_pool->buf_size, DMA_FROM_DEVICE);
3965
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003966 data = (void *)phys_to_virt(buf_phys_addr);
3967 if (!data)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003968 break;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003969
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003970 mvpp2_frag_free(bm_pool, data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003971 }
3972
3973 /* Update BM driver with number of buffers removed from pool */
3974 bm_pool->buf_num -= i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003975}
3976
3977/* Cleanup pool */
3978static int mvpp2_bm_pool_destroy(struct platform_device *pdev,
3979 struct mvpp2 *priv,
3980 struct mvpp2_bm_pool *bm_pool)
3981{
Marcin Wojtas3f518502014-07-10 16:52:13 -03003982 u32 val;
3983
Marcin Wojtas4229d502015-12-03 15:20:50 +01003984 mvpp2_bm_bufs_free(&pdev->dev, priv, bm_pool);
Ezequiel Garciad74c96c2014-07-21 13:48:13 -03003985 if (bm_pool->buf_num) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03003986 WARN(1, "cannot free all buffers in pool %d\n", bm_pool->id);
3987 return 0;
3988 }
3989
3990 val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
3991 val |= MVPP2_BM_STOP_MASK;
3992 mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
3993
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003994 dma_free_coherent(&pdev->dev, bm_pool->size_bytes,
Marcin Wojtas3f518502014-07-10 16:52:13 -03003995 bm_pool->virt_addr,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003996 bm_pool->dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003997 return 0;
3998}
3999
4000static int mvpp2_bm_pools_init(struct platform_device *pdev,
4001 struct mvpp2 *priv)
4002{
4003 int i, err, size;
4004 struct mvpp2_bm_pool *bm_pool;
4005
4006 /* Create all pools with maximum size */
4007 size = MVPP2_BM_POOL_SIZE_MAX;
4008 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
4009 bm_pool = &priv->bm_pools[i];
4010 bm_pool->id = i;
4011 err = mvpp2_bm_pool_create(pdev, priv, bm_pool, size);
4012 if (err)
4013 goto err_unroll_pools;
4014 mvpp2_bm_pool_bufsize_set(priv, bm_pool, 0);
4015 }
4016 return 0;
4017
4018err_unroll_pools:
4019 dev_err(&pdev->dev, "failed to create BM pool %d, size %d\n", i, size);
4020 for (i = i - 1; i >= 0; i--)
4021 mvpp2_bm_pool_destroy(pdev, priv, &priv->bm_pools[i]);
4022 return err;
4023}
4024
4025static int mvpp2_bm_init(struct platform_device *pdev, struct mvpp2 *priv)
4026{
4027 int i, err;
4028
4029 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
4030 /* Mask BM all interrupts */
4031 mvpp2_write(priv, MVPP2_BM_INTR_MASK_REG(i), 0);
4032 /* Clear BM cause register */
4033 mvpp2_write(priv, MVPP2_BM_INTR_CAUSE_REG(i), 0);
4034 }
4035
4036 /* Allocate and initialize BM pools */
4037 priv->bm_pools = devm_kcalloc(&pdev->dev, MVPP2_BM_POOLS_NUM,
Markus Elfring81f915e2017-04-17 09:06:33 +02004038 sizeof(*priv->bm_pools), GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004039 if (!priv->bm_pools)
4040 return -ENOMEM;
4041
4042 err = mvpp2_bm_pools_init(pdev, priv);
4043 if (err < 0)
4044 return err;
4045 return 0;
4046}
4047
4048/* Attach long pool to rxq */
4049static void mvpp2_rxq_long_pool_set(struct mvpp2_port *port,
4050 int lrxq, int long_pool)
4051{
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004052 u32 val, mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004053 int prxq;
4054
4055 /* Get queue physical ID */
4056 prxq = port->rxqs[lrxq]->id;
4057
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004058 if (port->priv->hw_version == MVPP21)
4059 mask = MVPP21_RXQ_POOL_LONG_MASK;
4060 else
4061 mask = MVPP22_RXQ_POOL_LONG_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004062
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004063 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
4064 val &= ~mask;
4065 val |= (long_pool << MVPP2_RXQ_POOL_LONG_OFFS) & mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004066 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
4067}
4068
4069/* Attach short pool to rxq */
4070static void mvpp2_rxq_short_pool_set(struct mvpp2_port *port,
4071 int lrxq, int short_pool)
4072{
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004073 u32 val, mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004074 int prxq;
4075
4076 /* Get queue physical ID */
4077 prxq = port->rxqs[lrxq]->id;
4078
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004079 if (port->priv->hw_version == MVPP21)
4080 mask = MVPP21_RXQ_POOL_SHORT_MASK;
4081 else
4082 mask = MVPP22_RXQ_POOL_SHORT_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004083
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004084 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
4085 val &= ~mask;
4086 val |= (short_pool << MVPP2_RXQ_POOL_SHORT_OFFS) & mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004087 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
4088}
4089
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004090static void *mvpp2_buf_alloc(struct mvpp2_port *port,
4091 struct mvpp2_bm_pool *bm_pool,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004092 dma_addr_t *buf_dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004093 phys_addr_t *buf_phys_addr,
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004094 gfp_t gfp_mask)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004095{
Thomas Petazzoni20396132017-03-07 16:53:00 +01004096 dma_addr_t dma_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004097 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004098
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004099 data = mvpp2_frag_alloc(bm_pool);
4100 if (!data)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004101 return NULL;
4102
Thomas Petazzoni20396132017-03-07 16:53:00 +01004103 dma_addr = dma_map_single(port->dev->dev.parent, data,
4104 MVPP2_RX_BUF_SIZE(bm_pool->pkt_size),
4105 DMA_FROM_DEVICE);
4106 if (unlikely(dma_mapping_error(port->dev->dev.parent, dma_addr))) {
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004107 mvpp2_frag_free(bm_pool, data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004108 return NULL;
4109 }
Thomas Petazzoni20396132017-03-07 16:53:00 +01004110 *buf_dma_addr = dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004111 *buf_phys_addr = virt_to_phys(data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004112
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004113 return data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004114}
4115
Marcin Wojtas3f518502014-07-10 16:52:13 -03004116/* Release buffer to BM */
4117static inline void mvpp2_bm_pool_put(struct mvpp2_port *port, int pool,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004118 dma_addr_t buf_dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004119 phys_addr_t buf_phys_addr)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004120{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004121 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01004122
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004123 if (port->priv->hw_version == MVPP22) {
4124 u32 val = 0;
4125
4126 if (sizeof(dma_addr_t) == 8)
4127 val |= upper_32_bits(buf_dma_addr) &
4128 MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK;
4129
4130 if (sizeof(phys_addr_t) == 8)
4131 val |= (upper_32_bits(buf_phys_addr)
4132 << MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT) &
4133 MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK;
4134
Thomas Petazzonia7868412017-03-07 16:53:13 +01004135 mvpp2_percpu_write(port->priv, cpu,
4136 MVPP22_BM_ADDR_HIGH_RLS_REG, val);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004137 }
4138
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004139 /* MVPP2_BM_VIRT_RLS_REG is not interpreted by HW, and simply
4140 * returned in the "cookie" field of the RX
4141 * descriptor. Instead of storing the virtual address, we
4142 * store the physical address
4143 */
Thomas Petazzonia7868412017-03-07 16:53:13 +01004144 mvpp2_percpu_write(port->priv, cpu,
4145 MVPP2_BM_VIRT_RLS_REG, buf_phys_addr);
4146 mvpp2_percpu_write(port->priv, cpu,
4147 MVPP2_BM_PHY_RLS_REG(pool), buf_dma_addr);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004148
4149 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03004150}
4151
Marcin Wojtas3f518502014-07-10 16:52:13 -03004152/* Allocate buffers for the pool */
4153static int mvpp2_bm_bufs_add(struct mvpp2_port *port,
4154 struct mvpp2_bm_pool *bm_pool, int buf_num)
4155{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004156 int i, buf_size, total_size;
Thomas Petazzoni20396132017-03-07 16:53:00 +01004157 dma_addr_t dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004158 phys_addr_t phys_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004159 void *buf;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004160
4161 buf_size = MVPP2_RX_BUF_SIZE(bm_pool->pkt_size);
4162 total_size = MVPP2_RX_TOTAL_SIZE(buf_size);
4163
4164 if (buf_num < 0 ||
4165 (buf_num + bm_pool->buf_num > bm_pool->size)) {
4166 netdev_err(port->dev,
4167 "cannot allocate %d buffers for pool %d\n",
4168 buf_num, bm_pool->id);
4169 return 0;
4170 }
4171
Marcin Wojtas3f518502014-07-10 16:52:13 -03004172 for (i = 0; i < buf_num; i++) {
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004173 buf = mvpp2_buf_alloc(port, bm_pool, &dma_addr,
4174 &phys_addr, GFP_KERNEL);
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004175 if (!buf)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004176 break;
4177
Thomas Petazzoni20396132017-03-07 16:53:00 +01004178 mvpp2_bm_pool_put(port, bm_pool->id, dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004179 phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004180 }
4181
4182 /* Update BM driver with number of buffers added to pool */
4183 bm_pool->buf_num += i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004184
4185 netdev_dbg(port->dev,
4186 "%s pool %d: pkt_size=%4d, buf_size=%4d, total_size=%4d\n",
4187 bm_pool->type == MVPP2_BM_SWF_SHORT ? "short" : " long",
4188 bm_pool->id, bm_pool->pkt_size, buf_size, total_size);
4189
4190 netdev_dbg(port->dev,
4191 "%s pool %d: %d of %d buffers added\n",
4192 bm_pool->type == MVPP2_BM_SWF_SHORT ? "short" : " long",
4193 bm_pool->id, i, buf_num);
4194 return i;
4195}
4196
4197/* Notify the driver that BM pool is being used as specific type and return the
4198 * pool pointer on success
4199 */
4200static struct mvpp2_bm_pool *
4201mvpp2_bm_pool_use(struct mvpp2_port *port, int pool, enum mvpp2_bm_type type,
4202 int pkt_size)
4203{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004204 struct mvpp2_bm_pool *new_pool = &port->priv->bm_pools[pool];
4205 int num;
4206
4207 if (new_pool->type != MVPP2_BM_FREE && new_pool->type != type) {
4208 netdev_err(port->dev, "mixing pool types is forbidden\n");
4209 return NULL;
4210 }
4211
Marcin Wojtas3f518502014-07-10 16:52:13 -03004212 if (new_pool->type == MVPP2_BM_FREE)
4213 new_pool->type = type;
4214
4215 /* Allocate buffers in case BM pool is used as long pool, but packet
4216 * size doesn't match MTU or BM pool hasn't being used yet
4217 */
4218 if (((type == MVPP2_BM_SWF_LONG) && (pkt_size > new_pool->pkt_size)) ||
4219 (new_pool->pkt_size == 0)) {
4220 int pkts_num;
4221
4222 /* Set default buffer number or free all the buffers in case
4223 * the pool is not empty
4224 */
4225 pkts_num = new_pool->buf_num;
4226 if (pkts_num == 0)
4227 pkts_num = type == MVPP2_BM_SWF_LONG ?
4228 MVPP2_BM_LONG_BUF_NUM :
4229 MVPP2_BM_SHORT_BUF_NUM;
4230 else
Marcin Wojtas4229d502015-12-03 15:20:50 +01004231 mvpp2_bm_bufs_free(port->dev->dev.parent,
4232 port->priv, new_pool);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004233
4234 new_pool->pkt_size = pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004235 new_pool->frag_size =
4236 SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
4237 MVPP2_SKB_SHINFO_SIZE;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004238
4239 /* Allocate buffers for this pool */
4240 num = mvpp2_bm_bufs_add(port, new_pool, pkts_num);
4241 if (num != pkts_num) {
4242 WARN(1, "pool %d: %d of %d allocated\n",
4243 new_pool->id, num, pkts_num);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004244 return NULL;
4245 }
4246 }
4247
4248 mvpp2_bm_pool_bufsize_set(port->priv, new_pool,
4249 MVPP2_RX_BUF_SIZE(new_pool->pkt_size));
4250
Marcin Wojtas3f518502014-07-10 16:52:13 -03004251 return new_pool;
4252}
4253
4254/* Initialize pools for swf */
4255static int mvpp2_swf_bm_pool_init(struct mvpp2_port *port)
4256{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004257 int rxq;
4258
4259 if (!port->pool_long) {
4260 port->pool_long =
4261 mvpp2_bm_pool_use(port, MVPP2_BM_SWF_LONG_POOL(port->id),
4262 MVPP2_BM_SWF_LONG,
4263 port->pkt_size);
4264 if (!port->pool_long)
4265 return -ENOMEM;
4266
Marcin Wojtas3f518502014-07-10 16:52:13 -03004267 port->pool_long->port_map |= (1 << port->id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004268
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004269 for (rxq = 0; rxq < port->nrxqs; rxq++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004270 mvpp2_rxq_long_pool_set(port, rxq, port->pool_long->id);
4271 }
4272
4273 if (!port->pool_short) {
4274 port->pool_short =
4275 mvpp2_bm_pool_use(port, MVPP2_BM_SWF_SHORT_POOL,
4276 MVPP2_BM_SWF_SHORT,
4277 MVPP2_BM_SHORT_PKT_SIZE);
4278 if (!port->pool_short)
4279 return -ENOMEM;
4280
Marcin Wojtas3f518502014-07-10 16:52:13 -03004281 port->pool_short->port_map |= (1 << port->id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004282
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004283 for (rxq = 0; rxq < port->nrxqs; rxq++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004284 mvpp2_rxq_short_pool_set(port, rxq,
4285 port->pool_short->id);
4286 }
4287
4288 return 0;
4289}
4290
4291static int mvpp2_bm_update_mtu(struct net_device *dev, int mtu)
4292{
4293 struct mvpp2_port *port = netdev_priv(dev);
4294 struct mvpp2_bm_pool *port_pool = port->pool_long;
4295 int num, pkts_num = port_pool->buf_num;
4296 int pkt_size = MVPP2_RX_PKT_SIZE(mtu);
4297
4298 /* Update BM pool with new buffer size */
Marcin Wojtas4229d502015-12-03 15:20:50 +01004299 mvpp2_bm_bufs_free(dev->dev.parent, port->priv, port_pool);
Ezequiel Garciad74c96c2014-07-21 13:48:13 -03004300 if (port_pool->buf_num) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004301 WARN(1, "cannot free all buffers in pool %d\n", port_pool->id);
4302 return -EIO;
4303 }
4304
4305 port_pool->pkt_size = pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004306 port_pool->frag_size = SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
4307 MVPP2_SKB_SHINFO_SIZE;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004308 num = mvpp2_bm_bufs_add(port, port_pool, pkts_num);
4309 if (num != pkts_num) {
4310 WARN(1, "pool %d: %d of %d allocated\n",
4311 port_pool->id, num, pkts_num);
4312 return -EIO;
4313 }
4314
4315 mvpp2_bm_pool_bufsize_set(port->priv, port_pool,
4316 MVPP2_RX_BUF_SIZE(port_pool->pkt_size));
4317 dev->mtu = mtu;
4318 netdev_update_features(dev);
4319 return 0;
4320}
4321
4322static inline void mvpp2_interrupts_enable(struct mvpp2_port *port)
4323{
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004324 int i, sw_thread_mask = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004325
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004326 for (i = 0; i < port->nqvecs; i++)
4327 sw_thread_mask |= port->qvecs[i].sw_thread_mask;
4328
Marcin Wojtas3f518502014-07-10 16:52:13 -03004329 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004330 MVPP2_ISR_ENABLE_INTERRUPT(sw_thread_mask));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004331}
4332
4333static inline void mvpp2_interrupts_disable(struct mvpp2_port *port)
4334{
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004335 int i, sw_thread_mask = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004336
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004337 for (i = 0; i < port->nqvecs; i++)
4338 sw_thread_mask |= port->qvecs[i].sw_thread_mask;
4339
Marcin Wojtas3f518502014-07-10 16:52:13 -03004340 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004341 MVPP2_ISR_DISABLE_INTERRUPT(sw_thread_mask));
4342}
4343
4344static inline void mvpp2_qvec_interrupt_enable(struct mvpp2_queue_vector *qvec)
4345{
4346 struct mvpp2_port *port = qvec->port;
4347
4348 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
4349 MVPP2_ISR_ENABLE_INTERRUPT(qvec->sw_thread_mask));
4350}
4351
4352static inline void mvpp2_qvec_interrupt_disable(struct mvpp2_queue_vector *qvec)
4353{
4354 struct mvpp2_port *port = qvec->port;
4355
4356 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
4357 MVPP2_ISR_DISABLE_INTERRUPT(qvec->sw_thread_mask));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004358}
4359
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004360/* Mask the current CPU's Rx/Tx interrupts
4361 * Called by on_each_cpu(), guaranteed to run with migration disabled,
4362 * using smp_processor_id() is OK.
4363 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004364static void mvpp2_interrupts_mask(void *arg)
4365{
4366 struct mvpp2_port *port = arg;
4367
Thomas Petazzonia7868412017-03-07 16:53:13 +01004368 mvpp2_percpu_write(port->priv, smp_processor_id(),
4369 MVPP2_ISR_RX_TX_MASK_REG(port->id), 0);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004370}
4371
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004372/* Unmask the current CPU's Rx/Tx interrupts.
4373 * Called by on_each_cpu(), guaranteed to run with migration disabled,
4374 * using smp_processor_id() is OK.
4375 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004376static void mvpp2_interrupts_unmask(void *arg)
4377{
4378 struct mvpp2_port *port = arg;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02004379 u32 val;
4380
4381 val = MVPP2_CAUSE_MISC_SUM_MASK |
4382 MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
4383 if (port->has_tx_irqs)
4384 val |= MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004385
Thomas Petazzonia7868412017-03-07 16:53:13 +01004386 mvpp2_percpu_write(port->priv, smp_processor_id(),
Thomas Petazzoni213f4282017-08-03 10:42:00 +02004387 MVPP2_ISR_RX_TX_MASK_REG(port->id), val);
4388}
4389
4390static void
4391mvpp2_shared_interrupt_mask_unmask(struct mvpp2_port *port, bool mask)
4392{
4393 u32 val;
4394 int i;
4395
4396 if (port->priv->hw_version != MVPP22)
4397 return;
4398
4399 if (mask)
4400 val = 0;
4401 else
4402 val = MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
4403
4404 for (i = 0; i < port->nqvecs; i++) {
4405 struct mvpp2_queue_vector *v = port->qvecs + i;
4406
4407 if (v->type != MVPP2_QUEUE_VECTOR_SHARED)
4408 continue;
4409
4410 mvpp2_percpu_write(port->priv, v->sw_thread_id,
4411 MVPP2_ISR_RX_TX_MASK_REG(port->id), val);
4412 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004413}
4414
4415/* Port configuration routines */
4416
Antoine Ténartf84bf382017-08-22 19:08:27 +02004417static void mvpp22_gop_init_rgmii(struct mvpp2_port *port)
4418{
4419 struct mvpp2 *priv = port->priv;
4420 u32 val;
4421
4422 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
4423 val |= GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT;
4424 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
4425
4426 regmap_read(priv->sysctrl_base, GENCONF_CTRL0, &val);
4427 if (port->gop_id == 2)
4428 val |= GENCONF_CTRL0_PORT0_RGMII | GENCONF_CTRL0_PORT1_RGMII;
4429 else if (port->gop_id == 3)
4430 val |= GENCONF_CTRL0_PORT1_RGMII_MII;
4431 regmap_write(priv->sysctrl_base, GENCONF_CTRL0, val);
4432}
4433
4434static void mvpp22_gop_init_sgmii(struct mvpp2_port *port)
4435{
4436 struct mvpp2 *priv = port->priv;
4437 u32 val;
4438
4439 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
4440 val |= GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT |
4441 GENCONF_PORT_CTRL0_RX_DATA_SAMPLE;
4442 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
4443
4444 if (port->gop_id > 1) {
4445 regmap_read(priv->sysctrl_base, GENCONF_CTRL0, &val);
4446 if (port->gop_id == 2)
4447 val &= ~GENCONF_CTRL0_PORT0_RGMII;
4448 else if (port->gop_id == 3)
4449 val &= ~GENCONF_CTRL0_PORT1_RGMII_MII;
4450 regmap_write(priv->sysctrl_base, GENCONF_CTRL0, val);
4451 }
4452}
4453
4454static void mvpp22_gop_init_10gkr(struct mvpp2_port *port)
4455{
4456 struct mvpp2 *priv = port->priv;
4457 void __iomem *mpcs = priv->iface_base + MVPP22_MPCS_BASE(port->gop_id);
4458 void __iomem *xpcs = priv->iface_base + MVPP22_XPCS_BASE(port->gop_id);
4459 u32 val;
4460
4461 /* XPCS */
4462 val = readl(xpcs + MVPP22_XPCS_CFG0);
4463 val &= ~(MVPP22_XPCS_CFG0_PCS_MODE(0x3) |
4464 MVPP22_XPCS_CFG0_ACTIVE_LANE(0x3));
4465 val |= MVPP22_XPCS_CFG0_ACTIVE_LANE(2);
4466 writel(val, xpcs + MVPP22_XPCS_CFG0);
4467
4468 /* MPCS */
4469 val = readl(mpcs + MVPP22_MPCS_CTRL);
4470 val &= ~MVPP22_MPCS_CTRL_FWD_ERR_CONN;
4471 writel(val, mpcs + MVPP22_MPCS_CTRL);
4472
4473 val = readl(mpcs + MVPP22_MPCS_CLK_RESET);
4474 val &= ~(MVPP22_MPCS_CLK_RESET_DIV_RATIO(0x7) | MAC_CLK_RESET_MAC |
4475 MAC_CLK_RESET_SD_RX | MAC_CLK_RESET_SD_TX);
4476 val |= MVPP22_MPCS_CLK_RESET_DIV_RATIO(1);
4477 writel(val, mpcs + MVPP22_MPCS_CLK_RESET);
4478
4479 val &= ~MVPP22_MPCS_CLK_RESET_DIV_SET;
4480 val |= MAC_CLK_RESET_MAC | MAC_CLK_RESET_SD_RX | MAC_CLK_RESET_SD_TX;
4481 writel(val, mpcs + MVPP22_MPCS_CLK_RESET);
4482}
4483
4484static int mvpp22_gop_init(struct mvpp2_port *port)
4485{
4486 struct mvpp2 *priv = port->priv;
4487 u32 val;
4488
4489 if (!priv->sysctrl_base)
4490 return 0;
4491
4492 switch (port->phy_interface) {
4493 case PHY_INTERFACE_MODE_RGMII:
4494 case PHY_INTERFACE_MODE_RGMII_ID:
4495 case PHY_INTERFACE_MODE_RGMII_RXID:
4496 case PHY_INTERFACE_MODE_RGMII_TXID:
4497 if (port->gop_id == 0)
4498 goto invalid_conf;
4499 mvpp22_gop_init_rgmii(port);
4500 break;
4501 case PHY_INTERFACE_MODE_SGMII:
4502 mvpp22_gop_init_sgmii(port);
4503 break;
4504 case PHY_INTERFACE_MODE_10GKR:
4505 if (port->gop_id != 0)
4506 goto invalid_conf;
4507 mvpp22_gop_init_10gkr(port);
4508 break;
4509 default:
4510 goto unsupported_conf;
4511 }
4512
4513 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL1, &val);
4514 val |= GENCONF_PORT_CTRL1_RESET(port->gop_id) |
4515 GENCONF_PORT_CTRL1_EN(port->gop_id);
4516 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL1, val);
4517
4518 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
4519 val |= GENCONF_PORT_CTRL0_CLK_DIV_PHASE_CLR;
4520 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
4521
4522 regmap_read(priv->sysctrl_base, GENCONF_SOFT_RESET1, &val);
4523 val |= GENCONF_SOFT_RESET1_GOP;
4524 regmap_write(priv->sysctrl_base, GENCONF_SOFT_RESET1, val);
4525
4526unsupported_conf:
4527 return 0;
4528
4529invalid_conf:
4530 netdev_err(port->dev, "Invalid port configuration\n");
4531 return -EINVAL;
4532}
4533
Antoine Tenartfd3651b2017-09-01 11:04:54 +02004534static void mvpp22_gop_unmask_irq(struct mvpp2_port *port)
4535{
4536 u32 val;
4537
4538 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
4539 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4540 /* Enable the GMAC link status irq for this port */
4541 val = readl(port->base + MVPP22_GMAC_INT_SUM_MASK);
4542 val |= MVPP22_GMAC_INT_SUM_MASK_LINK_STAT;
4543 writel(val, port->base + MVPP22_GMAC_INT_SUM_MASK);
4544 }
4545
4546 if (port->gop_id == 0) {
4547 /* Enable the XLG/GIG irqs for this port */
4548 val = readl(port->base + MVPP22_XLG_EXT_INT_MASK);
4549 if (port->phy_interface == PHY_INTERFACE_MODE_10GKR)
4550 val |= MVPP22_XLG_EXT_INT_MASK_XLG;
4551 else
4552 val |= MVPP22_XLG_EXT_INT_MASK_GIG;
4553 writel(val, port->base + MVPP22_XLG_EXT_INT_MASK);
4554 }
4555}
4556
4557static void mvpp22_gop_mask_irq(struct mvpp2_port *port)
4558{
4559 u32 val;
4560
4561 if (port->gop_id == 0) {
4562 val = readl(port->base + MVPP22_XLG_EXT_INT_MASK);
4563 val &= ~(MVPP22_XLG_EXT_INT_MASK_XLG |
4564 MVPP22_XLG_EXT_INT_MASK_GIG);
4565 writel(val, port->base + MVPP22_XLG_EXT_INT_MASK);
4566 }
4567
4568 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
4569 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4570 val = readl(port->base + MVPP22_GMAC_INT_SUM_MASK);
4571 val &= ~MVPP22_GMAC_INT_SUM_MASK_LINK_STAT;
4572 writel(val, port->base + MVPP22_GMAC_INT_SUM_MASK);
4573 }
4574}
4575
4576static void mvpp22_gop_setup_irq(struct mvpp2_port *port)
4577{
4578 u32 val;
4579
4580 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
4581 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4582 val = readl(port->base + MVPP22_GMAC_INT_MASK);
4583 val |= MVPP22_GMAC_INT_MASK_LINK_STAT;
4584 writel(val, port->base + MVPP22_GMAC_INT_MASK);
4585 }
4586
4587 if (port->gop_id == 0) {
4588 val = readl(port->base + MVPP22_XLG_INT_MASK);
4589 val |= MVPP22_XLG_INT_MASK_LINK;
4590 writel(val, port->base + MVPP22_XLG_INT_MASK);
4591 }
4592
4593 mvpp22_gop_unmask_irq(port);
4594}
4595
Antoine Tenart542897d2017-08-30 10:29:15 +02004596static int mvpp22_comphy_init(struct mvpp2_port *port)
4597{
4598 enum phy_mode mode;
4599 int ret;
4600
4601 if (!port->comphy)
4602 return 0;
4603
4604 switch (port->phy_interface) {
4605 case PHY_INTERFACE_MODE_SGMII:
4606 mode = PHY_MODE_SGMII;
4607 break;
4608 case PHY_INTERFACE_MODE_10GKR:
4609 mode = PHY_MODE_10GKR;
4610 break;
4611 default:
4612 return -EINVAL;
4613 }
4614
4615 ret = phy_set_mode(port->comphy, mode);
4616 if (ret)
4617 return ret;
4618
4619 return phy_power_on(port->comphy);
4620}
4621
Antoine Ténart39193572017-08-22 19:08:24 +02004622static void mvpp2_port_mii_gmac_configure_mode(struct mvpp2_port *port)
4623{
4624 u32 val;
4625
4626 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4627 val = readl(port->base + MVPP22_GMAC_CTRL_4_REG);
4628 val |= MVPP22_CTRL4_SYNC_BYPASS_DIS | MVPP22_CTRL4_DP_CLK_SEL |
4629 MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
4630 val &= ~MVPP22_CTRL4_EXT_PIN_GMII_SEL;
4631 writel(val, port->base + MVPP22_GMAC_CTRL_4_REG);
4632
4633 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4634 val |= MVPP2_GMAC_DISABLE_PADDING;
4635 val &= ~MVPP2_GMAC_FLOW_CTRL_MASK;
4636 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
Antoine Tenart1df22702017-09-01 11:04:52 +02004637 } else if (phy_interface_mode_is_rgmii(port->phy_interface)) {
Antoine Ténart39193572017-08-22 19:08:24 +02004638 val = readl(port->base + MVPP22_GMAC_CTRL_4_REG);
4639 val |= MVPP22_CTRL4_EXT_PIN_GMII_SEL |
4640 MVPP22_CTRL4_SYNC_BYPASS_DIS |
4641 MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
4642 val &= ~MVPP22_CTRL4_DP_CLK_SEL;
4643 writel(val, port->base + MVPP22_GMAC_CTRL_4_REG);
4644
4645 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4646 val &= ~MVPP2_GMAC_DISABLE_PADDING;
4647 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4648 }
4649
4650 /* The port is connected to a copper PHY */
4651 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4652 val &= ~MVPP2_GMAC_PORT_TYPE_MASK;
4653 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4654
4655 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4656 val |= MVPP2_GMAC_IN_BAND_AUTONEG_BYPASS |
4657 MVPP2_GMAC_AN_SPEED_EN | MVPP2_GMAC_FLOW_CTRL_AUTONEG |
4658 MVPP2_GMAC_AN_DUPLEX_EN;
4659 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
4660 val |= MVPP2_GMAC_IN_BAND_AUTONEG;
4661 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4662}
4663
4664static void mvpp2_port_mii_gmac_configure(struct mvpp2_port *port)
4665{
4666 u32 val;
4667
4668 /* Force link down */
4669 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4670 val &= ~MVPP2_GMAC_FORCE_LINK_PASS;
4671 val |= MVPP2_GMAC_FORCE_LINK_DOWN;
4672 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4673
4674 /* Set the GMAC in a reset state */
4675 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4676 val |= MVPP2_GMAC_PORT_RESET_MASK;
4677 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4678
4679 /* Configure the PCS and in-band AN */
4680 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4681 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4682 val |= MVPP2_GMAC_INBAND_AN_MASK | MVPP2_GMAC_PCS_ENABLE_MASK;
Antoine Tenart1df22702017-09-01 11:04:52 +02004683 } else if (phy_interface_mode_is_rgmii(port->phy_interface)) {
Antoine Ténart39193572017-08-22 19:08:24 +02004684 val &= ~MVPP2_GMAC_PCS_ENABLE_MASK;
Antoine Ténart39193572017-08-22 19:08:24 +02004685 }
4686 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4687
4688 mvpp2_port_mii_gmac_configure_mode(port);
4689
4690 /* Unset the GMAC reset state */
4691 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4692 val &= ~MVPP2_GMAC_PORT_RESET_MASK;
4693 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4694
4695 /* Stop forcing link down */
4696 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4697 val &= ~MVPP2_GMAC_FORCE_LINK_DOWN;
4698 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4699}
4700
Antoine Ténart77321952017-08-22 19:08:25 +02004701static void mvpp2_port_mii_xlg_configure(struct mvpp2_port *port)
4702{
4703 u32 val;
4704
4705 if (port->gop_id != 0)
4706 return;
4707
4708 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
4709 val |= MVPP22_XLG_CTRL0_RX_FLOW_CTRL_EN;
4710 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
4711
4712 val = readl(port->base + MVPP22_XLG_CTRL4_REG);
4713 val &= ~MVPP22_XLG_CTRL4_MACMODSELECT_GMAC;
4714 val |= MVPP22_XLG_CTRL4_FWD_FC | MVPP22_XLG_CTRL4_FWD_PFC;
4715 writel(val, port->base + MVPP22_XLG_CTRL4_REG);
4716}
4717
Thomas Petazzoni26975822017-03-07 16:53:14 +01004718static void mvpp22_port_mii_set(struct mvpp2_port *port)
4719{
4720 u32 val;
4721
Thomas Petazzoni26975822017-03-07 16:53:14 +01004722 /* Only GOP port 0 has an XLG MAC */
4723 if (port->gop_id == 0) {
4724 val = readl(port->base + MVPP22_XLG_CTRL3_REG);
4725 val &= ~MVPP22_XLG_CTRL3_MACMODESELECT_MASK;
Antoine Ténart725757a2017-06-12 16:01:39 +02004726
4727 if (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
4728 port->phy_interface == PHY_INTERFACE_MODE_10GKR)
4729 val |= MVPP22_XLG_CTRL3_MACMODESELECT_10G;
4730 else
4731 val |= MVPP22_XLG_CTRL3_MACMODESELECT_GMAC;
4732
Thomas Petazzoni26975822017-03-07 16:53:14 +01004733 writel(val, port->base + MVPP22_XLG_CTRL3_REG);
4734 }
Thomas Petazzoni26975822017-03-07 16:53:14 +01004735}
4736
Marcin Wojtas3f518502014-07-10 16:52:13 -03004737static void mvpp2_port_mii_set(struct mvpp2_port *port)
4738{
Thomas Petazzoni26975822017-03-07 16:53:14 +01004739 if (port->priv->hw_version == MVPP22)
4740 mvpp22_port_mii_set(port);
4741
Antoine Tenart1df22702017-09-01 11:04:52 +02004742 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
Antoine Ténart39193572017-08-22 19:08:24 +02004743 port->phy_interface == PHY_INTERFACE_MODE_SGMII)
4744 mvpp2_port_mii_gmac_configure(port);
Antoine Ténart77321952017-08-22 19:08:25 +02004745 else if (port->phy_interface == PHY_INTERFACE_MODE_10GKR)
4746 mvpp2_port_mii_xlg_configure(port);
Marcin Wojtas08a23752014-07-21 13:48:12 -03004747}
4748
4749static void mvpp2_port_fc_adv_enable(struct mvpp2_port *port)
4750{
4751 u32 val;
4752
4753 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4754 val |= MVPP2_GMAC_FC_ADV_EN;
4755 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004756}
4757
4758static void mvpp2_port_enable(struct mvpp2_port *port)
4759{
4760 u32 val;
4761
Antoine Ténart725757a2017-06-12 16:01:39 +02004762 /* Only GOP port 0 has an XLG MAC */
4763 if (port->gop_id == 0 &&
4764 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
4765 port->phy_interface == PHY_INTERFACE_MODE_10GKR)) {
4766 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
4767 val |= MVPP22_XLG_CTRL0_PORT_EN |
4768 MVPP22_XLG_CTRL0_MAC_RESET_DIS;
4769 val &= ~MVPP22_XLG_CTRL0_MIB_CNT_DIS;
4770 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
4771 } else {
4772 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4773 val |= MVPP2_GMAC_PORT_EN_MASK;
4774 val |= MVPP2_GMAC_MIB_CNTR_EN_MASK;
4775 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4776 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004777}
4778
4779static void mvpp2_port_disable(struct mvpp2_port *port)
4780{
4781 u32 val;
4782
Antoine Ténart725757a2017-06-12 16:01:39 +02004783 /* Only GOP port 0 has an XLG MAC */
4784 if (port->gop_id == 0 &&
4785 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
4786 port->phy_interface == PHY_INTERFACE_MODE_10GKR)) {
4787 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
4788 val &= ~(MVPP22_XLG_CTRL0_PORT_EN |
4789 MVPP22_XLG_CTRL0_MAC_RESET_DIS);
4790 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
4791 } else {
4792 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4793 val &= ~(MVPP2_GMAC_PORT_EN_MASK);
4794 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4795 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004796}
4797
4798/* Set IEEE 802.3x Flow Control Xon Packet Transmission Mode */
4799static void mvpp2_port_periodic_xon_disable(struct mvpp2_port *port)
4800{
4801 u32 val;
4802
4803 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG) &
4804 ~MVPP2_GMAC_PERIODIC_XON_EN_MASK;
4805 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
4806}
4807
4808/* Configure loopback port */
4809static void mvpp2_port_loopback_set(struct mvpp2_port *port)
4810{
4811 u32 val;
4812
4813 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
4814
4815 if (port->speed == 1000)
4816 val |= MVPP2_GMAC_GMII_LB_EN_MASK;
4817 else
4818 val &= ~MVPP2_GMAC_GMII_LB_EN_MASK;
4819
4820 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
4821 val |= MVPP2_GMAC_PCS_LB_EN_MASK;
4822 else
4823 val &= ~MVPP2_GMAC_PCS_LB_EN_MASK;
4824
4825 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
4826}
4827
Miquel Raynal118d6292017-11-06 22:56:53 +01004828struct mvpp2_ethtool_counter {
4829 unsigned int offset;
4830 const char string[ETH_GSTRING_LEN];
4831 bool reg_is_64b;
4832};
4833
4834static u64 mvpp2_read_count(struct mvpp2_port *port,
4835 const struct mvpp2_ethtool_counter *counter)
4836{
4837 u64 val;
4838
4839 val = readl(port->stats_base + counter->offset);
4840 if (counter->reg_is_64b)
4841 val += (u64)readl(port->stats_base + counter->offset + 4) << 32;
4842
4843 return val;
4844}
4845
4846/* Due to the fact that software statistics and hardware statistics are, by
4847 * design, incremented at different moments in the chain of packet processing,
4848 * it is very likely that incoming packets could have been dropped after being
4849 * counted by hardware but before reaching software statistics (most probably
4850 * multicast packets), and in the oppposite way, during transmission, FCS bytes
4851 * are added in between as well as TSO skb will be split and header bytes added.
4852 * Hence, statistics gathered from userspace with ifconfig (software) and
4853 * ethtool (hardware) cannot be compared.
4854 */
4855static const struct mvpp2_ethtool_counter mvpp2_ethtool_regs[] = {
4856 { MVPP2_MIB_GOOD_OCTETS_RCVD, "good_octets_received", true },
4857 { MVPP2_MIB_BAD_OCTETS_RCVD, "bad_octets_received" },
4858 { MVPP2_MIB_CRC_ERRORS_SENT, "crc_errors_sent" },
4859 { MVPP2_MIB_UNICAST_FRAMES_RCVD, "unicast_frames_received" },
4860 { MVPP2_MIB_BROADCAST_FRAMES_RCVD, "broadcast_frames_received" },
4861 { MVPP2_MIB_MULTICAST_FRAMES_RCVD, "multicast_frames_received" },
4862 { MVPP2_MIB_FRAMES_64_OCTETS, "frames_64_octets" },
4863 { MVPP2_MIB_FRAMES_65_TO_127_OCTETS, "frames_65_to_127_octet" },
4864 { MVPP2_MIB_FRAMES_128_TO_255_OCTETS, "frames_128_to_255_octet" },
4865 { MVPP2_MIB_FRAMES_256_TO_511_OCTETS, "frames_256_to_511_octet" },
4866 { MVPP2_MIB_FRAMES_512_TO_1023_OCTETS, "frames_512_to_1023_octet" },
4867 { MVPP2_MIB_FRAMES_1024_TO_MAX_OCTETS, "frames_1024_to_max_octet" },
4868 { MVPP2_MIB_GOOD_OCTETS_SENT, "good_octets_sent", true },
4869 { MVPP2_MIB_UNICAST_FRAMES_SENT, "unicast_frames_sent" },
4870 { MVPP2_MIB_MULTICAST_FRAMES_SENT, "multicast_frames_sent" },
4871 { MVPP2_MIB_BROADCAST_FRAMES_SENT, "broadcast_frames_sent" },
4872 { MVPP2_MIB_FC_SENT, "fc_sent" },
4873 { MVPP2_MIB_FC_RCVD, "fc_received" },
4874 { MVPP2_MIB_RX_FIFO_OVERRUN, "rx_fifo_overrun" },
4875 { MVPP2_MIB_UNDERSIZE_RCVD, "undersize_received" },
4876 { MVPP2_MIB_FRAGMENTS_RCVD, "fragments_received" },
4877 { MVPP2_MIB_OVERSIZE_RCVD, "oversize_received" },
4878 { MVPP2_MIB_JABBER_RCVD, "jabber_received" },
4879 { MVPP2_MIB_MAC_RCV_ERROR, "mac_receive_error" },
4880 { MVPP2_MIB_BAD_CRC_EVENT, "bad_crc_event" },
4881 { MVPP2_MIB_COLLISION, "collision" },
4882 { MVPP2_MIB_LATE_COLLISION, "late_collision" },
4883};
4884
4885static void mvpp2_ethtool_get_strings(struct net_device *netdev, u32 sset,
4886 u8 *data)
4887{
4888 if (sset == ETH_SS_STATS) {
4889 int i;
4890
4891 for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_regs); i++)
4892 memcpy(data + i * ETH_GSTRING_LEN,
4893 &mvpp2_ethtool_regs[i].string, ETH_GSTRING_LEN);
4894 }
4895}
4896
4897static void mvpp2_gather_hw_statistics(struct work_struct *work)
4898{
4899 struct delayed_work *del_work = to_delayed_work(work);
Miquel Raynale5c500e2017-11-08 08:59:40 +01004900 struct mvpp2_port *port = container_of(del_work, struct mvpp2_port,
4901 stats_work);
Miquel Raynal118d6292017-11-06 22:56:53 +01004902 u64 *pstats;
Miquel Raynale5c500e2017-11-08 08:59:40 +01004903 int i;
Miquel Raynal118d6292017-11-06 22:56:53 +01004904
Miquel Raynale5c500e2017-11-08 08:59:40 +01004905 mutex_lock(&port->gather_stats_lock);
Miquel Raynal118d6292017-11-06 22:56:53 +01004906
Miquel Raynale5c500e2017-11-08 08:59:40 +01004907 pstats = port->ethtool_stats;
4908 for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_regs); i++)
4909 *pstats++ += mvpp2_read_count(port, &mvpp2_ethtool_regs[i]);
Miquel Raynal118d6292017-11-06 22:56:53 +01004910
4911 /* No need to read again the counters right after this function if it
4912 * was called asynchronously by the user (ie. use of ethtool).
4913 */
Miquel Raynale5c500e2017-11-08 08:59:40 +01004914 cancel_delayed_work(&port->stats_work);
4915 queue_delayed_work(port->priv->stats_queue, &port->stats_work,
Miquel Raynal118d6292017-11-06 22:56:53 +01004916 MVPP2_MIB_COUNTERS_STATS_DELAY);
4917
Miquel Raynale5c500e2017-11-08 08:59:40 +01004918 mutex_unlock(&port->gather_stats_lock);
Miquel Raynal118d6292017-11-06 22:56:53 +01004919}
4920
4921static void mvpp2_ethtool_get_stats(struct net_device *dev,
4922 struct ethtool_stats *stats, u64 *data)
4923{
4924 struct mvpp2_port *port = netdev_priv(dev);
4925
Miquel Raynale5c500e2017-11-08 08:59:40 +01004926 /* Update statistics for the given port, then take the lock to avoid
4927 * concurrent accesses on the ethtool_stats structure during its copy.
4928 */
4929 mvpp2_gather_hw_statistics(&port->stats_work.work);
Miquel Raynal118d6292017-11-06 22:56:53 +01004930
Miquel Raynale5c500e2017-11-08 08:59:40 +01004931 mutex_lock(&port->gather_stats_lock);
Miquel Raynal118d6292017-11-06 22:56:53 +01004932 memcpy(data, port->ethtool_stats,
4933 sizeof(u64) * ARRAY_SIZE(mvpp2_ethtool_regs));
Miquel Raynale5c500e2017-11-08 08:59:40 +01004934 mutex_unlock(&port->gather_stats_lock);
Miquel Raynal118d6292017-11-06 22:56:53 +01004935}
4936
4937static int mvpp2_ethtool_get_sset_count(struct net_device *dev, int sset)
4938{
4939 if (sset == ETH_SS_STATS)
4940 return ARRAY_SIZE(mvpp2_ethtool_regs);
4941
4942 return -EOPNOTSUPP;
4943}
4944
Marcin Wojtas3f518502014-07-10 16:52:13 -03004945static void mvpp2_port_reset(struct mvpp2_port *port)
4946{
4947 u32 val;
Miquel Raynal118d6292017-11-06 22:56:53 +01004948 unsigned int i;
4949
4950 /* Read the GOP statistics to reset the hardware counters */
4951 for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_regs); i++)
4952 mvpp2_read_count(port, &mvpp2_ethtool_regs[i]);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004953
4954 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
4955 ~MVPP2_GMAC_PORT_RESET_MASK;
4956 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4957
4958 while (readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
4959 MVPP2_GMAC_PORT_RESET_MASK)
4960 continue;
4961}
4962
4963/* Change maximum receive size of the port */
4964static inline void mvpp2_gmac_max_rx_size_set(struct mvpp2_port *port)
4965{
4966 u32 val;
4967
4968 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4969 val &= ~MVPP2_GMAC_MAX_RX_SIZE_MASK;
4970 val |= (((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
4971 MVPP2_GMAC_MAX_RX_SIZE_OFFS);
4972 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4973}
4974
Stefan Chulski76eb1b12017-08-22 19:08:26 +02004975/* Change maximum receive size of the port */
4976static inline void mvpp2_xlg_max_rx_size_set(struct mvpp2_port *port)
4977{
4978 u32 val;
4979
4980 val = readl(port->base + MVPP22_XLG_CTRL1_REG);
4981 val &= ~MVPP22_XLG_CTRL1_FRAMESIZELIMIT_MASK;
4982 val |= ((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
Antoine Ténartec15ecd2017-08-25 15:24:46 +02004983 MVPP22_XLG_CTRL1_FRAMESIZELIMIT_OFFS;
Stefan Chulski76eb1b12017-08-22 19:08:26 +02004984 writel(val, port->base + MVPP22_XLG_CTRL1_REG);
4985}
4986
Marcin Wojtas3f518502014-07-10 16:52:13 -03004987/* Set defaults to the MVPP2 port */
4988static void mvpp2_defaults_set(struct mvpp2_port *port)
4989{
4990 int tx_port_num, val, queue, ptxq, lrxq;
4991
Thomas Petazzoni3d9017d2017-03-07 16:53:11 +01004992 if (port->priv->hw_version == MVPP21) {
4993 /* Configure port to loopback if needed */
4994 if (port->flags & MVPP2_F_LOOPBACK)
4995 mvpp2_port_loopback_set(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004996
Thomas Petazzoni3d9017d2017-03-07 16:53:11 +01004997 /* Update TX FIFO MIN Threshold */
4998 val = readl(port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
4999 val &= ~MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK;
5000 /* Min. TX threshold must be less than minimal packet length */
5001 val |= MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(64 - 4 - 2);
5002 writel(val, port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
5003 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03005004
5005 /* Disable Legacy WRR, Disable EJP, Release from reset */
5006 tx_port_num = mvpp2_egress_port(port);
5007 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG,
5008 tx_port_num);
5009 mvpp2_write(port->priv, MVPP2_TXP_SCHED_CMD_1_REG, 0);
5010
5011 /* Close bandwidth for all queues */
5012 for (queue = 0; queue < MVPP2_MAX_TXQ; queue++) {
5013 ptxq = mvpp2_txq_phys(port->id, queue);
5014 mvpp2_write(port->priv,
5015 MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(ptxq), 0);
5016 }
5017
5018 /* Set refill period to 1 usec, refill tokens
5019 * and bucket size to maximum
5020 */
5021 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PERIOD_REG,
5022 port->priv->tclk / USEC_PER_SEC);
5023 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_REFILL_REG);
5024 val &= ~MVPP2_TXP_REFILL_PERIOD_ALL_MASK;
5025 val |= MVPP2_TXP_REFILL_PERIOD_MASK(1);
5026 val |= MVPP2_TXP_REFILL_TOKENS_ALL_MASK;
5027 mvpp2_write(port->priv, MVPP2_TXP_SCHED_REFILL_REG, val);
5028 val = MVPP2_TXP_TOKEN_SIZE_MAX;
5029 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
5030
5031 /* Set MaximumLowLatencyPacketSize value to 256 */
5032 mvpp2_write(port->priv, MVPP2_RX_CTRL_REG(port->id),
5033 MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK |
5034 MVPP2_RX_LOW_LATENCY_PKT_SIZE(256));
5035
5036 /* Enable Rx cache snoop */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005037 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005038 queue = port->rxqs[lrxq]->id;
5039 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
5040 val |= MVPP2_SNOOP_PKT_SIZE_MASK |
5041 MVPP2_SNOOP_BUF_HDR_MASK;
5042 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
5043 }
5044
5045 /* At default, mask all interrupts to all present cpus */
5046 mvpp2_interrupts_disable(port);
5047}
5048
5049/* Enable/disable receiving packets */
5050static void mvpp2_ingress_enable(struct mvpp2_port *port)
5051{
5052 u32 val;
5053 int lrxq, queue;
5054
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005055 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005056 queue = port->rxqs[lrxq]->id;
5057 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
5058 val &= ~MVPP2_RXQ_DISABLE_MASK;
5059 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
5060 }
5061}
5062
5063static void mvpp2_ingress_disable(struct mvpp2_port *port)
5064{
5065 u32 val;
5066 int lrxq, queue;
5067
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005068 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005069 queue = port->rxqs[lrxq]->id;
5070 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
5071 val |= MVPP2_RXQ_DISABLE_MASK;
5072 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
5073 }
5074}
5075
5076/* Enable transmit via physical egress queue
5077 * - HW starts take descriptors from DRAM
5078 */
5079static void mvpp2_egress_enable(struct mvpp2_port *port)
5080{
5081 u32 qmap;
5082 int queue;
5083 int tx_port_num = mvpp2_egress_port(port);
5084
5085 /* Enable all initialized TXs. */
5086 qmap = 0;
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005087 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005088 struct mvpp2_tx_queue *txq = port->txqs[queue];
5089
Markus Elfringdbbb2f02017-04-17 14:07:52 +02005090 if (txq->descs)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005091 qmap |= (1 << queue);
5092 }
5093
5094 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
5095 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG, qmap);
5096}
5097
5098/* Disable transmit via physical egress queue
5099 * - HW doesn't take descriptors from DRAM
5100 */
5101static void mvpp2_egress_disable(struct mvpp2_port *port)
5102{
5103 u32 reg_data;
5104 int delay;
5105 int tx_port_num = mvpp2_egress_port(port);
5106
5107 /* Issue stop command for active channels only */
5108 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
5109 reg_data = (mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG)) &
5110 MVPP2_TXP_SCHED_ENQ_MASK;
5111 if (reg_data != 0)
5112 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG,
5113 (reg_data << MVPP2_TXP_SCHED_DISQ_OFFSET));
5114
5115 /* Wait for all Tx activity to terminate. */
5116 delay = 0;
5117 do {
5118 if (delay >= MVPP2_TX_DISABLE_TIMEOUT_MSEC) {
5119 netdev_warn(port->dev,
5120 "Tx stop timed out, status=0x%08x\n",
5121 reg_data);
5122 break;
5123 }
5124 mdelay(1);
5125 delay++;
5126
5127 /* Check port TX Command register that all
5128 * Tx queues are stopped
5129 */
5130 reg_data = mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG);
5131 } while (reg_data & MVPP2_TXP_SCHED_ENQ_MASK);
5132}
5133
5134/* Rx descriptors helper methods */
5135
5136/* Get number of Rx descriptors occupied by received packets */
5137static inline int
5138mvpp2_rxq_received(struct mvpp2_port *port, int rxq_id)
5139{
5140 u32 val = mvpp2_read(port->priv, MVPP2_RXQ_STATUS_REG(rxq_id));
5141
5142 return val & MVPP2_RXQ_OCCUPIED_MASK;
5143}
5144
5145/* Update Rx queue status with the number of occupied and available
5146 * Rx descriptor slots.
5147 */
5148static inline void
5149mvpp2_rxq_status_update(struct mvpp2_port *port, int rxq_id,
5150 int used_count, int free_count)
5151{
5152 /* Decrement the number of used descriptors and increment count
5153 * increment the number of free descriptors.
5154 */
5155 u32 val = used_count | (free_count << MVPP2_RXQ_NUM_NEW_OFFSET);
5156
5157 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_UPDATE_REG(rxq_id), val);
5158}
5159
5160/* Get pointer to next RX descriptor to be processed by SW */
5161static inline struct mvpp2_rx_desc *
5162mvpp2_rxq_next_desc_get(struct mvpp2_rx_queue *rxq)
5163{
5164 int rx_desc = rxq->next_desc_to_proc;
5165
5166 rxq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(rxq, rx_desc);
5167 prefetch(rxq->descs + rxq->next_desc_to_proc);
5168 return rxq->descs + rx_desc;
5169}
5170
5171/* Set rx queue offset */
5172static void mvpp2_rxq_offset_set(struct mvpp2_port *port,
5173 int prxq, int offset)
5174{
5175 u32 val;
5176
5177 /* Convert offset from bytes to units of 32 bytes */
5178 offset = offset >> 5;
5179
5180 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
5181 val &= ~MVPP2_RXQ_PACKET_OFFSET_MASK;
5182
5183 /* Offset is in */
5184 val |= ((offset << MVPP2_RXQ_PACKET_OFFSET_OFFS) &
5185 MVPP2_RXQ_PACKET_OFFSET_MASK);
5186
5187 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
5188}
5189
Marcin Wojtas3f518502014-07-10 16:52:13 -03005190/* Tx descriptors helper methods */
5191
Marcin Wojtas3f518502014-07-10 16:52:13 -03005192/* Get pointer to next Tx descriptor to be processed (send) by HW */
5193static struct mvpp2_tx_desc *
5194mvpp2_txq_next_desc_get(struct mvpp2_tx_queue *txq)
5195{
5196 int tx_desc = txq->next_desc_to_proc;
5197
5198 txq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(txq, tx_desc);
5199 return txq->descs + tx_desc;
5200}
5201
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005202/* Update HW with number of aggregated Tx descriptors to be sent
5203 *
5204 * Called only from mvpp2_tx(), so migration is disabled, using
5205 * smp_processor_id() is OK.
5206 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03005207static void mvpp2_aggr_txq_pend_desc_add(struct mvpp2_port *port, int pending)
5208{
5209 /* aggregated access - relevant TXQ number is written in TX desc */
Thomas Petazzonia7868412017-03-07 16:53:13 +01005210 mvpp2_percpu_write(port->priv, smp_processor_id(),
5211 MVPP2_AGGR_TXQ_UPDATE_REG, pending);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005212}
5213
5214
5215/* Check if there are enough free descriptors in aggregated txq.
5216 * If not, update the number of occupied descriptors and repeat the check.
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005217 *
5218 * Called only from mvpp2_tx(), so migration is disabled, using
5219 * smp_processor_id() is OK.
Marcin Wojtas3f518502014-07-10 16:52:13 -03005220 */
5221static int mvpp2_aggr_desc_num_check(struct mvpp2 *priv,
5222 struct mvpp2_tx_queue *aggr_txq, int num)
5223{
Antoine Tenart02856a32017-10-30 11:23:32 +01005224 if ((aggr_txq->count + num) > MVPP2_AGGR_TXQ_SIZE) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005225 /* Update number of occupied aggregated Tx descriptors */
5226 int cpu = smp_processor_id();
5227 u32 val = mvpp2_read(priv, MVPP2_AGGR_TXQ_STATUS_REG(cpu));
5228
5229 aggr_txq->count = val & MVPP2_AGGR_TXQ_PENDING_MASK;
5230 }
5231
Antoine Tenart02856a32017-10-30 11:23:32 +01005232 if ((aggr_txq->count + num) > MVPP2_AGGR_TXQ_SIZE)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005233 return -ENOMEM;
5234
5235 return 0;
5236}
5237
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005238/* Reserved Tx descriptors allocation request
5239 *
5240 * Called only from mvpp2_txq_reserved_desc_num_proc(), itself called
5241 * only by mvpp2_tx(), so migration is disabled, using
5242 * smp_processor_id() is OK.
5243 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03005244static int mvpp2_txq_alloc_reserved_desc(struct mvpp2 *priv,
5245 struct mvpp2_tx_queue *txq, int num)
5246{
5247 u32 val;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005248 int cpu = smp_processor_id();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005249
5250 val = (txq->id << MVPP2_TXQ_RSVD_REQ_Q_OFFSET) | num;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005251 mvpp2_percpu_write(priv, cpu, MVPP2_TXQ_RSVD_REQ_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005252
Thomas Petazzonia7868412017-03-07 16:53:13 +01005253 val = mvpp2_percpu_read(priv, cpu, MVPP2_TXQ_RSVD_RSLT_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005254
5255 return val & MVPP2_TXQ_RSVD_RSLT_MASK;
5256}
5257
5258/* Check if there are enough reserved descriptors for transmission.
5259 * If not, request chunk of reserved descriptors and check again.
5260 */
5261static int mvpp2_txq_reserved_desc_num_proc(struct mvpp2 *priv,
5262 struct mvpp2_tx_queue *txq,
5263 struct mvpp2_txq_pcpu *txq_pcpu,
5264 int num)
5265{
5266 int req, cpu, desc_count;
5267
5268 if (txq_pcpu->reserved_num >= num)
5269 return 0;
5270
5271 /* Not enough descriptors reserved! Update the reserved descriptor
5272 * count and check again.
5273 */
5274
5275 desc_count = 0;
5276 /* Compute total of used descriptors */
5277 for_each_present_cpu(cpu) {
5278 struct mvpp2_txq_pcpu *txq_pcpu_aux;
5279
5280 txq_pcpu_aux = per_cpu_ptr(txq->pcpu, cpu);
5281 desc_count += txq_pcpu_aux->count;
5282 desc_count += txq_pcpu_aux->reserved_num;
5283 }
5284
5285 req = max(MVPP2_CPU_DESC_CHUNK, num - txq_pcpu->reserved_num);
5286 desc_count += req;
5287
5288 if (desc_count >
5289 (txq->size - (num_present_cpus() * MVPP2_CPU_DESC_CHUNK)))
5290 return -ENOMEM;
5291
5292 txq_pcpu->reserved_num += mvpp2_txq_alloc_reserved_desc(priv, txq, req);
5293
5294 /* OK, the descriptor cound has been updated: check again. */
5295 if (txq_pcpu->reserved_num < num)
5296 return -ENOMEM;
5297 return 0;
5298}
5299
5300/* Release the last allocated Tx descriptor. Useful to handle DMA
5301 * mapping failures in the Tx path.
5302 */
5303static void mvpp2_txq_desc_put(struct mvpp2_tx_queue *txq)
5304{
5305 if (txq->next_desc_to_proc == 0)
5306 txq->next_desc_to_proc = txq->last_desc - 1;
5307 else
5308 txq->next_desc_to_proc--;
5309}
5310
5311/* Set Tx descriptors fields relevant for CSUM calculation */
5312static u32 mvpp2_txq_desc_csum(int l3_offs, int l3_proto,
5313 int ip_hdr_len, int l4_proto)
5314{
5315 u32 command;
5316
5317 /* fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
5318 * G_L4_chk, L4_type required only for checksum calculation
5319 */
5320 command = (l3_offs << MVPP2_TXD_L3_OFF_SHIFT);
5321 command |= (ip_hdr_len << MVPP2_TXD_IP_HLEN_SHIFT);
5322 command |= MVPP2_TXD_IP_CSUM_DISABLE;
5323
5324 if (l3_proto == swab16(ETH_P_IP)) {
5325 command &= ~MVPP2_TXD_IP_CSUM_DISABLE; /* enable IPv4 csum */
5326 command &= ~MVPP2_TXD_L3_IP6; /* enable IPv4 */
5327 } else {
5328 command |= MVPP2_TXD_L3_IP6; /* enable IPv6 */
5329 }
5330
5331 if (l4_proto == IPPROTO_TCP) {
5332 command &= ~MVPP2_TXD_L4_UDP; /* enable TCP */
5333 command &= ~MVPP2_TXD_L4_CSUM_FRAG; /* generate L4 csum */
5334 } else if (l4_proto == IPPROTO_UDP) {
5335 command |= MVPP2_TXD_L4_UDP; /* enable UDP */
5336 command &= ~MVPP2_TXD_L4_CSUM_FRAG; /* generate L4 csum */
5337 } else {
5338 command |= MVPP2_TXD_L4_CSUM_NOT;
5339 }
5340
5341 return command;
5342}
5343
5344/* Get number of sent descriptors and decrement counter.
5345 * The number of sent descriptors is returned.
5346 * Per-CPU access
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005347 *
5348 * Called only from mvpp2_txq_done(), called from mvpp2_tx()
5349 * (migration disabled) and from the TX completion tasklet (migration
5350 * disabled) so using smp_processor_id() is OK.
Marcin Wojtas3f518502014-07-10 16:52:13 -03005351 */
5352static inline int mvpp2_txq_sent_desc_proc(struct mvpp2_port *port,
5353 struct mvpp2_tx_queue *txq)
5354{
5355 u32 val;
5356
5357 /* Reading status reg resets transmitted descriptor counter */
Thomas Petazzonia7868412017-03-07 16:53:13 +01005358 val = mvpp2_percpu_read(port->priv, smp_processor_id(),
5359 MVPP2_TXQ_SENT_REG(txq->id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005360
5361 return (val & MVPP2_TRANSMITTED_COUNT_MASK) >>
5362 MVPP2_TRANSMITTED_COUNT_OFFSET;
5363}
5364
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005365/* Called through on_each_cpu(), so runs on all CPUs, with migration
5366 * disabled, therefore using smp_processor_id() is OK.
5367 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03005368static void mvpp2_txq_sent_counter_clear(void *arg)
5369{
5370 struct mvpp2_port *port = arg;
5371 int queue;
5372
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005373 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005374 int id = port->txqs[queue]->id;
5375
Thomas Petazzonia7868412017-03-07 16:53:13 +01005376 mvpp2_percpu_read(port->priv, smp_processor_id(),
5377 MVPP2_TXQ_SENT_REG(id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005378 }
5379}
5380
5381/* Set max sizes for Tx queues */
5382static void mvpp2_txp_max_tx_size_set(struct mvpp2_port *port)
5383{
5384 u32 val, size, mtu;
5385 int txq, tx_port_num;
5386
5387 mtu = port->pkt_size * 8;
5388 if (mtu > MVPP2_TXP_MTU_MAX)
5389 mtu = MVPP2_TXP_MTU_MAX;
5390
5391 /* WA for wrong Token bucket update: Set MTU value = 3*real MTU value */
5392 mtu = 3 * mtu;
5393
5394 /* Indirect access to registers */
5395 tx_port_num = mvpp2_egress_port(port);
5396 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
5397
5398 /* Set MTU */
5399 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_MTU_REG);
5400 val &= ~MVPP2_TXP_MTU_MAX;
5401 val |= mtu;
5402 mvpp2_write(port->priv, MVPP2_TXP_SCHED_MTU_REG, val);
5403
5404 /* TXP token size and all TXQs token size must be larger that MTU */
5405 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG);
5406 size = val & MVPP2_TXP_TOKEN_SIZE_MAX;
5407 if (size < mtu) {
5408 size = mtu;
5409 val &= ~MVPP2_TXP_TOKEN_SIZE_MAX;
5410 val |= size;
5411 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
5412 }
5413
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005414 for (txq = 0; txq < port->ntxqs; txq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005415 val = mvpp2_read(port->priv,
5416 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq));
5417 size = val & MVPP2_TXQ_TOKEN_SIZE_MAX;
5418
5419 if (size < mtu) {
5420 size = mtu;
5421 val &= ~MVPP2_TXQ_TOKEN_SIZE_MAX;
5422 val |= size;
5423 mvpp2_write(port->priv,
5424 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq),
5425 val);
5426 }
5427 }
5428}
5429
5430/* Set the number of packets that will be received before Rx interrupt
5431 * will be generated by HW.
5432 */
5433static void mvpp2_rx_pkts_coal_set(struct mvpp2_port *port,
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01005434 struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005435{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005436 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005437
Thomas Petazzonif8b0d5f2017-02-21 11:28:03 +01005438 if (rxq->pkts_coal > MVPP2_OCCUPIED_THRESH_MASK)
5439 rxq->pkts_coal = MVPP2_OCCUPIED_THRESH_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005440
Thomas Petazzonia7868412017-03-07 16:53:13 +01005441 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
5442 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_THRESH_REG,
5443 rxq->pkts_coal);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005444
5445 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005446}
5447
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005448/* For some reason in the LSP this is done on each CPU. Why ? */
5449static void mvpp2_tx_pkts_coal_set(struct mvpp2_port *port,
5450 struct mvpp2_tx_queue *txq)
5451{
5452 int cpu = get_cpu();
5453 u32 val;
5454
5455 if (txq->done_pkts_coal > MVPP2_TXQ_THRESH_MASK)
5456 txq->done_pkts_coal = MVPP2_TXQ_THRESH_MASK;
5457
5458 val = (txq->done_pkts_coal << MVPP2_TXQ_THRESH_OFFSET);
5459 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5460 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_THRESH_REG, val);
5461
5462 put_cpu();
5463}
5464
Thomas Petazzoniab426762017-02-21 11:28:04 +01005465static u32 mvpp2_usec_to_cycles(u32 usec, unsigned long clk_hz)
5466{
5467 u64 tmp = (u64)clk_hz * usec;
5468
5469 do_div(tmp, USEC_PER_SEC);
5470
5471 return tmp > U32_MAX ? U32_MAX : tmp;
5472}
5473
5474static u32 mvpp2_cycles_to_usec(u32 cycles, unsigned long clk_hz)
5475{
5476 u64 tmp = (u64)cycles * USEC_PER_SEC;
5477
5478 do_div(tmp, clk_hz);
5479
5480 return tmp > U32_MAX ? U32_MAX : tmp;
5481}
5482
Marcin Wojtas3f518502014-07-10 16:52:13 -03005483/* Set the time delay in usec before Rx interrupt */
5484static void mvpp2_rx_time_coal_set(struct mvpp2_port *port,
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01005485 struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005486{
Thomas Petazzoniab426762017-02-21 11:28:04 +01005487 unsigned long freq = port->priv->tclk;
5488 u32 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005489
Thomas Petazzoniab426762017-02-21 11:28:04 +01005490 if (val > MVPP2_MAX_ISR_RX_THRESHOLD) {
5491 rxq->time_coal =
5492 mvpp2_cycles_to_usec(MVPP2_MAX_ISR_RX_THRESHOLD, freq);
5493
5494 /* re-evaluate to get actual register value */
5495 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
5496 }
5497
Marcin Wojtas3f518502014-07-10 16:52:13 -03005498 mvpp2_write(port->priv, MVPP2_ISR_RX_THRESHOLD_REG(rxq->id), val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005499}
5500
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005501static void mvpp2_tx_time_coal_set(struct mvpp2_port *port)
5502{
5503 unsigned long freq = port->priv->tclk;
5504 u32 val = mvpp2_usec_to_cycles(port->tx_time_coal, freq);
5505
5506 if (val > MVPP2_MAX_ISR_TX_THRESHOLD) {
5507 port->tx_time_coal =
5508 mvpp2_cycles_to_usec(MVPP2_MAX_ISR_TX_THRESHOLD, freq);
5509
5510 /* re-evaluate to get actual register value */
5511 val = mvpp2_usec_to_cycles(port->tx_time_coal, freq);
5512 }
5513
5514 mvpp2_write(port->priv, MVPP2_ISR_TX_THRESHOLD_REG(port->id), val);
5515}
5516
Marcin Wojtas3f518502014-07-10 16:52:13 -03005517/* Free Tx queue skbuffs */
5518static void mvpp2_txq_bufs_free(struct mvpp2_port *port,
5519 struct mvpp2_tx_queue *txq,
5520 struct mvpp2_txq_pcpu *txq_pcpu, int num)
5521{
5522 int i;
5523
5524 for (i = 0; i < num; i++) {
Thomas Petazzoni83544912016-12-21 11:28:49 +01005525 struct mvpp2_txq_pcpu_buf *tx_buf =
5526 txq_pcpu->buffs + txq_pcpu->txq_get_index;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005527
Antoine Tenart20920262017-10-23 15:24:30 +02005528 if (!IS_TSO_HEADER(txq_pcpu, tx_buf->dma))
5529 dma_unmap_single(port->dev->dev.parent, tx_buf->dma,
5530 tx_buf->size, DMA_TO_DEVICE);
Thomas Petazzoni36fb7432017-02-21 11:28:05 +01005531 if (tx_buf->skb)
5532 dev_kfree_skb_any(tx_buf->skb);
5533
5534 mvpp2_txq_inc_get(txq_pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005535 }
5536}
5537
5538static inline struct mvpp2_rx_queue *mvpp2_get_rx_queue(struct mvpp2_port *port,
5539 u32 cause)
5540{
5541 int queue = fls(cause) - 1;
5542
5543 return port->rxqs[queue];
5544}
5545
5546static inline struct mvpp2_tx_queue *mvpp2_get_tx_queue(struct mvpp2_port *port,
5547 u32 cause)
5548{
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005549 int queue = fls(cause) - 1;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005550
5551 return port->txqs[queue];
5552}
5553
5554/* Handle end of transmission */
5555static void mvpp2_txq_done(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
5556 struct mvpp2_txq_pcpu *txq_pcpu)
5557{
5558 struct netdev_queue *nq = netdev_get_tx_queue(port->dev, txq->log_id);
5559 int tx_done;
5560
5561 if (txq_pcpu->cpu != smp_processor_id())
5562 netdev_err(port->dev, "wrong cpu on the end of Tx processing\n");
5563
5564 tx_done = mvpp2_txq_sent_desc_proc(port, txq);
5565 if (!tx_done)
5566 return;
5567 mvpp2_txq_bufs_free(port, txq, txq_pcpu, tx_done);
5568
5569 txq_pcpu->count -= tx_done;
5570
5571 if (netif_tx_queue_stopped(nq))
Antoine Tenart1d17db02017-10-30 11:23:31 +01005572 if (txq_pcpu->count <= txq_pcpu->wake_threshold)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005573 netif_tx_wake_queue(nq);
5574}
5575
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005576static unsigned int mvpp2_tx_done(struct mvpp2_port *port, u32 cause,
5577 int cpu)
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005578{
5579 struct mvpp2_tx_queue *txq;
5580 struct mvpp2_txq_pcpu *txq_pcpu;
5581 unsigned int tx_todo = 0;
5582
5583 while (cause) {
5584 txq = mvpp2_get_tx_queue(port, cause);
5585 if (!txq)
5586 break;
5587
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005588 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005589
5590 if (txq_pcpu->count) {
5591 mvpp2_txq_done(port, txq, txq_pcpu);
5592 tx_todo += txq_pcpu->count;
5593 }
5594
5595 cause &= ~(1 << txq->log_id);
5596 }
5597 return tx_todo;
5598}
5599
Marcin Wojtas3f518502014-07-10 16:52:13 -03005600/* Rx/Tx queue initialization/cleanup methods */
5601
5602/* Allocate and initialize descriptors for aggr TXQ */
5603static int mvpp2_aggr_txq_init(struct platform_device *pdev,
Antoine Ténart85affd72017-08-23 09:46:55 +02005604 struct mvpp2_tx_queue *aggr_txq, int cpu,
Marcin Wojtas3f518502014-07-10 16:52:13 -03005605 struct mvpp2 *priv)
5606{
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005607 u32 txq_dma;
5608
Marcin Wojtas3f518502014-07-10 16:52:13 -03005609 /* Allocate memory for TX descriptors */
5610 aggr_txq->descs = dma_alloc_coherent(&pdev->dev,
Antoine Ténart85affd72017-08-23 09:46:55 +02005611 MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005612 &aggr_txq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005613 if (!aggr_txq->descs)
5614 return -ENOMEM;
5615
Antoine Tenart02856a32017-10-30 11:23:32 +01005616 aggr_txq->last_desc = MVPP2_AGGR_TXQ_SIZE - 1;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005617
5618 /* Aggr TXQ no reset WA */
5619 aggr_txq->next_desc_to_proc = mvpp2_read(priv,
5620 MVPP2_AGGR_TXQ_INDEX_REG(cpu));
5621
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005622 /* Set Tx descriptors queue starting address indirect
5623 * access
5624 */
5625 if (priv->hw_version == MVPP21)
5626 txq_dma = aggr_txq->descs_dma;
5627 else
5628 txq_dma = aggr_txq->descs_dma >>
5629 MVPP22_AGGR_TXQ_DESC_ADDR_OFFS;
5630
5631 mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu), txq_dma);
Antoine Ténart85affd72017-08-23 09:46:55 +02005632 mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu),
5633 MVPP2_AGGR_TXQ_SIZE);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005634
5635 return 0;
5636}
5637
5638/* Create a specified Rx queue */
5639static int mvpp2_rxq_init(struct mvpp2_port *port,
5640 struct mvpp2_rx_queue *rxq)
5641
5642{
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005643 u32 rxq_dma;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005644 int cpu;
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005645
Marcin Wojtas3f518502014-07-10 16:52:13 -03005646 rxq->size = port->rx_ring_size;
5647
5648 /* Allocate memory for RX descriptors */
5649 rxq->descs = dma_alloc_coherent(port->dev->dev.parent,
5650 rxq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005651 &rxq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005652 if (!rxq->descs)
5653 return -ENOMEM;
5654
Marcin Wojtas3f518502014-07-10 16:52:13 -03005655 rxq->last_desc = rxq->size - 1;
5656
5657 /* Zero occupied and non-occupied counters - direct access */
5658 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
5659
5660 /* Set Rx descriptors queue starting address - indirect access */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005661 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005662 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005663 if (port->priv->hw_version == MVPP21)
5664 rxq_dma = rxq->descs_dma;
5665 else
5666 rxq_dma = rxq->descs_dma >> MVPP22_DESC_ADDR_OFFS;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005667 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_ADDR_REG, rxq_dma);
5668 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_SIZE_REG, rxq->size);
5669 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_INDEX_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005670 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005671
5672 /* Set Offset */
5673 mvpp2_rxq_offset_set(port, rxq->id, NET_SKB_PAD);
5674
5675 /* Set coalescing pkts and time */
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01005676 mvpp2_rx_pkts_coal_set(port, rxq);
5677 mvpp2_rx_time_coal_set(port, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005678
5679 /* Add number of descriptors ready for receiving packets */
5680 mvpp2_rxq_status_update(port, rxq->id, 0, rxq->size);
5681
5682 return 0;
5683}
5684
5685/* Push packets received by the RXQ to BM pool */
5686static void mvpp2_rxq_drop_pkts(struct mvpp2_port *port,
5687 struct mvpp2_rx_queue *rxq)
5688{
5689 int rx_received, i;
5690
5691 rx_received = mvpp2_rxq_received(port, rxq->id);
5692 if (!rx_received)
5693 return;
5694
5695 for (i = 0; i < rx_received; i++) {
5696 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005697 u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
5698 int pool;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005699
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005700 pool = (status & MVPP2_RXD_BM_POOL_ID_MASK) >>
5701 MVPP2_RXD_BM_POOL_ID_OFFS;
5702
Thomas Petazzoni7d7627b2017-06-22 14:23:20 +02005703 mvpp2_bm_pool_put(port, pool,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005704 mvpp2_rxdesc_dma_addr_get(port, rx_desc),
5705 mvpp2_rxdesc_cookie_get(port, rx_desc));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005706 }
5707 mvpp2_rxq_status_update(port, rxq->id, rx_received, rx_received);
5708}
5709
5710/* Cleanup Rx queue */
5711static void mvpp2_rxq_deinit(struct mvpp2_port *port,
5712 struct mvpp2_rx_queue *rxq)
5713{
Thomas Petazzonia7868412017-03-07 16:53:13 +01005714 int cpu;
5715
Marcin Wojtas3f518502014-07-10 16:52:13 -03005716 mvpp2_rxq_drop_pkts(port, rxq);
5717
5718 if (rxq->descs)
5719 dma_free_coherent(port->dev->dev.parent,
5720 rxq->size * MVPP2_DESC_ALIGNED_SIZE,
5721 rxq->descs,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005722 rxq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005723
5724 rxq->descs = NULL;
5725 rxq->last_desc = 0;
5726 rxq->next_desc_to_proc = 0;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005727 rxq->descs_dma = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005728
5729 /* Clear Rx descriptors queue starting address and size;
5730 * free descriptor number
5731 */
5732 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005733 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005734 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
5735 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_ADDR_REG, 0);
5736 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_SIZE_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005737 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005738}
5739
5740/* Create and initialize a Tx queue */
5741static int mvpp2_txq_init(struct mvpp2_port *port,
5742 struct mvpp2_tx_queue *txq)
5743{
5744 u32 val;
5745 int cpu, desc, desc_per_txq, tx_port_num;
5746 struct mvpp2_txq_pcpu *txq_pcpu;
5747
5748 txq->size = port->tx_ring_size;
5749
5750 /* Allocate memory for Tx descriptors */
5751 txq->descs = dma_alloc_coherent(port->dev->dev.parent,
5752 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005753 &txq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005754 if (!txq->descs)
5755 return -ENOMEM;
5756
Marcin Wojtas3f518502014-07-10 16:52:13 -03005757 txq->last_desc = txq->size - 1;
5758
5759 /* Set Tx descriptors queue starting address - indirect access */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005760 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005761 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5762 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_ADDR_REG,
5763 txq->descs_dma);
5764 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_SIZE_REG,
5765 txq->size & MVPP2_TXQ_DESC_SIZE_MASK);
5766 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_INDEX_REG, 0);
5767 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_RSVD_CLR_REG,
5768 txq->id << MVPP2_TXQ_RSVD_CLR_OFFSET);
5769 val = mvpp2_percpu_read(port->priv, cpu, MVPP2_TXQ_PENDING_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005770 val &= ~MVPP2_TXQ_PENDING_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005771 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PENDING_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005772
5773 /* Calculate base address in prefetch buffer. We reserve 16 descriptors
5774 * for each existing TXQ.
5775 * TCONTS for PON port must be continuous from 0 to MVPP2_MAX_TCONT
5776 * GBE ports assumed to be continious from 0 to MVPP2_MAX_PORTS
5777 */
5778 desc_per_txq = 16;
5779 desc = (port->id * MVPP2_MAX_TXQ * desc_per_txq) +
5780 (txq->log_id * desc_per_txq);
5781
Thomas Petazzonia7868412017-03-07 16:53:13 +01005782 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG,
5783 MVPP2_PREF_BUF_PTR(desc) | MVPP2_PREF_BUF_SIZE_16 |
5784 MVPP2_PREF_BUF_THRESH(desc_per_txq / 2));
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005785 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005786
5787 /* WRR / EJP configuration - indirect access */
5788 tx_port_num = mvpp2_egress_port(port);
5789 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
5790
5791 val = mvpp2_read(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id));
5792 val &= ~MVPP2_TXQ_REFILL_PERIOD_ALL_MASK;
5793 val |= MVPP2_TXQ_REFILL_PERIOD_MASK(1);
5794 val |= MVPP2_TXQ_REFILL_TOKENS_ALL_MASK;
5795 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id), val);
5796
5797 val = MVPP2_TXQ_TOKEN_SIZE_MAX;
5798 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq->log_id),
5799 val);
5800
5801 for_each_present_cpu(cpu) {
5802 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
5803 txq_pcpu->size = txq->size;
Markus Elfring02c91ec2017-04-17 08:09:07 +02005804 txq_pcpu->buffs = kmalloc_array(txq_pcpu->size,
5805 sizeof(*txq_pcpu->buffs),
5806 GFP_KERNEL);
Thomas Petazzoni83544912016-12-21 11:28:49 +01005807 if (!txq_pcpu->buffs)
Markus Elfring20b1e162017-04-17 12:58:33 +02005808 goto cleanup;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005809
5810 txq_pcpu->count = 0;
5811 txq_pcpu->reserved_num = 0;
5812 txq_pcpu->txq_put_index = 0;
5813 txq_pcpu->txq_get_index = 0;
Antoine Ténart186cd4d2017-08-23 09:46:56 +02005814
Antoine Tenart1d17db02017-10-30 11:23:31 +01005815 txq_pcpu->stop_threshold = txq->size - MVPP2_MAX_SKB_DESCS;
5816 txq_pcpu->wake_threshold = txq_pcpu->stop_threshold / 2;
5817
Antoine Ténart186cd4d2017-08-23 09:46:56 +02005818 txq_pcpu->tso_headers =
5819 dma_alloc_coherent(port->dev->dev.parent,
Yan Markman822eaf72017-10-23 15:24:29 +02005820 txq_pcpu->size * TSO_HEADER_SIZE,
Antoine Ténart186cd4d2017-08-23 09:46:56 +02005821 &txq_pcpu->tso_headers_dma,
5822 GFP_KERNEL);
5823 if (!txq_pcpu->tso_headers)
5824 goto cleanup;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005825 }
5826
5827 return 0;
Markus Elfring20b1e162017-04-17 12:58:33 +02005828cleanup:
Marcin Wojtas71ce3912015-08-06 19:00:29 +02005829 for_each_present_cpu(cpu) {
5830 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Thomas Petazzoni83544912016-12-21 11:28:49 +01005831 kfree(txq_pcpu->buffs);
Antoine Ténart186cd4d2017-08-23 09:46:56 +02005832
5833 dma_free_coherent(port->dev->dev.parent,
Yan Markman822eaf72017-10-23 15:24:29 +02005834 txq_pcpu->size * TSO_HEADER_SIZE,
Antoine Ténart186cd4d2017-08-23 09:46:56 +02005835 txq_pcpu->tso_headers,
5836 txq_pcpu->tso_headers_dma);
Marcin Wojtas71ce3912015-08-06 19:00:29 +02005837 }
5838
5839 dma_free_coherent(port->dev->dev.parent,
5840 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005841 txq->descs, txq->descs_dma);
Marcin Wojtas71ce3912015-08-06 19:00:29 +02005842
5843 return -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005844}
5845
5846/* Free allocated TXQ resources */
5847static void mvpp2_txq_deinit(struct mvpp2_port *port,
5848 struct mvpp2_tx_queue *txq)
5849{
5850 struct mvpp2_txq_pcpu *txq_pcpu;
5851 int cpu;
5852
5853 for_each_present_cpu(cpu) {
5854 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Thomas Petazzoni83544912016-12-21 11:28:49 +01005855 kfree(txq_pcpu->buffs);
Antoine Ténart186cd4d2017-08-23 09:46:56 +02005856
5857 dma_free_coherent(port->dev->dev.parent,
Yan Markman822eaf72017-10-23 15:24:29 +02005858 txq_pcpu->size * TSO_HEADER_SIZE,
Antoine Ténart186cd4d2017-08-23 09:46:56 +02005859 txq_pcpu->tso_headers,
5860 txq_pcpu->tso_headers_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005861 }
5862
5863 if (txq->descs)
5864 dma_free_coherent(port->dev->dev.parent,
5865 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005866 txq->descs, txq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005867
5868 txq->descs = NULL;
5869 txq->last_desc = 0;
5870 txq->next_desc_to_proc = 0;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005871 txq->descs_dma = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005872
5873 /* Set minimum bandwidth for disabled TXQs */
5874 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(txq->id), 0);
5875
5876 /* Set Tx descriptors queue starting address and size */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005877 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005878 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5879 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_ADDR_REG, 0);
5880 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_SIZE_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005881 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005882}
5883
5884/* Cleanup Tx ports */
5885static void mvpp2_txq_clean(struct mvpp2_port *port, struct mvpp2_tx_queue *txq)
5886{
5887 struct mvpp2_txq_pcpu *txq_pcpu;
5888 int delay, pending, cpu;
5889 u32 val;
5890
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005891 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005892 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5893 val = mvpp2_percpu_read(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005894 val |= MVPP2_TXQ_DRAIN_EN_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005895 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005896
5897 /* The napi queue has been stopped so wait for all packets
5898 * to be transmitted.
5899 */
5900 delay = 0;
5901 do {
5902 if (delay >= MVPP2_TX_PENDING_TIMEOUT_MSEC) {
5903 netdev_warn(port->dev,
5904 "port %d: cleaning queue %d timed out\n",
5905 port->id, txq->log_id);
5906 break;
5907 }
5908 mdelay(1);
5909 delay++;
5910
Thomas Petazzonia7868412017-03-07 16:53:13 +01005911 pending = mvpp2_percpu_read(port->priv, cpu,
5912 MVPP2_TXQ_PENDING_REG);
5913 pending &= MVPP2_TXQ_PENDING_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005914 } while (pending);
5915
5916 val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005917 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG, val);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005918 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005919
5920 for_each_present_cpu(cpu) {
5921 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
5922
5923 /* Release all packets */
5924 mvpp2_txq_bufs_free(port, txq, txq_pcpu, txq_pcpu->count);
5925
5926 /* Reset queue */
5927 txq_pcpu->count = 0;
5928 txq_pcpu->txq_put_index = 0;
5929 txq_pcpu->txq_get_index = 0;
5930 }
5931}
5932
5933/* Cleanup all Tx queues */
5934static void mvpp2_cleanup_txqs(struct mvpp2_port *port)
5935{
5936 struct mvpp2_tx_queue *txq;
5937 int queue;
5938 u32 val;
5939
5940 val = mvpp2_read(port->priv, MVPP2_TX_PORT_FLUSH_REG);
5941
5942 /* Reset Tx ports and delete Tx queues */
5943 val |= MVPP2_TX_PORT_FLUSH_MASK(port->id);
5944 mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
5945
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005946 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005947 txq = port->txqs[queue];
5948 mvpp2_txq_clean(port, txq);
5949 mvpp2_txq_deinit(port, txq);
5950 }
5951
5952 on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
5953
5954 val &= ~MVPP2_TX_PORT_FLUSH_MASK(port->id);
5955 mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
5956}
5957
5958/* Cleanup all Rx queues */
5959static void mvpp2_cleanup_rxqs(struct mvpp2_port *port)
5960{
5961 int queue;
5962
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005963 for (queue = 0; queue < port->nrxqs; queue++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005964 mvpp2_rxq_deinit(port, port->rxqs[queue]);
5965}
5966
5967/* Init all Rx queues for port */
5968static int mvpp2_setup_rxqs(struct mvpp2_port *port)
5969{
5970 int queue, err;
5971
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005972 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005973 err = mvpp2_rxq_init(port, port->rxqs[queue]);
5974 if (err)
5975 goto err_cleanup;
5976 }
5977 return 0;
5978
5979err_cleanup:
5980 mvpp2_cleanup_rxqs(port);
5981 return err;
5982}
5983
5984/* Init all tx queues for port */
5985static int mvpp2_setup_txqs(struct mvpp2_port *port)
5986{
5987 struct mvpp2_tx_queue *txq;
5988 int queue, err;
5989
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005990 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005991 txq = port->txqs[queue];
5992 err = mvpp2_txq_init(port, txq);
5993 if (err)
5994 goto err_cleanup;
5995 }
5996
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005997 if (port->has_tx_irqs) {
5998 mvpp2_tx_time_coal_set(port);
5999 for (queue = 0; queue < port->ntxqs; queue++) {
6000 txq = port->txqs[queue];
6001 mvpp2_tx_pkts_coal_set(port, txq);
6002 }
6003 }
6004
Marcin Wojtas3f518502014-07-10 16:52:13 -03006005 on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
6006 return 0;
6007
6008err_cleanup:
6009 mvpp2_cleanup_txqs(port);
6010 return err;
6011}
6012
6013/* The callback for per-port interrupt */
6014static irqreturn_t mvpp2_isr(int irq, void *dev_id)
6015{
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006016 struct mvpp2_queue_vector *qv = dev_id;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006017
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006018 mvpp2_qvec_interrupt_disable(qv);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006019
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006020 napi_schedule(&qv->napi);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006021
6022 return IRQ_HANDLED;
6023}
6024
Antoine Tenartfd3651b2017-09-01 11:04:54 +02006025/* Per-port interrupt for link status changes */
6026static irqreturn_t mvpp2_link_status_isr(int irq, void *dev_id)
6027{
6028 struct mvpp2_port *port = (struct mvpp2_port *)dev_id;
6029 struct net_device *dev = port->dev;
6030 bool event = false, link = false;
6031 u32 val;
6032
6033 mvpp22_gop_mask_irq(port);
6034
6035 if (port->gop_id == 0 &&
6036 port->phy_interface == PHY_INTERFACE_MODE_10GKR) {
6037 val = readl(port->base + MVPP22_XLG_INT_STAT);
6038 if (val & MVPP22_XLG_INT_STAT_LINK) {
6039 event = true;
6040 val = readl(port->base + MVPP22_XLG_STATUS);
6041 if (val & MVPP22_XLG_STATUS_LINK_UP)
6042 link = true;
6043 }
6044 } else if (phy_interface_mode_is_rgmii(port->phy_interface) ||
6045 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
6046 val = readl(port->base + MVPP22_GMAC_INT_STAT);
6047 if (val & MVPP22_GMAC_INT_STAT_LINK) {
6048 event = true;
6049 val = readl(port->base + MVPP2_GMAC_STATUS0);
6050 if (val & MVPP2_GMAC_STATUS0_LINK_UP)
6051 link = true;
6052 }
6053 }
6054
6055 if (!netif_running(dev) || !event)
6056 goto handled;
6057
6058 if (link) {
6059 mvpp2_interrupts_enable(port);
6060
6061 mvpp2_egress_enable(port);
6062 mvpp2_ingress_enable(port);
6063 netif_carrier_on(dev);
6064 netif_tx_wake_all_queues(dev);
6065 } else {
6066 netif_tx_stop_all_queues(dev);
6067 netif_carrier_off(dev);
6068 mvpp2_ingress_disable(port);
6069 mvpp2_egress_disable(port);
6070
6071 mvpp2_interrupts_disable(port);
6072 }
6073
6074handled:
6075 mvpp22_gop_unmask_irq(port);
6076 return IRQ_HANDLED;
6077}
6078
Antoine Tenart65a2c092017-08-30 10:29:18 +02006079static void mvpp2_gmac_set_autoneg(struct mvpp2_port *port,
6080 struct phy_device *phydev)
6081{
6082 u32 val;
6083
6084 if (port->phy_interface != PHY_INTERFACE_MODE_RGMII &&
6085 port->phy_interface != PHY_INTERFACE_MODE_RGMII_ID &&
6086 port->phy_interface != PHY_INTERFACE_MODE_RGMII_RXID &&
6087 port->phy_interface != PHY_INTERFACE_MODE_RGMII_TXID &&
6088 port->phy_interface != PHY_INTERFACE_MODE_SGMII)
6089 return;
6090
6091 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
6092 val &= ~(MVPP2_GMAC_CONFIG_MII_SPEED |
6093 MVPP2_GMAC_CONFIG_GMII_SPEED |
6094 MVPP2_GMAC_CONFIG_FULL_DUPLEX |
6095 MVPP2_GMAC_AN_SPEED_EN |
6096 MVPP2_GMAC_AN_DUPLEX_EN);
6097
6098 if (phydev->duplex)
6099 val |= MVPP2_GMAC_CONFIG_FULL_DUPLEX;
6100
6101 if (phydev->speed == SPEED_1000)
6102 val |= MVPP2_GMAC_CONFIG_GMII_SPEED;
6103 else if (phydev->speed == SPEED_100)
6104 val |= MVPP2_GMAC_CONFIG_MII_SPEED;
6105
6106 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
Antoine Tenart65a2c092017-08-30 10:29:18 +02006107}
6108
Marcin Wojtas3f518502014-07-10 16:52:13 -03006109/* Adjust link */
6110static void mvpp2_link_event(struct net_device *dev)
6111{
6112 struct mvpp2_port *port = netdev_priv(dev);
Philippe Reynes8e072692016-06-28 00:08:11 +02006113 struct phy_device *phydev = dev->phydev;
Antoine Tenart89273bc2017-08-30 10:29:19 +02006114 bool link_reconfigured = false;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006115 u32 val;
6116
6117 if (phydev->link) {
Antoine Tenart89273bc2017-08-30 10:29:19 +02006118 if (port->phy_interface != phydev->interface && port->comphy) {
6119 /* disable current port for reconfiguration */
6120 mvpp2_interrupts_disable(port);
6121 netif_carrier_off(port->dev);
6122 mvpp2_port_disable(port);
6123 phy_power_off(port->comphy);
6124
6125 /* comphy reconfiguration */
6126 port->phy_interface = phydev->interface;
6127 mvpp22_comphy_init(port);
6128
6129 /* gop/mac reconfiguration */
6130 mvpp22_gop_init(port);
6131 mvpp2_port_mii_set(port);
6132
6133 link_reconfigured = true;
6134 }
6135
Marcin Wojtas3f518502014-07-10 16:52:13 -03006136 if ((port->speed != phydev->speed) ||
6137 (port->duplex != phydev->duplex)) {
Antoine Tenart65a2c092017-08-30 10:29:18 +02006138 mvpp2_gmac_set_autoneg(port, phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006139
6140 port->duplex = phydev->duplex;
6141 port->speed = phydev->speed;
6142 }
6143 }
6144
Antoine Tenart89273bc2017-08-30 10:29:19 +02006145 if (phydev->link != port->link || link_reconfigured) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006146 port->link = phydev->link;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006147
Marcin Wojtas3f518502014-07-10 16:52:13 -03006148 if (phydev->link) {
Antoine Tenart65a2c092017-08-30 10:29:18 +02006149 if (port->phy_interface == PHY_INTERFACE_MODE_RGMII ||
6150 port->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
6151 port->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID ||
6152 port->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID ||
6153 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
6154 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
6155 val |= (MVPP2_GMAC_FORCE_LINK_PASS |
6156 MVPP2_GMAC_FORCE_LINK_DOWN);
6157 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
6158 }
Antoine Tenartf55744a2017-08-30 10:29:17 +02006159
6160 mvpp2_interrupts_enable(port);
6161 mvpp2_port_enable(port);
6162
Marcin Wojtas3f518502014-07-10 16:52:13 -03006163 mvpp2_egress_enable(port);
6164 mvpp2_ingress_enable(port);
Antoine Tenartf55744a2017-08-30 10:29:17 +02006165 netif_carrier_on(dev);
6166 netif_tx_wake_all_queues(dev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006167 } else {
Antoine Tenart968b2112017-08-30 10:29:16 +02006168 port->duplex = -1;
6169 port->speed = 0;
6170
Antoine Tenartf55744a2017-08-30 10:29:17 +02006171 netif_tx_stop_all_queues(dev);
6172 netif_carrier_off(dev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006173 mvpp2_ingress_disable(port);
6174 mvpp2_egress_disable(port);
Antoine Tenartf55744a2017-08-30 10:29:17 +02006175
6176 mvpp2_port_disable(port);
6177 mvpp2_interrupts_disable(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006178 }
Antoine Tenart968b2112017-08-30 10:29:16 +02006179
Marcin Wojtas3f518502014-07-10 16:52:13 -03006180 phy_print_status(phydev);
6181 }
6182}
6183
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006184static void mvpp2_timer_set(struct mvpp2_port_pcpu *port_pcpu)
6185{
6186 ktime_t interval;
6187
6188 if (!port_pcpu->timer_scheduled) {
6189 port_pcpu->timer_scheduled = true;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01006190 interval = MVPP2_TXDONE_HRTIMER_PERIOD_NS;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006191 hrtimer_start(&port_pcpu->tx_done_timer, interval,
6192 HRTIMER_MODE_REL_PINNED);
6193 }
6194}
6195
6196static void mvpp2_tx_proc_cb(unsigned long data)
6197{
6198 struct net_device *dev = (struct net_device *)data;
6199 struct mvpp2_port *port = netdev_priv(dev);
6200 struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
6201 unsigned int tx_todo, cause;
6202
6203 if (!netif_running(dev))
6204 return;
6205 port_pcpu->timer_scheduled = false;
6206
6207 /* Process all the Tx queues */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006208 cause = (1 << port->ntxqs) - 1;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006209 tx_todo = mvpp2_tx_done(port, cause, smp_processor_id());
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006210
6211 /* Set the timer in case not all the packets were processed */
6212 if (tx_todo)
6213 mvpp2_timer_set(port_pcpu);
6214}
6215
6216static enum hrtimer_restart mvpp2_hr_timer_cb(struct hrtimer *timer)
6217{
6218 struct mvpp2_port_pcpu *port_pcpu = container_of(timer,
6219 struct mvpp2_port_pcpu,
6220 tx_done_timer);
6221
6222 tasklet_schedule(&port_pcpu->tx_done_tasklet);
6223
6224 return HRTIMER_NORESTART;
6225}
6226
Marcin Wojtas3f518502014-07-10 16:52:13 -03006227/* Main RX/TX processing routines */
6228
6229/* Display more error info */
6230static void mvpp2_rx_error(struct mvpp2_port *port,
6231 struct mvpp2_rx_desc *rx_desc)
6232{
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006233 u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
6234 size_t sz = mvpp2_rxdesc_size_get(port, rx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006235
6236 switch (status & MVPP2_RXD_ERR_CODE_MASK) {
6237 case MVPP2_RXD_ERR_CRC:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006238 netdev_err(port->dev, "bad rx status %08x (crc error), size=%zu\n",
6239 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006240 break;
6241 case MVPP2_RXD_ERR_OVERRUN:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006242 netdev_err(port->dev, "bad rx status %08x (overrun error), size=%zu\n",
6243 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006244 break;
6245 case MVPP2_RXD_ERR_RESOURCE:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006246 netdev_err(port->dev, "bad rx status %08x (resource error), size=%zu\n",
6247 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006248 break;
6249 }
6250}
6251
6252/* Handle RX checksum offload */
6253static void mvpp2_rx_csum(struct mvpp2_port *port, u32 status,
6254 struct sk_buff *skb)
6255{
6256 if (((status & MVPP2_RXD_L3_IP4) &&
6257 !(status & MVPP2_RXD_IP4_HEADER_ERR)) ||
6258 (status & MVPP2_RXD_L3_IP6))
6259 if (((status & MVPP2_RXD_L4_UDP) ||
6260 (status & MVPP2_RXD_L4_TCP)) &&
6261 (status & MVPP2_RXD_L4_CSUM_OK)) {
6262 skb->csum = 0;
6263 skb->ip_summed = CHECKSUM_UNNECESSARY;
6264 return;
6265 }
6266
6267 skb->ip_summed = CHECKSUM_NONE;
6268}
6269
6270/* Reuse skb if possible, or allocate a new skb and add it to BM pool */
6271static int mvpp2_rx_refill(struct mvpp2_port *port,
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006272 struct mvpp2_bm_pool *bm_pool, int pool)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006273{
Thomas Petazzoni20396132017-03-07 16:53:00 +01006274 dma_addr_t dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01006275 phys_addr_t phys_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006276 void *buf;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006277
Marcin Wojtas3f518502014-07-10 16:52:13 -03006278 /* No recycle or too many buffers are in use, so allocate a new skb */
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01006279 buf = mvpp2_buf_alloc(port, bm_pool, &dma_addr, &phys_addr,
6280 GFP_ATOMIC);
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006281 if (!buf)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006282 return -ENOMEM;
6283
Thomas Petazzoni7d7627b2017-06-22 14:23:20 +02006284 mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
Thomas Petazzoni7ef7e1d2017-02-21 11:28:07 +01006285
Marcin Wojtas3f518502014-07-10 16:52:13 -03006286 return 0;
6287}
6288
6289/* Handle tx checksum */
6290static u32 mvpp2_skb_tx_csum(struct mvpp2_port *port, struct sk_buff *skb)
6291{
6292 if (skb->ip_summed == CHECKSUM_PARTIAL) {
6293 int ip_hdr_len = 0;
6294 u8 l4_proto;
6295
6296 if (skb->protocol == htons(ETH_P_IP)) {
6297 struct iphdr *ip4h = ip_hdr(skb);
6298
6299 /* Calculate IPv4 checksum and L4 checksum */
6300 ip_hdr_len = ip4h->ihl;
6301 l4_proto = ip4h->protocol;
6302 } else if (skb->protocol == htons(ETH_P_IPV6)) {
6303 struct ipv6hdr *ip6h = ipv6_hdr(skb);
6304
6305 /* Read l4_protocol from one of IPv6 extra headers */
6306 if (skb_network_header_len(skb) > 0)
6307 ip_hdr_len = (skb_network_header_len(skb) >> 2);
6308 l4_proto = ip6h->nexthdr;
6309 } else {
6310 return MVPP2_TXD_L4_CSUM_NOT;
6311 }
6312
6313 return mvpp2_txq_desc_csum(skb_network_offset(skb),
6314 skb->protocol, ip_hdr_len, l4_proto);
6315 }
6316
6317 return MVPP2_TXD_L4_CSUM_NOT | MVPP2_TXD_IP_CSUM_DISABLE;
6318}
6319
Marcin Wojtas3f518502014-07-10 16:52:13 -03006320/* Main rx processing */
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006321static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi,
6322 int rx_todo, struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006323{
6324 struct net_device *dev = port->dev;
Marcin Wojtasb5015852015-12-03 15:20:51 +01006325 int rx_received;
6326 int rx_done = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006327 u32 rcvd_pkts = 0;
6328 u32 rcvd_bytes = 0;
6329
6330 /* Get number of received packets and clamp the to-do */
6331 rx_received = mvpp2_rxq_received(port, rxq->id);
6332 if (rx_todo > rx_received)
6333 rx_todo = rx_received;
6334
Marcin Wojtasb5015852015-12-03 15:20:51 +01006335 while (rx_done < rx_todo) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006336 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
6337 struct mvpp2_bm_pool *bm_pool;
6338 struct sk_buff *skb;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006339 unsigned int frag_size;
Thomas Petazzoni20396132017-03-07 16:53:00 +01006340 dma_addr_t dma_addr;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006341 phys_addr_t phys_addr;
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006342 u32 rx_status;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006343 int pool, rx_bytes, err;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006344 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006345
Marcin Wojtasb5015852015-12-03 15:20:51 +01006346 rx_done++;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006347 rx_status = mvpp2_rxdesc_status_get(port, rx_desc);
6348 rx_bytes = mvpp2_rxdesc_size_get(port, rx_desc);
6349 rx_bytes -= MVPP2_MH_SIZE;
6350 dma_addr = mvpp2_rxdesc_dma_addr_get(port, rx_desc);
6351 phys_addr = mvpp2_rxdesc_cookie_get(port, rx_desc);
6352 data = (void *)phys_to_virt(phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006353
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006354 pool = (rx_status & MVPP2_RXD_BM_POOL_ID_MASK) >>
6355 MVPP2_RXD_BM_POOL_ID_OFFS;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006356 bm_pool = &port->priv->bm_pools[pool];
Marcin Wojtas3f518502014-07-10 16:52:13 -03006357
6358 /* In case of an error, release the requested buffer pointer
6359 * to the Buffer Manager. This request process is controlled
6360 * by the hardware, and the information about the buffer is
6361 * comprised by the RX descriptor.
6362 */
6363 if (rx_status & MVPP2_RXD_ERR_SUMMARY) {
Markus Elfring8a524882017-04-17 10:52:02 +02006364err_drop_frame:
Marcin Wojtas3f518502014-07-10 16:52:13 -03006365 dev->stats.rx_errors++;
6366 mvpp2_rx_error(port, rx_desc);
Marcin Wojtasb5015852015-12-03 15:20:51 +01006367 /* Return the buffer to the pool */
Thomas Petazzoni7d7627b2017-06-22 14:23:20 +02006368 mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006369 continue;
6370 }
6371
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006372 if (bm_pool->frag_size > PAGE_SIZE)
6373 frag_size = 0;
6374 else
6375 frag_size = bm_pool->frag_size;
6376
6377 skb = build_skb(data, frag_size);
6378 if (!skb) {
6379 netdev_warn(port->dev, "skb build failed\n");
6380 goto err_drop_frame;
6381 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006382
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006383 err = mvpp2_rx_refill(port, bm_pool, pool);
Marcin Wojtasb5015852015-12-03 15:20:51 +01006384 if (err) {
6385 netdev_err(port->dev, "failed to refill BM pools\n");
6386 goto err_drop_frame;
6387 }
6388
Thomas Petazzoni20396132017-03-07 16:53:00 +01006389 dma_unmap_single(dev->dev.parent, dma_addr,
Marcin Wojtas4229d502015-12-03 15:20:50 +01006390 bm_pool->buf_size, DMA_FROM_DEVICE);
6391
Marcin Wojtas3f518502014-07-10 16:52:13 -03006392 rcvd_pkts++;
6393 rcvd_bytes += rx_bytes;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006394
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006395 skb_reserve(skb, MVPP2_MH_SIZE + NET_SKB_PAD);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006396 skb_put(skb, rx_bytes);
6397 skb->protocol = eth_type_trans(skb, dev);
6398 mvpp2_rx_csum(port, rx_status, skb);
6399
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006400 napi_gro_receive(napi, skb);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006401 }
6402
6403 if (rcvd_pkts) {
6404 struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
6405
6406 u64_stats_update_begin(&stats->syncp);
6407 stats->rx_packets += rcvd_pkts;
6408 stats->rx_bytes += rcvd_bytes;
6409 u64_stats_update_end(&stats->syncp);
6410 }
6411
6412 /* Update Rx queue management counters */
6413 wmb();
Marcin Wojtasb5015852015-12-03 15:20:51 +01006414 mvpp2_rxq_status_update(port, rxq->id, rx_done, rx_done);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006415
6416 return rx_todo;
6417}
6418
6419static inline void
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006420tx_desc_unmap_put(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006421 struct mvpp2_tx_desc *desc)
6422{
Antoine Tenart20920262017-10-23 15:24:30 +02006423 struct mvpp2_txq_pcpu *txq_pcpu = this_cpu_ptr(txq->pcpu);
6424
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006425 dma_addr_t buf_dma_addr =
6426 mvpp2_txdesc_dma_addr_get(port, desc);
6427 size_t buf_sz =
6428 mvpp2_txdesc_size_get(port, desc);
Antoine Tenart20920262017-10-23 15:24:30 +02006429 if (!IS_TSO_HEADER(txq_pcpu, buf_dma_addr))
6430 dma_unmap_single(port->dev->dev.parent, buf_dma_addr,
6431 buf_sz, DMA_TO_DEVICE);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006432 mvpp2_txq_desc_put(txq);
6433}
6434
6435/* Handle tx fragmentation processing */
6436static int mvpp2_tx_frag_process(struct mvpp2_port *port, struct sk_buff *skb,
6437 struct mvpp2_tx_queue *aggr_txq,
6438 struct mvpp2_tx_queue *txq)
6439{
6440 struct mvpp2_txq_pcpu *txq_pcpu = this_cpu_ptr(txq->pcpu);
6441 struct mvpp2_tx_desc *tx_desc;
6442 int i;
Thomas Petazzoni20396132017-03-07 16:53:00 +01006443 dma_addr_t buf_dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006444
6445 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
6446 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6447 void *addr = page_address(frag->page.p) + frag->page_offset;
6448
6449 tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006450 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6451 mvpp2_txdesc_size_set(port, tx_desc, frag->size);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006452
Thomas Petazzoni20396132017-03-07 16:53:00 +01006453 buf_dma_addr = dma_map_single(port->dev->dev.parent, addr,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006454 frag->size,
6455 DMA_TO_DEVICE);
Thomas Petazzoni20396132017-03-07 16:53:00 +01006456 if (dma_mapping_error(port->dev->dev.parent, buf_dma_addr)) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006457 mvpp2_txq_desc_put(txq);
Markus Elfring32bae632017-04-17 11:36:34 +02006458 goto cleanup;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006459 }
6460
Antoine Tenart6eb5d372017-10-30 11:23:33 +01006461 mvpp2_txdesc_dma_addr_set(port, tx_desc, buf_dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006462
6463 if (i == (skb_shinfo(skb)->nr_frags - 1)) {
6464 /* Last descriptor */
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006465 mvpp2_txdesc_cmd_set(port, tx_desc,
6466 MVPP2_TXD_L_DESC);
6467 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006468 } else {
6469 /* Descriptor in the middle: Not First, Not Last */
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006470 mvpp2_txdesc_cmd_set(port, tx_desc, 0);
6471 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006472 }
6473 }
6474
6475 return 0;
Markus Elfring32bae632017-04-17 11:36:34 +02006476cleanup:
Marcin Wojtas3f518502014-07-10 16:52:13 -03006477 /* Release all descriptors that were used to map fragments of
6478 * this packet, as well as the corresponding DMA mappings
6479 */
6480 for (i = i - 1; i >= 0; i--) {
6481 tx_desc = txq->descs + i;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006482 tx_desc_unmap_put(port, txq, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006483 }
6484
6485 return -ENOMEM;
6486}
6487
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006488static inline void mvpp2_tso_put_hdr(struct sk_buff *skb,
6489 struct net_device *dev,
6490 struct mvpp2_tx_queue *txq,
6491 struct mvpp2_tx_queue *aggr_txq,
6492 struct mvpp2_txq_pcpu *txq_pcpu,
6493 int hdr_sz)
6494{
6495 struct mvpp2_port *port = netdev_priv(dev);
6496 struct mvpp2_tx_desc *tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
6497 dma_addr_t addr;
6498
6499 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6500 mvpp2_txdesc_size_set(port, tx_desc, hdr_sz);
6501
6502 addr = txq_pcpu->tso_headers_dma +
6503 txq_pcpu->txq_put_index * TSO_HEADER_SIZE;
Antoine Tenart6eb5d372017-10-30 11:23:33 +01006504 mvpp2_txdesc_dma_addr_set(port, tx_desc, addr);
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006505
6506 mvpp2_txdesc_cmd_set(port, tx_desc, mvpp2_skb_tx_csum(port, skb) |
6507 MVPP2_TXD_F_DESC |
6508 MVPP2_TXD_PADDING_DISABLE);
6509 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
6510}
6511
6512static inline int mvpp2_tso_put_data(struct sk_buff *skb,
6513 struct net_device *dev, struct tso_t *tso,
6514 struct mvpp2_tx_queue *txq,
6515 struct mvpp2_tx_queue *aggr_txq,
6516 struct mvpp2_txq_pcpu *txq_pcpu,
6517 int sz, bool left, bool last)
6518{
6519 struct mvpp2_port *port = netdev_priv(dev);
6520 struct mvpp2_tx_desc *tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
6521 dma_addr_t buf_dma_addr;
6522
6523 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6524 mvpp2_txdesc_size_set(port, tx_desc, sz);
6525
6526 buf_dma_addr = dma_map_single(dev->dev.parent, tso->data, sz,
6527 DMA_TO_DEVICE);
6528 if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
6529 mvpp2_txq_desc_put(txq);
6530 return -ENOMEM;
6531 }
6532
Antoine Tenart6eb5d372017-10-30 11:23:33 +01006533 mvpp2_txdesc_dma_addr_set(port, tx_desc, buf_dma_addr);
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006534
6535 if (!left) {
6536 mvpp2_txdesc_cmd_set(port, tx_desc, MVPP2_TXD_L_DESC);
6537 if (last) {
6538 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
6539 return 0;
6540 }
6541 } else {
6542 mvpp2_txdesc_cmd_set(port, tx_desc, 0);
6543 }
6544
6545 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
6546 return 0;
6547}
6548
6549static int mvpp2_tx_tso(struct sk_buff *skb, struct net_device *dev,
6550 struct mvpp2_tx_queue *txq,
6551 struct mvpp2_tx_queue *aggr_txq,
6552 struct mvpp2_txq_pcpu *txq_pcpu)
6553{
6554 struct mvpp2_port *port = netdev_priv(dev);
6555 struct tso_t tso;
6556 int hdr_sz = skb_transport_offset(skb) + tcp_hdrlen(skb);
6557 int i, len, descs = 0;
6558
6559 /* Check number of available descriptors */
6560 if (mvpp2_aggr_desc_num_check(port->priv, aggr_txq,
6561 tso_count_descs(skb)) ||
6562 mvpp2_txq_reserved_desc_num_proc(port->priv, txq, txq_pcpu,
6563 tso_count_descs(skb)))
6564 return 0;
6565
6566 tso_start(skb, &tso);
6567 len = skb->len - hdr_sz;
6568 while (len > 0) {
6569 int left = min_t(int, skb_shinfo(skb)->gso_size, len);
6570 char *hdr = txq_pcpu->tso_headers +
6571 txq_pcpu->txq_put_index * TSO_HEADER_SIZE;
6572
6573 len -= left;
6574 descs++;
6575
6576 tso_build_hdr(skb, hdr, &tso, left, len == 0);
6577 mvpp2_tso_put_hdr(skb, dev, txq, aggr_txq, txq_pcpu, hdr_sz);
6578
6579 while (left > 0) {
6580 int sz = min_t(int, tso.size, left);
6581 left -= sz;
6582 descs++;
6583
6584 if (mvpp2_tso_put_data(skb, dev, &tso, txq, aggr_txq,
6585 txq_pcpu, sz, left, len == 0))
6586 goto release;
6587 tso_build_data(skb, &tso, sz);
6588 }
6589 }
6590
6591 return descs;
6592
6593release:
6594 for (i = descs - 1; i >= 0; i--) {
6595 struct mvpp2_tx_desc *tx_desc = txq->descs + i;
6596 tx_desc_unmap_put(port, txq, tx_desc);
6597 }
6598 return 0;
6599}
6600
Marcin Wojtas3f518502014-07-10 16:52:13 -03006601/* Main tx processing */
6602static int mvpp2_tx(struct sk_buff *skb, struct net_device *dev)
6603{
6604 struct mvpp2_port *port = netdev_priv(dev);
6605 struct mvpp2_tx_queue *txq, *aggr_txq;
6606 struct mvpp2_txq_pcpu *txq_pcpu;
6607 struct mvpp2_tx_desc *tx_desc;
Thomas Petazzoni20396132017-03-07 16:53:00 +01006608 dma_addr_t buf_dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006609 int frags = 0;
6610 u16 txq_id;
6611 u32 tx_cmd;
6612
6613 txq_id = skb_get_queue_mapping(skb);
6614 txq = port->txqs[txq_id];
6615 txq_pcpu = this_cpu_ptr(txq->pcpu);
6616 aggr_txq = &port->priv->aggr_txqs[smp_processor_id()];
6617
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006618 if (skb_is_gso(skb)) {
6619 frags = mvpp2_tx_tso(skb, dev, txq, aggr_txq, txq_pcpu);
6620 goto out;
6621 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006622 frags = skb_shinfo(skb)->nr_frags + 1;
6623
6624 /* Check number of available descriptors */
6625 if (mvpp2_aggr_desc_num_check(port->priv, aggr_txq, frags) ||
6626 mvpp2_txq_reserved_desc_num_proc(port->priv, txq,
6627 txq_pcpu, frags)) {
6628 frags = 0;
6629 goto out;
6630 }
6631
6632 /* Get a descriptor for the first part of the packet */
6633 tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006634 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6635 mvpp2_txdesc_size_set(port, tx_desc, skb_headlen(skb));
Marcin Wojtas3f518502014-07-10 16:52:13 -03006636
Thomas Petazzoni20396132017-03-07 16:53:00 +01006637 buf_dma_addr = dma_map_single(dev->dev.parent, skb->data,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006638 skb_headlen(skb), DMA_TO_DEVICE);
Thomas Petazzoni20396132017-03-07 16:53:00 +01006639 if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006640 mvpp2_txq_desc_put(txq);
6641 frags = 0;
6642 goto out;
6643 }
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006644
Antoine Tenart6eb5d372017-10-30 11:23:33 +01006645 mvpp2_txdesc_dma_addr_set(port, tx_desc, buf_dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006646
6647 tx_cmd = mvpp2_skb_tx_csum(port, skb);
6648
6649 if (frags == 1) {
6650 /* First and Last descriptor */
6651 tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_L_DESC;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006652 mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
6653 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006654 } else {
6655 /* First but not Last */
6656 tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_PADDING_DISABLE;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006657 mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
6658 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006659
6660 /* Continue with other skb fragments */
6661 if (mvpp2_tx_frag_process(port, skb, aggr_txq, txq)) {
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006662 tx_desc_unmap_put(port, txq, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006663 frags = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006664 }
6665 }
6666
Marcin Wojtas3f518502014-07-10 16:52:13 -03006667out:
6668 if (frags > 0) {
6669 struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006670 struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
6671
6672 txq_pcpu->reserved_num -= frags;
6673 txq_pcpu->count += frags;
6674 aggr_txq->count += frags;
6675
6676 /* Enable transmit */
6677 wmb();
6678 mvpp2_aggr_txq_pend_desc_add(port, frags);
6679
Antoine Tenart1d17db02017-10-30 11:23:31 +01006680 if (txq_pcpu->count >= txq_pcpu->stop_threshold)
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006681 netif_tx_stop_queue(nq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006682
6683 u64_stats_update_begin(&stats->syncp);
6684 stats->tx_packets++;
6685 stats->tx_bytes += skb->len;
6686 u64_stats_update_end(&stats->syncp);
6687 } else {
6688 dev->stats.tx_dropped++;
6689 dev_kfree_skb_any(skb);
6690 }
6691
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006692 /* Finalize TX processing */
Antoine Tenart082297e2017-10-23 15:24:31 +02006693 if (!port->has_tx_irqs && txq_pcpu->count >= txq->done_pkts_coal)
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006694 mvpp2_txq_done(port, txq, txq_pcpu);
6695
6696 /* Set the timer in case not all frags were processed */
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006697 if (!port->has_tx_irqs && txq_pcpu->count <= frags &&
6698 txq_pcpu->count > 0) {
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006699 struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
6700
6701 mvpp2_timer_set(port_pcpu);
6702 }
6703
Marcin Wojtas3f518502014-07-10 16:52:13 -03006704 return NETDEV_TX_OK;
6705}
6706
6707static inline void mvpp2_cause_error(struct net_device *dev, int cause)
6708{
6709 if (cause & MVPP2_CAUSE_FCS_ERR_MASK)
6710 netdev_err(dev, "FCS error\n");
6711 if (cause & MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK)
6712 netdev_err(dev, "rx fifo overrun error\n");
6713 if (cause & MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK)
6714 netdev_err(dev, "tx fifo underrun error\n");
6715}
6716
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006717static int mvpp2_poll(struct napi_struct *napi, int budget)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006718{
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006719 u32 cause_rx_tx, cause_rx, cause_tx, cause_misc;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006720 int rx_done = 0;
6721 struct mvpp2_port *port = netdev_priv(napi->dev);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006722 struct mvpp2_queue_vector *qv;
Thomas Petazzonia7868412017-03-07 16:53:13 +01006723 int cpu = smp_processor_id();
Marcin Wojtas3f518502014-07-10 16:52:13 -03006724
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006725 qv = container_of(napi, struct mvpp2_queue_vector, napi);
6726
Marcin Wojtas3f518502014-07-10 16:52:13 -03006727 /* Rx/Tx cause register
6728 *
6729 * Bits 0-15: each bit indicates received packets on the Rx queue
6730 * (bit 0 is for Rx queue 0).
6731 *
6732 * Bits 16-23: each bit indicates transmitted packets on the Tx queue
6733 * (bit 16 is for Tx queue 0).
6734 *
6735 * Each CPU has its own Rx/Tx cause register
6736 */
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006737 cause_rx_tx = mvpp2_percpu_read(port->priv, qv->sw_thread_id,
Thomas Petazzonia7868412017-03-07 16:53:13 +01006738 MVPP2_ISR_RX_TX_CAUSE_REG(port->id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03006739
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006740 cause_misc = cause_rx_tx & MVPP2_CAUSE_MISC_SUM_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006741 if (cause_misc) {
6742 mvpp2_cause_error(port->dev, cause_misc);
6743
6744 /* Clear the cause register */
6745 mvpp2_write(port->priv, MVPP2_ISR_MISC_CAUSE_REG, 0);
Thomas Petazzonia7868412017-03-07 16:53:13 +01006746 mvpp2_percpu_write(port->priv, cpu,
6747 MVPP2_ISR_RX_TX_CAUSE_REG(port->id),
6748 cause_rx_tx & ~MVPP2_CAUSE_MISC_SUM_MASK);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006749 }
6750
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006751 cause_tx = cause_rx_tx & MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
6752 if (cause_tx) {
6753 cause_tx >>= MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_OFFSET;
6754 mvpp2_tx_done(port, cause_tx, qv->sw_thread_id);
6755 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006756
6757 /* Process RX packets */
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006758 cause_rx = cause_rx_tx & MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
6759 cause_rx <<= qv->first_rxq;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006760 cause_rx |= qv->pending_cause_rx;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006761 while (cause_rx && budget > 0) {
6762 int count;
6763 struct mvpp2_rx_queue *rxq;
6764
6765 rxq = mvpp2_get_rx_queue(port, cause_rx);
6766 if (!rxq)
6767 break;
6768
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006769 count = mvpp2_rx(port, napi, budget, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006770 rx_done += count;
6771 budget -= count;
6772 if (budget > 0) {
6773 /* Clear the bit associated to this Rx queue
6774 * so that next iteration will continue from
6775 * the next Rx queue.
6776 */
6777 cause_rx &= ~(1 << rxq->logic_rxq);
6778 }
6779 }
6780
6781 if (budget > 0) {
6782 cause_rx = 0;
Eric Dumazet6ad20162017-01-30 08:22:01 -08006783 napi_complete_done(napi, rx_done);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006784
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006785 mvpp2_qvec_interrupt_enable(qv);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006786 }
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006787 qv->pending_cause_rx = cause_rx;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006788 return rx_done;
6789}
6790
6791/* Set hw internals when starting port */
6792static void mvpp2_start_dev(struct mvpp2_port *port)
6793{
Philippe Reynes8e072692016-06-28 00:08:11 +02006794 struct net_device *ndev = port->dev;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006795 int i;
Philippe Reynes8e072692016-06-28 00:08:11 +02006796
Stefan Chulski76eb1b12017-08-22 19:08:26 +02006797 if (port->gop_id == 0 &&
6798 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
6799 port->phy_interface == PHY_INTERFACE_MODE_10GKR))
6800 mvpp2_xlg_max_rx_size_set(port);
6801 else
6802 mvpp2_gmac_max_rx_size_set(port);
6803
Marcin Wojtas3f518502014-07-10 16:52:13 -03006804 mvpp2_txp_max_tx_size_set(port);
6805
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006806 for (i = 0; i < port->nqvecs; i++)
6807 napi_enable(&port->qvecs[i].napi);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006808
6809 /* Enable interrupts on all CPUs */
6810 mvpp2_interrupts_enable(port);
6811
Antoine Tenart542897d2017-08-30 10:29:15 +02006812 if (port->priv->hw_version == MVPP22) {
6813 mvpp22_comphy_init(port);
Antoine Ténartf84bf382017-08-22 19:08:27 +02006814 mvpp22_gop_init(port);
Antoine Tenart542897d2017-08-30 10:29:15 +02006815 }
Antoine Ténartf84bf382017-08-22 19:08:27 +02006816
Antoine Ténart2055d622017-08-22 19:08:23 +02006817 mvpp2_port_mii_set(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006818 mvpp2_port_enable(port);
Antoine Tenart5997c862017-09-01 11:04:53 +02006819 if (ndev->phydev)
6820 phy_start(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006821 netif_tx_start_all_queues(port->dev);
6822}
6823
6824/* Set hw internals when stopping port */
6825static void mvpp2_stop_dev(struct mvpp2_port *port)
6826{
Philippe Reynes8e072692016-06-28 00:08:11 +02006827 struct net_device *ndev = port->dev;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006828 int i;
Philippe Reynes8e072692016-06-28 00:08:11 +02006829
Marcin Wojtas3f518502014-07-10 16:52:13 -03006830 /* Stop new packets from arriving to RXQs */
6831 mvpp2_ingress_disable(port);
6832
6833 mdelay(10);
6834
6835 /* Disable interrupts on all CPUs */
6836 mvpp2_interrupts_disable(port);
6837
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006838 for (i = 0; i < port->nqvecs; i++)
6839 napi_disable(&port->qvecs[i].napi);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006840
6841 netif_carrier_off(port->dev);
6842 netif_tx_stop_all_queues(port->dev);
6843
6844 mvpp2_egress_disable(port);
6845 mvpp2_port_disable(port);
Antoine Tenart5997c862017-09-01 11:04:53 +02006846 if (ndev->phydev)
6847 phy_stop(ndev->phydev);
Antoine Tenart542897d2017-08-30 10:29:15 +02006848 phy_power_off(port->comphy);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006849}
6850
Marcin Wojtas3f518502014-07-10 16:52:13 -03006851static int mvpp2_check_ringparam_valid(struct net_device *dev,
6852 struct ethtool_ringparam *ring)
6853{
6854 u16 new_rx_pending = ring->rx_pending;
6855 u16 new_tx_pending = ring->tx_pending;
6856
6857 if (ring->rx_pending == 0 || ring->tx_pending == 0)
6858 return -EINVAL;
6859
6860 if (ring->rx_pending > MVPP2_MAX_RXD)
6861 new_rx_pending = MVPP2_MAX_RXD;
6862 else if (!IS_ALIGNED(ring->rx_pending, 16))
6863 new_rx_pending = ALIGN(ring->rx_pending, 16);
6864
6865 if (ring->tx_pending > MVPP2_MAX_TXD)
6866 new_tx_pending = MVPP2_MAX_TXD;
6867 else if (!IS_ALIGNED(ring->tx_pending, 32))
6868 new_tx_pending = ALIGN(ring->tx_pending, 32);
6869
6870 if (ring->rx_pending != new_rx_pending) {
6871 netdev_info(dev, "illegal Rx ring size value %d, round to %d\n",
6872 ring->rx_pending, new_rx_pending);
6873 ring->rx_pending = new_rx_pending;
6874 }
6875
6876 if (ring->tx_pending != new_tx_pending) {
6877 netdev_info(dev, "illegal Tx ring size value %d, round to %d\n",
6878 ring->tx_pending, new_tx_pending);
6879 ring->tx_pending = new_tx_pending;
6880 }
6881
6882 return 0;
6883}
6884
Thomas Petazzoni26975822017-03-07 16:53:14 +01006885static void mvpp21_get_mac_address(struct mvpp2_port *port, unsigned char *addr)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006886{
6887 u32 mac_addr_l, mac_addr_m, mac_addr_h;
6888
6889 mac_addr_l = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
6890 mac_addr_m = readl(port->priv->lms_base + MVPP2_SRC_ADDR_MIDDLE);
6891 mac_addr_h = readl(port->priv->lms_base + MVPP2_SRC_ADDR_HIGH);
6892 addr[0] = (mac_addr_h >> 24) & 0xFF;
6893 addr[1] = (mac_addr_h >> 16) & 0xFF;
6894 addr[2] = (mac_addr_h >> 8) & 0xFF;
6895 addr[3] = mac_addr_h & 0xFF;
6896 addr[4] = mac_addr_m & 0xFF;
6897 addr[5] = (mac_addr_l >> MVPP2_GMAC_SA_LOW_OFFS) & 0xFF;
6898}
6899
6900static int mvpp2_phy_connect(struct mvpp2_port *port)
6901{
6902 struct phy_device *phy_dev;
6903
Antoine Tenart5997c862017-09-01 11:04:53 +02006904 /* No PHY is attached */
6905 if (!port->phy_node)
6906 return 0;
6907
Marcin Wojtas3f518502014-07-10 16:52:13 -03006908 phy_dev = of_phy_connect(port->dev, port->phy_node, mvpp2_link_event, 0,
6909 port->phy_interface);
6910 if (!phy_dev) {
6911 netdev_err(port->dev, "cannot connect to phy\n");
6912 return -ENODEV;
6913 }
6914 phy_dev->supported &= PHY_GBIT_FEATURES;
6915 phy_dev->advertising = phy_dev->supported;
6916
Marcin Wojtas3f518502014-07-10 16:52:13 -03006917 port->link = 0;
6918 port->duplex = 0;
6919 port->speed = 0;
6920
6921 return 0;
6922}
6923
6924static void mvpp2_phy_disconnect(struct mvpp2_port *port)
6925{
Philippe Reynes8e072692016-06-28 00:08:11 +02006926 struct net_device *ndev = port->dev;
6927
Antoine Tenart5997c862017-09-01 11:04:53 +02006928 if (!ndev->phydev)
6929 return;
6930
Philippe Reynes8e072692016-06-28 00:08:11 +02006931 phy_disconnect(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006932}
6933
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006934static int mvpp2_irqs_init(struct mvpp2_port *port)
6935{
6936 int err, i;
6937
6938 for (i = 0; i < port->nqvecs; i++) {
6939 struct mvpp2_queue_vector *qv = port->qvecs + i;
6940
Marc Zyngier13c249a2017-11-04 12:33:47 +00006941 if (qv->type == MVPP2_QUEUE_VECTOR_PRIVATE)
6942 irq_set_status_flags(qv->irq, IRQ_NO_BALANCING);
6943
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006944 err = request_irq(qv->irq, mvpp2_isr, 0, port->dev->name, qv);
6945 if (err)
6946 goto err;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006947
6948 if (qv->type == MVPP2_QUEUE_VECTOR_PRIVATE)
6949 irq_set_affinity_hint(qv->irq,
6950 cpumask_of(qv->sw_thread_id));
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006951 }
6952
6953 return 0;
6954err:
6955 for (i = 0; i < port->nqvecs; i++) {
6956 struct mvpp2_queue_vector *qv = port->qvecs + i;
6957
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006958 irq_set_affinity_hint(qv->irq, NULL);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006959 free_irq(qv->irq, qv);
6960 }
6961
6962 return err;
6963}
6964
6965static void mvpp2_irqs_deinit(struct mvpp2_port *port)
6966{
6967 int i;
6968
6969 for (i = 0; i < port->nqvecs; i++) {
6970 struct mvpp2_queue_vector *qv = port->qvecs + i;
6971
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006972 irq_set_affinity_hint(qv->irq, NULL);
Marc Zyngier13c249a2017-11-04 12:33:47 +00006973 irq_clear_status_flags(qv->irq, IRQ_NO_BALANCING);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006974 free_irq(qv->irq, qv);
6975 }
6976}
6977
Antoine Tenart1d7d15d2017-10-30 11:23:30 +01006978static void mvpp22_init_rss(struct mvpp2_port *port)
6979{
6980 struct mvpp2 *priv = port->priv;
6981 int i;
6982
6983 /* Set the table width: replace the whole classifier Rx queue number
6984 * with the ones configured in RSS table entries.
6985 */
6986 mvpp2_write(priv, MVPP22_RSS_INDEX, MVPP22_RSS_INDEX_TABLE(0));
6987 mvpp2_write(priv, MVPP22_RSS_WIDTH, 8);
6988
6989 /* Loop through the classifier Rx Queues and map them to a RSS table.
6990 * Map them all to the first table (0) by default.
6991 */
6992 for (i = 0; i < MVPP2_CLS_RX_QUEUES; i++) {
6993 mvpp2_write(priv, MVPP22_RSS_INDEX, MVPP22_RSS_INDEX_QUEUE(i));
6994 mvpp2_write(priv, MVPP22_RSS_TABLE,
6995 MVPP22_RSS_TABLE_POINTER(0));
6996 }
6997
6998 /* Configure the first table to evenly distribute the packets across
6999 * real Rx Queues. The table entries map a hash to an port Rx Queue.
7000 */
7001 for (i = 0; i < MVPP22_RSS_TABLE_ENTRIES; i++) {
7002 u32 sel = MVPP22_RSS_INDEX_TABLE(0) |
7003 MVPP22_RSS_INDEX_TABLE_ENTRY(i);
7004 mvpp2_write(priv, MVPP22_RSS_INDEX, sel);
7005
7006 mvpp2_write(priv, MVPP22_RSS_TABLE_ENTRY, i % port->nrxqs);
7007 }
7008
7009}
7010
Marcin Wojtas3f518502014-07-10 16:52:13 -03007011static int mvpp2_open(struct net_device *dev)
7012{
7013 struct mvpp2_port *port = netdev_priv(dev);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007014 struct mvpp2 *priv = port->priv;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007015 unsigned char mac_bcast[ETH_ALEN] = {
7016 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7017 int err;
7018
7019 err = mvpp2_prs_mac_da_accept(port->priv, port->id, mac_bcast, true);
7020 if (err) {
7021 netdev_err(dev, "mvpp2_prs_mac_da_accept BC failed\n");
7022 return err;
7023 }
7024 err = mvpp2_prs_mac_da_accept(port->priv, port->id,
7025 dev->dev_addr, true);
7026 if (err) {
7027 netdev_err(dev, "mvpp2_prs_mac_da_accept MC failed\n");
7028 return err;
7029 }
7030 err = mvpp2_prs_tag_mode_set(port->priv, port->id, MVPP2_TAG_TYPE_MH);
7031 if (err) {
7032 netdev_err(dev, "mvpp2_prs_tag_mode_set failed\n");
7033 return err;
7034 }
7035 err = mvpp2_prs_def_flow(port);
7036 if (err) {
7037 netdev_err(dev, "mvpp2_prs_def_flow failed\n");
7038 return err;
7039 }
7040
7041 /* Allocate the Rx/Tx queues */
7042 err = mvpp2_setup_rxqs(port);
7043 if (err) {
7044 netdev_err(port->dev, "cannot allocate Rx queues\n");
7045 return err;
7046 }
7047
7048 err = mvpp2_setup_txqs(port);
7049 if (err) {
7050 netdev_err(port->dev, "cannot allocate Tx queues\n");
7051 goto err_cleanup_rxqs;
7052 }
7053
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007054 err = mvpp2_irqs_init(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007055 if (err) {
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007056 netdev_err(port->dev, "cannot init IRQs\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03007057 goto err_cleanup_txqs;
7058 }
7059
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007060 if (priv->hw_version == MVPP22 && !port->phy_node && port->link_irq) {
7061 err = request_irq(port->link_irq, mvpp2_link_status_isr, 0,
7062 dev->name, port);
7063 if (err) {
7064 netdev_err(port->dev, "cannot request link IRQ %d\n",
7065 port->link_irq);
7066 goto err_free_irq;
7067 }
7068
7069 mvpp22_gop_setup_irq(port);
7070 }
7071
Marcin Wojtas3f518502014-07-10 16:52:13 -03007072 /* In default link is down */
7073 netif_carrier_off(port->dev);
7074
7075 err = mvpp2_phy_connect(port);
7076 if (err < 0)
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007077 goto err_free_link_irq;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007078
7079 /* Unmask interrupts on all CPUs */
7080 on_each_cpu(mvpp2_interrupts_unmask, port, 1);
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007081 mvpp2_shared_interrupt_mask_unmask(port, false);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007082
7083 mvpp2_start_dev(port);
7084
Antoine Tenart1d7d15d2017-10-30 11:23:30 +01007085 if (priv->hw_version == MVPP22)
7086 mvpp22_init_rss(port);
7087
Miquel Raynal118d6292017-11-06 22:56:53 +01007088 /* Start hardware statistics gathering */
Miquel Raynale5c500e2017-11-08 08:59:40 +01007089 queue_delayed_work(priv->stats_queue, &port->stats_work,
Miquel Raynal118d6292017-11-06 22:56:53 +01007090 MVPP2_MIB_COUNTERS_STATS_DELAY);
7091
Marcin Wojtas3f518502014-07-10 16:52:13 -03007092 return 0;
7093
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007094err_free_link_irq:
7095 if (priv->hw_version == MVPP22 && !port->phy_node && port->link_irq)
7096 free_irq(port->link_irq, port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007097err_free_irq:
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007098 mvpp2_irqs_deinit(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007099err_cleanup_txqs:
7100 mvpp2_cleanup_txqs(port);
7101err_cleanup_rxqs:
7102 mvpp2_cleanup_rxqs(port);
7103 return err;
7104}
7105
7106static int mvpp2_stop(struct net_device *dev)
7107{
7108 struct mvpp2_port *port = netdev_priv(dev);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007109 struct mvpp2_port_pcpu *port_pcpu;
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007110 struct mvpp2 *priv = port->priv;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007111 int cpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007112
7113 mvpp2_stop_dev(port);
7114 mvpp2_phy_disconnect(port);
7115
7116 /* Mask interrupts on all CPUs */
7117 on_each_cpu(mvpp2_interrupts_mask, port, 1);
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007118 mvpp2_shared_interrupt_mask_unmask(port, true);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007119
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007120 if (priv->hw_version == MVPP22 && !port->phy_node && port->link_irq)
7121 free_irq(port->link_irq, port);
7122
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007123 mvpp2_irqs_deinit(port);
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007124 if (!port->has_tx_irqs) {
7125 for_each_present_cpu(cpu) {
7126 port_pcpu = per_cpu_ptr(port->pcpu, cpu);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007127
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007128 hrtimer_cancel(&port_pcpu->tx_done_timer);
7129 port_pcpu->timer_scheduled = false;
7130 tasklet_kill(&port_pcpu->tx_done_tasklet);
7131 }
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007132 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03007133 mvpp2_cleanup_rxqs(port);
7134 mvpp2_cleanup_txqs(port);
7135
Miquel Raynale5c500e2017-11-08 08:59:40 +01007136 cancel_delayed_work_sync(&port->stats_work);
Miquel Raynal118d6292017-11-06 22:56:53 +01007137
Marcin Wojtas3f518502014-07-10 16:52:13 -03007138 return 0;
7139}
7140
7141static void mvpp2_set_rx_mode(struct net_device *dev)
7142{
7143 struct mvpp2_port *port = netdev_priv(dev);
7144 struct mvpp2 *priv = port->priv;
7145 struct netdev_hw_addr *ha;
7146 int id = port->id;
7147 bool allmulti = dev->flags & IFF_ALLMULTI;
7148
7149 mvpp2_prs_mac_promisc_set(priv, id, dev->flags & IFF_PROMISC);
7150 mvpp2_prs_mac_multi_set(priv, id, MVPP2_PE_MAC_MC_ALL, allmulti);
7151 mvpp2_prs_mac_multi_set(priv, id, MVPP2_PE_MAC_MC_IP6, allmulti);
7152
7153 /* Remove all port->id's mcast enries */
7154 mvpp2_prs_mcast_del_all(priv, id);
7155
7156 if (allmulti && !netdev_mc_empty(dev)) {
7157 netdev_for_each_mc_addr(ha, dev)
7158 mvpp2_prs_mac_da_accept(priv, id, ha->addr, true);
7159 }
7160}
7161
7162static int mvpp2_set_mac_address(struct net_device *dev, void *p)
7163{
7164 struct mvpp2_port *port = netdev_priv(dev);
7165 const struct sockaddr *addr = p;
7166 int err;
7167
7168 if (!is_valid_ether_addr(addr->sa_data)) {
7169 err = -EADDRNOTAVAIL;
Markus Elfringc1175542017-04-17 11:10:47 +02007170 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007171 }
7172
7173 if (!netif_running(dev)) {
7174 err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
7175 if (!err)
7176 return 0;
7177 /* Reconfigure parser to accept the original MAC address */
7178 err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
7179 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02007180 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007181 }
7182
7183 mvpp2_stop_dev(port);
7184
7185 err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
7186 if (!err)
7187 goto out_start;
7188
7189 /* Reconfigure parser accept the original MAC address */
7190 err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
7191 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02007192 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007193out_start:
7194 mvpp2_start_dev(port);
7195 mvpp2_egress_enable(port);
7196 mvpp2_ingress_enable(port);
7197 return 0;
Markus Elfringc1175542017-04-17 11:10:47 +02007198log_error:
Markus Elfringdfd42402017-04-17 11:20:41 +02007199 netdev_err(dev, "failed to change MAC address\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03007200 return err;
7201}
7202
7203static int mvpp2_change_mtu(struct net_device *dev, int mtu)
7204{
7205 struct mvpp2_port *port = netdev_priv(dev);
7206 int err;
7207
Jarod Wilson57779872016-10-17 15:54:06 -04007208 if (!IS_ALIGNED(MVPP2_RX_PKT_SIZE(mtu), 8)) {
7209 netdev_info(dev, "illegal MTU value %d, round to %d\n", mtu,
7210 ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8));
7211 mtu = ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007212 }
7213
7214 if (!netif_running(dev)) {
7215 err = mvpp2_bm_update_mtu(dev, mtu);
7216 if (!err) {
7217 port->pkt_size = MVPP2_RX_PKT_SIZE(mtu);
7218 return 0;
7219 }
7220
7221 /* Reconfigure BM to the original MTU */
7222 err = mvpp2_bm_update_mtu(dev, dev->mtu);
7223 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02007224 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007225 }
7226
7227 mvpp2_stop_dev(port);
7228
7229 err = mvpp2_bm_update_mtu(dev, mtu);
7230 if (!err) {
7231 port->pkt_size = MVPP2_RX_PKT_SIZE(mtu);
7232 goto out_start;
7233 }
7234
7235 /* Reconfigure BM to the original MTU */
7236 err = mvpp2_bm_update_mtu(dev, dev->mtu);
7237 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02007238 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007239
7240out_start:
7241 mvpp2_start_dev(port);
7242 mvpp2_egress_enable(port);
7243 mvpp2_ingress_enable(port);
7244
7245 return 0;
Markus Elfringc1175542017-04-17 11:10:47 +02007246log_error:
Markus Elfringdfd42402017-04-17 11:20:41 +02007247 netdev_err(dev, "failed to change MTU\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03007248 return err;
7249}
7250
stephen hemmingerbc1f4472017-01-06 19:12:52 -08007251static void
Marcin Wojtas3f518502014-07-10 16:52:13 -03007252mvpp2_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
7253{
7254 struct mvpp2_port *port = netdev_priv(dev);
7255 unsigned int start;
7256 int cpu;
7257
7258 for_each_possible_cpu(cpu) {
7259 struct mvpp2_pcpu_stats *cpu_stats;
7260 u64 rx_packets;
7261 u64 rx_bytes;
7262 u64 tx_packets;
7263 u64 tx_bytes;
7264
7265 cpu_stats = per_cpu_ptr(port->stats, cpu);
7266 do {
7267 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
7268 rx_packets = cpu_stats->rx_packets;
7269 rx_bytes = cpu_stats->rx_bytes;
7270 tx_packets = cpu_stats->tx_packets;
7271 tx_bytes = cpu_stats->tx_bytes;
7272 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
7273
7274 stats->rx_packets += rx_packets;
7275 stats->rx_bytes += rx_bytes;
7276 stats->tx_packets += tx_packets;
7277 stats->tx_bytes += tx_bytes;
7278 }
7279
7280 stats->rx_errors = dev->stats.rx_errors;
7281 stats->rx_dropped = dev->stats.rx_dropped;
7282 stats->tx_dropped = dev->stats.tx_dropped;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007283}
7284
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007285static int mvpp2_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
7286{
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007287 int ret;
7288
Philippe Reynes8e072692016-06-28 00:08:11 +02007289 if (!dev->phydev)
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007290 return -ENOTSUPP;
7291
Philippe Reynes8e072692016-06-28 00:08:11 +02007292 ret = phy_mii_ioctl(dev->phydev, ifr, cmd);
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007293 if (!ret)
7294 mvpp2_link_event(dev);
7295
7296 return ret;
7297}
7298
Marcin Wojtas3f518502014-07-10 16:52:13 -03007299/* Ethtool methods */
7300
Marcin Wojtas3f518502014-07-10 16:52:13 -03007301/* Set interrupt coalescing for ethtools */
7302static int mvpp2_ethtool_set_coalesce(struct net_device *dev,
7303 struct ethtool_coalesce *c)
7304{
7305 struct mvpp2_port *port = netdev_priv(dev);
7306 int queue;
7307
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007308 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007309 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
7310
7311 rxq->time_coal = c->rx_coalesce_usecs;
7312 rxq->pkts_coal = c->rx_max_coalesced_frames;
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01007313 mvpp2_rx_pkts_coal_set(port, rxq);
7314 mvpp2_rx_time_coal_set(port, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007315 }
7316
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007317 if (port->has_tx_irqs) {
7318 port->tx_time_coal = c->tx_coalesce_usecs;
7319 mvpp2_tx_time_coal_set(port);
7320 }
7321
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007322 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007323 struct mvpp2_tx_queue *txq = port->txqs[queue];
7324
7325 txq->done_pkts_coal = c->tx_max_coalesced_frames;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007326
7327 if (port->has_tx_irqs)
7328 mvpp2_tx_pkts_coal_set(port, txq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007329 }
7330
Marcin Wojtas3f518502014-07-10 16:52:13 -03007331 return 0;
7332}
7333
7334/* get coalescing for ethtools */
7335static int mvpp2_ethtool_get_coalesce(struct net_device *dev,
7336 struct ethtool_coalesce *c)
7337{
7338 struct mvpp2_port *port = netdev_priv(dev);
7339
7340 c->rx_coalesce_usecs = port->rxqs[0]->time_coal;
7341 c->rx_max_coalesced_frames = port->rxqs[0]->pkts_coal;
7342 c->tx_max_coalesced_frames = port->txqs[0]->done_pkts_coal;
7343 return 0;
7344}
7345
7346static void mvpp2_ethtool_get_drvinfo(struct net_device *dev,
7347 struct ethtool_drvinfo *drvinfo)
7348{
7349 strlcpy(drvinfo->driver, MVPP2_DRIVER_NAME,
7350 sizeof(drvinfo->driver));
7351 strlcpy(drvinfo->version, MVPP2_DRIVER_VERSION,
7352 sizeof(drvinfo->version));
7353 strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
7354 sizeof(drvinfo->bus_info));
7355}
7356
7357static void mvpp2_ethtool_get_ringparam(struct net_device *dev,
7358 struct ethtool_ringparam *ring)
7359{
7360 struct mvpp2_port *port = netdev_priv(dev);
7361
7362 ring->rx_max_pending = MVPP2_MAX_RXD;
7363 ring->tx_max_pending = MVPP2_MAX_TXD;
7364 ring->rx_pending = port->rx_ring_size;
7365 ring->tx_pending = port->tx_ring_size;
7366}
7367
7368static int mvpp2_ethtool_set_ringparam(struct net_device *dev,
7369 struct ethtool_ringparam *ring)
7370{
7371 struct mvpp2_port *port = netdev_priv(dev);
7372 u16 prev_rx_ring_size = port->rx_ring_size;
7373 u16 prev_tx_ring_size = port->tx_ring_size;
7374 int err;
7375
7376 err = mvpp2_check_ringparam_valid(dev, ring);
7377 if (err)
7378 return err;
7379
7380 if (!netif_running(dev)) {
7381 port->rx_ring_size = ring->rx_pending;
7382 port->tx_ring_size = ring->tx_pending;
7383 return 0;
7384 }
7385
7386 /* The interface is running, so we have to force a
7387 * reallocation of the queues
7388 */
7389 mvpp2_stop_dev(port);
7390 mvpp2_cleanup_rxqs(port);
7391 mvpp2_cleanup_txqs(port);
7392
7393 port->rx_ring_size = ring->rx_pending;
7394 port->tx_ring_size = ring->tx_pending;
7395
7396 err = mvpp2_setup_rxqs(port);
7397 if (err) {
7398 /* Reallocate Rx queues with the original ring size */
7399 port->rx_ring_size = prev_rx_ring_size;
7400 ring->rx_pending = prev_rx_ring_size;
7401 err = mvpp2_setup_rxqs(port);
7402 if (err)
7403 goto err_out;
7404 }
7405 err = mvpp2_setup_txqs(port);
7406 if (err) {
7407 /* Reallocate Tx queues with the original ring size */
7408 port->tx_ring_size = prev_tx_ring_size;
7409 ring->tx_pending = prev_tx_ring_size;
7410 err = mvpp2_setup_txqs(port);
7411 if (err)
7412 goto err_clean_rxqs;
7413 }
7414
7415 mvpp2_start_dev(port);
7416 mvpp2_egress_enable(port);
7417 mvpp2_ingress_enable(port);
7418
7419 return 0;
7420
7421err_clean_rxqs:
7422 mvpp2_cleanup_rxqs(port);
7423err_out:
Markus Elfringdfd42402017-04-17 11:20:41 +02007424 netdev_err(dev, "failed to change ring parameters");
Marcin Wojtas3f518502014-07-10 16:52:13 -03007425 return err;
7426}
7427
7428/* Device ops */
7429
7430static const struct net_device_ops mvpp2_netdev_ops = {
7431 .ndo_open = mvpp2_open,
7432 .ndo_stop = mvpp2_stop,
7433 .ndo_start_xmit = mvpp2_tx,
7434 .ndo_set_rx_mode = mvpp2_set_rx_mode,
7435 .ndo_set_mac_address = mvpp2_set_mac_address,
7436 .ndo_change_mtu = mvpp2_change_mtu,
7437 .ndo_get_stats64 = mvpp2_get_stats64,
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007438 .ndo_do_ioctl = mvpp2_ioctl,
Marcin Wojtas3f518502014-07-10 16:52:13 -03007439};
7440
7441static const struct ethtool_ops mvpp2_eth_tool_ops = {
Florian Fainelli00606c42016-11-15 11:19:48 -08007442 .nway_reset = phy_ethtool_nway_reset,
Marcin Wojtas3f518502014-07-10 16:52:13 -03007443 .get_link = ethtool_op_get_link,
Marcin Wojtas3f518502014-07-10 16:52:13 -03007444 .set_coalesce = mvpp2_ethtool_set_coalesce,
7445 .get_coalesce = mvpp2_ethtool_get_coalesce,
7446 .get_drvinfo = mvpp2_ethtool_get_drvinfo,
7447 .get_ringparam = mvpp2_ethtool_get_ringparam,
7448 .set_ringparam = mvpp2_ethtool_set_ringparam,
Miquel Raynal118d6292017-11-06 22:56:53 +01007449 .get_strings = mvpp2_ethtool_get_strings,
7450 .get_ethtool_stats = mvpp2_ethtool_get_stats,
7451 .get_sset_count = mvpp2_ethtool_get_sset_count,
Philippe Reynesfb773e92016-06-28 00:08:12 +02007452 .get_link_ksettings = phy_ethtool_get_link_ksettings,
7453 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Marcin Wojtas3f518502014-07-10 16:52:13 -03007454};
7455
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007456/* Used for PPv2.1, or PPv2.2 with the old Device Tree binding that
7457 * had a single IRQ defined per-port.
7458 */
7459static int mvpp2_simple_queue_vectors_init(struct mvpp2_port *port,
7460 struct device_node *port_node)
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007461{
7462 struct mvpp2_queue_vector *v = &port->qvecs[0];
7463
7464 v->first_rxq = 0;
7465 v->nrxqs = port->nrxqs;
7466 v->type = MVPP2_QUEUE_VECTOR_SHARED;
7467 v->sw_thread_id = 0;
7468 v->sw_thread_mask = *cpumask_bits(cpu_online_mask);
7469 v->port = port;
7470 v->irq = irq_of_parse_and_map(port_node, 0);
7471 if (v->irq <= 0)
7472 return -EINVAL;
7473 netif_napi_add(port->dev, &v->napi, mvpp2_poll,
7474 NAPI_POLL_WEIGHT);
7475
7476 port->nqvecs = 1;
7477
7478 return 0;
7479}
7480
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007481static int mvpp2_multi_queue_vectors_init(struct mvpp2_port *port,
7482 struct device_node *port_node)
7483{
7484 struct mvpp2_queue_vector *v;
7485 int i, ret;
7486
7487 port->nqvecs = num_possible_cpus();
7488 if (queue_mode == MVPP2_QDIST_SINGLE_MODE)
7489 port->nqvecs += 1;
7490
7491 for (i = 0; i < port->nqvecs; i++) {
7492 char irqname[16];
7493
7494 v = port->qvecs + i;
7495
7496 v->port = port;
7497 v->type = MVPP2_QUEUE_VECTOR_PRIVATE;
7498 v->sw_thread_id = i;
7499 v->sw_thread_mask = BIT(i);
7500
7501 snprintf(irqname, sizeof(irqname), "tx-cpu%d", i);
7502
7503 if (queue_mode == MVPP2_QDIST_MULTI_MODE) {
7504 v->first_rxq = i * MVPP2_DEFAULT_RXQ;
7505 v->nrxqs = MVPP2_DEFAULT_RXQ;
7506 } else if (queue_mode == MVPP2_QDIST_SINGLE_MODE &&
7507 i == (port->nqvecs - 1)) {
7508 v->first_rxq = 0;
7509 v->nrxqs = port->nrxqs;
7510 v->type = MVPP2_QUEUE_VECTOR_SHARED;
7511 strncpy(irqname, "rx-shared", sizeof(irqname));
7512 }
7513
7514 v->irq = of_irq_get_byname(port_node, irqname);
7515 if (v->irq <= 0) {
7516 ret = -EINVAL;
7517 goto err;
7518 }
7519
7520 netif_napi_add(port->dev, &v->napi, mvpp2_poll,
7521 NAPI_POLL_WEIGHT);
7522 }
7523
7524 return 0;
7525
7526err:
7527 for (i = 0; i < port->nqvecs; i++)
7528 irq_dispose_mapping(port->qvecs[i].irq);
7529 return ret;
7530}
7531
7532static int mvpp2_queue_vectors_init(struct mvpp2_port *port,
7533 struct device_node *port_node)
7534{
7535 if (port->has_tx_irqs)
7536 return mvpp2_multi_queue_vectors_init(port, port_node);
7537 else
7538 return mvpp2_simple_queue_vectors_init(port, port_node);
7539}
7540
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007541static void mvpp2_queue_vectors_deinit(struct mvpp2_port *port)
7542{
7543 int i;
7544
7545 for (i = 0; i < port->nqvecs; i++)
7546 irq_dispose_mapping(port->qvecs[i].irq);
7547}
7548
7549/* Configure Rx queue group interrupt for this port */
7550static void mvpp2_rx_irqs_setup(struct mvpp2_port *port)
7551{
7552 struct mvpp2 *priv = port->priv;
7553 u32 val;
7554 int i;
7555
7556 if (priv->hw_version == MVPP21) {
7557 mvpp2_write(priv, MVPP21_ISR_RXQ_GROUP_REG(port->id),
7558 port->nrxqs);
7559 return;
7560 }
7561
7562 /* Handle the more complicated PPv2.2 case */
7563 for (i = 0; i < port->nqvecs; i++) {
7564 struct mvpp2_queue_vector *qv = port->qvecs + i;
7565
7566 if (!qv->nrxqs)
7567 continue;
7568
7569 val = qv->sw_thread_id;
7570 val |= port->id << MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_OFFSET;
7571 mvpp2_write(priv, MVPP22_ISR_RXQ_GROUP_INDEX_REG, val);
7572
7573 val = qv->first_rxq;
7574 val |= qv->nrxqs << MVPP22_ISR_RXQ_SUB_GROUP_SIZE_OFFSET;
7575 mvpp2_write(priv, MVPP22_ISR_RXQ_SUB_GROUP_CONFIG_REG, val);
7576 }
7577}
7578
Marcin Wojtas3f518502014-07-10 16:52:13 -03007579/* Initialize port HW */
7580static int mvpp2_port_init(struct mvpp2_port *port)
7581{
7582 struct device *dev = port->dev->dev.parent;
7583 struct mvpp2 *priv = port->priv;
7584 struct mvpp2_txq_pcpu *txq_pcpu;
7585 int queue, cpu, err;
7586
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007587 /* Checks for hardware constraints */
7588 if (port->first_rxq + port->nrxqs >
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01007589 MVPP2_MAX_PORTS * priv->max_port_rxqs)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007590 return -EINVAL;
7591
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007592 if (port->nrxqs % 4 || (port->nrxqs > priv->max_port_rxqs) ||
7593 (port->ntxqs > MVPP2_MAX_TXQ))
7594 return -EINVAL;
7595
Marcin Wojtas3f518502014-07-10 16:52:13 -03007596 /* Disable port */
7597 mvpp2_egress_disable(port);
7598 mvpp2_port_disable(port);
7599
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007600 port->tx_time_coal = MVPP2_TXDONE_COAL_USEC;
7601
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007602 port->txqs = devm_kcalloc(dev, port->ntxqs, sizeof(*port->txqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03007603 GFP_KERNEL);
7604 if (!port->txqs)
7605 return -ENOMEM;
7606
7607 /* Associate physical Tx queues to this port and initialize.
7608 * The mapping is predefined.
7609 */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007610 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007611 int queue_phy_id = mvpp2_txq_phys(port->id, queue);
7612 struct mvpp2_tx_queue *txq;
7613
7614 txq = devm_kzalloc(dev, sizeof(*txq), GFP_KERNEL);
Christophe Jaillet177c8d12017-02-19 10:19:57 +01007615 if (!txq) {
7616 err = -ENOMEM;
7617 goto err_free_percpu;
7618 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03007619
7620 txq->pcpu = alloc_percpu(struct mvpp2_txq_pcpu);
7621 if (!txq->pcpu) {
7622 err = -ENOMEM;
7623 goto err_free_percpu;
7624 }
7625
7626 txq->id = queue_phy_id;
7627 txq->log_id = queue;
7628 txq->done_pkts_coal = MVPP2_TXDONE_COAL_PKTS_THRESH;
7629 for_each_present_cpu(cpu) {
7630 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
7631 txq_pcpu->cpu = cpu;
7632 }
7633
7634 port->txqs[queue] = txq;
7635 }
7636
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007637 port->rxqs = devm_kcalloc(dev, port->nrxqs, sizeof(*port->rxqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03007638 GFP_KERNEL);
7639 if (!port->rxqs) {
7640 err = -ENOMEM;
7641 goto err_free_percpu;
7642 }
7643
7644 /* Allocate and initialize Rx queue for this port */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007645 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007646 struct mvpp2_rx_queue *rxq;
7647
7648 /* Map physical Rx queue to port's logical Rx queue */
7649 rxq = devm_kzalloc(dev, sizeof(*rxq), GFP_KERNEL);
Jisheng Zhangd82b0c22016-03-31 17:01:23 +08007650 if (!rxq) {
7651 err = -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007652 goto err_free_percpu;
Jisheng Zhangd82b0c22016-03-31 17:01:23 +08007653 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03007654 /* Map this Rx queue to a physical queue */
7655 rxq->id = port->first_rxq + queue;
7656 rxq->port = port->id;
7657 rxq->logic_rxq = queue;
7658
7659 port->rxqs[queue] = rxq;
7660 }
7661
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007662 mvpp2_rx_irqs_setup(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007663
7664 /* Create Rx descriptor rings */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007665 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007666 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
7667
7668 rxq->size = port->rx_ring_size;
7669 rxq->pkts_coal = MVPP2_RX_COAL_PKTS;
7670 rxq->time_coal = MVPP2_RX_COAL_USEC;
7671 }
7672
7673 mvpp2_ingress_disable(port);
7674
7675 /* Port default configuration */
7676 mvpp2_defaults_set(port);
7677
7678 /* Port's classifier configuration */
7679 mvpp2_cls_oversize_rxq_set(port);
7680 mvpp2_cls_port_config(port);
7681
7682 /* Provide an initial Rx packet size */
7683 port->pkt_size = MVPP2_RX_PKT_SIZE(port->dev->mtu);
7684
7685 /* Initialize pools for swf */
7686 err = mvpp2_swf_bm_pool_init(port);
7687 if (err)
7688 goto err_free_percpu;
7689
7690 return 0;
7691
7692err_free_percpu:
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007693 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007694 if (!port->txqs[queue])
7695 continue;
7696 free_percpu(port->txqs[queue]->pcpu);
7697 }
7698 return err;
7699}
7700
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007701/* Checks if the port DT description has the TX interrupts
7702 * described. On PPv2.1, there are no such interrupts. On PPv2.2,
7703 * there are available, but we need to keep support for old DTs.
7704 */
7705static bool mvpp2_port_has_tx_irqs(struct mvpp2 *priv,
7706 struct device_node *port_node)
7707{
7708 char *irqs[5] = { "rx-shared", "tx-cpu0", "tx-cpu1",
7709 "tx-cpu2", "tx-cpu3" };
7710 int ret, i;
7711
7712 if (priv->hw_version == MVPP21)
7713 return false;
7714
7715 for (i = 0; i < 5; i++) {
7716 ret = of_property_match_string(port_node, "interrupt-names",
7717 irqs[i]);
7718 if (ret < 0)
7719 return false;
7720 }
7721
7722 return true;
7723}
7724
Antoine Tenart3ba8c812017-09-02 11:06:47 +02007725static void mvpp2_port_copy_mac_addr(struct net_device *dev, struct mvpp2 *priv,
7726 struct device_node *port_node,
7727 char **mac_from)
7728{
7729 struct mvpp2_port *port = netdev_priv(dev);
7730 char hw_mac_addr[ETH_ALEN] = {0};
7731 const char *dt_mac_addr;
7732
7733 dt_mac_addr = of_get_mac_address(port_node);
7734 if (dt_mac_addr && is_valid_ether_addr(dt_mac_addr)) {
7735 *mac_from = "device tree";
7736 ether_addr_copy(dev->dev_addr, dt_mac_addr);
Antoine Tenart688cbaf2017-09-02 11:06:49 +02007737 return;
Antoine Tenart3ba8c812017-09-02 11:06:47 +02007738 }
Antoine Tenart688cbaf2017-09-02 11:06:49 +02007739
7740 if (priv->hw_version == MVPP21) {
7741 mvpp21_get_mac_address(port, hw_mac_addr);
7742 if (is_valid_ether_addr(hw_mac_addr)) {
7743 *mac_from = "hardware";
7744 ether_addr_copy(dev->dev_addr, hw_mac_addr);
7745 return;
7746 }
7747 }
7748
7749 *mac_from = "random";
7750 eth_hw_addr_random(dev);
Antoine Tenart3ba8c812017-09-02 11:06:47 +02007751}
7752
Marcin Wojtas3f518502014-07-10 16:52:13 -03007753/* Ports initialization */
7754static int mvpp2_port_probe(struct platform_device *pdev,
7755 struct device_node *port_node,
Yan Markman6bf69a12017-09-25 14:59:47 +02007756 struct mvpp2 *priv, int index)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007757{
7758 struct device_node *phy_node;
Antoine Tenart542897d2017-08-30 10:29:15 +02007759 struct phy *comphy;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007760 struct mvpp2_port *port;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007761 struct mvpp2_port_pcpu *port_pcpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007762 struct net_device *dev;
7763 struct resource *res;
Antoine Tenart3ba8c812017-09-02 11:06:47 +02007764 char *mac_from = "";
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007765 unsigned int ntxqs, nrxqs;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007766 bool has_tx_irqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007767 u32 id;
7768 int features;
7769 int phy_mode;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007770 int err, i, cpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007771
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007772 has_tx_irqs = mvpp2_port_has_tx_irqs(priv, port_node);
7773
7774 if (!has_tx_irqs)
7775 queue_mode = MVPP2_QDIST_SINGLE_MODE;
7776
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007777 ntxqs = MVPP2_MAX_TXQ;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007778 if (priv->hw_version == MVPP22 && queue_mode == MVPP2_QDIST_MULTI_MODE)
7779 nrxqs = MVPP2_DEFAULT_RXQ * num_possible_cpus();
7780 else
7781 nrxqs = MVPP2_DEFAULT_RXQ;
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007782
7783 dev = alloc_etherdev_mqs(sizeof(*port), ntxqs, nrxqs);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007784 if (!dev)
7785 return -ENOMEM;
7786
7787 phy_node = of_parse_phandle(port_node, "phy", 0);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007788 phy_mode = of_get_phy_mode(port_node);
7789 if (phy_mode < 0) {
7790 dev_err(&pdev->dev, "incorrect phy mode\n");
7791 err = phy_mode;
7792 goto err_free_netdev;
7793 }
7794
Antoine Tenart542897d2017-08-30 10:29:15 +02007795 comphy = devm_of_phy_get(&pdev->dev, port_node, NULL);
7796 if (IS_ERR(comphy)) {
7797 if (PTR_ERR(comphy) == -EPROBE_DEFER) {
7798 err = -EPROBE_DEFER;
7799 goto err_free_netdev;
7800 }
7801 comphy = NULL;
7802 }
7803
Marcin Wojtas3f518502014-07-10 16:52:13 -03007804 if (of_property_read_u32(port_node, "port-id", &id)) {
7805 err = -EINVAL;
7806 dev_err(&pdev->dev, "missing port-id value\n");
7807 goto err_free_netdev;
7808 }
7809
7810 dev->tx_queue_len = MVPP2_MAX_TXD;
7811 dev->watchdog_timeo = 5 * HZ;
7812 dev->netdev_ops = &mvpp2_netdev_ops;
7813 dev->ethtool_ops = &mvpp2_eth_tool_ops;
7814
7815 port = netdev_priv(dev);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007816 port->dev = dev;
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007817 port->ntxqs = ntxqs;
7818 port->nrxqs = nrxqs;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007819 port->priv = priv;
7820 port->has_tx_irqs = has_tx_irqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007821
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007822 err = mvpp2_queue_vectors_init(port, port_node);
7823 if (err)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007824 goto err_free_netdev;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007825
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007826 port->link_irq = of_irq_get_byname(port_node, "link");
7827 if (port->link_irq == -EPROBE_DEFER) {
7828 err = -EPROBE_DEFER;
7829 goto err_deinit_qvecs;
7830 }
7831 if (port->link_irq <= 0)
7832 /* the link irq is optional */
7833 port->link_irq = 0;
7834
Marcin Wojtas3f518502014-07-10 16:52:13 -03007835 if (of_property_read_bool(port_node, "marvell,loopback"))
7836 port->flags |= MVPP2_F_LOOPBACK;
7837
Marcin Wojtas3f518502014-07-10 16:52:13 -03007838 port->id = id;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01007839 if (priv->hw_version == MVPP21)
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007840 port->first_rxq = port->id * port->nrxqs;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01007841 else
7842 port->first_rxq = port->id * priv->max_port_rxqs;
7843
Marcin Wojtas3f518502014-07-10 16:52:13 -03007844 port->phy_node = phy_node;
7845 port->phy_interface = phy_mode;
Antoine Tenart542897d2017-08-30 10:29:15 +02007846 port->comphy = comphy;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007847
Thomas Petazzonia7868412017-03-07 16:53:13 +01007848 if (priv->hw_version == MVPP21) {
7849 res = platform_get_resource(pdev, IORESOURCE_MEM, 2 + id);
7850 port->base = devm_ioremap_resource(&pdev->dev, res);
7851 if (IS_ERR(port->base)) {
7852 err = PTR_ERR(port->base);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007853 goto err_free_irq;
Thomas Petazzonia7868412017-03-07 16:53:13 +01007854 }
Miquel Raynal118d6292017-11-06 22:56:53 +01007855
7856 port->stats_base = port->priv->lms_base +
7857 MVPP21_MIB_COUNTERS_OFFSET +
7858 port->gop_id * MVPP21_MIB_COUNTERS_PORT_SZ;
Thomas Petazzonia7868412017-03-07 16:53:13 +01007859 } else {
7860 if (of_property_read_u32(port_node, "gop-port-id",
7861 &port->gop_id)) {
7862 err = -EINVAL;
7863 dev_err(&pdev->dev, "missing gop-port-id value\n");
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007864 goto err_deinit_qvecs;
Thomas Petazzonia7868412017-03-07 16:53:13 +01007865 }
7866
7867 port->base = priv->iface_base + MVPP22_GMAC_BASE(port->gop_id);
Miquel Raynal118d6292017-11-06 22:56:53 +01007868 port->stats_base = port->priv->iface_base +
7869 MVPP22_MIB_COUNTERS_OFFSET +
7870 port->gop_id * MVPP22_MIB_COUNTERS_PORT_SZ;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007871 }
7872
Miquel Raynal118d6292017-11-06 22:56:53 +01007873 /* Alloc per-cpu and ethtool stats */
Marcin Wojtas3f518502014-07-10 16:52:13 -03007874 port->stats = netdev_alloc_pcpu_stats(struct mvpp2_pcpu_stats);
7875 if (!port->stats) {
7876 err = -ENOMEM;
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007877 goto err_free_irq;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007878 }
7879
Miquel Raynal118d6292017-11-06 22:56:53 +01007880 port->ethtool_stats = devm_kcalloc(&pdev->dev,
7881 ARRAY_SIZE(mvpp2_ethtool_regs),
7882 sizeof(u64), GFP_KERNEL);
7883 if (!port->ethtool_stats) {
7884 err = -ENOMEM;
7885 goto err_free_stats;
7886 }
7887
Miquel Raynale5c500e2017-11-08 08:59:40 +01007888 mutex_init(&port->gather_stats_lock);
7889 INIT_DELAYED_WORK(&port->stats_work, mvpp2_gather_hw_statistics);
7890
Antoine Tenart3ba8c812017-09-02 11:06:47 +02007891 mvpp2_port_copy_mac_addr(dev, priv, port_node, &mac_from);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007892
7893 port->tx_ring_size = MVPP2_MAX_TXD;
7894 port->rx_ring_size = MVPP2_MAX_RXD;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007895 SET_NETDEV_DEV(dev, &pdev->dev);
7896
7897 err = mvpp2_port_init(port);
7898 if (err < 0) {
7899 dev_err(&pdev->dev, "failed to init port %d\n", id);
7900 goto err_free_stats;
7901 }
Thomas Petazzoni26975822017-03-07 16:53:14 +01007902
Thomas Petazzoni26975822017-03-07 16:53:14 +01007903 mvpp2_port_periodic_xon_disable(port);
7904
7905 if (priv->hw_version == MVPP21)
7906 mvpp2_port_fc_adv_enable(port);
7907
7908 mvpp2_port_reset(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007909
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007910 port->pcpu = alloc_percpu(struct mvpp2_port_pcpu);
7911 if (!port->pcpu) {
7912 err = -ENOMEM;
7913 goto err_free_txq_pcpu;
7914 }
7915
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007916 if (!port->has_tx_irqs) {
7917 for_each_present_cpu(cpu) {
7918 port_pcpu = per_cpu_ptr(port->pcpu, cpu);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007919
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007920 hrtimer_init(&port_pcpu->tx_done_timer, CLOCK_MONOTONIC,
7921 HRTIMER_MODE_REL_PINNED);
7922 port_pcpu->tx_done_timer.function = mvpp2_hr_timer_cb;
7923 port_pcpu->timer_scheduled = false;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007924
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007925 tasklet_init(&port_pcpu->tx_done_tasklet,
7926 mvpp2_tx_proc_cb,
7927 (unsigned long)dev);
7928 }
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007929 }
7930
Antoine Ténart186cd4d2017-08-23 09:46:56 +02007931 features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007932 dev->features = features | NETIF_F_RXCSUM;
7933 dev->hw_features |= features | NETIF_F_RXCSUM | NETIF_F_GRO;
7934 dev->vlan_features |= features;
Antoine Tenart1d17db02017-10-30 11:23:31 +01007935 dev->gso_max_segs = MVPP2_MAX_TSO_SEGS;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007936
Jarod Wilson57779872016-10-17 15:54:06 -04007937 /* MTU range: 68 - 9676 */
7938 dev->min_mtu = ETH_MIN_MTU;
7939 /* 9676 == 9700 - 20 and rounding to 8 */
7940 dev->max_mtu = 9676;
7941
Marcin Wojtas3f518502014-07-10 16:52:13 -03007942 err = register_netdev(dev);
7943 if (err < 0) {
7944 dev_err(&pdev->dev, "failed to register netdev\n");
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007945 goto err_free_port_pcpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007946 }
7947 netdev_info(dev, "Using %s mac address %pM\n", mac_from, dev->dev_addr);
7948
Yan Markman6bf69a12017-09-25 14:59:47 +02007949 priv->port_list[index] = port;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007950 return 0;
7951
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007952err_free_port_pcpu:
7953 free_percpu(port->pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007954err_free_txq_pcpu:
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007955 for (i = 0; i < port->ntxqs; i++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007956 free_percpu(port->txqs[i]->pcpu);
7957err_free_stats:
7958 free_percpu(port->stats);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007959err_free_irq:
7960 if (port->link_irq)
7961 irq_dispose_mapping(port->link_irq);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007962err_deinit_qvecs:
7963 mvpp2_queue_vectors_deinit(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007964err_free_netdev:
Peter Chenccb80392016-08-01 15:02:37 +08007965 of_node_put(phy_node);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007966 free_netdev(dev);
7967 return err;
7968}
7969
7970/* Ports removal routine */
7971static void mvpp2_port_remove(struct mvpp2_port *port)
7972{
7973 int i;
7974
7975 unregister_netdev(port->dev);
Peter Chenccb80392016-08-01 15:02:37 +08007976 of_node_put(port->phy_node);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007977 free_percpu(port->pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007978 free_percpu(port->stats);
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007979 for (i = 0; i < port->ntxqs; i++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007980 free_percpu(port->txqs[i]->pcpu);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007981 mvpp2_queue_vectors_deinit(port);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007982 if (port->link_irq)
7983 irq_dispose_mapping(port->link_irq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007984 free_netdev(port->dev);
7985}
7986
7987/* Initialize decoding windows */
7988static void mvpp2_conf_mbus_windows(const struct mbus_dram_target_info *dram,
7989 struct mvpp2 *priv)
7990{
7991 u32 win_enable;
7992 int i;
7993
7994 for (i = 0; i < 6; i++) {
7995 mvpp2_write(priv, MVPP2_WIN_BASE(i), 0);
7996 mvpp2_write(priv, MVPP2_WIN_SIZE(i), 0);
7997
7998 if (i < 4)
7999 mvpp2_write(priv, MVPP2_WIN_REMAP(i), 0);
8000 }
8001
8002 win_enable = 0;
8003
8004 for (i = 0; i < dram->num_cs; i++) {
8005 const struct mbus_dram_window *cs = dram->cs + i;
8006
8007 mvpp2_write(priv, MVPP2_WIN_BASE(i),
8008 (cs->base & 0xffff0000) | (cs->mbus_attr << 8) |
8009 dram->mbus_dram_target_id);
8010
8011 mvpp2_write(priv, MVPP2_WIN_SIZE(i),
8012 (cs->size - 1) & 0xffff0000);
8013
8014 win_enable |= (1 << i);
8015 }
8016
8017 mvpp2_write(priv, MVPP2_BASE_ADDR_ENABLE, win_enable);
8018}
8019
8020/* Initialize Rx FIFO's */
8021static void mvpp2_rx_fifo_init(struct mvpp2 *priv)
8022{
8023 int port;
8024
8025 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
8026 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
Antoine Tenart2d1d7df2017-10-30 11:23:28 +01008027 MVPP2_RX_FIFO_PORT_DATA_SIZE_4KB);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008028 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
Antoine Tenart2d1d7df2017-10-30 11:23:28 +01008029 MVPP2_RX_FIFO_PORT_ATTR_SIZE_4KB);
8030 }
8031
8032 mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
8033 MVPP2_RX_FIFO_PORT_MIN_PKT);
8034 mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
8035}
8036
8037static void mvpp22_rx_fifo_init(struct mvpp2 *priv)
8038{
8039 int port;
8040
8041 /* The FIFO size parameters are set depending on the maximum speed a
8042 * given port can handle:
8043 * - Port 0: 10Gbps
8044 * - Port 1: 2.5Gbps
8045 * - Ports 2 and 3: 1Gbps
8046 */
8047
8048 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(0),
8049 MVPP2_RX_FIFO_PORT_DATA_SIZE_32KB);
8050 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(0),
8051 MVPP2_RX_FIFO_PORT_ATTR_SIZE_32KB);
8052
8053 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(1),
8054 MVPP2_RX_FIFO_PORT_DATA_SIZE_8KB);
8055 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(1),
8056 MVPP2_RX_FIFO_PORT_ATTR_SIZE_8KB);
8057
8058 for (port = 2; port < MVPP2_MAX_PORTS; port++) {
8059 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
8060 MVPP2_RX_FIFO_PORT_DATA_SIZE_4KB);
8061 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
8062 MVPP2_RX_FIFO_PORT_ATTR_SIZE_4KB);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008063 }
8064
8065 mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
8066 MVPP2_RX_FIFO_PORT_MIN_PKT);
8067 mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
8068}
8069
Antoine Tenart7c10f972017-10-30 11:23:29 +01008070/* Initialize Tx FIFO's */
8071static void mvpp22_tx_fifo_init(struct mvpp2 *priv)
8072{
8073 int port;
8074
8075 for (port = 0; port < MVPP2_MAX_PORTS; port++)
8076 mvpp2_write(priv, MVPP22_TX_FIFO_SIZE_REG(port),
8077 MVPP22_TX_FIFO_DATA_SIZE_3KB);
8078}
8079
Thomas Petazzoni6763ce32017-03-07 16:53:15 +01008080static void mvpp2_axi_init(struct mvpp2 *priv)
8081{
8082 u32 val, rdval, wrval;
8083
8084 mvpp2_write(priv, MVPP22_BM_ADDR_HIGH_RLS_REG, 0x0);
8085
8086 /* AXI Bridge Configuration */
8087
8088 rdval = MVPP22_AXI_CODE_CACHE_RD_CACHE
8089 << MVPP22_AXI_ATTR_CACHE_OFFS;
8090 rdval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
8091 << MVPP22_AXI_ATTR_DOMAIN_OFFS;
8092
8093 wrval = MVPP22_AXI_CODE_CACHE_WR_CACHE
8094 << MVPP22_AXI_ATTR_CACHE_OFFS;
8095 wrval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
8096 << MVPP22_AXI_ATTR_DOMAIN_OFFS;
8097
8098 /* BM */
8099 mvpp2_write(priv, MVPP22_AXI_BM_WR_ATTR_REG, wrval);
8100 mvpp2_write(priv, MVPP22_AXI_BM_RD_ATTR_REG, rdval);
8101
8102 /* Descriptors */
8103 mvpp2_write(priv, MVPP22_AXI_AGGRQ_DESCR_RD_ATTR_REG, rdval);
8104 mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_WR_ATTR_REG, wrval);
8105 mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_RD_ATTR_REG, rdval);
8106 mvpp2_write(priv, MVPP22_AXI_RXQ_DESCR_WR_ATTR_REG, wrval);
8107
8108 /* Buffer Data */
8109 mvpp2_write(priv, MVPP22_AXI_TX_DATA_RD_ATTR_REG, rdval);
8110 mvpp2_write(priv, MVPP22_AXI_RX_DATA_WR_ATTR_REG, wrval);
8111
8112 val = MVPP22_AXI_CODE_CACHE_NON_CACHE
8113 << MVPP22_AXI_CODE_CACHE_OFFS;
8114 val |= MVPP22_AXI_CODE_DOMAIN_SYSTEM
8115 << MVPP22_AXI_CODE_DOMAIN_OFFS;
8116 mvpp2_write(priv, MVPP22_AXI_RD_NORMAL_CODE_REG, val);
8117 mvpp2_write(priv, MVPP22_AXI_WR_NORMAL_CODE_REG, val);
8118
8119 val = MVPP22_AXI_CODE_CACHE_RD_CACHE
8120 << MVPP22_AXI_CODE_CACHE_OFFS;
8121 val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
8122 << MVPP22_AXI_CODE_DOMAIN_OFFS;
8123
8124 mvpp2_write(priv, MVPP22_AXI_RD_SNOOP_CODE_REG, val);
8125
8126 val = MVPP22_AXI_CODE_CACHE_WR_CACHE
8127 << MVPP22_AXI_CODE_CACHE_OFFS;
8128 val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
8129 << MVPP22_AXI_CODE_DOMAIN_OFFS;
8130
8131 mvpp2_write(priv, MVPP22_AXI_WR_SNOOP_CODE_REG, val);
8132}
8133
Marcin Wojtas3f518502014-07-10 16:52:13 -03008134/* Initialize network controller common part HW */
8135static int mvpp2_init(struct platform_device *pdev, struct mvpp2 *priv)
8136{
8137 const struct mbus_dram_target_info *dram_target_info;
8138 int err, i;
Marcin Wojtas08a23752014-07-21 13:48:12 -03008139 u32 val;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008140
Marcin Wojtas3f518502014-07-10 16:52:13 -03008141 /* MBUS windows configuration */
8142 dram_target_info = mv_mbus_dram_info();
8143 if (dram_target_info)
8144 mvpp2_conf_mbus_windows(dram_target_info, priv);
8145
Thomas Petazzoni6763ce32017-03-07 16:53:15 +01008146 if (priv->hw_version == MVPP22)
8147 mvpp2_axi_init(priv);
8148
Marcin Wojtas08a23752014-07-21 13:48:12 -03008149 /* Disable HW PHY polling */
Thomas Petazzoni26975822017-03-07 16:53:14 +01008150 if (priv->hw_version == MVPP21) {
8151 val = readl(priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
8152 val |= MVPP2_PHY_AN_STOP_SMI0_MASK;
8153 writel(val, priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
8154 } else {
8155 val = readl(priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
8156 val &= ~MVPP22_SMI_POLLING_EN;
8157 writel(val, priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
8158 }
Marcin Wojtas08a23752014-07-21 13:48:12 -03008159
Marcin Wojtas3f518502014-07-10 16:52:13 -03008160 /* Allocate and initialize aggregated TXQs */
8161 priv->aggr_txqs = devm_kcalloc(&pdev->dev, num_present_cpus(),
Markus Elfringd7ce3ce2017-04-17 08:48:23 +02008162 sizeof(*priv->aggr_txqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03008163 GFP_KERNEL);
8164 if (!priv->aggr_txqs)
8165 return -ENOMEM;
8166
8167 for_each_present_cpu(i) {
8168 priv->aggr_txqs[i].id = i;
8169 priv->aggr_txqs[i].size = MVPP2_AGGR_TXQ_SIZE;
Antoine Ténart85affd72017-08-23 09:46:55 +02008170 err = mvpp2_aggr_txq_init(pdev, &priv->aggr_txqs[i], i, priv);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008171 if (err < 0)
8172 return err;
8173 }
8174
Antoine Tenart7c10f972017-10-30 11:23:29 +01008175 /* Fifo Init */
8176 if (priv->hw_version == MVPP21) {
Antoine Tenart2d1d7df2017-10-30 11:23:28 +01008177 mvpp2_rx_fifo_init(priv);
Antoine Tenart7c10f972017-10-30 11:23:29 +01008178 } else {
Antoine Tenart2d1d7df2017-10-30 11:23:28 +01008179 mvpp22_rx_fifo_init(priv);
Antoine Tenart7c10f972017-10-30 11:23:29 +01008180 mvpp22_tx_fifo_init(priv);
8181 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03008182
Thomas Petazzoni26975822017-03-07 16:53:14 +01008183 if (priv->hw_version == MVPP21)
8184 writel(MVPP2_EXT_GLOBAL_CTRL_DEFAULT,
8185 priv->lms_base + MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008186
8187 /* Allow cache snoop when transmiting packets */
8188 mvpp2_write(priv, MVPP2_TX_SNOOP_REG, 0x1);
8189
8190 /* Buffer Manager initialization */
8191 err = mvpp2_bm_init(pdev, priv);
8192 if (err < 0)
8193 return err;
8194
8195 /* Parser default initialization */
8196 err = mvpp2_prs_default_init(pdev, priv);
8197 if (err < 0)
8198 return err;
8199
8200 /* Classifier default initialization */
8201 mvpp2_cls_init(priv);
8202
8203 return 0;
8204}
8205
8206static int mvpp2_probe(struct platform_device *pdev)
8207{
8208 struct device_node *dn = pdev->dev.of_node;
8209 struct device_node *port_node;
8210 struct mvpp2 *priv;
8211 struct resource *res;
Thomas Petazzonia7868412017-03-07 16:53:13 +01008212 void __iomem *base;
Miquel Raynal118d6292017-11-06 22:56:53 +01008213 int i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008214 int err;
8215
Markus Elfring0b92e592017-04-17 08:38:32 +02008216 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008217 if (!priv)
8218 return -ENOMEM;
8219
Thomas Petazzonifaca9242017-03-07 16:53:06 +01008220 priv->hw_version =
8221 (unsigned long)of_device_get_match_data(&pdev->dev);
8222
Marcin Wojtas3f518502014-07-10 16:52:13 -03008223 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thomas Petazzonia7868412017-03-07 16:53:13 +01008224 base = devm_ioremap_resource(&pdev->dev, res);
8225 if (IS_ERR(base))
8226 return PTR_ERR(base);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008227
Thomas Petazzonia7868412017-03-07 16:53:13 +01008228 if (priv->hw_version == MVPP21) {
8229 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
8230 priv->lms_base = devm_ioremap_resource(&pdev->dev, res);
8231 if (IS_ERR(priv->lms_base))
8232 return PTR_ERR(priv->lms_base);
8233 } else {
8234 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
8235 priv->iface_base = devm_ioremap_resource(&pdev->dev, res);
8236 if (IS_ERR(priv->iface_base))
8237 return PTR_ERR(priv->iface_base);
Antoine Ténartf84bf382017-08-22 19:08:27 +02008238
8239 priv->sysctrl_base =
8240 syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
8241 "marvell,system-controller");
8242 if (IS_ERR(priv->sysctrl_base))
8243 /* The system controller regmap is optional for dt
8244 * compatibility reasons. When not provided, the
8245 * configuration of the GoP relies on the
8246 * firmware/bootloader.
8247 */
8248 priv->sysctrl_base = NULL;
Thomas Petazzonia7868412017-03-07 16:53:13 +01008249 }
8250
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02008251 for (i = 0; i < MVPP2_MAX_THREADS; i++) {
Thomas Petazzonia7868412017-03-07 16:53:13 +01008252 u32 addr_space_sz;
8253
8254 addr_space_sz = (priv->hw_version == MVPP21 ?
8255 MVPP21_ADDR_SPACE_SZ : MVPP22_ADDR_SPACE_SZ);
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02008256 priv->swth_base[i] = base + i * addr_space_sz;
Thomas Petazzonia7868412017-03-07 16:53:13 +01008257 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03008258
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01008259 if (priv->hw_version == MVPP21)
8260 priv->max_port_rxqs = 8;
8261 else
8262 priv->max_port_rxqs = 32;
8263
Marcin Wojtas3f518502014-07-10 16:52:13 -03008264 priv->pp_clk = devm_clk_get(&pdev->dev, "pp_clk");
8265 if (IS_ERR(priv->pp_clk))
8266 return PTR_ERR(priv->pp_clk);
8267 err = clk_prepare_enable(priv->pp_clk);
8268 if (err < 0)
8269 return err;
8270
8271 priv->gop_clk = devm_clk_get(&pdev->dev, "gop_clk");
8272 if (IS_ERR(priv->gop_clk)) {
8273 err = PTR_ERR(priv->gop_clk);
8274 goto err_pp_clk;
8275 }
8276 err = clk_prepare_enable(priv->gop_clk);
8277 if (err < 0)
8278 goto err_pp_clk;
8279
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008280 if (priv->hw_version == MVPP22) {
8281 priv->mg_clk = devm_clk_get(&pdev->dev, "mg_clk");
8282 if (IS_ERR(priv->mg_clk)) {
8283 err = PTR_ERR(priv->mg_clk);
8284 goto err_gop_clk;
8285 }
8286
8287 err = clk_prepare_enable(priv->mg_clk);
8288 if (err < 0)
8289 goto err_gop_clk;
Gregory CLEMENT4792ea02017-09-29 14:27:39 +02008290
8291 priv->axi_clk = devm_clk_get(&pdev->dev, "axi_clk");
8292 if (IS_ERR(priv->axi_clk)) {
8293 err = PTR_ERR(priv->axi_clk);
8294 if (err == -EPROBE_DEFER)
8295 goto err_gop_clk;
8296 priv->axi_clk = NULL;
8297 } else {
8298 err = clk_prepare_enable(priv->axi_clk);
8299 if (err < 0)
8300 goto err_gop_clk;
8301 }
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008302 }
8303
Marcin Wojtas3f518502014-07-10 16:52:13 -03008304 /* Get system's tclk rate */
8305 priv->tclk = clk_get_rate(priv->pp_clk);
8306
Thomas Petazzoni2067e0a2017-03-07 16:53:19 +01008307 if (priv->hw_version == MVPP22) {
8308 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(40));
8309 if (err)
8310 goto err_mg_clk;
8311 /* Sadly, the BM pools all share the same register to
8312 * store the high 32 bits of their address. So they
8313 * must all have the same high 32 bits, which forces
8314 * us to restrict coherent memory to DMA_BIT_MASK(32).
8315 */
8316 err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
8317 if (err)
8318 goto err_mg_clk;
8319 }
8320
Marcin Wojtas3f518502014-07-10 16:52:13 -03008321 /* Initialize network controller */
8322 err = mvpp2_init(pdev, priv);
8323 if (err < 0) {
8324 dev_err(&pdev->dev, "failed to initialize controller\n");
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008325 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008326 }
8327
Miquel Raynal118d6292017-11-06 22:56:53 +01008328 priv->port_count = of_get_available_child_count(dn);
8329 if (priv->port_count == 0) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03008330 dev_err(&pdev->dev, "no ports enabled\n");
Wei Yongjun575a1932014-07-20 22:02:43 +08008331 err = -ENODEV;
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008332 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008333 }
8334
Miquel Raynal118d6292017-11-06 22:56:53 +01008335 priv->port_list = devm_kcalloc(&pdev->dev, priv->port_count,
Markus Elfring0b92e592017-04-17 08:38:32 +02008336 sizeof(*priv->port_list),
8337 GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008338 if (!priv->port_list) {
8339 err = -ENOMEM;
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008340 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008341 }
8342
8343 /* Initialize ports */
Yan Markman6bf69a12017-09-25 14:59:47 +02008344 i = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008345 for_each_available_child_of_node(dn, port_node) {
Yan Markman6bf69a12017-09-25 14:59:47 +02008346 err = mvpp2_port_probe(pdev, port_node, priv, i);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008347 if (err < 0)
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008348 goto err_mg_clk;
Yan Markman6bf69a12017-09-25 14:59:47 +02008349 i++;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008350 }
8351
Miquel Raynal118d6292017-11-06 22:56:53 +01008352 /* Statistics must be gathered regularly because some of them (like
8353 * packets counters) are 32-bit registers and could overflow quite
8354 * quickly. For instance, a 10Gb link used at full bandwidth with the
8355 * smallest packets (64B) will overflow a 32-bit counter in less than
8356 * 30 seconds. Then, use a workqueue to fill 64-bit counters.
8357 */
Miquel Raynal118d6292017-11-06 22:56:53 +01008358 snprintf(priv->queue_name, sizeof(priv->queue_name),
8359 "stats-wq-%s%s", netdev_name(priv->port_list[0]->dev),
8360 priv->port_count > 1 ? "+" : "");
8361 priv->stats_queue = create_singlethread_workqueue(priv->queue_name);
8362 if (!priv->stats_queue) {
8363 err = -ENOMEM;
8364 goto err_mg_clk;
8365 }
8366
Marcin Wojtas3f518502014-07-10 16:52:13 -03008367 platform_set_drvdata(pdev, priv);
8368 return 0;
8369
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008370err_mg_clk:
Gregory CLEMENT4792ea02017-09-29 14:27:39 +02008371 clk_disable_unprepare(priv->axi_clk);
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008372 if (priv->hw_version == MVPP22)
8373 clk_disable_unprepare(priv->mg_clk);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008374err_gop_clk:
8375 clk_disable_unprepare(priv->gop_clk);
8376err_pp_clk:
8377 clk_disable_unprepare(priv->pp_clk);
8378 return err;
8379}
8380
8381static int mvpp2_remove(struct platform_device *pdev)
8382{
8383 struct mvpp2 *priv = platform_get_drvdata(pdev);
8384 struct device_node *dn = pdev->dev.of_node;
8385 struct device_node *port_node;
8386 int i = 0;
8387
Miquel Raynale5c500e2017-11-08 08:59:40 +01008388 flush_workqueue(priv->stats_queue);
Miquel Raynal118d6292017-11-06 22:56:53 +01008389 destroy_workqueue(priv->stats_queue);
Miquel Raynal118d6292017-11-06 22:56:53 +01008390
Marcin Wojtas3f518502014-07-10 16:52:13 -03008391 for_each_available_child_of_node(dn, port_node) {
Miquel Raynale5c500e2017-11-08 08:59:40 +01008392 if (priv->port_list[i]) {
8393 mutex_destroy(&priv->port_list[i]->gather_stats_lock);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008394 mvpp2_port_remove(priv->port_list[i]);
Miquel Raynale5c500e2017-11-08 08:59:40 +01008395 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03008396 i++;
8397 }
8398
8399 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
8400 struct mvpp2_bm_pool *bm_pool = &priv->bm_pools[i];
8401
8402 mvpp2_bm_pool_destroy(pdev, priv, bm_pool);
8403 }
8404
8405 for_each_present_cpu(i) {
8406 struct mvpp2_tx_queue *aggr_txq = &priv->aggr_txqs[i];
8407
8408 dma_free_coherent(&pdev->dev,
8409 MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
8410 aggr_txq->descs,
Thomas Petazzoni20396132017-03-07 16:53:00 +01008411 aggr_txq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008412 }
8413
Gregory CLEMENT4792ea02017-09-29 14:27:39 +02008414 clk_disable_unprepare(priv->axi_clk);
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008415 clk_disable_unprepare(priv->mg_clk);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008416 clk_disable_unprepare(priv->pp_clk);
8417 clk_disable_unprepare(priv->gop_clk);
8418
8419 return 0;
8420}
8421
8422static const struct of_device_id mvpp2_match[] = {
Thomas Petazzonifaca9242017-03-07 16:53:06 +01008423 {
8424 .compatible = "marvell,armada-375-pp2",
8425 .data = (void *)MVPP21,
8426 },
Thomas Petazzonifc5e1552017-03-07 16:53:20 +01008427 {
8428 .compatible = "marvell,armada-7k-pp22",
8429 .data = (void *)MVPP22,
8430 },
Marcin Wojtas3f518502014-07-10 16:52:13 -03008431 { }
8432};
8433MODULE_DEVICE_TABLE(of, mvpp2_match);
8434
8435static struct platform_driver mvpp2_driver = {
8436 .probe = mvpp2_probe,
8437 .remove = mvpp2_remove,
8438 .driver = {
8439 .name = MVPP2_DRIVER_NAME,
8440 .of_match_table = mvpp2_match,
8441 },
8442};
8443
8444module_platform_driver(mvpp2_driver);
8445
8446MODULE_DESCRIPTION("Marvell PPv2 Ethernet Driver - www.marvell.com");
8447MODULE_AUTHOR("Marcin Wojtas <mw@semihalf.com>");
Ezequiel Garciac6340992014-07-14 10:34:47 -03008448MODULE_LICENSE("GPL v2");