blob: 981fedeef67b253f970321267d11d03583d7891c [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
802/* Definitions */
803
804/* Shared Packet Processor resources */
805struct mvpp2 {
806 /* Shared registers' base addresses */
Marcin Wojtas3f518502014-07-10 16:52:13 -0300807 void __iomem *lms_base;
Thomas Petazzonia7868412017-03-07 16:53:13 +0100808 void __iomem *iface_base;
809
Thomas Petazzonidf089aa2017-08-03 10:41:58 +0200810 /* On PPv2.2, each "software thread" can access the base
811 * register through a separate address space, each 64 KB apart
812 * from each other. Typically, such address spaces will be
813 * used per CPU.
Thomas Petazzonia7868412017-03-07 16:53:13 +0100814 */
Thomas Petazzonidf089aa2017-08-03 10:41:58 +0200815 void __iomem *swth_base[MVPP2_MAX_THREADS];
Marcin Wojtas3f518502014-07-10 16:52:13 -0300816
Antoine Ténartf84bf382017-08-22 19:08:27 +0200817 /* On PPv2.2, some port control registers are located into the system
818 * controller space. These registers are accessible through a regmap.
819 */
820 struct regmap *sysctrl_base;
821
Marcin Wojtas3f518502014-07-10 16:52:13 -0300822 /* Common clocks */
823 struct clk *pp_clk;
824 struct clk *gop_clk;
Thomas Petazzonifceb55d2017-03-07 16:53:18 +0100825 struct clk *mg_clk;
Gregory CLEMENT4792ea02017-09-29 14:27:39 +0200826 struct clk *axi_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300827
828 /* List of pointers to port structures */
829 struct mvpp2_port **port_list;
830
831 /* Aggregated TXQs */
832 struct mvpp2_tx_queue *aggr_txqs;
833
834 /* BM pools */
835 struct mvpp2_bm_pool *bm_pools;
836
837 /* PRS shadow table */
838 struct mvpp2_prs_shadow *prs_shadow;
839 /* PRS auxiliary table for double vlan entries control */
840 bool *prs_double_vlans;
841
842 /* Tclk value */
843 u32 tclk;
Thomas Petazzonifaca9242017-03-07 16:53:06 +0100844
845 /* HW version */
846 enum { MVPP21, MVPP22 } hw_version;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +0100847
848 /* Maximum number of RXQs per port */
849 unsigned int max_port_rxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300850};
851
852struct mvpp2_pcpu_stats {
853 struct u64_stats_sync syncp;
854 u64 rx_packets;
855 u64 rx_bytes;
856 u64 tx_packets;
857 u64 tx_bytes;
858};
859
Marcin Wojtasedc660f2015-08-06 19:00:30 +0200860/* Per-CPU port control */
861struct mvpp2_port_pcpu {
862 struct hrtimer tx_done_timer;
863 bool timer_scheduled;
864 /* Tasklet for egress finalization */
865 struct tasklet_struct tx_done_tasklet;
866};
867
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +0200868struct mvpp2_queue_vector {
869 int irq;
870 struct napi_struct napi;
871 enum { MVPP2_QUEUE_VECTOR_SHARED, MVPP2_QUEUE_VECTOR_PRIVATE } type;
872 int sw_thread_id;
873 u16 sw_thread_mask;
874 int first_rxq;
875 int nrxqs;
876 u32 pending_cause_rx;
877 struct mvpp2_port *port;
878};
879
Marcin Wojtas3f518502014-07-10 16:52:13 -0300880struct mvpp2_port {
881 u8 id;
882
Thomas Petazzonia7868412017-03-07 16:53:13 +0100883 /* Index of the port from the "group of ports" complex point
884 * of view
885 */
886 int gop_id;
887
Antoine Tenartfd3651b2017-09-01 11:04:54 +0200888 int link_irq;
889
Marcin Wojtas3f518502014-07-10 16:52:13 -0300890 struct mvpp2 *priv;
891
892 /* Per-port registers' base address */
893 void __iomem *base;
894
895 struct mvpp2_rx_queue **rxqs;
Thomas Petazzoni09f83972017-08-03 10:41:57 +0200896 unsigned int nrxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300897 struct mvpp2_tx_queue **txqs;
Thomas Petazzoni09f83972017-08-03 10:41:57 +0200898 unsigned int ntxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300899 struct net_device *dev;
900
901 int pkt_size;
902
Marcin Wojtasedc660f2015-08-06 19:00:30 +0200903 /* Per-CPU port control */
904 struct mvpp2_port_pcpu __percpu *pcpu;
905
Marcin Wojtas3f518502014-07-10 16:52:13 -0300906 /* Flags */
907 unsigned long flags;
908
909 u16 tx_ring_size;
910 u16 rx_ring_size;
911 struct mvpp2_pcpu_stats __percpu *stats;
912
Marcin Wojtas3f518502014-07-10 16:52:13 -0300913 phy_interface_t phy_interface;
914 struct device_node *phy_node;
Antoine Tenart542897d2017-08-30 10:29:15 +0200915 struct phy *comphy;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300916 unsigned int link;
917 unsigned int duplex;
918 unsigned int speed;
919
920 struct mvpp2_bm_pool *pool_long;
921 struct mvpp2_bm_pool *pool_short;
922
923 /* Index of first port's physical RXQ */
924 u8 first_rxq;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +0200925
926 struct mvpp2_queue_vector qvecs[MVPP2_MAX_QVECS];
927 unsigned int nqvecs;
Thomas Petazzoni213f4282017-08-03 10:42:00 +0200928 bool has_tx_irqs;
929
930 u32 tx_time_coal;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300931};
932
933/* The mvpp2_tx_desc and mvpp2_rx_desc structures describe the
934 * layout of the transmit and reception DMA descriptors, and their
935 * layout is therefore defined by the hardware design
936 */
937
938#define MVPP2_TXD_L3_OFF_SHIFT 0
939#define MVPP2_TXD_IP_HLEN_SHIFT 8
940#define MVPP2_TXD_L4_CSUM_FRAG BIT(13)
941#define MVPP2_TXD_L4_CSUM_NOT BIT(14)
942#define MVPP2_TXD_IP_CSUM_DISABLE BIT(15)
943#define MVPP2_TXD_PADDING_DISABLE BIT(23)
944#define MVPP2_TXD_L4_UDP BIT(24)
945#define MVPP2_TXD_L3_IP6 BIT(26)
946#define MVPP2_TXD_L_DESC BIT(28)
947#define MVPP2_TXD_F_DESC BIT(29)
948
949#define MVPP2_RXD_ERR_SUMMARY BIT(15)
950#define MVPP2_RXD_ERR_CODE_MASK (BIT(13) | BIT(14))
951#define MVPP2_RXD_ERR_CRC 0x0
952#define MVPP2_RXD_ERR_OVERRUN BIT(13)
953#define MVPP2_RXD_ERR_RESOURCE (BIT(13) | BIT(14))
954#define MVPP2_RXD_BM_POOL_ID_OFFS 16
955#define MVPP2_RXD_BM_POOL_ID_MASK (BIT(16) | BIT(17) | BIT(18))
956#define MVPP2_RXD_HWF_SYNC BIT(21)
957#define MVPP2_RXD_L4_CSUM_OK BIT(22)
958#define MVPP2_RXD_IP4_HEADER_ERR BIT(24)
959#define MVPP2_RXD_L4_TCP BIT(25)
960#define MVPP2_RXD_L4_UDP BIT(26)
961#define MVPP2_RXD_L3_IP4 BIT(28)
962#define MVPP2_RXD_L3_IP6 BIT(30)
963#define MVPP2_RXD_BUF_HDR BIT(31)
964
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100965/* HW TX descriptor for PPv2.1 */
966struct mvpp21_tx_desc {
Marcin Wojtas3f518502014-07-10 16:52:13 -0300967 u32 command; /* Options used by HW for packet transmitting.*/
968 u8 packet_offset; /* the offset from the buffer beginning */
969 u8 phys_txq; /* destination queue ID */
970 u16 data_size; /* data size of transmitted packet in bytes */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100971 u32 buf_dma_addr; /* physical addr of transmitted buffer */
Marcin Wojtas3f518502014-07-10 16:52:13 -0300972 u32 buf_cookie; /* cookie for access to TX buffer in tx path */
973 u32 reserved1[3]; /* hw_cmd (for future use, BM, PON, PNC) */
974 u32 reserved2; /* reserved (for future use) */
975};
976
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100977/* HW RX descriptor for PPv2.1 */
978struct mvpp21_rx_desc {
Marcin Wojtas3f518502014-07-10 16:52:13 -0300979 u32 status; /* info about received packet */
980 u16 reserved1; /* parser_info (for future use, PnC) */
981 u16 data_size; /* size of received packet in bytes */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100982 u32 buf_dma_addr; /* physical address of the buffer */
Marcin Wojtas3f518502014-07-10 16:52:13 -0300983 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
984 u16 reserved2; /* gem_port_id (for future use, PON) */
985 u16 reserved3; /* csum_l4 (for future use, PnC) */
986 u8 reserved4; /* bm_qset (for future use, BM) */
987 u8 reserved5;
988 u16 reserved6; /* classify_info (for future use, PnC) */
989 u32 reserved7; /* flow_id (for future use, PnC) */
990 u32 reserved8;
991};
992
Thomas Petazzonie7c53592017-03-07 16:53:08 +0100993/* HW TX descriptor for PPv2.2 */
994struct mvpp22_tx_desc {
995 u32 command;
996 u8 packet_offset;
997 u8 phys_txq;
998 u16 data_size;
999 u64 reserved1;
1000 u64 buf_dma_addr_ptp;
1001 u64 buf_cookie_misc;
1002};
1003
1004/* HW RX descriptor for PPv2.2 */
1005struct mvpp22_rx_desc {
1006 u32 status;
1007 u16 reserved1;
1008 u16 data_size;
1009 u32 reserved2;
1010 u32 reserved3;
1011 u64 buf_dma_addr_key_hash;
1012 u64 buf_cookie_misc;
1013};
1014
Thomas Petazzoni054f6372017-03-07 16:53:07 +01001015/* Opaque type used by the driver to manipulate the HW TX and RX
1016 * descriptors
1017 */
1018struct mvpp2_tx_desc {
1019 union {
1020 struct mvpp21_tx_desc pp21;
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001021 struct mvpp22_tx_desc pp22;
Thomas Petazzoni054f6372017-03-07 16:53:07 +01001022 };
1023};
1024
1025struct mvpp2_rx_desc {
1026 union {
1027 struct mvpp21_rx_desc pp21;
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001028 struct mvpp22_rx_desc pp22;
Thomas Petazzoni054f6372017-03-07 16:53:07 +01001029 };
1030};
1031
Thomas Petazzoni83544912016-12-21 11:28:49 +01001032struct mvpp2_txq_pcpu_buf {
1033 /* Transmitted SKB */
1034 struct sk_buff *skb;
1035
1036 /* Physical address of transmitted buffer */
Thomas Petazzoni20396132017-03-07 16:53:00 +01001037 dma_addr_t dma;
Thomas Petazzoni83544912016-12-21 11:28:49 +01001038
1039 /* Size transmitted */
1040 size_t size;
1041};
1042
Marcin Wojtas3f518502014-07-10 16:52:13 -03001043/* Per-CPU Tx queue control */
1044struct mvpp2_txq_pcpu {
1045 int cpu;
1046
1047 /* Number of Tx DMA descriptors in the descriptor ring */
1048 int size;
1049
1050 /* Number of currently used Tx DMA descriptor in the
1051 * descriptor ring
1052 */
1053 int count;
1054
Antoine Tenart1d17db02017-10-30 11:23:31 +01001055 int wake_threshold;
1056 int stop_threshold;
1057
Marcin Wojtas3f518502014-07-10 16:52:13 -03001058 /* Number of Tx DMA descriptors reserved for each CPU */
1059 int reserved_num;
1060
Thomas Petazzoni83544912016-12-21 11:28:49 +01001061 /* Infos about transmitted buffers */
1062 struct mvpp2_txq_pcpu_buf *buffs;
Marcin Wojtas71ce3912015-08-06 19:00:29 +02001063
Marcin Wojtas3f518502014-07-10 16:52:13 -03001064 /* Index of last TX DMA descriptor that was inserted */
1065 int txq_put_index;
1066
1067 /* Index of the TX DMA descriptor to be cleaned up */
1068 int txq_get_index;
Antoine Ténart186cd4d2017-08-23 09:46:56 +02001069
1070 /* DMA buffer for TSO headers */
1071 char *tso_headers;
1072 dma_addr_t tso_headers_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001073};
1074
1075struct mvpp2_tx_queue {
1076 /* Physical number of this Tx queue */
1077 u8 id;
1078
1079 /* Logical number of this Tx queue */
1080 u8 log_id;
1081
1082 /* Number of Tx DMA descriptors in the descriptor ring */
1083 int size;
1084
1085 /* Number of currently used Tx DMA descriptor in the descriptor ring */
1086 int count;
1087
1088 /* Per-CPU control of physical Tx queues */
1089 struct mvpp2_txq_pcpu __percpu *pcpu;
1090
Marcin Wojtas3f518502014-07-10 16:52:13 -03001091 u32 done_pkts_coal;
1092
1093 /* Virtual address of thex Tx DMA descriptors array */
1094 struct mvpp2_tx_desc *descs;
1095
1096 /* DMA address of the Tx DMA descriptors array */
Thomas Petazzoni20396132017-03-07 16:53:00 +01001097 dma_addr_t descs_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001098
1099 /* Index of the last Tx DMA descriptor */
1100 int last_desc;
1101
1102 /* Index of the next Tx DMA descriptor to process */
1103 int next_desc_to_proc;
1104};
1105
1106struct mvpp2_rx_queue {
1107 /* RX queue number, in the range 0-31 for physical RXQs */
1108 u8 id;
1109
1110 /* Num of rx descriptors in the rx descriptor ring */
1111 int size;
1112
1113 u32 pkts_coal;
1114 u32 time_coal;
1115
1116 /* Virtual address of the RX DMA descriptors array */
1117 struct mvpp2_rx_desc *descs;
1118
1119 /* DMA address of the RX DMA descriptors array */
Thomas Petazzoni20396132017-03-07 16:53:00 +01001120 dma_addr_t descs_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001121
1122 /* Index of the last RX DMA descriptor */
1123 int last_desc;
1124
1125 /* Index of the next RX DMA descriptor to process */
1126 int next_desc_to_proc;
1127
1128 /* ID of port to which physical RXQ is mapped */
1129 int port;
1130
1131 /* Port's logic RXQ number to which physical RXQ is mapped */
1132 int logic_rxq;
1133};
1134
1135union mvpp2_prs_tcam_entry {
1136 u32 word[MVPP2_PRS_TCAM_WORDS];
1137 u8 byte[MVPP2_PRS_TCAM_WORDS * 4];
1138};
1139
1140union mvpp2_prs_sram_entry {
1141 u32 word[MVPP2_PRS_SRAM_WORDS];
1142 u8 byte[MVPP2_PRS_SRAM_WORDS * 4];
1143};
1144
1145struct mvpp2_prs_entry {
1146 u32 index;
1147 union mvpp2_prs_tcam_entry tcam;
1148 union mvpp2_prs_sram_entry sram;
1149};
1150
1151struct mvpp2_prs_shadow {
1152 bool valid;
1153 bool finish;
1154
1155 /* Lookup ID */
1156 int lu;
1157
1158 /* User defined offset */
1159 int udf;
1160
1161 /* Result info */
1162 u32 ri;
1163 u32 ri_mask;
1164};
1165
1166struct mvpp2_cls_flow_entry {
1167 u32 index;
1168 u32 data[MVPP2_CLS_FLOWS_TBL_DATA_WORDS];
1169};
1170
1171struct mvpp2_cls_lookup_entry {
1172 u32 lkpid;
1173 u32 way;
1174 u32 data;
1175};
1176
1177struct mvpp2_bm_pool {
1178 /* Pool number in the range 0-7 */
1179 int id;
1180 enum mvpp2_bm_type type;
1181
1182 /* Buffer Pointers Pool External (BPPE) size */
1183 int size;
Thomas Petazzonid01524d2017-03-07 16:53:09 +01001184 /* BPPE size in bytes */
1185 int size_bytes;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001186 /* Number of buffers for this pool */
1187 int buf_num;
1188 /* Pool buffer size */
1189 int buf_size;
1190 /* Packet size */
1191 int pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01001192 int frag_size;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001193
1194 /* BPPE virtual base address */
1195 u32 *virt_addr;
Thomas Petazzoni20396132017-03-07 16:53:00 +01001196 /* BPPE DMA base address */
1197 dma_addr_t dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001198
1199 /* Ports using BM pool */
1200 u32 port_map;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001201};
1202
Antoine Tenart20920262017-10-23 15:24:30 +02001203#define IS_TSO_HEADER(txq_pcpu, addr) \
1204 ((addr) >= (txq_pcpu)->tso_headers_dma && \
1205 (addr) < (txq_pcpu)->tso_headers_dma + \
1206 (txq_pcpu)->size * TSO_HEADER_SIZE)
1207
Thomas Petazzoni213f4282017-08-03 10:42:00 +02001208/* Queue modes */
1209#define MVPP2_QDIST_SINGLE_MODE 0
1210#define MVPP2_QDIST_MULTI_MODE 1
1211
1212static int queue_mode = MVPP2_QDIST_SINGLE_MODE;
1213
1214module_param(queue_mode, int, 0444);
1215MODULE_PARM_DESC(queue_mode, "Set queue_mode (single=0, multi=1)");
1216
Marcin Wojtas3f518502014-07-10 16:52:13 -03001217#define MVPP2_DRIVER_NAME "mvpp2"
1218#define MVPP2_DRIVER_VERSION "1.0"
1219
1220/* Utility/helper methods */
1221
1222static void mvpp2_write(struct mvpp2 *priv, u32 offset, u32 data)
1223{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001224 writel(data, priv->swth_base[0] + offset);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001225}
1226
1227static u32 mvpp2_read(struct mvpp2 *priv, u32 offset)
1228{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001229 return readl(priv->swth_base[0] + offset);
Thomas Petazzonia7868412017-03-07 16:53:13 +01001230}
1231
1232/* These accessors should be used to access:
1233 *
1234 * - per-CPU registers, where each CPU has its own copy of the
1235 * register.
1236 *
1237 * MVPP2_BM_VIRT_ALLOC_REG
1238 * MVPP2_BM_ADDR_HIGH_ALLOC
1239 * MVPP22_BM_ADDR_HIGH_RLS_REG
1240 * MVPP2_BM_VIRT_RLS_REG
1241 * MVPP2_ISR_RX_TX_CAUSE_REG
1242 * MVPP2_ISR_RX_TX_MASK_REG
1243 * MVPP2_TXQ_NUM_REG
1244 * MVPP2_AGGR_TXQ_UPDATE_REG
1245 * MVPP2_TXQ_RSVD_REQ_REG
1246 * MVPP2_TXQ_RSVD_RSLT_REG
1247 * MVPP2_TXQ_SENT_REG
1248 * MVPP2_RXQ_NUM_REG
1249 *
1250 * - global registers that must be accessed through a specific CPU
1251 * window, because they are related to an access to a per-CPU
1252 * register
1253 *
1254 * MVPP2_BM_PHY_ALLOC_REG (related to MVPP2_BM_VIRT_ALLOC_REG)
1255 * MVPP2_BM_PHY_RLS_REG (related to MVPP2_BM_VIRT_RLS_REG)
1256 * MVPP2_RXQ_THRESH_REG (related to MVPP2_RXQ_NUM_REG)
1257 * MVPP2_RXQ_DESC_ADDR_REG (related to MVPP2_RXQ_NUM_REG)
1258 * MVPP2_RXQ_DESC_SIZE_REG (related to MVPP2_RXQ_NUM_REG)
1259 * MVPP2_RXQ_INDEX_REG (related to MVPP2_RXQ_NUM_REG)
1260 * MVPP2_TXQ_PENDING_REG (related to MVPP2_TXQ_NUM_REG)
1261 * MVPP2_TXQ_DESC_ADDR_REG (related to MVPP2_TXQ_NUM_REG)
1262 * MVPP2_TXQ_DESC_SIZE_REG (related to MVPP2_TXQ_NUM_REG)
1263 * MVPP2_TXQ_INDEX_REG (related to MVPP2_TXQ_NUM_REG)
1264 * MVPP2_TXQ_PENDING_REG (related to MVPP2_TXQ_NUM_REG)
1265 * MVPP2_TXQ_PREF_BUF_REG (related to MVPP2_TXQ_NUM_REG)
1266 * MVPP2_TXQ_PREF_BUF_REG (related to MVPP2_TXQ_NUM_REG)
1267 */
1268static void mvpp2_percpu_write(struct mvpp2 *priv, int cpu,
1269 u32 offset, u32 data)
1270{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001271 writel(data, priv->swth_base[cpu] + offset);
Thomas Petazzonia7868412017-03-07 16:53:13 +01001272}
1273
1274static u32 mvpp2_percpu_read(struct mvpp2 *priv, int cpu,
1275 u32 offset)
1276{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001277 return readl(priv->swth_base[cpu] + offset);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001278}
1279
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001280static dma_addr_t mvpp2_txdesc_dma_addr_get(struct mvpp2_port *port,
1281 struct mvpp2_tx_desc *tx_desc)
1282{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001283 if (port->priv->hw_version == MVPP21)
1284 return tx_desc->pp21.buf_dma_addr;
1285 else
1286 return tx_desc->pp22.buf_dma_addr_ptp & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001287}
1288
1289static void mvpp2_txdesc_dma_addr_set(struct mvpp2_port *port,
1290 struct mvpp2_tx_desc *tx_desc,
1291 dma_addr_t dma_addr)
1292{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001293 if (port->priv->hw_version == MVPP21) {
1294 tx_desc->pp21.buf_dma_addr = dma_addr;
1295 } else {
1296 u64 val = (u64)dma_addr;
1297
1298 tx_desc->pp22.buf_dma_addr_ptp &= ~GENMASK_ULL(40, 0);
1299 tx_desc->pp22.buf_dma_addr_ptp |= val;
1300 }
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001301}
1302
1303static size_t mvpp2_txdesc_size_get(struct mvpp2_port *port,
1304 struct mvpp2_tx_desc *tx_desc)
1305{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001306 if (port->priv->hw_version == MVPP21)
1307 return tx_desc->pp21.data_size;
1308 else
1309 return tx_desc->pp22.data_size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001310}
1311
1312static void mvpp2_txdesc_size_set(struct mvpp2_port *port,
1313 struct mvpp2_tx_desc *tx_desc,
1314 size_t size)
1315{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001316 if (port->priv->hw_version == MVPP21)
1317 tx_desc->pp21.data_size = size;
1318 else
1319 tx_desc->pp22.data_size = size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001320}
1321
1322static void mvpp2_txdesc_txq_set(struct mvpp2_port *port,
1323 struct mvpp2_tx_desc *tx_desc,
1324 unsigned int txq)
1325{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001326 if (port->priv->hw_version == MVPP21)
1327 tx_desc->pp21.phys_txq = txq;
1328 else
1329 tx_desc->pp22.phys_txq = txq;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001330}
1331
1332static void mvpp2_txdesc_cmd_set(struct mvpp2_port *port,
1333 struct mvpp2_tx_desc *tx_desc,
1334 unsigned int command)
1335{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001336 if (port->priv->hw_version == MVPP21)
1337 tx_desc->pp21.command = command;
1338 else
1339 tx_desc->pp22.command = command;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001340}
1341
1342static void mvpp2_txdesc_offset_set(struct mvpp2_port *port,
1343 struct mvpp2_tx_desc *tx_desc,
1344 unsigned int offset)
1345{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001346 if (port->priv->hw_version == MVPP21)
1347 tx_desc->pp21.packet_offset = offset;
1348 else
1349 tx_desc->pp22.packet_offset = offset;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001350}
1351
1352static unsigned int mvpp2_txdesc_offset_get(struct mvpp2_port *port,
1353 struct mvpp2_tx_desc *tx_desc)
1354{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001355 if (port->priv->hw_version == MVPP21)
1356 return tx_desc->pp21.packet_offset;
1357 else
1358 return tx_desc->pp22.packet_offset;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001359}
1360
1361static dma_addr_t mvpp2_rxdesc_dma_addr_get(struct mvpp2_port *port,
1362 struct mvpp2_rx_desc *rx_desc)
1363{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001364 if (port->priv->hw_version == MVPP21)
1365 return rx_desc->pp21.buf_dma_addr;
1366 else
1367 return rx_desc->pp22.buf_dma_addr_key_hash & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001368}
1369
1370static unsigned long mvpp2_rxdesc_cookie_get(struct mvpp2_port *port,
1371 struct mvpp2_rx_desc *rx_desc)
1372{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001373 if (port->priv->hw_version == MVPP21)
1374 return rx_desc->pp21.buf_cookie;
1375 else
1376 return rx_desc->pp22.buf_cookie_misc & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001377}
1378
1379static size_t mvpp2_rxdesc_size_get(struct mvpp2_port *port,
1380 struct mvpp2_rx_desc *rx_desc)
1381{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001382 if (port->priv->hw_version == MVPP21)
1383 return rx_desc->pp21.data_size;
1384 else
1385 return rx_desc->pp22.data_size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001386}
1387
1388static u32 mvpp2_rxdesc_status_get(struct mvpp2_port *port,
1389 struct mvpp2_rx_desc *rx_desc)
1390{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001391 if (port->priv->hw_version == MVPP21)
1392 return rx_desc->pp21.status;
1393 else
1394 return rx_desc->pp22.status;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001395}
1396
Marcin Wojtas3f518502014-07-10 16:52:13 -03001397static void mvpp2_txq_inc_get(struct mvpp2_txq_pcpu *txq_pcpu)
1398{
1399 txq_pcpu->txq_get_index++;
1400 if (txq_pcpu->txq_get_index == txq_pcpu->size)
1401 txq_pcpu->txq_get_index = 0;
1402}
1403
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001404static void mvpp2_txq_inc_put(struct mvpp2_port *port,
1405 struct mvpp2_txq_pcpu *txq_pcpu,
Marcin Wojtas71ce3912015-08-06 19:00:29 +02001406 struct sk_buff *skb,
1407 struct mvpp2_tx_desc *tx_desc)
Marcin Wojtas3f518502014-07-10 16:52:13 -03001408{
Thomas Petazzoni83544912016-12-21 11:28:49 +01001409 struct mvpp2_txq_pcpu_buf *tx_buf =
1410 txq_pcpu->buffs + txq_pcpu->txq_put_index;
1411 tx_buf->skb = skb;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001412 tx_buf->size = mvpp2_txdesc_size_get(port, tx_desc);
1413 tx_buf->dma = mvpp2_txdesc_dma_addr_get(port, tx_desc) +
1414 mvpp2_txdesc_offset_get(port, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001415 txq_pcpu->txq_put_index++;
1416 if (txq_pcpu->txq_put_index == txq_pcpu->size)
1417 txq_pcpu->txq_put_index = 0;
1418}
1419
1420/* Get number of physical egress port */
1421static inline int mvpp2_egress_port(struct mvpp2_port *port)
1422{
1423 return MVPP2_MAX_TCONT + port->id;
1424}
1425
1426/* Get number of physical TXQ */
1427static inline int mvpp2_txq_phys(int port, int txq)
1428{
1429 return (MVPP2_MAX_TCONT + port) * MVPP2_MAX_TXQ + txq;
1430}
1431
1432/* Parser configuration routines */
1433
1434/* Update parser tcam and sram hw entries */
1435static int mvpp2_prs_hw_write(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1436{
1437 int i;
1438
1439 if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1440 return -EINVAL;
1441
1442 /* Clear entry invalidation bit */
1443 pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] &= ~MVPP2_PRS_TCAM_INV_MASK;
1444
1445 /* Write tcam index - indirect access */
1446 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1447 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1448 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), pe->tcam.word[i]);
1449
1450 /* Write sram index - indirect access */
1451 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1452 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1453 mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), pe->sram.word[i]);
1454
1455 return 0;
1456}
1457
1458/* Read tcam entry from hw */
1459static int mvpp2_prs_hw_read(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1460{
1461 int i;
1462
1463 if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1464 return -EINVAL;
1465
1466 /* Write tcam index - indirect access */
1467 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1468
1469 pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] = mvpp2_read(priv,
1470 MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD));
1471 if (pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] & MVPP2_PRS_TCAM_INV_MASK)
1472 return MVPP2_PRS_TCAM_ENTRY_INVALID;
1473
1474 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1475 pe->tcam.word[i] = mvpp2_read(priv, MVPP2_PRS_TCAM_DATA_REG(i));
1476
1477 /* Write sram index - indirect access */
1478 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1479 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1480 pe->sram.word[i] = mvpp2_read(priv, MVPP2_PRS_SRAM_DATA_REG(i));
1481
1482 return 0;
1483}
1484
1485/* Invalidate tcam hw entry */
1486static void mvpp2_prs_hw_inv(struct mvpp2 *priv, int index)
1487{
1488 /* Write index - indirect access */
1489 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
1490 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD),
1491 MVPP2_PRS_TCAM_INV_MASK);
1492}
1493
1494/* Enable shadow table entry and set its lookup ID */
1495static void mvpp2_prs_shadow_set(struct mvpp2 *priv, int index, int lu)
1496{
1497 priv->prs_shadow[index].valid = true;
1498 priv->prs_shadow[index].lu = lu;
1499}
1500
1501/* Update ri fields in shadow table entry */
1502static void mvpp2_prs_shadow_ri_set(struct mvpp2 *priv, int index,
1503 unsigned int ri, unsigned int ri_mask)
1504{
1505 priv->prs_shadow[index].ri_mask = ri_mask;
1506 priv->prs_shadow[index].ri = ri;
1507}
1508
1509/* Update lookup field in tcam sw entry */
1510static void mvpp2_prs_tcam_lu_set(struct mvpp2_prs_entry *pe, unsigned int lu)
1511{
1512 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_LU_BYTE);
1513
1514 pe->tcam.byte[MVPP2_PRS_TCAM_LU_BYTE] = lu;
1515 pe->tcam.byte[enable_off] = MVPP2_PRS_LU_MASK;
1516}
1517
1518/* Update mask for single port in tcam sw entry */
1519static void mvpp2_prs_tcam_port_set(struct mvpp2_prs_entry *pe,
1520 unsigned int port, bool add)
1521{
1522 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1523
1524 if (add)
1525 pe->tcam.byte[enable_off] &= ~(1 << port);
1526 else
1527 pe->tcam.byte[enable_off] |= 1 << port;
1528}
1529
1530/* Update port map in tcam sw entry */
1531static void mvpp2_prs_tcam_port_map_set(struct mvpp2_prs_entry *pe,
1532 unsigned int ports)
1533{
1534 unsigned char port_mask = MVPP2_PRS_PORT_MASK;
1535 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1536
1537 pe->tcam.byte[MVPP2_PRS_TCAM_PORT_BYTE] = 0;
1538 pe->tcam.byte[enable_off] &= ~port_mask;
1539 pe->tcam.byte[enable_off] |= ~ports & MVPP2_PRS_PORT_MASK;
1540}
1541
1542/* Obtain port map from tcam sw entry */
1543static unsigned int mvpp2_prs_tcam_port_map_get(struct mvpp2_prs_entry *pe)
1544{
1545 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1546
1547 return ~(pe->tcam.byte[enable_off]) & MVPP2_PRS_PORT_MASK;
1548}
1549
1550/* Set byte of data and its enable bits in tcam sw entry */
1551static void mvpp2_prs_tcam_data_byte_set(struct mvpp2_prs_entry *pe,
1552 unsigned int offs, unsigned char byte,
1553 unsigned char enable)
1554{
1555 pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)] = byte;
1556 pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)] = enable;
1557}
1558
1559/* Get byte of data and its enable bits from tcam sw entry */
1560static void mvpp2_prs_tcam_data_byte_get(struct mvpp2_prs_entry *pe,
1561 unsigned int offs, unsigned char *byte,
1562 unsigned char *enable)
1563{
1564 *byte = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)];
1565 *enable = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)];
1566}
1567
1568/* Compare tcam data bytes with a pattern */
1569static bool mvpp2_prs_tcam_data_cmp(struct mvpp2_prs_entry *pe, int offs,
1570 u16 data)
1571{
1572 int off = MVPP2_PRS_TCAM_DATA_BYTE(offs);
1573 u16 tcam_data;
1574
Antoine Tenartef4816f2017-10-24 11:41:26 +02001575 tcam_data = (pe->tcam.byte[off + 1] << 8) | pe->tcam.byte[off];
Marcin Wojtas3f518502014-07-10 16:52:13 -03001576 if (tcam_data != data)
1577 return false;
1578 return true;
1579}
1580
1581/* Update ai bits in tcam sw entry */
1582static void mvpp2_prs_tcam_ai_update(struct mvpp2_prs_entry *pe,
1583 unsigned int bits, unsigned int enable)
1584{
1585 int i, ai_idx = MVPP2_PRS_TCAM_AI_BYTE;
1586
1587 for (i = 0; i < MVPP2_PRS_AI_BITS; i++) {
1588
1589 if (!(enable & BIT(i)))
1590 continue;
1591
1592 if (bits & BIT(i))
1593 pe->tcam.byte[ai_idx] |= 1 << i;
1594 else
1595 pe->tcam.byte[ai_idx] &= ~(1 << i);
1596 }
1597
1598 pe->tcam.byte[MVPP2_PRS_TCAM_EN_OFFS(ai_idx)] |= enable;
1599}
1600
1601/* Get ai bits from tcam sw entry */
1602static int mvpp2_prs_tcam_ai_get(struct mvpp2_prs_entry *pe)
1603{
1604 return pe->tcam.byte[MVPP2_PRS_TCAM_AI_BYTE];
1605}
1606
1607/* Set ethertype in tcam sw entry */
1608static void mvpp2_prs_match_etype(struct mvpp2_prs_entry *pe, int offset,
1609 unsigned short ethertype)
1610{
1611 mvpp2_prs_tcam_data_byte_set(pe, offset + 0, ethertype >> 8, 0xff);
1612 mvpp2_prs_tcam_data_byte_set(pe, offset + 1, ethertype & 0xff, 0xff);
1613}
1614
1615/* Set bits in sram sw entry */
1616static void mvpp2_prs_sram_bits_set(struct mvpp2_prs_entry *pe, int bit_num,
1617 int val)
1618{
1619 pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] |= (val << (bit_num % 8));
1620}
1621
1622/* Clear bits in sram sw entry */
1623static void mvpp2_prs_sram_bits_clear(struct mvpp2_prs_entry *pe, int bit_num,
1624 int val)
1625{
1626 pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] &= ~(val << (bit_num % 8));
1627}
1628
1629/* Update ri bits in sram sw entry */
1630static void mvpp2_prs_sram_ri_update(struct mvpp2_prs_entry *pe,
1631 unsigned int bits, unsigned int mask)
1632{
1633 unsigned int i;
1634
1635 for (i = 0; i < MVPP2_PRS_SRAM_RI_CTRL_BITS; i++) {
1636 int ri_off = MVPP2_PRS_SRAM_RI_OFFS;
1637
1638 if (!(mask & BIT(i)))
1639 continue;
1640
1641 if (bits & BIT(i))
1642 mvpp2_prs_sram_bits_set(pe, ri_off + i, 1);
1643 else
1644 mvpp2_prs_sram_bits_clear(pe, ri_off + i, 1);
1645
1646 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_RI_CTRL_OFFS + i, 1);
1647 }
1648}
1649
1650/* Obtain ri bits from sram sw entry */
1651static int mvpp2_prs_sram_ri_get(struct mvpp2_prs_entry *pe)
1652{
1653 return pe->sram.word[MVPP2_PRS_SRAM_RI_WORD];
1654}
1655
1656/* Update ai bits in sram sw entry */
1657static void mvpp2_prs_sram_ai_update(struct mvpp2_prs_entry *pe,
1658 unsigned int bits, unsigned int mask)
1659{
1660 unsigned int i;
1661 int ai_off = MVPP2_PRS_SRAM_AI_OFFS;
1662
1663 for (i = 0; i < MVPP2_PRS_SRAM_AI_CTRL_BITS; i++) {
1664
1665 if (!(mask & BIT(i)))
1666 continue;
1667
1668 if (bits & BIT(i))
1669 mvpp2_prs_sram_bits_set(pe, ai_off + i, 1);
1670 else
1671 mvpp2_prs_sram_bits_clear(pe, ai_off + i, 1);
1672
1673 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_AI_CTRL_OFFS + i, 1);
1674 }
1675}
1676
1677/* Read ai bits from sram sw entry */
1678static int mvpp2_prs_sram_ai_get(struct mvpp2_prs_entry *pe)
1679{
1680 u8 bits;
1681 int ai_off = MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_AI_OFFS);
1682 int ai_en_off = ai_off + 1;
1683 int ai_shift = MVPP2_PRS_SRAM_AI_OFFS % 8;
1684
1685 bits = (pe->sram.byte[ai_off] >> ai_shift) |
1686 (pe->sram.byte[ai_en_off] << (8 - ai_shift));
1687
1688 return bits;
1689}
1690
1691/* In sram sw entry set lookup ID field of the tcam key to be used in the next
1692 * lookup interation
1693 */
1694static void mvpp2_prs_sram_next_lu_set(struct mvpp2_prs_entry *pe,
1695 unsigned int lu)
1696{
1697 int sram_next_off = MVPP2_PRS_SRAM_NEXT_LU_OFFS;
1698
1699 mvpp2_prs_sram_bits_clear(pe, sram_next_off,
1700 MVPP2_PRS_SRAM_NEXT_LU_MASK);
1701 mvpp2_prs_sram_bits_set(pe, sram_next_off, lu);
1702}
1703
1704/* In the sram sw entry set sign and value of the next lookup offset
1705 * and the offset value generated to the classifier
1706 */
1707static void mvpp2_prs_sram_shift_set(struct mvpp2_prs_entry *pe, int shift,
1708 unsigned int op)
1709{
1710 /* Set sign */
1711 if (shift < 0) {
1712 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1713 shift = 0 - shift;
1714 } else {
1715 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1716 }
1717
1718 /* Set value */
1719 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_SHIFT_OFFS)] =
1720 (unsigned char)shift;
1721
1722 /* Reset and set operation */
1723 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS,
1724 MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK);
1725 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS, op);
1726
1727 /* Set base offset as current */
1728 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1729}
1730
1731/* In the sram sw entry set sign and value of the user defined offset
1732 * generated to the classifier
1733 */
1734static void mvpp2_prs_sram_offset_set(struct mvpp2_prs_entry *pe,
1735 unsigned int type, int offset,
1736 unsigned int op)
1737{
1738 /* Set sign */
1739 if (offset < 0) {
1740 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1741 offset = 0 - offset;
1742 } else {
1743 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1744 }
1745
1746 /* Set value */
1747 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_OFFS,
1748 MVPP2_PRS_SRAM_UDF_MASK);
1749 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_OFFS, offset);
1750 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1751 MVPP2_PRS_SRAM_UDF_BITS)] &=
1752 ~(MVPP2_PRS_SRAM_UDF_MASK >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1753 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1754 MVPP2_PRS_SRAM_UDF_BITS)] |=
1755 (offset >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1756
1757 /* Set offset type */
1758 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS,
1759 MVPP2_PRS_SRAM_UDF_TYPE_MASK);
1760 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS, type);
1761
1762 /* Set offset operation */
1763 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS,
1764 MVPP2_PRS_SRAM_OP_SEL_UDF_MASK);
1765 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS, op);
1766
1767 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1768 MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] &=
1769 ~(MVPP2_PRS_SRAM_OP_SEL_UDF_MASK >>
1770 (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1771
1772 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1773 MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] |=
1774 (op >> (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1775
1776 /* Set base offset as current */
1777 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1778}
1779
1780/* Find parser flow entry */
1781static struct mvpp2_prs_entry *mvpp2_prs_flow_find(struct mvpp2 *priv, int flow)
1782{
1783 struct mvpp2_prs_entry *pe;
1784 int tid;
1785
1786 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
1787 if (!pe)
1788 return NULL;
1789 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
1790
1791 /* Go through the all entires with MVPP2_PRS_LU_FLOWS */
1792 for (tid = MVPP2_PRS_TCAM_SRAM_SIZE - 1; tid >= 0; tid--) {
1793 u8 bits;
1794
1795 if (!priv->prs_shadow[tid].valid ||
1796 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_FLOWS)
1797 continue;
1798
1799 pe->index = tid;
1800 mvpp2_prs_hw_read(priv, pe);
1801 bits = mvpp2_prs_sram_ai_get(pe);
1802
1803 /* Sram store classification lookup ID in AI bits [5:0] */
1804 if ((bits & MVPP2_PRS_FLOW_ID_MASK) == flow)
1805 return pe;
1806 }
1807 kfree(pe);
1808
1809 return NULL;
1810}
1811
1812/* Return first free tcam index, seeking from start to end */
1813static int mvpp2_prs_tcam_first_free(struct mvpp2 *priv, unsigned char start,
1814 unsigned char end)
1815{
1816 int tid;
1817
1818 if (start > end)
1819 swap(start, end);
1820
1821 if (end >= MVPP2_PRS_TCAM_SRAM_SIZE)
1822 end = MVPP2_PRS_TCAM_SRAM_SIZE - 1;
1823
1824 for (tid = start; tid <= end; tid++) {
1825 if (!priv->prs_shadow[tid].valid)
1826 return tid;
1827 }
1828
1829 return -EINVAL;
1830}
1831
1832/* Enable/disable dropping all mac da's */
1833static void mvpp2_prs_mac_drop_all_set(struct mvpp2 *priv, int port, bool add)
1834{
1835 struct mvpp2_prs_entry pe;
1836
1837 if (priv->prs_shadow[MVPP2_PE_DROP_ALL].valid) {
1838 /* Entry exist - update port only */
1839 pe.index = MVPP2_PE_DROP_ALL;
1840 mvpp2_prs_hw_read(priv, &pe);
1841 } else {
1842 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001843 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001844 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1845 pe.index = MVPP2_PE_DROP_ALL;
1846
1847 /* Non-promiscuous mode for all ports - DROP unknown packets */
1848 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
1849 MVPP2_PRS_RI_DROP_MASK);
1850
1851 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1852 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1853
1854 /* Update shadow table */
1855 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1856
1857 /* Mask all ports */
1858 mvpp2_prs_tcam_port_map_set(&pe, 0);
1859 }
1860
1861 /* Update port mask */
1862 mvpp2_prs_tcam_port_set(&pe, port, add);
1863
1864 mvpp2_prs_hw_write(priv, &pe);
1865}
1866
1867/* Set port to promiscuous mode */
1868static void mvpp2_prs_mac_promisc_set(struct mvpp2 *priv, int port, bool add)
1869{
1870 struct mvpp2_prs_entry pe;
1871
Joe Perchesdbedd442015-03-06 20:49:12 -08001872 /* Promiscuous mode - Accept unknown packets */
Marcin Wojtas3f518502014-07-10 16:52:13 -03001873
1874 if (priv->prs_shadow[MVPP2_PE_MAC_PROMISCUOUS].valid) {
1875 /* Entry exist - update port only */
1876 pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1877 mvpp2_prs_hw_read(priv, &pe);
1878 } else {
1879 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001880 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001881 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1882 pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1883
1884 /* Continue - set next lookup */
1885 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
1886
1887 /* Set result info bits */
1888 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_UCAST,
1889 MVPP2_PRS_RI_L2_CAST_MASK);
1890
1891 /* Shift to ethertype */
1892 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
1893 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1894
1895 /* Mask all ports */
1896 mvpp2_prs_tcam_port_map_set(&pe, 0);
1897
1898 /* Update shadow table */
1899 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1900 }
1901
1902 /* Update port mask */
1903 mvpp2_prs_tcam_port_set(&pe, port, add);
1904
1905 mvpp2_prs_hw_write(priv, &pe);
1906}
1907
1908/* Accept multicast */
1909static void mvpp2_prs_mac_multi_set(struct mvpp2 *priv, int port, int index,
1910 bool add)
1911{
1912 struct mvpp2_prs_entry pe;
1913 unsigned char da_mc;
1914
1915 /* Ethernet multicast address first byte is
1916 * 0x01 for IPv4 and 0x33 for IPv6
1917 */
1918 da_mc = (index == MVPP2_PE_MAC_MC_ALL) ? 0x01 : 0x33;
1919
1920 if (priv->prs_shadow[index].valid) {
1921 /* Entry exist - update port only */
1922 pe.index = index;
1923 mvpp2_prs_hw_read(priv, &pe);
1924 } else {
1925 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001926 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001927 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1928 pe.index = index;
1929
1930 /* Continue - set next lookup */
1931 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
1932
1933 /* Set result info bits */
1934 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_MCAST,
1935 MVPP2_PRS_RI_L2_CAST_MASK);
1936
1937 /* Update tcam entry data first byte */
1938 mvpp2_prs_tcam_data_byte_set(&pe, 0, da_mc, 0xff);
1939
1940 /* Shift to ethertype */
1941 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
1942 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1943
1944 /* Mask all ports */
1945 mvpp2_prs_tcam_port_map_set(&pe, 0);
1946
1947 /* Update shadow table */
1948 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1949 }
1950
1951 /* Update port mask */
1952 mvpp2_prs_tcam_port_set(&pe, port, add);
1953
1954 mvpp2_prs_hw_write(priv, &pe);
1955}
1956
1957/* Set entry for dsa packets */
1958static void mvpp2_prs_dsa_tag_set(struct mvpp2 *priv, int port, bool add,
1959 bool tagged, bool extend)
1960{
1961 struct mvpp2_prs_entry pe;
1962 int tid, shift;
1963
1964 if (extend) {
1965 tid = tagged ? MVPP2_PE_EDSA_TAGGED : MVPP2_PE_EDSA_UNTAGGED;
1966 shift = 8;
1967 } else {
1968 tid = tagged ? MVPP2_PE_DSA_TAGGED : MVPP2_PE_DSA_UNTAGGED;
1969 shift = 4;
1970 }
1971
1972 if (priv->prs_shadow[tid].valid) {
1973 /* Entry exist - update port only */
1974 pe.index = tid;
1975 mvpp2_prs_hw_read(priv, &pe);
1976 } else {
1977 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001978 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001979 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
1980 pe.index = tid;
1981
1982 /* Shift 4 bytes if DSA tag or 8 bytes in case of EDSA tag*/
1983 mvpp2_prs_sram_shift_set(&pe, shift,
1984 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1985
1986 /* Update shadow table */
1987 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
1988
1989 if (tagged) {
1990 /* Set tagged bit in DSA tag */
1991 mvpp2_prs_tcam_data_byte_set(&pe, 0,
1992 MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
1993 MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
1994 /* Clear all ai bits for next iteration */
1995 mvpp2_prs_sram_ai_update(&pe, 0,
1996 MVPP2_PRS_SRAM_AI_MASK);
1997 /* If packet is tagged continue check vlans */
1998 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
1999 } else {
2000 /* Set result info bits to 'no vlans' */
2001 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
2002 MVPP2_PRS_RI_VLAN_MASK);
2003 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2004 }
2005
2006 /* Mask all ports */
2007 mvpp2_prs_tcam_port_map_set(&pe, 0);
2008 }
2009
2010 /* Update port mask */
2011 mvpp2_prs_tcam_port_set(&pe, port, add);
2012
2013 mvpp2_prs_hw_write(priv, &pe);
2014}
2015
2016/* Set entry for dsa ethertype */
2017static void mvpp2_prs_dsa_tag_ethertype_set(struct mvpp2 *priv, int port,
2018 bool add, bool tagged, bool extend)
2019{
2020 struct mvpp2_prs_entry pe;
2021 int tid, shift, port_mask;
2022
2023 if (extend) {
2024 tid = tagged ? MVPP2_PE_ETYPE_EDSA_TAGGED :
2025 MVPP2_PE_ETYPE_EDSA_UNTAGGED;
2026 port_mask = 0;
2027 shift = 8;
2028 } else {
2029 tid = tagged ? MVPP2_PE_ETYPE_DSA_TAGGED :
2030 MVPP2_PE_ETYPE_DSA_UNTAGGED;
2031 port_mask = MVPP2_PRS_PORT_MASK;
2032 shift = 4;
2033 }
2034
2035 if (priv->prs_shadow[tid].valid) {
2036 /* Entry exist - update port only */
2037 pe.index = tid;
2038 mvpp2_prs_hw_read(priv, &pe);
2039 } else {
2040 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002041 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002042 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
2043 pe.index = tid;
2044
2045 /* Set ethertype */
2046 mvpp2_prs_match_etype(&pe, 0, ETH_P_EDSA);
2047 mvpp2_prs_match_etype(&pe, 2, 0);
2048
2049 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DSA_MASK,
2050 MVPP2_PRS_RI_DSA_MASK);
2051 /* Shift ethertype + 2 byte reserved + tag*/
2052 mvpp2_prs_sram_shift_set(&pe, 2 + MVPP2_ETH_TYPE_LEN + shift,
2053 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2054
2055 /* Update shadow table */
2056 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
2057
2058 if (tagged) {
2059 /* Set tagged bit in DSA tag */
2060 mvpp2_prs_tcam_data_byte_set(&pe,
2061 MVPP2_ETH_TYPE_LEN + 2 + 3,
2062 MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
2063 MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
2064 /* Clear all ai bits for next iteration */
2065 mvpp2_prs_sram_ai_update(&pe, 0,
2066 MVPP2_PRS_SRAM_AI_MASK);
2067 /* If packet is tagged continue check vlans */
2068 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2069 } else {
2070 /* Set result info bits to 'no vlans' */
2071 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
2072 MVPP2_PRS_RI_VLAN_MASK);
2073 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2074 }
2075 /* Mask/unmask all ports, depending on dsa type */
2076 mvpp2_prs_tcam_port_map_set(&pe, port_mask);
2077 }
2078
2079 /* Update port mask */
2080 mvpp2_prs_tcam_port_set(&pe, port, add);
2081
2082 mvpp2_prs_hw_write(priv, &pe);
2083}
2084
2085/* Search for existing single/triple vlan entry */
2086static struct mvpp2_prs_entry *mvpp2_prs_vlan_find(struct mvpp2 *priv,
2087 unsigned short tpid, int ai)
2088{
2089 struct mvpp2_prs_entry *pe;
2090 int tid;
2091
2092 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2093 if (!pe)
2094 return NULL;
2095 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2096
2097 /* Go through the all entries with MVPP2_PRS_LU_VLAN */
2098 for (tid = MVPP2_PE_FIRST_FREE_TID;
2099 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
2100 unsigned int ri_bits, ai_bits;
2101 bool match;
2102
2103 if (!priv->prs_shadow[tid].valid ||
2104 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
2105 continue;
2106
2107 pe->index = tid;
2108
2109 mvpp2_prs_hw_read(priv, pe);
2110 match = mvpp2_prs_tcam_data_cmp(pe, 0, swab16(tpid));
2111 if (!match)
2112 continue;
2113
2114 /* Get vlan type */
2115 ri_bits = mvpp2_prs_sram_ri_get(pe);
2116 ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
2117
2118 /* Get current ai value from tcam */
2119 ai_bits = mvpp2_prs_tcam_ai_get(pe);
2120 /* Clear double vlan bit */
2121 ai_bits &= ~MVPP2_PRS_DBL_VLAN_AI_BIT;
2122
2123 if (ai != ai_bits)
2124 continue;
2125
2126 if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
2127 ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
2128 return pe;
2129 }
2130 kfree(pe);
2131
2132 return NULL;
2133}
2134
2135/* Add/update single/triple vlan entry */
2136static int mvpp2_prs_vlan_add(struct mvpp2 *priv, unsigned short tpid, int ai,
2137 unsigned int port_map)
2138{
2139 struct mvpp2_prs_entry *pe;
2140 int tid_aux, tid;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302141 int ret = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002142
2143 pe = mvpp2_prs_vlan_find(priv, tpid, ai);
2144
2145 if (!pe) {
2146 /* Create new tcam entry */
2147 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_LAST_FREE_TID,
2148 MVPP2_PE_FIRST_FREE_TID);
2149 if (tid < 0)
2150 return tid;
2151
2152 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2153 if (!pe)
2154 return -ENOMEM;
2155
2156 /* Get last double vlan tid */
2157 for (tid_aux = MVPP2_PE_LAST_FREE_TID;
2158 tid_aux >= MVPP2_PE_FIRST_FREE_TID; tid_aux--) {
2159 unsigned int ri_bits;
2160
2161 if (!priv->prs_shadow[tid_aux].valid ||
2162 priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
2163 continue;
2164
2165 pe->index = tid_aux;
2166 mvpp2_prs_hw_read(priv, pe);
2167 ri_bits = mvpp2_prs_sram_ri_get(pe);
2168 if ((ri_bits & MVPP2_PRS_RI_VLAN_MASK) ==
2169 MVPP2_PRS_RI_VLAN_DOUBLE)
2170 break;
2171 }
2172
Sudip Mukherjee43737472014-11-01 16:59:34 +05302173 if (tid <= tid_aux) {
2174 ret = -EINVAL;
Markus Elfringf9fd0e32017-04-17 13:50:35 +02002175 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302176 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002177
Markus Elfringbd6aaf52017-04-17 10:40:32 +02002178 memset(pe, 0, sizeof(*pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002179 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2180 pe->index = tid;
2181
2182 mvpp2_prs_match_etype(pe, 0, tpid);
2183
2184 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_L2);
2185 /* Shift 4 bytes - skip 1 vlan tag */
2186 mvpp2_prs_sram_shift_set(pe, MVPP2_VLAN_TAG_LEN,
2187 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2188 /* Clear all ai bits for next iteration */
2189 mvpp2_prs_sram_ai_update(pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2190
2191 if (ai == MVPP2_PRS_SINGLE_VLAN_AI) {
2192 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_SINGLE,
2193 MVPP2_PRS_RI_VLAN_MASK);
2194 } else {
2195 ai |= MVPP2_PRS_DBL_VLAN_AI_BIT;
2196 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_TRIPLE,
2197 MVPP2_PRS_RI_VLAN_MASK);
2198 }
2199 mvpp2_prs_tcam_ai_update(pe, ai, MVPP2_PRS_SRAM_AI_MASK);
2200
2201 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_VLAN);
2202 }
2203 /* Update ports' mask */
2204 mvpp2_prs_tcam_port_map_set(pe, port_map);
2205
2206 mvpp2_prs_hw_write(priv, pe);
Markus Elfringf9fd0e32017-04-17 13:50:35 +02002207free_pe:
Marcin Wojtas3f518502014-07-10 16:52:13 -03002208 kfree(pe);
2209
Sudip Mukherjee43737472014-11-01 16:59:34 +05302210 return ret;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002211}
2212
2213/* Get first free double vlan ai number */
2214static int mvpp2_prs_double_vlan_ai_free_get(struct mvpp2 *priv)
2215{
2216 int i;
2217
2218 for (i = 1; i < MVPP2_PRS_DBL_VLANS_MAX; i++) {
2219 if (!priv->prs_double_vlans[i])
2220 return i;
2221 }
2222
2223 return -EINVAL;
2224}
2225
2226/* Search for existing double vlan entry */
2227static struct mvpp2_prs_entry *mvpp2_prs_double_vlan_find(struct mvpp2 *priv,
2228 unsigned short tpid1,
2229 unsigned short tpid2)
2230{
2231 struct mvpp2_prs_entry *pe;
2232 int tid;
2233
2234 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2235 if (!pe)
2236 return NULL;
2237 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2238
2239 /* Go through the all entries with MVPP2_PRS_LU_VLAN */
2240 for (tid = MVPP2_PE_FIRST_FREE_TID;
2241 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
2242 unsigned int ri_mask;
2243 bool match;
2244
2245 if (!priv->prs_shadow[tid].valid ||
2246 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
2247 continue;
2248
2249 pe->index = tid;
2250 mvpp2_prs_hw_read(priv, pe);
2251
2252 match = mvpp2_prs_tcam_data_cmp(pe, 0, swab16(tpid1))
2253 && mvpp2_prs_tcam_data_cmp(pe, 4, swab16(tpid2));
2254
2255 if (!match)
2256 continue;
2257
2258 ri_mask = mvpp2_prs_sram_ri_get(pe) & MVPP2_PRS_RI_VLAN_MASK;
2259 if (ri_mask == MVPP2_PRS_RI_VLAN_DOUBLE)
2260 return pe;
2261 }
2262 kfree(pe);
2263
2264 return NULL;
2265}
2266
2267/* Add or update double vlan entry */
2268static int mvpp2_prs_double_vlan_add(struct mvpp2 *priv, unsigned short tpid1,
2269 unsigned short tpid2,
2270 unsigned int port_map)
2271{
2272 struct mvpp2_prs_entry *pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302273 int tid_aux, tid, ai, ret = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002274
2275 pe = mvpp2_prs_double_vlan_find(priv, tpid1, tpid2);
2276
2277 if (!pe) {
2278 /* Create new tcam entry */
2279 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2280 MVPP2_PE_LAST_FREE_TID);
2281 if (tid < 0)
2282 return tid;
2283
2284 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2285 if (!pe)
2286 return -ENOMEM;
2287
2288 /* Set ai value for new double vlan entry */
2289 ai = mvpp2_prs_double_vlan_ai_free_get(priv);
Sudip Mukherjee43737472014-11-01 16:59:34 +05302290 if (ai < 0) {
2291 ret = ai;
Markus Elfringc9a7e122017-04-17 13:03:49 +02002292 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302293 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002294
2295 /* Get first single/triple vlan tid */
2296 for (tid_aux = MVPP2_PE_FIRST_FREE_TID;
2297 tid_aux <= MVPP2_PE_LAST_FREE_TID; tid_aux++) {
2298 unsigned int ri_bits;
2299
2300 if (!priv->prs_shadow[tid_aux].valid ||
2301 priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
2302 continue;
2303
2304 pe->index = tid_aux;
2305 mvpp2_prs_hw_read(priv, pe);
2306 ri_bits = mvpp2_prs_sram_ri_get(pe);
2307 ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
2308 if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
2309 ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
2310 break;
2311 }
2312
Sudip Mukherjee43737472014-11-01 16:59:34 +05302313 if (tid >= tid_aux) {
2314 ret = -ERANGE;
Markus Elfringc9a7e122017-04-17 13:03:49 +02002315 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302316 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002317
Markus Elfringbd6aaf52017-04-17 10:40:32 +02002318 memset(pe, 0, sizeof(*pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002319 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2320 pe->index = tid;
2321
2322 priv->prs_double_vlans[ai] = true;
2323
2324 mvpp2_prs_match_etype(pe, 0, tpid1);
2325 mvpp2_prs_match_etype(pe, 4, tpid2);
2326
2327 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_VLAN);
2328 /* Shift 8 bytes - skip 2 vlan tags */
2329 mvpp2_prs_sram_shift_set(pe, 2 * MVPP2_VLAN_TAG_LEN,
2330 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2331 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_DOUBLE,
2332 MVPP2_PRS_RI_VLAN_MASK);
2333 mvpp2_prs_sram_ai_update(pe, ai | MVPP2_PRS_DBL_VLAN_AI_BIT,
2334 MVPP2_PRS_SRAM_AI_MASK);
2335
2336 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_VLAN);
2337 }
2338
2339 /* Update ports' mask */
2340 mvpp2_prs_tcam_port_map_set(pe, port_map);
2341 mvpp2_prs_hw_write(priv, pe);
Markus Elfringc9a7e122017-04-17 13:03:49 +02002342free_pe:
Marcin Wojtas3f518502014-07-10 16:52:13 -03002343 kfree(pe);
Sudip Mukherjee43737472014-11-01 16:59:34 +05302344 return ret;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002345}
2346
2347/* IPv4 header parsing for fragmentation and L4 offset */
2348static int mvpp2_prs_ip4_proto(struct mvpp2 *priv, unsigned short proto,
2349 unsigned int ri, unsigned int ri_mask)
2350{
2351 struct mvpp2_prs_entry pe;
2352 int tid;
2353
2354 if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
2355 (proto != IPPROTO_IGMP))
2356 return -EINVAL;
2357
Stefan Chulskiaff3da32017-09-25 14:59:46 +02002358 /* Not fragmented packet */
Marcin Wojtas3f518502014-07-10 16:52:13 -03002359 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2360 MVPP2_PE_LAST_FREE_TID);
2361 if (tid < 0)
2362 return tid;
2363
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002364 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002365 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
2366 pe.index = tid;
2367
2368 /* Set next lu to IPv4 */
2369 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2370 mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2371 /* Set L4 offset */
2372 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
2373 sizeof(struct iphdr) - 4,
2374 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2375 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
2376 MVPP2_PRS_IPV4_DIP_AI_BIT);
Stefan Chulskiaff3da32017-09-25 14:59:46 +02002377 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask | MVPP2_PRS_RI_IP_FRAG_MASK);
2378
2379 mvpp2_prs_tcam_data_byte_set(&pe, 2, 0x00,
2380 MVPP2_PRS_TCAM_PROTO_MASK_L);
2381 mvpp2_prs_tcam_data_byte_set(&pe, 3, 0x00,
2382 MVPP2_PRS_TCAM_PROTO_MASK);
Marcin Wojtas3f518502014-07-10 16:52:13 -03002383
2384 mvpp2_prs_tcam_data_byte_set(&pe, 5, proto, MVPP2_PRS_TCAM_PROTO_MASK);
2385 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
2386 /* Unmask all ports */
2387 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2388
2389 /* Update shadow table and hw entry */
2390 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2391 mvpp2_prs_hw_write(priv, &pe);
2392
Stefan Chulskiaff3da32017-09-25 14:59:46 +02002393 /* Fragmented packet */
Marcin Wojtas3f518502014-07-10 16:52:13 -03002394 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2395 MVPP2_PE_LAST_FREE_TID);
2396 if (tid < 0)
2397 return tid;
2398
2399 pe.index = tid;
2400 /* Clear ri before updating */
2401 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2402 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2403 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
2404
Stefan Chulskiaff3da32017-09-25 14:59:46 +02002405 mvpp2_prs_sram_ri_update(&pe, ri | MVPP2_PRS_RI_IP_FRAG_TRUE,
2406 ri_mask | MVPP2_PRS_RI_IP_FRAG_MASK);
2407
2408 mvpp2_prs_tcam_data_byte_set(&pe, 2, 0x00, 0x0);
2409 mvpp2_prs_tcam_data_byte_set(&pe, 3, 0x00, 0x0);
Marcin Wojtas3f518502014-07-10 16:52:13 -03002410
2411 /* Update shadow table and hw entry */
2412 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2413 mvpp2_prs_hw_write(priv, &pe);
2414
2415 return 0;
2416}
2417
2418/* IPv4 L3 multicast or broadcast */
2419static int mvpp2_prs_ip4_cast(struct mvpp2 *priv, unsigned short l3_cast)
2420{
2421 struct mvpp2_prs_entry pe;
2422 int mask, tid;
2423
2424 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2425 MVPP2_PE_LAST_FREE_TID);
2426 if (tid < 0)
2427 return tid;
2428
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002429 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002430 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
2431 pe.index = tid;
2432
2433 switch (l3_cast) {
2434 case MVPP2_PRS_L3_MULTI_CAST:
2435 mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV4_MC,
2436 MVPP2_PRS_IPV4_MC_MASK);
2437 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
2438 MVPP2_PRS_RI_L3_ADDR_MASK);
2439 break;
2440 case MVPP2_PRS_L3_BROAD_CAST:
2441 mask = MVPP2_PRS_IPV4_BC_MASK;
2442 mvpp2_prs_tcam_data_byte_set(&pe, 0, mask, mask);
2443 mvpp2_prs_tcam_data_byte_set(&pe, 1, mask, mask);
2444 mvpp2_prs_tcam_data_byte_set(&pe, 2, mask, mask);
2445 mvpp2_prs_tcam_data_byte_set(&pe, 3, mask, mask);
2446 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_BCAST,
2447 MVPP2_PRS_RI_L3_ADDR_MASK);
2448 break;
2449 default:
2450 return -EINVAL;
2451 }
2452
2453 /* Finished: go to flowid generation */
2454 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2455 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2456
2457 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
2458 MVPP2_PRS_IPV4_DIP_AI_BIT);
2459 /* Unmask all ports */
2460 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2461
2462 /* Update shadow table and hw entry */
2463 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2464 mvpp2_prs_hw_write(priv, &pe);
2465
2466 return 0;
2467}
2468
2469/* Set entries for protocols over IPv6 */
2470static int mvpp2_prs_ip6_proto(struct mvpp2 *priv, unsigned short proto,
2471 unsigned int ri, unsigned int ri_mask)
2472{
2473 struct mvpp2_prs_entry pe;
2474 int tid;
2475
2476 if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
2477 (proto != IPPROTO_ICMPV6) && (proto != IPPROTO_IPIP))
2478 return -EINVAL;
2479
2480 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2481 MVPP2_PE_LAST_FREE_TID);
2482 if (tid < 0)
2483 return tid;
2484
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002485 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002486 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2487 pe.index = tid;
2488
2489 /* Finished: go to flowid generation */
2490 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2491 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2492 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
2493 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
2494 sizeof(struct ipv6hdr) - 6,
2495 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2496
2497 mvpp2_prs_tcam_data_byte_set(&pe, 0, proto, MVPP2_PRS_TCAM_PROTO_MASK);
2498 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
2499 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2500 /* Unmask all ports */
2501 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2502
2503 /* Write HW */
2504 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
2505 mvpp2_prs_hw_write(priv, &pe);
2506
2507 return 0;
2508}
2509
2510/* IPv6 L3 multicast entry */
2511static int mvpp2_prs_ip6_cast(struct mvpp2 *priv, unsigned short l3_cast)
2512{
2513 struct mvpp2_prs_entry pe;
2514 int tid;
2515
2516 if (l3_cast != MVPP2_PRS_L3_MULTI_CAST)
2517 return -EINVAL;
2518
2519 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2520 MVPP2_PE_LAST_FREE_TID);
2521 if (tid < 0)
2522 return tid;
2523
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002524 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002525 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2526 pe.index = tid;
2527
2528 /* Finished: go to flowid generation */
2529 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2530 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
2531 MVPP2_PRS_RI_L3_ADDR_MASK);
2532 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
2533 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2534 /* Shift back to IPv6 NH */
2535 mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2536
2537 mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV6_MC,
2538 MVPP2_PRS_IPV6_MC_MASK);
2539 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2540 /* Unmask all ports */
2541 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2542
2543 /* Update shadow table and hw entry */
2544 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
2545 mvpp2_prs_hw_write(priv, &pe);
2546
2547 return 0;
2548}
2549
2550/* Parser per-port initialization */
2551static void mvpp2_prs_hw_port_init(struct mvpp2 *priv, int port, int lu_first,
2552 int lu_max, int offset)
2553{
2554 u32 val;
2555
2556 /* Set lookup ID */
2557 val = mvpp2_read(priv, MVPP2_PRS_INIT_LOOKUP_REG);
2558 val &= ~MVPP2_PRS_PORT_LU_MASK(port);
2559 val |= MVPP2_PRS_PORT_LU_VAL(port, lu_first);
2560 mvpp2_write(priv, MVPP2_PRS_INIT_LOOKUP_REG, val);
2561
2562 /* Set maximum number of loops for packet received from port */
2563 val = mvpp2_read(priv, MVPP2_PRS_MAX_LOOP_REG(port));
2564 val &= ~MVPP2_PRS_MAX_LOOP_MASK(port);
2565 val |= MVPP2_PRS_MAX_LOOP_VAL(port, lu_max);
2566 mvpp2_write(priv, MVPP2_PRS_MAX_LOOP_REG(port), val);
2567
2568 /* Set initial offset for packet header extraction for the first
2569 * searching loop
2570 */
2571 val = mvpp2_read(priv, MVPP2_PRS_INIT_OFFS_REG(port));
2572 val &= ~MVPP2_PRS_INIT_OFF_MASK(port);
2573 val |= MVPP2_PRS_INIT_OFF_VAL(port, offset);
2574 mvpp2_write(priv, MVPP2_PRS_INIT_OFFS_REG(port), val);
2575}
2576
2577/* Default flow entries initialization for all ports */
2578static void mvpp2_prs_def_flow_init(struct mvpp2 *priv)
2579{
2580 struct mvpp2_prs_entry pe;
2581 int port;
2582
2583 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002584 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002585 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2586 pe.index = MVPP2_PE_FIRST_DEFAULT_FLOW - port;
2587
2588 /* Mask all ports */
2589 mvpp2_prs_tcam_port_map_set(&pe, 0);
2590
2591 /* Set flow ID*/
2592 mvpp2_prs_sram_ai_update(&pe, port, MVPP2_PRS_FLOW_ID_MASK);
2593 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
2594
2595 /* Update shadow table and hw entry */
2596 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_FLOWS);
2597 mvpp2_prs_hw_write(priv, &pe);
2598 }
2599}
2600
2601/* Set default entry for Marvell Header field */
2602static void mvpp2_prs_mh_init(struct mvpp2 *priv)
2603{
2604 struct mvpp2_prs_entry pe;
2605
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002606 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002607
2608 pe.index = MVPP2_PE_MH_DEFAULT;
2609 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MH);
2610 mvpp2_prs_sram_shift_set(&pe, MVPP2_MH_SIZE,
2611 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2612 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_MAC);
2613
2614 /* Unmask all ports */
2615 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2616
2617 /* Update shadow table and hw entry */
2618 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MH);
2619 mvpp2_prs_hw_write(priv, &pe);
2620}
2621
2622/* Set default entires (place holder) for promiscuous, non-promiscuous and
2623 * multicast MAC addresses
2624 */
2625static void mvpp2_prs_mac_init(struct mvpp2 *priv)
2626{
2627 struct mvpp2_prs_entry pe;
2628
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002629 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002630
2631 /* Non-promiscuous mode for all ports - DROP unknown packets */
2632 pe.index = MVPP2_PE_MAC_NON_PROMISCUOUS;
2633 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
2634
2635 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
2636 MVPP2_PRS_RI_DROP_MASK);
2637 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2638 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2639
2640 /* Unmask all ports */
2641 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2642
2643 /* Update shadow table and hw entry */
2644 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2645 mvpp2_prs_hw_write(priv, &pe);
2646
2647 /* place holders only - no ports */
2648 mvpp2_prs_mac_drop_all_set(priv, 0, false);
2649 mvpp2_prs_mac_promisc_set(priv, 0, false);
Antoine Tenart20746d72017-10-24 11:41:27 +02002650 mvpp2_prs_mac_multi_set(priv, 0, MVPP2_PE_MAC_MC_ALL, false);
2651 mvpp2_prs_mac_multi_set(priv, 0, MVPP2_PE_MAC_MC_IP6, false);
Marcin Wojtas3f518502014-07-10 16:52:13 -03002652}
2653
2654/* Set default entries for various types of dsa packets */
2655static void mvpp2_prs_dsa_init(struct mvpp2 *priv)
2656{
2657 struct mvpp2_prs_entry pe;
2658
2659 /* None tagged EDSA entry - place holder */
2660 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
2661 MVPP2_PRS_EDSA);
2662
2663 /* Tagged EDSA entry - place holder */
2664 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
2665
2666 /* None tagged DSA entry - place holder */
2667 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
2668 MVPP2_PRS_DSA);
2669
2670 /* Tagged DSA entry - place holder */
2671 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
2672
2673 /* None tagged EDSA ethertype entry - place holder*/
2674 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
2675 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
2676
2677 /* Tagged EDSA ethertype entry - place holder*/
2678 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
2679 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
2680
2681 /* None tagged DSA ethertype entry */
2682 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
2683 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
2684
2685 /* Tagged DSA ethertype entry */
2686 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
2687 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
2688
2689 /* Set default entry, in case DSA or EDSA tag not found */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002690 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002691 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
2692 pe.index = MVPP2_PE_DSA_DEFAULT;
2693 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2694
2695 /* Shift 0 bytes */
2696 mvpp2_prs_sram_shift_set(&pe, 0, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2697 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2698
2699 /* Clear all sram ai bits for next iteration */
2700 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2701
2702 /* Unmask all ports */
2703 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2704
2705 mvpp2_prs_hw_write(priv, &pe);
2706}
2707
2708/* Match basic ethertypes */
2709static int mvpp2_prs_etype_init(struct mvpp2 *priv)
2710{
2711 struct mvpp2_prs_entry pe;
2712 int tid;
2713
2714 /* Ethertype: PPPoE */
2715 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2716 MVPP2_PE_LAST_FREE_TID);
2717 if (tid < 0)
2718 return tid;
2719
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002720 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002721 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2722 pe.index = tid;
2723
2724 mvpp2_prs_match_etype(&pe, 0, ETH_P_PPP_SES);
2725
2726 mvpp2_prs_sram_shift_set(&pe, MVPP2_PPPOE_HDR_SIZE,
2727 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2728 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2729 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_PPPOE_MASK,
2730 MVPP2_PRS_RI_PPPOE_MASK);
2731
2732 /* Update shadow table and hw entry */
2733 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2734 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2735 priv->prs_shadow[pe.index].finish = false;
2736 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_PPPOE_MASK,
2737 MVPP2_PRS_RI_PPPOE_MASK);
2738 mvpp2_prs_hw_write(priv, &pe);
2739
2740 /* Ethertype: ARP */
2741 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2742 MVPP2_PE_LAST_FREE_TID);
2743 if (tid < 0)
2744 return tid;
2745
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002746 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002747 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2748 pe.index = tid;
2749
2750 mvpp2_prs_match_etype(&pe, 0, ETH_P_ARP);
2751
2752 /* Generate flow in the next iteration*/
2753 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2754 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2755 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_ARP,
2756 MVPP2_PRS_RI_L3_PROTO_MASK);
2757 /* Set L3 offset */
2758 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2759 MVPP2_ETH_TYPE_LEN,
2760 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2761
2762 /* Update shadow table and hw entry */
2763 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2764 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2765 priv->prs_shadow[pe.index].finish = true;
2766 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_ARP,
2767 MVPP2_PRS_RI_L3_PROTO_MASK);
2768 mvpp2_prs_hw_write(priv, &pe);
2769
2770 /* Ethertype: LBTD */
2771 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2772 MVPP2_PE_LAST_FREE_TID);
2773 if (tid < 0)
2774 return tid;
2775
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002776 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002777 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2778 pe.index = tid;
2779
2780 mvpp2_prs_match_etype(&pe, 0, MVPP2_IP_LBDT_TYPE);
2781
2782 /* Generate flow in the next iteration*/
2783 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2784 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2785 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2786 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2787 MVPP2_PRS_RI_CPU_CODE_MASK |
2788 MVPP2_PRS_RI_UDF3_MASK);
2789 /* Set L3 offset */
2790 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2791 MVPP2_ETH_TYPE_LEN,
2792 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2793
2794 /* Update shadow table and hw entry */
2795 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2796 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2797 priv->prs_shadow[pe.index].finish = true;
2798 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2799 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2800 MVPP2_PRS_RI_CPU_CODE_MASK |
2801 MVPP2_PRS_RI_UDF3_MASK);
2802 mvpp2_prs_hw_write(priv, &pe);
2803
2804 /* Ethertype: IPv4 without options */
2805 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2806 MVPP2_PE_LAST_FREE_TID);
2807 if (tid < 0)
2808 return tid;
2809
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002810 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002811 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2812 pe.index = tid;
2813
2814 mvpp2_prs_match_etype(&pe, 0, ETH_P_IP);
2815 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2816 MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
2817 MVPP2_PRS_IPV4_HEAD_MASK |
2818 MVPP2_PRS_IPV4_IHL_MASK);
2819
2820 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2821 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
2822 MVPP2_PRS_RI_L3_PROTO_MASK);
2823 /* Skip eth_type + 4 bytes of IP header */
2824 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
2825 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2826 /* Set L3 offset */
2827 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2828 MVPP2_ETH_TYPE_LEN,
2829 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2830
2831 /* Update shadow table and hw entry */
2832 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2833 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2834 priv->prs_shadow[pe.index].finish = false;
2835 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4,
2836 MVPP2_PRS_RI_L3_PROTO_MASK);
2837 mvpp2_prs_hw_write(priv, &pe);
2838
2839 /* Ethertype: IPv4 with options */
2840 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2841 MVPP2_PE_LAST_FREE_TID);
2842 if (tid < 0)
2843 return tid;
2844
2845 pe.index = tid;
2846
2847 /* Clear tcam data before updating */
2848 pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(MVPP2_ETH_TYPE_LEN)] = 0x0;
2849 pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(MVPP2_ETH_TYPE_LEN)] = 0x0;
2850
2851 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2852 MVPP2_PRS_IPV4_HEAD,
2853 MVPP2_PRS_IPV4_HEAD_MASK);
2854
2855 /* Clear ri before updating */
2856 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2857 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2858 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
2859 MVPP2_PRS_RI_L3_PROTO_MASK);
2860
2861 /* Update shadow table and hw entry */
2862 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2863 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2864 priv->prs_shadow[pe.index].finish = false;
2865 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4_OPT,
2866 MVPP2_PRS_RI_L3_PROTO_MASK);
2867 mvpp2_prs_hw_write(priv, &pe);
2868
2869 /* Ethertype: IPv6 without options */
2870 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2871 MVPP2_PE_LAST_FREE_TID);
2872 if (tid < 0)
2873 return tid;
2874
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002875 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002876 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2877 pe.index = tid;
2878
2879 mvpp2_prs_match_etype(&pe, 0, ETH_P_IPV6);
2880
2881 /* Skip DIP of IPV6 header */
2882 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 8 +
2883 MVPP2_MAX_L3_ADDR_SIZE,
2884 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2885 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2886 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
2887 MVPP2_PRS_RI_L3_PROTO_MASK);
2888 /* Set L3 offset */
2889 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2890 MVPP2_ETH_TYPE_LEN,
2891 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2892
2893 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2894 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2895 priv->prs_shadow[pe.index].finish = false;
2896 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP6,
2897 MVPP2_PRS_RI_L3_PROTO_MASK);
2898 mvpp2_prs_hw_write(priv, &pe);
2899
2900 /* Default entry for MVPP2_PRS_LU_L2 - Unknown ethtype */
2901 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2902 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2903 pe.index = MVPP2_PE_ETH_TYPE_UN;
2904
2905 /* Unmask all ports */
2906 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2907
2908 /* Generate flow in the next iteration*/
2909 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2910 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2911 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
2912 MVPP2_PRS_RI_L3_PROTO_MASK);
2913 /* Set L3 offset even it's unknown L3 */
2914 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2915 MVPP2_ETH_TYPE_LEN,
2916 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2917
2918 /* Update shadow table and hw entry */
2919 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2920 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2921 priv->prs_shadow[pe.index].finish = true;
2922 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_UN,
2923 MVPP2_PRS_RI_L3_PROTO_MASK);
2924 mvpp2_prs_hw_write(priv, &pe);
2925
2926 return 0;
2927}
2928
2929/* Configure vlan entries and detect up to 2 successive VLAN tags.
2930 * Possible options:
2931 * 0x8100, 0x88A8
2932 * 0x8100, 0x8100
2933 * 0x8100
2934 * 0x88A8
2935 */
2936static int mvpp2_prs_vlan_init(struct platform_device *pdev, struct mvpp2 *priv)
2937{
2938 struct mvpp2_prs_entry pe;
2939 int err;
2940
2941 priv->prs_double_vlans = devm_kcalloc(&pdev->dev, sizeof(bool),
2942 MVPP2_PRS_DBL_VLANS_MAX,
2943 GFP_KERNEL);
2944 if (!priv->prs_double_vlans)
2945 return -ENOMEM;
2946
2947 /* Double VLAN: 0x8100, 0x88A8 */
2948 err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021AD,
2949 MVPP2_PRS_PORT_MASK);
2950 if (err)
2951 return err;
2952
2953 /* Double VLAN: 0x8100, 0x8100 */
2954 err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021Q,
2955 MVPP2_PRS_PORT_MASK);
2956 if (err)
2957 return err;
2958
2959 /* Single VLAN: 0x88a8 */
2960 err = mvpp2_prs_vlan_add(priv, ETH_P_8021AD, MVPP2_PRS_SINGLE_VLAN_AI,
2961 MVPP2_PRS_PORT_MASK);
2962 if (err)
2963 return err;
2964
2965 /* Single VLAN: 0x8100 */
2966 err = mvpp2_prs_vlan_add(priv, ETH_P_8021Q, MVPP2_PRS_SINGLE_VLAN_AI,
2967 MVPP2_PRS_PORT_MASK);
2968 if (err)
2969 return err;
2970
2971 /* Set default double vlan entry */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002972 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002973 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2974 pe.index = MVPP2_PE_VLAN_DBL;
2975
2976 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2977 /* Clear ai for next iterations */
2978 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2979 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_DOUBLE,
2980 MVPP2_PRS_RI_VLAN_MASK);
2981
2982 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_DBL_VLAN_AI_BIT,
2983 MVPP2_PRS_DBL_VLAN_AI_BIT);
2984 /* Unmask all ports */
2985 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2986
2987 /* Update shadow table and hw entry */
2988 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
2989 mvpp2_prs_hw_write(priv, &pe);
2990
2991 /* Set default vlan none entry */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002992 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002993 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2994 pe.index = MVPP2_PE_VLAN_NONE;
2995
2996 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2997 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
2998 MVPP2_PRS_RI_VLAN_MASK);
2999
3000 /* Unmask all ports */
3001 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3002
3003 /* Update shadow table and hw entry */
3004 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
3005 mvpp2_prs_hw_write(priv, &pe);
3006
3007 return 0;
3008}
3009
3010/* Set entries for PPPoE ethertype */
3011static int mvpp2_prs_pppoe_init(struct mvpp2 *priv)
3012{
3013 struct mvpp2_prs_entry pe;
3014 int tid;
3015
3016 /* IPv4 over PPPoE with options */
3017 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3018 MVPP2_PE_LAST_FREE_TID);
3019 if (tid < 0)
3020 return tid;
3021
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003022 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003023 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
3024 pe.index = tid;
3025
3026 mvpp2_prs_match_etype(&pe, 0, PPP_IP);
3027
3028 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
3029 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
3030 MVPP2_PRS_RI_L3_PROTO_MASK);
3031 /* Skip eth_type + 4 bytes of IP header */
3032 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
3033 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3034 /* Set L3 offset */
3035 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
3036 MVPP2_ETH_TYPE_LEN,
3037 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3038
3039 /* Update shadow table and hw entry */
3040 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3041 mvpp2_prs_hw_write(priv, &pe);
3042
3043 /* IPv4 over PPPoE without options */
3044 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3045 MVPP2_PE_LAST_FREE_TID);
3046 if (tid < 0)
3047 return tid;
3048
3049 pe.index = tid;
3050
3051 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
3052 MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
3053 MVPP2_PRS_IPV4_HEAD_MASK |
3054 MVPP2_PRS_IPV4_IHL_MASK);
3055
3056 /* Clear ri before updating */
3057 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
3058 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
3059 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
3060 MVPP2_PRS_RI_L3_PROTO_MASK);
3061
3062 /* Update shadow table and hw entry */
3063 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3064 mvpp2_prs_hw_write(priv, &pe);
3065
3066 /* IPv6 over PPPoE */
3067 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3068 MVPP2_PE_LAST_FREE_TID);
3069 if (tid < 0)
3070 return tid;
3071
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003072 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003073 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
3074 pe.index = tid;
3075
3076 mvpp2_prs_match_etype(&pe, 0, PPP_IPV6);
3077
3078 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
3079 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
3080 MVPP2_PRS_RI_L3_PROTO_MASK);
3081 /* Skip eth_type + 4 bytes of IPv6 header */
3082 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
3083 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3084 /* Set L3 offset */
3085 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
3086 MVPP2_ETH_TYPE_LEN,
3087 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3088
3089 /* Update shadow table and hw entry */
3090 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3091 mvpp2_prs_hw_write(priv, &pe);
3092
3093 /* Non-IP over PPPoE */
3094 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3095 MVPP2_PE_LAST_FREE_TID);
3096 if (tid < 0)
3097 return tid;
3098
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003099 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003100 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
3101 pe.index = tid;
3102
3103 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
3104 MVPP2_PRS_RI_L3_PROTO_MASK);
3105
3106 /* Finished: go to flowid generation */
3107 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3108 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3109 /* Set L3 offset even if it's unknown L3 */
3110 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
3111 MVPP2_ETH_TYPE_LEN,
3112 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3113
3114 /* Update shadow table and hw entry */
3115 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3116 mvpp2_prs_hw_write(priv, &pe);
3117
3118 return 0;
3119}
3120
3121/* Initialize entries for IPv4 */
3122static int mvpp2_prs_ip4_init(struct mvpp2 *priv)
3123{
3124 struct mvpp2_prs_entry pe;
3125 int err;
3126
3127 /* Set entries for TCP, UDP and IGMP over IPv4 */
3128 err = mvpp2_prs_ip4_proto(priv, IPPROTO_TCP, MVPP2_PRS_RI_L4_TCP,
3129 MVPP2_PRS_RI_L4_PROTO_MASK);
3130 if (err)
3131 return err;
3132
3133 err = mvpp2_prs_ip4_proto(priv, IPPROTO_UDP, MVPP2_PRS_RI_L4_UDP,
3134 MVPP2_PRS_RI_L4_PROTO_MASK);
3135 if (err)
3136 return err;
3137
3138 err = mvpp2_prs_ip4_proto(priv, IPPROTO_IGMP,
3139 MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
3140 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
3141 MVPP2_PRS_RI_CPU_CODE_MASK |
3142 MVPP2_PRS_RI_UDF3_MASK);
3143 if (err)
3144 return err;
3145
3146 /* IPv4 Broadcast */
3147 err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_BROAD_CAST);
3148 if (err)
3149 return err;
3150
3151 /* IPv4 Multicast */
3152 err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
3153 if (err)
3154 return err;
3155
3156 /* Default IPv4 entry for unknown protocols */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003157 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003158 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
3159 pe.index = MVPP2_PE_IP4_PROTO_UN;
3160
3161 /* Set next lu to IPv4 */
3162 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
3163 mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3164 /* Set L4 offset */
3165 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
3166 sizeof(struct iphdr) - 4,
3167 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3168 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
3169 MVPP2_PRS_IPV4_DIP_AI_BIT);
3170 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3171 MVPP2_PRS_RI_L4_PROTO_MASK);
3172
3173 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
3174 /* Unmask all ports */
3175 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3176
3177 /* Update shadow table and hw entry */
3178 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3179 mvpp2_prs_hw_write(priv, &pe);
3180
3181 /* Default IPv4 entry for unicast address */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003182 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003183 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
3184 pe.index = MVPP2_PE_IP4_ADDR_UN;
3185
3186 /* Finished: go to flowid generation */
3187 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3188 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3189 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
3190 MVPP2_PRS_RI_L3_ADDR_MASK);
3191
3192 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
3193 MVPP2_PRS_IPV4_DIP_AI_BIT);
3194 /* Unmask all ports */
3195 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3196
3197 /* Update shadow table and hw entry */
3198 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3199 mvpp2_prs_hw_write(priv, &pe);
3200
3201 return 0;
3202}
3203
3204/* Initialize entries for IPv6 */
3205static int mvpp2_prs_ip6_init(struct mvpp2 *priv)
3206{
3207 struct mvpp2_prs_entry pe;
3208 int tid, err;
3209
3210 /* Set entries for TCP, UDP and ICMP over IPv6 */
3211 err = mvpp2_prs_ip6_proto(priv, IPPROTO_TCP,
3212 MVPP2_PRS_RI_L4_TCP,
3213 MVPP2_PRS_RI_L4_PROTO_MASK);
3214 if (err)
3215 return err;
3216
3217 err = mvpp2_prs_ip6_proto(priv, IPPROTO_UDP,
3218 MVPP2_PRS_RI_L4_UDP,
3219 MVPP2_PRS_RI_L4_PROTO_MASK);
3220 if (err)
3221 return err;
3222
3223 err = mvpp2_prs_ip6_proto(priv, IPPROTO_ICMPV6,
3224 MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
3225 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
3226 MVPP2_PRS_RI_CPU_CODE_MASK |
3227 MVPP2_PRS_RI_UDF3_MASK);
3228 if (err)
3229 return err;
3230
3231 /* IPv4 is the last header. This is similar case as 6-TCP or 17-UDP */
3232 /* Result Info: UDF7=1, DS lite */
3233 err = mvpp2_prs_ip6_proto(priv, IPPROTO_IPIP,
3234 MVPP2_PRS_RI_UDF7_IP6_LITE,
3235 MVPP2_PRS_RI_UDF7_MASK);
3236 if (err)
3237 return err;
3238
3239 /* IPv6 multicast */
3240 err = mvpp2_prs_ip6_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
3241 if (err)
3242 return err;
3243
3244 /* Entry for checking hop limit */
3245 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3246 MVPP2_PE_LAST_FREE_TID);
3247 if (tid < 0)
3248 return tid;
3249
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003250 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003251 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3252 pe.index = tid;
3253
3254 /* Finished: go to flowid generation */
3255 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3256 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3257 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN |
3258 MVPP2_PRS_RI_DROP_MASK,
3259 MVPP2_PRS_RI_L3_PROTO_MASK |
3260 MVPP2_PRS_RI_DROP_MASK);
3261
3262 mvpp2_prs_tcam_data_byte_set(&pe, 1, 0x00, MVPP2_PRS_IPV6_HOP_MASK);
3263 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3264 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3265
3266 /* Update shadow table and hw entry */
3267 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3268 mvpp2_prs_hw_write(priv, &pe);
3269
3270 /* Default IPv6 entry for unknown protocols */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003271 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003272 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3273 pe.index = MVPP2_PE_IP6_PROTO_UN;
3274
3275 /* Finished: go to flowid generation */
3276 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3277 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3278 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3279 MVPP2_PRS_RI_L4_PROTO_MASK);
3280 /* Set L4 offset relatively to our current place */
3281 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
3282 sizeof(struct ipv6hdr) - 4,
3283 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3284
3285 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3286 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3287 /* Unmask all ports */
3288 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3289
3290 /* Update shadow table and hw entry */
3291 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3292 mvpp2_prs_hw_write(priv, &pe);
3293
3294 /* Default IPv6 entry for unknown ext protocols */
3295 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3296 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3297 pe.index = MVPP2_PE_IP6_EXT_PROTO_UN;
3298
3299 /* Finished: go to flowid generation */
3300 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3301 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3302 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3303 MVPP2_PRS_RI_L4_PROTO_MASK);
3304
3305 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_EXT_AI_BIT,
3306 MVPP2_PRS_IPV6_EXT_AI_BIT);
3307 /* Unmask all ports */
3308 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
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 unicast address */
3315 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3316 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3317 pe.index = MVPP2_PE_IP6_ADDR_UN;
3318
3319 /* Finished: go to IPv6 again */
3320 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
3321 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
3322 MVPP2_PRS_RI_L3_ADDR_MASK);
3323 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3324 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3325 /* Shift back to IPV6 NH */
3326 mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3327
3328 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3329 /* Unmask all ports */
3330 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3331
3332 /* Update shadow table and hw entry */
3333 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
3334 mvpp2_prs_hw_write(priv, &pe);
3335
3336 return 0;
3337}
3338
3339/* Parser default initialization */
3340static int mvpp2_prs_default_init(struct platform_device *pdev,
3341 struct mvpp2 *priv)
3342{
3343 int err, index, i;
3344
3345 /* Enable tcam table */
3346 mvpp2_write(priv, MVPP2_PRS_TCAM_CTRL_REG, MVPP2_PRS_TCAM_EN_MASK);
3347
3348 /* Clear all tcam and sram entries */
3349 for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++) {
3350 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
3351 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
3352 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), 0);
3353
3354 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, index);
3355 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
3356 mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), 0);
3357 }
3358
3359 /* Invalidate all tcam entries */
3360 for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++)
3361 mvpp2_prs_hw_inv(priv, index);
3362
3363 priv->prs_shadow = devm_kcalloc(&pdev->dev, MVPP2_PRS_TCAM_SRAM_SIZE,
Markus Elfring37df25e2017-04-17 09:12:34 +02003364 sizeof(*priv->prs_shadow),
Marcin Wojtas3f518502014-07-10 16:52:13 -03003365 GFP_KERNEL);
3366 if (!priv->prs_shadow)
3367 return -ENOMEM;
3368
3369 /* Always start from lookup = 0 */
3370 for (index = 0; index < MVPP2_MAX_PORTS; index++)
3371 mvpp2_prs_hw_port_init(priv, index, MVPP2_PRS_LU_MH,
3372 MVPP2_PRS_PORT_LU_MAX, 0);
3373
3374 mvpp2_prs_def_flow_init(priv);
3375
3376 mvpp2_prs_mh_init(priv);
3377
3378 mvpp2_prs_mac_init(priv);
3379
3380 mvpp2_prs_dsa_init(priv);
3381
3382 err = mvpp2_prs_etype_init(priv);
3383 if (err)
3384 return err;
3385
3386 err = mvpp2_prs_vlan_init(pdev, priv);
3387 if (err)
3388 return err;
3389
3390 err = mvpp2_prs_pppoe_init(priv);
3391 if (err)
3392 return err;
3393
3394 err = mvpp2_prs_ip6_init(priv);
3395 if (err)
3396 return err;
3397
3398 err = mvpp2_prs_ip4_init(priv);
3399 if (err)
3400 return err;
3401
3402 return 0;
3403}
3404
3405/* Compare MAC DA with tcam entry data */
3406static bool mvpp2_prs_mac_range_equals(struct mvpp2_prs_entry *pe,
3407 const u8 *da, unsigned char *mask)
3408{
3409 unsigned char tcam_byte, tcam_mask;
3410 int index;
3411
3412 for (index = 0; index < ETH_ALEN; index++) {
3413 mvpp2_prs_tcam_data_byte_get(pe, index, &tcam_byte, &tcam_mask);
3414 if (tcam_mask != mask[index])
3415 return false;
3416
3417 if ((tcam_mask & tcam_byte) != (da[index] & mask[index]))
3418 return false;
3419 }
3420
3421 return true;
3422}
3423
3424/* Find tcam entry with matched pair <MAC DA, port> */
3425static struct mvpp2_prs_entry *
3426mvpp2_prs_mac_da_range_find(struct mvpp2 *priv, int pmap, const u8 *da,
3427 unsigned char *mask, int udf_type)
3428{
3429 struct mvpp2_prs_entry *pe;
3430 int tid;
3431
Antoine Tenart239dd4e2017-10-24 11:41:28 +02003432 pe = kzalloc(sizeof(*pe), GFP_ATOMIC);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003433 if (!pe)
3434 return NULL;
3435 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
3436
3437 /* Go through the all entires with MVPP2_PRS_LU_MAC */
3438 for (tid = MVPP2_PE_FIRST_FREE_TID;
3439 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
3440 unsigned int entry_pmap;
3441
3442 if (!priv->prs_shadow[tid].valid ||
3443 (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
3444 (priv->prs_shadow[tid].udf != udf_type))
3445 continue;
3446
3447 pe->index = tid;
3448 mvpp2_prs_hw_read(priv, pe);
3449 entry_pmap = mvpp2_prs_tcam_port_map_get(pe);
3450
3451 if (mvpp2_prs_mac_range_equals(pe, da, mask) &&
3452 entry_pmap == pmap)
3453 return pe;
3454 }
3455 kfree(pe);
3456
3457 return NULL;
3458}
3459
3460/* Update parser's mac da entry */
3461static int mvpp2_prs_mac_da_accept(struct mvpp2 *priv, int port,
3462 const u8 *da, bool add)
3463{
3464 struct mvpp2_prs_entry *pe;
3465 unsigned int pmap, len, ri;
3466 unsigned char mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3467 int tid;
3468
3469 /* Scan TCAM and see if entry with this <MAC DA, port> already exist */
3470 pe = mvpp2_prs_mac_da_range_find(priv, (1 << port), da, mask,
3471 MVPP2_PRS_UDF_MAC_DEF);
3472
3473 /* No such entry */
3474 if (!pe) {
3475 if (!add)
3476 return 0;
3477
3478 /* Create new TCAM entry */
3479 /* Find first range mac entry*/
3480 for (tid = MVPP2_PE_FIRST_FREE_TID;
3481 tid <= MVPP2_PE_LAST_FREE_TID; tid++)
3482 if (priv->prs_shadow[tid].valid &&
3483 (priv->prs_shadow[tid].lu == MVPP2_PRS_LU_MAC) &&
3484 (priv->prs_shadow[tid].udf ==
3485 MVPP2_PRS_UDF_MAC_RANGE))
3486 break;
3487
3488 /* Go through the all entries from first to last */
3489 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3490 tid - 1);
3491 if (tid < 0)
3492 return tid;
3493
Antoine Tenart239dd4e2017-10-24 11:41:28 +02003494 pe = kzalloc(sizeof(*pe), GFP_ATOMIC);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003495 if (!pe)
Amitoj Kaur Chawlac2bb7bc2016-02-04 19:25:26 +05303496 return -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003497 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
3498 pe->index = tid;
3499
3500 /* Mask all ports */
3501 mvpp2_prs_tcam_port_map_set(pe, 0);
3502 }
3503
3504 /* Update port mask */
3505 mvpp2_prs_tcam_port_set(pe, port, add);
3506
3507 /* Invalidate the entry if no ports are left enabled */
3508 pmap = mvpp2_prs_tcam_port_map_get(pe);
3509 if (pmap == 0) {
3510 if (add) {
3511 kfree(pe);
Amitoj Kaur Chawlac2bb7bc2016-02-04 19:25:26 +05303512 return -EINVAL;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003513 }
3514 mvpp2_prs_hw_inv(priv, pe->index);
3515 priv->prs_shadow[pe->index].valid = false;
3516 kfree(pe);
3517 return 0;
3518 }
3519
3520 /* Continue - set next lookup */
3521 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_DSA);
3522
3523 /* Set match on DA */
3524 len = ETH_ALEN;
3525 while (len--)
3526 mvpp2_prs_tcam_data_byte_set(pe, len, da[len], 0xff);
3527
3528 /* Set result info bits */
3529 if (is_broadcast_ether_addr(da))
3530 ri = MVPP2_PRS_RI_L2_BCAST;
3531 else if (is_multicast_ether_addr(da))
3532 ri = MVPP2_PRS_RI_L2_MCAST;
3533 else
3534 ri = MVPP2_PRS_RI_L2_UCAST | MVPP2_PRS_RI_MAC_ME_MASK;
3535
3536 mvpp2_prs_sram_ri_update(pe, ri, MVPP2_PRS_RI_L2_CAST_MASK |
3537 MVPP2_PRS_RI_MAC_ME_MASK);
3538 mvpp2_prs_shadow_ri_set(priv, pe->index, ri, MVPP2_PRS_RI_L2_CAST_MASK |
3539 MVPP2_PRS_RI_MAC_ME_MASK);
3540
3541 /* Shift to ethertype */
3542 mvpp2_prs_sram_shift_set(pe, 2 * ETH_ALEN,
3543 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3544
3545 /* Update shadow table and hw entry */
3546 priv->prs_shadow[pe->index].udf = MVPP2_PRS_UDF_MAC_DEF;
3547 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_MAC);
3548 mvpp2_prs_hw_write(priv, pe);
3549
3550 kfree(pe);
3551
3552 return 0;
3553}
3554
3555static int mvpp2_prs_update_mac_da(struct net_device *dev, const u8 *da)
3556{
3557 struct mvpp2_port *port = netdev_priv(dev);
3558 int err;
3559
3560 /* Remove old parser entry */
3561 err = mvpp2_prs_mac_da_accept(port->priv, port->id, dev->dev_addr,
3562 false);
3563 if (err)
3564 return err;
3565
3566 /* Add new parser entry */
3567 err = mvpp2_prs_mac_da_accept(port->priv, port->id, da, true);
3568 if (err)
3569 return err;
3570
3571 /* Set addr in the device */
3572 ether_addr_copy(dev->dev_addr, da);
3573
3574 return 0;
3575}
3576
3577/* Delete all port's multicast simple (not range) entries */
3578static void mvpp2_prs_mcast_del_all(struct mvpp2 *priv, int port)
3579{
3580 struct mvpp2_prs_entry pe;
3581 int index, tid;
3582
3583 for (tid = MVPP2_PE_FIRST_FREE_TID;
3584 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
3585 unsigned char da[ETH_ALEN], da_mask[ETH_ALEN];
3586
3587 if (!priv->prs_shadow[tid].valid ||
3588 (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
3589 (priv->prs_shadow[tid].udf != MVPP2_PRS_UDF_MAC_DEF))
3590 continue;
3591
3592 /* Only simple mac entries */
3593 pe.index = tid;
3594 mvpp2_prs_hw_read(priv, &pe);
3595
3596 /* Read mac addr from entry */
3597 for (index = 0; index < ETH_ALEN; index++)
3598 mvpp2_prs_tcam_data_byte_get(&pe, index, &da[index],
3599 &da_mask[index]);
3600
3601 if (is_multicast_ether_addr(da) && !is_broadcast_ether_addr(da))
3602 /* Delete this entry */
3603 mvpp2_prs_mac_da_accept(priv, port, da, false);
3604 }
3605}
3606
3607static int mvpp2_prs_tag_mode_set(struct mvpp2 *priv, int port, int type)
3608{
3609 switch (type) {
3610 case MVPP2_TAG_TYPE_EDSA:
3611 /* Add port to EDSA entries */
3612 mvpp2_prs_dsa_tag_set(priv, port, true,
3613 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3614 mvpp2_prs_dsa_tag_set(priv, port, true,
3615 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3616 /* Remove port from DSA entries */
3617 mvpp2_prs_dsa_tag_set(priv, port, false,
3618 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3619 mvpp2_prs_dsa_tag_set(priv, port, false,
3620 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3621 break;
3622
3623 case MVPP2_TAG_TYPE_DSA:
3624 /* Add port to DSA entries */
3625 mvpp2_prs_dsa_tag_set(priv, port, true,
3626 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3627 mvpp2_prs_dsa_tag_set(priv, port, true,
3628 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3629 /* Remove port from EDSA entries */
3630 mvpp2_prs_dsa_tag_set(priv, port, false,
3631 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3632 mvpp2_prs_dsa_tag_set(priv, port, false,
3633 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3634 break;
3635
3636 case MVPP2_TAG_TYPE_MH:
3637 case MVPP2_TAG_TYPE_NONE:
3638 /* Remove port form EDSA and DSA entries */
3639 mvpp2_prs_dsa_tag_set(priv, port, false,
3640 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3641 mvpp2_prs_dsa_tag_set(priv, port, false,
3642 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3643 mvpp2_prs_dsa_tag_set(priv, port, false,
3644 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3645 mvpp2_prs_dsa_tag_set(priv, port, false,
3646 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3647 break;
3648
3649 default:
3650 if ((type < 0) || (type > MVPP2_TAG_TYPE_EDSA))
3651 return -EINVAL;
3652 }
3653
3654 return 0;
3655}
3656
3657/* Set prs flow for the port */
3658static int mvpp2_prs_def_flow(struct mvpp2_port *port)
3659{
3660 struct mvpp2_prs_entry *pe;
3661 int tid;
3662
3663 pe = mvpp2_prs_flow_find(port->priv, port->id);
3664
3665 /* Such entry not exist */
3666 if (!pe) {
3667 /* Go through the all entires from last to first */
3668 tid = mvpp2_prs_tcam_first_free(port->priv,
3669 MVPP2_PE_LAST_FREE_TID,
3670 MVPP2_PE_FIRST_FREE_TID);
3671 if (tid < 0)
3672 return tid;
3673
3674 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
3675 if (!pe)
3676 return -ENOMEM;
3677
3678 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
3679 pe->index = tid;
3680
3681 /* Set flow ID*/
3682 mvpp2_prs_sram_ai_update(pe, port->id, MVPP2_PRS_FLOW_ID_MASK);
3683 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
3684
3685 /* Update shadow table */
3686 mvpp2_prs_shadow_set(port->priv, pe->index, MVPP2_PRS_LU_FLOWS);
3687 }
3688
3689 mvpp2_prs_tcam_port_map_set(pe, (1 << port->id));
3690 mvpp2_prs_hw_write(port->priv, pe);
3691 kfree(pe);
3692
3693 return 0;
3694}
3695
3696/* Classifier configuration routines */
3697
3698/* Update classification flow table registers */
3699static void mvpp2_cls_flow_write(struct mvpp2 *priv,
3700 struct mvpp2_cls_flow_entry *fe)
3701{
3702 mvpp2_write(priv, MVPP2_CLS_FLOW_INDEX_REG, fe->index);
3703 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL0_REG, fe->data[0]);
3704 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL1_REG, fe->data[1]);
3705 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL2_REG, fe->data[2]);
3706}
3707
3708/* Update classification lookup table register */
3709static void mvpp2_cls_lookup_write(struct mvpp2 *priv,
3710 struct mvpp2_cls_lookup_entry *le)
3711{
3712 u32 val;
3713
3714 val = (le->way << MVPP2_CLS_LKP_INDEX_WAY_OFFS) | le->lkpid;
3715 mvpp2_write(priv, MVPP2_CLS_LKP_INDEX_REG, val);
3716 mvpp2_write(priv, MVPP2_CLS_LKP_TBL_REG, le->data);
3717}
3718
3719/* Classifier default initialization */
3720static void mvpp2_cls_init(struct mvpp2 *priv)
3721{
3722 struct mvpp2_cls_lookup_entry le;
3723 struct mvpp2_cls_flow_entry fe;
3724 int index;
3725
3726 /* Enable classifier */
3727 mvpp2_write(priv, MVPP2_CLS_MODE_REG, MVPP2_CLS_MODE_ACTIVE_MASK);
3728
3729 /* Clear classifier flow table */
Arnd Bergmanne8f967c2016-11-24 17:28:12 +01003730 memset(&fe.data, 0, sizeof(fe.data));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003731 for (index = 0; index < MVPP2_CLS_FLOWS_TBL_SIZE; index++) {
3732 fe.index = index;
3733 mvpp2_cls_flow_write(priv, &fe);
3734 }
3735
3736 /* Clear classifier lookup table */
3737 le.data = 0;
3738 for (index = 0; index < MVPP2_CLS_LKP_TBL_SIZE; index++) {
3739 le.lkpid = index;
3740 le.way = 0;
3741 mvpp2_cls_lookup_write(priv, &le);
3742
3743 le.way = 1;
3744 mvpp2_cls_lookup_write(priv, &le);
3745 }
3746}
3747
3748static void mvpp2_cls_port_config(struct mvpp2_port *port)
3749{
3750 struct mvpp2_cls_lookup_entry le;
3751 u32 val;
3752
3753 /* Set way for the port */
3754 val = mvpp2_read(port->priv, MVPP2_CLS_PORT_WAY_REG);
3755 val &= ~MVPP2_CLS_PORT_WAY_MASK(port->id);
3756 mvpp2_write(port->priv, MVPP2_CLS_PORT_WAY_REG, val);
3757
3758 /* Pick the entry to be accessed in lookup ID decoding table
3759 * according to the way and lkpid.
3760 */
3761 le.lkpid = port->id;
3762 le.way = 0;
3763 le.data = 0;
3764
3765 /* Set initial CPU queue for receiving packets */
3766 le.data &= ~MVPP2_CLS_LKP_TBL_RXQ_MASK;
3767 le.data |= port->first_rxq;
3768
3769 /* Disable classification engines */
3770 le.data &= ~MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK;
3771
3772 /* Update lookup ID table entry */
3773 mvpp2_cls_lookup_write(port->priv, &le);
3774}
3775
3776/* Set CPU queue number for oversize packets */
3777static void mvpp2_cls_oversize_rxq_set(struct mvpp2_port *port)
3778{
3779 u32 val;
3780
3781 mvpp2_write(port->priv, MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port->id),
3782 port->first_rxq & MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK);
3783
3784 mvpp2_write(port->priv, MVPP2_CLS_SWFWD_P2HQ_REG(port->id),
3785 (port->first_rxq >> MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS));
3786
3787 val = mvpp2_read(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG);
3788 val |= MVPP2_CLS_SWFWD_PCTRL_MASK(port->id);
3789 mvpp2_write(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG, val);
3790}
3791
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003792static void *mvpp2_frag_alloc(const struct mvpp2_bm_pool *pool)
3793{
3794 if (likely(pool->frag_size <= PAGE_SIZE))
3795 return netdev_alloc_frag(pool->frag_size);
3796 else
3797 return kmalloc(pool->frag_size, GFP_ATOMIC);
3798}
3799
3800static void mvpp2_frag_free(const struct mvpp2_bm_pool *pool, void *data)
3801{
3802 if (likely(pool->frag_size <= PAGE_SIZE))
3803 skb_free_frag(data);
3804 else
3805 kfree(data);
3806}
3807
Marcin Wojtas3f518502014-07-10 16:52:13 -03003808/* Buffer Manager configuration routines */
3809
3810/* Create pool */
3811static int mvpp2_bm_pool_create(struct platform_device *pdev,
3812 struct mvpp2 *priv,
3813 struct mvpp2_bm_pool *bm_pool, int size)
3814{
Marcin Wojtas3f518502014-07-10 16:52:13 -03003815 u32 val;
3816
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003817 /* Number of buffer pointers must be a multiple of 16, as per
3818 * hardware constraints
3819 */
3820 if (!IS_ALIGNED(size, 16))
3821 return -EINVAL;
3822
3823 /* PPv2.1 needs 8 bytes per buffer pointer, PPv2.2 needs 16
3824 * bytes per buffer pointer
3825 */
3826 if (priv->hw_version == MVPP21)
3827 bm_pool->size_bytes = 2 * sizeof(u32) * size;
3828 else
3829 bm_pool->size_bytes = 2 * sizeof(u64) * size;
3830
3831 bm_pool->virt_addr = dma_alloc_coherent(&pdev->dev, bm_pool->size_bytes,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003832 &bm_pool->dma_addr,
Marcin Wojtas3f518502014-07-10 16:52:13 -03003833 GFP_KERNEL);
3834 if (!bm_pool->virt_addr)
3835 return -ENOMEM;
3836
Thomas Petazzonid3158802017-02-21 11:28:13 +01003837 if (!IS_ALIGNED((unsigned long)bm_pool->virt_addr,
3838 MVPP2_BM_POOL_PTR_ALIGN)) {
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003839 dma_free_coherent(&pdev->dev, bm_pool->size_bytes,
3840 bm_pool->virt_addr, bm_pool->dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003841 dev_err(&pdev->dev, "BM pool %d is not %d bytes aligned\n",
3842 bm_pool->id, MVPP2_BM_POOL_PTR_ALIGN);
3843 return -ENOMEM;
3844 }
3845
3846 mvpp2_write(priv, MVPP2_BM_POOL_BASE_REG(bm_pool->id),
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003847 lower_32_bits(bm_pool->dma_addr));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003848 mvpp2_write(priv, MVPP2_BM_POOL_SIZE_REG(bm_pool->id), size);
3849
3850 val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
3851 val |= MVPP2_BM_START_MASK;
3852 mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
3853
3854 bm_pool->type = MVPP2_BM_FREE;
3855 bm_pool->size = size;
3856 bm_pool->pkt_size = 0;
3857 bm_pool->buf_num = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003858
3859 return 0;
3860}
3861
3862/* Set pool buffer size */
3863static void mvpp2_bm_pool_bufsize_set(struct mvpp2 *priv,
3864 struct mvpp2_bm_pool *bm_pool,
3865 int buf_size)
3866{
3867 u32 val;
3868
3869 bm_pool->buf_size = buf_size;
3870
3871 val = ALIGN(buf_size, 1 << MVPP2_POOL_BUF_SIZE_OFFSET);
3872 mvpp2_write(priv, MVPP2_POOL_BUF_SIZE_REG(bm_pool->id), val);
3873}
3874
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003875static void mvpp2_bm_bufs_get_addrs(struct device *dev, struct mvpp2 *priv,
3876 struct mvpp2_bm_pool *bm_pool,
3877 dma_addr_t *dma_addr,
3878 phys_addr_t *phys_addr)
3879{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02003880 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01003881
3882 *dma_addr = mvpp2_percpu_read(priv, cpu,
3883 MVPP2_BM_PHY_ALLOC_REG(bm_pool->id));
3884 *phys_addr = mvpp2_percpu_read(priv, cpu, MVPP2_BM_VIRT_ALLOC_REG);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003885
3886 if (priv->hw_version == MVPP22) {
3887 u32 val;
3888 u32 dma_addr_highbits, phys_addr_highbits;
3889
Thomas Petazzonia7868412017-03-07 16:53:13 +01003890 val = mvpp2_percpu_read(priv, cpu, MVPP22_BM_ADDR_HIGH_ALLOC);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003891 dma_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_PHYS_MASK);
3892 phys_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_VIRT_MASK) >>
3893 MVPP22_BM_ADDR_HIGH_VIRT_SHIFT;
3894
3895 if (sizeof(dma_addr_t) == 8)
3896 *dma_addr |= (u64)dma_addr_highbits << 32;
3897
3898 if (sizeof(phys_addr_t) == 8)
3899 *phys_addr |= (u64)phys_addr_highbits << 32;
3900 }
Thomas Petazzonia704bb52017-06-10 23:18:22 +02003901
3902 put_cpu();
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003903}
3904
Ezequiel Garcia7861f122014-07-21 13:48:14 -03003905/* Free all buffers from the pool */
Marcin Wojtas4229d502015-12-03 15:20:50 +01003906static void mvpp2_bm_bufs_free(struct device *dev, struct mvpp2 *priv,
3907 struct mvpp2_bm_pool *bm_pool)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003908{
3909 int i;
3910
Ezequiel Garcia7861f122014-07-21 13:48:14 -03003911 for (i = 0; i < bm_pool->buf_num; i++) {
Thomas Petazzoni20396132017-03-07 16:53:00 +01003912 dma_addr_t buf_dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003913 phys_addr_t buf_phys_addr;
3914 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003915
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003916 mvpp2_bm_bufs_get_addrs(dev, priv, bm_pool,
3917 &buf_dma_addr, &buf_phys_addr);
Marcin Wojtas4229d502015-12-03 15:20:50 +01003918
Thomas Petazzoni20396132017-03-07 16:53:00 +01003919 dma_unmap_single(dev, buf_dma_addr,
Marcin Wojtas4229d502015-12-03 15:20:50 +01003920 bm_pool->buf_size, DMA_FROM_DEVICE);
3921
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003922 data = (void *)phys_to_virt(buf_phys_addr);
3923 if (!data)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003924 break;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003925
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003926 mvpp2_frag_free(bm_pool, data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003927 }
3928
3929 /* Update BM driver with number of buffers removed from pool */
3930 bm_pool->buf_num -= i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003931}
3932
3933/* Cleanup pool */
3934static int mvpp2_bm_pool_destroy(struct platform_device *pdev,
3935 struct mvpp2 *priv,
3936 struct mvpp2_bm_pool *bm_pool)
3937{
Marcin Wojtas3f518502014-07-10 16:52:13 -03003938 u32 val;
3939
Marcin Wojtas4229d502015-12-03 15:20:50 +01003940 mvpp2_bm_bufs_free(&pdev->dev, priv, bm_pool);
Ezequiel Garciad74c96c2014-07-21 13:48:13 -03003941 if (bm_pool->buf_num) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03003942 WARN(1, "cannot free all buffers in pool %d\n", bm_pool->id);
3943 return 0;
3944 }
3945
3946 val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
3947 val |= MVPP2_BM_STOP_MASK;
3948 mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
3949
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003950 dma_free_coherent(&pdev->dev, bm_pool->size_bytes,
Marcin Wojtas3f518502014-07-10 16:52:13 -03003951 bm_pool->virt_addr,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003952 bm_pool->dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003953 return 0;
3954}
3955
3956static int mvpp2_bm_pools_init(struct platform_device *pdev,
3957 struct mvpp2 *priv)
3958{
3959 int i, err, size;
3960 struct mvpp2_bm_pool *bm_pool;
3961
3962 /* Create all pools with maximum size */
3963 size = MVPP2_BM_POOL_SIZE_MAX;
3964 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
3965 bm_pool = &priv->bm_pools[i];
3966 bm_pool->id = i;
3967 err = mvpp2_bm_pool_create(pdev, priv, bm_pool, size);
3968 if (err)
3969 goto err_unroll_pools;
3970 mvpp2_bm_pool_bufsize_set(priv, bm_pool, 0);
3971 }
3972 return 0;
3973
3974err_unroll_pools:
3975 dev_err(&pdev->dev, "failed to create BM pool %d, size %d\n", i, size);
3976 for (i = i - 1; i >= 0; i--)
3977 mvpp2_bm_pool_destroy(pdev, priv, &priv->bm_pools[i]);
3978 return err;
3979}
3980
3981static int mvpp2_bm_init(struct platform_device *pdev, struct mvpp2 *priv)
3982{
3983 int i, err;
3984
3985 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
3986 /* Mask BM all interrupts */
3987 mvpp2_write(priv, MVPP2_BM_INTR_MASK_REG(i), 0);
3988 /* Clear BM cause register */
3989 mvpp2_write(priv, MVPP2_BM_INTR_CAUSE_REG(i), 0);
3990 }
3991
3992 /* Allocate and initialize BM pools */
3993 priv->bm_pools = devm_kcalloc(&pdev->dev, MVPP2_BM_POOLS_NUM,
Markus Elfring81f915e2017-04-17 09:06:33 +02003994 sizeof(*priv->bm_pools), GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003995 if (!priv->bm_pools)
3996 return -ENOMEM;
3997
3998 err = mvpp2_bm_pools_init(pdev, priv);
3999 if (err < 0)
4000 return err;
4001 return 0;
4002}
4003
4004/* Attach long pool to rxq */
4005static void mvpp2_rxq_long_pool_set(struct mvpp2_port *port,
4006 int lrxq, int long_pool)
4007{
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004008 u32 val, mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004009 int prxq;
4010
4011 /* Get queue physical ID */
4012 prxq = port->rxqs[lrxq]->id;
4013
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004014 if (port->priv->hw_version == MVPP21)
4015 mask = MVPP21_RXQ_POOL_LONG_MASK;
4016 else
4017 mask = MVPP22_RXQ_POOL_LONG_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004018
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004019 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
4020 val &= ~mask;
4021 val |= (long_pool << MVPP2_RXQ_POOL_LONG_OFFS) & mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004022 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
4023}
4024
4025/* Attach short pool to rxq */
4026static void mvpp2_rxq_short_pool_set(struct mvpp2_port *port,
4027 int lrxq, int short_pool)
4028{
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004029 u32 val, mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004030 int prxq;
4031
4032 /* Get queue physical ID */
4033 prxq = port->rxqs[lrxq]->id;
4034
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004035 if (port->priv->hw_version == MVPP21)
4036 mask = MVPP21_RXQ_POOL_SHORT_MASK;
4037 else
4038 mask = MVPP22_RXQ_POOL_SHORT_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004039
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004040 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
4041 val &= ~mask;
4042 val |= (short_pool << MVPP2_RXQ_POOL_SHORT_OFFS) & mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004043 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
4044}
4045
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004046static void *mvpp2_buf_alloc(struct mvpp2_port *port,
4047 struct mvpp2_bm_pool *bm_pool,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004048 dma_addr_t *buf_dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004049 phys_addr_t *buf_phys_addr,
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004050 gfp_t gfp_mask)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004051{
Thomas Petazzoni20396132017-03-07 16:53:00 +01004052 dma_addr_t dma_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004053 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004054
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004055 data = mvpp2_frag_alloc(bm_pool);
4056 if (!data)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004057 return NULL;
4058
Thomas Petazzoni20396132017-03-07 16:53:00 +01004059 dma_addr = dma_map_single(port->dev->dev.parent, data,
4060 MVPP2_RX_BUF_SIZE(bm_pool->pkt_size),
4061 DMA_FROM_DEVICE);
4062 if (unlikely(dma_mapping_error(port->dev->dev.parent, dma_addr))) {
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004063 mvpp2_frag_free(bm_pool, data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004064 return NULL;
4065 }
Thomas Petazzoni20396132017-03-07 16:53:00 +01004066 *buf_dma_addr = dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004067 *buf_phys_addr = virt_to_phys(data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004068
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004069 return data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004070}
4071
Marcin Wojtas3f518502014-07-10 16:52:13 -03004072/* Release buffer to BM */
4073static inline void mvpp2_bm_pool_put(struct mvpp2_port *port, int pool,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004074 dma_addr_t buf_dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004075 phys_addr_t buf_phys_addr)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004076{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004077 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01004078
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004079 if (port->priv->hw_version == MVPP22) {
4080 u32 val = 0;
4081
4082 if (sizeof(dma_addr_t) == 8)
4083 val |= upper_32_bits(buf_dma_addr) &
4084 MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK;
4085
4086 if (sizeof(phys_addr_t) == 8)
4087 val |= (upper_32_bits(buf_phys_addr)
4088 << MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT) &
4089 MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK;
4090
Thomas Petazzonia7868412017-03-07 16:53:13 +01004091 mvpp2_percpu_write(port->priv, cpu,
4092 MVPP22_BM_ADDR_HIGH_RLS_REG, val);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004093 }
4094
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004095 /* MVPP2_BM_VIRT_RLS_REG is not interpreted by HW, and simply
4096 * returned in the "cookie" field of the RX
4097 * descriptor. Instead of storing the virtual address, we
4098 * store the physical address
4099 */
Thomas Petazzonia7868412017-03-07 16:53:13 +01004100 mvpp2_percpu_write(port->priv, cpu,
4101 MVPP2_BM_VIRT_RLS_REG, buf_phys_addr);
4102 mvpp2_percpu_write(port->priv, cpu,
4103 MVPP2_BM_PHY_RLS_REG(pool), buf_dma_addr);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004104
4105 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03004106}
4107
Marcin Wojtas3f518502014-07-10 16:52:13 -03004108/* Allocate buffers for the pool */
4109static int mvpp2_bm_bufs_add(struct mvpp2_port *port,
4110 struct mvpp2_bm_pool *bm_pool, int buf_num)
4111{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004112 int i, buf_size, total_size;
Thomas Petazzoni20396132017-03-07 16:53:00 +01004113 dma_addr_t dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004114 phys_addr_t phys_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004115 void *buf;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004116
4117 buf_size = MVPP2_RX_BUF_SIZE(bm_pool->pkt_size);
4118 total_size = MVPP2_RX_TOTAL_SIZE(buf_size);
4119
4120 if (buf_num < 0 ||
4121 (buf_num + bm_pool->buf_num > bm_pool->size)) {
4122 netdev_err(port->dev,
4123 "cannot allocate %d buffers for pool %d\n",
4124 buf_num, bm_pool->id);
4125 return 0;
4126 }
4127
Marcin Wojtas3f518502014-07-10 16:52:13 -03004128 for (i = 0; i < buf_num; i++) {
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004129 buf = mvpp2_buf_alloc(port, bm_pool, &dma_addr,
4130 &phys_addr, GFP_KERNEL);
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004131 if (!buf)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004132 break;
4133
Thomas Petazzoni20396132017-03-07 16:53:00 +01004134 mvpp2_bm_pool_put(port, bm_pool->id, dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004135 phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004136 }
4137
4138 /* Update BM driver with number of buffers added to pool */
4139 bm_pool->buf_num += i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004140
4141 netdev_dbg(port->dev,
4142 "%s pool %d: pkt_size=%4d, buf_size=%4d, total_size=%4d\n",
4143 bm_pool->type == MVPP2_BM_SWF_SHORT ? "short" : " long",
4144 bm_pool->id, bm_pool->pkt_size, buf_size, total_size);
4145
4146 netdev_dbg(port->dev,
4147 "%s pool %d: %d of %d buffers added\n",
4148 bm_pool->type == MVPP2_BM_SWF_SHORT ? "short" : " long",
4149 bm_pool->id, i, buf_num);
4150 return i;
4151}
4152
4153/* Notify the driver that BM pool is being used as specific type and return the
4154 * pool pointer on success
4155 */
4156static struct mvpp2_bm_pool *
4157mvpp2_bm_pool_use(struct mvpp2_port *port, int pool, enum mvpp2_bm_type type,
4158 int pkt_size)
4159{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004160 struct mvpp2_bm_pool *new_pool = &port->priv->bm_pools[pool];
4161 int num;
4162
4163 if (new_pool->type != MVPP2_BM_FREE && new_pool->type != type) {
4164 netdev_err(port->dev, "mixing pool types is forbidden\n");
4165 return NULL;
4166 }
4167
Marcin Wojtas3f518502014-07-10 16:52:13 -03004168 if (new_pool->type == MVPP2_BM_FREE)
4169 new_pool->type = type;
4170
4171 /* Allocate buffers in case BM pool is used as long pool, but packet
4172 * size doesn't match MTU or BM pool hasn't being used yet
4173 */
4174 if (((type == MVPP2_BM_SWF_LONG) && (pkt_size > new_pool->pkt_size)) ||
4175 (new_pool->pkt_size == 0)) {
4176 int pkts_num;
4177
4178 /* Set default buffer number or free all the buffers in case
4179 * the pool is not empty
4180 */
4181 pkts_num = new_pool->buf_num;
4182 if (pkts_num == 0)
4183 pkts_num = type == MVPP2_BM_SWF_LONG ?
4184 MVPP2_BM_LONG_BUF_NUM :
4185 MVPP2_BM_SHORT_BUF_NUM;
4186 else
Marcin Wojtas4229d502015-12-03 15:20:50 +01004187 mvpp2_bm_bufs_free(port->dev->dev.parent,
4188 port->priv, new_pool);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004189
4190 new_pool->pkt_size = pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004191 new_pool->frag_size =
4192 SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
4193 MVPP2_SKB_SHINFO_SIZE;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004194
4195 /* Allocate buffers for this pool */
4196 num = mvpp2_bm_bufs_add(port, new_pool, pkts_num);
4197 if (num != pkts_num) {
4198 WARN(1, "pool %d: %d of %d allocated\n",
4199 new_pool->id, num, pkts_num);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004200 return NULL;
4201 }
4202 }
4203
4204 mvpp2_bm_pool_bufsize_set(port->priv, new_pool,
4205 MVPP2_RX_BUF_SIZE(new_pool->pkt_size));
4206
Marcin Wojtas3f518502014-07-10 16:52:13 -03004207 return new_pool;
4208}
4209
4210/* Initialize pools for swf */
4211static int mvpp2_swf_bm_pool_init(struct mvpp2_port *port)
4212{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004213 int rxq;
4214
4215 if (!port->pool_long) {
4216 port->pool_long =
4217 mvpp2_bm_pool_use(port, MVPP2_BM_SWF_LONG_POOL(port->id),
4218 MVPP2_BM_SWF_LONG,
4219 port->pkt_size);
4220 if (!port->pool_long)
4221 return -ENOMEM;
4222
Marcin Wojtas3f518502014-07-10 16:52:13 -03004223 port->pool_long->port_map |= (1 << port->id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004224
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004225 for (rxq = 0; rxq < port->nrxqs; rxq++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004226 mvpp2_rxq_long_pool_set(port, rxq, port->pool_long->id);
4227 }
4228
4229 if (!port->pool_short) {
4230 port->pool_short =
4231 mvpp2_bm_pool_use(port, MVPP2_BM_SWF_SHORT_POOL,
4232 MVPP2_BM_SWF_SHORT,
4233 MVPP2_BM_SHORT_PKT_SIZE);
4234 if (!port->pool_short)
4235 return -ENOMEM;
4236
Marcin Wojtas3f518502014-07-10 16:52:13 -03004237 port->pool_short->port_map |= (1 << port->id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004238
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004239 for (rxq = 0; rxq < port->nrxqs; rxq++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004240 mvpp2_rxq_short_pool_set(port, rxq,
4241 port->pool_short->id);
4242 }
4243
4244 return 0;
4245}
4246
4247static int mvpp2_bm_update_mtu(struct net_device *dev, int mtu)
4248{
4249 struct mvpp2_port *port = netdev_priv(dev);
4250 struct mvpp2_bm_pool *port_pool = port->pool_long;
4251 int num, pkts_num = port_pool->buf_num;
4252 int pkt_size = MVPP2_RX_PKT_SIZE(mtu);
4253
4254 /* Update BM pool with new buffer size */
Marcin Wojtas4229d502015-12-03 15:20:50 +01004255 mvpp2_bm_bufs_free(dev->dev.parent, port->priv, port_pool);
Ezequiel Garciad74c96c2014-07-21 13:48:13 -03004256 if (port_pool->buf_num) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004257 WARN(1, "cannot free all buffers in pool %d\n", port_pool->id);
4258 return -EIO;
4259 }
4260
4261 port_pool->pkt_size = pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004262 port_pool->frag_size = SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
4263 MVPP2_SKB_SHINFO_SIZE;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004264 num = mvpp2_bm_bufs_add(port, port_pool, pkts_num);
4265 if (num != pkts_num) {
4266 WARN(1, "pool %d: %d of %d allocated\n",
4267 port_pool->id, num, pkts_num);
4268 return -EIO;
4269 }
4270
4271 mvpp2_bm_pool_bufsize_set(port->priv, port_pool,
4272 MVPP2_RX_BUF_SIZE(port_pool->pkt_size));
4273 dev->mtu = mtu;
4274 netdev_update_features(dev);
4275 return 0;
4276}
4277
4278static inline void mvpp2_interrupts_enable(struct mvpp2_port *port)
4279{
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004280 int i, sw_thread_mask = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004281
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004282 for (i = 0; i < port->nqvecs; i++)
4283 sw_thread_mask |= port->qvecs[i].sw_thread_mask;
4284
Marcin Wojtas3f518502014-07-10 16:52:13 -03004285 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004286 MVPP2_ISR_ENABLE_INTERRUPT(sw_thread_mask));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004287}
4288
4289static inline void mvpp2_interrupts_disable(struct mvpp2_port *port)
4290{
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004291 int i, sw_thread_mask = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004292
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004293 for (i = 0; i < port->nqvecs; i++)
4294 sw_thread_mask |= port->qvecs[i].sw_thread_mask;
4295
Marcin Wojtas3f518502014-07-10 16:52:13 -03004296 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004297 MVPP2_ISR_DISABLE_INTERRUPT(sw_thread_mask));
4298}
4299
4300static inline void mvpp2_qvec_interrupt_enable(struct mvpp2_queue_vector *qvec)
4301{
4302 struct mvpp2_port *port = qvec->port;
4303
4304 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
4305 MVPP2_ISR_ENABLE_INTERRUPT(qvec->sw_thread_mask));
4306}
4307
4308static inline void mvpp2_qvec_interrupt_disable(struct mvpp2_queue_vector *qvec)
4309{
4310 struct mvpp2_port *port = qvec->port;
4311
4312 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
4313 MVPP2_ISR_DISABLE_INTERRUPT(qvec->sw_thread_mask));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004314}
4315
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004316/* Mask the current CPU's Rx/Tx interrupts
4317 * Called by on_each_cpu(), guaranteed to run with migration disabled,
4318 * using smp_processor_id() is OK.
4319 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004320static void mvpp2_interrupts_mask(void *arg)
4321{
4322 struct mvpp2_port *port = arg;
4323
Thomas Petazzonia7868412017-03-07 16:53:13 +01004324 mvpp2_percpu_write(port->priv, smp_processor_id(),
4325 MVPP2_ISR_RX_TX_MASK_REG(port->id), 0);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004326}
4327
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004328/* Unmask the current CPU's Rx/Tx interrupts.
4329 * Called by on_each_cpu(), guaranteed to run with migration disabled,
4330 * using smp_processor_id() is OK.
4331 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004332static void mvpp2_interrupts_unmask(void *arg)
4333{
4334 struct mvpp2_port *port = arg;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02004335 u32 val;
4336
4337 val = MVPP2_CAUSE_MISC_SUM_MASK |
4338 MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
4339 if (port->has_tx_irqs)
4340 val |= MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004341
Thomas Petazzonia7868412017-03-07 16:53:13 +01004342 mvpp2_percpu_write(port->priv, smp_processor_id(),
Thomas Petazzoni213f4282017-08-03 10:42:00 +02004343 MVPP2_ISR_RX_TX_MASK_REG(port->id), val);
4344}
4345
4346static void
4347mvpp2_shared_interrupt_mask_unmask(struct mvpp2_port *port, bool mask)
4348{
4349 u32 val;
4350 int i;
4351
4352 if (port->priv->hw_version != MVPP22)
4353 return;
4354
4355 if (mask)
4356 val = 0;
4357 else
4358 val = MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
4359
4360 for (i = 0; i < port->nqvecs; i++) {
4361 struct mvpp2_queue_vector *v = port->qvecs + i;
4362
4363 if (v->type != MVPP2_QUEUE_VECTOR_SHARED)
4364 continue;
4365
4366 mvpp2_percpu_write(port->priv, v->sw_thread_id,
4367 MVPP2_ISR_RX_TX_MASK_REG(port->id), val);
4368 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004369}
4370
4371/* Port configuration routines */
4372
Antoine Ténartf84bf382017-08-22 19:08:27 +02004373static void mvpp22_gop_init_rgmii(struct mvpp2_port *port)
4374{
4375 struct mvpp2 *priv = port->priv;
4376 u32 val;
4377
4378 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
4379 val |= GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT;
4380 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
4381
4382 regmap_read(priv->sysctrl_base, GENCONF_CTRL0, &val);
4383 if (port->gop_id == 2)
4384 val |= GENCONF_CTRL0_PORT0_RGMII | GENCONF_CTRL0_PORT1_RGMII;
4385 else if (port->gop_id == 3)
4386 val |= GENCONF_CTRL0_PORT1_RGMII_MII;
4387 regmap_write(priv->sysctrl_base, GENCONF_CTRL0, val);
4388}
4389
4390static void mvpp22_gop_init_sgmii(struct mvpp2_port *port)
4391{
4392 struct mvpp2 *priv = port->priv;
4393 u32 val;
4394
4395 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
4396 val |= GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT |
4397 GENCONF_PORT_CTRL0_RX_DATA_SAMPLE;
4398 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
4399
4400 if (port->gop_id > 1) {
4401 regmap_read(priv->sysctrl_base, GENCONF_CTRL0, &val);
4402 if (port->gop_id == 2)
4403 val &= ~GENCONF_CTRL0_PORT0_RGMII;
4404 else if (port->gop_id == 3)
4405 val &= ~GENCONF_CTRL0_PORT1_RGMII_MII;
4406 regmap_write(priv->sysctrl_base, GENCONF_CTRL0, val);
4407 }
4408}
4409
4410static void mvpp22_gop_init_10gkr(struct mvpp2_port *port)
4411{
4412 struct mvpp2 *priv = port->priv;
4413 void __iomem *mpcs = priv->iface_base + MVPP22_MPCS_BASE(port->gop_id);
4414 void __iomem *xpcs = priv->iface_base + MVPP22_XPCS_BASE(port->gop_id);
4415 u32 val;
4416
4417 /* XPCS */
4418 val = readl(xpcs + MVPP22_XPCS_CFG0);
4419 val &= ~(MVPP22_XPCS_CFG0_PCS_MODE(0x3) |
4420 MVPP22_XPCS_CFG0_ACTIVE_LANE(0x3));
4421 val |= MVPP22_XPCS_CFG0_ACTIVE_LANE(2);
4422 writel(val, xpcs + MVPP22_XPCS_CFG0);
4423
4424 /* MPCS */
4425 val = readl(mpcs + MVPP22_MPCS_CTRL);
4426 val &= ~MVPP22_MPCS_CTRL_FWD_ERR_CONN;
4427 writel(val, mpcs + MVPP22_MPCS_CTRL);
4428
4429 val = readl(mpcs + MVPP22_MPCS_CLK_RESET);
4430 val &= ~(MVPP22_MPCS_CLK_RESET_DIV_RATIO(0x7) | MAC_CLK_RESET_MAC |
4431 MAC_CLK_RESET_SD_RX | MAC_CLK_RESET_SD_TX);
4432 val |= MVPP22_MPCS_CLK_RESET_DIV_RATIO(1);
4433 writel(val, mpcs + MVPP22_MPCS_CLK_RESET);
4434
4435 val &= ~MVPP22_MPCS_CLK_RESET_DIV_SET;
4436 val |= MAC_CLK_RESET_MAC | MAC_CLK_RESET_SD_RX | MAC_CLK_RESET_SD_TX;
4437 writel(val, mpcs + MVPP22_MPCS_CLK_RESET);
4438}
4439
4440static int mvpp22_gop_init(struct mvpp2_port *port)
4441{
4442 struct mvpp2 *priv = port->priv;
4443 u32 val;
4444
4445 if (!priv->sysctrl_base)
4446 return 0;
4447
4448 switch (port->phy_interface) {
4449 case PHY_INTERFACE_MODE_RGMII:
4450 case PHY_INTERFACE_MODE_RGMII_ID:
4451 case PHY_INTERFACE_MODE_RGMII_RXID:
4452 case PHY_INTERFACE_MODE_RGMII_TXID:
4453 if (port->gop_id == 0)
4454 goto invalid_conf;
4455 mvpp22_gop_init_rgmii(port);
4456 break;
4457 case PHY_INTERFACE_MODE_SGMII:
4458 mvpp22_gop_init_sgmii(port);
4459 break;
4460 case PHY_INTERFACE_MODE_10GKR:
4461 if (port->gop_id != 0)
4462 goto invalid_conf;
4463 mvpp22_gop_init_10gkr(port);
4464 break;
4465 default:
4466 goto unsupported_conf;
4467 }
4468
4469 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL1, &val);
4470 val |= GENCONF_PORT_CTRL1_RESET(port->gop_id) |
4471 GENCONF_PORT_CTRL1_EN(port->gop_id);
4472 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL1, val);
4473
4474 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
4475 val |= GENCONF_PORT_CTRL0_CLK_DIV_PHASE_CLR;
4476 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
4477
4478 regmap_read(priv->sysctrl_base, GENCONF_SOFT_RESET1, &val);
4479 val |= GENCONF_SOFT_RESET1_GOP;
4480 regmap_write(priv->sysctrl_base, GENCONF_SOFT_RESET1, val);
4481
4482unsupported_conf:
4483 return 0;
4484
4485invalid_conf:
4486 netdev_err(port->dev, "Invalid port configuration\n");
4487 return -EINVAL;
4488}
4489
Antoine Tenartfd3651b2017-09-01 11:04:54 +02004490static void mvpp22_gop_unmask_irq(struct mvpp2_port *port)
4491{
4492 u32 val;
4493
4494 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
4495 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4496 /* Enable the GMAC link status irq for this port */
4497 val = readl(port->base + MVPP22_GMAC_INT_SUM_MASK);
4498 val |= MVPP22_GMAC_INT_SUM_MASK_LINK_STAT;
4499 writel(val, port->base + MVPP22_GMAC_INT_SUM_MASK);
4500 }
4501
4502 if (port->gop_id == 0) {
4503 /* Enable the XLG/GIG irqs for this port */
4504 val = readl(port->base + MVPP22_XLG_EXT_INT_MASK);
4505 if (port->phy_interface == PHY_INTERFACE_MODE_10GKR)
4506 val |= MVPP22_XLG_EXT_INT_MASK_XLG;
4507 else
4508 val |= MVPP22_XLG_EXT_INT_MASK_GIG;
4509 writel(val, port->base + MVPP22_XLG_EXT_INT_MASK);
4510 }
4511}
4512
4513static void mvpp22_gop_mask_irq(struct mvpp2_port *port)
4514{
4515 u32 val;
4516
4517 if (port->gop_id == 0) {
4518 val = readl(port->base + MVPP22_XLG_EXT_INT_MASK);
4519 val &= ~(MVPP22_XLG_EXT_INT_MASK_XLG |
4520 MVPP22_XLG_EXT_INT_MASK_GIG);
4521 writel(val, port->base + MVPP22_XLG_EXT_INT_MASK);
4522 }
4523
4524 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
4525 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4526 val = readl(port->base + MVPP22_GMAC_INT_SUM_MASK);
4527 val &= ~MVPP22_GMAC_INT_SUM_MASK_LINK_STAT;
4528 writel(val, port->base + MVPP22_GMAC_INT_SUM_MASK);
4529 }
4530}
4531
4532static void mvpp22_gop_setup_irq(struct mvpp2_port *port)
4533{
4534 u32 val;
4535
4536 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
4537 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4538 val = readl(port->base + MVPP22_GMAC_INT_MASK);
4539 val |= MVPP22_GMAC_INT_MASK_LINK_STAT;
4540 writel(val, port->base + MVPP22_GMAC_INT_MASK);
4541 }
4542
4543 if (port->gop_id == 0) {
4544 val = readl(port->base + MVPP22_XLG_INT_MASK);
4545 val |= MVPP22_XLG_INT_MASK_LINK;
4546 writel(val, port->base + MVPP22_XLG_INT_MASK);
4547 }
4548
4549 mvpp22_gop_unmask_irq(port);
4550}
4551
Antoine Tenart542897d2017-08-30 10:29:15 +02004552static int mvpp22_comphy_init(struct mvpp2_port *port)
4553{
4554 enum phy_mode mode;
4555 int ret;
4556
4557 if (!port->comphy)
4558 return 0;
4559
4560 switch (port->phy_interface) {
4561 case PHY_INTERFACE_MODE_SGMII:
4562 mode = PHY_MODE_SGMII;
4563 break;
4564 case PHY_INTERFACE_MODE_10GKR:
4565 mode = PHY_MODE_10GKR;
4566 break;
4567 default:
4568 return -EINVAL;
4569 }
4570
4571 ret = phy_set_mode(port->comphy, mode);
4572 if (ret)
4573 return ret;
4574
4575 return phy_power_on(port->comphy);
4576}
4577
Antoine Ténart39193572017-08-22 19:08:24 +02004578static void mvpp2_port_mii_gmac_configure_mode(struct mvpp2_port *port)
4579{
4580 u32 val;
4581
4582 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4583 val = readl(port->base + MVPP22_GMAC_CTRL_4_REG);
4584 val |= MVPP22_CTRL4_SYNC_BYPASS_DIS | MVPP22_CTRL4_DP_CLK_SEL |
4585 MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
4586 val &= ~MVPP22_CTRL4_EXT_PIN_GMII_SEL;
4587 writel(val, port->base + MVPP22_GMAC_CTRL_4_REG);
4588
4589 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4590 val |= MVPP2_GMAC_DISABLE_PADDING;
4591 val &= ~MVPP2_GMAC_FLOW_CTRL_MASK;
4592 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
Antoine Tenart1df22702017-09-01 11:04:52 +02004593 } else if (phy_interface_mode_is_rgmii(port->phy_interface)) {
Antoine Ténart39193572017-08-22 19:08:24 +02004594 val = readl(port->base + MVPP22_GMAC_CTRL_4_REG);
4595 val |= MVPP22_CTRL4_EXT_PIN_GMII_SEL |
4596 MVPP22_CTRL4_SYNC_BYPASS_DIS |
4597 MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
4598 val &= ~MVPP22_CTRL4_DP_CLK_SEL;
4599 writel(val, port->base + MVPP22_GMAC_CTRL_4_REG);
4600
4601 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4602 val &= ~MVPP2_GMAC_DISABLE_PADDING;
4603 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4604 }
4605
4606 /* The port is connected to a copper PHY */
4607 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4608 val &= ~MVPP2_GMAC_PORT_TYPE_MASK;
4609 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4610
4611 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4612 val |= MVPP2_GMAC_IN_BAND_AUTONEG_BYPASS |
4613 MVPP2_GMAC_AN_SPEED_EN | MVPP2_GMAC_FLOW_CTRL_AUTONEG |
4614 MVPP2_GMAC_AN_DUPLEX_EN;
4615 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
4616 val |= MVPP2_GMAC_IN_BAND_AUTONEG;
4617 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4618}
4619
4620static void mvpp2_port_mii_gmac_configure(struct mvpp2_port *port)
4621{
4622 u32 val;
4623
4624 /* Force link down */
4625 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4626 val &= ~MVPP2_GMAC_FORCE_LINK_PASS;
4627 val |= MVPP2_GMAC_FORCE_LINK_DOWN;
4628 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4629
4630 /* Set the GMAC in a reset state */
4631 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4632 val |= MVPP2_GMAC_PORT_RESET_MASK;
4633 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4634
4635 /* Configure the PCS and in-band AN */
4636 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4637 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4638 val |= MVPP2_GMAC_INBAND_AN_MASK | MVPP2_GMAC_PCS_ENABLE_MASK;
Antoine Tenart1df22702017-09-01 11:04:52 +02004639 } else if (phy_interface_mode_is_rgmii(port->phy_interface)) {
Antoine Ténart39193572017-08-22 19:08:24 +02004640 val &= ~MVPP2_GMAC_PCS_ENABLE_MASK;
Antoine Ténart39193572017-08-22 19:08:24 +02004641 }
4642 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4643
4644 mvpp2_port_mii_gmac_configure_mode(port);
4645
4646 /* Unset the GMAC reset state */
4647 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4648 val &= ~MVPP2_GMAC_PORT_RESET_MASK;
4649 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4650
4651 /* Stop forcing link down */
4652 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4653 val &= ~MVPP2_GMAC_FORCE_LINK_DOWN;
4654 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4655}
4656
Antoine Ténart77321952017-08-22 19:08:25 +02004657static void mvpp2_port_mii_xlg_configure(struct mvpp2_port *port)
4658{
4659 u32 val;
4660
4661 if (port->gop_id != 0)
4662 return;
4663
4664 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
4665 val |= MVPP22_XLG_CTRL0_RX_FLOW_CTRL_EN;
4666 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
4667
4668 val = readl(port->base + MVPP22_XLG_CTRL4_REG);
4669 val &= ~MVPP22_XLG_CTRL4_MACMODSELECT_GMAC;
4670 val |= MVPP22_XLG_CTRL4_FWD_FC | MVPP22_XLG_CTRL4_FWD_PFC;
4671 writel(val, port->base + MVPP22_XLG_CTRL4_REG);
4672}
4673
Thomas Petazzoni26975822017-03-07 16:53:14 +01004674static void mvpp22_port_mii_set(struct mvpp2_port *port)
4675{
4676 u32 val;
4677
Thomas Petazzoni26975822017-03-07 16:53:14 +01004678 /* Only GOP port 0 has an XLG MAC */
4679 if (port->gop_id == 0) {
4680 val = readl(port->base + MVPP22_XLG_CTRL3_REG);
4681 val &= ~MVPP22_XLG_CTRL3_MACMODESELECT_MASK;
Antoine Ténart725757a2017-06-12 16:01:39 +02004682
4683 if (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
4684 port->phy_interface == PHY_INTERFACE_MODE_10GKR)
4685 val |= MVPP22_XLG_CTRL3_MACMODESELECT_10G;
4686 else
4687 val |= MVPP22_XLG_CTRL3_MACMODESELECT_GMAC;
4688
Thomas Petazzoni26975822017-03-07 16:53:14 +01004689 writel(val, port->base + MVPP22_XLG_CTRL3_REG);
4690 }
Thomas Petazzoni26975822017-03-07 16:53:14 +01004691}
4692
Marcin Wojtas3f518502014-07-10 16:52:13 -03004693static void mvpp2_port_mii_set(struct mvpp2_port *port)
4694{
Thomas Petazzoni26975822017-03-07 16:53:14 +01004695 if (port->priv->hw_version == MVPP22)
4696 mvpp22_port_mii_set(port);
4697
Antoine Tenart1df22702017-09-01 11:04:52 +02004698 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
Antoine Ténart39193572017-08-22 19:08:24 +02004699 port->phy_interface == PHY_INTERFACE_MODE_SGMII)
4700 mvpp2_port_mii_gmac_configure(port);
Antoine Ténart77321952017-08-22 19:08:25 +02004701 else if (port->phy_interface == PHY_INTERFACE_MODE_10GKR)
4702 mvpp2_port_mii_xlg_configure(port);
Marcin Wojtas08a23752014-07-21 13:48:12 -03004703}
4704
4705static void mvpp2_port_fc_adv_enable(struct mvpp2_port *port)
4706{
4707 u32 val;
4708
4709 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4710 val |= MVPP2_GMAC_FC_ADV_EN;
4711 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004712}
4713
4714static void mvpp2_port_enable(struct mvpp2_port *port)
4715{
4716 u32 val;
4717
Antoine Ténart725757a2017-06-12 16:01:39 +02004718 /* Only GOP port 0 has an XLG MAC */
4719 if (port->gop_id == 0 &&
4720 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
4721 port->phy_interface == PHY_INTERFACE_MODE_10GKR)) {
4722 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
4723 val |= MVPP22_XLG_CTRL0_PORT_EN |
4724 MVPP22_XLG_CTRL0_MAC_RESET_DIS;
4725 val &= ~MVPP22_XLG_CTRL0_MIB_CNT_DIS;
4726 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
4727 } else {
4728 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4729 val |= MVPP2_GMAC_PORT_EN_MASK;
4730 val |= MVPP2_GMAC_MIB_CNTR_EN_MASK;
4731 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4732 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004733}
4734
4735static void mvpp2_port_disable(struct mvpp2_port *port)
4736{
4737 u32 val;
4738
Antoine Ténart725757a2017-06-12 16:01:39 +02004739 /* Only GOP port 0 has an XLG MAC */
4740 if (port->gop_id == 0 &&
4741 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
4742 port->phy_interface == PHY_INTERFACE_MODE_10GKR)) {
4743 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
4744 val &= ~(MVPP22_XLG_CTRL0_PORT_EN |
4745 MVPP22_XLG_CTRL0_MAC_RESET_DIS);
4746 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
4747 } else {
4748 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4749 val &= ~(MVPP2_GMAC_PORT_EN_MASK);
4750 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4751 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004752}
4753
4754/* Set IEEE 802.3x Flow Control Xon Packet Transmission Mode */
4755static void mvpp2_port_periodic_xon_disable(struct mvpp2_port *port)
4756{
4757 u32 val;
4758
4759 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG) &
4760 ~MVPP2_GMAC_PERIODIC_XON_EN_MASK;
4761 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
4762}
4763
4764/* Configure loopback port */
4765static void mvpp2_port_loopback_set(struct mvpp2_port *port)
4766{
4767 u32 val;
4768
4769 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
4770
4771 if (port->speed == 1000)
4772 val |= MVPP2_GMAC_GMII_LB_EN_MASK;
4773 else
4774 val &= ~MVPP2_GMAC_GMII_LB_EN_MASK;
4775
4776 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
4777 val |= MVPP2_GMAC_PCS_LB_EN_MASK;
4778 else
4779 val &= ~MVPP2_GMAC_PCS_LB_EN_MASK;
4780
4781 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
4782}
4783
4784static void mvpp2_port_reset(struct mvpp2_port *port)
4785{
4786 u32 val;
4787
4788 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
4789 ~MVPP2_GMAC_PORT_RESET_MASK;
4790 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4791
4792 while (readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
4793 MVPP2_GMAC_PORT_RESET_MASK)
4794 continue;
4795}
4796
4797/* Change maximum receive size of the port */
4798static inline void mvpp2_gmac_max_rx_size_set(struct mvpp2_port *port)
4799{
4800 u32 val;
4801
4802 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4803 val &= ~MVPP2_GMAC_MAX_RX_SIZE_MASK;
4804 val |= (((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
4805 MVPP2_GMAC_MAX_RX_SIZE_OFFS);
4806 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4807}
4808
Stefan Chulski76eb1b12017-08-22 19:08:26 +02004809/* Change maximum receive size of the port */
4810static inline void mvpp2_xlg_max_rx_size_set(struct mvpp2_port *port)
4811{
4812 u32 val;
4813
4814 val = readl(port->base + MVPP22_XLG_CTRL1_REG);
4815 val &= ~MVPP22_XLG_CTRL1_FRAMESIZELIMIT_MASK;
4816 val |= ((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
Antoine Ténartec15ecd2017-08-25 15:24:46 +02004817 MVPP22_XLG_CTRL1_FRAMESIZELIMIT_OFFS;
Stefan Chulski76eb1b12017-08-22 19:08:26 +02004818 writel(val, port->base + MVPP22_XLG_CTRL1_REG);
4819}
4820
Marcin Wojtas3f518502014-07-10 16:52:13 -03004821/* Set defaults to the MVPP2 port */
4822static void mvpp2_defaults_set(struct mvpp2_port *port)
4823{
4824 int tx_port_num, val, queue, ptxq, lrxq;
4825
Thomas Petazzoni3d9017d2017-03-07 16:53:11 +01004826 if (port->priv->hw_version == MVPP21) {
4827 /* Configure port to loopback if needed */
4828 if (port->flags & MVPP2_F_LOOPBACK)
4829 mvpp2_port_loopback_set(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004830
Thomas Petazzoni3d9017d2017-03-07 16:53:11 +01004831 /* Update TX FIFO MIN Threshold */
4832 val = readl(port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
4833 val &= ~MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK;
4834 /* Min. TX threshold must be less than minimal packet length */
4835 val |= MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(64 - 4 - 2);
4836 writel(val, port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
4837 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004838
4839 /* Disable Legacy WRR, Disable EJP, Release from reset */
4840 tx_port_num = mvpp2_egress_port(port);
4841 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG,
4842 tx_port_num);
4843 mvpp2_write(port->priv, MVPP2_TXP_SCHED_CMD_1_REG, 0);
4844
4845 /* Close bandwidth for all queues */
4846 for (queue = 0; queue < MVPP2_MAX_TXQ; queue++) {
4847 ptxq = mvpp2_txq_phys(port->id, queue);
4848 mvpp2_write(port->priv,
4849 MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(ptxq), 0);
4850 }
4851
4852 /* Set refill period to 1 usec, refill tokens
4853 * and bucket size to maximum
4854 */
4855 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PERIOD_REG,
4856 port->priv->tclk / USEC_PER_SEC);
4857 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_REFILL_REG);
4858 val &= ~MVPP2_TXP_REFILL_PERIOD_ALL_MASK;
4859 val |= MVPP2_TXP_REFILL_PERIOD_MASK(1);
4860 val |= MVPP2_TXP_REFILL_TOKENS_ALL_MASK;
4861 mvpp2_write(port->priv, MVPP2_TXP_SCHED_REFILL_REG, val);
4862 val = MVPP2_TXP_TOKEN_SIZE_MAX;
4863 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
4864
4865 /* Set MaximumLowLatencyPacketSize value to 256 */
4866 mvpp2_write(port->priv, MVPP2_RX_CTRL_REG(port->id),
4867 MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK |
4868 MVPP2_RX_LOW_LATENCY_PKT_SIZE(256));
4869
4870 /* Enable Rx cache snoop */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004871 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004872 queue = port->rxqs[lrxq]->id;
4873 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
4874 val |= MVPP2_SNOOP_PKT_SIZE_MASK |
4875 MVPP2_SNOOP_BUF_HDR_MASK;
4876 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
4877 }
4878
4879 /* At default, mask all interrupts to all present cpus */
4880 mvpp2_interrupts_disable(port);
4881}
4882
4883/* Enable/disable receiving packets */
4884static void mvpp2_ingress_enable(struct mvpp2_port *port)
4885{
4886 u32 val;
4887 int lrxq, queue;
4888
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004889 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004890 queue = port->rxqs[lrxq]->id;
4891 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
4892 val &= ~MVPP2_RXQ_DISABLE_MASK;
4893 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
4894 }
4895}
4896
4897static void mvpp2_ingress_disable(struct mvpp2_port *port)
4898{
4899 u32 val;
4900 int lrxq, queue;
4901
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004902 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004903 queue = port->rxqs[lrxq]->id;
4904 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
4905 val |= MVPP2_RXQ_DISABLE_MASK;
4906 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
4907 }
4908}
4909
4910/* Enable transmit via physical egress queue
4911 * - HW starts take descriptors from DRAM
4912 */
4913static void mvpp2_egress_enable(struct mvpp2_port *port)
4914{
4915 u32 qmap;
4916 int queue;
4917 int tx_port_num = mvpp2_egress_port(port);
4918
4919 /* Enable all initialized TXs. */
4920 qmap = 0;
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004921 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004922 struct mvpp2_tx_queue *txq = port->txqs[queue];
4923
Markus Elfringdbbb2f02017-04-17 14:07:52 +02004924 if (txq->descs)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004925 qmap |= (1 << queue);
4926 }
4927
4928 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
4929 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG, qmap);
4930}
4931
4932/* Disable transmit via physical egress queue
4933 * - HW doesn't take descriptors from DRAM
4934 */
4935static void mvpp2_egress_disable(struct mvpp2_port *port)
4936{
4937 u32 reg_data;
4938 int delay;
4939 int tx_port_num = mvpp2_egress_port(port);
4940
4941 /* Issue stop command for active channels only */
4942 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
4943 reg_data = (mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG)) &
4944 MVPP2_TXP_SCHED_ENQ_MASK;
4945 if (reg_data != 0)
4946 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG,
4947 (reg_data << MVPP2_TXP_SCHED_DISQ_OFFSET));
4948
4949 /* Wait for all Tx activity to terminate. */
4950 delay = 0;
4951 do {
4952 if (delay >= MVPP2_TX_DISABLE_TIMEOUT_MSEC) {
4953 netdev_warn(port->dev,
4954 "Tx stop timed out, status=0x%08x\n",
4955 reg_data);
4956 break;
4957 }
4958 mdelay(1);
4959 delay++;
4960
4961 /* Check port TX Command register that all
4962 * Tx queues are stopped
4963 */
4964 reg_data = mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG);
4965 } while (reg_data & MVPP2_TXP_SCHED_ENQ_MASK);
4966}
4967
4968/* Rx descriptors helper methods */
4969
4970/* Get number of Rx descriptors occupied by received packets */
4971static inline int
4972mvpp2_rxq_received(struct mvpp2_port *port, int rxq_id)
4973{
4974 u32 val = mvpp2_read(port->priv, MVPP2_RXQ_STATUS_REG(rxq_id));
4975
4976 return val & MVPP2_RXQ_OCCUPIED_MASK;
4977}
4978
4979/* Update Rx queue status with the number of occupied and available
4980 * Rx descriptor slots.
4981 */
4982static inline void
4983mvpp2_rxq_status_update(struct mvpp2_port *port, int rxq_id,
4984 int used_count, int free_count)
4985{
4986 /* Decrement the number of used descriptors and increment count
4987 * increment the number of free descriptors.
4988 */
4989 u32 val = used_count | (free_count << MVPP2_RXQ_NUM_NEW_OFFSET);
4990
4991 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_UPDATE_REG(rxq_id), val);
4992}
4993
4994/* Get pointer to next RX descriptor to be processed by SW */
4995static inline struct mvpp2_rx_desc *
4996mvpp2_rxq_next_desc_get(struct mvpp2_rx_queue *rxq)
4997{
4998 int rx_desc = rxq->next_desc_to_proc;
4999
5000 rxq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(rxq, rx_desc);
5001 prefetch(rxq->descs + rxq->next_desc_to_proc);
5002 return rxq->descs + rx_desc;
5003}
5004
5005/* Set rx queue offset */
5006static void mvpp2_rxq_offset_set(struct mvpp2_port *port,
5007 int prxq, int offset)
5008{
5009 u32 val;
5010
5011 /* Convert offset from bytes to units of 32 bytes */
5012 offset = offset >> 5;
5013
5014 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
5015 val &= ~MVPP2_RXQ_PACKET_OFFSET_MASK;
5016
5017 /* Offset is in */
5018 val |= ((offset << MVPP2_RXQ_PACKET_OFFSET_OFFS) &
5019 MVPP2_RXQ_PACKET_OFFSET_MASK);
5020
5021 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
5022}
5023
Marcin Wojtas3f518502014-07-10 16:52:13 -03005024/* Tx descriptors helper methods */
5025
Marcin Wojtas3f518502014-07-10 16:52:13 -03005026/* Get pointer to next Tx descriptor to be processed (send) by HW */
5027static struct mvpp2_tx_desc *
5028mvpp2_txq_next_desc_get(struct mvpp2_tx_queue *txq)
5029{
5030 int tx_desc = txq->next_desc_to_proc;
5031
5032 txq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(txq, tx_desc);
5033 return txq->descs + tx_desc;
5034}
5035
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005036/* Update HW with number of aggregated Tx descriptors to be sent
5037 *
5038 * Called only from mvpp2_tx(), so migration is disabled, using
5039 * smp_processor_id() is OK.
5040 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03005041static void mvpp2_aggr_txq_pend_desc_add(struct mvpp2_port *port, int pending)
5042{
5043 /* aggregated access - relevant TXQ number is written in TX desc */
Thomas Petazzonia7868412017-03-07 16:53:13 +01005044 mvpp2_percpu_write(port->priv, smp_processor_id(),
5045 MVPP2_AGGR_TXQ_UPDATE_REG, pending);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005046}
5047
5048
5049/* Check if there are enough free descriptors in aggregated txq.
5050 * If not, update the number of occupied descriptors and repeat the check.
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005051 *
5052 * Called only from mvpp2_tx(), so migration is disabled, using
5053 * smp_processor_id() is OK.
Marcin Wojtas3f518502014-07-10 16:52:13 -03005054 */
5055static int mvpp2_aggr_desc_num_check(struct mvpp2 *priv,
5056 struct mvpp2_tx_queue *aggr_txq, int num)
5057{
Antoine Tenart02856a32017-10-30 11:23:32 +01005058 if ((aggr_txq->count + num) > MVPP2_AGGR_TXQ_SIZE) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005059 /* Update number of occupied aggregated Tx descriptors */
5060 int cpu = smp_processor_id();
5061 u32 val = mvpp2_read(priv, MVPP2_AGGR_TXQ_STATUS_REG(cpu));
5062
5063 aggr_txq->count = val & MVPP2_AGGR_TXQ_PENDING_MASK;
5064 }
5065
Antoine Tenart02856a32017-10-30 11:23:32 +01005066 if ((aggr_txq->count + num) > MVPP2_AGGR_TXQ_SIZE)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005067 return -ENOMEM;
5068
5069 return 0;
5070}
5071
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005072/* Reserved Tx descriptors allocation request
5073 *
5074 * Called only from mvpp2_txq_reserved_desc_num_proc(), itself called
5075 * only by mvpp2_tx(), so migration is disabled, using
5076 * smp_processor_id() is OK.
5077 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03005078static int mvpp2_txq_alloc_reserved_desc(struct mvpp2 *priv,
5079 struct mvpp2_tx_queue *txq, int num)
5080{
5081 u32 val;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005082 int cpu = smp_processor_id();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005083
5084 val = (txq->id << MVPP2_TXQ_RSVD_REQ_Q_OFFSET) | num;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005085 mvpp2_percpu_write(priv, cpu, MVPP2_TXQ_RSVD_REQ_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005086
Thomas Petazzonia7868412017-03-07 16:53:13 +01005087 val = mvpp2_percpu_read(priv, cpu, MVPP2_TXQ_RSVD_RSLT_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005088
5089 return val & MVPP2_TXQ_RSVD_RSLT_MASK;
5090}
5091
5092/* Check if there are enough reserved descriptors for transmission.
5093 * If not, request chunk of reserved descriptors and check again.
5094 */
5095static int mvpp2_txq_reserved_desc_num_proc(struct mvpp2 *priv,
5096 struct mvpp2_tx_queue *txq,
5097 struct mvpp2_txq_pcpu *txq_pcpu,
5098 int num)
5099{
5100 int req, cpu, desc_count;
5101
5102 if (txq_pcpu->reserved_num >= num)
5103 return 0;
5104
5105 /* Not enough descriptors reserved! Update the reserved descriptor
5106 * count and check again.
5107 */
5108
5109 desc_count = 0;
5110 /* Compute total of used descriptors */
5111 for_each_present_cpu(cpu) {
5112 struct mvpp2_txq_pcpu *txq_pcpu_aux;
5113
5114 txq_pcpu_aux = per_cpu_ptr(txq->pcpu, cpu);
5115 desc_count += txq_pcpu_aux->count;
5116 desc_count += txq_pcpu_aux->reserved_num;
5117 }
5118
5119 req = max(MVPP2_CPU_DESC_CHUNK, num - txq_pcpu->reserved_num);
5120 desc_count += req;
5121
5122 if (desc_count >
5123 (txq->size - (num_present_cpus() * MVPP2_CPU_DESC_CHUNK)))
5124 return -ENOMEM;
5125
5126 txq_pcpu->reserved_num += mvpp2_txq_alloc_reserved_desc(priv, txq, req);
5127
5128 /* OK, the descriptor cound has been updated: check again. */
5129 if (txq_pcpu->reserved_num < num)
5130 return -ENOMEM;
5131 return 0;
5132}
5133
5134/* Release the last allocated Tx descriptor. Useful to handle DMA
5135 * mapping failures in the Tx path.
5136 */
5137static void mvpp2_txq_desc_put(struct mvpp2_tx_queue *txq)
5138{
5139 if (txq->next_desc_to_proc == 0)
5140 txq->next_desc_to_proc = txq->last_desc - 1;
5141 else
5142 txq->next_desc_to_proc--;
5143}
5144
5145/* Set Tx descriptors fields relevant for CSUM calculation */
5146static u32 mvpp2_txq_desc_csum(int l3_offs, int l3_proto,
5147 int ip_hdr_len, int l4_proto)
5148{
5149 u32 command;
5150
5151 /* fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
5152 * G_L4_chk, L4_type required only for checksum calculation
5153 */
5154 command = (l3_offs << MVPP2_TXD_L3_OFF_SHIFT);
5155 command |= (ip_hdr_len << MVPP2_TXD_IP_HLEN_SHIFT);
5156 command |= MVPP2_TXD_IP_CSUM_DISABLE;
5157
5158 if (l3_proto == swab16(ETH_P_IP)) {
5159 command &= ~MVPP2_TXD_IP_CSUM_DISABLE; /* enable IPv4 csum */
5160 command &= ~MVPP2_TXD_L3_IP6; /* enable IPv4 */
5161 } else {
5162 command |= MVPP2_TXD_L3_IP6; /* enable IPv6 */
5163 }
5164
5165 if (l4_proto == IPPROTO_TCP) {
5166 command &= ~MVPP2_TXD_L4_UDP; /* enable TCP */
5167 command &= ~MVPP2_TXD_L4_CSUM_FRAG; /* generate L4 csum */
5168 } else if (l4_proto == IPPROTO_UDP) {
5169 command |= MVPP2_TXD_L4_UDP; /* enable UDP */
5170 command &= ~MVPP2_TXD_L4_CSUM_FRAG; /* generate L4 csum */
5171 } else {
5172 command |= MVPP2_TXD_L4_CSUM_NOT;
5173 }
5174
5175 return command;
5176}
5177
5178/* Get number of sent descriptors and decrement counter.
5179 * The number of sent descriptors is returned.
5180 * Per-CPU access
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005181 *
5182 * Called only from mvpp2_txq_done(), called from mvpp2_tx()
5183 * (migration disabled) and from the TX completion tasklet (migration
5184 * disabled) so using smp_processor_id() is OK.
Marcin Wojtas3f518502014-07-10 16:52:13 -03005185 */
5186static inline int mvpp2_txq_sent_desc_proc(struct mvpp2_port *port,
5187 struct mvpp2_tx_queue *txq)
5188{
5189 u32 val;
5190
5191 /* Reading status reg resets transmitted descriptor counter */
Thomas Petazzonia7868412017-03-07 16:53:13 +01005192 val = mvpp2_percpu_read(port->priv, smp_processor_id(),
5193 MVPP2_TXQ_SENT_REG(txq->id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005194
5195 return (val & MVPP2_TRANSMITTED_COUNT_MASK) >>
5196 MVPP2_TRANSMITTED_COUNT_OFFSET;
5197}
5198
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005199/* Called through on_each_cpu(), so runs on all CPUs, with migration
5200 * disabled, therefore using smp_processor_id() is OK.
5201 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03005202static void mvpp2_txq_sent_counter_clear(void *arg)
5203{
5204 struct mvpp2_port *port = arg;
5205 int queue;
5206
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005207 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005208 int id = port->txqs[queue]->id;
5209
Thomas Petazzonia7868412017-03-07 16:53:13 +01005210 mvpp2_percpu_read(port->priv, smp_processor_id(),
5211 MVPP2_TXQ_SENT_REG(id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005212 }
5213}
5214
5215/* Set max sizes for Tx queues */
5216static void mvpp2_txp_max_tx_size_set(struct mvpp2_port *port)
5217{
5218 u32 val, size, mtu;
5219 int txq, tx_port_num;
5220
5221 mtu = port->pkt_size * 8;
5222 if (mtu > MVPP2_TXP_MTU_MAX)
5223 mtu = MVPP2_TXP_MTU_MAX;
5224
5225 /* WA for wrong Token bucket update: Set MTU value = 3*real MTU value */
5226 mtu = 3 * mtu;
5227
5228 /* Indirect access to registers */
5229 tx_port_num = mvpp2_egress_port(port);
5230 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
5231
5232 /* Set MTU */
5233 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_MTU_REG);
5234 val &= ~MVPP2_TXP_MTU_MAX;
5235 val |= mtu;
5236 mvpp2_write(port->priv, MVPP2_TXP_SCHED_MTU_REG, val);
5237
5238 /* TXP token size and all TXQs token size must be larger that MTU */
5239 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG);
5240 size = val & MVPP2_TXP_TOKEN_SIZE_MAX;
5241 if (size < mtu) {
5242 size = mtu;
5243 val &= ~MVPP2_TXP_TOKEN_SIZE_MAX;
5244 val |= size;
5245 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
5246 }
5247
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005248 for (txq = 0; txq < port->ntxqs; txq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005249 val = mvpp2_read(port->priv,
5250 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq));
5251 size = val & MVPP2_TXQ_TOKEN_SIZE_MAX;
5252
5253 if (size < mtu) {
5254 size = mtu;
5255 val &= ~MVPP2_TXQ_TOKEN_SIZE_MAX;
5256 val |= size;
5257 mvpp2_write(port->priv,
5258 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq),
5259 val);
5260 }
5261 }
5262}
5263
5264/* Set the number of packets that will be received before Rx interrupt
5265 * will be generated by HW.
5266 */
5267static void mvpp2_rx_pkts_coal_set(struct mvpp2_port *port,
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01005268 struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005269{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005270 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005271
Thomas Petazzonif8b0d5f2017-02-21 11:28:03 +01005272 if (rxq->pkts_coal > MVPP2_OCCUPIED_THRESH_MASK)
5273 rxq->pkts_coal = MVPP2_OCCUPIED_THRESH_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005274
Thomas Petazzonia7868412017-03-07 16:53:13 +01005275 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
5276 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_THRESH_REG,
5277 rxq->pkts_coal);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005278
5279 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005280}
5281
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005282/* For some reason in the LSP this is done on each CPU. Why ? */
5283static void mvpp2_tx_pkts_coal_set(struct mvpp2_port *port,
5284 struct mvpp2_tx_queue *txq)
5285{
5286 int cpu = get_cpu();
5287 u32 val;
5288
5289 if (txq->done_pkts_coal > MVPP2_TXQ_THRESH_MASK)
5290 txq->done_pkts_coal = MVPP2_TXQ_THRESH_MASK;
5291
5292 val = (txq->done_pkts_coal << MVPP2_TXQ_THRESH_OFFSET);
5293 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5294 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_THRESH_REG, val);
5295
5296 put_cpu();
5297}
5298
Thomas Petazzoniab426762017-02-21 11:28:04 +01005299static u32 mvpp2_usec_to_cycles(u32 usec, unsigned long clk_hz)
5300{
5301 u64 tmp = (u64)clk_hz * usec;
5302
5303 do_div(tmp, USEC_PER_SEC);
5304
5305 return tmp > U32_MAX ? U32_MAX : tmp;
5306}
5307
5308static u32 mvpp2_cycles_to_usec(u32 cycles, unsigned long clk_hz)
5309{
5310 u64 tmp = (u64)cycles * USEC_PER_SEC;
5311
5312 do_div(tmp, clk_hz);
5313
5314 return tmp > U32_MAX ? U32_MAX : tmp;
5315}
5316
Marcin Wojtas3f518502014-07-10 16:52:13 -03005317/* Set the time delay in usec before Rx interrupt */
5318static void mvpp2_rx_time_coal_set(struct mvpp2_port *port,
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01005319 struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005320{
Thomas Petazzoniab426762017-02-21 11:28:04 +01005321 unsigned long freq = port->priv->tclk;
5322 u32 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005323
Thomas Petazzoniab426762017-02-21 11:28:04 +01005324 if (val > MVPP2_MAX_ISR_RX_THRESHOLD) {
5325 rxq->time_coal =
5326 mvpp2_cycles_to_usec(MVPP2_MAX_ISR_RX_THRESHOLD, freq);
5327
5328 /* re-evaluate to get actual register value */
5329 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
5330 }
5331
Marcin Wojtas3f518502014-07-10 16:52:13 -03005332 mvpp2_write(port->priv, MVPP2_ISR_RX_THRESHOLD_REG(rxq->id), val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005333}
5334
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005335static void mvpp2_tx_time_coal_set(struct mvpp2_port *port)
5336{
5337 unsigned long freq = port->priv->tclk;
5338 u32 val = mvpp2_usec_to_cycles(port->tx_time_coal, freq);
5339
5340 if (val > MVPP2_MAX_ISR_TX_THRESHOLD) {
5341 port->tx_time_coal =
5342 mvpp2_cycles_to_usec(MVPP2_MAX_ISR_TX_THRESHOLD, freq);
5343
5344 /* re-evaluate to get actual register value */
5345 val = mvpp2_usec_to_cycles(port->tx_time_coal, freq);
5346 }
5347
5348 mvpp2_write(port->priv, MVPP2_ISR_TX_THRESHOLD_REG(port->id), val);
5349}
5350
Marcin Wojtas3f518502014-07-10 16:52:13 -03005351/* Free Tx queue skbuffs */
5352static void mvpp2_txq_bufs_free(struct mvpp2_port *port,
5353 struct mvpp2_tx_queue *txq,
5354 struct mvpp2_txq_pcpu *txq_pcpu, int num)
5355{
5356 int i;
5357
5358 for (i = 0; i < num; i++) {
Thomas Petazzoni83544912016-12-21 11:28:49 +01005359 struct mvpp2_txq_pcpu_buf *tx_buf =
5360 txq_pcpu->buffs + txq_pcpu->txq_get_index;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005361
Antoine Tenart20920262017-10-23 15:24:30 +02005362 if (!IS_TSO_HEADER(txq_pcpu, tx_buf->dma))
5363 dma_unmap_single(port->dev->dev.parent, tx_buf->dma,
5364 tx_buf->size, DMA_TO_DEVICE);
Thomas Petazzoni36fb7432017-02-21 11:28:05 +01005365 if (tx_buf->skb)
5366 dev_kfree_skb_any(tx_buf->skb);
5367
5368 mvpp2_txq_inc_get(txq_pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005369 }
5370}
5371
5372static inline struct mvpp2_rx_queue *mvpp2_get_rx_queue(struct mvpp2_port *port,
5373 u32 cause)
5374{
5375 int queue = fls(cause) - 1;
5376
5377 return port->rxqs[queue];
5378}
5379
5380static inline struct mvpp2_tx_queue *mvpp2_get_tx_queue(struct mvpp2_port *port,
5381 u32 cause)
5382{
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005383 int queue = fls(cause) - 1;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005384
5385 return port->txqs[queue];
5386}
5387
5388/* Handle end of transmission */
5389static void mvpp2_txq_done(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
5390 struct mvpp2_txq_pcpu *txq_pcpu)
5391{
5392 struct netdev_queue *nq = netdev_get_tx_queue(port->dev, txq->log_id);
5393 int tx_done;
5394
5395 if (txq_pcpu->cpu != smp_processor_id())
5396 netdev_err(port->dev, "wrong cpu on the end of Tx processing\n");
5397
5398 tx_done = mvpp2_txq_sent_desc_proc(port, txq);
5399 if (!tx_done)
5400 return;
5401 mvpp2_txq_bufs_free(port, txq, txq_pcpu, tx_done);
5402
5403 txq_pcpu->count -= tx_done;
5404
5405 if (netif_tx_queue_stopped(nq))
Antoine Tenart1d17db02017-10-30 11:23:31 +01005406 if (txq_pcpu->count <= txq_pcpu->wake_threshold)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005407 netif_tx_wake_queue(nq);
5408}
5409
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005410static unsigned int mvpp2_tx_done(struct mvpp2_port *port, u32 cause,
5411 int cpu)
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005412{
5413 struct mvpp2_tx_queue *txq;
5414 struct mvpp2_txq_pcpu *txq_pcpu;
5415 unsigned int tx_todo = 0;
5416
5417 while (cause) {
5418 txq = mvpp2_get_tx_queue(port, cause);
5419 if (!txq)
5420 break;
5421
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005422 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005423
5424 if (txq_pcpu->count) {
5425 mvpp2_txq_done(port, txq, txq_pcpu);
5426 tx_todo += txq_pcpu->count;
5427 }
5428
5429 cause &= ~(1 << txq->log_id);
5430 }
5431 return tx_todo;
5432}
5433
Marcin Wojtas3f518502014-07-10 16:52:13 -03005434/* Rx/Tx queue initialization/cleanup methods */
5435
5436/* Allocate and initialize descriptors for aggr TXQ */
5437static int mvpp2_aggr_txq_init(struct platform_device *pdev,
Antoine Ténart85affd72017-08-23 09:46:55 +02005438 struct mvpp2_tx_queue *aggr_txq, int cpu,
Marcin Wojtas3f518502014-07-10 16:52:13 -03005439 struct mvpp2 *priv)
5440{
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005441 u32 txq_dma;
5442
Marcin Wojtas3f518502014-07-10 16:52:13 -03005443 /* Allocate memory for TX descriptors */
5444 aggr_txq->descs = dma_alloc_coherent(&pdev->dev,
Antoine Ténart85affd72017-08-23 09:46:55 +02005445 MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005446 &aggr_txq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005447 if (!aggr_txq->descs)
5448 return -ENOMEM;
5449
Antoine Tenart02856a32017-10-30 11:23:32 +01005450 aggr_txq->last_desc = MVPP2_AGGR_TXQ_SIZE - 1;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005451
5452 /* Aggr TXQ no reset WA */
5453 aggr_txq->next_desc_to_proc = mvpp2_read(priv,
5454 MVPP2_AGGR_TXQ_INDEX_REG(cpu));
5455
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005456 /* Set Tx descriptors queue starting address indirect
5457 * access
5458 */
5459 if (priv->hw_version == MVPP21)
5460 txq_dma = aggr_txq->descs_dma;
5461 else
5462 txq_dma = aggr_txq->descs_dma >>
5463 MVPP22_AGGR_TXQ_DESC_ADDR_OFFS;
5464
5465 mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu), txq_dma);
Antoine Ténart85affd72017-08-23 09:46:55 +02005466 mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu),
5467 MVPP2_AGGR_TXQ_SIZE);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005468
5469 return 0;
5470}
5471
5472/* Create a specified Rx queue */
5473static int mvpp2_rxq_init(struct mvpp2_port *port,
5474 struct mvpp2_rx_queue *rxq)
5475
5476{
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005477 u32 rxq_dma;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005478 int cpu;
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005479
Marcin Wojtas3f518502014-07-10 16:52:13 -03005480 rxq->size = port->rx_ring_size;
5481
5482 /* Allocate memory for RX descriptors */
5483 rxq->descs = dma_alloc_coherent(port->dev->dev.parent,
5484 rxq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005485 &rxq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005486 if (!rxq->descs)
5487 return -ENOMEM;
5488
Marcin Wojtas3f518502014-07-10 16:52:13 -03005489 rxq->last_desc = rxq->size - 1;
5490
5491 /* Zero occupied and non-occupied counters - direct access */
5492 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
5493
5494 /* Set Rx descriptors queue starting address - indirect access */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005495 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005496 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005497 if (port->priv->hw_version == MVPP21)
5498 rxq_dma = rxq->descs_dma;
5499 else
5500 rxq_dma = rxq->descs_dma >> MVPP22_DESC_ADDR_OFFS;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005501 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_ADDR_REG, rxq_dma);
5502 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_SIZE_REG, rxq->size);
5503 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_INDEX_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005504 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005505
5506 /* Set Offset */
5507 mvpp2_rxq_offset_set(port, rxq->id, NET_SKB_PAD);
5508
5509 /* Set coalescing pkts and time */
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01005510 mvpp2_rx_pkts_coal_set(port, rxq);
5511 mvpp2_rx_time_coal_set(port, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005512
5513 /* Add number of descriptors ready for receiving packets */
5514 mvpp2_rxq_status_update(port, rxq->id, 0, rxq->size);
5515
5516 return 0;
5517}
5518
5519/* Push packets received by the RXQ to BM pool */
5520static void mvpp2_rxq_drop_pkts(struct mvpp2_port *port,
5521 struct mvpp2_rx_queue *rxq)
5522{
5523 int rx_received, i;
5524
5525 rx_received = mvpp2_rxq_received(port, rxq->id);
5526 if (!rx_received)
5527 return;
5528
5529 for (i = 0; i < rx_received; i++) {
5530 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005531 u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
5532 int pool;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005533
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005534 pool = (status & MVPP2_RXD_BM_POOL_ID_MASK) >>
5535 MVPP2_RXD_BM_POOL_ID_OFFS;
5536
Thomas Petazzoni7d7627b2017-06-22 14:23:20 +02005537 mvpp2_bm_pool_put(port, pool,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005538 mvpp2_rxdesc_dma_addr_get(port, rx_desc),
5539 mvpp2_rxdesc_cookie_get(port, rx_desc));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005540 }
5541 mvpp2_rxq_status_update(port, rxq->id, rx_received, rx_received);
5542}
5543
5544/* Cleanup Rx queue */
5545static void mvpp2_rxq_deinit(struct mvpp2_port *port,
5546 struct mvpp2_rx_queue *rxq)
5547{
Thomas Petazzonia7868412017-03-07 16:53:13 +01005548 int cpu;
5549
Marcin Wojtas3f518502014-07-10 16:52:13 -03005550 mvpp2_rxq_drop_pkts(port, rxq);
5551
5552 if (rxq->descs)
5553 dma_free_coherent(port->dev->dev.parent,
5554 rxq->size * MVPP2_DESC_ALIGNED_SIZE,
5555 rxq->descs,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005556 rxq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005557
5558 rxq->descs = NULL;
5559 rxq->last_desc = 0;
5560 rxq->next_desc_to_proc = 0;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005561 rxq->descs_dma = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005562
5563 /* Clear Rx descriptors queue starting address and size;
5564 * free descriptor number
5565 */
5566 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005567 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005568 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
5569 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_ADDR_REG, 0);
5570 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_SIZE_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005571 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005572}
5573
5574/* Create and initialize a Tx queue */
5575static int mvpp2_txq_init(struct mvpp2_port *port,
5576 struct mvpp2_tx_queue *txq)
5577{
5578 u32 val;
5579 int cpu, desc, desc_per_txq, tx_port_num;
5580 struct mvpp2_txq_pcpu *txq_pcpu;
5581
5582 txq->size = port->tx_ring_size;
5583
5584 /* Allocate memory for Tx descriptors */
5585 txq->descs = dma_alloc_coherent(port->dev->dev.parent,
5586 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005587 &txq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005588 if (!txq->descs)
5589 return -ENOMEM;
5590
Marcin Wojtas3f518502014-07-10 16:52:13 -03005591 txq->last_desc = txq->size - 1;
5592
5593 /* Set Tx descriptors queue starting address - indirect access */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005594 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005595 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5596 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_ADDR_REG,
5597 txq->descs_dma);
5598 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_SIZE_REG,
5599 txq->size & MVPP2_TXQ_DESC_SIZE_MASK);
5600 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_INDEX_REG, 0);
5601 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_RSVD_CLR_REG,
5602 txq->id << MVPP2_TXQ_RSVD_CLR_OFFSET);
5603 val = mvpp2_percpu_read(port->priv, cpu, MVPP2_TXQ_PENDING_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005604 val &= ~MVPP2_TXQ_PENDING_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005605 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PENDING_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005606
5607 /* Calculate base address in prefetch buffer. We reserve 16 descriptors
5608 * for each existing TXQ.
5609 * TCONTS for PON port must be continuous from 0 to MVPP2_MAX_TCONT
5610 * GBE ports assumed to be continious from 0 to MVPP2_MAX_PORTS
5611 */
5612 desc_per_txq = 16;
5613 desc = (port->id * MVPP2_MAX_TXQ * desc_per_txq) +
5614 (txq->log_id * desc_per_txq);
5615
Thomas Petazzonia7868412017-03-07 16:53:13 +01005616 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG,
5617 MVPP2_PREF_BUF_PTR(desc) | MVPP2_PREF_BUF_SIZE_16 |
5618 MVPP2_PREF_BUF_THRESH(desc_per_txq / 2));
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005619 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005620
5621 /* WRR / EJP configuration - indirect access */
5622 tx_port_num = mvpp2_egress_port(port);
5623 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
5624
5625 val = mvpp2_read(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id));
5626 val &= ~MVPP2_TXQ_REFILL_PERIOD_ALL_MASK;
5627 val |= MVPP2_TXQ_REFILL_PERIOD_MASK(1);
5628 val |= MVPP2_TXQ_REFILL_TOKENS_ALL_MASK;
5629 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id), val);
5630
5631 val = MVPP2_TXQ_TOKEN_SIZE_MAX;
5632 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq->log_id),
5633 val);
5634
5635 for_each_present_cpu(cpu) {
5636 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
5637 txq_pcpu->size = txq->size;
Markus Elfring02c91ec2017-04-17 08:09:07 +02005638 txq_pcpu->buffs = kmalloc_array(txq_pcpu->size,
5639 sizeof(*txq_pcpu->buffs),
5640 GFP_KERNEL);
Thomas Petazzoni83544912016-12-21 11:28:49 +01005641 if (!txq_pcpu->buffs)
Markus Elfring20b1e162017-04-17 12:58:33 +02005642 goto cleanup;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005643
5644 txq_pcpu->count = 0;
5645 txq_pcpu->reserved_num = 0;
5646 txq_pcpu->txq_put_index = 0;
5647 txq_pcpu->txq_get_index = 0;
Antoine Ténart186cd4d2017-08-23 09:46:56 +02005648
Antoine Tenart1d17db02017-10-30 11:23:31 +01005649 txq_pcpu->stop_threshold = txq->size - MVPP2_MAX_SKB_DESCS;
5650 txq_pcpu->wake_threshold = txq_pcpu->stop_threshold / 2;
5651
Antoine Ténart186cd4d2017-08-23 09:46:56 +02005652 txq_pcpu->tso_headers =
5653 dma_alloc_coherent(port->dev->dev.parent,
Yan Markman822eaf72017-10-23 15:24:29 +02005654 txq_pcpu->size * TSO_HEADER_SIZE,
Antoine Ténart186cd4d2017-08-23 09:46:56 +02005655 &txq_pcpu->tso_headers_dma,
5656 GFP_KERNEL);
5657 if (!txq_pcpu->tso_headers)
5658 goto cleanup;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005659 }
5660
5661 return 0;
Markus Elfring20b1e162017-04-17 12:58:33 +02005662cleanup:
Marcin Wojtas71ce3912015-08-06 19:00:29 +02005663 for_each_present_cpu(cpu) {
5664 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Thomas Petazzoni83544912016-12-21 11:28:49 +01005665 kfree(txq_pcpu->buffs);
Antoine Ténart186cd4d2017-08-23 09:46:56 +02005666
5667 dma_free_coherent(port->dev->dev.parent,
Yan Markman822eaf72017-10-23 15:24:29 +02005668 txq_pcpu->size * TSO_HEADER_SIZE,
Antoine Ténart186cd4d2017-08-23 09:46:56 +02005669 txq_pcpu->tso_headers,
5670 txq_pcpu->tso_headers_dma);
Marcin Wojtas71ce3912015-08-06 19:00:29 +02005671 }
5672
5673 dma_free_coherent(port->dev->dev.parent,
5674 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005675 txq->descs, txq->descs_dma);
Marcin Wojtas71ce3912015-08-06 19:00:29 +02005676
5677 return -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005678}
5679
5680/* Free allocated TXQ resources */
5681static void mvpp2_txq_deinit(struct mvpp2_port *port,
5682 struct mvpp2_tx_queue *txq)
5683{
5684 struct mvpp2_txq_pcpu *txq_pcpu;
5685 int cpu;
5686
5687 for_each_present_cpu(cpu) {
5688 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Thomas Petazzoni83544912016-12-21 11:28:49 +01005689 kfree(txq_pcpu->buffs);
Antoine Ténart186cd4d2017-08-23 09:46:56 +02005690
5691 dma_free_coherent(port->dev->dev.parent,
Yan Markman822eaf72017-10-23 15:24:29 +02005692 txq_pcpu->size * TSO_HEADER_SIZE,
Antoine Ténart186cd4d2017-08-23 09:46:56 +02005693 txq_pcpu->tso_headers,
5694 txq_pcpu->tso_headers_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005695 }
5696
5697 if (txq->descs)
5698 dma_free_coherent(port->dev->dev.parent,
5699 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005700 txq->descs, txq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005701
5702 txq->descs = NULL;
5703 txq->last_desc = 0;
5704 txq->next_desc_to_proc = 0;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005705 txq->descs_dma = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005706
5707 /* Set minimum bandwidth for disabled TXQs */
5708 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(txq->id), 0);
5709
5710 /* Set Tx descriptors queue starting address and size */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005711 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005712 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5713 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_ADDR_REG, 0);
5714 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_SIZE_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005715 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005716}
5717
5718/* Cleanup Tx ports */
5719static void mvpp2_txq_clean(struct mvpp2_port *port, struct mvpp2_tx_queue *txq)
5720{
5721 struct mvpp2_txq_pcpu *txq_pcpu;
5722 int delay, pending, cpu;
5723 u32 val;
5724
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005725 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005726 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5727 val = mvpp2_percpu_read(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005728 val |= MVPP2_TXQ_DRAIN_EN_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005729 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005730
5731 /* The napi queue has been stopped so wait for all packets
5732 * to be transmitted.
5733 */
5734 delay = 0;
5735 do {
5736 if (delay >= MVPP2_TX_PENDING_TIMEOUT_MSEC) {
5737 netdev_warn(port->dev,
5738 "port %d: cleaning queue %d timed out\n",
5739 port->id, txq->log_id);
5740 break;
5741 }
5742 mdelay(1);
5743 delay++;
5744
Thomas Petazzonia7868412017-03-07 16:53:13 +01005745 pending = mvpp2_percpu_read(port->priv, cpu,
5746 MVPP2_TXQ_PENDING_REG);
5747 pending &= MVPP2_TXQ_PENDING_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005748 } while (pending);
5749
5750 val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005751 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG, val);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005752 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005753
5754 for_each_present_cpu(cpu) {
5755 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
5756
5757 /* Release all packets */
5758 mvpp2_txq_bufs_free(port, txq, txq_pcpu, txq_pcpu->count);
5759
5760 /* Reset queue */
5761 txq_pcpu->count = 0;
5762 txq_pcpu->txq_put_index = 0;
5763 txq_pcpu->txq_get_index = 0;
5764 }
5765}
5766
5767/* Cleanup all Tx queues */
5768static void mvpp2_cleanup_txqs(struct mvpp2_port *port)
5769{
5770 struct mvpp2_tx_queue *txq;
5771 int queue;
5772 u32 val;
5773
5774 val = mvpp2_read(port->priv, MVPP2_TX_PORT_FLUSH_REG);
5775
5776 /* Reset Tx ports and delete Tx queues */
5777 val |= MVPP2_TX_PORT_FLUSH_MASK(port->id);
5778 mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
5779
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005780 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005781 txq = port->txqs[queue];
5782 mvpp2_txq_clean(port, txq);
5783 mvpp2_txq_deinit(port, txq);
5784 }
5785
5786 on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
5787
5788 val &= ~MVPP2_TX_PORT_FLUSH_MASK(port->id);
5789 mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
5790}
5791
5792/* Cleanup all Rx queues */
5793static void mvpp2_cleanup_rxqs(struct mvpp2_port *port)
5794{
5795 int queue;
5796
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005797 for (queue = 0; queue < port->nrxqs; queue++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005798 mvpp2_rxq_deinit(port, port->rxqs[queue]);
5799}
5800
5801/* Init all Rx queues for port */
5802static int mvpp2_setup_rxqs(struct mvpp2_port *port)
5803{
5804 int queue, err;
5805
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005806 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005807 err = mvpp2_rxq_init(port, port->rxqs[queue]);
5808 if (err)
5809 goto err_cleanup;
5810 }
5811 return 0;
5812
5813err_cleanup:
5814 mvpp2_cleanup_rxqs(port);
5815 return err;
5816}
5817
5818/* Init all tx queues for port */
5819static int mvpp2_setup_txqs(struct mvpp2_port *port)
5820{
5821 struct mvpp2_tx_queue *txq;
5822 int queue, err;
5823
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005824 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005825 txq = port->txqs[queue];
5826 err = mvpp2_txq_init(port, txq);
5827 if (err)
5828 goto err_cleanup;
5829 }
5830
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005831 if (port->has_tx_irqs) {
5832 mvpp2_tx_time_coal_set(port);
5833 for (queue = 0; queue < port->ntxqs; queue++) {
5834 txq = port->txqs[queue];
5835 mvpp2_tx_pkts_coal_set(port, txq);
5836 }
5837 }
5838
Marcin Wojtas3f518502014-07-10 16:52:13 -03005839 on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
5840 return 0;
5841
5842err_cleanup:
5843 mvpp2_cleanup_txqs(port);
5844 return err;
5845}
5846
5847/* The callback for per-port interrupt */
5848static irqreturn_t mvpp2_isr(int irq, void *dev_id)
5849{
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02005850 struct mvpp2_queue_vector *qv = dev_id;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005851
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02005852 mvpp2_qvec_interrupt_disable(qv);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005853
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02005854 napi_schedule(&qv->napi);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005855
5856 return IRQ_HANDLED;
5857}
5858
Antoine Tenartfd3651b2017-09-01 11:04:54 +02005859/* Per-port interrupt for link status changes */
5860static irqreturn_t mvpp2_link_status_isr(int irq, void *dev_id)
5861{
5862 struct mvpp2_port *port = (struct mvpp2_port *)dev_id;
5863 struct net_device *dev = port->dev;
5864 bool event = false, link = false;
5865 u32 val;
5866
5867 mvpp22_gop_mask_irq(port);
5868
5869 if (port->gop_id == 0 &&
5870 port->phy_interface == PHY_INTERFACE_MODE_10GKR) {
5871 val = readl(port->base + MVPP22_XLG_INT_STAT);
5872 if (val & MVPP22_XLG_INT_STAT_LINK) {
5873 event = true;
5874 val = readl(port->base + MVPP22_XLG_STATUS);
5875 if (val & MVPP22_XLG_STATUS_LINK_UP)
5876 link = true;
5877 }
5878 } else if (phy_interface_mode_is_rgmii(port->phy_interface) ||
5879 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
5880 val = readl(port->base + MVPP22_GMAC_INT_STAT);
5881 if (val & MVPP22_GMAC_INT_STAT_LINK) {
5882 event = true;
5883 val = readl(port->base + MVPP2_GMAC_STATUS0);
5884 if (val & MVPP2_GMAC_STATUS0_LINK_UP)
5885 link = true;
5886 }
5887 }
5888
5889 if (!netif_running(dev) || !event)
5890 goto handled;
5891
5892 if (link) {
5893 mvpp2_interrupts_enable(port);
5894
5895 mvpp2_egress_enable(port);
5896 mvpp2_ingress_enable(port);
5897 netif_carrier_on(dev);
5898 netif_tx_wake_all_queues(dev);
5899 } else {
5900 netif_tx_stop_all_queues(dev);
5901 netif_carrier_off(dev);
5902 mvpp2_ingress_disable(port);
5903 mvpp2_egress_disable(port);
5904
5905 mvpp2_interrupts_disable(port);
5906 }
5907
5908handled:
5909 mvpp22_gop_unmask_irq(port);
5910 return IRQ_HANDLED;
5911}
5912
Antoine Tenart65a2c092017-08-30 10:29:18 +02005913static void mvpp2_gmac_set_autoneg(struct mvpp2_port *port,
5914 struct phy_device *phydev)
5915{
5916 u32 val;
5917
5918 if (port->phy_interface != PHY_INTERFACE_MODE_RGMII &&
5919 port->phy_interface != PHY_INTERFACE_MODE_RGMII_ID &&
5920 port->phy_interface != PHY_INTERFACE_MODE_RGMII_RXID &&
5921 port->phy_interface != PHY_INTERFACE_MODE_RGMII_TXID &&
5922 port->phy_interface != PHY_INTERFACE_MODE_SGMII)
5923 return;
5924
5925 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5926 val &= ~(MVPP2_GMAC_CONFIG_MII_SPEED |
5927 MVPP2_GMAC_CONFIG_GMII_SPEED |
5928 MVPP2_GMAC_CONFIG_FULL_DUPLEX |
5929 MVPP2_GMAC_AN_SPEED_EN |
5930 MVPP2_GMAC_AN_DUPLEX_EN);
5931
5932 if (phydev->duplex)
5933 val |= MVPP2_GMAC_CONFIG_FULL_DUPLEX;
5934
5935 if (phydev->speed == SPEED_1000)
5936 val |= MVPP2_GMAC_CONFIG_GMII_SPEED;
5937 else if (phydev->speed == SPEED_100)
5938 val |= MVPP2_GMAC_CONFIG_MII_SPEED;
5939
5940 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
Antoine Tenart65a2c092017-08-30 10:29:18 +02005941}
5942
Marcin Wojtas3f518502014-07-10 16:52:13 -03005943/* Adjust link */
5944static void mvpp2_link_event(struct net_device *dev)
5945{
5946 struct mvpp2_port *port = netdev_priv(dev);
Philippe Reynes8e072692016-06-28 00:08:11 +02005947 struct phy_device *phydev = dev->phydev;
Antoine Tenart89273bc2017-08-30 10:29:19 +02005948 bool link_reconfigured = false;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005949 u32 val;
5950
5951 if (phydev->link) {
Antoine Tenart89273bc2017-08-30 10:29:19 +02005952 if (port->phy_interface != phydev->interface && port->comphy) {
5953 /* disable current port for reconfiguration */
5954 mvpp2_interrupts_disable(port);
5955 netif_carrier_off(port->dev);
5956 mvpp2_port_disable(port);
5957 phy_power_off(port->comphy);
5958
5959 /* comphy reconfiguration */
5960 port->phy_interface = phydev->interface;
5961 mvpp22_comphy_init(port);
5962
5963 /* gop/mac reconfiguration */
5964 mvpp22_gop_init(port);
5965 mvpp2_port_mii_set(port);
5966
5967 link_reconfigured = true;
5968 }
5969
Marcin Wojtas3f518502014-07-10 16:52:13 -03005970 if ((port->speed != phydev->speed) ||
5971 (port->duplex != phydev->duplex)) {
Antoine Tenart65a2c092017-08-30 10:29:18 +02005972 mvpp2_gmac_set_autoneg(port, phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005973
5974 port->duplex = phydev->duplex;
5975 port->speed = phydev->speed;
5976 }
5977 }
5978
Antoine Tenart89273bc2017-08-30 10:29:19 +02005979 if (phydev->link != port->link || link_reconfigured) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005980 port->link = phydev->link;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005981
Marcin Wojtas3f518502014-07-10 16:52:13 -03005982 if (phydev->link) {
Antoine Tenart65a2c092017-08-30 10:29:18 +02005983 if (port->phy_interface == PHY_INTERFACE_MODE_RGMII ||
5984 port->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
5985 port->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID ||
5986 port->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID ||
5987 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
5988 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5989 val |= (MVPP2_GMAC_FORCE_LINK_PASS |
5990 MVPP2_GMAC_FORCE_LINK_DOWN);
5991 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5992 }
Antoine Tenartf55744a2017-08-30 10:29:17 +02005993
5994 mvpp2_interrupts_enable(port);
5995 mvpp2_port_enable(port);
5996
Marcin Wojtas3f518502014-07-10 16:52:13 -03005997 mvpp2_egress_enable(port);
5998 mvpp2_ingress_enable(port);
Antoine Tenartf55744a2017-08-30 10:29:17 +02005999 netif_carrier_on(dev);
6000 netif_tx_wake_all_queues(dev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006001 } else {
Antoine Tenart968b2112017-08-30 10:29:16 +02006002 port->duplex = -1;
6003 port->speed = 0;
6004
Antoine Tenartf55744a2017-08-30 10:29:17 +02006005 netif_tx_stop_all_queues(dev);
6006 netif_carrier_off(dev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006007 mvpp2_ingress_disable(port);
6008 mvpp2_egress_disable(port);
Antoine Tenartf55744a2017-08-30 10:29:17 +02006009
6010 mvpp2_port_disable(port);
6011 mvpp2_interrupts_disable(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006012 }
Antoine Tenart968b2112017-08-30 10:29:16 +02006013
Marcin Wojtas3f518502014-07-10 16:52:13 -03006014 phy_print_status(phydev);
6015 }
6016}
6017
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006018static void mvpp2_timer_set(struct mvpp2_port_pcpu *port_pcpu)
6019{
6020 ktime_t interval;
6021
6022 if (!port_pcpu->timer_scheduled) {
6023 port_pcpu->timer_scheduled = true;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01006024 interval = MVPP2_TXDONE_HRTIMER_PERIOD_NS;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006025 hrtimer_start(&port_pcpu->tx_done_timer, interval,
6026 HRTIMER_MODE_REL_PINNED);
6027 }
6028}
6029
6030static void mvpp2_tx_proc_cb(unsigned long data)
6031{
6032 struct net_device *dev = (struct net_device *)data;
6033 struct mvpp2_port *port = netdev_priv(dev);
6034 struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
6035 unsigned int tx_todo, cause;
6036
6037 if (!netif_running(dev))
6038 return;
6039 port_pcpu->timer_scheduled = false;
6040
6041 /* Process all the Tx queues */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006042 cause = (1 << port->ntxqs) - 1;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006043 tx_todo = mvpp2_tx_done(port, cause, smp_processor_id());
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006044
6045 /* Set the timer in case not all the packets were processed */
6046 if (tx_todo)
6047 mvpp2_timer_set(port_pcpu);
6048}
6049
6050static enum hrtimer_restart mvpp2_hr_timer_cb(struct hrtimer *timer)
6051{
6052 struct mvpp2_port_pcpu *port_pcpu = container_of(timer,
6053 struct mvpp2_port_pcpu,
6054 tx_done_timer);
6055
6056 tasklet_schedule(&port_pcpu->tx_done_tasklet);
6057
6058 return HRTIMER_NORESTART;
6059}
6060
Marcin Wojtas3f518502014-07-10 16:52:13 -03006061/* Main RX/TX processing routines */
6062
6063/* Display more error info */
6064static void mvpp2_rx_error(struct mvpp2_port *port,
6065 struct mvpp2_rx_desc *rx_desc)
6066{
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006067 u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
6068 size_t sz = mvpp2_rxdesc_size_get(port, rx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006069
6070 switch (status & MVPP2_RXD_ERR_CODE_MASK) {
6071 case MVPP2_RXD_ERR_CRC:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006072 netdev_err(port->dev, "bad rx status %08x (crc error), size=%zu\n",
6073 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006074 break;
6075 case MVPP2_RXD_ERR_OVERRUN:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006076 netdev_err(port->dev, "bad rx status %08x (overrun error), size=%zu\n",
6077 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006078 break;
6079 case MVPP2_RXD_ERR_RESOURCE:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006080 netdev_err(port->dev, "bad rx status %08x (resource error), size=%zu\n",
6081 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006082 break;
6083 }
6084}
6085
6086/* Handle RX checksum offload */
6087static void mvpp2_rx_csum(struct mvpp2_port *port, u32 status,
6088 struct sk_buff *skb)
6089{
6090 if (((status & MVPP2_RXD_L3_IP4) &&
6091 !(status & MVPP2_RXD_IP4_HEADER_ERR)) ||
6092 (status & MVPP2_RXD_L3_IP6))
6093 if (((status & MVPP2_RXD_L4_UDP) ||
6094 (status & MVPP2_RXD_L4_TCP)) &&
6095 (status & MVPP2_RXD_L4_CSUM_OK)) {
6096 skb->csum = 0;
6097 skb->ip_summed = CHECKSUM_UNNECESSARY;
6098 return;
6099 }
6100
6101 skb->ip_summed = CHECKSUM_NONE;
6102}
6103
6104/* Reuse skb if possible, or allocate a new skb and add it to BM pool */
6105static int mvpp2_rx_refill(struct mvpp2_port *port,
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006106 struct mvpp2_bm_pool *bm_pool, int pool)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006107{
Thomas Petazzoni20396132017-03-07 16:53:00 +01006108 dma_addr_t dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01006109 phys_addr_t phys_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006110 void *buf;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006111
Marcin Wojtas3f518502014-07-10 16:52:13 -03006112 /* No recycle or too many buffers are in use, so allocate a new skb */
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01006113 buf = mvpp2_buf_alloc(port, bm_pool, &dma_addr, &phys_addr,
6114 GFP_ATOMIC);
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006115 if (!buf)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006116 return -ENOMEM;
6117
Thomas Petazzoni7d7627b2017-06-22 14:23:20 +02006118 mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
Thomas Petazzoni7ef7e1d2017-02-21 11:28:07 +01006119
Marcin Wojtas3f518502014-07-10 16:52:13 -03006120 return 0;
6121}
6122
6123/* Handle tx checksum */
6124static u32 mvpp2_skb_tx_csum(struct mvpp2_port *port, struct sk_buff *skb)
6125{
6126 if (skb->ip_summed == CHECKSUM_PARTIAL) {
6127 int ip_hdr_len = 0;
6128 u8 l4_proto;
6129
6130 if (skb->protocol == htons(ETH_P_IP)) {
6131 struct iphdr *ip4h = ip_hdr(skb);
6132
6133 /* Calculate IPv4 checksum and L4 checksum */
6134 ip_hdr_len = ip4h->ihl;
6135 l4_proto = ip4h->protocol;
6136 } else if (skb->protocol == htons(ETH_P_IPV6)) {
6137 struct ipv6hdr *ip6h = ipv6_hdr(skb);
6138
6139 /* Read l4_protocol from one of IPv6 extra headers */
6140 if (skb_network_header_len(skb) > 0)
6141 ip_hdr_len = (skb_network_header_len(skb) >> 2);
6142 l4_proto = ip6h->nexthdr;
6143 } else {
6144 return MVPP2_TXD_L4_CSUM_NOT;
6145 }
6146
6147 return mvpp2_txq_desc_csum(skb_network_offset(skb),
6148 skb->protocol, ip_hdr_len, l4_proto);
6149 }
6150
6151 return MVPP2_TXD_L4_CSUM_NOT | MVPP2_TXD_IP_CSUM_DISABLE;
6152}
6153
Marcin Wojtas3f518502014-07-10 16:52:13 -03006154/* Main rx processing */
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006155static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi,
6156 int rx_todo, struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006157{
6158 struct net_device *dev = port->dev;
Marcin Wojtasb5015852015-12-03 15:20:51 +01006159 int rx_received;
6160 int rx_done = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006161 u32 rcvd_pkts = 0;
6162 u32 rcvd_bytes = 0;
6163
6164 /* Get number of received packets and clamp the to-do */
6165 rx_received = mvpp2_rxq_received(port, rxq->id);
6166 if (rx_todo > rx_received)
6167 rx_todo = rx_received;
6168
Marcin Wojtasb5015852015-12-03 15:20:51 +01006169 while (rx_done < rx_todo) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006170 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
6171 struct mvpp2_bm_pool *bm_pool;
6172 struct sk_buff *skb;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006173 unsigned int frag_size;
Thomas Petazzoni20396132017-03-07 16:53:00 +01006174 dma_addr_t dma_addr;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006175 phys_addr_t phys_addr;
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006176 u32 rx_status;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006177 int pool, rx_bytes, err;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006178 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006179
Marcin Wojtasb5015852015-12-03 15:20:51 +01006180 rx_done++;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006181 rx_status = mvpp2_rxdesc_status_get(port, rx_desc);
6182 rx_bytes = mvpp2_rxdesc_size_get(port, rx_desc);
6183 rx_bytes -= MVPP2_MH_SIZE;
6184 dma_addr = mvpp2_rxdesc_dma_addr_get(port, rx_desc);
6185 phys_addr = mvpp2_rxdesc_cookie_get(port, rx_desc);
6186 data = (void *)phys_to_virt(phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006187
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006188 pool = (rx_status & MVPP2_RXD_BM_POOL_ID_MASK) >>
6189 MVPP2_RXD_BM_POOL_ID_OFFS;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006190 bm_pool = &port->priv->bm_pools[pool];
Marcin Wojtas3f518502014-07-10 16:52:13 -03006191
6192 /* In case of an error, release the requested buffer pointer
6193 * to the Buffer Manager. This request process is controlled
6194 * by the hardware, and the information about the buffer is
6195 * comprised by the RX descriptor.
6196 */
6197 if (rx_status & MVPP2_RXD_ERR_SUMMARY) {
Markus Elfring8a524882017-04-17 10:52:02 +02006198err_drop_frame:
Marcin Wojtas3f518502014-07-10 16:52:13 -03006199 dev->stats.rx_errors++;
6200 mvpp2_rx_error(port, rx_desc);
Marcin Wojtasb5015852015-12-03 15:20:51 +01006201 /* Return the buffer to the pool */
Thomas Petazzoni7d7627b2017-06-22 14:23:20 +02006202 mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006203 continue;
6204 }
6205
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006206 if (bm_pool->frag_size > PAGE_SIZE)
6207 frag_size = 0;
6208 else
6209 frag_size = bm_pool->frag_size;
6210
6211 skb = build_skb(data, frag_size);
6212 if (!skb) {
6213 netdev_warn(port->dev, "skb build failed\n");
6214 goto err_drop_frame;
6215 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006216
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006217 err = mvpp2_rx_refill(port, bm_pool, pool);
Marcin Wojtasb5015852015-12-03 15:20:51 +01006218 if (err) {
6219 netdev_err(port->dev, "failed to refill BM pools\n");
6220 goto err_drop_frame;
6221 }
6222
Thomas Petazzoni20396132017-03-07 16:53:00 +01006223 dma_unmap_single(dev->dev.parent, dma_addr,
Marcin Wojtas4229d502015-12-03 15:20:50 +01006224 bm_pool->buf_size, DMA_FROM_DEVICE);
6225
Marcin Wojtas3f518502014-07-10 16:52:13 -03006226 rcvd_pkts++;
6227 rcvd_bytes += rx_bytes;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006228
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006229 skb_reserve(skb, MVPP2_MH_SIZE + NET_SKB_PAD);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006230 skb_put(skb, rx_bytes);
6231 skb->protocol = eth_type_trans(skb, dev);
6232 mvpp2_rx_csum(port, rx_status, skb);
6233
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006234 napi_gro_receive(napi, skb);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006235 }
6236
6237 if (rcvd_pkts) {
6238 struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
6239
6240 u64_stats_update_begin(&stats->syncp);
6241 stats->rx_packets += rcvd_pkts;
6242 stats->rx_bytes += rcvd_bytes;
6243 u64_stats_update_end(&stats->syncp);
6244 }
6245
6246 /* Update Rx queue management counters */
6247 wmb();
Marcin Wojtasb5015852015-12-03 15:20:51 +01006248 mvpp2_rxq_status_update(port, rxq->id, rx_done, rx_done);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006249
6250 return rx_todo;
6251}
6252
6253static inline void
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006254tx_desc_unmap_put(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006255 struct mvpp2_tx_desc *desc)
6256{
Antoine Tenart20920262017-10-23 15:24:30 +02006257 struct mvpp2_txq_pcpu *txq_pcpu = this_cpu_ptr(txq->pcpu);
6258
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006259 dma_addr_t buf_dma_addr =
6260 mvpp2_txdesc_dma_addr_get(port, desc);
6261 size_t buf_sz =
6262 mvpp2_txdesc_size_get(port, desc);
Antoine Tenart20920262017-10-23 15:24:30 +02006263 if (!IS_TSO_HEADER(txq_pcpu, buf_dma_addr))
6264 dma_unmap_single(port->dev->dev.parent, buf_dma_addr,
6265 buf_sz, DMA_TO_DEVICE);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006266 mvpp2_txq_desc_put(txq);
6267}
6268
6269/* Handle tx fragmentation processing */
6270static int mvpp2_tx_frag_process(struct mvpp2_port *port, struct sk_buff *skb,
6271 struct mvpp2_tx_queue *aggr_txq,
6272 struct mvpp2_tx_queue *txq)
6273{
6274 struct mvpp2_txq_pcpu *txq_pcpu = this_cpu_ptr(txq->pcpu);
6275 struct mvpp2_tx_desc *tx_desc;
6276 int i;
Thomas Petazzoni20396132017-03-07 16:53:00 +01006277 dma_addr_t buf_dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006278
6279 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
6280 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6281 void *addr = page_address(frag->page.p) + frag->page_offset;
6282
6283 tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006284 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6285 mvpp2_txdesc_size_set(port, tx_desc, frag->size);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006286
Thomas Petazzoni20396132017-03-07 16:53:00 +01006287 buf_dma_addr = dma_map_single(port->dev->dev.parent, addr,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006288 frag->size,
6289 DMA_TO_DEVICE);
Thomas Petazzoni20396132017-03-07 16:53:00 +01006290 if (dma_mapping_error(port->dev->dev.parent, buf_dma_addr)) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006291 mvpp2_txq_desc_put(txq);
Markus Elfring32bae632017-04-17 11:36:34 +02006292 goto cleanup;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006293 }
6294
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006295 mvpp2_txdesc_offset_set(port, tx_desc,
6296 buf_dma_addr & MVPP2_TX_DESC_ALIGN);
6297 mvpp2_txdesc_dma_addr_set(port, tx_desc,
6298 buf_dma_addr & ~MVPP2_TX_DESC_ALIGN);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006299
6300 if (i == (skb_shinfo(skb)->nr_frags - 1)) {
6301 /* Last descriptor */
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006302 mvpp2_txdesc_cmd_set(port, tx_desc,
6303 MVPP2_TXD_L_DESC);
6304 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006305 } else {
6306 /* Descriptor in the middle: Not First, Not Last */
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006307 mvpp2_txdesc_cmd_set(port, tx_desc, 0);
6308 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006309 }
6310 }
6311
6312 return 0;
Markus Elfring32bae632017-04-17 11:36:34 +02006313cleanup:
Marcin Wojtas3f518502014-07-10 16:52:13 -03006314 /* Release all descriptors that were used to map fragments of
6315 * this packet, as well as the corresponding DMA mappings
6316 */
6317 for (i = i - 1; i >= 0; i--) {
6318 tx_desc = txq->descs + i;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006319 tx_desc_unmap_put(port, txq, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006320 }
6321
6322 return -ENOMEM;
6323}
6324
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006325static inline void mvpp2_tso_put_hdr(struct sk_buff *skb,
6326 struct net_device *dev,
6327 struct mvpp2_tx_queue *txq,
6328 struct mvpp2_tx_queue *aggr_txq,
6329 struct mvpp2_txq_pcpu *txq_pcpu,
6330 int hdr_sz)
6331{
6332 struct mvpp2_port *port = netdev_priv(dev);
6333 struct mvpp2_tx_desc *tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
6334 dma_addr_t addr;
6335
6336 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6337 mvpp2_txdesc_size_set(port, tx_desc, hdr_sz);
6338
6339 addr = txq_pcpu->tso_headers_dma +
6340 txq_pcpu->txq_put_index * TSO_HEADER_SIZE;
6341 mvpp2_txdesc_offset_set(port, tx_desc, addr & MVPP2_TX_DESC_ALIGN);
6342 mvpp2_txdesc_dma_addr_set(port, tx_desc, addr & ~MVPP2_TX_DESC_ALIGN);
6343
6344 mvpp2_txdesc_cmd_set(port, tx_desc, mvpp2_skb_tx_csum(port, skb) |
6345 MVPP2_TXD_F_DESC |
6346 MVPP2_TXD_PADDING_DISABLE);
6347 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
6348}
6349
6350static inline int mvpp2_tso_put_data(struct sk_buff *skb,
6351 struct net_device *dev, struct tso_t *tso,
6352 struct mvpp2_tx_queue *txq,
6353 struct mvpp2_tx_queue *aggr_txq,
6354 struct mvpp2_txq_pcpu *txq_pcpu,
6355 int sz, bool left, bool last)
6356{
6357 struct mvpp2_port *port = netdev_priv(dev);
6358 struct mvpp2_tx_desc *tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
6359 dma_addr_t buf_dma_addr;
6360
6361 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6362 mvpp2_txdesc_size_set(port, tx_desc, sz);
6363
6364 buf_dma_addr = dma_map_single(dev->dev.parent, tso->data, sz,
6365 DMA_TO_DEVICE);
6366 if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
6367 mvpp2_txq_desc_put(txq);
6368 return -ENOMEM;
6369 }
6370
6371 mvpp2_txdesc_offset_set(port, tx_desc,
6372 buf_dma_addr & MVPP2_TX_DESC_ALIGN);
6373 mvpp2_txdesc_dma_addr_set(port, tx_desc,
6374 buf_dma_addr & ~MVPP2_TX_DESC_ALIGN);
6375
6376 if (!left) {
6377 mvpp2_txdesc_cmd_set(port, tx_desc, MVPP2_TXD_L_DESC);
6378 if (last) {
6379 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
6380 return 0;
6381 }
6382 } else {
6383 mvpp2_txdesc_cmd_set(port, tx_desc, 0);
6384 }
6385
6386 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
6387 return 0;
6388}
6389
6390static int mvpp2_tx_tso(struct sk_buff *skb, struct net_device *dev,
6391 struct mvpp2_tx_queue *txq,
6392 struct mvpp2_tx_queue *aggr_txq,
6393 struct mvpp2_txq_pcpu *txq_pcpu)
6394{
6395 struct mvpp2_port *port = netdev_priv(dev);
6396 struct tso_t tso;
6397 int hdr_sz = skb_transport_offset(skb) + tcp_hdrlen(skb);
6398 int i, len, descs = 0;
6399
6400 /* Check number of available descriptors */
6401 if (mvpp2_aggr_desc_num_check(port->priv, aggr_txq,
6402 tso_count_descs(skb)) ||
6403 mvpp2_txq_reserved_desc_num_proc(port->priv, txq, txq_pcpu,
6404 tso_count_descs(skb)))
6405 return 0;
6406
6407 tso_start(skb, &tso);
6408 len = skb->len - hdr_sz;
6409 while (len > 0) {
6410 int left = min_t(int, skb_shinfo(skb)->gso_size, len);
6411 char *hdr = txq_pcpu->tso_headers +
6412 txq_pcpu->txq_put_index * TSO_HEADER_SIZE;
6413
6414 len -= left;
6415 descs++;
6416
6417 tso_build_hdr(skb, hdr, &tso, left, len == 0);
6418 mvpp2_tso_put_hdr(skb, dev, txq, aggr_txq, txq_pcpu, hdr_sz);
6419
6420 while (left > 0) {
6421 int sz = min_t(int, tso.size, left);
6422 left -= sz;
6423 descs++;
6424
6425 if (mvpp2_tso_put_data(skb, dev, &tso, txq, aggr_txq,
6426 txq_pcpu, sz, left, len == 0))
6427 goto release;
6428 tso_build_data(skb, &tso, sz);
6429 }
6430 }
6431
6432 return descs;
6433
6434release:
6435 for (i = descs - 1; i >= 0; i--) {
6436 struct mvpp2_tx_desc *tx_desc = txq->descs + i;
6437 tx_desc_unmap_put(port, txq, tx_desc);
6438 }
6439 return 0;
6440}
6441
Marcin Wojtas3f518502014-07-10 16:52:13 -03006442/* Main tx processing */
6443static int mvpp2_tx(struct sk_buff *skb, struct net_device *dev)
6444{
6445 struct mvpp2_port *port = netdev_priv(dev);
6446 struct mvpp2_tx_queue *txq, *aggr_txq;
6447 struct mvpp2_txq_pcpu *txq_pcpu;
6448 struct mvpp2_tx_desc *tx_desc;
Thomas Petazzoni20396132017-03-07 16:53:00 +01006449 dma_addr_t buf_dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006450 int frags = 0;
6451 u16 txq_id;
6452 u32 tx_cmd;
6453
6454 txq_id = skb_get_queue_mapping(skb);
6455 txq = port->txqs[txq_id];
6456 txq_pcpu = this_cpu_ptr(txq->pcpu);
6457 aggr_txq = &port->priv->aggr_txqs[smp_processor_id()];
6458
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006459 if (skb_is_gso(skb)) {
6460 frags = mvpp2_tx_tso(skb, dev, txq, aggr_txq, txq_pcpu);
6461 goto out;
6462 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006463 frags = skb_shinfo(skb)->nr_frags + 1;
6464
6465 /* Check number of available descriptors */
6466 if (mvpp2_aggr_desc_num_check(port->priv, aggr_txq, frags) ||
6467 mvpp2_txq_reserved_desc_num_proc(port->priv, txq,
6468 txq_pcpu, frags)) {
6469 frags = 0;
6470 goto out;
6471 }
6472
6473 /* Get a descriptor for the first part of the packet */
6474 tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006475 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6476 mvpp2_txdesc_size_set(port, tx_desc, skb_headlen(skb));
Marcin Wojtas3f518502014-07-10 16:52:13 -03006477
Thomas Petazzoni20396132017-03-07 16:53:00 +01006478 buf_dma_addr = dma_map_single(dev->dev.parent, skb->data,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006479 skb_headlen(skb), DMA_TO_DEVICE);
Thomas Petazzoni20396132017-03-07 16:53:00 +01006480 if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006481 mvpp2_txq_desc_put(txq);
6482 frags = 0;
6483 goto out;
6484 }
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006485
6486 mvpp2_txdesc_offset_set(port, tx_desc,
6487 buf_dma_addr & MVPP2_TX_DESC_ALIGN);
6488 mvpp2_txdesc_dma_addr_set(port, tx_desc,
6489 buf_dma_addr & ~MVPP2_TX_DESC_ALIGN);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006490
6491 tx_cmd = mvpp2_skb_tx_csum(port, skb);
6492
6493 if (frags == 1) {
6494 /* First and Last descriptor */
6495 tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_L_DESC;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006496 mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
6497 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006498 } else {
6499 /* First but not Last */
6500 tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_PADDING_DISABLE;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006501 mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
6502 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006503
6504 /* Continue with other skb fragments */
6505 if (mvpp2_tx_frag_process(port, skb, aggr_txq, txq)) {
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006506 tx_desc_unmap_put(port, txq, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006507 frags = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006508 }
6509 }
6510
Marcin Wojtas3f518502014-07-10 16:52:13 -03006511out:
6512 if (frags > 0) {
6513 struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006514 struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
6515
6516 txq_pcpu->reserved_num -= frags;
6517 txq_pcpu->count += frags;
6518 aggr_txq->count += frags;
6519
6520 /* Enable transmit */
6521 wmb();
6522 mvpp2_aggr_txq_pend_desc_add(port, frags);
6523
Antoine Tenart1d17db02017-10-30 11:23:31 +01006524 if (txq_pcpu->count >= txq_pcpu->stop_threshold)
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006525 netif_tx_stop_queue(nq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006526
6527 u64_stats_update_begin(&stats->syncp);
6528 stats->tx_packets++;
6529 stats->tx_bytes += skb->len;
6530 u64_stats_update_end(&stats->syncp);
6531 } else {
6532 dev->stats.tx_dropped++;
6533 dev_kfree_skb_any(skb);
6534 }
6535
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006536 /* Finalize TX processing */
Antoine Tenart082297e2017-10-23 15:24:31 +02006537 if (!port->has_tx_irqs && txq_pcpu->count >= txq->done_pkts_coal)
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006538 mvpp2_txq_done(port, txq, txq_pcpu);
6539
6540 /* Set the timer in case not all frags were processed */
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006541 if (!port->has_tx_irqs && txq_pcpu->count <= frags &&
6542 txq_pcpu->count > 0) {
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006543 struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
6544
6545 mvpp2_timer_set(port_pcpu);
6546 }
6547
Marcin Wojtas3f518502014-07-10 16:52:13 -03006548 return NETDEV_TX_OK;
6549}
6550
6551static inline void mvpp2_cause_error(struct net_device *dev, int cause)
6552{
6553 if (cause & MVPP2_CAUSE_FCS_ERR_MASK)
6554 netdev_err(dev, "FCS error\n");
6555 if (cause & MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK)
6556 netdev_err(dev, "rx fifo overrun error\n");
6557 if (cause & MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK)
6558 netdev_err(dev, "tx fifo underrun error\n");
6559}
6560
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006561static int mvpp2_poll(struct napi_struct *napi, int budget)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006562{
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006563 u32 cause_rx_tx, cause_rx, cause_tx, cause_misc;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006564 int rx_done = 0;
6565 struct mvpp2_port *port = netdev_priv(napi->dev);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006566 struct mvpp2_queue_vector *qv;
Thomas Petazzonia7868412017-03-07 16:53:13 +01006567 int cpu = smp_processor_id();
Marcin Wojtas3f518502014-07-10 16:52:13 -03006568
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006569 qv = container_of(napi, struct mvpp2_queue_vector, napi);
6570
Marcin Wojtas3f518502014-07-10 16:52:13 -03006571 /* Rx/Tx cause register
6572 *
6573 * Bits 0-15: each bit indicates received packets on the Rx queue
6574 * (bit 0 is for Rx queue 0).
6575 *
6576 * Bits 16-23: each bit indicates transmitted packets on the Tx queue
6577 * (bit 16 is for Tx queue 0).
6578 *
6579 * Each CPU has its own Rx/Tx cause register
6580 */
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006581 cause_rx_tx = mvpp2_percpu_read(port->priv, qv->sw_thread_id,
Thomas Petazzonia7868412017-03-07 16:53:13 +01006582 MVPP2_ISR_RX_TX_CAUSE_REG(port->id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03006583
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006584 cause_misc = cause_rx_tx & MVPP2_CAUSE_MISC_SUM_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006585 if (cause_misc) {
6586 mvpp2_cause_error(port->dev, cause_misc);
6587
6588 /* Clear the cause register */
6589 mvpp2_write(port->priv, MVPP2_ISR_MISC_CAUSE_REG, 0);
Thomas Petazzonia7868412017-03-07 16:53:13 +01006590 mvpp2_percpu_write(port->priv, cpu,
6591 MVPP2_ISR_RX_TX_CAUSE_REG(port->id),
6592 cause_rx_tx & ~MVPP2_CAUSE_MISC_SUM_MASK);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006593 }
6594
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006595 cause_tx = cause_rx_tx & MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
6596 if (cause_tx) {
6597 cause_tx >>= MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_OFFSET;
6598 mvpp2_tx_done(port, cause_tx, qv->sw_thread_id);
6599 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006600
6601 /* Process RX packets */
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006602 cause_rx = cause_rx_tx & MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
6603 cause_rx <<= qv->first_rxq;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006604 cause_rx |= qv->pending_cause_rx;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006605 while (cause_rx && budget > 0) {
6606 int count;
6607 struct mvpp2_rx_queue *rxq;
6608
6609 rxq = mvpp2_get_rx_queue(port, cause_rx);
6610 if (!rxq)
6611 break;
6612
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006613 count = mvpp2_rx(port, napi, budget, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006614 rx_done += count;
6615 budget -= count;
6616 if (budget > 0) {
6617 /* Clear the bit associated to this Rx queue
6618 * so that next iteration will continue from
6619 * the next Rx queue.
6620 */
6621 cause_rx &= ~(1 << rxq->logic_rxq);
6622 }
6623 }
6624
6625 if (budget > 0) {
6626 cause_rx = 0;
Eric Dumazet6ad20162017-01-30 08:22:01 -08006627 napi_complete_done(napi, rx_done);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006628
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006629 mvpp2_qvec_interrupt_enable(qv);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006630 }
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006631 qv->pending_cause_rx = cause_rx;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006632 return rx_done;
6633}
6634
6635/* Set hw internals when starting port */
6636static void mvpp2_start_dev(struct mvpp2_port *port)
6637{
Philippe Reynes8e072692016-06-28 00:08:11 +02006638 struct net_device *ndev = port->dev;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006639 int i;
Philippe Reynes8e072692016-06-28 00:08:11 +02006640
Stefan Chulski76eb1b12017-08-22 19:08:26 +02006641 if (port->gop_id == 0 &&
6642 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
6643 port->phy_interface == PHY_INTERFACE_MODE_10GKR))
6644 mvpp2_xlg_max_rx_size_set(port);
6645 else
6646 mvpp2_gmac_max_rx_size_set(port);
6647
Marcin Wojtas3f518502014-07-10 16:52:13 -03006648 mvpp2_txp_max_tx_size_set(port);
6649
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006650 for (i = 0; i < port->nqvecs; i++)
6651 napi_enable(&port->qvecs[i].napi);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006652
6653 /* Enable interrupts on all CPUs */
6654 mvpp2_interrupts_enable(port);
6655
Antoine Tenart542897d2017-08-30 10:29:15 +02006656 if (port->priv->hw_version == MVPP22) {
6657 mvpp22_comphy_init(port);
Antoine Ténartf84bf382017-08-22 19:08:27 +02006658 mvpp22_gop_init(port);
Antoine Tenart542897d2017-08-30 10:29:15 +02006659 }
Antoine Ténartf84bf382017-08-22 19:08:27 +02006660
Antoine Ténart2055d622017-08-22 19:08:23 +02006661 mvpp2_port_mii_set(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006662 mvpp2_port_enable(port);
Antoine Tenart5997c862017-09-01 11:04:53 +02006663 if (ndev->phydev)
6664 phy_start(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006665 netif_tx_start_all_queues(port->dev);
6666}
6667
6668/* Set hw internals when stopping port */
6669static void mvpp2_stop_dev(struct mvpp2_port *port)
6670{
Philippe Reynes8e072692016-06-28 00:08:11 +02006671 struct net_device *ndev = port->dev;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006672 int i;
Philippe Reynes8e072692016-06-28 00:08:11 +02006673
Marcin Wojtas3f518502014-07-10 16:52:13 -03006674 /* Stop new packets from arriving to RXQs */
6675 mvpp2_ingress_disable(port);
6676
6677 mdelay(10);
6678
6679 /* Disable interrupts on all CPUs */
6680 mvpp2_interrupts_disable(port);
6681
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006682 for (i = 0; i < port->nqvecs; i++)
6683 napi_disable(&port->qvecs[i].napi);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006684
6685 netif_carrier_off(port->dev);
6686 netif_tx_stop_all_queues(port->dev);
6687
6688 mvpp2_egress_disable(port);
6689 mvpp2_port_disable(port);
Antoine Tenart5997c862017-09-01 11:04:53 +02006690 if (ndev->phydev)
6691 phy_stop(ndev->phydev);
Antoine Tenart542897d2017-08-30 10:29:15 +02006692 phy_power_off(port->comphy);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006693}
6694
Marcin Wojtas3f518502014-07-10 16:52:13 -03006695static int mvpp2_check_ringparam_valid(struct net_device *dev,
6696 struct ethtool_ringparam *ring)
6697{
6698 u16 new_rx_pending = ring->rx_pending;
6699 u16 new_tx_pending = ring->tx_pending;
6700
6701 if (ring->rx_pending == 0 || ring->tx_pending == 0)
6702 return -EINVAL;
6703
6704 if (ring->rx_pending > MVPP2_MAX_RXD)
6705 new_rx_pending = MVPP2_MAX_RXD;
6706 else if (!IS_ALIGNED(ring->rx_pending, 16))
6707 new_rx_pending = ALIGN(ring->rx_pending, 16);
6708
6709 if (ring->tx_pending > MVPP2_MAX_TXD)
6710 new_tx_pending = MVPP2_MAX_TXD;
6711 else if (!IS_ALIGNED(ring->tx_pending, 32))
6712 new_tx_pending = ALIGN(ring->tx_pending, 32);
6713
6714 if (ring->rx_pending != new_rx_pending) {
6715 netdev_info(dev, "illegal Rx ring size value %d, round to %d\n",
6716 ring->rx_pending, new_rx_pending);
6717 ring->rx_pending = new_rx_pending;
6718 }
6719
6720 if (ring->tx_pending != new_tx_pending) {
6721 netdev_info(dev, "illegal Tx ring size value %d, round to %d\n",
6722 ring->tx_pending, new_tx_pending);
6723 ring->tx_pending = new_tx_pending;
6724 }
6725
6726 return 0;
6727}
6728
Thomas Petazzoni26975822017-03-07 16:53:14 +01006729static void mvpp21_get_mac_address(struct mvpp2_port *port, unsigned char *addr)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006730{
6731 u32 mac_addr_l, mac_addr_m, mac_addr_h;
6732
6733 mac_addr_l = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
6734 mac_addr_m = readl(port->priv->lms_base + MVPP2_SRC_ADDR_MIDDLE);
6735 mac_addr_h = readl(port->priv->lms_base + MVPP2_SRC_ADDR_HIGH);
6736 addr[0] = (mac_addr_h >> 24) & 0xFF;
6737 addr[1] = (mac_addr_h >> 16) & 0xFF;
6738 addr[2] = (mac_addr_h >> 8) & 0xFF;
6739 addr[3] = mac_addr_h & 0xFF;
6740 addr[4] = mac_addr_m & 0xFF;
6741 addr[5] = (mac_addr_l >> MVPP2_GMAC_SA_LOW_OFFS) & 0xFF;
6742}
6743
6744static int mvpp2_phy_connect(struct mvpp2_port *port)
6745{
6746 struct phy_device *phy_dev;
6747
Antoine Tenart5997c862017-09-01 11:04:53 +02006748 /* No PHY is attached */
6749 if (!port->phy_node)
6750 return 0;
6751
Marcin Wojtas3f518502014-07-10 16:52:13 -03006752 phy_dev = of_phy_connect(port->dev, port->phy_node, mvpp2_link_event, 0,
6753 port->phy_interface);
6754 if (!phy_dev) {
6755 netdev_err(port->dev, "cannot connect to phy\n");
6756 return -ENODEV;
6757 }
6758 phy_dev->supported &= PHY_GBIT_FEATURES;
6759 phy_dev->advertising = phy_dev->supported;
6760
Marcin Wojtas3f518502014-07-10 16:52:13 -03006761 port->link = 0;
6762 port->duplex = 0;
6763 port->speed = 0;
6764
6765 return 0;
6766}
6767
6768static void mvpp2_phy_disconnect(struct mvpp2_port *port)
6769{
Philippe Reynes8e072692016-06-28 00:08:11 +02006770 struct net_device *ndev = port->dev;
6771
Antoine Tenart5997c862017-09-01 11:04:53 +02006772 if (!ndev->phydev)
6773 return;
6774
Philippe Reynes8e072692016-06-28 00:08:11 +02006775 phy_disconnect(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006776}
6777
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006778static int mvpp2_irqs_init(struct mvpp2_port *port)
6779{
6780 int err, i;
6781
6782 for (i = 0; i < port->nqvecs; i++) {
6783 struct mvpp2_queue_vector *qv = port->qvecs + i;
6784
6785 err = request_irq(qv->irq, mvpp2_isr, 0, port->dev->name, qv);
6786 if (err)
6787 goto err;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006788
6789 if (qv->type == MVPP2_QUEUE_VECTOR_PRIVATE)
6790 irq_set_affinity_hint(qv->irq,
6791 cpumask_of(qv->sw_thread_id));
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006792 }
6793
6794 return 0;
6795err:
6796 for (i = 0; i < port->nqvecs; i++) {
6797 struct mvpp2_queue_vector *qv = port->qvecs + i;
6798
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006799 irq_set_affinity_hint(qv->irq, NULL);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006800 free_irq(qv->irq, qv);
6801 }
6802
6803 return err;
6804}
6805
6806static void mvpp2_irqs_deinit(struct mvpp2_port *port)
6807{
6808 int i;
6809
6810 for (i = 0; i < port->nqvecs; i++) {
6811 struct mvpp2_queue_vector *qv = port->qvecs + i;
6812
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006813 irq_set_affinity_hint(qv->irq, NULL);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006814 free_irq(qv->irq, qv);
6815 }
6816}
6817
Antoine Tenart1d7d15d2017-10-30 11:23:30 +01006818static void mvpp22_init_rss(struct mvpp2_port *port)
6819{
6820 struct mvpp2 *priv = port->priv;
6821 int i;
6822
6823 /* Set the table width: replace the whole classifier Rx queue number
6824 * with the ones configured in RSS table entries.
6825 */
6826 mvpp2_write(priv, MVPP22_RSS_INDEX, MVPP22_RSS_INDEX_TABLE(0));
6827 mvpp2_write(priv, MVPP22_RSS_WIDTH, 8);
6828
6829 /* Loop through the classifier Rx Queues and map them to a RSS table.
6830 * Map them all to the first table (0) by default.
6831 */
6832 for (i = 0; i < MVPP2_CLS_RX_QUEUES; i++) {
6833 mvpp2_write(priv, MVPP22_RSS_INDEX, MVPP22_RSS_INDEX_QUEUE(i));
6834 mvpp2_write(priv, MVPP22_RSS_TABLE,
6835 MVPP22_RSS_TABLE_POINTER(0));
6836 }
6837
6838 /* Configure the first table to evenly distribute the packets across
6839 * real Rx Queues. The table entries map a hash to an port Rx Queue.
6840 */
6841 for (i = 0; i < MVPP22_RSS_TABLE_ENTRIES; i++) {
6842 u32 sel = MVPP22_RSS_INDEX_TABLE(0) |
6843 MVPP22_RSS_INDEX_TABLE_ENTRY(i);
6844 mvpp2_write(priv, MVPP22_RSS_INDEX, sel);
6845
6846 mvpp2_write(priv, MVPP22_RSS_TABLE_ENTRY, i % port->nrxqs);
6847 }
6848
6849}
6850
Marcin Wojtas3f518502014-07-10 16:52:13 -03006851static int mvpp2_open(struct net_device *dev)
6852{
6853 struct mvpp2_port *port = netdev_priv(dev);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02006854 struct mvpp2 *priv = port->priv;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006855 unsigned char mac_bcast[ETH_ALEN] = {
6856 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
6857 int err;
6858
6859 err = mvpp2_prs_mac_da_accept(port->priv, port->id, mac_bcast, true);
6860 if (err) {
6861 netdev_err(dev, "mvpp2_prs_mac_da_accept BC failed\n");
6862 return err;
6863 }
6864 err = mvpp2_prs_mac_da_accept(port->priv, port->id,
6865 dev->dev_addr, true);
6866 if (err) {
6867 netdev_err(dev, "mvpp2_prs_mac_da_accept MC failed\n");
6868 return err;
6869 }
6870 err = mvpp2_prs_tag_mode_set(port->priv, port->id, MVPP2_TAG_TYPE_MH);
6871 if (err) {
6872 netdev_err(dev, "mvpp2_prs_tag_mode_set failed\n");
6873 return err;
6874 }
6875 err = mvpp2_prs_def_flow(port);
6876 if (err) {
6877 netdev_err(dev, "mvpp2_prs_def_flow failed\n");
6878 return err;
6879 }
6880
6881 /* Allocate the Rx/Tx queues */
6882 err = mvpp2_setup_rxqs(port);
6883 if (err) {
6884 netdev_err(port->dev, "cannot allocate Rx queues\n");
6885 return err;
6886 }
6887
6888 err = mvpp2_setup_txqs(port);
6889 if (err) {
6890 netdev_err(port->dev, "cannot allocate Tx queues\n");
6891 goto err_cleanup_rxqs;
6892 }
6893
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006894 err = mvpp2_irqs_init(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006895 if (err) {
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006896 netdev_err(port->dev, "cannot init IRQs\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03006897 goto err_cleanup_txqs;
6898 }
6899
Antoine Tenartfd3651b2017-09-01 11:04:54 +02006900 if (priv->hw_version == MVPP22 && !port->phy_node && port->link_irq) {
6901 err = request_irq(port->link_irq, mvpp2_link_status_isr, 0,
6902 dev->name, port);
6903 if (err) {
6904 netdev_err(port->dev, "cannot request link IRQ %d\n",
6905 port->link_irq);
6906 goto err_free_irq;
6907 }
6908
6909 mvpp22_gop_setup_irq(port);
6910 }
6911
Marcin Wojtas3f518502014-07-10 16:52:13 -03006912 /* In default link is down */
6913 netif_carrier_off(port->dev);
6914
6915 err = mvpp2_phy_connect(port);
6916 if (err < 0)
Antoine Tenartfd3651b2017-09-01 11:04:54 +02006917 goto err_free_link_irq;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006918
6919 /* Unmask interrupts on all CPUs */
6920 on_each_cpu(mvpp2_interrupts_unmask, port, 1);
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006921 mvpp2_shared_interrupt_mask_unmask(port, false);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006922
6923 mvpp2_start_dev(port);
6924
Antoine Tenart1d7d15d2017-10-30 11:23:30 +01006925 if (priv->hw_version == MVPP22)
6926 mvpp22_init_rss(port);
6927
Marcin Wojtas3f518502014-07-10 16:52:13 -03006928 return 0;
6929
Antoine Tenartfd3651b2017-09-01 11:04:54 +02006930err_free_link_irq:
6931 if (priv->hw_version == MVPP22 && !port->phy_node && port->link_irq)
6932 free_irq(port->link_irq, port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006933err_free_irq:
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006934 mvpp2_irqs_deinit(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006935err_cleanup_txqs:
6936 mvpp2_cleanup_txqs(port);
6937err_cleanup_rxqs:
6938 mvpp2_cleanup_rxqs(port);
6939 return err;
6940}
6941
6942static int mvpp2_stop(struct net_device *dev)
6943{
6944 struct mvpp2_port *port = netdev_priv(dev);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006945 struct mvpp2_port_pcpu *port_pcpu;
Antoine Tenartfd3651b2017-09-01 11:04:54 +02006946 struct mvpp2 *priv = port->priv;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006947 int cpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006948
6949 mvpp2_stop_dev(port);
6950 mvpp2_phy_disconnect(port);
6951
6952 /* Mask interrupts on all CPUs */
6953 on_each_cpu(mvpp2_interrupts_mask, port, 1);
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006954 mvpp2_shared_interrupt_mask_unmask(port, true);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006955
Antoine Tenartfd3651b2017-09-01 11:04:54 +02006956 if (priv->hw_version == MVPP22 && !port->phy_node && port->link_irq)
6957 free_irq(port->link_irq, port);
6958
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006959 mvpp2_irqs_deinit(port);
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006960 if (!port->has_tx_irqs) {
6961 for_each_present_cpu(cpu) {
6962 port_pcpu = per_cpu_ptr(port->pcpu, cpu);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006963
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006964 hrtimer_cancel(&port_pcpu->tx_done_timer);
6965 port_pcpu->timer_scheduled = false;
6966 tasklet_kill(&port_pcpu->tx_done_tasklet);
6967 }
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006968 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006969 mvpp2_cleanup_rxqs(port);
6970 mvpp2_cleanup_txqs(port);
6971
6972 return 0;
6973}
6974
6975static void mvpp2_set_rx_mode(struct net_device *dev)
6976{
6977 struct mvpp2_port *port = netdev_priv(dev);
6978 struct mvpp2 *priv = port->priv;
6979 struct netdev_hw_addr *ha;
6980 int id = port->id;
6981 bool allmulti = dev->flags & IFF_ALLMULTI;
6982
6983 mvpp2_prs_mac_promisc_set(priv, id, dev->flags & IFF_PROMISC);
6984 mvpp2_prs_mac_multi_set(priv, id, MVPP2_PE_MAC_MC_ALL, allmulti);
6985 mvpp2_prs_mac_multi_set(priv, id, MVPP2_PE_MAC_MC_IP6, allmulti);
6986
6987 /* Remove all port->id's mcast enries */
6988 mvpp2_prs_mcast_del_all(priv, id);
6989
6990 if (allmulti && !netdev_mc_empty(dev)) {
6991 netdev_for_each_mc_addr(ha, dev)
6992 mvpp2_prs_mac_da_accept(priv, id, ha->addr, true);
6993 }
6994}
6995
6996static int mvpp2_set_mac_address(struct net_device *dev, void *p)
6997{
6998 struct mvpp2_port *port = netdev_priv(dev);
6999 const struct sockaddr *addr = p;
7000 int err;
7001
7002 if (!is_valid_ether_addr(addr->sa_data)) {
7003 err = -EADDRNOTAVAIL;
Markus Elfringc1175542017-04-17 11:10:47 +02007004 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007005 }
7006
7007 if (!netif_running(dev)) {
7008 err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
7009 if (!err)
7010 return 0;
7011 /* Reconfigure parser to accept the original MAC address */
7012 err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
7013 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02007014 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007015 }
7016
7017 mvpp2_stop_dev(port);
7018
7019 err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
7020 if (!err)
7021 goto out_start;
7022
7023 /* Reconfigure parser accept the original MAC address */
7024 err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
7025 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02007026 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007027out_start:
7028 mvpp2_start_dev(port);
7029 mvpp2_egress_enable(port);
7030 mvpp2_ingress_enable(port);
7031 return 0;
Markus Elfringc1175542017-04-17 11:10:47 +02007032log_error:
Markus Elfringdfd42402017-04-17 11:20:41 +02007033 netdev_err(dev, "failed to change MAC address\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03007034 return err;
7035}
7036
7037static int mvpp2_change_mtu(struct net_device *dev, int mtu)
7038{
7039 struct mvpp2_port *port = netdev_priv(dev);
7040 int err;
7041
Jarod Wilson57779872016-10-17 15:54:06 -04007042 if (!IS_ALIGNED(MVPP2_RX_PKT_SIZE(mtu), 8)) {
7043 netdev_info(dev, "illegal MTU value %d, round to %d\n", mtu,
7044 ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8));
7045 mtu = ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007046 }
7047
7048 if (!netif_running(dev)) {
7049 err = mvpp2_bm_update_mtu(dev, mtu);
7050 if (!err) {
7051 port->pkt_size = MVPP2_RX_PKT_SIZE(mtu);
7052 return 0;
7053 }
7054
7055 /* Reconfigure BM to the original MTU */
7056 err = mvpp2_bm_update_mtu(dev, dev->mtu);
7057 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02007058 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007059 }
7060
7061 mvpp2_stop_dev(port);
7062
7063 err = mvpp2_bm_update_mtu(dev, mtu);
7064 if (!err) {
7065 port->pkt_size = MVPP2_RX_PKT_SIZE(mtu);
7066 goto out_start;
7067 }
7068
7069 /* Reconfigure BM to the original MTU */
7070 err = mvpp2_bm_update_mtu(dev, dev->mtu);
7071 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02007072 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007073
7074out_start:
7075 mvpp2_start_dev(port);
7076 mvpp2_egress_enable(port);
7077 mvpp2_ingress_enable(port);
7078
7079 return 0;
Markus Elfringc1175542017-04-17 11:10:47 +02007080log_error:
Markus Elfringdfd42402017-04-17 11:20:41 +02007081 netdev_err(dev, "failed to change MTU\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03007082 return err;
7083}
7084
stephen hemmingerbc1f4472017-01-06 19:12:52 -08007085static void
Marcin Wojtas3f518502014-07-10 16:52:13 -03007086mvpp2_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
7087{
7088 struct mvpp2_port *port = netdev_priv(dev);
7089 unsigned int start;
7090 int cpu;
7091
7092 for_each_possible_cpu(cpu) {
7093 struct mvpp2_pcpu_stats *cpu_stats;
7094 u64 rx_packets;
7095 u64 rx_bytes;
7096 u64 tx_packets;
7097 u64 tx_bytes;
7098
7099 cpu_stats = per_cpu_ptr(port->stats, cpu);
7100 do {
7101 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
7102 rx_packets = cpu_stats->rx_packets;
7103 rx_bytes = cpu_stats->rx_bytes;
7104 tx_packets = cpu_stats->tx_packets;
7105 tx_bytes = cpu_stats->tx_bytes;
7106 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
7107
7108 stats->rx_packets += rx_packets;
7109 stats->rx_bytes += rx_bytes;
7110 stats->tx_packets += tx_packets;
7111 stats->tx_bytes += tx_bytes;
7112 }
7113
7114 stats->rx_errors = dev->stats.rx_errors;
7115 stats->rx_dropped = dev->stats.rx_dropped;
7116 stats->tx_dropped = dev->stats.tx_dropped;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007117}
7118
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007119static int mvpp2_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
7120{
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007121 int ret;
7122
Philippe Reynes8e072692016-06-28 00:08:11 +02007123 if (!dev->phydev)
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007124 return -ENOTSUPP;
7125
Philippe Reynes8e072692016-06-28 00:08:11 +02007126 ret = phy_mii_ioctl(dev->phydev, ifr, cmd);
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007127 if (!ret)
7128 mvpp2_link_event(dev);
7129
7130 return ret;
7131}
7132
Marcin Wojtas3f518502014-07-10 16:52:13 -03007133/* Ethtool methods */
7134
Marcin Wojtas3f518502014-07-10 16:52:13 -03007135/* Set interrupt coalescing for ethtools */
7136static int mvpp2_ethtool_set_coalesce(struct net_device *dev,
7137 struct ethtool_coalesce *c)
7138{
7139 struct mvpp2_port *port = netdev_priv(dev);
7140 int queue;
7141
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007142 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007143 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
7144
7145 rxq->time_coal = c->rx_coalesce_usecs;
7146 rxq->pkts_coal = c->rx_max_coalesced_frames;
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01007147 mvpp2_rx_pkts_coal_set(port, rxq);
7148 mvpp2_rx_time_coal_set(port, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007149 }
7150
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007151 if (port->has_tx_irqs) {
7152 port->tx_time_coal = c->tx_coalesce_usecs;
7153 mvpp2_tx_time_coal_set(port);
7154 }
7155
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007156 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007157 struct mvpp2_tx_queue *txq = port->txqs[queue];
7158
7159 txq->done_pkts_coal = c->tx_max_coalesced_frames;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007160
7161 if (port->has_tx_irqs)
7162 mvpp2_tx_pkts_coal_set(port, txq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007163 }
7164
Marcin Wojtas3f518502014-07-10 16:52:13 -03007165 return 0;
7166}
7167
7168/* get coalescing for ethtools */
7169static int mvpp2_ethtool_get_coalesce(struct net_device *dev,
7170 struct ethtool_coalesce *c)
7171{
7172 struct mvpp2_port *port = netdev_priv(dev);
7173
7174 c->rx_coalesce_usecs = port->rxqs[0]->time_coal;
7175 c->rx_max_coalesced_frames = port->rxqs[0]->pkts_coal;
7176 c->tx_max_coalesced_frames = port->txqs[0]->done_pkts_coal;
7177 return 0;
7178}
7179
7180static void mvpp2_ethtool_get_drvinfo(struct net_device *dev,
7181 struct ethtool_drvinfo *drvinfo)
7182{
7183 strlcpy(drvinfo->driver, MVPP2_DRIVER_NAME,
7184 sizeof(drvinfo->driver));
7185 strlcpy(drvinfo->version, MVPP2_DRIVER_VERSION,
7186 sizeof(drvinfo->version));
7187 strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
7188 sizeof(drvinfo->bus_info));
7189}
7190
7191static void mvpp2_ethtool_get_ringparam(struct net_device *dev,
7192 struct ethtool_ringparam *ring)
7193{
7194 struct mvpp2_port *port = netdev_priv(dev);
7195
7196 ring->rx_max_pending = MVPP2_MAX_RXD;
7197 ring->tx_max_pending = MVPP2_MAX_TXD;
7198 ring->rx_pending = port->rx_ring_size;
7199 ring->tx_pending = port->tx_ring_size;
7200}
7201
7202static int mvpp2_ethtool_set_ringparam(struct net_device *dev,
7203 struct ethtool_ringparam *ring)
7204{
7205 struct mvpp2_port *port = netdev_priv(dev);
7206 u16 prev_rx_ring_size = port->rx_ring_size;
7207 u16 prev_tx_ring_size = port->tx_ring_size;
7208 int err;
7209
7210 err = mvpp2_check_ringparam_valid(dev, ring);
7211 if (err)
7212 return err;
7213
7214 if (!netif_running(dev)) {
7215 port->rx_ring_size = ring->rx_pending;
7216 port->tx_ring_size = ring->tx_pending;
7217 return 0;
7218 }
7219
7220 /* The interface is running, so we have to force a
7221 * reallocation of the queues
7222 */
7223 mvpp2_stop_dev(port);
7224 mvpp2_cleanup_rxqs(port);
7225 mvpp2_cleanup_txqs(port);
7226
7227 port->rx_ring_size = ring->rx_pending;
7228 port->tx_ring_size = ring->tx_pending;
7229
7230 err = mvpp2_setup_rxqs(port);
7231 if (err) {
7232 /* Reallocate Rx queues with the original ring size */
7233 port->rx_ring_size = prev_rx_ring_size;
7234 ring->rx_pending = prev_rx_ring_size;
7235 err = mvpp2_setup_rxqs(port);
7236 if (err)
7237 goto err_out;
7238 }
7239 err = mvpp2_setup_txqs(port);
7240 if (err) {
7241 /* Reallocate Tx queues with the original ring size */
7242 port->tx_ring_size = prev_tx_ring_size;
7243 ring->tx_pending = prev_tx_ring_size;
7244 err = mvpp2_setup_txqs(port);
7245 if (err)
7246 goto err_clean_rxqs;
7247 }
7248
7249 mvpp2_start_dev(port);
7250 mvpp2_egress_enable(port);
7251 mvpp2_ingress_enable(port);
7252
7253 return 0;
7254
7255err_clean_rxqs:
7256 mvpp2_cleanup_rxqs(port);
7257err_out:
Markus Elfringdfd42402017-04-17 11:20:41 +02007258 netdev_err(dev, "failed to change ring parameters");
Marcin Wojtas3f518502014-07-10 16:52:13 -03007259 return err;
7260}
7261
7262/* Device ops */
7263
7264static const struct net_device_ops mvpp2_netdev_ops = {
7265 .ndo_open = mvpp2_open,
7266 .ndo_stop = mvpp2_stop,
7267 .ndo_start_xmit = mvpp2_tx,
7268 .ndo_set_rx_mode = mvpp2_set_rx_mode,
7269 .ndo_set_mac_address = mvpp2_set_mac_address,
7270 .ndo_change_mtu = mvpp2_change_mtu,
7271 .ndo_get_stats64 = mvpp2_get_stats64,
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007272 .ndo_do_ioctl = mvpp2_ioctl,
Marcin Wojtas3f518502014-07-10 16:52:13 -03007273};
7274
7275static const struct ethtool_ops mvpp2_eth_tool_ops = {
Florian Fainelli00606c42016-11-15 11:19:48 -08007276 .nway_reset = phy_ethtool_nway_reset,
Marcin Wojtas3f518502014-07-10 16:52:13 -03007277 .get_link = ethtool_op_get_link,
Marcin Wojtas3f518502014-07-10 16:52:13 -03007278 .set_coalesce = mvpp2_ethtool_set_coalesce,
7279 .get_coalesce = mvpp2_ethtool_get_coalesce,
7280 .get_drvinfo = mvpp2_ethtool_get_drvinfo,
7281 .get_ringparam = mvpp2_ethtool_get_ringparam,
7282 .set_ringparam = mvpp2_ethtool_set_ringparam,
Philippe Reynesfb773e92016-06-28 00:08:12 +02007283 .get_link_ksettings = phy_ethtool_get_link_ksettings,
7284 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Marcin Wojtas3f518502014-07-10 16:52:13 -03007285};
7286
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007287/* Used for PPv2.1, or PPv2.2 with the old Device Tree binding that
7288 * had a single IRQ defined per-port.
7289 */
7290static int mvpp2_simple_queue_vectors_init(struct mvpp2_port *port,
7291 struct device_node *port_node)
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007292{
7293 struct mvpp2_queue_vector *v = &port->qvecs[0];
7294
7295 v->first_rxq = 0;
7296 v->nrxqs = port->nrxqs;
7297 v->type = MVPP2_QUEUE_VECTOR_SHARED;
7298 v->sw_thread_id = 0;
7299 v->sw_thread_mask = *cpumask_bits(cpu_online_mask);
7300 v->port = port;
7301 v->irq = irq_of_parse_and_map(port_node, 0);
7302 if (v->irq <= 0)
7303 return -EINVAL;
7304 netif_napi_add(port->dev, &v->napi, mvpp2_poll,
7305 NAPI_POLL_WEIGHT);
7306
7307 port->nqvecs = 1;
7308
7309 return 0;
7310}
7311
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007312static int mvpp2_multi_queue_vectors_init(struct mvpp2_port *port,
7313 struct device_node *port_node)
7314{
7315 struct mvpp2_queue_vector *v;
7316 int i, ret;
7317
7318 port->nqvecs = num_possible_cpus();
7319 if (queue_mode == MVPP2_QDIST_SINGLE_MODE)
7320 port->nqvecs += 1;
7321
7322 for (i = 0; i < port->nqvecs; i++) {
7323 char irqname[16];
7324
7325 v = port->qvecs + i;
7326
7327 v->port = port;
7328 v->type = MVPP2_QUEUE_VECTOR_PRIVATE;
7329 v->sw_thread_id = i;
7330 v->sw_thread_mask = BIT(i);
7331
7332 snprintf(irqname, sizeof(irqname), "tx-cpu%d", i);
7333
7334 if (queue_mode == MVPP2_QDIST_MULTI_MODE) {
7335 v->first_rxq = i * MVPP2_DEFAULT_RXQ;
7336 v->nrxqs = MVPP2_DEFAULT_RXQ;
7337 } else if (queue_mode == MVPP2_QDIST_SINGLE_MODE &&
7338 i == (port->nqvecs - 1)) {
7339 v->first_rxq = 0;
7340 v->nrxqs = port->nrxqs;
7341 v->type = MVPP2_QUEUE_VECTOR_SHARED;
7342 strncpy(irqname, "rx-shared", sizeof(irqname));
7343 }
7344
7345 v->irq = of_irq_get_byname(port_node, irqname);
7346 if (v->irq <= 0) {
7347 ret = -EINVAL;
7348 goto err;
7349 }
7350
7351 netif_napi_add(port->dev, &v->napi, mvpp2_poll,
7352 NAPI_POLL_WEIGHT);
7353 }
7354
7355 return 0;
7356
7357err:
7358 for (i = 0; i < port->nqvecs; i++)
7359 irq_dispose_mapping(port->qvecs[i].irq);
7360 return ret;
7361}
7362
7363static int mvpp2_queue_vectors_init(struct mvpp2_port *port,
7364 struct device_node *port_node)
7365{
7366 if (port->has_tx_irqs)
7367 return mvpp2_multi_queue_vectors_init(port, port_node);
7368 else
7369 return mvpp2_simple_queue_vectors_init(port, port_node);
7370}
7371
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007372static void mvpp2_queue_vectors_deinit(struct mvpp2_port *port)
7373{
7374 int i;
7375
7376 for (i = 0; i < port->nqvecs; i++)
7377 irq_dispose_mapping(port->qvecs[i].irq);
7378}
7379
7380/* Configure Rx queue group interrupt for this port */
7381static void mvpp2_rx_irqs_setup(struct mvpp2_port *port)
7382{
7383 struct mvpp2 *priv = port->priv;
7384 u32 val;
7385 int i;
7386
7387 if (priv->hw_version == MVPP21) {
7388 mvpp2_write(priv, MVPP21_ISR_RXQ_GROUP_REG(port->id),
7389 port->nrxqs);
7390 return;
7391 }
7392
7393 /* Handle the more complicated PPv2.2 case */
7394 for (i = 0; i < port->nqvecs; i++) {
7395 struct mvpp2_queue_vector *qv = port->qvecs + i;
7396
7397 if (!qv->nrxqs)
7398 continue;
7399
7400 val = qv->sw_thread_id;
7401 val |= port->id << MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_OFFSET;
7402 mvpp2_write(priv, MVPP22_ISR_RXQ_GROUP_INDEX_REG, val);
7403
7404 val = qv->first_rxq;
7405 val |= qv->nrxqs << MVPP22_ISR_RXQ_SUB_GROUP_SIZE_OFFSET;
7406 mvpp2_write(priv, MVPP22_ISR_RXQ_SUB_GROUP_CONFIG_REG, val);
7407 }
7408}
7409
Marcin Wojtas3f518502014-07-10 16:52:13 -03007410/* Initialize port HW */
7411static int mvpp2_port_init(struct mvpp2_port *port)
7412{
7413 struct device *dev = port->dev->dev.parent;
7414 struct mvpp2 *priv = port->priv;
7415 struct mvpp2_txq_pcpu *txq_pcpu;
7416 int queue, cpu, err;
7417
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007418 /* Checks for hardware constraints */
7419 if (port->first_rxq + port->nrxqs >
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01007420 MVPP2_MAX_PORTS * priv->max_port_rxqs)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007421 return -EINVAL;
7422
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007423 if (port->nrxqs % 4 || (port->nrxqs > priv->max_port_rxqs) ||
7424 (port->ntxqs > MVPP2_MAX_TXQ))
7425 return -EINVAL;
7426
Marcin Wojtas3f518502014-07-10 16:52:13 -03007427 /* Disable port */
7428 mvpp2_egress_disable(port);
7429 mvpp2_port_disable(port);
7430
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007431 port->tx_time_coal = MVPP2_TXDONE_COAL_USEC;
7432
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007433 port->txqs = devm_kcalloc(dev, port->ntxqs, sizeof(*port->txqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03007434 GFP_KERNEL);
7435 if (!port->txqs)
7436 return -ENOMEM;
7437
7438 /* Associate physical Tx queues to this port and initialize.
7439 * The mapping is predefined.
7440 */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007441 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007442 int queue_phy_id = mvpp2_txq_phys(port->id, queue);
7443 struct mvpp2_tx_queue *txq;
7444
7445 txq = devm_kzalloc(dev, sizeof(*txq), GFP_KERNEL);
Christophe Jaillet177c8d12017-02-19 10:19:57 +01007446 if (!txq) {
7447 err = -ENOMEM;
7448 goto err_free_percpu;
7449 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03007450
7451 txq->pcpu = alloc_percpu(struct mvpp2_txq_pcpu);
7452 if (!txq->pcpu) {
7453 err = -ENOMEM;
7454 goto err_free_percpu;
7455 }
7456
7457 txq->id = queue_phy_id;
7458 txq->log_id = queue;
7459 txq->done_pkts_coal = MVPP2_TXDONE_COAL_PKTS_THRESH;
7460 for_each_present_cpu(cpu) {
7461 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
7462 txq_pcpu->cpu = cpu;
7463 }
7464
7465 port->txqs[queue] = txq;
7466 }
7467
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007468 port->rxqs = devm_kcalloc(dev, port->nrxqs, sizeof(*port->rxqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03007469 GFP_KERNEL);
7470 if (!port->rxqs) {
7471 err = -ENOMEM;
7472 goto err_free_percpu;
7473 }
7474
7475 /* Allocate and initialize Rx queue for this port */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007476 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007477 struct mvpp2_rx_queue *rxq;
7478
7479 /* Map physical Rx queue to port's logical Rx queue */
7480 rxq = devm_kzalloc(dev, sizeof(*rxq), GFP_KERNEL);
Jisheng Zhangd82b0c22016-03-31 17:01:23 +08007481 if (!rxq) {
7482 err = -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007483 goto err_free_percpu;
Jisheng Zhangd82b0c22016-03-31 17:01:23 +08007484 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03007485 /* Map this Rx queue to a physical queue */
7486 rxq->id = port->first_rxq + queue;
7487 rxq->port = port->id;
7488 rxq->logic_rxq = queue;
7489
7490 port->rxqs[queue] = rxq;
7491 }
7492
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007493 mvpp2_rx_irqs_setup(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007494
7495 /* Create Rx descriptor rings */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007496 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007497 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
7498
7499 rxq->size = port->rx_ring_size;
7500 rxq->pkts_coal = MVPP2_RX_COAL_PKTS;
7501 rxq->time_coal = MVPP2_RX_COAL_USEC;
7502 }
7503
7504 mvpp2_ingress_disable(port);
7505
7506 /* Port default configuration */
7507 mvpp2_defaults_set(port);
7508
7509 /* Port's classifier configuration */
7510 mvpp2_cls_oversize_rxq_set(port);
7511 mvpp2_cls_port_config(port);
7512
7513 /* Provide an initial Rx packet size */
7514 port->pkt_size = MVPP2_RX_PKT_SIZE(port->dev->mtu);
7515
7516 /* Initialize pools for swf */
7517 err = mvpp2_swf_bm_pool_init(port);
7518 if (err)
7519 goto err_free_percpu;
7520
7521 return 0;
7522
7523err_free_percpu:
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007524 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007525 if (!port->txqs[queue])
7526 continue;
7527 free_percpu(port->txqs[queue]->pcpu);
7528 }
7529 return err;
7530}
7531
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007532/* Checks if the port DT description has the TX interrupts
7533 * described. On PPv2.1, there are no such interrupts. On PPv2.2,
7534 * there are available, but we need to keep support for old DTs.
7535 */
7536static bool mvpp2_port_has_tx_irqs(struct mvpp2 *priv,
7537 struct device_node *port_node)
7538{
7539 char *irqs[5] = { "rx-shared", "tx-cpu0", "tx-cpu1",
7540 "tx-cpu2", "tx-cpu3" };
7541 int ret, i;
7542
7543 if (priv->hw_version == MVPP21)
7544 return false;
7545
7546 for (i = 0; i < 5; i++) {
7547 ret = of_property_match_string(port_node, "interrupt-names",
7548 irqs[i]);
7549 if (ret < 0)
7550 return false;
7551 }
7552
7553 return true;
7554}
7555
Antoine Tenart3ba8c812017-09-02 11:06:47 +02007556static void mvpp2_port_copy_mac_addr(struct net_device *dev, struct mvpp2 *priv,
7557 struct device_node *port_node,
7558 char **mac_from)
7559{
7560 struct mvpp2_port *port = netdev_priv(dev);
7561 char hw_mac_addr[ETH_ALEN] = {0};
7562 const char *dt_mac_addr;
7563
7564 dt_mac_addr = of_get_mac_address(port_node);
7565 if (dt_mac_addr && is_valid_ether_addr(dt_mac_addr)) {
7566 *mac_from = "device tree";
7567 ether_addr_copy(dev->dev_addr, dt_mac_addr);
Antoine Tenart688cbaf2017-09-02 11:06:49 +02007568 return;
Antoine Tenart3ba8c812017-09-02 11:06:47 +02007569 }
Antoine Tenart688cbaf2017-09-02 11:06:49 +02007570
7571 if (priv->hw_version == MVPP21) {
7572 mvpp21_get_mac_address(port, hw_mac_addr);
7573 if (is_valid_ether_addr(hw_mac_addr)) {
7574 *mac_from = "hardware";
7575 ether_addr_copy(dev->dev_addr, hw_mac_addr);
7576 return;
7577 }
7578 }
7579
7580 *mac_from = "random";
7581 eth_hw_addr_random(dev);
Antoine Tenart3ba8c812017-09-02 11:06:47 +02007582}
7583
Marcin Wojtas3f518502014-07-10 16:52:13 -03007584/* Ports initialization */
7585static int mvpp2_port_probe(struct platform_device *pdev,
7586 struct device_node *port_node,
Yan Markman6bf69a12017-09-25 14:59:47 +02007587 struct mvpp2 *priv, int index)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007588{
7589 struct device_node *phy_node;
Antoine Tenart542897d2017-08-30 10:29:15 +02007590 struct phy *comphy;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007591 struct mvpp2_port *port;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007592 struct mvpp2_port_pcpu *port_pcpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007593 struct net_device *dev;
7594 struct resource *res;
Antoine Tenart3ba8c812017-09-02 11:06:47 +02007595 char *mac_from = "";
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007596 unsigned int ntxqs, nrxqs;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007597 bool has_tx_irqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007598 u32 id;
7599 int features;
7600 int phy_mode;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007601 int err, i, cpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007602
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007603 has_tx_irqs = mvpp2_port_has_tx_irqs(priv, port_node);
7604
7605 if (!has_tx_irqs)
7606 queue_mode = MVPP2_QDIST_SINGLE_MODE;
7607
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007608 ntxqs = MVPP2_MAX_TXQ;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007609 if (priv->hw_version == MVPP22 && queue_mode == MVPP2_QDIST_MULTI_MODE)
7610 nrxqs = MVPP2_DEFAULT_RXQ * num_possible_cpus();
7611 else
7612 nrxqs = MVPP2_DEFAULT_RXQ;
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007613
7614 dev = alloc_etherdev_mqs(sizeof(*port), ntxqs, nrxqs);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007615 if (!dev)
7616 return -ENOMEM;
7617
7618 phy_node = of_parse_phandle(port_node, "phy", 0);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007619 phy_mode = of_get_phy_mode(port_node);
7620 if (phy_mode < 0) {
7621 dev_err(&pdev->dev, "incorrect phy mode\n");
7622 err = phy_mode;
7623 goto err_free_netdev;
7624 }
7625
Antoine Tenart542897d2017-08-30 10:29:15 +02007626 comphy = devm_of_phy_get(&pdev->dev, port_node, NULL);
7627 if (IS_ERR(comphy)) {
7628 if (PTR_ERR(comphy) == -EPROBE_DEFER) {
7629 err = -EPROBE_DEFER;
7630 goto err_free_netdev;
7631 }
7632 comphy = NULL;
7633 }
7634
Marcin Wojtas3f518502014-07-10 16:52:13 -03007635 if (of_property_read_u32(port_node, "port-id", &id)) {
7636 err = -EINVAL;
7637 dev_err(&pdev->dev, "missing port-id value\n");
7638 goto err_free_netdev;
7639 }
7640
7641 dev->tx_queue_len = MVPP2_MAX_TXD;
7642 dev->watchdog_timeo = 5 * HZ;
7643 dev->netdev_ops = &mvpp2_netdev_ops;
7644 dev->ethtool_ops = &mvpp2_eth_tool_ops;
7645
7646 port = netdev_priv(dev);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007647 port->dev = dev;
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007648 port->ntxqs = ntxqs;
7649 port->nrxqs = nrxqs;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007650 port->priv = priv;
7651 port->has_tx_irqs = has_tx_irqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007652
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007653 err = mvpp2_queue_vectors_init(port, port_node);
7654 if (err)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007655 goto err_free_netdev;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007656
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007657 port->link_irq = of_irq_get_byname(port_node, "link");
7658 if (port->link_irq == -EPROBE_DEFER) {
7659 err = -EPROBE_DEFER;
7660 goto err_deinit_qvecs;
7661 }
7662 if (port->link_irq <= 0)
7663 /* the link irq is optional */
7664 port->link_irq = 0;
7665
Marcin Wojtas3f518502014-07-10 16:52:13 -03007666 if (of_property_read_bool(port_node, "marvell,loopback"))
7667 port->flags |= MVPP2_F_LOOPBACK;
7668
Marcin Wojtas3f518502014-07-10 16:52:13 -03007669 port->id = id;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01007670 if (priv->hw_version == MVPP21)
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007671 port->first_rxq = port->id * port->nrxqs;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01007672 else
7673 port->first_rxq = port->id * priv->max_port_rxqs;
7674
Marcin Wojtas3f518502014-07-10 16:52:13 -03007675 port->phy_node = phy_node;
7676 port->phy_interface = phy_mode;
Antoine Tenart542897d2017-08-30 10:29:15 +02007677 port->comphy = comphy;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007678
Thomas Petazzonia7868412017-03-07 16:53:13 +01007679 if (priv->hw_version == MVPP21) {
7680 res = platform_get_resource(pdev, IORESOURCE_MEM, 2 + id);
7681 port->base = devm_ioremap_resource(&pdev->dev, res);
7682 if (IS_ERR(port->base)) {
7683 err = PTR_ERR(port->base);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007684 goto err_free_irq;
Thomas Petazzonia7868412017-03-07 16:53:13 +01007685 }
7686 } else {
7687 if (of_property_read_u32(port_node, "gop-port-id",
7688 &port->gop_id)) {
7689 err = -EINVAL;
7690 dev_err(&pdev->dev, "missing gop-port-id value\n");
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007691 goto err_deinit_qvecs;
Thomas Petazzonia7868412017-03-07 16:53:13 +01007692 }
7693
7694 port->base = priv->iface_base + MVPP22_GMAC_BASE(port->gop_id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007695 }
7696
7697 /* Alloc per-cpu stats */
7698 port->stats = netdev_alloc_pcpu_stats(struct mvpp2_pcpu_stats);
7699 if (!port->stats) {
7700 err = -ENOMEM;
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007701 goto err_free_irq;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007702 }
7703
Antoine Tenart3ba8c812017-09-02 11:06:47 +02007704 mvpp2_port_copy_mac_addr(dev, priv, port_node, &mac_from);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007705
7706 port->tx_ring_size = MVPP2_MAX_TXD;
7707 port->rx_ring_size = MVPP2_MAX_RXD;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007708 SET_NETDEV_DEV(dev, &pdev->dev);
7709
7710 err = mvpp2_port_init(port);
7711 if (err < 0) {
7712 dev_err(&pdev->dev, "failed to init port %d\n", id);
7713 goto err_free_stats;
7714 }
Thomas Petazzoni26975822017-03-07 16:53:14 +01007715
Thomas Petazzoni26975822017-03-07 16:53:14 +01007716 mvpp2_port_periodic_xon_disable(port);
7717
7718 if (priv->hw_version == MVPP21)
7719 mvpp2_port_fc_adv_enable(port);
7720
7721 mvpp2_port_reset(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007722
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007723 port->pcpu = alloc_percpu(struct mvpp2_port_pcpu);
7724 if (!port->pcpu) {
7725 err = -ENOMEM;
7726 goto err_free_txq_pcpu;
7727 }
7728
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007729 if (!port->has_tx_irqs) {
7730 for_each_present_cpu(cpu) {
7731 port_pcpu = per_cpu_ptr(port->pcpu, cpu);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007732
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007733 hrtimer_init(&port_pcpu->tx_done_timer, CLOCK_MONOTONIC,
7734 HRTIMER_MODE_REL_PINNED);
7735 port_pcpu->tx_done_timer.function = mvpp2_hr_timer_cb;
7736 port_pcpu->timer_scheduled = false;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007737
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007738 tasklet_init(&port_pcpu->tx_done_tasklet,
7739 mvpp2_tx_proc_cb,
7740 (unsigned long)dev);
7741 }
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007742 }
7743
Antoine Ténart186cd4d2017-08-23 09:46:56 +02007744 features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007745 dev->features = features | NETIF_F_RXCSUM;
7746 dev->hw_features |= features | NETIF_F_RXCSUM | NETIF_F_GRO;
7747 dev->vlan_features |= features;
Antoine Tenart1d17db02017-10-30 11:23:31 +01007748 dev->gso_max_segs = MVPP2_MAX_TSO_SEGS;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007749
Jarod Wilson57779872016-10-17 15:54:06 -04007750 /* MTU range: 68 - 9676 */
7751 dev->min_mtu = ETH_MIN_MTU;
7752 /* 9676 == 9700 - 20 and rounding to 8 */
7753 dev->max_mtu = 9676;
7754
Marcin Wojtas3f518502014-07-10 16:52:13 -03007755 err = register_netdev(dev);
7756 if (err < 0) {
7757 dev_err(&pdev->dev, "failed to register netdev\n");
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007758 goto err_free_port_pcpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007759 }
7760 netdev_info(dev, "Using %s mac address %pM\n", mac_from, dev->dev_addr);
7761
Yan Markman6bf69a12017-09-25 14:59:47 +02007762 priv->port_list[index] = port;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007763 return 0;
7764
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007765err_free_port_pcpu:
7766 free_percpu(port->pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007767err_free_txq_pcpu:
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007768 for (i = 0; i < port->ntxqs; i++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007769 free_percpu(port->txqs[i]->pcpu);
7770err_free_stats:
7771 free_percpu(port->stats);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007772err_free_irq:
7773 if (port->link_irq)
7774 irq_dispose_mapping(port->link_irq);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007775err_deinit_qvecs:
7776 mvpp2_queue_vectors_deinit(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007777err_free_netdev:
Peter Chenccb80392016-08-01 15:02:37 +08007778 of_node_put(phy_node);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007779 free_netdev(dev);
7780 return err;
7781}
7782
7783/* Ports removal routine */
7784static void mvpp2_port_remove(struct mvpp2_port *port)
7785{
7786 int i;
7787
7788 unregister_netdev(port->dev);
Peter Chenccb80392016-08-01 15:02:37 +08007789 of_node_put(port->phy_node);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007790 free_percpu(port->pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007791 free_percpu(port->stats);
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007792 for (i = 0; i < port->ntxqs; i++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007793 free_percpu(port->txqs[i]->pcpu);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007794 mvpp2_queue_vectors_deinit(port);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007795 if (port->link_irq)
7796 irq_dispose_mapping(port->link_irq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007797 free_netdev(port->dev);
7798}
7799
7800/* Initialize decoding windows */
7801static void mvpp2_conf_mbus_windows(const struct mbus_dram_target_info *dram,
7802 struct mvpp2 *priv)
7803{
7804 u32 win_enable;
7805 int i;
7806
7807 for (i = 0; i < 6; i++) {
7808 mvpp2_write(priv, MVPP2_WIN_BASE(i), 0);
7809 mvpp2_write(priv, MVPP2_WIN_SIZE(i), 0);
7810
7811 if (i < 4)
7812 mvpp2_write(priv, MVPP2_WIN_REMAP(i), 0);
7813 }
7814
7815 win_enable = 0;
7816
7817 for (i = 0; i < dram->num_cs; i++) {
7818 const struct mbus_dram_window *cs = dram->cs + i;
7819
7820 mvpp2_write(priv, MVPP2_WIN_BASE(i),
7821 (cs->base & 0xffff0000) | (cs->mbus_attr << 8) |
7822 dram->mbus_dram_target_id);
7823
7824 mvpp2_write(priv, MVPP2_WIN_SIZE(i),
7825 (cs->size - 1) & 0xffff0000);
7826
7827 win_enable |= (1 << i);
7828 }
7829
7830 mvpp2_write(priv, MVPP2_BASE_ADDR_ENABLE, win_enable);
7831}
7832
7833/* Initialize Rx FIFO's */
7834static void mvpp2_rx_fifo_init(struct mvpp2 *priv)
7835{
7836 int port;
7837
7838 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
7839 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
Antoine Tenart2d1d7df2017-10-30 11:23:28 +01007840 MVPP2_RX_FIFO_PORT_DATA_SIZE_4KB);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007841 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
Antoine Tenart2d1d7df2017-10-30 11:23:28 +01007842 MVPP2_RX_FIFO_PORT_ATTR_SIZE_4KB);
7843 }
7844
7845 mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
7846 MVPP2_RX_FIFO_PORT_MIN_PKT);
7847 mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
7848}
7849
7850static void mvpp22_rx_fifo_init(struct mvpp2 *priv)
7851{
7852 int port;
7853
7854 /* The FIFO size parameters are set depending on the maximum speed a
7855 * given port can handle:
7856 * - Port 0: 10Gbps
7857 * - Port 1: 2.5Gbps
7858 * - Ports 2 and 3: 1Gbps
7859 */
7860
7861 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(0),
7862 MVPP2_RX_FIFO_PORT_DATA_SIZE_32KB);
7863 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(0),
7864 MVPP2_RX_FIFO_PORT_ATTR_SIZE_32KB);
7865
7866 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(1),
7867 MVPP2_RX_FIFO_PORT_DATA_SIZE_8KB);
7868 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(1),
7869 MVPP2_RX_FIFO_PORT_ATTR_SIZE_8KB);
7870
7871 for (port = 2; port < MVPP2_MAX_PORTS; port++) {
7872 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
7873 MVPP2_RX_FIFO_PORT_DATA_SIZE_4KB);
7874 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
7875 MVPP2_RX_FIFO_PORT_ATTR_SIZE_4KB);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007876 }
7877
7878 mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
7879 MVPP2_RX_FIFO_PORT_MIN_PKT);
7880 mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
7881}
7882
Antoine Tenart7c10f972017-10-30 11:23:29 +01007883/* Initialize Tx FIFO's */
7884static void mvpp22_tx_fifo_init(struct mvpp2 *priv)
7885{
7886 int port;
7887
7888 for (port = 0; port < MVPP2_MAX_PORTS; port++)
7889 mvpp2_write(priv, MVPP22_TX_FIFO_SIZE_REG(port),
7890 MVPP22_TX_FIFO_DATA_SIZE_3KB);
7891}
7892
Thomas Petazzoni6763ce32017-03-07 16:53:15 +01007893static void mvpp2_axi_init(struct mvpp2 *priv)
7894{
7895 u32 val, rdval, wrval;
7896
7897 mvpp2_write(priv, MVPP22_BM_ADDR_HIGH_RLS_REG, 0x0);
7898
7899 /* AXI Bridge Configuration */
7900
7901 rdval = MVPP22_AXI_CODE_CACHE_RD_CACHE
7902 << MVPP22_AXI_ATTR_CACHE_OFFS;
7903 rdval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
7904 << MVPP22_AXI_ATTR_DOMAIN_OFFS;
7905
7906 wrval = MVPP22_AXI_CODE_CACHE_WR_CACHE
7907 << MVPP22_AXI_ATTR_CACHE_OFFS;
7908 wrval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
7909 << MVPP22_AXI_ATTR_DOMAIN_OFFS;
7910
7911 /* BM */
7912 mvpp2_write(priv, MVPP22_AXI_BM_WR_ATTR_REG, wrval);
7913 mvpp2_write(priv, MVPP22_AXI_BM_RD_ATTR_REG, rdval);
7914
7915 /* Descriptors */
7916 mvpp2_write(priv, MVPP22_AXI_AGGRQ_DESCR_RD_ATTR_REG, rdval);
7917 mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_WR_ATTR_REG, wrval);
7918 mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_RD_ATTR_REG, rdval);
7919 mvpp2_write(priv, MVPP22_AXI_RXQ_DESCR_WR_ATTR_REG, wrval);
7920
7921 /* Buffer Data */
7922 mvpp2_write(priv, MVPP22_AXI_TX_DATA_RD_ATTR_REG, rdval);
7923 mvpp2_write(priv, MVPP22_AXI_RX_DATA_WR_ATTR_REG, wrval);
7924
7925 val = MVPP22_AXI_CODE_CACHE_NON_CACHE
7926 << MVPP22_AXI_CODE_CACHE_OFFS;
7927 val |= MVPP22_AXI_CODE_DOMAIN_SYSTEM
7928 << MVPP22_AXI_CODE_DOMAIN_OFFS;
7929 mvpp2_write(priv, MVPP22_AXI_RD_NORMAL_CODE_REG, val);
7930 mvpp2_write(priv, MVPP22_AXI_WR_NORMAL_CODE_REG, val);
7931
7932 val = MVPP22_AXI_CODE_CACHE_RD_CACHE
7933 << MVPP22_AXI_CODE_CACHE_OFFS;
7934 val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
7935 << MVPP22_AXI_CODE_DOMAIN_OFFS;
7936
7937 mvpp2_write(priv, MVPP22_AXI_RD_SNOOP_CODE_REG, val);
7938
7939 val = MVPP22_AXI_CODE_CACHE_WR_CACHE
7940 << MVPP22_AXI_CODE_CACHE_OFFS;
7941 val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
7942 << MVPP22_AXI_CODE_DOMAIN_OFFS;
7943
7944 mvpp2_write(priv, MVPP22_AXI_WR_SNOOP_CODE_REG, val);
7945}
7946
Marcin Wojtas3f518502014-07-10 16:52:13 -03007947/* Initialize network controller common part HW */
7948static int mvpp2_init(struct platform_device *pdev, struct mvpp2 *priv)
7949{
7950 const struct mbus_dram_target_info *dram_target_info;
7951 int err, i;
Marcin Wojtas08a23752014-07-21 13:48:12 -03007952 u32 val;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007953
Marcin Wojtas3f518502014-07-10 16:52:13 -03007954 /* MBUS windows configuration */
7955 dram_target_info = mv_mbus_dram_info();
7956 if (dram_target_info)
7957 mvpp2_conf_mbus_windows(dram_target_info, priv);
7958
Thomas Petazzoni6763ce32017-03-07 16:53:15 +01007959 if (priv->hw_version == MVPP22)
7960 mvpp2_axi_init(priv);
7961
Marcin Wojtas08a23752014-07-21 13:48:12 -03007962 /* Disable HW PHY polling */
Thomas Petazzoni26975822017-03-07 16:53:14 +01007963 if (priv->hw_version == MVPP21) {
7964 val = readl(priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
7965 val |= MVPP2_PHY_AN_STOP_SMI0_MASK;
7966 writel(val, priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
7967 } else {
7968 val = readl(priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
7969 val &= ~MVPP22_SMI_POLLING_EN;
7970 writel(val, priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
7971 }
Marcin Wojtas08a23752014-07-21 13:48:12 -03007972
Marcin Wojtas3f518502014-07-10 16:52:13 -03007973 /* Allocate and initialize aggregated TXQs */
7974 priv->aggr_txqs = devm_kcalloc(&pdev->dev, num_present_cpus(),
Markus Elfringd7ce3ce2017-04-17 08:48:23 +02007975 sizeof(*priv->aggr_txqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03007976 GFP_KERNEL);
7977 if (!priv->aggr_txqs)
7978 return -ENOMEM;
7979
7980 for_each_present_cpu(i) {
7981 priv->aggr_txqs[i].id = i;
7982 priv->aggr_txqs[i].size = MVPP2_AGGR_TXQ_SIZE;
Antoine Ténart85affd72017-08-23 09:46:55 +02007983 err = mvpp2_aggr_txq_init(pdev, &priv->aggr_txqs[i], i, priv);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007984 if (err < 0)
7985 return err;
7986 }
7987
Antoine Tenart7c10f972017-10-30 11:23:29 +01007988 /* Fifo Init */
7989 if (priv->hw_version == MVPP21) {
Antoine Tenart2d1d7df2017-10-30 11:23:28 +01007990 mvpp2_rx_fifo_init(priv);
Antoine Tenart7c10f972017-10-30 11:23:29 +01007991 } else {
Antoine Tenart2d1d7df2017-10-30 11:23:28 +01007992 mvpp22_rx_fifo_init(priv);
Antoine Tenart7c10f972017-10-30 11:23:29 +01007993 mvpp22_tx_fifo_init(priv);
7994 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03007995
Thomas Petazzoni26975822017-03-07 16:53:14 +01007996 if (priv->hw_version == MVPP21)
7997 writel(MVPP2_EXT_GLOBAL_CTRL_DEFAULT,
7998 priv->lms_base + MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007999
8000 /* Allow cache snoop when transmiting packets */
8001 mvpp2_write(priv, MVPP2_TX_SNOOP_REG, 0x1);
8002
8003 /* Buffer Manager initialization */
8004 err = mvpp2_bm_init(pdev, priv);
8005 if (err < 0)
8006 return err;
8007
8008 /* Parser default initialization */
8009 err = mvpp2_prs_default_init(pdev, priv);
8010 if (err < 0)
8011 return err;
8012
8013 /* Classifier default initialization */
8014 mvpp2_cls_init(priv);
8015
8016 return 0;
8017}
8018
8019static int mvpp2_probe(struct platform_device *pdev)
8020{
8021 struct device_node *dn = pdev->dev.of_node;
8022 struct device_node *port_node;
8023 struct mvpp2 *priv;
8024 struct resource *res;
Thomas Petazzonia7868412017-03-07 16:53:13 +01008025 void __iomem *base;
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02008026 int port_count, i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008027 int err;
8028
Markus Elfring0b92e592017-04-17 08:38:32 +02008029 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008030 if (!priv)
8031 return -ENOMEM;
8032
Thomas Petazzonifaca9242017-03-07 16:53:06 +01008033 priv->hw_version =
8034 (unsigned long)of_device_get_match_data(&pdev->dev);
8035
Marcin Wojtas3f518502014-07-10 16:52:13 -03008036 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thomas Petazzonia7868412017-03-07 16:53:13 +01008037 base = devm_ioremap_resource(&pdev->dev, res);
8038 if (IS_ERR(base))
8039 return PTR_ERR(base);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008040
Thomas Petazzonia7868412017-03-07 16:53:13 +01008041 if (priv->hw_version == MVPP21) {
8042 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
8043 priv->lms_base = devm_ioremap_resource(&pdev->dev, res);
8044 if (IS_ERR(priv->lms_base))
8045 return PTR_ERR(priv->lms_base);
8046 } else {
8047 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
8048 priv->iface_base = devm_ioremap_resource(&pdev->dev, res);
8049 if (IS_ERR(priv->iface_base))
8050 return PTR_ERR(priv->iface_base);
Antoine Ténartf84bf382017-08-22 19:08:27 +02008051
8052 priv->sysctrl_base =
8053 syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
8054 "marvell,system-controller");
8055 if (IS_ERR(priv->sysctrl_base))
8056 /* The system controller regmap is optional for dt
8057 * compatibility reasons. When not provided, the
8058 * configuration of the GoP relies on the
8059 * firmware/bootloader.
8060 */
8061 priv->sysctrl_base = NULL;
Thomas Petazzonia7868412017-03-07 16:53:13 +01008062 }
8063
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02008064 for (i = 0; i < MVPP2_MAX_THREADS; i++) {
Thomas Petazzonia7868412017-03-07 16:53:13 +01008065 u32 addr_space_sz;
8066
8067 addr_space_sz = (priv->hw_version == MVPP21 ?
8068 MVPP21_ADDR_SPACE_SZ : MVPP22_ADDR_SPACE_SZ);
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02008069 priv->swth_base[i] = base + i * addr_space_sz;
Thomas Petazzonia7868412017-03-07 16:53:13 +01008070 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03008071
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01008072 if (priv->hw_version == MVPP21)
8073 priv->max_port_rxqs = 8;
8074 else
8075 priv->max_port_rxqs = 32;
8076
Marcin Wojtas3f518502014-07-10 16:52:13 -03008077 priv->pp_clk = devm_clk_get(&pdev->dev, "pp_clk");
8078 if (IS_ERR(priv->pp_clk))
8079 return PTR_ERR(priv->pp_clk);
8080 err = clk_prepare_enable(priv->pp_clk);
8081 if (err < 0)
8082 return err;
8083
8084 priv->gop_clk = devm_clk_get(&pdev->dev, "gop_clk");
8085 if (IS_ERR(priv->gop_clk)) {
8086 err = PTR_ERR(priv->gop_clk);
8087 goto err_pp_clk;
8088 }
8089 err = clk_prepare_enable(priv->gop_clk);
8090 if (err < 0)
8091 goto err_pp_clk;
8092
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008093 if (priv->hw_version == MVPP22) {
8094 priv->mg_clk = devm_clk_get(&pdev->dev, "mg_clk");
8095 if (IS_ERR(priv->mg_clk)) {
8096 err = PTR_ERR(priv->mg_clk);
8097 goto err_gop_clk;
8098 }
8099
8100 err = clk_prepare_enable(priv->mg_clk);
8101 if (err < 0)
8102 goto err_gop_clk;
Gregory CLEMENT4792ea02017-09-29 14:27:39 +02008103
8104 priv->axi_clk = devm_clk_get(&pdev->dev, "axi_clk");
8105 if (IS_ERR(priv->axi_clk)) {
8106 err = PTR_ERR(priv->axi_clk);
8107 if (err == -EPROBE_DEFER)
8108 goto err_gop_clk;
8109 priv->axi_clk = NULL;
8110 } else {
8111 err = clk_prepare_enable(priv->axi_clk);
8112 if (err < 0)
8113 goto err_gop_clk;
8114 }
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008115 }
8116
Marcin Wojtas3f518502014-07-10 16:52:13 -03008117 /* Get system's tclk rate */
8118 priv->tclk = clk_get_rate(priv->pp_clk);
8119
Thomas Petazzoni2067e0a2017-03-07 16:53:19 +01008120 if (priv->hw_version == MVPP22) {
8121 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(40));
8122 if (err)
8123 goto err_mg_clk;
8124 /* Sadly, the BM pools all share the same register to
8125 * store the high 32 bits of their address. So they
8126 * must all have the same high 32 bits, which forces
8127 * us to restrict coherent memory to DMA_BIT_MASK(32).
8128 */
8129 err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
8130 if (err)
8131 goto err_mg_clk;
8132 }
8133
Marcin Wojtas3f518502014-07-10 16:52:13 -03008134 /* Initialize network controller */
8135 err = mvpp2_init(pdev, priv);
8136 if (err < 0) {
8137 dev_err(&pdev->dev, "failed to initialize controller\n");
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008138 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008139 }
8140
8141 port_count = of_get_available_child_count(dn);
8142 if (port_count == 0) {
8143 dev_err(&pdev->dev, "no ports enabled\n");
Wei Yongjun575a1932014-07-20 22:02:43 +08008144 err = -ENODEV;
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008145 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008146 }
8147
8148 priv->port_list = devm_kcalloc(&pdev->dev, port_count,
Markus Elfring0b92e592017-04-17 08:38:32 +02008149 sizeof(*priv->port_list),
8150 GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008151 if (!priv->port_list) {
8152 err = -ENOMEM;
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008153 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008154 }
8155
8156 /* Initialize ports */
Yan Markman6bf69a12017-09-25 14:59:47 +02008157 i = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008158 for_each_available_child_of_node(dn, port_node) {
Yan Markman6bf69a12017-09-25 14:59:47 +02008159 err = mvpp2_port_probe(pdev, port_node, priv, i);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008160 if (err < 0)
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008161 goto err_mg_clk;
Yan Markman6bf69a12017-09-25 14:59:47 +02008162 i++;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008163 }
8164
8165 platform_set_drvdata(pdev, priv);
8166 return 0;
8167
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008168err_mg_clk:
Gregory CLEMENT4792ea02017-09-29 14:27:39 +02008169 clk_disable_unprepare(priv->axi_clk);
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008170 if (priv->hw_version == MVPP22)
8171 clk_disable_unprepare(priv->mg_clk);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008172err_gop_clk:
8173 clk_disable_unprepare(priv->gop_clk);
8174err_pp_clk:
8175 clk_disable_unprepare(priv->pp_clk);
8176 return err;
8177}
8178
8179static int mvpp2_remove(struct platform_device *pdev)
8180{
8181 struct mvpp2 *priv = platform_get_drvdata(pdev);
8182 struct device_node *dn = pdev->dev.of_node;
8183 struct device_node *port_node;
8184 int i = 0;
8185
8186 for_each_available_child_of_node(dn, port_node) {
8187 if (priv->port_list[i])
8188 mvpp2_port_remove(priv->port_list[i]);
8189 i++;
8190 }
8191
8192 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
8193 struct mvpp2_bm_pool *bm_pool = &priv->bm_pools[i];
8194
8195 mvpp2_bm_pool_destroy(pdev, priv, bm_pool);
8196 }
8197
8198 for_each_present_cpu(i) {
8199 struct mvpp2_tx_queue *aggr_txq = &priv->aggr_txqs[i];
8200
8201 dma_free_coherent(&pdev->dev,
8202 MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
8203 aggr_txq->descs,
Thomas Petazzoni20396132017-03-07 16:53:00 +01008204 aggr_txq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008205 }
8206
Gregory CLEMENT4792ea02017-09-29 14:27:39 +02008207 clk_disable_unprepare(priv->axi_clk);
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008208 clk_disable_unprepare(priv->mg_clk);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008209 clk_disable_unprepare(priv->pp_clk);
8210 clk_disable_unprepare(priv->gop_clk);
8211
8212 return 0;
8213}
8214
8215static const struct of_device_id mvpp2_match[] = {
Thomas Petazzonifaca9242017-03-07 16:53:06 +01008216 {
8217 .compatible = "marvell,armada-375-pp2",
8218 .data = (void *)MVPP21,
8219 },
Thomas Petazzonifc5e1552017-03-07 16:53:20 +01008220 {
8221 .compatible = "marvell,armada-7k-pp22",
8222 .data = (void *)MVPP22,
8223 },
Marcin Wojtas3f518502014-07-10 16:52:13 -03008224 { }
8225};
8226MODULE_DEVICE_TABLE(of, mvpp2_match);
8227
8228static struct platform_driver mvpp2_driver = {
8229 .probe = mvpp2_probe,
8230 .remove = mvpp2_remove,
8231 .driver = {
8232 .name = MVPP2_DRIVER_NAME,
8233 .of_match_table = mvpp2_match,
8234 },
8235};
8236
8237module_platform_driver(mvpp2_driver);
8238
8239MODULE_DESCRIPTION("Marvell PPv2 Ethernet Driver - www.marvell.com");
8240MODULE_AUTHOR("Marcin Wojtas <mw@semihalf.com>");
Ezequiel Garciac6340992014-07-14 10:34:47 -03008241MODULE_LICENSE("GPL v2");