blob: c37d2be8496b2a1832ac09e986402fe5459bdeb6 [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
604
605/* Tcam structure:
606 * - lookup ID - 4 bits
607 * - port ID - 1 byte
608 * - additional information - 1 byte
609 * - header data - 8 bytes
610 * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(5)->(0).
611 */
612#define MVPP2_PRS_AI_BITS 8
613#define MVPP2_PRS_PORT_MASK 0xff
614#define MVPP2_PRS_LU_MASK 0xf
615#define MVPP2_PRS_TCAM_DATA_BYTE(offs) \
616 (((offs) - ((offs) % 2)) * 2 + ((offs) % 2))
617#define MVPP2_PRS_TCAM_DATA_BYTE_EN(offs) \
618 (((offs) * 2) - ((offs) % 2) + 2)
619#define MVPP2_PRS_TCAM_AI_BYTE 16
620#define MVPP2_PRS_TCAM_PORT_BYTE 17
621#define MVPP2_PRS_TCAM_LU_BYTE 20
622#define MVPP2_PRS_TCAM_EN_OFFS(offs) ((offs) + 2)
623#define MVPP2_PRS_TCAM_INV_WORD 5
Maxime Chevallier56beda32018-02-28 10:14:13 +0100624
625#define MVPP2_PRS_VID_TCAM_BYTE 2
626
627/* There is a TCAM range reserved for VLAN filtering entries, range size is 33
628 * 10 VLAN ID filter entries per port
629 * 1 default VLAN filter entry per port
630 * It is assumed that there are 3 ports for filter, not including loopback port
631 */
632#define MVPP2_PRS_VLAN_FILT_MAX 11
633#define MVPP2_PRS_VLAN_FILT_RANGE_SIZE 33
634
635#define MVPP2_PRS_VLAN_FILT_MAX_ENTRY (MVPP2_PRS_VLAN_FILT_MAX - 2)
636#define MVPP2_PRS_VLAN_FILT_DFLT_ENTRY (MVPP2_PRS_VLAN_FILT_MAX - 1)
637
Marcin Wojtas3f518502014-07-10 16:52:13 -0300638/* Tcam entries ID */
639#define MVPP2_PE_DROP_ALL 0
640#define MVPP2_PE_FIRST_FREE_TID 1
Maxime Chevallier56beda32018-02-28 10:14:13 +0100641
642/* VLAN filtering range */
643#define MVPP2_PE_VID_FILT_RANGE_END (MVPP2_PRS_TCAM_SRAM_SIZE - 31)
644#define MVPP2_PE_VID_FILT_RANGE_START (MVPP2_PE_VID_FILT_RANGE_END - \
645 MVPP2_PRS_VLAN_FILT_RANGE_SIZE + 1)
646#define MVPP2_PE_LAST_FREE_TID (MVPP2_PE_VID_FILT_RANGE_START - 1)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300647#define MVPP2_PE_IP6_EXT_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 30)
648#define MVPP2_PE_MAC_MC_IP6 (MVPP2_PRS_TCAM_SRAM_SIZE - 29)
649#define MVPP2_PE_IP6_ADDR_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 28)
650#define MVPP2_PE_IP4_ADDR_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 27)
651#define MVPP2_PE_LAST_DEFAULT_FLOW (MVPP2_PRS_TCAM_SRAM_SIZE - 26)
Maxime Chevallier56beda32018-02-28 10:14:13 +0100652#define MVPP2_PE_FIRST_DEFAULT_FLOW (MVPP2_PRS_TCAM_SRAM_SIZE - 21)
653#define MVPP2_PE_EDSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 20)
654#define MVPP2_PE_EDSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 19)
655#define MVPP2_PE_DSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 18)
656#define MVPP2_PE_DSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 17)
657#define MVPP2_PE_ETYPE_EDSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 16)
658#define MVPP2_PE_ETYPE_EDSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 15)
659#define MVPP2_PE_ETYPE_DSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 14)
660#define MVPP2_PE_ETYPE_DSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 13)
661#define MVPP2_PE_MH_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 12)
662#define MVPP2_PE_DSA_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 11)
663#define MVPP2_PE_IP6_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 10)
664#define MVPP2_PE_IP4_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 9)
665#define MVPP2_PE_ETH_TYPE_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 8)
666#define MVPP2_PE_VID_FLTR_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 7)
667#define MVPP2_PE_VID_EDSA_FLTR_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 6)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300668#define MVPP2_PE_VLAN_DBL (MVPP2_PRS_TCAM_SRAM_SIZE - 5)
669#define MVPP2_PE_VLAN_NONE (MVPP2_PRS_TCAM_SRAM_SIZE - 4)
670#define MVPP2_PE_MAC_MC_ALL (MVPP2_PRS_TCAM_SRAM_SIZE - 3)
671#define MVPP2_PE_MAC_PROMISCUOUS (MVPP2_PRS_TCAM_SRAM_SIZE - 2)
672#define MVPP2_PE_MAC_NON_PROMISCUOUS (MVPP2_PRS_TCAM_SRAM_SIZE - 1)
673
Maxime Chevallier56beda32018-02-28 10:14:13 +0100674#define MVPP2_PRS_VID_PORT_FIRST(port) (MVPP2_PE_VID_FILT_RANGE_START + \
675 ((port) * MVPP2_PRS_VLAN_FILT_MAX))
676#define MVPP2_PRS_VID_PORT_LAST(port) (MVPP2_PRS_VID_PORT_FIRST(port) \
677 + MVPP2_PRS_VLAN_FILT_MAX_ENTRY)
678/* Index of default vid filter for given port */
679#define MVPP2_PRS_VID_PORT_DFLT(port) (MVPP2_PRS_VID_PORT_FIRST(port) \
680 + MVPP2_PRS_VLAN_FILT_DFLT_ENTRY)
681
Marcin Wojtas3f518502014-07-10 16:52:13 -0300682/* Sram structure
683 * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(3)->(0).
684 */
685#define MVPP2_PRS_SRAM_RI_OFFS 0
686#define MVPP2_PRS_SRAM_RI_WORD 0
687#define MVPP2_PRS_SRAM_RI_CTRL_OFFS 32
688#define MVPP2_PRS_SRAM_RI_CTRL_WORD 1
689#define MVPP2_PRS_SRAM_RI_CTRL_BITS 32
690#define MVPP2_PRS_SRAM_SHIFT_OFFS 64
691#define MVPP2_PRS_SRAM_SHIFT_SIGN_BIT 72
692#define MVPP2_PRS_SRAM_UDF_OFFS 73
693#define MVPP2_PRS_SRAM_UDF_BITS 8
694#define MVPP2_PRS_SRAM_UDF_MASK 0xff
695#define MVPP2_PRS_SRAM_UDF_SIGN_BIT 81
696#define MVPP2_PRS_SRAM_UDF_TYPE_OFFS 82
697#define MVPP2_PRS_SRAM_UDF_TYPE_MASK 0x7
698#define MVPP2_PRS_SRAM_UDF_TYPE_L3 1
699#define MVPP2_PRS_SRAM_UDF_TYPE_L4 4
700#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS 85
701#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK 0x3
702#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD 1
703#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP4_ADD 2
704#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP6_ADD 3
705#define MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS 87
706#define MVPP2_PRS_SRAM_OP_SEL_UDF_BITS 2
707#define MVPP2_PRS_SRAM_OP_SEL_UDF_MASK 0x3
708#define MVPP2_PRS_SRAM_OP_SEL_UDF_ADD 0
709#define MVPP2_PRS_SRAM_OP_SEL_UDF_IP4_ADD 2
710#define MVPP2_PRS_SRAM_OP_SEL_UDF_IP6_ADD 3
711#define MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS 89
712#define MVPP2_PRS_SRAM_AI_OFFS 90
713#define MVPP2_PRS_SRAM_AI_CTRL_OFFS 98
714#define MVPP2_PRS_SRAM_AI_CTRL_BITS 8
715#define MVPP2_PRS_SRAM_AI_MASK 0xff
716#define MVPP2_PRS_SRAM_NEXT_LU_OFFS 106
717#define MVPP2_PRS_SRAM_NEXT_LU_MASK 0xf
718#define MVPP2_PRS_SRAM_LU_DONE_BIT 110
719#define MVPP2_PRS_SRAM_LU_GEN_BIT 111
720
721/* Sram result info bits assignment */
722#define MVPP2_PRS_RI_MAC_ME_MASK 0x1
723#define MVPP2_PRS_RI_DSA_MASK 0x2
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100724#define MVPP2_PRS_RI_VLAN_MASK (BIT(2) | BIT(3))
725#define MVPP2_PRS_RI_VLAN_NONE 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300726#define MVPP2_PRS_RI_VLAN_SINGLE BIT(2)
727#define MVPP2_PRS_RI_VLAN_DOUBLE BIT(3)
728#define MVPP2_PRS_RI_VLAN_TRIPLE (BIT(2) | BIT(3))
729#define MVPP2_PRS_RI_CPU_CODE_MASK 0x70
730#define MVPP2_PRS_RI_CPU_CODE_RX_SPEC BIT(4)
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100731#define MVPP2_PRS_RI_L2_CAST_MASK (BIT(9) | BIT(10))
732#define MVPP2_PRS_RI_L2_UCAST 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300733#define MVPP2_PRS_RI_L2_MCAST BIT(9)
734#define MVPP2_PRS_RI_L2_BCAST BIT(10)
735#define MVPP2_PRS_RI_PPPOE_MASK 0x800
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100736#define MVPP2_PRS_RI_L3_PROTO_MASK (BIT(12) | BIT(13) | BIT(14))
737#define MVPP2_PRS_RI_L3_UN 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300738#define MVPP2_PRS_RI_L3_IP4 BIT(12)
739#define MVPP2_PRS_RI_L3_IP4_OPT BIT(13)
740#define MVPP2_PRS_RI_L3_IP4_OTHER (BIT(12) | BIT(13))
741#define MVPP2_PRS_RI_L3_IP6 BIT(14)
742#define MVPP2_PRS_RI_L3_IP6_EXT (BIT(12) | BIT(14))
743#define MVPP2_PRS_RI_L3_ARP (BIT(13) | BIT(14))
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100744#define MVPP2_PRS_RI_L3_ADDR_MASK (BIT(15) | BIT(16))
745#define MVPP2_PRS_RI_L3_UCAST 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300746#define MVPP2_PRS_RI_L3_MCAST BIT(15)
747#define MVPP2_PRS_RI_L3_BCAST (BIT(15) | BIT(16))
748#define MVPP2_PRS_RI_IP_FRAG_MASK 0x20000
Stefan Chulskiaff3da32017-09-25 14:59:46 +0200749#define MVPP2_PRS_RI_IP_FRAG_TRUE BIT(17)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300750#define MVPP2_PRS_RI_UDF3_MASK 0x300000
751#define MVPP2_PRS_RI_UDF3_RX_SPECIAL BIT(21)
752#define MVPP2_PRS_RI_L4_PROTO_MASK 0x1c00000
753#define MVPP2_PRS_RI_L4_TCP BIT(22)
754#define MVPP2_PRS_RI_L4_UDP BIT(23)
755#define MVPP2_PRS_RI_L4_OTHER (BIT(22) | BIT(23))
756#define MVPP2_PRS_RI_UDF7_MASK 0x60000000
757#define MVPP2_PRS_RI_UDF7_IP6_LITE BIT(29)
758#define MVPP2_PRS_RI_DROP_MASK 0x80000000
759
760/* Sram additional info bits assignment */
761#define MVPP2_PRS_IPV4_DIP_AI_BIT BIT(0)
762#define MVPP2_PRS_IPV6_NO_EXT_AI_BIT BIT(0)
763#define MVPP2_PRS_IPV6_EXT_AI_BIT BIT(1)
764#define MVPP2_PRS_IPV6_EXT_AH_AI_BIT BIT(2)
765#define MVPP2_PRS_IPV6_EXT_AH_LEN_AI_BIT BIT(3)
766#define MVPP2_PRS_IPV6_EXT_AH_L4_AI_BIT BIT(4)
767#define MVPP2_PRS_SINGLE_VLAN_AI 0
768#define MVPP2_PRS_DBL_VLAN_AI_BIT BIT(7)
Maxime Chevallier56beda32018-02-28 10:14:13 +0100769#define MVPP2_PRS_EDSA_VID_AI_BIT BIT(0)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300770
771/* DSA/EDSA type */
772#define MVPP2_PRS_TAGGED true
773#define MVPP2_PRS_UNTAGGED false
774#define MVPP2_PRS_EDSA true
775#define MVPP2_PRS_DSA false
776
777/* MAC entries, shadow udf */
778enum mvpp2_prs_udf {
779 MVPP2_PRS_UDF_MAC_DEF,
780 MVPP2_PRS_UDF_MAC_RANGE,
781 MVPP2_PRS_UDF_L2_DEF,
782 MVPP2_PRS_UDF_L2_DEF_COPY,
783 MVPP2_PRS_UDF_L2_USER,
784};
785
786/* Lookup ID */
787enum mvpp2_prs_lookup {
788 MVPP2_PRS_LU_MH,
789 MVPP2_PRS_LU_MAC,
790 MVPP2_PRS_LU_DSA,
791 MVPP2_PRS_LU_VLAN,
Maxime Chevallier56beda32018-02-28 10:14:13 +0100792 MVPP2_PRS_LU_VID,
Marcin Wojtas3f518502014-07-10 16:52:13 -0300793 MVPP2_PRS_LU_L2,
794 MVPP2_PRS_LU_PPPOE,
795 MVPP2_PRS_LU_IP4,
796 MVPP2_PRS_LU_IP6,
797 MVPP2_PRS_LU_FLOWS,
798 MVPP2_PRS_LU_LAST,
799};
800
801/* L3 cast enum */
802enum mvpp2_prs_l3_cast {
803 MVPP2_PRS_L3_UNI_CAST,
804 MVPP2_PRS_L3_MULTI_CAST,
805 MVPP2_PRS_L3_BROAD_CAST
806};
807
808/* Classifier constants */
809#define MVPP2_CLS_FLOWS_TBL_SIZE 512
810#define MVPP2_CLS_FLOWS_TBL_DATA_WORDS 3
811#define MVPP2_CLS_LKP_TBL_SIZE 64
Antoine Tenart1d7d15d2017-10-30 11:23:30 +0100812#define MVPP2_CLS_RX_QUEUES 256
813
814/* RSS constants */
815#define MVPP22_RSS_TABLE_ENTRIES 32
Marcin Wojtas3f518502014-07-10 16:52:13 -0300816
817/* BM constants */
Marcin Wojtas3f518502014-07-10 16:52:13 -0300818#define MVPP2_BM_LONG_BUF_NUM 1024
819#define MVPP2_BM_SHORT_BUF_NUM 2048
820#define MVPP2_BM_POOL_SIZE_MAX (16*1024 - MVPP2_BM_POOL_PTR_ALIGN/4)
821#define MVPP2_BM_POOL_PTR_ALIGN 128
Marcin Wojtas3f518502014-07-10 16:52:13 -0300822
823/* BM cookie (32 bits) definition */
824#define MVPP2_BM_COOKIE_POOL_OFFS 8
825#define MVPP2_BM_COOKIE_CPU_OFFS 24
826
Stefan Chulski01d04932018-03-05 15:16:50 +0100827#define MVPP2_BM_SHORT_FRAME_SIZE 512
828#define MVPP2_BM_LONG_FRAME_SIZE 2048
Marcin Wojtas3f518502014-07-10 16:52:13 -0300829/* BM short pool packet size
830 * These value assure that for SWF the total number
831 * of bytes allocated for each buffer will be 512
832 */
Stefan Chulski01d04932018-03-05 15:16:50 +0100833#define MVPP2_BM_SHORT_PKT_SIZE MVPP2_RX_MAX_PKT_SIZE(MVPP2_BM_SHORT_FRAME_SIZE)
834#define MVPP2_BM_LONG_PKT_SIZE MVPP2_RX_MAX_PKT_SIZE(MVPP2_BM_LONG_FRAME_SIZE)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300835
Thomas Petazzonia7868412017-03-07 16:53:13 +0100836#define MVPP21_ADDR_SPACE_SZ 0
837#define MVPP22_ADDR_SPACE_SZ SZ_64K
838
Thomas Petazzonidf089aa2017-08-03 10:41:58 +0200839#define MVPP2_MAX_THREADS 8
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +0200840#define MVPP2_MAX_QVECS MVPP2_MAX_THREADS
Thomas Petazzonia7868412017-03-07 16:53:13 +0100841
Stefan Chulski01d04932018-03-05 15:16:50 +0100842enum mvpp2_bm_pool_log_num {
843 MVPP2_BM_SHORT,
844 MVPP2_BM_LONG,
845 MVPP2_BM_POOLS_NUM
Marcin Wojtas3f518502014-07-10 16:52:13 -0300846};
847
Stefan Chulski01d04932018-03-05 15:16:50 +0100848static struct {
849 int pkt_size;
850 int buf_num;
851} mvpp2_pools[MVPP2_BM_POOLS_NUM];
852
Miquel Raynal118d6292017-11-06 22:56:53 +0100853/* GMAC MIB Counters register definitions */
854#define MVPP21_MIB_COUNTERS_OFFSET 0x1000
855#define MVPP21_MIB_COUNTERS_PORT_SZ 0x400
856#define MVPP22_MIB_COUNTERS_OFFSET 0x0
857#define MVPP22_MIB_COUNTERS_PORT_SZ 0x100
858
859#define MVPP2_MIB_GOOD_OCTETS_RCVD 0x0
860#define MVPP2_MIB_BAD_OCTETS_RCVD 0x8
861#define MVPP2_MIB_CRC_ERRORS_SENT 0xc
862#define MVPP2_MIB_UNICAST_FRAMES_RCVD 0x10
863#define MVPP2_MIB_BROADCAST_FRAMES_RCVD 0x18
864#define MVPP2_MIB_MULTICAST_FRAMES_RCVD 0x1c
865#define MVPP2_MIB_FRAMES_64_OCTETS 0x20
866#define MVPP2_MIB_FRAMES_65_TO_127_OCTETS 0x24
867#define MVPP2_MIB_FRAMES_128_TO_255_OCTETS 0x28
868#define MVPP2_MIB_FRAMES_256_TO_511_OCTETS 0x2c
869#define MVPP2_MIB_FRAMES_512_TO_1023_OCTETS 0x30
870#define MVPP2_MIB_FRAMES_1024_TO_MAX_OCTETS 0x34
871#define MVPP2_MIB_GOOD_OCTETS_SENT 0x38
872#define MVPP2_MIB_UNICAST_FRAMES_SENT 0x40
873#define MVPP2_MIB_MULTICAST_FRAMES_SENT 0x48
874#define MVPP2_MIB_BROADCAST_FRAMES_SENT 0x4c
875#define MVPP2_MIB_FC_SENT 0x54
876#define MVPP2_MIB_FC_RCVD 0x58
877#define MVPP2_MIB_RX_FIFO_OVERRUN 0x5c
878#define MVPP2_MIB_UNDERSIZE_RCVD 0x60
879#define MVPP2_MIB_FRAGMENTS_RCVD 0x64
880#define MVPP2_MIB_OVERSIZE_RCVD 0x68
881#define MVPP2_MIB_JABBER_RCVD 0x6c
882#define MVPP2_MIB_MAC_RCV_ERROR 0x70
883#define MVPP2_MIB_BAD_CRC_EVENT 0x74
884#define MVPP2_MIB_COLLISION 0x78
885#define MVPP2_MIB_LATE_COLLISION 0x7c
886
887#define MVPP2_MIB_COUNTERS_STATS_DELAY (1 * HZ)
888
Marcin Wojtas3f518502014-07-10 16:52:13 -0300889/* Definitions */
890
891/* Shared Packet Processor resources */
892struct mvpp2 {
893 /* Shared registers' base addresses */
Marcin Wojtas3f518502014-07-10 16:52:13 -0300894 void __iomem *lms_base;
Thomas Petazzonia7868412017-03-07 16:53:13 +0100895 void __iomem *iface_base;
896
Thomas Petazzonidf089aa2017-08-03 10:41:58 +0200897 /* On PPv2.2, each "software thread" can access the base
898 * register through a separate address space, each 64 KB apart
899 * from each other. Typically, such address spaces will be
900 * used per CPU.
Thomas Petazzonia7868412017-03-07 16:53:13 +0100901 */
Thomas Petazzonidf089aa2017-08-03 10:41:58 +0200902 void __iomem *swth_base[MVPP2_MAX_THREADS];
Marcin Wojtas3f518502014-07-10 16:52:13 -0300903
Antoine Ténartf84bf382017-08-22 19:08:27 +0200904 /* On PPv2.2, some port control registers are located into the system
905 * controller space. These registers are accessible through a regmap.
906 */
907 struct regmap *sysctrl_base;
908
Marcin Wojtas3f518502014-07-10 16:52:13 -0300909 /* Common clocks */
910 struct clk *pp_clk;
911 struct clk *gop_clk;
Thomas Petazzonifceb55d2017-03-07 16:53:18 +0100912 struct clk *mg_clk;
Gregory CLEMENT4792ea02017-09-29 14:27:39 +0200913 struct clk *axi_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300914
915 /* List of pointers to port structures */
Miquel Raynal118d6292017-11-06 22:56:53 +0100916 int port_count;
Marcin Wojtasbf147152018-01-18 13:31:42 +0100917 struct mvpp2_port *port_list[MVPP2_MAX_PORTS];
Marcin Wojtas3f518502014-07-10 16:52:13 -0300918
919 /* Aggregated TXQs */
920 struct mvpp2_tx_queue *aggr_txqs;
921
922 /* BM pools */
923 struct mvpp2_bm_pool *bm_pools;
924
925 /* PRS shadow table */
926 struct mvpp2_prs_shadow *prs_shadow;
927 /* PRS auxiliary table for double vlan entries control */
928 bool *prs_double_vlans;
929
930 /* Tclk value */
931 u32 tclk;
Thomas Petazzonifaca9242017-03-07 16:53:06 +0100932
933 /* HW version */
934 enum { MVPP21, MVPP22 } hw_version;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +0100935
936 /* Maximum number of RXQs per port */
937 unsigned int max_port_rxqs;
Miquel Raynal118d6292017-11-06 22:56:53 +0100938
Miquel Raynale5c500e2017-11-08 08:59:40 +0100939 /* Workqueue to gather hardware statistics */
Miquel Raynal118d6292017-11-06 22:56:53 +0100940 char queue_name[30];
941 struct workqueue_struct *stats_queue;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300942};
943
944struct mvpp2_pcpu_stats {
945 struct u64_stats_sync syncp;
946 u64 rx_packets;
947 u64 rx_bytes;
948 u64 tx_packets;
949 u64 tx_bytes;
950};
951
Marcin Wojtasedc660f2015-08-06 19:00:30 +0200952/* Per-CPU port control */
953struct mvpp2_port_pcpu {
954 struct hrtimer tx_done_timer;
955 bool timer_scheduled;
956 /* Tasklet for egress finalization */
957 struct tasklet_struct tx_done_tasklet;
958};
959
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +0200960struct mvpp2_queue_vector {
961 int irq;
962 struct napi_struct napi;
963 enum { MVPP2_QUEUE_VECTOR_SHARED, MVPP2_QUEUE_VECTOR_PRIVATE } type;
964 int sw_thread_id;
965 u16 sw_thread_mask;
966 int first_rxq;
967 int nrxqs;
968 u32 pending_cause_rx;
969 struct mvpp2_port *port;
970};
971
Marcin Wojtas3f518502014-07-10 16:52:13 -0300972struct mvpp2_port {
973 u8 id;
974
Thomas Petazzonia7868412017-03-07 16:53:13 +0100975 /* Index of the port from the "group of ports" complex point
976 * of view
977 */
978 int gop_id;
979
Antoine Tenartfd3651b2017-09-01 11:04:54 +0200980 int link_irq;
981
Marcin Wojtas3f518502014-07-10 16:52:13 -0300982 struct mvpp2 *priv;
983
Marcin Wojtas24812222018-01-18 13:31:43 +0100984 /* Firmware node associated to the port */
985 struct fwnode_handle *fwnode;
986
Marcin Wojtas3f518502014-07-10 16:52:13 -0300987 /* Per-port registers' base address */
988 void __iomem *base;
Miquel Raynal118d6292017-11-06 22:56:53 +0100989 void __iomem *stats_base;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300990
991 struct mvpp2_rx_queue **rxqs;
Thomas Petazzoni09f83972017-08-03 10:41:57 +0200992 unsigned int nrxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300993 struct mvpp2_tx_queue **txqs;
Thomas Petazzoni09f83972017-08-03 10:41:57 +0200994 unsigned int ntxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300995 struct net_device *dev;
996
997 int pkt_size;
998
Marcin Wojtasedc660f2015-08-06 19:00:30 +0200999 /* Per-CPU port control */
1000 struct mvpp2_port_pcpu __percpu *pcpu;
1001
Marcin Wojtas3f518502014-07-10 16:52:13 -03001002 /* Flags */
1003 unsigned long flags;
1004
1005 u16 tx_ring_size;
1006 u16 rx_ring_size;
1007 struct mvpp2_pcpu_stats __percpu *stats;
Miquel Raynal118d6292017-11-06 22:56:53 +01001008 u64 *ethtool_stats;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001009
Miquel Raynale5c500e2017-11-08 08:59:40 +01001010 /* Per-port work and its lock to gather hardware statistics */
1011 struct mutex gather_stats_lock;
1012 struct delayed_work stats_work;
1013
Marcin Wojtas3f518502014-07-10 16:52:13 -03001014 phy_interface_t phy_interface;
1015 struct device_node *phy_node;
Antoine Tenart542897d2017-08-30 10:29:15 +02001016 struct phy *comphy;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001017 unsigned int link;
1018 unsigned int duplex;
1019 unsigned int speed;
1020
1021 struct mvpp2_bm_pool *pool_long;
1022 struct mvpp2_bm_pool *pool_short;
1023
1024 /* Index of first port's physical RXQ */
1025 u8 first_rxq;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02001026
1027 struct mvpp2_queue_vector qvecs[MVPP2_MAX_QVECS];
1028 unsigned int nqvecs;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02001029 bool has_tx_irqs;
1030
1031 u32 tx_time_coal;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001032};
1033
1034/* The mvpp2_tx_desc and mvpp2_rx_desc structures describe the
1035 * layout of the transmit and reception DMA descriptors, and their
1036 * layout is therefore defined by the hardware design
1037 */
1038
1039#define MVPP2_TXD_L3_OFF_SHIFT 0
1040#define MVPP2_TXD_IP_HLEN_SHIFT 8
1041#define MVPP2_TXD_L4_CSUM_FRAG BIT(13)
1042#define MVPP2_TXD_L4_CSUM_NOT BIT(14)
1043#define MVPP2_TXD_IP_CSUM_DISABLE BIT(15)
1044#define MVPP2_TXD_PADDING_DISABLE BIT(23)
1045#define MVPP2_TXD_L4_UDP BIT(24)
1046#define MVPP2_TXD_L3_IP6 BIT(26)
1047#define MVPP2_TXD_L_DESC BIT(28)
1048#define MVPP2_TXD_F_DESC BIT(29)
1049
1050#define MVPP2_RXD_ERR_SUMMARY BIT(15)
1051#define MVPP2_RXD_ERR_CODE_MASK (BIT(13) | BIT(14))
1052#define MVPP2_RXD_ERR_CRC 0x0
1053#define MVPP2_RXD_ERR_OVERRUN BIT(13)
1054#define MVPP2_RXD_ERR_RESOURCE (BIT(13) | BIT(14))
1055#define MVPP2_RXD_BM_POOL_ID_OFFS 16
1056#define MVPP2_RXD_BM_POOL_ID_MASK (BIT(16) | BIT(17) | BIT(18))
1057#define MVPP2_RXD_HWF_SYNC BIT(21)
1058#define MVPP2_RXD_L4_CSUM_OK BIT(22)
1059#define MVPP2_RXD_IP4_HEADER_ERR BIT(24)
1060#define MVPP2_RXD_L4_TCP BIT(25)
1061#define MVPP2_RXD_L4_UDP BIT(26)
1062#define MVPP2_RXD_L3_IP4 BIT(28)
1063#define MVPP2_RXD_L3_IP6 BIT(30)
1064#define MVPP2_RXD_BUF_HDR BIT(31)
1065
Thomas Petazzoni054f6372017-03-07 16:53:07 +01001066/* HW TX descriptor for PPv2.1 */
1067struct mvpp21_tx_desc {
Marcin Wojtas3f518502014-07-10 16:52:13 -03001068 u32 command; /* Options used by HW for packet transmitting.*/
1069 u8 packet_offset; /* the offset from the buffer beginning */
1070 u8 phys_txq; /* destination queue ID */
1071 u16 data_size; /* data size of transmitted packet in bytes */
Thomas Petazzoni20396132017-03-07 16:53:00 +01001072 u32 buf_dma_addr; /* physical addr of transmitted buffer */
Marcin Wojtas3f518502014-07-10 16:52:13 -03001073 u32 buf_cookie; /* cookie for access to TX buffer in tx path */
1074 u32 reserved1[3]; /* hw_cmd (for future use, BM, PON, PNC) */
1075 u32 reserved2; /* reserved (for future use) */
1076};
1077
Thomas Petazzoni054f6372017-03-07 16:53:07 +01001078/* HW RX descriptor for PPv2.1 */
1079struct mvpp21_rx_desc {
Marcin Wojtas3f518502014-07-10 16:52:13 -03001080 u32 status; /* info about received packet */
1081 u16 reserved1; /* parser_info (for future use, PnC) */
1082 u16 data_size; /* size of received packet in bytes */
Thomas Petazzoni20396132017-03-07 16:53:00 +01001083 u32 buf_dma_addr; /* physical address of the buffer */
Marcin Wojtas3f518502014-07-10 16:52:13 -03001084 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
1085 u16 reserved2; /* gem_port_id (for future use, PON) */
1086 u16 reserved3; /* csum_l4 (for future use, PnC) */
1087 u8 reserved4; /* bm_qset (for future use, BM) */
1088 u8 reserved5;
1089 u16 reserved6; /* classify_info (for future use, PnC) */
1090 u32 reserved7; /* flow_id (for future use, PnC) */
1091 u32 reserved8;
1092};
1093
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001094/* HW TX descriptor for PPv2.2 */
1095struct mvpp22_tx_desc {
1096 u32 command;
1097 u8 packet_offset;
1098 u8 phys_txq;
1099 u16 data_size;
1100 u64 reserved1;
1101 u64 buf_dma_addr_ptp;
1102 u64 buf_cookie_misc;
1103};
1104
1105/* HW RX descriptor for PPv2.2 */
1106struct mvpp22_rx_desc {
1107 u32 status;
1108 u16 reserved1;
1109 u16 data_size;
1110 u32 reserved2;
1111 u32 reserved3;
1112 u64 buf_dma_addr_key_hash;
1113 u64 buf_cookie_misc;
1114};
1115
Thomas Petazzoni054f6372017-03-07 16:53:07 +01001116/* Opaque type used by the driver to manipulate the HW TX and RX
1117 * descriptors
1118 */
1119struct mvpp2_tx_desc {
1120 union {
1121 struct mvpp21_tx_desc pp21;
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001122 struct mvpp22_tx_desc pp22;
Thomas Petazzoni054f6372017-03-07 16:53:07 +01001123 };
1124};
1125
1126struct mvpp2_rx_desc {
1127 union {
1128 struct mvpp21_rx_desc pp21;
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001129 struct mvpp22_rx_desc pp22;
Thomas Petazzoni054f6372017-03-07 16:53:07 +01001130 };
1131};
1132
Thomas Petazzoni83544912016-12-21 11:28:49 +01001133struct mvpp2_txq_pcpu_buf {
1134 /* Transmitted SKB */
1135 struct sk_buff *skb;
1136
1137 /* Physical address of transmitted buffer */
Thomas Petazzoni20396132017-03-07 16:53:00 +01001138 dma_addr_t dma;
Thomas Petazzoni83544912016-12-21 11:28:49 +01001139
1140 /* Size transmitted */
1141 size_t size;
1142};
1143
Marcin Wojtas3f518502014-07-10 16:52:13 -03001144/* Per-CPU Tx queue control */
1145struct mvpp2_txq_pcpu {
1146 int cpu;
1147
1148 /* Number of Tx DMA descriptors in the descriptor ring */
1149 int size;
1150
1151 /* Number of currently used Tx DMA descriptor in the
1152 * descriptor ring
1153 */
1154 int count;
1155
Antoine Tenart1d17db02017-10-30 11:23:31 +01001156 int wake_threshold;
1157 int stop_threshold;
1158
Marcin Wojtas3f518502014-07-10 16:52:13 -03001159 /* Number of Tx DMA descriptors reserved for each CPU */
1160 int reserved_num;
1161
Thomas Petazzoni83544912016-12-21 11:28:49 +01001162 /* Infos about transmitted buffers */
1163 struct mvpp2_txq_pcpu_buf *buffs;
Marcin Wojtas71ce3912015-08-06 19:00:29 +02001164
Marcin Wojtas3f518502014-07-10 16:52:13 -03001165 /* Index of last TX DMA descriptor that was inserted */
1166 int txq_put_index;
1167
1168 /* Index of the TX DMA descriptor to be cleaned up */
1169 int txq_get_index;
Antoine Ténart186cd4d2017-08-23 09:46:56 +02001170
1171 /* DMA buffer for TSO headers */
1172 char *tso_headers;
1173 dma_addr_t tso_headers_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001174};
1175
1176struct mvpp2_tx_queue {
1177 /* Physical number of this Tx queue */
1178 u8 id;
1179
1180 /* Logical number of this Tx queue */
1181 u8 log_id;
1182
1183 /* Number of Tx DMA descriptors in the descriptor ring */
1184 int size;
1185
1186 /* Number of currently used Tx DMA descriptor in the descriptor ring */
1187 int count;
1188
1189 /* Per-CPU control of physical Tx queues */
1190 struct mvpp2_txq_pcpu __percpu *pcpu;
1191
Marcin Wojtas3f518502014-07-10 16:52:13 -03001192 u32 done_pkts_coal;
1193
1194 /* Virtual address of thex Tx DMA descriptors array */
1195 struct mvpp2_tx_desc *descs;
1196
1197 /* DMA address of the Tx DMA descriptors array */
Thomas Petazzoni20396132017-03-07 16:53:00 +01001198 dma_addr_t descs_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001199
1200 /* Index of the last Tx DMA descriptor */
1201 int last_desc;
1202
1203 /* Index of the next Tx DMA descriptor to process */
1204 int next_desc_to_proc;
1205};
1206
1207struct mvpp2_rx_queue {
1208 /* RX queue number, in the range 0-31 for physical RXQs */
1209 u8 id;
1210
1211 /* Num of rx descriptors in the rx descriptor ring */
1212 int size;
1213
1214 u32 pkts_coal;
1215 u32 time_coal;
1216
1217 /* Virtual address of the RX DMA descriptors array */
1218 struct mvpp2_rx_desc *descs;
1219
1220 /* DMA address of the RX DMA descriptors array */
Thomas Petazzoni20396132017-03-07 16:53:00 +01001221 dma_addr_t descs_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001222
1223 /* Index of the last RX DMA descriptor */
1224 int last_desc;
1225
1226 /* Index of the next RX DMA descriptor to process */
1227 int next_desc_to_proc;
1228
1229 /* ID of port to which physical RXQ is mapped */
1230 int port;
1231
1232 /* Port's logic RXQ number to which physical RXQ is mapped */
1233 int logic_rxq;
1234};
1235
1236union mvpp2_prs_tcam_entry {
1237 u32 word[MVPP2_PRS_TCAM_WORDS];
1238 u8 byte[MVPP2_PRS_TCAM_WORDS * 4];
1239};
1240
1241union mvpp2_prs_sram_entry {
1242 u32 word[MVPP2_PRS_SRAM_WORDS];
1243 u8 byte[MVPP2_PRS_SRAM_WORDS * 4];
1244};
1245
1246struct mvpp2_prs_entry {
1247 u32 index;
1248 union mvpp2_prs_tcam_entry tcam;
1249 union mvpp2_prs_sram_entry sram;
1250};
1251
1252struct mvpp2_prs_shadow {
1253 bool valid;
1254 bool finish;
1255
1256 /* Lookup ID */
1257 int lu;
1258
1259 /* User defined offset */
1260 int udf;
1261
1262 /* Result info */
1263 u32 ri;
1264 u32 ri_mask;
1265};
1266
1267struct mvpp2_cls_flow_entry {
1268 u32 index;
1269 u32 data[MVPP2_CLS_FLOWS_TBL_DATA_WORDS];
1270};
1271
1272struct mvpp2_cls_lookup_entry {
1273 u32 lkpid;
1274 u32 way;
1275 u32 data;
1276};
1277
1278struct mvpp2_bm_pool {
1279 /* Pool number in the range 0-7 */
1280 int id;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001281
1282 /* Buffer Pointers Pool External (BPPE) size */
1283 int size;
Thomas Petazzonid01524d2017-03-07 16:53:09 +01001284 /* BPPE size in bytes */
1285 int size_bytes;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001286 /* Number of buffers for this pool */
1287 int buf_num;
1288 /* Pool buffer size */
1289 int buf_size;
1290 /* Packet size */
1291 int pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01001292 int frag_size;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001293
1294 /* BPPE virtual base address */
1295 u32 *virt_addr;
Thomas Petazzoni20396132017-03-07 16:53:00 +01001296 /* BPPE DMA base address */
1297 dma_addr_t dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001298
1299 /* Ports using BM pool */
1300 u32 port_map;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001301};
1302
Antoine Tenart20920262017-10-23 15:24:30 +02001303#define IS_TSO_HEADER(txq_pcpu, addr) \
1304 ((addr) >= (txq_pcpu)->tso_headers_dma && \
1305 (addr) < (txq_pcpu)->tso_headers_dma + \
1306 (txq_pcpu)->size * TSO_HEADER_SIZE)
1307
Thomas Petazzoni213f4282017-08-03 10:42:00 +02001308/* Queue modes */
1309#define MVPP2_QDIST_SINGLE_MODE 0
1310#define MVPP2_QDIST_MULTI_MODE 1
1311
1312static int queue_mode = MVPP2_QDIST_SINGLE_MODE;
1313
1314module_param(queue_mode, int, 0444);
1315MODULE_PARM_DESC(queue_mode, "Set queue_mode (single=0, multi=1)");
1316
Marcin Wojtas3f518502014-07-10 16:52:13 -03001317#define MVPP2_DRIVER_NAME "mvpp2"
1318#define MVPP2_DRIVER_VERSION "1.0"
1319
1320/* Utility/helper methods */
1321
1322static void mvpp2_write(struct mvpp2 *priv, u32 offset, u32 data)
1323{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001324 writel(data, priv->swth_base[0] + offset);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001325}
1326
1327static u32 mvpp2_read(struct mvpp2 *priv, u32 offset)
1328{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001329 return readl(priv->swth_base[0] + offset);
Thomas Petazzonia7868412017-03-07 16:53:13 +01001330}
1331
1332/* These accessors should be used to access:
1333 *
1334 * - per-CPU registers, where each CPU has its own copy of the
1335 * register.
1336 *
1337 * MVPP2_BM_VIRT_ALLOC_REG
1338 * MVPP2_BM_ADDR_HIGH_ALLOC
1339 * MVPP22_BM_ADDR_HIGH_RLS_REG
1340 * MVPP2_BM_VIRT_RLS_REG
1341 * MVPP2_ISR_RX_TX_CAUSE_REG
1342 * MVPP2_ISR_RX_TX_MASK_REG
1343 * MVPP2_TXQ_NUM_REG
1344 * MVPP2_AGGR_TXQ_UPDATE_REG
1345 * MVPP2_TXQ_RSVD_REQ_REG
1346 * MVPP2_TXQ_RSVD_RSLT_REG
1347 * MVPP2_TXQ_SENT_REG
1348 * MVPP2_RXQ_NUM_REG
1349 *
1350 * - global registers that must be accessed through a specific CPU
1351 * window, because they are related to an access to a per-CPU
1352 * register
1353 *
1354 * MVPP2_BM_PHY_ALLOC_REG (related to MVPP2_BM_VIRT_ALLOC_REG)
1355 * MVPP2_BM_PHY_RLS_REG (related to MVPP2_BM_VIRT_RLS_REG)
1356 * MVPP2_RXQ_THRESH_REG (related to MVPP2_RXQ_NUM_REG)
1357 * MVPP2_RXQ_DESC_ADDR_REG (related to MVPP2_RXQ_NUM_REG)
1358 * MVPP2_RXQ_DESC_SIZE_REG (related to MVPP2_RXQ_NUM_REG)
1359 * MVPP2_RXQ_INDEX_REG (related to MVPP2_RXQ_NUM_REG)
1360 * MVPP2_TXQ_PENDING_REG (related to MVPP2_TXQ_NUM_REG)
1361 * MVPP2_TXQ_DESC_ADDR_REG (related to MVPP2_TXQ_NUM_REG)
1362 * MVPP2_TXQ_DESC_SIZE_REG (related to MVPP2_TXQ_NUM_REG)
1363 * MVPP2_TXQ_INDEX_REG (related to MVPP2_TXQ_NUM_REG)
1364 * MVPP2_TXQ_PENDING_REG (related to MVPP2_TXQ_NUM_REG)
1365 * MVPP2_TXQ_PREF_BUF_REG (related to MVPP2_TXQ_NUM_REG)
1366 * MVPP2_TXQ_PREF_BUF_REG (related to MVPP2_TXQ_NUM_REG)
1367 */
1368static void mvpp2_percpu_write(struct mvpp2 *priv, int cpu,
1369 u32 offset, u32 data)
1370{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001371 writel(data, priv->swth_base[cpu] + offset);
Thomas Petazzonia7868412017-03-07 16:53:13 +01001372}
1373
1374static u32 mvpp2_percpu_read(struct mvpp2 *priv, int cpu,
1375 u32 offset)
1376{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001377 return readl(priv->swth_base[cpu] + offset);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001378}
1379
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001380static dma_addr_t mvpp2_txdesc_dma_addr_get(struct mvpp2_port *port,
1381 struct mvpp2_tx_desc *tx_desc)
1382{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001383 if (port->priv->hw_version == MVPP21)
1384 return tx_desc->pp21.buf_dma_addr;
1385 else
1386 return tx_desc->pp22.buf_dma_addr_ptp & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001387}
1388
1389static void mvpp2_txdesc_dma_addr_set(struct mvpp2_port *port,
1390 struct mvpp2_tx_desc *tx_desc,
1391 dma_addr_t dma_addr)
1392{
Antoine Tenart6eb5d372017-10-30 11:23:33 +01001393 dma_addr_t addr, offset;
1394
1395 addr = dma_addr & ~MVPP2_TX_DESC_ALIGN;
1396 offset = dma_addr & MVPP2_TX_DESC_ALIGN;
1397
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001398 if (port->priv->hw_version == MVPP21) {
Antoine Tenart6eb5d372017-10-30 11:23:33 +01001399 tx_desc->pp21.buf_dma_addr = addr;
1400 tx_desc->pp21.packet_offset = offset;
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001401 } else {
Antoine Tenart6eb5d372017-10-30 11:23:33 +01001402 u64 val = (u64)addr;
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001403
1404 tx_desc->pp22.buf_dma_addr_ptp &= ~GENMASK_ULL(40, 0);
1405 tx_desc->pp22.buf_dma_addr_ptp |= val;
Antoine Tenart6eb5d372017-10-30 11:23:33 +01001406 tx_desc->pp22.packet_offset = offset;
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001407 }
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001408}
1409
1410static size_t mvpp2_txdesc_size_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.data_size;
1415 else
1416 return tx_desc->pp22.data_size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001417}
1418
1419static void mvpp2_txdesc_size_set(struct mvpp2_port *port,
1420 struct mvpp2_tx_desc *tx_desc,
1421 size_t size)
1422{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001423 if (port->priv->hw_version == MVPP21)
1424 tx_desc->pp21.data_size = size;
1425 else
1426 tx_desc->pp22.data_size = size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001427}
1428
1429static void mvpp2_txdesc_txq_set(struct mvpp2_port *port,
1430 struct mvpp2_tx_desc *tx_desc,
1431 unsigned int txq)
1432{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001433 if (port->priv->hw_version == MVPP21)
1434 tx_desc->pp21.phys_txq = txq;
1435 else
1436 tx_desc->pp22.phys_txq = txq;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001437}
1438
1439static void mvpp2_txdesc_cmd_set(struct mvpp2_port *port,
1440 struct mvpp2_tx_desc *tx_desc,
1441 unsigned int command)
1442{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001443 if (port->priv->hw_version == MVPP21)
1444 tx_desc->pp21.command = command;
1445 else
1446 tx_desc->pp22.command = command;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001447}
1448
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001449static unsigned int mvpp2_txdesc_offset_get(struct mvpp2_port *port,
1450 struct mvpp2_tx_desc *tx_desc)
1451{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001452 if (port->priv->hw_version == MVPP21)
1453 return tx_desc->pp21.packet_offset;
1454 else
1455 return tx_desc->pp22.packet_offset;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001456}
1457
1458static dma_addr_t mvpp2_rxdesc_dma_addr_get(struct mvpp2_port *port,
1459 struct mvpp2_rx_desc *rx_desc)
1460{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001461 if (port->priv->hw_version == MVPP21)
1462 return rx_desc->pp21.buf_dma_addr;
1463 else
1464 return rx_desc->pp22.buf_dma_addr_key_hash & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001465}
1466
1467static unsigned long mvpp2_rxdesc_cookie_get(struct mvpp2_port *port,
1468 struct mvpp2_rx_desc *rx_desc)
1469{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001470 if (port->priv->hw_version == MVPP21)
1471 return rx_desc->pp21.buf_cookie;
1472 else
1473 return rx_desc->pp22.buf_cookie_misc & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001474}
1475
1476static size_t mvpp2_rxdesc_size_get(struct mvpp2_port *port,
1477 struct mvpp2_rx_desc *rx_desc)
1478{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001479 if (port->priv->hw_version == MVPP21)
1480 return rx_desc->pp21.data_size;
1481 else
1482 return rx_desc->pp22.data_size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001483}
1484
1485static u32 mvpp2_rxdesc_status_get(struct mvpp2_port *port,
1486 struct mvpp2_rx_desc *rx_desc)
1487{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001488 if (port->priv->hw_version == MVPP21)
1489 return rx_desc->pp21.status;
1490 else
1491 return rx_desc->pp22.status;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001492}
1493
Marcin Wojtas3f518502014-07-10 16:52:13 -03001494static void mvpp2_txq_inc_get(struct mvpp2_txq_pcpu *txq_pcpu)
1495{
1496 txq_pcpu->txq_get_index++;
1497 if (txq_pcpu->txq_get_index == txq_pcpu->size)
1498 txq_pcpu->txq_get_index = 0;
1499}
1500
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001501static void mvpp2_txq_inc_put(struct mvpp2_port *port,
1502 struct mvpp2_txq_pcpu *txq_pcpu,
Marcin Wojtas71ce3912015-08-06 19:00:29 +02001503 struct sk_buff *skb,
1504 struct mvpp2_tx_desc *tx_desc)
Marcin Wojtas3f518502014-07-10 16:52:13 -03001505{
Thomas Petazzoni83544912016-12-21 11:28:49 +01001506 struct mvpp2_txq_pcpu_buf *tx_buf =
1507 txq_pcpu->buffs + txq_pcpu->txq_put_index;
1508 tx_buf->skb = skb;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001509 tx_buf->size = mvpp2_txdesc_size_get(port, tx_desc);
1510 tx_buf->dma = mvpp2_txdesc_dma_addr_get(port, tx_desc) +
1511 mvpp2_txdesc_offset_get(port, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001512 txq_pcpu->txq_put_index++;
1513 if (txq_pcpu->txq_put_index == txq_pcpu->size)
1514 txq_pcpu->txq_put_index = 0;
1515}
1516
1517/* Get number of physical egress port */
1518static inline int mvpp2_egress_port(struct mvpp2_port *port)
1519{
1520 return MVPP2_MAX_TCONT + port->id;
1521}
1522
1523/* Get number of physical TXQ */
1524static inline int mvpp2_txq_phys(int port, int txq)
1525{
1526 return (MVPP2_MAX_TCONT + port) * MVPP2_MAX_TXQ + txq;
1527}
1528
1529/* Parser configuration routines */
1530
1531/* Update parser tcam and sram hw entries */
1532static int mvpp2_prs_hw_write(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1533{
1534 int i;
1535
1536 if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1537 return -EINVAL;
1538
1539 /* Clear entry invalidation bit */
1540 pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] &= ~MVPP2_PRS_TCAM_INV_MASK;
1541
1542 /* Write tcam index - indirect access */
1543 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1544 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1545 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), pe->tcam.word[i]);
1546
1547 /* Write sram index - indirect access */
1548 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1549 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1550 mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), pe->sram.word[i]);
1551
1552 return 0;
1553}
1554
1555/* Read tcam entry from hw */
1556static int mvpp2_prs_hw_read(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1557{
1558 int i;
1559
1560 if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1561 return -EINVAL;
1562
1563 /* Write tcam index - indirect access */
1564 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1565
1566 pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] = mvpp2_read(priv,
1567 MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD));
1568 if (pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] & MVPP2_PRS_TCAM_INV_MASK)
1569 return MVPP2_PRS_TCAM_ENTRY_INVALID;
1570
1571 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1572 pe->tcam.word[i] = mvpp2_read(priv, MVPP2_PRS_TCAM_DATA_REG(i));
1573
1574 /* Write sram index - indirect access */
1575 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1576 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1577 pe->sram.word[i] = mvpp2_read(priv, MVPP2_PRS_SRAM_DATA_REG(i));
1578
1579 return 0;
1580}
1581
1582/* Invalidate tcam hw entry */
1583static void mvpp2_prs_hw_inv(struct mvpp2 *priv, int index)
1584{
1585 /* Write index - indirect access */
1586 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
1587 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD),
1588 MVPP2_PRS_TCAM_INV_MASK);
1589}
1590
1591/* Enable shadow table entry and set its lookup ID */
1592static void mvpp2_prs_shadow_set(struct mvpp2 *priv, int index, int lu)
1593{
1594 priv->prs_shadow[index].valid = true;
1595 priv->prs_shadow[index].lu = lu;
1596}
1597
1598/* Update ri fields in shadow table entry */
1599static void mvpp2_prs_shadow_ri_set(struct mvpp2 *priv, int index,
1600 unsigned int ri, unsigned int ri_mask)
1601{
1602 priv->prs_shadow[index].ri_mask = ri_mask;
1603 priv->prs_shadow[index].ri = ri;
1604}
1605
1606/* Update lookup field in tcam sw entry */
1607static void mvpp2_prs_tcam_lu_set(struct mvpp2_prs_entry *pe, unsigned int lu)
1608{
1609 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_LU_BYTE);
1610
1611 pe->tcam.byte[MVPP2_PRS_TCAM_LU_BYTE] = lu;
1612 pe->tcam.byte[enable_off] = MVPP2_PRS_LU_MASK;
1613}
1614
1615/* Update mask for single port in tcam sw entry */
1616static void mvpp2_prs_tcam_port_set(struct mvpp2_prs_entry *pe,
1617 unsigned int port, bool add)
1618{
1619 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1620
1621 if (add)
1622 pe->tcam.byte[enable_off] &= ~(1 << port);
1623 else
1624 pe->tcam.byte[enable_off] |= 1 << port;
1625}
1626
1627/* Update port map in tcam sw entry */
1628static void mvpp2_prs_tcam_port_map_set(struct mvpp2_prs_entry *pe,
1629 unsigned int ports)
1630{
1631 unsigned char port_mask = MVPP2_PRS_PORT_MASK;
1632 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1633
1634 pe->tcam.byte[MVPP2_PRS_TCAM_PORT_BYTE] = 0;
1635 pe->tcam.byte[enable_off] &= ~port_mask;
1636 pe->tcam.byte[enable_off] |= ~ports & MVPP2_PRS_PORT_MASK;
1637}
1638
1639/* Obtain port map from tcam sw entry */
1640static unsigned int mvpp2_prs_tcam_port_map_get(struct mvpp2_prs_entry *pe)
1641{
1642 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1643
1644 return ~(pe->tcam.byte[enable_off]) & MVPP2_PRS_PORT_MASK;
1645}
1646
1647/* Set byte of data and its enable bits in tcam sw entry */
1648static void mvpp2_prs_tcam_data_byte_set(struct mvpp2_prs_entry *pe,
1649 unsigned int offs, unsigned char byte,
1650 unsigned char enable)
1651{
1652 pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)] = byte;
1653 pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)] = enable;
1654}
1655
1656/* Get byte of data and its enable bits from tcam sw entry */
1657static void mvpp2_prs_tcam_data_byte_get(struct mvpp2_prs_entry *pe,
1658 unsigned int offs, unsigned char *byte,
1659 unsigned char *enable)
1660{
1661 *byte = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)];
1662 *enable = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)];
1663}
1664
1665/* Compare tcam data bytes with a pattern */
1666static bool mvpp2_prs_tcam_data_cmp(struct mvpp2_prs_entry *pe, int offs,
1667 u16 data)
1668{
1669 int off = MVPP2_PRS_TCAM_DATA_BYTE(offs);
1670 u16 tcam_data;
1671
Antoine Tenartef4816f2017-10-24 11:41:26 +02001672 tcam_data = (pe->tcam.byte[off + 1] << 8) | pe->tcam.byte[off];
Marcin Wojtas3f518502014-07-10 16:52:13 -03001673 if (tcam_data != data)
1674 return false;
1675 return true;
1676}
1677
1678/* Update ai bits in tcam sw entry */
1679static void mvpp2_prs_tcam_ai_update(struct mvpp2_prs_entry *pe,
1680 unsigned int bits, unsigned int enable)
1681{
1682 int i, ai_idx = MVPP2_PRS_TCAM_AI_BYTE;
1683
1684 for (i = 0; i < MVPP2_PRS_AI_BITS; i++) {
1685
1686 if (!(enable & BIT(i)))
1687 continue;
1688
1689 if (bits & BIT(i))
1690 pe->tcam.byte[ai_idx] |= 1 << i;
1691 else
1692 pe->tcam.byte[ai_idx] &= ~(1 << i);
1693 }
1694
1695 pe->tcam.byte[MVPP2_PRS_TCAM_EN_OFFS(ai_idx)] |= enable;
1696}
1697
1698/* Get ai bits from tcam sw entry */
1699static int mvpp2_prs_tcam_ai_get(struct mvpp2_prs_entry *pe)
1700{
1701 return pe->tcam.byte[MVPP2_PRS_TCAM_AI_BYTE];
1702}
1703
1704/* Set ethertype in tcam sw entry */
1705static void mvpp2_prs_match_etype(struct mvpp2_prs_entry *pe, int offset,
1706 unsigned short ethertype)
1707{
1708 mvpp2_prs_tcam_data_byte_set(pe, offset + 0, ethertype >> 8, 0xff);
1709 mvpp2_prs_tcam_data_byte_set(pe, offset + 1, ethertype & 0xff, 0xff);
1710}
1711
Maxime Chevallier56beda32018-02-28 10:14:13 +01001712/* Set vid in tcam sw entry */
1713static void mvpp2_prs_match_vid(struct mvpp2_prs_entry *pe, int offset,
1714 unsigned short vid)
1715{
1716 mvpp2_prs_tcam_data_byte_set(pe, offset + 0, (vid & 0xf00) >> 8, 0xf);
1717 mvpp2_prs_tcam_data_byte_set(pe, offset + 1, vid & 0xff, 0xff);
1718}
1719
Marcin Wojtas3f518502014-07-10 16:52:13 -03001720/* Set bits in sram sw entry */
1721static void mvpp2_prs_sram_bits_set(struct mvpp2_prs_entry *pe, int bit_num,
1722 int val)
1723{
1724 pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] |= (val << (bit_num % 8));
1725}
1726
1727/* Clear bits in sram sw entry */
1728static void mvpp2_prs_sram_bits_clear(struct mvpp2_prs_entry *pe, int bit_num,
1729 int val)
1730{
1731 pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] &= ~(val << (bit_num % 8));
1732}
1733
1734/* Update ri bits in sram sw entry */
1735static void mvpp2_prs_sram_ri_update(struct mvpp2_prs_entry *pe,
1736 unsigned int bits, unsigned int mask)
1737{
1738 unsigned int i;
1739
1740 for (i = 0; i < MVPP2_PRS_SRAM_RI_CTRL_BITS; i++) {
1741 int ri_off = MVPP2_PRS_SRAM_RI_OFFS;
1742
1743 if (!(mask & BIT(i)))
1744 continue;
1745
1746 if (bits & BIT(i))
1747 mvpp2_prs_sram_bits_set(pe, ri_off + i, 1);
1748 else
1749 mvpp2_prs_sram_bits_clear(pe, ri_off + i, 1);
1750
1751 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_RI_CTRL_OFFS + i, 1);
1752 }
1753}
1754
1755/* Obtain ri bits from sram sw entry */
1756static int mvpp2_prs_sram_ri_get(struct mvpp2_prs_entry *pe)
1757{
1758 return pe->sram.word[MVPP2_PRS_SRAM_RI_WORD];
1759}
1760
1761/* Update ai bits in sram sw entry */
1762static void mvpp2_prs_sram_ai_update(struct mvpp2_prs_entry *pe,
1763 unsigned int bits, unsigned int mask)
1764{
1765 unsigned int i;
1766 int ai_off = MVPP2_PRS_SRAM_AI_OFFS;
1767
1768 for (i = 0; i < MVPP2_PRS_SRAM_AI_CTRL_BITS; i++) {
1769
1770 if (!(mask & BIT(i)))
1771 continue;
1772
1773 if (bits & BIT(i))
1774 mvpp2_prs_sram_bits_set(pe, ai_off + i, 1);
1775 else
1776 mvpp2_prs_sram_bits_clear(pe, ai_off + i, 1);
1777
1778 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_AI_CTRL_OFFS + i, 1);
1779 }
1780}
1781
1782/* Read ai bits from sram sw entry */
1783static int mvpp2_prs_sram_ai_get(struct mvpp2_prs_entry *pe)
1784{
1785 u8 bits;
1786 int ai_off = MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_AI_OFFS);
1787 int ai_en_off = ai_off + 1;
1788 int ai_shift = MVPP2_PRS_SRAM_AI_OFFS % 8;
1789
1790 bits = (pe->sram.byte[ai_off] >> ai_shift) |
1791 (pe->sram.byte[ai_en_off] << (8 - ai_shift));
1792
1793 return bits;
1794}
1795
1796/* In sram sw entry set lookup ID field of the tcam key to be used in the next
1797 * lookup interation
1798 */
1799static void mvpp2_prs_sram_next_lu_set(struct mvpp2_prs_entry *pe,
1800 unsigned int lu)
1801{
1802 int sram_next_off = MVPP2_PRS_SRAM_NEXT_LU_OFFS;
1803
1804 mvpp2_prs_sram_bits_clear(pe, sram_next_off,
1805 MVPP2_PRS_SRAM_NEXT_LU_MASK);
1806 mvpp2_prs_sram_bits_set(pe, sram_next_off, lu);
1807}
1808
1809/* In the sram sw entry set sign and value of the next lookup offset
1810 * and the offset value generated to the classifier
1811 */
1812static void mvpp2_prs_sram_shift_set(struct mvpp2_prs_entry *pe, int shift,
1813 unsigned int op)
1814{
1815 /* Set sign */
1816 if (shift < 0) {
1817 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1818 shift = 0 - shift;
1819 } else {
1820 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1821 }
1822
1823 /* Set value */
1824 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_SHIFT_OFFS)] =
1825 (unsigned char)shift;
1826
1827 /* Reset and set operation */
1828 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS,
1829 MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK);
1830 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS, op);
1831
1832 /* Set base offset as current */
1833 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1834}
1835
1836/* In the sram sw entry set sign and value of the user defined offset
1837 * generated to the classifier
1838 */
1839static void mvpp2_prs_sram_offset_set(struct mvpp2_prs_entry *pe,
1840 unsigned int type, int offset,
1841 unsigned int op)
1842{
1843 /* Set sign */
1844 if (offset < 0) {
1845 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1846 offset = 0 - offset;
1847 } else {
1848 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1849 }
1850
1851 /* Set value */
1852 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_OFFS,
1853 MVPP2_PRS_SRAM_UDF_MASK);
1854 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_OFFS, offset);
1855 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1856 MVPP2_PRS_SRAM_UDF_BITS)] &=
1857 ~(MVPP2_PRS_SRAM_UDF_MASK >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1858 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1859 MVPP2_PRS_SRAM_UDF_BITS)] |=
1860 (offset >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1861
1862 /* Set offset type */
1863 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS,
1864 MVPP2_PRS_SRAM_UDF_TYPE_MASK);
1865 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS, type);
1866
1867 /* Set offset operation */
1868 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS,
1869 MVPP2_PRS_SRAM_OP_SEL_UDF_MASK);
1870 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS, op);
1871
1872 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1873 MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] &=
1874 ~(MVPP2_PRS_SRAM_OP_SEL_UDF_MASK >>
1875 (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1876
1877 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1878 MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] |=
1879 (op >> (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1880
1881 /* Set base offset as current */
1882 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1883}
1884
1885/* Find parser flow entry */
1886static struct mvpp2_prs_entry *mvpp2_prs_flow_find(struct mvpp2 *priv, int flow)
1887{
1888 struct mvpp2_prs_entry *pe;
1889 int tid;
1890
1891 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
1892 if (!pe)
1893 return NULL;
1894 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
1895
1896 /* Go through the all entires with MVPP2_PRS_LU_FLOWS */
1897 for (tid = MVPP2_PRS_TCAM_SRAM_SIZE - 1; tid >= 0; tid--) {
1898 u8 bits;
1899
1900 if (!priv->prs_shadow[tid].valid ||
1901 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_FLOWS)
1902 continue;
1903
1904 pe->index = tid;
1905 mvpp2_prs_hw_read(priv, pe);
1906 bits = mvpp2_prs_sram_ai_get(pe);
1907
1908 /* Sram store classification lookup ID in AI bits [5:0] */
1909 if ((bits & MVPP2_PRS_FLOW_ID_MASK) == flow)
1910 return pe;
1911 }
1912 kfree(pe);
1913
1914 return NULL;
1915}
1916
1917/* Return first free tcam index, seeking from start to end */
1918static int mvpp2_prs_tcam_first_free(struct mvpp2 *priv, unsigned char start,
1919 unsigned char end)
1920{
1921 int tid;
1922
1923 if (start > end)
1924 swap(start, end);
1925
1926 if (end >= MVPP2_PRS_TCAM_SRAM_SIZE)
1927 end = MVPP2_PRS_TCAM_SRAM_SIZE - 1;
1928
1929 for (tid = start; tid <= end; tid++) {
1930 if (!priv->prs_shadow[tid].valid)
1931 return tid;
1932 }
1933
1934 return -EINVAL;
1935}
1936
1937/* Enable/disable dropping all mac da's */
1938static void mvpp2_prs_mac_drop_all_set(struct mvpp2 *priv, int port, bool add)
1939{
1940 struct mvpp2_prs_entry pe;
1941
1942 if (priv->prs_shadow[MVPP2_PE_DROP_ALL].valid) {
1943 /* Entry exist - update port only */
1944 pe.index = MVPP2_PE_DROP_ALL;
1945 mvpp2_prs_hw_read(priv, &pe);
1946 } else {
1947 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001948 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001949 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1950 pe.index = MVPP2_PE_DROP_ALL;
1951
1952 /* Non-promiscuous mode for all ports - DROP unknown packets */
1953 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
1954 MVPP2_PRS_RI_DROP_MASK);
1955
1956 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1957 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1958
1959 /* Update shadow table */
1960 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1961
1962 /* Mask all ports */
1963 mvpp2_prs_tcam_port_map_set(&pe, 0);
1964 }
1965
1966 /* Update port mask */
1967 mvpp2_prs_tcam_port_set(&pe, port, add);
1968
1969 mvpp2_prs_hw_write(priv, &pe);
1970}
1971
1972/* Set port to promiscuous mode */
1973static void mvpp2_prs_mac_promisc_set(struct mvpp2 *priv, int port, bool add)
1974{
1975 struct mvpp2_prs_entry pe;
1976
Joe Perchesdbedd442015-03-06 20:49:12 -08001977 /* Promiscuous mode - Accept unknown packets */
Marcin Wojtas3f518502014-07-10 16:52:13 -03001978
1979 if (priv->prs_shadow[MVPP2_PE_MAC_PROMISCUOUS].valid) {
1980 /* Entry exist - update port only */
1981 pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1982 mvpp2_prs_hw_read(priv, &pe);
1983 } else {
1984 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001985 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001986 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1987 pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1988
1989 /* Continue - set next lookup */
1990 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
1991
1992 /* Set result info bits */
1993 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_UCAST,
1994 MVPP2_PRS_RI_L2_CAST_MASK);
1995
1996 /* Shift to ethertype */
1997 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
1998 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1999
2000 /* Mask all ports */
2001 mvpp2_prs_tcam_port_map_set(&pe, 0);
2002
2003 /* Update shadow table */
2004 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2005 }
2006
2007 /* Update port mask */
2008 mvpp2_prs_tcam_port_set(&pe, port, add);
2009
2010 mvpp2_prs_hw_write(priv, &pe);
2011}
2012
2013/* Accept multicast */
2014static void mvpp2_prs_mac_multi_set(struct mvpp2 *priv, int port, int index,
2015 bool add)
2016{
2017 struct mvpp2_prs_entry pe;
2018 unsigned char da_mc;
2019
2020 /* Ethernet multicast address first byte is
2021 * 0x01 for IPv4 and 0x33 for IPv6
2022 */
2023 da_mc = (index == MVPP2_PE_MAC_MC_ALL) ? 0x01 : 0x33;
2024
2025 if (priv->prs_shadow[index].valid) {
2026 /* Entry exist - update port only */
2027 pe.index = index;
2028 mvpp2_prs_hw_read(priv, &pe);
2029 } else {
2030 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002031 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002032 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
2033 pe.index = index;
2034
2035 /* Continue - set next lookup */
2036 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
2037
2038 /* Set result info bits */
2039 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_MCAST,
2040 MVPP2_PRS_RI_L2_CAST_MASK);
2041
2042 /* Update tcam entry data first byte */
2043 mvpp2_prs_tcam_data_byte_set(&pe, 0, da_mc, 0xff);
2044
2045 /* Shift to ethertype */
2046 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
2047 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2048
2049 /* Mask all ports */
2050 mvpp2_prs_tcam_port_map_set(&pe, 0);
2051
2052 /* Update shadow table */
2053 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2054 }
2055
2056 /* Update port mask */
2057 mvpp2_prs_tcam_port_set(&pe, port, add);
2058
2059 mvpp2_prs_hw_write(priv, &pe);
2060}
2061
2062/* Set entry for dsa packets */
2063static void mvpp2_prs_dsa_tag_set(struct mvpp2 *priv, int port, bool add,
2064 bool tagged, bool extend)
2065{
2066 struct mvpp2_prs_entry pe;
2067 int tid, shift;
2068
2069 if (extend) {
2070 tid = tagged ? MVPP2_PE_EDSA_TAGGED : MVPP2_PE_EDSA_UNTAGGED;
2071 shift = 8;
2072 } else {
2073 tid = tagged ? MVPP2_PE_DSA_TAGGED : MVPP2_PE_DSA_UNTAGGED;
2074 shift = 4;
2075 }
2076
2077 if (priv->prs_shadow[tid].valid) {
2078 /* Entry exist - update port only */
2079 pe.index = tid;
2080 mvpp2_prs_hw_read(priv, &pe);
2081 } else {
2082 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002083 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002084 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
2085 pe.index = tid;
2086
Marcin Wojtas3f518502014-07-10 16:52:13 -03002087 /* Update shadow table */
2088 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
2089
2090 if (tagged) {
2091 /* Set tagged bit in DSA tag */
2092 mvpp2_prs_tcam_data_byte_set(&pe, 0,
Maxime Chevallier56beda32018-02-28 10:14:13 +01002093 MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
2094 MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
2095
2096 /* Set ai bits for next iteration */
2097 if (extend)
2098 mvpp2_prs_sram_ai_update(&pe, 1,
2099 MVPP2_PRS_SRAM_AI_MASK);
2100 else
2101 mvpp2_prs_sram_ai_update(&pe, 0,
2102 MVPP2_PRS_SRAM_AI_MASK);
2103
2104 /* If packet is tagged continue check vid filtering */
2105 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VID);
Marcin Wojtas3f518502014-07-10 16:52:13 -03002106 } else {
Maxime Chevallier56beda32018-02-28 10:14:13 +01002107 /* Shift 4 bytes for DSA tag or 8 bytes for EDSA tag*/
2108 mvpp2_prs_sram_shift_set(&pe, shift,
2109 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2110
Marcin Wojtas3f518502014-07-10 16:52:13 -03002111 /* Set result info bits to 'no vlans' */
2112 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
2113 MVPP2_PRS_RI_VLAN_MASK);
2114 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2115 }
2116
2117 /* Mask all ports */
2118 mvpp2_prs_tcam_port_map_set(&pe, 0);
2119 }
2120
2121 /* Update port mask */
2122 mvpp2_prs_tcam_port_set(&pe, port, add);
2123
2124 mvpp2_prs_hw_write(priv, &pe);
2125}
2126
2127/* Set entry for dsa ethertype */
2128static void mvpp2_prs_dsa_tag_ethertype_set(struct mvpp2 *priv, int port,
2129 bool add, bool tagged, bool extend)
2130{
2131 struct mvpp2_prs_entry pe;
2132 int tid, shift, port_mask;
2133
2134 if (extend) {
2135 tid = tagged ? MVPP2_PE_ETYPE_EDSA_TAGGED :
2136 MVPP2_PE_ETYPE_EDSA_UNTAGGED;
2137 port_mask = 0;
2138 shift = 8;
2139 } else {
2140 tid = tagged ? MVPP2_PE_ETYPE_DSA_TAGGED :
2141 MVPP2_PE_ETYPE_DSA_UNTAGGED;
2142 port_mask = MVPP2_PRS_PORT_MASK;
2143 shift = 4;
2144 }
2145
2146 if (priv->prs_shadow[tid].valid) {
2147 /* Entry exist - update port only */
2148 pe.index = tid;
2149 mvpp2_prs_hw_read(priv, &pe);
2150 } else {
2151 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002152 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002153 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
2154 pe.index = tid;
2155
2156 /* Set ethertype */
2157 mvpp2_prs_match_etype(&pe, 0, ETH_P_EDSA);
2158 mvpp2_prs_match_etype(&pe, 2, 0);
2159
2160 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DSA_MASK,
2161 MVPP2_PRS_RI_DSA_MASK);
2162 /* Shift ethertype + 2 byte reserved + tag*/
2163 mvpp2_prs_sram_shift_set(&pe, 2 + MVPP2_ETH_TYPE_LEN + shift,
2164 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2165
2166 /* Update shadow table */
2167 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
2168
2169 if (tagged) {
2170 /* Set tagged bit in DSA tag */
2171 mvpp2_prs_tcam_data_byte_set(&pe,
2172 MVPP2_ETH_TYPE_LEN + 2 + 3,
2173 MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
2174 MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
2175 /* Clear all ai bits for next iteration */
2176 mvpp2_prs_sram_ai_update(&pe, 0,
2177 MVPP2_PRS_SRAM_AI_MASK);
2178 /* If packet is tagged continue check vlans */
2179 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2180 } else {
2181 /* Set result info bits to 'no vlans' */
2182 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
2183 MVPP2_PRS_RI_VLAN_MASK);
2184 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2185 }
2186 /* Mask/unmask all ports, depending on dsa type */
2187 mvpp2_prs_tcam_port_map_set(&pe, port_mask);
2188 }
2189
2190 /* Update port mask */
2191 mvpp2_prs_tcam_port_set(&pe, port, add);
2192
2193 mvpp2_prs_hw_write(priv, &pe);
2194}
2195
2196/* Search for existing single/triple vlan entry */
2197static struct mvpp2_prs_entry *mvpp2_prs_vlan_find(struct mvpp2 *priv,
2198 unsigned short tpid, int ai)
2199{
2200 struct mvpp2_prs_entry *pe;
2201 int tid;
2202
2203 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2204 if (!pe)
2205 return NULL;
2206 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2207
2208 /* Go through the all entries with MVPP2_PRS_LU_VLAN */
2209 for (tid = MVPP2_PE_FIRST_FREE_TID;
2210 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
2211 unsigned int ri_bits, ai_bits;
2212 bool match;
2213
2214 if (!priv->prs_shadow[tid].valid ||
2215 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
2216 continue;
2217
2218 pe->index = tid;
2219
2220 mvpp2_prs_hw_read(priv, pe);
2221 match = mvpp2_prs_tcam_data_cmp(pe, 0, swab16(tpid));
2222 if (!match)
2223 continue;
2224
2225 /* Get vlan type */
2226 ri_bits = mvpp2_prs_sram_ri_get(pe);
2227 ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
2228
2229 /* Get current ai value from tcam */
2230 ai_bits = mvpp2_prs_tcam_ai_get(pe);
2231 /* Clear double vlan bit */
2232 ai_bits &= ~MVPP2_PRS_DBL_VLAN_AI_BIT;
2233
2234 if (ai != ai_bits)
2235 continue;
2236
2237 if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
2238 ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
2239 return pe;
2240 }
2241 kfree(pe);
2242
2243 return NULL;
2244}
2245
2246/* Add/update single/triple vlan entry */
2247static int mvpp2_prs_vlan_add(struct mvpp2 *priv, unsigned short tpid, int ai,
2248 unsigned int port_map)
2249{
2250 struct mvpp2_prs_entry *pe;
2251 int tid_aux, tid;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302252 int ret = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002253
2254 pe = mvpp2_prs_vlan_find(priv, tpid, ai);
2255
2256 if (!pe) {
2257 /* Create new tcam entry */
2258 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_LAST_FREE_TID,
2259 MVPP2_PE_FIRST_FREE_TID);
2260 if (tid < 0)
2261 return tid;
2262
2263 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2264 if (!pe)
2265 return -ENOMEM;
2266
2267 /* Get last double vlan tid */
2268 for (tid_aux = MVPP2_PE_LAST_FREE_TID;
2269 tid_aux >= MVPP2_PE_FIRST_FREE_TID; tid_aux--) {
2270 unsigned int ri_bits;
2271
2272 if (!priv->prs_shadow[tid_aux].valid ||
2273 priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
2274 continue;
2275
2276 pe->index = tid_aux;
2277 mvpp2_prs_hw_read(priv, pe);
2278 ri_bits = mvpp2_prs_sram_ri_get(pe);
2279 if ((ri_bits & MVPP2_PRS_RI_VLAN_MASK) ==
2280 MVPP2_PRS_RI_VLAN_DOUBLE)
2281 break;
2282 }
2283
Sudip Mukherjee43737472014-11-01 16:59:34 +05302284 if (tid <= tid_aux) {
2285 ret = -EINVAL;
Markus Elfringf9fd0e32017-04-17 13:50:35 +02002286 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302287 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002288
Markus Elfringbd6aaf52017-04-17 10:40:32 +02002289 memset(pe, 0, sizeof(*pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002290 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2291 pe->index = tid;
2292
2293 mvpp2_prs_match_etype(pe, 0, tpid);
2294
Maxime Chevallier56beda32018-02-28 10:14:13 +01002295 /* VLAN tag detected, proceed with VID filtering */
2296 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_VID);
2297
Marcin Wojtas3f518502014-07-10 16:52:13 -03002298 /* Clear all ai bits for next iteration */
2299 mvpp2_prs_sram_ai_update(pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2300
2301 if (ai == MVPP2_PRS_SINGLE_VLAN_AI) {
2302 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_SINGLE,
2303 MVPP2_PRS_RI_VLAN_MASK);
2304 } else {
2305 ai |= MVPP2_PRS_DBL_VLAN_AI_BIT;
2306 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_TRIPLE,
2307 MVPP2_PRS_RI_VLAN_MASK);
2308 }
2309 mvpp2_prs_tcam_ai_update(pe, ai, MVPP2_PRS_SRAM_AI_MASK);
2310
2311 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_VLAN);
2312 }
2313 /* Update ports' mask */
2314 mvpp2_prs_tcam_port_map_set(pe, port_map);
2315
2316 mvpp2_prs_hw_write(priv, pe);
Markus Elfringf9fd0e32017-04-17 13:50:35 +02002317free_pe:
Marcin Wojtas3f518502014-07-10 16:52:13 -03002318 kfree(pe);
2319
Sudip Mukherjee43737472014-11-01 16:59:34 +05302320 return ret;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002321}
2322
2323/* Get first free double vlan ai number */
2324static int mvpp2_prs_double_vlan_ai_free_get(struct mvpp2 *priv)
2325{
2326 int i;
2327
2328 for (i = 1; i < MVPP2_PRS_DBL_VLANS_MAX; i++) {
2329 if (!priv->prs_double_vlans[i])
2330 return i;
2331 }
2332
2333 return -EINVAL;
2334}
2335
2336/* Search for existing double vlan entry */
2337static struct mvpp2_prs_entry *mvpp2_prs_double_vlan_find(struct mvpp2 *priv,
2338 unsigned short tpid1,
2339 unsigned short tpid2)
2340{
2341 struct mvpp2_prs_entry *pe;
2342 int tid;
2343
2344 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2345 if (!pe)
2346 return NULL;
2347 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2348
2349 /* Go through the all entries with MVPP2_PRS_LU_VLAN */
2350 for (tid = MVPP2_PE_FIRST_FREE_TID;
2351 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
2352 unsigned int ri_mask;
2353 bool match;
2354
2355 if (!priv->prs_shadow[tid].valid ||
2356 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
2357 continue;
2358
2359 pe->index = tid;
2360 mvpp2_prs_hw_read(priv, pe);
2361
2362 match = mvpp2_prs_tcam_data_cmp(pe, 0, swab16(tpid1))
2363 && mvpp2_prs_tcam_data_cmp(pe, 4, swab16(tpid2));
2364
2365 if (!match)
2366 continue;
2367
2368 ri_mask = mvpp2_prs_sram_ri_get(pe) & MVPP2_PRS_RI_VLAN_MASK;
2369 if (ri_mask == MVPP2_PRS_RI_VLAN_DOUBLE)
2370 return pe;
2371 }
2372 kfree(pe);
2373
2374 return NULL;
2375}
2376
2377/* Add or update double vlan entry */
2378static int mvpp2_prs_double_vlan_add(struct mvpp2 *priv, unsigned short tpid1,
2379 unsigned short tpid2,
2380 unsigned int port_map)
2381{
2382 struct mvpp2_prs_entry *pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302383 int tid_aux, tid, ai, ret = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002384
2385 pe = mvpp2_prs_double_vlan_find(priv, tpid1, tpid2);
2386
2387 if (!pe) {
2388 /* Create new tcam entry */
2389 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2390 MVPP2_PE_LAST_FREE_TID);
2391 if (tid < 0)
2392 return tid;
2393
2394 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2395 if (!pe)
2396 return -ENOMEM;
2397
2398 /* Set ai value for new double vlan entry */
2399 ai = mvpp2_prs_double_vlan_ai_free_get(priv);
Sudip Mukherjee43737472014-11-01 16:59:34 +05302400 if (ai < 0) {
2401 ret = ai;
Markus Elfringc9a7e122017-04-17 13:03:49 +02002402 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302403 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002404
2405 /* Get first single/triple vlan tid */
2406 for (tid_aux = MVPP2_PE_FIRST_FREE_TID;
2407 tid_aux <= MVPP2_PE_LAST_FREE_TID; tid_aux++) {
2408 unsigned int ri_bits;
2409
2410 if (!priv->prs_shadow[tid_aux].valid ||
2411 priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
2412 continue;
2413
2414 pe->index = tid_aux;
2415 mvpp2_prs_hw_read(priv, pe);
2416 ri_bits = mvpp2_prs_sram_ri_get(pe);
2417 ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
2418 if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
2419 ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
2420 break;
2421 }
2422
Sudip Mukherjee43737472014-11-01 16:59:34 +05302423 if (tid >= tid_aux) {
2424 ret = -ERANGE;
Markus Elfringc9a7e122017-04-17 13:03:49 +02002425 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302426 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002427
Markus Elfringbd6aaf52017-04-17 10:40:32 +02002428 memset(pe, 0, sizeof(*pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002429 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2430 pe->index = tid;
2431
2432 priv->prs_double_vlans[ai] = true;
2433
2434 mvpp2_prs_match_etype(pe, 0, tpid1);
2435 mvpp2_prs_match_etype(pe, 4, tpid2);
2436
2437 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_VLAN);
Maxime Chevallier56beda32018-02-28 10:14:13 +01002438 /* Shift 4 bytes - skip outer vlan tag */
2439 mvpp2_prs_sram_shift_set(pe, MVPP2_VLAN_TAG_LEN,
Marcin Wojtas3f518502014-07-10 16:52:13 -03002440 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2441 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_DOUBLE,
2442 MVPP2_PRS_RI_VLAN_MASK);
2443 mvpp2_prs_sram_ai_update(pe, ai | MVPP2_PRS_DBL_VLAN_AI_BIT,
2444 MVPP2_PRS_SRAM_AI_MASK);
2445
2446 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_VLAN);
2447 }
2448
2449 /* Update ports' mask */
2450 mvpp2_prs_tcam_port_map_set(pe, port_map);
2451 mvpp2_prs_hw_write(priv, pe);
Markus Elfringc9a7e122017-04-17 13:03:49 +02002452free_pe:
Marcin Wojtas3f518502014-07-10 16:52:13 -03002453 kfree(pe);
Sudip Mukherjee43737472014-11-01 16:59:34 +05302454 return ret;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002455}
2456
2457/* IPv4 header parsing for fragmentation and L4 offset */
2458static int mvpp2_prs_ip4_proto(struct mvpp2 *priv, unsigned short proto,
2459 unsigned int ri, unsigned int ri_mask)
2460{
2461 struct mvpp2_prs_entry pe;
2462 int tid;
2463
2464 if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
2465 (proto != IPPROTO_IGMP))
2466 return -EINVAL;
2467
Stefan Chulskiaff3da32017-09-25 14:59:46 +02002468 /* Not fragmented packet */
Marcin Wojtas3f518502014-07-10 16:52:13 -03002469 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2470 MVPP2_PE_LAST_FREE_TID);
2471 if (tid < 0)
2472 return tid;
2473
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002474 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002475 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
2476 pe.index = tid;
2477
2478 /* Set next lu to IPv4 */
2479 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2480 mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2481 /* Set L4 offset */
2482 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
2483 sizeof(struct iphdr) - 4,
2484 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2485 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
2486 MVPP2_PRS_IPV4_DIP_AI_BIT);
Stefan Chulskiaff3da32017-09-25 14:59:46 +02002487 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask | MVPP2_PRS_RI_IP_FRAG_MASK);
2488
2489 mvpp2_prs_tcam_data_byte_set(&pe, 2, 0x00,
2490 MVPP2_PRS_TCAM_PROTO_MASK_L);
2491 mvpp2_prs_tcam_data_byte_set(&pe, 3, 0x00,
2492 MVPP2_PRS_TCAM_PROTO_MASK);
Marcin Wojtas3f518502014-07-10 16:52:13 -03002493
2494 mvpp2_prs_tcam_data_byte_set(&pe, 5, proto, MVPP2_PRS_TCAM_PROTO_MASK);
2495 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
2496 /* Unmask all ports */
2497 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2498
2499 /* Update shadow table and hw entry */
2500 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2501 mvpp2_prs_hw_write(priv, &pe);
2502
Stefan Chulskiaff3da32017-09-25 14:59:46 +02002503 /* Fragmented packet */
Marcin Wojtas3f518502014-07-10 16:52:13 -03002504 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2505 MVPP2_PE_LAST_FREE_TID);
2506 if (tid < 0)
2507 return tid;
2508
2509 pe.index = tid;
2510 /* Clear ri before updating */
2511 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2512 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2513 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
2514
Stefan Chulskiaff3da32017-09-25 14:59:46 +02002515 mvpp2_prs_sram_ri_update(&pe, ri | MVPP2_PRS_RI_IP_FRAG_TRUE,
2516 ri_mask | MVPP2_PRS_RI_IP_FRAG_MASK);
2517
2518 mvpp2_prs_tcam_data_byte_set(&pe, 2, 0x00, 0x0);
2519 mvpp2_prs_tcam_data_byte_set(&pe, 3, 0x00, 0x0);
Marcin Wojtas3f518502014-07-10 16:52:13 -03002520
2521 /* Update shadow table and hw entry */
2522 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2523 mvpp2_prs_hw_write(priv, &pe);
2524
2525 return 0;
2526}
2527
2528/* IPv4 L3 multicast or broadcast */
2529static int mvpp2_prs_ip4_cast(struct mvpp2 *priv, unsigned short l3_cast)
2530{
2531 struct mvpp2_prs_entry pe;
2532 int mask, tid;
2533
2534 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2535 MVPP2_PE_LAST_FREE_TID);
2536 if (tid < 0)
2537 return tid;
2538
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002539 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002540 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
2541 pe.index = tid;
2542
2543 switch (l3_cast) {
2544 case MVPP2_PRS_L3_MULTI_CAST:
2545 mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV4_MC,
2546 MVPP2_PRS_IPV4_MC_MASK);
2547 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
2548 MVPP2_PRS_RI_L3_ADDR_MASK);
2549 break;
2550 case MVPP2_PRS_L3_BROAD_CAST:
2551 mask = MVPP2_PRS_IPV4_BC_MASK;
2552 mvpp2_prs_tcam_data_byte_set(&pe, 0, mask, mask);
2553 mvpp2_prs_tcam_data_byte_set(&pe, 1, mask, mask);
2554 mvpp2_prs_tcam_data_byte_set(&pe, 2, mask, mask);
2555 mvpp2_prs_tcam_data_byte_set(&pe, 3, mask, mask);
2556 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_BCAST,
2557 MVPP2_PRS_RI_L3_ADDR_MASK);
2558 break;
2559 default:
2560 return -EINVAL;
2561 }
2562
2563 /* Finished: go to flowid generation */
2564 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2565 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2566
2567 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
2568 MVPP2_PRS_IPV4_DIP_AI_BIT);
2569 /* Unmask all ports */
2570 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2571
2572 /* Update shadow table and hw entry */
2573 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2574 mvpp2_prs_hw_write(priv, &pe);
2575
2576 return 0;
2577}
2578
2579/* Set entries for protocols over IPv6 */
2580static int mvpp2_prs_ip6_proto(struct mvpp2 *priv, unsigned short proto,
2581 unsigned int ri, unsigned int ri_mask)
2582{
2583 struct mvpp2_prs_entry pe;
2584 int tid;
2585
2586 if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
2587 (proto != IPPROTO_ICMPV6) && (proto != IPPROTO_IPIP))
2588 return -EINVAL;
2589
2590 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2591 MVPP2_PE_LAST_FREE_TID);
2592 if (tid < 0)
2593 return tid;
2594
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002595 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002596 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2597 pe.index = tid;
2598
2599 /* Finished: go to flowid generation */
2600 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2601 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2602 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
2603 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
2604 sizeof(struct ipv6hdr) - 6,
2605 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2606
2607 mvpp2_prs_tcam_data_byte_set(&pe, 0, proto, MVPP2_PRS_TCAM_PROTO_MASK);
2608 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
2609 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2610 /* Unmask all ports */
2611 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2612
2613 /* Write HW */
2614 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
2615 mvpp2_prs_hw_write(priv, &pe);
2616
2617 return 0;
2618}
2619
2620/* IPv6 L3 multicast entry */
2621static int mvpp2_prs_ip6_cast(struct mvpp2 *priv, unsigned short l3_cast)
2622{
2623 struct mvpp2_prs_entry pe;
2624 int tid;
2625
2626 if (l3_cast != MVPP2_PRS_L3_MULTI_CAST)
2627 return -EINVAL;
2628
2629 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2630 MVPP2_PE_LAST_FREE_TID);
2631 if (tid < 0)
2632 return tid;
2633
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002634 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002635 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2636 pe.index = tid;
2637
2638 /* Finished: go to flowid generation */
2639 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2640 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
2641 MVPP2_PRS_RI_L3_ADDR_MASK);
2642 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
2643 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2644 /* Shift back to IPv6 NH */
2645 mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2646
2647 mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV6_MC,
2648 MVPP2_PRS_IPV6_MC_MASK);
2649 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2650 /* Unmask all ports */
2651 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2652
2653 /* Update shadow table and hw entry */
2654 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
2655 mvpp2_prs_hw_write(priv, &pe);
2656
2657 return 0;
2658}
2659
2660/* Parser per-port initialization */
2661static void mvpp2_prs_hw_port_init(struct mvpp2 *priv, int port, int lu_first,
2662 int lu_max, int offset)
2663{
2664 u32 val;
2665
2666 /* Set lookup ID */
2667 val = mvpp2_read(priv, MVPP2_PRS_INIT_LOOKUP_REG);
2668 val &= ~MVPP2_PRS_PORT_LU_MASK(port);
2669 val |= MVPP2_PRS_PORT_LU_VAL(port, lu_first);
2670 mvpp2_write(priv, MVPP2_PRS_INIT_LOOKUP_REG, val);
2671
2672 /* Set maximum number of loops for packet received from port */
2673 val = mvpp2_read(priv, MVPP2_PRS_MAX_LOOP_REG(port));
2674 val &= ~MVPP2_PRS_MAX_LOOP_MASK(port);
2675 val |= MVPP2_PRS_MAX_LOOP_VAL(port, lu_max);
2676 mvpp2_write(priv, MVPP2_PRS_MAX_LOOP_REG(port), val);
2677
2678 /* Set initial offset for packet header extraction for the first
2679 * searching loop
2680 */
2681 val = mvpp2_read(priv, MVPP2_PRS_INIT_OFFS_REG(port));
2682 val &= ~MVPP2_PRS_INIT_OFF_MASK(port);
2683 val |= MVPP2_PRS_INIT_OFF_VAL(port, offset);
2684 mvpp2_write(priv, MVPP2_PRS_INIT_OFFS_REG(port), val);
2685}
2686
2687/* Default flow entries initialization for all ports */
2688static void mvpp2_prs_def_flow_init(struct mvpp2 *priv)
2689{
2690 struct mvpp2_prs_entry pe;
2691 int port;
2692
2693 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002694 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002695 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2696 pe.index = MVPP2_PE_FIRST_DEFAULT_FLOW - port;
2697
2698 /* Mask all ports */
2699 mvpp2_prs_tcam_port_map_set(&pe, 0);
2700
2701 /* Set flow ID*/
2702 mvpp2_prs_sram_ai_update(&pe, port, MVPP2_PRS_FLOW_ID_MASK);
2703 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
2704
2705 /* Update shadow table and hw entry */
2706 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_FLOWS);
2707 mvpp2_prs_hw_write(priv, &pe);
2708 }
2709}
2710
2711/* Set default entry for Marvell Header field */
2712static void mvpp2_prs_mh_init(struct mvpp2 *priv)
2713{
2714 struct mvpp2_prs_entry pe;
2715
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002716 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002717
2718 pe.index = MVPP2_PE_MH_DEFAULT;
2719 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MH);
2720 mvpp2_prs_sram_shift_set(&pe, MVPP2_MH_SIZE,
2721 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2722 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_MAC);
2723
2724 /* Unmask all ports */
2725 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2726
2727 /* Update shadow table and hw entry */
2728 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MH);
2729 mvpp2_prs_hw_write(priv, &pe);
2730}
2731
2732/* Set default entires (place holder) for promiscuous, non-promiscuous and
2733 * multicast MAC addresses
2734 */
2735static void mvpp2_prs_mac_init(struct mvpp2 *priv)
2736{
2737 struct mvpp2_prs_entry pe;
2738
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002739 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002740
2741 /* Non-promiscuous mode for all ports - DROP unknown packets */
2742 pe.index = MVPP2_PE_MAC_NON_PROMISCUOUS;
2743 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
2744
2745 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
2746 MVPP2_PRS_RI_DROP_MASK);
2747 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2748 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2749
2750 /* Unmask all ports */
2751 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2752
2753 /* Update shadow table and hw entry */
2754 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2755 mvpp2_prs_hw_write(priv, &pe);
2756
2757 /* place holders only - no ports */
2758 mvpp2_prs_mac_drop_all_set(priv, 0, false);
2759 mvpp2_prs_mac_promisc_set(priv, 0, false);
Antoine Tenart20746d72017-10-24 11:41:27 +02002760 mvpp2_prs_mac_multi_set(priv, 0, MVPP2_PE_MAC_MC_ALL, false);
2761 mvpp2_prs_mac_multi_set(priv, 0, MVPP2_PE_MAC_MC_IP6, false);
Marcin Wojtas3f518502014-07-10 16:52:13 -03002762}
2763
2764/* Set default entries for various types of dsa packets */
2765static void mvpp2_prs_dsa_init(struct mvpp2 *priv)
2766{
2767 struct mvpp2_prs_entry pe;
2768
2769 /* None tagged EDSA entry - place holder */
2770 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
2771 MVPP2_PRS_EDSA);
2772
2773 /* Tagged EDSA entry - place holder */
2774 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
2775
2776 /* None tagged DSA entry - place holder */
2777 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
2778 MVPP2_PRS_DSA);
2779
2780 /* Tagged DSA entry - place holder */
2781 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
2782
2783 /* None tagged EDSA ethertype entry - place holder*/
2784 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
2785 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
2786
2787 /* Tagged EDSA ethertype entry - place holder*/
2788 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
2789 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
2790
2791 /* None tagged DSA ethertype entry */
2792 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
2793 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
2794
2795 /* Tagged DSA ethertype entry */
2796 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
2797 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
2798
2799 /* Set default entry, in case DSA or EDSA tag not found */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002800 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002801 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
2802 pe.index = MVPP2_PE_DSA_DEFAULT;
2803 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2804
2805 /* Shift 0 bytes */
2806 mvpp2_prs_sram_shift_set(&pe, 0, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2807 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2808
2809 /* Clear all sram ai bits for next iteration */
2810 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2811
2812 /* Unmask all ports */
2813 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2814
2815 mvpp2_prs_hw_write(priv, &pe);
2816}
2817
Maxime Chevallier56beda32018-02-28 10:14:13 +01002818/* Initialize parser entries for VID filtering */
2819static void mvpp2_prs_vid_init(struct mvpp2 *priv)
2820{
2821 struct mvpp2_prs_entry pe;
2822
2823 memset(&pe, 0, sizeof(pe));
2824
2825 /* Set default vid entry */
2826 pe.index = MVPP2_PE_VID_FLTR_DEFAULT;
2827 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VID);
2828
2829 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_EDSA_VID_AI_BIT);
2830
2831 /* Skip VLAN header - Set offset to 4 bytes */
2832 mvpp2_prs_sram_shift_set(&pe, MVPP2_VLAN_TAG_LEN,
2833 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2834
2835 /* Clear all ai bits for next iteration */
2836 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2837
2838 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2839
2840 /* Unmask all ports */
2841 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2842
2843 /* Update shadow table and hw entry */
2844 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VID);
2845 mvpp2_prs_hw_write(priv, &pe);
2846
2847 /* Set default vid entry for extended DSA*/
2848 memset(&pe, 0, sizeof(pe));
2849
2850 /* Set default vid entry */
2851 pe.index = MVPP2_PE_VID_EDSA_FLTR_DEFAULT;
2852 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VID);
2853
2854 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_EDSA_VID_AI_BIT,
2855 MVPP2_PRS_EDSA_VID_AI_BIT);
2856
2857 /* Skip VLAN header - Set offset to 8 bytes */
2858 mvpp2_prs_sram_shift_set(&pe, MVPP2_VLAN_TAG_EDSA_LEN,
2859 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2860
2861 /* Clear all ai bits for next iteration */
2862 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2863
2864 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2865
2866 /* Unmask all ports */
2867 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2868
2869 /* Update shadow table and hw entry */
2870 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VID);
2871 mvpp2_prs_hw_write(priv, &pe);
2872}
2873
Marcin Wojtas3f518502014-07-10 16:52:13 -03002874/* Match basic ethertypes */
2875static int mvpp2_prs_etype_init(struct mvpp2 *priv)
2876{
2877 struct mvpp2_prs_entry pe;
2878 int tid;
2879
2880 /* Ethertype: PPPoE */
2881 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2882 MVPP2_PE_LAST_FREE_TID);
2883 if (tid < 0)
2884 return tid;
2885
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002886 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002887 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2888 pe.index = tid;
2889
2890 mvpp2_prs_match_etype(&pe, 0, ETH_P_PPP_SES);
2891
2892 mvpp2_prs_sram_shift_set(&pe, MVPP2_PPPOE_HDR_SIZE,
2893 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2894 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2895 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_PPPOE_MASK,
2896 MVPP2_PRS_RI_PPPOE_MASK);
2897
2898 /* Update shadow table and hw entry */
2899 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2900 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2901 priv->prs_shadow[pe.index].finish = false;
2902 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_PPPOE_MASK,
2903 MVPP2_PRS_RI_PPPOE_MASK);
2904 mvpp2_prs_hw_write(priv, &pe);
2905
2906 /* Ethertype: ARP */
2907 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2908 MVPP2_PE_LAST_FREE_TID);
2909 if (tid < 0)
2910 return tid;
2911
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002912 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002913 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2914 pe.index = tid;
2915
2916 mvpp2_prs_match_etype(&pe, 0, ETH_P_ARP);
2917
2918 /* Generate flow in the next iteration*/
2919 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2920 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2921 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_ARP,
2922 MVPP2_PRS_RI_L3_PROTO_MASK);
2923 /* Set L3 offset */
2924 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2925 MVPP2_ETH_TYPE_LEN,
2926 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2927
2928 /* Update shadow table and hw entry */
2929 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2930 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2931 priv->prs_shadow[pe.index].finish = true;
2932 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_ARP,
2933 MVPP2_PRS_RI_L3_PROTO_MASK);
2934 mvpp2_prs_hw_write(priv, &pe);
2935
2936 /* Ethertype: LBTD */
2937 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2938 MVPP2_PE_LAST_FREE_TID);
2939 if (tid < 0)
2940 return tid;
2941
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002942 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002943 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2944 pe.index = tid;
2945
2946 mvpp2_prs_match_etype(&pe, 0, MVPP2_IP_LBDT_TYPE);
2947
2948 /* Generate flow in the next iteration*/
2949 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2950 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2951 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2952 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2953 MVPP2_PRS_RI_CPU_CODE_MASK |
2954 MVPP2_PRS_RI_UDF3_MASK);
2955 /* Set L3 offset */
2956 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2957 MVPP2_ETH_TYPE_LEN,
2958 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2959
2960 /* Update shadow table and hw entry */
2961 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2962 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2963 priv->prs_shadow[pe.index].finish = true;
2964 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2965 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2966 MVPP2_PRS_RI_CPU_CODE_MASK |
2967 MVPP2_PRS_RI_UDF3_MASK);
2968 mvpp2_prs_hw_write(priv, &pe);
2969
2970 /* Ethertype: IPv4 without options */
2971 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2972 MVPP2_PE_LAST_FREE_TID);
2973 if (tid < 0)
2974 return tid;
2975
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002976 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002977 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2978 pe.index = tid;
2979
2980 mvpp2_prs_match_etype(&pe, 0, ETH_P_IP);
2981 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2982 MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
2983 MVPP2_PRS_IPV4_HEAD_MASK |
2984 MVPP2_PRS_IPV4_IHL_MASK);
2985
2986 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2987 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
2988 MVPP2_PRS_RI_L3_PROTO_MASK);
2989 /* Skip eth_type + 4 bytes of IP header */
2990 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
2991 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2992 /* Set L3 offset */
2993 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2994 MVPP2_ETH_TYPE_LEN,
2995 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2996
2997 /* Update shadow table and hw entry */
2998 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2999 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
3000 priv->prs_shadow[pe.index].finish = false;
3001 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4,
3002 MVPP2_PRS_RI_L3_PROTO_MASK);
3003 mvpp2_prs_hw_write(priv, &pe);
3004
3005 /* Ethertype: IPv4 with options */
3006 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3007 MVPP2_PE_LAST_FREE_TID);
3008 if (tid < 0)
3009 return tid;
3010
3011 pe.index = tid;
3012
3013 /* Clear tcam data before updating */
3014 pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(MVPP2_ETH_TYPE_LEN)] = 0x0;
3015 pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(MVPP2_ETH_TYPE_LEN)] = 0x0;
3016
3017 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
3018 MVPP2_PRS_IPV4_HEAD,
3019 MVPP2_PRS_IPV4_HEAD_MASK);
3020
3021 /* Clear ri before updating */
3022 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
3023 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
3024 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
3025 MVPP2_PRS_RI_L3_PROTO_MASK);
3026
3027 /* Update shadow table and hw entry */
3028 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
3029 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
3030 priv->prs_shadow[pe.index].finish = false;
3031 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4_OPT,
3032 MVPP2_PRS_RI_L3_PROTO_MASK);
3033 mvpp2_prs_hw_write(priv, &pe);
3034
3035 /* Ethertype: IPv6 without options */
3036 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3037 MVPP2_PE_LAST_FREE_TID);
3038 if (tid < 0)
3039 return tid;
3040
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003041 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003042 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
3043 pe.index = tid;
3044
3045 mvpp2_prs_match_etype(&pe, 0, ETH_P_IPV6);
3046
3047 /* Skip DIP of IPV6 header */
3048 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 8 +
3049 MVPP2_MAX_L3_ADDR_SIZE,
3050 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3051 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
3052 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
3053 MVPP2_PRS_RI_L3_PROTO_MASK);
3054 /* Set L3 offset */
3055 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
3056 MVPP2_ETH_TYPE_LEN,
3057 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3058
3059 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
3060 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
3061 priv->prs_shadow[pe.index].finish = false;
3062 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP6,
3063 MVPP2_PRS_RI_L3_PROTO_MASK);
3064 mvpp2_prs_hw_write(priv, &pe);
3065
3066 /* Default entry for MVPP2_PRS_LU_L2 - Unknown ethtype */
3067 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3068 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
3069 pe.index = MVPP2_PE_ETH_TYPE_UN;
3070
3071 /* Unmask all ports */
3072 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3073
3074 /* Generate flow in the next iteration*/
3075 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3076 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3077 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
3078 MVPP2_PRS_RI_L3_PROTO_MASK);
3079 /* Set L3 offset even it's unknown L3 */
3080 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
3081 MVPP2_ETH_TYPE_LEN,
3082 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3083
3084 /* Update shadow table and hw entry */
3085 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
3086 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
3087 priv->prs_shadow[pe.index].finish = true;
3088 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_UN,
3089 MVPP2_PRS_RI_L3_PROTO_MASK);
3090 mvpp2_prs_hw_write(priv, &pe);
3091
3092 return 0;
3093}
3094
3095/* Configure vlan entries and detect up to 2 successive VLAN tags.
3096 * Possible options:
3097 * 0x8100, 0x88A8
3098 * 0x8100, 0x8100
3099 * 0x8100
3100 * 0x88A8
3101 */
3102static int mvpp2_prs_vlan_init(struct platform_device *pdev, struct mvpp2 *priv)
3103{
3104 struct mvpp2_prs_entry pe;
3105 int err;
3106
3107 priv->prs_double_vlans = devm_kcalloc(&pdev->dev, sizeof(bool),
3108 MVPP2_PRS_DBL_VLANS_MAX,
3109 GFP_KERNEL);
3110 if (!priv->prs_double_vlans)
3111 return -ENOMEM;
3112
3113 /* Double VLAN: 0x8100, 0x88A8 */
3114 err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021AD,
3115 MVPP2_PRS_PORT_MASK);
3116 if (err)
3117 return err;
3118
3119 /* Double VLAN: 0x8100, 0x8100 */
3120 err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021Q,
3121 MVPP2_PRS_PORT_MASK);
3122 if (err)
3123 return err;
3124
3125 /* Single VLAN: 0x88a8 */
3126 err = mvpp2_prs_vlan_add(priv, ETH_P_8021AD, MVPP2_PRS_SINGLE_VLAN_AI,
3127 MVPP2_PRS_PORT_MASK);
3128 if (err)
3129 return err;
3130
3131 /* Single VLAN: 0x8100 */
3132 err = mvpp2_prs_vlan_add(priv, ETH_P_8021Q, MVPP2_PRS_SINGLE_VLAN_AI,
3133 MVPP2_PRS_PORT_MASK);
3134 if (err)
3135 return err;
3136
3137 /* Set default double vlan entry */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003138 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003139 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
3140 pe.index = MVPP2_PE_VLAN_DBL;
3141
Maxime Chevallier56beda32018-02-28 10:14:13 +01003142 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VID);
3143
Marcin Wojtas3f518502014-07-10 16:52:13 -03003144 /* Clear ai for next iterations */
3145 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
3146 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_DOUBLE,
3147 MVPP2_PRS_RI_VLAN_MASK);
3148
3149 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_DBL_VLAN_AI_BIT,
3150 MVPP2_PRS_DBL_VLAN_AI_BIT);
3151 /* Unmask all ports */
3152 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3153
3154 /* Update shadow table and hw entry */
3155 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
3156 mvpp2_prs_hw_write(priv, &pe);
3157
3158 /* Set default vlan none entry */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003159 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003160 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
3161 pe.index = MVPP2_PE_VLAN_NONE;
3162
3163 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
3164 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
3165 MVPP2_PRS_RI_VLAN_MASK);
3166
3167 /* Unmask all ports */
3168 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3169
3170 /* Update shadow table and hw entry */
3171 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
3172 mvpp2_prs_hw_write(priv, &pe);
3173
3174 return 0;
3175}
3176
3177/* Set entries for PPPoE ethertype */
3178static int mvpp2_prs_pppoe_init(struct mvpp2 *priv)
3179{
3180 struct mvpp2_prs_entry pe;
3181 int tid;
3182
3183 /* IPv4 over PPPoE with options */
3184 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3185 MVPP2_PE_LAST_FREE_TID);
3186 if (tid < 0)
3187 return tid;
3188
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003189 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003190 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
3191 pe.index = tid;
3192
3193 mvpp2_prs_match_etype(&pe, 0, PPP_IP);
3194
3195 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
3196 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
3197 MVPP2_PRS_RI_L3_PROTO_MASK);
3198 /* Skip eth_type + 4 bytes of IP header */
3199 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
3200 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3201 /* Set L3 offset */
3202 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
3203 MVPP2_ETH_TYPE_LEN,
3204 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3205
3206 /* Update shadow table and hw entry */
3207 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3208 mvpp2_prs_hw_write(priv, &pe);
3209
3210 /* IPv4 over PPPoE without options */
3211 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3212 MVPP2_PE_LAST_FREE_TID);
3213 if (tid < 0)
3214 return tid;
3215
3216 pe.index = tid;
3217
3218 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
3219 MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
3220 MVPP2_PRS_IPV4_HEAD_MASK |
3221 MVPP2_PRS_IPV4_IHL_MASK);
3222
3223 /* Clear ri before updating */
3224 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
3225 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
3226 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
3227 MVPP2_PRS_RI_L3_PROTO_MASK);
3228
3229 /* Update shadow table and hw entry */
3230 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3231 mvpp2_prs_hw_write(priv, &pe);
3232
3233 /* IPv6 over PPPoE */
3234 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3235 MVPP2_PE_LAST_FREE_TID);
3236 if (tid < 0)
3237 return tid;
3238
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003239 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003240 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
3241 pe.index = tid;
3242
3243 mvpp2_prs_match_etype(&pe, 0, PPP_IPV6);
3244
3245 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
3246 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
3247 MVPP2_PRS_RI_L3_PROTO_MASK);
3248 /* Skip eth_type + 4 bytes of IPv6 header */
3249 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
3250 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3251 /* Set L3 offset */
3252 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
3253 MVPP2_ETH_TYPE_LEN,
3254 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3255
3256 /* Update shadow table and hw entry */
3257 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3258 mvpp2_prs_hw_write(priv, &pe);
3259
3260 /* Non-IP over PPPoE */
3261 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3262 MVPP2_PE_LAST_FREE_TID);
3263 if (tid < 0)
3264 return tid;
3265
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003266 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003267 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
3268 pe.index = tid;
3269
3270 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
3271 MVPP2_PRS_RI_L3_PROTO_MASK);
3272
3273 /* Finished: go to flowid generation */
3274 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3275 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3276 /* Set L3 offset even if it's unknown L3 */
3277 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
3278 MVPP2_ETH_TYPE_LEN,
3279 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3280
3281 /* Update shadow table and hw entry */
3282 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3283 mvpp2_prs_hw_write(priv, &pe);
3284
3285 return 0;
3286}
3287
3288/* Initialize entries for IPv4 */
3289static int mvpp2_prs_ip4_init(struct mvpp2 *priv)
3290{
3291 struct mvpp2_prs_entry pe;
3292 int err;
3293
3294 /* Set entries for TCP, UDP and IGMP over IPv4 */
3295 err = mvpp2_prs_ip4_proto(priv, IPPROTO_TCP, MVPP2_PRS_RI_L4_TCP,
3296 MVPP2_PRS_RI_L4_PROTO_MASK);
3297 if (err)
3298 return err;
3299
3300 err = mvpp2_prs_ip4_proto(priv, IPPROTO_UDP, MVPP2_PRS_RI_L4_UDP,
3301 MVPP2_PRS_RI_L4_PROTO_MASK);
3302 if (err)
3303 return err;
3304
3305 err = mvpp2_prs_ip4_proto(priv, IPPROTO_IGMP,
3306 MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
3307 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
3308 MVPP2_PRS_RI_CPU_CODE_MASK |
3309 MVPP2_PRS_RI_UDF3_MASK);
3310 if (err)
3311 return err;
3312
3313 /* IPv4 Broadcast */
3314 err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_BROAD_CAST);
3315 if (err)
3316 return err;
3317
3318 /* IPv4 Multicast */
3319 err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
3320 if (err)
3321 return err;
3322
3323 /* Default IPv4 entry for unknown protocols */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003324 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003325 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
3326 pe.index = MVPP2_PE_IP4_PROTO_UN;
3327
3328 /* Set next lu to IPv4 */
3329 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
3330 mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3331 /* Set L4 offset */
3332 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
3333 sizeof(struct iphdr) - 4,
3334 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3335 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
3336 MVPP2_PRS_IPV4_DIP_AI_BIT);
3337 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3338 MVPP2_PRS_RI_L4_PROTO_MASK);
3339
3340 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
3341 /* Unmask all ports */
3342 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3343
3344 /* Update shadow table and hw entry */
3345 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3346 mvpp2_prs_hw_write(priv, &pe);
3347
3348 /* Default IPv4 entry for unicast address */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003349 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003350 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
3351 pe.index = MVPP2_PE_IP4_ADDR_UN;
3352
3353 /* Finished: go to flowid generation */
3354 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3355 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3356 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
3357 MVPP2_PRS_RI_L3_ADDR_MASK);
3358
3359 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
3360 MVPP2_PRS_IPV4_DIP_AI_BIT);
3361 /* Unmask all ports */
3362 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3363
3364 /* Update shadow table and hw entry */
3365 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3366 mvpp2_prs_hw_write(priv, &pe);
3367
3368 return 0;
3369}
3370
3371/* Initialize entries for IPv6 */
3372static int mvpp2_prs_ip6_init(struct mvpp2 *priv)
3373{
3374 struct mvpp2_prs_entry pe;
3375 int tid, err;
3376
3377 /* Set entries for TCP, UDP and ICMP over IPv6 */
3378 err = mvpp2_prs_ip6_proto(priv, IPPROTO_TCP,
3379 MVPP2_PRS_RI_L4_TCP,
3380 MVPP2_PRS_RI_L4_PROTO_MASK);
3381 if (err)
3382 return err;
3383
3384 err = mvpp2_prs_ip6_proto(priv, IPPROTO_UDP,
3385 MVPP2_PRS_RI_L4_UDP,
3386 MVPP2_PRS_RI_L4_PROTO_MASK);
3387 if (err)
3388 return err;
3389
3390 err = mvpp2_prs_ip6_proto(priv, IPPROTO_ICMPV6,
3391 MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
3392 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
3393 MVPP2_PRS_RI_CPU_CODE_MASK |
3394 MVPP2_PRS_RI_UDF3_MASK);
3395 if (err)
3396 return err;
3397
3398 /* IPv4 is the last header. This is similar case as 6-TCP or 17-UDP */
3399 /* Result Info: UDF7=1, DS lite */
3400 err = mvpp2_prs_ip6_proto(priv, IPPROTO_IPIP,
3401 MVPP2_PRS_RI_UDF7_IP6_LITE,
3402 MVPP2_PRS_RI_UDF7_MASK);
3403 if (err)
3404 return err;
3405
3406 /* IPv6 multicast */
3407 err = mvpp2_prs_ip6_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
3408 if (err)
3409 return err;
3410
3411 /* Entry for checking hop limit */
3412 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3413 MVPP2_PE_LAST_FREE_TID);
3414 if (tid < 0)
3415 return tid;
3416
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003417 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003418 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3419 pe.index = tid;
3420
3421 /* Finished: go to flowid generation */
3422 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3423 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3424 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN |
3425 MVPP2_PRS_RI_DROP_MASK,
3426 MVPP2_PRS_RI_L3_PROTO_MASK |
3427 MVPP2_PRS_RI_DROP_MASK);
3428
3429 mvpp2_prs_tcam_data_byte_set(&pe, 1, 0x00, MVPP2_PRS_IPV6_HOP_MASK);
3430 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3431 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3432
3433 /* Update shadow table and hw entry */
3434 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3435 mvpp2_prs_hw_write(priv, &pe);
3436
3437 /* Default IPv6 entry for unknown protocols */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003438 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003439 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3440 pe.index = MVPP2_PE_IP6_PROTO_UN;
3441
3442 /* Finished: go to flowid generation */
3443 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3444 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3445 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3446 MVPP2_PRS_RI_L4_PROTO_MASK);
3447 /* Set L4 offset relatively to our current place */
3448 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
3449 sizeof(struct ipv6hdr) - 4,
3450 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3451
3452 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3453 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3454 /* Unmask all ports */
3455 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3456
3457 /* Update shadow table and hw entry */
3458 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3459 mvpp2_prs_hw_write(priv, &pe);
3460
3461 /* Default IPv6 entry for unknown ext protocols */
3462 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3463 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3464 pe.index = MVPP2_PE_IP6_EXT_PROTO_UN;
3465
3466 /* Finished: go to flowid generation */
3467 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3468 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3469 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3470 MVPP2_PRS_RI_L4_PROTO_MASK);
3471
3472 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_EXT_AI_BIT,
3473 MVPP2_PRS_IPV6_EXT_AI_BIT);
3474 /* Unmask all ports */
3475 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3476
3477 /* Update shadow table and hw entry */
3478 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3479 mvpp2_prs_hw_write(priv, &pe);
3480
3481 /* Default IPv6 entry for unicast address */
3482 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3483 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3484 pe.index = MVPP2_PE_IP6_ADDR_UN;
3485
3486 /* Finished: go to IPv6 again */
3487 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
3488 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
3489 MVPP2_PRS_RI_L3_ADDR_MASK);
3490 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3491 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3492 /* Shift back to IPV6 NH */
3493 mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3494
3495 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3496 /* Unmask all ports */
3497 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3498
3499 /* Update shadow table and hw entry */
3500 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
3501 mvpp2_prs_hw_write(priv, &pe);
3502
3503 return 0;
3504}
3505
Maxime Chevallier56beda32018-02-28 10:14:13 +01003506/* Find tcam entry with matched pair <vid,port> */
3507static int mvpp2_prs_vid_range_find(struct mvpp2 *priv, int pmap, u16 vid,
3508 u16 mask)
3509{
3510 unsigned char byte[2], enable[2];
3511 struct mvpp2_prs_entry pe;
3512 u16 rvid, rmask;
3513 int tid;
3514
3515 /* Go through the all entries with MVPP2_PRS_LU_VID */
3516 for (tid = MVPP2_PE_VID_FILT_RANGE_START;
3517 tid <= MVPP2_PE_VID_FILT_RANGE_END; tid++) {
3518 if (!priv->prs_shadow[tid].valid ||
3519 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VID)
3520 continue;
3521
3522 pe.index = tid;
3523
3524 mvpp2_prs_hw_read(priv, &pe);
3525 mvpp2_prs_tcam_data_byte_get(&pe, 2, &byte[0], &enable[0]);
3526 mvpp2_prs_tcam_data_byte_get(&pe, 3, &byte[1], &enable[1]);
3527
3528 rvid = ((byte[0] & 0xf) << 8) + byte[1];
3529 rmask = ((enable[0] & 0xf) << 8) + enable[1];
3530
3531 if (rvid != vid || rmask != mask)
3532 continue;
3533
3534 return tid;
3535 }
3536
3537 return 0;
3538}
3539
3540/* Write parser entry for VID filtering */
3541static int mvpp2_prs_vid_entry_add(struct mvpp2_port *port, u16 vid)
3542{
3543 unsigned int vid_start = MVPP2_PE_VID_FILT_RANGE_START +
3544 port->id * MVPP2_PRS_VLAN_FILT_MAX;
3545 unsigned int mask = 0xfff, reg_val, shift;
3546 struct mvpp2 *priv = port->priv;
3547 struct mvpp2_prs_entry pe;
3548 int tid;
3549
3550 /* Scan TCAM and see if entry with this <vid,port> already exist */
3551 tid = mvpp2_prs_vid_range_find(priv, (1 << port->id), vid, mask);
3552
3553 reg_val = mvpp2_read(priv, MVPP2_MH_REG(port->id));
3554 if (reg_val & MVPP2_DSA_EXTENDED)
3555 shift = MVPP2_VLAN_TAG_EDSA_LEN;
3556 else
3557 shift = MVPP2_VLAN_TAG_LEN;
3558
3559 /* No such entry */
3560 if (!tid) {
3561 memset(&pe, 0, sizeof(pe));
3562
3563 /* Go through all entries from first to last in vlan range */
3564 tid = mvpp2_prs_tcam_first_free(priv, vid_start,
3565 vid_start +
3566 MVPP2_PRS_VLAN_FILT_MAX_ENTRY);
3567
3568 /* There isn't room for a new VID filter */
3569 if (tid < 0)
3570 return tid;
3571
3572 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VID);
3573 pe.index = tid;
3574
3575 /* Mask all ports */
3576 mvpp2_prs_tcam_port_map_set(&pe, 0);
3577 } else {
3578 mvpp2_prs_hw_read(priv, &pe);
3579 }
3580
3581 /* Enable the current port */
3582 mvpp2_prs_tcam_port_set(&pe, port->id, true);
3583
3584 /* Continue - set next lookup */
3585 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
3586
3587 /* Skip VLAN header - Set offset to 4 or 8 bytes */
3588 mvpp2_prs_sram_shift_set(&pe, shift, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3589
3590 /* Set match on VID */
3591 mvpp2_prs_match_vid(&pe, MVPP2_PRS_VID_TCAM_BYTE, vid);
3592
3593 /* Clear all ai bits for next iteration */
3594 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
3595
3596 /* Update shadow table */
3597 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VID);
3598 mvpp2_prs_hw_write(priv, &pe);
3599
3600 return 0;
3601}
3602
3603/* Write parser entry for VID filtering */
3604static void mvpp2_prs_vid_entry_remove(struct mvpp2_port *port, u16 vid)
3605{
3606 struct mvpp2 *priv = port->priv;
3607 int tid;
3608
3609 /* Scan TCAM and see if entry with this <vid,port> already exist */
3610 tid = mvpp2_prs_vid_range_find(priv, (1 << port->id), vid, 0xfff);
3611
3612 /* No such entry */
3613 if (tid)
3614 return;
3615
3616 mvpp2_prs_hw_inv(priv, tid);
3617 priv->prs_shadow[tid].valid = false;
3618}
3619
3620/* Remove all existing VID filters on this port */
3621static void mvpp2_prs_vid_remove_all(struct mvpp2_port *port)
3622{
3623 struct mvpp2 *priv = port->priv;
3624 int tid;
3625
3626 for (tid = MVPP2_PRS_VID_PORT_FIRST(port->id);
3627 tid <= MVPP2_PRS_VID_PORT_LAST(port->id); tid++) {
3628 if (priv->prs_shadow[tid].valid)
3629 mvpp2_prs_vid_entry_remove(port, tid);
3630 }
3631}
3632
3633/* Remove VID filering entry for this port */
3634static void mvpp2_prs_vid_disable_filtering(struct mvpp2_port *port)
3635{
3636 unsigned int tid = MVPP2_PRS_VID_PORT_DFLT(port->id);
3637 struct mvpp2 *priv = port->priv;
3638
3639 /* Invalidate the guard entry */
3640 mvpp2_prs_hw_inv(priv, tid);
3641
3642 priv->prs_shadow[tid].valid = false;
3643}
3644
3645/* Add guard entry that drops packets when no VID is matched on this port */
3646static void mvpp2_prs_vid_enable_filtering(struct mvpp2_port *port)
3647{
3648 unsigned int tid = MVPP2_PRS_VID_PORT_DFLT(port->id);
3649 struct mvpp2 *priv = port->priv;
3650 unsigned int reg_val, shift;
3651 struct mvpp2_prs_entry pe;
3652
3653 if (priv->prs_shadow[tid].valid)
3654 return;
3655
3656 memset(&pe, 0, sizeof(pe));
3657
3658 pe.index = tid;
3659
3660 reg_val = mvpp2_read(priv, MVPP2_MH_REG(port->id));
3661 if (reg_val & MVPP2_DSA_EXTENDED)
3662 shift = MVPP2_VLAN_TAG_EDSA_LEN;
3663 else
3664 shift = MVPP2_VLAN_TAG_LEN;
3665
3666 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VID);
3667
3668 /* Mask all ports */
3669 mvpp2_prs_tcam_port_map_set(&pe, 0);
3670
3671 /* Update port mask */
3672 mvpp2_prs_tcam_port_set(&pe, port->id, true);
3673
3674 /* Continue - set next lookup */
3675 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
3676
3677 /* Skip VLAN header - Set offset to 4 or 8 bytes */
3678 mvpp2_prs_sram_shift_set(&pe, shift, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3679
3680 /* Drop VLAN packets that don't belong to any VIDs on this port */
3681 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
3682 MVPP2_PRS_RI_DROP_MASK);
3683
3684 /* Clear all ai bits for next iteration */
3685 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
3686
3687 /* Update shadow table */
3688 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VID);
3689 mvpp2_prs_hw_write(priv, &pe);
3690}
3691
Marcin Wojtas3f518502014-07-10 16:52:13 -03003692/* Parser default initialization */
3693static int mvpp2_prs_default_init(struct platform_device *pdev,
3694 struct mvpp2 *priv)
3695{
3696 int err, index, i;
3697
3698 /* Enable tcam table */
3699 mvpp2_write(priv, MVPP2_PRS_TCAM_CTRL_REG, MVPP2_PRS_TCAM_EN_MASK);
3700
3701 /* Clear all tcam and sram entries */
3702 for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++) {
3703 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
3704 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
3705 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), 0);
3706
3707 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, index);
3708 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
3709 mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), 0);
3710 }
3711
3712 /* Invalidate all tcam entries */
3713 for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++)
3714 mvpp2_prs_hw_inv(priv, index);
3715
3716 priv->prs_shadow = devm_kcalloc(&pdev->dev, MVPP2_PRS_TCAM_SRAM_SIZE,
Markus Elfring37df25e2017-04-17 09:12:34 +02003717 sizeof(*priv->prs_shadow),
Marcin Wojtas3f518502014-07-10 16:52:13 -03003718 GFP_KERNEL);
3719 if (!priv->prs_shadow)
3720 return -ENOMEM;
3721
3722 /* Always start from lookup = 0 */
3723 for (index = 0; index < MVPP2_MAX_PORTS; index++)
3724 mvpp2_prs_hw_port_init(priv, index, MVPP2_PRS_LU_MH,
3725 MVPP2_PRS_PORT_LU_MAX, 0);
3726
3727 mvpp2_prs_def_flow_init(priv);
3728
3729 mvpp2_prs_mh_init(priv);
3730
3731 mvpp2_prs_mac_init(priv);
3732
3733 mvpp2_prs_dsa_init(priv);
3734
Maxime Chevallier56beda32018-02-28 10:14:13 +01003735 mvpp2_prs_vid_init(priv);
3736
Marcin Wojtas3f518502014-07-10 16:52:13 -03003737 err = mvpp2_prs_etype_init(priv);
3738 if (err)
3739 return err;
3740
3741 err = mvpp2_prs_vlan_init(pdev, priv);
3742 if (err)
3743 return err;
3744
3745 err = mvpp2_prs_pppoe_init(priv);
3746 if (err)
3747 return err;
3748
3749 err = mvpp2_prs_ip6_init(priv);
3750 if (err)
3751 return err;
3752
3753 err = mvpp2_prs_ip4_init(priv);
3754 if (err)
3755 return err;
3756
3757 return 0;
3758}
3759
3760/* Compare MAC DA with tcam entry data */
3761static bool mvpp2_prs_mac_range_equals(struct mvpp2_prs_entry *pe,
3762 const u8 *da, unsigned char *mask)
3763{
3764 unsigned char tcam_byte, tcam_mask;
3765 int index;
3766
3767 for (index = 0; index < ETH_ALEN; index++) {
3768 mvpp2_prs_tcam_data_byte_get(pe, index, &tcam_byte, &tcam_mask);
3769 if (tcam_mask != mask[index])
3770 return false;
3771
3772 if ((tcam_mask & tcam_byte) != (da[index] & mask[index]))
3773 return false;
3774 }
3775
3776 return true;
3777}
3778
3779/* Find tcam entry with matched pair <MAC DA, port> */
3780static struct mvpp2_prs_entry *
3781mvpp2_prs_mac_da_range_find(struct mvpp2 *priv, int pmap, const u8 *da,
3782 unsigned char *mask, int udf_type)
3783{
3784 struct mvpp2_prs_entry *pe;
3785 int tid;
3786
Antoine Tenart239dd4e2017-10-24 11:41:28 +02003787 pe = kzalloc(sizeof(*pe), GFP_ATOMIC);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003788 if (!pe)
3789 return NULL;
3790 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
3791
3792 /* Go through the all entires with MVPP2_PRS_LU_MAC */
3793 for (tid = MVPP2_PE_FIRST_FREE_TID;
3794 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
3795 unsigned int entry_pmap;
3796
3797 if (!priv->prs_shadow[tid].valid ||
3798 (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
3799 (priv->prs_shadow[tid].udf != udf_type))
3800 continue;
3801
3802 pe->index = tid;
3803 mvpp2_prs_hw_read(priv, pe);
3804 entry_pmap = mvpp2_prs_tcam_port_map_get(pe);
3805
3806 if (mvpp2_prs_mac_range_equals(pe, da, mask) &&
3807 entry_pmap == pmap)
3808 return pe;
3809 }
3810 kfree(pe);
3811
3812 return NULL;
3813}
3814
3815/* Update parser's mac da entry */
3816static int mvpp2_prs_mac_da_accept(struct mvpp2 *priv, int port,
3817 const u8 *da, bool add)
3818{
3819 struct mvpp2_prs_entry *pe;
3820 unsigned int pmap, len, ri;
3821 unsigned char mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3822 int tid;
3823
3824 /* Scan TCAM and see if entry with this <MAC DA, port> already exist */
3825 pe = mvpp2_prs_mac_da_range_find(priv, (1 << port), da, mask,
3826 MVPP2_PRS_UDF_MAC_DEF);
3827
3828 /* No such entry */
3829 if (!pe) {
3830 if (!add)
3831 return 0;
3832
3833 /* Create new TCAM entry */
3834 /* Find first range mac entry*/
3835 for (tid = MVPP2_PE_FIRST_FREE_TID;
3836 tid <= MVPP2_PE_LAST_FREE_TID; tid++)
3837 if (priv->prs_shadow[tid].valid &&
3838 (priv->prs_shadow[tid].lu == MVPP2_PRS_LU_MAC) &&
3839 (priv->prs_shadow[tid].udf ==
3840 MVPP2_PRS_UDF_MAC_RANGE))
3841 break;
3842
3843 /* Go through the all entries from first to last */
3844 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3845 tid - 1);
3846 if (tid < 0)
3847 return tid;
3848
Antoine Tenart239dd4e2017-10-24 11:41:28 +02003849 pe = kzalloc(sizeof(*pe), GFP_ATOMIC);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003850 if (!pe)
Amitoj Kaur Chawlac2bb7bc2016-02-04 19:25:26 +05303851 return -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003852 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
3853 pe->index = tid;
3854
3855 /* Mask all ports */
3856 mvpp2_prs_tcam_port_map_set(pe, 0);
3857 }
3858
3859 /* Update port mask */
3860 mvpp2_prs_tcam_port_set(pe, port, add);
3861
3862 /* Invalidate the entry if no ports are left enabled */
3863 pmap = mvpp2_prs_tcam_port_map_get(pe);
3864 if (pmap == 0) {
3865 if (add) {
3866 kfree(pe);
Amitoj Kaur Chawlac2bb7bc2016-02-04 19:25:26 +05303867 return -EINVAL;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003868 }
3869 mvpp2_prs_hw_inv(priv, pe->index);
3870 priv->prs_shadow[pe->index].valid = false;
3871 kfree(pe);
3872 return 0;
3873 }
3874
3875 /* Continue - set next lookup */
3876 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_DSA);
3877
3878 /* Set match on DA */
3879 len = ETH_ALEN;
3880 while (len--)
3881 mvpp2_prs_tcam_data_byte_set(pe, len, da[len], 0xff);
3882
3883 /* Set result info bits */
3884 if (is_broadcast_ether_addr(da))
3885 ri = MVPP2_PRS_RI_L2_BCAST;
3886 else if (is_multicast_ether_addr(da))
3887 ri = MVPP2_PRS_RI_L2_MCAST;
3888 else
3889 ri = MVPP2_PRS_RI_L2_UCAST | MVPP2_PRS_RI_MAC_ME_MASK;
3890
3891 mvpp2_prs_sram_ri_update(pe, ri, MVPP2_PRS_RI_L2_CAST_MASK |
3892 MVPP2_PRS_RI_MAC_ME_MASK);
3893 mvpp2_prs_shadow_ri_set(priv, pe->index, ri, MVPP2_PRS_RI_L2_CAST_MASK |
3894 MVPP2_PRS_RI_MAC_ME_MASK);
3895
3896 /* Shift to ethertype */
3897 mvpp2_prs_sram_shift_set(pe, 2 * ETH_ALEN,
3898 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3899
3900 /* Update shadow table and hw entry */
3901 priv->prs_shadow[pe->index].udf = MVPP2_PRS_UDF_MAC_DEF;
3902 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_MAC);
3903 mvpp2_prs_hw_write(priv, pe);
3904
3905 kfree(pe);
3906
3907 return 0;
3908}
3909
3910static int mvpp2_prs_update_mac_da(struct net_device *dev, const u8 *da)
3911{
3912 struct mvpp2_port *port = netdev_priv(dev);
3913 int err;
3914
3915 /* Remove old parser entry */
3916 err = mvpp2_prs_mac_da_accept(port->priv, port->id, dev->dev_addr,
3917 false);
3918 if (err)
3919 return err;
3920
3921 /* Add new parser entry */
3922 err = mvpp2_prs_mac_da_accept(port->priv, port->id, da, true);
3923 if (err)
3924 return err;
3925
3926 /* Set addr in the device */
3927 ether_addr_copy(dev->dev_addr, da);
3928
3929 return 0;
3930}
3931
3932/* Delete all port's multicast simple (not range) entries */
3933static void mvpp2_prs_mcast_del_all(struct mvpp2 *priv, int port)
3934{
3935 struct mvpp2_prs_entry pe;
3936 int index, tid;
3937
3938 for (tid = MVPP2_PE_FIRST_FREE_TID;
3939 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
3940 unsigned char da[ETH_ALEN], da_mask[ETH_ALEN];
3941
3942 if (!priv->prs_shadow[tid].valid ||
3943 (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
3944 (priv->prs_shadow[tid].udf != MVPP2_PRS_UDF_MAC_DEF))
3945 continue;
3946
3947 /* Only simple mac entries */
3948 pe.index = tid;
3949 mvpp2_prs_hw_read(priv, &pe);
3950
3951 /* Read mac addr from entry */
3952 for (index = 0; index < ETH_ALEN; index++)
3953 mvpp2_prs_tcam_data_byte_get(&pe, index, &da[index],
3954 &da_mask[index]);
3955
3956 if (is_multicast_ether_addr(da) && !is_broadcast_ether_addr(da))
3957 /* Delete this entry */
3958 mvpp2_prs_mac_da_accept(priv, port, da, false);
3959 }
3960}
3961
3962static int mvpp2_prs_tag_mode_set(struct mvpp2 *priv, int port, int type)
3963{
3964 switch (type) {
3965 case MVPP2_TAG_TYPE_EDSA:
3966 /* Add port to EDSA entries */
3967 mvpp2_prs_dsa_tag_set(priv, port, true,
3968 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3969 mvpp2_prs_dsa_tag_set(priv, port, true,
3970 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3971 /* Remove port from DSA entries */
3972 mvpp2_prs_dsa_tag_set(priv, port, false,
3973 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3974 mvpp2_prs_dsa_tag_set(priv, port, false,
3975 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3976 break;
3977
3978 case MVPP2_TAG_TYPE_DSA:
3979 /* Add port to DSA entries */
3980 mvpp2_prs_dsa_tag_set(priv, port, true,
3981 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3982 mvpp2_prs_dsa_tag_set(priv, port, true,
3983 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3984 /* Remove port from EDSA entries */
3985 mvpp2_prs_dsa_tag_set(priv, port, false,
3986 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3987 mvpp2_prs_dsa_tag_set(priv, port, false,
3988 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3989 break;
3990
3991 case MVPP2_TAG_TYPE_MH:
3992 case MVPP2_TAG_TYPE_NONE:
3993 /* Remove port form EDSA and DSA entries */
3994 mvpp2_prs_dsa_tag_set(priv, port, false,
3995 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3996 mvpp2_prs_dsa_tag_set(priv, port, false,
3997 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3998 mvpp2_prs_dsa_tag_set(priv, port, false,
3999 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
4000 mvpp2_prs_dsa_tag_set(priv, port, false,
4001 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
4002 break;
4003
4004 default:
4005 if ((type < 0) || (type > MVPP2_TAG_TYPE_EDSA))
4006 return -EINVAL;
4007 }
4008
4009 return 0;
4010}
4011
4012/* Set prs flow for the port */
4013static int mvpp2_prs_def_flow(struct mvpp2_port *port)
4014{
4015 struct mvpp2_prs_entry *pe;
4016 int tid;
4017
4018 pe = mvpp2_prs_flow_find(port->priv, port->id);
4019
4020 /* Such entry not exist */
4021 if (!pe) {
4022 /* Go through the all entires from last to first */
4023 tid = mvpp2_prs_tcam_first_free(port->priv,
4024 MVPP2_PE_LAST_FREE_TID,
4025 MVPP2_PE_FIRST_FREE_TID);
4026 if (tid < 0)
4027 return tid;
4028
4029 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
4030 if (!pe)
4031 return -ENOMEM;
4032
4033 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
4034 pe->index = tid;
4035
4036 /* Set flow ID*/
4037 mvpp2_prs_sram_ai_update(pe, port->id, MVPP2_PRS_FLOW_ID_MASK);
4038 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
4039
4040 /* Update shadow table */
4041 mvpp2_prs_shadow_set(port->priv, pe->index, MVPP2_PRS_LU_FLOWS);
4042 }
4043
4044 mvpp2_prs_tcam_port_map_set(pe, (1 << port->id));
4045 mvpp2_prs_hw_write(port->priv, pe);
4046 kfree(pe);
4047
4048 return 0;
4049}
4050
4051/* Classifier configuration routines */
4052
4053/* Update classification flow table registers */
4054static void mvpp2_cls_flow_write(struct mvpp2 *priv,
4055 struct mvpp2_cls_flow_entry *fe)
4056{
4057 mvpp2_write(priv, MVPP2_CLS_FLOW_INDEX_REG, fe->index);
4058 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL0_REG, fe->data[0]);
4059 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL1_REG, fe->data[1]);
4060 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL2_REG, fe->data[2]);
4061}
4062
4063/* Update classification lookup table register */
4064static void mvpp2_cls_lookup_write(struct mvpp2 *priv,
4065 struct mvpp2_cls_lookup_entry *le)
4066{
4067 u32 val;
4068
4069 val = (le->way << MVPP2_CLS_LKP_INDEX_WAY_OFFS) | le->lkpid;
4070 mvpp2_write(priv, MVPP2_CLS_LKP_INDEX_REG, val);
4071 mvpp2_write(priv, MVPP2_CLS_LKP_TBL_REG, le->data);
4072}
4073
4074/* Classifier default initialization */
4075static void mvpp2_cls_init(struct mvpp2 *priv)
4076{
4077 struct mvpp2_cls_lookup_entry le;
4078 struct mvpp2_cls_flow_entry fe;
4079 int index;
4080
4081 /* Enable classifier */
4082 mvpp2_write(priv, MVPP2_CLS_MODE_REG, MVPP2_CLS_MODE_ACTIVE_MASK);
4083
4084 /* Clear classifier flow table */
Arnd Bergmanne8f967c2016-11-24 17:28:12 +01004085 memset(&fe.data, 0, sizeof(fe.data));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004086 for (index = 0; index < MVPP2_CLS_FLOWS_TBL_SIZE; index++) {
4087 fe.index = index;
4088 mvpp2_cls_flow_write(priv, &fe);
4089 }
4090
4091 /* Clear classifier lookup table */
4092 le.data = 0;
4093 for (index = 0; index < MVPP2_CLS_LKP_TBL_SIZE; index++) {
4094 le.lkpid = index;
4095 le.way = 0;
4096 mvpp2_cls_lookup_write(priv, &le);
4097
4098 le.way = 1;
4099 mvpp2_cls_lookup_write(priv, &le);
4100 }
4101}
4102
4103static void mvpp2_cls_port_config(struct mvpp2_port *port)
4104{
4105 struct mvpp2_cls_lookup_entry le;
4106 u32 val;
4107
4108 /* Set way for the port */
4109 val = mvpp2_read(port->priv, MVPP2_CLS_PORT_WAY_REG);
4110 val &= ~MVPP2_CLS_PORT_WAY_MASK(port->id);
4111 mvpp2_write(port->priv, MVPP2_CLS_PORT_WAY_REG, val);
4112
4113 /* Pick the entry to be accessed in lookup ID decoding table
4114 * according to the way and lkpid.
4115 */
4116 le.lkpid = port->id;
4117 le.way = 0;
4118 le.data = 0;
4119
4120 /* Set initial CPU queue for receiving packets */
4121 le.data &= ~MVPP2_CLS_LKP_TBL_RXQ_MASK;
4122 le.data |= port->first_rxq;
4123
4124 /* Disable classification engines */
4125 le.data &= ~MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK;
4126
4127 /* Update lookup ID table entry */
4128 mvpp2_cls_lookup_write(port->priv, &le);
4129}
4130
4131/* Set CPU queue number for oversize packets */
4132static void mvpp2_cls_oversize_rxq_set(struct mvpp2_port *port)
4133{
4134 u32 val;
4135
4136 mvpp2_write(port->priv, MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port->id),
4137 port->first_rxq & MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK);
4138
4139 mvpp2_write(port->priv, MVPP2_CLS_SWFWD_P2HQ_REG(port->id),
4140 (port->first_rxq >> MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS));
4141
4142 val = mvpp2_read(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG);
4143 val |= MVPP2_CLS_SWFWD_PCTRL_MASK(port->id);
4144 mvpp2_write(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG, val);
4145}
4146
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004147static void *mvpp2_frag_alloc(const struct mvpp2_bm_pool *pool)
4148{
4149 if (likely(pool->frag_size <= PAGE_SIZE))
4150 return netdev_alloc_frag(pool->frag_size);
4151 else
4152 return kmalloc(pool->frag_size, GFP_ATOMIC);
4153}
4154
4155static void mvpp2_frag_free(const struct mvpp2_bm_pool *pool, void *data)
4156{
4157 if (likely(pool->frag_size <= PAGE_SIZE))
4158 skb_free_frag(data);
4159 else
4160 kfree(data);
4161}
4162
Marcin Wojtas3f518502014-07-10 16:52:13 -03004163/* Buffer Manager configuration routines */
4164
4165/* Create pool */
4166static int mvpp2_bm_pool_create(struct platform_device *pdev,
4167 struct mvpp2 *priv,
4168 struct mvpp2_bm_pool *bm_pool, int size)
4169{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004170 u32 val;
4171
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004172 /* Number of buffer pointers must be a multiple of 16, as per
4173 * hardware constraints
4174 */
4175 if (!IS_ALIGNED(size, 16))
4176 return -EINVAL;
4177
4178 /* PPv2.1 needs 8 bytes per buffer pointer, PPv2.2 needs 16
4179 * bytes per buffer pointer
4180 */
4181 if (priv->hw_version == MVPP21)
4182 bm_pool->size_bytes = 2 * sizeof(u32) * size;
4183 else
4184 bm_pool->size_bytes = 2 * sizeof(u64) * size;
4185
4186 bm_pool->virt_addr = dma_alloc_coherent(&pdev->dev, bm_pool->size_bytes,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004187 &bm_pool->dma_addr,
Marcin Wojtas3f518502014-07-10 16:52:13 -03004188 GFP_KERNEL);
4189 if (!bm_pool->virt_addr)
4190 return -ENOMEM;
4191
Thomas Petazzonid3158802017-02-21 11:28:13 +01004192 if (!IS_ALIGNED((unsigned long)bm_pool->virt_addr,
4193 MVPP2_BM_POOL_PTR_ALIGN)) {
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004194 dma_free_coherent(&pdev->dev, bm_pool->size_bytes,
4195 bm_pool->virt_addr, bm_pool->dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004196 dev_err(&pdev->dev, "BM pool %d is not %d bytes aligned\n",
4197 bm_pool->id, MVPP2_BM_POOL_PTR_ALIGN);
4198 return -ENOMEM;
4199 }
4200
4201 mvpp2_write(priv, MVPP2_BM_POOL_BASE_REG(bm_pool->id),
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004202 lower_32_bits(bm_pool->dma_addr));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004203 mvpp2_write(priv, MVPP2_BM_POOL_SIZE_REG(bm_pool->id), size);
4204
4205 val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
4206 val |= MVPP2_BM_START_MASK;
4207 mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
4208
Marcin Wojtas3f518502014-07-10 16:52:13 -03004209 bm_pool->size = size;
4210 bm_pool->pkt_size = 0;
4211 bm_pool->buf_num = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004212
4213 return 0;
4214}
4215
4216/* Set pool buffer size */
4217static void mvpp2_bm_pool_bufsize_set(struct mvpp2 *priv,
4218 struct mvpp2_bm_pool *bm_pool,
4219 int buf_size)
4220{
4221 u32 val;
4222
4223 bm_pool->buf_size = buf_size;
4224
4225 val = ALIGN(buf_size, 1 << MVPP2_POOL_BUF_SIZE_OFFSET);
4226 mvpp2_write(priv, MVPP2_POOL_BUF_SIZE_REG(bm_pool->id), val);
4227}
4228
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004229static void mvpp2_bm_bufs_get_addrs(struct device *dev, struct mvpp2 *priv,
4230 struct mvpp2_bm_pool *bm_pool,
4231 dma_addr_t *dma_addr,
4232 phys_addr_t *phys_addr)
4233{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004234 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01004235
4236 *dma_addr = mvpp2_percpu_read(priv, cpu,
4237 MVPP2_BM_PHY_ALLOC_REG(bm_pool->id));
4238 *phys_addr = mvpp2_percpu_read(priv, cpu, MVPP2_BM_VIRT_ALLOC_REG);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004239
4240 if (priv->hw_version == MVPP22) {
4241 u32 val;
4242 u32 dma_addr_highbits, phys_addr_highbits;
4243
Thomas Petazzonia7868412017-03-07 16:53:13 +01004244 val = mvpp2_percpu_read(priv, cpu, MVPP22_BM_ADDR_HIGH_ALLOC);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004245 dma_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_PHYS_MASK);
4246 phys_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_VIRT_MASK) >>
4247 MVPP22_BM_ADDR_HIGH_VIRT_SHIFT;
4248
4249 if (sizeof(dma_addr_t) == 8)
4250 *dma_addr |= (u64)dma_addr_highbits << 32;
4251
4252 if (sizeof(phys_addr_t) == 8)
4253 *phys_addr |= (u64)phys_addr_highbits << 32;
4254 }
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004255
4256 put_cpu();
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004257}
4258
Ezequiel Garcia7861f122014-07-21 13:48:14 -03004259/* Free all buffers from the pool */
Marcin Wojtas4229d502015-12-03 15:20:50 +01004260static void mvpp2_bm_bufs_free(struct device *dev, struct mvpp2 *priv,
Stefan Chulskieffbf5f2018-03-05 15:16:51 +01004261 struct mvpp2_bm_pool *bm_pool, int buf_num)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004262{
4263 int i;
4264
Stefan Chulskieffbf5f2018-03-05 15:16:51 +01004265 if (buf_num > bm_pool->buf_num) {
4266 WARN(1, "Pool does not have so many bufs pool(%d) bufs(%d)\n",
4267 bm_pool->id, buf_num);
4268 buf_num = bm_pool->buf_num;
4269 }
4270
4271 for (i = 0; i < buf_num; i++) {
Thomas Petazzoni20396132017-03-07 16:53:00 +01004272 dma_addr_t buf_dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004273 phys_addr_t buf_phys_addr;
4274 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004275
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004276 mvpp2_bm_bufs_get_addrs(dev, priv, bm_pool,
4277 &buf_dma_addr, &buf_phys_addr);
Marcin Wojtas4229d502015-12-03 15:20:50 +01004278
Thomas Petazzoni20396132017-03-07 16:53:00 +01004279 dma_unmap_single(dev, buf_dma_addr,
Marcin Wojtas4229d502015-12-03 15:20:50 +01004280 bm_pool->buf_size, DMA_FROM_DEVICE);
4281
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004282 data = (void *)phys_to_virt(buf_phys_addr);
4283 if (!data)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004284 break;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004285
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004286 mvpp2_frag_free(bm_pool, data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004287 }
4288
4289 /* Update BM driver with number of buffers removed from pool */
4290 bm_pool->buf_num -= i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004291}
4292
Stefan Chulskieffbf5f2018-03-05 15:16:51 +01004293/* Check number of buffers in BM pool */
4294int mvpp2_check_hw_buf_num(struct mvpp2 *priv, struct mvpp2_bm_pool *bm_pool)
4295{
4296 int buf_num = 0;
4297
4298 buf_num += mvpp2_read(priv, MVPP2_BM_POOL_PTRS_NUM_REG(bm_pool->id)) &
4299 MVPP22_BM_POOL_PTRS_NUM_MASK;
4300 buf_num += mvpp2_read(priv, MVPP2_BM_BPPI_PTRS_NUM_REG(bm_pool->id)) &
4301 MVPP2_BM_BPPI_PTR_NUM_MASK;
4302
4303 /* HW has one buffer ready which is not reflected in the counters */
4304 if (buf_num)
4305 buf_num += 1;
4306
4307 return buf_num;
4308}
4309
Marcin Wojtas3f518502014-07-10 16:52:13 -03004310/* Cleanup pool */
4311static int mvpp2_bm_pool_destroy(struct platform_device *pdev,
4312 struct mvpp2 *priv,
4313 struct mvpp2_bm_pool *bm_pool)
4314{
Stefan Chulskieffbf5f2018-03-05 15:16:51 +01004315 int buf_num;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004316 u32 val;
4317
Stefan Chulskieffbf5f2018-03-05 15:16:51 +01004318 buf_num = mvpp2_check_hw_buf_num(priv, bm_pool);
4319 mvpp2_bm_bufs_free(&pdev->dev, priv, bm_pool, buf_num);
4320
4321 /* Check buffer counters after free */
4322 buf_num = mvpp2_check_hw_buf_num(priv, bm_pool);
4323 if (buf_num) {
4324 WARN(1, "cannot free all buffers in pool %d, buf_num left %d\n",
4325 bm_pool->id, bm_pool->buf_num);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004326 return 0;
4327 }
4328
4329 val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
4330 val |= MVPP2_BM_STOP_MASK;
4331 mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
4332
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004333 dma_free_coherent(&pdev->dev, bm_pool->size_bytes,
Marcin Wojtas3f518502014-07-10 16:52:13 -03004334 bm_pool->virt_addr,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004335 bm_pool->dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004336 return 0;
4337}
4338
4339static int mvpp2_bm_pools_init(struct platform_device *pdev,
4340 struct mvpp2 *priv)
4341{
4342 int i, err, size;
4343 struct mvpp2_bm_pool *bm_pool;
4344
4345 /* Create all pools with maximum size */
4346 size = MVPP2_BM_POOL_SIZE_MAX;
4347 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
4348 bm_pool = &priv->bm_pools[i];
4349 bm_pool->id = i;
4350 err = mvpp2_bm_pool_create(pdev, priv, bm_pool, size);
4351 if (err)
4352 goto err_unroll_pools;
4353 mvpp2_bm_pool_bufsize_set(priv, bm_pool, 0);
4354 }
4355 return 0;
4356
4357err_unroll_pools:
4358 dev_err(&pdev->dev, "failed to create BM pool %d, size %d\n", i, size);
4359 for (i = i - 1; i >= 0; i--)
4360 mvpp2_bm_pool_destroy(pdev, priv, &priv->bm_pools[i]);
4361 return err;
4362}
4363
4364static int mvpp2_bm_init(struct platform_device *pdev, struct mvpp2 *priv)
4365{
4366 int i, err;
4367
4368 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
4369 /* Mask BM all interrupts */
4370 mvpp2_write(priv, MVPP2_BM_INTR_MASK_REG(i), 0);
4371 /* Clear BM cause register */
4372 mvpp2_write(priv, MVPP2_BM_INTR_CAUSE_REG(i), 0);
4373 }
4374
4375 /* Allocate and initialize BM pools */
4376 priv->bm_pools = devm_kcalloc(&pdev->dev, MVPP2_BM_POOLS_NUM,
Markus Elfring81f915e2017-04-17 09:06:33 +02004377 sizeof(*priv->bm_pools), GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004378 if (!priv->bm_pools)
4379 return -ENOMEM;
4380
4381 err = mvpp2_bm_pools_init(pdev, priv);
4382 if (err < 0)
4383 return err;
4384 return 0;
4385}
4386
Stefan Chulski01d04932018-03-05 15:16:50 +01004387static void mvpp2_setup_bm_pool(void)
4388{
4389 /* Short pool */
4390 mvpp2_pools[MVPP2_BM_SHORT].buf_num = MVPP2_BM_SHORT_BUF_NUM;
4391 mvpp2_pools[MVPP2_BM_SHORT].pkt_size = MVPP2_BM_SHORT_PKT_SIZE;
4392
4393 /* Long pool */
4394 mvpp2_pools[MVPP2_BM_LONG].buf_num = MVPP2_BM_LONG_BUF_NUM;
4395 mvpp2_pools[MVPP2_BM_LONG].pkt_size = MVPP2_BM_LONG_PKT_SIZE;
4396}
4397
Marcin Wojtas3f518502014-07-10 16:52:13 -03004398/* Attach long pool to rxq */
4399static void mvpp2_rxq_long_pool_set(struct mvpp2_port *port,
4400 int lrxq, int long_pool)
4401{
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004402 u32 val, mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004403 int prxq;
4404
4405 /* Get queue physical ID */
4406 prxq = port->rxqs[lrxq]->id;
4407
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004408 if (port->priv->hw_version == MVPP21)
4409 mask = MVPP21_RXQ_POOL_LONG_MASK;
4410 else
4411 mask = MVPP22_RXQ_POOL_LONG_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004412
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004413 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
4414 val &= ~mask;
4415 val |= (long_pool << MVPP2_RXQ_POOL_LONG_OFFS) & mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004416 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
4417}
4418
4419/* Attach short pool to rxq */
4420static void mvpp2_rxq_short_pool_set(struct mvpp2_port *port,
4421 int lrxq, int short_pool)
4422{
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004423 u32 val, mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004424 int prxq;
4425
4426 /* Get queue physical ID */
4427 prxq = port->rxqs[lrxq]->id;
4428
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004429 if (port->priv->hw_version == MVPP21)
4430 mask = MVPP21_RXQ_POOL_SHORT_MASK;
4431 else
4432 mask = MVPP22_RXQ_POOL_SHORT_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004433
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004434 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
4435 val &= ~mask;
4436 val |= (short_pool << MVPP2_RXQ_POOL_SHORT_OFFS) & mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004437 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
4438}
4439
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004440static void *mvpp2_buf_alloc(struct mvpp2_port *port,
4441 struct mvpp2_bm_pool *bm_pool,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004442 dma_addr_t *buf_dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004443 phys_addr_t *buf_phys_addr,
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004444 gfp_t gfp_mask)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004445{
Thomas Petazzoni20396132017-03-07 16:53:00 +01004446 dma_addr_t dma_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004447 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004448
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004449 data = mvpp2_frag_alloc(bm_pool);
4450 if (!data)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004451 return NULL;
4452
Thomas Petazzoni20396132017-03-07 16:53:00 +01004453 dma_addr = dma_map_single(port->dev->dev.parent, data,
4454 MVPP2_RX_BUF_SIZE(bm_pool->pkt_size),
4455 DMA_FROM_DEVICE);
4456 if (unlikely(dma_mapping_error(port->dev->dev.parent, dma_addr))) {
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004457 mvpp2_frag_free(bm_pool, data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004458 return NULL;
4459 }
Thomas Petazzoni20396132017-03-07 16:53:00 +01004460 *buf_dma_addr = dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004461 *buf_phys_addr = virt_to_phys(data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004462
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004463 return data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004464}
4465
Marcin Wojtas3f518502014-07-10 16:52:13 -03004466/* Release buffer to BM */
4467static inline void mvpp2_bm_pool_put(struct mvpp2_port *port, int pool,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004468 dma_addr_t buf_dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004469 phys_addr_t buf_phys_addr)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004470{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004471 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01004472
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004473 if (port->priv->hw_version == MVPP22) {
4474 u32 val = 0;
4475
4476 if (sizeof(dma_addr_t) == 8)
4477 val |= upper_32_bits(buf_dma_addr) &
4478 MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK;
4479
4480 if (sizeof(phys_addr_t) == 8)
4481 val |= (upper_32_bits(buf_phys_addr)
4482 << MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT) &
4483 MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK;
4484
Thomas Petazzonia7868412017-03-07 16:53:13 +01004485 mvpp2_percpu_write(port->priv, cpu,
4486 MVPP22_BM_ADDR_HIGH_RLS_REG, val);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004487 }
4488
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004489 /* MVPP2_BM_VIRT_RLS_REG is not interpreted by HW, and simply
4490 * returned in the "cookie" field of the RX
4491 * descriptor. Instead of storing the virtual address, we
4492 * store the physical address
4493 */
Thomas Petazzonia7868412017-03-07 16:53:13 +01004494 mvpp2_percpu_write(port->priv, cpu,
4495 MVPP2_BM_VIRT_RLS_REG, buf_phys_addr);
4496 mvpp2_percpu_write(port->priv, cpu,
4497 MVPP2_BM_PHY_RLS_REG(pool), buf_dma_addr);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004498
4499 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03004500}
4501
Marcin Wojtas3f518502014-07-10 16:52:13 -03004502/* Allocate buffers for the pool */
4503static int mvpp2_bm_bufs_add(struct mvpp2_port *port,
4504 struct mvpp2_bm_pool *bm_pool, int buf_num)
4505{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004506 int i, buf_size, total_size;
Thomas Petazzoni20396132017-03-07 16:53:00 +01004507 dma_addr_t dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004508 phys_addr_t phys_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004509 void *buf;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004510
4511 buf_size = MVPP2_RX_BUF_SIZE(bm_pool->pkt_size);
4512 total_size = MVPP2_RX_TOTAL_SIZE(buf_size);
4513
4514 if (buf_num < 0 ||
4515 (buf_num + bm_pool->buf_num > bm_pool->size)) {
4516 netdev_err(port->dev,
4517 "cannot allocate %d buffers for pool %d\n",
4518 buf_num, bm_pool->id);
4519 return 0;
4520 }
4521
Marcin Wojtas3f518502014-07-10 16:52:13 -03004522 for (i = 0; i < buf_num; i++) {
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004523 buf = mvpp2_buf_alloc(port, bm_pool, &dma_addr,
4524 &phys_addr, GFP_KERNEL);
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004525 if (!buf)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004526 break;
4527
Thomas Petazzoni20396132017-03-07 16:53:00 +01004528 mvpp2_bm_pool_put(port, bm_pool->id, dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004529 phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004530 }
4531
4532 /* Update BM driver with number of buffers added to pool */
4533 bm_pool->buf_num += i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004534
4535 netdev_dbg(port->dev,
Stefan Chulski01d04932018-03-05 15:16:50 +01004536 "pool %d: pkt_size=%4d, buf_size=%4d, total_size=%4d\n",
Marcin Wojtas3f518502014-07-10 16:52:13 -03004537 bm_pool->id, bm_pool->pkt_size, buf_size, total_size);
4538
4539 netdev_dbg(port->dev,
Stefan Chulski01d04932018-03-05 15:16:50 +01004540 "pool %d: %d of %d buffers added\n",
Marcin Wojtas3f518502014-07-10 16:52:13 -03004541 bm_pool->id, i, buf_num);
4542 return i;
4543}
4544
4545/* Notify the driver that BM pool is being used as specific type and return the
4546 * pool pointer on success
4547 */
4548static struct mvpp2_bm_pool *
Stefan Chulski01d04932018-03-05 15:16:50 +01004549mvpp2_bm_pool_use(struct mvpp2_port *port, unsigned pool, int pkt_size)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004550{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004551 struct mvpp2_bm_pool *new_pool = &port->priv->bm_pools[pool];
4552 int num;
4553
Stefan Chulski01d04932018-03-05 15:16:50 +01004554 if (pool >= MVPP2_BM_POOLS_NUM) {
4555 netdev_err(port->dev, "Invalid pool %d\n", pool);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004556 return NULL;
4557 }
4558
Marcin Wojtas3f518502014-07-10 16:52:13 -03004559 /* Allocate buffers in case BM pool is used as long pool, but packet
4560 * size doesn't match MTU or BM pool hasn't being used yet
4561 */
Stefan Chulski01d04932018-03-05 15:16:50 +01004562 if (new_pool->pkt_size == 0) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004563 int pkts_num;
4564
4565 /* Set default buffer number or free all the buffers in case
4566 * the pool is not empty
4567 */
4568 pkts_num = new_pool->buf_num;
4569 if (pkts_num == 0)
Stefan Chulski01d04932018-03-05 15:16:50 +01004570 pkts_num = mvpp2_pools[pool].buf_num;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004571 else
Marcin Wojtas4229d502015-12-03 15:20:50 +01004572 mvpp2_bm_bufs_free(port->dev->dev.parent,
Stefan Chulskieffbf5f2018-03-05 15:16:51 +01004573 port->priv, new_pool, pkts_num);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004574
4575 new_pool->pkt_size = pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004576 new_pool->frag_size =
4577 SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
4578 MVPP2_SKB_SHINFO_SIZE;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004579
4580 /* Allocate buffers for this pool */
4581 num = mvpp2_bm_bufs_add(port, new_pool, pkts_num);
4582 if (num != pkts_num) {
4583 WARN(1, "pool %d: %d of %d allocated\n",
4584 new_pool->id, num, pkts_num);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004585 return NULL;
4586 }
4587 }
4588
4589 mvpp2_bm_pool_bufsize_set(port->priv, new_pool,
4590 MVPP2_RX_BUF_SIZE(new_pool->pkt_size));
4591
Marcin Wojtas3f518502014-07-10 16:52:13 -03004592 return new_pool;
4593}
4594
4595/* Initialize pools for swf */
4596static int mvpp2_swf_bm_pool_init(struct mvpp2_port *port)
4597{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004598 int rxq;
4599
4600 if (!port->pool_long) {
4601 port->pool_long =
Stefan Chulski01d04932018-03-05 15:16:50 +01004602 mvpp2_bm_pool_use(port, MVPP2_BM_LONG,
4603 mvpp2_pools[MVPP2_BM_LONG].pkt_size);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004604 if (!port->pool_long)
4605 return -ENOMEM;
4606
Marcin Wojtas3f518502014-07-10 16:52:13 -03004607 port->pool_long->port_map |= (1 << port->id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004608
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004609 for (rxq = 0; rxq < port->nrxqs; rxq++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004610 mvpp2_rxq_long_pool_set(port, rxq, port->pool_long->id);
4611 }
4612
4613 if (!port->pool_short) {
4614 port->pool_short =
Stefan Chulski01d04932018-03-05 15:16:50 +01004615 mvpp2_bm_pool_use(port, MVPP2_BM_SHORT,
4616 mvpp2_pools[MVPP2_BM_SHORT].pkt_size);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004617 if (!port->pool_short)
4618 return -ENOMEM;
4619
Marcin Wojtas3f518502014-07-10 16:52:13 -03004620 port->pool_short->port_map |= (1 << port->id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004621
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004622 for (rxq = 0; rxq < port->nrxqs; rxq++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004623 mvpp2_rxq_short_pool_set(port, rxq,
4624 port->pool_short->id);
4625 }
4626
4627 return 0;
4628}
4629
4630static int mvpp2_bm_update_mtu(struct net_device *dev, int mtu)
4631{
4632 struct mvpp2_port *port = netdev_priv(dev);
4633 struct mvpp2_bm_pool *port_pool = port->pool_long;
4634 int num, pkts_num = port_pool->buf_num;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004635
4636 /* Update BM pool with new buffer size */
Stefan Chulskieffbf5f2018-03-05 15:16:51 +01004637 mvpp2_bm_bufs_free(dev->dev.parent, port->priv, port_pool,
4638 port_pool->buf_num);
Ezequiel Garciad74c96c2014-07-21 13:48:13 -03004639 if (port_pool->buf_num) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004640 WARN(1, "cannot free all buffers in pool %d\n", port_pool->id);
4641 return -EIO;
4642 }
4643
Marcin Wojtas3f518502014-07-10 16:52:13 -03004644 num = mvpp2_bm_bufs_add(port, port_pool, pkts_num);
4645 if (num != pkts_num) {
4646 WARN(1, "pool %d: %d of %d allocated\n",
4647 port_pool->id, num, pkts_num);
4648 return -EIO;
4649 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004650 dev->mtu = mtu;
4651 netdev_update_features(dev);
4652 return 0;
4653}
4654
4655static inline void mvpp2_interrupts_enable(struct mvpp2_port *port)
4656{
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004657 int i, sw_thread_mask = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004658
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004659 for (i = 0; i < port->nqvecs; i++)
4660 sw_thread_mask |= port->qvecs[i].sw_thread_mask;
4661
Marcin Wojtas3f518502014-07-10 16:52:13 -03004662 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004663 MVPP2_ISR_ENABLE_INTERRUPT(sw_thread_mask));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004664}
4665
4666static inline void mvpp2_interrupts_disable(struct mvpp2_port *port)
4667{
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004668 int i, sw_thread_mask = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004669
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004670 for (i = 0; i < port->nqvecs; i++)
4671 sw_thread_mask |= port->qvecs[i].sw_thread_mask;
4672
Marcin Wojtas3f518502014-07-10 16:52:13 -03004673 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004674 MVPP2_ISR_DISABLE_INTERRUPT(sw_thread_mask));
4675}
4676
4677static inline void mvpp2_qvec_interrupt_enable(struct mvpp2_queue_vector *qvec)
4678{
4679 struct mvpp2_port *port = qvec->port;
4680
4681 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
4682 MVPP2_ISR_ENABLE_INTERRUPT(qvec->sw_thread_mask));
4683}
4684
4685static inline void mvpp2_qvec_interrupt_disable(struct mvpp2_queue_vector *qvec)
4686{
4687 struct mvpp2_port *port = qvec->port;
4688
4689 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
4690 MVPP2_ISR_DISABLE_INTERRUPT(qvec->sw_thread_mask));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004691}
4692
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004693/* Mask the current CPU's Rx/Tx interrupts
4694 * Called by on_each_cpu(), guaranteed to run with migration disabled,
4695 * using smp_processor_id() is OK.
4696 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004697static void mvpp2_interrupts_mask(void *arg)
4698{
4699 struct mvpp2_port *port = arg;
4700
Thomas Petazzonia7868412017-03-07 16:53:13 +01004701 mvpp2_percpu_write(port->priv, smp_processor_id(),
4702 MVPP2_ISR_RX_TX_MASK_REG(port->id), 0);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004703}
4704
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004705/* Unmask the current CPU's Rx/Tx interrupts.
4706 * Called by on_each_cpu(), guaranteed to run with migration disabled,
4707 * using smp_processor_id() is OK.
4708 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004709static void mvpp2_interrupts_unmask(void *arg)
4710{
4711 struct mvpp2_port *port = arg;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02004712 u32 val;
4713
4714 val = MVPP2_CAUSE_MISC_SUM_MASK |
4715 MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
4716 if (port->has_tx_irqs)
4717 val |= MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004718
Thomas Petazzonia7868412017-03-07 16:53:13 +01004719 mvpp2_percpu_write(port->priv, smp_processor_id(),
Thomas Petazzoni213f4282017-08-03 10:42:00 +02004720 MVPP2_ISR_RX_TX_MASK_REG(port->id), val);
4721}
4722
4723static void
4724mvpp2_shared_interrupt_mask_unmask(struct mvpp2_port *port, bool mask)
4725{
4726 u32 val;
4727 int i;
4728
4729 if (port->priv->hw_version != MVPP22)
4730 return;
4731
4732 if (mask)
4733 val = 0;
4734 else
4735 val = MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
4736
4737 for (i = 0; i < port->nqvecs; i++) {
4738 struct mvpp2_queue_vector *v = port->qvecs + i;
4739
4740 if (v->type != MVPP2_QUEUE_VECTOR_SHARED)
4741 continue;
4742
4743 mvpp2_percpu_write(port->priv, v->sw_thread_id,
4744 MVPP2_ISR_RX_TX_MASK_REG(port->id), val);
4745 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004746}
4747
4748/* Port configuration routines */
4749
Antoine Ténartf84bf382017-08-22 19:08:27 +02004750static void mvpp22_gop_init_rgmii(struct mvpp2_port *port)
4751{
4752 struct mvpp2 *priv = port->priv;
4753 u32 val;
4754
4755 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
4756 val |= GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT;
4757 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
4758
4759 regmap_read(priv->sysctrl_base, GENCONF_CTRL0, &val);
4760 if (port->gop_id == 2)
4761 val |= GENCONF_CTRL0_PORT0_RGMII | GENCONF_CTRL0_PORT1_RGMII;
4762 else if (port->gop_id == 3)
4763 val |= GENCONF_CTRL0_PORT1_RGMII_MII;
4764 regmap_write(priv->sysctrl_base, GENCONF_CTRL0, val);
4765}
4766
4767static void mvpp22_gop_init_sgmii(struct mvpp2_port *port)
4768{
4769 struct mvpp2 *priv = port->priv;
4770 u32 val;
4771
4772 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
4773 val |= GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT |
4774 GENCONF_PORT_CTRL0_RX_DATA_SAMPLE;
4775 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
4776
4777 if (port->gop_id > 1) {
4778 regmap_read(priv->sysctrl_base, GENCONF_CTRL0, &val);
4779 if (port->gop_id == 2)
4780 val &= ~GENCONF_CTRL0_PORT0_RGMII;
4781 else if (port->gop_id == 3)
4782 val &= ~GENCONF_CTRL0_PORT1_RGMII_MII;
4783 regmap_write(priv->sysctrl_base, GENCONF_CTRL0, val);
4784 }
4785}
4786
4787static void mvpp22_gop_init_10gkr(struct mvpp2_port *port)
4788{
4789 struct mvpp2 *priv = port->priv;
4790 void __iomem *mpcs = priv->iface_base + MVPP22_MPCS_BASE(port->gop_id);
4791 void __iomem *xpcs = priv->iface_base + MVPP22_XPCS_BASE(port->gop_id);
4792 u32 val;
4793
4794 /* XPCS */
4795 val = readl(xpcs + MVPP22_XPCS_CFG0);
4796 val &= ~(MVPP22_XPCS_CFG0_PCS_MODE(0x3) |
4797 MVPP22_XPCS_CFG0_ACTIVE_LANE(0x3));
4798 val |= MVPP22_XPCS_CFG0_ACTIVE_LANE(2);
4799 writel(val, xpcs + MVPP22_XPCS_CFG0);
4800
4801 /* MPCS */
4802 val = readl(mpcs + MVPP22_MPCS_CTRL);
4803 val &= ~MVPP22_MPCS_CTRL_FWD_ERR_CONN;
4804 writel(val, mpcs + MVPP22_MPCS_CTRL);
4805
4806 val = readl(mpcs + MVPP22_MPCS_CLK_RESET);
4807 val &= ~(MVPP22_MPCS_CLK_RESET_DIV_RATIO(0x7) | MAC_CLK_RESET_MAC |
4808 MAC_CLK_RESET_SD_RX | MAC_CLK_RESET_SD_TX);
4809 val |= MVPP22_MPCS_CLK_RESET_DIV_RATIO(1);
4810 writel(val, mpcs + MVPP22_MPCS_CLK_RESET);
4811
4812 val &= ~MVPP22_MPCS_CLK_RESET_DIV_SET;
4813 val |= MAC_CLK_RESET_MAC | MAC_CLK_RESET_SD_RX | MAC_CLK_RESET_SD_TX;
4814 writel(val, mpcs + MVPP22_MPCS_CLK_RESET);
4815}
4816
4817static int mvpp22_gop_init(struct mvpp2_port *port)
4818{
4819 struct mvpp2 *priv = port->priv;
4820 u32 val;
4821
4822 if (!priv->sysctrl_base)
4823 return 0;
4824
4825 switch (port->phy_interface) {
4826 case PHY_INTERFACE_MODE_RGMII:
4827 case PHY_INTERFACE_MODE_RGMII_ID:
4828 case PHY_INTERFACE_MODE_RGMII_RXID:
4829 case PHY_INTERFACE_MODE_RGMII_TXID:
4830 if (port->gop_id == 0)
4831 goto invalid_conf;
4832 mvpp22_gop_init_rgmii(port);
4833 break;
4834 case PHY_INTERFACE_MODE_SGMII:
4835 mvpp22_gop_init_sgmii(port);
4836 break;
4837 case PHY_INTERFACE_MODE_10GKR:
4838 if (port->gop_id != 0)
4839 goto invalid_conf;
4840 mvpp22_gop_init_10gkr(port);
4841 break;
4842 default:
4843 goto unsupported_conf;
4844 }
4845
4846 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL1, &val);
4847 val |= GENCONF_PORT_CTRL1_RESET(port->gop_id) |
4848 GENCONF_PORT_CTRL1_EN(port->gop_id);
4849 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL1, val);
4850
4851 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
4852 val |= GENCONF_PORT_CTRL0_CLK_DIV_PHASE_CLR;
4853 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
4854
4855 regmap_read(priv->sysctrl_base, GENCONF_SOFT_RESET1, &val);
4856 val |= GENCONF_SOFT_RESET1_GOP;
4857 regmap_write(priv->sysctrl_base, GENCONF_SOFT_RESET1, val);
4858
4859unsupported_conf:
4860 return 0;
4861
4862invalid_conf:
4863 netdev_err(port->dev, "Invalid port configuration\n");
4864 return -EINVAL;
4865}
4866
Antoine Tenartfd3651b2017-09-01 11:04:54 +02004867static void mvpp22_gop_unmask_irq(struct mvpp2_port *port)
4868{
4869 u32 val;
4870
4871 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
4872 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4873 /* Enable the GMAC link status irq for this port */
4874 val = readl(port->base + MVPP22_GMAC_INT_SUM_MASK);
4875 val |= MVPP22_GMAC_INT_SUM_MASK_LINK_STAT;
4876 writel(val, port->base + MVPP22_GMAC_INT_SUM_MASK);
4877 }
4878
4879 if (port->gop_id == 0) {
4880 /* Enable the XLG/GIG irqs for this port */
4881 val = readl(port->base + MVPP22_XLG_EXT_INT_MASK);
4882 if (port->phy_interface == PHY_INTERFACE_MODE_10GKR)
4883 val |= MVPP22_XLG_EXT_INT_MASK_XLG;
4884 else
4885 val |= MVPP22_XLG_EXT_INT_MASK_GIG;
4886 writel(val, port->base + MVPP22_XLG_EXT_INT_MASK);
4887 }
4888}
4889
4890static void mvpp22_gop_mask_irq(struct mvpp2_port *port)
4891{
4892 u32 val;
4893
4894 if (port->gop_id == 0) {
4895 val = readl(port->base + MVPP22_XLG_EXT_INT_MASK);
4896 val &= ~(MVPP22_XLG_EXT_INT_MASK_XLG |
4897 MVPP22_XLG_EXT_INT_MASK_GIG);
4898 writel(val, port->base + MVPP22_XLG_EXT_INT_MASK);
4899 }
4900
4901 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
4902 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4903 val = readl(port->base + MVPP22_GMAC_INT_SUM_MASK);
4904 val &= ~MVPP22_GMAC_INT_SUM_MASK_LINK_STAT;
4905 writel(val, port->base + MVPP22_GMAC_INT_SUM_MASK);
4906 }
4907}
4908
4909static void mvpp22_gop_setup_irq(struct mvpp2_port *port)
4910{
4911 u32 val;
4912
4913 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
4914 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4915 val = readl(port->base + MVPP22_GMAC_INT_MASK);
4916 val |= MVPP22_GMAC_INT_MASK_LINK_STAT;
4917 writel(val, port->base + MVPP22_GMAC_INT_MASK);
4918 }
4919
4920 if (port->gop_id == 0) {
4921 val = readl(port->base + MVPP22_XLG_INT_MASK);
4922 val |= MVPP22_XLG_INT_MASK_LINK;
4923 writel(val, port->base + MVPP22_XLG_INT_MASK);
4924 }
4925
4926 mvpp22_gop_unmask_irq(port);
4927}
4928
Antoine Tenart542897d2017-08-30 10:29:15 +02004929static int mvpp22_comphy_init(struct mvpp2_port *port)
4930{
4931 enum phy_mode mode;
4932 int ret;
4933
4934 if (!port->comphy)
4935 return 0;
4936
4937 switch (port->phy_interface) {
4938 case PHY_INTERFACE_MODE_SGMII:
4939 mode = PHY_MODE_SGMII;
4940 break;
4941 case PHY_INTERFACE_MODE_10GKR:
4942 mode = PHY_MODE_10GKR;
4943 break;
4944 default:
4945 return -EINVAL;
4946 }
4947
4948 ret = phy_set_mode(port->comphy, mode);
4949 if (ret)
4950 return ret;
4951
4952 return phy_power_on(port->comphy);
4953}
4954
Antoine Ténart39193572017-08-22 19:08:24 +02004955static void mvpp2_port_mii_gmac_configure_mode(struct mvpp2_port *port)
4956{
4957 u32 val;
4958
4959 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4960 val = readl(port->base + MVPP22_GMAC_CTRL_4_REG);
4961 val |= MVPP22_CTRL4_SYNC_BYPASS_DIS | MVPP22_CTRL4_DP_CLK_SEL |
4962 MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
4963 val &= ~MVPP22_CTRL4_EXT_PIN_GMII_SEL;
4964 writel(val, port->base + MVPP22_GMAC_CTRL_4_REG);
Antoine Tenart1df22702017-09-01 11:04:52 +02004965 } else if (phy_interface_mode_is_rgmii(port->phy_interface)) {
Antoine Ténart39193572017-08-22 19:08:24 +02004966 val = readl(port->base + MVPP22_GMAC_CTRL_4_REG);
4967 val |= MVPP22_CTRL4_EXT_PIN_GMII_SEL |
4968 MVPP22_CTRL4_SYNC_BYPASS_DIS |
4969 MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
4970 val &= ~MVPP22_CTRL4_DP_CLK_SEL;
4971 writel(val, port->base + MVPP22_GMAC_CTRL_4_REG);
Antoine Ténart39193572017-08-22 19:08:24 +02004972 }
4973
4974 /* The port is connected to a copper PHY */
4975 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4976 val &= ~MVPP2_GMAC_PORT_TYPE_MASK;
4977 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4978
4979 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4980 val |= MVPP2_GMAC_IN_BAND_AUTONEG_BYPASS |
4981 MVPP2_GMAC_AN_SPEED_EN | MVPP2_GMAC_FLOW_CTRL_AUTONEG |
4982 MVPP2_GMAC_AN_DUPLEX_EN;
4983 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
4984 val |= MVPP2_GMAC_IN_BAND_AUTONEG;
4985 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4986}
4987
4988static void mvpp2_port_mii_gmac_configure(struct mvpp2_port *port)
4989{
4990 u32 val;
4991
4992 /* Force link down */
4993 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4994 val &= ~MVPP2_GMAC_FORCE_LINK_PASS;
4995 val |= MVPP2_GMAC_FORCE_LINK_DOWN;
4996 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4997
4998 /* Set the GMAC in a reset state */
4999 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
5000 val |= MVPP2_GMAC_PORT_RESET_MASK;
5001 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
5002
5003 /* Configure the PCS and in-band AN */
5004 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
5005 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
5006 val |= MVPP2_GMAC_INBAND_AN_MASK | MVPP2_GMAC_PCS_ENABLE_MASK;
Antoine Tenart1df22702017-09-01 11:04:52 +02005007 } else if (phy_interface_mode_is_rgmii(port->phy_interface)) {
Antoine Ténart39193572017-08-22 19:08:24 +02005008 val &= ~MVPP2_GMAC_PCS_ENABLE_MASK;
Antoine Ténart39193572017-08-22 19:08:24 +02005009 }
5010 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
5011
5012 mvpp2_port_mii_gmac_configure_mode(port);
5013
5014 /* Unset the GMAC reset state */
5015 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
5016 val &= ~MVPP2_GMAC_PORT_RESET_MASK;
5017 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
5018
5019 /* Stop forcing link down */
5020 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5021 val &= ~MVPP2_GMAC_FORCE_LINK_DOWN;
5022 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5023}
5024
Antoine Ténart77321952017-08-22 19:08:25 +02005025static void mvpp2_port_mii_xlg_configure(struct mvpp2_port *port)
5026{
5027 u32 val;
5028
5029 if (port->gop_id != 0)
5030 return;
5031
5032 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
5033 val |= MVPP22_XLG_CTRL0_RX_FLOW_CTRL_EN;
5034 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
5035
5036 val = readl(port->base + MVPP22_XLG_CTRL4_REG);
5037 val &= ~MVPP22_XLG_CTRL4_MACMODSELECT_GMAC;
5038 val |= MVPP22_XLG_CTRL4_FWD_FC | MVPP22_XLG_CTRL4_FWD_PFC;
5039 writel(val, port->base + MVPP22_XLG_CTRL4_REG);
5040}
5041
Thomas Petazzoni26975822017-03-07 16:53:14 +01005042static void mvpp22_port_mii_set(struct mvpp2_port *port)
5043{
5044 u32 val;
5045
Thomas Petazzoni26975822017-03-07 16:53:14 +01005046 /* Only GOP port 0 has an XLG MAC */
5047 if (port->gop_id == 0) {
5048 val = readl(port->base + MVPP22_XLG_CTRL3_REG);
5049 val &= ~MVPP22_XLG_CTRL3_MACMODESELECT_MASK;
Antoine Ténart725757a2017-06-12 16:01:39 +02005050
5051 if (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
5052 port->phy_interface == PHY_INTERFACE_MODE_10GKR)
5053 val |= MVPP22_XLG_CTRL3_MACMODESELECT_10G;
5054 else
5055 val |= MVPP22_XLG_CTRL3_MACMODESELECT_GMAC;
5056
Thomas Petazzoni26975822017-03-07 16:53:14 +01005057 writel(val, port->base + MVPP22_XLG_CTRL3_REG);
5058 }
Thomas Petazzoni26975822017-03-07 16:53:14 +01005059}
5060
Marcin Wojtas3f518502014-07-10 16:52:13 -03005061static void mvpp2_port_mii_set(struct mvpp2_port *port)
5062{
Thomas Petazzoni26975822017-03-07 16:53:14 +01005063 if (port->priv->hw_version == MVPP22)
5064 mvpp22_port_mii_set(port);
5065
Antoine Tenart1df22702017-09-01 11:04:52 +02005066 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
Antoine Ténart39193572017-08-22 19:08:24 +02005067 port->phy_interface == PHY_INTERFACE_MODE_SGMII)
5068 mvpp2_port_mii_gmac_configure(port);
Antoine Ténart77321952017-08-22 19:08:25 +02005069 else if (port->phy_interface == PHY_INTERFACE_MODE_10GKR)
5070 mvpp2_port_mii_xlg_configure(port);
Marcin Wojtas08a23752014-07-21 13:48:12 -03005071}
5072
5073static void mvpp2_port_fc_adv_enable(struct mvpp2_port *port)
5074{
5075 u32 val;
5076
5077 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5078 val |= MVPP2_GMAC_FC_ADV_EN;
5079 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005080}
5081
5082static void mvpp2_port_enable(struct mvpp2_port *port)
5083{
5084 u32 val;
5085
Antoine Ténart725757a2017-06-12 16:01:39 +02005086 /* Only GOP port 0 has an XLG MAC */
5087 if (port->gop_id == 0 &&
5088 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
5089 port->phy_interface == PHY_INTERFACE_MODE_10GKR)) {
5090 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
5091 val |= MVPP22_XLG_CTRL0_PORT_EN |
5092 MVPP22_XLG_CTRL0_MAC_RESET_DIS;
5093 val &= ~MVPP22_XLG_CTRL0_MIB_CNT_DIS;
5094 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
5095 } else {
5096 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
5097 val |= MVPP2_GMAC_PORT_EN_MASK;
5098 val |= MVPP2_GMAC_MIB_CNTR_EN_MASK;
5099 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
5100 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03005101}
5102
5103static void mvpp2_port_disable(struct mvpp2_port *port)
5104{
5105 u32 val;
5106
Antoine Ténart725757a2017-06-12 16:01:39 +02005107 /* Only GOP port 0 has an XLG MAC */
5108 if (port->gop_id == 0 &&
5109 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
5110 port->phy_interface == PHY_INTERFACE_MODE_10GKR)) {
5111 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
5112 val &= ~(MVPP22_XLG_CTRL0_PORT_EN |
5113 MVPP22_XLG_CTRL0_MAC_RESET_DIS);
5114 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
5115 } else {
5116 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
5117 val &= ~(MVPP2_GMAC_PORT_EN_MASK);
5118 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
5119 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03005120}
5121
5122/* Set IEEE 802.3x Flow Control Xon Packet Transmission Mode */
5123static void mvpp2_port_periodic_xon_disable(struct mvpp2_port *port)
5124{
5125 u32 val;
5126
5127 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG) &
5128 ~MVPP2_GMAC_PERIODIC_XON_EN_MASK;
5129 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
5130}
5131
5132/* Configure loopback port */
5133static void mvpp2_port_loopback_set(struct mvpp2_port *port)
5134{
5135 u32 val;
5136
5137 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
5138
5139 if (port->speed == 1000)
5140 val |= MVPP2_GMAC_GMII_LB_EN_MASK;
5141 else
5142 val &= ~MVPP2_GMAC_GMII_LB_EN_MASK;
5143
5144 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
5145 val |= MVPP2_GMAC_PCS_LB_EN_MASK;
5146 else
5147 val &= ~MVPP2_GMAC_PCS_LB_EN_MASK;
5148
5149 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
5150}
5151
Miquel Raynal118d6292017-11-06 22:56:53 +01005152struct mvpp2_ethtool_counter {
5153 unsigned int offset;
5154 const char string[ETH_GSTRING_LEN];
5155 bool reg_is_64b;
5156};
5157
5158static u64 mvpp2_read_count(struct mvpp2_port *port,
5159 const struct mvpp2_ethtool_counter *counter)
5160{
5161 u64 val;
5162
5163 val = readl(port->stats_base + counter->offset);
5164 if (counter->reg_is_64b)
5165 val += (u64)readl(port->stats_base + counter->offset + 4) << 32;
5166
5167 return val;
5168}
5169
5170/* Due to the fact that software statistics and hardware statistics are, by
5171 * design, incremented at different moments in the chain of packet processing,
5172 * it is very likely that incoming packets could have been dropped after being
5173 * counted by hardware but before reaching software statistics (most probably
5174 * multicast packets), and in the oppposite way, during transmission, FCS bytes
5175 * are added in between as well as TSO skb will be split and header bytes added.
5176 * Hence, statistics gathered from userspace with ifconfig (software) and
5177 * ethtool (hardware) cannot be compared.
5178 */
5179static const struct mvpp2_ethtool_counter mvpp2_ethtool_regs[] = {
5180 { MVPP2_MIB_GOOD_OCTETS_RCVD, "good_octets_received", true },
5181 { MVPP2_MIB_BAD_OCTETS_RCVD, "bad_octets_received" },
5182 { MVPP2_MIB_CRC_ERRORS_SENT, "crc_errors_sent" },
5183 { MVPP2_MIB_UNICAST_FRAMES_RCVD, "unicast_frames_received" },
5184 { MVPP2_MIB_BROADCAST_FRAMES_RCVD, "broadcast_frames_received" },
5185 { MVPP2_MIB_MULTICAST_FRAMES_RCVD, "multicast_frames_received" },
5186 { MVPP2_MIB_FRAMES_64_OCTETS, "frames_64_octets" },
5187 { MVPP2_MIB_FRAMES_65_TO_127_OCTETS, "frames_65_to_127_octet" },
5188 { MVPP2_MIB_FRAMES_128_TO_255_OCTETS, "frames_128_to_255_octet" },
5189 { MVPP2_MIB_FRAMES_256_TO_511_OCTETS, "frames_256_to_511_octet" },
5190 { MVPP2_MIB_FRAMES_512_TO_1023_OCTETS, "frames_512_to_1023_octet" },
5191 { MVPP2_MIB_FRAMES_1024_TO_MAX_OCTETS, "frames_1024_to_max_octet" },
5192 { MVPP2_MIB_GOOD_OCTETS_SENT, "good_octets_sent", true },
5193 { MVPP2_MIB_UNICAST_FRAMES_SENT, "unicast_frames_sent" },
5194 { MVPP2_MIB_MULTICAST_FRAMES_SENT, "multicast_frames_sent" },
5195 { MVPP2_MIB_BROADCAST_FRAMES_SENT, "broadcast_frames_sent" },
5196 { MVPP2_MIB_FC_SENT, "fc_sent" },
5197 { MVPP2_MIB_FC_RCVD, "fc_received" },
5198 { MVPP2_MIB_RX_FIFO_OVERRUN, "rx_fifo_overrun" },
5199 { MVPP2_MIB_UNDERSIZE_RCVD, "undersize_received" },
5200 { MVPP2_MIB_FRAGMENTS_RCVD, "fragments_received" },
5201 { MVPP2_MIB_OVERSIZE_RCVD, "oversize_received" },
5202 { MVPP2_MIB_JABBER_RCVD, "jabber_received" },
5203 { MVPP2_MIB_MAC_RCV_ERROR, "mac_receive_error" },
5204 { MVPP2_MIB_BAD_CRC_EVENT, "bad_crc_event" },
5205 { MVPP2_MIB_COLLISION, "collision" },
5206 { MVPP2_MIB_LATE_COLLISION, "late_collision" },
5207};
5208
5209static void mvpp2_ethtool_get_strings(struct net_device *netdev, u32 sset,
5210 u8 *data)
5211{
5212 if (sset == ETH_SS_STATS) {
5213 int i;
5214
5215 for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_regs); i++)
5216 memcpy(data + i * ETH_GSTRING_LEN,
5217 &mvpp2_ethtool_regs[i].string, ETH_GSTRING_LEN);
5218 }
5219}
5220
5221static void mvpp2_gather_hw_statistics(struct work_struct *work)
5222{
5223 struct delayed_work *del_work = to_delayed_work(work);
Miquel Raynale5c500e2017-11-08 08:59:40 +01005224 struct mvpp2_port *port = container_of(del_work, struct mvpp2_port,
5225 stats_work);
Miquel Raynal118d6292017-11-06 22:56:53 +01005226 u64 *pstats;
Miquel Raynale5c500e2017-11-08 08:59:40 +01005227 int i;
Miquel Raynal118d6292017-11-06 22:56:53 +01005228
Miquel Raynale5c500e2017-11-08 08:59:40 +01005229 mutex_lock(&port->gather_stats_lock);
Miquel Raynal118d6292017-11-06 22:56:53 +01005230
Miquel Raynale5c500e2017-11-08 08:59:40 +01005231 pstats = port->ethtool_stats;
5232 for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_regs); i++)
5233 *pstats++ += mvpp2_read_count(port, &mvpp2_ethtool_regs[i]);
Miquel Raynal118d6292017-11-06 22:56:53 +01005234
5235 /* No need to read again the counters right after this function if it
5236 * was called asynchronously by the user (ie. use of ethtool).
5237 */
Miquel Raynale5c500e2017-11-08 08:59:40 +01005238 cancel_delayed_work(&port->stats_work);
5239 queue_delayed_work(port->priv->stats_queue, &port->stats_work,
Miquel Raynal118d6292017-11-06 22:56:53 +01005240 MVPP2_MIB_COUNTERS_STATS_DELAY);
5241
Miquel Raynale5c500e2017-11-08 08:59:40 +01005242 mutex_unlock(&port->gather_stats_lock);
Miquel Raynal118d6292017-11-06 22:56:53 +01005243}
5244
5245static void mvpp2_ethtool_get_stats(struct net_device *dev,
5246 struct ethtool_stats *stats, u64 *data)
5247{
5248 struct mvpp2_port *port = netdev_priv(dev);
5249
Miquel Raynale5c500e2017-11-08 08:59:40 +01005250 /* Update statistics for the given port, then take the lock to avoid
5251 * concurrent accesses on the ethtool_stats structure during its copy.
5252 */
5253 mvpp2_gather_hw_statistics(&port->stats_work.work);
Miquel Raynal118d6292017-11-06 22:56:53 +01005254
Miquel Raynale5c500e2017-11-08 08:59:40 +01005255 mutex_lock(&port->gather_stats_lock);
Miquel Raynal118d6292017-11-06 22:56:53 +01005256 memcpy(data, port->ethtool_stats,
5257 sizeof(u64) * ARRAY_SIZE(mvpp2_ethtool_regs));
Miquel Raynale5c500e2017-11-08 08:59:40 +01005258 mutex_unlock(&port->gather_stats_lock);
Miquel Raynal118d6292017-11-06 22:56:53 +01005259}
5260
5261static int mvpp2_ethtool_get_sset_count(struct net_device *dev, int sset)
5262{
5263 if (sset == ETH_SS_STATS)
5264 return ARRAY_SIZE(mvpp2_ethtool_regs);
5265
5266 return -EOPNOTSUPP;
5267}
5268
Marcin Wojtas3f518502014-07-10 16:52:13 -03005269static void mvpp2_port_reset(struct mvpp2_port *port)
5270{
5271 u32 val;
Miquel Raynal118d6292017-11-06 22:56:53 +01005272 unsigned int i;
5273
5274 /* Read the GOP statistics to reset the hardware counters */
5275 for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_regs); i++)
5276 mvpp2_read_count(port, &mvpp2_ethtool_regs[i]);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005277
5278 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
5279 ~MVPP2_GMAC_PORT_RESET_MASK;
5280 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
5281
5282 while (readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
5283 MVPP2_GMAC_PORT_RESET_MASK)
5284 continue;
5285}
5286
5287/* Change maximum receive size of the port */
5288static inline void mvpp2_gmac_max_rx_size_set(struct mvpp2_port *port)
5289{
5290 u32 val;
5291
5292 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
5293 val &= ~MVPP2_GMAC_MAX_RX_SIZE_MASK;
5294 val |= (((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
5295 MVPP2_GMAC_MAX_RX_SIZE_OFFS);
5296 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
5297}
5298
Stefan Chulski76eb1b12017-08-22 19:08:26 +02005299/* Change maximum receive size of the port */
5300static inline void mvpp2_xlg_max_rx_size_set(struct mvpp2_port *port)
5301{
5302 u32 val;
5303
5304 val = readl(port->base + MVPP22_XLG_CTRL1_REG);
5305 val &= ~MVPP22_XLG_CTRL1_FRAMESIZELIMIT_MASK;
5306 val |= ((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
Antoine Ténartec15ecd2017-08-25 15:24:46 +02005307 MVPP22_XLG_CTRL1_FRAMESIZELIMIT_OFFS;
Stefan Chulski76eb1b12017-08-22 19:08:26 +02005308 writel(val, port->base + MVPP22_XLG_CTRL1_REG);
5309}
5310
Marcin Wojtas3f518502014-07-10 16:52:13 -03005311/* Set defaults to the MVPP2 port */
5312static void mvpp2_defaults_set(struct mvpp2_port *port)
5313{
5314 int tx_port_num, val, queue, ptxq, lrxq;
5315
Thomas Petazzoni3d9017d2017-03-07 16:53:11 +01005316 if (port->priv->hw_version == MVPP21) {
5317 /* Configure port to loopback if needed */
5318 if (port->flags & MVPP2_F_LOOPBACK)
5319 mvpp2_port_loopback_set(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005320
Thomas Petazzoni3d9017d2017-03-07 16:53:11 +01005321 /* Update TX FIFO MIN Threshold */
5322 val = readl(port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
5323 val &= ~MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK;
5324 /* Min. TX threshold must be less than minimal packet length */
5325 val |= MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(64 - 4 - 2);
5326 writel(val, port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
5327 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03005328
5329 /* Disable Legacy WRR, Disable EJP, Release from reset */
5330 tx_port_num = mvpp2_egress_port(port);
5331 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG,
5332 tx_port_num);
5333 mvpp2_write(port->priv, MVPP2_TXP_SCHED_CMD_1_REG, 0);
5334
5335 /* Close bandwidth for all queues */
5336 for (queue = 0; queue < MVPP2_MAX_TXQ; queue++) {
5337 ptxq = mvpp2_txq_phys(port->id, queue);
5338 mvpp2_write(port->priv,
5339 MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(ptxq), 0);
5340 }
5341
5342 /* Set refill period to 1 usec, refill tokens
5343 * and bucket size to maximum
5344 */
5345 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PERIOD_REG,
5346 port->priv->tclk / USEC_PER_SEC);
5347 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_REFILL_REG);
5348 val &= ~MVPP2_TXP_REFILL_PERIOD_ALL_MASK;
5349 val |= MVPP2_TXP_REFILL_PERIOD_MASK(1);
5350 val |= MVPP2_TXP_REFILL_TOKENS_ALL_MASK;
5351 mvpp2_write(port->priv, MVPP2_TXP_SCHED_REFILL_REG, val);
5352 val = MVPP2_TXP_TOKEN_SIZE_MAX;
5353 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
5354
5355 /* Set MaximumLowLatencyPacketSize value to 256 */
5356 mvpp2_write(port->priv, MVPP2_RX_CTRL_REG(port->id),
5357 MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK |
5358 MVPP2_RX_LOW_LATENCY_PKT_SIZE(256));
5359
5360 /* Enable Rx cache snoop */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005361 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005362 queue = port->rxqs[lrxq]->id;
5363 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
5364 val |= MVPP2_SNOOP_PKT_SIZE_MASK |
5365 MVPP2_SNOOP_BUF_HDR_MASK;
5366 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
5367 }
5368
5369 /* At default, mask all interrupts to all present cpus */
5370 mvpp2_interrupts_disable(port);
5371}
5372
5373/* Enable/disable receiving packets */
5374static void mvpp2_ingress_enable(struct mvpp2_port *port)
5375{
5376 u32 val;
5377 int lrxq, queue;
5378
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005379 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005380 queue = port->rxqs[lrxq]->id;
5381 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
5382 val &= ~MVPP2_RXQ_DISABLE_MASK;
5383 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
5384 }
5385}
5386
5387static void mvpp2_ingress_disable(struct mvpp2_port *port)
5388{
5389 u32 val;
5390 int lrxq, queue;
5391
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005392 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005393 queue = port->rxqs[lrxq]->id;
5394 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
5395 val |= MVPP2_RXQ_DISABLE_MASK;
5396 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
5397 }
5398}
5399
5400/* Enable transmit via physical egress queue
5401 * - HW starts take descriptors from DRAM
5402 */
5403static void mvpp2_egress_enable(struct mvpp2_port *port)
5404{
5405 u32 qmap;
5406 int queue;
5407 int tx_port_num = mvpp2_egress_port(port);
5408
5409 /* Enable all initialized TXs. */
5410 qmap = 0;
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005411 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005412 struct mvpp2_tx_queue *txq = port->txqs[queue];
5413
Markus Elfringdbbb2f02017-04-17 14:07:52 +02005414 if (txq->descs)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005415 qmap |= (1 << queue);
5416 }
5417
5418 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
5419 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG, qmap);
5420}
5421
5422/* Disable transmit via physical egress queue
5423 * - HW doesn't take descriptors from DRAM
5424 */
5425static void mvpp2_egress_disable(struct mvpp2_port *port)
5426{
5427 u32 reg_data;
5428 int delay;
5429 int tx_port_num = mvpp2_egress_port(port);
5430
5431 /* Issue stop command for active channels only */
5432 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
5433 reg_data = (mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG)) &
5434 MVPP2_TXP_SCHED_ENQ_MASK;
5435 if (reg_data != 0)
5436 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG,
5437 (reg_data << MVPP2_TXP_SCHED_DISQ_OFFSET));
5438
5439 /* Wait for all Tx activity to terminate. */
5440 delay = 0;
5441 do {
5442 if (delay >= MVPP2_TX_DISABLE_TIMEOUT_MSEC) {
5443 netdev_warn(port->dev,
5444 "Tx stop timed out, status=0x%08x\n",
5445 reg_data);
5446 break;
5447 }
5448 mdelay(1);
5449 delay++;
5450
5451 /* Check port TX Command register that all
5452 * Tx queues are stopped
5453 */
5454 reg_data = mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG);
5455 } while (reg_data & MVPP2_TXP_SCHED_ENQ_MASK);
5456}
5457
5458/* Rx descriptors helper methods */
5459
5460/* Get number of Rx descriptors occupied by received packets */
5461static inline int
5462mvpp2_rxq_received(struct mvpp2_port *port, int rxq_id)
5463{
5464 u32 val = mvpp2_read(port->priv, MVPP2_RXQ_STATUS_REG(rxq_id));
5465
5466 return val & MVPP2_RXQ_OCCUPIED_MASK;
5467}
5468
5469/* Update Rx queue status with the number of occupied and available
5470 * Rx descriptor slots.
5471 */
5472static inline void
5473mvpp2_rxq_status_update(struct mvpp2_port *port, int rxq_id,
5474 int used_count, int free_count)
5475{
5476 /* Decrement the number of used descriptors and increment count
5477 * increment the number of free descriptors.
5478 */
5479 u32 val = used_count | (free_count << MVPP2_RXQ_NUM_NEW_OFFSET);
5480
5481 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_UPDATE_REG(rxq_id), val);
5482}
5483
5484/* Get pointer to next RX descriptor to be processed by SW */
5485static inline struct mvpp2_rx_desc *
5486mvpp2_rxq_next_desc_get(struct mvpp2_rx_queue *rxq)
5487{
5488 int rx_desc = rxq->next_desc_to_proc;
5489
5490 rxq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(rxq, rx_desc);
5491 prefetch(rxq->descs + rxq->next_desc_to_proc);
5492 return rxq->descs + rx_desc;
5493}
5494
5495/* Set rx queue offset */
5496static void mvpp2_rxq_offset_set(struct mvpp2_port *port,
5497 int prxq, int offset)
5498{
5499 u32 val;
5500
5501 /* Convert offset from bytes to units of 32 bytes */
5502 offset = offset >> 5;
5503
5504 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
5505 val &= ~MVPP2_RXQ_PACKET_OFFSET_MASK;
5506
5507 /* Offset is in */
5508 val |= ((offset << MVPP2_RXQ_PACKET_OFFSET_OFFS) &
5509 MVPP2_RXQ_PACKET_OFFSET_MASK);
5510
5511 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
5512}
5513
Marcin Wojtas3f518502014-07-10 16:52:13 -03005514/* Tx descriptors helper methods */
5515
Marcin Wojtas3f518502014-07-10 16:52:13 -03005516/* Get pointer to next Tx descriptor to be processed (send) by HW */
5517static struct mvpp2_tx_desc *
5518mvpp2_txq_next_desc_get(struct mvpp2_tx_queue *txq)
5519{
5520 int tx_desc = txq->next_desc_to_proc;
5521
5522 txq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(txq, tx_desc);
5523 return txq->descs + tx_desc;
5524}
5525
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005526/* Update HW with number of aggregated Tx descriptors to be sent
5527 *
5528 * Called only from mvpp2_tx(), so migration is disabled, using
5529 * smp_processor_id() is OK.
5530 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03005531static void mvpp2_aggr_txq_pend_desc_add(struct mvpp2_port *port, int pending)
5532{
5533 /* aggregated access - relevant TXQ number is written in TX desc */
Thomas Petazzonia7868412017-03-07 16:53:13 +01005534 mvpp2_percpu_write(port->priv, smp_processor_id(),
5535 MVPP2_AGGR_TXQ_UPDATE_REG, pending);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005536}
5537
5538
5539/* Check if there are enough free descriptors in aggregated txq.
5540 * If not, update the number of occupied descriptors and repeat the check.
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005541 *
5542 * Called only from mvpp2_tx(), so migration is disabled, using
5543 * smp_processor_id() is OK.
Marcin Wojtas3f518502014-07-10 16:52:13 -03005544 */
5545static int mvpp2_aggr_desc_num_check(struct mvpp2 *priv,
5546 struct mvpp2_tx_queue *aggr_txq, int num)
5547{
Antoine Tenart02856a32017-10-30 11:23:32 +01005548 if ((aggr_txq->count + num) > MVPP2_AGGR_TXQ_SIZE) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005549 /* Update number of occupied aggregated Tx descriptors */
5550 int cpu = smp_processor_id();
5551 u32 val = mvpp2_read(priv, MVPP2_AGGR_TXQ_STATUS_REG(cpu));
5552
5553 aggr_txq->count = val & MVPP2_AGGR_TXQ_PENDING_MASK;
5554 }
5555
Antoine Tenart02856a32017-10-30 11:23:32 +01005556 if ((aggr_txq->count + num) > MVPP2_AGGR_TXQ_SIZE)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005557 return -ENOMEM;
5558
5559 return 0;
5560}
5561
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005562/* Reserved Tx descriptors allocation request
5563 *
5564 * Called only from mvpp2_txq_reserved_desc_num_proc(), itself called
5565 * only by mvpp2_tx(), so migration is disabled, using
5566 * smp_processor_id() is OK.
5567 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03005568static int mvpp2_txq_alloc_reserved_desc(struct mvpp2 *priv,
5569 struct mvpp2_tx_queue *txq, int num)
5570{
5571 u32 val;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005572 int cpu = smp_processor_id();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005573
5574 val = (txq->id << MVPP2_TXQ_RSVD_REQ_Q_OFFSET) | num;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005575 mvpp2_percpu_write(priv, cpu, MVPP2_TXQ_RSVD_REQ_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005576
Thomas Petazzonia7868412017-03-07 16:53:13 +01005577 val = mvpp2_percpu_read(priv, cpu, MVPP2_TXQ_RSVD_RSLT_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005578
5579 return val & MVPP2_TXQ_RSVD_RSLT_MASK;
5580}
5581
5582/* Check if there are enough reserved descriptors for transmission.
5583 * If not, request chunk of reserved descriptors and check again.
5584 */
5585static int mvpp2_txq_reserved_desc_num_proc(struct mvpp2 *priv,
5586 struct mvpp2_tx_queue *txq,
5587 struct mvpp2_txq_pcpu *txq_pcpu,
5588 int num)
5589{
5590 int req, cpu, desc_count;
5591
5592 if (txq_pcpu->reserved_num >= num)
5593 return 0;
5594
5595 /* Not enough descriptors reserved! Update the reserved descriptor
5596 * count and check again.
5597 */
5598
5599 desc_count = 0;
5600 /* Compute total of used descriptors */
5601 for_each_present_cpu(cpu) {
5602 struct mvpp2_txq_pcpu *txq_pcpu_aux;
5603
5604 txq_pcpu_aux = per_cpu_ptr(txq->pcpu, cpu);
5605 desc_count += txq_pcpu_aux->count;
5606 desc_count += txq_pcpu_aux->reserved_num;
5607 }
5608
5609 req = max(MVPP2_CPU_DESC_CHUNK, num - txq_pcpu->reserved_num);
5610 desc_count += req;
5611
5612 if (desc_count >
5613 (txq->size - (num_present_cpus() * MVPP2_CPU_DESC_CHUNK)))
5614 return -ENOMEM;
5615
5616 txq_pcpu->reserved_num += mvpp2_txq_alloc_reserved_desc(priv, txq, req);
5617
5618 /* OK, the descriptor cound has been updated: check again. */
5619 if (txq_pcpu->reserved_num < num)
5620 return -ENOMEM;
5621 return 0;
5622}
5623
5624/* Release the last allocated Tx descriptor. Useful to handle DMA
5625 * mapping failures in the Tx path.
5626 */
5627static void mvpp2_txq_desc_put(struct mvpp2_tx_queue *txq)
5628{
5629 if (txq->next_desc_to_proc == 0)
5630 txq->next_desc_to_proc = txq->last_desc - 1;
5631 else
5632 txq->next_desc_to_proc--;
5633}
5634
5635/* Set Tx descriptors fields relevant for CSUM calculation */
5636static u32 mvpp2_txq_desc_csum(int l3_offs, int l3_proto,
5637 int ip_hdr_len, int l4_proto)
5638{
5639 u32 command;
5640
5641 /* fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
5642 * G_L4_chk, L4_type required only for checksum calculation
5643 */
5644 command = (l3_offs << MVPP2_TXD_L3_OFF_SHIFT);
5645 command |= (ip_hdr_len << MVPP2_TXD_IP_HLEN_SHIFT);
5646 command |= MVPP2_TXD_IP_CSUM_DISABLE;
5647
5648 if (l3_proto == swab16(ETH_P_IP)) {
5649 command &= ~MVPP2_TXD_IP_CSUM_DISABLE; /* enable IPv4 csum */
5650 command &= ~MVPP2_TXD_L3_IP6; /* enable IPv4 */
5651 } else {
5652 command |= MVPP2_TXD_L3_IP6; /* enable IPv6 */
5653 }
5654
5655 if (l4_proto == IPPROTO_TCP) {
5656 command &= ~MVPP2_TXD_L4_UDP; /* enable TCP */
5657 command &= ~MVPP2_TXD_L4_CSUM_FRAG; /* generate L4 csum */
5658 } else if (l4_proto == IPPROTO_UDP) {
5659 command |= MVPP2_TXD_L4_UDP; /* enable UDP */
5660 command &= ~MVPP2_TXD_L4_CSUM_FRAG; /* generate L4 csum */
5661 } else {
5662 command |= MVPP2_TXD_L4_CSUM_NOT;
5663 }
5664
5665 return command;
5666}
5667
5668/* Get number of sent descriptors and decrement counter.
5669 * The number of sent descriptors is returned.
5670 * Per-CPU access
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005671 *
5672 * Called only from mvpp2_txq_done(), called from mvpp2_tx()
5673 * (migration disabled) and from the TX completion tasklet (migration
5674 * disabled) so using smp_processor_id() is OK.
Marcin Wojtas3f518502014-07-10 16:52:13 -03005675 */
5676static inline int mvpp2_txq_sent_desc_proc(struct mvpp2_port *port,
5677 struct mvpp2_tx_queue *txq)
5678{
5679 u32 val;
5680
5681 /* Reading status reg resets transmitted descriptor counter */
Thomas Petazzonia7868412017-03-07 16:53:13 +01005682 val = mvpp2_percpu_read(port->priv, smp_processor_id(),
5683 MVPP2_TXQ_SENT_REG(txq->id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005684
5685 return (val & MVPP2_TRANSMITTED_COUNT_MASK) >>
5686 MVPP2_TRANSMITTED_COUNT_OFFSET;
5687}
5688
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005689/* Called through on_each_cpu(), so runs on all CPUs, with migration
5690 * disabled, therefore using smp_processor_id() is OK.
5691 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03005692static void mvpp2_txq_sent_counter_clear(void *arg)
5693{
5694 struct mvpp2_port *port = arg;
5695 int queue;
5696
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005697 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005698 int id = port->txqs[queue]->id;
5699
Thomas Petazzonia7868412017-03-07 16:53:13 +01005700 mvpp2_percpu_read(port->priv, smp_processor_id(),
5701 MVPP2_TXQ_SENT_REG(id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005702 }
5703}
5704
5705/* Set max sizes for Tx queues */
5706static void mvpp2_txp_max_tx_size_set(struct mvpp2_port *port)
5707{
5708 u32 val, size, mtu;
5709 int txq, tx_port_num;
5710
5711 mtu = port->pkt_size * 8;
5712 if (mtu > MVPP2_TXP_MTU_MAX)
5713 mtu = MVPP2_TXP_MTU_MAX;
5714
5715 /* WA for wrong Token bucket update: Set MTU value = 3*real MTU value */
5716 mtu = 3 * mtu;
5717
5718 /* Indirect access to registers */
5719 tx_port_num = mvpp2_egress_port(port);
5720 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
5721
5722 /* Set MTU */
5723 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_MTU_REG);
5724 val &= ~MVPP2_TXP_MTU_MAX;
5725 val |= mtu;
5726 mvpp2_write(port->priv, MVPP2_TXP_SCHED_MTU_REG, val);
5727
5728 /* TXP token size and all TXQs token size must be larger that MTU */
5729 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG);
5730 size = val & MVPP2_TXP_TOKEN_SIZE_MAX;
5731 if (size < mtu) {
5732 size = mtu;
5733 val &= ~MVPP2_TXP_TOKEN_SIZE_MAX;
5734 val |= size;
5735 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
5736 }
5737
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005738 for (txq = 0; txq < port->ntxqs; txq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005739 val = mvpp2_read(port->priv,
5740 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq));
5741 size = val & MVPP2_TXQ_TOKEN_SIZE_MAX;
5742
5743 if (size < mtu) {
5744 size = mtu;
5745 val &= ~MVPP2_TXQ_TOKEN_SIZE_MAX;
5746 val |= size;
5747 mvpp2_write(port->priv,
5748 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq),
5749 val);
5750 }
5751 }
5752}
5753
5754/* Set the number of packets that will be received before Rx interrupt
5755 * will be generated by HW.
5756 */
5757static void mvpp2_rx_pkts_coal_set(struct mvpp2_port *port,
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01005758 struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005759{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005760 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005761
Thomas Petazzonif8b0d5f2017-02-21 11:28:03 +01005762 if (rxq->pkts_coal > MVPP2_OCCUPIED_THRESH_MASK)
5763 rxq->pkts_coal = MVPP2_OCCUPIED_THRESH_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005764
Thomas Petazzonia7868412017-03-07 16:53:13 +01005765 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
5766 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_THRESH_REG,
5767 rxq->pkts_coal);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005768
5769 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005770}
5771
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005772/* For some reason in the LSP this is done on each CPU. Why ? */
5773static void mvpp2_tx_pkts_coal_set(struct mvpp2_port *port,
5774 struct mvpp2_tx_queue *txq)
5775{
5776 int cpu = get_cpu();
5777 u32 val;
5778
5779 if (txq->done_pkts_coal > MVPP2_TXQ_THRESH_MASK)
5780 txq->done_pkts_coal = MVPP2_TXQ_THRESH_MASK;
5781
5782 val = (txq->done_pkts_coal << MVPP2_TXQ_THRESH_OFFSET);
5783 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5784 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_THRESH_REG, val);
5785
5786 put_cpu();
5787}
5788
Thomas Petazzoniab426762017-02-21 11:28:04 +01005789static u32 mvpp2_usec_to_cycles(u32 usec, unsigned long clk_hz)
5790{
5791 u64 tmp = (u64)clk_hz * usec;
5792
5793 do_div(tmp, USEC_PER_SEC);
5794
5795 return tmp > U32_MAX ? U32_MAX : tmp;
5796}
5797
5798static u32 mvpp2_cycles_to_usec(u32 cycles, unsigned long clk_hz)
5799{
5800 u64 tmp = (u64)cycles * USEC_PER_SEC;
5801
5802 do_div(tmp, clk_hz);
5803
5804 return tmp > U32_MAX ? U32_MAX : tmp;
5805}
5806
Marcin Wojtas3f518502014-07-10 16:52:13 -03005807/* Set the time delay in usec before Rx interrupt */
5808static void mvpp2_rx_time_coal_set(struct mvpp2_port *port,
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01005809 struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005810{
Thomas Petazzoniab426762017-02-21 11:28:04 +01005811 unsigned long freq = port->priv->tclk;
5812 u32 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005813
Thomas Petazzoniab426762017-02-21 11:28:04 +01005814 if (val > MVPP2_MAX_ISR_RX_THRESHOLD) {
5815 rxq->time_coal =
5816 mvpp2_cycles_to_usec(MVPP2_MAX_ISR_RX_THRESHOLD, freq);
5817
5818 /* re-evaluate to get actual register value */
5819 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
5820 }
5821
Marcin Wojtas3f518502014-07-10 16:52:13 -03005822 mvpp2_write(port->priv, MVPP2_ISR_RX_THRESHOLD_REG(rxq->id), val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005823}
5824
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005825static void mvpp2_tx_time_coal_set(struct mvpp2_port *port)
5826{
5827 unsigned long freq = port->priv->tclk;
5828 u32 val = mvpp2_usec_to_cycles(port->tx_time_coal, freq);
5829
5830 if (val > MVPP2_MAX_ISR_TX_THRESHOLD) {
5831 port->tx_time_coal =
5832 mvpp2_cycles_to_usec(MVPP2_MAX_ISR_TX_THRESHOLD, freq);
5833
5834 /* re-evaluate to get actual register value */
5835 val = mvpp2_usec_to_cycles(port->tx_time_coal, freq);
5836 }
5837
5838 mvpp2_write(port->priv, MVPP2_ISR_TX_THRESHOLD_REG(port->id), val);
5839}
5840
Marcin Wojtas3f518502014-07-10 16:52:13 -03005841/* Free Tx queue skbuffs */
5842static void mvpp2_txq_bufs_free(struct mvpp2_port *port,
5843 struct mvpp2_tx_queue *txq,
5844 struct mvpp2_txq_pcpu *txq_pcpu, int num)
5845{
5846 int i;
5847
5848 for (i = 0; i < num; i++) {
Thomas Petazzoni83544912016-12-21 11:28:49 +01005849 struct mvpp2_txq_pcpu_buf *tx_buf =
5850 txq_pcpu->buffs + txq_pcpu->txq_get_index;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005851
Antoine Tenart20920262017-10-23 15:24:30 +02005852 if (!IS_TSO_HEADER(txq_pcpu, tx_buf->dma))
5853 dma_unmap_single(port->dev->dev.parent, tx_buf->dma,
5854 tx_buf->size, DMA_TO_DEVICE);
Thomas Petazzoni36fb7432017-02-21 11:28:05 +01005855 if (tx_buf->skb)
5856 dev_kfree_skb_any(tx_buf->skb);
5857
5858 mvpp2_txq_inc_get(txq_pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005859 }
5860}
5861
5862static inline struct mvpp2_rx_queue *mvpp2_get_rx_queue(struct mvpp2_port *port,
5863 u32 cause)
5864{
5865 int queue = fls(cause) - 1;
5866
5867 return port->rxqs[queue];
5868}
5869
5870static inline struct mvpp2_tx_queue *mvpp2_get_tx_queue(struct mvpp2_port *port,
5871 u32 cause)
5872{
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005873 int queue = fls(cause) - 1;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005874
5875 return port->txqs[queue];
5876}
5877
5878/* Handle end of transmission */
5879static void mvpp2_txq_done(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
5880 struct mvpp2_txq_pcpu *txq_pcpu)
5881{
5882 struct netdev_queue *nq = netdev_get_tx_queue(port->dev, txq->log_id);
5883 int tx_done;
5884
5885 if (txq_pcpu->cpu != smp_processor_id())
5886 netdev_err(port->dev, "wrong cpu on the end of Tx processing\n");
5887
5888 tx_done = mvpp2_txq_sent_desc_proc(port, txq);
5889 if (!tx_done)
5890 return;
5891 mvpp2_txq_bufs_free(port, txq, txq_pcpu, tx_done);
5892
5893 txq_pcpu->count -= tx_done;
5894
5895 if (netif_tx_queue_stopped(nq))
Antoine Tenart1d17db02017-10-30 11:23:31 +01005896 if (txq_pcpu->count <= txq_pcpu->wake_threshold)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005897 netif_tx_wake_queue(nq);
5898}
5899
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005900static unsigned int mvpp2_tx_done(struct mvpp2_port *port, u32 cause,
5901 int cpu)
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005902{
5903 struct mvpp2_tx_queue *txq;
5904 struct mvpp2_txq_pcpu *txq_pcpu;
5905 unsigned int tx_todo = 0;
5906
5907 while (cause) {
5908 txq = mvpp2_get_tx_queue(port, cause);
5909 if (!txq)
5910 break;
5911
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005912 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005913
5914 if (txq_pcpu->count) {
5915 mvpp2_txq_done(port, txq, txq_pcpu);
5916 tx_todo += txq_pcpu->count;
5917 }
5918
5919 cause &= ~(1 << txq->log_id);
5920 }
5921 return tx_todo;
5922}
5923
Marcin Wojtas3f518502014-07-10 16:52:13 -03005924/* Rx/Tx queue initialization/cleanup methods */
5925
5926/* Allocate and initialize descriptors for aggr TXQ */
5927static int mvpp2_aggr_txq_init(struct platform_device *pdev,
Antoine Ténart85affd72017-08-23 09:46:55 +02005928 struct mvpp2_tx_queue *aggr_txq, int cpu,
Marcin Wojtas3f518502014-07-10 16:52:13 -03005929 struct mvpp2 *priv)
5930{
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005931 u32 txq_dma;
5932
Marcin Wojtas3f518502014-07-10 16:52:13 -03005933 /* Allocate memory for TX descriptors */
Yan Markmana154f8e2017-11-30 10:49:46 +01005934 aggr_txq->descs = dma_zalloc_coherent(&pdev->dev,
Antoine Ténart85affd72017-08-23 09:46:55 +02005935 MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005936 &aggr_txq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005937 if (!aggr_txq->descs)
5938 return -ENOMEM;
5939
Antoine Tenart02856a32017-10-30 11:23:32 +01005940 aggr_txq->last_desc = MVPP2_AGGR_TXQ_SIZE - 1;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005941
5942 /* Aggr TXQ no reset WA */
5943 aggr_txq->next_desc_to_proc = mvpp2_read(priv,
5944 MVPP2_AGGR_TXQ_INDEX_REG(cpu));
5945
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005946 /* Set Tx descriptors queue starting address indirect
5947 * access
5948 */
5949 if (priv->hw_version == MVPP21)
5950 txq_dma = aggr_txq->descs_dma;
5951 else
5952 txq_dma = aggr_txq->descs_dma >>
5953 MVPP22_AGGR_TXQ_DESC_ADDR_OFFS;
5954
5955 mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu), txq_dma);
Antoine Ténart85affd72017-08-23 09:46:55 +02005956 mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu),
5957 MVPP2_AGGR_TXQ_SIZE);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005958
5959 return 0;
5960}
5961
5962/* Create a specified Rx queue */
5963static int mvpp2_rxq_init(struct mvpp2_port *port,
5964 struct mvpp2_rx_queue *rxq)
5965
5966{
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005967 u32 rxq_dma;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005968 int cpu;
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005969
Marcin Wojtas3f518502014-07-10 16:52:13 -03005970 rxq->size = port->rx_ring_size;
5971
5972 /* Allocate memory for RX descriptors */
5973 rxq->descs = dma_alloc_coherent(port->dev->dev.parent,
5974 rxq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005975 &rxq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005976 if (!rxq->descs)
5977 return -ENOMEM;
5978
Marcin Wojtas3f518502014-07-10 16:52:13 -03005979 rxq->last_desc = rxq->size - 1;
5980
5981 /* Zero occupied and non-occupied counters - direct access */
5982 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
5983
5984 /* Set Rx descriptors queue starting address - indirect access */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005985 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005986 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005987 if (port->priv->hw_version == MVPP21)
5988 rxq_dma = rxq->descs_dma;
5989 else
5990 rxq_dma = rxq->descs_dma >> MVPP22_DESC_ADDR_OFFS;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005991 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_ADDR_REG, rxq_dma);
5992 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_SIZE_REG, rxq->size);
5993 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_INDEX_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005994 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005995
5996 /* Set Offset */
5997 mvpp2_rxq_offset_set(port, rxq->id, NET_SKB_PAD);
5998
5999 /* Set coalescing pkts and time */
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01006000 mvpp2_rx_pkts_coal_set(port, rxq);
6001 mvpp2_rx_time_coal_set(port, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006002
6003 /* Add number of descriptors ready for receiving packets */
6004 mvpp2_rxq_status_update(port, rxq->id, 0, rxq->size);
6005
6006 return 0;
6007}
6008
6009/* Push packets received by the RXQ to BM pool */
6010static void mvpp2_rxq_drop_pkts(struct mvpp2_port *port,
6011 struct mvpp2_rx_queue *rxq)
6012{
6013 int rx_received, i;
6014
6015 rx_received = mvpp2_rxq_received(port, rxq->id);
6016 if (!rx_received)
6017 return;
6018
6019 for (i = 0; i < rx_received; i++) {
6020 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006021 u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
6022 int pool;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006023
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006024 pool = (status & MVPP2_RXD_BM_POOL_ID_MASK) >>
6025 MVPP2_RXD_BM_POOL_ID_OFFS;
6026
Thomas Petazzoni7d7627b2017-06-22 14:23:20 +02006027 mvpp2_bm_pool_put(port, pool,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006028 mvpp2_rxdesc_dma_addr_get(port, rx_desc),
6029 mvpp2_rxdesc_cookie_get(port, rx_desc));
Marcin Wojtas3f518502014-07-10 16:52:13 -03006030 }
6031 mvpp2_rxq_status_update(port, rxq->id, rx_received, rx_received);
6032}
6033
6034/* Cleanup Rx queue */
6035static void mvpp2_rxq_deinit(struct mvpp2_port *port,
6036 struct mvpp2_rx_queue *rxq)
6037{
Thomas Petazzonia7868412017-03-07 16:53:13 +01006038 int cpu;
6039
Marcin Wojtas3f518502014-07-10 16:52:13 -03006040 mvpp2_rxq_drop_pkts(port, rxq);
6041
6042 if (rxq->descs)
6043 dma_free_coherent(port->dev->dev.parent,
6044 rxq->size * MVPP2_DESC_ALIGNED_SIZE,
6045 rxq->descs,
Thomas Petazzoni20396132017-03-07 16:53:00 +01006046 rxq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006047
6048 rxq->descs = NULL;
6049 rxq->last_desc = 0;
6050 rxq->next_desc_to_proc = 0;
Thomas Petazzoni20396132017-03-07 16:53:00 +01006051 rxq->descs_dma = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006052
6053 /* Clear Rx descriptors queue starting address and size;
6054 * free descriptor number
6055 */
6056 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02006057 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01006058 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
6059 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_ADDR_REG, 0);
6060 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_SIZE_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02006061 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03006062}
6063
6064/* Create and initialize a Tx queue */
6065static int mvpp2_txq_init(struct mvpp2_port *port,
6066 struct mvpp2_tx_queue *txq)
6067{
6068 u32 val;
6069 int cpu, desc, desc_per_txq, tx_port_num;
6070 struct mvpp2_txq_pcpu *txq_pcpu;
6071
6072 txq->size = port->tx_ring_size;
6073
6074 /* Allocate memory for Tx descriptors */
6075 txq->descs = dma_alloc_coherent(port->dev->dev.parent,
6076 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01006077 &txq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006078 if (!txq->descs)
6079 return -ENOMEM;
6080
Marcin Wojtas3f518502014-07-10 16:52:13 -03006081 txq->last_desc = txq->size - 1;
6082
6083 /* Set Tx descriptors queue starting address - indirect access */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02006084 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01006085 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
6086 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_ADDR_REG,
6087 txq->descs_dma);
6088 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_SIZE_REG,
6089 txq->size & MVPP2_TXQ_DESC_SIZE_MASK);
6090 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_INDEX_REG, 0);
6091 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_RSVD_CLR_REG,
6092 txq->id << MVPP2_TXQ_RSVD_CLR_OFFSET);
6093 val = mvpp2_percpu_read(port->priv, cpu, MVPP2_TXQ_PENDING_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006094 val &= ~MVPP2_TXQ_PENDING_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01006095 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PENDING_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006096
6097 /* Calculate base address in prefetch buffer. We reserve 16 descriptors
6098 * for each existing TXQ.
6099 * TCONTS for PON port must be continuous from 0 to MVPP2_MAX_TCONT
6100 * GBE ports assumed to be continious from 0 to MVPP2_MAX_PORTS
6101 */
6102 desc_per_txq = 16;
6103 desc = (port->id * MVPP2_MAX_TXQ * desc_per_txq) +
6104 (txq->log_id * desc_per_txq);
6105
Thomas Petazzonia7868412017-03-07 16:53:13 +01006106 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG,
6107 MVPP2_PREF_BUF_PTR(desc) | MVPP2_PREF_BUF_SIZE_16 |
6108 MVPP2_PREF_BUF_THRESH(desc_per_txq / 2));
Thomas Petazzonia704bb52017-06-10 23:18:22 +02006109 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03006110
6111 /* WRR / EJP configuration - indirect access */
6112 tx_port_num = mvpp2_egress_port(port);
6113 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
6114
6115 val = mvpp2_read(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id));
6116 val &= ~MVPP2_TXQ_REFILL_PERIOD_ALL_MASK;
6117 val |= MVPP2_TXQ_REFILL_PERIOD_MASK(1);
6118 val |= MVPP2_TXQ_REFILL_TOKENS_ALL_MASK;
6119 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id), val);
6120
6121 val = MVPP2_TXQ_TOKEN_SIZE_MAX;
6122 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq->log_id),
6123 val);
6124
6125 for_each_present_cpu(cpu) {
6126 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
6127 txq_pcpu->size = txq->size;
Markus Elfring02c91ec2017-04-17 08:09:07 +02006128 txq_pcpu->buffs = kmalloc_array(txq_pcpu->size,
6129 sizeof(*txq_pcpu->buffs),
6130 GFP_KERNEL);
Thomas Petazzoni83544912016-12-21 11:28:49 +01006131 if (!txq_pcpu->buffs)
Antoine Tenartba2d8d82017-11-28 14:19:48 +01006132 return -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006133
6134 txq_pcpu->count = 0;
6135 txq_pcpu->reserved_num = 0;
6136 txq_pcpu->txq_put_index = 0;
6137 txq_pcpu->txq_get_index = 0;
Antoine Tenartb70d4a52017-12-11 09:13:25 +01006138 txq_pcpu->tso_headers = NULL;
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006139
Antoine Tenart1d17db02017-10-30 11:23:31 +01006140 txq_pcpu->stop_threshold = txq->size - MVPP2_MAX_SKB_DESCS;
6141 txq_pcpu->wake_threshold = txq_pcpu->stop_threshold / 2;
6142
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006143 txq_pcpu->tso_headers =
6144 dma_alloc_coherent(port->dev->dev.parent,
Yan Markman822eaf72017-10-23 15:24:29 +02006145 txq_pcpu->size * TSO_HEADER_SIZE,
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006146 &txq_pcpu->tso_headers_dma,
6147 GFP_KERNEL);
6148 if (!txq_pcpu->tso_headers)
Antoine Tenartba2d8d82017-11-28 14:19:48 +01006149 return -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006150 }
6151
6152 return 0;
6153}
6154
6155/* Free allocated TXQ resources */
6156static void mvpp2_txq_deinit(struct mvpp2_port *port,
6157 struct mvpp2_tx_queue *txq)
6158{
6159 struct mvpp2_txq_pcpu *txq_pcpu;
6160 int cpu;
6161
6162 for_each_present_cpu(cpu) {
6163 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Thomas Petazzoni83544912016-12-21 11:28:49 +01006164 kfree(txq_pcpu->buffs);
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006165
Antoine Tenartb70d4a52017-12-11 09:13:25 +01006166 if (txq_pcpu->tso_headers)
6167 dma_free_coherent(port->dev->dev.parent,
6168 txq_pcpu->size * TSO_HEADER_SIZE,
6169 txq_pcpu->tso_headers,
6170 txq_pcpu->tso_headers_dma);
6171
6172 txq_pcpu->tso_headers = NULL;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006173 }
6174
6175 if (txq->descs)
6176 dma_free_coherent(port->dev->dev.parent,
6177 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01006178 txq->descs, txq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006179
6180 txq->descs = NULL;
6181 txq->last_desc = 0;
6182 txq->next_desc_to_proc = 0;
Thomas Petazzoni20396132017-03-07 16:53:00 +01006183 txq->descs_dma = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006184
6185 /* Set minimum bandwidth for disabled TXQs */
6186 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(txq->id), 0);
6187
6188 /* Set Tx descriptors queue starting address and size */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02006189 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01006190 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
6191 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_ADDR_REG, 0);
6192 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_SIZE_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02006193 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03006194}
6195
6196/* Cleanup Tx ports */
6197static void mvpp2_txq_clean(struct mvpp2_port *port, struct mvpp2_tx_queue *txq)
6198{
6199 struct mvpp2_txq_pcpu *txq_pcpu;
6200 int delay, pending, cpu;
6201 u32 val;
6202
Thomas Petazzonia704bb52017-06-10 23:18:22 +02006203 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01006204 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
6205 val = mvpp2_percpu_read(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006206 val |= MVPP2_TXQ_DRAIN_EN_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01006207 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006208
6209 /* The napi queue has been stopped so wait for all packets
6210 * to be transmitted.
6211 */
6212 delay = 0;
6213 do {
6214 if (delay >= MVPP2_TX_PENDING_TIMEOUT_MSEC) {
6215 netdev_warn(port->dev,
6216 "port %d: cleaning queue %d timed out\n",
6217 port->id, txq->log_id);
6218 break;
6219 }
6220 mdelay(1);
6221 delay++;
6222
Thomas Petazzonia7868412017-03-07 16:53:13 +01006223 pending = mvpp2_percpu_read(port->priv, cpu,
6224 MVPP2_TXQ_PENDING_REG);
6225 pending &= MVPP2_TXQ_PENDING_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006226 } while (pending);
6227
6228 val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01006229 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG, val);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02006230 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03006231
6232 for_each_present_cpu(cpu) {
6233 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
6234
6235 /* Release all packets */
6236 mvpp2_txq_bufs_free(port, txq, txq_pcpu, txq_pcpu->count);
6237
6238 /* Reset queue */
6239 txq_pcpu->count = 0;
6240 txq_pcpu->txq_put_index = 0;
6241 txq_pcpu->txq_get_index = 0;
6242 }
6243}
6244
6245/* Cleanup all Tx queues */
6246static void mvpp2_cleanup_txqs(struct mvpp2_port *port)
6247{
6248 struct mvpp2_tx_queue *txq;
6249 int queue;
6250 u32 val;
6251
6252 val = mvpp2_read(port->priv, MVPP2_TX_PORT_FLUSH_REG);
6253
6254 /* Reset Tx ports and delete Tx queues */
6255 val |= MVPP2_TX_PORT_FLUSH_MASK(port->id);
6256 mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
6257
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006258 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006259 txq = port->txqs[queue];
6260 mvpp2_txq_clean(port, txq);
6261 mvpp2_txq_deinit(port, txq);
6262 }
6263
6264 on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
6265
6266 val &= ~MVPP2_TX_PORT_FLUSH_MASK(port->id);
6267 mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
6268}
6269
6270/* Cleanup all Rx queues */
6271static void mvpp2_cleanup_rxqs(struct mvpp2_port *port)
6272{
6273 int queue;
6274
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006275 for (queue = 0; queue < port->nrxqs; queue++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006276 mvpp2_rxq_deinit(port, port->rxqs[queue]);
6277}
6278
6279/* Init all Rx queues for port */
6280static int mvpp2_setup_rxqs(struct mvpp2_port *port)
6281{
6282 int queue, err;
6283
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006284 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006285 err = mvpp2_rxq_init(port, port->rxqs[queue]);
6286 if (err)
6287 goto err_cleanup;
6288 }
6289 return 0;
6290
6291err_cleanup:
6292 mvpp2_cleanup_rxqs(port);
6293 return err;
6294}
6295
6296/* Init all tx queues for port */
6297static int mvpp2_setup_txqs(struct mvpp2_port *port)
6298{
6299 struct mvpp2_tx_queue *txq;
6300 int queue, err;
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 err = mvpp2_txq_init(port, txq);
6305 if (err)
6306 goto err_cleanup;
6307 }
6308
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006309 if (port->has_tx_irqs) {
6310 mvpp2_tx_time_coal_set(port);
6311 for (queue = 0; queue < port->ntxqs; queue++) {
6312 txq = port->txqs[queue];
6313 mvpp2_tx_pkts_coal_set(port, txq);
6314 }
6315 }
6316
Marcin Wojtas3f518502014-07-10 16:52:13 -03006317 on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
6318 return 0;
6319
6320err_cleanup:
6321 mvpp2_cleanup_txqs(port);
6322 return err;
6323}
6324
6325/* The callback for per-port interrupt */
6326static irqreturn_t mvpp2_isr(int irq, void *dev_id)
6327{
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006328 struct mvpp2_queue_vector *qv = dev_id;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006329
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006330 mvpp2_qvec_interrupt_disable(qv);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006331
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006332 napi_schedule(&qv->napi);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006333
6334 return IRQ_HANDLED;
6335}
6336
Antoine Tenartfd3651b2017-09-01 11:04:54 +02006337/* Per-port interrupt for link status changes */
6338static irqreturn_t mvpp2_link_status_isr(int irq, void *dev_id)
6339{
6340 struct mvpp2_port *port = (struct mvpp2_port *)dev_id;
6341 struct net_device *dev = port->dev;
6342 bool event = false, link = false;
6343 u32 val;
6344
6345 mvpp22_gop_mask_irq(port);
6346
6347 if (port->gop_id == 0 &&
6348 port->phy_interface == PHY_INTERFACE_MODE_10GKR) {
6349 val = readl(port->base + MVPP22_XLG_INT_STAT);
6350 if (val & MVPP22_XLG_INT_STAT_LINK) {
6351 event = true;
6352 val = readl(port->base + MVPP22_XLG_STATUS);
6353 if (val & MVPP22_XLG_STATUS_LINK_UP)
6354 link = true;
6355 }
6356 } else if (phy_interface_mode_is_rgmii(port->phy_interface) ||
6357 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
6358 val = readl(port->base + MVPP22_GMAC_INT_STAT);
6359 if (val & MVPP22_GMAC_INT_STAT_LINK) {
6360 event = true;
6361 val = readl(port->base + MVPP2_GMAC_STATUS0);
6362 if (val & MVPP2_GMAC_STATUS0_LINK_UP)
6363 link = true;
6364 }
6365 }
6366
6367 if (!netif_running(dev) || !event)
6368 goto handled;
6369
6370 if (link) {
6371 mvpp2_interrupts_enable(port);
6372
6373 mvpp2_egress_enable(port);
6374 mvpp2_ingress_enable(port);
6375 netif_carrier_on(dev);
6376 netif_tx_wake_all_queues(dev);
6377 } else {
6378 netif_tx_stop_all_queues(dev);
6379 netif_carrier_off(dev);
6380 mvpp2_ingress_disable(port);
6381 mvpp2_egress_disable(port);
6382
6383 mvpp2_interrupts_disable(port);
6384 }
6385
6386handled:
6387 mvpp22_gop_unmask_irq(port);
6388 return IRQ_HANDLED;
6389}
6390
Antoine Tenart65a2c092017-08-30 10:29:18 +02006391static void mvpp2_gmac_set_autoneg(struct mvpp2_port *port,
6392 struct phy_device *phydev)
6393{
6394 u32 val;
6395
6396 if (port->phy_interface != PHY_INTERFACE_MODE_RGMII &&
6397 port->phy_interface != PHY_INTERFACE_MODE_RGMII_ID &&
6398 port->phy_interface != PHY_INTERFACE_MODE_RGMII_RXID &&
6399 port->phy_interface != PHY_INTERFACE_MODE_RGMII_TXID &&
6400 port->phy_interface != PHY_INTERFACE_MODE_SGMII)
6401 return;
6402
6403 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
6404 val &= ~(MVPP2_GMAC_CONFIG_MII_SPEED |
6405 MVPP2_GMAC_CONFIG_GMII_SPEED |
6406 MVPP2_GMAC_CONFIG_FULL_DUPLEX |
6407 MVPP2_GMAC_AN_SPEED_EN |
6408 MVPP2_GMAC_AN_DUPLEX_EN);
6409
6410 if (phydev->duplex)
6411 val |= MVPP2_GMAC_CONFIG_FULL_DUPLEX;
6412
6413 if (phydev->speed == SPEED_1000)
6414 val |= MVPP2_GMAC_CONFIG_GMII_SPEED;
6415 else if (phydev->speed == SPEED_100)
6416 val |= MVPP2_GMAC_CONFIG_MII_SPEED;
6417
6418 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
Antoine Tenart65a2c092017-08-30 10:29:18 +02006419}
6420
Marcin Wojtas3f518502014-07-10 16:52:13 -03006421/* Adjust link */
6422static void mvpp2_link_event(struct net_device *dev)
6423{
6424 struct mvpp2_port *port = netdev_priv(dev);
Philippe Reynes8e072692016-06-28 00:08:11 +02006425 struct phy_device *phydev = dev->phydev;
Antoine Tenart89273bc2017-08-30 10:29:19 +02006426 bool link_reconfigured = false;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006427 u32 val;
6428
6429 if (phydev->link) {
Antoine Tenart89273bc2017-08-30 10:29:19 +02006430 if (port->phy_interface != phydev->interface && port->comphy) {
6431 /* disable current port for reconfiguration */
6432 mvpp2_interrupts_disable(port);
6433 netif_carrier_off(port->dev);
6434 mvpp2_port_disable(port);
6435 phy_power_off(port->comphy);
6436
6437 /* comphy reconfiguration */
6438 port->phy_interface = phydev->interface;
6439 mvpp22_comphy_init(port);
6440
6441 /* gop/mac reconfiguration */
6442 mvpp22_gop_init(port);
6443 mvpp2_port_mii_set(port);
6444
6445 link_reconfigured = true;
6446 }
6447
Marcin Wojtas3f518502014-07-10 16:52:13 -03006448 if ((port->speed != phydev->speed) ||
6449 (port->duplex != phydev->duplex)) {
Antoine Tenart65a2c092017-08-30 10:29:18 +02006450 mvpp2_gmac_set_autoneg(port, phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006451
6452 port->duplex = phydev->duplex;
6453 port->speed = phydev->speed;
6454 }
6455 }
6456
Antoine Tenart89273bc2017-08-30 10:29:19 +02006457 if (phydev->link != port->link || link_reconfigured) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006458 port->link = phydev->link;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006459
Marcin Wojtas3f518502014-07-10 16:52:13 -03006460 if (phydev->link) {
Antoine Tenart65a2c092017-08-30 10:29:18 +02006461 if (port->phy_interface == PHY_INTERFACE_MODE_RGMII ||
6462 port->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
6463 port->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID ||
6464 port->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID ||
6465 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
6466 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
6467 val |= (MVPP2_GMAC_FORCE_LINK_PASS |
6468 MVPP2_GMAC_FORCE_LINK_DOWN);
6469 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
6470 }
Antoine Tenartf55744a2017-08-30 10:29:17 +02006471
6472 mvpp2_interrupts_enable(port);
6473 mvpp2_port_enable(port);
6474
Marcin Wojtas3f518502014-07-10 16:52:13 -03006475 mvpp2_egress_enable(port);
6476 mvpp2_ingress_enable(port);
Antoine Tenartf55744a2017-08-30 10:29:17 +02006477 netif_carrier_on(dev);
6478 netif_tx_wake_all_queues(dev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006479 } else {
Antoine Tenart968b2112017-08-30 10:29:16 +02006480 port->duplex = -1;
6481 port->speed = 0;
6482
Antoine Tenartf55744a2017-08-30 10:29:17 +02006483 netif_tx_stop_all_queues(dev);
6484 netif_carrier_off(dev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006485 mvpp2_ingress_disable(port);
6486 mvpp2_egress_disable(port);
Antoine Tenartf55744a2017-08-30 10:29:17 +02006487
6488 mvpp2_port_disable(port);
6489 mvpp2_interrupts_disable(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006490 }
Antoine Tenart968b2112017-08-30 10:29:16 +02006491
Marcin Wojtas3f518502014-07-10 16:52:13 -03006492 phy_print_status(phydev);
6493 }
6494}
6495
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006496static void mvpp2_timer_set(struct mvpp2_port_pcpu *port_pcpu)
6497{
6498 ktime_t interval;
6499
6500 if (!port_pcpu->timer_scheduled) {
6501 port_pcpu->timer_scheduled = true;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01006502 interval = MVPP2_TXDONE_HRTIMER_PERIOD_NS;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006503 hrtimer_start(&port_pcpu->tx_done_timer, interval,
6504 HRTIMER_MODE_REL_PINNED);
6505 }
6506}
6507
6508static void mvpp2_tx_proc_cb(unsigned long data)
6509{
6510 struct net_device *dev = (struct net_device *)data;
6511 struct mvpp2_port *port = netdev_priv(dev);
6512 struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
6513 unsigned int tx_todo, cause;
6514
6515 if (!netif_running(dev))
6516 return;
6517 port_pcpu->timer_scheduled = false;
6518
6519 /* Process all the Tx queues */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006520 cause = (1 << port->ntxqs) - 1;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006521 tx_todo = mvpp2_tx_done(port, cause, smp_processor_id());
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006522
6523 /* Set the timer in case not all the packets were processed */
6524 if (tx_todo)
6525 mvpp2_timer_set(port_pcpu);
6526}
6527
6528static enum hrtimer_restart mvpp2_hr_timer_cb(struct hrtimer *timer)
6529{
6530 struct mvpp2_port_pcpu *port_pcpu = container_of(timer,
6531 struct mvpp2_port_pcpu,
6532 tx_done_timer);
6533
6534 tasklet_schedule(&port_pcpu->tx_done_tasklet);
6535
6536 return HRTIMER_NORESTART;
6537}
6538
Marcin Wojtas3f518502014-07-10 16:52:13 -03006539/* Main RX/TX processing routines */
6540
6541/* Display more error info */
6542static void mvpp2_rx_error(struct mvpp2_port *port,
6543 struct mvpp2_rx_desc *rx_desc)
6544{
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006545 u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
6546 size_t sz = mvpp2_rxdesc_size_get(port, rx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006547
6548 switch (status & MVPP2_RXD_ERR_CODE_MASK) {
6549 case MVPP2_RXD_ERR_CRC:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006550 netdev_err(port->dev, "bad rx status %08x (crc error), size=%zu\n",
6551 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006552 break;
6553 case MVPP2_RXD_ERR_OVERRUN:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006554 netdev_err(port->dev, "bad rx status %08x (overrun error), size=%zu\n",
6555 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006556 break;
6557 case MVPP2_RXD_ERR_RESOURCE:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006558 netdev_err(port->dev, "bad rx status %08x (resource error), size=%zu\n",
6559 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006560 break;
6561 }
6562}
6563
6564/* Handle RX checksum offload */
6565static void mvpp2_rx_csum(struct mvpp2_port *port, u32 status,
6566 struct sk_buff *skb)
6567{
6568 if (((status & MVPP2_RXD_L3_IP4) &&
6569 !(status & MVPP2_RXD_IP4_HEADER_ERR)) ||
6570 (status & MVPP2_RXD_L3_IP6))
6571 if (((status & MVPP2_RXD_L4_UDP) ||
6572 (status & MVPP2_RXD_L4_TCP)) &&
6573 (status & MVPP2_RXD_L4_CSUM_OK)) {
6574 skb->csum = 0;
6575 skb->ip_summed = CHECKSUM_UNNECESSARY;
6576 return;
6577 }
6578
6579 skb->ip_summed = CHECKSUM_NONE;
6580}
6581
6582/* Reuse skb if possible, or allocate a new skb and add it to BM pool */
6583static int mvpp2_rx_refill(struct mvpp2_port *port,
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006584 struct mvpp2_bm_pool *bm_pool, int pool)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006585{
Thomas Petazzoni20396132017-03-07 16:53:00 +01006586 dma_addr_t dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01006587 phys_addr_t phys_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006588 void *buf;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006589
Marcin Wojtas3f518502014-07-10 16:52:13 -03006590 /* No recycle or too many buffers are in use, so allocate a new skb */
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01006591 buf = mvpp2_buf_alloc(port, bm_pool, &dma_addr, &phys_addr,
6592 GFP_ATOMIC);
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006593 if (!buf)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006594 return -ENOMEM;
6595
Thomas Petazzoni7d7627b2017-06-22 14:23:20 +02006596 mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
Thomas Petazzoni7ef7e1d2017-02-21 11:28:07 +01006597
Marcin Wojtas3f518502014-07-10 16:52:13 -03006598 return 0;
6599}
6600
6601/* Handle tx checksum */
6602static u32 mvpp2_skb_tx_csum(struct mvpp2_port *port, struct sk_buff *skb)
6603{
6604 if (skb->ip_summed == CHECKSUM_PARTIAL) {
6605 int ip_hdr_len = 0;
6606 u8 l4_proto;
6607
6608 if (skb->protocol == htons(ETH_P_IP)) {
6609 struct iphdr *ip4h = ip_hdr(skb);
6610
6611 /* Calculate IPv4 checksum and L4 checksum */
6612 ip_hdr_len = ip4h->ihl;
6613 l4_proto = ip4h->protocol;
6614 } else if (skb->protocol == htons(ETH_P_IPV6)) {
6615 struct ipv6hdr *ip6h = ipv6_hdr(skb);
6616
6617 /* Read l4_protocol from one of IPv6 extra headers */
6618 if (skb_network_header_len(skb) > 0)
6619 ip_hdr_len = (skb_network_header_len(skb) >> 2);
6620 l4_proto = ip6h->nexthdr;
6621 } else {
6622 return MVPP2_TXD_L4_CSUM_NOT;
6623 }
6624
6625 return mvpp2_txq_desc_csum(skb_network_offset(skb),
6626 skb->protocol, ip_hdr_len, l4_proto);
6627 }
6628
6629 return MVPP2_TXD_L4_CSUM_NOT | MVPP2_TXD_IP_CSUM_DISABLE;
6630}
6631
Marcin Wojtas3f518502014-07-10 16:52:13 -03006632/* Main rx processing */
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006633static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi,
6634 int rx_todo, struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006635{
6636 struct net_device *dev = port->dev;
Marcin Wojtasb5015852015-12-03 15:20:51 +01006637 int rx_received;
6638 int rx_done = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006639 u32 rcvd_pkts = 0;
6640 u32 rcvd_bytes = 0;
6641
6642 /* Get number of received packets and clamp the to-do */
6643 rx_received = mvpp2_rxq_received(port, rxq->id);
6644 if (rx_todo > rx_received)
6645 rx_todo = rx_received;
6646
Marcin Wojtasb5015852015-12-03 15:20:51 +01006647 while (rx_done < rx_todo) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006648 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
6649 struct mvpp2_bm_pool *bm_pool;
6650 struct sk_buff *skb;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006651 unsigned int frag_size;
Thomas Petazzoni20396132017-03-07 16:53:00 +01006652 dma_addr_t dma_addr;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006653 phys_addr_t phys_addr;
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006654 u32 rx_status;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006655 int pool, rx_bytes, err;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006656 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006657
Marcin Wojtasb5015852015-12-03 15:20:51 +01006658 rx_done++;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006659 rx_status = mvpp2_rxdesc_status_get(port, rx_desc);
6660 rx_bytes = mvpp2_rxdesc_size_get(port, rx_desc);
6661 rx_bytes -= MVPP2_MH_SIZE;
6662 dma_addr = mvpp2_rxdesc_dma_addr_get(port, rx_desc);
6663 phys_addr = mvpp2_rxdesc_cookie_get(port, rx_desc);
6664 data = (void *)phys_to_virt(phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006665
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006666 pool = (rx_status & MVPP2_RXD_BM_POOL_ID_MASK) >>
6667 MVPP2_RXD_BM_POOL_ID_OFFS;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006668 bm_pool = &port->priv->bm_pools[pool];
Marcin Wojtas3f518502014-07-10 16:52:13 -03006669
6670 /* In case of an error, release the requested buffer pointer
6671 * to the Buffer Manager. This request process is controlled
6672 * by the hardware, and the information about the buffer is
6673 * comprised by the RX descriptor.
6674 */
6675 if (rx_status & MVPP2_RXD_ERR_SUMMARY) {
Markus Elfring8a524882017-04-17 10:52:02 +02006676err_drop_frame:
Marcin Wojtas3f518502014-07-10 16:52:13 -03006677 dev->stats.rx_errors++;
6678 mvpp2_rx_error(port, rx_desc);
Marcin Wojtasb5015852015-12-03 15:20:51 +01006679 /* Return the buffer to the pool */
Thomas Petazzoni7d7627b2017-06-22 14:23:20 +02006680 mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006681 continue;
6682 }
6683
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006684 if (bm_pool->frag_size > PAGE_SIZE)
6685 frag_size = 0;
6686 else
6687 frag_size = bm_pool->frag_size;
6688
6689 skb = build_skb(data, frag_size);
6690 if (!skb) {
6691 netdev_warn(port->dev, "skb build failed\n");
6692 goto err_drop_frame;
6693 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006694
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006695 err = mvpp2_rx_refill(port, bm_pool, pool);
Marcin Wojtasb5015852015-12-03 15:20:51 +01006696 if (err) {
6697 netdev_err(port->dev, "failed to refill BM pools\n");
6698 goto err_drop_frame;
6699 }
6700
Thomas Petazzoni20396132017-03-07 16:53:00 +01006701 dma_unmap_single(dev->dev.parent, dma_addr,
Marcin Wojtas4229d502015-12-03 15:20:50 +01006702 bm_pool->buf_size, DMA_FROM_DEVICE);
6703
Marcin Wojtas3f518502014-07-10 16:52:13 -03006704 rcvd_pkts++;
6705 rcvd_bytes += rx_bytes;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006706
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006707 skb_reserve(skb, MVPP2_MH_SIZE + NET_SKB_PAD);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006708 skb_put(skb, rx_bytes);
6709 skb->protocol = eth_type_trans(skb, dev);
6710 mvpp2_rx_csum(port, rx_status, skb);
6711
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006712 napi_gro_receive(napi, skb);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006713 }
6714
6715 if (rcvd_pkts) {
6716 struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
6717
6718 u64_stats_update_begin(&stats->syncp);
6719 stats->rx_packets += rcvd_pkts;
6720 stats->rx_bytes += rcvd_bytes;
6721 u64_stats_update_end(&stats->syncp);
6722 }
6723
6724 /* Update Rx queue management counters */
6725 wmb();
Marcin Wojtasb5015852015-12-03 15:20:51 +01006726 mvpp2_rxq_status_update(port, rxq->id, rx_done, rx_done);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006727
6728 return rx_todo;
6729}
6730
6731static inline void
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006732tx_desc_unmap_put(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006733 struct mvpp2_tx_desc *desc)
6734{
Antoine Tenart20920262017-10-23 15:24:30 +02006735 struct mvpp2_txq_pcpu *txq_pcpu = this_cpu_ptr(txq->pcpu);
6736
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006737 dma_addr_t buf_dma_addr =
6738 mvpp2_txdesc_dma_addr_get(port, desc);
6739 size_t buf_sz =
6740 mvpp2_txdesc_size_get(port, desc);
Antoine Tenart20920262017-10-23 15:24:30 +02006741 if (!IS_TSO_HEADER(txq_pcpu, buf_dma_addr))
6742 dma_unmap_single(port->dev->dev.parent, buf_dma_addr,
6743 buf_sz, DMA_TO_DEVICE);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006744 mvpp2_txq_desc_put(txq);
6745}
6746
6747/* Handle tx fragmentation processing */
6748static int mvpp2_tx_frag_process(struct mvpp2_port *port, struct sk_buff *skb,
6749 struct mvpp2_tx_queue *aggr_txq,
6750 struct mvpp2_tx_queue *txq)
6751{
6752 struct mvpp2_txq_pcpu *txq_pcpu = this_cpu_ptr(txq->pcpu);
6753 struct mvpp2_tx_desc *tx_desc;
6754 int i;
Thomas Petazzoni20396132017-03-07 16:53:00 +01006755 dma_addr_t buf_dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006756
6757 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
6758 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6759 void *addr = page_address(frag->page.p) + frag->page_offset;
6760
6761 tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006762 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6763 mvpp2_txdesc_size_set(port, tx_desc, frag->size);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006764
Thomas Petazzoni20396132017-03-07 16:53:00 +01006765 buf_dma_addr = dma_map_single(port->dev->dev.parent, addr,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006766 frag->size,
6767 DMA_TO_DEVICE);
Thomas Petazzoni20396132017-03-07 16:53:00 +01006768 if (dma_mapping_error(port->dev->dev.parent, buf_dma_addr)) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006769 mvpp2_txq_desc_put(txq);
Markus Elfring32bae632017-04-17 11:36:34 +02006770 goto cleanup;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006771 }
6772
Antoine Tenart6eb5d372017-10-30 11:23:33 +01006773 mvpp2_txdesc_dma_addr_set(port, tx_desc, buf_dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006774
6775 if (i == (skb_shinfo(skb)->nr_frags - 1)) {
6776 /* Last descriptor */
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006777 mvpp2_txdesc_cmd_set(port, tx_desc,
6778 MVPP2_TXD_L_DESC);
6779 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006780 } else {
6781 /* Descriptor in the middle: Not First, Not Last */
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006782 mvpp2_txdesc_cmd_set(port, tx_desc, 0);
6783 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006784 }
6785 }
6786
6787 return 0;
Markus Elfring32bae632017-04-17 11:36:34 +02006788cleanup:
Marcin Wojtas3f518502014-07-10 16:52:13 -03006789 /* Release all descriptors that were used to map fragments of
6790 * this packet, as well as the corresponding DMA mappings
6791 */
6792 for (i = i - 1; i >= 0; i--) {
6793 tx_desc = txq->descs + i;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006794 tx_desc_unmap_put(port, txq, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006795 }
6796
6797 return -ENOMEM;
6798}
6799
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006800static inline void mvpp2_tso_put_hdr(struct sk_buff *skb,
6801 struct net_device *dev,
6802 struct mvpp2_tx_queue *txq,
6803 struct mvpp2_tx_queue *aggr_txq,
6804 struct mvpp2_txq_pcpu *txq_pcpu,
6805 int hdr_sz)
6806{
6807 struct mvpp2_port *port = netdev_priv(dev);
6808 struct mvpp2_tx_desc *tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
6809 dma_addr_t addr;
6810
6811 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6812 mvpp2_txdesc_size_set(port, tx_desc, hdr_sz);
6813
6814 addr = txq_pcpu->tso_headers_dma +
6815 txq_pcpu->txq_put_index * TSO_HEADER_SIZE;
Antoine Tenart6eb5d372017-10-30 11:23:33 +01006816 mvpp2_txdesc_dma_addr_set(port, tx_desc, addr);
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006817
6818 mvpp2_txdesc_cmd_set(port, tx_desc, mvpp2_skb_tx_csum(port, skb) |
6819 MVPP2_TXD_F_DESC |
6820 MVPP2_TXD_PADDING_DISABLE);
6821 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
6822}
6823
6824static inline int mvpp2_tso_put_data(struct sk_buff *skb,
6825 struct net_device *dev, struct tso_t *tso,
6826 struct mvpp2_tx_queue *txq,
6827 struct mvpp2_tx_queue *aggr_txq,
6828 struct mvpp2_txq_pcpu *txq_pcpu,
6829 int sz, bool left, bool last)
6830{
6831 struct mvpp2_port *port = netdev_priv(dev);
6832 struct mvpp2_tx_desc *tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
6833 dma_addr_t buf_dma_addr;
6834
6835 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6836 mvpp2_txdesc_size_set(port, tx_desc, sz);
6837
6838 buf_dma_addr = dma_map_single(dev->dev.parent, tso->data, sz,
6839 DMA_TO_DEVICE);
6840 if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
6841 mvpp2_txq_desc_put(txq);
6842 return -ENOMEM;
6843 }
6844
Antoine Tenart6eb5d372017-10-30 11:23:33 +01006845 mvpp2_txdesc_dma_addr_set(port, tx_desc, buf_dma_addr);
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006846
6847 if (!left) {
6848 mvpp2_txdesc_cmd_set(port, tx_desc, MVPP2_TXD_L_DESC);
6849 if (last) {
6850 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
6851 return 0;
6852 }
6853 } else {
6854 mvpp2_txdesc_cmd_set(port, tx_desc, 0);
6855 }
6856
6857 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
6858 return 0;
6859}
6860
6861static int mvpp2_tx_tso(struct sk_buff *skb, struct net_device *dev,
6862 struct mvpp2_tx_queue *txq,
6863 struct mvpp2_tx_queue *aggr_txq,
6864 struct mvpp2_txq_pcpu *txq_pcpu)
6865{
6866 struct mvpp2_port *port = netdev_priv(dev);
6867 struct tso_t tso;
6868 int hdr_sz = skb_transport_offset(skb) + tcp_hdrlen(skb);
6869 int i, len, descs = 0;
6870
6871 /* Check number of available descriptors */
6872 if (mvpp2_aggr_desc_num_check(port->priv, aggr_txq,
6873 tso_count_descs(skb)) ||
6874 mvpp2_txq_reserved_desc_num_proc(port->priv, txq, txq_pcpu,
6875 tso_count_descs(skb)))
6876 return 0;
6877
6878 tso_start(skb, &tso);
6879 len = skb->len - hdr_sz;
6880 while (len > 0) {
6881 int left = min_t(int, skb_shinfo(skb)->gso_size, len);
6882 char *hdr = txq_pcpu->tso_headers +
6883 txq_pcpu->txq_put_index * TSO_HEADER_SIZE;
6884
6885 len -= left;
6886 descs++;
6887
6888 tso_build_hdr(skb, hdr, &tso, left, len == 0);
6889 mvpp2_tso_put_hdr(skb, dev, txq, aggr_txq, txq_pcpu, hdr_sz);
6890
6891 while (left > 0) {
6892 int sz = min_t(int, tso.size, left);
6893 left -= sz;
6894 descs++;
6895
6896 if (mvpp2_tso_put_data(skb, dev, &tso, txq, aggr_txq,
6897 txq_pcpu, sz, left, len == 0))
6898 goto release;
6899 tso_build_data(skb, &tso, sz);
6900 }
6901 }
6902
6903 return descs;
6904
6905release:
6906 for (i = descs - 1; i >= 0; i--) {
6907 struct mvpp2_tx_desc *tx_desc = txq->descs + i;
6908 tx_desc_unmap_put(port, txq, tx_desc);
6909 }
6910 return 0;
6911}
6912
Marcin Wojtas3f518502014-07-10 16:52:13 -03006913/* Main tx processing */
6914static int mvpp2_tx(struct sk_buff *skb, struct net_device *dev)
6915{
6916 struct mvpp2_port *port = netdev_priv(dev);
6917 struct mvpp2_tx_queue *txq, *aggr_txq;
6918 struct mvpp2_txq_pcpu *txq_pcpu;
6919 struct mvpp2_tx_desc *tx_desc;
Thomas Petazzoni20396132017-03-07 16:53:00 +01006920 dma_addr_t buf_dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006921 int frags = 0;
6922 u16 txq_id;
6923 u32 tx_cmd;
6924
6925 txq_id = skb_get_queue_mapping(skb);
6926 txq = port->txqs[txq_id];
6927 txq_pcpu = this_cpu_ptr(txq->pcpu);
6928 aggr_txq = &port->priv->aggr_txqs[smp_processor_id()];
6929
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006930 if (skb_is_gso(skb)) {
6931 frags = mvpp2_tx_tso(skb, dev, txq, aggr_txq, txq_pcpu);
6932 goto out;
6933 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006934 frags = skb_shinfo(skb)->nr_frags + 1;
6935
6936 /* Check number of available descriptors */
6937 if (mvpp2_aggr_desc_num_check(port->priv, aggr_txq, frags) ||
6938 mvpp2_txq_reserved_desc_num_proc(port->priv, txq,
6939 txq_pcpu, frags)) {
6940 frags = 0;
6941 goto out;
6942 }
6943
6944 /* Get a descriptor for the first part of the packet */
6945 tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006946 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6947 mvpp2_txdesc_size_set(port, tx_desc, skb_headlen(skb));
Marcin Wojtas3f518502014-07-10 16:52:13 -03006948
Thomas Petazzoni20396132017-03-07 16:53:00 +01006949 buf_dma_addr = dma_map_single(dev->dev.parent, skb->data,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006950 skb_headlen(skb), DMA_TO_DEVICE);
Thomas Petazzoni20396132017-03-07 16:53:00 +01006951 if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006952 mvpp2_txq_desc_put(txq);
6953 frags = 0;
6954 goto out;
6955 }
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006956
Antoine Tenart6eb5d372017-10-30 11:23:33 +01006957 mvpp2_txdesc_dma_addr_set(port, tx_desc, buf_dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006958
6959 tx_cmd = mvpp2_skb_tx_csum(port, skb);
6960
6961 if (frags == 1) {
6962 /* First and Last descriptor */
6963 tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_L_DESC;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006964 mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
6965 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006966 } else {
6967 /* First but not Last */
6968 tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_PADDING_DISABLE;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006969 mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
6970 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006971
6972 /* Continue with other skb fragments */
6973 if (mvpp2_tx_frag_process(port, skb, aggr_txq, txq)) {
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006974 tx_desc_unmap_put(port, txq, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006975 frags = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006976 }
6977 }
6978
Marcin Wojtas3f518502014-07-10 16:52:13 -03006979out:
6980 if (frags > 0) {
6981 struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006982 struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
6983
6984 txq_pcpu->reserved_num -= frags;
6985 txq_pcpu->count += frags;
6986 aggr_txq->count += frags;
6987
6988 /* Enable transmit */
6989 wmb();
6990 mvpp2_aggr_txq_pend_desc_add(port, frags);
6991
Antoine Tenart1d17db02017-10-30 11:23:31 +01006992 if (txq_pcpu->count >= txq_pcpu->stop_threshold)
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006993 netif_tx_stop_queue(nq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006994
6995 u64_stats_update_begin(&stats->syncp);
6996 stats->tx_packets++;
6997 stats->tx_bytes += skb->len;
6998 u64_stats_update_end(&stats->syncp);
6999 } else {
7000 dev->stats.tx_dropped++;
7001 dev_kfree_skb_any(skb);
7002 }
7003
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007004 /* Finalize TX processing */
Antoine Tenart082297e2017-10-23 15:24:31 +02007005 if (!port->has_tx_irqs && txq_pcpu->count >= txq->done_pkts_coal)
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007006 mvpp2_txq_done(port, txq, txq_pcpu);
7007
7008 /* Set the timer in case not all frags were processed */
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007009 if (!port->has_tx_irqs && txq_pcpu->count <= frags &&
7010 txq_pcpu->count > 0) {
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007011 struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
7012
7013 mvpp2_timer_set(port_pcpu);
7014 }
7015
Marcin Wojtas3f518502014-07-10 16:52:13 -03007016 return NETDEV_TX_OK;
7017}
7018
7019static inline void mvpp2_cause_error(struct net_device *dev, int cause)
7020{
7021 if (cause & MVPP2_CAUSE_FCS_ERR_MASK)
7022 netdev_err(dev, "FCS error\n");
7023 if (cause & MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK)
7024 netdev_err(dev, "rx fifo overrun error\n");
7025 if (cause & MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK)
7026 netdev_err(dev, "tx fifo underrun error\n");
7027}
7028
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007029static int mvpp2_poll(struct napi_struct *napi, int budget)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007030{
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007031 u32 cause_rx_tx, cause_rx, cause_tx, cause_misc;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007032 int rx_done = 0;
7033 struct mvpp2_port *port = netdev_priv(napi->dev);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007034 struct mvpp2_queue_vector *qv;
Thomas Petazzonia7868412017-03-07 16:53:13 +01007035 int cpu = smp_processor_id();
Marcin Wojtas3f518502014-07-10 16:52:13 -03007036
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007037 qv = container_of(napi, struct mvpp2_queue_vector, napi);
7038
Marcin Wojtas3f518502014-07-10 16:52:13 -03007039 /* Rx/Tx cause register
7040 *
7041 * Bits 0-15: each bit indicates received packets on the Rx queue
7042 * (bit 0 is for Rx queue 0).
7043 *
7044 * Bits 16-23: each bit indicates transmitted packets on the Tx queue
7045 * (bit 16 is for Tx queue 0).
7046 *
7047 * Each CPU has its own Rx/Tx cause register
7048 */
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007049 cause_rx_tx = mvpp2_percpu_read(port->priv, qv->sw_thread_id,
Thomas Petazzonia7868412017-03-07 16:53:13 +01007050 MVPP2_ISR_RX_TX_CAUSE_REG(port->id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03007051
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007052 cause_misc = cause_rx_tx & MVPP2_CAUSE_MISC_SUM_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007053 if (cause_misc) {
7054 mvpp2_cause_error(port->dev, cause_misc);
7055
7056 /* Clear the cause register */
7057 mvpp2_write(port->priv, MVPP2_ISR_MISC_CAUSE_REG, 0);
Thomas Petazzonia7868412017-03-07 16:53:13 +01007058 mvpp2_percpu_write(port->priv, cpu,
7059 MVPP2_ISR_RX_TX_CAUSE_REG(port->id),
7060 cause_rx_tx & ~MVPP2_CAUSE_MISC_SUM_MASK);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007061 }
7062
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007063 cause_tx = cause_rx_tx & MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
7064 if (cause_tx) {
7065 cause_tx >>= MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_OFFSET;
7066 mvpp2_tx_done(port, cause_tx, qv->sw_thread_id);
7067 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03007068
7069 /* Process RX packets */
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007070 cause_rx = cause_rx_tx & MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
7071 cause_rx <<= qv->first_rxq;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007072 cause_rx |= qv->pending_cause_rx;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007073 while (cause_rx && budget > 0) {
7074 int count;
7075 struct mvpp2_rx_queue *rxq;
7076
7077 rxq = mvpp2_get_rx_queue(port, cause_rx);
7078 if (!rxq)
7079 break;
7080
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007081 count = mvpp2_rx(port, napi, budget, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007082 rx_done += count;
7083 budget -= count;
7084 if (budget > 0) {
7085 /* Clear the bit associated to this Rx queue
7086 * so that next iteration will continue from
7087 * the next Rx queue.
7088 */
7089 cause_rx &= ~(1 << rxq->logic_rxq);
7090 }
7091 }
7092
7093 if (budget > 0) {
7094 cause_rx = 0;
Eric Dumazet6ad20162017-01-30 08:22:01 -08007095 napi_complete_done(napi, rx_done);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007096
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007097 mvpp2_qvec_interrupt_enable(qv);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007098 }
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007099 qv->pending_cause_rx = cause_rx;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007100 return rx_done;
7101}
7102
7103/* Set hw internals when starting port */
7104static void mvpp2_start_dev(struct mvpp2_port *port)
7105{
Philippe Reynes8e072692016-06-28 00:08:11 +02007106 struct net_device *ndev = port->dev;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007107 int i;
Philippe Reynes8e072692016-06-28 00:08:11 +02007108
Stefan Chulski76eb1b12017-08-22 19:08:26 +02007109 if (port->gop_id == 0 &&
7110 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
7111 port->phy_interface == PHY_INTERFACE_MODE_10GKR))
7112 mvpp2_xlg_max_rx_size_set(port);
7113 else
7114 mvpp2_gmac_max_rx_size_set(port);
7115
Marcin Wojtas3f518502014-07-10 16:52:13 -03007116 mvpp2_txp_max_tx_size_set(port);
7117
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007118 for (i = 0; i < port->nqvecs; i++)
7119 napi_enable(&port->qvecs[i].napi);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007120
7121 /* Enable interrupts on all CPUs */
7122 mvpp2_interrupts_enable(port);
7123
Antoine Tenart542897d2017-08-30 10:29:15 +02007124 if (port->priv->hw_version == MVPP22) {
7125 mvpp22_comphy_init(port);
Antoine Ténartf84bf382017-08-22 19:08:27 +02007126 mvpp22_gop_init(port);
Antoine Tenart542897d2017-08-30 10:29:15 +02007127 }
Antoine Ténartf84bf382017-08-22 19:08:27 +02007128
Antoine Ténart2055d622017-08-22 19:08:23 +02007129 mvpp2_port_mii_set(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007130 mvpp2_port_enable(port);
Antoine Tenart5997c862017-09-01 11:04:53 +02007131 if (ndev->phydev)
7132 phy_start(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007133 netif_tx_start_all_queues(port->dev);
7134}
7135
7136/* Set hw internals when stopping port */
7137static void mvpp2_stop_dev(struct mvpp2_port *port)
7138{
Philippe Reynes8e072692016-06-28 00:08:11 +02007139 struct net_device *ndev = port->dev;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007140 int i;
Philippe Reynes8e072692016-06-28 00:08:11 +02007141
Marcin Wojtas3f518502014-07-10 16:52:13 -03007142 /* Stop new packets from arriving to RXQs */
7143 mvpp2_ingress_disable(port);
7144
7145 mdelay(10);
7146
7147 /* Disable interrupts on all CPUs */
7148 mvpp2_interrupts_disable(port);
7149
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007150 for (i = 0; i < port->nqvecs; i++)
7151 napi_disable(&port->qvecs[i].napi);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007152
7153 netif_carrier_off(port->dev);
7154 netif_tx_stop_all_queues(port->dev);
7155
7156 mvpp2_egress_disable(port);
7157 mvpp2_port_disable(port);
Antoine Tenart5997c862017-09-01 11:04:53 +02007158 if (ndev->phydev)
7159 phy_stop(ndev->phydev);
Antoine Tenart542897d2017-08-30 10:29:15 +02007160 phy_power_off(port->comphy);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007161}
7162
Marcin Wojtas3f518502014-07-10 16:52:13 -03007163static int mvpp2_check_ringparam_valid(struct net_device *dev,
7164 struct ethtool_ringparam *ring)
7165{
7166 u16 new_rx_pending = ring->rx_pending;
7167 u16 new_tx_pending = ring->tx_pending;
7168
7169 if (ring->rx_pending == 0 || ring->tx_pending == 0)
7170 return -EINVAL;
7171
Yan Markman7cf87e42017-12-11 09:13:26 +01007172 if (ring->rx_pending > MVPP2_MAX_RXD_MAX)
7173 new_rx_pending = MVPP2_MAX_RXD_MAX;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007174 else if (!IS_ALIGNED(ring->rx_pending, 16))
7175 new_rx_pending = ALIGN(ring->rx_pending, 16);
7176
Yan Markman7cf87e42017-12-11 09:13:26 +01007177 if (ring->tx_pending > MVPP2_MAX_TXD_MAX)
7178 new_tx_pending = MVPP2_MAX_TXD_MAX;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007179 else if (!IS_ALIGNED(ring->tx_pending, 32))
7180 new_tx_pending = ALIGN(ring->tx_pending, 32);
7181
Antoine Tenart76e583c2017-11-28 14:19:51 +01007182 /* The Tx ring size cannot be smaller than the minimum number of
7183 * descriptors needed for TSO.
7184 */
7185 if (new_tx_pending < MVPP2_MAX_SKB_DESCS)
7186 new_tx_pending = ALIGN(MVPP2_MAX_SKB_DESCS, 32);
7187
Marcin Wojtas3f518502014-07-10 16:52:13 -03007188 if (ring->rx_pending != new_rx_pending) {
7189 netdev_info(dev, "illegal Rx ring size value %d, round to %d\n",
7190 ring->rx_pending, new_rx_pending);
7191 ring->rx_pending = new_rx_pending;
7192 }
7193
7194 if (ring->tx_pending != new_tx_pending) {
7195 netdev_info(dev, "illegal Tx ring size value %d, round to %d\n",
7196 ring->tx_pending, new_tx_pending);
7197 ring->tx_pending = new_tx_pending;
7198 }
7199
7200 return 0;
7201}
7202
Thomas Petazzoni26975822017-03-07 16:53:14 +01007203static void mvpp21_get_mac_address(struct mvpp2_port *port, unsigned char *addr)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007204{
7205 u32 mac_addr_l, mac_addr_m, mac_addr_h;
7206
7207 mac_addr_l = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
7208 mac_addr_m = readl(port->priv->lms_base + MVPP2_SRC_ADDR_MIDDLE);
7209 mac_addr_h = readl(port->priv->lms_base + MVPP2_SRC_ADDR_HIGH);
7210 addr[0] = (mac_addr_h >> 24) & 0xFF;
7211 addr[1] = (mac_addr_h >> 16) & 0xFF;
7212 addr[2] = (mac_addr_h >> 8) & 0xFF;
7213 addr[3] = mac_addr_h & 0xFF;
7214 addr[4] = mac_addr_m & 0xFF;
7215 addr[5] = (mac_addr_l >> MVPP2_GMAC_SA_LOW_OFFS) & 0xFF;
7216}
7217
7218static int mvpp2_phy_connect(struct mvpp2_port *port)
7219{
7220 struct phy_device *phy_dev;
7221
Antoine Tenart5997c862017-09-01 11:04:53 +02007222 /* No PHY is attached */
7223 if (!port->phy_node)
7224 return 0;
7225
Marcin Wojtas3f518502014-07-10 16:52:13 -03007226 phy_dev = of_phy_connect(port->dev, port->phy_node, mvpp2_link_event, 0,
7227 port->phy_interface);
7228 if (!phy_dev) {
7229 netdev_err(port->dev, "cannot connect to phy\n");
7230 return -ENODEV;
7231 }
7232 phy_dev->supported &= PHY_GBIT_FEATURES;
7233 phy_dev->advertising = phy_dev->supported;
7234
Marcin Wojtas3f518502014-07-10 16:52:13 -03007235 port->link = 0;
7236 port->duplex = 0;
7237 port->speed = 0;
7238
7239 return 0;
7240}
7241
7242static void mvpp2_phy_disconnect(struct mvpp2_port *port)
7243{
Philippe Reynes8e072692016-06-28 00:08:11 +02007244 struct net_device *ndev = port->dev;
7245
Antoine Tenart5997c862017-09-01 11:04:53 +02007246 if (!ndev->phydev)
7247 return;
7248
Philippe Reynes8e072692016-06-28 00:08:11 +02007249 phy_disconnect(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007250}
7251
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007252static int mvpp2_irqs_init(struct mvpp2_port *port)
7253{
7254 int err, i;
7255
7256 for (i = 0; i < port->nqvecs; i++) {
7257 struct mvpp2_queue_vector *qv = port->qvecs + i;
7258
Marc Zyngier13c249a2017-11-04 12:33:47 +00007259 if (qv->type == MVPP2_QUEUE_VECTOR_PRIVATE)
7260 irq_set_status_flags(qv->irq, IRQ_NO_BALANCING);
7261
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007262 err = request_irq(qv->irq, mvpp2_isr, 0, port->dev->name, qv);
7263 if (err)
7264 goto err;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007265
7266 if (qv->type == MVPP2_QUEUE_VECTOR_PRIVATE)
7267 irq_set_affinity_hint(qv->irq,
7268 cpumask_of(qv->sw_thread_id));
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007269 }
7270
7271 return 0;
7272err:
7273 for (i = 0; i < port->nqvecs; i++) {
7274 struct mvpp2_queue_vector *qv = port->qvecs + i;
7275
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007276 irq_set_affinity_hint(qv->irq, NULL);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007277 free_irq(qv->irq, qv);
7278 }
7279
7280 return err;
7281}
7282
7283static void mvpp2_irqs_deinit(struct mvpp2_port *port)
7284{
7285 int i;
7286
7287 for (i = 0; i < port->nqvecs; i++) {
7288 struct mvpp2_queue_vector *qv = port->qvecs + i;
7289
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007290 irq_set_affinity_hint(qv->irq, NULL);
Marc Zyngier13c249a2017-11-04 12:33:47 +00007291 irq_clear_status_flags(qv->irq, IRQ_NO_BALANCING);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007292 free_irq(qv->irq, qv);
7293 }
7294}
7295
Antoine Tenart1d7d15d2017-10-30 11:23:30 +01007296static void mvpp22_init_rss(struct mvpp2_port *port)
7297{
7298 struct mvpp2 *priv = port->priv;
7299 int i;
7300
7301 /* Set the table width: replace the whole classifier Rx queue number
7302 * with the ones configured in RSS table entries.
7303 */
7304 mvpp2_write(priv, MVPP22_RSS_INDEX, MVPP22_RSS_INDEX_TABLE(0));
7305 mvpp2_write(priv, MVPP22_RSS_WIDTH, 8);
7306
7307 /* Loop through the classifier Rx Queues and map them to a RSS table.
7308 * Map them all to the first table (0) by default.
7309 */
7310 for (i = 0; i < MVPP2_CLS_RX_QUEUES; i++) {
7311 mvpp2_write(priv, MVPP22_RSS_INDEX, MVPP22_RSS_INDEX_QUEUE(i));
7312 mvpp2_write(priv, MVPP22_RSS_TABLE,
7313 MVPP22_RSS_TABLE_POINTER(0));
7314 }
7315
7316 /* Configure the first table to evenly distribute the packets across
7317 * real Rx Queues. The table entries map a hash to an port Rx Queue.
7318 */
7319 for (i = 0; i < MVPP22_RSS_TABLE_ENTRIES; i++) {
7320 u32 sel = MVPP22_RSS_INDEX_TABLE(0) |
7321 MVPP22_RSS_INDEX_TABLE_ENTRY(i);
7322 mvpp2_write(priv, MVPP22_RSS_INDEX, sel);
7323
7324 mvpp2_write(priv, MVPP22_RSS_TABLE_ENTRY, i % port->nrxqs);
7325 }
7326
7327}
7328
Marcin Wojtas3f518502014-07-10 16:52:13 -03007329static int mvpp2_open(struct net_device *dev)
7330{
7331 struct mvpp2_port *port = netdev_priv(dev);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007332 struct mvpp2 *priv = port->priv;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007333 unsigned char mac_bcast[ETH_ALEN] = {
7334 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7335 int err;
7336
7337 err = mvpp2_prs_mac_da_accept(port->priv, port->id, mac_bcast, true);
7338 if (err) {
7339 netdev_err(dev, "mvpp2_prs_mac_da_accept BC failed\n");
7340 return err;
7341 }
7342 err = mvpp2_prs_mac_da_accept(port->priv, port->id,
7343 dev->dev_addr, true);
7344 if (err) {
7345 netdev_err(dev, "mvpp2_prs_mac_da_accept MC failed\n");
7346 return err;
7347 }
7348 err = mvpp2_prs_tag_mode_set(port->priv, port->id, MVPP2_TAG_TYPE_MH);
7349 if (err) {
7350 netdev_err(dev, "mvpp2_prs_tag_mode_set failed\n");
7351 return err;
7352 }
7353 err = mvpp2_prs_def_flow(port);
7354 if (err) {
7355 netdev_err(dev, "mvpp2_prs_def_flow failed\n");
7356 return err;
7357 }
7358
7359 /* Allocate the Rx/Tx queues */
7360 err = mvpp2_setup_rxqs(port);
7361 if (err) {
7362 netdev_err(port->dev, "cannot allocate Rx queues\n");
7363 return err;
7364 }
7365
7366 err = mvpp2_setup_txqs(port);
7367 if (err) {
7368 netdev_err(port->dev, "cannot allocate Tx queues\n");
7369 goto err_cleanup_rxqs;
7370 }
7371
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007372 err = mvpp2_irqs_init(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007373 if (err) {
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007374 netdev_err(port->dev, "cannot init IRQs\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03007375 goto err_cleanup_txqs;
7376 }
7377
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007378 if (priv->hw_version == MVPP22 && !port->phy_node && port->link_irq) {
7379 err = request_irq(port->link_irq, mvpp2_link_status_isr, 0,
7380 dev->name, port);
7381 if (err) {
7382 netdev_err(port->dev, "cannot request link IRQ %d\n",
7383 port->link_irq);
7384 goto err_free_irq;
7385 }
7386
7387 mvpp22_gop_setup_irq(port);
7388 }
7389
Marcin Wojtas3f518502014-07-10 16:52:13 -03007390 /* In default link is down */
7391 netif_carrier_off(port->dev);
7392
7393 err = mvpp2_phy_connect(port);
7394 if (err < 0)
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007395 goto err_free_link_irq;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007396
7397 /* Unmask interrupts on all CPUs */
7398 on_each_cpu(mvpp2_interrupts_unmask, port, 1);
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007399 mvpp2_shared_interrupt_mask_unmask(port, false);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007400
7401 mvpp2_start_dev(port);
7402
Antoine Tenart1d7d15d2017-10-30 11:23:30 +01007403 if (priv->hw_version == MVPP22)
7404 mvpp22_init_rss(port);
7405
Miquel Raynal118d6292017-11-06 22:56:53 +01007406 /* Start hardware statistics gathering */
Miquel Raynale5c500e2017-11-08 08:59:40 +01007407 queue_delayed_work(priv->stats_queue, &port->stats_work,
Miquel Raynal118d6292017-11-06 22:56:53 +01007408 MVPP2_MIB_COUNTERS_STATS_DELAY);
7409
Marcin Wojtas3f518502014-07-10 16:52:13 -03007410 return 0;
7411
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007412err_free_link_irq:
7413 if (priv->hw_version == MVPP22 && !port->phy_node && port->link_irq)
7414 free_irq(port->link_irq, port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007415err_free_irq:
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007416 mvpp2_irqs_deinit(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007417err_cleanup_txqs:
7418 mvpp2_cleanup_txqs(port);
7419err_cleanup_rxqs:
7420 mvpp2_cleanup_rxqs(port);
7421 return err;
7422}
7423
7424static int mvpp2_stop(struct net_device *dev)
7425{
7426 struct mvpp2_port *port = netdev_priv(dev);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007427 struct mvpp2_port_pcpu *port_pcpu;
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007428 struct mvpp2 *priv = port->priv;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007429 int cpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007430
7431 mvpp2_stop_dev(port);
7432 mvpp2_phy_disconnect(port);
7433
7434 /* Mask interrupts on all CPUs */
7435 on_each_cpu(mvpp2_interrupts_mask, port, 1);
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007436 mvpp2_shared_interrupt_mask_unmask(port, true);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007437
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007438 if (priv->hw_version == MVPP22 && !port->phy_node && port->link_irq)
7439 free_irq(port->link_irq, port);
7440
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007441 mvpp2_irqs_deinit(port);
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007442 if (!port->has_tx_irqs) {
7443 for_each_present_cpu(cpu) {
7444 port_pcpu = per_cpu_ptr(port->pcpu, cpu);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007445
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007446 hrtimer_cancel(&port_pcpu->tx_done_timer);
7447 port_pcpu->timer_scheduled = false;
7448 tasklet_kill(&port_pcpu->tx_done_tasklet);
7449 }
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007450 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03007451 mvpp2_cleanup_rxqs(port);
7452 mvpp2_cleanup_txqs(port);
7453
Miquel Raynale5c500e2017-11-08 08:59:40 +01007454 cancel_delayed_work_sync(&port->stats_work);
Miquel Raynal118d6292017-11-06 22:56:53 +01007455
Marcin Wojtas3f518502014-07-10 16:52:13 -03007456 return 0;
7457}
7458
7459static void mvpp2_set_rx_mode(struct net_device *dev)
7460{
7461 struct mvpp2_port *port = netdev_priv(dev);
7462 struct mvpp2 *priv = port->priv;
7463 struct netdev_hw_addr *ha;
7464 int id = port->id;
7465 bool allmulti = dev->flags & IFF_ALLMULTI;
7466
Mikulas Patocka7ac8ff92018-02-11 18:10:28 -05007467retry:
Marcin Wojtas3f518502014-07-10 16:52:13 -03007468 mvpp2_prs_mac_promisc_set(priv, id, dev->flags & IFF_PROMISC);
7469 mvpp2_prs_mac_multi_set(priv, id, MVPP2_PE_MAC_MC_ALL, allmulti);
7470 mvpp2_prs_mac_multi_set(priv, id, MVPP2_PE_MAC_MC_IP6, allmulti);
7471
7472 /* Remove all port->id's mcast enries */
7473 mvpp2_prs_mcast_del_all(priv, id);
7474
Mikulas Patocka7ac8ff92018-02-11 18:10:28 -05007475 if (!allmulti) {
7476 netdev_for_each_mc_addr(ha, dev) {
7477 if (mvpp2_prs_mac_da_accept(priv, id, ha->addr, true)) {
7478 allmulti = true;
7479 goto retry;
7480 }
7481 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03007482 }
Maxime Chevallier56beda32018-02-28 10:14:13 +01007483
7484 /* Disable VLAN filtering in promiscuous mode */
7485 if (dev->flags & IFF_PROMISC)
7486 mvpp2_prs_vid_disable_filtering(port);
7487 else
7488 mvpp2_prs_vid_enable_filtering(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007489}
7490
7491static int mvpp2_set_mac_address(struct net_device *dev, void *p)
7492{
7493 struct mvpp2_port *port = netdev_priv(dev);
7494 const struct sockaddr *addr = p;
7495 int err;
7496
7497 if (!is_valid_ether_addr(addr->sa_data)) {
7498 err = -EADDRNOTAVAIL;
Markus Elfringc1175542017-04-17 11:10:47 +02007499 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007500 }
7501
7502 if (!netif_running(dev)) {
7503 err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
7504 if (!err)
7505 return 0;
7506 /* Reconfigure parser to accept the original MAC address */
7507 err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
7508 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02007509 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007510 }
7511
7512 mvpp2_stop_dev(port);
7513
7514 err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
7515 if (!err)
7516 goto out_start;
7517
7518 /* Reconfigure parser accept the original MAC address */
7519 err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
7520 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02007521 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007522out_start:
7523 mvpp2_start_dev(port);
7524 mvpp2_egress_enable(port);
7525 mvpp2_ingress_enable(port);
7526 return 0;
Markus Elfringc1175542017-04-17 11:10:47 +02007527log_error:
Markus Elfringdfd42402017-04-17 11:20:41 +02007528 netdev_err(dev, "failed to change MAC address\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03007529 return err;
7530}
7531
7532static int mvpp2_change_mtu(struct net_device *dev, int mtu)
7533{
7534 struct mvpp2_port *port = netdev_priv(dev);
7535 int err;
7536
Jarod Wilson57779872016-10-17 15:54:06 -04007537 if (!IS_ALIGNED(MVPP2_RX_PKT_SIZE(mtu), 8)) {
7538 netdev_info(dev, "illegal MTU value %d, round to %d\n", mtu,
7539 ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8));
7540 mtu = ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007541 }
7542
7543 if (!netif_running(dev)) {
7544 err = mvpp2_bm_update_mtu(dev, mtu);
7545 if (!err) {
7546 port->pkt_size = MVPP2_RX_PKT_SIZE(mtu);
7547 return 0;
7548 }
7549
7550 /* Reconfigure BM to the original MTU */
7551 err = mvpp2_bm_update_mtu(dev, dev->mtu);
7552 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02007553 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007554 }
7555
7556 mvpp2_stop_dev(port);
7557
7558 err = mvpp2_bm_update_mtu(dev, mtu);
7559 if (!err) {
7560 port->pkt_size = MVPP2_RX_PKT_SIZE(mtu);
7561 goto out_start;
7562 }
7563
7564 /* Reconfigure BM to the original MTU */
7565 err = mvpp2_bm_update_mtu(dev, dev->mtu);
7566 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02007567 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007568
7569out_start:
7570 mvpp2_start_dev(port);
7571 mvpp2_egress_enable(port);
7572 mvpp2_ingress_enable(port);
7573
7574 return 0;
Markus Elfringc1175542017-04-17 11:10:47 +02007575log_error:
Markus Elfringdfd42402017-04-17 11:20:41 +02007576 netdev_err(dev, "failed to change MTU\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03007577 return err;
7578}
7579
stephen hemmingerbc1f4472017-01-06 19:12:52 -08007580static void
Marcin Wojtas3f518502014-07-10 16:52:13 -03007581mvpp2_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
7582{
7583 struct mvpp2_port *port = netdev_priv(dev);
7584 unsigned int start;
7585 int cpu;
7586
7587 for_each_possible_cpu(cpu) {
7588 struct mvpp2_pcpu_stats *cpu_stats;
7589 u64 rx_packets;
7590 u64 rx_bytes;
7591 u64 tx_packets;
7592 u64 tx_bytes;
7593
7594 cpu_stats = per_cpu_ptr(port->stats, cpu);
7595 do {
7596 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
7597 rx_packets = cpu_stats->rx_packets;
7598 rx_bytes = cpu_stats->rx_bytes;
7599 tx_packets = cpu_stats->tx_packets;
7600 tx_bytes = cpu_stats->tx_bytes;
7601 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
7602
7603 stats->rx_packets += rx_packets;
7604 stats->rx_bytes += rx_bytes;
7605 stats->tx_packets += tx_packets;
7606 stats->tx_bytes += tx_bytes;
7607 }
7608
7609 stats->rx_errors = dev->stats.rx_errors;
7610 stats->rx_dropped = dev->stats.rx_dropped;
7611 stats->tx_dropped = dev->stats.tx_dropped;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007612}
7613
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007614static int mvpp2_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
7615{
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007616 int ret;
7617
Philippe Reynes8e072692016-06-28 00:08:11 +02007618 if (!dev->phydev)
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007619 return -ENOTSUPP;
7620
Philippe Reynes8e072692016-06-28 00:08:11 +02007621 ret = phy_mii_ioctl(dev->phydev, ifr, cmd);
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007622 if (!ret)
7623 mvpp2_link_event(dev);
7624
7625 return ret;
7626}
7627
Maxime Chevallier56beda32018-02-28 10:14:13 +01007628static int mvpp2_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
7629{
7630 struct mvpp2_port *port = netdev_priv(dev);
7631 int ret;
7632
7633 ret = mvpp2_prs_vid_entry_add(port, vid);
7634 if (ret)
7635 netdev_err(dev, "rx-vlan-filter offloading cannot accept more than %d VIDs per port\n",
7636 MVPP2_PRS_VLAN_FILT_MAX - 1);
7637 return ret;
7638}
7639
7640static int mvpp2_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
7641{
7642 struct mvpp2_port *port = netdev_priv(dev);
7643
7644 mvpp2_prs_vid_entry_remove(port, vid);
7645 return 0;
7646}
7647
7648static int mvpp2_set_features(struct net_device *dev,
7649 netdev_features_t features)
7650{
7651 netdev_features_t changed = dev->features ^ features;
7652 struct mvpp2_port *port = netdev_priv(dev);
7653
7654 if (changed & NETIF_F_HW_VLAN_CTAG_FILTER) {
7655 if (features & NETIF_F_HW_VLAN_CTAG_FILTER) {
7656 mvpp2_prs_vid_enable_filtering(port);
7657 } else {
7658 /* Invalidate all registered VID filters for this
7659 * port
7660 */
7661 mvpp2_prs_vid_remove_all(port);
7662
7663 mvpp2_prs_vid_disable_filtering(port);
7664 }
7665 }
7666
7667 return 0;
7668}
7669
Marcin Wojtas3f518502014-07-10 16:52:13 -03007670/* Ethtool methods */
7671
Marcin Wojtas3f518502014-07-10 16:52:13 -03007672/* Set interrupt coalescing for ethtools */
7673static int mvpp2_ethtool_set_coalesce(struct net_device *dev,
7674 struct ethtool_coalesce *c)
7675{
7676 struct mvpp2_port *port = netdev_priv(dev);
7677 int queue;
7678
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007679 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007680 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
7681
7682 rxq->time_coal = c->rx_coalesce_usecs;
7683 rxq->pkts_coal = c->rx_max_coalesced_frames;
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01007684 mvpp2_rx_pkts_coal_set(port, rxq);
7685 mvpp2_rx_time_coal_set(port, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007686 }
7687
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007688 if (port->has_tx_irqs) {
7689 port->tx_time_coal = c->tx_coalesce_usecs;
7690 mvpp2_tx_time_coal_set(port);
7691 }
7692
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007693 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007694 struct mvpp2_tx_queue *txq = port->txqs[queue];
7695
7696 txq->done_pkts_coal = c->tx_max_coalesced_frames;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007697
7698 if (port->has_tx_irqs)
7699 mvpp2_tx_pkts_coal_set(port, txq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007700 }
7701
Marcin Wojtas3f518502014-07-10 16:52:13 -03007702 return 0;
7703}
7704
7705/* get coalescing for ethtools */
7706static int mvpp2_ethtool_get_coalesce(struct net_device *dev,
7707 struct ethtool_coalesce *c)
7708{
7709 struct mvpp2_port *port = netdev_priv(dev);
7710
Antoine Tenart385c2842017-12-11 09:13:27 +01007711 c->rx_coalesce_usecs = port->rxqs[0]->time_coal;
7712 c->rx_max_coalesced_frames = port->rxqs[0]->pkts_coal;
7713 c->tx_max_coalesced_frames = port->txqs[0]->done_pkts_coal;
Antoine Tenart24b28cc2017-12-11 09:13:28 +01007714 c->tx_coalesce_usecs = port->tx_time_coal;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007715 return 0;
7716}
7717
7718static void mvpp2_ethtool_get_drvinfo(struct net_device *dev,
7719 struct ethtool_drvinfo *drvinfo)
7720{
7721 strlcpy(drvinfo->driver, MVPP2_DRIVER_NAME,
7722 sizeof(drvinfo->driver));
7723 strlcpy(drvinfo->version, MVPP2_DRIVER_VERSION,
7724 sizeof(drvinfo->version));
7725 strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
7726 sizeof(drvinfo->bus_info));
7727}
7728
7729static void mvpp2_ethtool_get_ringparam(struct net_device *dev,
7730 struct ethtool_ringparam *ring)
7731{
7732 struct mvpp2_port *port = netdev_priv(dev);
7733
Yan Markman7cf87e42017-12-11 09:13:26 +01007734 ring->rx_max_pending = MVPP2_MAX_RXD_MAX;
7735 ring->tx_max_pending = MVPP2_MAX_TXD_MAX;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007736 ring->rx_pending = port->rx_ring_size;
7737 ring->tx_pending = port->tx_ring_size;
7738}
7739
7740static int mvpp2_ethtool_set_ringparam(struct net_device *dev,
7741 struct ethtool_ringparam *ring)
7742{
7743 struct mvpp2_port *port = netdev_priv(dev);
7744 u16 prev_rx_ring_size = port->rx_ring_size;
7745 u16 prev_tx_ring_size = port->tx_ring_size;
7746 int err;
7747
7748 err = mvpp2_check_ringparam_valid(dev, ring);
7749 if (err)
7750 return err;
7751
7752 if (!netif_running(dev)) {
7753 port->rx_ring_size = ring->rx_pending;
7754 port->tx_ring_size = ring->tx_pending;
7755 return 0;
7756 }
7757
7758 /* The interface is running, so we have to force a
7759 * reallocation of the queues
7760 */
7761 mvpp2_stop_dev(port);
7762 mvpp2_cleanup_rxqs(port);
7763 mvpp2_cleanup_txqs(port);
7764
7765 port->rx_ring_size = ring->rx_pending;
7766 port->tx_ring_size = ring->tx_pending;
7767
7768 err = mvpp2_setup_rxqs(port);
7769 if (err) {
7770 /* Reallocate Rx queues with the original ring size */
7771 port->rx_ring_size = prev_rx_ring_size;
7772 ring->rx_pending = prev_rx_ring_size;
7773 err = mvpp2_setup_rxqs(port);
7774 if (err)
7775 goto err_out;
7776 }
7777 err = mvpp2_setup_txqs(port);
7778 if (err) {
7779 /* Reallocate Tx queues with the original ring size */
7780 port->tx_ring_size = prev_tx_ring_size;
7781 ring->tx_pending = prev_tx_ring_size;
7782 err = mvpp2_setup_txqs(port);
7783 if (err)
7784 goto err_clean_rxqs;
7785 }
7786
7787 mvpp2_start_dev(port);
7788 mvpp2_egress_enable(port);
7789 mvpp2_ingress_enable(port);
7790
7791 return 0;
7792
7793err_clean_rxqs:
7794 mvpp2_cleanup_rxqs(port);
7795err_out:
Markus Elfringdfd42402017-04-17 11:20:41 +02007796 netdev_err(dev, "failed to change ring parameters");
Marcin Wojtas3f518502014-07-10 16:52:13 -03007797 return err;
7798}
7799
7800/* Device ops */
7801
7802static const struct net_device_ops mvpp2_netdev_ops = {
7803 .ndo_open = mvpp2_open,
7804 .ndo_stop = mvpp2_stop,
7805 .ndo_start_xmit = mvpp2_tx,
7806 .ndo_set_rx_mode = mvpp2_set_rx_mode,
7807 .ndo_set_mac_address = mvpp2_set_mac_address,
7808 .ndo_change_mtu = mvpp2_change_mtu,
7809 .ndo_get_stats64 = mvpp2_get_stats64,
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007810 .ndo_do_ioctl = mvpp2_ioctl,
Maxime Chevallier56beda32018-02-28 10:14:13 +01007811 .ndo_vlan_rx_add_vid = mvpp2_vlan_rx_add_vid,
7812 .ndo_vlan_rx_kill_vid = mvpp2_vlan_rx_kill_vid,
7813 .ndo_set_features = mvpp2_set_features,
Marcin Wojtas3f518502014-07-10 16:52:13 -03007814};
7815
7816static const struct ethtool_ops mvpp2_eth_tool_ops = {
Florian Fainelli00606c42016-11-15 11:19:48 -08007817 .nway_reset = phy_ethtool_nway_reset,
Marcin Wojtas3f518502014-07-10 16:52:13 -03007818 .get_link = ethtool_op_get_link,
Marcin Wojtas3f518502014-07-10 16:52:13 -03007819 .set_coalesce = mvpp2_ethtool_set_coalesce,
7820 .get_coalesce = mvpp2_ethtool_get_coalesce,
7821 .get_drvinfo = mvpp2_ethtool_get_drvinfo,
7822 .get_ringparam = mvpp2_ethtool_get_ringparam,
7823 .set_ringparam = mvpp2_ethtool_set_ringparam,
Miquel Raynal118d6292017-11-06 22:56:53 +01007824 .get_strings = mvpp2_ethtool_get_strings,
7825 .get_ethtool_stats = mvpp2_ethtool_get_stats,
7826 .get_sset_count = mvpp2_ethtool_get_sset_count,
Philippe Reynesfb773e92016-06-28 00:08:12 +02007827 .get_link_ksettings = phy_ethtool_get_link_ksettings,
7828 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Marcin Wojtas3f518502014-07-10 16:52:13 -03007829};
7830
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007831/* Used for PPv2.1, or PPv2.2 with the old Device Tree binding that
7832 * had a single IRQ defined per-port.
7833 */
7834static int mvpp2_simple_queue_vectors_init(struct mvpp2_port *port,
7835 struct device_node *port_node)
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007836{
7837 struct mvpp2_queue_vector *v = &port->qvecs[0];
7838
7839 v->first_rxq = 0;
7840 v->nrxqs = port->nrxqs;
7841 v->type = MVPP2_QUEUE_VECTOR_SHARED;
7842 v->sw_thread_id = 0;
7843 v->sw_thread_mask = *cpumask_bits(cpu_online_mask);
7844 v->port = port;
7845 v->irq = irq_of_parse_and_map(port_node, 0);
7846 if (v->irq <= 0)
7847 return -EINVAL;
7848 netif_napi_add(port->dev, &v->napi, mvpp2_poll,
7849 NAPI_POLL_WEIGHT);
7850
7851 port->nqvecs = 1;
7852
7853 return 0;
7854}
7855
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007856static int mvpp2_multi_queue_vectors_init(struct mvpp2_port *port,
7857 struct device_node *port_node)
7858{
7859 struct mvpp2_queue_vector *v;
7860 int i, ret;
7861
7862 port->nqvecs = num_possible_cpus();
7863 if (queue_mode == MVPP2_QDIST_SINGLE_MODE)
7864 port->nqvecs += 1;
7865
7866 for (i = 0; i < port->nqvecs; i++) {
7867 char irqname[16];
7868
7869 v = port->qvecs + i;
7870
7871 v->port = port;
7872 v->type = MVPP2_QUEUE_VECTOR_PRIVATE;
7873 v->sw_thread_id = i;
7874 v->sw_thread_mask = BIT(i);
7875
7876 snprintf(irqname, sizeof(irqname), "tx-cpu%d", i);
7877
7878 if (queue_mode == MVPP2_QDIST_MULTI_MODE) {
7879 v->first_rxq = i * MVPP2_DEFAULT_RXQ;
7880 v->nrxqs = MVPP2_DEFAULT_RXQ;
7881 } else if (queue_mode == MVPP2_QDIST_SINGLE_MODE &&
7882 i == (port->nqvecs - 1)) {
7883 v->first_rxq = 0;
7884 v->nrxqs = port->nrxqs;
7885 v->type = MVPP2_QUEUE_VECTOR_SHARED;
7886 strncpy(irqname, "rx-shared", sizeof(irqname));
7887 }
7888
Marcin Wojtasa75edc72018-01-18 13:31:44 +01007889 if (port_node)
7890 v->irq = of_irq_get_byname(port_node, irqname);
7891 else
7892 v->irq = fwnode_irq_get(port->fwnode, i);
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007893 if (v->irq <= 0) {
7894 ret = -EINVAL;
7895 goto err;
7896 }
7897
7898 netif_napi_add(port->dev, &v->napi, mvpp2_poll,
7899 NAPI_POLL_WEIGHT);
7900 }
7901
7902 return 0;
7903
7904err:
7905 for (i = 0; i < port->nqvecs; i++)
7906 irq_dispose_mapping(port->qvecs[i].irq);
7907 return ret;
7908}
7909
7910static int mvpp2_queue_vectors_init(struct mvpp2_port *port,
7911 struct device_node *port_node)
7912{
7913 if (port->has_tx_irqs)
7914 return mvpp2_multi_queue_vectors_init(port, port_node);
7915 else
7916 return mvpp2_simple_queue_vectors_init(port, port_node);
7917}
7918
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007919static void mvpp2_queue_vectors_deinit(struct mvpp2_port *port)
7920{
7921 int i;
7922
7923 for (i = 0; i < port->nqvecs; i++)
7924 irq_dispose_mapping(port->qvecs[i].irq);
7925}
7926
7927/* Configure Rx queue group interrupt for this port */
7928static void mvpp2_rx_irqs_setup(struct mvpp2_port *port)
7929{
7930 struct mvpp2 *priv = port->priv;
7931 u32 val;
7932 int i;
7933
7934 if (priv->hw_version == MVPP21) {
7935 mvpp2_write(priv, MVPP21_ISR_RXQ_GROUP_REG(port->id),
7936 port->nrxqs);
7937 return;
7938 }
7939
7940 /* Handle the more complicated PPv2.2 case */
7941 for (i = 0; i < port->nqvecs; i++) {
7942 struct mvpp2_queue_vector *qv = port->qvecs + i;
7943
7944 if (!qv->nrxqs)
7945 continue;
7946
7947 val = qv->sw_thread_id;
7948 val |= port->id << MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_OFFSET;
7949 mvpp2_write(priv, MVPP22_ISR_RXQ_GROUP_INDEX_REG, val);
7950
7951 val = qv->first_rxq;
7952 val |= qv->nrxqs << MVPP22_ISR_RXQ_SUB_GROUP_SIZE_OFFSET;
7953 mvpp2_write(priv, MVPP22_ISR_RXQ_SUB_GROUP_CONFIG_REG, val);
7954 }
7955}
7956
Marcin Wojtas3f518502014-07-10 16:52:13 -03007957/* Initialize port HW */
7958static int mvpp2_port_init(struct mvpp2_port *port)
7959{
7960 struct device *dev = port->dev->dev.parent;
7961 struct mvpp2 *priv = port->priv;
7962 struct mvpp2_txq_pcpu *txq_pcpu;
7963 int queue, cpu, err;
7964
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007965 /* Checks for hardware constraints */
7966 if (port->first_rxq + port->nrxqs >
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01007967 MVPP2_MAX_PORTS * priv->max_port_rxqs)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007968 return -EINVAL;
7969
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007970 if (port->nrxqs % 4 || (port->nrxqs > priv->max_port_rxqs) ||
7971 (port->ntxqs > MVPP2_MAX_TXQ))
7972 return -EINVAL;
7973
Marcin Wojtas3f518502014-07-10 16:52:13 -03007974 /* Disable port */
7975 mvpp2_egress_disable(port);
7976 mvpp2_port_disable(port);
7977
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007978 port->tx_time_coal = MVPP2_TXDONE_COAL_USEC;
7979
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007980 port->txqs = devm_kcalloc(dev, port->ntxqs, sizeof(*port->txqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03007981 GFP_KERNEL);
7982 if (!port->txqs)
7983 return -ENOMEM;
7984
7985 /* Associate physical Tx queues to this port and initialize.
7986 * The mapping is predefined.
7987 */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007988 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007989 int queue_phy_id = mvpp2_txq_phys(port->id, queue);
7990 struct mvpp2_tx_queue *txq;
7991
7992 txq = devm_kzalloc(dev, sizeof(*txq), GFP_KERNEL);
Christophe Jaillet177c8d12017-02-19 10:19:57 +01007993 if (!txq) {
7994 err = -ENOMEM;
7995 goto err_free_percpu;
7996 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03007997
7998 txq->pcpu = alloc_percpu(struct mvpp2_txq_pcpu);
7999 if (!txq->pcpu) {
8000 err = -ENOMEM;
8001 goto err_free_percpu;
8002 }
8003
8004 txq->id = queue_phy_id;
8005 txq->log_id = queue;
8006 txq->done_pkts_coal = MVPP2_TXDONE_COAL_PKTS_THRESH;
8007 for_each_present_cpu(cpu) {
8008 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
8009 txq_pcpu->cpu = cpu;
8010 }
8011
8012 port->txqs[queue] = txq;
8013 }
8014
Thomas Petazzoni09f83972017-08-03 10:41:57 +02008015 port->rxqs = devm_kcalloc(dev, port->nrxqs, sizeof(*port->rxqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03008016 GFP_KERNEL);
8017 if (!port->rxqs) {
8018 err = -ENOMEM;
8019 goto err_free_percpu;
8020 }
8021
8022 /* Allocate and initialize Rx queue for this port */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02008023 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03008024 struct mvpp2_rx_queue *rxq;
8025
8026 /* Map physical Rx queue to port's logical Rx queue */
8027 rxq = devm_kzalloc(dev, sizeof(*rxq), GFP_KERNEL);
Jisheng Zhangd82b0c22016-03-31 17:01:23 +08008028 if (!rxq) {
8029 err = -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008030 goto err_free_percpu;
Jisheng Zhangd82b0c22016-03-31 17:01:23 +08008031 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03008032 /* Map this Rx queue to a physical queue */
8033 rxq->id = port->first_rxq + queue;
8034 rxq->port = port->id;
8035 rxq->logic_rxq = queue;
8036
8037 port->rxqs[queue] = rxq;
8038 }
8039
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02008040 mvpp2_rx_irqs_setup(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008041
8042 /* Create Rx descriptor rings */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02008043 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03008044 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
8045
8046 rxq->size = port->rx_ring_size;
8047 rxq->pkts_coal = MVPP2_RX_COAL_PKTS;
8048 rxq->time_coal = MVPP2_RX_COAL_USEC;
8049 }
8050
8051 mvpp2_ingress_disable(port);
8052
8053 /* Port default configuration */
8054 mvpp2_defaults_set(port);
8055
8056 /* Port's classifier configuration */
8057 mvpp2_cls_oversize_rxq_set(port);
8058 mvpp2_cls_port_config(port);
8059
8060 /* Provide an initial Rx packet size */
8061 port->pkt_size = MVPP2_RX_PKT_SIZE(port->dev->mtu);
8062
8063 /* Initialize pools for swf */
8064 err = mvpp2_swf_bm_pool_init(port);
8065 if (err)
8066 goto err_free_percpu;
8067
8068 return 0;
8069
8070err_free_percpu:
Thomas Petazzoni09f83972017-08-03 10:41:57 +02008071 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03008072 if (!port->txqs[queue])
8073 continue;
8074 free_percpu(port->txqs[queue]->pcpu);
8075 }
8076 return err;
8077}
8078
Thomas Petazzoni213f4282017-08-03 10:42:00 +02008079/* Checks if the port DT description has the TX interrupts
8080 * described. On PPv2.1, there are no such interrupts. On PPv2.2,
8081 * there are available, but we need to keep support for old DTs.
8082 */
8083static bool mvpp2_port_has_tx_irqs(struct mvpp2 *priv,
8084 struct device_node *port_node)
8085{
8086 char *irqs[5] = { "rx-shared", "tx-cpu0", "tx-cpu1",
8087 "tx-cpu2", "tx-cpu3" };
8088 int ret, i;
8089
8090 if (priv->hw_version == MVPP21)
8091 return false;
8092
8093 for (i = 0; i < 5; i++) {
8094 ret = of_property_match_string(port_node, "interrupt-names",
8095 irqs[i]);
8096 if (ret < 0)
8097 return false;
8098 }
8099
8100 return true;
8101}
8102
Antoine Tenart3ba8c812017-09-02 11:06:47 +02008103static void mvpp2_port_copy_mac_addr(struct net_device *dev, struct mvpp2 *priv,
Marcin Wojtas24812222018-01-18 13:31:43 +01008104 struct fwnode_handle *fwnode,
Antoine Tenart3ba8c812017-09-02 11:06:47 +02008105 char **mac_from)
8106{
8107 struct mvpp2_port *port = netdev_priv(dev);
8108 char hw_mac_addr[ETH_ALEN] = {0};
Marcin Wojtas24812222018-01-18 13:31:43 +01008109 char fw_mac_addr[ETH_ALEN];
Antoine Tenart3ba8c812017-09-02 11:06:47 +02008110
Marcin Wojtas24812222018-01-18 13:31:43 +01008111 if (fwnode_get_mac_address(fwnode, fw_mac_addr, ETH_ALEN)) {
8112 *mac_from = "firmware node";
8113 ether_addr_copy(dev->dev_addr, fw_mac_addr);
Antoine Tenart688cbaf2017-09-02 11:06:49 +02008114 return;
Antoine Tenart3ba8c812017-09-02 11:06:47 +02008115 }
Antoine Tenart688cbaf2017-09-02 11:06:49 +02008116
8117 if (priv->hw_version == MVPP21) {
8118 mvpp21_get_mac_address(port, hw_mac_addr);
8119 if (is_valid_ether_addr(hw_mac_addr)) {
8120 *mac_from = "hardware";
8121 ether_addr_copy(dev->dev_addr, hw_mac_addr);
8122 return;
8123 }
8124 }
8125
8126 *mac_from = "random";
8127 eth_hw_addr_random(dev);
Antoine Tenart3ba8c812017-09-02 11:06:47 +02008128}
8129
Marcin Wojtas3f518502014-07-10 16:52:13 -03008130/* Ports initialization */
8131static int mvpp2_port_probe(struct platform_device *pdev,
Marcin Wojtas24812222018-01-18 13:31:43 +01008132 struct fwnode_handle *port_fwnode,
Marcin Wojtasbf147152018-01-18 13:31:42 +01008133 struct mvpp2 *priv)
Marcin Wojtas3f518502014-07-10 16:52:13 -03008134{
8135 struct device_node *phy_node;
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008136 struct phy *comphy = NULL;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008137 struct mvpp2_port *port;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02008138 struct mvpp2_port_pcpu *port_pcpu;
Marcin Wojtas24812222018-01-18 13:31:43 +01008139 struct device_node *port_node = to_of_node(port_fwnode);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008140 struct net_device *dev;
8141 struct resource *res;
Antoine Tenart3ba8c812017-09-02 11:06:47 +02008142 char *mac_from = "";
Thomas Petazzoni09f83972017-08-03 10:41:57 +02008143 unsigned int ntxqs, nrxqs;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02008144 bool has_tx_irqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008145 u32 id;
8146 int features;
8147 int phy_mode;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02008148 int err, i, cpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008149
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008150 if (port_node) {
8151 has_tx_irqs = mvpp2_port_has_tx_irqs(priv, port_node);
8152 } else {
8153 has_tx_irqs = true;
8154 queue_mode = MVPP2_QDIST_MULTI_MODE;
8155 }
Thomas Petazzoni213f4282017-08-03 10:42:00 +02008156
8157 if (!has_tx_irqs)
8158 queue_mode = MVPP2_QDIST_SINGLE_MODE;
8159
Thomas Petazzoni09f83972017-08-03 10:41:57 +02008160 ntxqs = MVPP2_MAX_TXQ;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02008161 if (priv->hw_version == MVPP22 && queue_mode == MVPP2_QDIST_MULTI_MODE)
8162 nrxqs = MVPP2_DEFAULT_RXQ * num_possible_cpus();
8163 else
8164 nrxqs = MVPP2_DEFAULT_RXQ;
Thomas Petazzoni09f83972017-08-03 10:41:57 +02008165
8166 dev = alloc_etherdev_mqs(sizeof(*port), ntxqs, nrxqs);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008167 if (!dev)
8168 return -ENOMEM;
8169
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008170 if (port_node)
8171 phy_node = of_parse_phandle(port_node, "phy", 0);
8172 else
8173 phy_node = NULL;
8174
Marcin Wojtas24812222018-01-18 13:31:43 +01008175 phy_mode = fwnode_get_phy_mode(port_fwnode);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008176 if (phy_mode < 0) {
8177 dev_err(&pdev->dev, "incorrect phy mode\n");
8178 err = phy_mode;
8179 goto err_free_netdev;
8180 }
8181
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008182 if (port_node) {
8183 comphy = devm_of_phy_get(&pdev->dev, port_node, NULL);
8184 if (IS_ERR(comphy)) {
8185 if (PTR_ERR(comphy) == -EPROBE_DEFER) {
8186 err = -EPROBE_DEFER;
8187 goto err_free_netdev;
8188 }
8189 comphy = NULL;
Antoine Tenart542897d2017-08-30 10:29:15 +02008190 }
Antoine Tenart542897d2017-08-30 10:29:15 +02008191 }
8192
Marcin Wojtas24812222018-01-18 13:31:43 +01008193 if (fwnode_property_read_u32(port_fwnode, "port-id", &id)) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03008194 err = -EINVAL;
8195 dev_err(&pdev->dev, "missing port-id value\n");
8196 goto err_free_netdev;
8197 }
8198
Yan Markman7cf87e42017-12-11 09:13:26 +01008199 dev->tx_queue_len = MVPP2_MAX_TXD_MAX;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008200 dev->watchdog_timeo = 5 * HZ;
8201 dev->netdev_ops = &mvpp2_netdev_ops;
8202 dev->ethtool_ops = &mvpp2_eth_tool_ops;
8203
8204 port = netdev_priv(dev);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02008205 port->dev = dev;
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008206 port->fwnode = port_fwnode;
Thomas Petazzoni09f83972017-08-03 10:41:57 +02008207 port->ntxqs = ntxqs;
8208 port->nrxqs = nrxqs;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02008209 port->priv = priv;
8210 port->has_tx_irqs = has_tx_irqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008211
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02008212 err = mvpp2_queue_vectors_init(port, port_node);
8213 if (err)
Marcin Wojtas3f518502014-07-10 16:52:13 -03008214 goto err_free_netdev;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008215
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008216 if (port_node)
8217 port->link_irq = of_irq_get_byname(port_node, "link");
8218 else
8219 port->link_irq = fwnode_irq_get(port_fwnode, port->nqvecs + 1);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02008220 if (port->link_irq == -EPROBE_DEFER) {
8221 err = -EPROBE_DEFER;
8222 goto err_deinit_qvecs;
8223 }
8224 if (port->link_irq <= 0)
8225 /* the link irq is optional */
8226 port->link_irq = 0;
8227
Marcin Wojtas24812222018-01-18 13:31:43 +01008228 if (fwnode_property_read_bool(port_fwnode, "marvell,loopback"))
Marcin Wojtas3f518502014-07-10 16:52:13 -03008229 port->flags |= MVPP2_F_LOOPBACK;
8230
Marcin Wojtas3f518502014-07-10 16:52:13 -03008231 port->id = id;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01008232 if (priv->hw_version == MVPP21)
Thomas Petazzoni09f83972017-08-03 10:41:57 +02008233 port->first_rxq = port->id * port->nrxqs;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01008234 else
8235 port->first_rxq = port->id * priv->max_port_rxqs;
8236
Marcin Wojtas3f518502014-07-10 16:52:13 -03008237 port->phy_node = phy_node;
8238 port->phy_interface = phy_mode;
Antoine Tenart542897d2017-08-30 10:29:15 +02008239 port->comphy = comphy;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008240
Thomas Petazzonia7868412017-03-07 16:53:13 +01008241 if (priv->hw_version == MVPP21) {
8242 res = platform_get_resource(pdev, IORESOURCE_MEM, 2 + id);
8243 port->base = devm_ioremap_resource(&pdev->dev, res);
8244 if (IS_ERR(port->base)) {
8245 err = PTR_ERR(port->base);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02008246 goto err_free_irq;
Thomas Petazzonia7868412017-03-07 16:53:13 +01008247 }
Miquel Raynal118d6292017-11-06 22:56:53 +01008248
8249 port->stats_base = port->priv->lms_base +
8250 MVPP21_MIB_COUNTERS_OFFSET +
8251 port->gop_id * MVPP21_MIB_COUNTERS_PORT_SZ;
Thomas Petazzonia7868412017-03-07 16:53:13 +01008252 } else {
Marcin Wojtas24812222018-01-18 13:31:43 +01008253 if (fwnode_property_read_u32(port_fwnode, "gop-port-id",
8254 &port->gop_id)) {
Thomas Petazzonia7868412017-03-07 16:53:13 +01008255 err = -EINVAL;
8256 dev_err(&pdev->dev, "missing gop-port-id value\n");
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02008257 goto err_deinit_qvecs;
Thomas Petazzonia7868412017-03-07 16:53:13 +01008258 }
8259
8260 port->base = priv->iface_base + MVPP22_GMAC_BASE(port->gop_id);
Miquel Raynal118d6292017-11-06 22:56:53 +01008261 port->stats_base = port->priv->iface_base +
8262 MVPP22_MIB_COUNTERS_OFFSET +
8263 port->gop_id * MVPP22_MIB_COUNTERS_PORT_SZ;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008264 }
8265
Miquel Raynal118d6292017-11-06 22:56:53 +01008266 /* Alloc per-cpu and ethtool stats */
Marcin Wojtas3f518502014-07-10 16:52:13 -03008267 port->stats = netdev_alloc_pcpu_stats(struct mvpp2_pcpu_stats);
8268 if (!port->stats) {
8269 err = -ENOMEM;
Antoine Tenartfd3651b2017-09-01 11:04:54 +02008270 goto err_free_irq;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008271 }
8272
Miquel Raynal118d6292017-11-06 22:56:53 +01008273 port->ethtool_stats = devm_kcalloc(&pdev->dev,
8274 ARRAY_SIZE(mvpp2_ethtool_regs),
8275 sizeof(u64), GFP_KERNEL);
8276 if (!port->ethtool_stats) {
8277 err = -ENOMEM;
8278 goto err_free_stats;
8279 }
8280
Miquel Raynale5c500e2017-11-08 08:59:40 +01008281 mutex_init(&port->gather_stats_lock);
8282 INIT_DELAYED_WORK(&port->stats_work, mvpp2_gather_hw_statistics);
8283
Marcin Wojtas24812222018-01-18 13:31:43 +01008284 mvpp2_port_copy_mac_addr(dev, priv, port_fwnode, &mac_from);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008285
Yan Markman7cf87e42017-12-11 09:13:26 +01008286 port->tx_ring_size = MVPP2_MAX_TXD_DFLT;
8287 port->rx_ring_size = MVPP2_MAX_RXD_DFLT;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008288 SET_NETDEV_DEV(dev, &pdev->dev);
8289
8290 err = mvpp2_port_init(port);
8291 if (err < 0) {
8292 dev_err(&pdev->dev, "failed to init port %d\n", id);
8293 goto err_free_stats;
8294 }
Thomas Petazzoni26975822017-03-07 16:53:14 +01008295
Thomas Petazzoni26975822017-03-07 16:53:14 +01008296 mvpp2_port_periodic_xon_disable(port);
8297
8298 if (priv->hw_version == MVPP21)
8299 mvpp2_port_fc_adv_enable(port);
8300
8301 mvpp2_port_reset(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008302
Marcin Wojtasedc660f2015-08-06 19:00:30 +02008303 port->pcpu = alloc_percpu(struct mvpp2_port_pcpu);
8304 if (!port->pcpu) {
8305 err = -ENOMEM;
8306 goto err_free_txq_pcpu;
8307 }
8308
Thomas Petazzoni213f4282017-08-03 10:42:00 +02008309 if (!port->has_tx_irqs) {
8310 for_each_present_cpu(cpu) {
8311 port_pcpu = per_cpu_ptr(port->pcpu, cpu);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02008312
Thomas Petazzoni213f4282017-08-03 10:42:00 +02008313 hrtimer_init(&port_pcpu->tx_done_timer, CLOCK_MONOTONIC,
8314 HRTIMER_MODE_REL_PINNED);
8315 port_pcpu->tx_done_timer.function = mvpp2_hr_timer_cb;
8316 port_pcpu->timer_scheduled = false;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02008317
Thomas Petazzoni213f4282017-08-03 10:42:00 +02008318 tasklet_init(&port_pcpu->tx_done_tasklet,
8319 mvpp2_tx_proc_cb,
8320 (unsigned long)dev);
8321 }
Marcin Wojtasedc660f2015-08-06 19:00:30 +02008322 }
8323
Antoine Ténart186cd4d2017-08-23 09:46:56 +02008324 features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008325 dev->features = features | NETIF_F_RXCSUM;
Maxime Chevallier56beda32018-02-28 10:14:13 +01008326 dev->hw_features |= features | NETIF_F_RXCSUM | NETIF_F_GRO |
8327 NETIF_F_HW_VLAN_CTAG_FILTER;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008328 dev->vlan_features |= features;
Antoine Tenart1d17db02017-10-30 11:23:31 +01008329 dev->gso_max_segs = MVPP2_MAX_TSO_SEGS;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008330
Jarod Wilson57779872016-10-17 15:54:06 -04008331 /* MTU range: 68 - 9676 */
8332 dev->min_mtu = ETH_MIN_MTU;
8333 /* 9676 == 9700 - 20 and rounding to 8 */
8334 dev->max_mtu = 9676;
8335
Marcin Wojtas3f518502014-07-10 16:52:13 -03008336 err = register_netdev(dev);
8337 if (err < 0) {
8338 dev_err(&pdev->dev, "failed to register netdev\n");
Marcin Wojtasedc660f2015-08-06 19:00:30 +02008339 goto err_free_port_pcpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008340 }
8341 netdev_info(dev, "Using %s mac address %pM\n", mac_from, dev->dev_addr);
8342
Marcin Wojtasbf147152018-01-18 13:31:42 +01008343 priv->port_list[priv->port_count++] = port;
8344
Marcin Wojtas3f518502014-07-10 16:52:13 -03008345 return 0;
8346
Marcin Wojtasedc660f2015-08-06 19:00:30 +02008347err_free_port_pcpu:
8348 free_percpu(port->pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008349err_free_txq_pcpu:
Thomas Petazzoni09f83972017-08-03 10:41:57 +02008350 for (i = 0; i < port->ntxqs; i++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03008351 free_percpu(port->txqs[i]->pcpu);
8352err_free_stats:
8353 free_percpu(port->stats);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02008354err_free_irq:
8355 if (port->link_irq)
8356 irq_dispose_mapping(port->link_irq);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02008357err_deinit_qvecs:
8358 mvpp2_queue_vectors_deinit(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008359err_free_netdev:
Peter Chenccb80392016-08-01 15:02:37 +08008360 of_node_put(phy_node);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008361 free_netdev(dev);
8362 return err;
8363}
8364
8365/* Ports removal routine */
8366static void mvpp2_port_remove(struct mvpp2_port *port)
8367{
8368 int i;
8369
8370 unregister_netdev(port->dev);
Peter Chenccb80392016-08-01 15:02:37 +08008371 of_node_put(port->phy_node);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02008372 free_percpu(port->pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008373 free_percpu(port->stats);
Thomas Petazzoni09f83972017-08-03 10:41:57 +02008374 for (i = 0; i < port->ntxqs; i++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03008375 free_percpu(port->txqs[i]->pcpu);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02008376 mvpp2_queue_vectors_deinit(port);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02008377 if (port->link_irq)
8378 irq_dispose_mapping(port->link_irq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008379 free_netdev(port->dev);
8380}
8381
8382/* Initialize decoding windows */
8383static void mvpp2_conf_mbus_windows(const struct mbus_dram_target_info *dram,
8384 struct mvpp2 *priv)
8385{
8386 u32 win_enable;
8387 int i;
8388
8389 for (i = 0; i < 6; i++) {
8390 mvpp2_write(priv, MVPP2_WIN_BASE(i), 0);
8391 mvpp2_write(priv, MVPP2_WIN_SIZE(i), 0);
8392
8393 if (i < 4)
8394 mvpp2_write(priv, MVPP2_WIN_REMAP(i), 0);
8395 }
8396
8397 win_enable = 0;
8398
8399 for (i = 0; i < dram->num_cs; i++) {
8400 const struct mbus_dram_window *cs = dram->cs + i;
8401
8402 mvpp2_write(priv, MVPP2_WIN_BASE(i),
8403 (cs->base & 0xffff0000) | (cs->mbus_attr << 8) |
8404 dram->mbus_dram_target_id);
8405
8406 mvpp2_write(priv, MVPP2_WIN_SIZE(i),
8407 (cs->size - 1) & 0xffff0000);
8408
8409 win_enable |= (1 << i);
8410 }
8411
8412 mvpp2_write(priv, MVPP2_BASE_ADDR_ENABLE, win_enable);
8413}
8414
8415/* Initialize Rx FIFO's */
8416static void mvpp2_rx_fifo_init(struct mvpp2 *priv)
8417{
8418 int port;
8419
8420 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
8421 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
Antoine Tenart2d1d7df2017-10-30 11:23:28 +01008422 MVPP2_RX_FIFO_PORT_DATA_SIZE_4KB);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008423 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
Antoine Tenart2d1d7df2017-10-30 11:23:28 +01008424 MVPP2_RX_FIFO_PORT_ATTR_SIZE_4KB);
8425 }
8426
8427 mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
8428 MVPP2_RX_FIFO_PORT_MIN_PKT);
8429 mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
8430}
8431
8432static void mvpp22_rx_fifo_init(struct mvpp2 *priv)
8433{
8434 int port;
8435
8436 /* The FIFO size parameters are set depending on the maximum speed a
8437 * given port can handle:
8438 * - Port 0: 10Gbps
8439 * - Port 1: 2.5Gbps
8440 * - Ports 2 and 3: 1Gbps
8441 */
8442
8443 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(0),
8444 MVPP2_RX_FIFO_PORT_DATA_SIZE_32KB);
8445 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(0),
8446 MVPP2_RX_FIFO_PORT_ATTR_SIZE_32KB);
8447
8448 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(1),
8449 MVPP2_RX_FIFO_PORT_DATA_SIZE_8KB);
8450 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(1),
8451 MVPP2_RX_FIFO_PORT_ATTR_SIZE_8KB);
8452
8453 for (port = 2; port < MVPP2_MAX_PORTS; port++) {
8454 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
8455 MVPP2_RX_FIFO_PORT_DATA_SIZE_4KB);
8456 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
8457 MVPP2_RX_FIFO_PORT_ATTR_SIZE_4KB);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008458 }
8459
8460 mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
8461 MVPP2_RX_FIFO_PORT_MIN_PKT);
8462 mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
8463}
8464
Yan Markman93ff1302018-03-05 15:16:52 +01008465/* Initialize Tx FIFO's: the total FIFO size is 19kB on PPv2.2 and 10G
8466 * interfaces must have a Tx FIFO size of 10kB. As only port 0 can do 10G,
8467 * configure its Tx FIFO size to 10kB and the others ports Tx FIFO size to 3kB.
8468 */
Antoine Tenart7c10f972017-10-30 11:23:29 +01008469static void mvpp22_tx_fifo_init(struct mvpp2 *priv)
8470{
Yan Markman93ff1302018-03-05 15:16:52 +01008471 int port, size, thrs;
Antoine Tenart7c10f972017-10-30 11:23:29 +01008472
Yan Markman93ff1302018-03-05 15:16:52 +01008473 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
8474 if (port == 0) {
8475 size = MVPP22_TX_FIFO_DATA_SIZE_10KB;
8476 thrs = MVPP2_TX_FIFO_THRESHOLD_10KB;
8477 } else {
8478 size = MVPP22_TX_FIFO_DATA_SIZE_3KB;
8479 thrs = MVPP2_TX_FIFO_THRESHOLD_3KB;
8480 }
8481 mvpp2_write(priv, MVPP22_TX_FIFO_SIZE_REG(port), size);
8482 mvpp2_write(priv, MVPP22_TX_FIFO_THRESH_REG(port), thrs);
8483 }
Antoine Tenart7c10f972017-10-30 11:23:29 +01008484}
8485
Thomas Petazzoni6763ce32017-03-07 16:53:15 +01008486static void mvpp2_axi_init(struct mvpp2 *priv)
8487{
8488 u32 val, rdval, wrval;
8489
8490 mvpp2_write(priv, MVPP22_BM_ADDR_HIGH_RLS_REG, 0x0);
8491
8492 /* AXI Bridge Configuration */
8493
8494 rdval = MVPP22_AXI_CODE_CACHE_RD_CACHE
8495 << MVPP22_AXI_ATTR_CACHE_OFFS;
8496 rdval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
8497 << MVPP22_AXI_ATTR_DOMAIN_OFFS;
8498
8499 wrval = MVPP22_AXI_CODE_CACHE_WR_CACHE
8500 << MVPP22_AXI_ATTR_CACHE_OFFS;
8501 wrval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
8502 << MVPP22_AXI_ATTR_DOMAIN_OFFS;
8503
8504 /* BM */
8505 mvpp2_write(priv, MVPP22_AXI_BM_WR_ATTR_REG, wrval);
8506 mvpp2_write(priv, MVPP22_AXI_BM_RD_ATTR_REG, rdval);
8507
8508 /* Descriptors */
8509 mvpp2_write(priv, MVPP22_AXI_AGGRQ_DESCR_RD_ATTR_REG, rdval);
8510 mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_WR_ATTR_REG, wrval);
8511 mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_RD_ATTR_REG, rdval);
8512 mvpp2_write(priv, MVPP22_AXI_RXQ_DESCR_WR_ATTR_REG, wrval);
8513
8514 /* Buffer Data */
8515 mvpp2_write(priv, MVPP22_AXI_TX_DATA_RD_ATTR_REG, rdval);
8516 mvpp2_write(priv, MVPP22_AXI_RX_DATA_WR_ATTR_REG, wrval);
8517
8518 val = MVPP22_AXI_CODE_CACHE_NON_CACHE
8519 << MVPP22_AXI_CODE_CACHE_OFFS;
8520 val |= MVPP22_AXI_CODE_DOMAIN_SYSTEM
8521 << MVPP22_AXI_CODE_DOMAIN_OFFS;
8522 mvpp2_write(priv, MVPP22_AXI_RD_NORMAL_CODE_REG, val);
8523 mvpp2_write(priv, MVPP22_AXI_WR_NORMAL_CODE_REG, val);
8524
8525 val = MVPP22_AXI_CODE_CACHE_RD_CACHE
8526 << MVPP22_AXI_CODE_CACHE_OFFS;
8527 val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
8528 << MVPP22_AXI_CODE_DOMAIN_OFFS;
8529
8530 mvpp2_write(priv, MVPP22_AXI_RD_SNOOP_CODE_REG, val);
8531
8532 val = MVPP22_AXI_CODE_CACHE_WR_CACHE
8533 << MVPP22_AXI_CODE_CACHE_OFFS;
8534 val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
8535 << MVPP22_AXI_CODE_DOMAIN_OFFS;
8536
8537 mvpp2_write(priv, MVPP22_AXI_WR_SNOOP_CODE_REG, val);
8538}
8539
Marcin Wojtas3f518502014-07-10 16:52:13 -03008540/* Initialize network controller common part HW */
8541static int mvpp2_init(struct platform_device *pdev, struct mvpp2 *priv)
8542{
8543 const struct mbus_dram_target_info *dram_target_info;
8544 int err, i;
Marcin Wojtas08a23752014-07-21 13:48:12 -03008545 u32 val;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008546
Marcin Wojtas3f518502014-07-10 16:52:13 -03008547 /* MBUS windows configuration */
8548 dram_target_info = mv_mbus_dram_info();
8549 if (dram_target_info)
8550 mvpp2_conf_mbus_windows(dram_target_info, priv);
8551
Thomas Petazzoni6763ce32017-03-07 16:53:15 +01008552 if (priv->hw_version == MVPP22)
8553 mvpp2_axi_init(priv);
8554
Marcin Wojtas08a23752014-07-21 13:48:12 -03008555 /* Disable HW PHY polling */
Thomas Petazzoni26975822017-03-07 16:53:14 +01008556 if (priv->hw_version == MVPP21) {
8557 val = readl(priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
8558 val |= MVPP2_PHY_AN_STOP_SMI0_MASK;
8559 writel(val, priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
8560 } else {
8561 val = readl(priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
8562 val &= ~MVPP22_SMI_POLLING_EN;
8563 writel(val, priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
8564 }
Marcin Wojtas08a23752014-07-21 13:48:12 -03008565
Marcin Wojtas3f518502014-07-10 16:52:13 -03008566 /* Allocate and initialize aggregated TXQs */
8567 priv->aggr_txqs = devm_kcalloc(&pdev->dev, num_present_cpus(),
Markus Elfringd7ce3ce2017-04-17 08:48:23 +02008568 sizeof(*priv->aggr_txqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03008569 GFP_KERNEL);
8570 if (!priv->aggr_txqs)
8571 return -ENOMEM;
8572
8573 for_each_present_cpu(i) {
8574 priv->aggr_txqs[i].id = i;
8575 priv->aggr_txqs[i].size = MVPP2_AGGR_TXQ_SIZE;
Antoine Ténart85affd72017-08-23 09:46:55 +02008576 err = mvpp2_aggr_txq_init(pdev, &priv->aggr_txqs[i], i, priv);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008577 if (err < 0)
8578 return err;
8579 }
8580
Antoine Tenart7c10f972017-10-30 11:23:29 +01008581 /* Fifo Init */
8582 if (priv->hw_version == MVPP21) {
Antoine Tenart2d1d7df2017-10-30 11:23:28 +01008583 mvpp2_rx_fifo_init(priv);
Antoine Tenart7c10f972017-10-30 11:23:29 +01008584 } else {
Antoine Tenart2d1d7df2017-10-30 11:23:28 +01008585 mvpp22_rx_fifo_init(priv);
Antoine Tenart7c10f972017-10-30 11:23:29 +01008586 mvpp22_tx_fifo_init(priv);
8587 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03008588
Thomas Petazzoni26975822017-03-07 16:53:14 +01008589 if (priv->hw_version == MVPP21)
8590 writel(MVPP2_EXT_GLOBAL_CTRL_DEFAULT,
8591 priv->lms_base + MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008592
8593 /* Allow cache snoop when transmiting packets */
8594 mvpp2_write(priv, MVPP2_TX_SNOOP_REG, 0x1);
8595
8596 /* Buffer Manager initialization */
8597 err = mvpp2_bm_init(pdev, priv);
8598 if (err < 0)
8599 return err;
8600
8601 /* Parser default initialization */
8602 err = mvpp2_prs_default_init(pdev, priv);
8603 if (err < 0)
8604 return err;
8605
8606 /* Classifier default initialization */
8607 mvpp2_cls_init(priv);
8608
8609 return 0;
8610}
8611
8612static int mvpp2_probe(struct platform_device *pdev)
8613{
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008614 const struct acpi_device_id *acpi_id;
Marcin Wojtas24812222018-01-18 13:31:43 +01008615 struct fwnode_handle *fwnode = pdev->dev.fwnode;
8616 struct fwnode_handle *port_fwnode;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008617 struct mvpp2 *priv;
8618 struct resource *res;
Thomas Petazzonia7868412017-03-07 16:53:13 +01008619 void __iomem *base;
Miquel Raynal118d6292017-11-06 22:56:53 +01008620 int i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008621 int err;
8622
Markus Elfring0b92e592017-04-17 08:38:32 +02008623 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008624 if (!priv)
8625 return -ENOMEM;
8626
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008627 if (has_acpi_companion(&pdev->dev)) {
8628 acpi_id = acpi_match_device(pdev->dev.driver->acpi_match_table,
8629 &pdev->dev);
8630 priv->hw_version = (unsigned long)acpi_id->driver_data;
8631 } else {
8632 priv->hw_version =
8633 (unsigned long)of_device_get_match_data(&pdev->dev);
8634 }
Thomas Petazzonifaca9242017-03-07 16:53:06 +01008635
Marcin Wojtas3f518502014-07-10 16:52:13 -03008636 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thomas Petazzonia7868412017-03-07 16:53:13 +01008637 base = devm_ioremap_resource(&pdev->dev, res);
8638 if (IS_ERR(base))
8639 return PTR_ERR(base);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008640
Thomas Petazzonia7868412017-03-07 16:53:13 +01008641 if (priv->hw_version == MVPP21) {
8642 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
8643 priv->lms_base = devm_ioremap_resource(&pdev->dev, res);
8644 if (IS_ERR(priv->lms_base))
8645 return PTR_ERR(priv->lms_base);
8646 } else {
8647 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008648 if (has_acpi_companion(&pdev->dev)) {
8649 /* In case the MDIO memory region is declared in
8650 * the ACPI, it can already appear as 'in-use'
8651 * in the OS. Because it is overlapped by second
8652 * region of the network controller, make
8653 * sure it is released, before requesting it again.
8654 * The care is taken by mvpp2 driver to avoid
8655 * concurrent access to this memory region.
8656 */
8657 release_resource(res);
8658 }
Thomas Petazzonia7868412017-03-07 16:53:13 +01008659 priv->iface_base = devm_ioremap_resource(&pdev->dev, res);
8660 if (IS_ERR(priv->iface_base))
8661 return PTR_ERR(priv->iface_base);
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008662 }
Antoine Ténartf84bf382017-08-22 19:08:27 +02008663
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008664 if (priv->hw_version == MVPP22 && dev_of_node(&pdev->dev)) {
Antoine Ténartf84bf382017-08-22 19:08:27 +02008665 priv->sysctrl_base =
8666 syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
8667 "marvell,system-controller");
8668 if (IS_ERR(priv->sysctrl_base))
8669 /* The system controller regmap is optional for dt
8670 * compatibility reasons. When not provided, the
8671 * configuration of the GoP relies on the
8672 * firmware/bootloader.
8673 */
8674 priv->sysctrl_base = NULL;
Thomas Petazzonia7868412017-03-07 16:53:13 +01008675 }
8676
Stefan Chulski01d04932018-03-05 15:16:50 +01008677 mvpp2_setup_bm_pool();
8678
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02008679 for (i = 0; i < MVPP2_MAX_THREADS; i++) {
Thomas Petazzonia7868412017-03-07 16:53:13 +01008680 u32 addr_space_sz;
8681
8682 addr_space_sz = (priv->hw_version == MVPP21 ?
8683 MVPP21_ADDR_SPACE_SZ : MVPP22_ADDR_SPACE_SZ);
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02008684 priv->swth_base[i] = base + i * addr_space_sz;
Thomas Petazzonia7868412017-03-07 16:53:13 +01008685 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03008686
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01008687 if (priv->hw_version == MVPP21)
8688 priv->max_port_rxqs = 8;
8689 else
8690 priv->max_port_rxqs = 32;
8691
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008692 if (dev_of_node(&pdev->dev)) {
8693 priv->pp_clk = devm_clk_get(&pdev->dev, "pp_clk");
8694 if (IS_ERR(priv->pp_clk))
8695 return PTR_ERR(priv->pp_clk);
8696 err = clk_prepare_enable(priv->pp_clk);
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008697 if (err < 0)
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008698 return err;
8699
8700 priv->gop_clk = devm_clk_get(&pdev->dev, "gop_clk");
8701 if (IS_ERR(priv->gop_clk)) {
8702 err = PTR_ERR(priv->gop_clk);
8703 goto err_pp_clk;
8704 }
8705 err = clk_prepare_enable(priv->gop_clk);
8706 if (err < 0)
8707 goto err_pp_clk;
8708
8709 if (priv->hw_version == MVPP22) {
8710 priv->mg_clk = devm_clk_get(&pdev->dev, "mg_clk");
8711 if (IS_ERR(priv->mg_clk)) {
8712 err = PTR_ERR(priv->mg_clk);
8713 goto err_gop_clk;
8714 }
8715
8716 err = clk_prepare_enable(priv->mg_clk);
8717 if (err < 0)
8718 goto err_gop_clk;
8719 }
Gregory CLEMENT4792ea02017-09-29 14:27:39 +02008720
8721 priv->axi_clk = devm_clk_get(&pdev->dev, "axi_clk");
8722 if (IS_ERR(priv->axi_clk)) {
8723 err = PTR_ERR(priv->axi_clk);
8724 if (err == -EPROBE_DEFER)
8725 goto err_gop_clk;
8726 priv->axi_clk = NULL;
8727 } else {
8728 err = clk_prepare_enable(priv->axi_clk);
8729 if (err < 0)
8730 goto err_gop_clk;
8731 }
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008732
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008733 /* Get system's tclk rate */
8734 priv->tclk = clk_get_rate(priv->pp_clk);
8735 } else if (device_property_read_u32(&pdev->dev, "clock-frequency",
8736 &priv->tclk)) {
8737 dev_err(&pdev->dev, "missing clock-frequency value\n");
8738 return -EINVAL;
8739 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03008740
Thomas Petazzoni2067e0a2017-03-07 16:53:19 +01008741 if (priv->hw_version == MVPP22) {
8742 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(40));
8743 if (err)
8744 goto err_mg_clk;
8745 /* Sadly, the BM pools all share the same register to
8746 * store the high 32 bits of their address. So they
8747 * must all have the same high 32 bits, which forces
8748 * us to restrict coherent memory to DMA_BIT_MASK(32).
8749 */
8750 err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
8751 if (err)
8752 goto err_mg_clk;
8753 }
8754
Marcin Wojtas3f518502014-07-10 16:52:13 -03008755 /* Initialize network controller */
8756 err = mvpp2_init(pdev, priv);
8757 if (err < 0) {
8758 dev_err(&pdev->dev, "failed to initialize controller\n");
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008759 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008760 }
8761
Marcin Wojtasbf147152018-01-18 13:31:42 +01008762 /* Initialize ports */
Marcin Wojtas24812222018-01-18 13:31:43 +01008763 fwnode_for_each_available_child_node(fwnode, port_fwnode) {
8764 err = mvpp2_port_probe(pdev, port_fwnode, priv);
Marcin Wojtasbf147152018-01-18 13:31:42 +01008765 if (err < 0)
8766 goto err_port_probe;
8767 }
8768
Miquel Raynal118d6292017-11-06 22:56:53 +01008769 if (priv->port_count == 0) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03008770 dev_err(&pdev->dev, "no ports enabled\n");
Wei Yongjun575a1932014-07-20 22:02:43 +08008771 err = -ENODEV;
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008772 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008773 }
8774
Miquel Raynal118d6292017-11-06 22:56:53 +01008775 /* Statistics must be gathered regularly because some of them (like
8776 * packets counters) are 32-bit registers and could overflow quite
8777 * quickly. For instance, a 10Gb link used at full bandwidth with the
8778 * smallest packets (64B) will overflow a 32-bit counter in less than
8779 * 30 seconds. Then, use a workqueue to fill 64-bit counters.
8780 */
Miquel Raynal118d6292017-11-06 22:56:53 +01008781 snprintf(priv->queue_name, sizeof(priv->queue_name),
8782 "stats-wq-%s%s", netdev_name(priv->port_list[0]->dev),
8783 priv->port_count > 1 ? "+" : "");
8784 priv->stats_queue = create_singlethread_workqueue(priv->queue_name);
8785 if (!priv->stats_queue) {
8786 err = -ENOMEM;
Antoine Tenart26146b02017-11-28 14:19:49 +01008787 goto err_port_probe;
Miquel Raynal118d6292017-11-06 22:56:53 +01008788 }
8789
Marcin Wojtas3f518502014-07-10 16:52:13 -03008790 platform_set_drvdata(pdev, priv);
8791 return 0;
8792
Antoine Tenart26146b02017-11-28 14:19:49 +01008793err_port_probe:
8794 i = 0;
Marcin Wojtas24812222018-01-18 13:31:43 +01008795 fwnode_for_each_available_child_node(fwnode, port_fwnode) {
Antoine Tenart26146b02017-11-28 14:19:49 +01008796 if (priv->port_list[i])
8797 mvpp2_port_remove(priv->port_list[i]);
8798 i++;
8799 }
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008800err_mg_clk:
Gregory CLEMENT4792ea02017-09-29 14:27:39 +02008801 clk_disable_unprepare(priv->axi_clk);
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008802 if (priv->hw_version == MVPP22)
8803 clk_disable_unprepare(priv->mg_clk);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008804err_gop_clk:
8805 clk_disable_unprepare(priv->gop_clk);
8806err_pp_clk:
8807 clk_disable_unprepare(priv->pp_clk);
8808 return err;
8809}
8810
8811static int mvpp2_remove(struct platform_device *pdev)
8812{
8813 struct mvpp2 *priv = platform_get_drvdata(pdev);
Marcin Wojtas24812222018-01-18 13:31:43 +01008814 struct fwnode_handle *fwnode = pdev->dev.fwnode;
8815 struct fwnode_handle *port_fwnode;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008816 int i = 0;
8817
Miquel Raynale5c500e2017-11-08 08:59:40 +01008818 flush_workqueue(priv->stats_queue);
Miquel Raynal118d6292017-11-06 22:56:53 +01008819 destroy_workqueue(priv->stats_queue);
Miquel Raynal118d6292017-11-06 22:56:53 +01008820
Marcin Wojtas24812222018-01-18 13:31:43 +01008821 fwnode_for_each_available_child_node(fwnode, port_fwnode) {
Miquel Raynale5c500e2017-11-08 08:59:40 +01008822 if (priv->port_list[i]) {
8823 mutex_destroy(&priv->port_list[i]->gather_stats_lock);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008824 mvpp2_port_remove(priv->port_list[i]);
Miquel Raynale5c500e2017-11-08 08:59:40 +01008825 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03008826 i++;
8827 }
8828
8829 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
8830 struct mvpp2_bm_pool *bm_pool = &priv->bm_pools[i];
8831
8832 mvpp2_bm_pool_destroy(pdev, priv, bm_pool);
8833 }
8834
8835 for_each_present_cpu(i) {
8836 struct mvpp2_tx_queue *aggr_txq = &priv->aggr_txqs[i];
8837
8838 dma_free_coherent(&pdev->dev,
8839 MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
8840 aggr_txq->descs,
Thomas Petazzoni20396132017-03-07 16:53:00 +01008841 aggr_txq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008842 }
8843
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008844 if (is_acpi_node(port_fwnode))
8845 return 0;
8846
Gregory CLEMENT4792ea02017-09-29 14:27:39 +02008847 clk_disable_unprepare(priv->axi_clk);
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008848 clk_disable_unprepare(priv->mg_clk);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008849 clk_disable_unprepare(priv->pp_clk);
8850 clk_disable_unprepare(priv->gop_clk);
8851
8852 return 0;
8853}
8854
8855static const struct of_device_id mvpp2_match[] = {
Thomas Petazzonifaca9242017-03-07 16:53:06 +01008856 {
8857 .compatible = "marvell,armada-375-pp2",
8858 .data = (void *)MVPP21,
8859 },
Thomas Petazzonifc5e1552017-03-07 16:53:20 +01008860 {
8861 .compatible = "marvell,armada-7k-pp22",
8862 .data = (void *)MVPP22,
8863 },
Marcin Wojtas3f518502014-07-10 16:52:13 -03008864 { }
8865};
8866MODULE_DEVICE_TABLE(of, mvpp2_match);
8867
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008868static const struct acpi_device_id mvpp2_acpi_match[] = {
8869 { "MRVL0110", MVPP22 },
8870 { },
8871};
8872MODULE_DEVICE_TABLE(acpi, mvpp2_acpi_match);
8873
Marcin Wojtas3f518502014-07-10 16:52:13 -03008874static struct platform_driver mvpp2_driver = {
8875 .probe = mvpp2_probe,
8876 .remove = mvpp2_remove,
8877 .driver = {
8878 .name = MVPP2_DRIVER_NAME,
8879 .of_match_table = mvpp2_match,
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008880 .acpi_match_table = ACPI_PTR(mvpp2_acpi_match),
Marcin Wojtas3f518502014-07-10 16:52:13 -03008881 },
8882};
8883
8884module_platform_driver(mvpp2_driver);
8885
8886MODULE_DESCRIPTION("Marvell PPv2 Ethernet Driver - www.marvell.com");
8887MODULE_AUTHOR("Marcin Wojtas <mw@semihalf.com>");
Ezequiel Garciac6340992014-07-14 10:34:47 -03008888MODULE_LICENSE("GPL v2");