blob: c7b8093395c98f7fa5c45adc04a4c9e37698c924 [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
Antoine Tenart7c10f972017-10-30 11:23:29 +010047#define MVPP22_TX_FIFO_SIZE_REG(port) (0x8860 + 4 * (port))
Marcin Wojtas3f518502014-07-10 16:52:13 -030048
49/* RX DMA Top Registers */
50#define MVPP2_RX_CTRL_REG(port) (0x140 + 4 * (port))
51#define MVPP2_RX_LOW_LATENCY_PKT_SIZE(s) (((s) & 0xfff) << 16)
52#define MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK BIT(31)
53#define MVPP2_POOL_BUF_SIZE_REG(pool) (0x180 + 4 * (pool))
54#define MVPP2_POOL_BUF_SIZE_OFFSET 5
55#define MVPP2_RXQ_CONFIG_REG(rxq) (0x800 + 4 * (rxq))
56#define MVPP2_SNOOP_PKT_SIZE_MASK 0x1ff
57#define MVPP2_SNOOP_BUF_HDR_MASK BIT(9)
58#define MVPP2_RXQ_POOL_SHORT_OFFS 20
Thomas Petazzoni5eac8922017-03-07 16:53:10 +010059#define MVPP21_RXQ_POOL_SHORT_MASK 0x700000
60#define MVPP22_RXQ_POOL_SHORT_MASK 0xf00000
Marcin Wojtas3f518502014-07-10 16:52:13 -030061#define MVPP2_RXQ_POOL_LONG_OFFS 24
Thomas Petazzoni5eac8922017-03-07 16:53:10 +010062#define MVPP21_RXQ_POOL_LONG_MASK 0x7000000
63#define MVPP22_RXQ_POOL_LONG_MASK 0xf000000
Marcin Wojtas3f518502014-07-10 16:52:13 -030064#define MVPP2_RXQ_PACKET_OFFSET_OFFS 28
65#define MVPP2_RXQ_PACKET_OFFSET_MASK 0x70000000
66#define MVPP2_RXQ_DISABLE_MASK BIT(31)
67
Maxime Chevallier56beda32018-02-28 10:14:13 +010068/* Top Registers */
69#define MVPP2_MH_REG(port) (0x5040 + 4 * (port))
70#define MVPP2_DSA_EXTENDED BIT(5)
71
Marcin Wojtas3f518502014-07-10 16:52:13 -030072/* Parser Registers */
73#define MVPP2_PRS_INIT_LOOKUP_REG 0x1000
74#define MVPP2_PRS_PORT_LU_MAX 0xf
75#define MVPP2_PRS_PORT_LU_MASK(port) (0xff << ((port) * 4))
76#define MVPP2_PRS_PORT_LU_VAL(port, val) ((val) << ((port) * 4))
77#define MVPP2_PRS_INIT_OFFS_REG(port) (0x1004 + ((port) & 4))
78#define MVPP2_PRS_INIT_OFF_MASK(port) (0x3f << (((port) % 4) * 8))
79#define MVPP2_PRS_INIT_OFF_VAL(port, val) ((val) << (((port) % 4) * 8))
80#define MVPP2_PRS_MAX_LOOP_REG(port) (0x100c + ((port) & 4))
81#define MVPP2_PRS_MAX_LOOP_MASK(port) (0xff << (((port) % 4) * 8))
82#define MVPP2_PRS_MAX_LOOP_VAL(port, val) ((val) << (((port) % 4) * 8))
83#define MVPP2_PRS_TCAM_IDX_REG 0x1100
84#define MVPP2_PRS_TCAM_DATA_REG(idx) (0x1104 + (idx) * 4)
85#define MVPP2_PRS_TCAM_INV_MASK BIT(31)
86#define MVPP2_PRS_SRAM_IDX_REG 0x1200
87#define MVPP2_PRS_SRAM_DATA_REG(idx) (0x1204 + (idx) * 4)
88#define MVPP2_PRS_TCAM_CTRL_REG 0x1230
89#define MVPP2_PRS_TCAM_EN_MASK BIT(0)
90
Antoine Tenart1d7d15d2017-10-30 11:23:30 +010091/* RSS Registers */
92#define MVPP22_RSS_INDEX 0x1500
Antoine Tenart8a7b7412017-12-08 10:24:20 +010093#define MVPP22_RSS_INDEX_TABLE_ENTRY(idx) (idx)
Antoine Tenart1d7d15d2017-10-30 11:23:30 +010094#define MVPP22_RSS_INDEX_TABLE(idx) ((idx) << 8)
95#define MVPP22_RSS_INDEX_QUEUE(idx) ((idx) << 16)
96#define MVPP22_RSS_TABLE_ENTRY 0x1508
97#define MVPP22_RSS_TABLE 0x1510
98#define MVPP22_RSS_TABLE_POINTER(p) (p)
99#define MVPP22_RSS_WIDTH 0x150c
100
Marcin Wojtas3f518502014-07-10 16:52:13 -0300101/* Classifier Registers */
102#define MVPP2_CLS_MODE_REG 0x1800
103#define MVPP2_CLS_MODE_ACTIVE_MASK BIT(0)
104#define MVPP2_CLS_PORT_WAY_REG 0x1810
105#define MVPP2_CLS_PORT_WAY_MASK(port) (1 << (port))
106#define MVPP2_CLS_LKP_INDEX_REG 0x1814
107#define MVPP2_CLS_LKP_INDEX_WAY_OFFS 6
108#define MVPP2_CLS_LKP_TBL_REG 0x1818
109#define MVPP2_CLS_LKP_TBL_RXQ_MASK 0xff
110#define MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK BIT(25)
111#define MVPP2_CLS_FLOW_INDEX_REG 0x1820
112#define MVPP2_CLS_FLOW_TBL0_REG 0x1824
113#define MVPP2_CLS_FLOW_TBL1_REG 0x1828
114#define MVPP2_CLS_FLOW_TBL2_REG 0x182c
115#define MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port) (0x1980 + ((port) * 4))
116#define MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS 3
117#define MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK 0x7
118#define MVPP2_CLS_SWFWD_P2HQ_REG(port) (0x19b0 + ((port) * 4))
119#define MVPP2_CLS_SWFWD_PCTRL_REG 0x19d0
120#define MVPP2_CLS_SWFWD_PCTRL_MASK(port) (1 << (port))
121
122/* Descriptor Manager Top Registers */
123#define MVPP2_RXQ_NUM_REG 0x2040
124#define MVPP2_RXQ_DESC_ADDR_REG 0x2044
Thomas Petazzonib02f31f2017-03-07 16:53:12 +0100125#define MVPP22_DESC_ADDR_OFFS 8
Marcin Wojtas3f518502014-07-10 16:52:13 -0300126#define MVPP2_RXQ_DESC_SIZE_REG 0x2048
127#define MVPP2_RXQ_DESC_SIZE_MASK 0x3ff0
128#define MVPP2_RXQ_STATUS_UPDATE_REG(rxq) (0x3000 + 4 * (rxq))
129#define MVPP2_RXQ_NUM_PROCESSED_OFFSET 0
130#define MVPP2_RXQ_NUM_NEW_OFFSET 16
131#define MVPP2_RXQ_STATUS_REG(rxq) (0x3400 + 4 * (rxq))
132#define MVPP2_RXQ_OCCUPIED_MASK 0x3fff
133#define MVPP2_RXQ_NON_OCCUPIED_OFFSET 16
134#define MVPP2_RXQ_NON_OCCUPIED_MASK 0x3fff0000
135#define MVPP2_RXQ_THRESH_REG 0x204c
136#define MVPP2_OCCUPIED_THRESH_OFFSET 0
137#define MVPP2_OCCUPIED_THRESH_MASK 0x3fff
138#define MVPP2_RXQ_INDEX_REG 0x2050
139#define MVPP2_TXQ_NUM_REG 0x2080
140#define MVPP2_TXQ_DESC_ADDR_REG 0x2084
141#define MVPP2_TXQ_DESC_SIZE_REG 0x2088
142#define MVPP2_TXQ_DESC_SIZE_MASK 0x3ff0
Thomas Petazzoni213f4282017-08-03 10:42:00 +0200143#define MVPP2_TXQ_THRESH_REG 0x2094
144#define MVPP2_TXQ_THRESH_OFFSET 16
145#define MVPP2_TXQ_THRESH_MASK 0x3fff
Marcin Wojtas3f518502014-07-10 16:52:13 -0300146#define MVPP2_AGGR_TXQ_UPDATE_REG 0x2090
Marcin Wojtas3f518502014-07-10 16:52:13 -0300147#define MVPP2_TXQ_INDEX_REG 0x2098
148#define MVPP2_TXQ_PREF_BUF_REG 0x209c
149#define MVPP2_PREF_BUF_PTR(desc) ((desc) & 0xfff)
150#define MVPP2_PREF_BUF_SIZE_4 (BIT(12) | BIT(13))
151#define MVPP2_PREF_BUF_SIZE_16 (BIT(12) | BIT(14))
152#define MVPP2_PREF_BUF_THRESH(val) ((val) << 17)
153#define MVPP2_TXQ_DRAIN_EN_MASK BIT(31)
154#define MVPP2_TXQ_PENDING_REG 0x20a0
155#define MVPP2_TXQ_PENDING_MASK 0x3fff
156#define MVPP2_TXQ_INT_STATUS_REG 0x20a4
157#define MVPP2_TXQ_SENT_REG(txq) (0x3c00 + 4 * (txq))
158#define MVPP2_TRANSMITTED_COUNT_OFFSET 16
159#define MVPP2_TRANSMITTED_COUNT_MASK 0x3fff0000
160#define MVPP2_TXQ_RSVD_REQ_REG 0x20b0
161#define MVPP2_TXQ_RSVD_REQ_Q_OFFSET 16
162#define MVPP2_TXQ_RSVD_RSLT_REG 0x20b4
163#define MVPP2_TXQ_RSVD_RSLT_MASK 0x3fff
164#define MVPP2_TXQ_RSVD_CLR_REG 0x20b8
165#define MVPP2_TXQ_RSVD_CLR_OFFSET 16
166#define MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu) (0x2100 + 4 * (cpu))
Thomas Petazzonib02f31f2017-03-07 16:53:12 +0100167#define MVPP22_AGGR_TXQ_DESC_ADDR_OFFS 8
Marcin Wojtas3f518502014-07-10 16:52:13 -0300168#define MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu) (0x2140 + 4 * (cpu))
169#define MVPP2_AGGR_TXQ_DESC_SIZE_MASK 0x3ff0
170#define MVPP2_AGGR_TXQ_STATUS_REG(cpu) (0x2180 + 4 * (cpu))
171#define MVPP2_AGGR_TXQ_PENDING_MASK 0x3fff
172#define MVPP2_AGGR_TXQ_INDEX_REG(cpu) (0x21c0 + 4 * (cpu))
173
174/* MBUS bridge registers */
175#define MVPP2_WIN_BASE(w) (0x4000 + ((w) << 2))
176#define MVPP2_WIN_SIZE(w) (0x4020 + ((w) << 2))
177#define MVPP2_WIN_REMAP(w) (0x4040 + ((w) << 2))
178#define MVPP2_BASE_ADDR_ENABLE 0x4060
179
Thomas Petazzoni6763ce32017-03-07 16:53:15 +0100180/* AXI Bridge Registers */
181#define MVPP22_AXI_BM_WR_ATTR_REG 0x4100
182#define MVPP22_AXI_BM_RD_ATTR_REG 0x4104
183#define MVPP22_AXI_AGGRQ_DESCR_RD_ATTR_REG 0x4110
184#define MVPP22_AXI_TXQ_DESCR_WR_ATTR_REG 0x4114
185#define MVPP22_AXI_TXQ_DESCR_RD_ATTR_REG 0x4118
186#define MVPP22_AXI_RXQ_DESCR_WR_ATTR_REG 0x411c
187#define MVPP22_AXI_RX_DATA_WR_ATTR_REG 0x4120
188#define MVPP22_AXI_TX_DATA_RD_ATTR_REG 0x4130
189#define MVPP22_AXI_RD_NORMAL_CODE_REG 0x4150
190#define MVPP22_AXI_RD_SNOOP_CODE_REG 0x4154
191#define MVPP22_AXI_WR_NORMAL_CODE_REG 0x4160
192#define MVPP22_AXI_WR_SNOOP_CODE_REG 0x4164
193
194/* Values for AXI Bridge registers */
195#define MVPP22_AXI_ATTR_CACHE_OFFS 0
196#define MVPP22_AXI_ATTR_DOMAIN_OFFS 12
197
198#define MVPP22_AXI_CODE_CACHE_OFFS 0
199#define MVPP22_AXI_CODE_DOMAIN_OFFS 4
200
201#define MVPP22_AXI_CODE_CACHE_NON_CACHE 0x3
202#define MVPP22_AXI_CODE_CACHE_WR_CACHE 0x7
203#define MVPP22_AXI_CODE_CACHE_RD_CACHE 0xb
204
205#define MVPP22_AXI_CODE_DOMAIN_OUTER_DOM 2
206#define MVPP22_AXI_CODE_DOMAIN_SYSTEM 3
207
Marcin Wojtas3f518502014-07-10 16:52:13 -0300208/* Interrupt Cause and Mask registers */
Thomas Petazzoni213f4282017-08-03 10:42:00 +0200209#define MVPP2_ISR_TX_THRESHOLD_REG(port) (0x5140 + 4 * (port))
210#define MVPP2_MAX_ISR_TX_THRESHOLD 0xfffff0
211
Marcin Wojtas3f518502014-07-10 16:52:13 -0300212#define MVPP2_ISR_RX_THRESHOLD_REG(rxq) (0x5200 + 4 * (rxq))
Thomas Petazzoniab426762017-02-21 11:28:04 +0100213#define MVPP2_MAX_ISR_RX_THRESHOLD 0xfffff0
Thomas Petazzonieb1e93a2017-08-03 10:41:55 +0200214#define MVPP21_ISR_RXQ_GROUP_REG(port) (0x5400 + 4 * (port))
Thomas Petazzonia73fef12017-03-07 16:53:16 +0100215
Antoine Ténart81b66302017-08-22 19:08:21 +0200216#define MVPP22_ISR_RXQ_GROUP_INDEX_REG 0x5400
Thomas Petazzonia73fef12017-03-07 16:53:16 +0100217#define MVPP22_ISR_RXQ_GROUP_INDEX_SUBGROUP_MASK 0xf
Antoine Ténart81b66302017-08-22 19:08:21 +0200218#define MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_MASK 0x380
219#define MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_OFFSET 7
Thomas Petazzonia73fef12017-03-07 16:53:16 +0100220
221#define MVPP22_ISR_RXQ_GROUP_INDEX_SUBGROUP_MASK 0xf
Antoine Ténart81b66302017-08-22 19:08:21 +0200222#define MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_MASK 0x380
Thomas Petazzonia73fef12017-03-07 16:53:16 +0100223
Antoine Ténart81b66302017-08-22 19:08:21 +0200224#define MVPP22_ISR_RXQ_SUB_GROUP_CONFIG_REG 0x5404
225#define MVPP22_ISR_RXQ_SUB_GROUP_STARTQ_MASK 0x1f
226#define MVPP22_ISR_RXQ_SUB_GROUP_SIZE_MASK 0xf00
227#define MVPP22_ISR_RXQ_SUB_GROUP_SIZE_OFFSET 8
Thomas Petazzonia73fef12017-03-07 16:53:16 +0100228
Marcin Wojtas3f518502014-07-10 16:52:13 -0300229#define MVPP2_ISR_ENABLE_REG(port) (0x5420 + 4 * (port))
230#define MVPP2_ISR_ENABLE_INTERRUPT(mask) ((mask) & 0xffff)
231#define MVPP2_ISR_DISABLE_INTERRUPT(mask) (((mask) << 16) & 0xffff0000)
232#define MVPP2_ISR_RX_TX_CAUSE_REG(port) (0x5480 + 4 * (port))
233#define MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK 0xffff
234#define MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK 0xff0000
Thomas Petazzoni213f4282017-08-03 10:42:00 +0200235#define MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_OFFSET 16
Marcin Wojtas3f518502014-07-10 16:52:13 -0300236#define MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK BIT(24)
237#define MVPP2_CAUSE_FCS_ERR_MASK BIT(25)
238#define MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK BIT(26)
239#define MVPP2_CAUSE_TX_EXCEPTION_SUM_MASK BIT(29)
240#define MVPP2_CAUSE_RX_EXCEPTION_SUM_MASK BIT(30)
241#define MVPP2_CAUSE_MISC_SUM_MASK BIT(31)
242#define MVPP2_ISR_RX_TX_MASK_REG(port) (0x54a0 + 4 * (port))
243#define MVPP2_ISR_PON_RX_TX_MASK_REG 0x54bc
244#define MVPP2_PON_CAUSE_RXQ_OCCUP_DESC_ALL_MASK 0xffff
245#define MVPP2_PON_CAUSE_TXP_OCCUP_DESC_ALL_MASK 0x3fc00000
246#define MVPP2_PON_CAUSE_MISC_SUM_MASK BIT(31)
247#define MVPP2_ISR_MISC_CAUSE_REG 0x55b0
248
249/* Buffer Manager registers */
250#define MVPP2_BM_POOL_BASE_REG(pool) (0x6000 + ((pool) * 4))
251#define MVPP2_BM_POOL_BASE_ADDR_MASK 0xfffff80
252#define MVPP2_BM_POOL_SIZE_REG(pool) (0x6040 + ((pool) * 4))
253#define MVPP2_BM_POOL_SIZE_MASK 0xfff0
254#define MVPP2_BM_POOL_READ_PTR_REG(pool) (0x6080 + ((pool) * 4))
255#define MVPP2_BM_POOL_GET_READ_PTR_MASK 0xfff0
256#define MVPP2_BM_POOL_PTRS_NUM_REG(pool) (0x60c0 + ((pool) * 4))
257#define MVPP2_BM_POOL_PTRS_NUM_MASK 0xfff0
258#define MVPP2_BM_BPPI_READ_PTR_REG(pool) (0x6100 + ((pool) * 4))
259#define MVPP2_BM_BPPI_PTRS_NUM_REG(pool) (0x6140 + ((pool) * 4))
260#define MVPP2_BM_BPPI_PTR_NUM_MASK 0x7ff
Stefan Chulskieffbf5f2018-03-05 15:16:51 +0100261#define MVPP22_BM_POOL_PTRS_NUM_MASK 0xfff8
Marcin Wojtas3f518502014-07-10 16:52:13 -0300262#define MVPP2_BM_BPPI_PREFETCH_FULL_MASK BIT(16)
263#define MVPP2_BM_POOL_CTRL_REG(pool) (0x6200 + ((pool) * 4))
264#define MVPP2_BM_START_MASK BIT(0)
265#define MVPP2_BM_STOP_MASK BIT(1)
266#define MVPP2_BM_STATE_MASK BIT(4)
267#define MVPP2_BM_LOW_THRESH_OFFS 8
268#define MVPP2_BM_LOW_THRESH_MASK 0x7f00
269#define MVPP2_BM_LOW_THRESH_VALUE(val) ((val) << \
270 MVPP2_BM_LOW_THRESH_OFFS)
271#define MVPP2_BM_HIGH_THRESH_OFFS 16
272#define MVPP2_BM_HIGH_THRESH_MASK 0x7f0000
273#define MVPP2_BM_HIGH_THRESH_VALUE(val) ((val) << \
274 MVPP2_BM_HIGH_THRESH_OFFS)
275#define MVPP2_BM_INTR_CAUSE_REG(pool) (0x6240 + ((pool) * 4))
276#define MVPP2_BM_RELEASED_DELAY_MASK BIT(0)
277#define MVPP2_BM_ALLOC_FAILED_MASK BIT(1)
278#define MVPP2_BM_BPPE_EMPTY_MASK BIT(2)
279#define MVPP2_BM_BPPE_FULL_MASK BIT(3)
280#define MVPP2_BM_AVAILABLE_BP_LOW_MASK BIT(4)
281#define MVPP2_BM_INTR_MASK_REG(pool) (0x6280 + ((pool) * 4))
282#define MVPP2_BM_PHY_ALLOC_REG(pool) (0x6400 + ((pool) * 4))
283#define MVPP2_BM_PHY_ALLOC_GRNTD_MASK BIT(0)
284#define MVPP2_BM_VIRT_ALLOC_REG 0x6440
Thomas Petazzonid01524d2017-03-07 16:53:09 +0100285#define MVPP22_BM_ADDR_HIGH_ALLOC 0x6444
286#define MVPP22_BM_ADDR_HIGH_PHYS_MASK 0xff
287#define MVPP22_BM_ADDR_HIGH_VIRT_MASK 0xff00
288#define MVPP22_BM_ADDR_HIGH_VIRT_SHIFT 8
Marcin Wojtas3f518502014-07-10 16:52:13 -0300289#define MVPP2_BM_PHY_RLS_REG(pool) (0x6480 + ((pool) * 4))
290#define MVPP2_BM_PHY_RLS_MC_BUFF_MASK BIT(0)
291#define MVPP2_BM_PHY_RLS_PRIO_EN_MASK BIT(1)
292#define MVPP2_BM_PHY_RLS_GRNTD_MASK BIT(2)
293#define MVPP2_BM_VIRT_RLS_REG 0x64c0
Thomas Petazzonid01524d2017-03-07 16:53:09 +0100294#define MVPP22_BM_ADDR_HIGH_RLS_REG 0x64c4
295#define MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK 0xff
Antoine Ténart81b66302017-08-22 19:08:21 +0200296#define MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK 0xff00
Thomas Petazzonid01524d2017-03-07 16:53:09 +0100297#define MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT 8
Marcin Wojtas3f518502014-07-10 16:52:13 -0300298
299/* TX Scheduler registers */
300#define MVPP2_TXP_SCHED_PORT_INDEX_REG 0x8000
301#define MVPP2_TXP_SCHED_Q_CMD_REG 0x8004
302#define MVPP2_TXP_SCHED_ENQ_MASK 0xff
303#define MVPP2_TXP_SCHED_DISQ_OFFSET 8
304#define MVPP2_TXP_SCHED_CMD_1_REG 0x8010
305#define MVPP2_TXP_SCHED_PERIOD_REG 0x8018
306#define MVPP2_TXP_SCHED_MTU_REG 0x801c
307#define MVPP2_TXP_MTU_MAX 0x7FFFF
308#define MVPP2_TXP_SCHED_REFILL_REG 0x8020
309#define MVPP2_TXP_REFILL_TOKENS_ALL_MASK 0x7ffff
310#define MVPP2_TXP_REFILL_PERIOD_ALL_MASK 0x3ff00000
311#define MVPP2_TXP_REFILL_PERIOD_MASK(v) ((v) << 20)
312#define MVPP2_TXP_SCHED_TOKEN_SIZE_REG 0x8024
313#define MVPP2_TXP_TOKEN_SIZE_MAX 0xffffffff
314#define MVPP2_TXQ_SCHED_REFILL_REG(q) (0x8040 + ((q) << 2))
315#define MVPP2_TXQ_REFILL_TOKENS_ALL_MASK 0x7ffff
316#define MVPP2_TXQ_REFILL_PERIOD_ALL_MASK 0x3ff00000
317#define MVPP2_TXQ_REFILL_PERIOD_MASK(v) ((v) << 20)
318#define MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(q) (0x8060 + ((q) << 2))
319#define MVPP2_TXQ_TOKEN_SIZE_MAX 0x7fffffff
320#define MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(q) (0x8080 + ((q) << 2))
321#define MVPP2_TXQ_TOKEN_CNTR_MAX 0xffffffff
322
323/* TX general registers */
324#define MVPP2_TX_SNOOP_REG 0x8800
325#define MVPP2_TX_PORT_FLUSH_REG 0x8810
326#define MVPP2_TX_PORT_FLUSH_MASK(port) (1 << (port))
327
328/* LMS registers */
329#define MVPP2_SRC_ADDR_MIDDLE 0x24
330#define MVPP2_SRC_ADDR_HIGH 0x28
Marcin Wojtas08a23752014-07-21 13:48:12 -0300331#define MVPP2_PHY_AN_CFG0_REG 0x34
332#define MVPP2_PHY_AN_STOP_SMI0_MASK BIT(7)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300333#define MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG 0x305c
Thomas Petazzoni31d76772017-02-21 11:28:10 +0100334#define MVPP2_EXT_GLOBAL_CTRL_DEFAULT 0x27
Marcin Wojtas3f518502014-07-10 16:52:13 -0300335
336/* Per-port registers */
337#define MVPP2_GMAC_CTRL_0_REG 0x0
Antoine Ténart81b66302017-08-22 19:08:21 +0200338#define MVPP2_GMAC_PORT_EN_MASK BIT(0)
Antoine Ténart39193572017-08-22 19:08:24 +0200339#define MVPP2_GMAC_PORT_TYPE_MASK BIT(1)
Antoine Ténart81b66302017-08-22 19:08:21 +0200340#define MVPP2_GMAC_MAX_RX_SIZE_OFFS 2
341#define MVPP2_GMAC_MAX_RX_SIZE_MASK 0x7ffc
342#define MVPP2_GMAC_MIB_CNTR_EN_MASK BIT(15)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300343#define MVPP2_GMAC_CTRL_1_REG 0x4
Antoine Ténart81b66302017-08-22 19:08:21 +0200344#define MVPP2_GMAC_PERIODIC_XON_EN_MASK BIT(1)
345#define MVPP2_GMAC_GMII_LB_EN_MASK BIT(5)
346#define MVPP2_GMAC_PCS_LB_EN_BIT 6
347#define MVPP2_GMAC_PCS_LB_EN_MASK BIT(6)
348#define MVPP2_GMAC_SA_LOW_OFFS 7
Marcin Wojtas3f518502014-07-10 16:52:13 -0300349#define MVPP2_GMAC_CTRL_2_REG 0x8
Antoine Ténart81b66302017-08-22 19:08:21 +0200350#define MVPP2_GMAC_INBAND_AN_MASK BIT(0)
Antoine Ténart39193572017-08-22 19:08:24 +0200351#define MVPP2_GMAC_FLOW_CTRL_MASK GENMASK(2, 1)
Antoine Ténart81b66302017-08-22 19:08:21 +0200352#define MVPP2_GMAC_PCS_ENABLE_MASK BIT(3)
Antoine Tenartc7dfc8c2017-09-25 14:59:48 +0200353#define MVPP2_GMAC_INTERNAL_CLK_MASK BIT(4)
Antoine Ténart39193572017-08-22 19:08:24 +0200354#define MVPP2_GMAC_DISABLE_PADDING BIT(5)
Antoine Ténart81b66302017-08-22 19:08:21 +0200355#define MVPP2_GMAC_PORT_RESET_MASK BIT(6)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300356#define MVPP2_GMAC_AUTONEG_CONFIG 0xc
Antoine Ténart81b66302017-08-22 19:08:21 +0200357#define MVPP2_GMAC_FORCE_LINK_DOWN BIT(0)
358#define MVPP2_GMAC_FORCE_LINK_PASS BIT(1)
Antoine Ténart39193572017-08-22 19:08:24 +0200359#define MVPP2_GMAC_IN_BAND_AUTONEG BIT(2)
360#define MVPP2_GMAC_IN_BAND_AUTONEG_BYPASS BIT(3)
Antoine Ténart81b66302017-08-22 19:08:21 +0200361#define MVPP2_GMAC_CONFIG_MII_SPEED BIT(5)
362#define MVPP2_GMAC_CONFIG_GMII_SPEED BIT(6)
363#define MVPP2_GMAC_AN_SPEED_EN BIT(7)
364#define MVPP2_GMAC_FC_ADV_EN BIT(9)
Antoine Ténart39193572017-08-22 19:08:24 +0200365#define MVPP2_GMAC_FLOW_CTRL_AUTONEG BIT(11)
Antoine Ténart81b66302017-08-22 19:08:21 +0200366#define MVPP2_GMAC_CONFIG_FULL_DUPLEX BIT(12)
367#define MVPP2_GMAC_AN_DUPLEX_EN BIT(13)
Antoine Tenartfd3651b2017-09-01 11:04:54 +0200368#define MVPP2_GMAC_STATUS0 0x10
369#define MVPP2_GMAC_STATUS0_LINK_UP BIT(0)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300370#define MVPP2_GMAC_PORT_FIFO_CFG_1_REG 0x1c
Antoine Ténart81b66302017-08-22 19:08:21 +0200371#define MVPP2_GMAC_TX_FIFO_MIN_TH_OFFS 6
372#define MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK 0x1fc0
373#define MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(v) (((v) << 6) & \
Marcin Wojtas3f518502014-07-10 16:52:13 -0300374 MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK)
Antoine Tenartfd3651b2017-09-01 11:04:54 +0200375#define MVPP22_GMAC_INT_STAT 0x20
376#define MVPP22_GMAC_INT_STAT_LINK BIT(1)
377#define MVPP22_GMAC_INT_MASK 0x24
378#define MVPP22_GMAC_INT_MASK_LINK_STAT BIT(1)
Thomas Petazzoni26975822017-03-07 16:53:14 +0100379#define MVPP22_GMAC_CTRL_4_REG 0x90
Antoine Ténart81b66302017-08-22 19:08:21 +0200380#define MVPP22_CTRL4_EXT_PIN_GMII_SEL BIT(0)
381#define MVPP22_CTRL4_DP_CLK_SEL BIT(5)
Antoine Ténart1068ec72017-08-22 19:08:22 +0200382#define MVPP22_CTRL4_SYNC_BYPASS_DIS BIT(6)
Antoine Ténart81b66302017-08-22 19:08:21 +0200383#define MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE BIT(7)
Antoine Tenartfd3651b2017-09-01 11:04:54 +0200384#define MVPP22_GMAC_INT_SUM_MASK 0xa4
385#define MVPP22_GMAC_INT_SUM_MASK_LINK_STAT BIT(1)
Thomas Petazzoni26975822017-03-07 16:53:14 +0100386
387/* Per-port XGMAC registers. PPv2.2 only, only for GOP port 0,
388 * relative to port->base.
389 */
Antoine Ténart725757a2017-06-12 16:01:39 +0200390#define MVPP22_XLG_CTRL0_REG 0x100
Antoine Ténart81b66302017-08-22 19:08:21 +0200391#define MVPP22_XLG_CTRL0_PORT_EN BIT(0)
392#define MVPP22_XLG_CTRL0_MAC_RESET_DIS BIT(1)
Antoine Ténart77321952017-08-22 19:08:25 +0200393#define MVPP22_XLG_CTRL0_RX_FLOW_CTRL_EN BIT(7)
Antoine Ténart81b66302017-08-22 19:08:21 +0200394#define MVPP22_XLG_CTRL0_MIB_CNT_DIS BIT(14)
Stefan Chulski76eb1b12017-08-22 19:08:26 +0200395#define MVPP22_XLG_CTRL1_REG 0x104
Antoine Ténartec15ecd2017-08-25 15:24:46 +0200396#define MVPP22_XLG_CTRL1_FRAMESIZELIMIT_OFFS 0
Stefan Chulski76eb1b12017-08-22 19:08:26 +0200397#define MVPP22_XLG_CTRL1_FRAMESIZELIMIT_MASK 0x1fff
Antoine Tenartfd3651b2017-09-01 11:04:54 +0200398#define MVPP22_XLG_STATUS 0x10c
399#define MVPP22_XLG_STATUS_LINK_UP BIT(0)
400#define MVPP22_XLG_INT_STAT 0x114
401#define MVPP22_XLG_INT_STAT_LINK BIT(1)
402#define MVPP22_XLG_INT_MASK 0x118
403#define MVPP22_XLG_INT_MASK_LINK BIT(1)
Thomas Petazzoni26975822017-03-07 16:53:14 +0100404#define MVPP22_XLG_CTRL3_REG 0x11c
Antoine Ténart81b66302017-08-22 19:08:21 +0200405#define MVPP22_XLG_CTRL3_MACMODESELECT_MASK (7 << 13)
406#define MVPP22_XLG_CTRL3_MACMODESELECT_GMAC (0 << 13)
407#define MVPP22_XLG_CTRL3_MACMODESELECT_10G (1 << 13)
Antoine Tenartfd3651b2017-09-01 11:04:54 +0200408#define MVPP22_XLG_EXT_INT_MASK 0x15c
409#define MVPP22_XLG_EXT_INT_MASK_XLG BIT(1)
410#define MVPP22_XLG_EXT_INT_MASK_GIG BIT(2)
Antoine Ténart77321952017-08-22 19:08:25 +0200411#define MVPP22_XLG_CTRL4_REG 0x184
412#define MVPP22_XLG_CTRL4_FWD_FC BIT(5)
413#define MVPP22_XLG_CTRL4_FWD_PFC BIT(6)
414#define MVPP22_XLG_CTRL4_MACMODSELECT_GMAC BIT(12)
415
Thomas Petazzoni26975822017-03-07 16:53:14 +0100416/* SMI registers. PPv2.2 only, relative to priv->iface_base. */
417#define MVPP22_SMI_MISC_CFG_REG 0x1204
Antoine Ténart81b66302017-08-22 19:08:21 +0200418#define MVPP22_SMI_POLLING_EN BIT(10)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300419
Thomas Petazzonia7868412017-03-07 16:53:13 +0100420#define MVPP22_GMAC_BASE(port) (0x7000 + (port) * 0x1000 + 0xe00)
421
Marcin Wojtas3f518502014-07-10 16:52:13 -0300422#define MVPP2_CAUSE_TXQ_SENT_DESC_ALL_MASK 0xff
423
424/* Descriptor ring Macros */
425#define MVPP2_QUEUE_NEXT_DESC(q, index) \
426 (((index) < (q)->last_desc) ? ((index) + 1) : 0)
427
Antoine Ténartf84bf382017-08-22 19:08:27 +0200428/* XPCS registers. PPv2.2 only */
429#define MVPP22_MPCS_BASE(port) (0x7000 + (port) * 0x1000)
430#define MVPP22_MPCS_CTRL 0x14
431#define MVPP22_MPCS_CTRL_FWD_ERR_CONN BIT(10)
432#define MVPP22_MPCS_CLK_RESET 0x14c
433#define MAC_CLK_RESET_SD_TX BIT(0)
434#define MAC_CLK_RESET_SD_RX BIT(1)
435#define MAC_CLK_RESET_MAC BIT(2)
436#define MVPP22_MPCS_CLK_RESET_DIV_RATIO(n) ((n) << 4)
437#define MVPP22_MPCS_CLK_RESET_DIV_SET BIT(11)
438
439/* XPCS registers. PPv2.2 only */
440#define MVPP22_XPCS_BASE(port) (0x7400 + (port) * 0x1000)
441#define MVPP22_XPCS_CFG0 0x0
442#define MVPP22_XPCS_CFG0_PCS_MODE(n) ((n) << 3)
443#define MVPP22_XPCS_CFG0_ACTIVE_LANE(n) ((n) << 5)
444
445/* System controller registers. Accessed through a regmap. */
446#define GENCONF_SOFT_RESET1 0x1108
447#define GENCONF_SOFT_RESET1_GOP BIT(6)
448#define GENCONF_PORT_CTRL0 0x1110
449#define GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT BIT(1)
450#define GENCONF_PORT_CTRL0_RX_DATA_SAMPLE BIT(29)
451#define GENCONF_PORT_CTRL0_CLK_DIV_PHASE_CLR BIT(31)
452#define GENCONF_PORT_CTRL1 0x1114
453#define GENCONF_PORT_CTRL1_EN(p) BIT(p)
454#define GENCONF_PORT_CTRL1_RESET(p) (BIT(p) << 28)
455#define GENCONF_CTRL0 0x1120
456#define GENCONF_CTRL0_PORT0_RGMII BIT(0)
457#define GENCONF_CTRL0_PORT1_RGMII_MII BIT(1)
458#define GENCONF_CTRL0_PORT1_RGMII BIT(2)
459
Marcin Wojtas3f518502014-07-10 16:52:13 -0300460/* Various constants */
461
462/* Coalescing */
Antoine Tenart86162282017-12-11 09:13:29 +0100463#define MVPP2_TXDONE_COAL_PKTS_THRESH 64
Marcin Wojtasedc660f2015-08-06 19:00:30 +0200464#define MVPP2_TXDONE_HRTIMER_PERIOD_NS 1000000UL
Thomas Petazzoni213f4282017-08-03 10:42:00 +0200465#define MVPP2_TXDONE_COAL_USEC 1000
Marcin Wojtas3f518502014-07-10 16:52:13 -0300466#define MVPP2_RX_COAL_PKTS 32
Antoine Tenart86162282017-12-11 09:13:29 +0100467#define MVPP2_RX_COAL_USEC 64
Marcin Wojtas3f518502014-07-10 16:52:13 -0300468
469/* The two bytes Marvell header. Either contains a special value used
470 * by Marvell switches when a specific hardware mode is enabled (not
471 * supported by this driver) or is filled automatically by zeroes on
472 * the RX side. Those two bytes being at the front of the Ethernet
473 * header, they allow to have the IP header aligned on a 4 bytes
474 * boundary automatically: the hardware skips those two bytes on its
475 * own.
476 */
477#define MVPP2_MH_SIZE 2
478#define MVPP2_ETH_TYPE_LEN 2
479#define MVPP2_PPPOE_HDR_SIZE 8
480#define MVPP2_VLAN_TAG_LEN 4
Maxime Chevallier56beda32018-02-28 10:14:13 +0100481#define MVPP2_VLAN_TAG_EDSA_LEN 8
Marcin Wojtas3f518502014-07-10 16:52:13 -0300482
483/* Lbtd 802.3 type */
484#define MVPP2_IP_LBDT_TYPE 0xfffa
485
Marcin Wojtas3f518502014-07-10 16:52:13 -0300486#define MVPP2_TX_CSUM_MAX_SIZE 9800
487
488/* Timeout constants */
489#define MVPP2_TX_DISABLE_TIMEOUT_MSEC 1000
490#define MVPP2_TX_PENDING_TIMEOUT_MSEC 1000
491
492#define MVPP2_TX_MTU_MAX 0x7ffff
493
494/* Maximum number of T-CONTs of PON port */
495#define MVPP2_MAX_TCONT 16
496
497/* Maximum number of supported ports */
498#define MVPP2_MAX_PORTS 4
499
500/* Maximum number of TXQs used by single port */
501#define MVPP2_MAX_TXQ 8
502
Antoine Tenart1d17db02017-10-30 11:23:31 +0100503/* MVPP2_MAX_TSO_SEGS is the maximum number of fragments to allow in the GSO
504 * skb. As we need a maxium of two descriptors per fragments (1 header, 1 data),
505 * multiply this value by two to count the maximum number of skb descs needed.
506 */
507#define MVPP2_MAX_TSO_SEGS 300
508#define MVPP2_MAX_SKB_DESCS (MVPP2_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
509
Marcin Wojtas3f518502014-07-10 16:52:13 -0300510/* Dfault number of RXQs in use */
511#define MVPP2_DEFAULT_RXQ 4
512
Marcin Wojtas3f518502014-07-10 16:52:13 -0300513/* Max number of Rx descriptors */
Yan Markman7cf87e42017-12-11 09:13:26 +0100514#define MVPP2_MAX_RXD_MAX 1024
515#define MVPP2_MAX_RXD_DFLT 128
Marcin Wojtas3f518502014-07-10 16:52:13 -0300516
517/* Max number of Tx descriptors */
Yan Markman7cf87e42017-12-11 09:13:26 +0100518#define MVPP2_MAX_TXD_MAX 2048
519#define MVPP2_MAX_TXD_DFLT 1024
Marcin Wojtas3f518502014-07-10 16:52:13 -0300520
521/* Amount of Tx descriptors that can be reserved at once by CPU */
522#define MVPP2_CPU_DESC_CHUNK 64
523
524/* Max number of Tx descriptors in each aggregated queue */
525#define MVPP2_AGGR_TXQ_SIZE 256
526
527/* Descriptor aligned size */
528#define MVPP2_DESC_ALIGNED_SIZE 32
529
530/* Descriptor alignment mask */
531#define MVPP2_TX_DESC_ALIGN (MVPP2_DESC_ALIGNED_SIZE - 1)
532
533/* RX FIFO constants */
Antoine Tenart2d1d7df2017-10-30 11:23:28 +0100534#define MVPP2_RX_FIFO_PORT_DATA_SIZE_32KB 0x8000
535#define MVPP2_RX_FIFO_PORT_DATA_SIZE_8KB 0x2000
536#define MVPP2_RX_FIFO_PORT_DATA_SIZE_4KB 0x1000
537#define MVPP2_RX_FIFO_PORT_ATTR_SIZE_32KB 0x200
538#define MVPP2_RX_FIFO_PORT_ATTR_SIZE_8KB 0x80
539#define MVPP2_RX_FIFO_PORT_ATTR_SIZE_4KB 0x40
540#define MVPP2_RX_FIFO_PORT_MIN_PKT 0x80
Marcin Wojtas3f518502014-07-10 16:52:13 -0300541
Antoine Tenart7c10f972017-10-30 11:23:29 +0100542/* TX FIFO constants */
543#define MVPP22_TX_FIFO_DATA_SIZE_10KB 0xa
544#define MVPP22_TX_FIFO_DATA_SIZE_3KB 0x3
545
Marcin Wojtas3f518502014-07-10 16:52:13 -0300546/* RX buffer constants */
547#define MVPP2_SKB_SHINFO_SIZE \
548 SKB_DATA_ALIGN(sizeof(struct skb_shared_info))
549
550#define MVPP2_RX_PKT_SIZE(mtu) \
551 ALIGN((mtu) + MVPP2_MH_SIZE + MVPP2_VLAN_TAG_LEN + \
Jisheng Zhang4a0a12d2016-04-01 17:11:05 +0800552 ETH_HLEN + ETH_FCS_LEN, cache_line_size())
Marcin Wojtas3f518502014-07-10 16:52:13 -0300553
554#define MVPP2_RX_BUF_SIZE(pkt_size) ((pkt_size) + NET_SKB_PAD)
555#define MVPP2_RX_TOTAL_SIZE(buf_size) ((buf_size) + MVPP2_SKB_SHINFO_SIZE)
556#define MVPP2_RX_MAX_PKT_SIZE(total_size) \
557 ((total_size) - NET_SKB_PAD - MVPP2_SKB_SHINFO_SIZE)
558
559#define MVPP2_BIT_TO_BYTE(bit) ((bit) / 8)
560
561/* IPv6 max L3 address size */
562#define MVPP2_MAX_L3_ADDR_SIZE 16
563
564/* Port flags */
565#define MVPP2_F_LOOPBACK BIT(0)
566
567/* Marvell tag types */
568enum mvpp2_tag_type {
569 MVPP2_TAG_TYPE_NONE = 0,
570 MVPP2_TAG_TYPE_MH = 1,
571 MVPP2_TAG_TYPE_DSA = 2,
572 MVPP2_TAG_TYPE_EDSA = 3,
573 MVPP2_TAG_TYPE_VLAN = 4,
574 MVPP2_TAG_TYPE_LAST = 5
575};
576
577/* Parser constants */
578#define MVPP2_PRS_TCAM_SRAM_SIZE 256
579#define MVPP2_PRS_TCAM_WORDS 6
580#define MVPP2_PRS_SRAM_WORDS 4
581#define MVPP2_PRS_FLOW_ID_SIZE 64
582#define MVPP2_PRS_FLOW_ID_MASK 0x3f
583#define MVPP2_PRS_TCAM_ENTRY_INVALID 1
584#define MVPP2_PRS_TCAM_DSA_TAGGED_BIT BIT(5)
585#define MVPP2_PRS_IPV4_HEAD 0x40
586#define MVPP2_PRS_IPV4_HEAD_MASK 0xf0
587#define MVPP2_PRS_IPV4_MC 0xe0
588#define MVPP2_PRS_IPV4_MC_MASK 0xf0
589#define MVPP2_PRS_IPV4_BC_MASK 0xff
590#define MVPP2_PRS_IPV4_IHL 0x5
591#define MVPP2_PRS_IPV4_IHL_MASK 0xf
592#define MVPP2_PRS_IPV6_MC 0xff
593#define MVPP2_PRS_IPV6_MC_MASK 0xff
594#define MVPP2_PRS_IPV6_HOP_MASK 0xff
595#define MVPP2_PRS_TCAM_PROTO_MASK 0xff
596#define MVPP2_PRS_TCAM_PROTO_MASK_L 0x3f
597#define MVPP2_PRS_DBL_VLANS_MAX 100
598
599/* Tcam structure:
600 * - lookup ID - 4 bits
601 * - port ID - 1 byte
602 * - additional information - 1 byte
603 * - header data - 8 bytes
604 * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(5)->(0).
605 */
606#define MVPP2_PRS_AI_BITS 8
607#define MVPP2_PRS_PORT_MASK 0xff
608#define MVPP2_PRS_LU_MASK 0xf
609#define MVPP2_PRS_TCAM_DATA_BYTE(offs) \
610 (((offs) - ((offs) % 2)) * 2 + ((offs) % 2))
611#define MVPP2_PRS_TCAM_DATA_BYTE_EN(offs) \
612 (((offs) * 2) - ((offs) % 2) + 2)
613#define MVPP2_PRS_TCAM_AI_BYTE 16
614#define MVPP2_PRS_TCAM_PORT_BYTE 17
615#define MVPP2_PRS_TCAM_LU_BYTE 20
616#define MVPP2_PRS_TCAM_EN_OFFS(offs) ((offs) + 2)
617#define MVPP2_PRS_TCAM_INV_WORD 5
Maxime Chevallier56beda32018-02-28 10:14:13 +0100618
619#define MVPP2_PRS_VID_TCAM_BYTE 2
620
621/* There is a TCAM range reserved for VLAN filtering entries, range size is 33
622 * 10 VLAN ID filter entries per port
623 * 1 default VLAN filter entry per port
624 * It is assumed that there are 3 ports for filter, not including loopback port
625 */
626#define MVPP2_PRS_VLAN_FILT_MAX 11
627#define MVPP2_PRS_VLAN_FILT_RANGE_SIZE 33
628
629#define MVPP2_PRS_VLAN_FILT_MAX_ENTRY (MVPP2_PRS_VLAN_FILT_MAX - 2)
630#define MVPP2_PRS_VLAN_FILT_DFLT_ENTRY (MVPP2_PRS_VLAN_FILT_MAX - 1)
631
Marcin Wojtas3f518502014-07-10 16:52:13 -0300632/* Tcam entries ID */
633#define MVPP2_PE_DROP_ALL 0
634#define MVPP2_PE_FIRST_FREE_TID 1
Maxime Chevallier56beda32018-02-28 10:14:13 +0100635
636/* VLAN filtering range */
637#define MVPP2_PE_VID_FILT_RANGE_END (MVPP2_PRS_TCAM_SRAM_SIZE - 31)
638#define MVPP2_PE_VID_FILT_RANGE_START (MVPP2_PE_VID_FILT_RANGE_END - \
639 MVPP2_PRS_VLAN_FILT_RANGE_SIZE + 1)
640#define MVPP2_PE_LAST_FREE_TID (MVPP2_PE_VID_FILT_RANGE_START - 1)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300641#define MVPP2_PE_IP6_EXT_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 30)
642#define MVPP2_PE_MAC_MC_IP6 (MVPP2_PRS_TCAM_SRAM_SIZE - 29)
643#define MVPP2_PE_IP6_ADDR_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 28)
644#define MVPP2_PE_IP4_ADDR_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 27)
645#define MVPP2_PE_LAST_DEFAULT_FLOW (MVPP2_PRS_TCAM_SRAM_SIZE - 26)
Maxime Chevallier56beda32018-02-28 10:14:13 +0100646#define MVPP2_PE_FIRST_DEFAULT_FLOW (MVPP2_PRS_TCAM_SRAM_SIZE - 21)
647#define MVPP2_PE_EDSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 20)
648#define MVPP2_PE_EDSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 19)
649#define MVPP2_PE_DSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 18)
650#define MVPP2_PE_DSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 17)
651#define MVPP2_PE_ETYPE_EDSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 16)
652#define MVPP2_PE_ETYPE_EDSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 15)
653#define MVPP2_PE_ETYPE_DSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 14)
654#define MVPP2_PE_ETYPE_DSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 13)
655#define MVPP2_PE_MH_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 12)
656#define MVPP2_PE_DSA_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 11)
657#define MVPP2_PE_IP6_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 10)
658#define MVPP2_PE_IP4_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 9)
659#define MVPP2_PE_ETH_TYPE_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 8)
660#define MVPP2_PE_VID_FLTR_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 7)
661#define MVPP2_PE_VID_EDSA_FLTR_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 6)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300662#define MVPP2_PE_VLAN_DBL (MVPP2_PRS_TCAM_SRAM_SIZE - 5)
663#define MVPP2_PE_VLAN_NONE (MVPP2_PRS_TCAM_SRAM_SIZE - 4)
664#define MVPP2_PE_MAC_MC_ALL (MVPP2_PRS_TCAM_SRAM_SIZE - 3)
665#define MVPP2_PE_MAC_PROMISCUOUS (MVPP2_PRS_TCAM_SRAM_SIZE - 2)
666#define MVPP2_PE_MAC_NON_PROMISCUOUS (MVPP2_PRS_TCAM_SRAM_SIZE - 1)
667
Maxime Chevallier56beda32018-02-28 10:14:13 +0100668#define MVPP2_PRS_VID_PORT_FIRST(port) (MVPP2_PE_VID_FILT_RANGE_START + \
669 ((port) * MVPP2_PRS_VLAN_FILT_MAX))
670#define MVPP2_PRS_VID_PORT_LAST(port) (MVPP2_PRS_VID_PORT_FIRST(port) \
671 + MVPP2_PRS_VLAN_FILT_MAX_ENTRY)
672/* Index of default vid filter for given port */
673#define MVPP2_PRS_VID_PORT_DFLT(port) (MVPP2_PRS_VID_PORT_FIRST(port) \
674 + MVPP2_PRS_VLAN_FILT_DFLT_ENTRY)
675
Marcin Wojtas3f518502014-07-10 16:52:13 -0300676/* Sram structure
677 * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(3)->(0).
678 */
679#define MVPP2_PRS_SRAM_RI_OFFS 0
680#define MVPP2_PRS_SRAM_RI_WORD 0
681#define MVPP2_PRS_SRAM_RI_CTRL_OFFS 32
682#define MVPP2_PRS_SRAM_RI_CTRL_WORD 1
683#define MVPP2_PRS_SRAM_RI_CTRL_BITS 32
684#define MVPP2_PRS_SRAM_SHIFT_OFFS 64
685#define MVPP2_PRS_SRAM_SHIFT_SIGN_BIT 72
686#define MVPP2_PRS_SRAM_UDF_OFFS 73
687#define MVPP2_PRS_SRAM_UDF_BITS 8
688#define MVPP2_PRS_SRAM_UDF_MASK 0xff
689#define MVPP2_PRS_SRAM_UDF_SIGN_BIT 81
690#define MVPP2_PRS_SRAM_UDF_TYPE_OFFS 82
691#define MVPP2_PRS_SRAM_UDF_TYPE_MASK 0x7
692#define MVPP2_PRS_SRAM_UDF_TYPE_L3 1
693#define MVPP2_PRS_SRAM_UDF_TYPE_L4 4
694#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS 85
695#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK 0x3
696#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD 1
697#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP4_ADD 2
698#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP6_ADD 3
699#define MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS 87
700#define MVPP2_PRS_SRAM_OP_SEL_UDF_BITS 2
701#define MVPP2_PRS_SRAM_OP_SEL_UDF_MASK 0x3
702#define MVPP2_PRS_SRAM_OP_SEL_UDF_ADD 0
703#define MVPP2_PRS_SRAM_OP_SEL_UDF_IP4_ADD 2
704#define MVPP2_PRS_SRAM_OP_SEL_UDF_IP6_ADD 3
705#define MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS 89
706#define MVPP2_PRS_SRAM_AI_OFFS 90
707#define MVPP2_PRS_SRAM_AI_CTRL_OFFS 98
708#define MVPP2_PRS_SRAM_AI_CTRL_BITS 8
709#define MVPP2_PRS_SRAM_AI_MASK 0xff
710#define MVPP2_PRS_SRAM_NEXT_LU_OFFS 106
711#define MVPP2_PRS_SRAM_NEXT_LU_MASK 0xf
712#define MVPP2_PRS_SRAM_LU_DONE_BIT 110
713#define MVPP2_PRS_SRAM_LU_GEN_BIT 111
714
715/* Sram result info bits assignment */
716#define MVPP2_PRS_RI_MAC_ME_MASK 0x1
717#define MVPP2_PRS_RI_DSA_MASK 0x2
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100718#define MVPP2_PRS_RI_VLAN_MASK (BIT(2) | BIT(3))
719#define MVPP2_PRS_RI_VLAN_NONE 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300720#define MVPP2_PRS_RI_VLAN_SINGLE BIT(2)
721#define MVPP2_PRS_RI_VLAN_DOUBLE BIT(3)
722#define MVPP2_PRS_RI_VLAN_TRIPLE (BIT(2) | BIT(3))
723#define MVPP2_PRS_RI_CPU_CODE_MASK 0x70
724#define MVPP2_PRS_RI_CPU_CODE_RX_SPEC BIT(4)
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100725#define MVPP2_PRS_RI_L2_CAST_MASK (BIT(9) | BIT(10))
726#define MVPP2_PRS_RI_L2_UCAST 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300727#define MVPP2_PRS_RI_L2_MCAST BIT(9)
728#define MVPP2_PRS_RI_L2_BCAST BIT(10)
729#define MVPP2_PRS_RI_PPPOE_MASK 0x800
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100730#define MVPP2_PRS_RI_L3_PROTO_MASK (BIT(12) | BIT(13) | BIT(14))
731#define MVPP2_PRS_RI_L3_UN 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300732#define MVPP2_PRS_RI_L3_IP4 BIT(12)
733#define MVPP2_PRS_RI_L3_IP4_OPT BIT(13)
734#define MVPP2_PRS_RI_L3_IP4_OTHER (BIT(12) | BIT(13))
735#define MVPP2_PRS_RI_L3_IP6 BIT(14)
736#define MVPP2_PRS_RI_L3_IP6_EXT (BIT(12) | BIT(14))
737#define MVPP2_PRS_RI_L3_ARP (BIT(13) | BIT(14))
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100738#define MVPP2_PRS_RI_L3_ADDR_MASK (BIT(15) | BIT(16))
739#define MVPP2_PRS_RI_L3_UCAST 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300740#define MVPP2_PRS_RI_L3_MCAST BIT(15)
741#define MVPP2_PRS_RI_L3_BCAST (BIT(15) | BIT(16))
742#define MVPP2_PRS_RI_IP_FRAG_MASK 0x20000
Stefan Chulskiaff3da32017-09-25 14:59:46 +0200743#define MVPP2_PRS_RI_IP_FRAG_TRUE BIT(17)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300744#define MVPP2_PRS_RI_UDF3_MASK 0x300000
745#define MVPP2_PRS_RI_UDF3_RX_SPECIAL BIT(21)
746#define MVPP2_PRS_RI_L4_PROTO_MASK 0x1c00000
747#define MVPP2_PRS_RI_L4_TCP BIT(22)
748#define MVPP2_PRS_RI_L4_UDP BIT(23)
749#define MVPP2_PRS_RI_L4_OTHER (BIT(22) | BIT(23))
750#define MVPP2_PRS_RI_UDF7_MASK 0x60000000
751#define MVPP2_PRS_RI_UDF7_IP6_LITE BIT(29)
752#define MVPP2_PRS_RI_DROP_MASK 0x80000000
753
754/* Sram additional info bits assignment */
755#define MVPP2_PRS_IPV4_DIP_AI_BIT BIT(0)
756#define MVPP2_PRS_IPV6_NO_EXT_AI_BIT BIT(0)
757#define MVPP2_PRS_IPV6_EXT_AI_BIT BIT(1)
758#define MVPP2_PRS_IPV6_EXT_AH_AI_BIT BIT(2)
759#define MVPP2_PRS_IPV6_EXT_AH_LEN_AI_BIT BIT(3)
760#define MVPP2_PRS_IPV6_EXT_AH_L4_AI_BIT BIT(4)
761#define MVPP2_PRS_SINGLE_VLAN_AI 0
762#define MVPP2_PRS_DBL_VLAN_AI_BIT BIT(7)
Maxime Chevallier56beda32018-02-28 10:14:13 +0100763#define MVPP2_PRS_EDSA_VID_AI_BIT BIT(0)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300764
765/* DSA/EDSA type */
766#define MVPP2_PRS_TAGGED true
767#define MVPP2_PRS_UNTAGGED false
768#define MVPP2_PRS_EDSA true
769#define MVPP2_PRS_DSA false
770
771/* MAC entries, shadow udf */
772enum mvpp2_prs_udf {
773 MVPP2_PRS_UDF_MAC_DEF,
774 MVPP2_PRS_UDF_MAC_RANGE,
775 MVPP2_PRS_UDF_L2_DEF,
776 MVPP2_PRS_UDF_L2_DEF_COPY,
777 MVPP2_PRS_UDF_L2_USER,
778};
779
780/* Lookup ID */
781enum mvpp2_prs_lookup {
782 MVPP2_PRS_LU_MH,
783 MVPP2_PRS_LU_MAC,
784 MVPP2_PRS_LU_DSA,
785 MVPP2_PRS_LU_VLAN,
Maxime Chevallier56beda32018-02-28 10:14:13 +0100786 MVPP2_PRS_LU_VID,
Marcin Wojtas3f518502014-07-10 16:52:13 -0300787 MVPP2_PRS_LU_L2,
788 MVPP2_PRS_LU_PPPOE,
789 MVPP2_PRS_LU_IP4,
790 MVPP2_PRS_LU_IP6,
791 MVPP2_PRS_LU_FLOWS,
792 MVPP2_PRS_LU_LAST,
793};
794
795/* L3 cast enum */
796enum mvpp2_prs_l3_cast {
797 MVPP2_PRS_L3_UNI_CAST,
798 MVPP2_PRS_L3_MULTI_CAST,
799 MVPP2_PRS_L3_BROAD_CAST
800};
801
802/* Classifier constants */
803#define MVPP2_CLS_FLOWS_TBL_SIZE 512
804#define MVPP2_CLS_FLOWS_TBL_DATA_WORDS 3
805#define MVPP2_CLS_LKP_TBL_SIZE 64
Antoine Tenart1d7d15d2017-10-30 11:23:30 +0100806#define MVPP2_CLS_RX_QUEUES 256
807
808/* RSS constants */
809#define MVPP22_RSS_TABLE_ENTRIES 32
Marcin Wojtas3f518502014-07-10 16:52:13 -0300810
811/* BM constants */
Marcin Wojtas3f518502014-07-10 16:52:13 -0300812#define MVPP2_BM_LONG_BUF_NUM 1024
813#define MVPP2_BM_SHORT_BUF_NUM 2048
814#define MVPP2_BM_POOL_SIZE_MAX (16*1024 - MVPP2_BM_POOL_PTR_ALIGN/4)
815#define MVPP2_BM_POOL_PTR_ALIGN 128
Marcin Wojtas3f518502014-07-10 16:52:13 -0300816
817/* BM cookie (32 bits) definition */
818#define MVPP2_BM_COOKIE_POOL_OFFS 8
819#define MVPP2_BM_COOKIE_CPU_OFFS 24
820
Stefan Chulski01d04932018-03-05 15:16:50 +0100821#define MVPP2_BM_SHORT_FRAME_SIZE 512
822#define MVPP2_BM_LONG_FRAME_SIZE 2048
Marcin Wojtas3f518502014-07-10 16:52:13 -0300823/* BM short pool packet size
824 * These value assure that for SWF the total number
825 * of bytes allocated for each buffer will be 512
826 */
Stefan Chulski01d04932018-03-05 15:16:50 +0100827#define MVPP2_BM_SHORT_PKT_SIZE MVPP2_RX_MAX_PKT_SIZE(MVPP2_BM_SHORT_FRAME_SIZE)
828#define MVPP2_BM_LONG_PKT_SIZE MVPP2_RX_MAX_PKT_SIZE(MVPP2_BM_LONG_FRAME_SIZE)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300829
Thomas Petazzonia7868412017-03-07 16:53:13 +0100830#define MVPP21_ADDR_SPACE_SZ 0
831#define MVPP22_ADDR_SPACE_SZ SZ_64K
832
Thomas Petazzonidf089aa2017-08-03 10:41:58 +0200833#define MVPP2_MAX_THREADS 8
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +0200834#define MVPP2_MAX_QVECS MVPP2_MAX_THREADS
Thomas Petazzonia7868412017-03-07 16:53:13 +0100835
Stefan Chulski01d04932018-03-05 15:16:50 +0100836enum mvpp2_bm_pool_log_num {
837 MVPP2_BM_SHORT,
838 MVPP2_BM_LONG,
839 MVPP2_BM_POOLS_NUM
Marcin Wojtas3f518502014-07-10 16:52:13 -0300840};
841
Stefan Chulski01d04932018-03-05 15:16:50 +0100842static struct {
843 int pkt_size;
844 int buf_num;
845} mvpp2_pools[MVPP2_BM_POOLS_NUM];
846
Miquel Raynal118d6292017-11-06 22:56:53 +0100847/* GMAC MIB Counters register definitions */
848#define MVPP21_MIB_COUNTERS_OFFSET 0x1000
849#define MVPP21_MIB_COUNTERS_PORT_SZ 0x400
850#define MVPP22_MIB_COUNTERS_OFFSET 0x0
851#define MVPP22_MIB_COUNTERS_PORT_SZ 0x100
852
853#define MVPP2_MIB_GOOD_OCTETS_RCVD 0x0
854#define MVPP2_MIB_BAD_OCTETS_RCVD 0x8
855#define MVPP2_MIB_CRC_ERRORS_SENT 0xc
856#define MVPP2_MIB_UNICAST_FRAMES_RCVD 0x10
857#define MVPP2_MIB_BROADCAST_FRAMES_RCVD 0x18
858#define MVPP2_MIB_MULTICAST_FRAMES_RCVD 0x1c
859#define MVPP2_MIB_FRAMES_64_OCTETS 0x20
860#define MVPP2_MIB_FRAMES_65_TO_127_OCTETS 0x24
861#define MVPP2_MIB_FRAMES_128_TO_255_OCTETS 0x28
862#define MVPP2_MIB_FRAMES_256_TO_511_OCTETS 0x2c
863#define MVPP2_MIB_FRAMES_512_TO_1023_OCTETS 0x30
864#define MVPP2_MIB_FRAMES_1024_TO_MAX_OCTETS 0x34
865#define MVPP2_MIB_GOOD_OCTETS_SENT 0x38
866#define MVPP2_MIB_UNICAST_FRAMES_SENT 0x40
867#define MVPP2_MIB_MULTICAST_FRAMES_SENT 0x48
868#define MVPP2_MIB_BROADCAST_FRAMES_SENT 0x4c
869#define MVPP2_MIB_FC_SENT 0x54
870#define MVPP2_MIB_FC_RCVD 0x58
871#define MVPP2_MIB_RX_FIFO_OVERRUN 0x5c
872#define MVPP2_MIB_UNDERSIZE_RCVD 0x60
873#define MVPP2_MIB_FRAGMENTS_RCVD 0x64
874#define MVPP2_MIB_OVERSIZE_RCVD 0x68
875#define MVPP2_MIB_JABBER_RCVD 0x6c
876#define MVPP2_MIB_MAC_RCV_ERROR 0x70
877#define MVPP2_MIB_BAD_CRC_EVENT 0x74
878#define MVPP2_MIB_COLLISION 0x78
879#define MVPP2_MIB_LATE_COLLISION 0x7c
880
881#define MVPP2_MIB_COUNTERS_STATS_DELAY (1 * HZ)
882
Marcin Wojtas3f518502014-07-10 16:52:13 -0300883/* Definitions */
884
885/* Shared Packet Processor resources */
886struct mvpp2 {
887 /* Shared registers' base addresses */
Marcin Wojtas3f518502014-07-10 16:52:13 -0300888 void __iomem *lms_base;
Thomas Petazzonia7868412017-03-07 16:53:13 +0100889 void __iomem *iface_base;
890
Thomas Petazzonidf089aa2017-08-03 10:41:58 +0200891 /* On PPv2.2, each "software thread" can access the base
892 * register through a separate address space, each 64 KB apart
893 * from each other. Typically, such address spaces will be
894 * used per CPU.
Thomas Petazzonia7868412017-03-07 16:53:13 +0100895 */
Thomas Petazzonidf089aa2017-08-03 10:41:58 +0200896 void __iomem *swth_base[MVPP2_MAX_THREADS];
Marcin Wojtas3f518502014-07-10 16:52:13 -0300897
Antoine Ténartf84bf382017-08-22 19:08:27 +0200898 /* On PPv2.2, some port control registers are located into the system
899 * controller space. These registers are accessible through a regmap.
900 */
901 struct regmap *sysctrl_base;
902
Marcin Wojtas3f518502014-07-10 16:52:13 -0300903 /* Common clocks */
904 struct clk *pp_clk;
905 struct clk *gop_clk;
Thomas Petazzonifceb55d2017-03-07 16:53:18 +0100906 struct clk *mg_clk;
Gregory CLEMENT4792ea02017-09-29 14:27:39 +0200907 struct clk *axi_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300908
909 /* List of pointers to port structures */
Miquel Raynal118d6292017-11-06 22:56:53 +0100910 int port_count;
Marcin Wojtasbf147152018-01-18 13:31:42 +0100911 struct mvpp2_port *port_list[MVPP2_MAX_PORTS];
Marcin Wojtas3f518502014-07-10 16:52:13 -0300912
913 /* Aggregated TXQs */
914 struct mvpp2_tx_queue *aggr_txqs;
915
916 /* BM pools */
917 struct mvpp2_bm_pool *bm_pools;
918
919 /* PRS shadow table */
920 struct mvpp2_prs_shadow *prs_shadow;
921 /* PRS auxiliary table for double vlan entries control */
922 bool *prs_double_vlans;
923
924 /* Tclk value */
925 u32 tclk;
Thomas Petazzonifaca9242017-03-07 16:53:06 +0100926
927 /* HW version */
928 enum { MVPP21, MVPP22 } hw_version;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +0100929
930 /* Maximum number of RXQs per port */
931 unsigned int max_port_rxqs;
Miquel Raynal118d6292017-11-06 22:56:53 +0100932
Miquel Raynale5c500e2017-11-08 08:59:40 +0100933 /* Workqueue to gather hardware statistics */
Miquel Raynal118d6292017-11-06 22:56:53 +0100934 char queue_name[30];
935 struct workqueue_struct *stats_queue;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300936};
937
938struct mvpp2_pcpu_stats {
939 struct u64_stats_sync syncp;
940 u64 rx_packets;
941 u64 rx_bytes;
942 u64 tx_packets;
943 u64 tx_bytes;
944};
945
Marcin Wojtasedc660f2015-08-06 19:00:30 +0200946/* Per-CPU port control */
947struct mvpp2_port_pcpu {
948 struct hrtimer tx_done_timer;
949 bool timer_scheduled;
950 /* Tasklet for egress finalization */
951 struct tasklet_struct tx_done_tasklet;
952};
953
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +0200954struct mvpp2_queue_vector {
955 int irq;
956 struct napi_struct napi;
957 enum { MVPP2_QUEUE_VECTOR_SHARED, MVPP2_QUEUE_VECTOR_PRIVATE } type;
958 int sw_thread_id;
959 u16 sw_thread_mask;
960 int first_rxq;
961 int nrxqs;
962 u32 pending_cause_rx;
963 struct mvpp2_port *port;
964};
965
Marcin Wojtas3f518502014-07-10 16:52:13 -0300966struct mvpp2_port {
967 u8 id;
968
Thomas Petazzonia7868412017-03-07 16:53:13 +0100969 /* Index of the port from the "group of ports" complex point
970 * of view
971 */
972 int gop_id;
973
Antoine Tenartfd3651b2017-09-01 11:04:54 +0200974 int link_irq;
975
Marcin Wojtas3f518502014-07-10 16:52:13 -0300976 struct mvpp2 *priv;
977
Marcin Wojtas24812222018-01-18 13:31:43 +0100978 /* Firmware node associated to the port */
979 struct fwnode_handle *fwnode;
980
Marcin Wojtas3f518502014-07-10 16:52:13 -0300981 /* Per-port registers' base address */
982 void __iomem *base;
Miquel Raynal118d6292017-11-06 22:56:53 +0100983 void __iomem *stats_base;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300984
985 struct mvpp2_rx_queue **rxqs;
Thomas Petazzoni09f83972017-08-03 10:41:57 +0200986 unsigned int nrxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300987 struct mvpp2_tx_queue **txqs;
Thomas Petazzoni09f83972017-08-03 10:41:57 +0200988 unsigned int ntxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300989 struct net_device *dev;
990
991 int pkt_size;
992
Marcin Wojtasedc660f2015-08-06 19:00:30 +0200993 /* Per-CPU port control */
994 struct mvpp2_port_pcpu __percpu *pcpu;
995
Marcin Wojtas3f518502014-07-10 16:52:13 -0300996 /* Flags */
997 unsigned long flags;
998
999 u16 tx_ring_size;
1000 u16 rx_ring_size;
1001 struct mvpp2_pcpu_stats __percpu *stats;
Miquel Raynal118d6292017-11-06 22:56:53 +01001002 u64 *ethtool_stats;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001003
Miquel Raynale5c500e2017-11-08 08:59:40 +01001004 /* Per-port work and its lock to gather hardware statistics */
1005 struct mutex gather_stats_lock;
1006 struct delayed_work stats_work;
1007
Marcin Wojtas3f518502014-07-10 16:52:13 -03001008 phy_interface_t phy_interface;
1009 struct device_node *phy_node;
Antoine Tenart542897d2017-08-30 10:29:15 +02001010 struct phy *comphy;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001011 unsigned int link;
1012 unsigned int duplex;
1013 unsigned int speed;
1014
1015 struct mvpp2_bm_pool *pool_long;
1016 struct mvpp2_bm_pool *pool_short;
1017
1018 /* Index of first port's physical RXQ */
1019 u8 first_rxq;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02001020
1021 struct mvpp2_queue_vector qvecs[MVPP2_MAX_QVECS];
1022 unsigned int nqvecs;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02001023 bool has_tx_irqs;
1024
1025 u32 tx_time_coal;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001026};
1027
1028/* The mvpp2_tx_desc and mvpp2_rx_desc structures describe the
1029 * layout of the transmit and reception DMA descriptors, and their
1030 * layout is therefore defined by the hardware design
1031 */
1032
1033#define MVPP2_TXD_L3_OFF_SHIFT 0
1034#define MVPP2_TXD_IP_HLEN_SHIFT 8
1035#define MVPP2_TXD_L4_CSUM_FRAG BIT(13)
1036#define MVPP2_TXD_L4_CSUM_NOT BIT(14)
1037#define MVPP2_TXD_IP_CSUM_DISABLE BIT(15)
1038#define MVPP2_TXD_PADDING_DISABLE BIT(23)
1039#define MVPP2_TXD_L4_UDP BIT(24)
1040#define MVPP2_TXD_L3_IP6 BIT(26)
1041#define MVPP2_TXD_L_DESC BIT(28)
1042#define MVPP2_TXD_F_DESC BIT(29)
1043
1044#define MVPP2_RXD_ERR_SUMMARY BIT(15)
1045#define MVPP2_RXD_ERR_CODE_MASK (BIT(13) | BIT(14))
1046#define MVPP2_RXD_ERR_CRC 0x0
1047#define MVPP2_RXD_ERR_OVERRUN BIT(13)
1048#define MVPP2_RXD_ERR_RESOURCE (BIT(13) | BIT(14))
1049#define MVPP2_RXD_BM_POOL_ID_OFFS 16
1050#define MVPP2_RXD_BM_POOL_ID_MASK (BIT(16) | BIT(17) | BIT(18))
1051#define MVPP2_RXD_HWF_SYNC BIT(21)
1052#define MVPP2_RXD_L4_CSUM_OK BIT(22)
1053#define MVPP2_RXD_IP4_HEADER_ERR BIT(24)
1054#define MVPP2_RXD_L4_TCP BIT(25)
1055#define MVPP2_RXD_L4_UDP BIT(26)
1056#define MVPP2_RXD_L3_IP4 BIT(28)
1057#define MVPP2_RXD_L3_IP6 BIT(30)
1058#define MVPP2_RXD_BUF_HDR BIT(31)
1059
Thomas Petazzoni054f6372017-03-07 16:53:07 +01001060/* HW TX descriptor for PPv2.1 */
1061struct mvpp21_tx_desc {
Marcin Wojtas3f518502014-07-10 16:52:13 -03001062 u32 command; /* Options used by HW for packet transmitting.*/
1063 u8 packet_offset; /* the offset from the buffer beginning */
1064 u8 phys_txq; /* destination queue ID */
1065 u16 data_size; /* data size of transmitted packet in bytes */
Thomas Petazzoni20396132017-03-07 16:53:00 +01001066 u32 buf_dma_addr; /* physical addr of transmitted buffer */
Marcin Wojtas3f518502014-07-10 16:52:13 -03001067 u32 buf_cookie; /* cookie for access to TX buffer in tx path */
1068 u32 reserved1[3]; /* hw_cmd (for future use, BM, PON, PNC) */
1069 u32 reserved2; /* reserved (for future use) */
1070};
1071
Thomas Petazzoni054f6372017-03-07 16:53:07 +01001072/* HW RX descriptor for PPv2.1 */
1073struct mvpp21_rx_desc {
Marcin Wojtas3f518502014-07-10 16:52:13 -03001074 u32 status; /* info about received packet */
1075 u16 reserved1; /* parser_info (for future use, PnC) */
1076 u16 data_size; /* size of received packet in bytes */
Thomas Petazzoni20396132017-03-07 16:53:00 +01001077 u32 buf_dma_addr; /* physical address of the buffer */
Marcin Wojtas3f518502014-07-10 16:52:13 -03001078 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
1079 u16 reserved2; /* gem_port_id (for future use, PON) */
1080 u16 reserved3; /* csum_l4 (for future use, PnC) */
1081 u8 reserved4; /* bm_qset (for future use, BM) */
1082 u8 reserved5;
1083 u16 reserved6; /* classify_info (for future use, PnC) */
1084 u32 reserved7; /* flow_id (for future use, PnC) */
1085 u32 reserved8;
1086};
1087
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001088/* HW TX descriptor for PPv2.2 */
1089struct mvpp22_tx_desc {
1090 u32 command;
1091 u8 packet_offset;
1092 u8 phys_txq;
1093 u16 data_size;
1094 u64 reserved1;
1095 u64 buf_dma_addr_ptp;
1096 u64 buf_cookie_misc;
1097};
1098
1099/* HW RX descriptor for PPv2.2 */
1100struct mvpp22_rx_desc {
1101 u32 status;
1102 u16 reserved1;
1103 u16 data_size;
1104 u32 reserved2;
1105 u32 reserved3;
1106 u64 buf_dma_addr_key_hash;
1107 u64 buf_cookie_misc;
1108};
1109
Thomas Petazzoni054f6372017-03-07 16:53:07 +01001110/* Opaque type used by the driver to manipulate the HW TX and RX
1111 * descriptors
1112 */
1113struct mvpp2_tx_desc {
1114 union {
1115 struct mvpp21_tx_desc pp21;
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001116 struct mvpp22_tx_desc pp22;
Thomas Petazzoni054f6372017-03-07 16:53:07 +01001117 };
1118};
1119
1120struct mvpp2_rx_desc {
1121 union {
1122 struct mvpp21_rx_desc pp21;
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001123 struct mvpp22_rx_desc pp22;
Thomas Petazzoni054f6372017-03-07 16:53:07 +01001124 };
1125};
1126
Thomas Petazzoni83544912016-12-21 11:28:49 +01001127struct mvpp2_txq_pcpu_buf {
1128 /* Transmitted SKB */
1129 struct sk_buff *skb;
1130
1131 /* Physical address of transmitted buffer */
Thomas Petazzoni20396132017-03-07 16:53:00 +01001132 dma_addr_t dma;
Thomas Petazzoni83544912016-12-21 11:28:49 +01001133
1134 /* Size transmitted */
1135 size_t size;
1136};
1137
Marcin Wojtas3f518502014-07-10 16:52:13 -03001138/* Per-CPU Tx queue control */
1139struct mvpp2_txq_pcpu {
1140 int cpu;
1141
1142 /* Number of Tx DMA descriptors in the descriptor ring */
1143 int size;
1144
1145 /* Number of currently used Tx DMA descriptor in the
1146 * descriptor ring
1147 */
1148 int count;
1149
Antoine Tenart1d17db02017-10-30 11:23:31 +01001150 int wake_threshold;
1151 int stop_threshold;
1152
Marcin Wojtas3f518502014-07-10 16:52:13 -03001153 /* Number of Tx DMA descriptors reserved for each CPU */
1154 int reserved_num;
1155
Thomas Petazzoni83544912016-12-21 11:28:49 +01001156 /* Infos about transmitted buffers */
1157 struct mvpp2_txq_pcpu_buf *buffs;
Marcin Wojtas71ce3912015-08-06 19:00:29 +02001158
Marcin Wojtas3f518502014-07-10 16:52:13 -03001159 /* Index of last TX DMA descriptor that was inserted */
1160 int txq_put_index;
1161
1162 /* Index of the TX DMA descriptor to be cleaned up */
1163 int txq_get_index;
Antoine Ténart186cd4d2017-08-23 09:46:56 +02001164
1165 /* DMA buffer for TSO headers */
1166 char *tso_headers;
1167 dma_addr_t tso_headers_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001168};
1169
1170struct mvpp2_tx_queue {
1171 /* Physical number of this Tx queue */
1172 u8 id;
1173
1174 /* Logical number of this Tx queue */
1175 u8 log_id;
1176
1177 /* Number of Tx DMA descriptors in the descriptor ring */
1178 int size;
1179
1180 /* Number of currently used Tx DMA descriptor in the descriptor ring */
1181 int count;
1182
1183 /* Per-CPU control of physical Tx queues */
1184 struct mvpp2_txq_pcpu __percpu *pcpu;
1185
Marcin Wojtas3f518502014-07-10 16:52:13 -03001186 u32 done_pkts_coal;
1187
1188 /* Virtual address of thex Tx DMA descriptors array */
1189 struct mvpp2_tx_desc *descs;
1190
1191 /* DMA address of the Tx DMA descriptors array */
Thomas Petazzoni20396132017-03-07 16:53:00 +01001192 dma_addr_t descs_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001193
1194 /* Index of the last Tx DMA descriptor */
1195 int last_desc;
1196
1197 /* Index of the next Tx DMA descriptor to process */
1198 int next_desc_to_proc;
1199};
1200
1201struct mvpp2_rx_queue {
1202 /* RX queue number, in the range 0-31 for physical RXQs */
1203 u8 id;
1204
1205 /* Num of rx descriptors in the rx descriptor ring */
1206 int size;
1207
1208 u32 pkts_coal;
1209 u32 time_coal;
1210
1211 /* Virtual address of the RX DMA descriptors array */
1212 struct mvpp2_rx_desc *descs;
1213
1214 /* DMA address of the RX DMA descriptors array */
Thomas Petazzoni20396132017-03-07 16:53:00 +01001215 dma_addr_t descs_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001216
1217 /* Index of the last RX DMA descriptor */
1218 int last_desc;
1219
1220 /* Index of the next RX DMA descriptor to process */
1221 int next_desc_to_proc;
1222
1223 /* ID of port to which physical RXQ is mapped */
1224 int port;
1225
1226 /* Port's logic RXQ number to which physical RXQ is mapped */
1227 int logic_rxq;
1228};
1229
1230union mvpp2_prs_tcam_entry {
1231 u32 word[MVPP2_PRS_TCAM_WORDS];
1232 u8 byte[MVPP2_PRS_TCAM_WORDS * 4];
1233};
1234
1235union mvpp2_prs_sram_entry {
1236 u32 word[MVPP2_PRS_SRAM_WORDS];
1237 u8 byte[MVPP2_PRS_SRAM_WORDS * 4];
1238};
1239
1240struct mvpp2_prs_entry {
1241 u32 index;
1242 union mvpp2_prs_tcam_entry tcam;
1243 union mvpp2_prs_sram_entry sram;
1244};
1245
1246struct mvpp2_prs_shadow {
1247 bool valid;
1248 bool finish;
1249
1250 /* Lookup ID */
1251 int lu;
1252
1253 /* User defined offset */
1254 int udf;
1255
1256 /* Result info */
1257 u32 ri;
1258 u32 ri_mask;
1259};
1260
1261struct mvpp2_cls_flow_entry {
1262 u32 index;
1263 u32 data[MVPP2_CLS_FLOWS_TBL_DATA_WORDS];
1264};
1265
1266struct mvpp2_cls_lookup_entry {
1267 u32 lkpid;
1268 u32 way;
1269 u32 data;
1270};
1271
1272struct mvpp2_bm_pool {
1273 /* Pool number in the range 0-7 */
1274 int id;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001275
1276 /* Buffer Pointers Pool External (BPPE) size */
1277 int size;
Thomas Petazzonid01524d2017-03-07 16:53:09 +01001278 /* BPPE size in bytes */
1279 int size_bytes;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001280 /* Number of buffers for this pool */
1281 int buf_num;
1282 /* Pool buffer size */
1283 int buf_size;
1284 /* Packet size */
1285 int pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01001286 int frag_size;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001287
1288 /* BPPE virtual base address */
1289 u32 *virt_addr;
Thomas Petazzoni20396132017-03-07 16:53:00 +01001290 /* BPPE DMA base address */
1291 dma_addr_t dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001292
1293 /* Ports using BM pool */
1294 u32 port_map;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001295};
1296
Antoine Tenart20920262017-10-23 15:24:30 +02001297#define IS_TSO_HEADER(txq_pcpu, addr) \
1298 ((addr) >= (txq_pcpu)->tso_headers_dma && \
1299 (addr) < (txq_pcpu)->tso_headers_dma + \
1300 (txq_pcpu)->size * TSO_HEADER_SIZE)
1301
Thomas Petazzoni213f4282017-08-03 10:42:00 +02001302/* Queue modes */
1303#define MVPP2_QDIST_SINGLE_MODE 0
1304#define MVPP2_QDIST_MULTI_MODE 1
1305
1306static int queue_mode = MVPP2_QDIST_SINGLE_MODE;
1307
1308module_param(queue_mode, int, 0444);
1309MODULE_PARM_DESC(queue_mode, "Set queue_mode (single=0, multi=1)");
1310
Marcin Wojtas3f518502014-07-10 16:52:13 -03001311#define MVPP2_DRIVER_NAME "mvpp2"
1312#define MVPP2_DRIVER_VERSION "1.0"
1313
1314/* Utility/helper methods */
1315
1316static void mvpp2_write(struct mvpp2 *priv, u32 offset, u32 data)
1317{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001318 writel(data, priv->swth_base[0] + offset);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001319}
1320
1321static u32 mvpp2_read(struct mvpp2 *priv, u32 offset)
1322{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001323 return readl(priv->swth_base[0] + offset);
Thomas Petazzonia7868412017-03-07 16:53:13 +01001324}
1325
1326/* These accessors should be used to access:
1327 *
1328 * - per-CPU registers, where each CPU has its own copy of the
1329 * register.
1330 *
1331 * MVPP2_BM_VIRT_ALLOC_REG
1332 * MVPP2_BM_ADDR_HIGH_ALLOC
1333 * MVPP22_BM_ADDR_HIGH_RLS_REG
1334 * MVPP2_BM_VIRT_RLS_REG
1335 * MVPP2_ISR_RX_TX_CAUSE_REG
1336 * MVPP2_ISR_RX_TX_MASK_REG
1337 * MVPP2_TXQ_NUM_REG
1338 * MVPP2_AGGR_TXQ_UPDATE_REG
1339 * MVPP2_TXQ_RSVD_REQ_REG
1340 * MVPP2_TXQ_RSVD_RSLT_REG
1341 * MVPP2_TXQ_SENT_REG
1342 * MVPP2_RXQ_NUM_REG
1343 *
1344 * - global registers that must be accessed through a specific CPU
1345 * window, because they are related to an access to a per-CPU
1346 * register
1347 *
1348 * MVPP2_BM_PHY_ALLOC_REG (related to MVPP2_BM_VIRT_ALLOC_REG)
1349 * MVPP2_BM_PHY_RLS_REG (related to MVPP2_BM_VIRT_RLS_REG)
1350 * MVPP2_RXQ_THRESH_REG (related to MVPP2_RXQ_NUM_REG)
1351 * MVPP2_RXQ_DESC_ADDR_REG (related to MVPP2_RXQ_NUM_REG)
1352 * MVPP2_RXQ_DESC_SIZE_REG (related to MVPP2_RXQ_NUM_REG)
1353 * MVPP2_RXQ_INDEX_REG (related to MVPP2_RXQ_NUM_REG)
1354 * MVPP2_TXQ_PENDING_REG (related to MVPP2_TXQ_NUM_REG)
1355 * MVPP2_TXQ_DESC_ADDR_REG (related to MVPP2_TXQ_NUM_REG)
1356 * MVPP2_TXQ_DESC_SIZE_REG (related to MVPP2_TXQ_NUM_REG)
1357 * MVPP2_TXQ_INDEX_REG (related to MVPP2_TXQ_NUM_REG)
1358 * MVPP2_TXQ_PENDING_REG (related to MVPP2_TXQ_NUM_REG)
1359 * MVPP2_TXQ_PREF_BUF_REG (related to MVPP2_TXQ_NUM_REG)
1360 * MVPP2_TXQ_PREF_BUF_REG (related to MVPP2_TXQ_NUM_REG)
1361 */
1362static void mvpp2_percpu_write(struct mvpp2 *priv, int cpu,
1363 u32 offset, u32 data)
1364{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001365 writel(data, priv->swth_base[cpu] + offset);
Thomas Petazzonia7868412017-03-07 16:53:13 +01001366}
1367
1368static u32 mvpp2_percpu_read(struct mvpp2 *priv, int cpu,
1369 u32 offset)
1370{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001371 return readl(priv->swth_base[cpu] + offset);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001372}
1373
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001374static dma_addr_t mvpp2_txdesc_dma_addr_get(struct mvpp2_port *port,
1375 struct mvpp2_tx_desc *tx_desc)
1376{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001377 if (port->priv->hw_version == MVPP21)
1378 return tx_desc->pp21.buf_dma_addr;
1379 else
1380 return tx_desc->pp22.buf_dma_addr_ptp & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001381}
1382
1383static void mvpp2_txdesc_dma_addr_set(struct mvpp2_port *port,
1384 struct mvpp2_tx_desc *tx_desc,
1385 dma_addr_t dma_addr)
1386{
Antoine Tenart6eb5d372017-10-30 11:23:33 +01001387 dma_addr_t addr, offset;
1388
1389 addr = dma_addr & ~MVPP2_TX_DESC_ALIGN;
1390 offset = dma_addr & MVPP2_TX_DESC_ALIGN;
1391
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001392 if (port->priv->hw_version == MVPP21) {
Antoine Tenart6eb5d372017-10-30 11:23:33 +01001393 tx_desc->pp21.buf_dma_addr = addr;
1394 tx_desc->pp21.packet_offset = offset;
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001395 } else {
Antoine Tenart6eb5d372017-10-30 11:23:33 +01001396 u64 val = (u64)addr;
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001397
1398 tx_desc->pp22.buf_dma_addr_ptp &= ~GENMASK_ULL(40, 0);
1399 tx_desc->pp22.buf_dma_addr_ptp |= val;
Antoine Tenart6eb5d372017-10-30 11:23:33 +01001400 tx_desc->pp22.packet_offset = offset;
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001401 }
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001402}
1403
1404static size_t mvpp2_txdesc_size_get(struct mvpp2_port *port,
1405 struct mvpp2_tx_desc *tx_desc)
1406{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001407 if (port->priv->hw_version == MVPP21)
1408 return tx_desc->pp21.data_size;
1409 else
1410 return tx_desc->pp22.data_size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001411}
1412
1413static void mvpp2_txdesc_size_set(struct mvpp2_port *port,
1414 struct mvpp2_tx_desc *tx_desc,
1415 size_t size)
1416{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001417 if (port->priv->hw_version == MVPP21)
1418 tx_desc->pp21.data_size = size;
1419 else
1420 tx_desc->pp22.data_size = size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001421}
1422
1423static void mvpp2_txdesc_txq_set(struct mvpp2_port *port,
1424 struct mvpp2_tx_desc *tx_desc,
1425 unsigned int txq)
1426{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001427 if (port->priv->hw_version == MVPP21)
1428 tx_desc->pp21.phys_txq = txq;
1429 else
1430 tx_desc->pp22.phys_txq = txq;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001431}
1432
1433static void mvpp2_txdesc_cmd_set(struct mvpp2_port *port,
1434 struct mvpp2_tx_desc *tx_desc,
1435 unsigned int command)
1436{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001437 if (port->priv->hw_version == MVPP21)
1438 tx_desc->pp21.command = command;
1439 else
1440 tx_desc->pp22.command = command;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001441}
1442
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001443static unsigned int mvpp2_txdesc_offset_get(struct mvpp2_port *port,
1444 struct mvpp2_tx_desc *tx_desc)
1445{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001446 if (port->priv->hw_version == MVPP21)
1447 return tx_desc->pp21.packet_offset;
1448 else
1449 return tx_desc->pp22.packet_offset;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001450}
1451
1452static dma_addr_t mvpp2_rxdesc_dma_addr_get(struct mvpp2_port *port,
1453 struct mvpp2_rx_desc *rx_desc)
1454{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001455 if (port->priv->hw_version == MVPP21)
1456 return rx_desc->pp21.buf_dma_addr;
1457 else
1458 return rx_desc->pp22.buf_dma_addr_key_hash & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001459}
1460
1461static unsigned long mvpp2_rxdesc_cookie_get(struct mvpp2_port *port,
1462 struct mvpp2_rx_desc *rx_desc)
1463{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001464 if (port->priv->hw_version == MVPP21)
1465 return rx_desc->pp21.buf_cookie;
1466 else
1467 return rx_desc->pp22.buf_cookie_misc & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001468}
1469
1470static size_t mvpp2_rxdesc_size_get(struct mvpp2_port *port,
1471 struct mvpp2_rx_desc *rx_desc)
1472{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001473 if (port->priv->hw_version == MVPP21)
1474 return rx_desc->pp21.data_size;
1475 else
1476 return rx_desc->pp22.data_size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001477}
1478
1479static u32 mvpp2_rxdesc_status_get(struct mvpp2_port *port,
1480 struct mvpp2_rx_desc *rx_desc)
1481{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001482 if (port->priv->hw_version == MVPP21)
1483 return rx_desc->pp21.status;
1484 else
1485 return rx_desc->pp22.status;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001486}
1487
Marcin Wojtas3f518502014-07-10 16:52:13 -03001488static void mvpp2_txq_inc_get(struct mvpp2_txq_pcpu *txq_pcpu)
1489{
1490 txq_pcpu->txq_get_index++;
1491 if (txq_pcpu->txq_get_index == txq_pcpu->size)
1492 txq_pcpu->txq_get_index = 0;
1493}
1494
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001495static void mvpp2_txq_inc_put(struct mvpp2_port *port,
1496 struct mvpp2_txq_pcpu *txq_pcpu,
Marcin Wojtas71ce3912015-08-06 19:00:29 +02001497 struct sk_buff *skb,
1498 struct mvpp2_tx_desc *tx_desc)
Marcin Wojtas3f518502014-07-10 16:52:13 -03001499{
Thomas Petazzoni83544912016-12-21 11:28:49 +01001500 struct mvpp2_txq_pcpu_buf *tx_buf =
1501 txq_pcpu->buffs + txq_pcpu->txq_put_index;
1502 tx_buf->skb = skb;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001503 tx_buf->size = mvpp2_txdesc_size_get(port, tx_desc);
1504 tx_buf->dma = mvpp2_txdesc_dma_addr_get(port, tx_desc) +
1505 mvpp2_txdesc_offset_get(port, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001506 txq_pcpu->txq_put_index++;
1507 if (txq_pcpu->txq_put_index == txq_pcpu->size)
1508 txq_pcpu->txq_put_index = 0;
1509}
1510
1511/* Get number of physical egress port */
1512static inline int mvpp2_egress_port(struct mvpp2_port *port)
1513{
1514 return MVPP2_MAX_TCONT + port->id;
1515}
1516
1517/* Get number of physical TXQ */
1518static inline int mvpp2_txq_phys(int port, int txq)
1519{
1520 return (MVPP2_MAX_TCONT + port) * MVPP2_MAX_TXQ + txq;
1521}
1522
1523/* Parser configuration routines */
1524
1525/* Update parser tcam and sram hw entries */
1526static int mvpp2_prs_hw_write(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1527{
1528 int i;
1529
1530 if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1531 return -EINVAL;
1532
1533 /* Clear entry invalidation bit */
1534 pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] &= ~MVPP2_PRS_TCAM_INV_MASK;
1535
1536 /* Write tcam index - indirect access */
1537 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1538 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1539 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), pe->tcam.word[i]);
1540
1541 /* Write sram index - indirect access */
1542 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1543 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1544 mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), pe->sram.word[i]);
1545
1546 return 0;
1547}
1548
1549/* Read tcam entry from hw */
1550static int mvpp2_prs_hw_read(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1551{
1552 int i;
1553
1554 if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1555 return -EINVAL;
1556
1557 /* Write tcam index - indirect access */
1558 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1559
1560 pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] = mvpp2_read(priv,
1561 MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD));
1562 if (pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] & MVPP2_PRS_TCAM_INV_MASK)
1563 return MVPP2_PRS_TCAM_ENTRY_INVALID;
1564
1565 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1566 pe->tcam.word[i] = mvpp2_read(priv, MVPP2_PRS_TCAM_DATA_REG(i));
1567
1568 /* Write sram index - indirect access */
1569 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1570 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1571 pe->sram.word[i] = mvpp2_read(priv, MVPP2_PRS_SRAM_DATA_REG(i));
1572
1573 return 0;
1574}
1575
1576/* Invalidate tcam hw entry */
1577static void mvpp2_prs_hw_inv(struct mvpp2 *priv, int index)
1578{
1579 /* Write index - indirect access */
1580 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
1581 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD),
1582 MVPP2_PRS_TCAM_INV_MASK);
1583}
1584
1585/* Enable shadow table entry and set its lookup ID */
1586static void mvpp2_prs_shadow_set(struct mvpp2 *priv, int index, int lu)
1587{
1588 priv->prs_shadow[index].valid = true;
1589 priv->prs_shadow[index].lu = lu;
1590}
1591
1592/* Update ri fields in shadow table entry */
1593static void mvpp2_prs_shadow_ri_set(struct mvpp2 *priv, int index,
1594 unsigned int ri, unsigned int ri_mask)
1595{
1596 priv->prs_shadow[index].ri_mask = ri_mask;
1597 priv->prs_shadow[index].ri = ri;
1598}
1599
1600/* Update lookup field in tcam sw entry */
1601static void mvpp2_prs_tcam_lu_set(struct mvpp2_prs_entry *pe, unsigned int lu)
1602{
1603 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_LU_BYTE);
1604
1605 pe->tcam.byte[MVPP2_PRS_TCAM_LU_BYTE] = lu;
1606 pe->tcam.byte[enable_off] = MVPP2_PRS_LU_MASK;
1607}
1608
1609/* Update mask for single port in tcam sw entry */
1610static void mvpp2_prs_tcam_port_set(struct mvpp2_prs_entry *pe,
1611 unsigned int port, bool add)
1612{
1613 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1614
1615 if (add)
1616 pe->tcam.byte[enable_off] &= ~(1 << port);
1617 else
1618 pe->tcam.byte[enable_off] |= 1 << port;
1619}
1620
1621/* Update port map in tcam sw entry */
1622static void mvpp2_prs_tcam_port_map_set(struct mvpp2_prs_entry *pe,
1623 unsigned int ports)
1624{
1625 unsigned char port_mask = MVPP2_PRS_PORT_MASK;
1626 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1627
1628 pe->tcam.byte[MVPP2_PRS_TCAM_PORT_BYTE] = 0;
1629 pe->tcam.byte[enable_off] &= ~port_mask;
1630 pe->tcam.byte[enable_off] |= ~ports & MVPP2_PRS_PORT_MASK;
1631}
1632
1633/* Obtain port map from tcam sw entry */
1634static unsigned int mvpp2_prs_tcam_port_map_get(struct mvpp2_prs_entry *pe)
1635{
1636 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1637
1638 return ~(pe->tcam.byte[enable_off]) & MVPP2_PRS_PORT_MASK;
1639}
1640
1641/* Set byte of data and its enable bits in tcam sw entry */
1642static void mvpp2_prs_tcam_data_byte_set(struct mvpp2_prs_entry *pe,
1643 unsigned int offs, unsigned char byte,
1644 unsigned char enable)
1645{
1646 pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)] = byte;
1647 pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)] = enable;
1648}
1649
1650/* Get byte of data and its enable bits from tcam sw entry */
1651static void mvpp2_prs_tcam_data_byte_get(struct mvpp2_prs_entry *pe,
1652 unsigned int offs, unsigned char *byte,
1653 unsigned char *enable)
1654{
1655 *byte = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)];
1656 *enable = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)];
1657}
1658
1659/* Compare tcam data bytes with a pattern */
1660static bool mvpp2_prs_tcam_data_cmp(struct mvpp2_prs_entry *pe, int offs,
1661 u16 data)
1662{
1663 int off = MVPP2_PRS_TCAM_DATA_BYTE(offs);
1664 u16 tcam_data;
1665
Antoine Tenartef4816f2017-10-24 11:41:26 +02001666 tcam_data = (pe->tcam.byte[off + 1] << 8) | pe->tcam.byte[off];
Marcin Wojtas3f518502014-07-10 16:52:13 -03001667 if (tcam_data != data)
1668 return false;
1669 return true;
1670}
1671
1672/* Update ai bits in tcam sw entry */
1673static void mvpp2_prs_tcam_ai_update(struct mvpp2_prs_entry *pe,
1674 unsigned int bits, unsigned int enable)
1675{
1676 int i, ai_idx = MVPP2_PRS_TCAM_AI_BYTE;
1677
1678 for (i = 0; i < MVPP2_PRS_AI_BITS; i++) {
1679
1680 if (!(enable & BIT(i)))
1681 continue;
1682
1683 if (bits & BIT(i))
1684 pe->tcam.byte[ai_idx] |= 1 << i;
1685 else
1686 pe->tcam.byte[ai_idx] &= ~(1 << i);
1687 }
1688
1689 pe->tcam.byte[MVPP2_PRS_TCAM_EN_OFFS(ai_idx)] |= enable;
1690}
1691
1692/* Get ai bits from tcam sw entry */
1693static int mvpp2_prs_tcam_ai_get(struct mvpp2_prs_entry *pe)
1694{
1695 return pe->tcam.byte[MVPP2_PRS_TCAM_AI_BYTE];
1696}
1697
1698/* Set ethertype in tcam sw entry */
1699static void mvpp2_prs_match_etype(struct mvpp2_prs_entry *pe, int offset,
1700 unsigned short ethertype)
1701{
1702 mvpp2_prs_tcam_data_byte_set(pe, offset + 0, ethertype >> 8, 0xff);
1703 mvpp2_prs_tcam_data_byte_set(pe, offset + 1, ethertype & 0xff, 0xff);
1704}
1705
Maxime Chevallier56beda32018-02-28 10:14:13 +01001706/* Set vid in tcam sw entry */
1707static void mvpp2_prs_match_vid(struct mvpp2_prs_entry *pe, int offset,
1708 unsigned short vid)
1709{
1710 mvpp2_prs_tcam_data_byte_set(pe, offset + 0, (vid & 0xf00) >> 8, 0xf);
1711 mvpp2_prs_tcam_data_byte_set(pe, offset + 1, vid & 0xff, 0xff);
1712}
1713
Marcin Wojtas3f518502014-07-10 16:52:13 -03001714/* Set bits in sram sw entry */
1715static void mvpp2_prs_sram_bits_set(struct mvpp2_prs_entry *pe, int bit_num,
1716 int val)
1717{
1718 pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] |= (val << (bit_num % 8));
1719}
1720
1721/* Clear bits in sram sw entry */
1722static void mvpp2_prs_sram_bits_clear(struct mvpp2_prs_entry *pe, int bit_num,
1723 int val)
1724{
1725 pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] &= ~(val << (bit_num % 8));
1726}
1727
1728/* Update ri bits in sram sw entry */
1729static void mvpp2_prs_sram_ri_update(struct mvpp2_prs_entry *pe,
1730 unsigned int bits, unsigned int mask)
1731{
1732 unsigned int i;
1733
1734 for (i = 0; i < MVPP2_PRS_SRAM_RI_CTRL_BITS; i++) {
1735 int ri_off = MVPP2_PRS_SRAM_RI_OFFS;
1736
1737 if (!(mask & BIT(i)))
1738 continue;
1739
1740 if (bits & BIT(i))
1741 mvpp2_prs_sram_bits_set(pe, ri_off + i, 1);
1742 else
1743 mvpp2_prs_sram_bits_clear(pe, ri_off + i, 1);
1744
1745 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_RI_CTRL_OFFS + i, 1);
1746 }
1747}
1748
1749/* Obtain ri bits from sram sw entry */
1750static int mvpp2_prs_sram_ri_get(struct mvpp2_prs_entry *pe)
1751{
1752 return pe->sram.word[MVPP2_PRS_SRAM_RI_WORD];
1753}
1754
1755/* Update ai bits in sram sw entry */
1756static void mvpp2_prs_sram_ai_update(struct mvpp2_prs_entry *pe,
1757 unsigned int bits, unsigned int mask)
1758{
1759 unsigned int i;
1760 int ai_off = MVPP2_PRS_SRAM_AI_OFFS;
1761
1762 for (i = 0; i < MVPP2_PRS_SRAM_AI_CTRL_BITS; i++) {
1763
1764 if (!(mask & BIT(i)))
1765 continue;
1766
1767 if (bits & BIT(i))
1768 mvpp2_prs_sram_bits_set(pe, ai_off + i, 1);
1769 else
1770 mvpp2_prs_sram_bits_clear(pe, ai_off + i, 1);
1771
1772 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_AI_CTRL_OFFS + i, 1);
1773 }
1774}
1775
1776/* Read ai bits from sram sw entry */
1777static int mvpp2_prs_sram_ai_get(struct mvpp2_prs_entry *pe)
1778{
1779 u8 bits;
1780 int ai_off = MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_AI_OFFS);
1781 int ai_en_off = ai_off + 1;
1782 int ai_shift = MVPP2_PRS_SRAM_AI_OFFS % 8;
1783
1784 bits = (pe->sram.byte[ai_off] >> ai_shift) |
1785 (pe->sram.byte[ai_en_off] << (8 - ai_shift));
1786
1787 return bits;
1788}
1789
1790/* In sram sw entry set lookup ID field of the tcam key to be used in the next
1791 * lookup interation
1792 */
1793static void mvpp2_prs_sram_next_lu_set(struct mvpp2_prs_entry *pe,
1794 unsigned int lu)
1795{
1796 int sram_next_off = MVPP2_PRS_SRAM_NEXT_LU_OFFS;
1797
1798 mvpp2_prs_sram_bits_clear(pe, sram_next_off,
1799 MVPP2_PRS_SRAM_NEXT_LU_MASK);
1800 mvpp2_prs_sram_bits_set(pe, sram_next_off, lu);
1801}
1802
1803/* In the sram sw entry set sign and value of the next lookup offset
1804 * and the offset value generated to the classifier
1805 */
1806static void mvpp2_prs_sram_shift_set(struct mvpp2_prs_entry *pe, int shift,
1807 unsigned int op)
1808{
1809 /* Set sign */
1810 if (shift < 0) {
1811 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1812 shift = 0 - shift;
1813 } else {
1814 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1815 }
1816
1817 /* Set value */
1818 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_SHIFT_OFFS)] =
1819 (unsigned char)shift;
1820
1821 /* Reset and set operation */
1822 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS,
1823 MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK);
1824 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS, op);
1825
1826 /* Set base offset as current */
1827 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1828}
1829
1830/* In the sram sw entry set sign and value of the user defined offset
1831 * generated to the classifier
1832 */
1833static void mvpp2_prs_sram_offset_set(struct mvpp2_prs_entry *pe,
1834 unsigned int type, int offset,
1835 unsigned int op)
1836{
1837 /* Set sign */
1838 if (offset < 0) {
1839 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1840 offset = 0 - offset;
1841 } else {
1842 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1843 }
1844
1845 /* Set value */
1846 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_OFFS,
1847 MVPP2_PRS_SRAM_UDF_MASK);
1848 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_OFFS, offset);
1849 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1850 MVPP2_PRS_SRAM_UDF_BITS)] &=
1851 ~(MVPP2_PRS_SRAM_UDF_MASK >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1852 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1853 MVPP2_PRS_SRAM_UDF_BITS)] |=
1854 (offset >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1855
1856 /* Set offset type */
1857 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS,
1858 MVPP2_PRS_SRAM_UDF_TYPE_MASK);
1859 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS, type);
1860
1861 /* Set offset operation */
1862 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS,
1863 MVPP2_PRS_SRAM_OP_SEL_UDF_MASK);
1864 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS, op);
1865
1866 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1867 MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] &=
1868 ~(MVPP2_PRS_SRAM_OP_SEL_UDF_MASK >>
1869 (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1870
1871 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1872 MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] |=
1873 (op >> (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1874
1875 /* Set base offset as current */
1876 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1877}
1878
1879/* Find parser flow entry */
1880static struct mvpp2_prs_entry *mvpp2_prs_flow_find(struct mvpp2 *priv, int flow)
1881{
1882 struct mvpp2_prs_entry *pe;
1883 int tid;
1884
1885 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
1886 if (!pe)
1887 return NULL;
1888 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
1889
1890 /* Go through the all entires with MVPP2_PRS_LU_FLOWS */
1891 for (tid = MVPP2_PRS_TCAM_SRAM_SIZE - 1; tid >= 0; tid--) {
1892 u8 bits;
1893
1894 if (!priv->prs_shadow[tid].valid ||
1895 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_FLOWS)
1896 continue;
1897
1898 pe->index = tid;
1899 mvpp2_prs_hw_read(priv, pe);
1900 bits = mvpp2_prs_sram_ai_get(pe);
1901
1902 /* Sram store classification lookup ID in AI bits [5:0] */
1903 if ((bits & MVPP2_PRS_FLOW_ID_MASK) == flow)
1904 return pe;
1905 }
1906 kfree(pe);
1907
1908 return NULL;
1909}
1910
1911/* Return first free tcam index, seeking from start to end */
1912static int mvpp2_prs_tcam_first_free(struct mvpp2 *priv, unsigned char start,
1913 unsigned char end)
1914{
1915 int tid;
1916
1917 if (start > end)
1918 swap(start, end);
1919
1920 if (end >= MVPP2_PRS_TCAM_SRAM_SIZE)
1921 end = MVPP2_PRS_TCAM_SRAM_SIZE - 1;
1922
1923 for (tid = start; tid <= end; tid++) {
1924 if (!priv->prs_shadow[tid].valid)
1925 return tid;
1926 }
1927
1928 return -EINVAL;
1929}
1930
1931/* Enable/disable dropping all mac da's */
1932static void mvpp2_prs_mac_drop_all_set(struct mvpp2 *priv, int port, bool add)
1933{
1934 struct mvpp2_prs_entry pe;
1935
1936 if (priv->prs_shadow[MVPP2_PE_DROP_ALL].valid) {
1937 /* Entry exist - update port only */
1938 pe.index = MVPP2_PE_DROP_ALL;
1939 mvpp2_prs_hw_read(priv, &pe);
1940 } else {
1941 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001942 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001943 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1944 pe.index = MVPP2_PE_DROP_ALL;
1945
1946 /* Non-promiscuous mode for all ports - DROP unknown packets */
1947 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
1948 MVPP2_PRS_RI_DROP_MASK);
1949
1950 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1951 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1952
1953 /* Update shadow table */
1954 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1955
1956 /* Mask all ports */
1957 mvpp2_prs_tcam_port_map_set(&pe, 0);
1958 }
1959
1960 /* Update port mask */
1961 mvpp2_prs_tcam_port_set(&pe, port, add);
1962
1963 mvpp2_prs_hw_write(priv, &pe);
1964}
1965
1966/* Set port to promiscuous mode */
1967static void mvpp2_prs_mac_promisc_set(struct mvpp2 *priv, int port, bool add)
1968{
1969 struct mvpp2_prs_entry pe;
1970
Joe Perchesdbedd442015-03-06 20:49:12 -08001971 /* Promiscuous mode - Accept unknown packets */
Marcin Wojtas3f518502014-07-10 16:52:13 -03001972
1973 if (priv->prs_shadow[MVPP2_PE_MAC_PROMISCUOUS].valid) {
1974 /* Entry exist - update port only */
1975 pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1976 mvpp2_prs_hw_read(priv, &pe);
1977 } else {
1978 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001979 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001980 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1981 pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1982
1983 /* Continue - set next lookup */
1984 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
1985
1986 /* Set result info bits */
1987 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_UCAST,
1988 MVPP2_PRS_RI_L2_CAST_MASK);
1989
1990 /* Shift to ethertype */
1991 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
1992 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1993
1994 /* Mask all ports */
1995 mvpp2_prs_tcam_port_map_set(&pe, 0);
1996
1997 /* Update shadow table */
1998 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1999 }
2000
2001 /* Update port mask */
2002 mvpp2_prs_tcam_port_set(&pe, port, add);
2003
2004 mvpp2_prs_hw_write(priv, &pe);
2005}
2006
2007/* Accept multicast */
2008static void mvpp2_prs_mac_multi_set(struct mvpp2 *priv, int port, int index,
2009 bool add)
2010{
2011 struct mvpp2_prs_entry pe;
2012 unsigned char da_mc;
2013
2014 /* Ethernet multicast address first byte is
2015 * 0x01 for IPv4 and 0x33 for IPv6
2016 */
2017 da_mc = (index == MVPP2_PE_MAC_MC_ALL) ? 0x01 : 0x33;
2018
2019 if (priv->prs_shadow[index].valid) {
2020 /* Entry exist - update port only */
2021 pe.index = index;
2022 mvpp2_prs_hw_read(priv, &pe);
2023 } else {
2024 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002025 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002026 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
2027 pe.index = index;
2028
2029 /* Continue - set next lookup */
2030 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
2031
2032 /* Set result info bits */
2033 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_MCAST,
2034 MVPP2_PRS_RI_L2_CAST_MASK);
2035
2036 /* Update tcam entry data first byte */
2037 mvpp2_prs_tcam_data_byte_set(&pe, 0, da_mc, 0xff);
2038
2039 /* Shift to ethertype */
2040 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
2041 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2042
2043 /* Mask all ports */
2044 mvpp2_prs_tcam_port_map_set(&pe, 0);
2045
2046 /* Update shadow table */
2047 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2048 }
2049
2050 /* Update port mask */
2051 mvpp2_prs_tcam_port_set(&pe, port, add);
2052
2053 mvpp2_prs_hw_write(priv, &pe);
2054}
2055
2056/* Set entry for dsa packets */
2057static void mvpp2_prs_dsa_tag_set(struct mvpp2 *priv, int port, bool add,
2058 bool tagged, bool extend)
2059{
2060 struct mvpp2_prs_entry pe;
2061 int tid, shift;
2062
2063 if (extend) {
2064 tid = tagged ? MVPP2_PE_EDSA_TAGGED : MVPP2_PE_EDSA_UNTAGGED;
2065 shift = 8;
2066 } else {
2067 tid = tagged ? MVPP2_PE_DSA_TAGGED : MVPP2_PE_DSA_UNTAGGED;
2068 shift = 4;
2069 }
2070
2071 if (priv->prs_shadow[tid].valid) {
2072 /* Entry exist - update port only */
2073 pe.index = tid;
2074 mvpp2_prs_hw_read(priv, &pe);
2075 } else {
2076 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002077 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002078 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
2079 pe.index = tid;
2080
Marcin Wojtas3f518502014-07-10 16:52:13 -03002081 /* Update shadow table */
2082 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
2083
2084 if (tagged) {
2085 /* Set tagged bit in DSA tag */
2086 mvpp2_prs_tcam_data_byte_set(&pe, 0,
Maxime Chevallier56beda32018-02-28 10:14:13 +01002087 MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
2088 MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
2089
2090 /* Set ai bits for next iteration */
2091 if (extend)
2092 mvpp2_prs_sram_ai_update(&pe, 1,
2093 MVPP2_PRS_SRAM_AI_MASK);
2094 else
2095 mvpp2_prs_sram_ai_update(&pe, 0,
2096 MVPP2_PRS_SRAM_AI_MASK);
2097
2098 /* If packet is tagged continue check vid filtering */
2099 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VID);
Marcin Wojtas3f518502014-07-10 16:52:13 -03002100 } else {
Maxime Chevallier56beda32018-02-28 10:14:13 +01002101 /* Shift 4 bytes for DSA tag or 8 bytes for EDSA tag*/
2102 mvpp2_prs_sram_shift_set(&pe, shift,
2103 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2104
Marcin Wojtas3f518502014-07-10 16:52:13 -03002105 /* Set result info bits to 'no vlans' */
2106 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
2107 MVPP2_PRS_RI_VLAN_MASK);
2108 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2109 }
2110
2111 /* Mask all ports */
2112 mvpp2_prs_tcam_port_map_set(&pe, 0);
2113 }
2114
2115 /* Update port mask */
2116 mvpp2_prs_tcam_port_set(&pe, port, add);
2117
2118 mvpp2_prs_hw_write(priv, &pe);
2119}
2120
2121/* Set entry for dsa ethertype */
2122static void mvpp2_prs_dsa_tag_ethertype_set(struct mvpp2 *priv, int port,
2123 bool add, bool tagged, bool extend)
2124{
2125 struct mvpp2_prs_entry pe;
2126 int tid, shift, port_mask;
2127
2128 if (extend) {
2129 tid = tagged ? MVPP2_PE_ETYPE_EDSA_TAGGED :
2130 MVPP2_PE_ETYPE_EDSA_UNTAGGED;
2131 port_mask = 0;
2132 shift = 8;
2133 } else {
2134 tid = tagged ? MVPP2_PE_ETYPE_DSA_TAGGED :
2135 MVPP2_PE_ETYPE_DSA_UNTAGGED;
2136 port_mask = MVPP2_PRS_PORT_MASK;
2137 shift = 4;
2138 }
2139
2140 if (priv->prs_shadow[tid].valid) {
2141 /* Entry exist - update port only */
2142 pe.index = tid;
2143 mvpp2_prs_hw_read(priv, &pe);
2144 } else {
2145 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002146 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002147 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
2148 pe.index = tid;
2149
2150 /* Set ethertype */
2151 mvpp2_prs_match_etype(&pe, 0, ETH_P_EDSA);
2152 mvpp2_prs_match_etype(&pe, 2, 0);
2153
2154 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DSA_MASK,
2155 MVPP2_PRS_RI_DSA_MASK);
2156 /* Shift ethertype + 2 byte reserved + tag*/
2157 mvpp2_prs_sram_shift_set(&pe, 2 + MVPP2_ETH_TYPE_LEN + shift,
2158 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2159
2160 /* Update shadow table */
2161 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
2162
2163 if (tagged) {
2164 /* Set tagged bit in DSA tag */
2165 mvpp2_prs_tcam_data_byte_set(&pe,
2166 MVPP2_ETH_TYPE_LEN + 2 + 3,
2167 MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
2168 MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
2169 /* Clear all ai bits for next iteration */
2170 mvpp2_prs_sram_ai_update(&pe, 0,
2171 MVPP2_PRS_SRAM_AI_MASK);
2172 /* If packet is tagged continue check vlans */
2173 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2174 } else {
2175 /* Set result info bits to 'no vlans' */
2176 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
2177 MVPP2_PRS_RI_VLAN_MASK);
2178 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2179 }
2180 /* Mask/unmask all ports, depending on dsa type */
2181 mvpp2_prs_tcam_port_map_set(&pe, port_mask);
2182 }
2183
2184 /* Update port mask */
2185 mvpp2_prs_tcam_port_set(&pe, port, add);
2186
2187 mvpp2_prs_hw_write(priv, &pe);
2188}
2189
2190/* Search for existing single/triple vlan entry */
2191static struct mvpp2_prs_entry *mvpp2_prs_vlan_find(struct mvpp2 *priv,
2192 unsigned short tpid, int ai)
2193{
2194 struct mvpp2_prs_entry *pe;
2195 int tid;
2196
2197 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2198 if (!pe)
2199 return NULL;
2200 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2201
2202 /* Go through the all entries with MVPP2_PRS_LU_VLAN */
2203 for (tid = MVPP2_PE_FIRST_FREE_TID;
2204 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
2205 unsigned int ri_bits, ai_bits;
2206 bool match;
2207
2208 if (!priv->prs_shadow[tid].valid ||
2209 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
2210 continue;
2211
2212 pe->index = tid;
2213
2214 mvpp2_prs_hw_read(priv, pe);
2215 match = mvpp2_prs_tcam_data_cmp(pe, 0, swab16(tpid));
2216 if (!match)
2217 continue;
2218
2219 /* Get vlan type */
2220 ri_bits = mvpp2_prs_sram_ri_get(pe);
2221 ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
2222
2223 /* Get current ai value from tcam */
2224 ai_bits = mvpp2_prs_tcam_ai_get(pe);
2225 /* Clear double vlan bit */
2226 ai_bits &= ~MVPP2_PRS_DBL_VLAN_AI_BIT;
2227
2228 if (ai != ai_bits)
2229 continue;
2230
2231 if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
2232 ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
2233 return pe;
2234 }
2235 kfree(pe);
2236
2237 return NULL;
2238}
2239
2240/* Add/update single/triple vlan entry */
2241static int mvpp2_prs_vlan_add(struct mvpp2 *priv, unsigned short tpid, int ai,
2242 unsigned int port_map)
2243{
2244 struct mvpp2_prs_entry *pe;
2245 int tid_aux, tid;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302246 int ret = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002247
2248 pe = mvpp2_prs_vlan_find(priv, tpid, ai);
2249
2250 if (!pe) {
2251 /* Create new tcam entry */
2252 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_LAST_FREE_TID,
2253 MVPP2_PE_FIRST_FREE_TID);
2254 if (tid < 0)
2255 return tid;
2256
2257 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2258 if (!pe)
2259 return -ENOMEM;
2260
2261 /* Get last double vlan tid */
2262 for (tid_aux = MVPP2_PE_LAST_FREE_TID;
2263 tid_aux >= MVPP2_PE_FIRST_FREE_TID; tid_aux--) {
2264 unsigned int ri_bits;
2265
2266 if (!priv->prs_shadow[tid_aux].valid ||
2267 priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
2268 continue;
2269
2270 pe->index = tid_aux;
2271 mvpp2_prs_hw_read(priv, pe);
2272 ri_bits = mvpp2_prs_sram_ri_get(pe);
2273 if ((ri_bits & MVPP2_PRS_RI_VLAN_MASK) ==
2274 MVPP2_PRS_RI_VLAN_DOUBLE)
2275 break;
2276 }
2277
Sudip Mukherjee43737472014-11-01 16:59:34 +05302278 if (tid <= tid_aux) {
2279 ret = -EINVAL;
Markus Elfringf9fd0e32017-04-17 13:50:35 +02002280 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302281 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002282
Markus Elfringbd6aaf52017-04-17 10:40:32 +02002283 memset(pe, 0, sizeof(*pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002284 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2285 pe->index = tid;
2286
2287 mvpp2_prs_match_etype(pe, 0, tpid);
2288
Maxime Chevallier56beda32018-02-28 10:14:13 +01002289 /* VLAN tag detected, proceed with VID filtering */
2290 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_VID);
2291
Marcin Wojtas3f518502014-07-10 16:52:13 -03002292 /* Clear all ai bits for next iteration */
2293 mvpp2_prs_sram_ai_update(pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2294
2295 if (ai == MVPP2_PRS_SINGLE_VLAN_AI) {
2296 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_SINGLE,
2297 MVPP2_PRS_RI_VLAN_MASK);
2298 } else {
2299 ai |= MVPP2_PRS_DBL_VLAN_AI_BIT;
2300 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_TRIPLE,
2301 MVPP2_PRS_RI_VLAN_MASK);
2302 }
2303 mvpp2_prs_tcam_ai_update(pe, ai, MVPP2_PRS_SRAM_AI_MASK);
2304
2305 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_VLAN);
2306 }
2307 /* Update ports' mask */
2308 mvpp2_prs_tcam_port_map_set(pe, port_map);
2309
2310 mvpp2_prs_hw_write(priv, pe);
Markus Elfringf9fd0e32017-04-17 13:50:35 +02002311free_pe:
Marcin Wojtas3f518502014-07-10 16:52:13 -03002312 kfree(pe);
2313
Sudip Mukherjee43737472014-11-01 16:59:34 +05302314 return ret;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002315}
2316
2317/* Get first free double vlan ai number */
2318static int mvpp2_prs_double_vlan_ai_free_get(struct mvpp2 *priv)
2319{
2320 int i;
2321
2322 for (i = 1; i < MVPP2_PRS_DBL_VLANS_MAX; i++) {
2323 if (!priv->prs_double_vlans[i])
2324 return i;
2325 }
2326
2327 return -EINVAL;
2328}
2329
2330/* Search for existing double vlan entry */
2331static struct mvpp2_prs_entry *mvpp2_prs_double_vlan_find(struct mvpp2 *priv,
2332 unsigned short tpid1,
2333 unsigned short tpid2)
2334{
2335 struct mvpp2_prs_entry *pe;
2336 int tid;
2337
2338 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2339 if (!pe)
2340 return NULL;
2341 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2342
2343 /* Go through the all entries with MVPP2_PRS_LU_VLAN */
2344 for (tid = MVPP2_PE_FIRST_FREE_TID;
2345 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
2346 unsigned int ri_mask;
2347 bool match;
2348
2349 if (!priv->prs_shadow[tid].valid ||
2350 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
2351 continue;
2352
2353 pe->index = tid;
2354 mvpp2_prs_hw_read(priv, pe);
2355
2356 match = mvpp2_prs_tcam_data_cmp(pe, 0, swab16(tpid1))
2357 && mvpp2_prs_tcam_data_cmp(pe, 4, swab16(tpid2));
2358
2359 if (!match)
2360 continue;
2361
2362 ri_mask = mvpp2_prs_sram_ri_get(pe) & MVPP2_PRS_RI_VLAN_MASK;
2363 if (ri_mask == MVPP2_PRS_RI_VLAN_DOUBLE)
2364 return pe;
2365 }
2366 kfree(pe);
2367
2368 return NULL;
2369}
2370
2371/* Add or update double vlan entry */
2372static int mvpp2_prs_double_vlan_add(struct mvpp2 *priv, unsigned short tpid1,
2373 unsigned short tpid2,
2374 unsigned int port_map)
2375{
2376 struct mvpp2_prs_entry *pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302377 int tid_aux, tid, ai, ret = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002378
2379 pe = mvpp2_prs_double_vlan_find(priv, tpid1, tpid2);
2380
2381 if (!pe) {
2382 /* Create new tcam entry */
2383 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2384 MVPP2_PE_LAST_FREE_TID);
2385 if (tid < 0)
2386 return tid;
2387
2388 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2389 if (!pe)
2390 return -ENOMEM;
2391
2392 /* Set ai value for new double vlan entry */
2393 ai = mvpp2_prs_double_vlan_ai_free_get(priv);
Sudip Mukherjee43737472014-11-01 16:59:34 +05302394 if (ai < 0) {
2395 ret = ai;
Markus Elfringc9a7e122017-04-17 13:03:49 +02002396 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302397 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002398
2399 /* Get first single/triple vlan tid */
2400 for (tid_aux = MVPP2_PE_FIRST_FREE_TID;
2401 tid_aux <= MVPP2_PE_LAST_FREE_TID; tid_aux++) {
2402 unsigned int ri_bits;
2403
2404 if (!priv->prs_shadow[tid_aux].valid ||
2405 priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
2406 continue;
2407
2408 pe->index = tid_aux;
2409 mvpp2_prs_hw_read(priv, pe);
2410 ri_bits = mvpp2_prs_sram_ri_get(pe);
2411 ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
2412 if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
2413 ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
2414 break;
2415 }
2416
Sudip Mukherjee43737472014-11-01 16:59:34 +05302417 if (tid >= tid_aux) {
2418 ret = -ERANGE;
Markus Elfringc9a7e122017-04-17 13:03:49 +02002419 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302420 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002421
Markus Elfringbd6aaf52017-04-17 10:40:32 +02002422 memset(pe, 0, sizeof(*pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002423 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2424 pe->index = tid;
2425
2426 priv->prs_double_vlans[ai] = true;
2427
2428 mvpp2_prs_match_etype(pe, 0, tpid1);
2429 mvpp2_prs_match_etype(pe, 4, tpid2);
2430
2431 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_VLAN);
Maxime Chevallier56beda32018-02-28 10:14:13 +01002432 /* Shift 4 bytes - skip outer vlan tag */
2433 mvpp2_prs_sram_shift_set(pe, MVPP2_VLAN_TAG_LEN,
Marcin Wojtas3f518502014-07-10 16:52:13 -03002434 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2435 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_DOUBLE,
2436 MVPP2_PRS_RI_VLAN_MASK);
2437 mvpp2_prs_sram_ai_update(pe, ai | MVPP2_PRS_DBL_VLAN_AI_BIT,
2438 MVPP2_PRS_SRAM_AI_MASK);
2439
2440 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_VLAN);
2441 }
2442
2443 /* Update ports' mask */
2444 mvpp2_prs_tcam_port_map_set(pe, port_map);
2445 mvpp2_prs_hw_write(priv, pe);
Markus Elfringc9a7e122017-04-17 13:03:49 +02002446free_pe:
Marcin Wojtas3f518502014-07-10 16:52:13 -03002447 kfree(pe);
Sudip Mukherjee43737472014-11-01 16:59:34 +05302448 return ret;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002449}
2450
2451/* IPv4 header parsing for fragmentation and L4 offset */
2452static int mvpp2_prs_ip4_proto(struct mvpp2 *priv, unsigned short proto,
2453 unsigned int ri, unsigned int ri_mask)
2454{
2455 struct mvpp2_prs_entry pe;
2456 int tid;
2457
2458 if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
2459 (proto != IPPROTO_IGMP))
2460 return -EINVAL;
2461
Stefan Chulskiaff3da32017-09-25 14:59:46 +02002462 /* Not fragmented packet */
Marcin Wojtas3f518502014-07-10 16:52:13 -03002463 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2464 MVPP2_PE_LAST_FREE_TID);
2465 if (tid < 0)
2466 return tid;
2467
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002468 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002469 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
2470 pe.index = tid;
2471
2472 /* Set next lu to IPv4 */
2473 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2474 mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2475 /* Set L4 offset */
2476 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
2477 sizeof(struct iphdr) - 4,
2478 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2479 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
2480 MVPP2_PRS_IPV4_DIP_AI_BIT);
Stefan Chulskiaff3da32017-09-25 14:59:46 +02002481 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask | MVPP2_PRS_RI_IP_FRAG_MASK);
2482
2483 mvpp2_prs_tcam_data_byte_set(&pe, 2, 0x00,
2484 MVPP2_PRS_TCAM_PROTO_MASK_L);
2485 mvpp2_prs_tcam_data_byte_set(&pe, 3, 0x00,
2486 MVPP2_PRS_TCAM_PROTO_MASK);
Marcin Wojtas3f518502014-07-10 16:52:13 -03002487
2488 mvpp2_prs_tcam_data_byte_set(&pe, 5, proto, MVPP2_PRS_TCAM_PROTO_MASK);
2489 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
2490 /* Unmask all ports */
2491 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2492
2493 /* Update shadow table and hw entry */
2494 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2495 mvpp2_prs_hw_write(priv, &pe);
2496
Stefan Chulskiaff3da32017-09-25 14:59:46 +02002497 /* Fragmented packet */
Marcin Wojtas3f518502014-07-10 16:52:13 -03002498 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2499 MVPP2_PE_LAST_FREE_TID);
2500 if (tid < 0)
2501 return tid;
2502
2503 pe.index = tid;
2504 /* Clear ri before updating */
2505 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2506 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2507 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
2508
Stefan Chulskiaff3da32017-09-25 14:59:46 +02002509 mvpp2_prs_sram_ri_update(&pe, ri | MVPP2_PRS_RI_IP_FRAG_TRUE,
2510 ri_mask | MVPP2_PRS_RI_IP_FRAG_MASK);
2511
2512 mvpp2_prs_tcam_data_byte_set(&pe, 2, 0x00, 0x0);
2513 mvpp2_prs_tcam_data_byte_set(&pe, 3, 0x00, 0x0);
Marcin Wojtas3f518502014-07-10 16:52:13 -03002514
2515 /* Update shadow table and hw entry */
2516 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2517 mvpp2_prs_hw_write(priv, &pe);
2518
2519 return 0;
2520}
2521
2522/* IPv4 L3 multicast or broadcast */
2523static int mvpp2_prs_ip4_cast(struct mvpp2 *priv, unsigned short l3_cast)
2524{
2525 struct mvpp2_prs_entry pe;
2526 int mask, tid;
2527
2528 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2529 MVPP2_PE_LAST_FREE_TID);
2530 if (tid < 0)
2531 return tid;
2532
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002533 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002534 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
2535 pe.index = tid;
2536
2537 switch (l3_cast) {
2538 case MVPP2_PRS_L3_MULTI_CAST:
2539 mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV4_MC,
2540 MVPP2_PRS_IPV4_MC_MASK);
2541 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
2542 MVPP2_PRS_RI_L3_ADDR_MASK);
2543 break;
2544 case MVPP2_PRS_L3_BROAD_CAST:
2545 mask = MVPP2_PRS_IPV4_BC_MASK;
2546 mvpp2_prs_tcam_data_byte_set(&pe, 0, mask, mask);
2547 mvpp2_prs_tcam_data_byte_set(&pe, 1, mask, mask);
2548 mvpp2_prs_tcam_data_byte_set(&pe, 2, mask, mask);
2549 mvpp2_prs_tcam_data_byte_set(&pe, 3, mask, mask);
2550 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_BCAST,
2551 MVPP2_PRS_RI_L3_ADDR_MASK);
2552 break;
2553 default:
2554 return -EINVAL;
2555 }
2556
2557 /* Finished: go to flowid generation */
2558 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2559 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2560
2561 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
2562 MVPP2_PRS_IPV4_DIP_AI_BIT);
2563 /* Unmask all ports */
2564 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2565
2566 /* Update shadow table and hw entry */
2567 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2568 mvpp2_prs_hw_write(priv, &pe);
2569
2570 return 0;
2571}
2572
2573/* Set entries for protocols over IPv6 */
2574static int mvpp2_prs_ip6_proto(struct mvpp2 *priv, unsigned short proto,
2575 unsigned int ri, unsigned int ri_mask)
2576{
2577 struct mvpp2_prs_entry pe;
2578 int tid;
2579
2580 if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
2581 (proto != IPPROTO_ICMPV6) && (proto != IPPROTO_IPIP))
2582 return -EINVAL;
2583
2584 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2585 MVPP2_PE_LAST_FREE_TID);
2586 if (tid < 0)
2587 return tid;
2588
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002589 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002590 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2591 pe.index = tid;
2592
2593 /* Finished: go to flowid generation */
2594 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2595 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2596 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
2597 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
2598 sizeof(struct ipv6hdr) - 6,
2599 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2600
2601 mvpp2_prs_tcam_data_byte_set(&pe, 0, proto, MVPP2_PRS_TCAM_PROTO_MASK);
2602 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
2603 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2604 /* Unmask all ports */
2605 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2606
2607 /* Write HW */
2608 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
2609 mvpp2_prs_hw_write(priv, &pe);
2610
2611 return 0;
2612}
2613
2614/* IPv6 L3 multicast entry */
2615static int mvpp2_prs_ip6_cast(struct mvpp2 *priv, unsigned short l3_cast)
2616{
2617 struct mvpp2_prs_entry pe;
2618 int tid;
2619
2620 if (l3_cast != MVPP2_PRS_L3_MULTI_CAST)
2621 return -EINVAL;
2622
2623 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2624 MVPP2_PE_LAST_FREE_TID);
2625 if (tid < 0)
2626 return tid;
2627
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002628 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002629 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2630 pe.index = tid;
2631
2632 /* Finished: go to flowid generation */
2633 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2634 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
2635 MVPP2_PRS_RI_L3_ADDR_MASK);
2636 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
2637 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2638 /* Shift back to IPv6 NH */
2639 mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2640
2641 mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV6_MC,
2642 MVPP2_PRS_IPV6_MC_MASK);
2643 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2644 /* Unmask all ports */
2645 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2646
2647 /* Update shadow table and hw entry */
2648 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
2649 mvpp2_prs_hw_write(priv, &pe);
2650
2651 return 0;
2652}
2653
2654/* Parser per-port initialization */
2655static void mvpp2_prs_hw_port_init(struct mvpp2 *priv, int port, int lu_first,
2656 int lu_max, int offset)
2657{
2658 u32 val;
2659
2660 /* Set lookup ID */
2661 val = mvpp2_read(priv, MVPP2_PRS_INIT_LOOKUP_REG);
2662 val &= ~MVPP2_PRS_PORT_LU_MASK(port);
2663 val |= MVPP2_PRS_PORT_LU_VAL(port, lu_first);
2664 mvpp2_write(priv, MVPP2_PRS_INIT_LOOKUP_REG, val);
2665
2666 /* Set maximum number of loops for packet received from port */
2667 val = mvpp2_read(priv, MVPP2_PRS_MAX_LOOP_REG(port));
2668 val &= ~MVPP2_PRS_MAX_LOOP_MASK(port);
2669 val |= MVPP2_PRS_MAX_LOOP_VAL(port, lu_max);
2670 mvpp2_write(priv, MVPP2_PRS_MAX_LOOP_REG(port), val);
2671
2672 /* Set initial offset for packet header extraction for the first
2673 * searching loop
2674 */
2675 val = mvpp2_read(priv, MVPP2_PRS_INIT_OFFS_REG(port));
2676 val &= ~MVPP2_PRS_INIT_OFF_MASK(port);
2677 val |= MVPP2_PRS_INIT_OFF_VAL(port, offset);
2678 mvpp2_write(priv, MVPP2_PRS_INIT_OFFS_REG(port), val);
2679}
2680
2681/* Default flow entries initialization for all ports */
2682static void mvpp2_prs_def_flow_init(struct mvpp2 *priv)
2683{
2684 struct mvpp2_prs_entry pe;
2685 int port;
2686
2687 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002688 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002689 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2690 pe.index = MVPP2_PE_FIRST_DEFAULT_FLOW - port;
2691
2692 /* Mask all ports */
2693 mvpp2_prs_tcam_port_map_set(&pe, 0);
2694
2695 /* Set flow ID*/
2696 mvpp2_prs_sram_ai_update(&pe, port, MVPP2_PRS_FLOW_ID_MASK);
2697 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
2698
2699 /* Update shadow table and hw entry */
2700 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_FLOWS);
2701 mvpp2_prs_hw_write(priv, &pe);
2702 }
2703}
2704
2705/* Set default entry for Marvell Header field */
2706static void mvpp2_prs_mh_init(struct mvpp2 *priv)
2707{
2708 struct mvpp2_prs_entry pe;
2709
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002710 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002711
2712 pe.index = MVPP2_PE_MH_DEFAULT;
2713 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MH);
2714 mvpp2_prs_sram_shift_set(&pe, MVPP2_MH_SIZE,
2715 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2716 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_MAC);
2717
2718 /* Unmask all ports */
2719 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2720
2721 /* Update shadow table and hw entry */
2722 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MH);
2723 mvpp2_prs_hw_write(priv, &pe);
2724}
2725
2726/* Set default entires (place holder) for promiscuous, non-promiscuous and
2727 * multicast MAC addresses
2728 */
2729static void mvpp2_prs_mac_init(struct mvpp2 *priv)
2730{
2731 struct mvpp2_prs_entry pe;
2732
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002733 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002734
2735 /* Non-promiscuous mode for all ports - DROP unknown packets */
2736 pe.index = MVPP2_PE_MAC_NON_PROMISCUOUS;
2737 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
2738
2739 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
2740 MVPP2_PRS_RI_DROP_MASK);
2741 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2742 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2743
2744 /* Unmask all ports */
2745 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2746
2747 /* Update shadow table and hw entry */
2748 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2749 mvpp2_prs_hw_write(priv, &pe);
2750
2751 /* place holders only - no ports */
2752 mvpp2_prs_mac_drop_all_set(priv, 0, false);
2753 mvpp2_prs_mac_promisc_set(priv, 0, false);
Antoine Tenart20746d72017-10-24 11:41:27 +02002754 mvpp2_prs_mac_multi_set(priv, 0, MVPP2_PE_MAC_MC_ALL, false);
2755 mvpp2_prs_mac_multi_set(priv, 0, MVPP2_PE_MAC_MC_IP6, false);
Marcin Wojtas3f518502014-07-10 16:52:13 -03002756}
2757
2758/* Set default entries for various types of dsa packets */
2759static void mvpp2_prs_dsa_init(struct mvpp2 *priv)
2760{
2761 struct mvpp2_prs_entry pe;
2762
2763 /* None tagged EDSA entry - place holder */
2764 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
2765 MVPP2_PRS_EDSA);
2766
2767 /* Tagged EDSA entry - place holder */
2768 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
2769
2770 /* None tagged DSA entry - place holder */
2771 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
2772 MVPP2_PRS_DSA);
2773
2774 /* Tagged DSA entry - place holder */
2775 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
2776
2777 /* None tagged EDSA ethertype entry - place holder*/
2778 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
2779 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
2780
2781 /* Tagged EDSA ethertype entry - place holder*/
2782 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
2783 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
2784
2785 /* None tagged DSA ethertype entry */
2786 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
2787 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
2788
2789 /* Tagged DSA ethertype entry */
2790 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
2791 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
2792
2793 /* Set default entry, in case DSA or EDSA tag not found */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002794 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002795 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
2796 pe.index = MVPP2_PE_DSA_DEFAULT;
2797 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2798
2799 /* Shift 0 bytes */
2800 mvpp2_prs_sram_shift_set(&pe, 0, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2801 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2802
2803 /* Clear all sram ai bits for next iteration */
2804 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2805
2806 /* Unmask all ports */
2807 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2808
2809 mvpp2_prs_hw_write(priv, &pe);
2810}
2811
Maxime Chevallier56beda32018-02-28 10:14:13 +01002812/* Initialize parser entries for VID filtering */
2813static void mvpp2_prs_vid_init(struct mvpp2 *priv)
2814{
2815 struct mvpp2_prs_entry pe;
2816
2817 memset(&pe, 0, sizeof(pe));
2818
2819 /* Set default vid entry */
2820 pe.index = MVPP2_PE_VID_FLTR_DEFAULT;
2821 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VID);
2822
2823 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_EDSA_VID_AI_BIT);
2824
2825 /* Skip VLAN header - Set offset to 4 bytes */
2826 mvpp2_prs_sram_shift_set(&pe, MVPP2_VLAN_TAG_LEN,
2827 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2828
2829 /* Clear all ai bits for next iteration */
2830 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2831
2832 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2833
2834 /* Unmask all ports */
2835 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2836
2837 /* Update shadow table and hw entry */
2838 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VID);
2839 mvpp2_prs_hw_write(priv, &pe);
2840
2841 /* Set default vid entry for extended DSA*/
2842 memset(&pe, 0, sizeof(pe));
2843
2844 /* Set default vid entry */
2845 pe.index = MVPP2_PE_VID_EDSA_FLTR_DEFAULT;
2846 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VID);
2847
2848 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_EDSA_VID_AI_BIT,
2849 MVPP2_PRS_EDSA_VID_AI_BIT);
2850
2851 /* Skip VLAN header - Set offset to 8 bytes */
2852 mvpp2_prs_sram_shift_set(&pe, MVPP2_VLAN_TAG_EDSA_LEN,
2853 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2854
2855 /* Clear all ai bits for next iteration */
2856 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2857
2858 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2859
2860 /* Unmask all ports */
2861 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2862
2863 /* Update shadow table and hw entry */
2864 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VID);
2865 mvpp2_prs_hw_write(priv, &pe);
2866}
2867
Marcin Wojtas3f518502014-07-10 16:52:13 -03002868/* Match basic ethertypes */
2869static int mvpp2_prs_etype_init(struct mvpp2 *priv)
2870{
2871 struct mvpp2_prs_entry pe;
2872 int tid;
2873
2874 /* Ethertype: PPPoE */
2875 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2876 MVPP2_PE_LAST_FREE_TID);
2877 if (tid < 0)
2878 return tid;
2879
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002880 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002881 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2882 pe.index = tid;
2883
2884 mvpp2_prs_match_etype(&pe, 0, ETH_P_PPP_SES);
2885
2886 mvpp2_prs_sram_shift_set(&pe, MVPP2_PPPOE_HDR_SIZE,
2887 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2888 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2889 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_PPPOE_MASK,
2890 MVPP2_PRS_RI_PPPOE_MASK);
2891
2892 /* Update shadow table and hw entry */
2893 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2894 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2895 priv->prs_shadow[pe.index].finish = false;
2896 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_PPPOE_MASK,
2897 MVPP2_PRS_RI_PPPOE_MASK);
2898 mvpp2_prs_hw_write(priv, &pe);
2899
2900 /* Ethertype: ARP */
2901 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2902 MVPP2_PE_LAST_FREE_TID);
2903 if (tid < 0)
2904 return tid;
2905
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002906 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002907 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2908 pe.index = tid;
2909
2910 mvpp2_prs_match_etype(&pe, 0, ETH_P_ARP);
2911
2912 /* Generate flow in the next iteration*/
2913 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2914 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2915 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_ARP,
2916 MVPP2_PRS_RI_L3_PROTO_MASK);
2917 /* Set L3 offset */
2918 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2919 MVPP2_ETH_TYPE_LEN,
2920 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2921
2922 /* Update shadow table and hw entry */
2923 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2924 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2925 priv->prs_shadow[pe.index].finish = true;
2926 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_ARP,
2927 MVPP2_PRS_RI_L3_PROTO_MASK);
2928 mvpp2_prs_hw_write(priv, &pe);
2929
2930 /* Ethertype: LBTD */
2931 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2932 MVPP2_PE_LAST_FREE_TID);
2933 if (tid < 0)
2934 return tid;
2935
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002936 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002937 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2938 pe.index = tid;
2939
2940 mvpp2_prs_match_etype(&pe, 0, MVPP2_IP_LBDT_TYPE);
2941
2942 /* Generate flow in the next iteration*/
2943 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2944 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2945 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2946 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2947 MVPP2_PRS_RI_CPU_CODE_MASK |
2948 MVPP2_PRS_RI_UDF3_MASK);
2949 /* Set L3 offset */
2950 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2951 MVPP2_ETH_TYPE_LEN,
2952 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2953
2954 /* Update shadow table and hw entry */
2955 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2956 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2957 priv->prs_shadow[pe.index].finish = true;
2958 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2959 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2960 MVPP2_PRS_RI_CPU_CODE_MASK |
2961 MVPP2_PRS_RI_UDF3_MASK);
2962 mvpp2_prs_hw_write(priv, &pe);
2963
2964 /* Ethertype: IPv4 without options */
2965 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2966 MVPP2_PE_LAST_FREE_TID);
2967 if (tid < 0)
2968 return tid;
2969
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002970 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002971 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2972 pe.index = tid;
2973
2974 mvpp2_prs_match_etype(&pe, 0, ETH_P_IP);
2975 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2976 MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
2977 MVPP2_PRS_IPV4_HEAD_MASK |
2978 MVPP2_PRS_IPV4_IHL_MASK);
2979
2980 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2981 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
2982 MVPP2_PRS_RI_L3_PROTO_MASK);
2983 /* Skip eth_type + 4 bytes of IP header */
2984 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
2985 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2986 /* Set L3 offset */
2987 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2988 MVPP2_ETH_TYPE_LEN,
2989 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2990
2991 /* Update shadow table and hw entry */
2992 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2993 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2994 priv->prs_shadow[pe.index].finish = false;
2995 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4,
2996 MVPP2_PRS_RI_L3_PROTO_MASK);
2997 mvpp2_prs_hw_write(priv, &pe);
2998
2999 /* Ethertype: IPv4 with options */
3000 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3001 MVPP2_PE_LAST_FREE_TID);
3002 if (tid < 0)
3003 return tid;
3004
3005 pe.index = tid;
3006
3007 /* Clear tcam data before updating */
3008 pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(MVPP2_ETH_TYPE_LEN)] = 0x0;
3009 pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(MVPP2_ETH_TYPE_LEN)] = 0x0;
3010
3011 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
3012 MVPP2_PRS_IPV4_HEAD,
3013 MVPP2_PRS_IPV4_HEAD_MASK);
3014
3015 /* Clear ri before updating */
3016 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
3017 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
3018 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
3019 MVPP2_PRS_RI_L3_PROTO_MASK);
3020
3021 /* Update shadow table and hw entry */
3022 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
3023 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
3024 priv->prs_shadow[pe.index].finish = false;
3025 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4_OPT,
3026 MVPP2_PRS_RI_L3_PROTO_MASK);
3027 mvpp2_prs_hw_write(priv, &pe);
3028
3029 /* Ethertype: IPv6 without options */
3030 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3031 MVPP2_PE_LAST_FREE_TID);
3032 if (tid < 0)
3033 return tid;
3034
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003035 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003036 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
3037 pe.index = tid;
3038
3039 mvpp2_prs_match_etype(&pe, 0, ETH_P_IPV6);
3040
3041 /* Skip DIP of IPV6 header */
3042 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 8 +
3043 MVPP2_MAX_L3_ADDR_SIZE,
3044 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3045 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
3046 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
3047 MVPP2_PRS_RI_L3_PROTO_MASK);
3048 /* Set L3 offset */
3049 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
3050 MVPP2_ETH_TYPE_LEN,
3051 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3052
3053 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
3054 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
3055 priv->prs_shadow[pe.index].finish = false;
3056 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP6,
3057 MVPP2_PRS_RI_L3_PROTO_MASK);
3058 mvpp2_prs_hw_write(priv, &pe);
3059
3060 /* Default entry for MVPP2_PRS_LU_L2 - Unknown ethtype */
3061 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3062 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
3063 pe.index = MVPP2_PE_ETH_TYPE_UN;
3064
3065 /* Unmask all ports */
3066 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3067
3068 /* Generate flow in the next iteration*/
3069 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3070 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3071 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
3072 MVPP2_PRS_RI_L3_PROTO_MASK);
3073 /* Set L3 offset even it's unknown L3 */
3074 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
3075 MVPP2_ETH_TYPE_LEN,
3076 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3077
3078 /* Update shadow table and hw entry */
3079 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
3080 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
3081 priv->prs_shadow[pe.index].finish = true;
3082 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_UN,
3083 MVPP2_PRS_RI_L3_PROTO_MASK);
3084 mvpp2_prs_hw_write(priv, &pe);
3085
3086 return 0;
3087}
3088
3089/* Configure vlan entries and detect up to 2 successive VLAN tags.
3090 * Possible options:
3091 * 0x8100, 0x88A8
3092 * 0x8100, 0x8100
3093 * 0x8100
3094 * 0x88A8
3095 */
3096static int mvpp2_prs_vlan_init(struct platform_device *pdev, struct mvpp2 *priv)
3097{
3098 struct mvpp2_prs_entry pe;
3099 int err;
3100
3101 priv->prs_double_vlans = devm_kcalloc(&pdev->dev, sizeof(bool),
3102 MVPP2_PRS_DBL_VLANS_MAX,
3103 GFP_KERNEL);
3104 if (!priv->prs_double_vlans)
3105 return -ENOMEM;
3106
3107 /* Double VLAN: 0x8100, 0x88A8 */
3108 err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021AD,
3109 MVPP2_PRS_PORT_MASK);
3110 if (err)
3111 return err;
3112
3113 /* Double VLAN: 0x8100, 0x8100 */
3114 err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021Q,
3115 MVPP2_PRS_PORT_MASK);
3116 if (err)
3117 return err;
3118
3119 /* Single VLAN: 0x88a8 */
3120 err = mvpp2_prs_vlan_add(priv, ETH_P_8021AD, MVPP2_PRS_SINGLE_VLAN_AI,
3121 MVPP2_PRS_PORT_MASK);
3122 if (err)
3123 return err;
3124
3125 /* Single VLAN: 0x8100 */
3126 err = mvpp2_prs_vlan_add(priv, ETH_P_8021Q, MVPP2_PRS_SINGLE_VLAN_AI,
3127 MVPP2_PRS_PORT_MASK);
3128 if (err)
3129 return err;
3130
3131 /* Set default double vlan entry */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003132 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003133 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
3134 pe.index = MVPP2_PE_VLAN_DBL;
3135
Maxime Chevallier56beda32018-02-28 10:14:13 +01003136 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VID);
3137
Marcin Wojtas3f518502014-07-10 16:52:13 -03003138 /* Clear ai for next iterations */
3139 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
3140 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_DOUBLE,
3141 MVPP2_PRS_RI_VLAN_MASK);
3142
3143 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_DBL_VLAN_AI_BIT,
3144 MVPP2_PRS_DBL_VLAN_AI_BIT);
3145 /* Unmask all ports */
3146 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3147
3148 /* Update shadow table and hw entry */
3149 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
3150 mvpp2_prs_hw_write(priv, &pe);
3151
3152 /* Set default vlan none entry */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003153 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003154 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
3155 pe.index = MVPP2_PE_VLAN_NONE;
3156
3157 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
3158 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
3159 MVPP2_PRS_RI_VLAN_MASK);
3160
3161 /* Unmask all ports */
3162 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3163
3164 /* Update shadow table and hw entry */
3165 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
3166 mvpp2_prs_hw_write(priv, &pe);
3167
3168 return 0;
3169}
3170
3171/* Set entries for PPPoE ethertype */
3172static int mvpp2_prs_pppoe_init(struct mvpp2 *priv)
3173{
3174 struct mvpp2_prs_entry pe;
3175 int tid;
3176
3177 /* IPv4 over PPPoE with options */
3178 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3179 MVPP2_PE_LAST_FREE_TID);
3180 if (tid < 0)
3181 return tid;
3182
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003183 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003184 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
3185 pe.index = tid;
3186
3187 mvpp2_prs_match_etype(&pe, 0, PPP_IP);
3188
3189 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
3190 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
3191 MVPP2_PRS_RI_L3_PROTO_MASK);
3192 /* Skip eth_type + 4 bytes of IP header */
3193 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
3194 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3195 /* Set L3 offset */
3196 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
3197 MVPP2_ETH_TYPE_LEN,
3198 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3199
3200 /* Update shadow table and hw entry */
3201 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3202 mvpp2_prs_hw_write(priv, &pe);
3203
3204 /* IPv4 over PPPoE without options */
3205 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3206 MVPP2_PE_LAST_FREE_TID);
3207 if (tid < 0)
3208 return tid;
3209
3210 pe.index = tid;
3211
3212 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
3213 MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
3214 MVPP2_PRS_IPV4_HEAD_MASK |
3215 MVPP2_PRS_IPV4_IHL_MASK);
3216
3217 /* Clear ri before updating */
3218 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
3219 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
3220 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
3221 MVPP2_PRS_RI_L3_PROTO_MASK);
3222
3223 /* Update shadow table and hw entry */
3224 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3225 mvpp2_prs_hw_write(priv, &pe);
3226
3227 /* IPv6 over PPPoE */
3228 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3229 MVPP2_PE_LAST_FREE_TID);
3230 if (tid < 0)
3231 return tid;
3232
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003233 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003234 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
3235 pe.index = tid;
3236
3237 mvpp2_prs_match_etype(&pe, 0, PPP_IPV6);
3238
3239 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
3240 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
3241 MVPP2_PRS_RI_L3_PROTO_MASK);
3242 /* Skip eth_type + 4 bytes of IPv6 header */
3243 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
3244 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3245 /* Set L3 offset */
3246 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
3247 MVPP2_ETH_TYPE_LEN,
3248 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3249
3250 /* Update shadow table and hw entry */
3251 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3252 mvpp2_prs_hw_write(priv, &pe);
3253
3254 /* Non-IP over PPPoE */
3255 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3256 MVPP2_PE_LAST_FREE_TID);
3257 if (tid < 0)
3258 return tid;
3259
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003260 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003261 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
3262 pe.index = tid;
3263
3264 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
3265 MVPP2_PRS_RI_L3_PROTO_MASK);
3266
3267 /* Finished: go to flowid generation */
3268 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3269 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3270 /* Set L3 offset even if it's unknown L3 */
3271 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
3272 MVPP2_ETH_TYPE_LEN,
3273 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3274
3275 /* Update shadow table and hw entry */
3276 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3277 mvpp2_prs_hw_write(priv, &pe);
3278
3279 return 0;
3280}
3281
3282/* Initialize entries for IPv4 */
3283static int mvpp2_prs_ip4_init(struct mvpp2 *priv)
3284{
3285 struct mvpp2_prs_entry pe;
3286 int err;
3287
3288 /* Set entries for TCP, UDP and IGMP over IPv4 */
3289 err = mvpp2_prs_ip4_proto(priv, IPPROTO_TCP, MVPP2_PRS_RI_L4_TCP,
3290 MVPP2_PRS_RI_L4_PROTO_MASK);
3291 if (err)
3292 return err;
3293
3294 err = mvpp2_prs_ip4_proto(priv, IPPROTO_UDP, MVPP2_PRS_RI_L4_UDP,
3295 MVPP2_PRS_RI_L4_PROTO_MASK);
3296 if (err)
3297 return err;
3298
3299 err = mvpp2_prs_ip4_proto(priv, IPPROTO_IGMP,
3300 MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
3301 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
3302 MVPP2_PRS_RI_CPU_CODE_MASK |
3303 MVPP2_PRS_RI_UDF3_MASK);
3304 if (err)
3305 return err;
3306
3307 /* IPv4 Broadcast */
3308 err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_BROAD_CAST);
3309 if (err)
3310 return err;
3311
3312 /* IPv4 Multicast */
3313 err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
3314 if (err)
3315 return err;
3316
3317 /* Default IPv4 entry for unknown protocols */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003318 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003319 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
3320 pe.index = MVPP2_PE_IP4_PROTO_UN;
3321
3322 /* Set next lu to IPv4 */
3323 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
3324 mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3325 /* Set L4 offset */
3326 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
3327 sizeof(struct iphdr) - 4,
3328 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3329 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
3330 MVPP2_PRS_IPV4_DIP_AI_BIT);
3331 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3332 MVPP2_PRS_RI_L4_PROTO_MASK);
3333
3334 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
3335 /* Unmask all ports */
3336 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3337
3338 /* Update shadow table and hw entry */
3339 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3340 mvpp2_prs_hw_write(priv, &pe);
3341
3342 /* Default IPv4 entry for unicast address */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003343 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003344 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
3345 pe.index = MVPP2_PE_IP4_ADDR_UN;
3346
3347 /* Finished: go to flowid generation */
3348 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3349 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3350 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
3351 MVPP2_PRS_RI_L3_ADDR_MASK);
3352
3353 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
3354 MVPP2_PRS_IPV4_DIP_AI_BIT);
3355 /* Unmask all ports */
3356 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3357
3358 /* Update shadow table and hw entry */
3359 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3360 mvpp2_prs_hw_write(priv, &pe);
3361
3362 return 0;
3363}
3364
3365/* Initialize entries for IPv6 */
3366static int mvpp2_prs_ip6_init(struct mvpp2 *priv)
3367{
3368 struct mvpp2_prs_entry pe;
3369 int tid, err;
3370
3371 /* Set entries for TCP, UDP and ICMP over IPv6 */
3372 err = mvpp2_prs_ip6_proto(priv, IPPROTO_TCP,
3373 MVPP2_PRS_RI_L4_TCP,
3374 MVPP2_PRS_RI_L4_PROTO_MASK);
3375 if (err)
3376 return err;
3377
3378 err = mvpp2_prs_ip6_proto(priv, IPPROTO_UDP,
3379 MVPP2_PRS_RI_L4_UDP,
3380 MVPP2_PRS_RI_L4_PROTO_MASK);
3381 if (err)
3382 return err;
3383
3384 err = mvpp2_prs_ip6_proto(priv, IPPROTO_ICMPV6,
3385 MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
3386 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
3387 MVPP2_PRS_RI_CPU_CODE_MASK |
3388 MVPP2_PRS_RI_UDF3_MASK);
3389 if (err)
3390 return err;
3391
3392 /* IPv4 is the last header. This is similar case as 6-TCP or 17-UDP */
3393 /* Result Info: UDF7=1, DS lite */
3394 err = mvpp2_prs_ip6_proto(priv, IPPROTO_IPIP,
3395 MVPP2_PRS_RI_UDF7_IP6_LITE,
3396 MVPP2_PRS_RI_UDF7_MASK);
3397 if (err)
3398 return err;
3399
3400 /* IPv6 multicast */
3401 err = mvpp2_prs_ip6_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
3402 if (err)
3403 return err;
3404
3405 /* Entry for checking hop limit */
3406 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3407 MVPP2_PE_LAST_FREE_TID);
3408 if (tid < 0)
3409 return tid;
3410
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003411 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003412 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3413 pe.index = tid;
3414
3415 /* Finished: go to flowid generation */
3416 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3417 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3418 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN |
3419 MVPP2_PRS_RI_DROP_MASK,
3420 MVPP2_PRS_RI_L3_PROTO_MASK |
3421 MVPP2_PRS_RI_DROP_MASK);
3422
3423 mvpp2_prs_tcam_data_byte_set(&pe, 1, 0x00, MVPP2_PRS_IPV6_HOP_MASK);
3424 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3425 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3426
3427 /* Update shadow table and hw entry */
3428 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3429 mvpp2_prs_hw_write(priv, &pe);
3430
3431 /* Default IPv6 entry for unknown protocols */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003432 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003433 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3434 pe.index = MVPP2_PE_IP6_PROTO_UN;
3435
3436 /* Finished: go to flowid generation */
3437 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3438 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3439 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3440 MVPP2_PRS_RI_L4_PROTO_MASK);
3441 /* Set L4 offset relatively to our current place */
3442 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
3443 sizeof(struct ipv6hdr) - 4,
3444 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3445
3446 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3447 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3448 /* Unmask all ports */
3449 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3450
3451 /* Update shadow table and hw entry */
3452 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3453 mvpp2_prs_hw_write(priv, &pe);
3454
3455 /* Default IPv6 entry for unknown ext protocols */
3456 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3457 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3458 pe.index = MVPP2_PE_IP6_EXT_PROTO_UN;
3459
3460 /* Finished: go to flowid generation */
3461 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3462 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3463 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3464 MVPP2_PRS_RI_L4_PROTO_MASK);
3465
3466 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_EXT_AI_BIT,
3467 MVPP2_PRS_IPV6_EXT_AI_BIT);
3468 /* Unmask all ports */
3469 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3470
3471 /* Update shadow table and hw entry */
3472 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3473 mvpp2_prs_hw_write(priv, &pe);
3474
3475 /* Default IPv6 entry for unicast address */
3476 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3477 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3478 pe.index = MVPP2_PE_IP6_ADDR_UN;
3479
3480 /* Finished: go to IPv6 again */
3481 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
3482 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
3483 MVPP2_PRS_RI_L3_ADDR_MASK);
3484 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3485 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3486 /* Shift back to IPV6 NH */
3487 mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3488
3489 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3490 /* Unmask all ports */
3491 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3492
3493 /* Update shadow table and hw entry */
3494 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
3495 mvpp2_prs_hw_write(priv, &pe);
3496
3497 return 0;
3498}
3499
Maxime Chevallier56beda32018-02-28 10:14:13 +01003500/* Find tcam entry with matched pair <vid,port> */
3501static int mvpp2_prs_vid_range_find(struct mvpp2 *priv, int pmap, u16 vid,
3502 u16 mask)
3503{
3504 unsigned char byte[2], enable[2];
3505 struct mvpp2_prs_entry pe;
3506 u16 rvid, rmask;
3507 int tid;
3508
3509 /* Go through the all entries with MVPP2_PRS_LU_VID */
3510 for (tid = MVPP2_PE_VID_FILT_RANGE_START;
3511 tid <= MVPP2_PE_VID_FILT_RANGE_END; tid++) {
3512 if (!priv->prs_shadow[tid].valid ||
3513 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VID)
3514 continue;
3515
3516 pe.index = tid;
3517
3518 mvpp2_prs_hw_read(priv, &pe);
3519 mvpp2_prs_tcam_data_byte_get(&pe, 2, &byte[0], &enable[0]);
3520 mvpp2_prs_tcam_data_byte_get(&pe, 3, &byte[1], &enable[1]);
3521
3522 rvid = ((byte[0] & 0xf) << 8) + byte[1];
3523 rmask = ((enable[0] & 0xf) << 8) + enable[1];
3524
3525 if (rvid != vid || rmask != mask)
3526 continue;
3527
3528 return tid;
3529 }
3530
3531 return 0;
3532}
3533
3534/* Write parser entry for VID filtering */
3535static int mvpp2_prs_vid_entry_add(struct mvpp2_port *port, u16 vid)
3536{
3537 unsigned int vid_start = MVPP2_PE_VID_FILT_RANGE_START +
3538 port->id * MVPP2_PRS_VLAN_FILT_MAX;
3539 unsigned int mask = 0xfff, reg_val, shift;
3540 struct mvpp2 *priv = port->priv;
3541 struct mvpp2_prs_entry pe;
3542 int tid;
3543
3544 /* Scan TCAM and see if entry with this <vid,port> already exist */
3545 tid = mvpp2_prs_vid_range_find(priv, (1 << port->id), vid, mask);
3546
3547 reg_val = mvpp2_read(priv, MVPP2_MH_REG(port->id));
3548 if (reg_val & MVPP2_DSA_EXTENDED)
3549 shift = MVPP2_VLAN_TAG_EDSA_LEN;
3550 else
3551 shift = MVPP2_VLAN_TAG_LEN;
3552
3553 /* No such entry */
3554 if (!tid) {
3555 memset(&pe, 0, sizeof(pe));
3556
3557 /* Go through all entries from first to last in vlan range */
3558 tid = mvpp2_prs_tcam_first_free(priv, vid_start,
3559 vid_start +
3560 MVPP2_PRS_VLAN_FILT_MAX_ENTRY);
3561
3562 /* There isn't room for a new VID filter */
3563 if (tid < 0)
3564 return tid;
3565
3566 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VID);
3567 pe.index = tid;
3568
3569 /* Mask all ports */
3570 mvpp2_prs_tcam_port_map_set(&pe, 0);
3571 } else {
3572 mvpp2_prs_hw_read(priv, &pe);
3573 }
3574
3575 /* Enable the current port */
3576 mvpp2_prs_tcam_port_set(&pe, port->id, true);
3577
3578 /* Continue - set next lookup */
3579 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
3580
3581 /* Skip VLAN header - Set offset to 4 or 8 bytes */
3582 mvpp2_prs_sram_shift_set(&pe, shift, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3583
3584 /* Set match on VID */
3585 mvpp2_prs_match_vid(&pe, MVPP2_PRS_VID_TCAM_BYTE, vid);
3586
3587 /* Clear all ai bits for next iteration */
3588 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
3589
3590 /* Update shadow table */
3591 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VID);
3592 mvpp2_prs_hw_write(priv, &pe);
3593
3594 return 0;
3595}
3596
3597/* Write parser entry for VID filtering */
3598static void mvpp2_prs_vid_entry_remove(struct mvpp2_port *port, u16 vid)
3599{
3600 struct mvpp2 *priv = port->priv;
3601 int tid;
3602
3603 /* Scan TCAM and see if entry with this <vid,port> already exist */
3604 tid = mvpp2_prs_vid_range_find(priv, (1 << port->id), vid, 0xfff);
3605
3606 /* No such entry */
3607 if (tid)
3608 return;
3609
3610 mvpp2_prs_hw_inv(priv, tid);
3611 priv->prs_shadow[tid].valid = false;
3612}
3613
3614/* Remove all existing VID filters on this port */
3615static void mvpp2_prs_vid_remove_all(struct mvpp2_port *port)
3616{
3617 struct mvpp2 *priv = port->priv;
3618 int tid;
3619
3620 for (tid = MVPP2_PRS_VID_PORT_FIRST(port->id);
3621 tid <= MVPP2_PRS_VID_PORT_LAST(port->id); tid++) {
3622 if (priv->prs_shadow[tid].valid)
3623 mvpp2_prs_vid_entry_remove(port, tid);
3624 }
3625}
3626
3627/* Remove VID filering entry for this port */
3628static void mvpp2_prs_vid_disable_filtering(struct mvpp2_port *port)
3629{
3630 unsigned int tid = MVPP2_PRS_VID_PORT_DFLT(port->id);
3631 struct mvpp2 *priv = port->priv;
3632
3633 /* Invalidate the guard entry */
3634 mvpp2_prs_hw_inv(priv, tid);
3635
3636 priv->prs_shadow[tid].valid = false;
3637}
3638
3639/* Add guard entry that drops packets when no VID is matched on this port */
3640static void mvpp2_prs_vid_enable_filtering(struct mvpp2_port *port)
3641{
3642 unsigned int tid = MVPP2_PRS_VID_PORT_DFLT(port->id);
3643 struct mvpp2 *priv = port->priv;
3644 unsigned int reg_val, shift;
3645 struct mvpp2_prs_entry pe;
3646
3647 if (priv->prs_shadow[tid].valid)
3648 return;
3649
3650 memset(&pe, 0, sizeof(pe));
3651
3652 pe.index = tid;
3653
3654 reg_val = mvpp2_read(priv, MVPP2_MH_REG(port->id));
3655 if (reg_val & MVPP2_DSA_EXTENDED)
3656 shift = MVPP2_VLAN_TAG_EDSA_LEN;
3657 else
3658 shift = MVPP2_VLAN_TAG_LEN;
3659
3660 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VID);
3661
3662 /* Mask all ports */
3663 mvpp2_prs_tcam_port_map_set(&pe, 0);
3664
3665 /* Update port mask */
3666 mvpp2_prs_tcam_port_set(&pe, port->id, true);
3667
3668 /* Continue - set next lookup */
3669 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
3670
3671 /* Skip VLAN header - Set offset to 4 or 8 bytes */
3672 mvpp2_prs_sram_shift_set(&pe, shift, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3673
3674 /* Drop VLAN packets that don't belong to any VIDs on this port */
3675 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
3676 MVPP2_PRS_RI_DROP_MASK);
3677
3678 /* Clear all ai bits for next iteration */
3679 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
3680
3681 /* Update shadow table */
3682 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VID);
3683 mvpp2_prs_hw_write(priv, &pe);
3684}
3685
Marcin Wojtas3f518502014-07-10 16:52:13 -03003686/* Parser default initialization */
3687static int mvpp2_prs_default_init(struct platform_device *pdev,
3688 struct mvpp2 *priv)
3689{
3690 int err, index, i;
3691
3692 /* Enable tcam table */
3693 mvpp2_write(priv, MVPP2_PRS_TCAM_CTRL_REG, MVPP2_PRS_TCAM_EN_MASK);
3694
3695 /* Clear all tcam and sram entries */
3696 for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++) {
3697 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
3698 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
3699 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), 0);
3700
3701 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, index);
3702 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
3703 mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), 0);
3704 }
3705
3706 /* Invalidate all tcam entries */
3707 for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++)
3708 mvpp2_prs_hw_inv(priv, index);
3709
3710 priv->prs_shadow = devm_kcalloc(&pdev->dev, MVPP2_PRS_TCAM_SRAM_SIZE,
Markus Elfring37df25e2017-04-17 09:12:34 +02003711 sizeof(*priv->prs_shadow),
Marcin Wojtas3f518502014-07-10 16:52:13 -03003712 GFP_KERNEL);
3713 if (!priv->prs_shadow)
3714 return -ENOMEM;
3715
3716 /* Always start from lookup = 0 */
3717 for (index = 0; index < MVPP2_MAX_PORTS; index++)
3718 mvpp2_prs_hw_port_init(priv, index, MVPP2_PRS_LU_MH,
3719 MVPP2_PRS_PORT_LU_MAX, 0);
3720
3721 mvpp2_prs_def_flow_init(priv);
3722
3723 mvpp2_prs_mh_init(priv);
3724
3725 mvpp2_prs_mac_init(priv);
3726
3727 mvpp2_prs_dsa_init(priv);
3728
Maxime Chevallier56beda32018-02-28 10:14:13 +01003729 mvpp2_prs_vid_init(priv);
3730
Marcin Wojtas3f518502014-07-10 16:52:13 -03003731 err = mvpp2_prs_etype_init(priv);
3732 if (err)
3733 return err;
3734
3735 err = mvpp2_prs_vlan_init(pdev, priv);
3736 if (err)
3737 return err;
3738
3739 err = mvpp2_prs_pppoe_init(priv);
3740 if (err)
3741 return err;
3742
3743 err = mvpp2_prs_ip6_init(priv);
3744 if (err)
3745 return err;
3746
3747 err = mvpp2_prs_ip4_init(priv);
3748 if (err)
3749 return err;
3750
3751 return 0;
3752}
3753
3754/* Compare MAC DA with tcam entry data */
3755static bool mvpp2_prs_mac_range_equals(struct mvpp2_prs_entry *pe,
3756 const u8 *da, unsigned char *mask)
3757{
3758 unsigned char tcam_byte, tcam_mask;
3759 int index;
3760
3761 for (index = 0; index < ETH_ALEN; index++) {
3762 mvpp2_prs_tcam_data_byte_get(pe, index, &tcam_byte, &tcam_mask);
3763 if (tcam_mask != mask[index])
3764 return false;
3765
3766 if ((tcam_mask & tcam_byte) != (da[index] & mask[index]))
3767 return false;
3768 }
3769
3770 return true;
3771}
3772
3773/* Find tcam entry with matched pair <MAC DA, port> */
3774static struct mvpp2_prs_entry *
3775mvpp2_prs_mac_da_range_find(struct mvpp2 *priv, int pmap, const u8 *da,
3776 unsigned char *mask, int udf_type)
3777{
3778 struct mvpp2_prs_entry *pe;
3779 int tid;
3780
Antoine Tenart239dd4e2017-10-24 11:41:28 +02003781 pe = kzalloc(sizeof(*pe), GFP_ATOMIC);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003782 if (!pe)
3783 return NULL;
3784 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
3785
3786 /* Go through the all entires with MVPP2_PRS_LU_MAC */
3787 for (tid = MVPP2_PE_FIRST_FREE_TID;
3788 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
3789 unsigned int entry_pmap;
3790
3791 if (!priv->prs_shadow[tid].valid ||
3792 (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
3793 (priv->prs_shadow[tid].udf != udf_type))
3794 continue;
3795
3796 pe->index = tid;
3797 mvpp2_prs_hw_read(priv, pe);
3798 entry_pmap = mvpp2_prs_tcam_port_map_get(pe);
3799
3800 if (mvpp2_prs_mac_range_equals(pe, da, mask) &&
3801 entry_pmap == pmap)
3802 return pe;
3803 }
3804 kfree(pe);
3805
3806 return NULL;
3807}
3808
3809/* Update parser's mac da entry */
3810static int mvpp2_prs_mac_da_accept(struct mvpp2 *priv, int port,
3811 const u8 *da, bool add)
3812{
3813 struct mvpp2_prs_entry *pe;
3814 unsigned int pmap, len, ri;
3815 unsigned char mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3816 int tid;
3817
3818 /* Scan TCAM and see if entry with this <MAC DA, port> already exist */
3819 pe = mvpp2_prs_mac_da_range_find(priv, (1 << port), da, mask,
3820 MVPP2_PRS_UDF_MAC_DEF);
3821
3822 /* No such entry */
3823 if (!pe) {
3824 if (!add)
3825 return 0;
3826
3827 /* Create new TCAM entry */
3828 /* Find first range mac entry*/
3829 for (tid = MVPP2_PE_FIRST_FREE_TID;
3830 tid <= MVPP2_PE_LAST_FREE_TID; tid++)
3831 if (priv->prs_shadow[tid].valid &&
3832 (priv->prs_shadow[tid].lu == MVPP2_PRS_LU_MAC) &&
3833 (priv->prs_shadow[tid].udf ==
3834 MVPP2_PRS_UDF_MAC_RANGE))
3835 break;
3836
3837 /* Go through the all entries from first to last */
3838 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3839 tid - 1);
3840 if (tid < 0)
3841 return tid;
3842
Antoine Tenart239dd4e2017-10-24 11:41:28 +02003843 pe = kzalloc(sizeof(*pe), GFP_ATOMIC);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003844 if (!pe)
Amitoj Kaur Chawlac2bb7bc2016-02-04 19:25:26 +05303845 return -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003846 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
3847 pe->index = tid;
3848
3849 /* Mask all ports */
3850 mvpp2_prs_tcam_port_map_set(pe, 0);
3851 }
3852
3853 /* Update port mask */
3854 mvpp2_prs_tcam_port_set(pe, port, add);
3855
3856 /* Invalidate the entry if no ports are left enabled */
3857 pmap = mvpp2_prs_tcam_port_map_get(pe);
3858 if (pmap == 0) {
3859 if (add) {
3860 kfree(pe);
Amitoj Kaur Chawlac2bb7bc2016-02-04 19:25:26 +05303861 return -EINVAL;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003862 }
3863 mvpp2_prs_hw_inv(priv, pe->index);
3864 priv->prs_shadow[pe->index].valid = false;
3865 kfree(pe);
3866 return 0;
3867 }
3868
3869 /* Continue - set next lookup */
3870 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_DSA);
3871
3872 /* Set match on DA */
3873 len = ETH_ALEN;
3874 while (len--)
3875 mvpp2_prs_tcam_data_byte_set(pe, len, da[len], 0xff);
3876
3877 /* Set result info bits */
3878 if (is_broadcast_ether_addr(da))
3879 ri = MVPP2_PRS_RI_L2_BCAST;
3880 else if (is_multicast_ether_addr(da))
3881 ri = MVPP2_PRS_RI_L2_MCAST;
3882 else
3883 ri = MVPP2_PRS_RI_L2_UCAST | MVPP2_PRS_RI_MAC_ME_MASK;
3884
3885 mvpp2_prs_sram_ri_update(pe, ri, MVPP2_PRS_RI_L2_CAST_MASK |
3886 MVPP2_PRS_RI_MAC_ME_MASK);
3887 mvpp2_prs_shadow_ri_set(priv, pe->index, ri, MVPP2_PRS_RI_L2_CAST_MASK |
3888 MVPP2_PRS_RI_MAC_ME_MASK);
3889
3890 /* Shift to ethertype */
3891 mvpp2_prs_sram_shift_set(pe, 2 * ETH_ALEN,
3892 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3893
3894 /* Update shadow table and hw entry */
3895 priv->prs_shadow[pe->index].udf = MVPP2_PRS_UDF_MAC_DEF;
3896 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_MAC);
3897 mvpp2_prs_hw_write(priv, pe);
3898
3899 kfree(pe);
3900
3901 return 0;
3902}
3903
3904static int mvpp2_prs_update_mac_da(struct net_device *dev, const u8 *da)
3905{
3906 struct mvpp2_port *port = netdev_priv(dev);
3907 int err;
3908
3909 /* Remove old parser entry */
3910 err = mvpp2_prs_mac_da_accept(port->priv, port->id, dev->dev_addr,
3911 false);
3912 if (err)
3913 return err;
3914
3915 /* Add new parser entry */
3916 err = mvpp2_prs_mac_da_accept(port->priv, port->id, da, true);
3917 if (err)
3918 return err;
3919
3920 /* Set addr in the device */
3921 ether_addr_copy(dev->dev_addr, da);
3922
3923 return 0;
3924}
3925
3926/* Delete all port's multicast simple (not range) entries */
3927static void mvpp2_prs_mcast_del_all(struct mvpp2 *priv, int port)
3928{
3929 struct mvpp2_prs_entry pe;
3930 int index, tid;
3931
3932 for (tid = MVPP2_PE_FIRST_FREE_TID;
3933 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
3934 unsigned char da[ETH_ALEN], da_mask[ETH_ALEN];
3935
3936 if (!priv->prs_shadow[tid].valid ||
3937 (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
3938 (priv->prs_shadow[tid].udf != MVPP2_PRS_UDF_MAC_DEF))
3939 continue;
3940
3941 /* Only simple mac entries */
3942 pe.index = tid;
3943 mvpp2_prs_hw_read(priv, &pe);
3944
3945 /* Read mac addr from entry */
3946 for (index = 0; index < ETH_ALEN; index++)
3947 mvpp2_prs_tcam_data_byte_get(&pe, index, &da[index],
3948 &da_mask[index]);
3949
3950 if (is_multicast_ether_addr(da) && !is_broadcast_ether_addr(da))
3951 /* Delete this entry */
3952 mvpp2_prs_mac_da_accept(priv, port, da, false);
3953 }
3954}
3955
3956static int mvpp2_prs_tag_mode_set(struct mvpp2 *priv, int port, int type)
3957{
3958 switch (type) {
3959 case MVPP2_TAG_TYPE_EDSA:
3960 /* Add port to EDSA entries */
3961 mvpp2_prs_dsa_tag_set(priv, port, true,
3962 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3963 mvpp2_prs_dsa_tag_set(priv, port, true,
3964 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3965 /* Remove port from DSA entries */
3966 mvpp2_prs_dsa_tag_set(priv, port, false,
3967 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3968 mvpp2_prs_dsa_tag_set(priv, port, false,
3969 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3970 break;
3971
3972 case MVPP2_TAG_TYPE_DSA:
3973 /* Add port to DSA entries */
3974 mvpp2_prs_dsa_tag_set(priv, port, true,
3975 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3976 mvpp2_prs_dsa_tag_set(priv, port, true,
3977 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3978 /* Remove port from EDSA entries */
3979 mvpp2_prs_dsa_tag_set(priv, port, false,
3980 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3981 mvpp2_prs_dsa_tag_set(priv, port, false,
3982 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3983 break;
3984
3985 case MVPP2_TAG_TYPE_MH:
3986 case MVPP2_TAG_TYPE_NONE:
3987 /* Remove port form EDSA and DSA entries */
3988 mvpp2_prs_dsa_tag_set(priv, port, false,
3989 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3990 mvpp2_prs_dsa_tag_set(priv, port, false,
3991 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3992 mvpp2_prs_dsa_tag_set(priv, port, false,
3993 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3994 mvpp2_prs_dsa_tag_set(priv, port, false,
3995 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3996 break;
3997
3998 default:
3999 if ((type < 0) || (type > MVPP2_TAG_TYPE_EDSA))
4000 return -EINVAL;
4001 }
4002
4003 return 0;
4004}
4005
4006/* Set prs flow for the port */
4007static int mvpp2_prs_def_flow(struct mvpp2_port *port)
4008{
4009 struct mvpp2_prs_entry *pe;
4010 int tid;
4011
4012 pe = mvpp2_prs_flow_find(port->priv, port->id);
4013
4014 /* Such entry not exist */
4015 if (!pe) {
4016 /* Go through the all entires from last to first */
4017 tid = mvpp2_prs_tcam_first_free(port->priv,
4018 MVPP2_PE_LAST_FREE_TID,
4019 MVPP2_PE_FIRST_FREE_TID);
4020 if (tid < 0)
4021 return tid;
4022
4023 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
4024 if (!pe)
4025 return -ENOMEM;
4026
4027 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
4028 pe->index = tid;
4029
4030 /* Set flow ID*/
4031 mvpp2_prs_sram_ai_update(pe, port->id, MVPP2_PRS_FLOW_ID_MASK);
4032 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
4033
4034 /* Update shadow table */
4035 mvpp2_prs_shadow_set(port->priv, pe->index, MVPP2_PRS_LU_FLOWS);
4036 }
4037
4038 mvpp2_prs_tcam_port_map_set(pe, (1 << port->id));
4039 mvpp2_prs_hw_write(port->priv, pe);
4040 kfree(pe);
4041
4042 return 0;
4043}
4044
4045/* Classifier configuration routines */
4046
4047/* Update classification flow table registers */
4048static void mvpp2_cls_flow_write(struct mvpp2 *priv,
4049 struct mvpp2_cls_flow_entry *fe)
4050{
4051 mvpp2_write(priv, MVPP2_CLS_FLOW_INDEX_REG, fe->index);
4052 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL0_REG, fe->data[0]);
4053 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL1_REG, fe->data[1]);
4054 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL2_REG, fe->data[2]);
4055}
4056
4057/* Update classification lookup table register */
4058static void mvpp2_cls_lookup_write(struct mvpp2 *priv,
4059 struct mvpp2_cls_lookup_entry *le)
4060{
4061 u32 val;
4062
4063 val = (le->way << MVPP2_CLS_LKP_INDEX_WAY_OFFS) | le->lkpid;
4064 mvpp2_write(priv, MVPP2_CLS_LKP_INDEX_REG, val);
4065 mvpp2_write(priv, MVPP2_CLS_LKP_TBL_REG, le->data);
4066}
4067
4068/* Classifier default initialization */
4069static void mvpp2_cls_init(struct mvpp2 *priv)
4070{
4071 struct mvpp2_cls_lookup_entry le;
4072 struct mvpp2_cls_flow_entry fe;
4073 int index;
4074
4075 /* Enable classifier */
4076 mvpp2_write(priv, MVPP2_CLS_MODE_REG, MVPP2_CLS_MODE_ACTIVE_MASK);
4077
4078 /* Clear classifier flow table */
Arnd Bergmanne8f967c2016-11-24 17:28:12 +01004079 memset(&fe.data, 0, sizeof(fe.data));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004080 for (index = 0; index < MVPP2_CLS_FLOWS_TBL_SIZE; index++) {
4081 fe.index = index;
4082 mvpp2_cls_flow_write(priv, &fe);
4083 }
4084
4085 /* Clear classifier lookup table */
4086 le.data = 0;
4087 for (index = 0; index < MVPP2_CLS_LKP_TBL_SIZE; index++) {
4088 le.lkpid = index;
4089 le.way = 0;
4090 mvpp2_cls_lookup_write(priv, &le);
4091
4092 le.way = 1;
4093 mvpp2_cls_lookup_write(priv, &le);
4094 }
4095}
4096
4097static void mvpp2_cls_port_config(struct mvpp2_port *port)
4098{
4099 struct mvpp2_cls_lookup_entry le;
4100 u32 val;
4101
4102 /* Set way for the port */
4103 val = mvpp2_read(port->priv, MVPP2_CLS_PORT_WAY_REG);
4104 val &= ~MVPP2_CLS_PORT_WAY_MASK(port->id);
4105 mvpp2_write(port->priv, MVPP2_CLS_PORT_WAY_REG, val);
4106
4107 /* Pick the entry to be accessed in lookup ID decoding table
4108 * according to the way and lkpid.
4109 */
4110 le.lkpid = port->id;
4111 le.way = 0;
4112 le.data = 0;
4113
4114 /* Set initial CPU queue for receiving packets */
4115 le.data &= ~MVPP2_CLS_LKP_TBL_RXQ_MASK;
4116 le.data |= port->first_rxq;
4117
4118 /* Disable classification engines */
4119 le.data &= ~MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK;
4120
4121 /* Update lookup ID table entry */
4122 mvpp2_cls_lookup_write(port->priv, &le);
4123}
4124
4125/* Set CPU queue number for oversize packets */
4126static void mvpp2_cls_oversize_rxq_set(struct mvpp2_port *port)
4127{
4128 u32 val;
4129
4130 mvpp2_write(port->priv, MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port->id),
4131 port->first_rxq & MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK);
4132
4133 mvpp2_write(port->priv, MVPP2_CLS_SWFWD_P2HQ_REG(port->id),
4134 (port->first_rxq >> MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS));
4135
4136 val = mvpp2_read(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG);
4137 val |= MVPP2_CLS_SWFWD_PCTRL_MASK(port->id);
4138 mvpp2_write(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG, val);
4139}
4140
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004141static void *mvpp2_frag_alloc(const struct mvpp2_bm_pool *pool)
4142{
4143 if (likely(pool->frag_size <= PAGE_SIZE))
4144 return netdev_alloc_frag(pool->frag_size);
4145 else
4146 return kmalloc(pool->frag_size, GFP_ATOMIC);
4147}
4148
4149static void mvpp2_frag_free(const struct mvpp2_bm_pool *pool, void *data)
4150{
4151 if (likely(pool->frag_size <= PAGE_SIZE))
4152 skb_free_frag(data);
4153 else
4154 kfree(data);
4155}
4156
Marcin Wojtas3f518502014-07-10 16:52:13 -03004157/* Buffer Manager configuration routines */
4158
4159/* Create pool */
4160static int mvpp2_bm_pool_create(struct platform_device *pdev,
4161 struct mvpp2 *priv,
4162 struct mvpp2_bm_pool *bm_pool, int size)
4163{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004164 u32 val;
4165
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004166 /* Number of buffer pointers must be a multiple of 16, as per
4167 * hardware constraints
4168 */
4169 if (!IS_ALIGNED(size, 16))
4170 return -EINVAL;
4171
4172 /* PPv2.1 needs 8 bytes per buffer pointer, PPv2.2 needs 16
4173 * bytes per buffer pointer
4174 */
4175 if (priv->hw_version == MVPP21)
4176 bm_pool->size_bytes = 2 * sizeof(u32) * size;
4177 else
4178 bm_pool->size_bytes = 2 * sizeof(u64) * size;
4179
4180 bm_pool->virt_addr = dma_alloc_coherent(&pdev->dev, bm_pool->size_bytes,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004181 &bm_pool->dma_addr,
Marcin Wojtas3f518502014-07-10 16:52:13 -03004182 GFP_KERNEL);
4183 if (!bm_pool->virt_addr)
4184 return -ENOMEM;
4185
Thomas Petazzonid3158802017-02-21 11:28:13 +01004186 if (!IS_ALIGNED((unsigned long)bm_pool->virt_addr,
4187 MVPP2_BM_POOL_PTR_ALIGN)) {
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004188 dma_free_coherent(&pdev->dev, bm_pool->size_bytes,
4189 bm_pool->virt_addr, bm_pool->dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004190 dev_err(&pdev->dev, "BM pool %d is not %d bytes aligned\n",
4191 bm_pool->id, MVPP2_BM_POOL_PTR_ALIGN);
4192 return -ENOMEM;
4193 }
4194
4195 mvpp2_write(priv, MVPP2_BM_POOL_BASE_REG(bm_pool->id),
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004196 lower_32_bits(bm_pool->dma_addr));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004197 mvpp2_write(priv, MVPP2_BM_POOL_SIZE_REG(bm_pool->id), size);
4198
4199 val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
4200 val |= MVPP2_BM_START_MASK;
4201 mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
4202
Marcin Wojtas3f518502014-07-10 16:52:13 -03004203 bm_pool->size = size;
4204 bm_pool->pkt_size = 0;
4205 bm_pool->buf_num = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004206
4207 return 0;
4208}
4209
4210/* Set pool buffer size */
4211static void mvpp2_bm_pool_bufsize_set(struct mvpp2 *priv,
4212 struct mvpp2_bm_pool *bm_pool,
4213 int buf_size)
4214{
4215 u32 val;
4216
4217 bm_pool->buf_size = buf_size;
4218
4219 val = ALIGN(buf_size, 1 << MVPP2_POOL_BUF_SIZE_OFFSET);
4220 mvpp2_write(priv, MVPP2_POOL_BUF_SIZE_REG(bm_pool->id), val);
4221}
4222
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004223static void mvpp2_bm_bufs_get_addrs(struct device *dev, struct mvpp2 *priv,
4224 struct mvpp2_bm_pool *bm_pool,
4225 dma_addr_t *dma_addr,
4226 phys_addr_t *phys_addr)
4227{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004228 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01004229
4230 *dma_addr = mvpp2_percpu_read(priv, cpu,
4231 MVPP2_BM_PHY_ALLOC_REG(bm_pool->id));
4232 *phys_addr = mvpp2_percpu_read(priv, cpu, MVPP2_BM_VIRT_ALLOC_REG);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004233
4234 if (priv->hw_version == MVPP22) {
4235 u32 val;
4236 u32 dma_addr_highbits, phys_addr_highbits;
4237
Thomas Petazzonia7868412017-03-07 16:53:13 +01004238 val = mvpp2_percpu_read(priv, cpu, MVPP22_BM_ADDR_HIGH_ALLOC);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004239 dma_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_PHYS_MASK);
4240 phys_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_VIRT_MASK) >>
4241 MVPP22_BM_ADDR_HIGH_VIRT_SHIFT;
4242
4243 if (sizeof(dma_addr_t) == 8)
4244 *dma_addr |= (u64)dma_addr_highbits << 32;
4245
4246 if (sizeof(phys_addr_t) == 8)
4247 *phys_addr |= (u64)phys_addr_highbits << 32;
4248 }
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004249
4250 put_cpu();
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004251}
4252
Ezequiel Garcia7861f122014-07-21 13:48:14 -03004253/* Free all buffers from the pool */
Marcin Wojtas4229d502015-12-03 15:20:50 +01004254static void mvpp2_bm_bufs_free(struct device *dev, struct mvpp2 *priv,
Stefan Chulskieffbf5f2018-03-05 15:16:51 +01004255 struct mvpp2_bm_pool *bm_pool, int buf_num)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004256{
4257 int i;
4258
Stefan Chulskieffbf5f2018-03-05 15:16:51 +01004259 if (buf_num > bm_pool->buf_num) {
4260 WARN(1, "Pool does not have so many bufs pool(%d) bufs(%d)\n",
4261 bm_pool->id, buf_num);
4262 buf_num = bm_pool->buf_num;
4263 }
4264
4265 for (i = 0; i < buf_num; i++) {
Thomas Petazzoni20396132017-03-07 16:53:00 +01004266 dma_addr_t buf_dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004267 phys_addr_t buf_phys_addr;
4268 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004269
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004270 mvpp2_bm_bufs_get_addrs(dev, priv, bm_pool,
4271 &buf_dma_addr, &buf_phys_addr);
Marcin Wojtas4229d502015-12-03 15:20:50 +01004272
Thomas Petazzoni20396132017-03-07 16:53:00 +01004273 dma_unmap_single(dev, buf_dma_addr,
Marcin Wojtas4229d502015-12-03 15:20:50 +01004274 bm_pool->buf_size, DMA_FROM_DEVICE);
4275
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004276 data = (void *)phys_to_virt(buf_phys_addr);
4277 if (!data)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004278 break;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004279
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004280 mvpp2_frag_free(bm_pool, data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004281 }
4282
4283 /* Update BM driver with number of buffers removed from pool */
4284 bm_pool->buf_num -= i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004285}
4286
Stefan Chulskieffbf5f2018-03-05 15:16:51 +01004287/* Check number of buffers in BM pool */
4288int mvpp2_check_hw_buf_num(struct mvpp2 *priv, struct mvpp2_bm_pool *bm_pool)
4289{
4290 int buf_num = 0;
4291
4292 buf_num += mvpp2_read(priv, MVPP2_BM_POOL_PTRS_NUM_REG(bm_pool->id)) &
4293 MVPP22_BM_POOL_PTRS_NUM_MASK;
4294 buf_num += mvpp2_read(priv, MVPP2_BM_BPPI_PTRS_NUM_REG(bm_pool->id)) &
4295 MVPP2_BM_BPPI_PTR_NUM_MASK;
4296
4297 /* HW has one buffer ready which is not reflected in the counters */
4298 if (buf_num)
4299 buf_num += 1;
4300
4301 return buf_num;
4302}
4303
Marcin Wojtas3f518502014-07-10 16:52:13 -03004304/* Cleanup pool */
4305static int mvpp2_bm_pool_destroy(struct platform_device *pdev,
4306 struct mvpp2 *priv,
4307 struct mvpp2_bm_pool *bm_pool)
4308{
Stefan Chulskieffbf5f2018-03-05 15:16:51 +01004309 int buf_num;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004310 u32 val;
4311
Stefan Chulskieffbf5f2018-03-05 15:16:51 +01004312 buf_num = mvpp2_check_hw_buf_num(priv, bm_pool);
4313 mvpp2_bm_bufs_free(&pdev->dev, priv, bm_pool, buf_num);
4314
4315 /* Check buffer counters after free */
4316 buf_num = mvpp2_check_hw_buf_num(priv, bm_pool);
4317 if (buf_num) {
4318 WARN(1, "cannot free all buffers in pool %d, buf_num left %d\n",
4319 bm_pool->id, bm_pool->buf_num);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004320 return 0;
4321 }
4322
4323 val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
4324 val |= MVPP2_BM_STOP_MASK;
4325 mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
4326
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004327 dma_free_coherent(&pdev->dev, bm_pool->size_bytes,
Marcin Wojtas3f518502014-07-10 16:52:13 -03004328 bm_pool->virt_addr,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004329 bm_pool->dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004330 return 0;
4331}
4332
4333static int mvpp2_bm_pools_init(struct platform_device *pdev,
4334 struct mvpp2 *priv)
4335{
4336 int i, err, size;
4337 struct mvpp2_bm_pool *bm_pool;
4338
4339 /* Create all pools with maximum size */
4340 size = MVPP2_BM_POOL_SIZE_MAX;
4341 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
4342 bm_pool = &priv->bm_pools[i];
4343 bm_pool->id = i;
4344 err = mvpp2_bm_pool_create(pdev, priv, bm_pool, size);
4345 if (err)
4346 goto err_unroll_pools;
4347 mvpp2_bm_pool_bufsize_set(priv, bm_pool, 0);
4348 }
4349 return 0;
4350
4351err_unroll_pools:
4352 dev_err(&pdev->dev, "failed to create BM pool %d, size %d\n", i, size);
4353 for (i = i - 1; i >= 0; i--)
4354 mvpp2_bm_pool_destroy(pdev, priv, &priv->bm_pools[i]);
4355 return err;
4356}
4357
4358static int mvpp2_bm_init(struct platform_device *pdev, struct mvpp2 *priv)
4359{
4360 int i, err;
4361
4362 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
4363 /* Mask BM all interrupts */
4364 mvpp2_write(priv, MVPP2_BM_INTR_MASK_REG(i), 0);
4365 /* Clear BM cause register */
4366 mvpp2_write(priv, MVPP2_BM_INTR_CAUSE_REG(i), 0);
4367 }
4368
4369 /* Allocate and initialize BM pools */
4370 priv->bm_pools = devm_kcalloc(&pdev->dev, MVPP2_BM_POOLS_NUM,
Markus Elfring81f915e2017-04-17 09:06:33 +02004371 sizeof(*priv->bm_pools), GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004372 if (!priv->bm_pools)
4373 return -ENOMEM;
4374
4375 err = mvpp2_bm_pools_init(pdev, priv);
4376 if (err < 0)
4377 return err;
4378 return 0;
4379}
4380
Stefan Chulski01d04932018-03-05 15:16:50 +01004381static void mvpp2_setup_bm_pool(void)
4382{
4383 /* Short pool */
4384 mvpp2_pools[MVPP2_BM_SHORT].buf_num = MVPP2_BM_SHORT_BUF_NUM;
4385 mvpp2_pools[MVPP2_BM_SHORT].pkt_size = MVPP2_BM_SHORT_PKT_SIZE;
4386
4387 /* Long pool */
4388 mvpp2_pools[MVPP2_BM_LONG].buf_num = MVPP2_BM_LONG_BUF_NUM;
4389 mvpp2_pools[MVPP2_BM_LONG].pkt_size = MVPP2_BM_LONG_PKT_SIZE;
4390}
4391
Marcin Wojtas3f518502014-07-10 16:52:13 -03004392/* Attach long pool to rxq */
4393static void mvpp2_rxq_long_pool_set(struct mvpp2_port *port,
4394 int lrxq, int long_pool)
4395{
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004396 u32 val, mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004397 int prxq;
4398
4399 /* Get queue physical ID */
4400 prxq = port->rxqs[lrxq]->id;
4401
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004402 if (port->priv->hw_version == MVPP21)
4403 mask = MVPP21_RXQ_POOL_LONG_MASK;
4404 else
4405 mask = MVPP22_RXQ_POOL_LONG_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004406
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004407 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
4408 val &= ~mask;
4409 val |= (long_pool << MVPP2_RXQ_POOL_LONG_OFFS) & mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004410 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
4411}
4412
4413/* Attach short pool to rxq */
4414static void mvpp2_rxq_short_pool_set(struct mvpp2_port *port,
4415 int lrxq, int short_pool)
4416{
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004417 u32 val, mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004418 int prxq;
4419
4420 /* Get queue physical ID */
4421 prxq = port->rxqs[lrxq]->id;
4422
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004423 if (port->priv->hw_version == MVPP21)
4424 mask = MVPP21_RXQ_POOL_SHORT_MASK;
4425 else
4426 mask = MVPP22_RXQ_POOL_SHORT_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004427
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004428 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
4429 val &= ~mask;
4430 val |= (short_pool << MVPP2_RXQ_POOL_SHORT_OFFS) & mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004431 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
4432}
4433
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004434static void *mvpp2_buf_alloc(struct mvpp2_port *port,
4435 struct mvpp2_bm_pool *bm_pool,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004436 dma_addr_t *buf_dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004437 phys_addr_t *buf_phys_addr,
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004438 gfp_t gfp_mask)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004439{
Thomas Petazzoni20396132017-03-07 16:53:00 +01004440 dma_addr_t dma_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004441 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004442
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004443 data = mvpp2_frag_alloc(bm_pool);
4444 if (!data)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004445 return NULL;
4446
Thomas Petazzoni20396132017-03-07 16:53:00 +01004447 dma_addr = dma_map_single(port->dev->dev.parent, data,
4448 MVPP2_RX_BUF_SIZE(bm_pool->pkt_size),
4449 DMA_FROM_DEVICE);
4450 if (unlikely(dma_mapping_error(port->dev->dev.parent, dma_addr))) {
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004451 mvpp2_frag_free(bm_pool, data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004452 return NULL;
4453 }
Thomas Petazzoni20396132017-03-07 16:53:00 +01004454 *buf_dma_addr = dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004455 *buf_phys_addr = virt_to_phys(data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004456
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004457 return data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004458}
4459
Marcin Wojtas3f518502014-07-10 16:52:13 -03004460/* Release buffer to BM */
4461static inline void mvpp2_bm_pool_put(struct mvpp2_port *port, int pool,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004462 dma_addr_t buf_dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004463 phys_addr_t buf_phys_addr)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004464{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004465 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01004466
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004467 if (port->priv->hw_version == MVPP22) {
4468 u32 val = 0;
4469
4470 if (sizeof(dma_addr_t) == 8)
4471 val |= upper_32_bits(buf_dma_addr) &
4472 MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK;
4473
4474 if (sizeof(phys_addr_t) == 8)
4475 val |= (upper_32_bits(buf_phys_addr)
4476 << MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT) &
4477 MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK;
4478
Thomas Petazzonia7868412017-03-07 16:53:13 +01004479 mvpp2_percpu_write(port->priv, cpu,
4480 MVPP22_BM_ADDR_HIGH_RLS_REG, val);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004481 }
4482
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004483 /* MVPP2_BM_VIRT_RLS_REG is not interpreted by HW, and simply
4484 * returned in the "cookie" field of the RX
4485 * descriptor. Instead of storing the virtual address, we
4486 * store the physical address
4487 */
Thomas Petazzonia7868412017-03-07 16:53:13 +01004488 mvpp2_percpu_write(port->priv, cpu,
4489 MVPP2_BM_VIRT_RLS_REG, buf_phys_addr);
4490 mvpp2_percpu_write(port->priv, cpu,
4491 MVPP2_BM_PHY_RLS_REG(pool), buf_dma_addr);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004492
4493 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03004494}
4495
Marcin Wojtas3f518502014-07-10 16:52:13 -03004496/* Allocate buffers for the pool */
4497static int mvpp2_bm_bufs_add(struct mvpp2_port *port,
4498 struct mvpp2_bm_pool *bm_pool, int buf_num)
4499{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004500 int i, buf_size, total_size;
Thomas Petazzoni20396132017-03-07 16:53:00 +01004501 dma_addr_t dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004502 phys_addr_t phys_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004503 void *buf;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004504
4505 buf_size = MVPP2_RX_BUF_SIZE(bm_pool->pkt_size);
4506 total_size = MVPP2_RX_TOTAL_SIZE(buf_size);
4507
4508 if (buf_num < 0 ||
4509 (buf_num + bm_pool->buf_num > bm_pool->size)) {
4510 netdev_err(port->dev,
4511 "cannot allocate %d buffers for pool %d\n",
4512 buf_num, bm_pool->id);
4513 return 0;
4514 }
4515
Marcin Wojtas3f518502014-07-10 16:52:13 -03004516 for (i = 0; i < buf_num; i++) {
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004517 buf = mvpp2_buf_alloc(port, bm_pool, &dma_addr,
4518 &phys_addr, GFP_KERNEL);
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004519 if (!buf)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004520 break;
4521
Thomas Petazzoni20396132017-03-07 16:53:00 +01004522 mvpp2_bm_pool_put(port, bm_pool->id, dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004523 phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004524 }
4525
4526 /* Update BM driver with number of buffers added to pool */
4527 bm_pool->buf_num += i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004528
4529 netdev_dbg(port->dev,
Stefan Chulski01d04932018-03-05 15:16:50 +01004530 "pool %d: pkt_size=%4d, buf_size=%4d, total_size=%4d\n",
Marcin Wojtas3f518502014-07-10 16:52:13 -03004531 bm_pool->id, bm_pool->pkt_size, buf_size, total_size);
4532
4533 netdev_dbg(port->dev,
Stefan Chulski01d04932018-03-05 15:16:50 +01004534 "pool %d: %d of %d buffers added\n",
Marcin Wojtas3f518502014-07-10 16:52:13 -03004535 bm_pool->id, i, buf_num);
4536 return i;
4537}
4538
4539/* Notify the driver that BM pool is being used as specific type and return the
4540 * pool pointer on success
4541 */
4542static struct mvpp2_bm_pool *
Stefan Chulski01d04932018-03-05 15:16:50 +01004543mvpp2_bm_pool_use(struct mvpp2_port *port, unsigned pool, int pkt_size)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004544{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004545 struct mvpp2_bm_pool *new_pool = &port->priv->bm_pools[pool];
4546 int num;
4547
Stefan Chulski01d04932018-03-05 15:16:50 +01004548 if (pool >= MVPP2_BM_POOLS_NUM) {
4549 netdev_err(port->dev, "Invalid pool %d\n", pool);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004550 return NULL;
4551 }
4552
Marcin Wojtas3f518502014-07-10 16:52:13 -03004553 /* Allocate buffers in case BM pool is used as long pool, but packet
4554 * size doesn't match MTU or BM pool hasn't being used yet
4555 */
Stefan Chulski01d04932018-03-05 15:16:50 +01004556 if (new_pool->pkt_size == 0) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004557 int pkts_num;
4558
4559 /* Set default buffer number or free all the buffers in case
4560 * the pool is not empty
4561 */
4562 pkts_num = new_pool->buf_num;
4563 if (pkts_num == 0)
Stefan Chulski01d04932018-03-05 15:16:50 +01004564 pkts_num = mvpp2_pools[pool].buf_num;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004565 else
Marcin Wojtas4229d502015-12-03 15:20:50 +01004566 mvpp2_bm_bufs_free(port->dev->dev.parent,
Stefan Chulskieffbf5f2018-03-05 15:16:51 +01004567 port->priv, new_pool, pkts_num);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004568
4569 new_pool->pkt_size = pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004570 new_pool->frag_size =
4571 SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
4572 MVPP2_SKB_SHINFO_SIZE;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004573
4574 /* Allocate buffers for this pool */
4575 num = mvpp2_bm_bufs_add(port, new_pool, pkts_num);
4576 if (num != pkts_num) {
4577 WARN(1, "pool %d: %d of %d allocated\n",
4578 new_pool->id, num, pkts_num);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004579 return NULL;
4580 }
4581 }
4582
4583 mvpp2_bm_pool_bufsize_set(port->priv, new_pool,
4584 MVPP2_RX_BUF_SIZE(new_pool->pkt_size));
4585
Marcin Wojtas3f518502014-07-10 16:52:13 -03004586 return new_pool;
4587}
4588
4589/* Initialize pools for swf */
4590static int mvpp2_swf_bm_pool_init(struct mvpp2_port *port)
4591{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004592 int rxq;
4593
4594 if (!port->pool_long) {
4595 port->pool_long =
Stefan Chulski01d04932018-03-05 15:16:50 +01004596 mvpp2_bm_pool_use(port, MVPP2_BM_LONG,
4597 mvpp2_pools[MVPP2_BM_LONG].pkt_size);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004598 if (!port->pool_long)
4599 return -ENOMEM;
4600
Marcin Wojtas3f518502014-07-10 16:52:13 -03004601 port->pool_long->port_map |= (1 << port->id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004602
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004603 for (rxq = 0; rxq < port->nrxqs; rxq++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004604 mvpp2_rxq_long_pool_set(port, rxq, port->pool_long->id);
4605 }
4606
4607 if (!port->pool_short) {
4608 port->pool_short =
Stefan Chulski01d04932018-03-05 15:16:50 +01004609 mvpp2_bm_pool_use(port, MVPP2_BM_SHORT,
4610 mvpp2_pools[MVPP2_BM_SHORT].pkt_size);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004611 if (!port->pool_short)
4612 return -ENOMEM;
4613
Marcin Wojtas3f518502014-07-10 16:52:13 -03004614 port->pool_short->port_map |= (1 << port->id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004615
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004616 for (rxq = 0; rxq < port->nrxqs; rxq++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004617 mvpp2_rxq_short_pool_set(port, rxq,
4618 port->pool_short->id);
4619 }
4620
4621 return 0;
4622}
4623
4624static int mvpp2_bm_update_mtu(struct net_device *dev, int mtu)
4625{
4626 struct mvpp2_port *port = netdev_priv(dev);
4627 struct mvpp2_bm_pool *port_pool = port->pool_long;
4628 int num, pkts_num = port_pool->buf_num;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004629
4630 /* Update BM pool with new buffer size */
Stefan Chulskieffbf5f2018-03-05 15:16:51 +01004631 mvpp2_bm_bufs_free(dev->dev.parent, port->priv, port_pool,
4632 port_pool->buf_num);
Ezequiel Garciad74c96c2014-07-21 13:48:13 -03004633 if (port_pool->buf_num) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004634 WARN(1, "cannot free all buffers in pool %d\n", port_pool->id);
4635 return -EIO;
4636 }
4637
Marcin Wojtas3f518502014-07-10 16:52:13 -03004638 num = mvpp2_bm_bufs_add(port, port_pool, pkts_num);
4639 if (num != pkts_num) {
4640 WARN(1, "pool %d: %d of %d allocated\n",
4641 port_pool->id, num, pkts_num);
4642 return -EIO;
4643 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004644 dev->mtu = mtu;
4645 netdev_update_features(dev);
4646 return 0;
4647}
4648
4649static inline void mvpp2_interrupts_enable(struct mvpp2_port *port)
4650{
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004651 int i, sw_thread_mask = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004652
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004653 for (i = 0; i < port->nqvecs; i++)
4654 sw_thread_mask |= port->qvecs[i].sw_thread_mask;
4655
Marcin Wojtas3f518502014-07-10 16:52:13 -03004656 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004657 MVPP2_ISR_ENABLE_INTERRUPT(sw_thread_mask));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004658}
4659
4660static inline void mvpp2_interrupts_disable(struct mvpp2_port *port)
4661{
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004662 int i, sw_thread_mask = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004663
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004664 for (i = 0; i < port->nqvecs; i++)
4665 sw_thread_mask |= port->qvecs[i].sw_thread_mask;
4666
Marcin Wojtas3f518502014-07-10 16:52:13 -03004667 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004668 MVPP2_ISR_DISABLE_INTERRUPT(sw_thread_mask));
4669}
4670
4671static inline void mvpp2_qvec_interrupt_enable(struct mvpp2_queue_vector *qvec)
4672{
4673 struct mvpp2_port *port = qvec->port;
4674
4675 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
4676 MVPP2_ISR_ENABLE_INTERRUPT(qvec->sw_thread_mask));
4677}
4678
4679static inline void mvpp2_qvec_interrupt_disable(struct mvpp2_queue_vector *qvec)
4680{
4681 struct mvpp2_port *port = qvec->port;
4682
4683 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
4684 MVPP2_ISR_DISABLE_INTERRUPT(qvec->sw_thread_mask));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004685}
4686
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004687/* Mask the current CPU's Rx/Tx interrupts
4688 * Called by on_each_cpu(), guaranteed to run with migration disabled,
4689 * using smp_processor_id() is OK.
4690 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004691static void mvpp2_interrupts_mask(void *arg)
4692{
4693 struct mvpp2_port *port = arg;
4694
Thomas Petazzonia7868412017-03-07 16:53:13 +01004695 mvpp2_percpu_write(port->priv, smp_processor_id(),
4696 MVPP2_ISR_RX_TX_MASK_REG(port->id), 0);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004697}
4698
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004699/* Unmask the current CPU's Rx/Tx interrupts.
4700 * Called by on_each_cpu(), guaranteed to run with migration disabled,
4701 * using smp_processor_id() is OK.
4702 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004703static void mvpp2_interrupts_unmask(void *arg)
4704{
4705 struct mvpp2_port *port = arg;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02004706 u32 val;
4707
4708 val = MVPP2_CAUSE_MISC_SUM_MASK |
4709 MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
4710 if (port->has_tx_irqs)
4711 val |= MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004712
Thomas Petazzonia7868412017-03-07 16:53:13 +01004713 mvpp2_percpu_write(port->priv, smp_processor_id(),
Thomas Petazzoni213f4282017-08-03 10:42:00 +02004714 MVPP2_ISR_RX_TX_MASK_REG(port->id), val);
4715}
4716
4717static void
4718mvpp2_shared_interrupt_mask_unmask(struct mvpp2_port *port, bool mask)
4719{
4720 u32 val;
4721 int i;
4722
4723 if (port->priv->hw_version != MVPP22)
4724 return;
4725
4726 if (mask)
4727 val = 0;
4728 else
4729 val = MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
4730
4731 for (i = 0; i < port->nqvecs; i++) {
4732 struct mvpp2_queue_vector *v = port->qvecs + i;
4733
4734 if (v->type != MVPP2_QUEUE_VECTOR_SHARED)
4735 continue;
4736
4737 mvpp2_percpu_write(port->priv, v->sw_thread_id,
4738 MVPP2_ISR_RX_TX_MASK_REG(port->id), val);
4739 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004740}
4741
4742/* Port configuration routines */
4743
Antoine Ténartf84bf382017-08-22 19:08:27 +02004744static void mvpp22_gop_init_rgmii(struct mvpp2_port *port)
4745{
4746 struct mvpp2 *priv = port->priv;
4747 u32 val;
4748
4749 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
4750 val |= GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT;
4751 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
4752
4753 regmap_read(priv->sysctrl_base, GENCONF_CTRL0, &val);
4754 if (port->gop_id == 2)
4755 val |= GENCONF_CTRL0_PORT0_RGMII | GENCONF_CTRL0_PORT1_RGMII;
4756 else if (port->gop_id == 3)
4757 val |= GENCONF_CTRL0_PORT1_RGMII_MII;
4758 regmap_write(priv->sysctrl_base, GENCONF_CTRL0, val);
4759}
4760
4761static void mvpp22_gop_init_sgmii(struct mvpp2_port *port)
4762{
4763 struct mvpp2 *priv = port->priv;
4764 u32 val;
4765
4766 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
4767 val |= GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT |
4768 GENCONF_PORT_CTRL0_RX_DATA_SAMPLE;
4769 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
4770
4771 if (port->gop_id > 1) {
4772 regmap_read(priv->sysctrl_base, GENCONF_CTRL0, &val);
4773 if (port->gop_id == 2)
4774 val &= ~GENCONF_CTRL0_PORT0_RGMII;
4775 else if (port->gop_id == 3)
4776 val &= ~GENCONF_CTRL0_PORT1_RGMII_MII;
4777 regmap_write(priv->sysctrl_base, GENCONF_CTRL0, val);
4778 }
4779}
4780
4781static void mvpp22_gop_init_10gkr(struct mvpp2_port *port)
4782{
4783 struct mvpp2 *priv = port->priv;
4784 void __iomem *mpcs = priv->iface_base + MVPP22_MPCS_BASE(port->gop_id);
4785 void __iomem *xpcs = priv->iface_base + MVPP22_XPCS_BASE(port->gop_id);
4786 u32 val;
4787
4788 /* XPCS */
4789 val = readl(xpcs + MVPP22_XPCS_CFG0);
4790 val &= ~(MVPP22_XPCS_CFG0_PCS_MODE(0x3) |
4791 MVPP22_XPCS_CFG0_ACTIVE_LANE(0x3));
4792 val |= MVPP22_XPCS_CFG0_ACTIVE_LANE(2);
4793 writel(val, xpcs + MVPP22_XPCS_CFG0);
4794
4795 /* MPCS */
4796 val = readl(mpcs + MVPP22_MPCS_CTRL);
4797 val &= ~MVPP22_MPCS_CTRL_FWD_ERR_CONN;
4798 writel(val, mpcs + MVPP22_MPCS_CTRL);
4799
4800 val = readl(mpcs + MVPP22_MPCS_CLK_RESET);
4801 val &= ~(MVPP22_MPCS_CLK_RESET_DIV_RATIO(0x7) | MAC_CLK_RESET_MAC |
4802 MAC_CLK_RESET_SD_RX | MAC_CLK_RESET_SD_TX);
4803 val |= MVPP22_MPCS_CLK_RESET_DIV_RATIO(1);
4804 writel(val, mpcs + MVPP22_MPCS_CLK_RESET);
4805
4806 val &= ~MVPP22_MPCS_CLK_RESET_DIV_SET;
4807 val |= MAC_CLK_RESET_MAC | MAC_CLK_RESET_SD_RX | MAC_CLK_RESET_SD_TX;
4808 writel(val, mpcs + MVPP22_MPCS_CLK_RESET);
4809}
4810
4811static int mvpp22_gop_init(struct mvpp2_port *port)
4812{
4813 struct mvpp2 *priv = port->priv;
4814 u32 val;
4815
4816 if (!priv->sysctrl_base)
4817 return 0;
4818
4819 switch (port->phy_interface) {
4820 case PHY_INTERFACE_MODE_RGMII:
4821 case PHY_INTERFACE_MODE_RGMII_ID:
4822 case PHY_INTERFACE_MODE_RGMII_RXID:
4823 case PHY_INTERFACE_MODE_RGMII_TXID:
4824 if (port->gop_id == 0)
4825 goto invalid_conf;
4826 mvpp22_gop_init_rgmii(port);
4827 break;
4828 case PHY_INTERFACE_MODE_SGMII:
4829 mvpp22_gop_init_sgmii(port);
4830 break;
4831 case PHY_INTERFACE_MODE_10GKR:
4832 if (port->gop_id != 0)
4833 goto invalid_conf;
4834 mvpp22_gop_init_10gkr(port);
4835 break;
4836 default:
4837 goto unsupported_conf;
4838 }
4839
4840 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL1, &val);
4841 val |= GENCONF_PORT_CTRL1_RESET(port->gop_id) |
4842 GENCONF_PORT_CTRL1_EN(port->gop_id);
4843 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL1, val);
4844
4845 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
4846 val |= GENCONF_PORT_CTRL0_CLK_DIV_PHASE_CLR;
4847 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
4848
4849 regmap_read(priv->sysctrl_base, GENCONF_SOFT_RESET1, &val);
4850 val |= GENCONF_SOFT_RESET1_GOP;
4851 regmap_write(priv->sysctrl_base, GENCONF_SOFT_RESET1, val);
4852
4853unsupported_conf:
4854 return 0;
4855
4856invalid_conf:
4857 netdev_err(port->dev, "Invalid port configuration\n");
4858 return -EINVAL;
4859}
4860
Antoine Tenartfd3651b2017-09-01 11:04:54 +02004861static void mvpp22_gop_unmask_irq(struct mvpp2_port *port)
4862{
4863 u32 val;
4864
4865 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
4866 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4867 /* Enable the GMAC link status irq for this port */
4868 val = readl(port->base + MVPP22_GMAC_INT_SUM_MASK);
4869 val |= MVPP22_GMAC_INT_SUM_MASK_LINK_STAT;
4870 writel(val, port->base + MVPP22_GMAC_INT_SUM_MASK);
4871 }
4872
4873 if (port->gop_id == 0) {
4874 /* Enable the XLG/GIG irqs for this port */
4875 val = readl(port->base + MVPP22_XLG_EXT_INT_MASK);
4876 if (port->phy_interface == PHY_INTERFACE_MODE_10GKR)
4877 val |= MVPP22_XLG_EXT_INT_MASK_XLG;
4878 else
4879 val |= MVPP22_XLG_EXT_INT_MASK_GIG;
4880 writel(val, port->base + MVPP22_XLG_EXT_INT_MASK);
4881 }
4882}
4883
4884static void mvpp22_gop_mask_irq(struct mvpp2_port *port)
4885{
4886 u32 val;
4887
4888 if (port->gop_id == 0) {
4889 val = readl(port->base + MVPP22_XLG_EXT_INT_MASK);
4890 val &= ~(MVPP22_XLG_EXT_INT_MASK_XLG |
4891 MVPP22_XLG_EXT_INT_MASK_GIG);
4892 writel(val, port->base + MVPP22_XLG_EXT_INT_MASK);
4893 }
4894
4895 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
4896 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4897 val = readl(port->base + MVPP22_GMAC_INT_SUM_MASK);
4898 val &= ~MVPP22_GMAC_INT_SUM_MASK_LINK_STAT;
4899 writel(val, port->base + MVPP22_GMAC_INT_SUM_MASK);
4900 }
4901}
4902
4903static void mvpp22_gop_setup_irq(struct mvpp2_port *port)
4904{
4905 u32 val;
4906
4907 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
4908 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4909 val = readl(port->base + MVPP22_GMAC_INT_MASK);
4910 val |= MVPP22_GMAC_INT_MASK_LINK_STAT;
4911 writel(val, port->base + MVPP22_GMAC_INT_MASK);
4912 }
4913
4914 if (port->gop_id == 0) {
4915 val = readl(port->base + MVPP22_XLG_INT_MASK);
4916 val |= MVPP22_XLG_INT_MASK_LINK;
4917 writel(val, port->base + MVPP22_XLG_INT_MASK);
4918 }
4919
4920 mvpp22_gop_unmask_irq(port);
4921}
4922
Antoine Tenart542897d2017-08-30 10:29:15 +02004923static int mvpp22_comphy_init(struct mvpp2_port *port)
4924{
4925 enum phy_mode mode;
4926 int ret;
4927
4928 if (!port->comphy)
4929 return 0;
4930
4931 switch (port->phy_interface) {
4932 case PHY_INTERFACE_MODE_SGMII:
4933 mode = PHY_MODE_SGMII;
4934 break;
4935 case PHY_INTERFACE_MODE_10GKR:
4936 mode = PHY_MODE_10GKR;
4937 break;
4938 default:
4939 return -EINVAL;
4940 }
4941
4942 ret = phy_set_mode(port->comphy, mode);
4943 if (ret)
4944 return ret;
4945
4946 return phy_power_on(port->comphy);
4947}
4948
Antoine Ténart39193572017-08-22 19:08:24 +02004949static void mvpp2_port_mii_gmac_configure_mode(struct mvpp2_port *port)
4950{
4951 u32 val;
4952
4953 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4954 val = readl(port->base + MVPP22_GMAC_CTRL_4_REG);
4955 val |= MVPP22_CTRL4_SYNC_BYPASS_DIS | MVPP22_CTRL4_DP_CLK_SEL |
4956 MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
4957 val &= ~MVPP22_CTRL4_EXT_PIN_GMII_SEL;
4958 writel(val, port->base + MVPP22_GMAC_CTRL_4_REG);
Antoine Tenart1df22702017-09-01 11:04:52 +02004959 } else if (phy_interface_mode_is_rgmii(port->phy_interface)) {
Antoine Ténart39193572017-08-22 19:08:24 +02004960 val = readl(port->base + MVPP22_GMAC_CTRL_4_REG);
4961 val |= MVPP22_CTRL4_EXT_PIN_GMII_SEL |
4962 MVPP22_CTRL4_SYNC_BYPASS_DIS |
4963 MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
4964 val &= ~MVPP22_CTRL4_DP_CLK_SEL;
4965 writel(val, port->base + MVPP22_GMAC_CTRL_4_REG);
Antoine Ténart39193572017-08-22 19:08:24 +02004966 }
4967
4968 /* The port is connected to a copper PHY */
4969 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4970 val &= ~MVPP2_GMAC_PORT_TYPE_MASK;
4971 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4972
4973 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4974 val |= MVPP2_GMAC_IN_BAND_AUTONEG_BYPASS |
4975 MVPP2_GMAC_AN_SPEED_EN | MVPP2_GMAC_FLOW_CTRL_AUTONEG |
4976 MVPP2_GMAC_AN_DUPLEX_EN;
4977 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
4978 val |= MVPP2_GMAC_IN_BAND_AUTONEG;
4979 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4980}
4981
4982static void mvpp2_port_mii_gmac_configure(struct mvpp2_port *port)
4983{
4984 u32 val;
4985
4986 /* Force link down */
4987 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4988 val &= ~MVPP2_GMAC_FORCE_LINK_PASS;
4989 val |= MVPP2_GMAC_FORCE_LINK_DOWN;
4990 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4991
4992 /* Set the GMAC in a reset state */
4993 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4994 val |= MVPP2_GMAC_PORT_RESET_MASK;
4995 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4996
4997 /* Configure the PCS and in-band AN */
4998 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4999 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
5000 val |= MVPP2_GMAC_INBAND_AN_MASK | MVPP2_GMAC_PCS_ENABLE_MASK;
Antoine Tenart1df22702017-09-01 11:04:52 +02005001 } else if (phy_interface_mode_is_rgmii(port->phy_interface)) {
Antoine Ténart39193572017-08-22 19:08:24 +02005002 val &= ~MVPP2_GMAC_PCS_ENABLE_MASK;
Antoine Ténart39193572017-08-22 19:08:24 +02005003 }
5004 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
5005
5006 mvpp2_port_mii_gmac_configure_mode(port);
5007
5008 /* Unset the GMAC reset state */
5009 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
5010 val &= ~MVPP2_GMAC_PORT_RESET_MASK;
5011 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
5012
5013 /* Stop forcing link down */
5014 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5015 val &= ~MVPP2_GMAC_FORCE_LINK_DOWN;
5016 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5017}
5018
Antoine Ténart77321952017-08-22 19:08:25 +02005019static void mvpp2_port_mii_xlg_configure(struct mvpp2_port *port)
5020{
5021 u32 val;
5022
5023 if (port->gop_id != 0)
5024 return;
5025
5026 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
5027 val |= MVPP22_XLG_CTRL0_RX_FLOW_CTRL_EN;
5028 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
5029
5030 val = readl(port->base + MVPP22_XLG_CTRL4_REG);
5031 val &= ~MVPP22_XLG_CTRL4_MACMODSELECT_GMAC;
5032 val |= MVPP22_XLG_CTRL4_FWD_FC | MVPP22_XLG_CTRL4_FWD_PFC;
5033 writel(val, port->base + MVPP22_XLG_CTRL4_REG);
5034}
5035
Thomas Petazzoni26975822017-03-07 16:53:14 +01005036static void mvpp22_port_mii_set(struct mvpp2_port *port)
5037{
5038 u32 val;
5039
Thomas Petazzoni26975822017-03-07 16:53:14 +01005040 /* Only GOP port 0 has an XLG MAC */
5041 if (port->gop_id == 0) {
5042 val = readl(port->base + MVPP22_XLG_CTRL3_REG);
5043 val &= ~MVPP22_XLG_CTRL3_MACMODESELECT_MASK;
Antoine Ténart725757a2017-06-12 16:01:39 +02005044
5045 if (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
5046 port->phy_interface == PHY_INTERFACE_MODE_10GKR)
5047 val |= MVPP22_XLG_CTRL3_MACMODESELECT_10G;
5048 else
5049 val |= MVPP22_XLG_CTRL3_MACMODESELECT_GMAC;
5050
Thomas Petazzoni26975822017-03-07 16:53:14 +01005051 writel(val, port->base + MVPP22_XLG_CTRL3_REG);
5052 }
Thomas Petazzoni26975822017-03-07 16:53:14 +01005053}
5054
Marcin Wojtas3f518502014-07-10 16:52:13 -03005055static void mvpp2_port_mii_set(struct mvpp2_port *port)
5056{
Thomas Petazzoni26975822017-03-07 16:53:14 +01005057 if (port->priv->hw_version == MVPP22)
5058 mvpp22_port_mii_set(port);
5059
Antoine Tenart1df22702017-09-01 11:04:52 +02005060 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
Antoine Ténart39193572017-08-22 19:08:24 +02005061 port->phy_interface == PHY_INTERFACE_MODE_SGMII)
5062 mvpp2_port_mii_gmac_configure(port);
Antoine Ténart77321952017-08-22 19:08:25 +02005063 else if (port->phy_interface == PHY_INTERFACE_MODE_10GKR)
5064 mvpp2_port_mii_xlg_configure(port);
Marcin Wojtas08a23752014-07-21 13:48:12 -03005065}
5066
5067static void mvpp2_port_fc_adv_enable(struct mvpp2_port *port)
5068{
5069 u32 val;
5070
5071 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5072 val |= MVPP2_GMAC_FC_ADV_EN;
5073 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005074}
5075
5076static void mvpp2_port_enable(struct mvpp2_port *port)
5077{
5078 u32 val;
5079
Antoine Ténart725757a2017-06-12 16:01:39 +02005080 /* Only GOP port 0 has an XLG MAC */
5081 if (port->gop_id == 0 &&
5082 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
5083 port->phy_interface == PHY_INTERFACE_MODE_10GKR)) {
5084 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
5085 val |= MVPP22_XLG_CTRL0_PORT_EN |
5086 MVPP22_XLG_CTRL0_MAC_RESET_DIS;
5087 val &= ~MVPP22_XLG_CTRL0_MIB_CNT_DIS;
5088 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
5089 } else {
5090 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
5091 val |= MVPP2_GMAC_PORT_EN_MASK;
5092 val |= MVPP2_GMAC_MIB_CNTR_EN_MASK;
5093 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
5094 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03005095}
5096
5097static void mvpp2_port_disable(struct mvpp2_port *port)
5098{
5099 u32 val;
5100
Antoine Ténart725757a2017-06-12 16:01:39 +02005101 /* Only GOP port 0 has an XLG MAC */
5102 if (port->gop_id == 0 &&
5103 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
5104 port->phy_interface == PHY_INTERFACE_MODE_10GKR)) {
5105 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
5106 val &= ~(MVPP22_XLG_CTRL0_PORT_EN |
5107 MVPP22_XLG_CTRL0_MAC_RESET_DIS);
5108 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
5109 } else {
5110 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
5111 val &= ~(MVPP2_GMAC_PORT_EN_MASK);
5112 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
5113 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03005114}
5115
5116/* Set IEEE 802.3x Flow Control Xon Packet Transmission Mode */
5117static void mvpp2_port_periodic_xon_disable(struct mvpp2_port *port)
5118{
5119 u32 val;
5120
5121 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG) &
5122 ~MVPP2_GMAC_PERIODIC_XON_EN_MASK;
5123 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
5124}
5125
5126/* Configure loopback port */
5127static void mvpp2_port_loopback_set(struct mvpp2_port *port)
5128{
5129 u32 val;
5130
5131 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
5132
5133 if (port->speed == 1000)
5134 val |= MVPP2_GMAC_GMII_LB_EN_MASK;
5135 else
5136 val &= ~MVPP2_GMAC_GMII_LB_EN_MASK;
5137
5138 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
5139 val |= MVPP2_GMAC_PCS_LB_EN_MASK;
5140 else
5141 val &= ~MVPP2_GMAC_PCS_LB_EN_MASK;
5142
5143 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
5144}
5145
Miquel Raynal118d6292017-11-06 22:56:53 +01005146struct mvpp2_ethtool_counter {
5147 unsigned int offset;
5148 const char string[ETH_GSTRING_LEN];
5149 bool reg_is_64b;
5150};
5151
5152static u64 mvpp2_read_count(struct mvpp2_port *port,
5153 const struct mvpp2_ethtool_counter *counter)
5154{
5155 u64 val;
5156
5157 val = readl(port->stats_base + counter->offset);
5158 if (counter->reg_is_64b)
5159 val += (u64)readl(port->stats_base + counter->offset + 4) << 32;
5160
5161 return val;
5162}
5163
5164/* Due to the fact that software statistics and hardware statistics are, by
5165 * design, incremented at different moments in the chain of packet processing,
5166 * it is very likely that incoming packets could have been dropped after being
5167 * counted by hardware but before reaching software statistics (most probably
5168 * multicast packets), and in the oppposite way, during transmission, FCS bytes
5169 * are added in between as well as TSO skb will be split and header bytes added.
5170 * Hence, statistics gathered from userspace with ifconfig (software) and
5171 * ethtool (hardware) cannot be compared.
5172 */
5173static const struct mvpp2_ethtool_counter mvpp2_ethtool_regs[] = {
5174 { MVPP2_MIB_GOOD_OCTETS_RCVD, "good_octets_received", true },
5175 { MVPP2_MIB_BAD_OCTETS_RCVD, "bad_octets_received" },
5176 { MVPP2_MIB_CRC_ERRORS_SENT, "crc_errors_sent" },
5177 { MVPP2_MIB_UNICAST_FRAMES_RCVD, "unicast_frames_received" },
5178 { MVPP2_MIB_BROADCAST_FRAMES_RCVD, "broadcast_frames_received" },
5179 { MVPP2_MIB_MULTICAST_FRAMES_RCVD, "multicast_frames_received" },
5180 { MVPP2_MIB_FRAMES_64_OCTETS, "frames_64_octets" },
5181 { MVPP2_MIB_FRAMES_65_TO_127_OCTETS, "frames_65_to_127_octet" },
5182 { MVPP2_MIB_FRAMES_128_TO_255_OCTETS, "frames_128_to_255_octet" },
5183 { MVPP2_MIB_FRAMES_256_TO_511_OCTETS, "frames_256_to_511_octet" },
5184 { MVPP2_MIB_FRAMES_512_TO_1023_OCTETS, "frames_512_to_1023_octet" },
5185 { MVPP2_MIB_FRAMES_1024_TO_MAX_OCTETS, "frames_1024_to_max_octet" },
5186 { MVPP2_MIB_GOOD_OCTETS_SENT, "good_octets_sent", true },
5187 { MVPP2_MIB_UNICAST_FRAMES_SENT, "unicast_frames_sent" },
5188 { MVPP2_MIB_MULTICAST_FRAMES_SENT, "multicast_frames_sent" },
5189 { MVPP2_MIB_BROADCAST_FRAMES_SENT, "broadcast_frames_sent" },
5190 { MVPP2_MIB_FC_SENT, "fc_sent" },
5191 { MVPP2_MIB_FC_RCVD, "fc_received" },
5192 { MVPP2_MIB_RX_FIFO_OVERRUN, "rx_fifo_overrun" },
5193 { MVPP2_MIB_UNDERSIZE_RCVD, "undersize_received" },
5194 { MVPP2_MIB_FRAGMENTS_RCVD, "fragments_received" },
5195 { MVPP2_MIB_OVERSIZE_RCVD, "oversize_received" },
5196 { MVPP2_MIB_JABBER_RCVD, "jabber_received" },
5197 { MVPP2_MIB_MAC_RCV_ERROR, "mac_receive_error" },
5198 { MVPP2_MIB_BAD_CRC_EVENT, "bad_crc_event" },
5199 { MVPP2_MIB_COLLISION, "collision" },
5200 { MVPP2_MIB_LATE_COLLISION, "late_collision" },
5201};
5202
5203static void mvpp2_ethtool_get_strings(struct net_device *netdev, u32 sset,
5204 u8 *data)
5205{
5206 if (sset == ETH_SS_STATS) {
5207 int i;
5208
5209 for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_regs); i++)
5210 memcpy(data + i * ETH_GSTRING_LEN,
5211 &mvpp2_ethtool_regs[i].string, ETH_GSTRING_LEN);
5212 }
5213}
5214
5215static void mvpp2_gather_hw_statistics(struct work_struct *work)
5216{
5217 struct delayed_work *del_work = to_delayed_work(work);
Miquel Raynale5c500e2017-11-08 08:59:40 +01005218 struct mvpp2_port *port = container_of(del_work, struct mvpp2_port,
5219 stats_work);
Miquel Raynal118d6292017-11-06 22:56:53 +01005220 u64 *pstats;
Miquel Raynale5c500e2017-11-08 08:59:40 +01005221 int i;
Miquel Raynal118d6292017-11-06 22:56:53 +01005222
Miquel Raynale5c500e2017-11-08 08:59:40 +01005223 mutex_lock(&port->gather_stats_lock);
Miquel Raynal118d6292017-11-06 22:56:53 +01005224
Miquel Raynale5c500e2017-11-08 08:59:40 +01005225 pstats = port->ethtool_stats;
5226 for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_regs); i++)
5227 *pstats++ += mvpp2_read_count(port, &mvpp2_ethtool_regs[i]);
Miquel Raynal118d6292017-11-06 22:56:53 +01005228
5229 /* No need to read again the counters right after this function if it
5230 * was called asynchronously by the user (ie. use of ethtool).
5231 */
Miquel Raynale5c500e2017-11-08 08:59:40 +01005232 cancel_delayed_work(&port->stats_work);
5233 queue_delayed_work(port->priv->stats_queue, &port->stats_work,
Miquel Raynal118d6292017-11-06 22:56:53 +01005234 MVPP2_MIB_COUNTERS_STATS_DELAY);
5235
Miquel Raynale5c500e2017-11-08 08:59:40 +01005236 mutex_unlock(&port->gather_stats_lock);
Miquel Raynal118d6292017-11-06 22:56:53 +01005237}
5238
5239static void mvpp2_ethtool_get_stats(struct net_device *dev,
5240 struct ethtool_stats *stats, u64 *data)
5241{
5242 struct mvpp2_port *port = netdev_priv(dev);
5243
Miquel Raynale5c500e2017-11-08 08:59:40 +01005244 /* Update statistics for the given port, then take the lock to avoid
5245 * concurrent accesses on the ethtool_stats structure during its copy.
5246 */
5247 mvpp2_gather_hw_statistics(&port->stats_work.work);
Miquel Raynal118d6292017-11-06 22:56:53 +01005248
Miquel Raynale5c500e2017-11-08 08:59:40 +01005249 mutex_lock(&port->gather_stats_lock);
Miquel Raynal118d6292017-11-06 22:56:53 +01005250 memcpy(data, port->ethtool_stats,
5251 sizeof(u64) * ARRAY_SIZE(mvpp2_ethtool_regs));
Miquel Raynale5c500e2017-11-08 08:59:40 +01005252 mutex_unlock(&port->gather_stats_lock);
Miquel Raynal118d6292017-11-06 22:56:53 +01005253}
5254
5255static int mvpp2_ethtool_get_sset_count(struct net_device *dev, int sset)
5256{
5257 if (sset == ETH_SS_STATS)
5258 return ARRAY_SIZE(mvpp2_ethtool_regs);
5259
5260 return -EOPNOTSUPP;
5261}
5262
Marcin Wojtas3f518502014-07-10 16:52:13 -03005263static void mvpp2_port_reset(struct mvpp2_port *port)
5264{
5265 u32 val;
Miquel Raynal118d6292017-11-06 22:56:53 +01005266 unsigned int i;
5267
5268 /* Read the GOP statistics to reset the hardware counters */
5269 for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_regs); i++)
5270 mvpp2_read_count(port, &mvpp2_ethtool_regs[i]);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005271
5272 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
5273 ~MVPP2_GMAC_PORT_RESET_MASK;
5274 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
5275
5276 while (readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
5277 MVPP2_GMAC_PORT_RESET_MASK)
5278 continue;
5279}
5280
5281/* Change maximum receive size of the port */
5282static inline void mvpp2_gmac_max_rx_size_set(struct mvpp2_port *port)
5283{
5284 u32 val;
5285
5286 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
5287 val &= ~MVPP2_GMAC_MAX_RX_SIZE_MASK;
5288 val |= (((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
5289 MVPP2_GMAC_MAX_RX_SIZE_OFFS);
5290 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
5291}
5292
Stefan Chulski76eb1b12017-08-22 19:08:26 +02005293/* Change maximum receive size of the port */
5294static inline void mvpp2_xlg_max_rx_size_set(struct mvpp2_port *port)
5295{
5296 u32 val;
5297
5298 val = readl(port->base + MVPP22_XLG_CTRL1_REG);
5299 val &= ~MVPP22_XLG_CTRL1_FRAMESIZELIMIT_MASK;
5300 val |= ((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
Antoine Ténartec15ecd2017-08-25 15:24:46 +02005301 MVPP22_XLG_CTRL1_FRAMESIZELIMIT_OFFS;
Stefan Chulski76eb1b12017-08-22 19:08:26 +02005302 writel(val, port->base + MVPP22_XLG_CTRL1_REG);
5303}
5304
Marcin Wojtas3f518502014-07-10 16:52:13 -03005305/* Set defaults to the MVPP2 port */
5306static void mvpp2_defaults_set(struct mvpp2_port *port)
5307{
5308 int tx_port_num, val, queue, ptxq, lrxq;
5309
Thomas Petazzoni3d9017d2017-03-07 16:53:11 +01005310 if (port->priv->hw_version == MVPP21) {
5311 /* Configure port to loopback if needed */
5312 if (port->flags & MVPP2_F_LOOPBACK)
5313 mvpp2_port_loopback_set(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005314
Thomas Petazzoni3d9017d2017-03-07 16:53:11 +01005315 /* Update TX FIFO MIN Threshold */
5316 val = readl(port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
5317 val &= ~MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK;
5318 /* Min. TX threshold must be less than minimal packet length */
5319 val |= MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(64 - 4 - 2);
5320 writel(val, port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
5321 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03005322
5323 /* Disable Legacy WRR, Disable EJP, Release from reset */
5324 tx_port_num = mvpp2_egress_port(port);
5325 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG,
5326 tx_port_num);
5327 mvpp2_write(port->priv, MVPP2_TXP_SCHED_CMD_1_REG, 0);
5328
5329 /* Close bandwidth for all queues */
5330 for (queue = 0; queue < MVPP2_MAX_TXQ; queue++) {
5331 ptxq = mvpp2_txq_phys(port->id, queue);
5332 mvpp2_write(port->priv,
5333 MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(ptxq), 0);
5334 }
5335
5336 /* Set refill period to 1 usec, refill tokens
5337 * and bucket size to maximum
5338 */
5339 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PERIOD_REG,
5340 port->priv->tclk / USEC_PER_SEC);
5341 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_REFILL_REG);
5342 val &= ~MVPP2_TXP_REFILL_PERIOD_ALL_MASK;
5343 val |= MVPP2_TXP_REFILL_PERIOD_MASK(1);
5344 val |= MVPP2_TXP_REFILL_TOKENS_ALL_MASK;
5345 mvpp2_write(port->priv, MVPP2_TXP_SCHED_REFILL_REG, val);
5346 val = MVPP2_TXP_TOKEN_SIZE_MAX;
5347 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
5348
5349 /* Set MaximumLowLatencyPacketSize value to 256 */
5350 mvpp2_write(port->priv, MVPP2_RX_CTRL_REG(port->id),
5351 MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK |
5352 MVPP2_RX_LOW_LATENCY_PKT_SIZE(256));
5353
5354 /* Enable Rx cache snoop */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005355 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005356 queue = port->rxqs[lrxq]->id;
5357 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
5358 val |= MVPP2_SNOOP_PKT_SIZE_MASK |
5359 MVPP2_SNOOP_BUF_HDR_MASK;
5360 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
5361 }
5362
5363 /* At default, mask all interrupts to all present cpus */
5364 mvpp2_interrupts_disable(port);
5365}
5366
5367/* Enable/disable receiving packets */
5368static void mvpp2_ingress_enable(struct mvpp2_port *port)
5369{
5370 u32 val;
5371 int lrxq, queue;
5372
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005373 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005374 queue = port->rxqs[lrxq]->id;
5375 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
5376 val &= ~MVPP2_RXQ_DISABLE_MASK;
5377 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
5378 }
5379}
5380
5381static void mvpp2_ingress_disable(struct mvpp2_port *port)
5382{
5383 u32 val;
5384 int lrxq, queue;
5385
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005386 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005387 queue = port->rxqs[lrxq]->id;
5388 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
5389 val |= MVPP2_RXQ_DISABLE_MASK;
5390 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
5391 }
5392}
5393
5394/* Enable transmit via physical egress queue
5395 * - HW starts take descriptors from DRAM
5396 */
5397static void mvpp2_egress_enable(struct mvpp2_port *port)
5398{
5399 u32 qmap;
5400 int queue;
5401 int tx_port_num = mvpp2_egress_port(port);
5402
5403 /* Enable all initialized TXs. */
5404 qmap = 0;
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005405 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005406 struct mvpp2_tx_queue *txq = port->txqs[queue];
5407
Markus Elfringdbbb2f02017-04-17 14:07:52 +02005408 if (txq->descs)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005409 qmap |= (1 << queue);
5410 }
5411
5412 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
5413 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG, qmap);
5414}
5415
5416/* Disable transmit via physical egress queue
5417 * - HW doesn't take descriptors from DRAM
5418 */
5419static void mvpp2_egress_disable(struct mvpp2_port *port)
5420{
5421 u32 reg_data;
5422 int delay;
5423 int tx_port_num = mvpp2_egress_port(port);
5424
5425 /* Issue stop command for active channels only */
5426 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
5427 reg_data = (mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG)) &
5428 MVPP2_TXP_SCHED_ENQ_MASK;
5429 if (reg_data != 0)
5430 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG,
5431 (reg_data << MVPP2_TXP_SCHED_DISQ_OFFSET));
5432
5433 /* Wait for all Tx activity to terminate. */
5434 delay = 0;
5435 do {
5436 if (delay >= MVPP2_TX_DISABLE_TIMEOUT_MSEC) {
5437 netdev_warn(port->dev,
5438 "Tx stop timed out, status=0x%08x\n",
5439 reg_data);
5440 break;
5441 }
5442 mdelay(1);
5443 delay++;
5444
5445 /* Check port TX Command register that all
5446 * Tx queues are stopped
5447 */
5448 reg_data = mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG);
5449 } while (reg_data & MVPP2_TXP_SCHED_ENQ_MASK);
5450}
5451
5452/* Rx descriptors helper methods */
5453
5454/* Get number of Rx descriptors occupied by received packets */
5455static inline int
5456mvpp2_rxq_received(struct mvpp2_port *port, int rxq_id)
5457{
5458 u32 val = mvpp2_read(port->priv, MVPP2_RXQ_STATUS_REG(rxq_id));
5459
5460 return val & MVPP2_RXQ_OCCUPIED_MASK;
5461}
5462
5463/* Update Rx queue status with the number of occupied and available
5464 * Rx descriptor slots.
5465 */
5466static inline void
5467mvpp2_rxq_status_update(struct mvpp2_port *port, int rxq_id,
5468 int used_count, int free_count)
5469{
5470 /* Decrement the number of used descriptors and increment count
5471 * increment the number of free descriptors.
5472 */
5473 u32 val = used_count | (free_count << MVPP2_RXQ_NUM_NEW_OFFSET);
5474
5475 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_UPDATE_REG(rxq_id), val);
5476}
5477
5478/* Get pointer to next RX descriptor to be processed by SW */
5479static inline struct mvpp2_rx_desc *
5480mvpp2_rxq_next_desc_get(struct mvpp2_rx_queue *rxq)
5481{
5482 int rx_desc = rxq->next_desc_to_proc;
5483
5484 rxq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(rxq, rx_desc);
5485 prefetch(rxq->descs + rxq->next_desc_to_proc);
5486 return rxq->descs + rx_desc;
5487}
5488
5489/* Set rx queue offset */
5490static void mvpp2_rxq_offset_set(struct mvpp2_port *port,
5491 int prxq, int offset)
5492{
5493 u32 val;
5494
5495 /* Convert offset from bytes to units of 32 bytes */
5496 offset = offset >> 5;
5497
5498 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
5499 val &= ~MVPP2_RXQ_PACKET_OFFSET_MASK;
5500
5501 /* Offset is in */
5502 val |= ((offset << MVPP2_RXQ_PACKET_OFFSET_OFFS) &
5503 MVPP2_RXQ_PACKET_OFFSET_MASK);
5504
5505 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
5506}
5507
Marcin Wojtas3f518502014-07-10 16:52:13 -03005508/* Tx descriptors helper methods */
5509
Marcin Wojtas3f518502014-07-10 16:52:13 -03005510/* Get pointer to next Tx descriptor to be processed (send) by HW */
5511static struct mvpp2_tx_desc *
5512mvpp2_txq_next_desc_get(struct mvpp2_tx_queue *txq)
5513{
5514 int tx_desc = txq->next_desc_to_proc;
5515
5516 txq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(txq, tx_desc);
5517 return txq->descs + tx_desc;
5518}
5519
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005520/* Update HW with number of aggregated Tx descriptors to be sent
5521 *
5522 * Called only from mvpp2_tx(), so migration is disabled, using
5523 * smp_processor_id() is OK.
5524 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03005525static void mvpp2_aggr_txq_pend_desc_add(struct mvpp2_port *port, int pending)
5526{
5527 /* aggregated access - relevant TXQ number is written in TX desc */
Thomas Petazzonia7868412017-03-07 16:53:13 +01005528 mvpp2_percpu_write(port->priv, smp_processor_id(),
5529 MVPP2_AGGR_TXQ_UPDATE_REG, pending);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005530}
5531
5532
5533/* Check if there are enough free descriptors in aggregated txq.
5534 * If not, update the number of occupied descriptors and repeat the check.
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005535 *
5536 * Called only from mvpp2_tx(), so migration is disabled, using
5537 * smp_processor_id() is OK.
Marcin Wojtas3f518502014-07-10 16:52:13 -03005538 */
5539static int mvpp2_aggr_desc_num_check(struct mvpp2 *priv,
5540 struct mvpp2_tx_queue *aggr_txq, int num)
5541{
Antoine Tenart02856a32017-10-30 11:23:32 +01005542 if ((aggr_txq->count + num) > MVPP2_AGGR_TXQ_SIZE) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005543 /* Update number of occupied aggregated Tx descriptors */
5544 int cpu = smp_processor_id();
5545 u32 val = mvpp2_read(priv, MVPP2_AGGR_TXQ_STATUS_REG(cpu));
5546
5547 aggr_txq->count = val & MVPP2_AGGR_TXQ_PENDING_MASK;
5548 }
5549
Antoine Tenart02856a32017-10-30 11:23:32 +01005550 if ((aggr_txq->count + num) > MVPP2_AGGR_TXQ_SIZE)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005551 return -ENOMEM;
5552
5553 return 0;
5554}
5555
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005556/* Reserved Tx descriptors allocation request
5557 *
5558 * Called only from mvpp2_txq_reserved_desc_num_proc(), itself called
5559 * only by mvpp2_tx(), so migration is disabled, using
5560 * smp_processor_id() is OK.
5561 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03005562static int mvpp2_txq_alloc_reserved_desc(struct mvpp2 *priv,
5563 struct mvpp2_tx_queue *txq, int num)
5564{
5565 u32 val;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005566 int cpu = smp_processor_id();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005567
5568 val = (txq->id << MVPP2_TXQ_RSVD_REQ_Q_OFFSET) | num;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005569 mvpp2_percpu_write(priv, cpu, MVPP2_TXQ_RSVD_REQ_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005570
Thomas Petazzonia7868412017-03-07 16:53:13 +01005571 val = mvpp2_percpu_read(priv, cpu, MVPP2_TXQ_RSVD_RSLT_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005572
5573 return val & MVPP2_TXQ_RSVD_RSLT_MASK;
5574}
5575
5576/* Check if there are enough reserved descriptors for transmission.
5577 * If not, request chunk of reserved descriptors and check again.
5578 */
5579static int mvpp2_txq_reserved_desc_num_proc(struct mvpp2 *priv,
5580 struct mvpp2_tx_queue *txq,
5581 struct mvpp2_txq_pcpu *txq_pcpu,
5582 int num)
5583{
5584 int req, cpu, desc_count;
5585
5586 if (txq_pcpu->reserved_num >= num)
5587 return 0;
5588
5589 /* Not enough descriptors reserved! Update the reserved descriptor
5590 * count and check again.
5591 */
5592
5593 desc_count = 0;
5594 /* Compute total of used descriptors */
5595 for_each_present_cpu(cpu) {
5596 struct mvpp2_txq_pcpu *txq_pcpu_aux;
5597
5598 txq_pcpu_aux = per_cpu_ptr(txq->pcpu, cpu);
5599 desc_count += txq_pcpu_aux->count;
5600 desc_count += txq_pcpu_aux->reserved_num;
5601 }
5602
5603 req = max(MVPP2_CPU_DESC_CHUNK, num - txq_pcpu->reserved_num);
5604 desc_count += req;
5605
5606 if (desc_count >
5607 (txq->size - (num_present_cpus() * MVPP2_CPU_DESC_CHUNK)))
5608 return -ENOMEM;
5609
5610 txq_pcpu->reserved_num += mvpp2_txq_alloc_reserved_desc(priv, txq, req);
5611
5612 /* OK, the descriptor cound has been updated: check again. */
5613 if (txq_pcpu->reserved_num < num)
5614 return -ENOMEM;
5615 return 0;
5616}
5617
5618/* Release the last allocated Tx descriptor. Useful to handle DMA
5619 * mapping failures in the Tx path.
5620 */
5621static void mvpp2_txq_desc_put(struct mvpp2_tx_queue *txq)
5622{
5623 if (txq->next_desc_to_proc == 0)
5624 txq->next_desc_to_proc = txq->last_desc - 1;
5625 else
5626 txq->next_desc_to_proc--;
5627}
5628
5629/* Set Tx descriptors fields relevant for CSUM calculation */
5630static u32 mvpp2_txq_desc_csum(int l3_offs, int l3_proto,
5631 int ip_hdr_len, int l4_proto)
5632{
5633 u32 command;
5634
5635 /* fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
5636 * G_L4_chk, L4_type required only for checksum calculation
5637 */
5638 command = (l3_offs << MVPP2_TXD_L3_OFF_SHIFT);
5639 command |= (ip_hdr_len << MVPP2_TXD_IP_HLEN_SHIFT);
5640 command |= MVPP2_TXD_IP_CSUM_DISABLE;
5641
5642 if (l3_proto == swab16(ETH_P_IP)) {
5643 command &= ~MVPP2_TXD_IP_CSUM_DISABLE; /* enable IPv4 csum */
5644 command &= ~MVPP2_TXD_L3_IP6; /* enable IPv4 */
5645 } else {
5646 command |= MVPP2_TXD_L3_IP6; /* enable IPv6 */
5647 }
5648
5649 if (l4_proto == IPPROTO_TCP) {
5650 command &= ~MVPP2_TXD_L4_UDP; /* enable TCP */
5651 command &= ~MVPP2_TXD_L4_CSUM_FRAG; /* generate L4 csum */
5652 } else if (l4_proto == IPPROTO_UDP) {
5653 command |= MVPP2_TXD_L4_UDP; /* enable UDP */
5654 command &= ~MVPP2_TXD_L4_CSUM_FRAG; /* generate L4 csum */
5655 } else {
5656 command |= MVPP2_TXD_L4_CSUM_NOT;
5657 }
5658
5659 return command;
5660}
5661
5662/* Get number of sent descriptors and decrement counter.
5663 * The number of sent descriptors is returned.
5664 * Per-CPU access
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005665 *
5666 * Called only from mvpp2_txq_done(), called from mvpp2_tx()
5667 * (migration disabled) and from the TX completion tasklet (migration
5668 * disabled) so using smp_processor_id() is OK.
Marcin Wojtas3f518502014-07-10 16:52:13 -03005669 */
5670static inline int mvpp2_txq_sent_desc_proc(struct mvpp2_port *port,
5671 struct mvpp2_tx_queue *txq)
5672{
5673 u32 val;
5674
5675 /* Reading status reg resets transmitted descriptor counter */
Thomas Petazzonia7868412017-03-07 16:53:13 +01005676 val = mvpp2_percpu_read(port->priv, smp_processor_id(),
5677 MVPP2_TXQ_SENT_REG(txq->id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005678
5679 return (val & MVPP2_TRANSMITTED_COUNT_MASK) >>
5680 MVPP2_TRANSMITTED_COUNT_OFFSET;
5681}
5682
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005683/* Called through on_each_cpu(), so runs on all CPUs, with migration
5684 * disabled, therefore using smp_processor_id() is OK.
5685 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03005686static void mvpp2_txq_sent_counter_clear(void *arg)
5687{
5688 struct mvpp2_port *port = arg;
5689 int queue;
5690
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005691 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005692 int id = port->txqs[queue]->id;
5693
Thomas Petazzonia7868412017-03-07 16:53:13 +01005694 mvpp2_percpu_read(port->priv, smp_processor_id(),
5695 MVPP2_TXQ_SENT_REG(id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005696 }
5697}
5698
5699/* Set max sizes for Tx queues */
5700static void mvpp2_txp_max_tx_size_set(struct mvpp2_port *port)
5701{
5702 u32 val, size, mtu;
5703 int txq, tx_port_num;
5704
5705 mtu = port->pkt_size * 8;
5706 if (mtu > MVPP2_TXP_MTU_MAX)
5707 mtu = MVPP2_TXP_MTU_MAX;
5708
5709 /* WA for wrong Token bucket update: Set MTU value = 3*real MTU value */
5710 mtu = 3 * mtu;
5711
5712 /* Indirect access to registers */
5713 tx_port_num = mvpp2_egress_port(port);
5714 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
5715
5716 /* Set MTU */
5717 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_MTU_REG);
5718 val &= ~MVPP2_TXP_MTU_MAX;
5719 val |= mtu;
5720 mvpp2_write(port->priv, MVPP2_TXP_SCHED_MTU_REG, val);
5721
5722 /* TXP token size and all TXQs token size must be larger that MTU */
5723 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG);
5724 size = val & MVPP2_TXP_TOKEN_SIZE_MAX;
5725 if (size < mtu) {
5726 size = mtu;
5727 val &= ~MVPP2_TXP_TOKEN_SIZE_MAX;
5728 val |= size;
5729 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
5730 }
5731
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005732 for (txq = 0; txq < port->ntxqs; txq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005733 val = mvpp2_read(port->priv,
5734 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq));
5735 size = val & MVPP2_TXQ_TOKEN_SIZE_MAX;
5736
5737 if (size < mtu) {
5738 size = mtu;
5739 val &= ~MVPP2_TXQ_TOKEN_SIZE_MAX;
5740 val |= size;
5741 mvpp2_write(port->priv,
5742 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq),
5743 val);
5744 }
5745 }
5746}
5747
5748/* Set the number of packets that will be received before Rx interrupt
5749 * will be generated by HW.
5750 */
5751static void mvpp2_rx_pkts_coal_set(struct mvpp2_port *port,
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01005752 struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005753{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005754 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005755
Thomas Petazzonif8b0d5f2017-02-21 11:28:03 +01005756 if (rxq->pkts_coal > MVPP2_OCCUPIED_THRESH_MASK)
5757 rxq->pkts_coal = MVPP2_OCCUPIED_THRESH_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005758
Thomas Petazzonia7868412017-03-07 16:53:13 +01005759 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
5760 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_THRESH_REG,
5761 rxq->pkts_coal);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005762
5763 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005764}
5765
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005766/* For some reason in the LSP this is done on each CPU. Why ? */
5767static void mvpp2_tx_pkts_coal_set(struct mvpp2_port *port,
5768 struct mvpp2_tx_queue *txq)
5769{
5770 int cpu = get_cpu();
5771 u32 val;
5772
5773 if (txq->done_pkts_coal > MVPP2_TXQ_THRESH_MASK)
5774 txq->done_pkts_coal = MVPP2_TXQ_THRESH_MASK;
5775
5776 val = (txq->done_pkts_coal << MVPP2_TXQ_THRESH_OFFSET);
5777 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5778 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_THRESH_REG, val);
5779
5780 put_cpu();
5781}
5782
Thomas Petazzoniab426762017-02-21 11:28:04 +01005783static u32 mvpp2_usec_to_cycles(u32 usec, unsigned long clk_hz)
5784{
5785 u64 tmp = (u64)clk_hz * usec;
5786
5787 do_div(tmp, USEC_PER_SEC);
5788
5789 return tmp > U32_MAX ? U32_MAX : tmp;
5790}
5791
5792static u32 mvpp2_cycles_to_usec(u32 cycles, unsigned long clk_hz)
5793{
5794 u64 tmp = (u64)cycles * USEC_PER_SEC;
5795
5796 do_div(tmp, clk_hz);
5797
5798 return tmp > U32_MAX ? U32_MAX : tmp;
5799}
5800
Marcin Wojtas3f518502014-07-10 16:52:13 -03005801/* Set the time delay in usec before Rx interrupt */
5802static void mvpp2_rx_time_coal_set(struct mvpp2_port *port,
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01005803 struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005804{
Thomas Petazzoniab426762017-02-21 11:28:04 +01005805 unsigned long freq = port->priv->tclk;
5806 u32 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005807
Thomas Petazzoniab426762017-02-21 11:28:04 +01005808 if (val > MVPP2_MAX_ISR_RX_THRESHOLD) {
5809 rxq->time_coal =
5810 mvpp2_cycles_to_usec(MVPP2_MAX_ISR_RX_THRESHOLD, freq);
5811
5812 /* re-evaluate to get actual register value */
5813 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
5814 }
5815
Marcin Wojtas3f518502014-07-10 16:52:13 -03005816 mvpp2_write(port->priv, MVPP2_ISR_RX_THRESHOLD_REG(rxq->id), val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005817}
5818
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005819static void mvpp2_tx_time_coal_set(struct mvpp2_port *port)
5820{
5821 unsigned long freq = port->priv->tclk;
5822 u32 val = mvpp2_usec_to_cycles(port->tx_time_coal, freq);
5823
5824 if (val > MVPP2_MAX_ISR_TX_THRESHOLD) {
5825 port->tx_time_coal =
5826 mvpp2_cycles_to_usec(MVPP2_MAX_ISR_TX_THRESHOLD, freq);
5827
5828 /* re-evaluate to get actual register value */
5829 val = mvpp2_usec_to_cycles(port->tx_time_coal, freq);
5830 }
5831
5832 mvpp2_write(port->priv, MVPP2_ISR_TX_THRESHOLD_REG(port->id), val);
5833}
5834
Marcin Wojtas3f518502014-07-10 16:52:13 -03005835/* Free Tx queue skbuffs */
5836static void mvpp2_txq_bufs_free(struct mvpp2_port *port,
5837 struct mvpp2_tx_queue *txq,
5838 struct mvpp2_txq_pcpu *txq_pcpu, int num)
5839{
5840 int i;
5841
5842 for (i = 0; i < num; i++) {
Thomas Petazzoni83544912016-12-21 11:28:49 +01005843 struct mvpp2_txq_pcpu_buf *tx_buf =
5844 txq_pcpu->buffs + txq_pcpu->txq_get_index;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005845
Antoine Tenart20920262017-10-23 15:24:30 +02005846 if (!IS_TSO_HEADER(txq_pcpu, tx_buf->dma))
5847 dma_unmap_single(port->dev->dev.parent, tx_buf->dma,
5848 tx_buf->size, DMA_TO_DEVICE);
Thomas Petazzoni36fb7432017-02-21 11:28:05 +01005849 if (tx_buf->skb)
5850 dev_kfree_skb_any(tx_buf->skb);
5851
5852 mvpp2_txq_inc_get(txq_pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005853 }
5854}
5855
5856static inline struct mvpp2_rx_queue *mvpp2_get_rx_queue(struct mvpp2_port *port,
5857 u32 cause)
5858{
5859 int queue = fls(cause) - 1;
5860
5861 return port->rxqs[queue];
5862}
5863
5864static inline struct mvpp2_tx_queue *mvpp2_get_tx_queue(struct mvpp2_port *port,
5865 u32 cause)
5866{
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005867 int queue = fls(cause) - 1;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005868
5869 return port->txqs[queue];
5870}
5871
5872/* Handle end of transmission */
5873static void mvpp2_txq_done(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
5874 struct mvpp2_txq_pcpu *txq_pcpu)
5875{
5876 struct netdev_queue *nq = netdev_get_tx_queue(port->dev, txq->log_id);
5877 int tx_done;
5878
5879 if (txq_pcpu->cpu != smp_processor_id())
5880 netdev_err(port->dev, "wrong cpu on the end of Tx processing\n");
5881
5882 tx_done = mvpp2_txq_sent_desc_proc(port, txq);
5883 if (!tx_done)
5884 return;
5885 mvpp2_txq_bufs_free(port, txq, txq_pcpu, tx_done);
5886
5887 txq_pcpu->count -= tx_done;
5888
5889 if (netif_tx_queue_stopped(nq))
Antoine Tenart1d17db02017-10-30 11:23:31 +01005890 if (txq_pcpu->count <= txq_pcpu->wake_threshold)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005891 netif_tx_wake_queue(nq);
5892}
5893
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005894static unsigned int mvpp2_tx_done(struct mvpp2_port *port, u32 cause,
5895 int cpu)
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005896{
5897 struct mvpp2_tx_queue *txq;
5898 struct mvpp2_txq_pcpu *txq_pcpu;
5899 unsigned int tx_todo = 0;
5900
5901 while (cause) {
5902 txq = mvpp2_get_tx_queue(port, cause);
5903 if (!txq)
5904 break;
5905
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005906 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005907
5908 if (txq_pcpu->count) {
5909 mvpp2_txq_done(port, txq, txq_pcpu);
5910 tx_todo += txq_pcpu->count;
5911 }
5912
5913 cause &= ~(1 << txq->log_id);
5914 }
5915 return tx_todo;
5916}
5917
Marcin Wojtas3f518502014-07-10 16:52:13 -03005918/* Rx/Tx queue initialization/cleanup methods */
5919
5920/* Allocate and initialize descriptors for aggr TXQ */
5921static int mvpp2_aggr_txq_init(struct platform_device *pdev,
Antoine Ténart85affd72017-08-23 09:46:55 +02005922 struct mvpp2_tx_queue *aggr_txq, int cpu,
Marcin Wojtas3f518502014-07-10 16:52:13 -03005923 struct mvpp2 *priv)
5924{
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005925 u32 txq_dma;
5926
Marcin Wojtas3f518502014-07-10 16:52:13 -03005927 /* Allocate memory for TX descriptors */
Yan Markmana154f8e2017-11-30 10:49:46 +01005928 aggr_txq->descs = dma_zalloc_coherent(&pdev->dev,
Antoine Ténart85affd72017-08-23 09:46:55 +02005929 MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005930 &aggr_txq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005931 if (!aggr_txq->descs)
5932 return -ENOMEM;
5933
Antoine Tenart02856a32017-10-30 11:23:32 +01005934 aggr_txq->last_desc = MVPP2_AGGR_TXQ_SIZE - 1;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005935
5936 /* Aggr TXQ no reset WA */
5937 aggr_txq->next_desc_to_proc = mvpp2_read(priv,
5938 MVPP2_AGGR_TXQ_INDEX_REG(cpu));
5939
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005940 /* Set Tx descriptors queue starting address indirect
5941 * access
5942 */
5943 if (priv->hw_version == MVPP21)
5944 txq_dma = aggr_txq->descs_dma;
5945 else
5946 txq_dma = aggr_txq->descs_dma >>
5947 MVPP22_AGGR_TXQ_DESC_ADDR_OFFS;
5948
5949 mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu), txq_dma);
Antoine Ténart85affd72017-08-23 09:46:55 +02005950 mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu),
5951 MVPP2_AGGR_TXQ_SIZE);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005952
5953 return 0;
5954}
5955
5956/* Create a specified Rx queue */
5957static int mvpp2_rxq_init(struct mvpp2_port *port,
5958 struct mvpp2_rx_queue *rxq)
5959
5960{
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005961 u32 rxq_dma;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005962 int cpu;
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005963
Marcin Wojtas3f518502014-07-10 16:52:13 -03005964 rxq->size = port->rx_ring_size;
5965
5966 /* Allocate memory for RX descriptors */
5967 rxq->descs = dma_alloc_coherent(port->dev->dev.parent,
5968 rxq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005969 &rxq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005970 if (!rxq->descs)
5971 return -ENOMEM;
5972
Marcin Wojtas3f518502014-07-10 16:52:13 -03005973 rxq->last_desc = rxq->size - 1;
5974
5975 /* Zero occupied and non-occupied counters - direct access */
5976 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
5977
5978 /* Set Rx descriptors queue starting address - indirect access */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005979 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005980 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005981 if (port->priv->hw_version == MVPP21)
5982 rxq_dma = rxq->descs_dma;
5983 else
5984 rxq_dma = rxq->descs_dma >> MVPP22_DESC_ADDR_OFFS;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005985 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_ADDR_REG, rxq_dma);
5986 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_SIZE_REG, rxq->size);
5987 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_INDEX_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005988 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005989
5990 /* Set Offset */
5991 mvpp2_rxq_offset_set(port, rxq->id, NET_SKB_PAD);
5992
5993 /* Set coalescing pkts and time */
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01005994 mvpp2_rx_pkts_coal_set(port, rxq);
5995 mvpp2_rx_time_coal_set(port, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005996
5997 /* Add number of descriptors ready for receiving packets */
5998 mvpp2_rxq_status_update(port, rxq->id, 0, rxq->size);
5999
6000 return 0;
6001}
6002
6003/* Push packets received by the RXQ to BM pool */
6004static void mvpp2_rxq_drop_pkts(struct mvpp2_port *port,
6005 struct mvpp2_rx_queue *rxq)
6006{
6007 int rx_received, i;
6008
6009 rx_received = mvpp2_rxq_received(port, rxq->id);
6010 if (!rx_received)
6011 return;
6012
6013 for (i = 0; i < rx_received; i++) {
6014 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006015 u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
6016 int pool;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006017
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006018 pool = (status & MVPP2_RXD_BM_POOL_ID_MASK) >>
6019 MVPP2_RXD_BM_POOL_ID_OFFS;
6020
Thomas Petazzoni7d7627b2017-06-22 14:23:20 +02006021 mvpp2_bm_pool_put(port, pool,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006022 mvpp2_rxdesc_dma_addr_get(port, rx_desc),
6023 mvpp2_rxdesc_cookie_get(port, rx_desc));
Marcin Wojtas3f518502014-07-10 16:52:13 -03006024 }
6025 mvpp2_rxq_status_update(port, rxq->id, rx_received, rx_received);
6026}
6027
6028/* Cleanup Rx queue */
6029static void mvpp2_rxq_deinit(struct mvpp2_port *port,
6030 struct mvpp2_rx_queue *rxq)
6031{
Thomas Petazzonia7868412017-03-07 16:53:13 +01006032 int cpu;
6033
Marcin Wojtas3f518502014-07-10 16:52:13 -03006034 mvpp2_rxq_drop_pkts(port, rxq);
6035
6036 if (rxq->descs)
6037 dma_free_coherent(port->dev->dev.parent,
6038 rxq->size * MVPP2_DESC_ALIGNED_SIZE,
6039 rxq->descs,
Thomas Petazzoni20396132017-03-07 16:53:00 +01006040 rxq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006041
6042 rxq->descs = NULL;
6043 rxq->last_desc = 0;
6044 rxq->next_desc_to_proc = 0;
Thomas Petazzoni20396132017-03-07 16:53:00 +01006045 rxq->descs_dma = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006046
6047 /* Clear Rx descriptors queue starting address and size;
6048 * free descriptor number
6049 */
6050 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02006051 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01006052 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
6053 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_ADDR_REG, 0);
6054 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_SIZE_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02006055 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03006056}
6057
6058/* Create and initialize a Tx queue */
6059static int mvpp2_txq_init(struct mvpp2_port *port,
6060 struct mvpp2_tx_queue *txq)
6061{
6062 u32 val;
6063 int cpu, desc, desc_per_txq, tx_port_num;
6064 struct mvpp2_txq_pcpu *txq_pcpu;
6065
6066 txq->size = port->tx_ring_size;
6067
6068 /* Allocate memory for Tx descriptors */
6069 txq->descs = dma_alloc_coherent(port->dev->dev.parent,
6070 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01006071 &txq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006072 if (!txq->descs)
6073 return -ENOMEM;
6074
Marcin Wojtas3f518502014-07-10 16:52:13 -03006075 txq->last_desc = txq->size - 1;
6076
6077 /* Set Tx descriptors queue starting address - indirect access */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02006078 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01006079 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
6080 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_ADDR_REG,
6081 txq->descs_dma);
6082 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_SIZE_REG,
6083 txq->size & MVPP2_TXQ_DESC_SIZE_MASK);
6084 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_INDEX_REG, 0);
6085 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_RSVD_CLR_REG,
6086 txq->id << MVPP2_TXQ_RSVD_CLR_OFFSET);
6087 val = mvpp2_percpu_read(port->priv, cpu, MVPP2_TXQ_PENDING_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006088 val &= ~MVPP2_TXQ_PENDING_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01006089 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PENDING_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006090
6091 /* Calculate base address in prefetch buffer. We reserve 16 descriptors
6092 * for each existing TXQ.
6093 * TCONTS for PON port must be continuous from 0 to MVPP2_MAX_TCONT
6094 * GBE ports assumed to be continious from 0 to MVPP2_MAX_PORTS
6095 */
6096 desc_per_txq = 16;
6097 desc = (port->id * MVPP2_MAX_TXQ * desc_per_txq) +
6098 (txq->log_id * desc_per_txq);
6099
Thomas Petazzonia7868412017-03-07 16:53:13 +01006100 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG,
6101 MVPP2_PREF_BUF_PTR(desc) | MVPP2_PREF_BUF_SIZE_16 |
6102 MVPP2_PREF_BUF_THRESH(desc_per_txq / 2));
Thomas Petazzonia704bb52017-06-10 23:18:22 +02006103 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03006104
6105 /* WRR / EJP configuration - indirect access */
6106 tx_port_num = mvpp2_egress_port(port);
6107 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
6108
6109 val = mvpp2_read(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id));
6110 val &= ~MVPP2_TXQ_REFILL_PERIOD_ALL_MASK;
6111 val |= MVPP2_TXQ_REFILL_PERIOD_MASK(1);
6112 val |= MVPP2_TXQ_REFILL_TOKENS_ALL_MASK;
6113 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id), val);
6114
6115 val = MVPP2_TXQ_TOKEN_SIZE_MAX;
6116 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq->log_id),
6117 val);
6118
6119 for_each_present_cpu(cpu) {
6120 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
6121 txq_pcpu->size = txq->size;
Markus Elfring02c91ec2017-04-17 08:09:07 +02006122 txq_pcpu->buffs = kmalloc_array(txq_pcpu->size,
6123 sizeof(*txq_pcpu->buffs),
6124 GFP_KERNEL);
Thomas Petazzoni83544912016-12-21 11:28:49 +01006125 if (!txq_pcpu->buffs)
Antoine Tenartba2d8d82017-11-28 14:19:48 +01006126 return -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006127
6128 txq_pcpu->count = 0;
6129 txq_pcpu->reserved_num = 0;
6130 txq_pcpu->txq_put_index = 0;
6131 txq_pcpu->txq_get_index = 0;
Antoine Tenartb70d4a52017-12-11 09:13:25 +01006132 txq_pcpu->tso_headers = NULL;
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006133
Antoine Tenart1d17db02017-10-30 11:23:31 +01006134 txq_pcpu->stop_threshold = txq->size - MVPP2_MAX_SKB_DESCS;
6135 txq_pcpu->wake_threshold = txq_pcpu->stop_threshold / 2;
6136
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006137 txq_pcpu->tso_headers =
6138 dma_alloc_coherent(port->dev->dev.parent,
Yan Markman822eaf72017-10-23 15:24:29 +02006139 txq_pcpu->size * TSO_HEADER_SIZE,
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006140 &txq_pcpu->tso_headers_dma,
6141 GFP_KERNEL);
6142 if (!txq_pcpu->tso_headers)
Antoine Tenartba2d8d82017-11-28 14:19:48 +01006143 return -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006144 }
6145
6146 return 0;
6147}
6148
6149/* Free allocated TXQ resources */
6150static void mvpp2_txq_deinit(struct mvpp2_port *port,
6151 struct mvpp2_tx_queue *txq)
6152{
6153 struct mvpp2_txq_pcpu *txq_pcpu;
6154 int cpu;
6155
6156 for_each_present_cpu(cpu) {
6157 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Thomas Petazzoni83544912016-12-21 11:28:49 +01006158 kfree(txq_pcpu->buffs);
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006159
Antoine Tenartb70d4a52017-12-11 09:13:25 +01006160 if (txq_pcpu->tso_headers)
6161 dma_free_coherent(port->dev->dev.parent,
6162 txq_pcpu->size * TSO_HEADER_SIZE,
6163 txq_pcpu->tso_headers,
6164 txq_pcpu->tso_headers_dma);
6165
6166 txq_pcpu->tso_headers = NULL;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006167 }
6168
6169 if (txq->descs)
6170 dma_free_coherent(port->dev->dev.parent,
6171 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01006172 txq->descs, txq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006173
6174 txq->descs = NULL;
6175 txq->last_desc = 0;
6176 txq->next_desc_to_proc = 0;
Thomas Petazzoni20396132017-03-07 16:53:00 +01006177 txq->descs_dma = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006178
6179 /* Set minimum bandwidth for disabled TXQs */
6180 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(txq->id), 0);
6181
6182 /* Set Tx descriptors queue starting address and size */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02006183 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01006184 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
6185 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_ADDR_REG, 0);
6186 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_SIZE_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02006187 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03006188}
6189
6190/* Cleanup Tx ports */
6191static void mvpp2_txq_clean(struct mvpp2_port *port, struct mvpp2_tx_queue *txq)
6192{
6193 struct mvpp2_txq_pcpu *txq_pcpu;
6194 int delay, pending, cpu;
6195 u32 val;
6196
Thomas Petazzonia704bb52017-06-10 23:18:22 +02006197 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01006198 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
6199 val = mvpp2_percpu_read(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006200 val |= MVPP2_TXQ_DRAIN_EN_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01006201 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006202
6203 /* The napi queue has been stopped so wait for all packets
6204 * to be transmitted.
6205 */
6206 delay = 0;
6207 do {
6208 if (delay >= MVPP2_TX_PENDING_TIMEOUT_MSEC) {
6209 netdev_warn(port->dev,
6210 "port %d: cleaning queue %d timed out\n",
6211 port->id, txq->log_id);
6212 break;
6213 }
6214 mdelay(1);
6215 delay++;
6216
Thomas Petazzonia7868412017-03-07 16:53:13 +01006217 pending = mvpp2_percpu_read(port->priv, cpu,
6218 MVPP2_TXQ_PENDING_REG);
6219 pending &= MVPP2_TXQ_PENDING_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006220 } while (pending);
6221
6222 val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01006223 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG, val);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02006224 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03006225
6226 for_each_present_cpu(cpu) {
6227 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
6228
6229 /* Release all packets */
6230 mvpp2_txq_bufs_free(port, txq, txq_pcpu, txq_pcpu->count);
6231
6232 /* Reset queue */
6233 txq_pcpu->count = 0;
6234 txq_pcpu->txq_put_index = 0;
6235 txq_pcpu->txq_get_index = 0;
6236 }
6237}
6238
6239/* Cleanup all Tx queues */
6240static void mvpp2_cleanup_txqs(struct mvpp2_port *port)
6241{
6242 struct mvpp2_tx_queue *txq;
6243 int queue;
6244 u32 val;
6245
6246 val = mvpp2_read(port->priv, MVPP2_TX_PORT_FLUSH_REG);
6247
6248 /* Reset Tx ports and delete Tx queues */
6249 val |= MVPP2_TX_PORT_FLUSH_MASK(port->id);
6250 mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
6251
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006252 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006253 txq = port->txqs[queue];
6254 mvpp2_txq_clean(port, txq);
6255 mvpp2_txq_deinit(port, txq);
6256 }
6257
6258 on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
6259
6260 val &= ~MVPP2_TX_PORT_FLUSH_MASK(port->id);
6261 mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
6262}
6263
6264/* Cleanup all Rx queues */
6265static void mvpp2_cleanup_rxqs(struct mvpp2_port *port)
6266{
6267 int queue;
6268
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006269 for (queue = 0; queue < port->nrxqs; queue++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006270 mvpp2_rxq_deinit(port, port->rxqs[queue]);
6271}
6272
6273/* Init all Rx queues for port */
6274static int mvpp2_setup_rxqs(struct mvpp2_port *port)
6275{
6276 int queue, err;
6277
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006278 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006279 err = mvpp2_rxq_init(port, port->rxqs[queue]);
6280 if (err)
6281 goto err_cleanup;
6282 }
6283 return 0;
6284
6285err_cleanup:
6286 mvpp2_cleanup_rxqs(port);
6287 return err;
6288}
6289
6290/* Init all tx queues for port */
6291static int mvpp2_setup_txqs(struct mvpp2_port *port)
6292{
6293 struct mvpp2_tx_queue *txq;
6294 int queue, err;
6295
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006296 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006297 txq = port->txqs[queue];
6298 err = mvpp2_txq_init(port, txq);
6299 if (err)
6300 goto err_cleanup;
6301 }
6302
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006303 if (port->has_tx_irqs) {
6304 mvpp2_tx_time_coal_set(port);
6305 for (queue = 0; queue < port->ntxqs; queue++) {
6306 txq = port->txqs[queue];
6307 mvpp2_tx_pkts_coal_set(port, txq);
6308 }
6309 }
6310
Marcin Wojtas3f518502014-07-10 16:52:13 -03006311 on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
6312 return 0;
6313
6314err_cleanup:
6315 mvpp2_cleanup_txqs(port);
6316 return err;
6317}
6318
6319/* The callback for per-port interrupt */
6320static irqreturn_t mvpp2_isr(int irq, void *dev_id)
6321{
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006322 struct mvpp2_queue_vector *qv = dev_id;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006323
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006324 mvpp2_qvec_interrupt_disable(qv);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006325
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006326 napi_schedule(&qv->napi);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006327
6328 return IRQ_HANDLED;
6329}
6330
Antoine Tenartfd3651b2017-09-01 11:04:54 +02006331/* Per-port interrupt for link status changes */
6332static irqreturn_t mvpp2_link_status_isr(int irq, void *dev_id)
6333{
6334 struct mvpp2_port *port = (struct mvpp2_port *)dev_id;
6335 struct net_device *dev = port->dev;
6336 bool event = false, link = false;
6337 u32 val;
6338
6339 mvpp22_gop_mask_irq(port);
6340
6341 if (port->gop_id == 0 &&
6342 port->phy_interface == PHY_INTERFACE_MODE_10GKR) {
6343 val = readl(port->base + MVPP22_XLG_INT_STAT);
6344 if (val & MVPP22_XLG_INT_STAT_LINK) {
6345 event = true;
6346 val = readl(port->base + MVPP22_XLG_STATUS);
6347 if (val & MVPP22_XLG_STATUS_LINK_UP)
6348 link = true;
6349 }
6350 } else if (phy_interface_mode_is_rgmii(port->phy_interface) ||
6351 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
6352 val = readl(port->base + MVPP22_GMAC_INT_STAT);
6353 if (val & MVPP22_GMAC_INT_STAT_LINK) {
6354 event = true;
6355 val = readl(port->base + MVPP2_GMAC_STATUS0);
6356 if (val & MVPP2_GMAC_STATUS0_LINK_UP)
6357 link = true;
6358 }
6359 }
6360
6361 if (!netif_running(dev) || !event)
6362 goto handled;
6363
6364 if (link) {
6365 mvpp2_interrupts_enable(port);
6366
6367 mvpp2_egress_enable(port);
6368 mvpp2_ingress_enable(port);
6369 netif_carrier_on(dev);
6370 netif_tx_wake_all_queues(dev);
6371 } else {
6372 netif_tx_stop_all_queues(dev);
6373 netif_carrier_off(dev);
6374 mvpp2_ingress_disable(port);
6375 mvpp2_egress_disable(port);
6376
6377 mvpp2_interrupts_disable(port);
6378 }
6379
6380handled:
6381 mvpp22_gop_unmask_irq(port);
6382 return IRQ_HANDLED;
6383}
6384
Antoine Tenart65a2c092017-08-30 10:29:18 +02006385static void mvpp2_gmac_set_autoneg(struct mvpp2_port *port,
6386 struct phy_device *phydev)
6387{
6388 u32 val;
6389
6390 if (port->phy_interface != PHY_INTERFACE_MODE_RGMII &&
6391 port->phy_interface != PHY_INTERFACE_MODE_RGMII_ID &&
6392 port->phy_interface != PHY_INTERFACE_MODE_RGMII_RXID &&
6393 port->phy_interface != PHY_INTERFACE_MODE_RGMII_TXID &&
6394 port->phy_interface != PHY_INTERFACE_MODE_SGMII)
6395 return;
6396
6397 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
6398 val &= ~(MVPP2_GMAC_CONFIG_MII_SPEED |
6399 MVPP2_GMAC_CONFIG_GMII_SPEED |
6400 MVPP2_GMAC_CONFIG_FULL_DUPLEX |
6401 MVPP2_GMAC_AN_SPEED_EN |
6402 MVPP2_GMAC_AN_DUPLEX_EN);
6403
6404 if (phydev->duplex)
6405 val |= MVPP2_GMAC_CONFIG_FULL_DUPLEX;
6406
6407 if (phydev->speed == SPEED_1000)
6408 val |= MVPP2_GMAC_CONFIG_GMII_SPEED;
6409 else if (phydev->speed == SPEED_100)
6410 val |= MVPP2_GMAC_CONFIG_MII_SPEED;
6411
6412 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
Antoine Tenart65a2c092017-08-30 10:29:18 +02006413}
6414
Marcin Wojtas3f518502014-07-10 16:52:13 -03006415/* Adjust link */
6416static void mvpp2_link_event(struct net_device *dev)
6417{
6418 struct mvpp2_port *port = netdev_priv(dev);
Philippe Reynes8e072692016-06-28 00:08:11 +02006419 struct phy_device *phydev = dev->phydev;
Antoine Tenart89273bc2017-08-30 10:29:19 +02006420 bool link_reconfigured = false;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006421 u32 val;
6422
6423 if (phydev->link) {
Antoine Tenart89273bc2017-08-30 10:29:19 +02006424 if (port->phy_interface != phydev->interface && port->comphy) {
6425 /* disable current port for reconfiguration */
6426 mvpp2_interrupts_disable(port);
6427 netif_carrier_off(port->dev);
6428 mvpp2_port_disable(port);
6429 phy_power_off(port->comphy);
6430
6431 /* comphy reconfiguration */
6432 port->phy_interface = phydev->interface;
6433 mvpp22_comphy_init(port);
6434
6435 /* gop/mac reconfiguration */
6436 mvpp22_gop_init(port);
6437 mvpp2_port_mii_set(port);
6438
6439 link_reconfigured = true;
6440 }
6441
Marcin Wojtas3f518502014-07-10 16:52:13 -03006442 if ((port->speed != phydev->speed) ||
6443 (port->duplex != phydev->duplex)) {
Antoine Tenart65a2c092017-08-30 10:29:18 +02006444 mvpp2_gmac_set_autoneg(port, phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006445
6446 port->duplex = phydev->duplex;
6447 port->speed = phydev->speed;
6448 }
6449 }
6450
Antoine Tenart89273bc2017-08-30 10:29:19 +02006451 if (phydev->link != port->link || link_reconfigured) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006452 port->link = phydev->link;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006453
Marcin Wojtas3f518502014-07-10 16:52:13 -03006454 if (phydev->link) {
Antoine Tenart65a2c092017-08-30 10:29:18 +02006455 if (port->phy_interface == PHY_INTERFACE_MODE_RGMII ||
6456 port->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
6457 port->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID ||
6458 port->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID ||
6459 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
6460 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
6461 val |= (MVPP2_GMAC_FORCE_LINK_PASS |
6462 MVPP2_GMAC_FORCE_LINK_DOWN);
6463 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
6464 }
Antoine Tenartf55744a2017-08-30 10:29:17 +02006465
6466 mvpp2_interrupts_enable(port);
6467 mvpp2_port_enable(port);
6468
Marcin Wojtas3f518502014-07-10 16:52:13 -03006469 mvpp2_egress_enable(port);
6470 mvpp2_ingress_enable(port);
Antoine Tenartf55744a2017-08-30 10:29:17 +02006471 netif_carrier_on(dev);
6472 netif_tx_wake_all_queues(dev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006473 } else {
Antoine Tenart968b2112017-08-30 10:29:16 +02006474 port->duplex = -1;
6475 port->speed = 0;
6476
Antoine Tenartf55744a2017-08-30 10:29:17 +02006477 netif_tx_stop_all_queues(dev);
6478 netif_carrier_off(dev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006479 mvpp2_ingress_disable(port);
6480 mvpp2_egress_disable(port);
Antoine Tenartf55744a2017-08-30 10:29:17 +02006481
6482 mvpp2_port_disable(port);
6483 mvpp2_interrupts_disable(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006484 }
Antoine Tenart968b2112017-08-30 10:29:16 +02006485
Marcin Wojtas3f518502014-07-10 16:52:13 -03006486 phy_print_status(phydev);
6487 }
6488}
6489
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006490static void mvpp2_timer_set(struct mvpp2_port_pcpu *port_pcpu)
6491{
6492 ktime_t interval;
6493
6494 if (!port_pcpu->timer_scheduled) {
6495 port_pcpu->timer_scheduled = true;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01006496 interval = MVPP2_TXDONE_HRTIMER_PERIOD_NS;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006497 hrtimer_start(&port_pcpu->tx_done_timer, interval,
6498 HRTIMER_MODE_REL_PINNED);
6499 }
6500}
6501
6502static void mvpp2_tx_proc_cb(unsigned long data)
6503{
6504 struct net_device *dev = (struct net_device *)data;
6505 struct mvpp2_port *port = netdev_priv(dev);
6506 struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
6507 unsigned int tx_todo, cause;
6508
6509 if (!netif_running(dev))
6510 return;
6511 port_pcpu->timer_scheduled = false;
6512
6513 /* Process all the Tx queues */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006514 cause = (1 << port->ntxqs) - 1;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006515 tx_todo = mvpp2_tx_done(port, cause, smp_processor_id());
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006516
6517 /* Set the timer in case not all the packets were processed */
6518 if (tx_todo)
6519 mvpp2_timer_set(port_pcpu);
6520}
6521
6522static enum hrtimer_restart mvpp2_hr_timer_cb(struct hrtimer *timer)
6523{
6524 struct mvpp2_port_pcpu *port_pcpu = container_of(timer,
6525 struct mvpp2_port_pcpu,
6526 tx_done_timer);
6527
6528 tasklet_schedule(&port_pcpu->tx_done_tasklet);
6529
6530 return HRTIMER_NORESTART;
6531}
6532
Marcin Wojtas3f518502014-07-10 16:52:13 -03006533/* Main RX/TX processing routines */
6534
6535/* Display more error info */
6536static void mvpp2_rx_error(struct mvpp2_port *port,
6537 struct mvpp2_rx_desc *rx_desc)
6538{
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006539 u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
6540 size_t sz = mvpp2_rxdesc_size_get(port, rx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006541
6542 switch (status & MVPP2_RXD_ERR_CODE_MASK) {
6543 case MVPP2_RXD_ERR_CRC:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006544 netdev_err(port->dev, "bad rx status %08x (crc error), size=%zu\n",
6545 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006546 break;
6547 case MVPP2_RXD_ERR_OVERRUN:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006548 netdev_err(port->dev, "bad rx status %08x (overrun error), size=%zu\n",
6549 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006550 break;
6551 case MVPP2_RXD_ERR_RESOURCE:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006552 netdev_err(port->dev, "bad rx status %08x (resource error), size=%zu\n",
6553 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006554 break;
6555 }
6556}
6557
6558/* Handle RX checksum offload */
6559static void mvpp2_rx_csum(struct mvpp2_port *port, u32 status,
6560 struct sk_buff *skb)
6561{
6562 if (((status & MVPP2_RXD_L3_IP4) &&
6563 !(status & MVPP2_RXD_IP4_HEADER_ERR)) ||
6564 (status & MVPP2_RXD_L3_IP6))
6565 if (((status & MVPP2_RXD_L4_UDP) ||
6566 (status & MVPP2_RXD_L4_TCP)) &&
6567 (status & MVPP2_RXD_L4_CSUM_OK)) {
6568 skb->csum = 0;
6569 skb->ip_summed = CHECKSUM_UNNECESSARY;
6570 return;
6571 }
6572
6573 skb->ip_summed = CHECKSUM_NONE;
6574}
6575
6576/* Reuse skb if possible, or allocate a new skb and add it to BM pool */
6577static int mvpp2_rx_refill(struct mvpp2_port *port,
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006578 struct mvpp2_bm_pool *bm_pool, int pool)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006579{
Thomas Petazzoni20396132017-03-07 16:53:00 +01006580 dma_addr_t dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01006581 phys_addr_t phys_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006582 void *buf;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006583
Marcin Wojtas3f518502014-07-10 16:52:13 -03006584 /* No recycle or too many buffers are in use, so allocate a new skb */
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01006585 buf = mvpp2_buf_alloc(port, bm_pool, &dma_addr, &phys_addr,
6586 GFP_ATOMIC);
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006587 if (!buf)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006588 return -ENOMEM;
6589
Thomas Petazzoni7d7627b2017-06-22 14:23:20 +02006590 mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
Thomas Petazzoni7ef7e1d2017-02-21 11:28:07 +01006591
Marcin Wojtas3f518502014-07-10 16:52:13 -03006592 return 0;
6593}
6594
6595/* Handle tx checksum */
6596static u32 mvpp2_skb_tx_csum(struct mvpp2_port *port, struct sk_buff *skb)
6597{
6598 if (skb->ip_summed == CHECKSUM_PARTIAL) {
6599 int ip_hdr_len = 0;
6600 u8 l4_proto;
6601
6602 if (skb->protocol == htons(ETH_P_IP)) {
6603 struct iphdr *ip4h = ip_hdr(skb);
6604
6605 /* Calculate IPv4 checksum and L4 checksum */
6606 ip_hdr_len = ip4h->ihl;
6607 l4_proto = ip4h->protocol;
6608 } else if (skb->protocol == htons(ETH_P_IPV6)) {
6609 struct ipv6hdr *ip6h = ipv6_hdr(skb);
6610
6611 /* Read l4_protocol from one of IPv6 extra headers */
6612 if (skb_network_header_len(skb) > 0)
6613 ip_hdr_len = (skb_network_header_len(skb) >> 2);
6614 l4_proto = ip6h->nexthdr;
6615 } else {
6616 return MVPP2_TXD_L4_CSUM_NOT;
6617 }
6618
6619 return mvpp2_txq_desc_csum(skb_network_offset(skb),
6620 skb->protocol, ip_hdr_len, l4_proto);
6621 }
6622
6623 return MVPP2_TXD_L4_CSUM_NOT | MVPP2_TXD_IP_CSUM_DISABLE;
6624}
6625
Marcin Wojtas3f518502014-07-10 16:52:13 -03006626/* Main rx processing */
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006627static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi,
6628 int rx_todo, struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006629{
6630 struct net_device *dev = port->dev;
Marcin Wojtasb5015852015-12-03 15:20:51 +01006631 int rx_received;
6632 int rx_done = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006633 u32 rcvd_pkts = 0;
6634 u32 rcvd_bytes = 0;
6635
6636 /* Get number of received packets and clamp the to-do */
6637 rx_received = mvpp2_rxq_received(port, rxq->id);
6638 if (rx_todo > rx_received)
6639 rx_todo = rx_received;
6640
Marcin Wojtasb5015852015-12-03 15:20:51 +01006641 while (rx_done < rx_todo) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006642 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
6643 struct mvpp2_bm_pool *bm_pool;
6644 struct sk_buff *skb;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006645 unsigned int frag_size;
Thomas Petazzoni20396132017-03-07 16:53:00 +01006646 dma_addr_t dma_addr;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006647 phys_addr_t phys_addr;
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006648 u32 rx_status;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006649 int pool, rx_bytes, err;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006650 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006651
Marcin Wojtasb5015852015-12-03 15:20:51 +01006652 rx_done++;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006653 rx_status = mvpp2_rxdesc_status_get(port, rx_desc);
6654 rx_bytes = mvpp2_rxdesc_size_get(port, rx_desc);
6655 rx_bytes -= MVPP2_MH_SIZE;
6656 dma_addr = mvpp2_rxdesc_dma_addr_get(port, rx_desc);
6657 phys_addr = mvpp2_rxdesc_cookie_get(port, rx_desc);
6658 data = (void *)phys_to_virt(phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006659
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006660 pool = (rx_status & MVPP2_RXD_BM_POOL_ID_MASK) >>
6661 MVPP2_RXD_BM_POOL_ID_OFFS;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006662 bm_pool = &port->priv->bm_pools[pool];
Marcin Wojtas3f518502014-07-10 16:52:13 -03006663
6664 /* In case of an error, release the requested buffer pointer
6665 * to the Buffer Manager. This request process is controlled
6666 * by the hardware, and the information about the buffer is
6667 * comprised by the RX descriptor.
6668 */
6669 if (rx_status & MVPP2_RXD_ERR_SUMMARY) {
Markus Elfring8a524882017-04-17 10:52:02 +02006670err_drop_frame:
Marcin Wojtas3f518502014-07-10 16:52:13 -03006671 dev->stats.rx_errors++;
6672 mvpp2_rx_error(port, rx_desc);
Marcin Wojtasb5015852015-12-03 15:20:51 +01006673 /* Return the buffer to the pool */
Thomas Petazzoni7d7627b2017-06-22 14:23:20 +02006674 mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006675 continue;
6676 }
6677
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006678 if (bm_pool->frag_size > PAGE_SIZE)
6679 frag_size = 0;
6680 else
6681 frag_size = bm_pool->frag_size;
6682
6683 skb = build_skb(data, frag_size);
6684 if (!skb) {
6685 netdev_warn(port->dev, "skb build failed\n");
6686 goto err_drop_frame;
6687 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006688
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006689 err = mvpp2_rx_refill(port, bm_pool, pool);
Marcin Wojtasb5015852015-12-03 15:20:51 +01006690 if (err) {
6691 netdev_err(port->dev, "failed to refill BM pools\n");
6692 goto err_drop_frame;
6693 }
6694
Thomas Petazzoni20396132017-03-07 16:53:00 +01006695 dma_unmap_single(dev->dev.parent, dma_addr,
Marcin Wojtas4229d502015-12-03 15:20:50 +01006696 bm_pool->buf_size, DMA_FROM_DEVICE);
6697
Marcin Wojtas3f518502014-07-10 16:52:13 -03006698 rcvd_pkts++;
6699 rcvd_bytes += rx_bytes;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006700
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006701 skb_reserve(skb, MVPP2_MH_SIZE + NET_SKB_PAD);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006702 skb_put(skb, rx_bytes);
6703 skb->protocol = eth_type_trans(skb, dev);
6704 mvpp2_rx_csum(port, rx_status, skb);
6705
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006706 napi_gro_receive(napi, skb);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006707 }
6708
6709 if (rcvd_pkts) {
6710 struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
6711
6712 u64_stats_update_begin(&stats->syncp);
6713 stats->rx_packets += rcvd_pkts;
6714 stats->rx_bytes += rcvd_bytes;
6715 u64_stats_update_end(&stats->syncp);
6716 }
6717
6718 /* Update Rx queue management counters */
6719 wmb();
Marcin Wojtasb5015852015-12-03 15:20:51 +01006720 mvpp2_rxq_status_update(port, rxq->id, rx_done, rx_done);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006721
6722 return rx_todo;
6723}
6724
6725static inline void
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006726tx_desc_unmap_put(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006727 struct mvpp2_tx_desc *desc)
6728{
Antoine Tenart20920262017-10-23 15:24:30 +02006729 struct mvpp2_txq_pcpu *txq_pcpu = this_cpu_ptr(txq->pcpu);
6730
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006731 dma_addr_t buf_dma_addr =
6732 mvpp2_txdesc_dma_addr_get(port, desc);
6733 size_t buf_sz =
6734 mvpp2_txdesc_size_get(port, desc);
Antoine Tenart20920262017-10-23 15:24:30 +02006735 if (!IS_TSO_HEADER(txq_pcpu, buf_dma_addr))
6736 dma_unmap_single(port->dev->dev.parent, buf_dma_addr,
6737 buf_sz, DMA_TO_DEVICE);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006738 mvpp2_txq_desc_put(txq);
6739}
6740
6741/* Handle tx fragmentation processing */
6742static int mvpp2_tx_frag_process(struct mvpp2_port *port, struct sk_buff *skb,
6743 struct mvpp2_tx_queue *aggr_txq,
6744 struct mvpp2_tx_queue *txq)
6745{
6746 struct mvpp2_txq_pcpu *txq_pcpu = this_cpu_ptr(txq->pcpu);
6747 struct mvpp2_tx_desc *tx_desc;
6748 int i;
Thomas Petazzoni20396132017-03-07 16:53:00 +01006749 dma_addr_t buf_dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006750
6751 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
6752 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6753 void *addr = page_address(frag->page.p) + frag->page_offset;
6754
6755 tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006756 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6757 mvpp2_txdesc_size_set(port, tx_desc, frag->size);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006758
Thomas Petazzoni20396132017-03-07 16:53:00 +01006759 buf_dma_addr = dma_map_single(port->dev->dev.parent, addr,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006760 frag->size,
6761 DMA_TO_DEVICE);
Thomas Petazzoni20396132017-03-07 16:53:00 +01006762 if (dma_mapping_error(port->dev->dev.parent, buf_dma_addr)) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006763 mvpp2_txq_desc_put(txq);
Markus Elfring32bae632017-04-17 11:36:34 +02006764 goto cleanup;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006765 }
6766
Antoine Tenart6eb5d372017-10-30 11:23:33 +01006767 mvpp2_txdesc_dma_addr_set(port, tx_desc, buf_dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006768
6769 if (i == (skb_shinfo(skb)->nr_frags - 1)) {
6770 /* Last descriptor */
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006771 mvpp2_txdesc_cmd_set(port, tx_desc,
6772 MVPP2_TXD_L_DESC);
6773 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006774 } else {
6775 /* Descriptor in the middle: Not First, Not Last */
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006776 mvpp2_txdesc_cmd_set(port, tx_desc, 0);
6777 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006778 }
6779 }
6780
6781 return 0;
Markus Elfring32bae632017-04-17 11:36:34 +02006782cleanup:
Marcin Wojtas3f518502014-07-10 16:52:13 -03006783 /* Release all descriptors that were used to map fragments of
6784 * this packet, as well as the corresponding DMA mappings
6785 */
6786 for (i = i - 1; i >= 0; i--) {
6787 tx_desc = txq->descs + i;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006788 tx_desc_unmap_put(port, txq, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006789 }
6790
6791 return -ENOMEM;
6792}
6793
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006794static inline void mvpp2_tso_put_hdr(struct sk_buff *skb,
6795 struct net_device *dev,
6796 struct mvpp2_tx_queue *txq,
6797 struct mvpp2_tx_queue *aggr_txq,
6798 struct mvpp2_txq_pcpu *txq_pcpu,
6799 int hdr_sz)
6800{
6801 struct mvpp2_port *port = netdev_priv(dev);
6802 struct mvpp2_tx_desc *tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
6803 dma_addr_t addr;
6804
6805 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6806 mvpp2_txdesc_size_set(port, tx_desc, hdr_sz);
6807
6808 addr = txq_pcpu->tso_headers_dma +
6809 txq_pcpu->txq_put_index * TSO_HEADER_SIZE;
Antoine Tenart6eb5d372017-10-30 11:23:33 +01006810 mvpp2_txdesc_dma_addr_set(port, tx_desc, addr);
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006811
6812 mvpp2_txdesc_cmd_set(port, tx_desc, mvpp2_skb_tx_csum(port, skb) |
6813 MVPP2_TXD_F_DESC |
6814 MVPP2_TXD_PADDING_DISABLE);
6815 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
6816}
6817
6818static inline int mvpp2_tso_put_data(struct sk_buff *skb,
6819 struct net_device *dev, struct tso_t *tso,
6820 struct mvpp2_tx_queue *txq,
6821 struct mvpp2_tx_queue *aggr_txq,
6822 struct mvpp2_txq_pcpu *txq_pcpu,
6823 int sz, bool left, bool last)
6824{
6825 struct mvpp2_port *port = netdev_priv(dev);
6826 struct mvpp2_tx_desc *tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
6827 dma_addr_t buf_dma_addr;
6828
6829 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6830 mvpp2_txdesc_size_set(port, tx_desc, sz);
6831
6832 buf_dma_addr = dma_map_single(dev->dev.parent, tso->data, sz,
6833 DMA_TO_DEVICE);
6834 if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
6835 mvpp2_txq_desc_put(txq);
6836 return -ENOMEM;
6837 }
6838
Antoine Tenart6eb5d372017-10-30 11:23:33 +01006839 mvpp2_txdesc_dma_addr_set(port, tx_desc, buf_dma_addr);
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006840
6841 if (!left) {
6842 mvpp2_txdesc_cmd_set(port, tx_desc, MVPP2_TXD_L_DESC);
6843 if (last) {
6844 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
6845 return 0;
6846 }
6847 } else {
6848 mvpp2_txdesc_cmd_set(port, tx_desc, 0);
6849 }
6850
6851 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
6852 return 0;
6853}
6854
6855static int mvpp2_tx_tso(struct sk_buff *skb, struct net_device *dev,
6856 struct mvpp2_tx_queue *txq,
6857 struct mvpp2_tx_queue *aggr_txq,
6858 struct mvpp2_txq_pcpu *txq_pcpu)
6859{
6860 struct mvpp2_port *port = netdev_priv(dev);
6861 struct tso_t tso;
6862 int hdr_sz = skb_transport_offset(skb) + tcp_hdrlen(skb);
6863 int i, len, descs = 0;
6864
6865 /* Check number of available descriptors */
6866 if (mvpp2_aggr_desc_num_check(port->priv, aggr_txq,
6867 tso_count_descs(skb)) ||
6868 mvpp2_txq_reserved_desc_num_proc(port->priv, txq, txq_pcpu,
6869 tso_count_descs(skb)))
6870 return 0;
6871
6872 tso_start(skb, &tso);
6873 len = skb->len - hdr_sz;
6874 while (len > 0) {
6875 int left = min_t(int, skb_shinfo(skb)->gso_size, len);
6876 char *hdr = txq_pcpu->tso_headers +
6877 txq_pcpu->txq_put_index * TSO_HEADER_SIZE;
6878
6879 len -= left;
6880 descs++;
6881
6882 tso_build_hdr(skb, hdr, &tso, left, len == 0);
6883 mvpp2_tso_put_hdr(skb, dev, txq, aggr_txq, txq_pcpu, hdr_sz);
6884
6885 while (left > 0) {
6886 int sz = min_t(int, tso.size, left);
6887 left -= sz;
6888 descs++;
6889
6890 if (mvpp2_tso_put_data(skb, dev, &tso, txq, aggr_txq,
6891 txq_pcpu, sz, left, len == 0))
6892 goto release;
6893 tso_build_data(skb, &tso, sz);
6894 }
6895 }
6896
6897 return descs;
6898
6899release:
6900 for (i = descs - 1; i >= 0; i--) {
6901 struct mvpp2_tx_desc *tx_desc = txq->descs + i;
6902 tx_desc_unmap_put(port, txq, tx_desc);
6903 }
6904 return 0;
6905}
6906
Marcin Wojtas3f518502014-07-10 16:52:13 -03006907/* Main tx processing */
6908static int mvpp2_tx(struct sk_buff *skb, struct net_device *dev)
6909{
6910 struct mvpp2_port *port = netdev_priv(dev);
6911 struct mvpp2_tx_queue *txq, *aggr_txq;
6912 struct mvpp2_txq_pcpu *txq_pcpu;
6913 struct mvpp2_tx_desc *tx_desc;
Thomas Petazzoni20396132017-03-07 16:53:00 +01006914 dma_addr_t buf_dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006915 int frags = 0;
6916 u16 txq_id;
6917 u32 tx_cmd;
6918
6919 txq_id = skb_get_queue_mapping(skb);
6920 txq = port->txqs[txq_id];
6921 txq_pcpu = this_cpu_ptr(txq->pcpu);
6922 aggr_txq = &port->priv->aggr_txqs[smp_processor_id()];
6923
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006924 if (skb_is_gso(skb)) {
6925 frags = mvpp2_tx_tso(skb, dev, txq, aggr_txq, txq_pcpu);
6926 goto out;
6927 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006928 frags = skb_shinfo(skb)->nr_frags + 1;
6929
6930 /* Check number of available descriptors */
6931 if (mvpp2_aggr_desc_num_check(port->priv, aggr_txq, frags) ||
6932 mvpp2_txq_reserved_desc_num_proc(port->priv, txq,
6933 txq_pcpu, frags)) {
6934 frags = 0;
6935 goto out;
6936 }
6937
6938 /* Get a descriptor for the first part of the packet */
6939 tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006940 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6941 mvpp2_txdesc_size_set(port, tx_desc, skb_headlen(skb));
Marcin Wojtas3f518502014-07-10 16:52:13 -03006942
Thomas Petazzoni20396132017-03-07 16:53:00 +01006943 buf_dma_addr = dma_map_single(dev->dev.parent, skb->data,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006944 skb_headlen(skb), DMA_TO_DEVICE);
Thomas Petazzoni20396132017-03-07 16:53:00 +01006945 if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006946 mvpp2_txq_desc_put(txq);
6947 frags = 0;
6948 goto out;
6949 }
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006950
Antoine Tenart6eb5d372017-10-30 11:23:33 +01006951 mvpp2_txdesc_dma_addr_set(port, tx_desc, buf_dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006952
6953 tx_cmd = mvpp2_skb_tx_csum(port, skb);
6954
6955 if (frags == 1) {
6956 /* First and Last descriptor */
6957 tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_L_DESC;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006958 mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
6959 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006960 } else {
6961 /* First but not Last */
6962 tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_PADDING_DISABLE;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006963 mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
6964 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006965
6966 /* Continue with other skb fragments */
6967 if (mvpp2_tx_frag_process(port, skb, aggr_txq, txq)) {
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006968 tx_desc_unmap_put(port, txq, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006969 frags = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006970 }
6971 }
6972
Marcin Wojtas3f518502014-07-10 16:52:13 -03006973out:
6974 if (frags > 0) {
6975 struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006976 struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
6977
6978 txq_pcpu->reserved_num -= frags;
6979 txq_pcpu->count += frags;
6980 aggr_txq->count += frags;
6981
6982 /* Enable transmit */
6983 wmb();
6984 mvpp2_aggr_txq_pend_desc_add(port, frags);
6985
Antoine Tenart1d17db02017-10-30 11:23:31 +01006986 if (txq_pcpu->count >= txq_pcpu->stop_threshold)
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006987 netif_tx_stop_queue(nq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006988
6989 u64_stats_update_begin(&stats->syncp);
6990 stats->tx_packets++;
6991 stats->tx_bytes += skb->len;
6992 u64_stats_update_end(&stats->syncp);
6993 } else {
6994 dev->stats.tx_dropped++;
6995 dev_kfree_skb_any(skb);
6996 }
6997
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006998 /* Finalize TX processing */
Antoine Tenart082297e2017-10-23 15:24:31 +02006999 if (!port->has_tx_irqs && txq_pcpu->count >= txq->done_pkts_coal)
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007000 mvpp2_txq_done(port, txq, txq_pcpu);
7001
7002 /* Set the timer in case not all frags were processed */
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007003 if (!port->has_tx_irqs && txq_pcpu->count <= frags &&
7004 txq_pcpu->count > 0) {
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007005 struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
7006
7007 mvpp2_timer_set(port_pcpu);
7008 }
7009
Marcin Wojtas3f518502014-07-10 16:52:13 -03007010 return NETDEV_TX_OK;
7011}
7012
7013static inline void mvpp2_cause_error(struct net_device *dev, int cause)
7014{
7015 if (cause & MVPP2_CAUSE_FCS_ERR_MASK)
7016 netdev_err(dev, "FCS error\n");
7017 if (cause & MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK)
7018 netdev_err(dev, "rx fifo overrun error\n");
7019 if (cause & MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK)
7020 netdev_err(dev, "tx fifo underrun error\n");
7021}
7022
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007023static int mvpp2_poll(struct napi_struct *napi, int budget)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007024{
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007025 u32 cause_rx_tx, cause_rx, cause_tx, cause_misc;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007026 int rx_done = 0;
7027 struct mvpp2_port *port = netdev_priv(napi->dev);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007028 struct mvpp2_queue_vector *qv;
Thomas Petazzonia7868412017-03-07 16:53:13 +01007029 int cpu = smp_processor_id();
Marcin Wojtas3f518502014-07-10 16:52:13 -03007030
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007031 qv = container_of(napi, struct mvpp2_queue_vector, napi);
7032
Marcin Wojtas3f518502014-07-10 16:52:13 -03007033 /* Rx/Tx cause register
7034 *
7035 * Bits 0-15: each bit indicates received packets on the Rx queue
7036 * (bit 0 is for Rx queue 0).
7037 *
7038 * Bits 16-23: each bit indicates transmitted packets on the Tx queue
7039 * (bit 16 is for Tx queue 0).
7040 *
7041 * Each CPU has its own Rx/Tx cause register
7042 */
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007043 cause_rx_tx = mvpp2_percpu_read(port->priv, qv->sw_thread_id,
Thomas Petazzonia7868412017-03-07 16:53:13 +01007044 MVPP2_ISR_RX_TX_CAUSE_REG(port->id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03007045
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007046 cause_misc = cause_rx_tx & MVPP2_CAUSE_MISC_SUM_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007047 if (cause_misc) {
7048 mvpp2_cause_error(port->dev, cause_misc);
7049
7050 /* Clear the cause register */
7051 mvpp2_write(port->priv, MVPP2_ISR_MISC_CAUSE_REG, 0);
Thomas Petazzonia7868412017-03-07 16:53:13 +01007052 mvpp2_percpu_write(port->priv, cpu,
7053 MVPP2_ISR_RX_TX_CAUSE_REG(port->id),
7054 cause_rx_tx & ~MVPP2_CAUSE_MISC_SUM_MASK);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007055 }
7056
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007057 cause_tx = cause_rx_tx & MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
7058 if (cause_tx) {
7059 cause_tx >>= MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_OFFSET;
7060 mvpp2_tx_done(port, cause_tx, qv->sw_thread_id);
7061 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03007062
7063 /* Process RX packets */
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007064 cause_rx = cause_rx_tx & MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
7065 cause_rx <<= qv->first_rxq;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007066 cause_rx |= qv->pending_cause_rx;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007067 while (cause_rx && budget > 0) {
7068 int count;
7069 struct mvpp2_rx_queue *rxq;
7070
7071 rxq = mvpp2_get_rx_queue(port, cause_rx);
7072 if (!rxq)
7073 break;
7074
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007075 count = mvpp2_rx(port, napi, budget, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007076 rx_done += count;
7077 budget -= count;
7078 if (budget > 0) {
7079 /* Clear the bit associated to this Rx queue
7080 * so that next iteration will continue from
7081 * the next Rx queue.
7082 */
7083 cause_rx &= ~(1 << rxq->logic_rxq);
7084 }
7085 }
7086
7087 if (budget > 0) {
7088 cause_rx = 0;
Eric Dumazet6ad20162017-01-30 08:22:01 -08007089 napi_complete_done(napi, rx_done);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007090
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007091 mvpp2_qvec_interrupt_enable(qv);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007092 }
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007093 qv->pending_cause_rx = cause_rx;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007094 return rx_done;
7095}
7096
7097/* Set hw internals when starting port */
7098static void mvpp2_start_dev(struct mvpp2_port *port)
7099{
Philippe Reynes8e072692016-06-28 00:08:11 +02007100 struct net_device *ndev = port->dev;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007101 int i;
Philippe Reynes8e072692016-06-28 00:08:11 +02007102
Stefan Chulski76eb1b12017-08-22 19:08:26 +02007103 if (port->gop_id == 0 &&
7104 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
7105 port->phy_interface == PHY_INTERFACE_MODE_10GKR))
7106 mvpp2_xlg_max_rx_size_set(port);
7107 else
7108 mvpp2_gmac_max_rx_size_set(port);
7109
Marcin Wojtas3f518502014-07-10 16:52:13 -03007110 mvpp2_txp_max_tx_size_set(port);
7111
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007112 for (i = 0; i < port->nqvecs; i++)
7113 napi_enable(&port->qvecs[i].napi);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007114
7115 /* Enable interrupts on all CPUs */
7116 mvpp2_interrupts_enable(port);
7117
Antoine Tenart542897d2017-08-30 10:29:15 +02007118 if (port->priv->hw_version == MVPP22) {
7119 mvpp22_comphy_init(port);
Antoine Ténartf84bf382017-08-22 19:08:27 +02007120 mvpp22_gop_init(port);
Antoine Tenart542897d2017-08-30 10:29:15 +02007121 }
Antoine Ténartf84bf382017-08-22 19:08:27 +02007122
Antoine Ténart2055d622017-08-22 19:08:23 +02007123 mvpp2_port_mii_set(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007124 mvpp2_port_enable(port);
Antoine Tenart5997c862017-09-01 11:04:53 +02007125 if (ndev->phydev)
7126 phy_start(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007127 netif_tx_start_all_queues(port->dev);
7128}
7129
7130/* Set hw internals when stopping port */
7131static void mvpp2_stop_dev(struct mvpp2_port *port)
7132{
Philippe Reynes8e072692016-06-28 00:08:11 +02007133 struct net_device *ndev = port->dev;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007134 int i;
Philippe Reynes8e072692016-06-28 00:08:11 +02007135
Marcin Wojtas3f518502014-07-10 16:52:13 -03007136 /* Stop new packets from arriving to RXQs */
7137 mvpp2_ingress_disable(port);
7138
7139 mdelay(10);
7140
7141 /* Disable interrupts on all CPUs */
7142 mvpp2_interrupts_disable(port);
7143
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007144 for (i = 0; i < port->nqvecs; i++)
7145 napi_disable(&port->qvecs[i].napi);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007146
7147 netif_carrier_off(port->dev);
7148 netif_tx_stop_all_queues(port->dev);
7149
7150 mvpp2_egress_disable(port);
7151 mvpp2_port_disable(port);
Antoine Tenart5997c862017-09-01 11:04:53 +02007152 if (ndev->phydev)
7153 phy_stop(ndev->phydev);
Antoine Tenart542897d2017-08-30 10:29:15 +02007154 phy_power_off(port->comphy);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007155}
7156
Marcin Wojtas3f518502014-07-10 16:52:13 -03007157static int mvpp2_check_ringparam_valid(struct net_device *dev,
7158 struct ethtool_ringparam *ring)
7159{
7160 u16 new_rx_pending = ring->rx_pending;
7161 u16 new_tx_pending = ring->tx_pending;
7162
7163 if (ring->rx_pending == 0 || ring->tx_pending == 0)
7164 return -EINVAL;
7165
Yan Markman7cf87e42017-12-11 09:13:26 +01007166 if (ring->rx_pending > MVPP2_MAX_RXD_MAX)
7167 new_rx_pending = MVPP2_MAX_RXD_MAX;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007168 else if (!IS_ALIGNED(ring->rx_pending, 16))
7169 new_rx_pending = ALIGN(ring->rx_pending, 16);
7170
Yan Markman7cf87e42017-12-11 09:13:26 +01007171 if (ring->tx_pending > MVPP2_MAX_TXD_MAX)
7172 new_tx_pending = MVPP2_MAX_TXD_MAX;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007173 else if (!IS_ALIGNED(ring->tx_pending, 32))
7174 new_tx_pending = ALIGN(ring->tx_pending, 32);
7175
Antoine Tenart76e583c2017-11-28 14:19:51 +01007176 /* The Tx ring size cannot be smaller than the minimum number of
7177 * descriptors needed for TSO.
7178 */
7179 if (new_tx_pending < MVPP2_MAX_SKB_DESCS)
7180 new_tx_pending = ALIGN(MVPP2_MAX_SKB_DESCS, 32);
7181
Marcin Wojtas3f518502014-07-10 16:52:13 -03007182 if (ring->rx_pending != new_rx_pending) {
7183 netdev_info(dev, "illegal Rx ring size value %d, round to %d\n",
7184 ring->rx_pending, new_rx_pending);
7185 ring->rx_pending = new_rx_pending;
7186 }
7187
7188 if (ring->tx_pending != new_tx_pending) {
7189 netdev_info(dev, "illegal Tx ring size value %d, round to %d\n",
7190 ring->tx_pending, new_tx_pending);
7191 ring->tx_pending = new_tx_pending;
7192 }
7193
7194 return 0;
7195}
7196
Thomas Petazzoni26975822017-03-07 16:53:14 +01007197static void mvpp21_get_mac_address(struct mvpp2_port *port, unsigned char *addr)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007198{
7199 u32 mac_addr_l, mac_addr_m, mac_addr_h;
7200
7201 mac_addr_l = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
7202 mac_addr_m = readl(port->priv->lms_base + MVPP2_SRC_ADDR_MIDDLE);
7203 mac_addr_h = readl(port->priv->lms_base + MVPP2_SRC_ADDR_HIGH);
7204 addr[0] = (mac_addr_h >> 24) & 0xFF;
7205 addr[1] = (mac_addr_h >> 16) & 0xFF;
7206 addr[2] = (mac_addr_h >> 8) & 0xFF;
7207 addr[3] = mac_addr_h & 0xFF;
7208 addr[4] = mac_addr_m & 0xFF;
7209 addr[5] = (mac_addr_l >> MVPP2_GMAC_SA_LOW_OFFS) & 0xFF;
7210}
7211
7212static int mvpp2_phy_connect(struct mvpp2_port *port)
7213{
7214 struct phy_device *phy_dev;
7215
Antoine Tenart5997c862017-09-01 11:04:53 +02007216 /* No PHY is attached */
7217 if (!port->phy_node)
7218 return 0;
7219
Marcin Wojtas3f518502014-07-10 16:52:13 -03007220 phy_dev = of_phy_connect(port->dev, port->phy_node, mvpp2_link_event, 0,
7221 port->phy_interface);
7222 if (!phy_dev) {
7223 netdev_err(port->dev, "cannot connect to phy\n");
7224 return -ENODEV;
7225 }
7226 phy_dev->supported &= PHY_GBIT_FEATURES;
7227 phy_dev->advertising = phy_dev->supported;
7228
Marcin Wojtas3f518502014-07-10 16:52:13 -03007229 port->link = 0;
7230 port->duplex = 0;
7231 port->speed = 0;
7232
7233 return 0;
7234}
7235
7236static void mvpp2_phy_disconnect(struct mvpp2_port *port)
7237{
Philippe Reynes8e072692016-06-28 00:08:11 +02007238 struct net_device *ndev = port->dev;
7239
Antoine Tenart5997c862017-09-01 11:04:53 +02007240 if (!ndev->phydev)
7241 return;
7242
Philippe Reynes8e072692016-06-28 00:08:11 +02007243 phy_disconnect(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007244}
7245
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007246static int mvpp2_irqs_init(struct mvpp2_port *port)
7247{
7248 int err, i;
7249
7250 for (i = 0; i < port->nqvecs; i++) {
7251 struct mvpp2_queue_vector *qv = port->qvecs + i;
7252
Marc Zyngier13c249a2017-11-04 12:33:47 +00007253 if (qv->type == MVPP2_QUEUE_VECTOR_PRIVATE)
7254 irq_set_status_flags(qv->irq, IRQ_NO_BALANCING);
7255
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007256 err = request_irq(qv->irq, mvpp2_isr, 0, port->dev->name, qv);
7257 if (err)
7258 goto err;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007259
7260 if (qv->type == MVPP2_QUEUE_VECTOR_PRIVATE)
7261 irq_set_affinity_hint(qv->irq,
7262 cpumask_of(qv->sw_thread_id));
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007263 }
7264
7265 return 0;
7266err:
7267 for (i = 0; i < port->nqvecs; i++) {
7268 struct mvpp2_queue_vector *qv = port->qvecs + i;
7269
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007270 irq_set_affinity_hint(qv->irq, NULL);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007271 free_irq(qv->irq, qv);
7272 }
7273
7274 return err;
7275}
7276
7277static void mvpp2_irqs_deinit(struct mvpp2_port *port)
7278{
7279 int i;
7280
7281 for (i = 0; i < port->nqvecs; i++) {
7282 struct mvpp2_queue_vector *qv = port->qvecs + i;
7283
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007284 irq_set_affinity_hint(qv->irq, NULL);
Marc Zyngier13c249a2017-11-04 12:33:47 +00007285 irq_clear_status_flags(qv->irq, IRQ_NO_BALANCING);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007286 free_irq(qv->irq, qv);
7287 }
7288}
7289
Antoine Tenart1d7d15d2017-10-30 11:23:30 +01007290static void mvpp22_init_rss(struct mvpp2_port *port)
7291{
7292 struct mvpp2 *priv = port->priv;
7293 int i;
7294
7295 /* Set the table width: replace the whole classifier Rx queue number
7296 * with the ones configured in RSS table entries.
7297 */
7298 mvpp2_write(priv, MVPP22_RSS_INDEX, MVPP22_RSS_INDEX_TABLE(0));
7299 mvpp2_write(priv, MVPP22_RSS_WIDTH, 8);
7300
7301 /* Loop through the classifier Rx Queues and map them to a RSS table.
7302 * Map them all to the first table (0) by default.
7303 */
7304 for (i = 0; i < MVPP2_CLS_RX_QUEUES; i++) {
7305 mvpp2_write(priv, MVPP22_RSS_INDEX, MVPP22_RSS_INDEX_QUEUE(i));
7306 mvpp2_write(priv, MVPP22_RSS_TABLE,
7307 MVPP22_RSS_TABLE_POINTER(0));
7308 }
7309
7310 /* Configure the first table to evenly distribute the packets across
7311 * real Rx Queues. The table entries map a hash to an port Rx Queue.
7312 */
7313 for (i = 0; i < MVPP22_RSS_TABLE_ENTRIES; i++) {
7314 u32 sel = MVPP22_RSS_INDEX_TABLE(0) |
7315 MVPP22_RSS_INDEX_TABLE_ENTRY(i);
7316 mvpp2_write(priv, MVPP22_RSS_INDEX, sel);
7317
7318 mvpp2_write(priv, MVPP22_RSS_TABLE_ENTRY, i % port->nrxqs);
7319 }
7320
7321}
7322
Marcin Wojtas3f518502014-07-10 16:52:13 -03007323static int mvpp2_open(struct net_device *dev)
7324{
7325 struct mvpp2_port *port = netdev_priv(dev);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007326 struct mvpp2 *priv = port->priv;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007327 unsigned char mac_bcast[ETH_ALEN] = {
7328 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7329 int err;
7330
7331 err = mvpp2_prs_mac_da_accept(port->priv, port->id, mac_bcast, true);
7332 if (err) {
7333 netdev_err(dev, "mvpp2_prs_mac_da_accept BC failed\n");
7334 return err;
7335 }
7336 err = mvpp2_prs_mac_da_accept(port->priv, port->id,
7337 dev->dev_addr, true);
7338 if (err) {
7339 netdev_err(dev, "mvpp2_prs_mac_da_accept MC failed\n");
7340 return err;
7341 }
7342 err = mvpp2_prs_tag_mode_set(port->priv, port->id, MVPP2_TAG_TYPE_MH);
7343 if (err) {
7344 netdev_err(dev, "mvpp2_prs_tag_mode_set failed\n");
7345 return err;
7346 }
7347 err = mvpp2_prs_def_flow(port);
7348 if (err) {
7349 netdev_err(dev, "mvpp2_prs_def_flow failed\n");
7350 return err;
7351 }
7352
7353 /* Allocate the Rx/Tx queues */
7354 err = mvpp2_setup_rxqs(port);
7355 if (err) {
7356 netdev_err(port->dev, "cannot allocate Rx queues\n");
7357 return err;
7358 }
7359
7360 err = mvpp2_setup_txqs(port);
7361 if (err) {
7362 netdev_err(port->dev, "cannot allocate Tx queues\n");
7363 goto err_cleanup_rxqs;
7364 }
7365
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007366 err = mvpp2_irqs_init(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007367 if (err) {
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007368 netdev_err(port->dev, "cannot init IRQs\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03007369 goto err_cleanup_txqs;
7370 }
7371
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007372 if (priv->hw_version == MVPP22 && !port->phy_node && port->link_irq) {
7373 err = request_irq(port->link_irq, mvpp2_link_status_isr, 0,
7374 dev->name, port);
7375 if (err) {
7376 netdev_err(port->dev, "cannot request link IRQ %d\n",
7377 port->link_irq);
7378 goto err_free_irq;
7379 }
7380
7381 mvpp22_gop_setup_irq(port);
7382 }
7383
Marcin Wojtas3f518502014-07-10 16:52:13 -03007384 /* In default link is down */
7385 netif_carrier_off(port->dev);
7386
7387 err = mvpp2_phy_connect(port);
7388 if (err < 0)
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007389 goto err_free_link_irq;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007390
7391 /* Unmask interrupts on all CPUs */
7392 on_each_cpu(mvpp2_interrupts_unmask, port, 1);
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007393 mvpp2_shared_interrupt_mask_unmask(port, false);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007394
7395 mvpp2_start_dev(port);
7396
Antoine Tenart1d7d15d2017-10-30 11:23:30 +01007397 if (priv->hw_version == MVPP22)
7398 mvpp22_init_rss(port);
7399
Miquel Raynal118d6292017-11-06 22:56:53 +01007400 /* Start hardware statistics gathering */
Miquel Raynale5c500e2017-11-08 08:59:40 +01007401 queue_delayed_work(priv->stats_queue, &port->stats_work,
Miquel Raynal118d6292017-11-06 22:56:53 +01007402 MVPP2_MIB_COUNTERS_STATS_DELAY);
7403
Marcin Wojtas3f518502014-07-10 16:52:13 -03007404 return 0;
7405
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007406err_free_link_irq:
7407 if (priv->hw_version == MVPP22 && !port->phy_node && port->link_irq)
7408 free_irq(port->link_irq, port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007409err_free_irq:
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007410 mvpp2_irqs_deinit(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007411err_cleanup_txqs:
7412 mvpp2_cleanup_txqs(port);
7413err_cleanup_rxqs:
7414 mvpp2_cleanup_rxqs(port);
7415 return err;
7416}
7417
7418static int mvpp2_stop(struct net_device *dev)
7419{
7420 struct mvpp2_port *port = netdev_priv(dev);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007421 struct mvpp2_port_pcpu *port_pcpu;
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007422 struct mvpp2 *priv = port->priv;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007423 int cpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007424
7425 mvpp2_stop_dev(port);
7426 mvpp2_phy_disconnect(port);
7427
7428 /* Mask interrupts on all CPUs */
7429 on_each_cpu(mvpp2_interrupts_mask, port, 1);
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007430 mvpp2_shared_interrupt_mask_unmask(port, true);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007431
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007432 if (priv->hw_version == MVPP22 && !port->phy_node && port->link_irq)
7433 free_irq(port->link_irq, port);
7434
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007435 mvpp2_irqs_deinit(port);
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007436 if (!port->has_tx_irqs) {
7437 for_each_present_cpu(cpu) {
7438 port_pcpu = per_cpu_ptr(port->pcpu, cpu);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007439
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007440 hrtimer_cancel(&port_pcpu->tx_done_timer);
7441 port_pcpu->timer_scheduled = false;
7442 tasklet_kill(&port_pcpu->tx_done_tasklet);
7443 }
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007444 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03007445 mvpp2_cleanup_rxqs(port);
7446 mvpp2_cleanup_txqs(port);
7447
Miquel Raynale5c500e2017-11-08 08:59:40 +01007448 cancel_delayed_work_sync(&port->stats_work);
Miquel Raynal118d6292017-11-06 22:56:53 +01007449
Marcin Wojtas3f518502014-07-10 16:52:13 -03007450 return 0;
7451}
7452
7453static void mvpp2_set_rx_mode(struct net_device *dev)
7454{
7455 struct mvpp2_port *port = netdev_priv(dev);
7456 struct mvpp2 *priv = port->priv;
7457 struct netdev_hw_addr *ha;
7458 int id = port->id;
7459 bool allmulti = dev->flags & IFF_ALLMULTI;
7460
Mikulas Patocka7ac8ff92018-02-11 18:10:28 -05007461retry:
Marcin Wojtas3f518502014-07-10 16:52:13 -03007462 mvpp2_prs_mac_promisc_set(priv, id, dev->flags & IFF_PROMISC);
7463 mvpp2_prs_mac_multi_set(priv, id, MVPP2_PE_MAC_MC_ALL, allmulti);
7464 mvpp2_prs_mac_multi_set(priv, id, MVPP2_PE_MAC_MC_IP6, allmulti);
7465
7466 /* Remove all port->id's mcast enries */
7467 mvpp2_prs_mcast_del_all(priv, id);
7468
Mikulas Patocka7ac8ff92018-02-11 18:10:28 -05007469 if (!allmulti) {
7470 netdev_for_each_mc_addr(ha, dev) {
7471 if (mvpp2_prs_mac_da_accept(priv, id, ha->addr, true)) {
7472 allmulti = true;
7473 goto retry;
7474 }
7475 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03007476 }
Maxime Chevallier56beda32018-02-28 10:14:13 +01007477
7478 /* Disable VLAN filtering in promiscuous mode */
7479 if (dev->flags & IFF_PROMISC)
7480 mvpp2_prs_vid_disable_filtering(port);
7481 else
7482 mvpp2_prs_vid_enable_filtering(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007483}
7484
7485static int mvpp2_set_mac_address(struct net_device *dev, void *p)
7486{
7487 struct mvpp2_port *port = netdev_priv(dev);
7488 const struct sockaddr *addr = p;
7489 int err;
7490
7491 if (!is_valid_ether_addr(addr->sa_data)) {
7492 err = -EADDRNOTAVAIL;
Markus Elfringc1175542017-04-17 11:10:47 +02007493 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007494 }
7495
7496 if (!netif_running(dev)) {
7497 err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
7498 if (!err)
7499 return 0;
7500 /* Reconfigure parser to accept the original MAC address */
7501 err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
7502 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02007503 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007504 }
7505
7506 mvpp2_stop_dev(port);
7507
7508 err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
7509 if (!err)
7510 goto out_start;
7511
7512 /* Reconfigure parser accept the original MAC address */
7513 err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
7514 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02007515 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007516out_start:
7517 mvpp2_start_dev(port);
7518 mvpp2_egress_enable(port);
7519 mvpp2_ingress_enable(port);
7520 return 0;
Markus Elfringc1175542017-04-17 11:10:47 +02007521log_error:
Markus Elfringdfd42402017-04-17 11:20:41 +02007522 netdev_err(dev, "failed to change MAC address\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03007523 return err;
7524}
7525
7526static int mvpp2_change_mtu(struct net_device *dev, int mtu)
7527{
7528 struct mvpp2_port *port = netdev_priv(dev);
7529 int err;
7530
Jarod Wilson57779872016-10-17 15:54:06 -04007531 if (!IS_ALIGNED(MVPP2_RX_PKT_SIZE(mtu), 8)) {
7532 netdev_info(dev, "illegal MTU value %d, round to %d\n", mtu,
7533 ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8));
7534 mtu = ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007535 }
7536
7537 if (!netif_running(dev)) {
7538 err = mvpp2_bm_update_mtu(dev, mtu);
7539 if (!err) {
7540 port->pkt_size = MVPP2_RX_PKT_SIZE(mtu);
7541 return 0;
7542 }
7543
7544 /* Reconfigure BM to the original MTU */
7545 err = mvpp2_bm_update_mtu(dev, dev->mtu);
7546 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02007547 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007548 }
7549
7550 mvpp2_stop_dev(port);
7551
7552 err = mvpp2_bm_update_mtu(dev, mtu);
7553 if (!err) {
7554 port->pkt_size = MVPP2_RX_PKT_SIZE(mtu);
7555 goto out_start;
7556 }
7557
7558 /* Reconfigure BM to the original MTU */
7559 err = mvpp2_bm_update_mtu(dev, dev->mtu);
7560 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02007561 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007562
7563out_start:
7564 mvpp2_start_dev(port);
7565 mvpp2_egress_enable(port);
7566 mvpp2_ingress_enable(port);
7567
7568 return 0;
Markus Elfringc1175542017-04-17 11:10:47 +02007569log_error:
Markus Elfringdfd42402017-04-17 11:20:41 +02007570 netdev_err(dev, "failed to change MTU\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03007571 return err;
7572}
7573
stephen hemmingerbc1f4472017-01-06 19:12:52 -08007574static void
Marcin Wojtas3f518502014-07-10 16:52:13 -03007575mvpp2_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
7576{
7577 struct mvpp2_port *port = netdev_priv(dev);
7578 unsigned int start;
7579 int cpu;
7580
7581 for_each_possible_cpu(cpu) {
7582 struct mvpp2_pcpu_stats *cpu_stats;
7583 u64 rx_packets;
7584 u64 rx_bytes;
7585 u64 tx_packets;
7586 u64 tx_bytes;
7587
7588 cpu_stats = per_cpu_ptr(port->stats, cpu);
7589 do {
7590 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
7591 rx_packets = cpu_stats->rx_packets;
7592 rx_bytes = cpu_stats->rx_bytes;
7593 tx_packets = cpu_stats->tx_packets;
7594 tx_bytes = cpu_stats->tx_bytes;
7595 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
7596
7597 stats->rx_packets += rx_packets;
7598 stats->rx_bytes += rx_bytes;
7599 stats->tx_packets += tx_packets;
7600 stats->tx_bytes += tx_bytes;
7601 }
7602
7603 stats->rx_errors = dev->stats.rx_errors;
7604 stats->rx_dropped = dev->stats.rx_dropped;
7605 stats->tx_dropped = dev->stats.tx_dropped;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007606}
7607
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007608static int mvpp2_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
7609{
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007610 int ret;
7611
Philippe Reynes8e072692016-06-28 00:08:11 +02007612 if (!dev->phydev)
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007613 return -ENOTSUPP;
7614
Philippe Reynes8e072692016-06-28 00:08:11 +02007615 ret = phy_mii_ioctl(dev->phydev, ifr, cmd);
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007616 if (!ret)
7617 mvpp2_link_event(dev);
7618
7619 return ret;
7620}
7621
Maxime Chevallier56beda32018-02-28 10:14:13 +01007622static int mvpp2_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
7623{
7624 struct mvpp2_port *port = netdev_priv(dev);
7625 int ret;
7626
7627 ret = mvpp2_prs_vid_entry_add(port, vid);
7628 if (ret)
7629 netdev_err(dev, "rx-vlan-filter offloading cannot accept more than %d VIDs per port\n",
7630 MVPP2_PRS_VLAN_FILT_MAX - 1);
7631 return ret;
7632}
7633
7634static int mvpp2_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
7635{
7636 struct mvpp2_port *port = netdev_priv(dev);
7637
7638 mvpp2_prs_vid_entry_remove(port, vid);
7639 return 0;
7640}
7641
7642static int mvpp2_set_features(struct net_device *dev,
7643 netdev_features_t features)
7644{
7645 netdev_features_t changed = dev->features ^ features;
7646 struct mvpp2_port *port = netdev_priv(dev);
7647
7648 if (changed & NETIF_F_HW_VLAN_CTAG_FILTER) {
7649 if (features & NETIF_F_HW_VLAN_CTAG_FILTER) {
7650 mvpp2_prs_vid_enable_filtering(port);
7651 } else {
7652 /* Invalidate all registered VID filters for this
7653 * port
7654 */
7655 mvpp2_prs_vid_remove_all(port);
7656
7657 mvpp2_prs_vid_disable_filtering(port);
7658 }
7659 }
7660
7661 return 0;
7662}
7663
Marcin Wojtas3f518502014-07-10 16:52:13 -03007664/* Ethtool methods */
7665
Marcin Wojtas3f518502014-07-10 16:52:13 -03007666/* Set interrupt coalescing for ethtools */
7667static int mvpp2_ethtool_set_coalesce(struct net_device *dev,
7668 struct ethtool_coalesce *c)
7669{
7670 struct mvpp2_port *port = netdev_priv(dev);
7671 int queue;
7672
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007673 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007674 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
7675
7676 rxq->time_coal = c->rx_coalesce_usecs;
7677 rxq->pkts_coal = c->rx_max_coalesced_frames;
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01007678 mvpp2_rx_pkts_coal_set(port, rxq);
7679 mvpp2_rx_time_coal_set(port, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007680 }
7681
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007682 if (port->has_tx_irqs) {
7683 port->tx_time_coal = c->tx_coalesce_usecs;
7684 mvpp2_tx_time_coal_set(port);
7685 }
7686
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007687 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007688 struct mvpp2_tx_queue *txq = port->txqs[queue];
7689
7690 txq->done_pkts_coal = c->tx_max_coalesced_frames;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007691
7692 if (port->has_tx_irqs)
7693 mvpp2_tx_pkts_coal_set(port, txq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007694 }
7695
Marcin Wojtas3f518502014-07-10 16:52:13 -03007696 return 0;
7697}
7698
7699/* get coalescing for ethtools */
7700static int mvpp2_ethtool_get_coalesce(struct net_device *dev,
7701 struct ethtool_coalesce *c)
7702{
7703 struct mvpp2_port *port = netdev_priv(dev);
7704
Antoine Tenart385c2842017-12-11 09:13:27 +01007705 c->rx_coalesce_usecs = port->rxqs[0]->time_coal;
7706 c->rx_max_coalesced_frames = port->rxqs[0]->pkts_coal;
7707 c->tx_max_coalesced_frames = port->txqs[0]->done_pkts_coal;
Antoine Tenart24b28cc2017-12-11 09:13:28 +01007708 c->tx_coalesce_usecs = port->tx_time_coal;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007709 return 0;
7710}
7711
7712static void mvpp2_ethtool_get_drvinfo(struct net_device *dev,
7713 struct ethtool_drvinfo *drvinfo)
7714{
7715 strlcpy(drvinfo->driver, MVPP2_DRIVER_NAME,
7716 sizeof(drvinfo->driver));
7717 strlcpy(drvinfo->version, MVPP2_DRIVER_VERSION,
7718 sizeof(drvinfo->version));
7719 strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
7720 sizeof(drvinfo->bus_info));
7721}
7722
7723static void mvpp2_ethtool_get_ringparam(struct net_device *dev,
7724 struct ethtool_ringparam *ring)
7725{
7726 struct mvpp2_port *port = netdev_priv(dev);
7727
Yan Markman7cf87e42017-12-11 09:13:26 +01007728 ring->rx_max_pending = MVPP2_MAX_RXD_MAX;
7729 ring->tx_max_pending = MVPP2_MAX_TXD_MAX;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007730 ring->rx_pending = port->rx_ring_size;
7731 ring->tx_pending = port->tx_ring_size;
7732}
7733
7734static int mvpp2_ethtool_set_ringparam(struct net_device *dev,
7735 struct ethtool_ringparam *ring)
7736{
7737 struct mvpp2_port *port = netdev_priv(dev);
7738 u16 prev_rx_ring_size = port->rx_ring_size;
7739 u16 prev_tx_ring_size = port->tx_ring_size;
7740 int err;
7741
7742 err = mvpp2_check_ringparam_valid(dev, ring);
7743 if (err)
7744 return err;
7745
7746 if (!netif_running(dev)) {
7747 port->rx_ring_size = ring->rx_pending;
7748 port->tx_ring_size = ring->tx_pending;
7749 return 0;
7750 }
7751
7752 /* The interface is running, so we have to force a
7753 * reallocation of the queues
7754 */
7755 mvpp2_stop_dev(port);
7756 mvpp2_cleanup_rxqs(port);
7757 mvpp2_cleanup_txqs(port);
7758
7759 port->rx_ring_size = ring->rx_pending;
7760 port->tx_ring_size = ring->tx_pending;
7761
7762 err = mvpp2_setup_rxqs(port);
7763 if (err) {
7764 /* Reallocate Rx queues with the original ring size */
7765 port->rx_ring_size = prev_rx_ring_size;
7766 ring->rx_pending = prev_rx_ring_size;
7767 err = mvpp2_setup_rxqs(port);
7768 if (err)
7769 goto err_out;
7770 }
7771 err = mvpp2_setup_txqs(port);
7772 if (err) {
7773 /* Reallocate Tx queues with the original ring size */
7774 port->tx_ring_size = prev_tx_ring_size;
7775 ring->tx_pending = prev_tx_ring_size;
7776 err = mvpp2_setup_txqs(port);
7777 if (err)
7778 goto err_clean_rxqs;
7779 }
7780
7781 mvpp2_start_dev(port);
7782 mvpp2_egress_enable(port);
7783 mvpp2_ingress_enable(port);
7784
7785 return 0;
7786
7787err_clean_rxqs:
7788 mvpp2_cleanup_rxqs(port);
7789err_out:
Markus Elfringdfd42402017-04-17 11:20:41 +02007790 netdev_err(dev, "failed to change ring parameters");
Marcin Wojtas3f518502014-07-10 16:52:13 -03007791 return err;
7792}
7793
7794/* Device ops */
7795
7796static const struct net_device_ops mvpp2_netdev_ops = {
7797 .ndo_open = mvpp2_open,
7798 .ndo_stop = mvpp2_stop,
7799 .ndo_start_xmit = mvpp2_tx,
7800 .ndo_set_rx_mode = mvpp2_set_rx_mode,
7801 .ndo_set_mac_address = mvpp2_set_mac_address,
7802 .ndo_change_mtu = mvpp2_change_mtu,
7803 .ndo_get_stats64 = mvpp2_get_stats64,
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007804 .ndo_do_ioctl = mvpp2_ioctl,
Maxime Chevallier56beda32018-02-28 10:14:13 +01007805 .ndo_vlan_rx_add_vid = mvpp2_vlan_rx_add_vid,
7806 .ndo_vlan_rx_kill_vid = mvpp2_vlan_rx_kill_vid,
7807 .ndo_set_features = mvpp2_set_features,
Marcin Wojtas3f518502014-07-10 16:52:13 -03007808};
7809
7810static const struct ethtool_ops mvpp2_eth_tool_ops = {
Florian Fainelli00606c42016-11-15 11:19:48 -08007811 .nway_reset = phy_ethtool_nway_reset,
Marcin Wojtas3f518502014-07-10 16:52:13 -03007812 .get_link = ethtool_op_get_link,
Marcin Wojtas3f518502014-07-10 16:52:13 -03007813 .set_coalesce = mvpp2_ethtool_set_coalesce,
7814 .get_coalesce = mvpp2_ethtool_get_coalesce,
7815 .get_drvinfo = mvpp2_ethtool_get_drvinfo,
7816 .get_ringparam = mvpp2_ethtool_get_ringparam,
7817 .set_ringparam = mvpp2_ethtool_set_ringparam,
Miquel Raynal118d6292017-11-06 22:56:53 +01007818 .get_strings = mvpp2_ethtool_get_strings,
7819 .get_ethtool_stats = mvpp2_ethtool_get_stats,
7820 .get_sset_count = mvpp2_ethtool_get_sset_count,
Philippe Reynesfb773e92016-06-28 00:08:12 +02007821 .get_link_ksettings = phy_ethtool_get_link_ksettings,
7822 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Marcin Wojtas3f518502014-07-10 16:52:13 -03007823};
7824
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007825/* Used for PPv2.1, or PPv2.2 with the old Device Tree binding that
7826 * had a single IRQ defined per-port.
7827 */
7828static int mvpp2_simple_queue_vectors_init(struct mvpp2_port *port,
7829 struct device_node *port_node)
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007830{
7831 struct mvpp2_queue_vector *v = &port->qvecs[0];
7832
7833 v->first_rxq = 0;
7834 v->nrxqs = port->nrxqs;
7835 v->type = MVPP2_QUEUE_VECTOR_SHARED;
7836 v->sw_thread_id = 0;
7837 v->sw_thread_mask = *cpumask_bits(cpu_online_mask);
7838 v->port = port;
7839 v->irq = irq_of_parse_and_map(port_node, 0);
7840 if (v->irq <= 0)
7841 return -EINVAL;
7842 netif_napi_add(port->dev, &v->napi, mvpp2_poll,
7843 NAPI_POLL_WEIGHT);
7844
7845 port->nqvecs = 1;
7846
7847 return 0;
7848}
7849
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007850static int mvpp2_multi_queue_vectors_init(struct mvpp2_port *port,
7851 struct device_node *port_node)
7852{
7853 struct mvpp2_queue_vector *v;
7854 int i, ret;
7855
7856 port->nqvecs = num_possible_cpus();
7857 if (queue_mode == MVPP2_QDIST_SINGLE_MODE)
7858 port->nqvecs += 1;
7859
7860 for (i = 0; i < port->nqvecs; i++) {
7861 char irqname[16];
7862
7863 v = port->qvecs + i;
7864
7865 v->port = port;
7866 v->type = MVPP2_QUEUE_VECTOR_PRIVATE;
7867 v->sw_thread_id = i;
7868 v->sw_thread_mask = BIT(i);
7869
7870 snprintf(irqname, sizeof(irqname), "tx-cpu%d", i);
7871
7872 if (queue_mode == MVPP2_QDIST_MULTI_MODE) {
7873 v->first_rxq = i * MVPP2_DEFAULT_RXQ;
7874 v->nrxqs = MVPP2_DEFAULT_RXQ;
7875 } else if (queue_mode == MVPP2_QDIST_SINGLE_MODE &&
7876 i == (port->nqvecs - 1)) {
7877 v->first_rxq = 0;
7878 v->nrxqs = port->nrxqs;
7879 v->type = MVPP2_QUEUE_VECTOR_SHARED;
7880 strncpy(irqname, "rx-shared", sizeof(irqname));
7881 }
7882
Marcin Wojtasa75edc72018-01-18 13:31:44 +01007883 if (port_node)
7884 v->irq = of_irq_get_byname(port_node, irqname);
7885 else
7886 v->irq = fwnode_irq_get(port->fwnode, i);
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007887 if (v->irq <= 0) {
7888 ret = -EINVAL;
7889 goto err;
7890 }
7891
7892 netif_napi_add(port->dev, &v->napi, mvpp2_poll,
7893 NAPI_POLL_WEIGHT);
7894 }
7895
7896 return 0;
7897
7898err:
7899 for (i = 0; i < port->nqvecs; i++)
7900 irq_dispose_mapping(port->qvecs[i].irq);
7901 return ret;
7902}
7903
7904static int mvpp2_queue_vectors_init(struct mvpp2_port *port,
7905 struct device_node *port_node)
7906{
7907 if (port->has_tx_irqs)
7908 return mvpp2_multi_queue_vectors_init(port, port_node);
7909 else
7910 return mvpp2_simple_queue_vectors_init(port, port_node);
7911}
7912
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007913static void mvpp2_queue_vectors_deinit(struct mvpp2_port *port)
7914{
7915 int i;
7916
7917 for (i = 0; i < port->nqvecs; i++)
7918 irq_dispose_mapping(port->qvecs[i].irq);
7919}
7920
7921/* Configure Rx queue group interrupt for this port */
7922static void mvpp2_rx_irqs_setup(struct mvpp2_port *port)
7923{
7924 struct mvpp2 *priv = port->priv;
7925 u32 val;
7926 int i;
7927
7928 if (priv->hw_version == MVPP21) {
7929 mvpp2_write(priv, MVPP21_ISR_RXQ_GROUP_REG(port->id),
7930 port->nrxqs);
7931 return;
7932 }
7933
7934 /* Handle the more complicated PPv2.2 case */
7935 for (i = 0; i < port->nqvecs; i++) {
7936 struct mvpp2_queue_vector *qv = port->qvecs + i;
7937
7938 if (!qv->nrxqs)
7939 continue;
7940
7941 val = qv->sw_thread_id;
7942 val |= port->id << MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_OFFSET;
7943 mvpp2_write(priv, MVPP22_ISR_RXQ_GROUP_INDEX_REG, val);
7944
7945 val = qv->first_rxq;
7946 val |= qv->nrxqs << MVPP22_ISR_RXQ_SUB_GROUP_SIZE_OFFSET;
7947 mvpp2_write(priv, MVPP22_ISR_RXQ_SUB_GROUP_CONFIG_REG, val);
7948 }
7949}
7950
Marcin Wojtas3f518502014-07-10 16:52:13 -03007951/* Initialize port HW */
7952static int mvpp2_port_init(struct mvpp2_port *port)
7953{
7954 struct device *dev = port->dev->dev.parent;
7955 struct mvpp2 *priv = port->priv;
7956 struct mvpp2_txq_pcpu *txq_pcpu;
7957 int queue, cpu, err;
7958
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007959 /* Checks for hardware constraints */
7960 if (port->first_rxq + port->nrxqs >
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01007961 MVPP2_MAX_PORTS * priv->max_port_rxqs)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007962 return -EINVAL;
7963
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007964 if (port->nrxqs % 4 || (port->nrxqs > priv->max_port_rxqs) ||
7965 (port->ntxqs > MVPP2_MAX_TXQ))
7966 return -EINVAL;
7967
Marcin Wojtas3f518502014-07-10 16:52:13 -03007968 /* Disable port */
7969 mvpp2_egress_disable(port);
7970 mvpp2_port_disable(port);
7971
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007972 port->tx_time_coal = MVPP2_TXDONE_COAL_USEC;
7973
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007974 port->txqs = devm_kcalloc(dev, port->ntxqs, sizeof(*port->txqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03007975 GFP_KERNEL);
7976 if (!port->txqs)
7977 return -ENOMEM;
7978
7979 /* Associate physical Tx queues to this port and initialize.
7980 * The mapping is predefined.
7981 */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007982 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007983 int queue_phy_id = mvpp2_txq_phys(port->id, queue);
7984 struct mvpp2_tx_queue *txq;
7985
7986 txq = devm_kzalloc(dev, sizeof(*txq), GFP_KERNEL);
Christophe Jaillet177c8d12017-02-19 10:19:57 +01007987 if (!txq) {
7988 err = -ENOMEM;
7989 goto err_free_percpu;
7990 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03007991
7992 txq->pcpu = alloc_percpu(struct mvpp2_txq_pcpu);
7993 if (!txq->pcpu) {
7994 err = -ENOMEM;
7995 goto err_free_percpu;
7996 }
7997
7998 txq->id = queue_phy_id;
7999 txq->log_id = queue;
8000 txq->done_pkts_coal = MVPP2_TXDONE_COAL_PKTS_THRESH;
8001 for_each_present_cpu(cpu) {
8002 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
8003 txq_pcpu->cpu = cpu;
8004 }
8005
8006 port->txqs[queue] = txq;
8007 }
8008
Thomas Petazzoni09f83972017-08-03 10:41:57 +02008009 port->rxqs = devm_kcalloc(dev, port->nrxqs, sizeof(*port->rxqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03008010 GFP_KERNEL);
8011 if (!port->rxqs) {
8012 err = -ENOMEM;
8013 goto err_free_percpu;
8014 }
8015
8016 /* Allocate and initialize Rx queue for this port */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02008017 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03008018 struct mvpp2_rx_queue *rxq;
8019
8020 /* Map physical Rx queue to port's logical Rx queue */
8021 rxq = devm_kzalloc(dev, sizeof(*rxq), GFP_KERNEL);
Jisheng Zhangd82b0c22016-03-31 17:01:23 +08008022 if (!rxq) {
8023 err = -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008024 goto err_free_percpu;
Jisheng Zhangd82b0c22016-03-31 17:01:23 +08008025 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03008026 /* Map this Rx queue to a physical queue */
8027 rxq->id = port->first_rxq + queue;
8028 rxq->port = port->id;
8029 rxq->logic_rxq = queue;
8030
8031 port->rxqs[queue] = rxq;
8032 }
8033
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02008034 mvpp2_rx_irqs_setup(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008035
8036 /* Create Rx descriptor rings */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02008037 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03008038 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
8039
8040 rxq->size = port->rx_ring_size;
8041 rxq->pkts_coal = MVPP2_RX_COAL_PKTS;
8042 rxq->time_coal = MVPP2_RX_COAL_USEC;
8043 }
8044
8045 mvpp2_ingress_disable(port);
8046
8047 /* Port default configuration */
8048 mvpp2_defaults_set(port);
8049
8050 /* Port's classifier configuration */
8051 mvpp2_cls_oversize_rxq_set(port);
8052 mvpp2_cls_port_config(port);
8053
8054 /* Provide an initial Rx packet size */
8055 port->pkt_size = MVPP2_RX_PKT_SIZE(port->dev->mtu);
8056
8057 /* Initialize pools for swf */
8058 err = mvpp2_swf_bm_pool_init(port);
8059 if (err)
8060 goto err_free_percpu;
8061
8062 return 0;
8063
8064err_free_percpu:
Thomas Petazzoni09f83972017-08-03 10:41:57 +02008065 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03008066 if (!port->txqs[queue])
8067 continue;
8068 free_percpu(port->txqs[queue]->pcpu);
8069 }
8070 return err;
8071}
8072
Thomas Petazzoni213f4282017-08-03 10:42:00 +02008073/* Checks if the port DT description has the TX interrupts
8074 * described. On PPv2.1, there are no such interrupts. On PPv2.2,
8075 * there are available, but we need to keep support for old DTs.
8076 */
8077static bool mvpp2_port_has_tx_irqs(struct mvpp2 *priv,
8078 struct device_node *port_node)
8079{
8080 char *irqs[5] = { "rx-shared", "tx-cpu0", "tx-cpu1",
8081 "tx-cpu2", "tx-cpu3" };
8082 int ret, i;
8083
8084 if (priv->hw_version == MVPP21)
8085 return false;
8086
8087 for (i = 0; i < 5; i++) {
8088 ret = of_property_match_string(port_node, "interrupt-names",
8089 irqs[i]);
8090 if (ret < 0)
8091 return false;
8092 }
8093
8094 return true;
8095}
8096
Antoine Tenart3ba8c812017-09-02 11:06:47 +02008097static void mvpp2_port_copy_mac_addr(struct net_device *dev, struct mvpp2 *priv,
Marcin Wojtas24812222018-01-18 13:31:43 +01008098 struct fwnode_handle *fwnode,
Antoine Tenart3ba8c812017-09-02 11:06:47 +02008099 char **mac_from)
8100{
8101 struct mvpp2_port *port = netdev_priv(dev);
8102 char hw_mac_addr[ETH_ALEN] = {0};
Marcin Wojtas24812222018-01-18 13:31:43 +01008103 char fw_mac_addr[ETH_ALEN];
Antoine Tenart3ba8c812017-09-02 11:06:47 +02008104
Marcin Wojtas24812222018-01-18 13:31:43 +01008105 if (fwnode_get_mac_address(fwnode, fw_mac_addr, ETH_ALEN)) {
8106 *mac_from = "firmware node";
8107 ether_addr_copy(dev->dev_addr, fw_mac_addr);
Antoine Tenart688cbaf2017-09-02 11:06:49 +02008108 return;
Antoine Tenart3ba8c812017-09-02 11:06:47 +02008109 }
Antoine Tenart688cbaf2017-09-02 11:06:49 +02008110
8111 if (priv->hw_version == MVPP21) {
8112 mvpp21_get_mac_address(port, hw_mac_addr);
8113 if (is_valid_ether_addr(hw_mac_addr)) {
8114 *mac_from = "hardware";
8115 ether_addr_copy(dev->dev_addr, hw_mac_addr);
8116 return;
8117 }
8118 }
8119
8120 *mac_from = "random";
8121 eth_hw_addr_random(dev);
Antoine Tenart3ba8c812017-09-02 11:06:47 +02008122}
8123
Marcin Wojtas3f518502014-07-10 16:52:13 -03008124/* Ports initialization */
8125static int mvpp2_port_probe(struct platform_device *pdev,
Marcin Wojtas24812222018-01-18 13:31:43 +01008126 struct fwnode_handle *port_fwnode,
Marcin Wojtasbf147152018-01-18 13:31:42 +01008127 struct mvpp2 *priv)
Marcin Wojtas3f518502014-07-10 16:52:13 -03008128{
8129 struct device_node *phy_node;
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008130 struct phy *comphy = NULL;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008131 struct mvpp2_port *port;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02008132 struct mvpp2_port_pcpu *port_pcpu;
Marcin Wojtas24812222018-01-18 13:31:43 +01008133 struct device_node *port_node = to_of_node(port_fwnode);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008134 struct net_device *dev;
8135 struct resource *res;
Antoine Tenart3ba8c812017-09-02 11:06:47 +02008136 char *mac_from = "";
Thomas Petazzoni09f83972017-08-03 10:41:57 +02008137 unsigned int ntxqs, nrxqs;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02008138 bool has_tx_irqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008139 u32 id;
8140 int features;
8141 int phy_mode;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02008142 int err, i, cpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008143
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008144 if (port_node) {
8145 has_tx_irqs = mvpp2_port_has_tx_irqs(priv, port_node);
8146 } else {
8147 has_tx_irqs = true;
8148 queue_mode = MVPP2_QDIST_MULTI_MODE;
8149 }
Thomas Petazzoni213f4282017-08-03 10:42:00 +02008150
8151 if (!has_tx_irqs)
8152 queue_mode = MVPP2_QDIST_SINGLE_MODE;
8153
Thomas Petazzoni09f83972017-08-03 10:41:57 +02008154 ntxqs = MVPP2_MAX_TXQ;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02008155 if (priv->hw_version == MVPP22 && queue_mode == MVPP2_QDIST_MULTI_MODE)
8156 nrxqs = MVPP2_DEFAULT_RXQ * num_possible_cpus();
8157 else
8158 nrxqs = MVPP2_DEFAULT_RXQ;
Thomas Petazzoni09f83972017-08-03 10:41:57 +02008159
8160 dev = alloc_etherdev_mqs(sizeof(*port), ntxqs, nrxqs);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008161 if (!dev)
8162 return -ENOMEM;
8163
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008164 if (port_node)
8165 phy_node = of_parse_phandle(port_node, "phy", 0);
8166 else
8167 phy_node = NULL;
8168
Marcin Wojtas24812222018-01-18 13:31:43 +01008169 phy_mode = fwnode_get_phy_mode(port_fwnode);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008170 if (phy_mode < 0) {
8171 dev_err(&pdev->dev, "incorrect phy mode\n");
8172 err = phy_mode;
8173 goto err_free_netdev;
8174 }
8175
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008176 if (port_node) {
8177 comphy = devm_of_phy_get(&pdev->dev, port_node, NULL);
8178 if (IS_ERR(comphy)) {
8179 if (PTR_ERR(comphy) == -EPROBE_DEFER) {
8180 err = -EPROBE_DEFER;
8181 goto err_free_netdev;
8182 }
8183 comphy = NULL;
Antoine Tenart542897d2017-08-30 10:29:15 +02008184 }
Antoine Tenart542897d2017-08-30 10:29:15 +02008185 }
8186
Marcin Wojtas24812222018-01-18 13:31:43 +01008187 if (fwnode_property_read_u32(port_fwnode, "port-id", &id)) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03008188 err = -EINVAL;
8189 dev_err(&pdev->dev, "missing port-id value\n");
8190 goto err_free_netdev;
8191 }
8192
Yan Markman7cf87e42017-12-11 09:13:26 +01008193 dev->tx_queue_len = MVPP2_MAX_TXD_MAX;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008194 dev->watchdog_timeo = 5 * HZ;
8195 dev->netdev_ops = &mvpp2_netdev_ops;
8196 dev->ethtool_ops = &mvpp2_eth_tool_ops;
8197
8198 port = netdev_priv(dev);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02008199 port->dev = dev;
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008200 port->fwnode = port_fwnode;
Thomas Petazzoni09f83972017-08-03 10:41:57 +02008201 port->ntxqs = ntxqs;
8202 port->nrxqs = nrxqs;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02008203 port->priv = priv;
8204 port->has_tx_irqs = has_tx_irqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008205
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02008206 err = mvpp2_queue_vectors_init(port, port_node);
8207 if (err)
Marcin Wojtas3f518502014-07-10 16:52:13 -03008208 goto err_free_netdev;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008209
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008210 if (port_node)
8211 port->link_irq = of_irq_get_byname(port_node, "link");
8212 else
8213 port->link_irq = fwnode_irq_get(port_fwnode, port->nqvecs + 1);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02008214 if (port->link_irq == -EPROBE_DEFER) {
8215 err = -EPROBE_DEFER;
8216 goto err_deinit_qvecs;
8217 }
8218 if (port->link_irq <= 0)
8219 /* the link irq is optional */
8220 port->link_irq = 0;
8221
Marcin Wojtas24812222018-01-18 13:31:43 +01008222 if (fwnode_property_read_bool(port_fwnode, "marvell,loopback"))
Marcin Wojtas3f518502014-07-10 16:52:13 -03008223 port->flags |= MVPP2_F_LOOPBACK;
8224
Marcin Wojtas3f518502014-07-10 16:52:13 -03008225 port->id = id;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01008226 if (priv->hw_version == MVPP21)
Thomas Petazzoni09f83972017-08-03 10:41:57 +02008227 port->first_rxq = port->id * port->nrxqs;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01008228 else
8229 port->first_rxq = port->id * priv->max_port_rxqs;
8230
Marcin Wojtas3f518502014-07-10 16:52:13 -03008231 port->phy_node = phy_node;
8232 port->phy_interface = phy_mode;
Antoine Tenart542897d2017-08-30 10:29:15 +02008233 port->comphy = comphy;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008234
Thomas Petazzonia7868412017-03-07 16:53:13 +01008235 if (priv->hw_version == MVPP21) {
8236 res = platform_get_resource(pdev, IORESOURCE_MEM, 2 + id);
8237 port->base = devm_ioremap_resource(&pdev->dev, res);
8238 if (IS_ERR(port->base)) {
8239 err = PTR_ERR(port->base);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02008240 goto err_free_irq;
Thomas Petazzonia7868412017-03-07 16:53:13 +01008241 }
Miquel Raynal118d6292017-11-06 22:56:53 +01008242
8243 port->stats_base = port->priv->lms_base +
8244 MVPP21_MIB_COUNTERS_OFFSET +
8245 port->gop_id * MVPP21_MIB_COUNTERS_PORT_SZ;
Thomas Petazzonia7868412017-03-07 16:53:13 +01008246 } else {
Marcin Wojtas24812222018-01-18 13:31:43 +01008247 if (fwnode_property_read_u32(port_fwnode, "gop-port-id",
8248 &port->gop_id)) {
Thomas Petazzonia7868412017-03-07 16:53:13 +01008249 err = -EINVAL;
8250 dev_err(&pdev->dev, "missing gop-port-id value\n");
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02008251 goto err_deinit_qvecs;
Thomas Petazzonia7868412017-03-07 16:53:13 +01008252 }
8253
8254 port->base = priv->iface_base + MVPP22_GMAC_BASE(port->gop_id);
Miquel Raynal118d6292017-11-06 22:56:53 +01008255 port->stats_base = port->priv->iface_base +
8256 MVPP22_MIB_COUNTERS_OFFSET +
8257 port->gop_id * MVPP22_MIB_COUNTERS_PORT_SZ;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008258 }
8259
Miquel Raynal118d6292017-11-06 22:56:53 +01008260 /* Alloc per-cpu and ethtool stats */
Marcin Wojtas3f518502014-07-10 16:52:13 -03008261 port->stats = netdev_alloc_pcpu_stats(struct mvpp2_pcpu_stats);
8262 if (!port->stats) {
8263 err = -ENOMEM;
Antoine Tenartfd3651b2017-09-01 11:04:54 +02008264 goto err_free_irq;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008265 }
8266
Miquel Raynal118d6292017-11-06 22:56:53 +01008267 port->ethtool_stats = devm_kcalloc(&pdev->dev,
8268 ARRAY_SIZE(mvpp2_ethtool_regs),
8269 sizeof(u64), GFP_KERNEL);
8270 if (!port->ethtool_stats) {
8271 err = -ENOMEM;
8272 goto err_free_stats;
8273 }
8274
Miquel Raynale5c500e2017-11-08 08:59:40 +01008275 mutex_init(&port->gather_stats_lock);
8276 INIT_DELAYED_WORK(&port->stats_work, mvpp2_gather_hw_statistics);
8277
Marcin Wojtas24812222018-01-18 13:31:43 +01008278 mvpp2_port_copy_mac_addr(dev, priv, port_fwnode, &mac_from);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008279
Yan Markman7cf87e42017-12-11 09:13:26 +01008280 port->tx_ring_size = MVPP2_MAX_TXD_DFLT;
8281 port->rx_ring_size = MVPP2_MAX_RXD_DFLT;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008282 SET_NETDEV_DEV(dev, &pdev->dev);
8283
8284 err = mvpp2_port_init(port);
8285 if (err < 0) {
8286 dev_err(&pdev->dev, "failed to init port %d\n", id);
8287 goto err_free_stats;
8288 }
Thomas Petazzoni26975822017-03-07 16:53:14 +01008289
Thomas Petazzoni26975822017-03-07 16:53:14 +01008290 mvpp2_port_periodic_xon_disable(port);
8291
8292 if (priv->hw_version == MVPP21)
8293 mvpp2_port_fc_adv_enable(port);
8294
8295 mvpp2_port_reset(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008296
Marcin Wojtasedc660f2015-08-06 19:00:30 +02008297 port->pcpu = alloc_percpu(struct mvpp2_port_pcpu);
8298 if (!port->pcpu) {
8299 err = -ENOMEM;
8300 goto err_free_txq_pcpu;
8301 }
8302
Thomas Petazzoni213f4282017-08-03 10:42:00 +02008303 if (!port->has_tx_irqs) {
8304 for_each_present_cpu(cpu) {
8305 port_pcpu = per_cpu_ptr(port->pcpu, cpu);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02008306
Thomas Petazzoni213f4282017-08-03 10:42:00 +02008307 hrtimer_init(&port_pcpu->tx_done_timer, CLOCK_MONOTONIC,
8308 HRTIMER_MODE_REL_PINNED);
8309 port_pcpu->tx_done_timer.function = mvpp2_hr_timer_cb;
8310 port_pcpu->timer_scheduled = false;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02008311
Thomas Petazzoni213f4282017-08-03 10:42:00 +02008312 tasklet_init(&port_pcpu->tx_done_tasklet,
8313 mvpp2_tx_proc_cb,
8314 (unsigned long)dev);
8315 }
Marcin Wojtasedc660f2015-08-06 19:00:30 +02008316 }
8317
Antoine Ténart186cd4d2017-08-23 09:46:56 +02008318 features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008319 dev->features = features | NETIF_F_RXCSUM;
Maxime Chevallier56beda32018-02-28 10:14:13 +01008320 dev->hw_features |= features | NETIF_F_RXCSUM | NETIF_F_GRO |
8321 NETIF_F_HW_VLAN_CTAG_FILTER;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008322 dev->vlan_features |= features;
Antoine Tenart1d17db02017-10-30 11:23:31 +01008323 dev->gso_max_segs = MVPP2_MAX_TSO_SEGS;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008324
Jarod Wilson57779872016-10-17 15:54:06 -04008325 /* MTU range: 68 - 9676 */
8326 dev->min_mtu = ETH_MIN_MTU;
8327 /* 9676 == 9700 - 20 and rounding to 8 */
8328 dev->max_mtu = 9676;
8329
Marcin Wojtas3f518502014-07-10 16:52:13 -03008330 err = register_netdev(dev);
8331 if (err < 0) {
8332 dev_err(&pdev->dev, "failed to register netdev\n");
Marcin Wojtasedc660f2015-08-06 19:00:30 +02008333 goto err_free_port_pcpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008334 }
8335 netdev_info(dev, "Using %s mac address %pM\n", mac_from, dev->dev_addr);
8336
Marcin Wojtasbf147152018-01-18 13:31:42 +01008337 priv->port_list[priv->port_count++] = port;
8338
Marcin Wojtas3f518502014-07-10 16:52:13 -03008339 return 0;
8340
Marcin Wojtasedc660f2015-08-06 19:00:30 +02008341err_free_port_pcpu:
8342 free_percpu(port->pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008343err_free_txq_pcpu:
Thomas Petazzoni09f83972017-08-03 10:41:57 +02008344 for (i = 0; i < port->ntxqs; i++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03008345 free_percpu(port->txqs[i]->pcpu);
8346err_free_stats:
8347 free_percpu(port->stats);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02008348err_free_irq:
8349 if (port->link_irq)
8350 irq_dispose_mapping(port->link_irq);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02008351err_deinit_qvecs:
8352 mvpp2_queue_vectors_deinit(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008353err_free_netdev:
Peter Chenccb80392016-08-01 15:02:37 +08008354 of_node_put(phy_node);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008355 free_netdev(dev);
8356 return err;
8357}
8358
8359/* Ports removal routine */
8360static void mvpp2_port_remove(struct mvpp2_port *port)
8361{
8362 int i;
8363
8364 unregister_netdev(port->dev);
Peter Chenccb80392016-08-01 15:02:37 +08008365 of_node_put(port->phy_node);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02008366 free_percpu(port->pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008367 free_percpu(port->stats);
Thomas Petazzoni09f83972017-08-03 10:41:57 +02008368 for (i = 0; i < port->ntxqs; i++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03008369 free_percpu(port->txqs[i]->pcpu);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02008370 mvpp2_queue_vectors_deinit(port);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02008371 if (port->link_irq)
8372 irq_dispose_mapping(port->link_irq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008373 free_netdev(port->dev);
8374}
8375
8376/* Initialize decoding windows */
8377static void mvpp2_conf_mbus_windows(const struct mbus_dram_target_info *dram,
8378 struct mvpp2 *priv)
8379{
8380 u32 win_enable;
8381 int i;
8382
8383 for (i = 0; i < 6; i++) {
8384 mvpp2_write(priv, MVPP2_WIN_BASE(i), 0);
8385 mvpp2_write(priv, MVPP2_WIN_SIZE(i), 0);
8386
8387 if (i < 4)
8388 mvpp2_write(priv, MVPP2_WIN_REMAP(i), 0);
8389 }
8390
8391 win_enable = 0;
8392
8393 for (i = 0; i < dram->num_cs; i++) {
8394 const struct mbus_dram_window *cs = dram->cs + i;
8395
8396 mvpp2_write(priv, MVPP2_WIN_BASE(i),
8397 (cs->base & 0xffff0000) | (cs->mbus_attr << 8) |
8398 dram->mbus_dram_target_id);
8399
8400 mvpp2_write(priv, MVPP2_WIN_SIZE(i),
8401 (cs->size - 1) & 0xffff0000);
8402
8403 win_enable |= (1 << i);
8404 }
8405
8406 mvpp2_write(priv, MVPP2_BASE_ADDR_ENABLE, win_enable);
8407}
8408
8409/* Initialize Rx FIFO's */
8410static void mvpp2_rx_fifo_init(struct mvpp2 *priv)
8411{
8412 int port;
8413
8414 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
8415 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
Antoine Tenart2d1d7df2017-10-30 11:23:28 +01008416 MVPP2_RX_FIFO_PORT_DATA_SIZE_4KB);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008417 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
Antoine Tenart2d1d7df2017-10-30 11:23:28 +01008418 MVPP2_RX_FIFO_PORT_ATTR_SIZE_4KB);
8419 }
8420
8421 mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
8422 MVPP2_RX_FIFO_PORT_MIN_PKT);
8423 mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
8424}
8425
8426static void mvpp22_rx_fifo_init(struct mvpp2 *priv)
8427{
8428 int port;
8429
8430 /* The FIFO size parameters are set depending on the maximum speed a
8431 * given port can handle:
8432 * - Port 0: 10Gbps
8433 * - Port 1: 2.5Gbps
8434 * - Ports 2 and 3: 1Gbps
8435 */
8436
8437 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(0),
8438 MVPP2_RX_FIFO_PORT_DATA_SIZE_32KB);
8439 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(0),
8440 MVPP2_RX_FIFO_PORT_ATTR_SIZE_32KB);
8441
8442 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(1),
8443 MVPP2_RX_FIFO_PORT_DATA_SIZE_8KB);
8444 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(1),
8445 MVPP2_RX_FIFO_PORT_ATTR_SIZE_8KB);
8446
8447 for (port = 2; port < MVPP2_MAX_PORTS; port++) {
8448 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
8449 MVPP2_RX_FIFO_PORT_DATA_SIZE_4KB);
8450 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
8451 MVPP2_RX_FIFO_PORT_ATTR_SIZE_4KB);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008452 }
8453
8454 mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
8455 MVPP2_RX_FIFO_PORT_MIN_PKT);
8456 mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
8457}
8458
Antoine Tenart7c10f972017-10-30 11:23:29 +01008459/* Initialize Tx FIFO's */
8460static void mvpp22_tx_fifo_init(struct mvpp2 *priv)
8461{
8462 int port;
8463
8464 for (port = 0; port < MVPP2_MAX_PORTS; port++)
8465 mvpp2_write(priv, MVPP22_TX_FIFO_SIZE_REG(port),
8466 MVPP22_TX_FIFO_DATA_SIZE_3KB);
8467}
8468
Thomas Petazzoni6763ce32017-03-07 16:53:15 +01008469static void mvpp2_axi_init(struct mvpp2 *priv)
8470{
8471 u32 val, rdval, wrval;
8472
8473 mvpp2_write(priv, MVPP22_BM_ADDR_HIGH_RLS_REG, 0x0);
8474
8475 /* AXI Bridge Configuration */
8476
8477 rdval = MVPP22_AXI_CODE_CACHE_RD_CACHE
8478 << MVPP22_AXI_ATTR_CACHE_OFFS;
8479 rdval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
8480 << MVPP22_AXI_ATTR_DOMAIN_OFFS;
8481
8482 wrval = MVPP22_AXI_CODE_CACHE_WR_CACHE
8483 << MVPP22_AXI_ATTR_CACHE_OFFS;
8484 wrval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
8485 << MVPP22_AXI_ATTR_DOMAIN_OFFS;
8486
8487 /* BM */
8488 mvpp2_write(priv, MVPP22_AXI_BM_WR_ATTR_REG, wrval);
8489 mvpp2_write(priv, MVPP22_AXI_BM_RD_ATTR_REG, rdval);
8490
8491 /* Descriptors */
8492 mvpp2_write(priv, MVPP22_AXI_AGGRQ_DESCR_RD_ATTR_REG, rdval);
8493 mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_WR_ATTR_REG, wrval);
8494 mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_RD_ATTR_REG, rdval);
8495 mvpp2_write(priv, MVPP22_AXI_RXQ_DESCR_WR_ATTR_REG, wrval);
8496
8497 /* Buffer Data */
8498 mvpp2_write(priv, MVPP22_AXI_TX_DATA_RD_ATTR_REG, rdval);
8499 mvpp2_write(priv, MVPP22_AXI_RX_DATA_WR_ATTR_REG, wrval);
8500
8501 val = MVPP22_AXI_CODE_CACHE_NON_CACHE
8502 << MVPP22_AXI_CODE_CACHE_OFFS;
8503 val |= MVPP22_AXI_CODE_DOMAIN_SYSTEM
8504 << MVPP22_AXI_CODE_DOMAIN_OFFS;
8505 mvpp2_write(priv, MVPP22_AXI_RD_NORMAL_CODE_REG, val);
8506 mvpp2_write(priv, MVPP22_AXI_WR_NORMAL_CODE_REG, val);
8507
8508 val = MVPP22_AXI_CODE_CACHE_RD_CACHE
8509 << MVPP22_AXI_CODE_CACHE_OFFS;
8510 val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
8511 << MVPP22_AXI_CODE_DOMAIN_OFFS;
8512
8513 mvpp2_write(priv, MVPP22_AXI_RD_SNOOP_CODE_REG, val);
8514
8515 val = MVPP22_AXI_CODE_CACHE_WR_CACHE
8516 << MVPP22_AXI_CODE_CACHE_OFFS;
8517 val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
8518 << MVPP22_AXI_CODE_DOMAIN_OFFS;
8519
8520 mvpp2_write(priv, MVPP22_AXI_WR_SNOOP_CODE_REG, val);
8521}
8522
Marcin Wojtas3f518502014-07-10 16:52:13 -03008523/* Initialize network controller common part HW */
8524static int mvpp2_init(struct platform_device *pdev, struct mvpp2 *priv)
8525{
8526 const struct mbus_dram_target_info *dram_target_info;
8527 int err, i;
Marcin Wojtas08a23752014-07-21 13:48:12 -03008528 u32 val;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008529
Marcin Wojtas3f518502014-07-10 16:52:13 -03008530 /* MBUS windows configuration */
8531 dram_target_info = mv_mbus_dram_info();
8532 if (dram_target_info)
8533 mvpp2_conf_mbus_windows(dram_target_info, priv);
8534
Thomas Petazzoni6763ce32017-03-07 16:53:15 +01008535 if (priv->hw_version == MVPP22)
8536 mvpp2_axi_init(priv);
8537
Marcin Wojtas08a23752014-07-21 13:48:12 -03008538 /* Disable HW PHY polling */
Thomas Petazzoni26975822017-03-07 16:53:14 +01008539 if (priv->hw_version == MVPP21) {
8540 val = readl(priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
8541 val |= MVPP2_PHY_AN_STOP_SMI0_MASK;
8542 writel(val, priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
8543 } else {
8544 val = readl(priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
8545 val &= ~MVPP22_SMI_POLLING_EN;
8546 writel(val, priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
8547 }
Marcin Wojtas08a23752014-07-21 13:48:12 -03008548
Marcin Wojtas3f518502014-07-10 16:52:13 -03008549 /* Allocate and initialize aggregated TXQs */
8550 priv->aggr_txqs = devm_kcalloc(&pdev->dev, num_present_cpus(),
Markus Elfringd7ce3ce2017-04-17 08:48:23 +02008551 sizeof(*priv->aggr_txqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03008552 GFP_KERNEL);
8553 if (!priv->aggr_txqs)
8554 return -ENOMEM;
8555
8556 for_each_present_cpu(i) {
8557 priv->aggr_txqs[i].id = i;
8558 priv->aggr_txqs[i].size = MVPP2_AGGR_TXQ_SIZE;
Antoine Ténart85affd72017-08-23 09:46:55 +02008559 err = mvpp2_aggr_txq_init(pdev, &priv->aggr_txqs[i], i, priv);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008560 if (err < 0)
8561 return err;
8562 }
8563
Antoine Tenart7c10f972017-10-30 11:23:29 +01008564 /* Fifo Init */
8565 if (priv->hw_version == MVPP21) {
Antoine Tenart2d1d7df2017-10-30 11:23:28 +01008566 mvpp2_rx_fifo_init(priv);
Antoine Tenart7c10f972017-10-30 11:23:29 +01008567 } else {
Antoine Tenart2d1d7df2017-10-30 11:23:28 +01008568 mvpp22_rx_fifo_init(priv);
Antoine Tenart7c10f972017-10-30 11:23:29 +01008569 mvpp22_tx_fifo_init(priv);
8570 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03008571
Thomas Petazzoni26975822017-03-07 16:53:14 +01008572 if (priv->hw_version == MVPP21)
8573 writel(MVPP2_EXT_GLOBAL_CTRL_DEFAULT,
8574 priv->lms_base + MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008575
8576 /* Allow cache snoop when transmiting packets */
8577 mvpp2_write(priv, MVPP2_TX_SNOOP_REG, 0x1);
8578
8579 /* Buffer Manager initialization */
8580 err = mvpp2_bm_init(pdev, priv);
8581 if (err < 0)
8582 return err;
8583
8584 /* Parser default initialization */
8585 err = mvpp2_prs_default_init(pdev, priv);
8586 if (err < 0)
8587 return err;
8588
8589 /* Classifier default initialization */
8590 mvpp2_cls_init(priv);
8591
8592 return 0;
8593}
8594
8595static int mvpp2_probe(struct platform_device *pdev)
8596{
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008597 const struct acpi_device_id *acpi_id;
Marcin Wojtas24812222018-01-18 13:31:43 +01008598 struct fwnode_handle *fwnode = pdev->dev.fwnode;
8599 struct fwnode_handle *port_fwnode;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008600 struct mvpp2 *priv;
8601 struct resource *res;
Thomas Petazzonia7868412017-03-07 16:53:13 +01008602 void __iomem *base;
Miquel Raynal118d6292017-11-06 22:56:53 +01008603 int i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008604 int err;
8605
Markus Elfring0b92e592017-04-17 08:38:32 +02008606 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008607 if (!priv)
8608 return -ENOMEM;
8609
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008610 if (has_acpi_companion(&pdev->dev)) {
8611 acpi_id = acpi_match_device(pdev->dev.driver->acpi_match_table,
8612 &pdev->dev);
8613 priv->hw_version = (unsigned long)acpi_id->driver_data;
8614 } else {
8615 priv->hw_version =
8616 (unsigned long)of_device_get_match_data(&pdev->dev);
8617 }
Thomas Petazzonifaca9242017-03-07 16:53:06 +01008618
Marcin Wojtas3f518502014-07-10 16:52:13 -03008619 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thomas Petazzonia7868412017-03-07 16:53:13 +01008620 base = devm_ioremap_resource(&pdev->dev, res);
8621 if (IS_ERR(base))
8622 return PTR_ERR(base);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008623
Thomas Petazzonia7868412017-03-07 16:53:13 +01008624 if (priv->hw_version == MVPP21) {
8625 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
8626 priv->lms_base = devm_ioremap_resource(&pdev->dev, res);
8627 if (IS_ERR(priv->lms_base))
8628 return PTR_ERR(priv->lms_base);
8629 } else {
8630 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008631 if (has_acpi_companion(&pdev->dev)) {
8632 /* In case the MDIO memory region is declared in
8633 * the ACPI, it can already appear as 'in-use'
8634 * in the OS. Because it is overlapped by second
8635 * region of the network controller, make
8636 * sure it is released, before requesting it again.
8637 * The care is taken by mvpp2 driver to avoid
8638 * concurrent access to this memory region.
8639 */
8640 release_resource(res);
8641 }
Thomas Petazzonia7868412017-03-07 16:53:13 +01008642 priv->iface_base = devm_ioremap_resource(&pdev->dev, res);
8643 if (IS_ERR(priv->iface_base))
8644 return PTR_ERR(priv->iface_base);
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008645 }
Antoine Ténartf84bf382017-08-22 19:08:27 +02008646
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008647 if (priv->hw_version == MVPP22 && dev_of_node(&pdev->dev)) {
Antoine Ténartf84bf382017-08-22 19:08:27 +02008648 priv->sysctrl_base =
8649 syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
8650 "marvell,system-controller");
8651 if (IS_ERR(priv->sysctrl_base))
8652 /* The system controller regmap is optional for dt
8653 * compatibility reasons. When not provided, the
8654 * configuration of the GoP relies on the
8655 * firmware/bootloader.
8656 */
8657 priv->sysctrl_base = NULL;
Thomas Petazzonia7868412017-03-07 16:53:13 +01008658 }
8659
Stefan Chulski01d04932018-03-05 15:16:50 +01008660 mvpp2_setup_bm_pool();
8661
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02008662 for (i = 0; i < MVPP2_MAX_THREADS; i++) {
Thomas Petazzonia7868412017-03-07 16:53:13 +01008663 u32 addr_space_sz;
8664
8665 addr_space_sz = (priv->hw_version == MVPP21 ?
8666 MVPP21_ADDR_SPACE_SZ : MVPP22_ADDR_SPACE_SZ);
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02008667 priv->swth_base[i] = base + i * addr_space_sz;
Thomas Petazzonia7868412017-03-07 16:53:13 +01008668 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03008669
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01008670 if (priv->hw_version == MVPP21)
8671 priv->max_port_rxqs = 8;
8672 else
8673 priv->max_port_rxqs = 32;
8674
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008675 if (dev_of_node(&pdev->dev)) {
8676 priv->pp_clk = devm_clk_get(&pdev->dev, "pp_clk");
8677 if (IS_ERR(priv->pp_clk))
8678 return PTR_ERR(priv->pp_clk);
8679 err = clk_prepare_enable(priv->pp_clk);
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008680 if (err < 0)
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008681 return err;
8682
8683 priv->gop_clk = devm_clk_get(&pdev->dev, "gop_clk");
8684 if (IS_ERR(priv->gop_clk)) {
8685 err = PTR_ERR(priv->gop_clk);
8686 goto err_pp_clk;
8687 }
8688 err = clk_prepare_enable(priv->gop_clk);
8689 if (err < 0)
8690 goto err_pp_clk;
8691
8692 if (priv->hw_version == MVPP22) {
8693 priv->mg_clk = devm_clk_get(&pdev->dev, "mg_clk");
8694 if (IS_ERR(priv->mg_clk)) {
8695 err = PTR_ERR(priv->mg_clk);
8696 goto err_gop_clk;
8697 }
8698
8699 err = clk_prepare_enable(priv->mg_clk);
8700 if (err < 0)
8701 goto err_gop_clk;
8702 }
Gregory CLEMENT4792ea02017-09-29 14:27:39 +02008703
8704 priv->axi_clk = devm_clk_get(&pdev->dev, "axi_clk");
8705 if (IS_ERR(priv->axi_clk)) {
8706 err = PTR_ERR(priv->axi_clk);
8707 if (err == -EPROBE_DEFER)
8708 goto err_gop_clk;
8709 priv->axi_clk = NULL;
8710 } else {
8711 err = clk_prepare_enable(priv->axi_clk);
8712 if (err < 0)
8713 goto err_gop_clk;
8714 }
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008715
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008716 /* Get system's tclk rate */
8717 priv->tclk = clk_get_rate(priv->pp_clk);
8718 } else if (device_property_read_u32(&pdev->dev, "clock-frequency",
8719 &priv->tclk)) {
8720 dev_err(&pdev->dev, "missing clock-frequency value\n");
8721 return -EINVAL;
8722 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03008723
Thomas Petazzoni2067e0a2017-03-07 16:53:19 +01008724 if (priv->hw_version == MVPP22) {
8725 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(40));
8726 if (err)
8727 goto err_mg_clk;
8728 /* Sadly, the BM pools all share the same register to
8729 * store the high 32 bits of their address. So they
8730 * must all have the same high 32 bits, which forces
8731 * us to restrict coherent memory to DMA_BIT_MASK(32).
8732 */
8733 err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
8734 if (err)
8735 goto err_mg_clk;
8736 }
8737
Marcin Wojtas3f518502014-07-10 16:52:13 -03008738 /* Initialize network controller */
8739 err = mvpp2_init(pdev, priv);
8740 if (err < 0) {
8741 dev_err(&pdev->dev, "failed to initialize controller\n");
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008742 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008743 }
8744
Marcin Wojtasbf147152018-01-18 13:31:42 +01008745 /* Initialize ports */
Marcin Wojtas24812222018-01-18 13:31:43 +01008746 fwnode_for_each_available_child_node(fwnode, port_fwnode) {
8747 err = mvpp2_port_probe(pdev, port_fwnode, priv);
Marcin Wojtasbf147152018-01-18 13:31:42 +01008748 if (err < 0)
8749 goto err_port_probe;
8750 }
8751
Miquel Raynal118d6292017-11-06 22:56:53 +01008752 if (priv->port_count == 0) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03008753 dev_err(&pdev->dev, "no ports enabled\n");
Wei Yongjun575a1932014-07-20 22:02:43 +08008754 err = -ENODEV;
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008755 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008756 }
8757
Miquel Raynal118d6292017-11-06 22:56:53 +01008758 /* Statistics must be gathered regularly because some of them (like
8759 * packets counters) are 32-bit registers and could overflow quite
8760 * quickly. For instance, a 10Gb link used at full bandwidth with the
8761 * smallest packets (64B) will overflow a 32-bit counter in less than
8762 * 30 seconds. Then, use a workqueue to fill 64-bit counters.
8763 */
Miquel Raynal118d6292017-11-06 22:56:53 +01008764 snprintf(priv->queue_name, sizeof(priv->queue_name),
8765 "stats-wq-%s%s", netdev_name(priv->port_list[0]->dev),
8766 priv->port_count > 1 ? "+" : "");
8767 priv->stats_queue = create_singlethread_workqueue(priv->queue_name);
8768 if (!priv->stats_queue) {
8769 err = -ENOMEM;
Antoine Tenart26146b02017-11-28 14:19:49 +01008770 goto err_port_probe;
Miquel Raynal118d6292017-11-06 22:56:53 +01008771 }
8772
Marcin Wojtas3f518502014-07-10 16:52:13 -03008773 platform_set_drvdata(pdev, priv);
8774 return 0;
8775
Antoine Tenart26146b02017-11-28 14:19:49 +01008776err_port_probe:
8777 i = 0;
Marcin Wojtas24812222018-01-18 13:31:43 +01008778 fwnode_for_each_available_child_node(fwnode, port_fwnode) {
Antoine Tenart26146b02017-11-28 14:19:49 +01008779 if (priv->port_list[i])
8780 mvpp2_port_remove(priv->port_list[i]);
8781 i++;
8782 }
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008783err_mg_clk:
Gregory CLEMENT4792ea02017-09-29 14:27:39 +02008784 clk_disable_unprepare(priv->axi_clk);
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008785 if (priv->hw_version == MVPP22)
8786 clk_disable_unprepare(priv->mg_clk);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008787err_gop_clk:
8788 clk_disable_unprepare(priv->gop_clk);
8789err_pp_clk:
8790 clk_disable_unprepare(priv->pp_clk);
8791 return err;
8792}
8793
8794static int mvpp2_remove(struct platform_device *pdev)
8795{
8796 struct mvpp2 *priv = platform_get_drvdata(pdev);
Marcin Wojtas24812222018-01-18 13:31:43 +01008797 struct fwnode_handle *fwnode = pdev->dev.fwnode;
8798 struct fwnode_handle *port_fwnode;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008799 int i = 0;
8800
Miquel Raynale5c500e2017-11-08 08:59:40 +01008801 flush_workqueue(priv->stats_queue);
Miquel Raynal118d6292017-11-06 22:56:53 +01008802 destroy_workqueue(priv->stats_queue);
Miquel Raynal118d6292017-11-06 22:56:53 +01008803
Marcin Wojtas24812222018-01-18 13:31:43 +01008804 fwnode_for_each_available_child_node(fwnode, port_fwnode) {
Miquel Raynale5c500e2017-11-08 08:59:40 +01008805 if (priv->port_list[i]) {
8806 mutex_destroy(&priv->port_list[i]->gather_stats_lock);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008807 mvpp2_port_remove(priv->port_list[i]);
Miquel Raynale5c500e2017-11-08 08:59:40 +01008808 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03008809 i++;
8810 }
8811
8812 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
8813 struct mvpp2_bm_pool *bm_pool = &priv->bm_pools[i];
8814
8815 mvpp2_bm_pool_destroy(pdev, priv, bm_pool);
8816 }
8817
8818 for_each_present_cpu(i) {
8819 struct mvpp2_tx_queue *aggr_txq = &priv->aggr_txqs[i];
8820
8821 dma_free_coherent(&pdev->dev,
8822 MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
8823 aggr_txq->descs,
Thomas Petazzoni20396132017-03-07 16:53:00 +01008824 aggr_txq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008825 }
8826
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008827 if (is_acpi_node(port_fwnode))
8828 return 0;
8829
Gregory CLEMENT4792ea02017-09-29 14:27:39 +02008830 clk_disable_unprepare(priv->axi_clk);
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008831 clk_disable_unprepare(priv->mg_clk);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008832 clk_disable_unprepare(priv->pp_clk);
8833 clk_disable_unprepare(priv->gop_clk);
8834
8835 return 0;
8836}
8837
8838static const struct of_device_id mvpp2_match[] = {
Thomas Petazzonifaca9242017-03-07 16:53:06 +01008839 {
8840 .compatible = "marvell,armada-375-pp2",
8841 .data = (void *)MVPP21,
8842 },
Thomas Petazzonifc5e1552017-03-07 16:53:20 +01008843 {
8844 .compatible = "marvell,armada-7k-pp22",
8845 .data = (void *)MVPP22,
8846 },
Marcin Wojtas3f518502014-07-10 16:52:13 -03008847 { }
8848};
8849MODULE_DEVICE_TABLE(of, mvpp2_match);
8850
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008851static const struct acpi_device_id mvpp2_acpi_match[] = {
8852 { "MRVL0110", MVPP22 },
8853 { },
8854};
8855MODULE_DEVICE_TABLE(acpi, mvpp2_acpi_match);
8856
Marcin Wojtas3f518502014-07-10 16:52:13 -03008857static struct platform_driver mvpp2_driver = {
8858 .probe = mvpp2_probe,
8859 .remove = mvpp2_remove,
8860 .driver = {
8861 .name = MVPP2_DRIVER_NAME,
8862 .of_match_table = mvpp2_match,
Marcin Wojtasa75edc72018-01-18 13:31:44 +01008863 .acpi_match_table = ACPI_PTR(mvpp2_acpi_match),
Marcin Wojtas3f518502014-07-10 16:52:13 -03008864 },
8865};
8866
8867module_platform_driver(mvpp2_driver);
8868
8869MODULE_DESCRIPTION("Marvell PPv2 Ethernet Driver - www.marvell.com");
8870MODULE_AUTHOR("Marcin Wojtas <mw@semihalf.com>");
Ezequiel Garciac6340992014-07-14 10:34:47 -03008871MODULE_LICENSE("GPL v2");