blob: 9bd35f2291d64a30da5f0d096df157d9888df1ec [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
Marcin Wojtasa75edc72018-01-18 13:31:44 +010013#include <linux/acpi.h>
Marcin Wojtas3f518502014-07-10 16:52:13 -030014#include <linux/kernel.h>
15#include <linux/netdevice.h>
16#include <linux/etherdevice.h>
17#include <linux/platform_device.h>
18#include <linux/skbuff.h>
19#include <linux/inetdevice.h>
20#include <linux/mbus.h>
21#include <linux/module.h>
Antoine Ténartf84bf382017-08-22 19:08:27 +020022#include <linux/mfd/syscon.h>
Marcin Wojtas3f518502014-07-10 16:52:13 -030023#include <linux/interrupt.h>
24#include <linux/cpumask.h>
25#include <linux/of.h>
26#include <linux/of_irq.h>
27#include <linux/of_mdio.h>
28#include <linux/of_net.h>
29#include <linux/of_address.h>
Thomas Petazzonifaca9242017-03-07 16:53:06 +010030#include <linux/of_device.h>
Marcin Wojtas3f518502014-07-10 16:52:13 -030031#include <linux/phy.h>
Antoine Tenart542897d2017-08-30 10:29:15 +020032#include <linux/phy/phy.h>
Marcin Wojtas3f518502014-07-10 16:52:13 -030033#include <linux/clk.h>
Marcin Wojtasedc660f2015-08-06 19:00:30 +020034#include <linux/hrtimer.h>
35#include <linux/ktime.h>
Antoine Ténartf84bf382017-08-22 19:08:27 +020036#include <linux/regmap.h>
Marcin Wojtas3f518502014-07-10 16:52:13 -030037#include <uapi/linux/ppp_defs.h>
38#include <net/ip.h>
39#include <net/ipv6.h>
Antoine Ténart186cd4d2017-08-23 09:46:56 +020040#include <net/tso.h>
Marcin Wojtas3f518502014-07-10 16:52:13 -030041
Antoine Tenart7c10f972017-10-30 11:23:29 +010042/* Fifo Registers */
Marcin Wojtas3f518502014-07-10 16:52:13 -030043#define MVPP2_RX_DATA_FIFO_SIZE_REG(port) (0x00 + 4 * (port))
44#define MVPP2_RX_ATTR_FIFO_SIZE_REG(port) (0x20 + 4 * (port))
45#define MVPP2_RX_MIN_PKT_SIZE_REG 0x60
46#define MVPP2_RX_FIFO_INIT_REG 0x64
Yan Markman93ff1302018-03-05 15:16:52 +010047#define MVPP22_TX_FIFO_THRESH_REG(port) (0x8840 + 4 * (port))
Antoine Tenart7c10f972017-10-30 11:23:29 +010048#define MVPP22_TX_FIFO_SIZE_REG(port) (0x8860 + 4 * (port))
Marcin Wojtas3f518502014-07-10 16:52:13 -030049
50/* RX DMA Top Registers */
51#define MVPP2_RX_CTRL_REG(port) (0x140 + 4 * (port))
52#define MVPP2_RX_LOW_LATENCY_PKT_SIZE(s) (((s) & 0xfff) << 16)
53#define MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK BIT(31)
54#define MVPP2_POOL_BUF_SIZE_REG(pool) (0x180 + 4 * (pool))
55#define MVPP2_POOL_BUF_SIZE_OFFSET 5
56#define MVPP2_RXQ_CONFIG_REG(rxq) (0x800 + 4 * (rxq))
57#define MVPP2_SNOOP_PKT_SIZE_MASK 0x1ff
58#define MVPP2_SNOOP_BUF_HDR_MASK BIT(9)
59#define MVPP2_RXQ_POOL_SHORT_OFFS 20
Thomas Petazzoni5eac8922017-03-07 16:53:10 +010060#define MVPP21_RXQ_POOL_SHORT_MASK 0x700000
61#define MVPP22_RXQ_POOL_SHORT_MASK 0xf00000
Marcin Wojtas3f518502014-07-10 16:52:13 -030062#define MVPP2_RXQ_POOL_LONG_OFFS 24
Thomas Petazzoni5eac8922017-03-07 16:53:10 +010063#define MVPP21_RXQ_POOL_LONG_MASK 0x7000000
64#define MVPP22_RXQ_POOL_LONG_MASK 0xf000000
Marcin Wojtas3f518502014-07-10 16:52:13 -030065#define MVPP2_RXQ_PACKET_OFFSET_OFFS 28
66#define MVPP2_RXQ_PACKET_OFFSET_MASK 0x70000000
67#define MVPP2_RXQ_DISABLE_MASK BIT(31)
68
Maxime Chevallier56beda32018-02-28 10:14:13 +010069/* Top Registers */
70#define MVPP2_MH_REG(port) (0x5040 + 4 * (port))
71#define MVPP2_DSA_EXTENDED BIT(5)
72
Marcin Wojtas3f518502014-07-10 16:52:13 -030073/* Parser Registers */
74#define MVPP2_PRS_INIT_LOOKUP_REG 0x1000
75#define MVPP2_PRS_PORT_LU_MAX 0xf
76#define MVPP2_PRS_PORT_LU_MASK(port) (0xff << ((port) * 4))
77#define MVPP2_PRS_PORT_LU_VAL(port, val) ((val) << ((port) * 4))
78#define MVPP2_PRS_INIT_OFFS_REG(port) (0x1004 + ((port) & 4))
79#define MVPP2_PRS_INIT_OFF_MASK(port) (0x3f << (((port) % 4) * 8))
80#define MVPP2_PRS_INIT_OFF_VAL(port, val) ((val) << (((port) % 4) * 8))
81#define MVPP2_PRS_MAX_LOOP_REG(port) (0x100c + ((port) & 4))
82#define MVPP2_PRS_MAX_LOOP_MASK(port) (0xff << (((port) % 4) * 8))
83#define MVPP2_PRS_MAX_LOOP_VAL(port, val) ((val) << (((port) % 4) * 8))
84#define MVPP2_PRS_TCAM_IDX_REG 0x1100
85#define MVPP2_PRS_TCAM_DATA_REG(idx) (0x1104 + (idx) * 4)
86#define MVPP2_PRS_TCAM_INV_MASK BIT(31)
87#define MVPP2_PRS_SRAM_IDX_REG 0x1200
88#define MVPP2_PRS_SRAM_DATA_REG(idx) (0x1204 + (idx) * 4)
89#define MVPP2_PRS_TCAM_CTRL_REG 0x1230
90#define MVPP2_PRS_TCAM_EN_MASK BIT(0)
91
Antoine Tenart1d7d15d2017-10-30 11:23:30 +010092/* RSS Registers */
93#define MVPP22_RSS_INDEX 0x1500
Antoine Tenart8a7b7412017-12-08 10:24:20 +010094#define MVPP22_RSS_INDEX_TABLE_ENTRY(idx) (idx)
Antoine Tenart1d7d15d2017-10-30 11:23:30 +010095#define MVPP22_RSS_INDEX_TABLE(idx) ((idx) << 8)
96#define MVPP22_RSS_INDEX_QUEUE(idx) ((idx) << 16)
97#define MVPP22_RSS_TABLE_ENTRY 0x1508
98#define MVPP22_RSS_TABLE 0x1510
99#define MVPP22_RSS_TABLE_POINTER(p) (p)
100#define MVPP22_RSS_WIDTH 0x150c
101
Marcin Wojtas3f518502014-07-10 16:52:13 -0300102/* Classifier Registers */
103#define MVPP2_CLS_MODE_REG 0x1800
104#define MVPP2_CLS_MODE_ACTIVE_MASK BIT(0)
105#define MVPP2_CLS_PORT_WAY_REG 0x1810
106#define MVPP2_CLS_PORT_WAY_MASK(port) (1 << (port))
107#define MVPP2_CLS_LKP_INDEX_REG 0x1814
108#define MVPP2_CLS_LKP_INDEX_WAY_OFFS 6
109#define MVPP2_CLS_LKP_TBL_REG 0x1818
110#define MVPP2_CLS_LKP_TBL_RXQ_MASK 0xff
111#define MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK BIT(25)
112#define MVPP2_CLS_FLOW_INDEX_REG 0x1820
113#define MVPP2_CLS_FLOW_TBL0_REG 0x1824
114#define MVPP2_CLS_FLOW_TBL1_REG 0x1828
115#define MVPP2_CLS_FLOW_TBL2_REG 0x182c
116#define MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port) (0x1980 + ((port) * 4))
117#define MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS 3
118#define MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK 0x7
119#define MVPP2_CLS_SWFWD_P2HQ_REG(port) (0x19b0 + ((port) * 4))
120#define MVPP2_CLS_SWFWD_PCTRL_REG 0x19d0
121#define MVPP2_CLS_SWFWD_PCTRL_MASK(port) (1 << (port))
122
123/* Descriptor Manager Top Registers */
124#define MVPP2_RXQ_NUM_REG 0x2040
125#define MVPP2_RXQ_DESC_ADDR_REG 0x2044
Thomas Petazzonib02f31f2017-03-07 16:53:12 +0100126#define MVPP22_DESC_ADDR_OFFS 8
Marcin Wojtas3f518502014-07-10 16:52:13 -0300127#define MVPP2_RXQ_DESC_SIZE_REG 0x2048
128#define MVPP2_RXQ_DESC_SIZE_MASK 0x3ff0
129#define MVPP2_RXQ_STATUS_UPDATE_REG(rxq) (0x3000 + 4 * (rxq))
130#define MVPP2_RXQ_NUM_PROCESSED_OFFSET 0
131#define MVPP2_RXQ_NUM_NEW_OFFSET 16
132#define MVPP2_RXQ_STATUS_REG(rxq) (0x3400 + 4 * (rxq))
133#define MVPP2_RXQ_OCCUPIED_MASK 0x3fff
134#define MVPP2_RXQ_NON_OCCUPIED_OFFSET 16
135#define MVPP2_RXQ_NON_OCCUPIED_MASK 0x3fff0000
136#define MVPP2_RXQ_THRESH_REG 0x204c
137#define MVPP2_OCCUPIED_THRESH_OFFSET 0
138#define MVPP2_OCCUPIED_THRESH_MASK 0x3fff
139#define MVPP2_RXQ_INDEX_REG 0x2050
140#define MVPP2_TXQ_NUM_REG 0x2080
141#define MVPP2_TXQ_DESC_ADDR_REG 0x2084
142#define MVPP2_TXQ_DESC_SIZE_REG 0x2088
143#define MVPP2_TXQ_DESC_SIZE_MASK 0x3ff0
Thomas Petazzoni213f4282017-08-03 10:42:00 +0200144#define MVPP2_TXQ_THRESH_REG 0x2094
145#define MVPP2_TXQ_THRESH_OFFSET 16
146#define MVPP2_TXQ_THRESH_MASK 0x3fff
Marcin Wojtas3f518502014-07-10 16:52:13 -0300147#define MVPP2_AGGR_TXQ_UPDATE_REG 0x2090
Marcin Wojtas3f518502014-07-10 16:52:13 -0300148#define MVPP2_TXQ_INDEX_REG 0x2098
149#define MVPP2_TXQ_PREF_BUF_REG 0x209c
150#define MVPP2_PREF_BUF_PTR(desc) ((desc) & 0xfff)
151#define MVPP2_PREF_BUF_SIZE_4 (BIT(12) | BIT(13))
152#define MVPP2_PREF_BUF_SIZE_16 (BIT(12) | BIT(14))
153#define MVPP2_PREF_BUF_THRESH(val) ((val) << 17)
154#define MVPP2_TXQ_DRAIN_EN_MASK BIT(31)
155#define MVPP2_TXQ_PENDING_REG 0x20a0
156#define MVPP2_TXQ_PENDING_MASK 0x3fff
157#define MVPP2_TXQ_INT_STATUS_REG 0x20a4
158#define MVPP2_TXQ_SENT_REG(txq) (0x3c00 + 4 * (txq))
159#define MVPP2_TRANSMITTED_COUNT_OFFSET 16
160#define MVPP2_TRANSMITTED_COUNT_MASK 0x3fff0000
161#define MVPP2_TXQ_RSVD_REQ_REG 0x20b0
162#define MVPP2_TXQ_RSVD_REQ_Q_OFFSET 16
163#define MVPP2_TXQ_RSVD_RSLT_REG 0x20b4
164#define MVPP2_TXQ_RSVD_RSLT_MASK 0x3fff
165#define MVPP2_TXQ_RSVD_CLR_REG 0x20b8
166#define MVPP2_TXQ_RSVD_CLR_OFFSET 16
167#define MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu) (0x2100 + 4 * (cpu))
Thomas Petazzonib02f31f2017-03-07 16:53:12 +0100168#define MVPP22_AGGR_TXQ_DESC_ADDR_OFFS 8
Marcin Wojtas3f518502014-07-10 16:52:13 -0300169#define MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu) (0x2140 + 4 * (cpu))
170#define MVPP2_AGGR_TXQ_DESC_SIZE_MASK 0x3ff0
171#define MVPP2_AGGR_TXQ_STATUS_REG(cpu) (0x2180 + 4 * (cpu))
172#define MVPP2_AGGR_TXQ_PENDING_MASK 0x3fff
173#define MVPP2_AGGR_TXQ_INDEX_REG(cpu) (0x21c0 + 4 * (cpu))
174
175/* MBUS bridge registers */
176#define MVPP2_WIN_BASE(w) (0x4000 + ((w) << 2))
177#define MVPP2_WIN_SIZE(w) (0x4020 + ((w) << 2))
178#define MVPP2_WIN_REMAP(w) (0x4040 + ((w) << 2))
179#define MVPP2_BASE_ADDR_ENABLE 0x4060
180
Thomas Petazzoni6763ce32017-03-07 16:53:15 +0100181/* AXI Bridge Registers */
182#define MVPP22_AXI_BM_WR_ATTR_REG 0x4100
183#define MVPP22_AXI_BM_RD_ATTR_REG 0x4104
184#define MVPP22_AXI_AGGRQ_DESCR_RD_ATTR_REG 0x4110
185#define MVPP22_AXI_TXQ_DESCR_WR_ATTR_REG 0x4114
186#define MVPP22_AXI_TXQ_DESCR_RD_ATTR_REG 0x4118
187#define MVPP22_AXI_RXQ_DESCR_WR_ATTR_REG 0x411c
188#define MVPP22_AXI_RX_DATA_WR_ATTR_REG 0x4120
189#define MVPP22_AXI_TX_DATA_RD_ATTR_REG 0x4130
190#define MVPP22_AXI_RD_NORMAL_CODE_REG 0x4150
191#define MVPP22_AXI_RD_SNOOP_CODE_REG 0x4154
192#define MVPP22_AXI_WR_NORMAL_CODE_REG 0x4160
193#define MVPP22_AXI_WR_SNOOP_CODE_REG 0x4164
194
195/* Values for AXI Bridge registers */
196#define MVPP22_AXI_ATTR_CACHE_OFFS 0
197#define MVPP22_AXI_ATTR_DOMAIN_OFFS 12
198
199#define MVPP22_AXI_CODE_CACHE_OFFS 0
200#define MVPP22_AXI_CODE_DOMAIN_OFFS 4
201
202#define MVPP22_AXI_CODE_CACHE_NON_CACHE 0x3
203#define MVPP22_AXI_CODE_CACHE_WR_CACHE 0x7
204#define MVPP22_AXI_CODE_CACHE_RD_CACHE 0xb
205
206#define MVPP22_AXI_CODE_DOMAIN_OUTER_DOM 2
207#define MVPP22_AXI_CODE_DOMAIN_SYSTEM 3
208
Marcin Wojtas3f518502014-07-10 16:52:13 -0300209/* Interrupt Cause and Mask registers */
Thomas Petazzoni213f4282017-08-03 10:42:00 +0200210#define MVPP2_ISR_TX_THRESHOLD_REG(port) (0x5140 + 4 * (port))
211#define MVPP2_MAX_ISR_TX_THRESHOLD 0xfffff0
212
Marcin Wojtas3f518502014-07-10 16:52:13 -0300213#define MVPP2_ISR_RX_THRESHOLD_REG(rxq) (0x5200 + 4 * (rxq))
Thomas Petazzoniab426762017-02-21 11:28:04 +0100214#define MVPP2_MAX_ISR_RX_THRESHOLD 0xfffff0
Thomas Petazzonieb1e93a2017-08-03 10:41:55 +0200215#define MVPP21_ISR_RXQ_GROUP_REG(port) (0x5400 + 4 * (port))
Thomas Petazzonia73fef12017-03-07 16:53:16 +0100216
Antoine Ténart81b66302017-08-22 19:08:21 +0200217#define MVPP22_ISR_RXQ_GROUP_INDEX_REG 0x5400
Thomas Petazzonia73fef12017-03-07 16:53:16 +0100218#define MVPP22_ISR_RXQ_GROUP_INDEX_SUBGROUP_MASK 0xf
Antoine Ténart81b66302017-08-22 19:08:21 +0200219#define MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_MASK 0x380
220#define MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_OFFSET 7
Thomas Petazzonia73fef12017-03-07 16:53:16 +0100221
222#define MVPP22_ISR_RXQ_GROUP_INDEX_SUBGROUP_MASK 0xf
Antoine Ténart81b66302017-08-22 19:08:21 +0200223#define MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_MASK 0x380
Thomas Petazzonia73fef12017-03-07 16:53:16 +0100224
Antoine Ténart81b66302017-08-22 19:08:21 +0200225#define MVPP22_ISR_RXQ_SUB_GROUP_CONFIG_REG 0x5404
226#define MVPP22_ISR_RXQ_SUB_GROUP_STARTQ_MASK 0x1f
227#define MVPP22_ISR_RXQ_SUB_GROUP_SIZE_MASK 0xf00
228#define MVPP22_ISR_RXQ_SUB_GROUP_SIZE_OFFSET 8
Thomas Petazzonia73fef12017-03-07 16:53:16 +0100229
Marcin Wojtas3f518502014-07-10 16:52:13 -0300230#define MVPP2_ISR_ENABLE_REG(port) (0x5420 + 4 * (port))
231#define MVPP2_ISR_ENABLE_INTERRUPT(mask) ((mask) & 0xffff)
232#define MVPP2_ISR_DISABLE_INTERRUPT(mask) (((mask) << 16) & 0xffff0000)
233#define MVPP2_ISR_RX_TX_CAUSE_REG(port) (0x5480 + 4 * (port))
234#define MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK 0xffff
235#define MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK 0xff0000
Thomas Petazzoni213f4282017-08-03 10:42:00 +0200236#define MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_OFFSET 16
Marcin Wojtas3f518502014-07-10 16:52:13 -0300237#define MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK BIT(24)
238#define MVPP2_CAUSE_FCS_ERR_MASK BIT(25)
239#define MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK BIT(26)
240#define MVPP2_CAUSE_TX_EXCEPTION_SUM_MASK BIT(29)
241#define MVPP2_CAUSE_RX_EXCEPTION_SUM_MASK BIT(30)
242#define MVPP2_CAUSE_MISC_SUM_MASK BIT(31)
243#define MVPP2_ISR_RX_TX_MASK_REG(port) (0x54a0 + 4 * (port))
244#define MVPP2_ISR_PON_RX_TX_MASK_REG 0x54bc
245#define MVPP2_PON_CAUSE_RXQ_OCCUP_DESC_ALL_MASK 0xffff
246#define MVPP2_PON_CAUSE_TXP_OCCUP_DESC_ALL_MASK 0x3fc00000
247#define MVPP2_PON_CAUSE_MISC_SUM_MASK BIT(31)
248#define MVPP2_ISR_MISC_CAUSE_REG 0x55b0
249
250/* Buffer Manager registers */
251#define MVPP2_BM_POOL_BASE_REG(pool) (0x6000 + ((pool) * 4))
252#define MVPP2_BM_POOL_BASE_ADDR_MASK 0xfffff80
253#define MVPP2_BM_POOL_SIZE_REG(pool) (0x6040 + ((pool) * 4))
254#define MVPP2_BM_POOL_SIZE_MASK 0xfff0
255#define MVPP2_BM_POOL_READ_PTR_REG(pool) (0x6080 + ((pool) * 4))
256#define MVPP2_BM_POOL_GET_READ_PTR_MASK 0xfff0
257#define MVPP2_BM_POOL_PTRS_NUM_REG(pool) (0x60c0 + ((pool) * 4))
258#define MVPP2_BM_POOL_PTRS_NUM_MASK 0xfff0
259#define MVPP2_BM_BPPI_READ_PTR_REG(pool) (0x6100 + ((pool) * 4))
260#define MVPP2_BM_BPPI_PTRS_NUM_REG(pool) (0x6140 + ((pool) * 4))
261#define MVPP2_BM_BPPI_PTR_NUM_MASK 0x7ff
Stefan Chulskieffbf5f2018-03-05 15:16:51 +0100262#define MVPP22_BM_POOL_PTRS_NUM_MASK 0xfff8
Marcin Wojtas3f518502014-07-10 16:52:13 -0300263#define MVPP2_BM_BPPI_PREFETCH_FULL_MASK BIT(16)
264#define MVPP2_BM_POOL_CTRL_REG(pool) (0x6200 + ((pool) * 4))
265#define MVPP2_BM_START_MASK BIT(0)
266#define MVPP2_BM_STOP_MASK BIT(1)
267#define MVPP2_BM_STATE_MASK BIT(4)
268#define MVPP2_BM_LOW_THRESH_OFFS 8
269#define MVPP2_BM_LOW_THRESH_MASK 0x7f00
270#define MVPP2_BM_LOW_THRESH_VALUE(val) ((val) << \
271 MVPP2_BM_LOW_THRESH_OFFS)
272#define MVPP2_BM_HIGH_THRESH_OFFS 16
273#define MVPP2_BM_HIGH_THRESH_MASK 0x7f0000
274#define MVPP2_BM_HIGH_THRESH_VALUE(val) ((val) << \
275 MVPP2_BM_HIGH_THRESH_OFFS)
276#define MVPP2_BM_INTR_CAUSE_REG(pool) (0x6240 + ((pool) * 4))
277#define MVPP2_BM_RELEASED_DELAY_MASK BIT(0)
278#define MVPP2_BM_ALLOC_FAILED_MASK BIT(1)
279#define MVPP2_BM_BPPE_EMPTY_MASK BIT(2)
280#define MVPP2_BM_BPPE_FULL_MASK BIT(3)
281#define MVPP2_BM_AVAILABLE_BP_LOW_MASK BIT(4)
282#define MVPP2_BM_INTR_MASK_REG(pool) (0x6280 + ((pool) * 4))
283#define MVPP2_BM_PHY_ALLOC_REG(pool) (0x6400 + ((pool) * 4))
284#define MVPP2_BM_PHY_ALLOC_GRNTD_MASK BIT(0)
285#define MVPP2_BM_VIRT_ALLOC_REG 0x6440
Thomas Petazzonid01524d2017-03-07 16:53:09 +0100286#define MVPP22_BM_ADDR_HIGH_ALLOC 0x6444
287#define MVPP22_BM_ADDR_HIGH_PHYS_MASK 0xff
288#define MVPP22_BM_ADDR_HIGH_VIRT_MASK 0xff00
289#define MVPP22_BM_ADDR_HIGH_VIRT_SHIFT 8
Marcin Wojtas3f518502014-07-10 16:52:13 -0300290#define MVPP2_BM_PHY_RLS_REG(pool) (0x6480 + ((pool) * 4))
291#define MVPP2_BM_PHY_RLS_MC_BUFF_MASK BIT(0)
292#define MVPP2_BM_PHY_RLS_PRIO_EN_MASK BIT(1)
293#define MVPP2_BM_PHY_RLS_GRNTD_MASK BIT(2)
294#define MVPP2_BM_VIRT_RLS_REG 0x64c0
Thomas Petazzonid01524d2017-03-07 16:53:09 +0100295#define MVPP22_BM_ADDR_HIGH_RLS_REG 0x64c4
296#define MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK 0xff
Antoine Ténart81b66302017-08-22 19:08:21 +0200297#define MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK 0xff00
Thomas Petazzonid01524d2017-03-07 16:53:09 +0100298#define MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT 8
Marcin Wojtas3f518502014-07-10 16:52:13 -0300299
300/* TX Scheduler registers */
301#define MVPP2_TXP_SCHED_PORT_INDEX_REG 0x8000
302#define MVPP2_TXP_SCHED_Q_CMD_REG 0x8004
303#define MVPP2_TXP_SCHED_ENQ_MASK 0xff
304#define MVPP2_TXP_SCHED_DISQ_OFFSET 8
305#define MVPP2_TXP_SCHED_CMD_1_REG 0x8010
306#define MVPP2_TXP_SCHED_PERIOD_REG 0x8018
307#define MVPP2_TXP_SCHED_MTU_REG 0x801c
308#define MVPP2_TXP_MTU_MAX 0x7FFFF
309#define MVPP2_TXP_SCHED_REFILL_REG 0x8020
310#define MVPP2_TXP_REFILL_TOKENS_ALL_MASK 0x7ffff
311#define MVPP2_TXP_REFILL_PERIOD_ALL_MASK 0x3ff00000
312#define MVPP2_TXP_REFILL_PERIOD_MASK(v) ((v) << 20)
313#define MVPP2_TXP_SCHED_TOKEN_SIZE_REG 0x8024
314#define MVPP2_TXP_TOKEN_SIZE_MAX 0xffffffff
315#define MVPP2_TXQ_SCHED_REFILL_REG(q) (0x8040 + ((q) << 2))
316#define MVPP2_TXQ_REFILL_TOKENS_ALL_MASK 0x7ffff
317#define MVPP2_TXQ_REFILL_PERIOD_ALL_MASK 0x3ff00000
318#define MVPP2_TXQ_REFILL_PERIOD_MASK(v) ((v) << 20)
319#define MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(q) (0x8060 + ((q) << 2))
320#define MVPP2_TXQ_TOKEN_SIZE_MAX 0x7fffffff
321#define MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(q) (0x8080 + ((q) << 2))
322#define MVPP2_TXQ_TOKEN_CNTR_MAX 0xffffffff
323
324/* TX general registers */
325#define MVPP2_TX_SNOOP_REG 0x8800
326#define MVPP2_TX_PORT_FLUSH_REG 0x8810
327#define MVPP2_TX_PORT_FLUSH_MASK(port) (1 << (port))
328
329/* LMS registers */
330#define MVPP2_SRC_ADDR_MIDDLE 0x24
331#define MVPP2_SRC_ADDR_HIGH 0x28
Marcin Wojtas08a23752014-07-21 13:48:12 -0300332#define MVPP2_PHY_AN_CFG0_REG 0x34
333#define MVPP2_PHY_AN_STOP_SMI0_MASK BIT(7)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300334#define MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG 0x305c
Thomas Petazzoni31d76772017-02-21 11:28:10 +0100335#define MVPP2_EXT_GLOBAL_CTRL_DEFAULT 0x27
Marcin Wojtas3f518502014-07-10 16:52:13 -0300336
337/* Per-port registers */
338#define MVPP2_GMAC_CTRL_0_REG 0x0
Antoine Ténart81b66302017-08-22 19:08:21 +0200339#define MVPP2_GMAC_PORT_EN_MASK BIT(0)
Antoine Ténart39193572017-08-22 19:08:24 +0200340#define MVPP2_GMAC_PORT_TYPE_MASK BIT(1)
Antoine Ténart81b66302017-08-22 19:08:21 +0200341#define MVPP2_GMAC_MAX_RX_SIZE_OFFS 2
342#define MVPP2_GMAC_MAX_RX_SIZE_MASK 0x7ffc
343#define MVPP2_GMAC_MIB_CNTR_EN_MASK BIT(15)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300344#define MVPP2_GMAC_CTRL_1_REG 0x4
Antoine Ténart81b66302017-08-22 19:08:21 +0200345#define MVPP2_GMAC_PERIODIC_XON_EN_MASK BIT(1)
346#define MVPP2_GMAC_GMII_LB_EN_MASK BIT(5)
347#define MVPP2_GMAC_PCS_LB_EN_BIT 6
348#define MVPP2_GMAC_PCS_LB_EN_MASK BIT(6)
349#define MVPP2_GMAC_SA_LOW_OFFS 7
Marcin Wojtas3f518502014-07-10 16:52:13 -0300350#define MVPP2_GMAC_CTRL_2_REG 0x8
Antoine Ténart81b66302017-08-22 19:08:21 +0200351#define MVPP2_GMAC_INBAND_AN_MASK BIT(0)
Antoine Ténart39193572017-08-22 19:08:24 +0200352#define MVPP2_GMAC_FLOW_CTRL_MASK GENMASK(2, 1)
Antoine Ténart81b66302017-08-22 19:08:21 +0200353#define MVPP2_GMAC_PCS_ENABLE_MASK BIT(3)
Antoine Tenartc7dfc8c2017-09-25 14:59:48 +0200354#define MVPP2_GMAC_INTERNAL_CLK_MASK BIT(4)
Antoine Ténart39193572017-08-22 19:08:24 +0200355#define MVPP2_GMAC_DISABLE_PADDING BIT(5)
Antoine Ténart81b66302017-08-22 19:08:21 +0200356#define MVPP2_GMAC_PORT_RESET_MASK BIT(6)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300357#define MVPP2_GMAC_AUTONEG_CONFIG 0xc
Antoine Ténart81b66302017-08-22 19:08:21 +0200358#define MVPP2_GMAC_FORCE_LINK_DOWN BIT(0)
359#define MVPP2_GMAC_FORCE_LINK_PASS BIT(1)
Antoine Ténart39193572017-08-22 19:08:24 +0200360#define MVPP2_GMAC_IN_BAND_AUTONEG BIT(2)
361#define MVPP2_GMAC_IN_BAND_AUTONEG_BYPASS BIT(3)
Antoine Ténart81b66302017-08-22 19:08:21 +0200362#define MVPP2_GMAC_CONFIG_MII_SPEED BIT(5)
363#define MVPP2_GMAC_CONFIG_GMII_SPEED BIT(6)
364#define MVPP2_GMAC_AN_SPEED_EN BIT(7)
365#define MVPP2_GMAC_FC_ADV_EN BIT(9)
Antoine Ténart39193572017-08-22 19:08:24 +0200366#define MVPP2_GMAC_FLOW_CTRL_AUTONEG BIT(11)
Antoine Ténart81b66302017-08-22 19:08:21 +0200367#define MVPP2_GMAC_CONFIG_FULL_DUPLEX BIT(12)
368#define MVPP2_GMAC_AN_DUPLEX_EN BIT(13)
Antoine Tenartfd3651b2017-09-01 11:04:54 +0200369#define MVPP2_GMAC_STATUS0 0x10
370#define MVPP2_GMAC_STATUS0_LINK_UP BIT(0)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300371#define MVPP2_GMAC_PORT_FIFO_CFG_1_REG 0x1c
Antoine Ténart81b66302017-08-22 19:08:21 +0200372#define MVPP2_GMAC_TX_FIFO_MIN_TH_OFFS 6
373#define MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK 0x1fc0
374#define MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(v) (((v) << 6) & \
Marcin Wojtas3f518502014-07-10 16:52:13 -0300375 MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK)
Antoine Tenartfd3651b2017-09-01 11:04:54 +0200376#define MVPP22_GMAC_INT_STAT 0x20
377#define MVPP22_GMAC_INT_STAT_LINK BIT(1)
378#define MVPP22_GMAC_INT_MASK 0x24
379#define MVPP22_GMAC_INT_MASK_LINK_STAT BIT(1)
Thomas Petazzoni26975822017-03-07 16:53:14 +0100380#define MVPP22_GMAC_CTRL_4_REG 0x90
Antoine Ténart81b66302017-08-22 19:08:21 +0200381#define MVPP22_CTRL4_EXT_PIN_GMII_SEL BIT(0)
382#define MVPP22_CTRL4_DP_CLK_SEL BIT(5)
Antoine Ténart1068ec72017-08-22 19:08:22 +0200383#define MVPP22_CTRL4_SYNC_BYPASS_DIS BIT(6)
Antoine Ténart81b66302017-08-22 19:08:21 +0200384#define MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE BIT(7)
Antoine Tenartfd3651b2017-09-01 11:04:54 +0200385#define MVPP22_GMAC_INT_SUM_MASK 0xa4
386#define MVPP22_GMAC_INT_SUM_MASK_LINK_STAT BIT(1)
Thomas Petazzoni26975822017-03-07 16:53:14 +0100387
388/* Per-port XGMAC registers. PPv2.2 only, only for GOP port 0,
389 * relative to port->base.
390 */
Antoine Ténart725757a2017-06-12 16:01:39 +0200391#define MVPP22_XLG_CTRL0_REG 0x100
Antoine Ténart81b66302017-08-22 19:08:21 +0200392#define MVPP22_XLG_CTRL0_PORT_EN BIT(0)
393#define MVPP22_XLG_CTRL0_MAC_RESET_DIS BIT(1)
Antoine Ténart77321952017-08-22 19:08:25 +0200394#define MVPP22_XLG_CTRL0_RX_FLOW_CTRL_EN BIT(7)
Antoine Ténart81b66302017-08-22 19:08:21 +0200395#define MVPP22_XLG_CTRL0_MIB_CNT_DIS BIT(14)
Stefan Chulski76eb1b12017-08-22 19:08:26 +0200396#define MVPP22_XLG_CTRL1_REG 0x104
Antoine Ténartec15ecd2017-08-25 15:24:46 +0200397#define MVPP22_XLG_CTRL1_FRAMESIZELIMIT_OFFS 0
Stefan Chulski76eb1b12017-08-22 19:08:26 +0200398#define MVPP22_XLG_CTRL1_FRAMESIZELIMIT_MASK 0x1fff
Antoine Tenartfd3651b2017-09-01 11:04:54 +0200399#define MVPP22_XLG_STATUS 0x10c
400#define MVPP22_XLG_STATUS_LINK_UP BIT(0)
401#define MVPP22_XLG_INT_STAT 0x114
402#define MVPP22_XLG_INT_STAT_LINK BIT(1)
403#define MVPP22_XLG_INT_MASK 0x118
404#define MVPP22_XLG_INT_MASK_LINK BIT(1)
Thomas Petazzoni26975822017-03-07 16:53:14 +0100405#define MVPP22_XLG_CTRL3_REG 0x11c
Antoine Ténart81b66302017-08-22 19:08:21 +0200406#define MVPP22_XLG_CTRL3_MACMODESELECT_MASK (7 << 13)
407#define MVPP22_XLG_CTRL3_MACMODESELECT_GMAC (0 << 13)
408#define MVPP22_XLG_CTRL3_MACMODESELECT_10G (1 << 13)
Antoine Tenartfd3651b2017-09-01 11:04:54 +0200409#define MVPP22_XLG_EXT_INT_MASK 0x15c
410#define MVPP22_XLG_EXT_INT_MASK_XLG BIT(1)
411#define MVPP22_XLG_EXT_INT_MASK_GIG BIT(2)
Antoine Ténart77321952017-08-22 19:08:25 +0200412#define MVPP22_XLG_CTRL4_REG 0x184
413#define MVPP22_XLG_CTRL4_FWD_FC BIT(5)
414#define MVPP22_XLG_CTRL4_FWD_PFC BIT(6)
415#define MVPP22_XLG_CTRL4_MACMODSELECT_GMAC BIT(12)
416
Thomas Petazzoni26975822017-03-07 16:53:14 +0100417/* SMI registers. PPv2.2 only, relative to priv->iface_base. */
418#define MVPP22_SMI_MISC_CFG_REG 0x1204
Antoine Ténart81b66302017-08-22 19:08:21 +0200419#define MVPP22_SMI_POLLING_EN BIT(10)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300420
Thomas Petazzonia7868412017-03-07 16:53:13 +0100421#define MVPP22_GMAC_BASE(port) (0x7000 + (port) * 0x1000 + 0xe00)
422
Marcin Wojtas3f518502014-07-10 16:52:13 -0300423#define MVPP2_CAUSE_TXQ_SENT_DESC_ALL_MASK 0xff
424
425/* Descriptor ring Macros */
426#define MVPP2_QUEUE_NEXT_DESC(q, index) \
427 (((index) < (q)->last_desc) ? ((index) + 1) : 0)
428
Antoine Ténartf84bf382017-08-22 19:08:27 +0200429/* XPCS registers. PPv2.2 only */
430#define MVPP22_MPCS_BASE(port) (0x7000 + (port) * 0x1000)
431#define MVPP22_MPCS_CTRL 0x14
432#define MVPP22_MPCS_CTRL_FWD_ERR_CONN BIT(10)
433#define MVPP22_MPCS_CLK_RESET 0x14c
434#define MAC_CLK_RESET_SD_TX BIT(0)
435#define MAC_CLK_RESET_SD_RX BIT(1)
436#define MAC_CLK_RESET_MAC BIT(2)
437#define MVPP22_MPCS_CLK_RESET_DIV_RATIO(n) ((n) << 4)
438#define MVPP22_MPCS_CLK_RESET_DIV_SET BIT(11)
439
440/* XPCS registers. PPv2.2 only */
441#define MVPP22_XPCS_BASE(port) (0x7400 + (port) * 0x1000)
442#define MVPP22_XPCS_CFG0 0x0
443#define MVPP22_XPCS_CFG0_PCS_MODE(n) ((n) << 3)
444#define MVPP22_XPCS_CFG0_ACTIVE_LANE(n) ((n) << 5)
445
446/* System controller registers. Accessed through a regmap. */
447#define GENCONF_SOFT_RESET1 0x1108
448#define GENCONF_SOFT_RESET1_GOP BIT(6)
449#define GENCONF_PORT_CTRL0 0x1110
450#define GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT BIT(1)
451#define GENCONF_PORT_CTRL0_RX_DATA_SAMPLE BIT(29)
452#define GENCONF_PORT_CTRL0_CLK_DIV_PHASE_CLR BIT(31)
453#define GENCONF_PORT_CTRL1 0x1114
454#define GENCONF_PORT_CTRL1_EN(p) BIT(p)
455#define GENCONF_PORT_CTRL1_RESET(p) (BIT(p) << 28)
456#define GENCONF_CTRL0 0x1120
457#define GENCONF_CTRL0_PORT0_RGMII BIT(0)
458#define GENCONF_CTRL0_PORT1_RGMII_MII BIT(1)
459#define GENCONF_CTRL0_PORT1_RGMII BIT(2)
460
Marcin Wojtas3f518502014-07-10 16:52:13 -0300461/* Various constants */
462
463/* Coalescing */
Antoine Tenart86162282017-12-11 09:13:29 +0100464#define MVPP2_TXDONE_COAL_PKTS_THRESH 64
Marcin Wojtasedc660f2015-08-06 19:00:30 +0200465#define MVPP2_TXDONE_HRTIMER_PERIOD_NS 1000000UL
Thomas Petazzoni213f4282017-08-03 10:42:00 +0200466#define MVPP2_TXDONE_COAL_USEC 1000
Marcin Wojtas3f518502014-07-10 16:52:13 -0300467#define MVPP2_RX_COAL_PKTS 32
Antoine Tenart86162282017-12-11 09:13:29 +0100468#define MVPP2_RX_COAL_USEC 64
Marcin Wojtas3f518502014-07-10 16:52:13 -0300469
470/* The two bytes Marvell header. Either contains a special value used
471 * by Marvell switches when a specific hardware mode is enabled (not
472 * supported by this driver) or is filled automatically by zeroes on
473 * the RX side. Those two bytes being at the front of the Ethernet
474 * header, they allow to have the IP header aligned on a 4 bytes
475 * boundary automatically: the hardware skips those two bytes on its
476 * own.
477 */
478#define MVPP2_MH_SIZE 2
479#define MVPP2_ETH_TYPE_LEN 2
480#define MVPP2_PPPOE_HDR_SIZE 8
481#define MVPP2_VLAN_TAG_LEN 4
Maxime Chevallier56beda32018-02-28 10:14:13 +0100482#define MVPP2_VLAN_TAG_EDSA_LEN 8
Marcin Wojtas3f518502014-07-10 16:52:13 -0300483
484/* Lbtd 802.3 type */
485#define MVPP2_IP_LBDT_TYPE 0xfffa
486
Marcin Wojtas3f518502014-07-10 16:52:13 -0300487#define MVPP2_TX_CSUM_MAX_SIZE 9800
488
489/* Timeout constants */
490#define MVPP2_TX_DISABLE_TIMEOUT_MSEC 1000
491#define MVPP2_TX_PENDING_TIMEOUT_MSEC 1000
492
493#define MVPP2_TX_MTU_MAX 0x7ffff
494
495/* Maximum number of T-CONTs of PON port */
496#define MVPP2_MAX_TCONT 16
497
498/* Maximum number of supported ports */
499#define MVPP2_MAX_PORTS 4
500
501/* Maximum number of TXQs used by single port */
502#define MVPP2_MAX_TXQ 8
503
Antoine Tenart1d17db02017-10-30 11:23:31 +0100504/* MVPP2_MAX_TSO_SEGS is the maximum number of fragments to allow in the GSO
505 * skb. As we need a maxium of two descriptors per fragments (1 header, 1 data),
506 * multiply this value by two to count the maximum number of skb descs needed.
507 */
508#define MVPP2_MAX_TSO_SEGS 300
509#define MVPP2_MAX_SKB_DESCS (MVPP2_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
510
Marcin Wojtas3f518502014-07-10 16:52:13 -0300511/* Dfault number of RXQs in use */
512#define MVPP2_DEFAULT_RXQ 4
513
Marcin Wojtas3f518502014-07-10 16:52:13 -0300514/* Max number of Rx descriptors */
Yan Markman7cf87e42017-12-11 09:13:26 +0100515#define MVPP2_MAX_RXD_MAX 1024
516#define MVPP2_MAX_RXD_DFLT 128
Marcin Wojtas3f518502014-07-10 16:52:13 -0300517
518/* Max number of Tx descriptors */
Yan Markman7cf87e42017-12-11 09:13:26 +0100519#define MVPP2_MAX_TXD_MAX 2048
520#define MVPP2_MAX_TXD_DFLT 1024
Marcin Wojtas3f518502014-07-10 16:52:13 -0300521
522/* Amount of Tx descriptors that can be reserved at once by CPU */
523#define MVPP2_CPU_DESC_CHUNK 64
524
525/* Max number of Tx descriptors in each aggregated queue */
526#define MVPP2_AGGR_TXQ_SIZE 256
527
528/* Descriptor aligned size */
529#define MVPP2_DESC_ALIGNED_SIZE 32
530
531/* Descriptor alignment mask */
532#define MVPP2_TX_DESC_ALIGN (MVPP2_DESC_ALIGNED_SIZE - 1)
533
534/* RX FIFO constants */
Antoine Tenart2d1d7df2017-10-30 11:23:28 +0100535#define MVPP2_RX_FIFO_PORT_DATA_SIZE_32KB 0x8000
536#define MVPP2_RX_FIFO_PORT_DATA_SIZE_8KB 0x2000
537#define MVPP2_RX_FIFO_PORT_DATA_SIZE_4KB 0x1000
538#define MVPP2_RX_FIFO_PORT_ATTR_SIZE_32KB 0x200
539#define MVPP2_RX_FIFO_PORT_ATTR_SIZE_8KB 0x80
540#define MVPP2_RX_FIFO_PORT_ATTR_SIZE_4KB 0x40
541#define MVPP2_RX_FIFO_PORT_MIN_PKT 0x80
Marcin Wojtas3f518502014-07-10 16:52:13 -0300542
Antoine Tenart7c10f972017-10-30 11:23:29 +0100543/* TX FIFO constants */
544#define MVPP22_TX_FIFO_DATA_SIZE_10KB 0xa
545#define MVPP22_TX_FIFO_DATA_SIZE_3KB 0x3
Yan Markman93ff1302018-03-05 15:16:52 +0100546#define MVPP2_TX_FIFO_THRESHOLD_MIN 256
547#define MVPP2_TX_FIFO_THRESHOLD_10KB \
548 (MVPP22_TX_FIFO_DATA_SIZE_10KB * 1024 - MVPP2_TX_FIFO_THRESHOLD_MIN)
549#define MVPP2_TX_FIFO_THRESHOLD_3KB \
550 (MVPP22_TX_FIFO_DATA_SIZE_3KB * 1024 - MVPP2_TX_FIFO_THRESHOLD_MIN)
Antoine Tenart7c10f972017-10-30 11:23:29 +0100551
Marcin Wojtas3f518502014-07-10 16:52:13 -0300552/* RX buffer constants */
553#define MVPP2_SKB_SHINFO_SIZE \
554 SKB_DATA_ALIGN(sizeof(struct skb_shared_info))
555
556#define MVPP2_RX_PKT_SIZE(mtu) \
557 ALIGN((mtu) + MVPP2_MH_SIZE + MVPP2_VLAN_TAG_LEN + \
Jisheng Zhang4a0a12d2016-04-01 17:11:05 +0800558 ETH_HLEN + ETH_FCS_LEN, cache_line_size())
Marcin Wojtas3f518502014-07-10 16:52:13 -0300559
560#define MVPP2_RX_BUF_SIZE(pkt_size) ((pkt_size) + NET_SKB_PAD)
561#define MVPP2_RX_TOTAL_SIZE(buf_size) ((buf_size) + MVPP2_SKB_SHINFO_SIZE)
562#define MVPP2_RX_MAX_PKT_SIZE(total_size) \
563 ((total_size) - NET_SKB_PAD - MVPP2_SKB_SHINFO_SIZE)
564
565#define MVPP2_BIT_TO_BYTE(bit) ((bit) / 8)
566
567/* IPv6 max L3 address size */
568#define MVPP2_MAX_L3_ADDR_SIZE 16
569
570/* Port flags */
571#define MVPP2_F_LOOPBACK BIT(0)
572
573/* Marvell tag types */
574enum mvpp2_tag_type {
575 MVPP2_TAG_TYPE_NONE = 0,
576 MVPP2_TAG_TYPE_MH = 1,
577 MVPP2_TAG_TYPE_DSA = 2,
578 MVPP2_TAG_TYPE_EDSA = 3,
579 MVPP2_TAG_TYPE_VLAN = 4,
580 MVPP2_TAG_TYPE_LAST = 5
581};
582
583/* Parser constants */
584#define MVPP2_PRS_TCAM_SRAM_SIZE 256
585#define MVPP2_PRS_TCAM_WORDS 6
586#define MVPP2_PRS_SRAM_WORDS 4
587#define MVPP2_PRS_FLOW_ID_SIZE 64
588#define MVPP2_PRS_FLOW_ID_MASK 0x3f
589#define MVPP2_PRS_TCAM_ENTRY_INVALID 1
590#define MVPP2_PRS_TCAM_DSA_TAGGED_BIT BIT(5)
591#define MVPP2_PRS_IPV4_HEAD 0x40
592#define MVPP2_PRS_IPV4_HEAD_MASK 0xf0
593#define MVPP2_PRS_IPV4_MC 0xe0
594#define MVPP2_PRS_IPV4_MC_MASK 0xf0
595#define MVPP2_PRS_IPV4_BC_MASK 0xff
596#define MVPP2_PRS_IPV4_IHL 0x5
597#define MVPP2_PRS_IPV4_IHL_MASK 0xf
598#define MVPP2_PRS_IPV6_MC 0xff
599#define MVPP2_PRS_IPV6_MC_MASK 0xff
600#define MVPP2_PRS_IPV6_HOP_MASK 0xff
601#define MVPP2_PRS_TCAM_PROTO_MASK 0xff
602#define MVPP2_PRS_TCAM_PROTO_MASK_L 0x3f
603#define MVPP2_PRS_DBL_VLANS_MAX 100
Maxime Chevallier10fea262018-03-07 15:18:04 +0100604#define MVPP2_PRS_CAST_MASK BIT(0)
605#define MVPP2_PRS_MCAST_VAL BIT(0)
606#define MVPP2_PRS_UCAST_VAL 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300607
608/* Tcam structure:
609 * - lookup ID - 4 bits
610 * - port ID - 1 byte
611 * - additional information - 1 byte
612 * - header data - 8 bytes
613 * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(5)->(0).
614 */
615#define MVPP2_PRS_AI_BITS 8
616#define MVPP2_PRS_PORT_MASK 0xff
617#define MVPP2_PRS_LU_MASK 0xf
618#define MVPP2_PRS_TCAM_DATA_BYTE(offs) \
619 (((offs) - ((offs) % 2)) * 2 + ((offs) % 2))
620#define MVPP2_PRS_TCAM_DATA_BYTE_EN(offs) \
621 (((offs) * 2) - ((offs) % 2) + 2)
622#define MVPP2_PRS_TCAM_AI_BYTE 16
623#define MVPP2_PRS_TCAM_PORT_BYTE 17
624#define MVPP2_PRS_TCAM_LU_BYTE 20
625#define MVPP2_PRS_TCAM_EN_OFFS(offs) ((offs) + 2)
626#define MVPP2_PRS_TCAM_INV_WORD 5
Maxime Chevallier56beda32018-02-28 10:14:13 +0100627
628#define MVPP2_PRS_VID_TCAM_BYTE 2
629
Maxime Chevallier10fea262018-03-07 15:18:04 +0100630/* TCAM range for unicast and multicast filtering. We have 25 entries per port,
631 * with 4 dedicated to UC filtering and the rest to multicast filtering.
632 * Additionnally we reserve one entry for the broadcast address, and one for
633 * each port's own address.
634 */
635#define MVPP2_PRS_MAC_UC_MC_FILT_MAX 25
636#define MVPP2_PRS_MAC_RANGE_SIZE 80
637
638/* Number of entries per port dedicated to UC and MC filtering */
639#define MVPP2_PRS_MAC_UC_FILT_MAX 4
640#define MVPP2_PRS_MAC_MC_FILT_MAX (MVPP2_PRS_MAC_UC_MC_FILT_MAX - \
641 MVPP2_PRS_MAC_UC_FILT_MAX)
642
Maxime Chevallier56beda32018-02-28 10:14:13 +0100643/* There is a TCAM range reserved for VLAN filtering entries, range size is 33
644 * 10 VLAN ID filter entries per port
645 * 1 default VLAN filter entry per port
646 * It is assumed that there are 3 ports for filter, not including loopback port
647 */
648#define MVPP2_PRS_VLAN_FILT_MAX 11
649#define MVPP2_PRS_VLAN_FILT_RANGE_SIZE 33
650
651#define MVPP2_PRS_VLAN_FILT_MAX_ENTRY (MVPP2_PRS_VLAN_FILT_MAX - 2)
652#define MVPP2_PRS_VLAN_FILT_DFLT_ENTRY (MVPP2_PRS_VLAN_FILT_MAX - 1)
653
Marcin Wojtas3f518502014-07-10 16:52:13 -0300654/* Tcam entries ID */
655#define MVPP2_PE_DROP_ALL 0
656#define MVPP2_PE_FIRST_FREE_TID 1
Maxime Chevallier56beda32018-02-28 10:14:13 +0100657
Maxime Chevallier10fea262018-03-07 15:18:04 +0100658/* MAC filtering range */
659#define MVPP2_PE_MAC_RANGE_END (MVPP2_PE_VID_FILT_RANGE_START - 1)
660#define MVPP2_PE_MAC_RANGE_START (MVPP2_PE_MAC_RANGE_END - \
661 MVPP2_PRS_MAC_RANGE_SIZE + 1)
Maxime Chevallier56beda32018-02-28 10:14:13 +0100662/* VLAN filtering range */
663#define MVPP2_PE_VID_FILT_RANGE_END (MVPP2_PRS_TCAM_SRAM_SIZE - 31)
664#define MVPP2_PE_VID_FILT_RANGE_START (MVPP2_PE_VID_FILT_RANGE_END - \
665 MVPP2_PRS_VLAN_FILT_RANGE_SIZE + 1)
666#define MVPP2_PE_LAST_FREE_TID (MVPP2_PE_VID_FILT_RANGE_START - 1)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300667#define MVPP2_PE_IP6_EXT_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 30)
Maxime Chevallier10fea262018-03-07 15:18:04 +0100668#define MVPP2_PE_IP6_ADDR_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 29)
669#define MVPP2_PE_IP4_ADDR_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 28)
670#define MVPP2_PE_LAST_DEFAULT_FLOW (MVPP2_PRS_TCAM_SRAM_SIZE - 27)
671#define MVPP2_PE_FIRST_DEFAULT_FLOW (MVPP2_PRS_TCAM_SRAM_SIZE - 22)
672#define MVPP2_PE_EDSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 21)
673#define MVPP2_PE_EDSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 20)
674#define MVPP2_PE_DSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 19)
675#define MVPP2_PE_DSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 18)
676#define MVPP2_PE_ETYPE_EDSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 17)
677#define MVPP2_PE_ETYPE_EDSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 16)
678#define MVPP2_PE_ETYPE_DSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 15)
679#define MVPP2_PE_ETYPE_DSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 14)
680#define MVPP2_PE_MH_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 13)
681#define MVPP2_PE_DSA_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 12)
682#define MVPP2_PE_IP6_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 11)
683#define MVPP2_PE_IP4_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 10)
684#define MVPP2_PE_ETH_TYPE_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 9)
685#define MVPP2_PE_VID_FLTR_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 8)
686#define MVPP2_PE_VID_EDSA_FLTR_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 7)
687#define MVPP2_PE_VLAN_DBL (MVPP2_PRS_TCAM_SRAM_SIZE - 6)
688#define MVPP2_PE_VLAN_NONE (MVPP2_PRS_TCAM_SRAM_SIZE - 5)
689/* reserved */
690#define MVPP2_PE_MAC_MC_PROMISCUOUS (MVPP2_PRS_TCAM_SRAM_SIZE - 3)
691#define MVPP2_PE_MAC_UC_PROMISCUOUS (MVPP2_PRS_TCAM_SRAM_SIZE - 2)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300692#define MVPP2_PE_MAC_NON_PROMISCUOUS (MVPP2_PRS_TCAM_SRAM_SIZE - 1)
693
Maxime Chevallier56beda32018-02-28 10:14:13 +0100694#define MVPP2_PRS_VID_PORT_FIRST(port) (MVPP2_PE_VID_FILT_RANGE_START + \
695 ((port) * MVPP2_PRS_VLAN_FILT_MAX))
696#define MVPP2_PRS_VID_PORT_LAST(port) (MVPP2_PRS_VID_PORT_FIRST(port) \
697 + MVPP2_PRS_VLAN_FILT_MAX_ENTRY)
698/* Index of default vid filter for given port */
699#define MVPP2_PRS_VID_PORT_DFLT(port) (MVPP2_PRS_VID_PORT_FIRST(port) \
700 + MVPP2_PRS_VLAN_FILT_DFLT_ENTRY)
701
Marcin Wojtas3f518502014-07-10 16:52:13 -0300702/* Sram structure
703 * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(3)->(0).
704 */
705#define MVPP2_PRS_SRAM_RI_OFFS 0
706#define MVPP2_PRS_SRAM_RI_WORD 0
707#define MVPP2_PRS_SRAM_RI_CTRL_OFFS 32
708#define MVPP2_PRS_SRAM_RI_CTRL_WORD 1
709#define MVPP2_PRS_SRAM_RI_CTRL_BITS 32
710#define MVPP2_PRS_SRAM_SHIFT_OFFS 64
711#define MVPP2_PRS_SRAM_SHIFT_SIGN_BIT 72
712#define MVPP2_PRS_SRAM_UDF_OFFS 73
713#define MVPP2_PRS_SRAM_UDF_BITS 8
714#define MVPP2_PRS_SRAM_UDF_MASK 0xff
715#define MVPP2_PRS_SRAM_UDF_SIGN_BIT 81
716#define MVPP2_PRS_SRAM_UDF_TYPE_OFFS 82
717#define MVPP2_PRS_SRAM_UDF_TYPE_MASK 0x7
718#define MVPP2_PRS_SRAM_UDF_TYPE_L3 1
719#define MVPP2_PRS_SRAM_UDF_TYPE_L4 4
720#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS 85
721#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK 0x3
722#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD 1
723#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP4_ADD 2
724#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP6_ADD 3
725#define MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS 87
726#define MVPP2_PRS_SRAM_OP_SEL_UDF_BITS 2
727#define MVPP2_PRS_SRAM_OP_SEL_UDF_MASK 0x3
728#define MVPP2_PRS_SRAM_OP_SEL_UDF_ADD 0
729#define MVPP2_PRS_SRAM_OP_SEL_UDF_IP4_ADD 2
730#define MVPP2_PRS_SRAM_OP_SEL_UDF_IP6_ADD 3
731#define MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS 89
732#define MVPP2_PRS_SRAM_AI_OFFS 90
733#define MVPP2_PRS_SRAM_AI_CTRL_OFFS 98
734#define MVPP2_PRS_SRAM_AI_CTRL_BITS 8
735#define MVPP2_PRS_SRAM_AI_MASK 0xff
736#define MVPP2_PRS_SRAM_NEXT_LU_OFFS 106
737#define MVPP2_PRS_SRAM_NEXT_LU_MASK 0xf
738#define MVPP2_PRS_SRAM_LU_DONE_BIT 110
739#define MVPP2_PRS_SRAM_LU_GEN_BIT 111
740
741/* Sram result info bits assignment */
742#define MVPP2_PRS_RI_MAC_ME_MASK 0x1
743#define MVPP2_PRS_RI_DSA_MASK 0x2
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100744#define MVPP2_PRS_RI_VLAN_MASK (BIT(2) | BIT(3))
745#define MVPP2_PRS_RI_VLAN_NONE 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300746#define MVPP2_PRS_RI_VLAN_SINGLE BIT(2)
747#define MVPP2_PRS_RI_VLAN_DOUBLE BIT(3)
748#define MVPP2_PRS_RI_VLAN_TRIPLE (BIT(2) | BIT(3))
749#define MVPP2_PRS_RI_CPU_CODE_MASK 0x70
750#define MVPP2_PRS_RI_CPU_CODE_RX_SPEC BIT(4)
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100751#define MVPP2_PRS_RI_L2_CAST_MASK (BIT(9) | BIT(10))
752#define MVPP2_PRS_RI_L2_UCAST 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300753#define MVPP2_PRS_RI_L2_MCAST BIT(9)
754#define MVPP2_PRS_RI_L2_BCAST BIT(10)
755#define MVPP2_PRS_RI_PPPOE_MASK 0x800
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100756#define MVPP2_PRS_RI_L3_PROTO_MASK (BIT(12) | BIT(13) | BIT(14))
757#define MVPP2_PRS_RI_L3_UN 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300758#define MVPP2_PRS_RI_L3_IP4 BIT(12)
759#define MVPP2_PRS_RI_L3_IP4_OPT BIT(13)
760#define MVPP2_PRS_RI_L3_IP4_OTHER (BIT(12) | BIT(13))
761#define MVPP2_PRS_RI_L3_IP6 BIT(14)
762#define MVPP2_PRS_RI_L3_IP6_EXT (BIT(12) | BIT(14))
763#define MVPP2_PRS_RI_L3_ARP (BIT(13) | BIT(14))
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100764#define MVPP2_PRS_RI_L3_ADDR_MASK (BIT(15) | BIT(16))
765#define MVPP2_PRS_RI_L3_UCAST 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300766#define MVPP2_PRS_RI_L3_MCAST BIT(15)
767#define MVPP2_PRS_RI_L3_BCAST (BIT(15) | BIT(16))
768#define MVPP2_PRS_RI_IP_FRAG_MASK 0x20000
Stefan Chulskiaff3da32017-09-25 14:59:46 +0200769#define MVPP2_PRS_RI_IP_FRAG_TRUE BIT(17)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300770#define MVPP2_PRS_RI_UDF3_MASK 0x300000
771#define MVPP2_PRS_RI_UDF3_RX_SPECIAL BIT(21)
772#define MVPP2_PRS_RI_L4_PROTO_MASK 0x1c00000
773#define MVPP2_PRS_RI_L4_TCP BIT(22)
774#define MVPP2_PRS_RI_L4_UDP BIT(23)
775#define MVPP2_PRS_RI_L4_OTHER (BIT(22) | BIT(23))
776#define MVPP2_PRS_RI_UDF7_MASK 0x60000000
777#define MVPP2_PRS_RI_UDF7_IP6_LITE BIT(29)
778#define MVPP2_PRS_RI_DROP_MASK 0x80000000
779
780/* Sram additional info bits assignment */
781#define MVPP2_PRS_IPV4_DIP_AI_BIT BIT(0)
782#define MVPP2_PRS_IPV6_NO_EXT_AI_BIT BIT(0)
783#define MVPP2_PRS_IPV6_EXT_AI_BIT BIT(1)
784#define MVPP2_PRS_IPV6_EXT_AH_AI_BIT BIT(2)
785#define MVPP2_PRS_IPV6_EXT_AH_LEN_AI_BIT BIT(3)
786#define MVPP2_PRS_IPV6_EXT_AH_L4_AI_BIT BIT(4)
787#define MVPP2_PRS_SINGLE_VLAN_AI 0
788#define MVPP2_PRS_DBL_VLAN_AI_BIT BIT(7)
Maxime Chevallier56beda32018-02-28 10:14:13 +0100789#define MVPP2_PRS_EDSA_VID_AI_BIT BIT(0)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300790
791/* DSA/EDSA type */
792#define MVPP2_PRS_TAGGED true
793#define MVPP2_PRS_UNTAGGED false
794#define MVPP2_PRS_EDSA true
795#define MVPP2_PRS_DSA false
796
797/* MAC entries, shadow udf */
798enum mvpp2_prs_udf {
799 MVPP2_PRS_UDF_MAC_DEF,
800 MVPP2_PRS_UDF_MAC_RANGE,
801 MVPP2_PRS_UDF_L2_DEF,
802 MVPP2_PRS_UDF_L2_DEF_COPY,
803 MVPP2_PRS_UDF_L2_USER,
804};
805
806/* Lookup ID */
807enum mvpp2_prs_lookup {
808 MVPP2_PRS_LU_MH,
809 MVPP2_PRS_LU_MAC,
810 MVPP2_PRS_LU_DSA,
811 MVPP2_PRS_LU_VLAN,
Maxime Chevallier56beda32018-02-28 10:14:13 +0100812 MVPP2_PRS_LU_VID,
Marcin Wojtas3f518502014-07-10 16:52:13 -0300813 MVPP2_PRS_LU_L2,
814 MVPP2_PRS_LU_PPPOE,
815 MVPP2_PRS_LU_IP4,
816 MVPP2_PRS_LU_IP6,
817 MVPP2_PRS_LU_FLOWS,
818 MVPP2_PRS_LU_LAST,
819};
820
Maxime Chevallier10fea262018-03-07 15:18:04 +0100821/* L2 cast enum */
822enum mvpp2_prs_l2_cast {
823 MVPP2_PRS_L2_UNI_CAST,
824 MVPP2_PRS_L2_MULTI_CAST,
825};
826
Marcin Wojtas3f518502014-07-10 16:52:13 -0300827/* L3 cast enum */
828enum mvpp2_prs_l3_cast {
829 MVPP2_PRS_L3_UNI_CAST,
830 MVPP2_PRS_L3_MULTI_CAST,
831 MVPP2_PRS_L3_BROAD_CAST
832};
833
834/* Classifier constants */
835#define MVPP2_CLS_FLOWS_TBL_SIZE 512
836#define MVPP2_CLS_FLOWS_TBL_DATA_WORDS 3
837#define MVPP2_CLS_LKP_TBL_SIZE 64
Antoine Tenart1d7d15d2017-10-30 11:23:30 +0100838#define MVPP2_CLS_RX_QUEUES 256
839
840/* RSS constants */
841#define MVPP22_RSS_TABLE_ENTRIES 32
Marcin Wojtas3f518502014-07-10 16:52:13 -0300842
843/* BM constants */
Stefan Chulski576193f2018-03-05 15:16:54 +0100844#define MVPP2_BM_JUMBO_BUF_NUM 512
Marcin Wojtas3f518502014-07-10 16:52:13 -0300845#define MVPP2_BM_LONG_BUF_NUM 1024
846#define MVPP2_BM_SHORT_BUF_NUM 2048
847#define MVPP2_BM_POOL_SIZE_MAX (16*1024 - MVPP2_BM_POOL_PTR_ALIGN/4)
848#define MVPP2_BM_POOL_PTR_ALIGN 128
Marcin Wojtas3f518502014-07-10 16:52:13 -0300849
850/* BM cookie (32 bits) definition */
851#define MVPP2_BM_COOKIE_POOL_OFFS 8
852#define MVPP2_BM_COOKIE_CPU_OFFS 24
853
Stefan Chulski01d04932018-03-05 15:16:50 +0100854#define MVPP2_BM_SHORT_FRAME_SIZE 512
855#define MVPP2_BM_LONG_FRAME_SIZE 2048
Stefan Chulski576193f2018-03-05 15:16:54 +0100856#define MVPP2_BM_JUMBO_FRAME_SIZE 10240
Marcin Wojtas3f518502014-07-10 16:52:13 -0300857/* BM short pool packet size
858 * These value assure that for SWF the total number
859 * of bytes allocated for each buffer will be 512
860 */
Stefan Chulski01d04932018-03-05 15:16:50 +0100861#define MVPP2_BM_SHORT_PKT_SIZE MVPP2_RX_MAX_PKT_SIZE(MVPP2_BM_SHORT_FRAME_SIZE)
862#define MVPP2_BM_LONG_PKT_SIZE MVPP2_RX_MAX_PKT_SIZE(MVPP2_BM_LONG_FRAME_SIZE)
Stefan Chulski576193f2018-03-05 15:16:54 +0100863#define MVPP2_BM_JUMBO_PKT_SIZE MVPP2_RX_MAX_PKT_SIZE(MVPP2_BM_JUMBO_FRAME_SIZE)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300864
Thomas Petazzonia7868412017-03-07 16:53:13 +0100865#define MVPP21_ADDR_SPACE_SZ 0
866#define MVPP22_ADDR_SPACE_SZ SZ_64K
867
Thomas Petazzonidf089aa2017-08-03 10:41:58 +0200868#define MVPP2_MAX_THREADS 8
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +0200869#define MVPP2_MAX_QVECS MVPP2_MAX_THREADS
Thomas Petazzonia7868412017-03-07 16:53:13 +0100870
Stefan Chulski01d04932018-03-05 15:16:50 +0100871enum mvpp2_bm_pool_log_num {
872 MVPP2_BM_SHORT,
873 MVPP2_BM_LONG,
Stefan Chulski576193f2018-03-05 15:16:54 +0100874 MVPP2_BM_JUMBO,
Stefan Chulski01d04932018-03-05 15:16:50 +0100875 MVPP2_BM_POOLS_NUM
Marcin Wojtas3f518502014-07-10 16:52:13 -0300876};
877
Stefan Chulski01d04932018-03-05 15:16:50 +0100878static struct {
879 int pkt_size;
880 int buf_num;
881} mvpp2_pools[MVPP2_BM_POOLS_NUM];
882
Miquel Raynal118d6292017-11-06 22:56:53 +0100883/* GMAC MIB Counters register definitions */
884#define MVPP21_MIB_COUNTERS_OFFSET 0x1000
885#define MVPP21_MIB_COUNTERS_PORT_SZ 0x400
886#define MVPP22_MIB_COUNTERS_OFFSET 0x0
887#define MVPP22_MIB_COUNTERS_PORT_SZ 0x100
888
889#define MVPP2_MIB_GOOD_OCTETS_RCVD 0x0
890#define MVPP2_MIB_BAD_OCTETS_RCVD 0x8
891#define MVPP2_MIB_CRC_ERRORS_SENT 0xc
892#define MVPP2_MIB_UNICAST_FRAMES_RCVD 0x10
893#define MVPP2_MIB_BROADCAST_FRAMES_RCVD 0x18
894#define MVPP2_MIB_MULTICAST_FRAMES_RCVD 0x1c
895#define MVPP2_MIB_FRAMES_64_OCTETS 0x20
896#define MVPP2_MIB_FRAMES_65_TO_127_OCTETS 0x24
897#define MVPP2_MIB_FRAMES_128_TO_255_OCTETS 0x28
898#define MVPP2_MIB_FRAMES_256_TO_511_OCTETS 0x2c
899#define MVPP2_MIB_FRAMES_512_TO_1023_OCTETS 0x30
900#define MVPP2_MIB_FRAMES_1024_TO_MAX_OCTETS 0x34
901#define MVPP2_MIB_GOOD_OCTETS_SENT 0x38
902#define MVPP2_MIB_UNICAST_FRAMES_SENT 0x40
903#define MVPP2_MIB_MULTICAST_FRAMES_SENT 0x48
904#define MVPP2_MIB_BROADCAST_FRAMES_SENT 0x4c
905#define MVPP2_MIB_FC_SENT 0x54
906#define MVPP2_MIB_FC_RCVD 0x58
907#define MVPP2_MIB_RX_FIFO_OVERRUN 0x5c
908#define MVPP2_MIB_UNDERSIZE_RCVD 0x60
909#define MVPP2_MIB_FRAGMENTS_RCVD 0x64
910#define MVPP2_MIB_OVERSIZE_RCVD 0x68
911#define MVPP2_MIB_JABBER_RCVD 0x6c
912#define MVPP2_MIB_MAC_RCV_ERROR 0x70
913#define MVPP2_MIB_BAD_CRC_EVENT 0x74
914#define MVPP2_MIB_COLLISION 0x78
915#define MVPP2_MIB_LATE_COLLISION 0x7c
916
917#define MVPP2_MIB_COUNTERS_STATS_DELAY (1 * HZ)
918
Marcin Wojtas3f518502014-07-10 16:52:13 -0300919/* Definitions */
920
921/* Shared Packet Processor resources */
922struct mvpp2 {
923 /* Shared registers' base addresses */
Marcin Wojtas3f518502014-07-10 16:52:13 -0300924 void __iomem *lms_base;
Thomas Petazzonia7868412017-03-07 16:53:13 +0100925 void __iomem *iface_base;
926
Thomas Petazzonidf089aa2017-08-03 10:41:58 +0200927 /* On PPv2.2, each "software thread" can access the base
928 * register through a separate address space, each 64 KB apart
929 * from each other. Typically, such address spaces will be
930 * used per CPU.
Thomas Petazzonia7868412017-03-07 16:53:13 +0100931 */
Thomas Petazzonidf089aa2017-08-03 10:41:58 +0200932 void __iomem *swth_base[MVPP2_MAX_THREADS];
Marcin Wojtas3f518502014-07-10 16:52:13 -0300933
Antoine Ténartf84bf382017-08-22 19:08:27 +0200934 /* On PPv2.2, some port control registers are located into the system
935 * controller space. These registers are accessible through a regmap.
936 */
937 struct regmap *sysctrl_base;
938
Marcin Wojtas3f518502014-07-10 16:52:13 -0300939 /* Common clocks */
940 struct clk *pp_clk;
941 struct clk *gop_clk;
Thomas Petazzonifceb55d2017-03-07 16:53:18 +0100942 struct clk *mg_clk;
Gregory CLEMENT4792ea02017-09-29 14:27:39 +0200943 struct clk *axi_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300944
945 /* List of pointers to port structures */
Miquel Raynal118d6292017-11-06 22:56:53 +0100946 int port_count;
Marcin Wojtasbf147152018-01-18 13:31:42 +0100947 struct mvpp2_port *port_list[MVPP2_MAX_PORTS];
Marcin Wojtas3f518502014-07-10 16:52:13 -0300948
949 /* Aggregated TXQs */
950 struct mvpp2_tx_queue *aggr_txqs;
951
952 /* BM pools */
953 struct mvpp2_bm_pool *bm_pools;
954
955 /* PRS shadow table */
956 struct mvpp2_prs_shadow *prs_shadow;
957 /* PRS auxiliary table for double vlan entries control */
958 bool *prs_double_vlans;
959
960 /* Tclk value */
961 u32 tclk;
Thomas Petazzonifaca9242017-03-07 16:53:06 +0100962
963 /* HW version */
964 enum { MVPP21, MVPP22 } hw_version;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +0100965
966 /* Maximum number of RXQs per port */
967 unsigned int max_port_rxqs;
Miquel Raynal118d6292017-11-06 22:56:53 +0100968
Miquel Raynale5c500e2017-11-08 08:59:40 +0100969 /* Workqueue to gather hardware statistics */
Miquel Raynal118d6292017-11-06 22:56:53 +0100970 char queue_name[30];
971 struct workqueue_struct *stats_queue;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300972};
973
974struct mvpp2_pcpu_stats {
975 struct u64_stats_sync syncp;
976 u64 rx_packets;
977 u64 rx_bytes;
978 u64 tx_packets;
979 u64 tx_bytes;
980};
981
Marcin Wojtasedc660f2015-08-06 19:00:30 +0200982/* Per-CPU port control */
983struct mvpp2_port_pcpu {
984 struct hrtimer tx_done_timer;
985 bool timer_scheduled;
986 /* Tasklet for egress finalization */
987 struct tasklet_struct tx_done_tasklet;
988};
989
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +0200990struct mvpp2_queue_vector {
991 int irq;
992 struct napi_struct napi;
993 enum { MVPP2_QUEUE_VECTOR_SHARED, MVPP2_QUEUE_VECTOR_PRIVATE } type;
994 int sw_thread_id;
995 u16 sw_thread_mask;
996 int first_rxq;
997 int nrxqs;
998 u32 pending_cause_rx;
999 struct mvpp2_port *port;
1000};
1001
Marcin Wojtas3f518502014-07-10 16:52:13 -03001002struct mvpp2_port {
1003 u8 id;
1004
Thomas Petazzonia7868412017-03-07 16:53:13 +01001005 /* Index of the port from the "group of ports" complex point
1006 * of view
1007 */
1008 int gop_id;
1009
Antoine Tenartfd3651b2017-09-01 11:04:54 +02001010 int link_irq;
1011
Marcin Wojtas3f518502014-07-10 16:52:13 -03001012 struct mvpp2 *priv;
1013
Marcin Wojtas24812222018-01-18 13:31:43 +01001014 /* Firmware node associated to the port */
1015 struct fwnode_handle *fwnode;
1016
Marcin Wojtas3f518502014-07-10 16:52:13 -03001017 /* Per-port registers' base address */
1018 void __iomem *base;
Miquel Raynal118d6292017-11-06 22:56:53 +01001019 void __iomem *stats_base;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001020
1021 struct mvpp2_rx_queue **rxqs;
Thomas Petazzoni09f83972017-08-03 10:41:57 +02001022 unsigned int nrxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001023 struct mvpp2_tx_queue **txqs;
Thomas Petazzoni09f83972017-08-03 10:41:57 +02001024 unsigned int ntxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001025 struct net_device *dev;
1026
1027 int pkt_size;
1028
Marcin Wojtasedc660f2015-08-06 19:00:30 +02001029 /* Per-CPU port control */
1030 struct mvpp2_port_pcpu __percpu *pcpu;
1031
Marcin Wojtas3f518502014-07-10 16:52:13 -03001032 /* Flags */
1033 unsigned long flags;
1034
1035 u16 tx_ring_size;
1036 u16 rx_ring_size;
1037 struct mvpp2_pcpu_stats __percpu *stats;
Miquel Raynal118d6292017-11-06 22:56:53 +01001038 u64 *ethtool_stats;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001039
Miquel Raynale5c500e2017-11-08 08:59:40 +01001040 /* Per-port work and its lock to gather hardware statistics */
1041 struct mutex gather_stats_lock;
1042 struct delayed_work stats_work;
1043
Marcin Wojtas3f518502014-07-10 16:52:13 -03001044 phy_interface_t phy_interface;
1045 struct device_node *phy_node;
Antoine Tenart542897d2017-08-30 10:29:15 +02001046 struct phy *comphy;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001047 unsigned int link;
1048 unsigned int duplex;
1049 unsigned int speed;
1050
1051 struct mvpp2_bm_pool *pool_long;
1052 struct mvpp2_bm_pool *pool_short;
1053
1054 /* Index of first port's physical RXQ */
1055 u8 first_rxq;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02001056
1057 struct mvpp2_queue_vector qvecs[MVPP2_MAX_QVECS];
1058 unsigned int nqvecs;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02001059 bool has_tx_irqs;
1060
1061 u32 tx_time_coal;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001062};
1063
1064/* The mvpp2_tx_desc and mvpp2_rx_desc structures describe the
1065 * layout of the transmit and reception DMA descriptors, and their
1066 * layout is therefore defined by the hardware design
1067 */
1068
1069#define MVPP2_TXD_L3_OFF_SHIFT 0
1070#define MVPP2_TXD_IP_HLEN_SHIFT 8
1071#define MVPP2_TXD_L4_CSUM_FRAG BIT(13)
1072#define MVPP2_TXD_L4_CSUM_NOT BIT(14)
1073#define MVPP2_TXD_IP_CSUM_DISABLE BIT(15)
1074#define MVPP2_TXD_PADDING_DISABLE BIT(23)
1075#define MVPP2_TXD_L4_UDP BIT(24)
1076#define MVPP2_TXD_L3_IP6 BIT(26)
1077#define MVPP2_TXD_L_DESC BIT(28)
1078#define MVPP2_TXD_F_DESC BIT(29)
1079
1080#define MVPP2_RXD_ERR_SUMMARY BIT(15)
1081#define MVPP2_RXD_ERR_CODE_MASK (BIT(13) | BIT(14))
1082#define MVPP2_RXD_ERR_CRC 0x0
1083#define MVPP2_RXD_ERR_OVERRUN BIT(13)
1084#define MVPP2_RXD_ERR_RESOURCE (BIT(13) | BIT(14))
1085#define MVPP2_RXD_BM_POOL_ID_OFFS 16
1086#define MVPP2_RXD_BM_POOL_ID_MASK (BIT(16) | BIT(17) | BIT(18))
1087#define MVPP2_RXD_HWF_SYNC BIT(21)
1088#define MVPP2_RXD_L4_CSUM_OK BIT(22)
1089#define MVPP2_RXD_IP4_HEADER_ERR BIT(24)
1090#define MVPP2_RXD_L4_TCP BIT(25)
1091#define MVPP2_RXD_L4_UDP BIT(26)
1092#define MVPP2_RXD_L3_IP4 BIT(28)
1093#define MVPP2_RXD_L3_IP6 BIT(30)
1094#define MVPP2_RXD_BUF_HDR BIT(31)
1095
Thomas Petazzoni054f6372017-03-07 16:53:07 +01001096/* HW TX descriptor for PPv2.1 */
1097struct mvpp21_tx_desc {
Marcin Wojtas3f518502014-07-10 16:52:13 -03001098 u32 command; /* Options used by HW for packet transmitting.*/
1099 u8 packet_offset; /* the offset from the buffer beginning */
1100 u8 phys_txq; /* destination queue ID */
1101 u16 data_size; /* data size of transmitted packet in bytes */
Thomas Petazzoni20396132017-03-07 16:53:00 +01001102 u32 buf_dma_addr; /* physical addr of transmitted buffer */
Marcin Wojtas3f518502014-07-10 16:52:13 -03001103 u32 buf_cookie; /* cookie for access to TX buffer in tx path */
1104 u32 reserved1[3]; /* hw_cmd (for future use, BM, PON, PNC) */
1105 u32 reserved2; /* reserved (for future use) */
1106};
1107
Thomas Petazzoni054f6372017-03-07 16:53:07 +01001108/* HW RX descriptor for PPv2.1 */
1109struct mvpp21_rx_desc {
Marcin Wojtas3f518502014-07-10 16:52:13 -03001110 u32 status; /* info about received packet */
1111 u16 reserved1; /* parser_info (for future use, PnC) */
1112 u16 data_size; /* size of received packet in bytes */
Thomas Petazzoni20396132017-03-07 16:53:00 +01001113 u32 buf_dma_addr; /* physical address of the buffer */
Marcin Wojtas3f518502014-07-10 16:52:13 -03001114 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
1115 u16 reserved2; /* gem_port_id (for future use, PON) */
1116 u16 reserved3; /* csum_l4 (for future use, PnC) */
1117 u8 reserved4; /* bm_qset (for future use, BM) */
1118 u8 reserved5;
1119 u16 reserved6; /* classify_info (for future use, PnC) */
1120 u32 reserved7; /* flow_id (for future use, PnC) */
1121 u32 reserved8;
1122};
1123
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001124/* HW TX descriptor for PPv2.2 */
1125struct mvpp22_tx_desc {
1126 u32 command;
1127 u8 packet_offset;
1128 u8 phys_txq;
1129 u16 data_size;
1130 u64 reserved1;
1131 u64 buf_dma_addr_ptp;
1132 u64 buf_cookie_misc;
1133};
1134
1135/* HW RX descriptor for PPv2.2 */
1136struct mvpp22_rx_desc {
1137 u32 status;
1138 u16 reserved1;
1139 u16 data_size;
1140 u32 reserved2;
1141 u32 reserved3;
1142 u64 buf_dma_addr_key_hash;
1143 u64 buf_cookie_misc;
1144};
1145
Thomas Petazzoni054f6372017-03-07 16:53:07 +01001146/* Opaque type used by the driver to manipulate the HW TX and RX
1147 * descriptors
1148 */
1149struct mvpp2_tx_desc {
1150 union {
1151 struct mvpp21_tx_desc pp21;
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001152 struct mvpp22_tx_desc pp22;
Thomas Petazzoni054f6372017-03-07 16:53:07 +01001153 };
1154};
1155
1156struct mvpp2_rx_desc {
1157 union {
1158 struct mvpp21_rx_desc pp21;
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001159 struct mvpp22_rx_desc pp22;
Thomas Petazzoni054f6372017-03-07 16:53:07 +01001160 };
1161};
1162
Thomas Petazzoni83544912016-12-21 11:28:49 +01001163struct mvpp2_txq_pcpu_buf {
1164 /* Transmitted SKB */
1165 struct sk_buff *skb;
1166
1167 /* Physical address of transmitted buffer */
Thomas Petazzoni20396132017-03-07 16:53:00 +01001168 dma_addr_t dma;
Thomas Petazzoni83544912016-12-21 11:28:49 +01001169
1170 /* Size transmitted */
1171 size_t size;
1172};
1173
Marcin Wojtas3f518502014-07-10 16:52:13 -03001174/* Per-CPU Tx queue control */
1175struct mvpp2_txq_pcpu {
1176 int cpu;
1177
1178 /* Number of Tx DMA descriptors in the descriptor ring */
1179 int size;
1180
1181 /* Number of currently used Tx DMA descriptor in the
1182 * descriptor ring
1183 */
1184 int count;
1185
Antoine Tenart1d17db02017-10-30 11:23:31 +01001186 int wake_threshold;
1187 int stop_threshold;
1188
Marcin Wojtas3f518502014-07-10 16:52:13 -03001189 /* Number of Tx DMA descriptors reserved for each CPU */
1190 int reserved_num;
1191
Thomas Petazzoni83544912016-12-21 11:28:49 +01001192 /* Infos about transmitted buffers */
1193 struct mvpp2_txq_pcpu_buf *buffs;
Marcin Wojtas71ce3912015-08-06 19:00:29 +02001194
Marcin Wojtas3f518502014-07-10 16:52:13 -03001195 /* Index of last TX DMA descriptor that was inserted */
1196 int txq_put_index;
1197
1198 /* Index of the TX DMA descriptor to be cleaned up */
1199 int txq_get_index;
Antoine Ténart186cd4d2017-08-23 09:46:56 +02001200
1201 /* DMA buffer for TSO headers */
1202 char *tso_headers;
1203 dma_addr_t tso_headers_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001204};
1205
1206struct mvpp2_tx_queue {
1207 /* Physical number of this Tx queue */
1208 u8 id;
1209
1210 /* Logical number of this Tx queue */
1211 u8 log_id;
1212
1213 /* Number of Tx DMA descriptors in the descriptor ring */
1214 int size;
1215
1216 /* Number of currently used Tx DMA descriptor in the descriptor ring */
1217 int count;
1218
1219 /* Per-CPU control of physical Tx queues */
1220 struct mvpp2_txq_pcpu __percpu *pcpu;
1221
Marcin Wojtas3f518502014-07-10 16:52:13 -03001222 u32 done_pkts_coal;
1223
1224 /* Virtual address of thex Tx DMA descriptors array */
1225 struct mvpp2_tx_desc *descs;
1226
1227 /* DMA address of the Tx DMA descriptors array */
Thomas Petazzoni20396132017-03-07 16:53:00 +01001228 dma_addr_t descs_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001229
1230 /* Index of the last Tx DMA descriptor */
1231 int last_desc;
1232
1233 /* Index of the next Tx DMA descriptor to process */
1234 int next_desc_to_proc;
1235};
1236
1237struct mvpp2_rx_queue {
1238 /* RX queue number, in the range 0-31 for physical RXQs */
1239 u8 id;
1240
1241 /* Num of rx descriptors in the rx descriptor ring */
1242 int size;
1243
1244 u32 pkts_coal;
1245 u32 time_coal;
1246
1247 /* Virtual address of the RX DMA descriptors array */
1248 struct mvpp2_rx_desc *descs;
1249
1250 /* DMA address of the RX DMA descriptors array */
Thomas Petazzoni20396132017-03-07 16:53:00 +01001251 dma_addr_t descs_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001252
1253 /* Index of the last RX DMA descriptor */
1254 int last_desc;
1255
1256 /* Index of the next RX DMA descriptor to process */
1257 int next_desc_to_proc;
1258
1259 /* ID of port to which physical RXQ is mapped */
1260 int port;
1261
1262 /* Port's logic RXQ number to which physical RXQ is mapped */
1263 int logic_rxq;
1264};
1265
1266union mvpp2_prs_tcam_entry {
1267 u32 word[MVPP2_PRS_TCAM_WORDS];
1268 u8 byte[MVPP2_PRS_TCAM_WORDS * 4];
1269};
1270
1271union mvpp2_prs_sram_entry {
1272 u32 word[MVPP2_PRS_SRAM_WORDS];
1273 u8 byte[MVPP2_PRS_SRAM_WORDS * 4];
1274};
1275
1276struct mvpp2_prs_entry {
1277 u32 index;
1278 union mvpp2_prs_tcam_entry tcam;
1279 union mvpp2_prs_sram_entry sram;
1280};
1281
1282struct mvpp2_prs_shadow {
1283 bool valid;
1284 bool finish;
1285
1286 /* Lookup ID */
1287 int lu;
1288
1289 /* User defined offset */
1290 int udf;
1291
1292 /* Result info */
1293 u32 ri;
1294 u32 ri_mask;
1295};
1296
1297struct mvpp2_cls_flow_entry {
1298 u32 index;
1299 u32 data[MVPP2_CLS_FLOWS_TBL_DATA_WORDS];
1300};
1301
1302struct mvpp2_cls_lookup_entry {
1303 u32 lkpid;
1304 u32 way;
1305 u32 data;
1306};
1307
1308struct mvpp2_bm_pool {
1309 /* Pool number in the range 0-7 */
1310 int id;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001311
1312 /* Buffer Pointers Pool External (BPPE) size */
1313 int size;
Thomas Petazzonid01524d2017-03-07 16:53:09 +01001314 /* BPPE size in bytes */
1315 int size_bytes;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001316 /* Number of buffers for this pool */
1317 int buf_num;
1318 /* Pool buffer size */
1319 int buf_size;
1320 /* Packet size */
1321 int pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01001322 int frag_size;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001323
1324 /* BPPE virtual base address */
1325 u32 *virt_addr;
Thomas Petazzoni20396132017-03-07 16:53:00 +01001326 /* BPPE DMA base address */
1327 dma_addr_t dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001328
1329 /* Ports using BM pool */
1330 u32 port_map;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001331};
1332
Antoine Tenart20920262017-10-23 15:24:30 +02001333#define IS_TSO_HEADER(txq_pcpu, addr) \
1334 ((addr) >= (txq_pcpu)->tso_headers_dma && \
1335 (addr) < (txq_pcpu)->tso_headers_dma + \
1336 (txq_pcpu)->size * TSO_HEADER_SIZE)
1337
Thomas Petazzoni213f4282017-08-03 10:42:00 +02001338/* Queue modes */
1339#define MVPP2_QDIST_SINGLE_MODE 0
1340#define MVPP2_QDIST_MULTI_MODE 1
1341
1342static int queue_mode = MVPP2_QDIST_SINGLE_MODE;
1343
1344module_param(queue_mode, int, 0444);
1345MODULE_PARM_DESC(queue_mode, "Set queue_mode (single=0, multi=1)");
1346
Marcin Wojtas3f518502014-07-10 16:52:13 -03001347#define MVPP2_DRIVER_NAME "mvpp2"
1348#define MVPP2_DRIVER_VERSION "1.0"
1349
1350/* Utility/helper methods */
1351
1352static void mvpp2_write(struct mvpp2 *priv, u32 offset, u32 data)
1353{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001354 writel(data, priv->swth_base[0] + offset);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001355}
1356
1357static u32 mvpp2_read(struct mvpp2 *priv, u32 offset)
1358{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001359 return readl(priv->swth_base[0] + offset);
Thomas Petazzonia7868412017-03-07 16:53:13 +01001360}
1361
1362/* These accessors should be used to access:
1363 *
1364 * - per-CPU registers, where each CPU has its own copy of the
1365 * register.
1366 *
1367 * MVPP2_BM_VIRT_ALLOC_REG
1368 * MVPP2_BM_ADDR_HIGH_ALLOC
1369 * MVPP22_BM_ADDR_HIGH_RLS_REG
1370 * MVPP2_BM_VIRT_RLS_REG
1371 * MVPP2_ISR_RX_TX_CAUSE_REG
1372 * MVPP2_ISR_RX_TX_MASK_REG
1373 * MVPP2_TXQ_NUM_REG
1374 * MVPP2_AGGR_TXQ_UPDATE_REG
1375 * MVPP2_TXQ_RSVD_REQ_REG
1376 * MVPP2_TXQ_RSVD_RSLT_REG
1377 * MVPP2_TXQ_SENT_REG
1378 * MVPP2_RXQ_NUM_REG
1379 *
1380 * - global registers that must be accessed through a specific CPU
1381 * window, because they are related to an access to a per-CPU
1382 * register
1383 *
1384 * MVPP2_BM_PHY_ALLOC_REG (related to MVPP2_BM_VIRT_ALLOC_REG)
1385 * MVPP2_BM_PHY_RLS_REG (related to MVPP2_BM_VIRT_RLS_REG)
1386 * MVPP2_RXQ_THRESH_REG (related to MVPP2_RXQ_NUM_REG)
1387 * MVPP2_RXQ_DESC_ADDR_REG (related to MVPP2_RXQ_NUM_REG)
1388 * MVPP2_RXQ_DESC_SIZE_REG (related to MVPP2_RXQ_NUM_REG)
1389 * MVPP2_RXQ_INDEX_REG (related to MVPP2_RXQ_NUM_REG)
1390 * MVPP2_TXQ_PENDING_REG (related to MVPP2_TXQ_NUM_REG)
1391 * MVPP2_TXQ_DESC_ADDR_REG (related to MVPP2_TXQ_NUM_REG)
1392 * MVPP2_TXQ_DESC_SIZE_REG (related to MVPP2_TXQ_NUM_REG)
1393 * MVPP2_TXQ_INDEX_REG (related to MVPP2_TXQ_NUM_REG)
1394 * MVPP2_TXQ_PENDING_REG (related to MVPP2_TXQ_NUM_REG)
1395 * MVPP2_TXQ_PREF_BUF_REG (related to MVPP2_TXQ_NUM_REG)
1396 * MVPP2_TXQ_PREF_BUF_REG (related to MVPP2_TXQ_NUM_REG)
1397 */
1398static void mvpp2_percpu_write(struct mvpp2 *priv, int cpu,
1399 u32 offset, u32 data)
1400{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001401 writel(data, priv->swth_base[cpu] + offset);
Thomas Petazzonia7868412017-03-07 16:53:13 +01001402}
1403
1404static u32 mvpp2_percpu_read(struct mvpp2 *priv, int cpu,
1405 u32 offset)
1406{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001407 return readl(priv->swth_base[cpu] + offset);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001408}
1409
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001410static dma_addr_t mvpp2_txdesc_dma_addr_get(struct mvpp2_port *port,
1411 struct mvpp2_tx_desc *tx_desc)
1412{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001413 if (port->priv->hw_version == MVPP21)
1414 return tx_desc->pp21.buf_dma_addr;
1415 else
1416 return tx_desc->pp22.buf_dma_addr_ptp & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001417}
1418
1419static void mvpp2_txdesc_dma_addr_set(struct mvpp2_port *port,
1420 struct mvpp2_tx_desc *tx_desc,
1421 dma_addr_t dma_addr)
1422{
Antoine Tenart6eb5d372017-10-30 11:23:33 +01001423 dma_addr_t addr, offset;
1424
1425 addr = dma_addr & ~MVPP2_TX_DESC_ALIGN;
1426 offset = dma_addr & MVPP2_TX_DESC_ALIGN;
1427
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001428 if (port->priv->hw_version == MVPP21) {
Antoine Tenart6eb5d372017-10-30 11:23:33 +01001429 tx_desc->pp21.buf_dma_addr = addr;
1430 tx_desc->pp21.packet_offset = offset;
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001431 } else {
Antoine Tenart6eb5d372017-10-30 11:23:33 +01001432 u64 val = (u64)addr;
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001433
1434 tx_desc->pp22.buf_dma_addr_ptp &= ~GENMASK_ULL(40, 0);
1435 tx_desc->pp22.buf_dma_addr_ptp |= val;
Antoine Tenart6eb5d372017-10-30 11:23:33 +01001436 tx_desc->pp22.packet_offset = offset;
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001437 }
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001438}
1439
1440static size_t mvpp2_txdesc_size_get(struct mvpp2_port *port,
1441 struct mvpp2_tx_desc *tx_desc)
1442{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001443 if (port->priv->hw_version == MVPP21)
1444 return tx_desc->pp21.data_size;
1445 else
1446 return tx_desc->pp22.data_size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001447}
1448
1449static void mvpp2_txdesc_size_set(struct mvpp2_port *port,
1450 struct mvpp2_tx_desc *tx_desc,
1451 size_t size)
1452{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001453 if (port->priv->hw_version == MVPP21)
1454 tx_desc->pp21.data_size = size;
1455 else
1456 tx_desc->pp22.data_size = size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001457}
1458
1459static void mvpp2_txdesc_txq_set(struct mvpp2_port *port,
1460 struct mvpp2_tx_desc *tx_desc,
1461 unsigned int txq)
1462{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001463 if (port->priv->hw_version == MVPP21)
1464 tx_desc->pp21.phys_txq = txq;
1465 else
1466 tx_desc->pp22.phys_txq = txq;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001467}
1468
1469static void mvpp2_txdesc_cmd_set(struct mvpp2_port *port,
1470 struct mvpp2_tx_desc *tx_desc,
1471 unsigned int command)
1472{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001473 if (port->priv->hw_version == MVPP21)
1474 tx_desc->pp21.command = command;
1475 else
1476 tx_desc->pp22.command = command;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001477}
1478
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001479static unsigned int mvpp2_txdesc_offset_get(struct mvpp2_port *port,
1480 struct mvpp2_tx_desc *tx_desc)
1481{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001482 if (port->priv->hw_version == MVPP21)
1483 return tx_desc->pp21.packet_offset;
1484 else
1485 return tx_desc->pp22.packet_offset;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001486}
1487
1488static dma_addr_t mvpp2_rxdesc_dma_addr_get(struct mvpp2_port *port,
1489 struct mvpp2_rx_desc *rx_desc)
1490{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001491 if (port->priv->hw_version == MVPP21)
1492 return rx_desc->pp21.buf_dma_addr;
1493 else
1494 return rx_desc->pp22.buf_dma_addr_key_hash & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001495}
1496
1497static unsigned long mvpp2_rxdesc_cookie_get(struct mvpp2_port *port,
1498 struct mvpp2_rx_desc *rx_desc)
1499{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001500 if (port->priv->hw_version == MVPP21)
1501 return rx_desc->pp21.buf_cookie;
1502 else
1503 return rx_desc->pp22.buf_cookie_misc & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001504}
1505
1506static size_t mvpp2_rxdesc_size_get(struct mvpp2_port *port,
1507 struct mvpp2_rx_desc *rx_desc)
1508{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001509 if (port->priv->hw_version == MVPP21)
1510 return rx_desc->pp21.data_size;
1511 else
1512 return rx_desc->pp22.data_size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001513}
1514
1515static u32 mvpp2_rxdesc_status_get(struct mvpp2_port *port,
1516 struct mvpp2_rx_desc *rx_desc)
1517{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001518 if (port->priv->hw_version == MVPP21)
1519 return rx_desc->pp21.status;
1520 else
1521 return rx_desc->pp22.status;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001522}
1523
Marcin Wojtas3f518502014-07-10 16:52:13 -03001524static void mvpp2_txq_inc_get(struct mvpp2_txq_pcpu *txq_pcpu)
1525{
1526 txq_pcpu->txq_get_index++;
1527 if (txq_pcpu->txq_get_index == txq_pcpu->size)
1528 txq_pcpu->txq_get_index = 0;
1529}
1530
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001531static void mvpp2_txq_inc_put(struct mvpp2_port *port,
1532 struct mvpp2_txq_pcpu *txq_pcpu,
Marcin Wojtas71ce3912015-08-06 19:00:29 +02001533 struct sk_buff *skb,
1534 struct mvpp2_tx_desc *tx_desc)
Marcin Wojtas3f518502014-07-10 16:52:13 -03001535{
Thomas Petazzoni83544912016-12-21 11:28:49 +01001536 struct mvpp2_txq_pcpu_buf *tx_buf =
1537 txq_pcpu->buffs + txq_pcpu->txq_put_index;
1538 tx_buf->skb = skb;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001539 tx_buf->size = mvpp2_txdesc_size_get(port, tx_desc);
1540 tx_buf->dma = mvpp2_txdesc_dma_addr_get(port, tx_desc) +
1541 mvpp2_txdesc_offset_get(port, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001542 txq_pcpu->txq_put_index++;
1543 if (txq_pcpu->txq_put_index == txq_pcpu->size)
1544 txq_pcpu->txq_put_index = 0;
1545}
1546
1547/* Get number of physical egress port */
1548static inline int mvpp2_egress_port(struct mvpp2_port *port)
1549{
1550 return MVPP2_MAX_TCONT + port->id;
1551}
1552
1553/* Get number of physical TXQ */
1554static inline int mvpp2_txq_phys(int port, int txq)
1555{
1556 return (MVPP2_MAX_TCONT + port) * MVPP2_MAX_TXQ + txq;
1557}
1558
1559/* Parser configuration routines */
1560
1561/* Update parser tcam and sram hw entries */
1562static int mvpp2_prs_hw_write(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1563{
1564 int i;
1565
1566 if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1567 return -EINVAL;
1568
1569 /* Clear entry invalidation bit */
1570 pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] &= ~MVPP2_PRS_TCAM_INV_MASK;
1571
1572 /* Write tcam index - indirect access */
1573 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1574 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1575 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), pe->tcam.word[i]);
1576
1577 /* Write sram index - indirect access */
1578 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1579 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1580 mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), pe->sram.word[i]);
1581
1582 return 0;
1583}
1584
1585/* Read tcam entry from hw */
1586static int mvpp2_prs_hw_read(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1587{
1588 int i;
1589
1590 if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1591 return -EINVAL;
1592
1593 /* Write tcam index - indirect access */
1594 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1595
1596 pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] = mvpp2_read(priv,
1597 MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD));
1598 if (pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] & MVPP2_PRS_TCAM_INV_MASK)
1599 return MVPP2_PRS_TCAM_ENTRY_INVALID;
1600
1601 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1602 pe->tcam.word[i] = mvpp2_read(priv, MVPP2_PRS_TCAM_DATA_REG(i));
1603
1604 /* Write sram index - indirect access */
1605 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1606 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1607 pe->sram.word[i] = mvpp2_read(priv, MVPP2_PRS_SRAM_DATA_REG(i));
1608
1609 return 0;
1610}
1611
1612/* Invalidate tcam hw entry */
1613static void mvpp2_prs_hw_inv(struct mvpp2 *priv, int index)
1614{
1615 /* Write index - indirect access */
1616 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
1617 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD),
1618 MVPP2_PRS_TCAM_INV_MASK);
1619}
1620
1621/* Enable shadow table entry and set its lookup ID */
1622static void mvpp2_prs_shadow_set(struct mvpp2 *priv, int index, int lu)
1623{
1624 priv->prs_shadow[index].valid = true;
1625 priv->prs_shadow[index].lu = lu;
1626}
1627
1628/* Update ri fields in shadow table entry */
1629static void mvpp2_prs_shadow_ri_set(struct mvpp2 *priv, int index,
1630 unsigned int ri, unsigned int ri_mask)
1631{
1632 priv->prs_shadow[index].ri_mask = ri_mask;
1633 priv->prs_shadow[index].ri = ri;
1634}
1635
1636/* Update lookup field in tcam sw entry */
1637static void mvpp2_prs_tcam_lu_set(struct mvpp2_prs_entry *pe, unsigned int lu)
1638{
1639 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_LU_BYTE);
1640
1641 pe->tcam.byte[MVPP2_PRS_TCAM_LU_BYTE] = lu;
1642 pe->tcam.byte[enable_off] = MVPP2_PRS_LU_MASK;
1643}
1644
1645/* Update mask for single port in tcam sw entry */
1646static void mvpp2_prs_tcam_port_set(struct mvpp2_prs_entry *pe,
1647 unsigned int port, bool add)
1648{
1649 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1650
1651 if (add)
1652 pe->tcam.byte[enable_off] &= ~(1 << port);
1653 else
1654 pe->tcam.byte[enable_off] |= 1 << port;
1655}
1656
1657/* Update port map in tcam sw entry */
1658static void mvpp2_prs_tcam_port_map_set(struct mvpp2_prs_entry *pe,
1659 unsigned int ports)
1660{
1661 unsigned char port_mask = MVPP2_PRS_PORT_MASK;
1662 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1663
1664 pe->tcam.byte[MVPP2_PRS_TCAM_PORT_BYTE] = 0;
1665 pe->tcam.byte[enable_off] &= ~port_mask;
1666 pe->tcam.byte[enable_off] |= ~ports & MVPP2_PRS_PORT_MASK;
1667}
1668
1669/* Obtain port map from tcam sw entry */
1670static unsigned int mvpp2_prs_tcam_port_map_get(struct mvpp2_prs_entry *pe)
1671{
1672 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1673
1674 return ~(pe->tcam.byte[enable_off]) & MVPP2_PRS_PORT_MASK;
1675}
1676
1677/* Set byte of data and its enable bits in tcam sw entry */
1678static void mvpp2_prs_tcam_data_byte_set(struct mvpp2_prs_entry *pe,
1679 unsigned int offs, unsigned char byte,
1680 unsigned char enable)
1681{
1682 pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)] = byte;
1683 pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)] = enable;
1684}
1685
1686/* Get byte of data and its enable bits from tcam sw entry */
1687static void mvpp2_prs_tcam_data_byte_get(struct mvpp2_prs_entry *pe,
1688 unsigned int offs, unsigned char *byte,
1689 unsigned char *enable)
1690{
1691 *byte = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)];
1692 *enable = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)];
1693}
1694
1695/* Compare tcam data bytes with a pattern */
1696static bool mvpp2_prs_tcam_data_cmp(struct mvpp2_prs_entry *pe, int offs,
1697 u16 data)
1698{
1699 int off = MVPP2_PRS_TCAM_DATA_BYTE(offs);
1700 u16 tcam_data;
1701
Antoine Tenartef4816f2017-10-24 11:41:26 +02001702 tcam_data = (pe->tcam.byte[off + 1] << 8) | pe->tcam.byte[off];
Marcin Wojtas3f518502014-07-10 16:52:13 -03001703 if (tcam_data != data)
1704 return false;
1705 return true;
1706}
1707
1708/* Update ai bits in tcam sw entry */
1709static void mvpp2_prs_tcam_ai_update(struct mvpp2_prs_entry *pe,
1710 unsigned int bits, unsigned int enable)
1711{
1712 int i, ai_idx = MVPP2_PRS_TCAM_AI_BYTE;
1713
1714 for (i = 0; i < MVPP2_PRS_AI_BITS; i++) {
1715
1716 if (!(enable & BIT(i)))
1717 continue;
1718
1719 if (bits & BIT(i))
1720 pe->tcam.byte[ai_idx] |= 1 << i;
1721 else
1722 pe->tcam.byte[ai_idx] &= ~(1 << i);
1723 }
1724
1725 pe->tcam.byte[MVPP2_PRS_TCAM_EN_OFFS(ai_idx)] |= enable;
1726}
1727
1728/* Get ai bits from tcam sw entry */
1729static int mvpp2_prs_tcam_ai_get(struct mvpp2_prs_entry *pe)
1730{
1731 return pe->tcam.byte[MVPP2_PRS_TCAM_AI_BYTE];
1732}
1733
1734/* Set ethertype in tcam sw entry */
1735static void mvpp2_prs_match_etype(struct mvpp2_prs_entry *pe, int offset,
1736 unsigned short ethertype)
1737{
1738 mvpp2_prs_tcam_data_byte_set(pe, offset + 0, ethertype >> 8, 0xff);
1739 mvpp2_prs_tcam_data_byte_set(pe, offset + 1, ethertype & 0xff, 0xff);
1740}
1741
Maxime Chevallier56beda32018-02-28 10:14:13 +01001742/* Set vid in tcam sw entry */
1743static void mvpp2_prs_match_vid(struct mvpp2_prs_entry *pe, int offset,
1744 unsigned short vid)
1745{
1746 mvpp2_prs_tcam_data_byte_set(pe, offset + 0, (vid & 0xf00) >> 8, 0xf);
1747 mvpp2_prs_tcam_data_byte_set(pe, offset + 1, vid & 0xff, 0xff);
1748}
1749
Marcin Wojtas3f518502014-07-10 16:52:13 -03001750/* Set bits in sram sw entry */
1751static void mvpp2_prs_sram_bits_set(struct mvpp2_prs_entry *pe, int bit_num,
1752 int val)
1753{
1754 pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] |= (val << (bit_num % 8));
1755}
1756
1757/* Clear bits in sram sw entry */
1758static void mvpp2_prs_sram_bits_clear(struct mvpp2_prs_entry *pe, int bit_num,
1759 int val)
1760{
1761 pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] &= ~(val << (bit_num % 8));
1762}
1763
1764/* Update ri bits in sram sw entry */
1765static void mvpp2_prs_sram_ri_update(struct mvpp2_prs_entry *pe,
1766 unsigned int bits, unsigned int mask)
1767{
1768 unsigned int i;
1769
1770 for (i = 0; i < MVPP2_PRS_SRAM_RI_CTRL_BITS; i++) {
1771 int ri_off = MVPP2_PRS_SRAM_RI_OFFS;
1772
1773 if (!(mask & BIT(i)))
1774 continue;
1775
1776 if (bits & BIT(i))
1777 mvpp2_prs_sram_bits_set(pe, ri_off + i, 1);
1778 else
1779 mvpp2_prs_sram_bits_clear(pe, ri_off + i, 1);
1780
1781 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_RI_CTRL_OFFS + i, 1);
1782 }
1783}
1784
1785/* Obtain ri bits from sram sw entry */
1786static int mvpp2_prs_sram_ri_get(struct mvpp2_prs_entry *pe)
1787{
1788 return pe->sram.word[MVPP2_PRS_SRAM_RI_WORD];
1789}
1790
1791/* Update ai bits in sram sw entry */
1792static void mvpp2_prs_sram_ai_update(struct mvpp2_prs_entry *pe,
1793 unsigned int bits, unsigned int mask)
1794{
1795 unsigned int i;
1796 int ai_off = MVPP2_PRS_SRAM_AI_OFFS;
1797
1798 for (i = 0; i < MVPP2_PRS_SRAM_AI_CTRL_BITS; i++) {
1799
1800 if (!(mask & BIT(i)))
1801 continue;
1802
1803 if (bits & BIT(i))
1804 mvpp2_prs_sram_bits_set(pe, ai_off + i, 1);
1805 else
1806 mvpp2_prs_sram_bits_clear(pe, ai_off + i, 1);
1807
1808 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_AI_CTRL_OFFS + i, 1);
1809 }
1810}
1811
1812/* Read ai bits from sram sw entry */
1813static int mvpp2_prs_sram_ai_get(struct mvpp2_prs_entry *pe)
1814{
1815 u8 bits;
1816 int ai_off = MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_AI_OFFS);
1817 int ai_en_off = ai_off + 1;
1818 int ai_shift = MVPP2_PRS_SRAM_AI_OFFS % 8;
1819
1820 bits = (pe->sram.byte[ai_off] >> ai_shift) |
1821 (pe->sram.byte[ai_en_off] << (8 - ai_shift));
1822
1823 return bits;
1824}
1825
1826/* In sram sw entry set lookup ID field of the tcam key to be used in the next
1827 * lookup interation
1828 */
1829static void mvpp2_prs_sram_next_lu_set(struct mvpp2_prs_entry *pe,
1830 unsigned int lu)
1831{
1832 int sram_next_off = MVPP2_PRS_SRAM_NEXT_LU_OFFS;
1833
1834 mvpp2_prs_sram_bits_clear(pe, sram_next_off,
1835 MVPP2_PRS_SRAM_NEXT_LU_MASK);
1836 mvpp2_prs_sram_bits_set(pe, sram_next_off, lu);
1837}
1838
1839/* In the sram sw entry set sign and value of the next lookup offset
1840 * and the offset value generated to the classifier
1841 */
1842static void mvpp2_prs_sram_shift_set(struct mvpp2_prs_entry *pe, int shift,
1843 unsigned int op)
1844{
1845 /* Set sign */
1846 if (shift < 0) {
1847 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1848 shift = 0 - shift;
1849 } else {
1850 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1851 }
1852
1853 /* Set value */
1854 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_SHIFT_OFFS)] =
1855 (unsigned char)shift;
1856
1857 /* Reset and set operation */
1858 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS,
1859 MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK);
1860 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS, op);
1861
1862 /* Set base offset as current */
1863 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1864}
1865
1866/* In the sram sw entry set sign and value of the user defined offset
1867 * generated to the classifier
1868 */
1869static void mvpp2_prs_sram_offset_set(struct mvpp2_prs_entry *pe,
1870 unsigned int type, int offset,
1871 unsigned int op)
1872{
1873 /* Set sign */
1874 if (offset < 0) {
1875 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1876 offset = 0 - offset;
1877 } else {
1878 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1879 }
1880
1881 /* Set value */
1882 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_OFFS,
1883 MVPP2_PRS_SRAM_UDF_MASK);
1884 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_OFFS, offset);
1885 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1886 MVPP2_PRS_SRAM_UDF_BITS)] &=
1887 ~(MVPP2_PRS_SRAM_UDF_MASK >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1888 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1889 MVPP2_PRS_SRAM_UDF_BITS)] |=
1890 (offset >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1891
1892 /* Set offset type */
1893 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS,
1894 MVPP2_PRS_SRAM_UDF_TYPE_MASK);
1895 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS, type);
1896
1897 /* Set offset operation */
1898 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS,
1899 MVPP2_PRS_SRAM_OP_SEL_UDF_MASK);
1900 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS, op);
1901
1902 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1903 MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] &=
1904 ~(MVPP2_PRS_SRAM_OP_SEL_UDF_MASK >>
1905 (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1906
1907 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1908 MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] |=
1909 (op >> (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1910
1911 /* Set base offset as current */
1912 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1913}
1914
1915/* Find parser flow entry */
1916static struct mvpp2_prs_entry *mvpp2_prs_flow_find(struct mvpp2 *priv, int flow)
1917{
1918 struct mvpp2_prs_entry *pe;
1919 int tid;
1920
1921 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
1922 if (!pe)
1923 return NULL;
1924 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
1925
1926 /* Go through the all entires with MVPP2_PRS_LU_FLOWS */
1927 for (tid = MVPP2_PRS_TCAM_SRAM_SIZE - 1; tid >= 0; tid--) {
1928 u8 bits;
1929
1930 if (!priv->prs_shadow[tid].valid ||
1931 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_FLOWS)
1932 continue;
1933
1934 pe->index = tid;
1935 mvpp2_prs_hw_read(priv, pe);
1936 bits = mvpp2_prs_sram_ai_get(pe);
1937
1938 /* Sram store classification lookup ID in AI bits [5:0] */
1939 if ((bits & MVPP2_PRS_FLOW_ID_MASK) == flow)
1940 return pe;
1941 }
1942 kfree(pe);
1943
1944 return NULL;
1945}
1946
1947/* Return first free tcam index, seeking from start to end */
1948static int mvpp2_prs_tcam_first_free(struct mvpp2 *priv, unsigned char start,
1949 unsigned char end)
1950{
1951 int tid;
1952
1953 if (start > end)
1954 swap(start, end);
1955
1956 if (end >= MVPP2_PRS_TCAM_SRAM_SIZE)
1957 end = MVPP2_PRS_TCAM_SRAM_SIZE - 1;
1958
1959 for (tid = start; tid <= end; tid++) {
1960 if (!priv->prs_shadow[tid].valid)
1961 return tid;
1962 }
1963
1964 return -EINVAL;
1965}
1966
1967/* Enable/disable dropping all mac da's */
1968static void mvpp2_prs_mac_drop_all_set(struct mvpp2 *priv, int port, bool add)
1969{
1970 struct mvpp2_prs_entry pe;
1971
1972 if (priv->prs_shadow[MVPP2_PE_DROP_ALL].valid) {
1973 /* Entry exist - update port only */
1974 pe.index = MVPP2_PE_DROP_ALL;
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_MAC);
1980 pe.index = MVPP2_PE_DROP_ALL;
1981
1982 /* Non-promiscuous mode for all ports - DROP unknown packets */
1983 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
1984 MVPP2_PRS_RI_DROP_MASK);
1985
1986 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1987 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1988
1989 /* Update shadow table */
1990 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1991
1992 /* Mask all ports */
1993 mvpp2_prs_tcam_port_map_set(&pe, 0);
1994 }
1995
1996 /* Update port mask */
1997 mvpp2_prs_tcam_port_set(&pe, port, add);
1998
1999 mvpp2_prs_hw_write(priv, &pe);
2000}
2001
Maxime Chevallier10fea262018-03-07 15:18:04 +01002002/* Set port to unicast or multicast promiscuous mode */
2003static void mvpp2_prs_mac_promisc_set(struct mvpp2 *priv, int port,
2004 enum mvpp2_prs_l2_cast l2_cast, bool add)
Marcin Wojtas3f518502014-07-10 16:52:13 -03002005{
2006 struct mvpp2_prs_entry pe;
Maxime Chevallier10fea262018-03-07 15:18:04 +01002007 unsigned char cast_match;
2008 unsigned int ri;
2009 int tid;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002010
Maxime Chevallier10fea262018-03-07 15:18:04 +01002011 if (l2_cast == MVPP2_PRS_L2_UNI_CAST) {
2012 cast_match = MVPP2_PRS_UCAST_VAL;
2013 tid = MVPP2_PE_MAC_UC_PROMISCUOUS;
2014 ri = MVPP2_PRS_RI_L2_UCAST;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002015 } else {
Maxime Chevallier10fea262018-03-07 15:18:04 +01002016 cast_match = MVPP2_PRS_MCAST_VAL;
2017 tid = MVPP2_PE_MAC_MC_PROMISCUOUS;
2018 ri = MVPP2_PRS_RI_L2_MCAST;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002019 }
2020
Maxime Chevallier10fea262018-03-07 15:18:04 +01002021 /* promiscuous mode - Accept unknown unicast or multicast packets */
2022 if (priv->prs_shadow[tid].valid) {
2023 pe.index = tid;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002024 mvpp2_prs_hw_read(priv, &pe);
2025 } else {
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002026 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002027 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
Maxime Chevallier10fea262018-03-07 15:18:04 +01002028 pe.index = tid;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002029
2030 /* Continue - set next lookup */
2031 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
2032
2033 /* Set result info bits */
Maxime Chevallier10fea262018-03-07 15:18:04 +01002034 mvpp2_prs_sram_ri_update(&pe, ri, MVPP2_PRS_RI_L2_CAST_MASK);
Marcin Wojtas3f518502014-07-10 16:52:13 -03002035
Maxime Chevallier10fea262018-03-07 15:18:04 +01002036 /* Match UC or MC addresses */
2037 mvpp2_prs_tcam_data_byte_set(&pe, 0, cast_match,
2038 MVPP2_PRS_CAST_MASK);
Marcin Wojtas3f518502014-07-10 16:52:13 -03002039
2040 /* Shift to ethertype */
2041 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
2042 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2043
2044 /* Mask all ports */
2045 mvpp2_prs_tcam_port_map_set(&pe, 0);
2046
2047 /* Update shadow table */
2048 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2049 }
2050
2051 /* Update port mask */
2052 mvpp2_prs_tcam_port_set(&pe, port, add);
2053
2054 mvpp2_prs_hw_write(priv, &pe);
2055}
2056
2057/* Set entry for dsa packets */
2058static void mvpp2_prs_dsa_tag_set(struct mvpp2 *priv, int port, bool add,
2059 bool tagged, bool extend)
2060{
2061 struct mvpp2_prs_entry pe;
2062 int tid, shift;
2063
2064 if (extend) {
2065 tid = tagged ? MVPP2_PE_EDSA_TAGGED : MVPP2_PE_EDSA_UNTAGGED;
2066 shift = 8;
2067 } else {
2068 tid = tagged ? MVPP2_PE_DSA_TAGGED : MVPP2_PE_DSA_UNTAGGED;
2069 shift = 4;
2070 }
2071
2072 if (priv->prs_shadow[tid].valid) {
2073 /* Entry exist - update port only */
2074 pe.index = tid;
2075 mvpp2_prs_hw_read(priv, &pe);
2076 } else {
2077 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002078 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002079 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
2080 pe.index = tid;
2081
Marcin Wojtas3f518502014-07-10 16:52:13 -03002082 /* Update shadow table */
2083 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
2084
2085 if (tagged) {
2086 /* Set tagged bit in DSA tag */
2087 mvpp2_prs_tcam_data_byte_set(&pe, 0,
Maxime Chevallier56beda32018-02-28 10:14:13 +01002088 MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
2089 MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
2090
2091 /* Set ai bits for next iteration */
2092 if (extend)
2093 mvpp2_prs_sram_ai_update(&pe, 1,
2094 MVPP2_PRS_SRAM_AI_MASK);
2095 else
2096 mvpp2_prs_sram_ai_update(&pe, 0,
2097 MVPP2_PRS_SRAM_AI_MASK);
2098
2099 /* If packet is tagged continue check vid filtering */
2100 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VID);
Marcin Wojtas3f518502014-07-10 16:52:13 -03002101 } else {
Maxime Chevallier56beda32018-02-28 10:14:13 +01002102 /* Shift 4 bytes for DSA tag or 8 bytes for EDSA tag*/
2103 mvpp2_prs_sram_shift_set(&pe, shift,
2104 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2105
Marcin Wojtas3f518502014-07-10 16:52:13 -03002106 /* Set result info bits to 'no vlans' */
2107 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
2108 MVPP2_PRS_RI_VLAN_MASK);
2109 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2110 }
2111
2112 /* Mask all ports */
2113 mvpp2_prs_tcam_port_map_set(&pe, 0);
2114 }
2115
2116 /* Update port mask */
2117 mvpp2_prs_tcam_port_set(&pe, port, add);
2118
2119 mvpp2_prs_hw_write(priv, &pe);
2120}
2121
2122/* Set entry for dsa ethertype */
2123static void mvpp2_prs_dsa_tag_ethertype_set(struct mvpp2 *priv, int port,
2124 bool add, bool tagged, bool extend)
2125{
2126 struct mvpp2_prs_entry pe;
2127 int tid, shift, port_mask;
2128
2129 if (extend) {
2130 tid = tagged ? MVPP2_PE_ETYPE_EDSA_TAGGED :
2131 MVPP2_PE_ETYPE_EDSA_UNTAGGED;
2132 port_mask = 0;
2133 shift = 8;
2134 } else {
2135 tid = tagged ? MVPP2_PE_ETYPE_DSA_TAGGED :
2136 MVPP2_PE_ETYPE_DSA_UNTAGGED;
2137 port_mask = MVPP2_PRS_PORT_MASK;
2138 shift = 4;
2139 }
2140
2141 if (priv->prs_shadow[tid].valid) {
2142 /* Entry exist - update port only */
2143 pe.index = tid;
2144 mvpp2_prs_hw_read(priv, &pe);
2145 } else {
2146 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002147 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002148 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
2149 pe.index = tid;
2150
2151 /* Set ethertype */
2152 mvpp2_prs_match_etype(&pe, 0, ETH_P_EDSA);
2153 mvpp2_prs_match_etype(&pe, 2, 0);
2154
2155 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DSA_MASK,
2156 MVPP2_PRS_RI_DSA_MASK);
2157 /* Shift ethertype + 2 byte reserved + tag*/
2158 mvpp2_prs_sram_shift_set(&pe, 2 + MVPP2_ETH_TYPE_LEN + shift,
2159 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2160
2161 /* Update shadow table */
2162 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
2163
2164 if (tagged) {
2165 /* Set tagged bit in DSA tag */
2166 mvpp2_prs_tcam_data_byte_set(&pe,
2167 MVPP2_ETH_TYPE_LEN + 2 + 3,
2168 MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
2169 MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
2170 /* Clear all ai bits for next iteration */
2171 mvpp2_prs_sram_ai_update(&pe, 0,
2172 MVPP2_PRS_SRAM_AI_MASK);
2173 /* If packet is tagged continue check vlans */
2174 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2175 } else {
2176 /* Set result info bits to 'no vlans' */
2177 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
2178 MVPP2_PRS_RI_VLAN_MASK);
2179 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2180 }
2181 /* Mask/unmask all ports, depending on dsa type */
2182 mvpp2_prs_tcam_port_map_set(&pe, port_mask);
2183 }
2184
2185 /* Update port mask */
2186 mvpp2_prs_tcam_port_set(&pe, port, add);
2187
2188 mvpp2_prs_hw_write(priv, &pe);
2189}
2190
2191/* Search for existing single/triple vlan entry */
2192static struct mvpp2_prs_entry *mvpp2_prs_vlan_find(struct mvpp2 *priv,
2193 unsigned short tpid, int ai)
2194{
2195 struct mvpp2_prs_entry *pe;
2196 int tid;
2197
2198 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2199 if (!pe)
2200 return NULL;
2201 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2202
2203 /* Go through the all entries with MVPP2_PRS_LU_VLAN */
2204 for (tid = MVPP2_PE_FIRST_FREE_TID;
2205 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
2206 unsigned int ri_bits, ai_bits;
2207 bool match;
2208
2209 if (!priv->prs_shadow[tid].valid ||
2210 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
2211 continue;
2212
2213 pe->index = tid;
2214
2215 mvpp2_prs_hw_read(priv, pe);
2216 match = mvpp2_prs_tcam_data_cmp(pe, 0, swab16(tpid));
2217 if (!match)
2218 continue;
2219
2220 /* Get vlan type */
2221 ri_bits = mvpp2_prs_sram_ri_get(pe);
2222 ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
2223
2224 /* Get current ai value from tcam */
2225 ai_bits = mvpp2_prs_tcam_ai_get(pe);
2226 /* Clear double vlan bit */
2227 ai_bits &= ~MVPP2_PRS_DBL_VLAN_AI_BIT;
2228
2229 if (ai != ai_bits)
2230 continue;
2231
2232 if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
2233 ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
2234 return pe;
2235 }
2236 kfree(pe);
2237
2238 return NULL;
2239}
2240
2241/* Add/update single/triple vlan entry */
2242static int mvpp2_prs_vlan_add(struct mvpp2 *priv, unsigned short tpid, int ai,
2243 unsigned int port_map)
2244{
2245 struct mvpp2_prs_entry *pe;
2246 int tid_aux, tid;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302247 int ret = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002248
2249 pe = mvpp2_prs_vlan_find(priv, tpid, ai);
2250
2251 if (!pe) {
2252 /* Create new tcam entry */
2253 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_LAST_FREE_TID,
2254 MVPP2_PE_FIRST_FREE_TID);
2255 if (tid < 0)
2256 return tid;
2257
2258 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2259 if (!pe)
2260 return -ENOMEM;
2261
2262 /* Get last double vlan tid */
2263 for (tid_aux = MVPP2_PE_LAST_FREE_TID;
2264 tid_aux >= MVPP2_PE_FIRST_FREE_TID; tid_aux--) {
2265 unsigned int ri_bits;
2266
2267 if (!priv->prs_shadow[tid_aux].valid ||
2268 priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
2269 continue;
2270
2271 pe->index = tid_aux;
2272 mvpp2_prs_hw_read(priv, pe);
2273 ri_bits = mvpp2_prs_sram_ri_get(pe);
2274 if ((ri_bits & MVPP2_PRS_RI_VLAN_MASK) ==
2275 MVPP2_PRS_RI_VLAN_DOUBLE)
2276 break;
2277 }
2278
Sudip Mukherjee43737472014-11-01 16:59:34 +05302279 if (tid <= tid_aux) {
2280 ret = -EINVAL;
Markus Elfringf9fd0e32017-04-17 13:50:35 +02002281 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302282 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002283
Markus Elfringbd6aaf52017-04-17 10:40:32 +02002284 memset(pe, 0, sizeof(*pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002285 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2286 pe->index = tid;
2287
2288 mvpp2_prs_match_etype(pe, 0, tpid);
2289
Maxime Chevallier56beda32018-02-28 10:14:13 +01002290 /* VLAN tag detected, proceed with VID filtering */
2291 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_VID);
2292
Marcin Wojtas3f518502014-07-10 16:52:13 -03002293 /* Clear all ai bits for next iteration */
2294 mvpp2_prs_sram_ai_update(pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2295
2296 if (ai == MVPP2_PRS_SINGLE_VLAN_AI) {
2297 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_SINGLE,
2298 MVPP2_PRS_RI_VLAN_MASK);
2299 } else {
2300 ai |= MVPP2_PRS_DBL_VLAN_AI_BIT;
2301 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_TRIPLE,
2302 MVPP2_PRS_RI_VLAN_MASK);
2303 }
2304 mvpp2_prs_tcam_ai_update(pe, ai, MVPP2_PRS_SRAM_AI_MASK);
2305
2306 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_VLAN);
2307 }
2308 /* Update ports' mask */
2309 mvpp2_prs_tcam_port_map_set(pe, port_map);
2310
2311 mvpp2_prs_hw_write(priv, pe);
Markus Elfringf9fd0e32017-04-17 13:50:35 +02002312free_pe:
Marcin Wojtas3f518502014-07-10 16:52:13 -03002313 kfree(pe);
2314
Sudip Mukherjee43737472014-11-01 16:59:34 +05302315 return ret;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002316}
2317
2318/* Get first free double vlan ai number */
2319static int mvpp2_prs_double_vlan_ai_free_get(struct mvpp2 *priv)
2320{
2321 int i;
2322
2323 for (i = 1; i < MVPP2_PRS_DBL_VLANS_MAX; i++) {
2324 if (!priv->prs_double_vlans[i])
2325 return i;
2326 }
2327
2328 return -EINVAL;
2329}
2330
2331/* Search for existing double vlan entry */
2332static struct mvpp2_prs_entry *mvpp2_prs_double_vlan_find(struct mvpp2 *priv,
2333 unsigned short tpid1,
2334 unsigned short tpid2)
2335{
2336 struct mvpp2_prs_entry *pe;
2337 int tid;
2338
2339 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2340 if (!pe)
2341 return NULL;
2342 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2343
2344 /* Go through the all entries with MVPP2_PRS_LU_VLAN */
2345 for (tid = MVPP2_PE_FIRST_FREE_TID;
2346 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
2347 unsigned int ri_mask;
2348 bool match;
2349
2350 if (!priv->prs_shadow[tid].valid ||
2351 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
2352 continue;
2353
2354 pe->index = tid;
2355 mvpp2_prs_hw_read(priv, pe);
2356
2357 match = mvpp2_prs_tcam_data_cmp(pe, 0, swab16(tpid1))
2358 && mvpp2_prs_tcam_data_cmp(pe, 4, swab16(tpid2));
2359
2360 if (!match)
2361 continue;
2362
2363 ri_mask = mvpp2_prs_sram_ri_get(pe) & MVPP2_PRS_RI_VLAN_MASK;
2364 if (ri_mask == MVPP2_PRS_RI_VLAN_DOUBLE)
2365 return pe;
2366 }
2367 kfree(pe);
2368
2369 return NULL;
2370}
2371
2372/* Add or update double vlan entry */
2373static int mvpp2_prs_double_vlan_add(struct mvpp2 *priv, unsigned short tpid1,
2374 unsigned short tpid2,
2375 unsigned int port_map)
2376{
2377 struct mvpp2_prs_entry *pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302378 int tid_aux, tid, ai, ret = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002379
2380 pe = mvpp2_prs_double_vlan_find(priv, tpid1, tpid2);
2381
2382 if (!pe) {
2383 /* Create new tcam entry */
2384 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2385 MVPP2_PE_LAST_FREE_TID);
2386 if (tid < 0)
2387 return tid;
2388
2389 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2390 if (!pe)
2391 return -ENOMEM;
2392
2393 /* Set ai value for new double vlan entry */
2394 ai = mvpp2_prs_double_vlan_ai_free_get(priv);
Sudip Mukherjee43737472014-11-01 16:59:34 +05302395 if (ai < 0) {
2396 ret = ai;
Markus Elfringc9a7e122017-04-17 13:03:49 +02002397 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302398 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002399
2400 /* Get first single/triple vlan tid */
2401 for (tid_aux = MVPP2_PE_FIRST_FREE_TID;
2402 tid_aux <= MVPP2_PE_LAST_FREE_TID; tid_aux++) {
2403 unsigned int ri_bits;
2404
2405 if (!priv->prs_shadow[tid_aux].valid ||
2406 priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
2407 continue;
2408
2409 pe->index = tid_aux;
2410 mvpp2_prs_hw_read(priv, pe);
2411 ri_bits = mvpp2_prs_sram_ri_get(pe);
2412 ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
2413 if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
2414 ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
2415 break;
2416 }
2417
Sudip Mukherjee43737472014-11-01 16:59:34 +05302418 if (tid >= tid_aux) {
2419 ret = -ERANGE;
Markus Elfringc9a7e122017-04-17 13:03:49 +02002420 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302421 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002422
Markus Elfringbd6aaf52017-04-17 10:40:32 +02002423 memset(pe, 0, sizeof(*pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002424 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2425 pe->index = tid;
2426
2427 priv->prs_double_vlans[ai] = true;
2428
2429 mvpp2_prs_match_etype(pe, 0, tpid1);
2430 mvpp2_prs_match_etype(pe, 4, tpid2);
2431
2432 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_VLAN);
Maxime Chevallier56beda32018-02-28 10:14:13 +01002433 /* Shift 4 bytes - skip outer vlan tag */
2434 mvpp2_prs_sram_shift_set(pe, MVPP2_VLAN_TAG_LEN,
Marcin Wojtas3f518502014-07-10 16:52:13 -03002435 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2436 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_DOUBLE,
2437 MVPP2_PRS_RI_VLAN_MASK);
2438 mvpp2_prs_sram_ai_update(pe, ai | MVPP2_PRS_DBL_VLAN_AI_BIT,
2439 MVPP2_PRS_SRAM_AI_MASK);
2440
2441 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_VLAN);
2442 }
2443
2444 /* Update ports' mask */
2445 mvpp2_prs_tcam_port_map_set(pe, port_map);
2446 mvpp2_prs_hw_write(priv, pe);
Markus Elfringc9a7e122017-04-17 13:03:49 +02002447free_pe:
Marcin Wojtas3f518502014-07-10 16:52:13 -03002448 kfree(pe);
Sudip Mukherjee43737472014-11-01 16:59:34 +05302449 return ret;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002450}
2451
2452/* IPv4 header parsing for fragmentation and L4 offset */
2453static int mvpp2_prs_ip4_proto(struct mvpp2 *priv, unsigned short proto,
2454 unsigned int ri, unsigned int ri_mask)
2455{
2456 struct mvpp2_prs_entry pe;
2457 int tid;
2458
2459 if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
2460 (proto != IPPROTO_IGMP))
2461 return -EINVAL;
2462
Stefan Chulskiaff3da32017-09-25 14:59:46 +02002463 /* Not fragmented packet */
Marcin Wojtas3f518502014-07-10 16:52:13 -03002464 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2465 MVPP2_PE_LAST_FREE_TID);
2466 if (tid < 0)
2467 return tid;
2468
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002469 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002470 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
2471 pe.index = tid;
2472
2473 /* Set next lu to IPv4 */
2474 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2475 mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2476 /* Set L4 offset */
2477 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
2478 sizeof(struct iphdr) - 4,
2479 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2480 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
2481 MVPP2_PRS_IPV4_DIP_AI_BIT);
Stefan Chulskiaff3da32017-09-25 14:59:46 +02002482 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask | MVPP2_PRS_RI_IP_FRAG_MASK);
2483
2484 mvpp2_prs_tcam_data_byte_set(&pe, 2, 0x00,
2485 MVPP2_PRS_TCAM_PROTO_MASK_L);
2486 mvpp2_prs_tcam_data_byte_set(&pe, 3, 0x00,
2487 MVPP2_PRS_TCAM_PROTO_MASK);
Marcin Wojtas3f518502014-07-10 16:52:13 -03002488
2489 mvpp2_prs_tcam_data_byte_set(&pe, 5, proto, MVPP2_PRS_TCAM_PROTO_MASK);
2490 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
2491 /* Unmask all ports */
2492 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2493
2494 /* Update shadow table and hw entry */
2495 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2496 mvpp2_prs_hw_write(priv, &pe);
2497
Stefan Chulskiaff3da32017-09-25 14:59:46 +02002498 /* Fragmented packet */
Marcin Wojtas3f518502014-07-10 16:52:13 -03002499 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2500 MVPP2_PE_LAST_FREE_TID);
2501 if (tid < 0)
2502 return tid;
2503
2504 pe.index = tid;
2505 /* Clear ri before updating */
2506 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2507 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2508 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
2509
Stefan Chulskiaff3da32017-09-25 14:59:46 +02002510 mvpp2_prs_sram_ri_update(&pe, ri | MVPP2_PRS_RI_IP_FRAG_TRUE,
2511 ri_mask | MVPP2_PRS_RI_IP_FRAG_MASK);
2512
2513 mvpp2_prs_tcam_data_byte_set(&pe, 2, 0x00, 0x0);
2514 mvpp2_prs_tcam_data_byte_set(&pe, 3, 0x00, 0x0);
Marcin Wojtas3f518502014-07-10 16:52:13 -03002515
2516 /* Update shadow table and hw entry */
2517 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2518 mvpp2_prs_hw_write(priv, &pe);
2519
2520 return 0;
2521}
2522
2523/* IPv4 L3 multicast or broadcast */
2524static int mvpp2_prs_ip4_cast(struct mvpp2 *priv, unsigned short l3_cast)
2525{
2526 struct mvpp2_prs_entry pe;
2527 int mask, tid;
2528
2529 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2530 MVPP2_PE_LAST_FREE_TID);
2531 if (tid < 0)
2532 return tid;
2533
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002534 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002535 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
2536 pe.index = tid;
2537
2538 switch (l3_cast) {
2539 case MVPP2_PRS_L3_MULTI_CAST:
2540 mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV4_MC,
2541 MVPP2_PRS_IPV4_MC_MASK);
2542 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
2543 MVPP2_PRS_RI_L3_ADDR_MASK);
2544 break;
2545 case MVPP2_PRS_L3_BROAD_CAST:
2546 mask = MVPP2_PRS_IPV4_BC_MASK;
2547 mvpp2_prs_tcam_data_byte_set(&pe, 0, mask, mask);
2548 mvpp2_prs_tcam_data_byte_set(&pe, 1, mask, mask);
2549 mvpp2_prs_tcam_data_byte_set(&pe, 2, mask, mask);
2550 mvpp2_prs_tcam_data_byte_set(&pe, 3, mask, mask);
2551 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_BCAST,
2552 MVPP2_PRS_RI_L3_ADDR_MASK);
2553 break;
2554 default:
2555 return -EINVAL;
2556 }
2557
2558 /* Finished: go to flowid generation */
2559 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2560 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2561
2562 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
2563 MVPP2_PRS_IPV4_DIP_AI_BIT);
2564 /* Unmask all ports */
2565 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2566
2567 /* Update shadow table and hw entry */
2568 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2569 mvpp2_prs_hw_write(priv, &pe);
2570
2571 return 0;
2572}
2573
2574/* Set entries for protocols over IPv6 */
2575static int mvpp2_prs_ip6_proto(struct mvpp2 *priv, unsigned short proto,
2576 unsigned int ri, unsigned int ri_mask)
2577{
2578 struct mvpp2_prs_entry pe;
2579 int tid;
2580
2581 if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
2582 (proto != IPPROTO_ICMPV6) && (proto != IPPROTO_IPIP))
2583 return -EINVAL;
2584
2585 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2586 MVPP2_PE_LAST_FREE_TID);
2587 if (tid < 0)
2588 return tid;
2589
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002590 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002591 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2592 pe.index = tid;
2593
2594 /* Finished: go to flowid generation */
2595 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2596 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2597 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
2598 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
2599 sizeof(struct ipv6hdr) - 6,
2600 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2601
2602 mvpp2_prs_tcam_data_byte_set(&pe, 0, proto, MVPP2_PRS_TCAM_PROTO_MASK);
2603 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
2604 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2605 /* Unmask all ports */
2606 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2607
2608 /* Write HW */
2609 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
2610 mvpp2_prs_hw_write(priv, &pe);
2611
2612 return 0;
2613}
2614
2615/* IPv6 L3 multicast entry */
2616static int mvpp2_prs_ip6_cast(struct mvpp2 *priv, unsigned short l3_cast)
2617{
2618 struct mvpp2_prs_entry pe;
2619 int tid;
2620
2621 if (l3_cast != MVPP2_PRS_L3_MULTI_CAST)
2622 return -EINVAL;
2623
2624 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2625 MVPP2_PE_LAST_FREE_TID);
2626 if (tid < 0)
2627 return tid;
2628
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002629 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002630 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2631 pe.index = tid;
2632
2633 /* Finished: go to flowid generation */
2634 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2635 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
2636 MVPP2_PRS_RI_L3_ADDR_MASK);
2637 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
2638 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2639 /* Shift back to IPv6 NH */
2640 mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2641
2642 mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV6_MC,
2643 MVPP2_PRS_IPV6_MC_MASK);
2644 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2645 /* Unmask all ports */
2646 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2647
2648 /* Update shadow table and hw entry */
2649 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
2650 mvpp2_prs_hw_write(priv, &pe);
2651
2652 return 0;
2653}
2654
2655/* Parser per-port initialization */
2656static void mvpp2_prs_hw_port_init(struct mvpp2 *priv, int port, int lu_first,
2657 int lu_max, int offset)
2658{
2659 u32 val;
2660
2661 /* Set lookup ID */
2662 val = mvpp2_read(priv, MVPP2_PRS_INIT_LOOKUP_REG);
2663 val &= ~MVPP2_PRS_PORT_LU_MASK(port);
2664 val |= MVPP2_PRS_PORT_LU_VAL(port, lu_first);
2665 mvpp2_write(priv, MVPP2_PRS_INIT_LOOKUP_REG, val);
2666
2667 /* Set maximum number of loops for packet received from port */
2668 val = mvpp2_read(priv, MVPP2_PRS_MAX_LOOP_REG(port));
2669 val &= ~MVPP2_PRS_MAX_LOOP_MASK(port);
2670 val |= MVPP2_PRS_MAX_LOOP_VAL(port, lu_max);
2671 mvpp2_write(priv, MVPP2_PRS_MAX_LOOP_REG(port), val);
2672
2673 /* Set initial offset for packet header extraction for the first
2674 * searching loop
2675 */
2676 val = mvpp2_read(priv, MVPP2_PRS_INIT_OFFS_REG(port));
2677 val &= ~MVPP2_PRS_INIT_OFF_MASK(port);
2678 val |= MVPP2_PRS_INIT_OFF_VAL(port, offset);
2679 mvpp2_write(priv, MVPP2_PRS_INIT_OFFS_REG(port), val);
2680}
2681
2682/* Default flow entries initialization for all ports */
2683static void mvpp2_prs_def_flow_init(struct mvpp2 *priv)
2684{
2685 struct mvpp2_prs_entry pe;
2686 int port;
2687
2688 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002689 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002690 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2691 pe.index = MVPP2_PE_FIRST_DEFAULT_FLOW - port;
2692
2693 /* Mask all ports */
2694 mvpp2_prs_tcam_port_map_set(&pe, 0);
2695
2696 /* Set flow ID*/
2697 mvpp2_prs_sram_ai_update(&pe, port, MVPP2_PRS_FLOW_ID_MASK);
2698 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
2699
2700 /* Update shadow table and hw entry */
2701 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_FLOWS);
2702 mvpp2_prs_hw_write(priv, &pe);
2703 }
2704}
2705
2706/* Set default entry for Marvell Header field */
2707static void mvpp2_prs_mh_init(struct mvpp2 *priv)
2708{
2709 struct mvpp2_prs_entry pe;
2710
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002711 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002712
2713 pe.index = MVPP2_PE_MH_DEFAULT;
2714 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MH);
2715 mvpp2_prs_sram_shift_set(&pe, MVPP2_MH_SIZE,
2716 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2717 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_MAC);
2718
2719 /* Unmask all ports */
2720 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2721
2722 /* Update shadow table and hw entry */
2723 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MH);
2724 mvpp2_prs_hw_write(priv, &pe);
2725}
2726
2727/* Set default entires (place holder) for promiscuous, non-promiscuous and
2728 * multicast MAC addresses
2729 */
2730static void mvpp2_prs_mac_init(struct mvpp2 *priv)
2731{
2732 struct mvpp2_prs_entry pe;
2733
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002734 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002735
2736 /* Non-promiscuous mode for all ports - DROP unknown packets */
2737 pe.index = MVPP2_PE_MAC_NON_PROMISCUOUS;
2738 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
2739
2740 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
2741 MVPP2_PRS_RI_DROP_MASK);
2742 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2743 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2744
2745 /* Unmask all ports */
2746 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2747
2748 /* Update shadow table and hw entry */
2749 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2750 mvpp2_prs_hw_write(priv, &pe);
2751
Maxime Chevallier10fea262018-03-07 15:18:04 +01002752 /* Create dummy entries for drop all and promiscuous modes */
Marcin Wojtas3f518502014-07-10 16:52:13 -03002753 mvpp2_prs_mac_drop_all_set(priv, 0, false);
Maxime Chevallier10fea262018-03-07 15:18:04 +01002754 mvpp2_prs_mac_promisc_set(priv, 0, MVPP2_PRS_L2_UNI_CAST, false);
2755 mvpp2_prs_mac_promisc_set(priv, 0, MVPP2_PRS_L2_MULTI_CAST, false);
Marcin Wojtas3f518502014-07-10 16:52:13 -03002756}
2757
2758/* Set default entries for various types of dsa packets */
2759static void mvpp2_prs_dsa_init(struct mvpp2 *priv)
2760{
2761 struct mvpp2_prs_entry pe;
2762
2763 /* None tagged EDSA entry - place holder */
2764 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
2765 MVPP2_PRS_EDSA);
2766
2767 /* Tagged EDSA entry - place holder */
2768 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
2769
2770 /* None tagged DSA entry - place holder */
2771 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
2772 MVPP2_PRS_DSA);
2773
2774 /* Tagged DSA entry - place holder */
2775 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
2776
2777 /* None tagged EDSA ethertype entry - place holder*/
2778 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
2779 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
2780
2781 /* Tagged EDSA ethertype entry - place holder*/
2782 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
2783 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
2784
2785 /* None tagged DSA ethertype entry */
2786 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
2787 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
2788
2789 /* Tagged DSA ethertype entry */
2790 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
2791 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
2792
2793 /* Set default entry, in case DSA or EDSA tag not found */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002794 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002795 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
2796 pe.index = MVPP2_PE_DSA_DEFAULT;
2797 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2798
2799 /* Shift 0 bytes */
2800 mvpp2_prs_sram_shift_set(&pe, 0, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2801 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2802
2803 /* Clear all sram ai bits for next iteration */
2804 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2805
2806 /* Unmask all ports */
2807 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2808
2809 mvpp2_prs_hw_write(priv, &pe);
2810}
2811
Maxime Chevallier56beda32018-02-28 10:14:13 +01002812/* Initialize parser entries for VID filtering */
2813static void mvpp2_prs_vid_init(struct mvpp2 *priv)
2814{
2815 struct mvpp2_prs_entry pe;
2816
2817 memset(&pe, 0, sizeof(pe));
2818
2819 /* Set default vid entry */
2820 pe.index = MVPP2_PE_VID_FLTR_DEFAULT;
2821 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VID);
2822
2823 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_EDSA_VID_AI_BIT);
2824
2825 /* Skip VLAN header - Set offset to 4 bytes */
2826 mvpp2_prs_sram_shift_set(&pe, MVPP2_VLAN_TAG_LEN,
2827 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2828
2829 /* Clear all ai bits for next iteration */
2830 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2831
2832 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2833
2834 /* Unmask all ports */
2835 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2836
2837 /* Update shadow table and hw entry */
2838 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VID);
2839 mvpp2_prs_hw_write(priv, &pe);
2840
2841 /* Set default vid entry for extended DSA*/
2842 memset(&pe, 0, sizeof(pe));
2843
2844 /* Set default vid entry */
2845 pe.index = MVPP2_PE_VID_EDSA_FLTR_DEFAULT;
2846 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VID);
2847
2848 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_EDSA_VID_AI_BIT,
2849 MVPP2_PRS_EDSA_VID_AI_BIT);
2850
2851 /* Skip VLAN header - Set offset to 8 bytes */
2852 mvpp2_prs_sram_shift_set(&pe, MVPP2_VLAN_TAG_EDSA_LEN,
2853 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2854
2855 /* Clear all ai bits for next iteration */
2856 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2857
2858 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2859
2860 /* Unmask all ports */
2861 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2862
2863 /* Update shadow table and hw entry */
2864 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VID);
2865 mvpp2_prs_hw_write(priv, &pe);
2866}
2867
Marcin Wojtas3f518502014-07-10 16:52:13 -03002868/* Match basic ethertypes */
2869static int mvpp2_prs_etype_init(struct mvpp2 *priv)
2870{
2871 struct mvpp2_prs_entry pe;
2872 int tid;
2873
2874 /* Ethertype: PPPoE */
2875 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2876 MVPP2_PE_LAST_FREE_TID);
2877 if (tid < 0)
2878 return tid;
2879
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002880 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002881 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2882 pe.index = tid;
2883
2884 mvpp2_prs_match_etype(&pe, 0, ETH_P_PPP_SES);
2885
2886 mvpp2_prs_sram_shift_set(&pe, MVPP2_PPPOE_HDR_SIZE,
2887 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2888 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2889 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_PPPOE_MASK,
2890 MVPP2_PRS_RI_PPPOE_MASK);
2891
2892 /* Update shadow table and hw entry */
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_PPPOE_MASK,
2897 MVPP2_PRS_RI_PPPOE_MASK);
2898 mvpp2_prs_hw_write(priv, &pe);
2899
2900 /* Ethertype: ARP */
2901 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2902 MVPP2_PE_LAST_FREE_TID);
2903 if (tid < 0)
2904 return tid;
2905
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002906 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002907 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2908 pe.index = tid;
2909
2910 mvpp2_prs_match_etype(&pe, 0, ETH_P_ARP);
2911
2912 /* Generate flow in the next iteration*/
2913 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2914 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2915 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_ARP,
2916 MVPP2_PRS_RI_L3_PROTO_MASK);
2917 /* Set L3 offset */
2918 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2919 MVPP2_ETH_TYPE_LEN,
2920 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2921
2922 /* Update shadow table and hw entry */
2923 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2924 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2925 priv->prs_shadow[pe.index].finish = true;
2926 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_ARP,
2927 MVPP2_PRS_RI_L3_PROTO_MASK);
2928 mvpp2_prs_hw_write(priv, &pe);
2929
2930 /* Ethertype: LBTD */
2931 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2932 MVPP2_PE_LAST_FREE_TID);
2933 if (tid < 0)
2934 return tid;
2935
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002936 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002937 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2938 pe.index = tid;
2939
2940 mvpp2_prs_match_etype(&pe, 0, MVPP2_IP_LBDT_TYPE);
2941
2942 /* Generate flow in the next iteration*/
2943 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2944 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2945 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2946 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2947 MVPP2_PRS_RI_CPU_CODE_MASK |
2948 MVPP2_PRS_RI_UDF3_MASK);
2949 /* Set L3 offset */
2950 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2951 MVPP2_ETH_TYPE_LEN,
2952 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2953
2954 /* Update shadow table and hw entry */
2955 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2956 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2957 priv->prs_shadow[pe.index].finish = true;
2958 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2959 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2960 MVPP2_PRS_RI_CPU_CODE_MASK |
2961 MVPP2_PRS_RI_UDF3_MASK);
2962 mvpp2_prs_hw_write(priv, &pe);
2963
2964 /* Ethertype: IPv4 without options */
2965 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2966 MVPP2_PE_LAST_FREE_TID);
2967 if (tid < 0)
2968 return tid;
2969
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002970 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002971 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2972 pe.index = tid;
2973
2974 mvpp2_prs_match_etype(&pe, 0, ETH_P_IP);
2975 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2976 MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
2977 MVPP2_PRS_IPV4_HEAD_MASK |
2978 MVPP2_PRS_IPV4_IHL_MASK);
2979
2980 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2981 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
2982 MVPP2_PRS_RI_L3_PROTO_MASK);
2983 /* Skip eth_type + 4 bytes of IP header */
2984 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
2985 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2986 /* Set L3 offset */
2987 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2988 MVPP2_ETH_TYPE_LEN,
2989 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2990
2991 /* Update shadow table and hw entry */
2992 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2993 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2994 priv->prs_shadow[pe.index].finish = false;
2995 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4,
2996 MVPP2_PRS_RI_L3_PROTO_MASK);
2997 mvpp2_prs_hw_write(priv, &pe);
2998
2999 /* Ethertype: IPv4 with options */
3000 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3001 MVPP2_PE_LAST_FREE_TID);
3002 if (tid < 0)
3003 return tid;
3004
3005 pe.index = tid;
3006
3007 /* Clear tcam data before updating */
3008 pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(MVPP2_ETH_TYPE_LEN)] = 0x0;
3009 pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(MVPP2_ETH_TYPE_LEN)] = 0x0;
3010
3011 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
3012 MVPP2_PRS_IPV4_HEAD,
3013 MVPP2_PRS_IPV4_HEAD_MASK);
3014
3015 /* Clear ri before updating */
3016 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
3017 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
3018 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
3019 MVPP2_PRS_RI_L3_PROTO_MASK);
3020
3021 /* Update shadow table and hw entry */
3022 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
3023 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
3024 priv->prs_shadow[pe.index].finish = false;
3025 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4_OPT,
3026 MVPP2_PRS_RI_L3_PROTO_MASK);
3027 mvpp2_prs_hw_write(priv, &pe);
3028
3029 /* Ethertype: IPv6 without options */
3030 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3031 MVPP2_PE_LAST_FREE_TID);
3032 if (tid < 0)
3033 return tid;
3034
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003035 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003036 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
3037 pe.index = tid;
3038
3039 mvpp2_prs_match_etype(&pe, 0, ETH_P_IPV6);
3040
3041 /* Skip DIP of IPV6 header */
3042 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 8 +
3043 MVPP2_MAX_L3_ADDR_SIZE,
3044 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3045 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
3046 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
3047 MVPP2_PRS_RI_L3_PROTO_MASK);
3048 /* Set L3 offset */
3049 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
3050 MVPP2_ETH_TYPE_LEN,
3051 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3052
3053 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
3054 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
3055 priv->prs_shadow[pe.index].finish = false;
3056 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP6,
3057 MVPP2_PRS_RI_L3_PROTO_MASK);
3058 mvpp2_prs_hw_write(priv, &pe);
3059
3060 /* Default entry for MVPP2_PRS_LU_L2 - Unknown ethtype */
3061 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3062 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
3063 pe.index = MVPP2_PE_ETH_TYPE_UN;
3064
3065 /* Unmask all ports */
3066 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3067
3068 /* Generate flow in the next iteration*/
3069 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3070 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3071 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
3072 MVPP2_PRS_RI_L3_PROTO_MASK);
3073 /* Set L3 offset even it's unknown L3 */
3074 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
3075 MVPP2_ETH_TYPE_LEN,
3076 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3077
3078 /* Update shadow table and hw entry */
3079 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
3080 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
3081 priv->prs_shadow[pe.index].finish = true;
3082 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_UN,
3083 MVPP2_PRS_RI_L3_PROTO_MASK);
3084 mvpp2_prs_hw_write(priv, &pe);
3085
3086 return 0;
3087}
3088
3089/* Configure vlan entries and detect up to 2 successive VLAN tags.
3090 * Possible options:
3091 * 0x8100, 0x88A8
3092 * 0x8100, 0x8100
3093 * 0x8100
3094 * 0x88A8
3095 */
3096static int mvpp2_prs_vlan_init(struct platform_device *pdev, struct mvpp2 *priv)
3097{
3098 struct mvpp2_prs_entry pe;
3099 int err;
3100
3101 priv->prs_double_vlans = devm_kcalloc(&pdev->dev, sizeof(bool),
3102 MVPP2_PRS_DBL_VLANS_MAX,
3103 GFP_KERNEL);
3104 if (!priv->prs_double_vlans)
3105 return -ENOMEM;
3106
3107 /* Double VLAN: 0x8100, 0x88A8 */
3108 err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021AD,
3109 MVPP2_PRS_PORT_MASK);
3110 if (err)
3111 return err;
3112
3113 /* Double VLAN: 0x8100, 0x8100 */
3114 err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021Q,
3115 MVPP2_PRS_PORT_MASK);
3116 if (err)
3117 return err;
3118
3119 /* Single VLAN: 0x88a8 */
3120 err = mvpp2_prs_vlan_add(priv, ETH_P_8021AD, MVPP2_PRS_SINGLE_VLAN_AI,
3121 MVPP2_PRS_PORT_MASK);
3122 if (err)
3123 return err;
3124
3125 /* Single VLAN: 0x8100 */
3126 err = mvpp2_prs_vlan_add(priv, ETH_P_8021Q, MVPP2_PRS_SINGLE_VLAN_AI,
3127 MVPP2_PRS_PORT_MASK);
3128 if (err)
3129 return err;
3130
3131 /* Set default double vlan entry */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003132 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003133 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
3134 pe.index = MVPP2_PE_VLAN_DBL;
3135
Maxime Chevallier56beda32018-02-28 10:14:13 +01003136 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VID);
3137
Marcin Wojtas3f518502014-07-10 16:52:13 -03003138 /* Clear ai for next iterations */
3139 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
3140 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_DOUBLE,
3141 MVPP2_PRS_RI_VLAN_MASK);
3142
3143 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_DBL_VLAN_AI_BIT,
3144 MVPP2_PRS_DBL_VLAN_AI_BIT);
3145 /* Unmask all ports */
3146 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3147
3148 /* Update shadow table and hw entry */
3149 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
3150 mvpp2_prs_hw_write(priv, &pe);
3151
3152 /* Set default vlan none entry */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003153 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003154 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
3155 pe.index = MVPP2_PE_VLAN_NONE;
3156
3157 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
3158 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
3159 MVPP2_PRS_RI_VLAN_MASK);
3160
3161 /* Unmask all ports */
3162 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3163
3164 /* Update shadow table and hw entry */
3165 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
3166 mvpp2_prs_hw_write(priv, &pe);
3167
3168 return 0;
3169}
3170
3171/* Set entries for PPPoE ethertype */
3172static int mvpp2_prs_pppoe_init(struct mvpp2 *priv)
3173{
3174 struct mvpp2_prs_entry pe;
3175 int tid;
3176
3177 /* IPv4 over PPPoE with options */
3178 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3179 MVPP2_PE_LAST_FREE_TID);
3180 if (tid < 0)
3181 return tid;
3182
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003183 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003184 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
3185 pe.index = tid;
3186
3187 mvpp2_prs_match_etype(&pe, 0, PPP_IP);
3188
3189 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
3190 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
3191 MVPP2_PRS_RI_L3_PROTO_MASK);
3192 /* Skip eth_type + 4 bytes of IP header */
3193 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
3194 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3195 /* Set L3 offset */
3196 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
3197 MVPP2_ETH_TYPE_LEN,
3198 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3199
3200 /* Update shadow table and hw entry */
3201 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3202 mvpp2_prs_hw_write(priv, &pe);
3203
3204 /* IPv4 over PPPoE without options */
3205 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3206 MVPP2_PE_LAST_FREE_TID);
3207 if (tid < 0)
3208 return tid;
3209
3210 pe.index = tid;
3211
3212 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
3213 MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
3214 MVPP2_PRS_IPV4_HEAD_MASK |
3215 MVPP2_PRS_IPV4_IHL_MASK);
3216
3217 /* Clear ri before updating */
3218 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
3219 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
3220 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
3221 MVPP2_PRS_RI_L3_PROTO_MASK);
3222
3223 /* Update shadow table and hw entry */
3224 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3225 mvpp2_prs_hw_write(priv, &pe);
3226
3227 /* IPv6 over PPPoE */
3228 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3229 MVPP2_PE_LAST_FREE_TID);
3230 if (tid < 0)
3231 return tid;
3232
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003233 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003234 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
3235 pe.index = tid;
3236
3237 mvpp2_prs_match_etype(&pe, 0, PPP_IPV6);
3238
3239 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
3240 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
3241 MVPP2_PRS_RI_L3_PROTO_MASK);
3242 /* Skip eth_type + 4 bytes of IPv6 header */
3243 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
3244 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3245 /* Set L3 offset */
3246 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
3247 MVPP2_ETH_TYPE_LEN,
3248 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3249
3250 /* Update shadow table and hw entry */
3251 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3252 mvpp2_prs_hw_write(priv, &pe);
3253
3254 /* Non-IP over PPPoE */
3255 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3256 MVPP2_PE_LAST_FREE_TID);
3257 if (tid < 0)
3258 return tid;
3259
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003260 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003261 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
3262 pe.index = tid;
3263
3264 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
3265 MVPP2_PRS_RI_L3_PROTO_MASK);
3266
3267 /* Finished: go to flowid generation */
3268 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3269 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3270 /* Set L3 offset even if it's unknown L3 */
3271 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
3272 MVPP2_ETH_TYPE_LEN,
3273 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3274
3275 /* Update shadow table and hw entry */
3276 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3277 mvpp2_prs_hw_write(priv, &pe);
3278
3279 return 0;
3280}
3281
3282/* Initialize entries for IPv4 */
3283static int mvpp2_prs_ip4_init(struct mvpp2 *priv)
3284{
3285 struct mvpp2_prs_entry pe;
3286 int err;
3287
3288 /* Set entries for TCP, UDP and IGMP over IPv4 */
3289 err = mvpp2_prs_ip4_proto(priv, IPPROTO_TCP, MVPP2_PRS_RI_L4_TCP,
3290 MVPP2_PRS_RI_L4_PROTO_MASK);
3291 if (err)
3292 return err;
3293
3294 err = mvpp2_prs_ip4_proto(priv, IPPROTO_UDP, MVPP2_PRS_RI_L4_UDP,
3295 MVPP2_PRS_RI_L4_PROTO_MASK);
3296 if (err)
3297 return err;
3298
3299 err = mvpp2_prs_ip4_proto(priv, IPPROTO_IGMP,
3300 MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
3301 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
3302 MVPP2_PRS_RI_CPU_CODE_MASK |
3303 MVPP2_PRS_RI_UDF3_MASK);
3304 if (err)
3305 return err;
3306
3307 /* IPv4 Broadcast */
3308 err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_BROAD_CAST);
3309 if (err)
3310 return err;
3311
3312 /* IPv4 Multicast */
3313 err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
3314 if (err)
3315 return err;
3316
3317 /* Default IPv4 entry for unknown protocols */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003318 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003319 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
3320 pe.index = MVPP2_PE_IP4_PROTO_UN;
3321
3322 /* Set next lu to IPv4 */
3323 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
3324 mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3325 /* Set L4 offset */
3326 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
3327 sizeof(struct iphdr) - 4,
3328 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3329 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
3330 MVPP2_PRS_IPV4_DIP_AI_BIT);
3331 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3332 MVPP2_PRS_RI_L4_PROTO_MASK);
3333
3334 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
3335 /* Unmask all ports */
3336 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3337
3338 /* Update shadow table and hw entry */
3339 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3340 mvpp2_prs_hw_write(priv, &pe);
3341
3342 /* Default IPv4 entry for unicast address */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003343 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003344 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
3345 pe.index = MVPP2_PE_IP4_ADDR_UN;
3346
3347 /* Finished: go to flowid generation */
3348 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3349 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3350 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
3351 MVPP2_PRS_RI_L3_ADDR_MASK);
3352
3353 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
3354 MVPP2_PRS_IPV4_DIP_AI_BIT);
3355 /* Unmask all ports */
3356 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3357
3358 /* Update shadow table and hw entry */
3359 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3360 mvpp2_prs_hw_write(priv, &pe);
3361
3362 return 0;
3363}
3364
3365/* Initialize entries for IPv6 */
3366static int mvpp2_prs_ip6_init(struct mvpp2 *priv)
3367{
3368 struct mvpp2_prs_entry pe;
3369 int tid, err;
3370
3371 /* Set entries for TCP, UDP and ICMP over IPv6 */
3372 err = mvpp2_prs_ip6_proto(priv, IPPROTO_TCP,
3373 MVPP2_PRS_RI_L4_TCP,
3374 MVPP2_PRS_RI_L4_PROTO_MASK);
3375 if (err)
3376 return err;
3377
3378 err = mvpp2_prs_ip6_proto(priv, IPPROTO_UDP,
3379 MVPP2_PRS_RI_L4_UDP,
3380 MVPP2_PRS_RI_L4_PROTO_MASK);
3381 if (err)
3382 return err;
3383
3384 err = mvpp2_prs_ip6_proto(priv, IPPROTO_ICMPV6,
3385 MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
3386 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
3387 MVPP2_PRS_RI_CPU_CODE_MASK |
3388 MVPP2_PRS_RI_UDF3_MASK);
3389 if (err)
3390 return err;
3391
3392 /* IPv4 is the last header. This is similar case as 6-TCP or 17-UDP */
3393 /* Result Info: UDF7=1, DS lite */
3394 err = mvpp2_prs_ip6_proto(priv, IPPROTO_IPIP,
3395 MVPP2_PRS_RI_UDF7_IP6_LITE,
3396 MVPP2_PRS_RI_UDF7_MASK);
3397 if (err)
3398 return err;
3399
3400 /* IPv6 multicast */
3401 err = mvpp2_prs_ip6_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
3402 if (err)
3403 return err;
3404
3405 /* Entry for checking hop limit */
3406 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3407 MVPP2_PE_LAST_FREE_TID);
3408 if (tid < 0)
3409 return tid;
3410
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003411 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003412 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3413 pe.index = tid;
3414
3415 /* Finished: go to flowid generation */
3416 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3417 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3418 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN |
3419 MVPP2_PRS_RI_DROP_MASK,
3420 MVPP2_PRS_RI_L3_PROTO_MASK |
3421 MVPP2_PRS_RI_DROP_MASK);
3422
3423 mvpp2_prs_tcam_data_byte_set(&pe, 1, 0x00, MVPP2_PRS_IPV6_HOP_MASK);
3424 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3425 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3426
3427 /* Update shadow table and hw entry */
3428 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3429 mvpp2_prs_hw_write(priv, &pe);
3430
3431 /* Default IPv6 entry for unknown protocols */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003432 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003433 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3434 pe.index = MVPP2_PE_IP6_PROTO_UN;
3435
3436 /* Finished: go to flowid generation */
3437 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3438 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3439 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3440 MVPP2_PRS_RI_L4_PROTO_MASK);
3441 /* Set L4 offset relatively to our current place */
3442 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
3443 sizeof(struct ipv6hdr) - 4,
3444 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3445
3446 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3447 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3448 /* Unmask all ports */
3449 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3450
3451 /* Update shadow table and hw entry */
3452 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3453 mvpp2_prs_hw_write(priv, &pe);
3454
3455 /* Default IPv6 entry for unknown ext protocols */
3456 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3457 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3458 pe.index = MVPP2_PE_IP6_EXT_PROTO_UN;
3459
3460 /* Finished: go to flowid generation */
3461 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3462 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3463 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3464 MVPP2_PRS_RI_L4_PROTO_MASK);
3465
3466 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_EXT_AI_BIT,
3467 MVPP2_PRS_IPV6_EXT_AI_BIT);
3468 /* Unmask all ports */
3469 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3470
3471 /* Update shadow table and hw entry */
3472 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3473 mvpp2_prs_hw_write(priv, &pe);
3474
3475 /* Default IPv6 entry for unicast address */
3476 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3477 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3478 pe.index = MVPP2_PE_IP6_ADDR_UN;
3479
3480 /* Finished: go to IPv6 again */
3481 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
3482 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
3483 MVPP2_PRS_RI_L3_ADDR_MASK);
3484 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3485 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3486 /* Shift back to IPV6 NH */
3487 mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3488
3489 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3490 /* Unmask all ports */
3491 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3492
3493 /* Update shadow table and hw entry */
3494 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
3495 mvpp2_prs_hw_write(priv, &pe);
3496
3497 return 0;
3498}
3499
Maxime Chevallier56beda32018-02-28 10:14:13 +01003500/* Find tcam entry with matched pair <vid,port> */
3501static int mvpp2_prs_vid_range_find(struct mvpp2 *priv, int pmap, u16 vid,
3502 u16 mask)
3503{
3504 unsigned char byte[2], enable[2];
3505 struct mvpp2_prs_entry pe;
3506 u16 rvid, rmask;
3507 int tid;
3508
3509 /* Go through the all entries with MVPP2_PRS_LU_VID */
3510 for (tid = MVPP2_PE_VID_FILT_RANGE_START;
3511 tid <= MVPP2_PE_VID_FILT_RANGE_END; tid++) {
3512 if (!priv->prs_shadow[tid].valid ||
3513 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VID)
3514 continue;
3515
3516 pe.index = tid;
3517
3518 mvpp2_prs_hw_read(priv, &pe);
3519 mvpp2_prs_tcam_data_byte_get(&pe, 2, &byte[0], &enable[0]);
3520 mvpp2_prs_tcam_data_byte_get(&pe, 3, &byte[1], &enable[1]);
3521
3522 rvid = ((byte[0] & 0xf) << 8) + byte[1];
3523 rmask = ((enable[0] & 0xf) << 8) + enable[1];
3524
3525 if (rvid != vid || rmask != mask)
3526 continue;
3527
3528 return tid;
3529 }
3530
3531 return 0;
3532}
3533
3534/* Write parser entry for VID filtering */
3535static int mvpp2_prs_vid_entry_add(struct mvpp2_port *port, u16 vid)
3536{
3537 unsigned int vid_start = MVPP2_PE_VID_FILT_RANGE_START +
3538 port->id * MVPP2_PRS_VLAN_FILT_MAX;
3539 unsigned int mask = 0xfff, reg_val, shift;
3540 struct mvpp2 *priv = port->priv;
3541 struct mvpp2_prs_entry pe;
3542 int tid;
3543
3544 /* Scan TCAM and see if entry with this <vid,port> already exist */
3545 tid = mvpp2_prs_vid_range_find(priv, (1 << port->id), vid, mask);
3546
3547 reg_val = mvpp2_read(priv, MVPP2_MH_REG(port->id));
3548 if (reg_val & MVPP2_DSA_EXTENDED)
3549 shift = MVPP2_VLAN_TAG_EDSA_LEN;
3550 else
3551 shift = MVPP2_VLAN_TAG_LEN;
3552
3553 /* No such entry */
3554 if (!tid) {
3555 memset(&pe, 0, sizeof(pe));
3556
3557 /* Go through all entries from first to last in vlan range */
3558 tid = mvpp2_prs_tcam_first_free(priv, vid_start,
3559 vid_start +
3560 MVPP2_PRS_VLAN_FILT_MAX_ENTRY);
3561
3562 /* There isn't room for a new VID filter */
3563 if (tid < 0)
3564 return tid;
3565
3566 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VID);
3567 pe.index = tid;
3568
3569 /* Mask all ports */
3570 mvpp2_prs_tcam_port_map_set(&pe, 0);
3571 } else {
3572 mvpp2_prs_hw_read(priv, &pe);
3573 }
3574
3575 /* Enable the current port */
3576 mvpp2_prs_tcam_port_set(&pe, port->id, true);
3577
3578 /* Continue - set next lookup */
3579 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
3580
3581 /* Skip VLAN header - Set offset to 4 or 8 bytes */
3582 mvpp2_prs_sram_shift_set(&pe, shift, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3583
3584 /* Set match on VID */
3585 mvpp2_prs_match_vid(&pe, MVPP2_PRS_VID_TCAM_BYTE, vid);
3586
3587 /* Clear all ai bits for next iteration */
3588 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
3589
3590 /* Update shadow table */
3591 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VID);
3592 mvpp2_prs_hw_write(priv, &pe);
3593
3594 return 0;
3595}
3596
3597/* Write parser entry for VID filtering */
3598static void mvpp2_prs_vid_entry_remove(struct mvpp2_port *port, u16 vid)
3599{
3600 struct mvpp2 *priv = port->priv;
3601 int tid;
3602
3603 /* Scan TCAM and see if entry with this <vid,port> already exist */
3604 tid = mvpp2_prs_vid_range_find(priv, (1 << port->id), vid, 0xfff);
3605
3606 /* No such entry */
3607 if (tid)
3608 return;
3609
3610 mvpp2_prs_hw_inv(priv, tid);
3611 priv->prs_shadow[tid].valid = false;
3612}
3613
3614/* Remove all existing VID filters on this port */
3615static void mvpp2_prs_vid_remove_all(struct mvpp2_port *port)
3616{
3617 struct mvpp2 *priv = port->priv;
3618 int tid;
3619
3620 for (tid = MVPP2_PRS_VID_PORT_FIRST(port->id);
3621 tid <= MVPP2_PRS_VID_PORT_LAST(port->id); tid++) {
3622 if (priv->prs_shadow[tid].valid)
3623 mvpp2_prs_vid_entry_remove(port, tid);
3624 }
3625}
3626
3627/* Remove VID filering entry for this port */
3628static void mvpp2_prs_vid_disable_filtering(struct mvpp2_port *port)
3629{
3630 unsigned int tid = MVPP2_PRS_VID_PORT_DFLT(port->id);
3631 struct mvpp2 *priv = port->priv;
3632
3633 /* Invalidate the guard entry */
3634 mvpp2_prs_hw_inv(priv, tid);
3635
3636 priv->prs_shadow[tid].valid = false;
3637}
3638
3639/* Add guard entry that drops packets when no VID is matched on this port */
3640static void mvpp2_prs_vid_enable_filtering(struct mvpp2_port *port)
3641{
3642 unsigned int tid = MVPP2_PRS_VID_PORT_DFLT(port->id);
3643 struct mvpp2 *priv = port->priv;
3644 unsigned int reg_val, shift;
3645 struct mvpp2_prs_entry pe;
3646
3647 if (priv->prs_shadow[tid].valid)
3648 return;
3649
3650 memset(&pe, 0, sizeof(pe));
3651
3652 pe.index = tid;
3653
3654 reg_val = mvpp2_read(priv, MVPP2_MH_REG(port->id));
3655 if (reg_val & MVPP2_DSA_EXTENDED)
3656 shift = MVPP2_VLAN_TAG_EDSA_LEN;
3657 else
3658 shift = MVPP2_VLAN_TAG_LEN;
3659
3660 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VID);
3661
3662 /* Mask all ports */
3663 mvpp2_prs_tcam_port_map_set(&pe, 0);
3664
3665 /* Update port mask */
3666 mvpp2_prs_tcam_port_set(&pe, port->id, true);
3667
3668 /* Continue - set next lookup */
3669 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
3670
3671 /* Skip VLAN header - Set offset to 4 or 8 bytes */
3672 mvpp2_prs_sram_shift_set(&pe, shift, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3673
3674 /* Drop VLAN packets that don't belong to any VIDs on this port */
3675 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
3676 MVPP2_PRS_RI_DROP_MASK);
3677
3678 /* Clear all ai bits for next iteration */
3679 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
3680
3681 /* Update shadow table */
3682 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VID);
3683 mvpp2_prs_hw_write(priv, &pe);
3684}
3685
Marcin Wojtas3f518502014-07-10 16:52:13 -03003686/* Parser default initialization */
3687static int mvpp2_prs_default_init(struct platform_device *pdev,
3688 struct mvpp2 *priv)
3689{
3690 int err, index, i;
3691
3692 /* Enable tcam table */
3693 mvpp2_write(priv, MVPP2_PRS_TCAM_CTRL_REG, MVPP2_PRS_TCAM_EN_MASK);
3694
3695 /* Clear all tcam and sram entries */
3696 for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++) {
3697 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
3698 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
3699 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), 0);
3700
3701 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, index);
3702 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
3703 mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), 0);
3704 }
3705
3706 /* Invalidate all tcam entries */
3707 for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++)
3708 mvpp2_prs_hw_inv(priv, index);
3709
3710 priv->prs_shadow = devm_kcalloc(&pdev->dev, MVPP2_PRS_TCAM_SRAM_SIZE,
Markus Elfring37df25e2017-04-17 09:12:34 +02003711 sizeof(*priv->prs_shadow),
Marcin Wojtas3f518502014-07-10 16:52:13 -03003712 GFP_KERNEL);
3713 if (!priv->prs_shadow)
3714 return -ENOMEM;
3715
3716 /* Always start from lookup = 0 */
3717 for (index = 0; index < MVPP2_MAX_PORTS; index++)
3718 mvpp2_prs_hw_port_init(priv, index, MVPP2_PRS_LU_MH,
3719 MVPP2_PRS_PORT_LU_MAX, 0);
3720
3721 mvpp2_prs_def_flow_init(priv);
3722
3723 mvpp2_prs_mh_init(priv);
3724
3725 mvpp2_prs_mac_init(priv);
3726
3727 mvpp2_prs_dsa_init(priv);
3728
Maxime Chevallier56beda32018-02-28 10:14:13 +01003729 mvpp2_prs_vid_init(priv);
3730
Marcin Wojtas3f518502014-07-10 16:52:13 -03003731 err = mvpp2_prs_etype_init(priv);
3732 if (err)
3733 return err;
3734
3735 err = mvpp2_prs_vlan_init(pdev, priv);
3736 if (err)
3737 return err;
3738
3739 err = mvpp2_prs_pppoe_init(priv);
3740 if (err)
3741 return err;
3742
3743 err = mvpp2_prs_ip6_init(priv);
3744 if (err)
3745 return err;
3746
3747 err = mvpp2_prs_ip4_init(priv);
3748 if (err)
3749 return err;
3750
3751 return 0;
3752}
3753
3754/* Compare MAC DA with tcam entry data */
3755static bool mvpp2_prs_mac_range_equals(struct mvpp2_prs_entry *pe,
3756 const u8 *da, unsigned char *mask)
3757{
3758 unsigned char tcam_byte, tcam_mask;
3759 int index;
3760
3761 for (index = 0; index < ETH_ALEN; index++) {
3762 mvpp2_prs_tcam_data_byte_get(pe, index, &tcam_byte, &tcam_mask);
3763 if (tcam_mask != mask[index])
3764 return false;
3765
3766 if ((tcam_mask & tcam_byte) != (da[index] & mask[index]))
3767 return false;
3768 }
3769
3770 return true;
3771}
3772
3773/* Find tcam entry with matched pair <MAC DA, port> */
3774static struct mvpp2_prs_entry *
3775mvpp2_prs_mac_da_range_find(struct mvpp2 *priv, int pmap, const u8 *da,
3776 unsigned char *mask, int udf_type)
3777{
3778 struct mvpp2_prs_entry *pe;
3779 int tid;
3780
Antoine Tenart239dd4e2017-10-24 11:41:28 +02003781 pe = kzalloc(sizeof(*pe), GFP_ATOMIC);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003782 if (!pe)
3783 return NULL;
3784 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
3785
3786 /* Go through the all entires with MVPP2_PRS_LU_MAC */
Maxime Chevallier10fea262018-03-07 15:18:04 +01003787 for (tid = MVPP2_PE_MAC_RANGE_START;
3788 tid <= MVPP2_PE_MAC_RANGE_END; tid++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03003789 unsigned int entry_pmap;
3790
3791 if (!priv->prs_shadow[tid].valid ||
3792 (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
3793 (priv->prs_shadow[tid].udf != udf_type))
3794 continue;
3795
3796 pe->index = tid;
3797 mvpp2_prs_hw_read(priv, pe);
3798 entry_pmap = mvpp2_prs_tcam_port_map_get(pe);
3799
3800 if (mvpp2_prs_mac_range_equals(pe, da, mask) &&
3801 entry_pmap == pmap)
3802 return pe;
3803 }
3804 kfree(pe);
3805
3806 return NULL;
3807}
3808
3809/* Update parser's mac da entry */
Maxime Chevallierce2a27c2018-03-07 15:18:03 +01003810static int mvpp2_prs_mac_da_accept(struct mvpp2_port *port, const u8 *da,
3811 bool add)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003812{
Marcin Wojtas3f518502014-07-10 16:52:13 -03003813 unsigned char mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
Maxime Chevallierce2a27c2018-03-07 15:18:03 +01003814 struct mvpp2 *priv = port->priv;
3815 unsigned int pmap, len, ri;
3816 struct mvpp2_prs_entry *pe;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003817 int tid;
3818
3819 /* Scan TCAM and see if entry with this <MAC DA, port> already exist */
Maxime Chevallierce2a27c2018-03-07 15:18:03 +01003820 pe = mvpp2_prs_mac_da_range_find(priv, BIT(port->id), da, mask,
Marcin Wojtas3f518502014-07-10 16:52:13 -03003821 MVPP2_PRS_UDF_MAC_DEF);
3822
3823 /* No such entry */
3824 if (!pe) {
3825 if (!add)
3826 return 0;
3827
3828 /* Create new TCAM entry */
Marcin Wojtas3f518502014-07-10 16:52:13 -03003829 /* Go through the all entries from first to last */
Maxime Chevallier10fea262018-03-07 15:18:04 +01003830 tid = mvpp2_prs_tcam_first_free(priv,
3831 MVPP2_PE_MAC_RANGE_START,
3832 MVPP2_PE_MAC_RANGE_END);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003833 if (tid < 0)
3834 return tid;
3835
Antoine Tenart239dd4e2017-10-24 11:41:28 +02003836 pe = kzalloc(sizeof(*pe), GFP_ATOMIC);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003837 if (!pe)
Amitoj Kaur Chawlac2bb7bc2016-02-04 19:25:26 +05303838 return -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003839 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
3840 pe->index = tid;
3841
3842 /* Mask all ports */
3843 mvpp2_prs_tcam_port_map_set(pe, 0);
3844 }
3845
3846 /* Update port mask */
Maxime Chevallierce2a27c2018-03-07 15:18:03 +01003847 mvpp2_prs_tcam_port_set(pe, port->id, add);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003848
3849 /* Invalidate the entry if no ports are left enabled */
3850 pmap = mvpp2_prs_tcam_port_map_get(pe);
3851 if (pmap == 0) {
3852 if (add) {
3853 kfree(pe);
Amitoj Kaur Chawlac2bb7bc2016-02-04 19:25:26 +05303854 return -EINVAL;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003855 }
3856 mvpp2_prs_hw_inv(priv, pe->index);
3857 priv->prs_shadow[pe->index].valid = false;
3858 kfree(pe);
3859 return 0;
3860 }
3861
3862 /* Continue - set next lookup */
3863 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_DSA);
3864
3865 /* Set match on DA */
3866 len = ETH_ALEN;
3867 while (len--)
3868 mvpp2_prs_tcam_data_byte_set(pe, len, da[len], 0xff);
3869
3870 /* Set result info bits */
Maxime Chevallier10fea262018-03-07 15:18:04 +01003871 if (is_broadcast_ether_addr(da)) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03003872 ri = MVPP2_PRS_RI_L2_BCAST;
Maxime Chevallier10fea262018-03-07 15:18:04 +01003873 } else if (is_multicast_ether_addr(da)) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03003874 ri = MVPP2_PRS_RI_L2_MCAST;
Maxime Chevallier10fea262018-03-07 15:18:04 +01003875 } else {
3876 ri = MVPP2_PRS_RI_L2_UCAST;
3877
3878 if (ether_addr_equal(da, port->dev->dev_addr))
3879 ri |= MVPP2_PRS_RI_MAC_ME_MASK;
3880 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03003881
3882 mvpp2_prs_sram_ri_update(pe, ri, MVPP2_PRS_RI_L2_CAST_MASK |
3883 MVPP2_PRS_RI_MAC_ME_MASK);
3884 mvpp2_prs_shadow_ri_set(priv, pe->index, ri, MVPP2_PRS_RI_L2_CAST_MASK |
3885 MVPP2_PRS_RI_MAC_ME_MASK);
3886
3887 /* Shift to ethertype */
3888 mvpp2_prs_sram_shift_set(pe, 2 * ETH_ALEN,
3889 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3890
3891 /* Update shadow table and hw entry */
3892 priv->prs_shadow[pe->index].udf = MVPP2_PRS_UDF_MAC_DEF;
3893 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_MAC);
3894 mvpp2_prs_hw_write(priv, pe);
3895
3896 kfree(pe);
3897
3898 return 0;
3899}
3900
3901static int mvpp2_prs_update_mac_da(struct net_device *dev, const u8 *da)
3902{
3903 struct mvpp2_port *port = netdev_priv(dev);
3904 int err;
3905
3906 /* Remove old parser entry */
Maxime Chevallierce2a27c2018-03-07 15:18:03 +01003907 err = mvpp2_prs_mac_da_accept(port, dev->dev_addr, false);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003908 if (err)
3909 return err;
3910
3911 /* Add new parser entry */
Maxime Chevallierce2a27c2018-03-07 15:18:03 +01003912 err = mvpp2_prs_mac_da_accept(port, da, true);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003913 if (err)
3914 return err;
3915
3916 /* Set addr in the device */
3917 ether_addr_copy(dev->dev_addr, da);
3918
3919 return 0;
3920}
3921
Maxime Chevallier10fea262018-03-07 15:18:04 +01003922static void mvpp2_prs_mac_del_all(struct mvpp2_port *port)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003923{
Maxime Chevallier10fea262018-03-07 15:18:04 +01003924 struct mvpp2 *priv = port->priv;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003925 struct mvpp2_prs_entry pe;
Maxime Chevallier10fea262018-03-07 15:18:04 +01003926 unsigned long pmap;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003927 int index, tid;
3928
Maxime Chevallier10fea262018-03-07 15:18:04 +01003929 for (tid = MVPP2_PE_MAC_RANGE_START;
3930 tid <= MVPP2_PE_MAC_RANGE_END; tid++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03003931 unsigned char da[ETH_ALEN], da_mask[ETH_ALEN];
3932
3933 if (!priv->prs_shadow[tid].valid ||
3934 (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
3935 (priv->prs_shadow[tid].udf != MVPP2_PRS_UDF_MAC_DEF))
3936 continue;
3937
Marcin Wojtas3f518502014-07-10 16:52:13 -03003938 pe.index = tid;
3939 mvpp2_prs_hw_read(priv, &pe);
3940
Maxime Chevallier10fea262018-03-07 15:18:04 +01003941 pmap = mvpp2_prs_tcam_port_map_get(&pe);
3942
3943 /* We only want entries active on this port */
3944 if (!test_bit(port->id, &pmap))
3945 continue;
3946
Marcin Wojtas3f518502014-07-10 16:52:13 -03003947 /* Read mac addr from entry */
3948 for (index = 0; index < ETH_ALEN; index++)
3949 mvpp2_prs_tcam_data_byte_get(&pe, index, &da[index],
3950 &da_mask[index]);
3951
Maxime Chevallier10fea262018-03-07 15:18:04 +01003952 /* Special cases : Don't remove broadcast and port's own
3953 * address
3954 */
3955 if (is_broadcast_ether_addr(da) ||
3956 ether_addr_equal(da, port->dev->dev_addr))
3957 continue;
3958
3959 /* Remove entry from TCAM */
3960 mvpp2_prs_mac_da_accept(port, da, false);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003961 }
3962}
3963
3964static int mvpp2_prs_tag_mode_set(struct mvpp2 *priv, int port, int type)
3965{
3966 switch (type) {
3967 case MVPP2_TAG_TYPE_EDSA:
3968 /* Add port to EDSA entries */
3969 mvpp2_prs_dsa_tag_set(priv, port, true,
3970 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3971 mvpp2_prs_dsa_tag_set(priv, port, true,
3972 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3973 /* Remove port from DSA entries */
3974 mvpp2_prs_dsa_tag_set(priv, port, false,
3975 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3976 mvpp2_prs_dsa_tag_set(priv, port, false,
3977 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3978 break;
3979
3980 case MVPP2_TAG_TYPE_DSA:
3981 /* Add port to DSA entries */
3982 mvpp2_prs_dsa_tag_set(priv, port, true,
3983 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3984 mvpp2_prs_dsa_tag_set(priv, port, true,
3985 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3986 /* Remove port from EDSA entries */
3987 mvpp2_prs_dsa_tag_set(priv, port, false,
3988 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3989 mvpp2_prs_dsa_tag_set(priv, port, false,
3990 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3991 break;
3992
3993 case MVPP2_TAG_TYPE_MH:
3994 case MVPP2_TAG_TYPE_NONE:
3995 /* Remove port form EDSA and DSA entries */
3996 mvpp2_prs_dsa_tag_set(priv, port, false,
3997 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3998 mvpp2_prs_dsa_tag_set(priv, port, false,
3999 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
4000 mvpp2_prs_dsa_tag_set(priv, port, false,
4001 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
4002 mvpp2_prs_dsa_tag_set(priv, port, false,
4003 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
4004 break;
4005
4006 default:
4007 if ((type < 0) || (type > MVPP2_TAG_TYPE_EDSA))
4008 return -EINVAL;
4009 }
4010
4011 return 0;
4012}
4013
4014/* Set prs flow for the port */
4015static int mvpp2_prs_def_flow(struct mvpp2_port *port)
4016{
4017 struct mvpp2_prs_entry *pe;
4018 int tid;
4019
4020 pe = mvpp2_prs_flow_find(port->priv, port->id);
4021
4022 /* Such entry not exist */
4023 if (!pe) {
4024 /* Go through the all entires from last to first */
4025 tid = mvpp2_prs_tcam_first_free(port->priv,
4026 MVPP2_PE_LAST_FREE_TID,
4027 MVPP2_PE_FIRST_FREE_TID);
4028 if (tid < 0)
4029 return tid;
4030
4031 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
4032 if (!pe)
4033 return -ENOMEM;
4034
4035 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
4036 pe->index = tid;
4037
4038 /* Set flow ID*/
4039 mvpp2_prs_sram_ai_update(pe, port->id, MVPP2_PRS_FLOW_ID_MASK);
4040 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
4041
4042 /* Update shadow table */
4043 mvpp2_prs_shadow_set(port->priv, pe->index, MVPP2_PRS_LU_FLOWS);
4044 }
4045
4046 mvpp2_prs_tcam_port_map_set(pe, (1 << port->id));
4047 mvpp2_prs_hw_write(port->priv, pe);
4048 kfree(pe);
4049
4050 return 0;
4051}
4052
4053/* Classifier configuration routines */
4054
4055/* Update classification flow table registers */
4056static void mvpp2_cls_flow_write(struct mvpp2 *priv,
4057 struct mvpp2_cls_flow_entry *fe)
4058{
4059 mvpp2_write(priv, MVPP2_CLS_FLOW_INDEX_REG, fe->index);
4060 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL0_REG, fe->data[0]);
4061 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL1_REG, fe->data[1]);
4062 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL2_REG, fe->data[2]);
4063}
4064
4065/* Update classification lookup table register */
4066static void mvpp2_cls_lookup_write(struct mvpp2 *priv,
4067 struct mvpp2_cls_lookup_entry *le)
4068{
4069 u32 val;
4070
4071 val = (le->way << MVPP2_CLS_LKP_INDEX_WAY_OFFS) | le->lkpid;
4072 mvpp2_write(priv, MVPP2_CLS_LKP_INDEX_REG, val);
4073 mvpp2_write(priv, MVPP2_CLS_LKP_TBL_REG, le->data);
4074}
4075
4076/* Classifier default initialization */
4077static void mvpp2_cls_init(struct mvpp2 *priv)
4078{
4079 struct mvpp2_cls_lookup_entry le;
4080 struct mvpp2_cls_flow_entry fe;
4081 int index;
4082
4083 /* Enable classifier */
4084 mvpp2_write(priv, MVPP2_CLS_MODE_REG, MVPP2_CLS_MODE_ACTIVE_MASK);
4085
4086 /* Clear classifier flow table */
Arnd Bergmanne8f967c2016-11-24 17:28:12 +01004087 memset(&fe.data, 0, sizeof(fe.data));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004088 for (index = 0; index < MVPP2_CLS_FLOWS_TBL_SIZE; index++) {
4089 fe.index = index;
4090 mvpp2_cls_flow_write(priv, &fe);
4091 }
4092
4093 /* Clear classifier lookup table */
4094 le.data = 0;
4095 for (index = 0; index < MVPP2_CLS_LKP_TBL_SIZE; index++) {
4096 le.lkpid = index;
4097 le.way = 0;
4098 mvpp2_cls_lookup_write(priv, &le);
4099
4100 le.way = 1;
4101 mvpp2_cls_lookup_write(priv, &le);
4102 }
4103}
4104
4105static void mvpp2_cls_port_config(struct mvpp2_port *port)
4106{
4107 struct mvpp2_cls_lookup_entry le;
4108 u32 val;
4109
4110 /* Set way for the port */
4111 val = mvpp2_read(port->priv, MVPP2_CLS_PORT_WAY_REG);
4112 val &= ~MVPP2_CLS_PORT_WAY_MASK(port->id);
4113 mvpp2_write(port->priv, MVPP2_CLS_PORT_WAY_REG, val);
4114
4115 /* Pick the entry to be accessed in lookup ID decoding table
4116 * according to the way and lkpid.
4117 */
4118 le.lkpid = port->id;
4119 le.way = 0;
4120 le.data = 0;
4121
4122 /* Set initial CPU queue for receiving packets */
4123 le.data &= ~MVPP2_CLS_LKP_TBL_RXQ_MASK;
4124 le.data |= port->first_rxq;
4125
4126 /* Disable classification engines */
4127 le.data &= ~MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK;
4128
4129 /* Update lookup ID table entry */
4130 mvpp2_cls_lookup_write(port->priv, &le);
4131}
4132
4133/* Set CPU queue number for oversize packets */
4134static void mvpp2_cls_oversize_rxq_set(struct mvpp2_port *port)
4135{
4136 u32 val;
4137
4138 mvpp2_write(port->priv, MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port->id),
4139 port->first_rxq & MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK);
4140
4141 mvpp2_write(port->priv, MVPP2_CLS_SWFWD_P2HQ_REG(port->id),
4142 (port->first_rxq >> MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS));
4143
4144 val = mvpp2_read(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG);
4145 val |= MVPP2_CLS_SWFWD_PCTRL_MASK(port->id);
4146 mvpp2_write(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG, val);
4147}
4148
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004149static void *mvpp2_frag_alloc(const struct mvpp2_bm_pool *pool)
4150{
4151 if (likely(pool->frag_size <= PAGE_SIZE))
4152 return netdev_alloc_frag(pool->frag_size);
4153 else
4154 return kmalloc(pool->frag_size, GFP_ATOMIC);
4155}
4156
4157static void mvpp2_frag_free(const struct mvpp2_bm_pool *pool, void *data)
4158{
4159 if (likely(pool->frag_size <= PAGE_SIZE))
4160 skb_free_frag(data);
4161 else
4162 kfree(data);
4163}
4164
Marcin Wojtas3f518502014-07-10 16:52:13 -03004165/* Buffer Manager configuration routines */
4166
4167/* Create pool */
4168static int mvpp2_bm_pool_create(struct platform_device *pdev,
4169 struct mvpp2 *priv,
4170 struct mvpp2_bm_pool *bm_pool, int size)
4171{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004172 u32 val;
4173
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004174 /* Number of buffer pointers must be a multiple of 16, as per
4175 * hardware constraints
4176 */
4177 if (!IS_ALIGNED(size, 16))
4178 return -EINVAL;
4179
4180 /* PPv2.1 needs 8 bytes per buffer pointer, PPv2.2 needs 16
4181 * bytes per buffer pointer
4182 */
4183 if (priv->hw_version == MVPP21)
4184 bm_pool->size_bytes = 2 * sizeof(u32) * size;
4185 else
4186 bm_pool->size_bytes = 2 * sizeof(u64) * size;
4187
4188 bm_pool->virt_addr = dma_alloc_coherent(&pdev->dev, bm_pool->size_bytes,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004189 &bm_pool->dma_addr,
Marcin Wojtas3f518502014-07-10 16:52:13 -03004190 GFP_KERNEL);
4191 if (!bm_pool->virt_addr)
4192 return -ENOMEM;
4193
Thomas Petazzonid3158802017-02-21 11:28:13 +01004194 if (!IS_ALIGNED((unsigned long)bm_pool->virt_addr,
4195 MVPP2_BM_POOL_PTR_ALIGN)) {
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004196 dma_free_coherent(&pdev->dev, bm_pool->size_bytes,
4197 bm_pool->virt_addr, bm_pool->dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004198 dev_err(&pdev->dev, "BM pool %d is not %d bytes aligned\n",
4199 bm_pool->id, MVPP2_BM_POOL_PTR_ALIGN);
4200 return -ENOMEM;
4201 }
4202
4203 mvpp2_write(priv, MVPP2_BM_POOL_BASE_REG(bm_pool->id),
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004204 lower_32_bits(bm_pool->dma_addr));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004205 mvpp2_write(priv, MVPP2_BM_POOL_SIZE_REG(bm_pool->id), size);
4206
4207 val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
4208 val |= MVPP2_BM_START_MASK;
4209 mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
4210
Marcin Wojtas3f518502014-07-10 16:52:13 -03004211 bm_pool->size = size;
4212 bm_pool->pkt_size = 0;
4213 bm_pool->buf_num = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004214
4215 return 0;
4216}
4217
4218/* Set pool buffer size */
4219static void mvpp2_bm_pool_bufsize_set(struct mvpp2 *priv,
4220 struct mvpp2_bm_pool *bm_pool,
4221 int buf_size)
4222{
4223 u32 val;
4224
4225 bm_pool->buf_size = buf_size;
4226
4227 val = ALIGN(buf_size, 1 << MVPP2_POOL_BUF_SIZE_OFFSET);
4228 mvpp2_write(priv, MVPP2_POOL_BUF_SIZE_REG(bm_pool->id), val);
4229}
4230
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004231static void mvpp2_bm_bufs_get_addrs(struct device *dev, struct mvpp2 *priv,
4232 struct mvpp2_bm_pool *bm_pool,
4233 dma_addr_t *dma_addr,
4234 phys_addr_t *phys_addr)
4235{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004236 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01004237
4238 *dma_addr = mvpp2_percpu_read(priv, cpu,
4239 MVPP2_BM_PHY_ALLOC_REG(bm_pool->id));
4240 *phys_addr = mvpp2_percpu_read(priv, cpu, MVPP2_BM_VIRT_ALLOC_REG);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004241
4242 if (priv->hw_version == MVPP22) {
4243 u32 val;
4244 u32 dma_addr_highbits, phys_addr_highbits;
4245
Thomas Petazzonia7868412017-03-07 16:53:13 +01004246 val = mvpp2_percpu_read(priv, cpu, MVPP22_BM_ADDR_HIGH_ALLOC);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004247 dma_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_PHYS_MASK);
4248 phys_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_VIRT_MASK) >>
4249 MVPP22_BM_ADDR_HIGH_VIRT_SHIFT;
4250
4251 if (sizeof(dma_addr_t) == 8)
4252 *dma_addr |= (u64)dma_addr_highbits << 32;
4253
4254 if (sizeof(phys_addr_t) == 8)
4255 *phys_addr |= (u64)phys_addr_highbits << 32;
4256 }
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004257
4258 put_cpu();
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004259}
4260
Ezequiel Garcia7861f122014-07-21 13:48:14 -03004261/* Free all buffers from the pool */
Marcin Wojtas4229d502015-12-03 15:20:50 +01004262static void mvpp2_bm_bufs_free(struct device *dev, struct mvpp2 *priv,
Stefan Chulskieffbf5f2018-03-05 15:16:51 +01004263 struct mvpp2_bm_pool *bm_pool, int buf_num)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004264{
4265 int i;
4266
Stefan Chulskieffbf5f2018-03-05 15:16:51 +01004267 if (buf_num > bm_pool->buf_num) {
4268 WARN(1, "Pool does not have so many bufs pool(%d) bufs(%d)\n",
4269 bm_pool->id, buf_num);
4270 buf_num = bm_pool->buf_num;
4271 }
4272
4273 for (i = 0; i < buf_num; i++) {
Thomas Petazzoni20396132017-03-07 16:53:00 +01004274 dma_addr_t buf_dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004275 phys_addr_t buf_phys_addr;
4276 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004277
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004278 mvpp2_bm_bufs_get_addrs(dev, priv, bm_pool,
4279 &buf_dma_addr, &buf_phys_addr);
Marcin Wojtas4229d502015-12-03 15:20:50 +01004280
Thomas Petazzoni20396132017-03-07 16:53:00 +01004281 dma_unmap_single(dev, buf_dma_addr,
Marcin Wojtas4229d502015-12-03 15:20:50 +01004282 bm_pool->buf_size, DMA_FROM_DEVICE);
4283
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004284 data = (void *)phys_to_virt(buf_phys_addr);
4285 if (!data)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004286 break;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004287
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004288 mvpp2_frag_free(bm_pool, data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004289 }
4290
4291 /* Update BM driver with number of buffers removed from pool */
4292 bm_pool->buf_num -= i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004293}
4294
Stefan Chulskieffbf5f2018-03-05 15:16:51 +01004295/* Check number of buffers in BM pool */
kbuild test robot6e61e102018-03-06 13:05:06 +08004296static int mvpp2_check_hw_buf_num(struct mvpp2 *priv, struct mvpp2_bm_pool *bm_pool)
Stefan Chulskieffbf5f2018-03-05 15:16:51 +01004297{
4298 int buf_num = 0;
4299
4300 buf_num += mvpp2_read(priv, MVPP2_BM_POOL_PTRS_NUM_REG(bm_pool->id)) &
4301 MVPP22_BM_POOL_PTRS_NUM_MASK;
4302 buf_num += mvpp2_read(priv, MVPP2_BM_BPPI_PTRS_NUM_REG(bm_pool->id)) &
4303 MVPP2_BM_BPPI_PTR_NUM_MASK;
4304
4305 /* HW has one buffer ready which is not reflected in the counters */
4306 if (buf_num)
4307 buf_num += 1;
4308
4309 return buf_num;
4310}
4311
Marcin Wojtas3f518502014-07-10 16:52:13 -03004312/* Cleanup pool */
4313static int mvpp2_bm_pool_destroy(struct platform_device *pdev,
4314 struct mvpp2 *priv,
4315 struct mvpp2_bm_pool *bm_pool)
4316{
Stefan Chulskieffbf5f2018-03-05 15:16:51 +01004317 int buf_num;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004318 u32 val;
4319
Stefan Chulskieffbf5f2018-03-05 15:16:51 +01004320 buf_num = mvpp2_check_hw_buf_num(priv, bm_pool);
4321 mvpp2_bm_bufs_free(&pdev->dev, priv, bm_pool, buf_num);
4322
4323 /* Check buffer counters after free */
4324 buf_num = mvpp2_check_hw_buf_num(priv, bm_pool);
4325 if (buf_num) {
4326 WARN(1, "cannot free all buffers in pool %d, buf_num left %d\n",
4327 bm_pool->id, bm_pool->buf_num);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004328 return 0;
4329 }
4330
4331 val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
4332 val |= MVPP2_BM_STOP_MASK;
4333 mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
4334
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004335 dma_free_coherent(&pdev->dev, bm_pool->size_bytes,
Marcin Wojtas3f518502014-07-10 16:52:13 -03004336 bm_pool->virt_addr,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004337 bm_pool->dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004338 return 0;
4339}
4340
4341static int mvpp2_bm_pools_init(struct platform_device *pdev,
4342 struct mvpp2 *priv)
4343{
4344 int i, err, size;
4345 struct mvpp2_bm_pool *bm_pool;
4346
4347 /* Create all pools with maximum size */
4348 size = MVPP2_BM_POOL_SIZE_MAX;
4349 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
4350 bm_pool = &priv->bm_pools[i];
4351 bm_pool->id = i;
4352 err = mvpp2_bm_pool_create(pdev, priv, bm_pool, size);
4353 if (err)
4354 goto err_unroll_pools;
4355 mvpp2_bm_pool_bufsize_set(priv, bm_pool, 0);
4356 }
4357 return 0;
4358
4359err_unroll_pools:
4360 dev_err(&pdev->dev, "failed to create BM pool %d, size %d\n", i, size);
4361 for (i = i - 1; i >= 0; i--)
4362 mvpp2_bm_pool_destroy(pdev, priv, &priv->bm_pools[i]);
4363 return err;
4364}
4365
4366static int mvpp2_bm_init(struct platform_device *pdev, struct mvpp2 *priv)
4367{
4368 int i, err;
4369
4370 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
4371 /* Mask BM all interrupts */
4372 mvpp2_write(priv, MVPP2_BM_INTR_MASK_REG(i), 0);
4373 /* Clear BM cause register */
4374 mvpp2_write(priv, MVPP2_BM_INTR_CAUSE_REG(i), 0);
4375 }
4376
4377 /* Allocate and initialize BM pools */
4378 priv->bm_pools = devm_kcalloc(&pdev->dev, MVPP2_BM_POOLS_NUM,
Markus Elfring81f915e2017-04-17 09:06:33 +02004379 sizeof(*priv->bm_pools), GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004380 if (!priv->bm_pools)
4381 return -ENOMEM;
4382
4383 err = mvpp2_bm_pools_init(pdev, priv);
4384 if (err < 0)
4385 return err;
4386 return 0;
4387}
4388
Stefan Chulski01d04932018-03-05 15:16:50 +01004389static void mvpp2_setup_bm_pool(void)
4390{
4391 /* Short pool */
4392 mvpp2_pools[MVPP2_BM_SHORT].buf_num = MVPP2_BM_SHORT_BUF_NUM;
4393 mvpp2_pools[MVPP2_BM_SHORT].pkt_size = MVPP2_BM_SHORT_PKT_SIZE;
4394
4395 /* Long pool */
4396 mvpp2_pools[MVPP2_BM_LONG].buf_num = MVPP2_BM_LONG_BUF_NUM;
4397 mvpp2_pools[MVPP2_BM_LONG].pkt_size = MVPP2_BM_LONG_PKT_SIZE;
Stefan Chulski576193f2018-03-05 15:16:54 +01004398
4399 /* Jumbo pool */
4400 mvpp2_pools[MVPP2_BM_JUMBO].buf_num = MVPP2_BM_JUMBO_BUF_NUM;
4401 mvpp2_pools[MVPP2_BM_JUMBO].pkt_size = MVPP2_BM_JUMBO_PKT_SIZE;
Stefan Chulski01d04932018-03-05 15:16:50 +01004402}
4403
Marcin Wojtas3f518502014-07-10 16:52:13 -03004404/* Attach long pool to rxq */
4405static void mvpp2_rxq_long_pool_set(struct mvpp2_port *port,
4406 int lrxq, int long_pool)
4407{
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004408 u32 val, mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004409 int prxq;
4410
4411 /* Get queue physical ID */
4412 prxq = port->rxqs[lrxq]->id;
4413
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004414 if (port->priv->hw_version == MVPP21)
4415 mask = MVPP21_RXQ_POOL_LONG_MASK;
4416 else
4417 mask = MVPP22_RXQ_POOL_LONG_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004418
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004419 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
4420 val &= ~mask;
4421 val |= (long_pool << MVPP2_RXQ_POOL_LONG_OFFS) & mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004422 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
4423}
4424
4425/* Attach short pool to rxq */
4426static void mvpp2_rxq_short_pool_set(struct mvpp2_port *port,
4427 int lrxq, int short_pool)
4428{
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004429 u32 val, mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004430 int prxq;
4431
4432 /* Get queue physical ID */
4433 prxq = port->rxqs[lrxq]->id;
4434
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004435 if (port->priv->hw_version == MVPP21)
4436 mask = MVPP21_RXQ_POOL_SHORT_MASK;
4437 else
4438 mask = MVPP22_RXQ_POOL_SHORT_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004439
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004440 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
4441 val &= ~mask;
4442 val |= (short_pool << MVPP2_RXQ_POOL_SHORT_OFFS) & mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004443 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
4444}
4445
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004446static void *mvpp2_buf_alloc(struct mvpp2_port *port,
4447 struct mvpp2_bm_pool *bm_pool,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004448 dma_addr_t *buf_dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004449 phys_addr_t *buf_phys_addr,
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004450 gfp_t gfp_mask)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004451{
Thomas Petazzoni20396132017-03-07 16:53:00 +01004452 dma_addr_t dma_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004453 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004454
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004455 data = mvpp2_frag_alloc(bm_pool);
4456 if (!data)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004457 return NULL;
4458
Thomas Petazzoni20396132017-03-07 16:53:00 +01004459 dma_addr = dma_map_single(port->dev->dev.parent, data,
4460 MVPP2_RX_BUF_SIZE(bm_pool->pkt_size),
4461 DMA_FROM_DEVICE);
4462 if (unlikely(dma_mapping_error(port->dev->dev.parent, dma_addr))) {
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004463 mvpp2_frag_free(bm_pool, data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004464 return NULL;
4465 }
Thomas Petazzoni20396132017-03-07 16:53:00 +01004466 *buf_dma_addr = dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004467 *buf_phys_addr = virt_to_phys(data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004468
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004469 return data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004470}
4471
Marcin Wojtas3f518502014-07-10 16:52:13 -03004472/* Release buffer to BM */
4473static inline void mvpp2_bm_pool_put(struct mvpp2_port *port, int pool,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004474 dma_addr_t buf_dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004475 phys_addr_t buf_phys_addr)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004476{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004477 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01004478
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004479 if (port->priv->hw_version == MVPP22) {
4480 u32 val = 0;
4481
4482 if (sizeof(dma_addr_t) == 8)
4483 val |= upper_32_bits(buf_dma_addr) &
4484 MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK;
4485
4486 if (sizeof(phys_addr_t) == 8)
4487 val |= (upper_32_bits(buf_phys_addr)
4488 << MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT) &
4489 MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK;
4490
Thomas Petazzonia7868412017-03-07 16:53:13 +01004491 mvpp2_percpu_write(port->priv, cpu,
4492 MVPP22_BM_ADDR_HIGH_RLS_REG, val);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004493 }
4494
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004495 /* MVPP2_BM_VIRT_RLS_REG is not interpreted by HW, and simply
4496 * returned in the "cookie" field of the RX
4497 * descriptor. Instead of storing the virtual address, we
4498 * store the physical address
4499 */
Thomas Petazzonia7868412017-03-07 16:53:13 +01004500 mvpp2_percpu_write(port->priv, cpu,
4501 MVPP2_BM_VIRT_RLS_REG, buf_phys_addr);
4502 mvpp2_percpu_write(port->priv, cpu,
4503 MVPP2_BM_PHY_RLS_REG(pool), buf_dma_addr);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004504
4505 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03004506}
4507
Marcin Wojtas3f518502014-07-10 16:52:13 -03004508/* Allocate buffers for the pool */
4509static int mvpp2_bm_bufs_add(struct mvpp2_port *port,
4510 struct mvpp2_bm_pool *bm_pool, int buf_num)
4511{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004512 int i, buf_size, total_size;
Thomas Petazzoni20396132017-03-07 16:53:00 +01004513 dma_addr_t dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004514 phys_addr_t phys_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004515 void *buf;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004516
4517 buf_size = MVPP2_RX_BUF_SIZE(bm_pool->pkt_size);
4518 total_size = MVPP2_RX_TOTAL_SIZE(buf_size);
4519
4520 if (buf_num < 0 ||
4521 (buf_num + bm_pool->buf_num > bm_pool->size)) {
4522 netdev_err(port->dev,
4523 "cannot allocate %d buffers for pool %d\n",
4524 buf_num, bm_pool->id);
4525 return 0;
4526 }
4527
Marcin Wojtas3f518502014-07-10 16:52:13 -03004528 for (i = 0; i < buf_num; i++) {
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004529 buf = mvpp2_buf_alloc(port, bm_pool, &dma_addr,
4530 &phys_addr, GFP_KERNEL);
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004531 if (!buf)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004532 break;
4533
Thomas Petazzoni20396132017-03-07 16:53:00 +01004534 mvpp2_bm_pool_put(port, bm_pool->id, dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004535 phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004536 }
4537
4538 /* Update BM driver with number of buffers added to pool */
4539 bm_pool->buf_num += i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004540
4541 netdev_dbg(port->dev,
Stefan Chulski01d04932018-03-05 15:16:50 +01004542 "pool %d: pkt_size=%4d, buf_size=%4d, total_size=%4d\n",
Marcin Wojtas3f518502014-07-10 16:52:13 -03004543 bm_pool->id, bm_pool->pkt_size, buf_size, total_size);
4544
4545 netdev_dbg(port->dev,
Stefan Chulski01d04932018-03-05 15:16:50 +01004546 "pool %d: %d of %d buffers added\n",
Marcin Wojtas3f518502014-07-10 16:52:13 -03004547 bm_pool->id, i, buf_num);
4548 return i;
4549}
4550
4551/* Notify the driver that BM pool is being used as specific type and return the
4552 * pool pointer on success
4553 */
4554static struct mvpp2_bm_pool *
Stefan Chulski01d04932018-03-05 15:16:50 +01004555mvpp2_bm_pool_use(struct mvpp2_port *port, unsigned pool, int pkt_size)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004556{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004557 struct mvpp2_bm_pool *new_pool = &port->priv->bm_pools[pool];
4558 int num;
4559
Stefan Chulski01d04932018-03-05 15:16:50 +01004560 if (pool >= MVPP2_BM_POOLS_NUM) {
4561 netdev_err(port->dev, "Invalid pool %d\n", pool);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004562 return NULL;
4563 }
4564
Marcin Wojtas3f518502014-07-10 16:52:13 -03004565 /* Allocate buffers in case BM pool is used as long pool, but packet
4566 * size doesn't match MTU or BM pool hasn't being used yet
4567 */
Stefan Chulski01d04932018-03-05 15:16:50 +01004568 if (new_pool->pkt_size == 0) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004569 int pkts_num;
4570
4571 /* Set default buffer number or free all the buffers in case
4572 * the pool is not empty
4573 */
4574 pkts_num = new_pool->buf_num;
4575 if (pkts_num == 0)
Stefan Chulski01d04932018-03-05 15:16:50 +01004576 pkts_num = mvpp2_pools[pool].buf_num;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004577 else
Marcin Wojtas4229d502015-12-03 15:20:50 +01004578 mvpp2_bm_bufs_free(port->dev->dev.parent,
Stefan Chulskieffbf5f2018-03-05 15:16:51 +01004579 port->priv, new_pool, pkts_num);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004580
4581 new_pool->pkt_size = pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004582 new_pool->frag_size =
4583 SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
4584 MVPP2_SKB_SHINFO_SIZE;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004585
4586 /* Allocate buffers for this pool */
4587 num = mvpp2_bm_bufs_add(port, new_pool, pkts_num);
4588 if (num != pkts_num) {
4589 WARN(1, "pool %d: %d of %d allocated\n",
4590 new_pool->id, num, pkts_num);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004591 return NULL;
4592 }
4593 }
4594
4595 mvpp2_bm_pool_bufsize_set(port->priv, new_pool,
4596 MVPP2_RX_BUF_SIZE(new_pool->pkt_size));
4597
Marcin Wojtas3f518502014-07-10 16:52:13 -03004598 return new_pool;
4599}
4600
4601/* Initialize pools for swf */
4602static int mvpp2_swf_bm_pool_init(struct mvpp2_port *port)
4603{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004604 int rxq;
Stefan Chulski576193f2018-03-05 15:16:54 +01004605 enum mvpp2_bm_pool_log_num long_log_pool, short_log_pool;
4606
4607 /* If port pkt_size is higher than 1518B:
4608 * HW Long pool - SW Jumbo pool, HW Short pool - SW Long pool
4609 * else: HW Long pool - SW Long pool, HW Short pool - SW Short pool
4610 */
4611 if (port->pkt_size > MVPP2_BM_LONG_PKT_SIZE) {
4612 long_log_pool = MVPP2_BM_JUMBO;
4613 short_log_pool = MVPP2_BM_LONG;
4614 } else {
4615 long_log_pool = MVPP2_BM_LONG;
4616 short_log_pool = MVPP2_BM_SHORT;
4617 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004618
4619 if (!port->pool_long) {
4620 port->pool_long =
Stefan Chulski576193f2018-03-05 15:16:54 +01004621 mvpp2_bm_pool_use(port, long_log_pool,
4622 mvpp2_pools[long_log_pool].pkt_size);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004623 if (!port->pool_long)
4624 return -ENOMEM;
4625
Stefan Chulski576193f2018-03-05 15:16:54 +01004626 port->pool_long->port_map |= BIT(port->id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004627
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004628 for (rxq = 0; rxq < port->nrxqs; rxq++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004629 mvpp2_rxq_long_pool_set(port, rxq, port->pool_long->id);
4630 }
4631
4632 if (!port->pool_short) {
4633 port->pool_short =
Stefan Chulski576193f2018-03-05 15:16:54 +01004634 mvpp2_bm_pool_use(port, short_log_pool,
4635 mvpp2_pools[long_log_pool].pkt_size);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004636 if (!port->pool_short)
4637 return -ENOMEM;
4638
Stefan Chulski576193f2018-03-05 15:16:54 +01004639 port->pool_short->port_map |= BIT(port->id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004640
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004641 for (rxq = 0; rxq < port->nrxqs; rxq++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004642 mvpp2_rxq_short_pool_set(port, rxq,
4643 port->pool_short->id);
4644 }
4645
4646 return 0;
4647}
4648
4649static int mvpp2_bm_update_mtu(struct net_device *dev, int mtu)
4650{
4651 struct mvpp2_port *port = netdev_priv(dev);
Stefan Chulski576193f2018-03-05 15:16:54 +01004652 enum mvpp2_bm_pool_log_num new_long_pool;
4653 int pkt_size = MVPP2_RX_PKT_SIZE(mtu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004654
Stefan Chulski576193f2018-03-05 15:16:54 +01004655 /* If port MTU is higher than 1518B:
4656 * HW Long pool - SW Jumbo pool, HW Short pool - SW Long pool
4657 * else: HW Long pool - SW Long pool, HW Short pool - SW Short pool
4658 */
4659 if (pkt_size > MVPP2_BM_LONG_PKT_SIZE)
4660 new_long_pool = MVPP2_BM_JUMBO;
4661 else
4662 new_long_pool = MVPP2_BM_LONG;
4663
4664 if (new_long_pool != port->pool_long->id) {
4665 /* Remove port from old short & long pool */
4666 port->pool_long = mvpp2_bm_pool_use(port, port->pool_long->id,
4667 port->pool_long->pkt_size);
4668 port->pool_long->port_map &= ~BIT(port->id);
4669 port->pool_long = NULL;
4670
4671 port->pool_short = mvpp2_bm_pool_use(port, port->pool_short->id,
4672 port->pool_short->pkt_size);
4673 port->pool_short->port_map &= ~BIT(port->id);
4674 port->pool_short = NULL;
4675
4676 port->pkt_size = pkt_size;
4677
4678 /* Add port to new short & long pool */
4679 mvpp2_swf_bm_pool_init(port);
4680
4681 /* Update L4 checksum when jumbo enable/disable on port */
4682 if (new_long_pool == MVPP2_BM_JUMBO && port->id != 0) {
4683 dev->features &= ~(NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM);
4684 dev->hw_features &= ~(NETIF_F_IP_CSUM |
4685 NETIF_F_IPV6_CSUM);
4686 } else {
4687 dev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
4688 dev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
4689 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004690 }
4691
Marcin Wojtas3f518502014-07-10 16:52:13 -03004692 dev->mtu = mtu;
Stefan Chulski576193f2018-03-05 15:16:54 +01004693 dev->wanted_features = dev->features;
4694
Marcin Wojtas3f518502014-07-10 16:52:13 -03004695 netdev_update_features(dev);
4696 return 0;
4697}
4698
4699static inline void mvpp2_interrupts_enable(struct mvpp2_port *port)
4700{
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004701 int i, sw_thread_mask = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004702
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004703 for (i = 0; i < port->nqvecs; i++)
4704 sw_thread_mask |= port->qvecs[i].sw_thread_mask;
4705
Marcin Wojtas3f518502014-07-10 16:52:13 -03004706 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004707 MVPP2_ISR_ENABLE_INTERRUPT(sw_thread_mask));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004708}
4709
4710static inline void mvpp2_interrupts_disable(struct mvpp2_port *port)
4711{
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004712 int i, sw_thread_mask = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004713
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004714 for (i = 0; i < port->nqvecs; i++)
4715 sw_thread_mask |= port->qvecs[i].sw_thread_mask;
4716
Marcin Wojtas3f518502014-07-10 16:52:13 -03004717 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004718 MVPP2_ISR_DISABLE_INTERRUPT(sw_thread_mask));
4719}
4720
4721static inline void mvpp2_qvec_interrupt_enable(struct mvpp2_queue_vector *qvec)
4722{
4723 struct mvpp2_port *port = qvec->port;
4724
4725 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
4726 MVPP2_ISR_ENABLE_INTERRUPT(qvec->sw_thread_mask));
4727}
4728
4729static inline void mvpp2_qvec_interrupt_disable(struct mvpp2_queue_vector *qvec)
4730{
4731 struct mvpp2_port *port = qvec->port;
4732
4733 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
4734 MVPP2_ISR_DISABLE_INTERRUPT(qvec->sw_thread_mask));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004735}
4736
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004737/* Mask the current CPU's Rx/Tx interrupts
4738 * Called by on_each_cpu(), guaranteed to run with migration disabled,
4739 * using smp_processor_id() is OK.
4740 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004741static void mvpp2_interrupts_mask(void *arg)
4742{
4743 struct mvpp2_port *port = arg;
4744
Thomas Petazzonia7868412017-03-07 16:53:13 +01004745 mvpp2_percpu_write(port->priv, smp_processor_id(),
4746 MVPP2_ISR_RX_TX_MASK_REG(port->id), 0);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004747}
4748
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004749/* Unmask the current CPU's Rx/Tx interrupts.
4750 * Called by on_each_cpu(), guaranteed to run with migration disabled,
4751 * using smp_processor_id() is OK.
4752 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004753static void mvpp2_interrupts_unmask(void *arg)
4754{
4755 struct mvpp2_port *port = arg;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02004756 u32 val;
4757
4758 val = MVPP2_CAUSE_MISC_SUM_MASK |
4759 MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
4760 if (port->has_tx_irqs)
4761 val |= MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004762
Thomas Petazzonia7868412017-03-07 16:53:13 +01004763 mvpp2_percpu_write(port->priv, smp_processor_id(),
Thomas Petazzoni213f4282017-08-03 10:42:00 +02004764 MVPP2_ISR_RX_TX_MASK_REG(port->id), val);
4765}
4766
4767static void
4768mvpp2_shared_interrupt_mask_unmask(struct mvpp2_port *port, bool mask)
4769{
4770 u32 val;
4771 int i;
4772
4773 if (port->priv->hw_version != MVPP22)
4774 return;
4775
4776 if (mask)
4777 val = 0;
4778 else
4779 val = MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
4780
4781 for (i = 0; i < port->nqvecs; i++) {
4782 struct mvpp2_queue_vector *v = port->qvecs + i;
4783
4784 if (v->type != MVPP2_QUEUE_VECTOR_SHARED)
4785 continue;
4786
4787 mvpp2_percpu_write(port->priv, v->sw_thread_id,
4788 MVPP2_ISR_RX_TX_MASK_REG(port->id), val);
4789 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004790}
4791
4792/* Port configuration routines */
4793
Antoine Ténartf84bf382017-08-22 19:08:27 +02004794static void mvpp22_gop_init_rgmii(struct mvpp2_port *port)
4795{
4796 struct mvpp2 *priv = port->priv;
4797 u32 val;
4798
4799 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
4800 val |= GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT;
4801 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
4802
4803 regmap_read(priv->sysctrl_base, GENCONF_CTRL0, &val);
4804 if (port->gop_id == 2)
4805 val |= GENCONF_CTRL0_PORT0_RGMII | GENCONF_CTRL0_PORT1_RGMII;
4806 else if (port->gop_id == 3)
4807 val |= GENCONF_CTRL0_PORT1_RGMII_MII;
4808 regmap_write(priv->sysctrl_base, GENCONF_CTRL0, val);
4809}
4810
4811static void mvpp22_gop_init_sgmii(struct mvpp2_port *port)
4812{
4813 struct mvpp2 *priv = port->priv;
4814 u32 val;
4815
4816 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
4817 val |= GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT |
4818 GENCONF_PORT_CTRL0_RX_DATA_SAMPLE;
4819 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
4820
4821 if (port->gop_id > 1) {
4822 regmap_read(priv->sysctrl_base, GENCONF_CTRL0, &val);
4823 if (port->gop_id == 2)
4824 val &= ~GENCONF_CTRL0_PORT0_RGMII;
4825 else if (port->gop_id == 3)
4826 val &= ~GENCONF_CTRL0_PORT1_RGMII_MII;
4827 regmap_write(priv->sysctrl_base, GENCONF_CTRL0, val);
4828 }
4829}
4830
4831static void mvpp22_gop_init_10gkr(struct mvpp2_port *port)
4832{
4833 struct mvpp2 *priv = port->priv;
4834 void __iomem *mpcs = priv->iface_base + MVPP22_MPCS_BASE(port->gop_id);
4835 void __iomem *xpcs = priv->iface_base + MVPP22_XPCS_BASE(port->gop_id);
4836 u32 val;
4837
4838 /* XPCS */
4839 val = readl(xpcs + MVPP22_XPCS_CFG0);
4840 val &= ~(MVPP22_XPCS_CFG0_PCS_MODE(0x3) |
4841 MVPP22_XPCS_CFG0_ACTIVE_LANE(0x3));
4842 val |= MVPP22_XPCS_CFG0_ACTIVE_LANE(2);
4843 writel(val, xpcs + MVPP22_XPCS_CFG0);
4844
4845 /* MPCS */
4846 val = readl(mpcs + MVPP22_MPCS_CTRL);
4847 val &= ~MVPP22_MPCS_CTRL_FWD_ERR_CONN;
4848 writel(val, mpcs + MVPP22_MPCS_CTRL);
4849
4850 val = readl(mpcs + MVPP22_MPCS_CLK_RESET);
4851 val &= ~(MVPP22_MPCS_CLK_RESET_DIV_RATIO(0x7) | MAC_CLK_RESET_MAC |
4852 MAC_CLK_RESET_SD_RX | MAC_CLK_RESET_SD_TX);
4853 val |= MVPP22_MPCS_CLK_RESET_DIV_RATIO(1);
4854 writel(val, mpcs + MVPP22_MPCS_CLK_RESET);
4855
4856 val &= ~MVPP22_MPCS_CLK_RESET_DIV_SET;
4857 val |= MAC_CLK_RESET_MAC | MAC_CLK_RESET_SD_RX | MAC_CLK_RESET_SD_TX;
4858 writel(val, mpcs + MVPP22_MPCS_CLK_RESET);
4859}
4860
4861static int mvpp22_gop_init(struct mvpp2_port *port)
4862{
4863 struct mvpp2 *priv = port->priv;
4864 u32 val;
4865
4866 if (!priv->sysctrl_base)
4867 return 0;
4868
4869 switch (port->phy_interface) {
4870 case PHY_INTERFACE_MODE_RGMII:
4871 case PHY_INTERFACE_MODE_RGMII_ID:
4872 case PHY_INTERFACE_MODE_RGMII_RXID:
4873 case PHY_INTERFACE_MODE_RGMII_TXID:
4874 if (port->gop_id == 0)
4875 goto invalid_conf;
4876 mvpp22_gop_init_rgmii(port);
4877 break;
4878 case PHY_INTERFACE_MODE_SGMII:
4879 mvpp22_gop_init_sgmii(port);
4880 break;
4881 case PHY_INTERFACE_MODE_10GKR:
4882 if (port->gop_id != 0)
4883 goto invalid_conf;
4884 mvpp22_gop_init_10gkr(port);
4885 break;
4886 default:
4887 goto unsupported_conf;
4888 }
4889
4890 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL1, &val);
4891 val |= GENCONF_PORT_CTRL1_RESET(port->gop_id) |
4892 GENCONF_PORT_CTRL1_EN(port->gop_id);
4893 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL1, val);
4894
4895 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
4896 val |= GENCONF_PORT_CTRL0_CLK_DIV_PHASE_CLR;
4897 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
4898
4899 regmap_read(priv->sysctrl_base, GENCONF_SOFT_RESET1, &val);
4900 val |= GENCONF_SOFT_RESET1_GOP;
4901 regmap_write(priv->sysctrl_base, GENCONF_SOFT_RESET1, val);
4902
4903unsupported_conf:
4904 return 0;
4905
4906invalid_conf:
4907 netdev_err(port->dev, "Invalid port configuration\n");
4908 return -EINVAL;
4909}
4910
Antoine Tenartfd3651b2017-09-01 11:04:54 +02004911static void mvpp22_gop_unmask_irq(struct mvpp2_port *port)
4912{
4913 u32 val;
4914
4915 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
4916 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4917 /* Enable the GMAC link status irq for this port */
4918 val = readl(port->base + MVPP22_GMAC_INT_SUM_MASK);
4919 val |= MVPP22_GMAC_INT_SUM_MASK_LINK_STAT;
4920 writel(val, port->base + MVPP22_GMAC_INT_SUM_MASK);
4921 }
4922
4923 if (port->gop_id == 0) {
4924 /* Enable the XLG/GIG irqs for this port */
4925 val = readl(port->base + MVPP22_XLG_EXT_INT_MASK);
4926 if (port->phy_interface == PHY_INTERFACE_MODE_10GKR)
4927 val |= MVPP22_XLG_EXT_INT_MASK_XLG;
4928 else
4929 val |= MVPP22_XLG_EXT_INT_MASK_GIG;
4930 writel(val, port->base + MVPP22_XLG_EXT_INT_MASK);
4931 }
4932}
4933
4934static void mvpp22_gop_mask_irq(struct mvpp2_port *port)
4935{
4936 u32 val;
4937
4938 if (port->gop_id == 0) {
4939 val = readl(port->base + MVPP22_XLG_EXT_INT_MASK);
4940 val &= ~(MVPP22_XLG_EXT_INT_MASK_XLG |
4941 MVPP22_XLG_EXT_INT_MASK_GIG);
4942 writel(val, port->base + MVPP22_XLG_EXT_INT_MASK);
4943 }
4944
4945 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
4946 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4947 val = readl(port->base + MVPP22_GMAC_INT_SUM_MASK);
4948 val &= ~MVPP22_GMAC_INT_SUM_MASK_LINK_STAT;
4949 writel(val, port->base + MVPP22_GMAC_INT_SUM_MASK);
4950 }
4951}
4952
4953static void mvpp22_gop_setup_irq(struct mvpp2_port *port)
4954{
4955 u32 val;
4956
4957 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
4958 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4959 val = readl(port->base + MVPP22_GMAC_INT_MASK);
4960 val |= MVPP22_GMAC_INT_MASK_LINK_STAT;
4961 writel(val, port->base + MVPP22_GMAC_INT_MASK);
4962 }
4963
4964 if (port->gop_id == 0) {
4965 val = readl(port->base + MVPP22_XLG_INT_MASK);
4966 val |= MVPP22_XLG_INT_MASK_LINK;
4967 writel(val, port->base + MVPP22_XLG_INT_MASK);
4968 }
4969
4970 mvpp22_gop_unmask_irq(port);
4971}
4972
Antoine Tenart542897d2017-08-30 10:29:15 +02004973static int mvpp22_comphy_init(struct mvpp2_port *port)
4974{
4975 enum phy_mode mode;
4976 int ret;
4977
4978 if (!port->comphy)
4979 return 0;
4980
4981 switch (port->phy_interface) {
4982 case PHY_INTERFACE_MODE_SGMII:
4983 mode = PHY_MODE_SGMII;
4984 break;
4985 case PHY_INTERFACE_MODE_10GKR:
4986 mode = PHY_MODE_10GKR;
4987 break;
4988 default:
4989 return -EINVAL;
4990 }
4991
4992 ret = phy_set_mode(port->comphy, mode);
4993 if (ret)
4994 return ret;
4995
4996 return phy_power_on(port->comphy);
4997}
4998
Antoine Ténart39193572017-08-22 19:08:24 +02004999static void mvpp2_port_mii_gmac_configure_mode(struct mvpp2_port *port)
5000{
5001 u32 val;
5002
5003 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
5004 val = readl(port->base + MVPP22_GMAC_CTRL_4_REG);
5005 val |= MVPP22_CTRL4_SYNC_BYPASS_DIS | MVPP22_CTRL4_DP_CLK_SEL |
5006 MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
5007 val &= ~MVPP22_CTRL4_EXT_PIN_GMII_SEL;
5008 writel(val, port->base + MVPP22_GMAC_CTRL_4_REG);
Antoine Tenart1df22702017-09-01 11:04:52 +02005009 } else if (phy_interface_mode_is_rgmii(port->phy_interface)) {
Antoine Ténart39193572017-08-22 19:08:24 +02005010 val = readl(port->base + MVPP22_GMAC_CTRL_4_REG);
5011 val |= MVPP22_CTRL4_EXT_PIN_GMII_SEL |
5012 MVPP22_CTRL4_SYNC_BYPASS_DIS |
5013 MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
5014 val &= ~MVPP22_CTRL4_DP_CLK_SEL;
5015 writel(val, port->base + MVPP22_GMAC_CTRL_4_REG);
Antoine Ténart39193572017-08-22 19:08:24 +02005016 }
5017
5018 /* The port is connected to a copper PHY */
5019 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
5020 val &= ~MVPP2_GMAC_PORT_TYPE_MASK;
5021 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
5022
5023 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5024 val |= MVPP2_GMAC_IN_BAND_AUTONEG_BYPASS |
5025 MVPP2_GMAC_AN_SPEED_EN | MVPP2_GMAC_FLOW_CTRL_AUTONEG |
5026 MVPP2_GMAC_AN_DUPLEX_EN;
5027 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
5028 val |= MVPP2_GMAC_IN_BAND_AUTONEG;
5029 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5030}
5031
5032static void mvpp2_port_mii_gmac_configure(struct mvpp2_port *port)
5033{
5034 u32 val;
5035
5036 /* Force link down */
5037 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5038 val &= ~MVPP2_GMAC_FORCE_LINK_PASS;
5039 val |= MVPP2_GMAC_FORCE_LINK_DOWN;
5040 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5041
5042 /* Set the GMAC in a reset state */
5043 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
5044 val |= MVPP2_GMAC_PORT_RESET_MASK;
5045 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
5046
5047 /* Configure the PCS and in-band AN */
5048 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
5049 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
5050 val |= MVPP2_GMAC_INBAND_AN_MASK | MVPP2_GMAC_PCS_ENABLE_MASK;
Antoine Tenart1df22702017-09-01 11:04:52 +02005051 } else if (phy_interface_mode_is_rgmii(port->phy_interface)) {
Antoine Ténart39193572017-08-22 19:08:24 +02005052 val &= ~MVPP2_GMAC_PCS_ENABLE_MASK;
Antoine Ténart39193572017-08-22 19:08:24 +02005053 }
5054 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
5055
5056 mvpp2_port_mii_gmac_configure_mode(port);
5057
5058 /* Unset the GMAC reset state */
5059 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
5060 val &= ~MVPP2_GMAC_PORT_RESET_MASK;
5061 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
5062
5063 /* Stop forcing link down */
5064 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5065 val &= ~MVPP2_GMAC_FORCE_LINK_DOWN;
5066 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5067}
5068
Antoine Ténart77321952017-08-22 19:08:25 +02005069static void mvpp2_port_mii_xlg_configure(struct mvpp2_port *port)
5070{
5071 u32 val;
5072
5073 if (port->gop_id != 0)
5074 return;
5075
5076 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
5077 val |= MVPP22_XLG_CTRL0_RX_FLOW_CTRL_EN;
5078 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
5079
5080 val = readl(port->base + MVPP22_XLG_CTRL4_REG);
5081 val &= ~MVPP22_XLG_CTRL4_MACMODSELECT_GMAC;
5082 val |= MVPP22_XLG_CTRL4_FWD_FC | MVPP22_XLG_CTRL4_FWD_PFC;
5083 writel(val, port->base + MVPP22_XLG_CTRL4_REG);
5084}
5085
Thomas Petazzoni26975822017-03-07 16:53:14 +01005086static void mvpp22_port_mii_set(struct mvpp2_port *port)
5087{
5088 u32 val;
5089
Thomas Petazzoni26975822017-03-07 16:53:14 +01005090 /* Only GOP port 0 has an XLG MAC */
5091 if (port->gop_id == 0) {
5092 val = readl(port->base + MVPP22_XLG_CTRL3_REG);
5093 val &= ~MVPP22_XLG_CTRL3_MACMODESELECT_MASK;
Antoine Ténart725757a2017-06-12 16:01:39 +02005094
5095 if (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
5096 port->phy_interface == PHY_INTERFACE_MODE_10GKR)
5097 val |= MVPP22_XLG_CTRL3_MACMODESELECT_10G;
5098 else
5099 val |= MVPP22_XLG_CTRL3_MACMODESELECT_GMAC;
5100
Thomas Petazzoni26975822017-03-07 16:53:14 +01005101 writel(val, port->base + MVPP22_XLG_CTRL3_REG);
5102 }
Thomas Petazzoni26975822017-03-07 16:53:14 +01005103}
5104
Marcin Wojtas3f518502014-07-10 16:52:13 -03005105static void mvpp2_port_mii_set(struct mvpp2_port *port)
5106{
Thomas Petazzoni26975822017-03-07 16:53:14 +01005107 if (port->priv->hw_version == MVPP22)
5108 mvpp22_port_mii_set(port);
5109
Antoine Tenart1df22702017-09-01 11:04:52 +02005110 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
Antoine Ténart39193572017-08-22 19:08:24 +02005111 port->phy_interface == PHY_INTERFACE_MODE_SGMII)
5112 mvpp2_port_mii_gmac_configure(port);
Antoine Ténart77321952017-08-22 19:08:25 +02005113 else if (port->phy_interface == PHY_INTERFACE_MODE_10GKR)
5114 mvpp2_port_mii_xlg_configure(port);
Marcin Wojtas08a23752014-07-21 13:48:12 -03005115}
5116
5117static void mvpp2_port_fc_adv_enable(struct mvpp2_port *port)
5118{
5119 u32 val;
5120
5121 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5122 val |= MVPP2_GMAC_FC_ADV_EN;
5123 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005124}
5125
5126static void mvpp2_port_enable(struct mvpp2_port *port)
5127{
5128 u32 val;
5129
Antoine Ténart725757a2017-06-12 16:01:39 +02005130 /* Only GOP port 0 has an XLG MAC */
5131 if (port->gop_id == 0 &&
5132 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
5133 port->phy_interface == PHY_INTERFACE_MODE_10GKR)) {
5134 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
5135 val |= MVPP22_XLG_CTRL0_PORT_EN |
5136 MVPP22_XLG_CTRL0_MAC_RESET_DIS;
5137 val &= ~MVPP22_XLG_CTRL0_MIB_CNT_DIS;
5138 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
5139 } else {
5140 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
5141 val |= MVPP2_GMAC_PORT_EN_MASK;
5142 val |= MVPP2_GMAC_MIB_CNTR_EN_MASK;
5143 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
5144 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03005145}
5146
5147static void mvpp2_port_disable(struct mvpp2_port *port)
5148{
5149 u32 val;
5150
Antoine Ténart725757a2017-06-12 16:01:39 +02005151 /* Only GOP port 0 has an XLG MAC */
5152 if (port->gop_id == 0 &&
5153 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
5154 port->phy_interface == PHY_INTERFACE_MODE_10GKR)) {
5155 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
5156 val &= ~(MVPP22_XLG_CTRL0_PORT_EN |
5157 MVPP22_XLG_CTRL0_MAC_RESET_DIS);
5158 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
5159 } else {
5160 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
5161 val &= ~(MVPP2_GMAC_PORT_EN_MASK);
5162 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
5163 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03005164}
5165
5166/* Set IEEE 802.3x Flow Control Xon Packet Transmission Mode */
5167static void mvpp2_port_periodic_xon_disable(struct mvpp2_port *port)
5168{
5169 u32 val;
5170
5171 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG) &
5172 ~MVPP2_GMAC_PERIODIC_XON_EN_MASK;
5173 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
5174}
5175
5176/* Configure loopback port */
5177static void mvpp2_port_loopback_set(struct mvpp2_port *port)
5178{
5179 u32 val;
5180
5181 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
5182
5183 if (port->speed == 1000)
5184 val |= MVPP2_GMAC_GMII_LB_EN_MASK;
5185 else
5186 val &= ~MVPP2_GMAC_GMII_LB_EN_MASK;
5187
5188 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
5189 val |= MVPP2_GMAC_PCS_LB_EN_MASK;
5190 else
5191 val &= ~MVPP2_GMAC_PCS_LB_EN_MASK;
5192
5193 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
5194}
5195
Miquel Raynal118d6292017-11-06 22:56:53 +01005196struct mvpp2_ethtool_counter {
5197 unsigned int offset;
5198 const char string[ETH_GSTRING_LEN];
5199 bool reg_is_64b;
5200};
5201
5202static u64 mvpp2_read_count(struct mvpp2_port *port,
5203 const struct mvpp2_ethtool_counter *counter)
5204{
5205 u64 val;
5206
5207 val = readl(port->stats_base + counter->offset);
5208 if (counter->reg_is_64b)
5209 val += (u64)readl(port->stats_base + counter->offset + 4) << 32;
5210
5211 return val;
5212}
5213
5214/* Due to the fact that software statistics and hardware statistics are, by
5215 * design, incremented at different moments in the chain of packet processing,
5216 * it is very likely that incoming packets could have been dropped after being
5217 * counted by hardware but before reaching software statistics (most probably
5218 * multicast packets), and in the oppposite way, during transmission, FCS bytes
5219 * are added in between as well as TSO skb will be split and header bytes added.
5220 * Hence, statistics gathered from userspace with ifconfig (software) and
5221 * ethtool (hardware) cannot be compared.
5222 */
5223static const struct mvpp2_ethtool_counter mvpp2_ethtool_regs[] = {
5224 { MVPP2_MIB_GOOD_OCTETS_RCVD, "good_octets_received", true },
5225 { MVPP2_MIB_BAD_OCTETS_RCVD, "bad_octets_received" },
5226 { MVPP2_MIB_CRC_ERRORS_SENT, "crc_errors_sent" },
5227 { MVPP2_MIB_UNICAST_FRAMES_RCVD, "unicast_frames_received" },
5228 { MVPP2_MIB_BROADCAST_FRAMES_RCVD, "broadcast_frames_received" },
5229 { MVPP2_MIB_MULTICAST_FRAMES_RCVD, "multicast_frames_received" },
5230 { MVPP2_MIB_FRAMES_64_OCTETS, "frames_64_octets" },
5231 { MVPP2_MIB_FRAMES_65_TO_127_OCTETS, "frames_65_to_127_octet" },
5232 { MVPP2_MIB_FRAMES_128_TO_255_OCTETS, "frames_128_to_255_octet" },
5233 { MVPP2_MIB_FRAMES_256_TO_511_OCTETS, "frames_256_to_511_octet" },
5234 { MVPP2_MIB_FRAMES_512_TO_1023_OCTETS, "frames_512_to_1023_octet" },
5235 { MVPP2_MIB_FRAMES_1024_TO_MAX_OCTETS, "frames_1024_to_max_octet" },
5236 { MVPP2_MIB_GOOD_OCTETS_SENT, "good_octets_sent", true },
5237 { MVPP2_MIB_UNICAST_FRAMES_SENT, "unicast_frames_sent" },
5238 { MVPP2_MIB_MULTICAST_FRAMES_SENT, "multicast_frames_sent" },
5239 { MVPP2_MIB_BROADCAST_FRAMES_SENT, "broadcast_frames_sent" },
5240 { MVPP2_MIB_FC_SENT, "fc_sent" },
5241 { MVPP2_MIB_FC_RCVD, "fc_received" },
5242 { MVPP2_MIB_RX_FIFO_OVERRUN, "rx_fifo_overrun" },
5243 { MVPP2_MIB_UNDERSIZE_RCVD, "undersize_received" },
5244 { MVPP2_MIB_FRAGMENTS_RCVD, "fragments_received" },
5245 { MVPP2_MIB_OVERSIZE_RCVD, "oversize_received" },
5246 { MVPP2_MIB_JABBER_RCVD, "jabber_received" },
5247 { MVPP2_MIB_MAC_RCV_ERROR, "mac_receive_error" },
5248 { MVPP2_MIB_BAD_CRC_EVENT, "bad_crc_event" },
5249 { MVPP2_MIB_COLLISION, "collision" },
5250 { MVPP2_MIB_LATE_COLLISION, "late_collision" },
5251};
5252
5253static void mvpp2_ethtool_get_strings(struct net_device *netdev, u32 sset,
5254 u8 *data)
5255{
5256 if (sset == ETH_SS_STATS) {
5257 int i;
5258
5259 for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_regs); i++)
5260 memcpy(data + i * ETH_GSTRING_LEN,
5261 &mvpp2_ethtool_regs[i].string, ETH_GSTRING_LEN);
5262 }
5263}
5264
5265static void mvpp2_gather_hw_statistics(struct work_struct *work)
5266{
5267 struct delayed_work *del_work = to_delayed_work(work);
Miquel Raynale5c500e2017-11-08 08:59:40 +01005268 struct mvpp2_port *port = container_of(del_work, struct mvpp2_port,
5269 stats_work);
Miquel Raynal118d6292017-11-06 22:56:53 +01005270 u64 *pstats;
Miquel Raynale5c500e2017-11-08 08:59:40 +01005271 int i;
Miquel Raynal118d6292017-11-06 22:56:53 +01005272
Miquel Raynale5c500e2017-11-08 08:59:40 +01005273 mutex_lock(&port->gather_stats_lock);
Miquel Raynal118d6292017-11-06 22:56:53 +01005274
Miquel Raynale5c500e2017-11-08 08:59:40 +01005275 pstats = port->ethtool_stats;
5276 for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_regs); i++)
5277 *pstats++ += mvpp2_read_count(port, &mvpp2_ethtool_regs[i]);
Miquel Raynal118d6292017-11-06 22:56:53 +01005278
5279 /* No need to read again the counters right after this function if it
5280 * was called asynchronously by the user (ie. use of ethtool).
5281 */
Miquel Raynale5c500e2017-11-08 08:59:40 +01005282 cancel_delayed_work(&port->stats_work);
5283 queue_delayed_work(port->priv->stats_queue, &port->stats_work,
Miquel Raynal118d6292017-11-06 22:56:53 +01005284 MVPP2_MIB_COUNTERS_STATS_DELAY);
5285
Miquel Raynale5c500e2017-11-08 08:59:40 +01005286 mutex_unlock(&port->gather_stats_lock);
Miquel Raynal118d6292017-11-06 22:56:53 +01005287}
5288
5289static void mvpp2_ethtool_get_stats(struct net_device *dev,
5290 struct ethtool_stats *stats, u64 *data)
5291{
5292 struct mvpp2_port *port = netdev_priv(dev);
5293
Miquel Raynale5c500e2017-11-08 08:59:40 +01005294 /* Update statistics for the given port, then take the lock to avoid
5295 * concurrent accesses on the ethtool_stats structure during its copy.
5296 */
5297 mvpp2_gather_hw_statistics(&port->stats_work.work);
Miquel Raynal118d6292017-11-06 22:56:53 +01005298
Miquel Raynale5c500e2017-11-08 08:59:40 +01005299 mutex_lock(&port->gather_stats_lock);
Miquel Raynal118d6292017-11-06 22:56:53 +01005300 memcpy(data, port->ethtool_stats,
5301 sizeof(u64) * ARRAY_SIZE(mvpp2_ethtool_regs));
Miquel Raynale5c500e2017-11-08 08:59:40 +01005302 mutex_unlock(&port->gather_stats_lock);
Miquel Raynal118d6292017-11-06 22:56:53 +01005303}
5304
5305static int mvpp2_ethtool_get_sset_count(struct net_device *dev, int sset)
5306{
5307 if (sset == ETH_SS_STATS)
5308 return ARRAY_SIZE(mvpp2_ethtool_regs);
5309
5310 return -EOPNOTSUPP;
5311}
5312
Marcin Wojtas3f518502014-07-10 16:52:13 -03005313static void mvpp2_port_reset(struct mvpp2_port *port)
5314{
5315 u32 val;
Miquel Raynal118d6292017-11-06 22:56:53 +01005316 unsigned int i;
5317
5318 /* Read the GOP statistics to reset the hardware counters */
5319 for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_regs); i++)
5320 mvpp2_read_count(port, &mvpp2_ethtool_regs[i]);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005321
5322 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
5323 ~MVPP2_GMAC_PORT_RESET_MASK;
5324 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
5325
5326 while (readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
5327 MVPP2_GMAC_PORT_RESET_MASK)
5328 continue;
5329}
5330
5331/* Change maximum receive size of the port */
5332static inline void mvpp2_gmac_max_rx_size_set(struct mvpp2_port *port)
5333{
5334 u32 val;
5335
5336 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
5337 val &= ~MVPP2_GMAC_MAX_RX_SIZE_MASK;
5338 val |= (((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
5339 MVPP2_GMAC_MAX_RX_SIZE_OFFS);
5340 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
5341}
5342
Stefan Chulski76eb1b12017-08-22 19:08:26 +02005343/* Change maximum receive size of the port */
5344static inline void mvpp2_xlg_max_rx_size_set(struct mvpp2_port *port)
5345{
5346 u32 val;
5347
5348 val = readl(port->base + MVPP22_XLG_CTRL1_REG);
5349 val &= ~MVPP22_XLG_CTRL1_FRAMESIZELIMIT_MASK;
5350 val |= ((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
Antoine Ténartec15ecd2017-08-25 15:24:46 +02005351 MVPP22_XLG_CTRL1_FRAMESIZELIMIT_OFFS;
Stefan Chulski76eb1b12017-08-22 19:08:26 +02005352 writel(val, port->base + MVPP22_XLG_CTRL1_REG);
5353}
5354
Marcin Wojtas3f518502014-07-10 16:52:13 -03005355/* Set defaults to the MVPP2 port */
5356static void mvpp2_defaults_set(struct mvpp2_port *port)
5357{
5358 int tx_port_num, val, queue, ptxq, lrxq;
5359
Thomas Petazzoni3d9017d2017-03-07 16:53:11 +01005360 if (port->priv->hw_version == MVPP21) {
5361 /* Configure port to loopback if needed */
5362 if (port->flags & MVPP2_F_LOOPBACK)
5363 mvpp2_port_loopback_set(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005364
Thomas Petazzoni3d9017d2017-03-07 16:53:11 +01005365 /* Update TX FIFO MIN Threshold */
5366 val = readl(port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
5367 val &= ~MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK;
5368 /* Min. TX threshold must be less than minimal packet length */
5369 val |= MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(64 - 4 - 2);
5370 writel(val, port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
5371 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03005372
5373 /* Disable Legacy WRR, Disable EJP, Release from reset */
5374 tx_port_num = mvpp2_egress_port(port);
5375 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG,
5376 tx_port_num);
5377 mvpp2_write(port->priv, MVPP2_TXP_SCHED_CMD_1_REG, 0);
5378
5379 /* Close bandwidth for all queues */
5380 for (queue = 0; queue < MVPP2_MAX_TXQ; queue++) {
5381 ptxq = mvpp2_txq_phys(port->id, queue);
5382 mvpp2_write(port->priv,
5383 MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(ptxq), 0);
5384 }
5385
5386 /* Set refill period to 1 usec, refill tokens
5387 * and bucket size to maximum
5388 */
5389 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PERIOD_REG,
5390 port->priv->tclk / USEC_PER_SEC);
5391 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_REFILL_REG);
5392 val &= ~MVPP2_TXP_REFILL_PERIOD_ALL_MASK;
5393 val |= MVPP2_TXP_REFILL_PERIOD_MASK(1);
5394 val |= MVPP2_TXP_REFILL_TOKENS_ALL_MASK;
5395 mvpp2_write(port->priv, MVPP2_TXP_SCHED_REFILL_REG, val);
5396 val = MVPP2_TXP_TOKEN_SIZE_MAX;
5397 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
5398
5399 /* Set MaximumLowLatencyPacketSize value to 256 */
5400 mvpp2_write(port->priv, MVPP2_RX_CTRL_REG(port->id),
5401 MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK |
5402 MVPP2_RX_LOW_LATENCY_PKT_SIZE(256));
5403
5404 /* Enable Rx cache snoop */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005405 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005406 queue = port->rxqs[lrxq]->id;
5407 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
5408 val |= MVPP2_SNOOP_PKT_SIZE_MASK |
5409 MVPP2_SNOOP_BUF_HDR_MASK;
5410 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
5411 }
5412
5413 /* At default, mask all interrupts to all present cpus */
5414 mvpp2_interrupts_disable(port);
5415}
5416
5417/* Enable/disable receiving packets */
5418static void mvpp2_ingress_enable(struct mvpp2_port *port)
5419{
5420 u32 val;
5421 int lrxq, queue;
5422
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005423 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005424 queue = port->rxqs[lrxq]->id;
5425 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
5426 val &= ~MVPP2_RXQ_DISABLE_MASK;
5427 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
5428 }
5429}
5430
5431static void mvpp2_ingress_disable(struct mvpp2_port *port)
5432{
5433 u32 val;
5434 int lrxq, queue;
5435
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005436 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005437 queue = port->rxqs[lrxq]->id;
5438 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
5439 val |= MVPP2_RXQ_DISABLE_MASK;
5440 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
5441 }
5442}
5443
5444/* Enable transmit via physical egress queue
5445 * - HW starts take descriptors from DRAM
5446 */
5447static void mvpp2_egress_enable(struct mvpp2_port *port)
5448{
5449 u32 qmap;
5450 int queue;
5451 int tx_port_num = mvpp2_egress_port(port);
5452
5453 /* Enable all initialized TXs. */
5454 qmap = 0;
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005455 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005456 struct mvpp2_tx_queue *txq = port->txqs[queue];
5457
Markus Elfringdbbb2f02017-04-17 14:07:52 +02005458 if (txq->descs)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005459 qmap |= (1 << queue);
5460 }
5461
5462 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
5463 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG, qmap);
5464}
5465
5466/* Disable transmit via physical egress queue
5467 * - HW doesn't take descriptors from DRAM
5468 */
5469static void mvpp2_egress_disable(struct mvpp2_port *port)
5470{
5471 u32 reg_data;
5472 int delay;
5473 int tx_port_num = mvpp2_egress_port(port);
5474
5475 /* Issue stop command for active channels only */
5476 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
5477 reg_data = (mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG)) &
5478 MVPP2_TXP_SCHED_ENQ_MASK;
5479 if (reg_data != 0)
5480 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG,
5481 (reg_data << MVPP2_TXP_SCHED_DISQ_OFFSET));
5482
5483 /* Wait for all Tx activity to terminate. */
5484 delay = 0;
5485 do {
5486 if (delay >= MVPP2_TX_DISABLE_TIMEOUT_MSEC) {
5487 netdev_warn(port->dev,
5488 "Tx stop timed out, status=0x%08x\n",
5489 reg_data);
5490 break;
5491 }
5492 mdelay(1);
5493 delay++;
5494
5495 /* Check port TX Command register that all
5496 * Tx queues are stopped
5497 */
5498 reg_data = mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG);
5499 } while (reg_data & MVPP2_TXP_SCHED_ENQ_MASK);
5500}
5501
5502/* Rx descriptors helper methods */
5503
5504/* Get number of Rx descriptors occupied by received packets */
5505static inline int
5506mvpp2_rxq_received(struct mvpp2_port *port, int rxq_id)
5507{
5508 u32 val = mvpp2_read(port->priv, MVPP2_RXQ_STATUS_REG(rxq_id));
5509
5510 return val & MVPP2_RXQ_OCCUPIED_MASK;
5511}
5512
5513/* Update Rx queue status with the number of occupied and available
5514 * Rx descriptor slots.
5515 */
5516static inline void
5517mvpp2_rxq_status_update(struct mvpp2_port *port, int rxq_id,
5518 int used_count, int free_count)
5519{
5520 /* Decrement the number of used descriptors and increment count
5521 * increment the number of free descriptors.
5522 */
5523 u32 val = used_count | (free_count << MVPP2_RXQ_NUM_NEW_OFFSET);
5524
5525 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_UPDATE_REG(rxq_id), val);
5526}
5527
5528/* Get pointer to next RX descriptor to be processed by SW */
5529static inline struct mvpp2_rx_desc *
5530mvpp2_rxq_next_desc_get(struct mvpp2_rx_queue *rxq)
5531{
5532 int rx_desc = rxq->next_desc_to_proc;
5533
5534 rxq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(rxq, rx_desc);
5535 prefetch(rxq->descs + rxq->next_desc_to_proc);
5536 return rxq->descs + rx_desc;
5537}
5538
5539/* Set rx queue offset */
5540static void mvpp2_rxq_offset_set(struct mvpp2_port *port,
5541 int prxq, int offset)
5542{
5543 u32 val;
5544
5545 /* Convert offset from bytes to units of 32 bytes */
5546 offset = offset >> 5;
5547
5548 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
5549 val &= ~MVPP2_RXQ_PACKET_OFFSET_MASK;
5550
5551 /* Offset is in */
5552 val |= ((offset << MVPP2_RXQ_PACKET_OFFSET_OFFS) &
5553 MVPP2_RXQ_PACKET_OFFSET_MASK);
5554
5555 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
5556}
5557
Marcin Wojtas3f518502014-07-10 16:52:13 -03005558/* Tx descriptors helper methods */
5559
Marcin Wojtas3f518502014-07-10 16:52:13 -03005560/* Get pointer to next Tx descriptor to be processed (send) by HW */
5561static struct mvpp2_tx_desc *
5562mvpp2_txq_next_desc_get(struct mvpp2_tx_queue *txq)
5563{
5564 int tx_desc = txq->next_desc_to_proc;
5565
5566 txq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(txq, tx_desc);
5567 return txq->descs + tx_desc;
5568}
5569
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005570/* Update HW with number of aggregated Tx descriptors to be sent
5571 *
5572 * Called only from mvpp2_tx(), so migration is disabled, using
5573 * smp_processor_id() is OK.
5574 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03005575static void mvpp2_aggr_txq_pend_desc_add(struct mvpp2_port *port, int pending)
5576{
5577 /* aggregated access - relevant TXQ number is written in TX desc */
Thomas Petazzonia7868412017-03-07 16:53:13 +01005578 mvpp2_percpu_write(port->priv, smp_processor_id(),
5579 MVPP2_AGGR_TXQ_UPDATE_REG, pending);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005580}
5581
5582
5583/* Check if there are enough free descriptors in aggregated txq.
5584 * If not, update the number of occupied descriptors and repeat the check.
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005585 *
5586 * Called only from mvpp2_tx(), so migration is disabled, using
5587 * smp_processor_id() is OK.
Marcin Wojtas3f518502014-07-10 16:52:13 -03005588 */
5589static int mvpp2_aggr_desc_num_check(struct mvpp2 *priv,
5590 struct mvpp2_tx_queue *aggr_txq, int num)
5591{
Antoine Tenart02856a32017-10-30 11:23:32 +01005592 if ((aggr_txq->count + num) > MVPP2_AGGR_TXQ_SIZE) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005593 /* Update number of occupied aggregated Tx descriptors */
5594 int cpu = smp_processor_id();
5595 u32 val = mvpp2_read(priv, MVPP2_AGGR_TXQ_STATUS_REG(cpu));
5596
5597 aggr_txq->count = val & MVPP2_AGGR_TXQ_PENDING_MASK;
5598 }
5599
Antoine Tenart02856a32017-10-30 11:23:32 +01005600 if ((aggr_txq->count + num) > MVPP2_AGGR_TXQ_SIZE)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005601 return -ENOMEM;
5602
5603 return 0;
5604}
5605
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005606/* Reserved Tx descriptors allocation request
5607 *
5608 * Called only from mvpp2_txq_reserved_desc_num_proc(), itself called
5609 * only by mvpp2_tx(), so migration is disabled, using
5610 * smp_processor_id() is OK.
5611 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03005612static int mvpp2_txq_alloc_reserved_desc(struct mvpp2 *priv,
5613 struct mvpp2_tx_queue *txq, int num)
5614{
5615 u32 val;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005616 int cpu = smp_processor_id();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005617
5618 val = (txq->id << MVPP2_TXQ_RSVD_REQ_Q_OFFSET) | num;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005619 mvpp2_percpu_write(priv, cpu, MVPP2_TXQ_RSVD_REQ_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005620
Thomas Petazzonia7868412017-03-07 16:53:13 +01005621 val = mvpp2_percpu_read(priv, cpu, MVPP2_TXQ_RSVD_RSLT_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005622
5623 return val & MVPP2_TXQ_RSVD_RSLT_MASK;
5624}
5625
5626/* Check if there are enough reserved descriptors for transmission.
5627 * If not, request chunk of reserved descriptors and check again.
5628 */
5629static int mvpp2_txq_reserved_desc_num_proc(struct mvpp2 *priv,
5630 struct mvpp2_tx_queue *txq,
5631 struct mvpp2_txq_pcpu *txq_pcpu,
5632 int num)
5633{
5634 int req, cpu, desc_count;
5635
5636 if (txq_pcpu->reserved_num >= num)
5637 return 0;
5638
5639 /* Not enough descriptors reserved! Update the reserved descriptor
5640 * count and check again.
5641 */
5642
5643 desc_count = 0;
5644 /* Compute total of used descriptors */
5645 for_each_present_cpu(cpu) {
5646 struct mvpp2_txq_pcpu *txq_pcpu_aux;
5647
5648 txq_pcpu_aux = per_cpu_ptr(txq->pcpu, cpu);
5649 desc_count += txq_pcpu_aux->count;
5650 desc_count += txq_pcpu_aux->reserved_num;
5651 }
5652
5653 req = max(MVPP2_CPU_DESC_CHUNK, num - txq_pcpu->reserved_num);
5654 desc_count += req;
5655
5656 if (desc_count >
5657 (txq->size - (num_present_cpus() * MVPP2_CPU_DESC_CHUNK)))
5658 return -ENOMEM;
5659
5660 txq_pcpu->reserved_num += mvpp2_txq_alloc_reserved_desc(priv, txq, req);
5661
5662 /* OK, the descriptor cound has been updated: check again. */
5663 if (txq_pcpu->reserved_num < num)
5664 return -ENOMEM;
5665 return 0;
5666}
5667
5668/* Release the last allocated Tx descriptor. Useful to handle DMA
5669 * mapping failures in the Tx path.
5670 */
5671static void mvpp2_txq_desc_put(struct mvpp2_tx_queue *txq)
5672{
5673 if (txq->next_desc_to_proc == 0)
5674 txq->next_desc_to_proc = txq->last_desc - 1;
5675 else
5676 txq->next_desc_to_proc--;
5677}
5678
5679/* Set Tx descriptors fields relevant for CSUM calculation */
5680static u32 mvpp2_txq_desc_csum(int l3_offs, int l3_proto,
5681 int ip_hdr_len, int l4_proto)
5682{
5683 u32 command;
5684
5685 /* fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
5686 * G_L4_chk, L4_type required only for checksum calculation
5687 */
5688 command = (l3_offs << MVPP2_TXD_L3_OFF_SHIFT);
5689 command |= (ip_hdr_len << MVPP2_TXD_IP_HLEN_SHIFT);
5690 command |= MVPP2_TXD_IP_CSUM_DISABLE;
5691
5692 if (l3_proto == swab16(ETH_P_IP)) {
5693 command &= ~MVPP2_TXD_IP_CSUM_DISABLE; /* enable IPv4 csum */
5694 command &= ~MVPP2_TXD_L3_IP6; /* enable IPv4 */
5695 } else {
5696 command |= MVPP2_TXD_L3_IP6; /* enable IPv6 */
5697 }
5698
5699 if (l4_proto == IPPROTO_TCP) {
5700 command &= ~MVPP2_TXD_L4_UDP; /* enable TCP */
5701 command &= ~MVPP2_TXD_L4_CSUM_FRAG; /* generate L4 csum */
5702 } else if (l4_proto == IPPROTO_UDP) {
5703 command |= MVPP2_TXD_L4_UDP; /* enable UDP */
5704 command &= ~MVPP2_TXD_L4_CSUM_FRAG; /* generate L4 csum */
5705 } else {
5706 command |= MVPP2_TXD_L4_CSUM_NOT;
5707 }
5708
5709 return command;
5710}
5711
5712/* Get number of sent descriptors and decrement counter.
5713 * The number of sent descriptors is returned.
5714 * Per-CPU access
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005715 *
5716 * Called only from mvpp2_txq_done(), called from mvpp2_tx()
5717 * (migration disabled) and from the TX completion tasklet (migration
5718 * disabled) so using smp_processor_id() is OK.
Marcin Wojtas3f518502014-07-10 16:52:13 -03005719 */
5720static inline int mvpp2_txq_sent_desc_proc(struct mvpp2_port *port,
5721 struct mvpp2_tx_queue *txq)
5722{
5723 u32 val;
5724
5725 /* Reading status reg resets transmitted descriptor counter */
Thomas Petazzonia7868412017-03-07 16:53:13 +01005726 val = mvpp2_percpu_read(port->priv, smp_processor_id(),
5727 MVPP2_TXQ_SENT_REG(txq->id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005728
5729 return (val & MVPP2_TRANSMITTED_COUNT_MASK) >>
5730 MVPP2_TRANSMITTED_COUNT_OFFSET;
5731}
5732
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005733/* Called through on_each_cpu(), so runs on all CPUs, with migration
5734 * disabled, therefore using smp_processor_id() is OK.
5735 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03005736static void mvpp2_txq_sent_counter_clear(void *arg)
5737{
5738 struct mvpp2_port *port = arg;
5739 int queue;
5740
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005741 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005742 int id = port->txqs[queue]->id;
5743
Thomas Petazzonia7868412017-03-07 16:53:13 +01005744 mvpp2_percpu_read(port->priv, smp_processor_id(),
5745 MVPP2_TXQ_SENT_REG(id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005746 }
5747}
5748
5749/* Set max sizes for Tx queues */
5750static void mvpp2_txp_max_tx_size_set(struct mvpp2_port *port)
5751{
5752 u32 val, size, mtu;
5753 int txq, tx_port_num;
5754
5755 mtu = port->pkt_size * 8;
5756 if (mtu > MVPP2_TXP_MTU_MAX)
5757 mtu = MVPP2_TXP_MTU_MAX;
5758
5759 /* WA for wrong Token bucket update: Set MTU value = 3*real MTU value */
5760 mtu = 3 * mtu;
5761
5762 /* Indirect access to registers */
5763 tx_port_num = mvpp2_egress_port(port);
5764 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
5765
5766 /* Set MTU */
5767 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_MTU_REG);
5768 val &= ~MVPP2_TXP_MTU_MAX;
5769 val |= mtu;
5770 mvpp2_write(port->priv, MVPP2_TXP_SCHED_MTU_REG, val);
5771
5772 /* TXP token size and all TXQs token size must be larger that MTU */
5773 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG);
5774 size = val & MVPP2_TXP_TOKEN_SIZE_MAX;
5775 if (size < mtu) {
5776 size = mtu;
5777 val &= ~MVPP2_TXP_TOKEN_SIZE_MAX;
5778 val |= size;
5779 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
5780 }
5781
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005782 for (txq = 0; txq < port->ntxqs; txq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005783 val = mvpp2_read(port->priv,
5784 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq));
5785 size = val & MVPP2_TXQ_TOKEN_SIZE_MAX;
5786
5787 if (size < mtu) {
5788 size = mtu;
5789 val &= ~MVPP2_TXQ_TOKEN_SIZE_MAX;
5790 val |= size;
5791 mvpp2_write(port->priv,
5792 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq),
5793 val);
5794 }
5795 }
5796}
5797
5798/* Set the number of packets that will be received before Rx interrupt
5799 * will be generated by HW.
5800 */
5801static void mvpp2_rx_pkts_coal_set(struct mvpp2_port *port,
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01005802 struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005803{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005804 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005805
Thomas Petazzonif8b0d5f2017-02-21 11:28:03 +01005806 if (rxq->pkts_coal > MVPP2_OCCUPIED_THRESH_MASK)
5807 rxq->pkts_coal = MVPP2_OCCUPIED_THRESH_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005808
Thomas Petazzonia7868412017-03-07 16:53:13 +01005809 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
5810 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_THRESH_REG,
5811 rxq->pkts_coal);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005812
5813 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005814}
5815
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005816/* For some reason in the LSP this is done on each CPU. Why ? */
5817static void mvpp2_tx_pkts_coal_set(struct mvpp2_port *port,
5818 struct mvpp2_tx_queue *txq)
5819{
5820 int cpu = get_cpu();
5821 u32 val;
5822
5823 if (txq->done_pkts_coal > MVPP2_TXQ_THRESH_MASK)
5824 txq->done_pkts_coal = MVPP2_TXQ_THRESH_MASK;
5825
5826 val = (txq->done_pkts_coal << MVPP2_TXQ_THRESH_OFFSET);
5827 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5828 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_THRESH_REG, val);
5829
5830 put_cpu();
5831}
5832
Thomas Petazzoniab426762017-02-21 11:28:04 +01005833static u32 mvpp2_usec_to_cycles(u32 usec, unsigned long clk_hz)
5834{
5835 u64 tmp = (u64)clk_hz * usec;
5836
5837 do_div(tmp, USEC_PER_SEC);
5838
5839 return tmp > U32_MAX ? U32_MAX : tmp;
5840}
5841
5842static u32 mvpp2_cycles_to_usec(u32 cycles, unsigned long clk_hz)
5843{
5844 u64 tmp = (u64)cycles * USEC_PER_SEC;
5845
5846 do_div(tmp, clk_hz);
5847
5848 return tmp > U32_MAX ? U32_MAX : tmp;
5849}
5850
Marcin Wojtas3f518502014-07-10 16:52:13 -03005851/* Set the time delay in usec before Rx interrupt */
5852static void mvpp2_rx_time_coal_set(struct mvpp2_port *port,
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01005853 struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005854{
Thomas Petazzoniab426762017-02-21 11:28:04 +01005855 unsigned long freq = port->priv->tclk;
5856 u32 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005857
Thomas Petazzoniab426762017-02-21 11:28:04 +01005858 if (val > MVPP2_MAX_ISR_RX_THRESHOLD) {
5859 rxq->time_coal =
5860 mvpp2_cycles_to_usec(MVPP2_MAX_ISR_RX_THRESHOLD, freq);
5861
5862 /* re-evaluate to get actual register value */
5863 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
5864 }
5865
Marcin Wojtas3f518502014-07-10 16:52:13 -03005866 mvpp2_write(port->priv, MVPP2_ISR_RX_THRESHOLD_REG(rxq->id), val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005867}
5868
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005869static void mvpp2_tx_time_coal_set(struct mvpp2_port *port)
5870{
5871 unsigned long freq = port->priv->tclk;
5872 u32 val = mvpp2_usec_to_cycles(port->tx_time_coal, freq);
5873
5874 if (val > MVPP2_MAX_ISR_TX_THRESHOLD) {
5875 port->tx_time_coal =
5876 mvpp2_cycles_to_usec(MVPP2_MAX_ISR_TX_THRESHOLD, freq);
5877
5878 /* re-evaluate to get actual register value */
5879 val = mvpp2_usec_to_cycles(port->tx_time_coal, freq);
5880 }
5881
5882 mvpp2_write(port->priv, MVPP2_ISR_TX_THRESHOLD_REG(port->id), val);
5883}
5884
Marcin Wojtas3f518502014-07-10 16:52:13 -03005885/* Free Tx queue skbuffs */
5886static void mvpp2_txq_bufs_free(struct mvpp2_port *port,
5887 struct mvpp2_tx_queue *txq,
5888 struct mvpp2_txq_pcpu *txq_pcpu, int num)
5889{
5890 int i;
5891
5892 for (i = 0; i < num; i++) {
Thomas Petazzoni83544912016-12-21 11:28:49 +01005893 struct mvpp2_txq_pcpu_buf *tx_buf =
5894 txq_pcpu->buffs + txq_pcpu->txq_get_index;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005895
Antoine Tenart20920262017-10-23 15:24:30 +02005896 if (!IS_TSO_HEADER(txq_pcpu, tx_buf->dma))
5897 dma_unmap_single(port->dev->dev.parent, tx_buf->dma,
5898 tx_buf->size, DMA_TO_DEVICE);
Thomas Petazzoni36fb7432017-02-21 11:28:05 +01005899 if (tx_buf->skb)
5900 dev_kfree_skb_any(tx_buf->skb);
5901
5902 mvpp2_txq_inc_get(txq_pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005903 }
5904}
5905
5906static inline struct mvpp2_rx_queue *mvpp2_get_rx_queue(struct mvpp2_port *port,
5907 u32 cause)
5908{
5909 int queue = fls(cause) - 1;
5910
5911 return port->rxqs[queue];
5912}
5913
5914static inline struct mvpp2_tx_queue *mvpp2_get_tx_queue(struct mvpp2_port *port,
5915 u32 cause)
5916{
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005917 int queue = fls(cause) - 1;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005918
5919 return port->txqs[queue];
5920}
5921
5922/* Handle end of transmission */
5923static void mvpp2_txq_done(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
5924 struct mvpp2_txq_pcpu *txq_pcpu)
5925{
5926 struct netdev_queue *nq = netdev_get_tx_queue(port->dev, txq->log_id);
5927 int tx_done;
5928
5929 if (txq_pcpu->cpu != smp_processor_id())
5930 netdev_err(port->dev, "wrong cpu on the end of Tx processing\n");
5931
5932 tx_done = mvpp2_txq_sent_desc_proc(port, txq);
5933 if (!tx_done)
5934 return;
5935 mvpp2_txq_bufs_free(port, txq, txq_pcpu, tx_done);
5936
5937 txq_pcpu->count -= tx_done;
5938
5939 if (netif_tx_queue_stopped(nq))
Antoine Tenart1d17db02017-10-30 11:23:31 +01005940 if (txq_pcpu->count <= txq_pcpu->wake_threshold)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005941 netif_tx_wake_queue(nq);
5942}
5943
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005944static unsigned int mvpp2_tx_done(struct mvpp2_port *port, u32 cause,
5945 int cpu)
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005946{
5947 struct mvpp2_tx_queue *txq;
5948 struct mvpp2_txq_pcpu *txq_pcpu;
5949 unsigned int tx_todo = 0;
5950
5951 while (cause) {
5952 txq = mvpp2_get_tx_queue(port, cause);
5953 if (!txq)
5954 break;
5955
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005956 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005957
5958 if (txq_pcpu->count) {
5959 mvpp2_txq_done(port, txq, txq_pcpu);
5960 tx_todo += txq_pcpu->count;
5961 }
5962
5963 cause &= ~(1 << txq->log_id);
5964 }
5965 return tx_todo;
5966}
5967
Marcin Wojtas3f518502014-07-10 16:52:13 -03005968/* Rx/Tx queue initialization/cleanup methods */
5969
5970/* Allocate and initialize descriptors for aggr TXQ */
5971static int mvpp2_aggr_txq_init(struct platform_device *pdev,
Antoine Ténart85affd72017-08-23 09:46:55 +02005972 struct mvpp2_tx_queue *aggr_txq, int cpu,
Marcin Wojtas3f518502014-07-10 16:52:13 -03005973 struct mvpp2 *priv)
5974{
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005975 u32 txq_dma;
5976
Marcin Wojtas3f518502014-07-10 16:52:13 -03005977 /* Allocate memory for TX descriptors */
Yan Markmana154f8e2017-11-30 10:49:46 +01005978 aggr_txq->descs = dma_zalloc_coherent(&pdev->dev,
Antoine Ténart85affd72017-08-23 09:46:55 +02005979 MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005980 &aggr_txq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005981 if (!aggr_txq->descs)
5982 return -ENOMEM;
5983
Antoine Tenart02856a32017-10-30 11:23:32 +01005984 aggr_txq->last_desc = MVPP2_AGGR_TXQ_SIZE - 1;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005985
5986 /* Aggr TXQ no reset WA */
5987 aggr_txq->next_desc_to_proc = mvpp2_read(priv,
5988 MVPP2_AGGR_TXQ_INDEX_REG(cpu));
5989
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005990 /* Set Tx descriptors queue starting address indirect
5991 * access
5992 */
5993 if (priv->hw_version == MVPP21)
5994 txq_dma = aggr_txq->descs_dma;
5995 else
5996 txq_dma = aggr_txq->descs_dma >>
5997 MVPP22_AGGR_TXQ_DESC_ADDR_OFFS;
5998
5999 mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu), txq_dma);
Antoine Ténart85affd72017-08-23 09:46:55 +02006000 mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu),
6001 MVPP2_AGGR_TXQ_SIZE);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006002
6003 return 0;
6004}
6005
6006/* Create a specified Rx queue */
6007static int mvpp2_rxq_init(struct mvpp2_port *port,
6008 struct mvpp2_rx_queue *rxq)
6009
6010{
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01006011 u32 rxq_dma;
Thomas Petazzonia7868412017-03-07 16:53:13 +01006012 int cpu;
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01006013
Marcin Wojtas3f518502014-07-10 16:52:13 -03006014 rxq->size = port->rx_ring_size;
6015
6016 /* Allocate memory for RX descriptors */
6017 rxq->descs = dma_alloc_coherent(port->dev->dev.parent,
6018 rxq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01006019 &rxq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006020 if (!rxq->descs)
6021 return -ENOMEM;
6022
Marcin Wojtas3f518502014-07-10 16:52:13 -03006023 rxq->last_desc = rxq->size - 1;
6024
6025 /* Zero occupied and non-occupied counters - direct access */
6026 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
6027
6028 /* Set Rx descriptors queue starting address - indirect access */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02006029 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01006030 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01006031 if (port->priv->hw_version == MVPP21)
6032 rxq_dma = rxq->descs_dma;
6033 else
6034 rxq_dma = rxq->descs_dma >> MVPP22_DESC_ADDR_OFFS;
Thomas Petazzonia7868412017-03-07 16:53:13 +01006035 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_ADDR_REG, rxq_dma);
6036 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_SIZE_REG, rxq->size);
6037 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_INDEX_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02006038 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03006039
6040 /* Set Offset */
6041 mvpp2_rxq_offset_set(port, rxq->id, NET_SKB_PAD);
6042
6043 /* Set coalescing pkts and time */
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01006044 mvpp2_rx_pkts_coal_set(port, rxq);
6045 mvpp2_rx_time_coal_set(port, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006046
6047 /* Add number of descriptors ready for receiving packets */
6048 mvpp2_rxq_status_update(port, rxq->id, 0, rxq->size);
6049
6050 return 0;
6051}
6052
6053/* Push packets received by the RXQ to BM pool */
6054static void mvpp2_rxq_drop_pkts(struct mvpp2_port *port,
6055 struct mvpp2_rx_queue *rxq)
6056{
6057 int rx_received, i;
6058
6059 rx_received = mvpp2_rxq_received(port, rxq->id);
6060 if (!rx_received)
6061 return;
6062
6063 for (i = 0; i < rx_received; i++) {
6064 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006065 u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
6066 int pool;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006067
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006068 pool = (status & MVPP2_RXD_BM_POOL_ID_MASK) >>
6069 MVPP2_RXD_BM_POOL_ID_OFFS;
6070
Thomas Petazzoni7d7627b2017-06-22 14:23:20 +02006071 mvpp2_bm_pool_put(port, pool,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006072 mvpp2_rxdesc_dma_addr_get(port, rx_desc),
6073 mvpp2_rxdesc_cookie_get(port, rx_desc));
Marcin Wojtas3f518502014-07-10 16:52:13 -03006074 }
6075 mvpp2_rxq_status_update(port, rxq->id, rx_received, rx_received);
6076}
6077
6078/* Cleanup Rx queue */
6079static void mvpp2_rxq_deinit(struct mvpp2_port *port,
6080 struct mvpp2_rx_queue *rxq)
6081{
Thomas Petazzonia7868412017-03-07 16:53:13 +01006082 int cpu;
6083
Marcin Wojtas3f518502014-07-10 16:52:13 -03006084 mvpp2_rxq_drop_pkts(port, rxq);
6085
6086 if (rxq->descs)
6087 dma_free_coherent(port->dev->dev.parent,
6088 rxq->size * MVPP2_DESC_ALIGNED_SIZE,
6089 rxq->descs,
Thomas Petazzoni20396132017-03-07 16:53:00 +01006090 rxq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006091
6092 rxq->descs = NULL;
6093 rxq->last_desc = 0;
6094 rxq->next_desc_to_proc = 0;
Thomas Petazzoni20396132017-03-07 16:53:00 +01006095 rxq->descs_dma = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006096
6097 /* Clear Rx descriptors queue starting address and size;
6098 * free descriptor number
6099 */
6100 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02006101 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01006102 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
6103 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_ADDR_REG, 0);
6104 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_SIZE_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02006105 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03006106}
6107
6108/* Create and initialize a Tx queue */
6109static int mvpp2_txq_init(struct mvpp2_port *port,
6110 struct mvpp2_tx_queue *txq)
6111{
6112 u32 val;
6113 int cpu, desc, desc_per_txq, tx_port_num;
6114 struct mvpp2_txq_pcpu *txq_pcpu;
6115
6116 txq->size = port->tx_ring_size;
6117
6118 /* Allocate memory for Tx descriptors */
6119 txq->descs = dma_alloc_coherent(port->dev->dev.parent,
6120 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01006121 &txq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006122 if (!txq->descs)
6123 return -ENOMEM;
6124
Marcin Wojtas3f518502014-07-10 16:52:13 -03006125 txq->last_desc = txq->size - 1;
6126
6127 /* Set Tx descriptors queue starting address - indirect access */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02006128 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01006129 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
6130 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_ADDR_REG,
6131 txq->descs_dma);
6132 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_SIZE_REG,
6133 txq->size & MVPP2_TXQ_DESC_SIZE_MASK);
6134 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_INDEX_REG, 0);
6135 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_RSVD_CLR_REG,
6136 txq->id << MVPP2_TXQ_RSVD_CLR_OFFSET);
6137 val = mvpp2_percpu_read(port->priv, cpu, MVPP2_TXQ_PENDING_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006138 val &= ~MVPP2_TXQ_PENDING_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01006139 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PENDING_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006140
6141 /* Calculate base address in prefetch buffer. We reserve 16 descriptors
6142 * for each existing TXQ.
6143 * TCONTS for PON port must be continuous from 0 to MVPP2_MAX_TCONT
6144 * GBE ports assumed to be continious from 0 to MVPP2_MAX_PORTS
6145 */
6146 desc_per_txq = 16;
6147 desc = (port->id * MVPP2_MAX_TXQ * desc_per_txq) +
6148 (txq->log_id * desc_per_txq);
6149
Thomas Petazzonia7868412017-03-07 16:53:13 +01006150 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG,
6151 MVPP2_PREF_BUF_PTR(desc) | MVPP2_PREF_BUF_SIZE_16 |
6152 MVPP2_PREF_BUF_THRESH(desc_per_txq / 2));
Thomas Petazzonia704bb52017-06-10 23:18:22 +02006153 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03006154
6155 /* WRR / EJP configuration - indirect access */
6156 tx_port_num = mvpp2_egress_port(port);
6157 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
6158
6159 val = mvpp2_read(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id));
6160 val &= ~MVPP2_TXQ_REFILL_PERIOD_ALL_MASK;
6161 val |= MVPP2_TXQ_REFILL_PERIOD_MASK(1);
6162 val |= MVPP2_TXQ_REFILL_TOKENS_ALL_MASK;
6163 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id), val);
6164
6165 val = MVPP2_TXQ_TOKEN_SIZE_MAX;
6166 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq->log_id),
6167 val);
6168
6169 for_each_present_cpu(cpu) {
6170 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
6171 txq_pcpu->size = txq->size;
Markus Elfring02c91ec2017-04-17 08:09:07 +02006172 txq_pcpu->buffs = kmalloc_array(txq_pcpu->size,
6173 sizeof(*txq_pcpu->buffs),
6174 GFP_KERNEL);
Thomas Petazzoni83544912016-12-21 11:28:49 +01006175 if (!txq_pcpu->buffs)
Antoine Tenartba2d8d82017-11-28 14:19:48 +01006176 return -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006177
6178 txq_pcpu->count = 0;
6179 txq_pcpu->reserved_num = 0;
6180 txq_pcpu->txq_put_index = 0;
6181 txq_pcpu->txq_get_index = 0;
Antoine Tenartb70d4a52017-12-11 09:13:25 +01006182 txq_pcpu->tso_headers = NULL;
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006183
Antoine Tenart1d17db02017-10-30 11:23:31 +01006184 txq_pcpu->stop_threshold = txq->size - MVPP2_MAX_SKB_DESCS;
6185 txq_pcpu->wake_threshold = txq_pcpu->stop_threshold / 2;
6186
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006187 txq_pcpu->tso_headers =
6188 dma_alloc_coherent(port->dev->dev.parent,
Yan Markman822eaf72017-10-23 15:24:29 +02006189 txq_pcpu->size * TSO_HEADER_SIZE,
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006190 &txq_pcpu->tso_headers_dma,
6191 GFP_KERNEL);
6192 if (!txq_pcpu->tso_headers)
Antoine Tenartba2d8d82017-11-28 14:19:48 +01006193 return -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006194 }
6195
6196 return 0;
6197}
6198
6199/* Free allocated TXQ resources */
6200static void mvpp2_txq_deinit(struct mvpp2_port *port,
6201 struct mvpp2_tx_queue *txq)
6202{
6203 struct mvpp2_txq_pcpu *txq_pcpu;
6204 int cpu;
6205
6206 for_each_present_cpu(cpu) {
6207 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Thomas Petazzoni83544912016-12-21 11:28:49 +01006208 kfree(txq_pcpu->buffs);
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006209
Antoine Tenartb70d4a52017-12-11 09:13:25 +01006210 if (txq_pcpu->tso_headers)
6211 dma_free_coherent(port->dev->dev.parent,
6212 txq_pcpu->size * TSO_HEADER_SIZE,
6213 txq_pcpu->tso_headers,
6214 txq_pcpu->tso_headers_dma);
6215
6216 txq_pcpu->tso_headers = NULL;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006217 }
6218
6219 if (txq->descs)
6220 dma_free_coherent(port->dev->dev.parent,
6221 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01006222 txq->descs, txq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006223
6224 txq->descs = NULL;
6225 txq->last_desc = 0;
6226 txq->next_desc_to_proc = 0;
Thomas Petazzoni20396132017-03-07 16:53:00 +01006227 txq->descs_dma = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006228
6229 /* Set minimum bandwidth for disabled TXQs */
6230 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(txq->id), 0);
6231
6232 /* Set Tx descriptors queue starting address and size */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02006233 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01006234 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
6235 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_ADDR_REG, 0);
6236 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_SIZE_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02006237 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03006238}
6239
6240/* Cleanup Tx ports */
6241static void mvpp2_txq_clean(struct mvpp2_port *port, struct mvpp2_tx_queue *txq)
6242{
6243 struct mvpp2_txq_pcpu *txq_pcpu;
6244 int delay, pending, cpu;
6245 u32 val;
6246
Thomas Petazzonia704bb52017-06-10 23:18:22 +02006247 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01006248 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
6249 val = mvpp2_percpu_read(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006250 val |= MVPP2_TXQ_DRAIN_EN_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01006251 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006252
6253 /* The napi queue has been stopped so wait for all packets
6254 * to be transmitted.
6255 */
6256 delay = 0;
6257 do {
6258 if (delay >= MVPP2_TX_PENDING_TIMEOUT_MSEC) {
6259 netdev_warn(port->dev,
6260 "port %d: cleaning queue %d timed out\n",
6261 port->id, txq->log_id);
6262 break;
6263 }
6264 mdelay(1);
6265 delay++;
6266
Thomas Petazzonia7868412017-03-07 16:53:13 +01006267 pending = mvpp2_percpu_read(port->priv, cpu,
6268 MVPP2_TXQ_PENDING_REG);
6269 pending &= MVPP2_TXQ_PENDING_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006270 } while (pending);
6271
6272 val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01006273 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG, val);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02006274 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03006275
6276 for_each_present_cpu(cpu) {
6277 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
6278
6279 /* Release all packets */
6280 mvpp2_txq_bufs_free(port, txq, txq_pcpu, txq_pcpu->count);
6281
6282 /* Reset queue */
6283 txq_pcpu->count = 0;
6284 txq_pcpu->txq_put_index = 0;
6285 txq_pcpu->txq_get_index = 0;
6286 }
6287}
6288
6289/* Cleanup all Tx queues */
6290static void mvpp2_cleanup_txqs(struct mvpp2_port *port)
6291{
6292 struct mvpp2_tx_queue *txq;
6293 int queue;
6294 u32 val;
6295
6296 val = mvpp2_read(port->priv, MVPP2_TX_PORT_FLUSH_REG);
6297
6298 /* Reset Tx ports and delete Tx queues */
6299 val |= MVPP2_TX_PORT_FLUSH_MASK(port->id);
6300 mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
6301
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006302 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006303 txq = port->txqs[queue];
6304 mvpp2_txq_clean(port, txq);
6305 mvpp2_txq_deinit(port, txq);
6306 }
6307
6308 on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
6309
6310 val &= ~MVPP2_TX_PORT_FLUSH_MASK(port->id);
6311 mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
6312}
6313
6314/* Cleanup all Rx queues */
6315static void mvpp2_cleanup_rxqs(struct mvpp2_port *port)
6316{
6317 int queue;
6318
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006319 for (queue = 0; queue < port->nrxqs; queue++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006320 mvpp2_rxq_deinit(port, port->rxqs[queue]);
6321}
6322
6323/* Init all Rx queues for port */
6324static int mvpp2_setup_rxqs(struct mvpp2_port *port)
6325{
6326 int queue, err;
6327
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006328 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006329 err = mvpp2_rxq_init(port, port->rxqs[queue]);
6330 if (err)
6331 goto err_cleanup;
6332 }
6333 return 0;
6334
6335err_cleanup:
6336 mvpp2_cleanup_rxqs(port);
6337 return err;
6338}
6339
6340/* Init all tx queues for port */
6341static int mvpp2_setup_txqs(struct mvpp2_port *port)
6342{
6343 struct mvpp2_tx_queue *txq;
6344 int queue, err;
6345
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006346 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006347 txq = port->txqs[queue];
6348 err = mvpp2_txq_init(port, txq);
6349 if (err)
6350 goto err_cleanup;
6351 }
6352
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006353 if (port->has_tx_irqs) {
6354 mvpp2_tx_time_coal_set(port);
6355 for (queue = 0; queue < port->ntxqs; queue++) {
6356 txq = port->txqs[queue];
6357 mvpp2_tx_pkts_coal_set(port, txq);
6358 }
6359 }
6360
Marcin Wojtas3f518502014-07-10 16:52:13 -03006361 on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
6362 return 0;
6363
6364err_cleanup:
6365 mvpp2_cleanup_txqs(port);
6366 return err;
6367}
6368
6369/* The callback for per-port interrupt */
6370static irqreturn_t mvpp2_isr(int irq, void *dev_id)
6371{
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006372 struct mvpp2_queue_vector *qv = dev_id;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006373
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006374 mvpp2_qvec_interrupt_disable(qv);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006375
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006376 napi_schedule(&qv->napi);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006377
6378 return IRQ_HANDLED;
6379}
6380
Antoine Tenartfd3651b2017-09-01 11:04:54 +02006381/* Per-port interrupt for link status changes */
6382static irqreturn_t mvpp2_link_status_isr(int irq, void *dev_id)
6383{
6384 struct mvpp2_port *port = (struct mvpp2_port *)dev_id;
6385 struct net_device *dev = port->dev;
6386 bool event = false, link = false;
6387 u32 val;
6388
6389 mvpp22_gop_mask_irq(port);
6390
6391 if (port->gop_id == 0 &&
6392 port->phy_interface == PHY_INTERFACE_MODE_10GKR) {
6393 val = readl(port->base + MVPP22_XLG_INT_STAT);
6394 if (val & MVPP22_XLG_INT_STAT_LINK) {
6395 event = true;
6396 val = readl(port->base + MVPP22_XLG_STATUS);
6397 if (val & MVPP22_XLG_STATUS_LINK_UP)
6398 link = true;
6399 }
6400 } else if (phy_interface_mode_is_rgmii(port->phy_interface) ||
6401 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
6402 val = readl(port->base + MVPP22_GMAC_INT_STAT);
6403 if (val & MVPP22_GMAC_INT_STAT_LINK) {
6404 event = true;
6405 val = readl(port->base + MVPP2_GMAC_STATUS0);
6406 if (val & MVPP2_GMAC_STATUS0_LINK_UP)
6407 link = true;
6408 }
6409 }
6410
6411 if (!netif_running(dev) || !event)
6412 goto handled;
6413
6414 if (link) {
6415 mvpp2_interrupts_enable(port);
6416
6417 mvpp2_egress_enable(port);
6418 mvpp2_ingress_enable(port);
6419 netif_carrier_on(dev);
6420 netif_tx_wake_all_queues(dev);
6421 } else {
6422 netif_tx_stop_all_queues(dev);
6423 netif_carrier_off(dev);
6424 mvpp2_ingress_disable(port);
6425 mvpp2_egress_disable(port);
6426
6427 mvpp2_interrupts_disable(port);
6428 }
6429
6430handled:
6431 mvpp22_gop_unmask_irq(port);
6432 return IRQ_HANDLED;
6433}
6434
Antoine Tenart65a2c092017-08-30 10:29:18 +02006435static void mvpp2_gmac_set_autoneg(struct mvpp2_port *port,
6436 struct phy_device *phydev)
6437{
6438 u32 val;
6439
6440 if (port->phy_interface != PHY_INTERFACE_MODE_RGMII &&
6441 port->phy_interface != PHY_INTERFACE_MODE_RGMII_ID &&
6442 port->phy_interface != PHY_INTERFACE_MODE_RGMII_RXID &&
6443 port->phy_interface != PHY_INTERFACE_MODE_RGMII_TXID &&
6444 port->phy_interface != PHY_INTERFACE_MODE_SGMII)
6445 return;
6446
6447 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
6448 val &= ~(MVPP2_GMAC_CONFIG_MII_SPEED |
6449 MVPP2_GMAC_CONFIG_GMII_SPEED |
6450 MVPP2_GMAC_CONFIG_FULL_DUPLEX |
6451 MVPP2_GMAC_AN_SPEED_EN |
6452 MVPP2_GMAC_AN_DUPLEX_EN);
6453
6454 if (phydev->duplex)
6455 val |= MVPP2_GMAC_CONFIG_FULL_DUPLEX;
6456
6457 if (phydev->speed == SPEED_1000)
6458 val |= MVPP2_GMAC_CONFIG_GMII_SPEED;
6459 else if (phydev->speed == SPEED_100)
6460 val |= MVPP2_GMAC_CONFIG_MII_SPEED;
6461
6462 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
Antoine Tenart65a2c092017-08-30 10:29:18 +02006463}
6464
Marcin Wojtas3f518502014-07-10 16:52:13 -03006465/* Adjust link */
6466static void mvpp2_link_event(struct net_device *dev)
6467{
6468 struct mvpp2_port *port = netdev_priv(dev);
Philippe Reynes8e072692016-06-28 00:08:11 +02006469 struct phy_device *phydev = dev->phydev;
Antoine Tenart89273bc2017-08-30 10:29:19 +02006470 bool link_reconfigured = false;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006471 u32 val;
6472
6473 if (phydev->link) {
Antoine Tenart89273bc2017-08-30 10:29:19 +02006474 if (port->phy_interface != phydev->interface && port->comphy) {
6475 /* disable current port for reconfiguration */
6476 mvpp2_interrupts_disable(port);
6477 netif_carrier_off(port->dev);
6478 mvpp2_port_disable(port);
6479 phy_power_off(port->comphy);
6480
6481 /* comphy reconfiguration */
6482 port->phy_interface = phydev->interface;
6483 mvpp22_comphy_init(port);
6484
6485 /* gop/mac reconfiguration */
6486 mvpp22_gop_init(port);
6487 mvpp2_port_mii_set(port);
6488
6489 link_reconfigured = true;
6490 }
6491
Marcin Wojtas3f518502014-07-10 16:52:13 -03006492 if ((port->speed != phydev->speed) ||
6493 (port->duplex != phydev->duplex)) {
Antoine Tenart65a2c092017-08-30 10:29:18 +02006494 mvpp2_gmac_set_autoneg(port, phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006495
6496 port->duplex = phydev->duplex;
6497 port->speed = phydev->speed;
6498 }
6499 }
6500
Antoine Tenart89273bc2017-08-30 10:29:19 +02006501 if (phydev->link != port->link || link_reconfigured) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006502 port->link = phydev->link;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006503
Marcin Wojtas3f518502014-07-10 16:52:13 -03006504 if (phydev->link) {
Antoine Tenart65a2c092017-08-30 10:29:18 +02006505 if (port->phy_interface == PHY_INTERFACE_MODE_RGMII ||
6506 port->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
6507 port->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID ||
6508 port->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID ||
6509 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
6510 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
6511 val |= (MVPP2_GMAC_FORCE_LINK_PASS |
6512 MVPP2_GMAC_FORCE_LINK_DOWN);
6513 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
6514 }
Antoine Tenartf55744a2017-08-30 10:29:17 +02006515
6516 mvpp2_interrupts_enable(port);
6517 mvpp2_port_enable(port);
6518
Marcin Wojtas3f518502014-07-10 16:52:13 -03006519 mvpp2_egress_enable(port);
6520 mvpp2_ingress_enable(port);
Antoine Tenartf55744a2017-08-30 10:29:17 +02006521 netif_carrier_on(dev);
6522 netif_tx_wake_all_queues(dev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006523 } else {
Antoine Tenart968b2112017-08-30 10:29:16 +02006524 port->duplex = -1;
6525 port->speed = 0;
6526
Antoine Tenartf55744a2017-08-30 10:29:17 +02006527 netif_tx_stop_all_queues(dev);
6528 netif_carrier_off(dev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006529 mvpp2_ingress_disable(port);
6530 mvpp2_egress_disable(port);
Antoine Tenartf55744a2017-08-30 10:29:17 +02006531
6532 mvpp2_port_disable(port);
6533 mvpp2_interrupts_disable(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006534 }
Antoine Tenart968b2112017-08-30 10:29:16 +02006535
Marcin Wojtas3f518502014-07-10 16:52:13 -03006536 phy_print_status(phydev);
6537 }
6538}
6539
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006540static void mvpp2_timer_set(struct mvpp2_port_pcpu *port_pcpu)
6541{
6542 ktime_t interval;
6543
6544 if (!port_pcpu->timer_scheduled) {
6545 port_pcpu->timer_scheduled = true;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01006546 interval = MVPP2_TXDONE_HRTIMER_PERIOD_NS;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006547 hrtimer_start(&port_pcpu->tx_done_timer, interval,
6548 HRTIMER_MODE_REL_PINNED);
6549 }
6550}
6551
6552static void mvpp2_tx_proc_cb(unsigned long data)
6553{
6554 struct net_device *dev = (struct net_device *)data;
6555 struct mvpp2_port *port = netdev_priv(dev);
6556 struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
6557 unsigned int tx_todo, cause;
6558
6559 if (!netif_running(dev))
6560 return;
6561 port_pcpu->timer_scheduled = false;
6562
6563 /* Process all the Tx queues */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006564 cause = (1 << port->ntxqs) - 1;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006565 tx_todo = mvpp2_tx_done(port, cause, smp_processor_id());
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006566
6567 /* Set the timer in case not all the packets were processed */
6568 if (tx_todo)
6569 mvpp2_timer_set(port_pcpu);
6570}
6571
6572static enum hrtimer_restart mvpp2_hr_timer_cb(struct hrtimer *timer)
6573{
6574 struct mvpp2_port_pcpu *port_pcpu = container_of(timer,
6575 struct mvpp2_port_pcpu,
6576 tx_done_timer);
6577
6578 tasklet_schedule(&port_pcpu->tx_done_tasklet);
6579
6580 return HRTIMER_NORESTART;
6581}
6582
Marcin Wojtas3f518502014-07-10 16:52:13 -03006583/* Main RX/TX processing routines */
6584
6585/* Display more error info */
6586static void mvpp2_rx_error(struct mvpp2_port *port,
6587 struct mvpp2_rx_desc *rx_desc)
6588{
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006589 u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
6590 size_t sz = mvpp2_rxdesc_size_get(port, rx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006591
6592 switch (status & MVPP2_RXD_ERR_CODE_MASK) {
6593 case MVPP2_RXD_ERR_CRC:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006594 netdev_err(port->dev, "bad rx status %08x (crc error), size=%zu\n",
6595 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006596 break;
6597 case MVPP2_RXD_ERR_OVERRUN:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006598 netdev_err(port->dev, "bad rx status %08x (overrun error), size=%zu\n",
6599 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006600 break;
6601 case MVPP2_RXD_ERR_RESOURCE:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006602 netdev_err(port->dev, "bad rx status %08x (resource error), size=%zu\n",
6603 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006604 break;
6605 }
6606}
6607
6608/* Handle RX checksum offload */
6609static void mvpp2_rx_csum(struct mvpp2_port *port, u32 status,
6610 struct sk_buff *skb)
6611{
6612 if (((status & MVPP2_RXD_L3_IP4) &&
6613 !(status & MVPP2_RXD_IP4_HEADER_ERR)) ||
6614 (status & MVPP2_RXD_L3_IP6))
6615 if (((status & MVPP2_RXD_L4_UDP) ||
6616 (status & MVPP2_RXD_L4_TCP)) &&
6617 (status & MVPP2_RXD_L4_CSUM_OK)) {
6618 skb->csum = 0;
6619 skb->ip_summed = CHECKSUM_UNNECESSARY;
6620 return;
6621 }
6622
6623 skb->ip_summed = CHECKSUM_NONE;
6624}
6625
6626/* Reuse skb if possible, or allocate a new skb and add it to BM pool */
6627static int mvpp2_rx_refill(struct mvpp2_port *port,
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006628 struct mvpp2_bm_pool *bm_pool, int pool)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006629{
Thomas Petazzoni20396132017-03-07 16:53:00 +01006630 dma_addr_t dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01006631 phys_addr_t phys_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006632 void *buf;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006633
Marcin Wojtas3f518502014-07-10 16:52:13 -03006634 /* No recycle or too many buffers are in use, so allocate a new skb */
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01006635 buf = mvpp2_buf_alloc(port, bm_pool, &dma_addr, &phys_addr,
6636 GFP_ATOMIC);
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006637 if (!buf)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006638 return -ENOMEM;
6639
Thomas Petazzoni7d7627b2017-06-22 14:23:20 +02006640 mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
Thomas Petazzoni7ef7e1d2017-02-21 11:28:07 +01006641
Marcin Wojtas3f518502014-07-10 16:52:13 -03006642 return 0;
6643}
6644
6645/* Handle tx checksum */
6646static u32 mvpp2_skb_tx_csum(struct mvpp2_port *port, struct sk_buff *skb)
6647{
6648 if (skb->ip_summed == CHECKSUM_PARTIAL) {
6649 int ip_hdr_len = 0;
6650 u8 l4_proto;
6651
6652 if (skb->protocol == htons(ETH_P_IP)) {
6653 struct iphdr *ip4h = ip_hdr(skb);
6654
6655 /* Calculate IPv4 checksum and L4 checksum */
6656 ip_hdr_len = ip4h->ihl;
6657 l4_proto = ip4h->protocol;
6658 } else if (skb->protocol == htons(ETH_P_IPV6)) {
6659 struct ipv6hdr *ip6h = ipv6_hdr(skb);
6660
6661 /* Read l4_protocol from one of IPv6 extra headers */
6662 if (skb_network_header_len(skb) > 0)
6663 ip_hdr_len = (skb_network_header_len(skb) >> 2);
6664 l4_proto = ip6h->nexthdr;
6665 } else {
6666 return MVPP2_TXD_L4_CSUM_NOT;
6667 }
6668
6669 return mvpp2_txq_desc_csum(skb_network_offset(skb),
6670 skb->protocol, ip_hdr_len, l4_proto);
6671 }
6672
6673 return MVPP2_TXD_L4_CSUM_NOT | MVPP2_TXD_IP_CSUM_DISABLE;
6674}
6675
Marcin Wojtas3f518502014-07-10 16:52:13 -03006676/* Main rx processing */
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006677static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi,
6678 int rx_todo, struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006679{
6680 struct net_device *dev = port->dev;
Marcin Wojtasb5015852015-12-03 15:20:51 +01006681 int rx_received;
6682 int rx_done = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006683 u32 rcvd_pkts = 0;
6684 u32 rcvd_bytes = 0;
6685
6686 /* Get number of received packets and clamp the to-do */
6687 rx_received = mvpp2_rxq_received(port, rxq->id);
6688 if (rx_todo > rx_received)
6689 rx_todo = rx_received;
6690
Marcin Wojtasb5015852015-12-03 15:20:51 +01006691 while (rx_done < rx_todo) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006692 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
6693 struct mvpp2_bm_pool *bm_pool;
6694 struct sk_buff *skb;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006695 unsigned int frag_size;
Thomas Petazzoni20396132017-03-07 16:53:00 +01006696 dma_addr_t dma_addr;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006697 phys_addr_t phys_addr;
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006698 u32 rx_status;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006699 int pool, rx_bytes, err;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006700 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006701
Marcin Wojtasb5015852015-12-03 15:20:51 +01006702 rx_done++;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006703 rx_status = mvpp2_rxdesc_status_get(port, rx_desc);
6704 rx_bytes = mvpp2_rxdesc_size_get(port, rx_desc);
6705 rx_bytes -= MVPP2_MH_SIZE;
6706 dma_addr = mvpp2_rxdesc_dma_addr_get(port, rx_desc);
6707 phys_addr = mvpp2_rxdesc_cookie_get(port, rx_desc);
6708 data = (void *)phys_to_virt(phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006709
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006710 pool = (rx_status & MVPP2_RXD_BM_POOL_ID_MASK) >>
6711 MVPP2_RXD_BM_POOL_ID_OFFS;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006712 bm_pool = &port->priv->bm_pools[pool];
Marcin Wojtas3f518502014-07-10 16:52:13 -03006713
6714 /* In case of an error, release the requested buffer pointer
6715 * to the Buffer Manager. This request process is controlled
6716 * by the hardware, and the information about the buffer is
6717 * comprised by the RX descriptor.
6718 */
6719 if (rx_status & MVPP2_RXD_ERR_SUMMARY) {
Markus Elfring8a524882017-04-17 10:52:02 +02006720err_drop_frame:
Marcin Wojtas3f518502014-07-10 16:52:13 -03006721 dev->stats.rx_errors++;
6722 mvpp2_rx_error(port, rx_desc);
Marcin Wojtasb5015852015-12-03 15:20:51 +01006723 /* Return the buffer to the pool */
Thomas Petazzoni7d7627b2017-06-22 14:23:20 +02006724 mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006725 continue;
6726 }
6727
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006728 if (bm_pool->frag_size > PAGE_SIZE)
6729 frag_size = 0;
6730 else
6731 frag_size = bm_pool->frag_size;
6732
6733 skb = build_skb(data, frag_size);
6734 if (!skb) {
6735 netdev_warn(port->dev, "skb build failed\n");
6736 goto err_drop_frame;
6737 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006738
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006739 err = mvpp2_rx_refill(port, bm_pool, pool);
Marcin Wojtasb5015852015-12-03 15:20:51 +01006740 if (err) {
6741 netdev_err(port->dev, "failed to refill BM pools\n");
6742 goto err_drop_frame;
6743 }
6744
Thomas Petazzoni20396132017-03-07 16:53:00 +01006745 dma_unmap_single(dev->dev.parent, dma_addr,
Marcin Wojtas4229d502015-12-03 15:20:50 +01006746 bm_pool->buf_size, DMA_FROM_DEVICE);
6747
Marcin Wojtas3f518502014-07-10 16:52:13 -03006748 rcvd_pkts++;
6749 rcvd_bytes += rx_bytes;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006750
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006751 skb_reserve(skb, MVPP2_MH_SIZE + NET_SKB_PAD);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006752 skb_put(skb, rx_bytes);
6753 skb->protocol = eth_type_trans(skb, dev);
6754 mvpp2_rx_csum(port, rx_status, skb);
6755
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006756 napi_gro_receive(napi, skb);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006757 }
6758
6759 if (rcvd_pkts) {
6760 struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
6761
6762 u64_stats_update_begin(&stats->syncp);
6763 stats->rx_packets += rcvd_pkts;
6764 stats->rx_bytes += rcvd_bytes;
6765 u64_stats_update_end(&stats->syncp);
6766 }
6767
6768 /* Update Rx queue management counters */
6769 wmb();
Marcin Wojtasb5015852015-12-03 15:20:51 +01006770 mvpp2_rxq_status_update(port, rxq->id, rx_done, rx_done);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006771
6772 return rx_todo;
6773}
6774
6775static inline void
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006776tx_desc_unmap_put(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006777 struct mvpp2_tx_desc *desc)
6778{
Antoine Tenart20920262017-10-23 15:24:30 +02006779 struct mvpp2_txq_pcpu *txq_pcpu = this_cpu_ptr(txq->pcpu);
6780
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006781 dma_addr_t buf_dma_addr =
6782 mvpp2_txdesc_dma_addr_get(port, desc);
6783 size_t buf_sz =
6784 mvpp2_txdesc_size_get(port, desc);
Antoine Tenart20920262017-10-23 15:24:30 +02006785 if (!IS_TSO_HEADER(txq_pcpu, buf_dma_addr))
6786 dma_unmap_single(port->dev->dev.parent, buf_dma_addr,
6787 buf_sz, DMA_TO_DEVICE);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006788 mvpp2_txq_desc_put(txq);
6789}
6790
6791/* Handle tx fragmentation processing */
6792static int mvpp2_tx_frag_process(struct mvpp2_port *port, struct sk_buff *skb,
6793 struct mvpp2_tx_queue *aggr_txq,
6794 struct mvpp2_tx_queue *txq)
6795{
6796 struct mvpp2_txq_pcpu *txq_pcpu = this_cpu_ptr(txq->pcpu);
6797 struct mvpp2_tx_desc *tx_desc;
6798 int i;
Thomas Petazzoni20396132017-03-07 16:53:00 +01006799 dma_addr_t buf_dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006800
6801 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
6802 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6803 void *addr = page_address(frag->page.p) + frag->page_offset;
6804
6805 tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006806 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6807 mvpp2_txdesc_size_set(port, tx_desc, frag->size);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006808
Thomas Petazzoni20396132017-03-07 16:53:00 +01006809 buf_dma_addr = dma_map_single(port->dev->dev.parent, addr,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006810 frag->size,
6811 DMA_TO_DEVICE);
Thomas Petazzoni20396132017-03-07 16:53:00 +01006812 if (dma_mapping_error(port->dev->dev.parent, buf_dma_addr)) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006813 mvpp2_txq_desc_put(txq);
Markus Elfring32bae632017-04-17 11:36:34 +02006814 goto cleanup;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006815 }
6816
Antoine Tenart6eb5d372017-10-30 11:23:33 +01006817 mvpp2_txdesc_dma_addr_set(port, tx_desc, buf_dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006818
6819 if (i == (skb_shinfo(skb)->nr_frags - 1)) {
6820 /* Last descriptor */
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006821 mvpp2_txdesc_cmd_set(port, tx_desc,
6822 MVPP2_TXD_L_DESC);
6823 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006824 } else {
6825 /* Descriptor in the middle: Not First, Not Last */
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006826 mvpp2_txdesc_cmd_set(port, tx_desc, 0);
6827 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006828 }
6829 }
6830
6831 return 0;
Markus Elfring32bae632017-04-17 11:36:34 +02006832cleanup:
Marcin Wojtas3f518502014-07-10 16:52:13 -03006833 /* Release all descriptors that were used to map fragments of
6834 * this packet, as well as the corresponding DMA mappings
6835 */
6836 for (i = i - 1; i >= 0; i--) {
6837 tx_desc = txq->descs + i;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006838 tx_desc_unmap_put(port, txq, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006839 }
6840
6841 return -ENOMEM;
6842}
6843
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006844static inline void mvpp2_tso_put_hdr(struct sk_buff *skb,
6845 struct net_device *dev,
6846 struct mvpp2_tx_queue *txq,
6847 struct mvpp2_tx_queue *aggr_txq,
6848 struct mvpp2_txq_pcpu *txq_pcpu,
6849 int hdr_sz)
6850{
6851 struct mvpp2_port *port = netdev_priv(dev);
6852 struct mvpp2_tx_desc *tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
6853 dma_addr_t addr;
6854
6855 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6856 mvpp2_txdesc_size_set(port, tx_desc, hdr_sz);
6857
6858 addr = txq_pcpu->tso_headers_dma +
6859 txq_pcpu->txq_put_index * TSO_HEADER_SIZE;
Antoine Tenart6eb5d372017-10-30 11:23:33 +01006860 mvpp2_txdesc_dma_addr_set(port, tx_desc, addr);
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006861
6862 mvpp2_txdesc_cmd_set(port, tx_desc, mvpp2_skb_tx_csum(port, skb) |
6863 MVPP2_TXD_F_DESC |
6864 MVPP2_TXD_PADDING_DISABLE);
6865 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
6866}
6867
6868static inline int mvpp2_tso_put_data(struct sk_buff *skb,
6869 struct net_device *dev, struct tso_t *tso,
6870 struct mvpp2_tx_queue *txq,
6871 struct mvpp2_tx_queue *aggr_txq,
6872 struct mvpp2_txq_pcpu *txq_pcpu,
6873 int sz, bool left, bool last)
6874{
6875 struct mvpp2_port *port = netdev_priv(dev);
6876 struct mvpp2_tx_desc *tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
6877 dma_addr_t buf_dma_addr;
6878
6879 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6880 mvpp2_txdesc_size_set(port, tx_desc, sz);
6881
6882 buf_dma_addr = dma_map_single(dev->dev.parent, tso->data, sz,
6883 DMA_TO_DEVICE);
6884 if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
6885 mvpp2_txq_desc_put(txq);
6886 return -ENOMEM;
6887 }
6888
Antoine Tenart6eb5d372017-10-30 11:23:33 +01006889 mvpp2_txdesc_dma_addr_set(port, tx_desc, buf_dma_addr);
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006890
6891 if (!left) {
6892 mvpp2_txdesc_cmd_set(port, tx_desc, MVPP2_TXD_L_DESC);
6893 if (last) {
6894 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
6895 return 0;
6896 }
6897 } else {
6898 mvpp2_txdesc_cmd_set(port, tx_desc, 0);
6899 }
6900
6901 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
6902 return 0;
6903}
6904
6905static int mvpp2_tx_tso(struct sk_buff *skb, struct net_device *dev,
6906 struct mvpp2_tx_queue *txq,
6907 struct mvpp2_tx_queue *aggr_txq,
6908 struct mvpp2_txq_pcpu *txq_pcpu)
6909{
6910 struct mvpp2_port *port = netdev_priv(dev);
6911 struct tso_t tso;
6912 int hdr_sz = skb_transport_offset(skb) + tcp_hdrlen(skb);
6913 int i, len, descs = 0;
6914
6915 /* Check number of available descriptors */
6916 if (mvpp2_aggr_desc_num_check(port->priv, aggr_txq,
6917 tso_count_descs(skb)) ||
6918 mvpp2_txq_reserved_desc_num_proc(port->priv, txq, txq_pcpu,
6919 tso_count_descs(skb)))
6920 return 0;
6921
6922 tso_start(skb, &tso);
6923 len = skb->len - hdr_sz;
6924 while (len > 0) {
6925 int left = min_t(int, skb_shinfo(skb)->gso_size, len);
6926 char *hdr = txq_pcpu->tso_headers +
6927 txq_pcpu->txq_put_index * TSO_HEADER_SIZE;
6928
6929 len -= left;
6930 descs++;
6931
6932 tso_build_hdr(skb, hdr, &tso, left, len == 0);
6933 mvpp2_tso_put_hdr(skb, dev, txq, aggr_txq, txq_pcpu, hdr_sz);
6934
6935 while (left > 0) {
6936 int sz = min_t(int, tso.size, left);
6937 left -= sz;
6938 descs++;
6939
6940 if (mvpp2_tso_put_data(skb, dev, &tso, txq, aggr_txq,
6941 txq_pcpu, sz, left, len == 0))
6942 goto release;
6943 tso_build_data(skb, &tso, sz);
6944 }
6945 }
6946
6947 return descs;
6948
6949release:
6950 for (i = descs - 1; i >= 0; i--) {
6951 struct mvpp2_tx_desc *tx_desc = txq->descs + i;
6952 tx_desc_unmap_put(port, txq, tx_desc);
6953 }
6954 return 0;
6955}
6956
Marcin Wojtas3f518502014-07-10 16:52:13 -03006957/* Main tx processing */
6958static int mvpp2_tx(struct sk_buff *skb, struct net_device *dev)
6959{
6960 struct mvpp2_port *port = netdev_priv(dev);
6961 struct mvpp2_tx_queue *txq, *aggr_txq;
6962 struct mvpp2_txq_pcpu *txq_pcpu;
6963 struct mvpp2_tx_desc *tx_desc;
Thomas Petazzoni20396132017-03-07 16:53:00 +01006964 dma_addr_t buf_dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006965 int frags = 0;
6966 u16 txq_id;
6967 u32 tx_cmd;
6968
6969 txq_id = skb_get_queue_mapping(skb);
6970 txq = port->txqs[txq_id];
6971 txq_pcpu = this_cpu_ptr(txq->pcpu);
6972 aggr_txq = &port->priv->aggr_txqs[smp_processor_id()];
6973
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006974 if (skb_is_gso(skb)) {
6975 frags = mvpp2_tx_tso(skb, dev, txq, aggr_txq, txq_pcpu);
6976 goto out;
6977 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006978 frags = skb_shinfo(skb)->nr_frags + 1;
6979
6980 /* Check number of available descriptors */
6981 if (mvpp2_aggr_desc_num_check(port->priv, aggr_txq, frags) ||
6982 mvpp2_txq_reserved_desc_num_proc(port->priv, txq,
6983 txq_pcpu, frags)) {
6984 frags = 0;
6985 goto out;
6986 }
6987
6988 /* Get a descriptor for the first part of the packet */
6989 tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006990 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6991 mvpp2_txdesc_size_set(port, tx_desc, skb_headlen(skb));
Marcin Wojtas3f518502014-07-10 16:52:13 -03006992
Thomas Petazzoni20396132017-03-07 16:53:00 +01006993 buf_dma_addr = dma_map_single(dev->dev.parent, skb->data,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006994 skb_headlen(skb), DMA_TO_DEVICE);
Thomas Petazzoni20396132017-03-07 16:53:00 +01006995 if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006996 mvpp2_txq_desc_put(txq);
6997 frags = 0;
6998 goto out;
6999 }
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01007000
Antoine Tenart6eb5d372017-10-30 11:23:33 +01007001 mvpp2_txdesc_dma_addr_set(port, tx_desc, buf_dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007002
7003 tx_cmd = mvpp2_skb_tx_csum(port, skb);
7004
7005 if (frags == 1) {
7006 /* First and Last descriptor */
7007 tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_L_DESC;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01007008 mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
7009 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007010 } else {
7011 /* First but not Last */
7012 tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_PADDING_DISABLE;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01007013 mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
7014 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007015
7016 /* Continue with other skb fragments */
7017 if (mvpp2_tx_frag_process(port, skb, aggr_txq, txq)) {
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01007018 tx_desc_unmap_put(port, txq, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007019 frags = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007020 }
7021 }
7022
Marcin Wojtas3f518502014-07-10 16:52:13 -03007023out:
7024 if (frags > 0) {
7025 struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
Antoine Ténart186cd4d2017-08-23 09:46:56 +02007026 struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
7027
7028 txq_pcpu->reserved_num -= frags;
7029 txq_pcpu->count += frags;
7030 aggr_txq->count += frags;
7031
7032 /* Enable transmit */
7033 wmb();
7034 mvpp2_aggr_txq_pend_desc_add(port, frags);
7035
Antoine Tenart1d17db02017-10-30 11:23:31 +01007036 if (txq_pcpu->count >= txq_pcpu->stop_threshold)
Antoine Ténart186cd4d2017-08-23 09:46:56 +02007037 netif_tx_stop_queue(nq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007038
7039 u64_stats_update_begin(&stats->syncp);
7040 stats->tx_packets++;
7041 stats->tx_bytes += skb->len;
7042 u64_stats_update_end(&stats->syncp);
7043 } else {
7044 dev->stats.tx_dropped++;
7045 dev_kfree_skb_any(skb);
7046 }
7047
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007048 /* Finalize TX processing */
Antoine Tenart082297e2017-10-23 15:24:31 +02007049 if (!port->has_tx_irqs && txq_pcpu->count >= txq->done_pkts_coal)
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007050 mvpp2_txq_done(port, txq, txq_pcpu);
7051
7052 /* Set the timer in case not all frags were processed */
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007053 if (!port->has_tx_irqs && txq_pcpu->count <= frags &&
7054 txq_pcpu->count > 0) {
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007055 struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
7056
7057 mvpp2_timer_set(port_pcpu);
7058 }
7059
Marcin Wojtas3f518502014-07-10 16:52:13 -03007060 return NETDEV_TX_OK;
7061}
7062
7063static inline void mvpp2_cause_error(struct net_device *dev, int cause)
7064{
7065 if (cause & MVPP2_CAUSE_FCS_ERR_MASK)
7066 netdev_err(dev, "FCS error\n");
7067 if (cause & MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK)
7068 netdev_err(dev, "rx fifo overrun error\n");
7069 if (cause & MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK)
7070 netdev_err(dev, "tx fifo underrun error\n");
7071}
7072
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007073static int mvpp2_poll(struct napi_struct *napi, int budget)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007074{
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007075 u32 cause_rx_tx, cause_rx, cause_tx, cause_misc;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007076 int rx_done = 0;
7077 struct mvpp2_port *port = netdev_priv(napi->dev);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007078 struct mvpp2_queue_vector *qv;
Thomas Petazzonia7868412017-03-07 16:53:13 +01007079 int cpu = smp_processor_id();
Marcin Wojtas3f518502014-07-10 16:52:13 -03007080
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007081 qv = container_of(napi, struct mvpp2_queue_vector, napi);
7082
Marcin Wojtas3f518502014-07-10 16:52:13 -03007083 /* Rx/Tx cause register
7084 *
7085 * Bits 0-15: each bit indicates received packets on the Rx queue
7086 * (bit 0 is for Rx queue 0).
7087 *
7088 * Bits 16-23: each bit indicates transmitted packets on the Tx queue
7089 * (bit 16 is for Tx queue 0).
7090 *
7091 * Each CPU has its own Rx/Tx cause register
7092 */
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007093 cause_rx_tx = mvpp2_percpu_read(port->priv, qv->sw_thread_id,
Thomas Petazzonia7868412017-03-07 16:53:13 +01007094 MVPP2_ISR_RX_TX_CAUSE_REG(port->id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03007095
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007096 cause_misc = cause_rx_tx & MVPP2_CAUSE_MISC_SUM_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007097 if (cause_misc) {
7098 mvpp2_cause_error(port->dev, cause_misc);
7099
7100 /* Clear the cause register */
7101 mvpp2_write(port->priv, MVPP2_ISR_MISC_CAUSE_REG, 0);
Thomas Petazzonia7868412017-03-07 16:53:13 +01007102 mvpp2_percpu_write(port->priv, cpu,
7103 MVPP2_ISR_RX_TX_CAUSE_REG(port->id),
7104 cause_rx_tx & ~MVPP2_CAUSE_MISC_SUM_MASK);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007105 }
7106
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007107 cause_tx = cause_rx_tx & MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
7108 if (cause_tx) {
7109 cause_tx >>= MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_OFFSET;
7110 mvpp2_tx_done(port, cause_tx, qv->sw_thread_id);
7111 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03007112
7113 /* Process RX packets */
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007114 cause_rx = cause_rx_tx & MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
7115 cause_rx <<= qv->first_rxq;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007116 cause_rx |= qv->pending_cause_rx;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007117 while (cause_rx && budget > 0) {
7118 int count;
7119 struct mvpp2_rx_queue *rxq;
7120
7121 rxq = mvpp2_get_rx_queue(port, cause_rx);
7122 if (!rxq)
7123 break;
7124
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007125 count = mvpp2_rx(port, napi, budget, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007126 rx_done += count;
7127 budget -= count;
7128 if (budget > 0) {
7129 /* Clear the bit associated to this Rx queue
7130 * so that next iteration will continue from
7131 * the next Rx queue.
7132 */
7133 cause_rx &= ~(1 << rxq->logic_rxq);
7134 }
7135 }
7136
7137 if (budget > 0) {
7138 cause_rx = 0;
Eric Dumazet6ad20162017-01-30 08:22:01 -08007139 napi_complete_done(napi, rx_done);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007140
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007141 mvpp2_qvec_interrupt_enable(qv);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007142 }
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007143 qv->pending_cause_rx = cause_rx;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007144 return rx_done;
7145}
7146
7147/* Set hw internals when starting port */
7148static void mvpp2_start_dev(struct mvpp2_port *port)
7149{
Philippe Reynes8e072692016-06-28 00:08:11 +02007150 struct net_device *ndev = port->dev;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007151 int i;
Philippe Reynes8e072692016-06-28 00:08:11 +02007152
Stefan Chulski76eb1b12017-08-22 19:08:26 +02007153 if (port->gop_id == 0 &&
7154 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
7155 port->phy_interface == PHY_INTERFACE_MODE_10GKR))
7156 mvpp2_xlg_max_rx_size_set(port);
7157 else
7158 mvpp2_gmac_max_rx_size_set(port);
7159
Marcin Wojtas3f518502014-07-10 16:52:13 -03007160 mvpp2_txp_max_tx_size_set(port);
7161
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007162 for (i = 0; i < port->nqvecs; i++)
7163 napi_enable(&port->qvecs[i].napi);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007164
7165 /* Enable interrupts on all CPUs */
7166 mvpp2_interrupts_enable(port);
7167
Antoine Tenart542897d2017-08-30 10:29:15 +02007168 if (port->priv->hw_version == MVPP22) {
7169 mvpp22_comphy_init(port);
Antoine Ténartf84bf382017-08-22 19:08:27 +02007170 mvpp22_gop_init(port);
Antoine Tenart542897d2017-08-30 10:29:15 +02007171 }
Antoine Ténartf84bf382017-08-22 19:08:27 +02007172
Antoine Ténart2055d622017-08-22 19:08:23 +02007173 mvpp2_port_mii_set(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007174 mvpp2_port_enable(port);
Antoine Tenart5997c862017-09-01 11:04:53 +02007175 if (ndev->phydev)
7176 phy_start(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007177 netif_tx_start_all_queues(port->dev);
7178}
7179
7180/* Set hw internals when stopping port */
7181static void mvpp2_stop_dev(struct mvpp2_port *port)
7182{
Philippe Reynes8e072692016-06-28 00:08:11 +02007183 struct net_device *ndev = port->dev;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007184 int i;
Philippe Reynes8e072692016-06-28 00:08:11 +02007185
Marcin Wojtas3f518502014-07-10 16:52:13 -03007186 /* Stop new packets from arriving to RXQs */
7187 mvpp2_ingress_disable(port);
7188
7189 mdelay(10);
7190
7191 /* Disable interrupts on all CPUs */
7192 mvpp2_interrupts_disable(port);
7193
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007194 for (i = 0; i < port->nqvecs; i++)
7195 napi_disable(&port->qvecs[i].napi);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007196
7197 netif_carrier_off(port->dev);
7198 netif_tx_stop_all_queues(port->dev);
7199
7200 mvpp2_egress_disable(port);
7201 mvpp2_port_disable(port);
Antoine Tenart5997c862017-09-01 11:04:53 +02007202 if (ndev->phydev)
7203 phy_stop(ndev->phydev);
Antoine Tenart542897d2017-08-30 10:29:15 +02007204 phy_power_off(port->comphy);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007205}
7206
Marcin Wojtas3f518502014-07-10 16:52:13 -03007207static int mvpp2_check_ringparam_valid(struct net_device *dev,
7208 struct ethtool_ringparam *ring)
7209{
7210 u16 new_rx_pending = ring->rx_pending;
7211 u16 new_tx_pending = ring->tx_pending;
7212
7213 if (ring->rx_pending == 0 || ring->tx_pending == 0)
7214 return -EINVAL;
7215
Yan Markman7cf87e42017-12-11 09:13:26 +01007216 if (ring->rx_pending > MVPP2_MAX_RXD_MAX)
7217 new_rx_pending = MVPP2_MAX_RXD_MAX;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007218 else if (!IS_ALIGNED(ring->rx_pending, 16))
7219 new_rx_pending = ALIGN(ring->rx_pending, 16);
7220
Yan Markman7cf87e42017-12-11 09:13:26 +01007221 if (ring->tx_pending > MVPP2_MAX_TXD_MAX)
7222 new_tx_pending = MVPP2_MAX_TXD_MAX;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007223 else if (!IS_ALIGNED(ring->tx_pending, 32))
7224 new_tx_pending = ALIGN(ring->tx_pending, 32);
7225
Antoine Tenart76e583c2017-11-28 14:19:51 +01007226 /* The Tx ring size cannot be smaller than the minimum number of
7227 * descriptors needed for TSO.
7228 */
7229 if (new_tx_pending < MVPP2_MAX_SKB_DESCS)
7230 new_tx_pending = ALIGN(MVPP2_MAX_SKB_DESCS, 32);
7231
Marcin Wojtas3f518502014-07-10 16:52:13 -03007232 if (ring->rx_pending != new_rx_pending) {
7233 netdev_info(dev, "illegal Rx ring size value %d, round to %d\n",
7234 ring->rx_pending, new_rx_pending);
7235 ring->rx_pending = new_rx_pending;
7236 }
7237
7238 if (ring->tx_pending != new_tx_pending) {
7239 netdev_info(dev, "illegal Tx ring size value %d, round to %d\n",
7240 ring->tx_pending, new_tx_pending);
7241 ring->tx_pending = new_tx_pending;
7242 }
7243
7244 return 0;
7245}
7246
Thomas Petazzoni26975822017-03-07 16:53:14 +01007247static void mvpp21_get_mac_address(struct mvpp2_port *port, unsigned char *addr)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007248{
7249 u32 mac_addr_l, mac_addr_m, mac_addr_h;
7250
7251 mac_addr_l = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
7252 mac_addr_m = readl(port->priv->lms_base + MVPP2_SRC_ADDR_MIDDLE);
7253 mac_addr_h = readl(port->priv->lms_base + MVPP2_SRC_ADDR_HIGH);
7254 addr[0] = (mac_addr_h >> 24) & 0xFF;
7255 addr[1] = (mac_addr_h >> 16) & 0xFF;
7256 addr[2] = (mac_addr_h >> 8) & 0xFF;
7257 addr[3] = mac_addr_h & 0xFF;
7258 addr[4] = mac_addr_m & 0xFF;
7259 addr[5] = (mac_addr_l >> MVPP2_GMAC_SA_LOW_OFFS) & 0xFF;
7260}
7261
7262static int mvpp2_phy_connect(struct mvpp2_port *port)
7263{
7264 struct phy_device *phy_dev;
7265
Antoine Tenart5997c862017-09-01 11:04:53 +02007266 /* No PHY is attached */
7267 if (!port->phy_node)
7268 return 0;
7269
Marcin Wojtas3f518502014-07-10 16:52:13 -03007270 phy_dev = of_phy_connect(port->dev, port->phy_node, mvpp2_link_event, 0,
7271 port->phy_interface);
7272 if (!phy_dev) {
7273 netdev_err(port->dev, "cannot connect to phy\n");
7274 return -ENODEV;
7275 }
7276 phy_dev->supported &= PHY_GBIT_FEATURES;
7277 phy_dev->advertising = phy_dev->supported;
7278
Marcin Wojtas3f518502014-07-10 16:52:13 -03007279 port->link = 0;
7280 port->duplex = 0;
7281 port->speed = 0;
7282
7283 return 0;
7284}
7285
7286static void mvpp2_phy_disconnect(struct mvpp2_port *port)
7287{
Philippe Reynes8e072692016-06-28 00:08:11 +02007288 struct net_device *ndev = port->dev;
7289
Antoine Tenart5997c862017-09-01 11:04:53 +02007290 if (!ndev->phydev)
7291 return;
7292
Philippe Reynes8e072692016-06-28 00:08:11 +02007293 phy_disconnect(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007294}
7295
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007296static int mvpp2_irqs_init(struct mvpp2_port *port)
7297{
7298 int err, i;
7299
7300 for (i = 0; i < port->nqvecs; i++) {
7301 struct mvpp2_queue_vector *qv = port->qvecs + i;
7302
Marc Zyngier13c249a2017-11-04 12:33:47 +00007303 if (qv->type == MVPP2_QUEUE_VECTOR_PRIVATE)
7304 irq_set_status_flags(qv->irq, IRQ_NO_BALANCING);
7305
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007306 err = request_irq(qv->irq, mvpp2_isr, 0, port->dev->name, qv);
7307 if (err)
7308 goto err;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007309
7310 if (qv->type == MVPP2_QUEUE_VECTOR_PRIVATE)
7311 irq_set_affinity_hint(qv->irq,
7312 cpumask_of(qv->sw_thread_id));
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007313 }
7314
7315 return 0;
7316err:
7317 for (i = 0; i < port->nqvecs; i++) {
7318 struct mvpp2_queue_vector *qv = port->qvecs + i;
7319
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007320 irq_set_affinity_hint(qv->irq, NULL);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007321 free_irq(qv->irq, qv);
7322 }
7323
7324 return err;
7325}
7326
7327static void mvpp2_irqs_deinit(struct mvpp2_port *port)
7328{
7329 int i;
7330
7331 for (i = 0; i < port->nqvecs; i++) {
7332 struct mvpp2_queue_vector *qv = port->qvecs + i;
7333
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007334 irq_set_affinity_hint(qv->irq, NULL);
Marc Zyngier13c249a2017-11-04 12:33:47 +00007335 irq_clear_status_flags(qv->irq, IRQ_NO_BALANCING);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007336 free_irq(qv->irq, qv);
7337 }
7338}
7339
Antoine Tenart1d7d15d2017-10-30 11:23:30 +01007340static void mvpp22_init_rss(struct mvpp2_port *port)
7341{
7342 struct mvpp2 *priv = port->priv;
7343 int i;
7344
7345 /* Set the table width: replace the whole classifier Rx queue number
7346 * with the ones configured in RSS table entries.
7347 */
7348 mvpp2_write(priv, MVPP22_RSS_INDEX, MVPP22_RSS_INDEX_TABLE(0));
7349 mvpp2_write(priv, MVPP22_RSS_WIDTH, 8);
7350
7351 /* Loop through the classifier Rx Queues and map them to a RSS table.
7352 * Map them all to the first table (0) by default.
7353 */
7354 for (i = 0; i < MVPP2_CLS_RX_QUEUES; i++) {
7355 mvpp2_write(priv, MVPP22_RSS_INDEX, MVPP22_RSS_INDEX_QUEUE(i));
7356 mvpp2_write(priv, MVPP22_RSS_TABLE,
7357 MVPP22_RSS_TABLE_POINTER(0));
7358 }
7359
7360 /* Configure the first table to evenly distribute the packets across
7361 * real Rx Queues. The table entries map a hash to an port Rx Queue.
7362 */
7363 for (i = 0; i < MVPP22_RSS_TABLE_ENTRIES; i++) {
7364 u32 sel = MVPP22_RSS_INDEX_TABLE(0) |
7365 MVPP22_RSS_INDEX_TABLE_ENTRY(i);
7366 mvpp2_write(priv, MVPP22_RSS_INDEX, sel);
7367
7368 mvpp2_write(priv, MVPP22_RSS_TABLE_ENTRY, i % port->nrxqs);
7369 }
7370
7371}
7372
Marcin Wojtas3f518502014-07-10 16:52:13 -03007373static int mvpp2_open(struct net_device *dev)
7374{
7375 struct mvpp2_port *port = netdev_priv(dev);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007376 struct mvpp2 *priv = port->priv;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007377 unsigned char mac_bcast[ETH_ALEN] = {
7378 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7379 int err;
7380
Maxime Chevallierce2a27c2018-03-07 15:18:03 +01007381 err = mvpp2_prs_mac_da_accept(port, mac_bcast, true);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007382 if (err) {
7383 netdev_err(dev, "mvpp2_prs_mac_da_accept BC failed\n");
7384 return err;
7385 }
Maxime Chevallierce2a27c2018-03-07 15:18:03 +01007386 err = mvpp2_prs_mac_da_accept(port, dev->dev_addr, true);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007387 if (err) {
Maxime Chevallierce2a27c2018-03-07 15:18:03 +01007388 netdev_err(dev, "mvpp2_prs_mac_da_accept own addr failed\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03007389 return err;
7390 }
7391 err = mvpp2_prs_tag_mode_set(port->priv, port->id, MVPP2_TAG_TYPE_MH);
7392 if (err) {
7393 netdev_err(dev, "mvpp2_prs_tag_mode_set failed\n");
7394 return err;
7395 }
7396 err = mvpp2_prs_def_flow(port);
7397 if (err) {
7398 netdev_err(dev, "mvpp2_prs_def_flow failed\n");
7399 return err;
7400 }
7401
7402 /* Allocate the Rx/Tx queues */
7403 err = mvpp2_setup_rxqs(port);
7404 if (err) {
7405 netdev_err(port->dev, "cannot allocate Rx queues\n");
7406 return err;
7407 }
7408
7409 err = mvpp2_setup_txqs(port);
7410 if (err) {
7411 netdev_err(port->dev, "cannot allocate Tx queues\n");
7412 goto err_cleanup_rxqs;
7413 }
7414
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007415 err = mvpp2_irqs_init(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007416 if (err) {
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007417 netdev_err(port->dev, "cannot init IRQs\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03007418 goto err_cleanup_txqs;
7419 }
7420
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007421 if (priv->hw_version == MVPP22 && !port->phy_node && port->link_irq) {
7422 err = request_irq(port->link_irq, mvpp2_link_status_isr, 0,
7423 dev->name, port);
7424 if (err) {
7425 netdev_err(port->dev, "cannot request link IRQ %d\n",
7426 port->link_irq);
7427 goto err_free_irq;
7428 }
7429
7430 mvpp22_gop_setup_irq(port);
7431 }
7432
Marcin Wojtas3f518502014-07-10 16:52:13 -03007433 /* In default link is down */
7434 netif_carrier_off(port->dev);
7435
7436 err = mvpp2_phy_connect(port);
7437 if (err < 0)
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007438 goto err_free_link_irq;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007439
7440 /* Unmask interrupts on all CPUs */
7441 on_each_cpu(mvpp2_interrupts_unmask, port, 1);
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007442 mvpp2_shared_interrupt_mask_unmask(port, false);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007443
7444 mvpp2_start_dev(port);
7445
Antoine Tenart1d7d15d2017-10-30 11:23:30 +01007446 if (priv->hw_version == MVPP22)
7447 mvpp22_init_rss(port);
7448
Miquel Raynal118d6292017-11-06 22:56:53 +01007449 /* Start hardware statistics gathering */
Miquel Raynale5c500e2017-11-08 08:59:40 +01007450 queue_delayed_work(priv->stats_queue, &port->stats_work,
Miquel Raynal118d6292017-11-06 22:56:53 +01007451 MVPP2_MIB_COUNTERS_STATS_DELAY);
7452
Marcin Wojtas3f518502014-07-10 16:52:13 -03007453 return 0;
7454
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007455err_free_link_irq:
7456 if (priv->hw_version == MVPP22 && !port->phy_node && port->link_irq)
7457 free_irq(port->link_irq, port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007458err_free_irq:
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007459 mvpp2_irqs_deinit(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007460err_cleanup_txqs:
7461 mvpp2_cleanup_txqs(port);
7462err_cleanup_rxqs:
7463 mvpp2_cleanup_rxqs(port);
7464 return err;
7465}
7466
7467static int mvpp2_stop(struct net_device *dev)
7468{
7469 struct mvpp2_port *port = netdev_priv(dev);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007470 struct mvpp2_port_pcpu *port_pcpu;
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007471 struct mvpp2 *priv = port->priv;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007472 int cpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007473
7474 mvpp2_stop_dev(port);
7475 mvpp2_phy_disconnect(port);
7476
7477 /* Mask interrupts on all CPUs */
7478 on_each_cpu(mvpp2_interrupts_mask, port, 1);
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007479 mvpp2_shared_interrupt_mask_unmask(port, true);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007480
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007481 if (priv->hw_version == MVPP22 && !port->phy_node && port->link_irq)
7482 free_irq(port->link_irq, port);
7483
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007484 mvpp2_irqs_deinit(port);
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007485 if (!port->has_tx_irqs) {
7486 for_each_present_cpu(cpu) {
7487 port_pcpu = per_cpu_ptr(port->pcpu, cpu);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007488
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007489 hrtimer_cancel(&port_pcpu->tx_done_timer);
7490 port_pcpu->timer_scheduled = false;
7491 tasklet_kill(&port_pcpu->tx_done_tasklet);
7492 }
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007493 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03007494 mvpp2_cleanup_rxqs(port);
7495 mvpp2_cleanup_txqs(port);
7496
Miquel Raynale5c500e2017-11-08 08:59:40 +01007497 cancel_delayed_work_sync(&port->stats_work);
Miquel Raynal118d6292017-11-06 22:56:53 +01007498
Marcin Wojtas3f518502014-07-10 16:52:13 -03007499 return 0;
7500}
7501
Maxime Chevallier10fea262018-03-07 15:18:04 +01007502static int mvpp2_prs_mac_da_accept_list(struct mvpp2_port *port,
7503 struct netdev_hw_addr_list *list)
7504{
7505 struct netdev_hw_addr *ha;
7506 int ret;
7507
7508 netdev_hw_addr_list_for_each(ha, list) {
7509 ret = mvpp2_prs_mac_da_accept(port, ha->addr, true);
7510 if (ret)
7511 return ret;
7512 }
7513
7514 return 0;
7515}
7516
7517static void mvpp2_set_rx_promisc(struct mvpp2_port *port, bool enable)
7518{
7519 if (!enable && (port->dev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
7520 mvpp2_prs_vid_enable_filtering(port);
7521 else
7522 mvpp2_prs_vid_disable_filtering(port);
7523
7524 mvpp2_prs_mac_promisc_set(port->priv, port->id,
7525 MVPP2_PRS_L2_UNI_CAST, enable);
7526
7527 mvpp2_prs_mac_promisc_set(port->priv, port->id,
7528 MVPP2_PRS_L2_MULTI_CAST, enable);
7529}
7530
Marcin Wojtas3f518502014-07-10 16:52:13 -03007531static void mvpp2_set_rx_mode(struct net_device *dev)
7532{
7533 struct mvpp2_port *port = netdev_priv(dev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007534
Maxime Chevallier10fea262018-03-07 15:18:04 +01007535 /* Clear the whole UC and MC list */
7536 mvpp2_prs_mac_del_all(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007537
Maxime Chevallier10fea262018-03-07 15:18:04 +01007538 if (dev->flags & IFF_PROMISC) {
7539 mvpp2_set_rx_promisc(port, true);
7540 return;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007541 }
Maxime Chevallier56beda32018-02-28 10:14:13 +01007542
Maxime Chevallier10fea262018-03-07 15:18:04 +01007543 mvpp2_set_rx_promisc(port, false);
7544
7545 if (netdev_uc_count(dev) > MVPP2_PRS_MAC_UC_FILT_MAX ||
7546 mvpp2_prs_mac_da_accept_list(port, &dev->uc))
7547 mvpp2_prs_mac_promisc_set(port->priv, port->id,
7548 MVPP2_PRS_L2_UNI_CAST, true);
7549
7550 if (dev->flags & IFF_ALLMULTI) {
7551 mvpp2_prs_mac_promisc_set(port->priv, port->id,
7552 MVPP2_PRS_L2_MULTI_CAST, true);
7553 return;
7554 }
7555
7556 if (netdev_mc_count(dev) > MVPP2_PRS_MAC_MC_FILT_MAX ||
7557 mvpp2_prs_mac_da_accept_list(port, &dev->mc))
7558 mvpp2_prs_mac_promisc_set(port->priv, port->id,
7559 MVPP2_PRS_L2_MULTI_CAST, true);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007560}
7561
7562static int mvpp2_set_mac_address(struct net_device *dev, void *p)
7563{
7564 struct mvpp2_port *port = netdev_priv(dev);
7565 const struct sockaddr *addr = p;
7566 int err;
7567
7568 if (!is_valid_ether_addr(addr->sa_data)) {
7569 err = -EADDRNOTAVAIL;
Markus Elfringc1175542017-04-17 11:10:47 +02007570 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007571 }
7572
7573 if (!netif_running(dev)) {
7574 err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
7575 if (!err)
7576 return 0;
7577 /* Reconfigure parser to accept the original MAC address */
7578 err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
7579 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02007580 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007581 }
7582
7583 mvpp2_stop_dev(port);
7584
7585 err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
7586 if (!err)
7587 goto out_start;
7588
7589 /* Reconfigure parser accept the original MAC address */
7590 err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
7591 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02007592 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007593out_start:
7594 mvpp2_start_dev(port);
7595 mvpp2_egress_enable(port);
7596 mvpp2_ingress_enable(port);
7597 return 0;
Markus Elfringc1175542017-04-17 11:10:47 +02007598log_error:
Markus Elfringdfd42402017-04-17 11:20:41 +02007599 netdev_err(dev, "failed to change MAC address\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03007600 return err;
7601}
7602
7603static int mvpp2_change_mtu(struct net_device *dev, int mtu)
7604{
7605 struct mvpp2_port *port = netdev_priv(dev);
7606 int err;
7607
Jarod Wilson57779872016-10-17 15:54:06 -04007608 if (!IS_ALIGNED(MVPP2_RX_PKT_SIZE(mtu), 8)) {
7609 netdev_info(dev, "illegal MTU value %d, round to %d\n", mtu,
7610 ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8));
7611 mtu = ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007612 }
7613
7614 if (!netif_running(dev)) {
7615 err = mvpp2_bm_update_mtu(dev, mtu);
7616 if (!err) {
7617 port->pkt_size = MVPP2_RX_PKT_SIZE(mtu);
7618 return 0;
7619 }
7620
7621 /* Reconfigure BM to the original MTU */
7622 err = mvpp2_bm_update_mtu(dev, dev->mtu);
7623 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02007624 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007625 }
7626
7627 mvpp2_stop_dev(port);
7628
7629 err = mvpp2_bm_update_mtu(dev, mtu);
7630 if (!err) {
7631 port->pkt_size = MVPP2_RX_PKT_SIZE(mtu);
7632 goto out_start;
7633 }
7634
7635 /* Reconfigure BM to the original MTU */
7636 err = mvpp2_bm_update_mtu(dev, dev->mtu);
7637 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02007638 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007639
7640out_start:
7641 mvpp2_start_dev(port);
7642 mvpp2_egress_enable(port);
7643 mvpp2_ingress_enable(port);
7644
7645 return 0;
Markus Elfringc1175542017-04-17 11:10:47 +02007646log_error:
Markus Elfringdfd42402017-04-17 11:20:41 +02007647 netdev_err(dev, "failed to change MTU\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03007648 return err;
7649}
7650
stephen hemmingerbc1f4472017-01-06 19:12:52 -08007651static void
Marcin Wojtas3f518502014-07-10 16:52:13 -03007652mvpp2_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
7653{
7654 struct mvpp2_port *port = netdev_priv(dev);
7655 unsigned int start;
7656 int cpu;
7657
7658 for_each_possible_cpu(cpu) {
7659 struct mvpp2_pcpu_stats *cpu_stats;
7660 u64 rx_packets;
7661 u64 rx_bytes;
7662 u64 tx_packets;
7663 u64 tx_bytes;
7664
7665 cpu_stats = per_cpu_ptr(port->stats, cpu);
7666 do {
7667 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
7668 rx_packets = cpu_stats->rx_packets;
7669 rx_bytes = cpu_stats->rx_bytes;
7670 tx_packets = cpu_stats->tx_packets;
7671 tx_bytes = cpu_stats->tx_bytes;
7672 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
7673
7674 stats->rx_packets += rx_packets;
7675 stats->rx_bytes += rx_bytes;
7676 stats->tx_packets += tx_packets;
7677 stats->tx_bytes += tx_bytes;
7678 }
7679
7680 stats->rx_errors = dev->stats.rx_errors;
7681 stats->rx_dropped = dev->stats.rx_dropped;
7682 stats->tx_dropped = dev->stats.tx_dropped;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007683}
7684
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007685static int mvpp2_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
7686{
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007687 int ret;
7688
Philippe Reynes8e072692016-06-28 00:08:11 +02007689 if (!dev->phydev)
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007690 return -ENOTSUPP;
7691
Philippe Reynes8e072692016-06-28 00:08:11 +02007692 ret = phy_mii_ioctl(dev->phydev, ifr, cmd);
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007693 if (!ret)
7694 mvpp2_link_event(dev);
7695
7696 return ret;
7697}
7698
Maxime Chevallier56beda32018-02-28 10:14:13 +01007699static int mvpp2_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
7700{
7701 struct mvpp2_port *port = netdev_priv(dev);
7702 int ret;
7703
7704 ret = mvpp2_prs_vid_entry_add(port, vid);
7705 if (ret)
7706 netdev_err(dev, "rx-vlan-filter offloading cannot accept more than %d VIDs per port\n",
7707 MVPP2_PRS_VLAN_FILT_MAX - 1);
7708 return ret;
7709}
7710
7711static int mvpp2_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
7712{
7713 struct mvpp2_port *port = netdev_priv(dev);
7714
7715 mvpp2_prs_vid_entry_remove(port, vid);
7716 return 0;
7717}
7718
7719static int mvpp2_set_features(struct net_device *dev,
7720 netdev_features_t features)
7721{
7722 netdev_features_t changed = dev->features ^ features;
7723 struct mvpp2_port *port = netdev_priv(dev);
7724
7725 if (changed & NETIF_F_HW_VLAN_CTAG_FILTER) {
7726 if (features & NETIF_F_HW_VLAN_CTAG_FILTER) {
7727 mvpp2_prs_vid_enable_filtering(port);
7728 } else {
7729 /* Invalidate all registered VID filters for this
7730 * port
7731 */
7732 mvpp2_prs_vid_remove_all(port);
7733
7734 mvpp2_prs_vid_disable_filtering(port);
7735 }
7736 }
7737
7738 return 0;
7739}
7740
Marcin Wojtas3f518502014-07-10 16:52:13 -03007741/* Ethtool methods */
7742
Marcin Wojtas3f518502014-07-10 16:52:13 -03007743/* Set interrupt coalescing for ethtools */
7744static int mvpp2_ethtool_set_coalesce(struct net_device *dev,
7745 struct ethtool_coalesce *c)
7746{
7747 struct mvpp2_port *port = netdev_priv(dev);
7748 int queue;
7749
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007750 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007751 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
7752
7753 rxq->time_coal = c->rx_coalesce_usecs;
7754 rxq->pkts_coal = c->rx_max_coalesced_frames;
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01007755 mvpp2_rx_pkts_coal_set(port, rxq);
7756 mvpp2_rx_time_coal_set(port, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007757 }
7758
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007759 if (port->has_tx_irqs) {
7760 port->tx_time_coal = c->tx_coalesce_usecs;
7761 mvpp2_tx_time_coal_set(port);
7762 }
7763
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007764 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007765 struct mvpp2_tx_queue *txq = port->txqs[queue];
7766
7767 txq->done_pkts_coal = c->tx_max_coalesced_frames;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007768
7769 if (port->has_tx_irqs)
7770 mvpp2_tx_pkts_coal_set(port, txq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007771 }
7772
Marcin Wojtas3f518502014-07-10 16:52:13 -03007773 return 0;
7774}
7775
7776/* get coalescing for ethtools */
7777static int mvpp2_ethtool_get_coalesce(struct net_device *dev,
7778 struct ethtool_coalesce *c)
7779{
7780 struct mvpp2_port *port = netdev_priv(dev);
7781
Antoine Tenart385c2842017-12-11 09:13:27 +01007782 c->rx_coalesce_usecs = port->rxqs[0]->time_coal;
7783 c->rx_max_coalesced_frames = port->rxqs[0]->pkts_coal;
7784 c->tx_max_coalesced_frames = port->txqs[0]->done_pkts_coal;
Antoine Tenart24b28cc2017-12-11 09:13:28 +01007785 c->tx_coalesce_usecs = port->tx_time_coal;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007786 return 0;
7787}
7788
7789static void mvpp2_ethtool_get_drvinfo(struct net_device *dev,
7790 struct ethtool_drvinfo *drvinfo)
7791{
7792 strlcpy(drvinfo->driver, MVPP2_DRIVER_NAME,
7793 sizeof(drvinfo->driver));
7794 strlcpy(drvinfo->version, MVPP2_DRIVER_VERSION,
7795 sizeof(drvinfo->version));
7796 strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
7797 sizeof(drvinfo->bus_info));
7798}
7799
7800static void mvpp2_ethtool_get_ringparam(struct net_device *dev,
7801 struct ethtool_ringparam *ring)
7802{
7803 struct mvpp2_port *port = netdev_priv(dev);
7804
Yan Markman7cf87e42017-12-11 09:13:26 +01007805 ring->rx_max_pending = MVPP2_MAX_RXD_MAX;
7806 ring->tx_max_pending = MVPP2_MAX_TXD_MAX;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007807 ring->rx_pending = port->rx_ring_size;
7808 ring->tx_pending = port->tx_ring_size;
7809}
7810
7811static int mvpp2_ethtool_set_ringparam(struct net_device *dev,
7812 struct ethtool_ringparam *ring)
7813{
7814 struct mvpp2_port *port = netdev_priv(dev);
7815 u16 prev_rx_ring_size = port->rx_ring_size;
7816 u16 prev_tx_ring_size = port->tx_ring_size;
7817 int err;
7818
7819 err = mvpp2_check_ringparam_valid(dev, ring);
7820 if (err)
7821 return err;
7822
7823 if (!netif_running(dev)) {
7824 port->rx_ring_size = ring->rx_pending;
7825 port->tx_ring_size = ring->tx_pending;
7826 return 0;
7827 }
7828
7829 /* The interface is running, so we have to force a
7830 * reallocation of the queues
7831 */
7832 mvpp2_stop_dev(port);
7833 mvpp2_cleanup_rxqs(port);
7834 mvpp2_cleanup_txqs(port);
7835
7836 port->rx_ring_size = ring->rx_pending;
7837 port->tx_ring_size = ring->tx_pending;
7838
7839 err = mvpp2_setup_rxqs(port);
7840 if (err) {
7841 /* Reallocate Rx queues with the original ring size */
7842 port->rx_ring_size = prev_rx_ring_size;
7843 ring->rx_pending = prev_rx_ring_size;
7844 err = mvpp2_setup_rxqs(port);
7845 if (err)
7846 goto err_out;
7847 }
7848 err = mvpp2_setup_txqs(port);
7849 if (err) {
7850 /* Reallocate Tx queues with the original ring size */
7851 port->tx_ring_size = prev_tx_ring_size;
7852 ring->tx_pending = prev_tx_ring_size;
7853 err = mvpp2_setup_txqs(port);
7854 if (err)
7855 goto err_clean_rxqs;
7856 }
7857
7858 mvpp2_start_dev(port);
7859 mvpp2_egress_enable(port);
7860 mvpp2_ingress_enable(port);
7861
7862 return 0;
7863
7864err_clean_rxqs:
7865 mvpp2_cleanup_rxqs(port);
7866err_out:
Markus Elfringdfd42402017-04-17 11:20:41 +02007867 netdev_err(dev, "failed to change ring parameters");
Marcin Wojtas3f518502014-07-10 16:52:13 -03007868 return err;
7869}
7870
7871/* Device ops */
7872
7873static const struct net_device_ops mvpp2_netdev_ops = {
7874 .ndo_open = mvpp2_open,
7875 .ndo_stop = mvpp2_stop,
7876 .ndo_start_xmit = mvpp2_tx,
7877 .ndo_set_rx_mode = mvpp2_set_rx_mode,
7878 .ndo_set_mac_address = mvpp2_set_mac_address,
7879 .ndo_change_mtu = mvpp2_change_mtu,
7880 .ndo_get_stats64 = mvpp2_get_stats64,
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007881 .ndo_do_ioctl = mvpp2_ioctl,
Maxime Chevallier56beda32018-02-28 10:14:13 +01007882 .ndo_vlan_rx_add_vid = mvpp2_vlan_rx_add_vid,
7883 .ndo_vlan_rx_kill_vid = mvpp2_vlan_rx_kill_vid,
7884 .ndo_set_features = mvpp2_set_features,
Marcin Wojtas3f518502014-07-10 16:52:13 -03007885};
7886
7887static const struct ethtool_ops mvpp2_eth_tool_ops = {
Florian Fainelli00606c42016-11-15 11:19:48 -08007888 .nway_reset = phy_ethtool_nway_reset,
Marcin Wojtas3f518502014-07-10 16:52:13 -03007889 .get_link = ethtool_op_get_link,
Marcin Wojtas3f518502014-07-10 16:52:13 -03007890 .set_coalesce = mvpp2_ethtool_set_coalesce,
7891 .get_coalesce = mvpp2_ethtool_get_coalesce,
7892 .get_drvinfo = mvpp2_ethtool_get_drvinfo,
7893 .get_ringparam = mvpp2_ethtool_get_ringparam,
7894 .set_ringparam = mvpp2_ethtool_set_ringparam,
Miquel Raynal118d6292017-11-06 22:56:53 +01007895 .get_strings = mvpp2_ethtool_get_strings,
7896 .get_ethtool_stats = mvpp2_ethtool_get_stats,
7897 .get_sset_count = mvpp2_ethtool_get_sset_count,
Philippe Reynesfb773e92016-06-28 00:08:12 +02007898 .get_link_ksettings = phy_ethtool_get_link_ksettings,
7899 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Marcin Wojtas3f518502014-07-10 16:52:13 -03007900};
7901
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007902/* Used for PPv2.1, or PPv2.2 with the old Device Tree binding that
7903 * had a single IRQ defined per-port.
7904 */
7905static int mvpp2_simple_queue_vectors_init(struct mvpp2_port *port,
7906 struct device_node *port_node)
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007907{
7908 struct mvpp2_queue_vector *v = &port->qvecs[0];
7909
7910 v->first_rxq = 0;
7911 v->nrxqs = port->nrxqs;
7912 v->type = MVPP2_QUEUE_VECTOR_SHARED;
7913 v->sw_thread_id = 0;
7914 v->sw_thread_mask = *cpumask_bits(cpu_online_mask);
7915 v->port = port;
7916 v->irq = irq_of_parse_and_map(port_node, 0);
7917 if (v->irq <= 0)
7918 return -EINVAL;
7919 netif_napi_add(port->dev, &v->napi, mvpp2_poll,
7920 NAPI_POLL_WEIGHT);
7921
7922 port->nqvecs = 1;
7923
7924 return 0;
7925}
7926
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007927static int mvpp2_multi_queue_vectors_init(struct mvpp2_port *port,
7928 struct device_node *port_node)
7929{
7930 struct mvpp2_queue_vector *v;
7931 int i, ret;
7932
7933 port->nqvecs = num_possible_cpus();
7934 if (queue_mode == MVPP2_QDIST_SINGLE_MODE)
7935 port->nqvecs += 1;
7936
7937 for (i = 0; i < port->nqvecs; i++) {
7938 char irqname[16];
7939
7940 v = port->qvecs + i;
7941
7942 v->port = port;
7943 v->type = MVPP2_QUEUE_VECTOR_PRIVATE;
7944 v->sw_thread_id = i;
7945 v->sw_thread_mask = BIT(i);
7946
7947 snprintf(irqname, sizeof(irqname), "tx-cpu%d", i);
7948
7949 if (queue_mode == MVPP2_QDIST_MULTI_MODE) {
7950 v->first_rxq = i * MVPP2_DEFAULT_RXQ;
7951 v->nrxqs = MVPP2_DEFAULT_RXQ;
7952 } else if (queue_mode == MVPP2_QDIST_SINGLE_MODE &&
7953 i == (port->nqvecs - 1)) {
7954 v->first_rxq = 0;
7955 v->nrxqs = port->nrxqs;
7956 v->type = MVPP2_QUEUE_VECTOR_SHARED;
7957 strncpy(irqname, "rx-shared", sizeof(irqname));
7958 }
7959
Marcin Wojtasa75edc72018-01-18 13:31:44 +01007960 if (port_node)
7961 v->irq = of_irq_get_byname(port_node, irqname);
7962 else
7963 v->irq = fwnode_irq_get(port->fwnode, i);
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007964 if (v->irq <= 0) {
7965 ret = -EINVAL;
7966 goto err;
7967 }
7968
7969 netif_napi_add(port->dev, &v->napi, mvpp2_poll,
7970 NAPI_POLL_WEIGHT);
7971 }
7972
7973 return 0;
7974
7975err:
7976 for (i = 0; i < port->nqvecs; i++)
7977 irq_dispose_mapping(port->qvecs[i].irq);
7978 return ret;
7979}
7980
7981static int mvpp2_queue_vectors_init(struct mvpp2_port *port,
7982 struct device_node *port_node)
7983{
7984 if (port->has_tx_irqs)
7985 return mvpp2_multi_queue_vectors_init(port, port_node);
7986 else
7987 return mvpp2_simple_queue_vectors_init(port, port_node);
7988}
7989
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007990static void mvpp2_queue_vectors_deinit(struct mvpp2_port *port)
7991{
7992 int i;
7993
7994 for (i = 0; i < port->nqvecs; i++)
7995 irq_dispose_mapping(port->qvecs[i].irq);
7996}
7997
7998/* Configure Rx queue group interrupt for this port */
7999static void mvpp2_rx_irqs_setup(struct mvpp2_port *port)
8000{
8001 struct mvpp2 *priv = port->priv;
8002 u32 val;
8003 int i;
8004
8005 if (priv->hw_version == MVPP21) {
8006 mvpp2_write(priv, MVPP21_ISR_RXQ_GROUP_REG(port->id),
8007 port->nrxqs);
8008 return;
8009 }
8010
8011 /* Handle the more complicated PPv2.2 case */
8012 for (i = 0; i < port->nqvecs; i++) {
8013 struct mvpp2_queue_vector *qv = port->qvecs + i;
8014
8015 if (!qv->nrxqs)
8016 continue;
8017
8018 val = qv->sw_thread_id;
8019 val |= port->id << MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_OFFSET;
8020 mvpp2_write(priv, MVPP22_ISR_RXQ_GROUP_INDEX_REG, val);
8021
8022 val = qv->first_rxq;
8023 val |= qv->nrxqs << MVPP22_ISR_RXQ_SUB_GROUP_SIZE_OFFSET;
8024 mvpp2_write(priv, MVPP22_ISR_RXQ_SUB_GROUP_CONFIG_REG, val);
8025 }
8026}
8027
Marcin Wojtas3f518502014-07-10 16:52:13 -03008028/* Initialize port HW */
8029static int mvpp2_port_init(struct mvpp2_port *port)
8030{
8031 struct device *dev = port->dev->dev.parent;
8032 struct mvpp2 *priv = port->priv;
8033 struct mvpp2_txq_pcpu *txq_pcpu;
8034 int queue, cpu, err;
8035
Thomas Petazzoni09f83972017-08-03 10:41:57 +02008036 /* Checks for hardware constraints */
8037 if (port->first_rxq + port->nrxqs >
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01008038 MVPP2_MAX_PORTS * priv->max_port_rxqs)
Marcin Wojtas3f518502014-07-10 16:52:13 -03008039 return -EINVAL;
8040
Thomas Petazzoni09f83972017-08-03 10:41:57 +02008041 if (port->nrxqs % 4 || (port->nrxqs > priv->max_port_rxqs) ||
8042 (port->ntxqs > MVPP2_MAX_TXQ))
8043 return -EINVAL;
8044
Marcin Wojtas3f518502014-07-10 16:52:13 -03008045 /* Disable port */
8046 mvpp2_egress_disable(port);
8047 mvpp2_port_disable(port);
8048
Thomas Petazzoni213f4282017-08-03 10:42:00 +02008049 port->tx_time_coal = MVPP2_TXDONE_COAL_USEC;
8050
Thomas Petazzoni09f83972017-08-03 10:41:57 +02008051 port->txqs = devm_kcalloc(dev, port->ntxqs, sizeof(*port->txqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03008052 GFP_KERNEL);
8053 if (!port->txqs)
8054 return -ENOMEM;
8055
8056 /* Associate physical Tx queues to this port and initialize.
8057 * The mapping is predefined.
8058 */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02008059 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03008060 int queue_phy_id = mvpp2_txq_phys(port->id, queue);
8061 struct mvpp2_tx_queue *txq;
8062
8063 txq = devm_kzalloc(dev, sizeof(*txq), GFP_KERNEL);
Christophe Jaillet177c8d12017-02-19 10:19:57 +01008064 if (!txq) {
8065 err = -ENOMEM;
8066 goto err_free_percpu;
8067 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03008068
8069 txq->pcpu = alloc_percpu(struct mvpp2_txq_pcpu);
8070 if (!txq->pcpu) {
8071 err = -ENOMEM;
8072 goto err_free_percpu;
8073 }
8074
8075 txq->id = queue_phy_id;
8076 txq->log_id = queue;
8077 txq->done_pkts_coal = MVPP2_TXDONE_COAL_PKTS_THRESH;
8078 for_each_present_cpu(cpu) {
8079 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
8080 txq_pcpu->cpu = cpu;
8081 }
8082
8083 port->txqs[queue] = txq;
8084 }
8085
Thomas Petazzoni09f83972017-08-03 10:41:57 +02008086 port->rxqs = devm_kcalloc(dev, port->nrxqs, sizeof(*port->rxqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03008087 GFP_KERNEL);
8088 if (!port->rxqs) {
8089 err = -ENOMEM;
8090 goto err_free_percpu;
8091 }
8092
8093 /* Allocate and initialize Rx queue for this port */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02008094 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03008095 struct mvpp2_rx_queue *rxq;
8096
8097 /* Map physical Rx queue to port's logical Rx queue */
8098 rxq = devm_kzalloc(dev, sizeof(*rxq), GFP_KERNEL);
Jisheng Zhangd82b0c22016-03-31 17:01:23 +08008099 if (!rxq) {
8100 err = -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008101 goto err_free_percpu;
Jisheng Zhangd82b0c22016-03-31 17:01:23 +08008102 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03008103 /* Map this Rx queue to a physical queue */
8104 rxq->id = port->first_rxq + queue;
8105 rxq->port = port->id;
8106 rxq->logic_rxq = queue;
8107
8108 port->rxqs[queue] = rxq;
8109 }
8110
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02008111 mvpp2_rx_irqs_setup(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008112
8113 /* Create Rx descriptor rings */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02008114 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03008115 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
8116
8117 rxq->size = port->rx_ring_size;
8118 rxq->pkts_coal = MVPP2_RX_COAL_PKTS;
8119 rxq->time_coal = MVPP2_RX_COAL_USEC;
8120 }
8121
8122 mvpp2_ingress_disable(port);
8123
8124 /* Port default configuration */
8125 mvpp2_defaults_set(port);
8126
8127 /* Port's classifier configuration */
8128 mvpp2_cls_oversize_rxq_set(port);
8129 mvpp2_cls_port_config(port);
8130
8131 /* Provide an initial Rx packet size */
8132 port->pkt_size = MVPP2_RX_PKT_SIZE(port->dev->mtu);
8133
8134 /* Initialize pools for swf */
8135 err = mvpp2_swf_bm_pool_init(port);
8136 if (err)
8137 goto err_free_percpu;
8138
8139 return 0;
8140
8141err_free_percpu:
Thomas Petazzoni09f83972017-08-03 10:41:57 +02008142 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03008143 if (!port->txqs[queue])
8144 continue;
8145 free_percpu(port->txqs[queue]->pcpu);
8146 }
8147 return err;
8148}
8149
Thomas Petazzoni213f4282017-08-03 10:42:00 +02008150/* Checks if the port DT description has the TX interrupts
8151 * described. On PPv2.1, there are no such interrupts. On PPv2.2,
8152 * there are available, but we need to keep support for old DTs.
8153 */
8154static bool mvpp2_port_has_tx_irqs(struct mvpp2 *priv,
8155 struct device_node *port_node)
8156{
8157 char *irqs[5] = { "rx-shared", "tx-cpu0", "tx-cpu1",
8158 "tx-cpu2", "tx-cpu3" };
8159 int ret, i;
8160
8161 if (priv->hw_version == MVPP21)
8162 return false;
8163
8164 for (i = 0; i < 5; i++) {
8165 ret = of_property_match_string(port_node, "interrupt-names",
8166 irqs[i]);
8167 if (ret < 0)
8168 return false;
8169 }
8170
8171 return true;
8172}
8173
Antoine Tenart3ba8c812017-09-02 11:06:47 +02008174static void mvpp2_port_copy_mac_addr(struct net_device *dev, struct mvpp2 *priv,
Marcin Wojtas24812222018-01-18 13:31:43 +01008175 struct fwnode_handle *fwnode,
Antoine Tenart3ba8c812017-09-02 11:06:47 +02008176 char **mac_from)
8177{
8178 struct mvpp2_port *port = netdev_priv(dev);
8179 char hw_mac_addr[ETH_ALEN] = {0};
Marcin Wojtas24812222018-01-18 13:31:43 +01008180 char fw_mac_addr[ETH_ALEN];
Antoine Tenart3ba8c812017-09-02 11:06:47 +02008181
Marcin Wojtas24812222018-01-18 13:31:43 +01008182 if (fwnode_get_mac_address(fwnode, fw_mac_addr, ETH_ALEN)) {
8183 *mac_from = "firmware node";
8184 ether_addr_copy(dev->dev_addr, fw_mac_addr);
Antoine Tenart688cbaf2017-09-02 11:06:49 +02008185 return;
Antoine Tenart3ba8c812017-09-02 11:06:47 +02008186 }
Antoine Tenart688cbaf2017-09-02 11:06:49 +02008187
8188 if (priv->hw_version == MVPP21) {
8189 mvpp21_get_mac_address(port, hw_mac_addr);
8190 if (is_valid_ether_addr(hw_mac_addr)) {
8191 *mac_from = "hardware";
8192 ether_addr_copy(dev->dev_addr, hw_mac_addr);
8193 return;
8194 }
8195 }
8196
8197 *mac_from = "random";
8198 eth_hw_addr_random(dev);
Antoine Tenart3ba8c812017-09-02 11:06:47 +02008199}
8200
Marcin Wojtas3f518502014-07-10 16:52:13 -03008201/* Ports initialization */
8202static int mvpp2_port_probe(struct platform_device *pdev,
Marcin Wojtas24812222018-01-18 13:31:43 +01008203 struct fwnode_handle *port_fwnode,
Marcin Wojtasbf147152018-01-18 13:31:42 +01008204 struct mvpp2 *priv)
Marcin Wojtas3f518502014-07-10 16:52:13 -03008205{
8206 struct device_node *phy_node;
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008207 struct phy *comphy = NULL;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008208 struct mvpp2_port *port;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02008209 struct mvpp2_port_pcpu *port_pcpu;
Marcin Wojtas24812222018-01-18 13:31:43 +01008210 struct device_node *port_node = to_of_node(port_fwnode);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008211 struct net_device *dev;
8212 struct resource *res;
Antoine Tenart3ba8c812017-09-02 11:06:47 +02008213 char *mac_from = "";
Thomas Petazzoni09f83972017-08-03 10:41:57 +02008214 unsigned int ntxqs, nrxqs;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02008215 bool has_tx_irqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008216 u32 id;
8217 int features;
8218 int phy_mode;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02008219 int err, i, cpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008220
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008221 if (port_node) {
8222 has_tx_irqs = mvpp2_port_has_tx_irqs(priv, port_node);
8223 } else {
8224 has_tx_irqs = true;
8225 queue_mode = MVPP2_QDIST_MULTI_MODE;
8226 }
Thomas Petazzoni213f4282017-08-03 10:42:00 +02008227
8228 if (!has_tx_irqs)
8229 queue_mode = MVPP2_QDIST_SINGLE_MODE;
8230
Thomas Petazzoni09f83972017-08-03 10:41:57 +02008231 ntxqs = MVPP2_MAX_TXQ;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02008232 if (priv->hw_version == MVPP22 && queue_mode == MVPP2_QDIST_MULTI_MODE)
8233 nrxqs = MVPP2_DEFAULT_RXQ * num_possible_cpus();
8234 else
8235 nrxqs = MVPP2_DEFAULT_RXQ;
Thomas Petazzoni09f83972017-08-03 10:41:57 +02008236
8237 dev = alloc_etherdev_mqs(sizeof(*port), ntxqs, nrxqs);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008238 if (!dev)
8239 return -ENOMEM;
8240
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008241 if (port_node)
8242 phy_node = of_parse_phandle(port_node, "phy", 0);
8243 else
8244 phy_node = NULL;
8245
Marcin Wojtas24812222018-01-18 13:31:43 +01008246 phy_mode = fwnode_get_phy_mode(port_fwnode);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008247 if (phy_mode < 0) {
8248 dev_err(&pdev->dev, "incorrect phy mode\n");
8249 err = phy_mode;
8250 goto err_free_netdev;
8251 }
8252
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008253 if (port_node) {
8254 comphy = devm_of_phy_get(&pdev->dev, port_node, NULL);
8255 if (IS_ERR(comphy)) {
8256 if (PTR_ERR(comphy) == -EPROBE_DEFER) {
8257 err = -EPROBE_DEFER;
8258 goto err_free_netdev;
8259 }
8260 comphy = NULL;
Antoine Tenart542897d2017-08-30 10:29:15 +02008261 }
Antoine Tenart542897d2017-08-30 10:29:15 +02008262 }
8263
Marcin Wojtas24812222018-01-18 13:31:43 +01008264 if (fwnode_property_read_u32(port_fwnode, "port-id", &id)) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03008265 err = -EINVAL;
8266 dev_err(&pdev->dev, "missing port-id value\n");
8267 goto err_free_netdev;
8268 }
8269
Yan Markman7cf87e42017-12-11 09:13:26 +01008270 dev->tx_queue_len = MVPP2_MAX_TXD_MAX;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008271 dev->watchdog_timeo = 5 * HZ;
8272 dev->netdev_ops = &mvpp2_netdev_ops;
8273 dev->ethtool_ops = &mvpp2_eth_tool_ops;
8274
8275 port = netdev_priv(dev);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02008276 port->dev = dev;
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008277 port->fwnode = port_fwnode;
Thomas Petazzoni09f83972017-08-03 10:41:57 +02008278 port->ntxqs = ntxqs;
8279 port->nrxqs = nrxqs;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02008280 port->priv = priv;
8281 port->has_tx_irqs = has_tx_irqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008282
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02008283 err = mvpp2_queue_vectors_init(port, port_node);
8284 if (err)
Marcin Wojtas3f518502014-07-10 16:52:13 -03008285 goto err_free_netdev;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008286
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008287 if (port_node)
8288 port->link_irq = of_irq_get_byname(port_node, "link");
8289 else
8290 port->link_irq = fwnode_irq_get(port_fwnode, port->nqvecs + 1);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02008291 if (port->link_irq == -EPROBE_DEFER) {
8292 err = -EPROBE_DEFER;
8293 goto err_deinit_qvecs;
8294 }
8295 if (port->link_irq <= 0)
8296 /* the link irq is optional */
8297 port->link_irq = 0;
8298
Marcin Wojtas24812222018-01-18 13:31:43 +01008299 if (fwnode_property_read_bool(port_fwnode, "marvell,loopback"))
Marcin Wojtas3f518502014-07-10 16:52:13 -03008300 port->flags |= MVPP2_F_LOOPBACK;
8301
Marcin Wojtas3f518502014-07-10 16:52:13 -03008302 port->id = id;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01008303 if (priv->hw_version == MVPP21)
Thomas Petazzoni09f83972017-08-03 10:41:57 +02008304 port->first_rxq = port->id * port->nrxqs;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01008305 else
8306 port->first_rxq = port->id * priv->max_port_rxqs;
8307
Marcin Wojtas3f518502014-07-10 16:52:13 -03008308 port->phy_node = phy_node;
8309 port->phy_interface = phy_mode;
Antoine Tenart542897d2017-08-30 10:29:15 +02008310 port->comphy = comphy;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008311
Thomas Petazzonia7868412017-03-07 16:53:13 +01008312 if (priv->hw_version == MVPP21) {
8313 res = platform_get_resource(pdev, IORESOURCE_MEM, 2 + id);
8314 port->base = devm_ioremap_resource(&pdev->dev, res);
8315 if (IS_ERR(port->base)) {
8316 err = PTR_ERR(port->base);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02008317 goto err_free_irq;
Thomas Petazzonia7868412017-03-07 16:53:13 +01008318 }
Miquel Raynal118d6292017-11-06 22:56:53 +01008319
8320 port->stats_base = port->priv->lms_base +
8321 MVPP21_MIB_COUNTERS_OFFSET +
8322 port->gop_id * MVPP21_MIB_COUNTERS_PORT_SZ;
Thomas Petazzonia7868412017-03-07 16:53:13 +01008323 } else {
Marcin Wojtas24812222018-01-18 13:31:43 +01008324 if (fwnode_property_read_u32(port_fwnode, "gop-port-id",
8325 &port->gop_id)) {
Thomas Petazzonia7868412017-03-07 16:53:13 +01008326 err = -EINVAL;
8327 dev_err(&pdev->dev, "missing gop-port-id value\n");
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02008328 goto err_deinit_qvecs;
Thomas Petazzonia7868412017-03-07 16:53:13 +01008329 }
8330
8331 port->base = priv->iface_base + MVPP22_GMAC_BASE(port->gop_id);
Miquel Raynal118d6292017-11-06 22:56:53 +01008332 port->stats_base = port->priv->iface_base +
8333 MVPP22_MIB_COUNTERS_OFFSET +
8334 port->gop_id * MVPP22_MIB_COUNTERS_PORT_SZ;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008335 }
8336
Miquel Raynal118d6292017-11-06 22:56:53 +01008337 /* Alloc per-cpu and ethtool stats */
Marcin Wojtas3f518502014-07-10 16:52:13 -03008338 port->stats = netdev_alloc_pcpu_stats(struct mvpp2_pcpu_stats);
8339 if (!port->stats) {
8340 err = -ENOMEM;
Antoine Tenartfd3651b2017-09-01 11:04:54 +02008341 goto err_free_irq;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008342 }
8343
Miquel Raynal118d6292017-11-06 22:56:53 +01008344 port->ethtool_stats = devm_kcalloc(&pdev->dev,
8345 ARRAY_SIZE(mvpp2_ethtool_regs),
8346 sizeof(u64), GFP_KERNEL);
8347 if (!port->ethtool_stats) {
8348 err = -ENOMEM;
8349 goto err_free_stats;
8350 }
8351
Miquel Raynale5c500e2017-11-08 08:59:40 +01008352 mutex_init(&port->gather_stats_lock);
8353 INIT_DELAYED_WORK(&port->stats_work, mvpp2_gather_hw_statistics);
8354
Marcin Wojtas24812222018-01-18 13:31:43 +01008355 mvpp2_port_copy_mac_addr(dev, priv, port_fwnode, &mac_from);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008356
Yan Markman7cf87e42017-12-11 09:13:26 +01008357 port->tx_ring_size = MVPP2_MAX_TXD_DFLT;
8358 port->rx_ring_size = MVPP2_MAX_RXD_DFLT;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008359 SET_NETDEV_DEV(dev, &pdev->dev);
8360
8361 err = mvpp2_port_init(port);
8362 if (err < 0) {
8363 dev_err(&pdev->dev, "failed to init port %d\n", id);
8364 goto err_free_stats;
8365 }
Thomas Petazzoni26975822017-03-07 16:53:14 +01008366
Thomas Petazzoni26975822017-03-07 16:53:14 +01008367 mvpp2_port_periodic_xon_disable(port);
8368
8369 if (priv->hw_version == MVPP21)
8370 mvpp2_port_fc_adv_enable(port);
8371
8372 mvpp2_port_reset(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008373
Marcin Wojtasedc660f2015-08-06 19:00:30 +02008374 port->pcpu = alloc_percpu(struct mvpp2_port_pcpu);
8375 if (!port->pcpu) {
8376 err = -ENOMEM;
8377 goto err_free_txq_pcpu;
8378 }
8379
Thomas Petazzoni213f4282017-08-03 10:42:00 +02008380 if (!port->has_tx_irqs) {
8381 for_each_present_cpu(cpu) {
8382 port_pcpu = per_cpu_ptr(port->pcpu, cpu);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02008383
Thomas Petazzoni213f4282017-08-03 10:42:00 +02008384 hrtimer_init(&port_pcpu->tx_done_timer, CLOCK_MONOTONIC,
8385 HRTIMER_MODE_REL_PINNED);
8386 port_pcpu->tx_done_timer.function = mvpp2_hr_timer_cb;
8387 port_pcpu->timer_scheduled = false;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02008388
Thomas Petazzoni213f4282017-08-03 10:42:00 +02008389 tasklet_init(&port_pcpu->tx_done_tasklet,
8390 mvpp2_tx_proc_cb,
8391 (unsigned long)dev);
8392 }
Marcin Wojtasedc660f2015-08-06 19:00:30 +02008393 }
8394
Antoine Tenart381c5672018-03-05 15:16:53 +01008395 features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
8396 NETIF_F_TSO;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008397 dev->features = features | NETIF_F_RXCSUM;
Maxime Chevallier56beda32018-02-28 10:14:13 +01008398 dev->hw_features |= features | NETIF_F_RXCSUM | NETIF_F_GRO |
8399 NETIF_F_HW_VLAN_CTAG_FILTER;
Stefan Chulski576193f2018-03-05 15:16:54 +01008400
8401 if (port->pool_long->id == MVPP2_BM_JUMBO && port->id != 0) {
8402 dev->features &= ~(NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM);
8403 dev->hw_features &= ~(NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM);
8404 }
8405
Marcin Wojtas3f518502014-07-10 16:52:13 -03008406 dev->vlan_features |= features;
Antoine Tenart1d17db02017-10-30 11:23:31 +01008407 dev->gso_max_segs = MVPP2_MAX_TSO_SEGS;
Maxime Chevallier10fea262018-03-07 15:18:04 +01008408 dev->priv_flags |= IFF_UNICAST_FLT;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008409
Stefan Chulski576193f2018-03-05 15:16:54 +01008410 /* MTU range: 68 - 9704 */
Jarod Wilson57779872016-10-17 15:54:06 -04008411 dev->min_mtu = ETH_MIN_MTU;
Stefan Chulski576193f2018-03-05 15:16:54 +01008412 /* 9704 == 9728 - 20 and rounding to 8 */
8413 dev->max_mtu = MVPP2_BM_JUMBO_PKT_SIZE;
Jarod Wilson57779872016-10-17 15:54:06 -04008414
Marcin Wojtas3f518502014-07-10 16:52:13 -03008415 err = register_netdev(dev);
8416 if (err < 0) {
8417 dev_err(&pdev->dev, "failed to register netdev\n");
Marcin Wojtasedc660f2015-08-06 19:00:30 +02008418 goto err_free_port_pcpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008419 }
8420 netdev_info(dev, "Using %s mac address %pM\n", mac_from, dev->dev_addr);
8421
Marcin Wojtasbf147152018-01-18 13:31:42 +01008422 priv->port_list[priv->port_count++] = port;
8423
Marcin Wojtas3f518502014-07-10 16:52:13 -03008424 return 0;
8425
Marcin Wojtasedc660f2015-08-06 19:00:30 +02008426err_free_port_pcpu:
8427 free_percpu(port->pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008428err_free_txq_pcpu:
Thomas Petazzoni09f83972017-08-03 10:41:57 +02008429 for (i = 0; i < port->ntxqs; i++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03008430 free_percpu(port->txqs[i]->pcpu);
8431err_free_stats:
8432 free_percpu(port->stats);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02008433err_free_irq:
8434 if (port->link_irq)
8435 irq_dispose_mapping(port->link_irq);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02008436err_deinit_qvecs:
8437 mvpp2_queue_vectors_deinit(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008438err_free_netdev:
Peter Chenccb80392016-08-01 15:02:37 +08008439 of_node_put(phy_node);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008440 free_netdev(dev);
8441 return err;
8442}
8443
8444/* Ports removal routine */
8445static void mvpp2_port_remove(struct mvpp2_port *port)
8446{
8447 int i;
8448
8449 unregister_netdev(port->dev);
Peter Chenccb80392016-08-01 15:02:37 +08008450 of_node_put(port->phy_node);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02008451 free_percpu(port->pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008452 free_percpu(port->stats);
Thomas Petazzoni09f83972017-08-03 10:41:57 +02008453 for (i = 0; i < port->ntxqs; i++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03008454 free_percpu(port->txqs[i]->pcpu);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02008455 mvpp2_queue_vectors_deinit(port);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02008456 if (port->link_irq)
8457 irq_dispose_mapping(port->link_irq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008458 free_netdev(port->dev);
8459}
8460
8461/* Initialize decoding windows */
8462static void mvpp2_conf_mbus_windows(const struct mbus_dram_target_info *dram,
8463 struct mvpp2 *priv)
8464{
8465 u32 win_enable;
8466 int i;
8467
8468 for (i = 0; i < 6; i++) {
8469 mvpp2_write(priv, MVPP2_WIN_BASE(i), 0);
8470 mvpp2_write(priv, MVPP2_WIN_SIZE(i), 0);
8471
8472 if (i < 4)
8473 mvpp2_write(priv, MVPP2_WIN_REMAP(i), 0);
8474 }
8475
8476 win_enable = 0;
8477
8478 for (i = 0; i < dram->num_cs; i++) {
8479 const struct mbus_dram_window *cs = dram->cs + i;
8480
8481 mvpp2_write(priv, MVPP2_WIN_BASE(i),
8482 (cs->base & 0xffff0000) | (cs->mbus_attr << 8) |
8483 dram->mbus_dram_target_id);
8484
8485 mvpp2_write(priv, MVPP2_WIN_SIZE(i),
8486 (cs->size - 1) & 0xffff0000);
8487
8488 win_enable |= (1 << i);
8489 }
8490
8491 mvpp2_write(priv, MVPP2_BASE_ADDR_ENABLE, win_enable);
8492}
8493
8494/* Initialize Rx FIFO's */
8495static void mvpp2_rx_fifo_init(struct mvpp2 *priv)
8496{
8497 int port;
8498
8499 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
8500 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
Antoine Tenart2d1d7df2017-10-30 11:23:28 +01008501 MVPP2_RX_FIFO_PORT_DATA_SIZE_4KB);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008502 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
Antoine Tenart2d1d7df2017-10-30 11:23:28 +01008503 MVPP2_RX_FIFO_PORT_ATTR_SIZE_4KB);
8504 }
8505
8506 mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
8507 MVPP2_RX_FIFO_PORT_MIN_PKT);
8508 mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
8509}
8510
8511static void mvpp22_rx_fifo_init(struct mvpp2 *priv)
8512{
8513 int port;
8514
8515 /* The FIFO size parameters are set depending on the maximum speed a
8516 * given port can handle:
8517 * - Port 0: 10Gbps
8518 * - Port 1: 2.5Gbps
8519 * - Ports 2 and 3: 1Gbps
8520 */
8521
8522 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(0),
8523 MVPP2_RX_FIFO_PORT_DATA_SIZE_32KB);
8524 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(0),
8525 MVPP2_RX_FIFO_PORT_ATTR_SIZE_32KB);
8526
8527 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(1),
8528 MVPP2_RX_FIFO_PORT_DATA_SIZE_8KB);
8529 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(1),
8530 MVPP2_RX_FIFO_PORT_ATTR_SIZE_8KB);
8531
8532 for (port = 2; port < MVPP2_MAX_PORTS; port++) {
8533 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
8534 MVPP2_RX_FIFO_PORT_DATA_SIZE_4KB);
8535 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
8536 MVPP2_RX_FIFO_PORT_ATTR_SIZE_4KB);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008537 }
8538
8539 mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
8540 MVPP2_RX_FIFO_PORT_MIN_PKT);
8541 mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
8542}
8543
Yan Markman93ff1302018-03-05 15:16:52 +01008544/* Initialize Tx FIFO's: the total FIFO size is 19kB on PPv2.2 and 10G
8545 * interfaces must have a Tx FIFO size of 10kB. As only port 0 can do 10G,
8546 * configure its Tx FIFO size to 10kB and the others ports Tx FIFO size to 3kB.
8547 */
Antoine Tenart7c10f972017-10-30 11:23:29 +01008548static void mvpp22_tx_fifo_init(struct mvpp2 *priv)
8549{
Yan Markman93ff1302018-03-05 15:16:52 +01008550 int port, size, thrs;
Antoine Tenart7c10f972017-10-30 11:23:29 +01008551
Yan Markman93ff1302018-03-05 15:16:52 +01008552 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
8553 if (port == 0) {
8554 size = MVPP22_TX_FIFO_DATA_SIZE_10KB;
8555 thrs = MVPP2_TX_FIFO_THRESHOLD_10KB;
8556 } else {
8557 size = MVPP22_TX_FIFO_DATA_SIZE_3KB;
8558 thrs = MVPP2_TX_FIFO_THRESHOLD_3KB;
8559 }
8560 mvpp2_write(priv, MVPP22_TX_FIFO_SIZE_REG(port), size);
8561 mvpp2_write(priv, MVPP22_TX_FIFO_THRESH_REG(port), thrs);
8562 }
Antoine Tenart7c10f972017-10-30 11:23:29 +01008563}
8564
Thomas Petazzoni6763ce32017-03-07 16:53:15 +01008565static void mvpp2_axi_init(struct mvpp2 *priv)
8566{
8567 u32 val, rdval, wrval;
8568
8569 mvpp2_write(priv, MVPP22_BM_ADDR_HIGH_RLS_REG, 0x0);
8570
8571 /* AXI Bridge Configuration */
8572
8573 rdval = MVPP22_AXI_CODE_CACHE_RD_CACHE
8574 << MVPP22_AXI_ATTR_CACHE_OFFS;
8575 rdval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
8576 << MVPP22_AXI_ATTR_DOMAIN_OFFS;
8577
8578 wrval = MVPP22_AXI_CODE_CACHE_WR_CACHE
8579 << MVPP22_AXI_ATTR_CACHE_OFFS;
8580 wrval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
8581 << MVPP22_AXI_ATTR_DOMAIN_OFFS;
8582
8583 /* BM */
8584 mvpp2_write(priv, MVPP22_AXI_BM_WR_ATTR_REG, wrval);
8585 mvpp2_write(priv, MVPP22_AXI_BM_RD_ATTR_REG, rdval);
8586
8587 /* Descriptors */
8588 mvpp2_write(priv, MVPP22_AXI_AGGRQ_DESCR_RD_ATTR_REG, rdval);
8589 mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_WR_ATTR_REG, wrval);
8590 mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_RD_ATTR_REG, rdval);
8591 mvpp2_write(priv, MVPP22_AXI_RXQ_DESCR_WR_ATTR_REG, wrval);
8592
8593 /* Buffer Data */
8594 mvpp2_write(priv, MVPP22_AXI_TX_DATA_RD_ATTR_REG, rdval);
8595 mvpp2_write(priv, MVPP22_AXI_RX_DATA_WR_ATTR_REG, wrval);
8596
8597 val = MVPP22_AXI_CODE_CACHE_NON_CACHE
8598 << MVPP22_AXI_CODE_CACHE_OFFS;
8599 val |= MVPP22_AXI_CODE_DOMAIN_SYSTEM
8600 << MVPP22_AXI_CODE_DOMAIN_OFFS;
8601 mvpp2_write(priv, MVPP22_AXI_RD_NORMAL_CODE_REG, val);
8602 mvpp2_write(priv, MVPP22_AXI_WR_NORMAL_CODE_REG, val);
8603
8604 val = MVPP22_AXI_CODE_CACHE_RD_CACHE
8605 << MVPP22_AXI_CODE_CACHE_OFFS;
8606 val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
8607 << MVPP22_AXI_CODE_DOMAIN_OFFS;
8608
8609 mvpp2_write(priv, MVPP22_AXI_RD_SNOOP_CODE_REG, val);
8610
8611 val = MVPP22_AXI_CODE_CACHE_WR_CACHE
8612 << MVPP22_AXI_CODE_CACHE_OFFS;
8613 val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
8614 << MVPP22_AXI_CODE_DOMAIN_OFFS;
8615
8616 mvpp2_write(priv, MVPP22_AXI_WR_SNOOP_CODE_REG, val);
8617}
8618
Marcin Wojtas3f518502014-07-10 16:52:13 -03008619/* Initialize network controller common part HW */
8620static int mvpp2_init(struct platform_device *pdev, struct mvpp2 *priv)
8621{
8622 const struct mbus_dram_target_info *dram_target_info;
8623 int err, i;
Marcin Wojtas08a23752014-07-21 13:48:12 -03008624 u32 val;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008625
Marcin Wojtas3f518502014-07-10 16:52:13 -03008626 /* MBUS windows configuration */
8627 dram_target_info = mv_mbus_dram_info();
8628 if (dram_target_info)
8629 mvpp2_conf_mbus_windows(dram_target_info, priv);
8630
Thomas Petazzoni6763ce32017-03-07 16:53:15 +01008631 if (priv->hw_version == MVPP22)
8632 mvpp2_axi_init(priv);
8633
Marcin Wojtas08a23752014-07-21 13:48:12 -03008634 /* Disable HW PHY polling */
Thomas Petazzoni26975822017-03-07 16:53:14 +01008635 if (priv->hw_version == MVPP21) {
8636 val = readl(priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
8637 val |= MVPP2_PHY_AN_STOP_SMI0_MASK;
8638 writel(val, priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
8639 } else {
8640 val = readl(priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
8641 val &= ~MVPP22_SMI_POLLING_EN;
8642 writel(val, priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
8643 }
Marcin Wojtas08a23752014-07-21 13:48:12 -03008644
Marcin Wojtas3f518502014-07-10 16:52:13 -03008645 /* Allocate and initialize aggregated TXQs */
8646 priv->aggr_txqs = devm_kcalloc(&pdev->dev, num_present_cpus(),
Markus Elfringd7ce3ce2017-04-17 08:48:23 +02008647 sizeof(*priv->aggr_txqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03008648 GFP_KERNEL);
8649 if (!priv->aggr_txqs)
8650 return -ENOMEM;
8651
8652 for_each_present_cpu(i) {
8653 priv->aggr_txqs[i].id = i;
8654 priv->aggr_txqs[i].size = MVPP2_AGGR_TXQ_SIZE;
Antoine Ténart85affd72017-08-23 09:46:55 +02008655 err = mvpp2_aggr_txq_init(pdev, &priv->aggr_txqs[i], i, priv);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008656 if (err < 0)
8657 return err;
8658 }
8659
Antoine Tenart7c10f972017-10-30 11:23:29 +01008660 /* Fifo Init */
8661 if (priv->hw_version == MVPP21) {
Antoine Tenart2d1d7df2017-10-30 11:23:28 +01008662 mvpp2_rx_fifo_init(priv);
Antoine Tenart7c10f972017-10-30 11:23:29 +01008663 } else {
Antoine Tenart2d1d7df2017-10-30 11:23:28 +01008664 mvpp22_rx_fifo_init(priv);
Antoine Tenart7c10f972017-10-30 11:23:29 +01008665 mvpp22_tx_fifo_init(priv);
8666 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03008667
Thomas Petazzoni26975822017-03-07 16:53:14 +01008668 if (priv->hw_version == MVPP21)
8669 writel(MVPP2_EXT_GLOBAL_CTRL_DEFAULT,
8670 priv->lms_base + MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008671
8672 /* Allow cache snoop when transmiting packets */
8673 mvpp2_write(priv, MVPP2_TX_SNOOP_REG, 0x1);
8674
8675 /* Buffer Manager initialization */
8676 err = mvpp2_bm_init(pdev, priv);
8677 if (err < 0)
8678 return err;
8679
8680 /* Parser default initialization */
8681 err = mvpp2_prs_default_init(pdev, priv);
8682 if (err < 0)
8683 return err;
8684
8685 /* Classifier default initialization */
8686 mvpp2_cls_init(priv);
8687
8688 return 0;
8689}
8690
8691static int mvpp2_probe(struct platform_device *pdev)
8692{
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008693 const struct acpi_device_id *acpi_id;
Marcin Wojtas24812222018-01-18 13:31:43 +01008694 struct fwnode_handle *fwnode = pdev->dev.fwnode;
8695 struct fwnode_handle *port_fwnode;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008696 struct mvpp2 *priv;
8697 struct resource *res;
Thomas Petazzonia7868412017-03-07 16:53:13 +01008698 void __iomem *base;
Miquel Raynal118d6292017-11-06 22:56:53 +01008699 int i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008700 int err;
8701
Markus Elfring0b92e592017-04-17 08:38:32 +02008702 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008703 if (!priv)
8704 return -ENOMEM;
8705
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008706 if (has_acpi_companion(&pdev->dev)) {
8707 acpi_id = acpi_match_device(pdev->dev.driver->acpi_match_table,
8708 &pdev->dev);
8709 priv->hw_version = (unsigned long)acpi_id->driver_data;
8710 } else {
8711 priv->hw_version =
8712 (unsigned long)of_device_get_match_data(&pdev->dev);
8713 }
Thomas Petazzonifaca9242017-03-07 16:53:06 +01008714
Marcin Wojtas3f518502014-07-10 16:52:13 -03008715 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thomas Petazzonia7868412017-03-07 16:53:13 +01008716 base = devm_ioremap_resource(&pdev->dev, res);
8717 if (IS_ERR(base))
8718 return PTR_ERR(base);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008719
Thomas Petazzonia7868412017-03-07 16:53:13 +01008720 if (priv->hw_version == MVPP21) {
8721 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
8722 priv->lms_base = devm_ioremap_resource(&pdev->dev, res);
8723 if (IS_ERR(priv->lms_base))
8724 return PTR_ERR(priv->lms_base);
8725 } else {
8726 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008727 if (has_acpi_companion(&pdev->dev)) {
8728 /* In case the MDIO memory region is declared in
8729 * the ACPI, it can already appear as 'in-use'
8730 * in the OS. Because it is overlapped by second
8731 * region of the network controller, make
8732 * sure it is released, before requesting it again.
8733 * The care is taken by mvpp2 driver to avoid
8734 * concurrent access to this memory region.
8735 */
8736 release_resource(res);
8737 }
Thomas Petazzonia7868412017-03-07 16:53:13 +01008738 priv->iface_base = devm_ioremap_resource(&pdev->dev, res);
8739 if (IS_ERR(priv->iface_base))
8740 return PTR_ERR(priv->iface_base);
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008741 }
Antoine Ténartf84bf382017-08-22 19:08:27 +02008742
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008743 if (priv->hw_version == MVPP22 && dev_of_node(&pdev->dev)) {
Antoine Ténartf84bf382017-08-22 19:08:27 +02008744 priv->sysctrl_base =
8745 syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
8746 "marvell,system-controller");
8747 if (IS_ERR(priv->sysctrl_base))
8748 /* The system controller regmap is optional for dt
8749 * compatibility reasons. When not provided, the
8750 * configuration of the GoP relies on the
8751 * firmware/bootloader.
8752 */
8753 priv->sysctrl_base = NULL;
Thomas Petazzonia7868412017-03-07 16:53:13 +01008754 }
8755
Stefan Chulski01d04932018-03-05 15:16:50 +01008756 mvpp2_setup_bm_pool();
8757
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02008758 for (i = 0; i < MVPP2_MAX_THREADS; i++) {
Thomas Petazzonia7868412017-03-07 16:53:13 +01008759 u32 addr_space_sz;
8760
8761 addr_space_sz = (priv->hw_version == MVPP21 ?
8762 MVPP21_ADDR_SPACE_SZ : MVPP22_ADDR_SPACE_SZ);
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02008763 priv->swth_base[i] = base + i * addr_space_sz;
Thomas Petazzonia7868412017-03-07 16:53:13 +01008764 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03008765
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01008766 if (priv->hw_version == MVPP21)
8767 priv->max_port_rxqs = 8;
8768 else
8769 priv->max_port_rxqs = 32;
8770
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008771 if (dev_of_node(&pdev->dev)) {
8772 priv->pp_clk = devm_clk_get(&pdev->dev, "pp_clk");
8773 if (IS_ERR(priv->pp_clk))
8774 return PTR_ERR(priv->pp_clk);
8775 err = clk_prepare_enable(priv->pp_clk);
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008776 if (err < 0)
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008777 return err;
8778
8779 priv->gop_clk = devm_clk_get(&pdev->dev, "gop_clk");
8780 if (IS_ERR(priv->gop_clk)) {
8781 err = PTR_ERR(priv->gop_clk);
8782 goto err_pp_clk;
8783 }
8784 err = clk_prepare_enable(priv->gop_clk);
8785 if (err < 0)
8786 goto err_pp_clk;
8787
8788 if (priv->hw_version == MVPP22) {
8789 priv->mg_clk = devm_clk_get(&pdev->dev, "mg_clk");
8790 if (IS_ERR(priv->mg_clk)) {
8791 err = PTR_ERR(priv->mg_clk);
8792 goto err_gop_clk;
8793 }
8794
8795 err = clk_prepare_enable(priv->mg_clk);
8796 if (err < 0)
8797 goto err_gop_clk;
8798 }
Gregory CLEMENT4792ea02017-09-29 14:27:39 +02008799
8800 priv->axi_clk = devm_clk_get(&pdev->dev, "axi_clk");
8801 if (IS_ERR(priv->axi_clk)) {
8802 err = PTR_ERR(priv->axi_clk);
8803 if (err == -EPROBE_DEFER)
8804 goto err_gop_clk;
8805 priv->axi_clk = NULL;
8806 } else {
8807 err = clk_prepare_enable(priv->axi_clk);
8808 if (err < 0)
8809 goto err_gop_clk;
8810 }
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008811
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008812 /* Get system's tclk rate */
8813 priv->tclk = clk_get_rate(priv->pp_clk);
8814 } else if (device_property_read_u32(&pdev->dev, "clock-frequency",
8815 &priv->tclk)) {
8816 dev_err(&pdev->dev, "missing clock-frequency value\n");
8817 return -EINVAL;
8818 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03008819
Thomas Petazzoni2067e0a2017-03-07 16:53:19 +01008820 if (priv->hw_version == MVPP22) {
8821 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(40));
8822 if (err)
8823 goto err_mg_clk;
8824 /* Sadly, the BM pools all share the same register to
8825 * store the high 32 bits of their address. So they
8826 * must all have the same high 32 bits, which forces
8827 * us to restrict coherent memory to DMA_BIT_MASK(32).
8828 */
8829 err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
8830 if (err)
8831 goto err_mg_clk;
8832 }
8833
Marcin Wojtas3f518502014-07-10 16:52:13 -03008834 /* Initialize network controller */
8835 err = mvpp2_init(pdev, priv);
8836 if (err < 0) {
8837 dev_err(&pdev->dev, "failed to initialize controller\n");
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008838 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008839 }
8840
Marcin Wojtasbf147152018-01-18 13:31:42 +01008841 /* Initialize ports */
Marcin Wojtas24812222018-01-18 13:31:43 +01008842 fwnode_for_each_available_child_node(fwnode, port_fwnode) {
8843 err = mvpp2_port_probe(pdev, port_fwnode, priv);
Marcin Wojtasbf147152018-01-18 13:31:42 +01008844 if (err < 0)
8845 goto err_port_probe;
8846 }
8847
Miquel Raynal118d6292017-11-06 22:56:53 +01008848 if (priv->port_count == 0) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03008849 dev_err(&pdev->dev, "no ports enabled\n");
Wei Yongjun575a1932014-07-20 22:02:43 +08008850 err = -ENODEV;
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008851 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008852 }
8853
Miquel Raynal118d6292017-11-06 22:56:53 +01008854 /* Statistics must be gathered regularly because some of them (like
8855 * packets counters) are 32-bit registers and could overflow quite
8856 * quickly. For instance, a 10Gb link used at full bandwidth with the
8857 * smallest packets (64B) will overflow a 32-bit counter in less than
8858 * 30 seconds. Then, use a workqueue to fill 64-bit counters.
8859 */
Miquel Raynal118d6292017-11-06 22:56:53 +01008860 snprintf(priv->queue_name, sizeof(priv->queue_name),
8861 "stats-wq-%s%s", netdev_name(priv->port_list[0]->dev),
8862 priv->port_count > 1 ? "+" : "");
8863 priv->stats_queue = create_singlethread_workqueue(priv->queue_name);
8864 if (!priv->stats_queue) {
8865 err = -ENOMEM;
Antoine Tenart26146b02017-11-28 14:19:49 +01008866 goto err_port_probe;
Miquel Raynal118d6292017-11-06 22:56:53 +01008867 }
8868
Marcin Wojtas3f518502014-07-10 16:52:13 -03008869 platform_set_drvdata(pdev, priv);
8870 return 0;
8871
Antoine Tenart26146b02017-11-28 14:19:49 +01008872err_port_probe:
8873 i = 0;
Marcin Wojtas24812222018-01-18 13:31:43 +01008874 fwnode_for_each_available_child_node(fwnode, port_fwnode) {
Antoine Tenart26146b02017-11-28 14:19:49 +01008875 if (priv->port_list[i])
8876 mvpp2_port_remove(priv->port_list[i]);
8877 i++;
8878 }
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008879err_mg_clk:
Gregory CLEMENT4792ea02017-09-29 14:27:39 +02008880 clk_disable_unprepare(priv->axi_clk);
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008881 if (priv->hw_version == MVPP22)
8882 clk_disable_unprepare(priv->mg_clk);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008883err_gop_clk:
8884 clk_disable_unprepare(priv->gop_clk);
8885err_pp_clk:
8886 clk_disable_unprepare(priv->pp_clk);
8887 return err;
8888}
8889
8890static int mvpp2_remove(struct platform_device *pdev)
8891{
8892 struct mvpp2 *priv = platform_get_drvdata(pdev);
Marcin Wojtas24812222018-01-18 13:31:43 +01008893 struct fwnode_handle *fwnode = pdev->dev.fwnode;
8894 struct fwnode_handle *port_fwnode;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008895 int i = 0;
8896
Miquel Raynale5c500e2017-11-08 08:59:40 +01008897 flush_workqueue(priv->stats_queue);
Miquel Raynal118d6292017-11-06 22:56:53 +01008898 destroy_workqueue(priv->stats_queue);
Miquel Raynal118d6292017-11-06 22:56:53 +01008899
Marcin Wojtas24812222018-01-18 13:31:43 +01008900 fwnode_for_each_available_child_node(fwnode, port_fwnode) {
Miquel Raynale5c500e2017-11-08 08:59:40 +01008901 if (priv->port_list[i]) {
8902 mutex_destroy(&priv->port_list[i]->gather_stats_lock);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008903 mvpp2_port_remove(priv->port_list[i]);
Miquel Raynale5c500e2017-11-08 08:59:40 +01008904 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03008905 i++;
8906 }
8907
8908 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
8909 struct mvpp2_bm_pool *bm_pool = &priv->bm_pools[i];
8910
8911 mvpp2_bm_pool_destroy(pdev, priv, bm_pool);
8912 }
8913
8914 for_each_present_cpu(i) {
8915 struct mvpp2_tx_queue *aggr_txq = &priv->aggr_txqs[i];
8916
8917 dma_free_coherent(&pdev->dev,
8918 MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
8919 aggr_txq->descs,
Thomas Petazzoni20396132017-03-07 16:53:00 +01008920 aggr_txq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008921 }
8922
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008923 if (is_acpi_node(port_fwnode))
8924 return 0;
8925
Gregory CLEMENT4792ea02017-09-29 14:27:39 +02008926 clk_disable_unprepare(priv->axi_clk);
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008927 clk_disable_unprepare(priv->mg_clk);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008928 clk_disable_unprepare(priv->pp_clk);
8929 clk_disable_unprepare(priv->gop_clk);
8930
8931 return 0;
8932}
8933
8934static const struct of_device_id mvpp2_match[] = {
Thomas Petazzonifaca9242017-03-07 16:53:06 +01008935 {
8936 .compatible = "marvell,armada-375-pp2",
8937 .data = (void *)MVPP21,
8938 },
Thomas Petazzonifc5e1552017-03-07 16:53:20 +01008939 {
8940 .compatible = "marvell,armada-7k-pp22",
8941 .data = (void *)MVPP22,
8942 },
Marcin Wojtas3f518502014-07-10 16:52:13 -03008943 { }
8944};
8945MODULE_DEVICE_TABLE(of, mvpp2_match);
8946
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008947static const struct acpi_device_id mvpp2_acpi_match[] = {
8948 { "MRVL0110", MVPP22 },
8949 { },
8950};
8951MODULE_DEVICE_TABLE(acpi, mvpp2_acpi_match);
8952
Marcin Wojtas3f518502014-07-10 16:52:13 -03008953static struct platform_driver mvpp2_driver = {
8954 .probe = mvpp2_probe,
8955 .remove = mvpp2_remove,
8956 .driver = {
8957 .name = MVPP2_DRIVER_NAME,
8958 .of_match_table = mvpp2_match,
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008959 .acpi_match_table = ACPI_PTR(mvpp2_acpi_match),
Marcin Wojtas3f518502014-07-10 16:52:13 -03008960 },
8961};
8962
8963module_platform_driver(mvpp2_driver);
8964
8965MODULE_DESCRIPTION("Marvell PPv2 Ethernet Driver - www.marvell.com");
8966MODULE_AUTHOR("Marcin Wojtas <mw@semihalf.com>");
Ezequiel Garciac6340992014-07-14 10:34:47 -03008967MODULE_LICENSE("GPL v2");