blob: 2b0ae35d2168f133d64e32a26a8fa1cfde2a21d0 [file] [log] [blame]
Marcin Wojtas3f518502014-07-10 16:52:13 -03001/*
2 * Driver for Marvell PPv2 network controller for Armada 375 SoC.
3 *
4 * Copyright (C) 2014 Marvell
5 *
6 * Marcin Wojtas <mw@semihalf.com>
7 *
8 * This file is licensed under the terms of the GNU General Public
9 * License version 2. This program is licensed "as is" without any
10 * warranty of any kind, whether express or implied.
11 */
12
13#include <linux/kernel.h>
14#include <linux/netdevice.h>
15#include <linux/etherdevice.h>
16#include <linux/platform_device.h>
17#include <linux/skbuff.h>
18#include <linux/inetdevice.h>
19#include <linux/mbus.h>
20#include <linux/module.h>
Antoine Ténartf84bf382017-08-22 19:08:27 +020021#include <linux/mfd/syscon.h>
Marcin Wojtas3f518502014-07-10 16:52:13 -030022#include <linux/interrupt.h>
23#include <linux/cpumask.h>
24#include <linux/of.h>
25#include <linux/of_irq.h>
26#include <linux/of_mdio.h>
27#include <linux/of_net.h>
28#include <linux/of_address.h>
Thomas Petazzonifaca9242017-03-07 16:53:06 +010029#include <linux/of_device.h>
Marcin Wojtas3f518502014-07-10 16:52:13 -030030#include <linux/phy.h>
Antoine Tenart542897d2017-08-30 10:29:15 +020031#include <linux/phy/phy.h>
Marcin Wojtas3f518502014-07-10 16:52:13 -030032#include <linux/clk.h>
Marcin Wojtasedc660f2015-08-06 19:00:30 +020033#include <linux/hrtimer.h>
34#include <linux/ktime.h>
Antoine Ténartf84bf382017-08-22 19:08:27 +020035#include <linux/regmap.h>
Marcin Wojtas3f518502014-07-10 16:52:13 -030036#include <uapi/linux/ppp_defs.h>
37#include <net/ip.h>
38#include <net/ipv6.h>
Antoine Ténart186cd4d2017-08-23 09:46:56 +020039#include <net/tso.h>
Marcin Wojtas3f518502014-07-10 16:52:13 -030040
41/* RX Fifo Registers */
42#define MVPP2_RX_DATA_FIFO_SIZE_REG(port) (0x00 + 4 * (port))
43#define MVPP2_RX_ATTR_FIFO_SIZE_REG(port) (0x20 + 4 * (port))
44#define MVPP2_RX_MIN_PKT_SIZE_REG 0x60
45#define MVPP2_RX_FIFO_INIT_REG 0x64
46
47/* RX DMA Top Registers */
48#define MVPP2_RX_CTRL_REG(port) (0x140 + 4 * (port))
49#define MVPP2_RX_LOW_LATENCY_PKT_SIZE(s) (((s) & 0xfff) << 16)
50#define MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK BIT(31)
51#define MVPP2_POOL_BUF_SIZE_REG(pool) (0x180 + 4 * (pool))
52#define MVPP2_POOL_BUF_SIZE_OFFSET 5
53#define MVPP2_RXQ_CONFIG_REG(rxq) (0x800 + 4 * (rxq))
54#define MVPP2_SNOOP_PKT_SIZE_MASK 0x1ff
55#define MVPP2_SNOOP_BUF_HDR_MASK BIT(9)
56#define MVPP2_RXQ_POOL_SHORT_OFFS 20
Thomas Petazzoni5eac8922017-03-07 16:53:10 +010057#define MVPP21_RXQ_POOL_SHORT_MASK 0x700000
58#define MVPP22_RXQ_POOL_SHORT_MASK 0xf00000
Marcin Wojtas3f518502014-07-10 16:52:13 -030059#define MVPP2_RXQ_POOL_LONG_OFFS 24
Thomas Petazzoni5eac8922017-03-07 16:53:10 +010060#define MVPP21_RXQ_POOL_LONG_MASK 0x7000000
61#define MVPP22_RXQ_POOL_LONG_MASK 0xf000000
Marcin Wojtas3f518502014-07-10 16:52:13 -030062#define MVPP2_RXQ_PACKET_OFFSET_OFFS 28
63#define MVPP2_RXQ_PACKET_OFFSET_MASK 0x70000000
64#define MVPP2_RXQ_DISABLE_MASK BIT(31)
65
66/* Parser Registers */
67#define MVPP2_PRS_INIT_LOOKUP_REG 0x1000
68#define MVPP2_PRS_PORT_LU_MAX 0xf
69#define MVPP2_PRS_PORT_LU_MASK(port) (0xff << ((port) * 4))
70#define MVPP2_PRS_PORT_LU_VAL(port, val) ((val) << ((port) * 4))
71#define MVPP2_PRS_INIT_OFFS_REG(port) (0x1004 + ((port) & 4))
72#define MVPP2_PRS_INIT_OFF_MASK(port) (0x3f << (((port) % 4) * 8))
73#define MVPP2_PRS_INIT_OFF_VAL(port, val) ((val) << (((port) % 4) * 8))
74#define MVPP2_PRS_MAX_LOOP_REG(port) (0x100c + ((port) & 4))
75#define MVPP2_PRS_MAX_LOOP_MASK(port) (0xff << (((port) % 4) * 8))
76#define MVPP2_PRS_MAX_LOOP_VAL(port, val) ((val) << (((port) % 4) * 8))
77#define MVPP2_PRS_TCAM_IDX_REG 0x1100
78#define MVPP2_PRS_TCAM_DATA_REG(idx) (0x1104 + (idx) * 4)
79#define MVPP2_PRS_TCAM_INV_MASK BIT(31)
80#define MVPP2_PRS_SRAM_IDX_REG 0x1200
81#define MVPP2_PRS_SRAM_DATA_REG(idx) (0x1204 + (idx) * 4)
82#define MVPP2_PRS_TCAM_CTRL_REG 0x1230
83#define MVPP2_PRS_TCAM_EN_MASK BIT(0)
84
85/* Classifier Registers */
86#define MVPP2_CLS_MODE_REG 0x1800
87#define MVPP2_CLS_MODE_ACTIVE_MASK BIT(0)
88#define MVPP2_CLS_PORT_WAY_REG 0x1810
89#define MVPP2_CLS_PORT_WAY_MASK(port) (1 << (port))
90#define MVPP2_CLS_LKP_INDEX_REG 0x1814
91#define MVPP2_CLS_LKP_INDEX_WAY_OFFS 6
92#define MVPP2_CLS_LKP_TBL_REG 0x1818
93#define MVPP2_CLS_LKP_TBL_RXQ_MASK 0xff
94#define MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK BIT(25)
95#define MVPP2_CLS_FLOW_INDEX_REG 0x1820
96#define MVPP2_CLS_FLOW_TBL0_REG 0x1824
97#define MVPP2_CLS_FLOW_TBL1_REG 0x1828
98#define MVPP2_CLS_FLOW_TBL2_REG 0x182c
99#define MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port) (0x1980 + ((port) * 4))
100#define MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS 3
101#define MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK 0x7
102#define MVPP2_CLS_SWFWD_P2HQ_REG(port) (0x19b0 + ((port) * 4))
103#define MVPP2_CLS_SWFWD_PCTRL_REG 0x19d0
104#define MVPP2_CLS_SWFWD_PCTRL_MASK(port) (1 << (port))
105
106/* Descriptor Manager Top Registers */
107#define MVPP2_RXQ_NUM_REG 0x2040
108#define MVPP2_RXQ_DESC_ADDR_REG 0x2044
Thomas Petazzonib02f31f2017-03-07 16:53:12 +0100109#define MVPP22_DESC_ADDR_OFFS 8
Marcin Wojtas3f518502014-07-10 16:52:13 -0300110#define MVPP2_RXQ_DESC_SIZE_REG 0x2048
111#define MVPP2_RXQ_DESC_SIZE_MASK 0x3ff0
112#define MVPP2_RXQ_STATUS_UPDATE_REG(rxq) (0x3000 + 4 * (rxq))
113#define MVPP2_RXQ_NUM_PROCESSED_OFFSET 0
114#define MVPP2_RXQ_NUM_NEW_OFFSET 16
115#define MVPP2_RXQ_STATUS_REG(rxq) (0x3400 + 4 * (rxq))
116#define MVPP2_RXQ_OCCUPIED_MASK 0x3fff
117#define MVPP2_RXQ_NON_OCCUPIED_OFFSET 16
118#define MVPP2_RXQ_NON_OCCUPIED_MASK 0x3fff0000
119#define MVPP2_RXQ_THRESH_REG 0x204c
120#define MVPP2_OCCUPIED_THRESH_OFFSET 0
121#define MVPP2_OCCUPIED_THRESH_MASK 0x3fff
122#define MVPP2_RXQ_INDEX_REG 0x2050
123#define MVPP2_TXQ_NUM_REG 0x2080
124#define MVPP2_TXQ_DESC_ADDR_REG 0x2084
125#define MVPP2_TXQ_DESC_SIZE_REG 0x2088
126#define MVPP2_TXQ_DESC_SIZE_MASK 0x3ff0
Thomas Petazzoni213f4282017-08-03 10:42:00 +0200127#define MVPP2_TXQ_THRESH_REG 0x2094
128#define MVPP2_TXQ_THRESH_OFFSET 16
129#define MVPP2_TXQ_THRESH_MASK 0x3fff
Marcin Wojtas3f518502014-07-10 16:52:13 -0300130#define MVPP2_AGGR_TXQ_UPDATE_REG 0x2090
Marcin Wojtas3f518502014-07-10 16:52:13 -0300131#define MVPP2_TXQ_INDEX_REG 0x2098
132#define MVPP2_TXQ_PREF_BUF_REG 0x209c
133#define MVPP2_PREF_BUF_PTR(desc) ((desc) & 0xfff)
134#define MVPP2_PREF_BUF_SIZE_4 (BIT(12) | BIT(13))
135#define MVPP2_PREF_BUF_SIZE_16 (BIT(12) | BIT(14))
136#define MVPP2_PREF_BUF_THRESH(val) ((val) << 17)
137#define MVPP2_TXQ_DRAIN_EN_MASK BIT(31)
138#define MVPP2_TXQ_PENDING_REG 0x20a0
139#define MVPP2_TXQ_PENDING_MASK 0x3fff
140#define MVPP2_TXQ_INT_STATUS_REG 0x20a4
141#define MVPP2_TXQ_SENT_REG(txq) (0x3c00 + 4 * (txq))
142#define MVPP2_TRANSMITTED_COUNT_OFFSET 16
143#define MVPP2_TRANSMITTED_COUNT_MASK 0x3fff0000
144#define MVPP2_TXQ_RSVD_REQ_REG 0x20b0
145#define MVPP2_TXQ_RSVD_REQ_Q_OFFSET 16
146#define MVPP2_TXQ_RSVD_RSLT_REG 0x20b4
147#define MVPP2_TXQ_RSVD_RSLT_MASK 0x3fff
148#define MVPP2_TXQ_RSVD_CLR_REG 0x20b8
149#define MVPP2_TXQ_RSVD_CLR_OFFSET 16
150#define MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu) (0x2100 + 4 * (cpu))
Thomas Petazzonib02f31f2017-03-07 16:53:12 +0100151#define MVPP22_AGGR_TXQ_DESC_ADDR_OFFS 8
Marcin Wojtas3f518502014-07-10 16:52:13 -0300152#define MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu) (0x2140 + 4 * (cpu))
153#define MVPP2_AGGR_TXQ_DESC_SIZE_MASK 0x3ff0
154#define MVPP2_AGGR_TXQ_STATUS_REG(cpu) (0x2180 + 4 * (cpu))
155#define MVPP2_AGGR_TXQ_PENDING_MASK 0x3fff
156#define MVPP2_AGGR_TXQ_INDEX_REG(cpu) (0x21c0 + 4 * (cpu))
157
158/* MBUS bridge registers */
159#define MVPP2_WIN_BASE(w) (0x4000 + ((w) << 2))
160#define MVPP2_WIN_SIZE(w) (0x4020 + ((w) << 2))
161#define MVPP2_WIN_REMAP(w) (0x4040 + ((w) << 2))
162#define MVPP2_BASE_ADDR_ENABLE 0x4060
163
Thomas Petazzoni6763ce32017-03-07 16:53:15 +0100164/* AXI Bridge Registers */
165#define MVPP22_AXI_BM_WR_ATTR_REG 0x4100
166#define MVPP22_AXI_BM_RD_ATTR_REG 0x4104
167#define MVPP22_AXI_AGGRQ_DESCR_RD_ATTR_REG 0x4110
168#define MVPP22_AXI_TXQ_DESCR_WR_ATTR_REG 0x4114
169#define MVPP22_AXI_TXQ_DESCR_RD_ATTR_REG 0x4118
170#define MVPP22_AXI_RXQ_DESCR_WR_ATTR_REG 0x411c
171#define MVPP22_AXI_RX_DATA_WR_ATTR_REG 0x4120
172#define MVPP22_AXI_TX_DATA_RD_ATTR_REG 0x4130
173#define MVPP22_AXI_RD_NORMAL_CODE_REG 0x4150
174#define MVPP22_AXI_RD_SNOOP_CODE_REG 0x4154
175#define MVPP22_AXI_WR_NORMAL_CODE_REG 0x4160
176#define MVPP22_AXI_WR_SNOOP_CODE_REG 0x4164
177
178/* Values for AXI Bridge registers */
179#define MVPP22_AXI_ATTR_CACHE_OFFS 0
180#define MVPP22_AXI_ATTR_DOMAIN_OFFS 12
181
182#define MVPP22_AXI_CODE_CACHE_OFFS 0
183#define MVPP22_AXI_CODE_DOMAIN_OFFS 4
184
185#define MVPP22_AXI_CODE_CACHE_NON_CACHE 0x3
186#define MVPP22_AXI_CODE_CACHE_WR_CACHE 0x7
187#define MVPP22_AXI_CODE_CACHE_RD_CACHE 0xb
188
189#define MVPP22_AXI_CODE_DOMAIN_OUTER_DOM 2
190#define MVPP22_AXI_CODE_DOMAIN_SYSTEM 3
191
Marcin Wojtas3f518502014-07-10 16:52:13 -0300192/* Interrupt Cause and Mask registers */
Thomas Petazzoni213f4282017-08-03 10:42:00 +0200193#define MVPP2_ISR_TX_THRESHOLD_REG(port) (0x5140 + 4 * (port))
194#define MVPP2_MAX_ISR_TX_THRESHOLD 0xfffff0
195
Marcin Wojtas3f518502014-07-10 16:52:13 -0300196#define MVPP2_ISR_RX_THRESHOLD_REG(rxq) (0x5200 + 4 * (rxq))
Thomas Petazzoniab426762017-02-21 11:28:04 +0100197#define MVPP2_MAX_ISR_RX_THRESHOLD 0xfffff0
Thomas Petazzonieb1e93a2017-08-03 10:41:55 +0200198#define MVPP21_ISR_RXQ_GROUP_REG(port) (0x5400 + 4 * (port))
Thomas Petazzonia73fef12017-03-07 16:53:16 +0100199
Antoine Ténart81b66302017-08-22 19:08:21 +0200200#define MVPP22_ISR_RXQ_GROUP_INDEX_REG 0x5400
Thomas Petazzonia73fef12017-03-07 16:53:16 +0100201#define MVPP22_ISR_RXQ_GROUP_INDEX_SUBGROUP_MASK 0xf
Antoine Ténart81b66302017-08-22 19:08:21 +0200202#define MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_MASK 0x380
203#define MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_OFFSET 7
Thomas Petazzonia73fef12017-03-07 16:53:16 +0100204
205#define MVPP22_ISR_RXQ_GROUP_INDEX_SUBGROUP_MASK 0xf
Antoine Ténart81b66302017-08-22 19:08:21 +0200206#define MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_MASK 0x380
Thomas Petazzonia73fef12017-03-07 16:53:16 +0100207
Antoine Ténart81b66302017-08-22 19:08:21 +0200208#define MVPP22_ISR_RXQ_SUB_GROUP_CONFIG_REG 0x5404
209#define MVPP22_ISR_RXQ_SUB_GROUP_STARTQ_MASK 0x1f
210#define MVPP22_ISR_RXQ_SUB_GROUP_SIZE_MASK 0xf00
211#define MVPP22_ISR_RXQ_SUB_GROUP_SIZE_OFFSET 8
Thomas Petazzonia73fef12017-03-07 16:53:16 +0100212
Marcin Wojtas3f518502014-07-10 16:52:13 -0300213#define MVPP2_ISR_ENABLE_REG(port) (0x5420 + 4 * (port))
214#define MVPP2_ISR_ENABLE_INTERRUPT(mask) ((mask) & 0xffff)
215#define MVPP2_ISR_DISABLE_INTERRUPT(mask) (((mask) << 16) & 0xffff0000)
216#define MVPP2_ISR_RX_TX_CAUSE_REG(port) (0x5480 + 4 * (port))
217#define MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK 0xffff
218#define MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK 0xff0000
Thomas Petazzoni213f4282017-08-03 10:42:00 +0200219#define MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_OFFSET 16
Marcin Wojtas3f518502014-07-10 16:52:13 -0300220#define MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK BIT(24)
221#define MVPP2_CAUSE_FCS_ERR_MASK BIT(25)
222#define MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK BIT(26)
223#define MVPP2_CAUSE_TX_EXCEPTION_SUM_MASK BIT(29)
224#define MVPP2_CAUSE_RX_EXCEPTION_SUM_MASK BIT(30)
225#define MVPP2_CAUSE_MISC_SUM_MASK BIT(31)
226#define MVPP2_ISR_RX_TX_MASK_REG(port) (0x54a0 + 4 * (port))
227#define MVPP2_ISR_PON_RX_TX_MASK_REG 0x54bc
228#define MVPP2_PON_CAUSE_RXQ_OCCUP_DESC_ALL_MASK 0xffff
229#define MVPP2_PON_CAUSE_TXP_OCCUP_DESC_ALL_MASK 0x3fc00000
230#define MVPP2_PON_CAUSE_MISC_SUM_MASK BIT(31)
231#define MVPP2_ISR_MISC_CAUSE_REG 0x55b0
232
233/* Buffer Manager registers */
234#define MVPP2_BM_POOL_BASE_REG(pool) (0x6000 + ((pool) * 4))
235#define MVPP2_BM_POOL_BASE_ADDR_MASK 0xfffff80
236#define MVPP2_BM_POOL_SIZE_REG(pool) (0x6040 + ((pool) * 4))
237#define MVPP2_BM_POOL_SIZE_MASK 0xfff0
238#define MVPP2_BM_POOL_READ_PTR_REG(pool) (0x6080 + ((pool) * 4))
239#define MVPP2_BM_POOL_GET_READ_PTR_MASK 0xfff0
240#define MVPP2_BM_POOL_PTRS_NUM_REG(pool) (0x60c0 + ((pool) * 4))
241#define MVPP2_BM_POOL_PTRS_NUM_MASK 0xfff0
242#define MVPP2_BM_BPPI_READ_PTR_REG(pool) (0x6100 + ((pool) * 4))
243#define MVPP2_BM_BPPI_PTRS_NUM_REG(pool) (0x6140 + ((pool) * 4))
244#define MVPP2_BM_BPPI_PTR_NUM_MASK 0x7ff
245#define MVPP2_BM_BPPI_PREFETCH_FULL_MASK BIT(16)
246#define MVPP2_BM_POOL_CTRL_REG(pool) (0x6200 + ((pool) * 4))
247#define MVPP2_BM_START_MASK BIT(0)
248#define MVPP2_BM_STOP_MASK BIT(1)
249#define MVPP2_BM_STATE_MASK BIT(4)
250#define MVPP2_BM_LOW_THRESH_OFFS 8
251#define MVPP2_BM_LOW_THRESH_MASK 0x7f00
252#define MVPP2_BM_LOW_THRESH_VALUE(val) ((val) << \
253 MVPP2_BM_LOW_THRESH_OFFS)
254#define MVPP2_BM_HIGH_THRESH_OFFS 16
255#define MVPP2_BM_HIGH_THRESH_MASK 0x7f0000
256#define MVPP2_BM_HIGH_THRESH_VALUE(val) ((val) << \
257 MVPP2_BM_HIGH_THRESH_OFFS)
258#define MVPP2_BM_INTR_CAUSE_REG(pool) (0x6240 + ((pool) * 4))
259#define MVPP2_BM_RELEASED_DELAY_MASK BIT(0)
260#define MVPP2_BM_ALLOC_FAILED_MASK BIT(1)
261#define MVPP2_BM_BPPE_EMPTY_MASK BIT(2)
262#define MVPP2_BM_BPPE_FULL_MASK BIT(3)
263#define MVPP2_BM_AVAILABLE_BP_LOW_MASK BIT(4)
264#define MVPP2_BM_INTR_MASK_REG(pool) (0x6280 + ((pool) * 4))
265#define MVPP2_BM_PHY_ALLOC_REG(pool) (0x6400 + ((pool) * 4))
266#define MVPP2_BM_PHY_ALLOC_GRNTD_MASK BIT(0)
267#define MVPP2_BM_VIRT_ALLOC_REG 0x6440
Thomas Petazzonid01524d2017-03-07 16:53:09 +0100268#define MVPP22_BM_ADDR_HIGH_ALLOC 0x6444
269#define MVPP22_BM_ADDR_HIGH_PHYS_MASK 0xff
270#define MVPP22_BM_ADDR_HIGH_VIRT_MASK 0xff00
271#define MVPP22_BM_ADDR_HIGH_VIRT_SHIFT 8
Marcin Wojtas3f518502014-07-10 16:52:13 -0300272#define MVPP2_BM_PHY_RLS_REG(pool) (0x6480 + ((pool) * 4))
273#define MVPP2_BM_PHY_RLS_MC_BUFF_MASK BIT(0)
274#define MVPP2_BM_PHY_RLS_PRIO_EN_MASK BIT(1)
275#define MVPP2_BM_PHY_RLS_GRNTD_MASK BIT(2)
276#define MVPP2_BM_VIRT_RLS_REG 0x64c0
Thomas Petazzonid01524d2017-03-07 16:53:09 +0100277#define MVPP22_BM_ADDR_HIGH_RLS_REG 0x64c4
278#define MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK 0xff
Antoine Ténart81b66302017-08-22 19:08:21 +0200279#define MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK 0xff00
Thomas Petazzonid01524d2017-03-07 16:53:09 +0100280#define MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT 8
Marcin Wojtas3f518502014-07-10 16:52:13 -0300281
282/* TX Scheduler registers */
283#define MVPP2_TXP_SCHED_PORT_INDEX_REG 0x8000
284#define MVPP2_TXP_SCHED_Q_CMD_REG 0x8004
285#define MVPP2_TXP_SCHED_ENQ_MASK 0xff
286#define MVPP2_TXP_SCHED_DISQ_OFFSET 8
287#define MVPP2_TXP_SCHED_CMD_1_REG 0x8010
288#define MVPP2_TXP_SCHED_PERIOD_REG 0x8018
289#define MVPP2_TXP_SCHED_MTU_REG 0x801c
290#define MVPP2_TXP_MTU_MAX 0x7FFFF
291#define MVPP2_TXP_SCHED_REFILL_REG 0x8020
292#define MVPP2_TXP_REFILL_TOKENS_ALL_MASK 0x7ffff
293#define MVPP2_TXP_REFILL_PERIOD_ALL_MASK 0x3ff00000
294#define MVPP2_TXP_REFILL_PERIOD_MASK(v) ((v) << 20)
295#define MVPP2_TXP_SCHED_TOKEN_SIZE_REG 0x8024
296#define MVPP2_TXP_TOKEN_SIZE_MAX 0xffffffff
297#define MVPP2_TXQ_SCHED_REFILL_REG(q) (0x8040 + ((q) << 2))
298#define MVPP2_TXQ_REFILL_TOKENS_ALL_MASK 0x7ffff
299#define MVPP2_TXQ_REFILL_PERIOD_ALL_MASK 0x3ff00000
300#define MVPP2_TXQ_REFILL_PERIOD_MASK(v) ((v) << 20)
301#define MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(q) (0x8060 + ((q) << 2))
302#define MVPP2_TXQ_TOKEN_SIZE_MAX 0x7fffffff
303#define MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(q) (0x8080 + ((q) << 2))
304#define MVPP2_TXQ_TOKEN_CNTR_MAX 0xffffffff
305
306/* TX general registers */
307#define MVPP2_TX_SNOOP_REG 0x8800
308#define MVPP2_TX_PORT_FLUSH_REG 0x8810
309#define MVPP2_TX_PORT_FLUSH_MASK(port) (1 << (port))
310
311/* LMS registers */
312#define MVPP2_SRC_ADDR_MIDDLE 0x24
313#define MVPP2_SRC_ADDR_HIGH 0x28
Marcin Wojtas08a23752014-07-21 13:48:12 -0300314#define MVPP2_PHY_AN_CFG0_REG 0x34
315#define MVPP2_PHY_AN_STOP_SMI0_MASK BIT(7)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300316#define MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG 0x305c
Thomas Petazzoni31d76772017-02-21 11:28:10 +0100317#define MVPP2_EXT_GLOBAL_CTRL_DEFAULT 0x27
Marcin Wojtas3f518502014-07-10 16:52:13 -0300318
319/* Per-port registers */
320#define MVPP2_GMAC_CTRL_0_REG 0x0
Antoine Ténart81b66302017-08-22 19:08:21 +0200321#define MVPP2_GMAC_PORT_EN_MASK BIT(0)
Antoine Ténart39193572017-08-22 19:08:24 +0200322#define MVPP2_GMAC_PORT_TYPE_MASK BIT(1)
Antoine Ténart81b66302017-08-22 19:08:21 +0200323#define MVPP2_GMAC_MAX_RX_SIZE_OFFS 2
324#define MVPP2_GMAC_MAX_RX_SIZE_MASK 0x7ffc
325#define MVPP2_GMAC_MIB_CNTR_EN_MASK BIT(15)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300326#define MVPP2_GMAC_CTRL_1_REG 0x4
Antoine Ténart81b66302017-08-22 19:08:21 +0200327#define MVPP2_GMAC_PERIODIC_XON_EN_MASK BIT(1)
328#define MVPP2_GMAC_GMII_LB_EN_MASK BIT(5)
329#define MVPP2_GMAC_PCS_LB_EN_BIT 6
330#define MVPP2_GMAC_PCS_LB_EN_MASK BIT(6)
331#define MVPP2_GMAC_SA_LOW_OFFS 7
Marcin Wojtas3f518502014-07-10 16:52:13 -0300332#define MVPP2_GMAC_CTRL_2_REG 0x8
Antoine Ténart81b66302017-08-22 19:08:21 +0200333#define MVPP2_GMAC_INBAND_AN_MASK BIT(0)
Antoine Ténart39193572017-08-22 19:08:24 +0200334#define MVPP2_GMAC_FLOW_CTRL_MASK GENMASK(2, 1)
Antoine Ténart81b66302017-08-22 19:08:21 +0200335#define MVPP2_GMAC_PCS_ENABLE_MASK BIT(3)
Antoine Tenartc7dfc8c2017-09-25 14:59:48 +0200336#define MVPP2_GMAC_INTERNAL_CLK_MASK BIT(4)
Antoine Ténart39193572017-08-22 19:08:24 +0200337#define MVPP2_GMAC_DISABLE_PADDING BIT(5)
Antoine Ténart81b66302017-08-22 19:08:21 +0200338#define MVPP2_GMAC_PORT_RESET_MASK BIT(6)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300339#define MVPP2_GMAC_AUTONEG_CONFIG 0xc
Antoine Ténart81b66302017-08-22 19:08:21 +0200340#define MVPP2_GMAC_FORCE_LINK_DOWN BIT(0)
341#define MVPP2_GMAC_FORCE_LINK_PASS BIT(1)
Antoine Ténart39193572017-08-22 19:08:24 +0200342#define MVPP2_GMAC_IN_BAND_AUTONEG BIT(2)
343#define MVPP2_GMAC_IN_BAND_AUTONEG_BYPASS BIT(3)
Antoine Ténart81b66302017-08-22 19:08:21 +0200344#define MVPP2_GMAC_CONFIG_MII_SPEED BIT(5)
345#define MVPP2_GMAC_CONFIG_GMII_SPEED BIT(6)
346#define MVPP2_GMAC_AN_SPEED_EN BIT(7)
347#define MVPP2_GMAC_FC_ADV_EN BIT(9)
Antoine Ténart39193572017-08-22 19:08:24 +0200348#define MVPP2_GMAC_FLOW_CTRL_AUTONEG BIT(11)
Antoine Ténart81b66302017-08-22 19:08:21 +0200349#define MVPP2_GMAC_CONFIG_FULL_DUPLEX BIT(12)
350#define MVPP2_GMAC_AN_DUPLEX_EN BIT(13)
Antoine Tenartfd3651b2017-09-01 11:04:54 +0200351#define MVPP2_GMAC_STATUS0 0x10
352#define MVPP2_GMAC_STATUS0_LINK_UP BIT(0)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300353#define MVPP2_GMAC_PORT_FIFO_CFG_1_REG 0x1c
Antoine Ténart81b66302017-08-22 19:08:21 +0200354#define MVPP2_GMAC_TX_FIFO_MIN_TH_OFFS 6
355#define MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK 0x1fc0
356#define MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(v) (((v) << 6) & \
Marcin Wojtas3f518502014-07-10 16:52:13 -0300357 MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK)
Antoine Tenartfd3651b2017-09-01 11:04:54 +0200358#define MVPP22_GMAC_INT_STAT 0x20
359#define MVPP22_GMAC_INT_STAT_LINK BIT(1)
360#define MVPP22_GMAC_INT_MASK 0x24
361#define MVPP22_GMAC_INT_MASK_LINK_STAT BIT(1)
Thomas Petazzoni26975822017-03-07 16:53:14 +0100362#define MVPP22_GMAC_CTRL_4_REG 0x90
Antoine Ténart81b66302017-08-22 19:08:21 +0200363#define MVPP22_CTRL4_EXT_PIN_GMII_SEL BIT(0)
364#define MVPP22_CTRL4_DP_CLK_SEL BIT(5)
Antoine Ténart1068ec72017-08-22 19:08:22 +0200365#define MVPP22_CTRL4_SYNC_BYPASS_DIS BIT(6)
Antoine Ténart81b66302017-08-22 19:08:21 +0200366#define MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE BIT(7)
Antoine Tenartfd3651b2017-09-01 11:04:54 +0200367#define MVPP22_GMAC_INT_SUM_MASK 0xa4
368#define MVPP22_GMAC_INT_SUM_MASK_LINK_STAT BIT(1)
Thomas Petazzoni26975822017-03-07 16:53:14 +0100369
370/* Per-port XGMAC registers. PPv2.2 only, only for GOP port 0,
371 * relative to port->base.
372 */
Antoine Ténart725757a2017-06-12 16:01:39 +0200373#define MVPP22_XLG_CTRL0_REG 0x100
Antoine Ténart81b66302017-08-22 19:08:21 +0200374#define MVPP22_XLG_CTRL0_PORT_EN BIT(0)
375#define MVPP22_XLG_CTRL0_MAC_RESET_DIS BIT(1)
Antoine Ténart77321952017-08-22 19:08:25 +0200376#define MVPP22_XLG_CTRL0_RX_FLOW_CTRL_EN BIT(7)
Antoine Ténart81b66302017-08-22 19:08:21 +0200377#define MVPP22_XLG_CTRL0_MIB_CNT_DIS BIT(14)
Stefan Chulski76eb1b12017-08-22 19:08:26 +0200378#define MVPP22_XLG_CTRL1_REG 0x104
Antoine Ténartec15ecd2017-08-25 15:24:46 +0200379#define MVPP22_XLG_CTRL1_FRAMESIZELIMIT_OFFS 0
Stefan Chulski76eb1b12017-08-22 19:08:26 +0200380#define MVPP22_XLG_CTRL1_FRAMESIZELIMIT_MASK 0x1fff
Antoine Tenartfd3651b2017-09-01 11:04:54 +0200381#define MVPP22_XLG_STATUS 0x10c
382#define MVPP22_XLG_STATUS_LINK_UP BIT(0)
383#define MVPP22_XLG_INT_STAT 0x114
384#define MVPP22_XLG_INT_STAT_LINK BIT(1)
385#define MVPP22_XLG_INT_MASK 0x118
386#define MVPP22_XLG_INT_MASK_LINK BIT(1)
Thomas Petazzoni26975822017-03-07 16:53:14 +0100387#define MVPP22_XLG_CTRL3_REG 0x11c
Antoine Ténart81b66302017-08-22 19:08:21 +0200388#define MVPP22_XLG_CTRL3_MACMODESELECT_MASK (7 << 13)
389#define MVPP22_XLG_CTRL3_MACMODESELECT_GMAC (0 << 13)
390#define MVPP22_XLG_CTRL3_MACMODESELECT_10G (1 << 13)
Antoine Tenartfd3651b2017-09-01 11:04:54 +0200391#define MVPP22_XLG_EXT_INT_MASK 0x15c
392#define MVPP22_XLG_EXT_INT_MASK_XLG BIT(1)
393#define MVPP22_XLG_EXT_INT_MASK_GIG BIT(2)
Antoine Ténart77321952017-08-22 19:08:25 +0200394#define MVPP22_XLG_CTRL4_REG 0x184
395#define MVPP22_XLG_CTRL4_FWD_FC BIT(5)
396#define MVPP22_XLG_CTRL4_FWD_PFC BIT(6)
397#define MVPP22_XLG_CTRL4_MACMODSELECT_GMAC BIT(12)
398
Thomas Petazzoni26975822017-03-07 16:53:14 +0100399/* SMI registers. PPv2.2 only, relative to priv->iface_base. */
400#define MVPP22_SMI_MISC_CFG_REG 0x1204
Antoine Ténart81b66302017-08-22 19:08:21 +0200401#define MVPP22_SMI_POLLING_EN BIT(10)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300402
Thomas Petazzonia7868412017-03-07 16:53:13 +0100403#define MVPP22_GMAC_BASE(port) (0x7000 + (port) * 0x1000 + 0xe00)
404
Marcin Wojtas3f518502014-07-10 16:52:13 -0300405#define MVPP2_CAUSE_TXQ_SENT_DESC_ALL_MASK 0xff
406
407/* Descriptor ring Macros */
408#define MVPP2_QUEUE_NEXT_DESC(q, index) \
409 (((index) < (q)->last_desc) ? ((index) + 1) : 0)
410
Antoine Ténartf84bf382017-08-22 19:08:27 +0200411/* XPCS registers. PPv2.2 only */
412#define MVPP22_MPCS_BASE(port) (0x7000 + (port) * 0x1000)
413#define MVPP22_MPCS_CTRL 0x14
414#define MVPP22_MPCS_CTRL_FWD_ERR_CONN BIT(10)
415#define MVPP22_MPCS_CLK_RESET 0x14c
416#define MAC_CLK_RESET_SD_TX BIT(0)
417#define MAC_CLK_RESET_SD_RX BIT(1)
418#define MAC_CLK_RESET_MAC BIT(2)
419#define MVPP22_MPCS_CLK_RESET_DIV_RATIO(n) ((n) << 4)
420#define MVPP22_MPCS_CLK_RESET_DIV_SET BIT(11)
421
422/* XPCS registers. PPv2.2 only */
423#define MVPP22_XPCS_BASE(port) (0x7400 + (port) * 0x1000)
424#define MVPP22_XPCS_CFG0 0x0
425#define MVPP22_XPCS_CFG0_PCS_MODE(n) ((n) << 3)
426#define MVPP22_XPCS_CFG0_ACTIVE_LANE(n) ((n) << 5)
427
428/* System controller registers. Accessed through a regmap. */
429#define GENCONF_SOFT_RESET1 0x1108
430#define GENCONF_SOFT_RESET1_GOP BIT(6)
431#define GENCONF_PORT_CTRL0 0x1110
432#define GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT BIT(1)
433#define GENCONF_PORT_CTRL0_RX_DATA_SAMPLE BIT(29)
434#define GENCONF_PORT_CTRL0_CLK_DIV_PHASE_CLR BIT(31)
435#define GENCONF_PORT_CTRL1 0x1114
436#define GENCONF_PORT_CTRL1_EN(p) BIT(p)
437#define GENCONF_PORT_CTRL1_RESET(p) (BIT(p) << 28)
438#define GENCONF_CTRL0 0x1120
439#define GENCONF_CTRL0_PORT0_RGMII BIT(0)
440#define GENCONF_CTRL0_PORT1_RGMII_MII BIT(1)
441#define GENCONF_CTRL0_PORT1_RGMII BIT(2)
442
Marcin Wojtas3f518502014-07-10 16:52:13 -0300443/* Various constants */
444
445/* Coalescing */
446#define MVPP2_TXDONE_COAL_PKTS_THRESH 15
Marcin Wojtasedc660f2015-08-06 19:00:30 +0200447#define MVPP2_TXDONE_HRTIMER_PERIOD_NS 1000000UL
Thomas Petazzoni213f4282017-08-03 10:42:00 +0200448#define MVPP2_TXDONE_COAL_USEC 1000
Marcin Wojtas3f518502014-07-10 16:52:13 -0300449#define MVPP2_RX_COAL_PKTS 32
450#define MVPP2_RX_COAL_USEC 100
451
452/* The two bytes Marvell header. Either contains a special value used
453 * by Marvell switches when a specific hardware mode is enabled (not
454 * supported by this driver) or is filled automatically by zeroes on
455 * the RX side. Those two bytes being at the front of the Ethernet
456 * header, they allow to have the IP header aligned on a 4 bytes
457 * boundary automatically: the hardware skips those two bytes on its
458 * own.
459 */
460#define MVPP2_MH_SIZE 2
461#define MVPP2_ETH_TYPE_LEN 2
462#define MVPP2_PPPOE_HDR_SIZE 8
463#define MVPP2_VLAN_TAG_LEN 4
464
465/* Lbtd 802.3 type */
466#define MVPP2_IP_LBDT_TYPE 0xfffa
467
Marcin Wojtas3f518502014-07-10 16:52:13 -0300468#define MVPP2_TX_CSUM_MAX_SIZE 9800
469
470/* Timeout constants */
471#define MVPP2_TX_DISABLE_TIMEOUT_MSEC 1000
472#define MVPP2_TX_PENDING_TIMEOUT_MSEC 1000
473
474#define MVPP2_TX_MTU_MAX 0x7ffff
475
476/* Maximum number of T-CONTs of PON port */
477#define MVPP2_MAX_TCONT 16
478
479/* Maximum number of supported ports */
480#define MVPP2_MAX_PORTS 4
481
482/* Maximum number of TXQs used by single port */
483#define MVPP2_MAX_TXQ 8
484
Marcin Wojtas3f518502014-07-10 16:52:13 -0300485/* Dfault number of RXQs in use */
486#define MVPP2_DEFAULT_RXQ 4
487
Marcin Wojtas3f518502014-07-10 16:52:13 -0300488/* Max number of Rx descriptors */
489#define MVPP2_MAX_RXD 128
490
491/* Max number of Tx descriptors */
492#define MVPP2_MAX_TXD 1024
493
494/* Amount of Tx descriptors that can be reserved at once by CPU */
495#define MVPP2_CPU_DESC_CHUNK 64
496
497/* Max number of Tx descriptors in each aggregated queue */
498#define MVPP2_AGGR_TXQ_SIZE 256
499
500/* Descriptor aligned size */
501#define MVPP2_DESC_ALIGNED_SIZE 32
502
503/* Descriptor alignment mask */
504#define MVPP2_TX_DESC_ALIGN (MVPP2_DESC_ALIGNED_SIZE - 1)
505
506/* RX FIFO constants */
Antoine Tenart2d1d7df2017-10-30 11:23:28 +0100507#define MVPP2_RX_FIFO_PORT_DATA_SIZE_32KB 0x8000
508#define MVPP2_RX_FIFO_PORT_DATA_SIZE_8KB 0x2000
509#define MVPP2_RX_FIFO_PORT_DATA_SIZE_4KB 0x1000
510#define MVPP2_RX_FIFO_PORT_ATTR_SIZE_32KB 0x200
511#define MVPP2_RX_FIFO_PORT_ATTR_SIZE_8KB 0x80
512#define MVPP2_RX_FIFO_PORT_ATTR_SIZE_4KB 0x40
513#define MVPP2_RX_FIFO_PORT_MIN_PKT 0x80
Marcin Wojtas3f518502014-07-10 16:52:13 -0300514
515/* RX buffer constants */
516#define MVPP2_SKB_SHINFO_SIZE \
517 SKB_DATA_ALIGN(sizeof(struct skb_shared_info))
518
519#define MVPP2_RX_PKT_SIZE(mtu) \
520 ALIGN((mtu) + MVPP2_MH_SIZE + MVPP2_VLAN_TAG_LEN + \
Jisheng Zhang4a0a12d2016-04-01 17:11:05 +0800521 ETH_HLEN + ETH_FCS_LEN, cache_line_size())
Marcin Wojtas3f518502014-07-10 16:52:13 -0300522
523#define MVPP2_RX_BUF_SIZE(pkt_size) ((pkt_size) + NET_SKB_PAD)
524#define MVPP2_RX_TOTAL_SIZE(buf_size) ((buf_size) + MVPP2_SKB_SHINFO_SIZE)
525#define MVPP2_RX_MAX_PKT_SIZE(total_size) \
526 ((total_size) - NET_SKB_PAD - MVPP2_SKB_SHINFO_SIZE)
527
528#define MVPP2_BIT_TO_BYTE(bit) ((bit) / 8)
529
530/* IPv6 max L3 address size */
531#define MVPP2_MAX_L3_ADDR_SIZE 16
532
533/* Port flags */
534#define MVPP2_F_LOOPBACK BIT(0)
535
536/* Marvell tag types */
537enum mvpp2_tag_type {
538 MVPP2_TAG_TYPE_NONE = 0,
539 MVPP2_TAG_TYPE_MH = 1,
540 MVPP2_TAG_TYPE_DSA = 2,
541 MVPP2_TAG_TYPE_EDSA = 3,
542 MVPP2_TAG_TYPE_VLAN = 4,
543 MVPP2_TAG_TYPE_LAST = 5
544};
545
546/* Parser constants */
547#define MVPP2_PRS_TCAM_SRAM_SIZE 256
548#define MVPP2_PRS_TCAM_WORDS 6
549#define MVPP2_PRS_SRAM_WORDS 4
550#define MVPP2_PRS_FLOW_ID_SIZE 64
551#define MVPP2_PRS_FLOW_ID_MASK 0x3f
552#define MVPP2_PRS_TCAM_ENTRY_INVALID 1
553#define MVPP2_PRS_TCAM_DSA_TAGGED_BIT BIT(5)
554#define MVPP2_PRS_IPV4_HEAD 0x40
555#define MVPP2_PRS_IPV4_HEAD_MASK 0xf0
556#define MVPP2_PRS_IPV4_MC 0xe0
557#define MVPP2_PRS_IPV4_MC_MASK 0xf0
558#define MVPP2_PRS_IPV4_BC_MASK 0xff
559#define MVPP2_PRS_IPV4_IHL 0x5
560#define MVPP2_PRS_IPV4_IHL_MASK 0xf
561#define MVPP2_PRS_IPV6_MC 0xff
562#define MVPP2_PRS_IPV6_MC_MASK 0xff
563#define MVPP2_PRS_IPV6_HOP_MASK 0xff
564#define MVPP2_PRS_TCAM_PROTO_MASK 0xff
565#define MVPP2_PRS_TCAM_PROTO_MASK_L 0x3f
566#define MVPP2_PRS_DBL_VLANS_MAX 100
567
568/* Tcam structure:
569 * - lookup ID - 4 bits
570 * - port ID - 1 byte
571 * - additional information - 1 byte
572 * - header data - 8 bytes
573 * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(5)->(0).
574 */
575#define MVPP2_PRS_AI_BITS 8
576#define MVPP2_PRS_PORT_MASK 0xff
577#define MVPP2_PRS_LU_MASK 0xf
578#define MVPP2_PRS_TCAM_DATA_BYTE(offs) \
579 (((offs) - ((offs) % 2)) * 2 + ((offs) % 2))
580#define MVPP2_PRS_TCAM_DATA_BYTE_EN(offs) \
581 (((offs) * 2) - ((offs) % 2) + 2)
582#define MVPP2_PRS_TCAM_AI_BYTE 16
583#define MVPP2_PRS_TCAM_PORT_BYTE 17
584#define MVPP2_PRS_TCAM_LU_BYTE 20
585#define MVPP2_PRS_TCAM_EN_OFFS(offs) ((offs) + 2)
586#define MVPP2_PRS_TCAM_INV_WORD 5
587/* Tcam entries ID */
588#define MVPP2_PE_DROP_ALL 0
589#define MVPP2_PE_FIRST_FREE_TID 1
590#define MVPP2_PE_LAST_FREE_TID (MVPP2_PRS_TCAM_SRAM_SIZE - 31)
591#define MVPP2_PE_IP6_EXT_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 30)
592#define MVPP2_PE_MAC_MC_IP6 (MVPP2_PRS_TCAM_SRAM_SIZE - 29)
593#define MVPP2_PE_IP6_ADDR_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 28)
594#define MVPP2_PE_IP4_ADDR_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 27)
595#define MVPP2_PE_LAST_DEFAULT_FLOW (MVPP2_PRS_TCAM_SRAM_SIZE - 26)
596#define MVPP2_PE_FIRST_DEFAULT_FLOW (MVPP2_PRS_TCAM_SRAM_SIZE - 19)
597#define MVPP2_PE_EDSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 18)
598#define MVPP2_PE_EDSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 17)
599#define MVPP2_PE_DSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 16)
600#define MVPP2_PE_DSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 15)
601#define MVPP2_PE_ETYPE_EDSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 14)
602#define MVPP2_PE_ETYPE_EDSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 13)
603#define MVPP2_PE_ETYPE_DSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 12)
604#define MVPP2_PE_ETYPE_DSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 11)
605#define MVPP2_PE_MH_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 10)
606#define MVPP2_PE_DSA_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 9)
607#define MVPP2_PE_IP6_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 8)
608#define MVPP2_PE_IP4_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 7)
609#define MVPP2_PE_ETH_TYPE_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 6)
610#define MVPP2_PE_VLAN_DBL (MVPP2_PRS_TCAM_SRAM_SIZE - 5)
611#define MVPP2_PE_VLAN_NONE (MVPP2_PRS_TCAM_SRAM_SIZE - 4)
612#define MVPP2_PE_MAC_MC_ALL (MVPP2_PRS_TCAM_SRAM_SIZE - 3)
613#define MVPP2_PE_MAC_PROMISCUOUS (MVPP2_PRS_TCAM_SRAM_SIZE - 2)
614#define MVPP2_PE_MAC_NON_PROMISCUOUS (MVPP2_PRS_TCAM_SRAM_SIZE - 1)
615
616/* Sram structure
617 * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(3)->(0).
618 */
619#define MVPP2_PRS_SRAM_RI_OFFS 0
620#define MVPP2_PRS_SRAM_RI_WORD 0
621#define MVPP2_PRS_SRAM_RI_CTRL_OFFS 32
622#define MVPP2_PRS_SRAM_RI_CTRL_WORD 1
623#define MVPP2_PRS_SRAM_RI_CTRL_BITS 32
624#define MVPP2_PRS_SRAM_SHIFT_OFFS 64
625#define MVPP2_PRS_SRAM_SHIFT_SIGN_BIT 72
626#define MVPP2_PRS_SRAM_UDF_OFFS 73
627#define MVPP2_PRS_SRAM_UDF_BITS 8
628#define MVPP2_PRS_SRAM_UDF_MASK 0xff
629#define MVPP2_PRS_SRAM_UDF_SIGN_BIT 81
630#define MVPP2_PRS_SRAM_UDF_TYPE_OFFS 82
631#define MVPP2_PRS_SRAM_UDF_TYPE_MASK 0x7
632#define MVPP2_PRS_SRAM_UDF_TYPE_L3 1
633#define MVPP2_PRS_SRAM_UDF_TYPE_L4 4
634#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS 85
635#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK 0x3
636#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD 1
637#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP4_ADD 2
638#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP6_ADD 3
639#define MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS 87
640#define MVPP2_PRS_SRAM_OP_SEL_UDF_BITS 2
641#define MVPP2_PRS_SRAM_OP_SEL_UDF_MASK 0x3
642#define MVPP2_PRS_SRAM_OP_SEL_UDF_ADD 0
643#define MVPP2_PRS_SRAM_OP_SEL_UDF_IP4_ADD 2
644#define MVPP2_PRS_SRAM_OP_SEL_UDF_IP6_ADD 3
645#define MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS 89
646#define MVPP2_PRS_SRAM_AI_OFFS 90
647#define MVPP2_PRS_SRAM_AI_CTRL_OFFS 98
648#define MVPP2_PRS_SRAM_AI_CTRL_BITS 8
649#define MVPP2_PRS_SRAM_AI_MASK 0xff
650#define MVPP2_PRS_SRAM_NEXT_LU_OFFS 106
651#define MVPP2_PRS_SRAM_NEXT_LU_MASK 0xf
652#define MVPP2_PRS_SRAM_LU_DONE_BIT 110
653#define MVPP2_PRS_SRAM_LU_GEN_BIT 111
654
655/* Sram result info bits assignment */
656#define MVPP2_PRS_RI_MAC_ME_MASK 0x1
657#define MVPP2_PRS_RI_DSA_MASK 0x2
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100658#define MVPP2_PRS_RI_VLAN_MASK (BIT(2) | BIT(3))
659#define MVPP2_PRS_RI_VLAN_NONE 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300660#define MVPP2_PRS_RI_VLAN_SINGLE BIT(2)
661#define MVPP2_PRS_RI_VLAN_DOUBLE BIT(3)
662#define MVPP2_PRS_RI_VLAN_TRIPLE (BIT(2) | BIT(3))
663#define MVPP2_PRS_RI_CPU_CODE_MASK 0x70
664#define MVPP2_PRS_RI_CPU_CODE_RX_SPEC BIT(4)
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100665#define MVPP2_PRS_RI_L2_CAST_MASK (BIT(9) | BIT(10))
666#define MVPP2_PRS_RI_L2_UCAST 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300667#define MVPP2_PRS_RI_L2_MCAST BIT(9)
668#define MVPP2_PRS_RI_L2_BCAST BIT(10)
669#define MVPP2_PRS_RI_PPPOE_MASK 0x800
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100670#define MVPP2_PRS_RI_L3_PROTO_MASK (BIT(12) | BIT(13) | BIT(14))
671#define MVPP2_PRS_RI_L3_UN 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300672#define MVPP2_PRS_RI_L3_IP4 BIT(12)
673#define MVPP2_PRS_RI_L3_IP4_OPT BIT(13)
674#define MVPP2_PRS_RI_L3_IP4_OTHER (BIT(12) | BIT(13))
675#define MVPP2_PRS_RI_L3_IP6 BIT(14)
676#define MVPP2_PRS_RI_L3_IP6_EXT (BIT(12) | BIT(14))
677#define MVPP2_PRS_RI_L3_ARP (BIT(13) | BIT(14))
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100678#define MVPP2_PRS_RI_L3_ADDR_MASK (BIT(15) | BIT(16))
679#define MVPP2_PRS_RI_L3_UCAST 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300680#define MVPP2_PRS_RI_L3_MCAST BIT(15)
681#define MVPP2_PRS_RI_L3_BCAST (BIT(15) | BIT(16))
682#define MVPP2_PRS_RI_IP_FRAG_MASK 0x20000
Stefan Chulskiaff3da32017-09-25 14:59:46 +0200683#define MVPP2_PRS_RI_IP_FRAG_TRUE BIT(17)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300684#define MVPP2_PRS_RI_UDF3_MASK 0x300000
685#define MVPP2_PRS_RI_UDF3_RX_SPECIAL BIT(21)
686#define MVPP2_PRS_RI_L4_PROTO_MASK 0x1c00000
687#define MVPP2_PRS_RI_L4_TCP BIT(22)
688#define MVPP2_PRS_RI_L4_UDP BIT(23)
689#define MVPP2_PRS_RI_L4_OTHER (BIT(22) | BIT(23))
690#define MVPP2_PRS_RI_UDF7_MASK 0x60000000
691#define MVPP2_PRS_RI_UDF7_IP6_LITE BIT(29)
692#define MVPP2_PRS_RI_DROP_MASK 0x80000000
693
694/* Sram additional info bits assignment */
695#define MVPP2_PRS_IPV4_DIP_AI_BIT BIT(0)
696#define MVPP2_PRS_IPV6_NO_EXT_AI_BIT BIT(0)
697#define MVPP2_PRS_IPV6_EXT_AI_BIT BIT(1)
698#define MVPP2_PRS_IPV6_EXT_AH_AI_BIT BIT(2)
699#define MVPP2_PRS_IPV6_EXT_AH_LEN_AI_BIT BIT(3)
700#define MVPP2_PRS_IPV6_EXT_AH_L4_AI_BIT BIT(4)
701#define MVPP2_PRS_SINGLE_VLAN_AI 0
702#define MVPP2_PRS_DBL_VLAN_AI_BIT BIT(7)
703
704/* DSA/EDSA type */
705#define MVPP2_PRS_TAGGED true
706#define MVPP2_PRS_UNTAGGED false
707#define MVPP2_PRS_EDSA true
708#define MVPP2_PRS_DSA false
709
710/* MAC entries, shadow udf */
711enum mvpp2_prs_udf {
712 MVPP2_PRS_UDF_MAC_DEF,
713 MVPP2_PRS_UDF_MAC_RANGE,
714 MVPP2_PRS_UDF_L2_DEF,
715 MVPP2_PRS_UDF_L2_DEF_COPY,
716 MVPP2_PRS_UDF_L2_USER,
717};
718
719/* Lookup ID */
720enum mvpp2_prs_lookup {
721 MVPP2_PRS_LU_MH,
722 MVPP2_PRS_LU_MAC,
723 MVPP2_PRS_LU_DSA,
724 MVPP2_PRS_LU_VLAN,
725 MVPP2_PRS_LU_L2,
726 MVPP2_PRS_LU_PPPOE,
727 MVPP2_PRS_LU_IP4,
728 MVPP2_PRS_LU_IP6,
729 MVPP2_PRS_LU_FLOWS,
730 MVPP2_PRS_LU_LAST,
731};
732
733/* L3 cast enum */
734enum mvpp2_prs_l3_cast {
735 MVPP2_PRS_L3_UNI_CAST,
736 MVPP2_PRS_L3_MULTI_CAST,
737 MVPP2_PRS_L3_BROAD_CAST
738};
739
740/* Classifier constants */
741#define MVPP2_CLS_FLOWS_TBL_SIZE 512
742#define MVPP2_CLS_FLOWS_TBL_DATA_WORDS 3
743#define MVPP2_CLS_LKP_TBL_SIZE 64
744
745/* BM constants */
746#define MVPP2_BM_POOLS_NUM 8
747#define MVPP2_BM_LONG_BUF_NUM 1024
748#define MVPP2_BM_SHORT_BUF_NUM 2048
749#define MVPP2_BM_POOL_SIZE_MAX (16*1024 - MVPP2_BM_POOL_PTR_ALIGN/4)
750#define MVPP2_BM_POOL_PTR_ALIGN 128
751#define MVPP2_BM_SWF_LONG_POOL(port) ((port > 2) ? 2 : port)
752#define MVPP2_BM_SWF_SHORT_POOL 3
753
754/* BM cookie (32 bits) definition */
755#define MVPP2_BM_COOKIE_POOL_OFFS 8
756#define MVPP2_BM_COOKIE_CPU_OFFS 24
757
758/* BM short pool packet size
759 * These value assure that for SWF the total number
760 * of bytes allocated for each buffer will be 512
761 */
762#define MVPP2_BM_SHORT_PKT_SIZE MVPP2_RX_MAX_PKT_SIZE(512)
763
Thomas Petazzonia7868412017-03-07 16:53:13 +0100764#define MVPP21_ADDR_SPACE_SZ 0
765#define MVPP22_ADDR_SPACE_SZ SZ_64K
766
Thomas Petazzonidf089aa2017-08-03 10:41:58 +0200767#define MVPP2_MAX_THREADS 8
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +0200768#define MVPP2_MAX_QVECS MVPP2_MAX_THREADS
Thomas Petazzonia7868412017-03-07 16:53:13 +0100769
Marcin Wojtas3f518502014-07-10 16:52:13 -0300770enum mvpp2_bm_type {
771 MVPP2_BM_FREE,
772 MVPP2_BM_SWF_LONG,
773 MVPP2_BM_SWF_SHORT
774};
775
776/* Definitions */
777
778/* Shared Packet Processor resources */
779struct mvpp2 {
780 /* Shared registers' base addresses */
Marcin Wojtas3f518502014-07-10 16:52:13 -0300781 void __iomem *lms_base;
Thomas Petazzonia7868412017-03-07 16:53:13 +0100782 void __iomem *iface_base;
783
Thomas Petazzonidf089aa2017-08-03 10:41:58 +0200784 /* On PPv2.2, each "software thread" can access the base
785 * register through a separate address space, each 64 KB apart
786 * from each other. Typically, such address spaces will be
787 * used per CPU.
Thomas Petazzonia7868412017-03-07 16:53:13 +0100788 */
Thomas Petazzonidf089aa2017-08-03 10:41:58 +0200789 void __iomem *swth_base[MVPP2_MAX_THREADS];
Marcin Wojtas3f518502014-07-10 16:52:13 -0300790
Antoine Ténartf84bf382017-08-22 19:08:27 +0200791 /* On PPv2.2, some port control registers are located into the system
792 * controller space. These registers are accessible through a regmap.
793 */
794 struct regmap *sysctrl_base;
795
Marcin Wojtas3f518502014-07-10 16:52:13 -0300796 /* Common clocks */
797 struct clk *pp_clk;
798 struct clk *gop_clk;
Thomas Petazzonifceb55d2017-03-07 16:53:18 +0100799 struct clk *mg_clk;
Gregory CLEMENT4792ea02017-09-29 14:27:39 +0200800 struct clk *axi_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300801
802 /* List of pointers to port structures */
803 struct mvpp2_port **port_list;
804
805 /* Aggregated TXQs */
806 struct mvpp2_tx_queue *aggr_txqs;
807
808 /* BM pools */
809 struct mvpp2_bm_pool *bm_pools;
810
811 /* PRS shadow table */
812 struct mvpp2_prs_shadow *prs_shadow;
813 /* PRS auxiliary table for double vlan entries control */
814 bool *prs_double_vlans;
815
816 /* Tclk value */
817 u32 tclk;
Thomas Petazzonifaca9242017-03-07 16:53:06 +0100818
819 /* HW version */
820 enum { MVPP21, MVPP22 } hw_version;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +0100821
822 /* Maximum number of RXQs per port */
823 unsigned int max_port_rxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300824};
825
826struct mvpp2_pcpu_stats {
827 struct u64_stats_sync syncp;
828 u64 rx_packets;
829 u64 rx_bytes;
830 u64 tx_packets;
831 u64 tx_bytes;
832};
833
Marcin Wojtasedc660f2015-08-06 19:00:30 +0200834/* Per-CPU port control */
835struct mvpp2_port_pcpu {
836 struct hrtimer tx_done_timer;
837 bool timer_scheduled;
838 /* Tasklet for egress finalization */
839 struct tasklet_struct tx_done_tasklet;
840};
841
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +0200842struct mvpp2_queue_vector {
843 int irq;
844 struct napi_struct napi;
845 enum { MVPP2_QUEUE_VECTOR_SHARED, MVPP2_QUEUE_VECTOR_PRIVATE } type;
846 int sw_thread_id;
847 u16 sw_thread_mask;
848 int first_rxq;
849 int nrxqs;
850 u32 pending_cause_rx;
851 struct mvpp2_port *port;
852};
853
Marcin Wojtas3f518502014-07-10 16:52:13 -0300854struct mvpp2_port {
855 u8 id;
856
Thomas Petazzonia7868412017-03-07 16:53:13 +0100857 /* Index of the port from the "group of ports" complex point
858 * of view
859 */
860 int gop_id;
861
Antoine Tenartfd3651b2017-09-01 11:04:54 +0200862 int link_irq;
863
Marcin Wojtas3f518502014-07-10 16:52:13 -0300864 struct mvpp2 *priv;
865
866 /* Per-port registers' base address */
867 void __iomem *base;
868
869 struct mvpp2_rx_queue **rxqs;
Thomas Petazzoni09f83972017-08-03 10:41:57 +0200870 unsigned int nrxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300871 struct mvpp2_tx_queue **txqs;
Thomas Petazzoni09f83972017-08-03 10:41:57 +0200872 unsigned int ntxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300873 struct net_device *dev;
874
875 int pkt_size;
876
Marcin Wojtasedc660f2015-08-06 19:00:30 +0200877 /* Per-CPU port control */
878 struct mvpp2_port_pcpu __percpu *pcpu;
879
Marcin Wojtas3f518502014-07-10 16:52:13 -0300880 /* Flags */
881 unsigned long flags;
882
883 u16 tx_ring_size;
884 u16 rx_ring_size;
885 struct mvpp2_pcpu_stats __percpu *stats;
886
Marcin Wojtas3f518502014-07-10 16:52:13 -0300887 phy_interface_t phy_interface;
888 struct device_node *phy_node;
Antoine Tenart542897d2017-08-30 10:29:15 +0200889 struct phy *comphy;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300890 unsigned int link;
891 unsigned int duplex;
892 unsigned int speed;
893
894 struct mvpp2_bm_pool *pool_long;
895 struct mvpp2_bm_pool *pool_short;
896
897 /* Index of first port's physical RXQ */
898 u8 first_rxq;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +0200899
900 struct mvpp2_queue_vector qvecs[MVPP2_MAX_QVECS];
901 unsigned int nqvecs;
Thomas Petazzoni213f4282017-08-03 10:42:00 +0200902 bool has_tx_irqs;
903
904 u32 tx_time_coal;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300905};
906
907/* The mvpp2_tx_desc and mvpp2_rx_desc structures describe the
908 * layout of the transmit and reception DMA descriptors, and their
909 * layout is therefore defined by the hardware design
910 */
911
912#define MVPP2_TXD_L3_OFF_SHIFT 0
913#define MVPP2_TXD_IP_HLEN_SHIFT 8
914#define MVPP2_TXD_L4_CSUM_FRAG BIT(13)
915#define MVPP2_TXD_L4_CSUM_NOT BIT(14)
916#define MVPP2_TXD_IP_CSUM_DISABLE BIT(15)
917#define MVPP2_TXD_PADDING_DISABLE BIT(23)
918#define MVPP2_TXD_L4_UDP BIT(24)
919#define MVPP2_TXD_L3_IP6 BIT(26)
920#define MVPP2_TXD_L_DESC BIT(28)
921#define MVPP2_TXD_F_DESC BIT(29)
922
923#define MVPP2_RXD_ERR_SUMMARY BIT(15)
924#define MVPP2_RXD_ERR_CODE_MASK (BIT(13) | BIT(14))
925#define MVPP2_RXD_ERR_CRC 0x0
926#define MVPP2_RXD_ERR_OVERRUN BIT(13)
927#define MVPP2_RXD_ERR_RESOURCE (BIT(13) | BIT(14))
928#define MVPP2_RXD_BM_POOL_ID_OFFS 16
929#define MVPP2_RXD_BM_POOL_ID_MASK (BIT(16) | BIT(17) | BIT(18))
930#define MVPP2_RXD_HWF_SYNC BIT(21)
931#define MVPP2_RXD_L4_CSUM_OK BIT(22)
932#define MVPP2_RXD_IP4_HEADER_ERR BIT(24)
933#define MVPP2_RXD_L4_TCP BIT(25)
934#define MVPP2_RXD_L4_UDP BIT(26)
935#define MVPP2_RXD_L3_IP4 BIT(28)
936#define MVPP2_RXD_L3_IP6 BIT(30)
937#define MVPP2_RXD_BUF_HDR BIT(31)
938
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100939/* HW TX descriptor for PPv2.1 */
940struct mvpp21_tx_desc {
Marcin Wojtas3f518502014-07-10 16:52:13 -0300941 u32 command; /* Options used by HW for packet transmitting.*/
942 u8 packet_offset; /* the offset from the buffer beginning */
943 u8 phys_txq; /* destination queue ID */
944 u16 data_size; /* data size of transmitted packet in bytes */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100945 u32 buf_dma_addr; /* physical addr of transmitted buffer */
Marcin Wojtas3f518502014-07-10 16:52:13 -0300946 u32 buf_cookie; /* cookie for access to TX buffer in tx path */
947 u32 reserved1[3]; /* hw_cmd (for future use, BM, PON, PNC) */
948 u32 reserved2; /* reserved (for future use) */
949};
950
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100951/* HW RX descriptor for PPv2.1 */
952struct mvpp21_rx_desc {
Marcin Wojtas3f518502014-07-10 16:52:13 -0300953 u32 status; /* info about received packet */
954 u16 reserved1; /* parser_info (for future use, PnC) */
955 u16 data_size; /* size of received packet in bytes */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100956 u32 buf_dma_addr; /* physical address of the buffer */
Marcin Wojtas3f518502014-07-10 16:52:13 -0300957 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
958 u16 reserved2; /* gem_port_id (for future use, PON) */
959 u16 reserved3; /* csum_l4 (for future use, PnC) */
960 u8 reserved4; /* bm_qset (for future use, BM) */
961 u8 reserved5;
962 u16 reserved6; /* classify_info (for future use, PnC) */
963 u32 reserved7; /* flow_id (for future use, PnC) */
964 u32 reserved8;
965};
966
Thomas Petazzonie7c53592017-03-07 16:53:08 +0100967/* HW TX descriptor for PPv2.2 */
968struct mvpp22_tx_desc {
969 u32 command;
970 u8 packet_offset;
971 u8 phys_txq;
972 u16 data_size;
973 u64 reserved1;
974 u64 buf_dma_addr_ptp;
975 u64 buf_cookie_misc;
976};
977
978/* HW RX descriptor for PPv2.2 */
979struct mvpp22_rx_desc {
980 u32 status;
981 u16 reserved1;
982 u16 data_size;
983 u32 reserved2;
984 u32 reserved3;
985 u64 buf_dma_addr_key_hash;
986 u64 buf_cookie_misc;
987};
988
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100989/* Opaque type used by the driver to manipulate the HW TX and RX
990 * descriptors
991 */
992struct mvpp2_tx_desc {
993 union {
994 struct mvpp21_tx_desc pp21;
Thomas Petazzonie7c53592017-03-07 16:53:08 +0100995 struct mvpp22_tx_desc pp22;
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100996 };
997};
998
999struct mvpp2_rx_desc {
1000 union {
1001 struct mvpp21_rx_desc pp21;
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001002 struct mvpp22_rx_desc pp22;
Thomas Petazzoni054f6372017-03-07 16:53:07 +01001003 };
1004};
1005
Thomas Petazzoni83544912016-12-21 11:28:49 +01001006struct mvpp2_txq_pcpu_buf {
1007 /* Transmitted SKB */
1008 struct sk_buff *skb;
1009
1010 /* Physical address of transmitted buffer */
Thomas Petazzoni20396132017-03-07 16:53:00 +01001011 dma_addr_t dma;
Thomas Petazzoni83544912016-12-21 11:28:49 +01001012
1013 /* Size transmitted */
1014 size_t size;
1015};
1016
Marcin Wojtas3f518502014-07-10 16:52:13 -03001017/* Per-CPU Tx queue control */
1018struct mvpp2_txq_pcpu {
1019 int cpu;
1020
1021 /* Number of Tx DMA descriptors in the descriptor ring */
1022 int size;
1023
1024 /* Number of currently used Tx DMA descriptor in the
1025 * descriptor ring
1026 */
1027 int count;
1028
1029 /* Number of Tx DMA descriptors reserved for each CPU */
1030 int reserved_num;
1031
Thomas Petazzoni83544912016-12-21 11:28:49 +01001032 /* Infos about transmitted buffers */
1033 struct mvpp2_txq_pcpu_buf *buffs;
Marcin Wojtas71ce3912015-08-06 19:00:29 +02001034
Marcin Wojtas3f518502014-07-10 16:52:13 -03001035 /* Index of last TX DMA descriptor that was inserted */
1036 int txq_put_index;
1037
1038 /* Index of the TX DMA descriptor to be cleaned up */
1039 int txq_get_index;
Antoine Ténart186cd4d2017-08-23 09:46:56 +02001040
1041 /* DMA buffer for TSO headers */
1042 char *tso_headers;
1043 dma_addr_t tso_headers_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001044};
1045
1046struct mvpp2_tx_queue {
1047 /* Physical number of this Tx queue */
1048 u8 id;
1049
1050 /* Logical number of this Tx queue */
1051 u8 log_id;
1052
1053 /* Number of Tx DMA descriptors in the descriptor ring */
1054 int size;
1055
1056 /* Number of currently used Tx DMA descriptor in the descriptor ring */
1057 int count;
1058
1059 /* Per-CPU control of physical Tx queues */
1060 struct mvpp2_txq_pcpu __percpu *pcpu;
1061
Marcin Wojtas3f518502014-07-10 16:52:13 -03001062 u32 done_pkts_coal;
1063
1064 /* Virtual address of thex Tx DMA descriptors array */
1065 struct mvpp2_tx_desc *descs;
1066
1067 /* DMA address of the Tx DMA descriptors array */
Thomas Petazzoni20396132017-03-07 16:53:00 +01001068 dma_addr_t descs_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001069
1070 /* Index of the last Tx DMA descriptor */
1071 int last_desc;
1072
1073 /* Index of the next Tx DMA descriptor to process */
1074 int next_desc_to_proc;
1075};
1076
1077struct mvpp2_rx_queue {
1078 /* RX queue number, in the range 0-31 for physical RXQs */
1079 u8 id;
1080
1081 /* Num of rx descriptors in the rx descriptor ring */
1082 int size;
1083
1084 u32 pkts_coal;
1085 u32 time_coal;
1086
1087 /* Virtual address of the RX DMA descriptors array */
1088 struct mvpp2_rx_desc *descs;
1089
1090 /* DMA address of the RX DMA descriptors array */
Thomas Petazzoni20396132017-03-07 16:53:00 +01001091 dma_addr_t descs_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001092
1093 /* Index of the last RX DMA descriptor */
1094 int last_desc;
1095
1096 /* Index of the next RX DMA descriptor to process */
1097 int next_desc_to_proc;
1098
1099 /* ID of port to which physical RXQ is mapped */
1100 int port;
1101
1102 /* Port's logic RXQ number to which physical RXQ is mapped */
1103 int logic_rxq;
1104};
1105
1106union mvpp2_prs_tcam_entry {
1107 u32 word[MVPP2_PRS_TCAM_WORDS];
1108 u8 byte[MVPP2_PRS_TCAM_WORDS * 4];
1109};
1110
1111union mvpp2_prs_sram_entry {
1112 u32 word[MVPP2_PRS_SRAM_WORDS];
1113 u8 byte[MVPP2_PRS_SRAM_WORDS * 4];
1114};
1115
1116struct mvpp2_prs_entry {
1117 u32 index;
1118 union mvpp2_prs_tcam_entry tcam;
1119 union mvpp2_prs_sram_entry sram;
1120};
1121
1122struct mvpp2_prs_shadow {
1123 bool valid;
1124 bool finish;
1125
1126 /* Lookup ID */
1127 int lu;
1128
1129 /* User defined offset */
1130 int udf;
1131
1132 /* Result info */
1133 u32 ri;
1134 u32 ri_mask;
1135};
1136
1137struct mvpp2_cls_flow_entry {
1138 u32 index;
1139 u32 data[MVPP2_CLS_FLOWS_TBL_DATA_WORDS];
1140};
1141
1142struct mvpp2_cls_lookup_entry {
1143 u32 lkpid;
1144 u32 way;
1145 u32 data;
1146};
1147
1148struct mvpp2_bm_pool {
1149 /* Pool number in the range 0-7 */
1150 int id;
1151 enum mvpp2_bm_type type;
1152
1153 /* Buffer Pointers Pool External (BPPE) size */
1154 int size;
Thomas Petazzonid01524d2017-03-07 16:53:09 +01001155 /* BPPE size in bytes */
1156 int size_bytes;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001157 /* Number of buffers for this pool */
1158 int buf_num;
1159 /* Pool buffer size */
1160 int buf_size;
1161 /* Packet size */
1162 int pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01001163 int frag_size;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001164
1165 /* BPPE virtual base address */
1166 u32 *virt_addr;
Thomas Petazzoni20396132017-03-07 16:53:00 +01001167 /* BPPE DMA base address */
1168 dma_addr_t dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001169
1170 /* Ports using BM pool */
1171 u32 port_map;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001172};
1173
Antoine Tenart20920262017-10-23 15:24:30 +02001174#define IS_TSO_HEADER(txq_pcpu, addr) \
1175 ((addr) >= (txq_pcpu)->tso_headers_dma && \
1176 (addr) < (txq_pcpu)->tso_headers_dma + \
1177 (txq_pcpu)->size * TSO_HEADER_SIZE)
1178
Thomas Petazzoni213f4282017-08-03 10:42:00 +02001179/* Queue modes */
1180#define MVPP2_QDIST_SINGLE_MODE 0
1181#define MVPP2_QDIST_MULTI_MODE 1
1182
1183static int queue_mode = MVPP2_QDIST_SINGLE_MODE;
1184
1185module_param(queue_mode, int, 0444);
1186MODULE_PARM_DESC(queue_mode, "Set queue_mode (single=0, multi=1)");
1187
Marcin Wojtas3f518502014-07-10 16:52:13 -03001188#define MVPP2_DRIVER_NAME "mvpp2"
1189#define MVPP2_DRIVER_VERSION "1.0"
1190
1191/* Utility/helper methods */
1192
1193static void mvpp2_write(struct mvpp2 *priv, u32 offset, u32 data)
1194{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001195 writel(data, priv->swth_base[0] + offset);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001196}
1197
1198static u32 mvpp2_read(struct mvpp2 *priv, u32 offset)
1199{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001200 return readl(priv->swth_base[0] + offset);
Thomas Petazzonia7868412017-03-07 16:53:13 +01001201}
1202
1203/* These accessors should be used to access:
1204 *
1205 * - per-CPU registers, where each CPU has its own copy of the
1206 * register.
1207 *
1208 * MVPP2_BM_VIRT_ALLOC_REG
1209 * MVPP2_BM_ADDR_HIGH_ALLOC
1210 * MVPP22_BM_ADDR_HIGH_RLS_REG
1211 * MVPP2_BM_VIRT_RLS_REG
1212 * MVPP2_ISR_RX_TX_CAUSE_REG
1213 * MVPP2_ISR_RX_TX_MASK_REG
1214 * MVPP2_TXQ_NUM_REG
1215 * MVPP2_AGGR_TXQ_UPDATE_REG
1216 * MVPP2_TXQ_RSVD_REQ_REG
1217 * MVPP2_TXQ_RSVD_RSLT_REG
1218 * MVPP2_TXQ_SENT_REG
1219 * MVPP2_RXQ_NUM_REG
1220 *
1221 * - global registers that must be accessed through a specific CPU
1222 * window, because they are related to an access to a per-CPU
1223 * register
1224 *
1225 * MVPP2_BM_PHY_ALLOC_REG (related to MVPP2_BM_VIRT_ALLOC_REG)
1226 * MVPP2_BM_PHY_RLS_REG (related to MVPP2_BM_VIRT_RLS_REG)
1227 * MVPP2_RXQ_THRESH_REG (related to MVPP2_RXQ_NUM_REG)
1228 * MVPP2_RXQ_DESC_ADDR_REG (related to MVPP2_RXQ_NUM_REG)
1229 * MVPP2_RXQ_DESC_SIZE_REG (related to MVPP2_RXQ_NUM_REG)
1230 * MVPP2_RXQ_INDEX_REG (related to MVPP2_RXQ_NUM_REG)
1231 * MVPP2_TXQ_PENDING_REG (related to MVPP2_TXQ_NUM_REG)
1232 * MVPP2_TXQ_DESC_ADDR_REG (related to MVPP2_TXQ_NUM_REG)
1233 * MVPP2_TXQ_DESC_SIZE_REG (related to MVPP2_TXQ_NUM_REG)
1234 * MVPP2_TXQ_INDEX_REG (related to MVPP2_TXQ_NUM_REG)
1235 * MVPP2_TXQ_PENDING_REG (related to MVPP2_TXQ_NUM_REG)
1236 * MVPP2_TXQ_PREF_BUF_REG (related to MVPP2_TXQ_NUM_REG)
1237 * MVPP2_TXQ_PREF_BUF_REG (related to MVPP2_TXQ_NUM_REG)
1238 */
1239static void mvpp2_percpu_write(struct mvpp2 *priv, int cpu,
1240 u32 offset, u32 data)
1241{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001242 writel(data, priv->swth_base[cpu] + offset);
Thomas Petazzonia7868412017-03-07 16:53:13 +01001243}
1244
1245static u32 mvpp2_percpu_read(struct mvpp2 *priv, int cpu,
1246 u32 offset)
1247{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001248 return readl(priv->swth_base[cpu] + offset);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001249}
1250
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001251static dma_addr_t mvpp2_txdesc_dma_addr_get(struct mvpp2_port *port,
1252 struct mvpp2_tx_desc *tx_desc)
1253{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001254 if (port->priv->hw_version == MVPP21)
1255 return tx_desc->pp21.buf_dma_addr;
1256 else
1257 return tx_desc->pp22.buf_dma_addr_ptp & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001258}
1259
1260static void mvpp2_txdesc_dma_addr_set(struct mvpp2_port *port,
1261 struct mvpp2_tx_desc *tx_desc,
1262 dma_addr_t dma_addr)
1263{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001264 if (port->priv->hw_version == MVPP21) {
1265 tx_desc->pp21.buf_dma_addr = dma_addr;
1266 } else {
1267 u64 val = (u64)dma_addr;
1268
1269 tx_desc->pp22.buf_dma_addr_ptp &= ~GENMASK_ULL(40, 0);
1270 tx_desc->pp22.buf_dma_addr_ptp |= val;
1271 }
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001272}
1273
1274static size_t mvpp2_txdesc_size_get(struct mvpp2_port *port,
1275 struct mvpp2_tx_desc *tx_desc)
1276{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001277 if (port->priv->hw_version == MVPP21)
1278 return tx_desc->pp21.data_size;
1279 else
1280 return tx_desc->pp22.data_size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001281}
1282
1283static void mvpp2_txdesc_size_set(struct mvpp2_port *port,
1284 struct mvpp2_tx_desc *tx_desc,
1285 size_t size)
1286{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001287 if (port->priv->hw_version == MVPP21)
1288 tx_desc->pp21.data_size = size;
1289 else
1290 tx_desc->pp22.data_size = size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001291}
1292
1293static void mvpp2_txdesc_txq_set(struct mvpp2_port *port,
1294 struct mvpp2_tx_desc *tx_desc,
1295 unsigned int txq)
1296{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001297 if (port->priv->hw_version == MVPP21)
1298 tx_desc->pp21.phys_txq = txq;
1299 else
1300 tx_desc->pp22.phys_txq = txq;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001301}
1302
1303static void mvpp2_txdesc_cmd_set(struct mvpp2_port *port,
1304 struct mvpp2_tx_desc *tx_desc,
1305 unsigned int command)
1306{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001307 if (port->priv->hw_version == MVPP21)
1308 tx_desc->pp21.command = command;
1309 else
1310 tx_desc->pp22.command = command;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001311}
1312
1313static void mvpp2_txdesc_offset_set(struct mvpp2_port *port,
1314 struct mvpp2_tx_desc *tx_desc,
1315 unsigned int offset)
1316{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001317 if (port->priv->hw_version == MVPP21)
1318 tx_desc->pp21.packet_offset = offset;
1319 else
1320 tx_desc->pp22.packet_offset = offset;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001321}
1322
1323static unsigned int mvpp2_txdesc_offset_get(struct mvpp2_port *port,
1324 struct mvpp2_tx_desc *tx_desc)
1325{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001326 if (port->priv->hw_version == MVPP21)
1327 return tx_desc->pp21.packet_offset;
1328 else
1329 return tx_desc->pp22.packet_offset;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001330}
1331
1332static dma_addr_t mvpp2_rxdesc_dma_addr_get(struct mvpp2_port *port,
1333 struct mvpp2_rx_desc *rx_desc)
1334{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001335 if (port->priv->hw_version == MVPP21)
1336 return rx_desc->pp21.buf_dma_addr;
1337 else
1338 return rx_desc->pp22.buf_dma_addr_key_hash & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001339}
1340
1341static unsigned long mvpp2_rxdesc_cookie_get(struct mvpp2_port *port,
1342 struct mvpp2_rx_desc *rx_desc)
1343{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001344 if (port->priv->hw_version == MVPP21)
1345 return rx_desc->pp21.buf_cookie;
1346 else
1347 return rx_desc->pp22.buf_cookie_misc & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001348}
1349
1350static size_t mvpp2_rxdesc_size_get(struct mvpp2_port *port,
1351 struct mvpp2_rx_desc *rx_desc)
1352{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001353 if (port->priv->hw_version == MVPP21)
1354 return rx_desc->pp21.data_size;
1355 else
1356 return rx_desc->pp22.data_size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001357}
1358
1359static u32 mvpp2_rxdesc_status_get(struct mvpp2_port *port,
1360 struct mvpp2_rx_desc *rx_desc)
1361{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001362 if (port->priv->hw_version == MVPP21)
1363 return rx_desc->pp21.status;
1364 else
1365 return rx_desc->pp22.status;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001366}
1367
Marcin Wojtas3f518502014-07-10 16:52:13 -03001368static void mvpp2_txq_inc_get(struct mvpp2_txq_pcpu *txq_pcpu)
1369{
1370 txq_pcpu->txq_get_index++;
1371 if (txq_pcpu->txq_get_index == txq_pcpu->size)
1372 txq_pcpu->txq_get_index = 0;
1373}
1374
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001375static void mvpp2_txq_inc_put(struct mvpp2_port *port,
1376 struct mvpp2_txq_pcpu *txq_pcpu,
Marcin Wojtas71ce3912015-08-06 19:00:29 +02001377 struct sk_buff *skb,
1378 struct mvpp2_tx_desc *tx_desc)
Marcin Wojtas3f518502014-07-10 16:52:13 -03001379{
Thomas Petazzoni83544912016-12-21 11:28:49 +01001380 struct mvpp2_txq_pcpu_buf *tx_buf =
1381 txq_pcpu->buffs + txq_pcpu->txq_put_index;
1382 tx_buf->skb = skb;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001383 tx_buf->size = mvpp2_txdesc_size_get(port, tx_desc);
1384 tx_buf->dma = mvpp2_txdesc_dma_addr_get(port, tx_desc) +
1385 mvpp2_txdesc_offset_get(port, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001386 txq_pcpu->txq_put_index++;
1387 if (txq_pcpu->txq_put_index == txq_pcpu->size)
1388 txq_pcpu->txq_put_index = 0;
1389}
1390
1391/* Get number of physical egress port */
1392static inline int mvpp2_egress_port(struct mvpp2_port *port)
1393{
1394 return MVPP2_MAX_TCONT + port->id;
1395}
1396
1397/* Get number of physical TXQ */
1398static inline int mvpp2_txq_phys(int port, int txq)
1399{
1400 return (MVPP2_MAX_TCONT + port) * MVPP2_MAX_TXQ + txq;
1401}
1402
1403/* Parser configuration routines */
1404
1405/* Update parser tcam and sram hw entries */
1406static int mvpp2_prs_hw_write(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1407{
1408 int i;
1409
1410 if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1411 return -EINVAL;
1412
1413 /* Clear entry invalidation bit */
1414 pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] &= ~MVPP2_PRS_TCAM_INV_MASK;
1415
1416 /* Write tcam index - indirect access */
1417 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1418 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1419 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), pe->tcam.word[i]);
1420
1421 /* Write sram index - indirect access */
1422 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1423 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1424 mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), pe->sram.word[i]);
1425
1426 return 0;
1427}
1428
1429/* Read tcam entry from hw */
1430static int mvpp2_prs_hw_read(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1431{
1432 int i;
1433
1434 if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1435 return -EINVAL;
1436
1437 /* Write tcam index - indirect access */
1438 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1439
1440 pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] = mvpp2_read(priv,
1441 MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD));
1442 if (pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] & MVPP2_PRS_TCAM_INV_MASK)
1443 return MVPP2_PRS_TCAM_ENTRY_INVALID;
1444
1445 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1446 pe->tcam.word[i] = mvpp2_read(priv, MVPP2_PRS_TCAM_DATA_REG(i));
1447
1448 /* Write sram index - indirect access */
1449 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1450 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1451 pe->sram.word[i] = mvpp2_read(priv, MVPP2_PRS_SRAM_DATA_REG(i));
1452
1453 return 0;
1454}
1455
1456/* Invalidate tcam hw entry */
1457static void mvpp2_prs_hw_inv(struct mvpp2 *priv, int index)
1458{
1459 /* Write index - indirect access */
1460 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
1461 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD),
1462 MVPP2_PRS_TCAM_INV_MASK);
1463}
1464
1465/* Enable shadow table entry and set its lookup ID */
1466static void mvpp2_prs_shadow_set(struct mvpp2 *priv, int index, int lu)
1467{
1468 priv->prs_shadow[index].valid = true;
1469 priv->prs_shadow[index].lu = lu;
1470}
1471
1472/* Update ri fields in shadow table entry */
1473static void mvpp2_prs_shadow_ri_set(struct mvpp2 *priv, int index,
1474 unsigned int ri, unsigned int ri_mask)
1475{
1476 priv->prs_shadow[index].ri_mask = ri_mask;
1477 priv->prs_shadow[index].ri = ri;
1478}
1479
1480/* Update lookup field in tcam sw entry */
1481static void mvpp2_prs_tcam_lu_set(struct mvpp2_prs_entry *pe, unsigned int lu)
1482{
1483 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_LU_BYTE);
1484
1485 pe->tcam.byte[MVPP2_PRS_TCAM_LU_BYTE] = lu;
1486 pe->tcam.byte[enable_off] = MVPP2_PRS_LU_MASK;
1487}
1488
1489/* Update mask for single port in tcam sw entry */
1490static void mvpp2_prs_tcam_port_set(struct mvpp2_prs_entry *pe,
1491 unsigned int port, bool add)
1492{
1493 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1494
1495 if (add)
1496 pe->tcam.byte[enable_off] &= ~(1 << port);
1497 else
1498 pe->tcam.byte[enable_off] |= 1 << port;
1499}
1500
1501/* Update port map in tcam sw entry */
1502static void mvpp2_prs_tcam_port_map_set(struct mvpp2_prs_entry *pe,
1503 unsigned int ports)
1504{
1505 unsigned char port_mask = MVPP2_PRS_PORT_MASK;
1506 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1507
1508 pe->tcam.byte[MVPP2_PRS_TCAM_PORT_BYTE] = 0;
1509 pe->tcam.byte[enable_off] &= ~port_mask;
1510 pe->tcam.byte[enable_off] |= ~ports & MVPP2_PRS_PORT_MASK;
1511}
1512
1513/* Obtain port map from tcam sw entry */
1514static unsigned int mvpp2_prs_tcam_port_map_get(struct mvpp2_prs_entry *pe)
1515{
1516 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1517
1518 return ~(pe->tcam.byte[enable_off]) & MVPP2_PRS_PORT_MASK;
1519}
1520
1521/* Set byte of data and its enable bits in tcam sw entry */
1522static void mvpp2_prs_tcam_data_byte_set(struct mvpp2_prs_entry *pe,
1523 unsigned int offs, unsigned char byte,
1524 unsigned char enable)
1525{
1526 pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)] = byte;
1527 pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)] = enable;
1528}
1529
1530/* Get byte of data and its enable bits from tcam sw entry */
1531static void mvpp2_prs_tcam_data_byte_get(struct mvpp2_prs_entry *pe,
1532 unsigned int offs, unsigned char *byte,
1533 unsigned char *enable)
1534{
1535 *byte = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)];
1536 *enable = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)];
1537}
1538
1539/* Compare tcam data bytes with a pattern */
1540static bool mvpp2_prs_tcam_data_cmp(struct mvpp2_prs_entry *pe, int offs,
1541 u16 data)
1542{
1543 int off = MVPP2_PRS_TCAM_DATA_BYTE(offs);
1544 u16 tcam_data;
1545
Antoine Tenartef4816f2017-10-24 11:41:26 +02001546 tcam_data = (pe->tcam.byte[off + 1] << 8) | pe->tcam.byte[off];
Marcin Wojtas3f518502014-07-10 16:52:13 -03001547 if (tcam_data != data)
1548 return false;
1549 return true;
1550}
1551
1552/* Update ai bits in tcam sw entry */
1553static void mvpp2_prs_tcam_ai_update(struct mvpp2_prs_entry *pe,
1554 unsigned int bits, unsigned int enable)
1555{
1556 int i, ai_idx = MVPP2_PRS_TCAM_AI_BYTE;
1557
1558 for (i = 0; i < MVPP2_PRS_AI_BITS; i++) {
1559
1560 if (!(enable & BIT(i)))
1561 continue;
1562
1563 if (bits & BIT(i))
1564 pe->tcam.byte[ai_idx] |= 1 << i;
1565 else
1566 pe->tcam.byte[ai_idx] &= ~(1 << i);
1567 }
1568
1569 pe->tcam.byte[MVPP2_PRS_TCAM_EN_OFFS(ai_idx)] |= enable;
1570}
1571
1572/* Get ai bits from tcam sw entry */
1573static int mvpp2_prs_tcam_ai_get(struct mvpp2_prs_entry *pe)
1574{
1575 return pe->tcam.byte[MVPP2_PRS_TCAM_AI_BYTE];
1576}
1577
1578/* Set ethertype in tcam sw entry */
1579static void mvpp2_prs_match_etype(struct mvpp2_prs_entry *pe, int offset,
1580 unsigned short ethertype)
1581{
1582 mvpp2_prs_tcam_data_byte_set(pe, offset + 0, ethertype >> 8, 0xff);
1583 mvpp2_prs_tcam_data_byte_set(pe, offset + 1, ethertype & 0xff, 0xff);
1584}
1585
1586/* Set bits in sram sw entry */
1587static void mvpp2_prs_sram_bits_set(struct mvpp2_prs_entry *pe, int bit_num,
1588 int val)
1589{
1590 pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] |= (val << (bit_num % 8));
1591}
1592
1593/* Clear bits in sram sw entry */
1594static void mvpp2_prs_sram_bits_clear(struct mvpp2_prs_entry *pe, int bit_num,
1595 int val)
1596{
1597 pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] &= ~(val << (bit_num % 8));
1598}
1599
1600/* Update ri bits in sram sw entry */
1601static void mvpp2_prs_sram_ri_update(struct mvpp2_prs_entry *pe,
1602 unsigned int bits, unsigned int mask)
1603{
1604 unsigned int i;
1605
1606 for (i = 0; i < MVPP2_PRS_SRAM_RI_CTRL_BITS; i++) {
1607 int ri_off = MVPP2_PRS_SRAM_RI_OFFS;
1608
1609 if (!(mask & BIT(i)))
1610 continue;
1611
1612 if (bits & BIT(i))
1613 mvpp2_prs_sram_bits_set(pe, ri_off + i, 1);
1614 else
1615 mvpp2_prs_sram_bits_clear(pe, ri_off + i, 1);
1616
1617 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_RI_CTRL_OFFS + i, 1);
1618 }
1619}
1620
1621/* Obtain ri bits from sram sw entry */
1622static int mvpp2_prs_sram_ri_get(struct mvpp2_prs_entry *pe)
1623{
1624 return pe->sram.word[MVPP2_PRS_SRAM_RI_WORD];
1625}
1626
1627/* Update ai bits in sram sw entry */
1628static void mvpp2_prs_sram_ai_update(struct mvpp2_prs_entry *pe,
1629 unsigned int bits, unsigned int mask)
1630{
1631 unsigned int i;
1632 int ai_off = MVPP2_PRS_SRAM_AI_OFFS;
1633
1634 for (i = 0; i < MVPP2_PRS_SRAM_AI_CTRL_BITS; i++) {
1635
1636 if (!(mask & BIT(i)))
1637 continue;
1638
1639 if (bits & BIT(i))
1640 mvpp2_prs_sram_bits_set(pe, ai_off + i, 1);
1641 else
1642 mvpp2_prs_sram_bits_clear(pe, ai_off + i, 1);
1643
1644 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_AI_CTRL_OFFS + i, 1);
1645 }
1646}
1647
1648/* Read ai bits from sram sw entry */
1649static int mvpp2_prs_sram_ai_get(struct mvpp2_prs_entry *pe)
1650{
1651 u8 bits;
1652 int ai_off = MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_AI_OFFS);
1653 int ai_en_off = ai_off + 1;
1654 int ai_shift = MVPP2_PRS_SRAM_AI_OFFS % 8;
1655
1656 bits = (pe->sram.byte[ai_off] >> ai_shift) |
1657 (pe->sram.byte[ai_en_off] << (8 - ai_shift));
1658
1659 return bits;
1660}
1661
1662/* In sram sw entry set lookup ID field of the tcam key to be used in the next
1663 * lookup interation
1664 */
1665static void mvpp2_prs_sram_next_lu_set(struct mvpp2_prs_entry *pe,
1666 unsigned int lu)
1667{
1668 int sram_next_off = MVPP2_PRS_SRAM_NEXT_LU_OFFS;
1669
1670 mvpp2_prs_sram_bits_clear(pe, sram_next_off,
1671 MVPP2_PRS_SRAM_NEXT_LU_MASK);
1672 mvpp2_prs_sram_bits_set(pe, sram_next_off, lu);
1673}
1674
1675/* In the sram sw entry set sign and value of the next lookup offset
1676 * and the offset value generated to the classifier
1677 */
1678static void mvpp2_prs_sram_shift_set(struct mvpp2_prs_entry *pe, int shift,
1679 unsigned int op)
1680{
1681 /* Set sign */
1682 if (shift < 0) {
1683 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1684 shift = 0 - shift;
1685 } else {
1686 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1687 }
1688
1689 /* Set value */
1690 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_SHIFT_OFFS)] =
1691 (unsigned char)shift;
1692
1693 /* Reset and set operation */
1694 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS,
1695 MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK);
1696 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS, op);
1697
1698 /* Set base offset as current */
1699 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1700}
1701
1702/* In the sram sw entry set sign and value of the user defined offset
1703 * generated to the classifier
1704 */
1705static void mvpp2_prs_sram_offset_set(struct mvpp2_prs_entry *pe,
1706 unsigned int type, int offset,
1707 unsigned int op)
1708{
1709 /* Set sign */
1710 if (offset < 0) {
1711 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1712 offset = 0 - offset;
1713 } else {
1714 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1715 }
1716
1717 /* Set value */
1718 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_OFFS,
1719 MVPP2_PRS_SRAM_UDF_MASK);
1720 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_OFFS, offset);
1721 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1722 MVPP2_PRS_SRAM_UDF_BITS)] &=
1723 ~(MVPP2_PRS_SRAM_UDF_MASK >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1724 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1725 MVPP2_PRS_SRAM_UDF_BITS)] |=
1726 (offset >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1727
1728 /* Set offset type */
1729 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS,
1730 MVPP2_PRS_SRAM_UDF_TYPE_MASK);
1731 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS, type);
1732
1733 /* Set offset operation */
1734 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS,
1735 MVPP2_PRS_SRAM_OP_SEL_UDF_MASK);
1736 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS, op);
1737
1738 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1739 MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] &=
1740 ~(MVPP2_PRS_SRAM_OP_SEL_UDF_MASK >>
1741 (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1742
1743 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1744 MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] |=
1745 (op >> (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1746
1747 /* Set base offset as current */
1748 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1749}
1750
1751/* Find parser flow entry */
1752static struct mvpp2_prs_entry *mvpp2_prs_flow_find(struct mvpp2 *priv, int flow)
1753{
1754 struct mvpp2_prs_entry *pe;
1755 int tid;
1756
1757 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
1758 if (!pe)
1759 return NULL;
1760 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
1761
1762 /* Go through the all entires with MVPP2_PRS_LU_FLOWS */
1763 for (tid = MVPP2_PRS_TCAM_SRAM_SIZE - 1; tid >= 0; tid--) {
1764 u8 bits;
1765
1766 if (!priv->prs_shadow[tid].valid ||
1767 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_FLOWS)
1768 continue;
1769
1770 pe->index = tid;
1771 mvpp2_prs_hw_read(priv, pe);
1772 bits = mvpp2_prs_sram_ai_get(pe);
1773
1774 /* Sram store classification lookup ID in AI bits [5:0] */
1775 if ((bits & MVPP2_PRS_FLOW_ID_MASK) == flow)
1776 return pe;
1777 }
1778 kfree(pe);
1779
1780 return NULL;
1781}
1782
1783/* Return first free tcam index, seeking from start to end */
1784static int mvpp2_prs_tcam_first_free(struct mvpp2 *priv, unsigned char start,
1785 unsigned char end)
1786{
1787 int tid;
1788
1789 if (start > end)
1790 swap(start, end);
1791
1792 if (end >= MVPP2_PRS_TCAM_SRAM_SIZE)
1793 end = MVPP2_PRS_TCAM_SRAM_SIZE - 1;
1794
1795 for (tid = start; tid <= end; tid++) {
1796 if (!priv->prs_shadow[tid].valid)
1797 return tid;
1798 }
1799
1800 return -EINVAL;
1801}
1802
1803/* Enable/disable dropping all mac da's */
1804static void mvpp2_prs_mac_drop_all_set(struct mvpp2 *priv, int port, bool add)
1805{
1806 struct mvpp2_prs_entry pe;
1807
1808 if (priv->prs_shadow[MVPP2_PE_DROP_ALL].valid) {
1809 /* Entry exist - update port only */
1810 pe.index = MVPP2_PE_DROP_ALL;
1811 mvpp2_prs_hw_read(priv, &pe);
1812 } else {
1813 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001814 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001815 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1816 pe.index = MVPP2_PE_DROP_ALL;
1817
1818 /* Non-promiscuous mode for all ports - DROP unknown packets */
1819 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
1820 MVPP2_PRS_RI_DROP_MASK);
1821
1822 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1823 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1824
1825 /* Update shadow table */
1826 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1827
1828 /* Mask all ports */
1829 mvpp2_prs_tcam_port_map_set(&pe, 0);
1830 }
1831
1832 /* Update port mask */
1833 mvpp2_prs_tcam_port_set(&pe, port, add);
1834
1835 mvpp2_prs_hw_write(priv, &pe);
1836}
1837
1838/* Set port to promiscuous mode */
1839static void mvpp2_prs_mac_promisc_set(struct mvpp2 *priv, int port, bool add)
1840{
1841 struct mvpp2_prs_entry pe;
1842
Joe Perchesdbedd442015-03-06 20:49:12 -08001843 /* Promiscuous mode - Accept unknown packets */
Marcin Wojtas3f518502014-07-10 16:52:13 -03001844
1845 if (priv->prs_shadow[MVPP2_PE_MAC_PROMISCUOUS].valid) {
1846 /* Entry exist - update port only */
1847 pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1848 mvpp2_prs_hw_read(priv, &pe);
1849 } else {
1850 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001851 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001852 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1853 pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1854
1855 /* Continue - set next lookup */
1856 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
1857
1858 /* Set result info bits */
1859 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_UCAST,
1860 MVPP2_PRS_RI_L2_CAST_MASK);
1861
1862 /* Shift to ethertype */
1863 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
1864 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1865
1866 /* Mask all ports */
1867 mvpp2_prs_tcam_port_map_set(&pe, 0);
1868
1869 /* Update shadow table */
1870 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1871 }
1872
1873 /* Update port mask */
1874 mvpp2_prs_tcam_port_set(&pe, port, add);
1875
1876 mvpp2_prs_hw_write(priv, &pe);
1877}
1878
1879/* Accept multicast */
1880static void mvpp2_prs_mac_multi_set(struct mvpp2 *priv, int port, int index,
1881 bool add)
1882{
1883 struct mvpp2_prs_entry pe;
1884 unsigned char da_mc;
1885
1886 /* Ethernet multicast address first byte is
1887 * 0x01 for IPv4 and 0x33 for IPv6
1888 */
1889 da_mc = (index == MVPP2_PE_MAC_MC_ALL) ? 0x01 : 0x33;
1890
1891 if (priv->prs_shadow[index].valid) {
1892 /* Entry exist - update port only */
1893 pe.index = index;
1894 mvpp2_prs_hw_read(priv, &pe);
1895 } else {
1896 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001897 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001898 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1899 pe.index = index;
1900
1901 /* Continue - set next lookup */
1902 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
1903
1904 /* Set result info bits */
1905 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_MCAST,
1906 MVPP2_PRS_RI_L2_CAST_MASK);
1907
1908 /* Update tcam entry data first byte */
1909 mvpp2_prs_tcam_data_byte_set(&pe, 0, da_mc, 0xff);
1910
1911 /* Shift to ethertype */
1912 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
1913 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1914
1915 /* Mask all ports */
1916 mvpp2_prs_tcam_port_map_set(&pe, 0);
1917
1918 /* Update shadow table */
1919 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1920 }
1921
1922 /* Update port mask */
1923 mvpp2_prs_tcam_port_set(&pe, port, add);
1924
1925 mvpp2_prs_hw_write(priv, &pe);
1926}
1927
1928/* Set entry for dsa packets */
1929static void mvpp2_prs_dsa_tag_set(struct mvpp2 *priv, int port, bool add,
1930 bool tagged, bool extend)
1931{
1932 struct mvpp2_prs_entry pe;
1933 int tid, shift;
1934
1935 if (extend) {
1936 tid = tagged ? MVPP2_PE_EDSA_TAGGED : MVPP2_PE_EDSA_UNTAGGED;
1937 shift = 8;
1938 } else {
1939 tid = tagged ? MVPP2_PE_DSA_TAGGED : MVPP2_PE_DSA_UNTAGGED;
1940 shift = 4;
1941 }
1942
1943 if (priv->prs_shadow[tid].valid) {
1944 /* Entry exist - update port only */
1945 pe.index = tid;
1946 mvpp2_prs_hw_read(priv, &pe);
1947 } else {
1948 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001949 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001950 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
1951 pe.index = tid;
1952
1953 /* Shift 4 bytes if DSA tag or 8 bytes in case of EDSA tag*/
1954 mvpp2_prs_sram_shift_set(&pe, shift,
1955 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1956
1957 /* Update shadow table */
1958 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
1959
1960 if (tagged) {
1961 /* Set tagged bit in DSA tag */
1962 mvpp2_prs_tcam_data_byte_set(&pe, 0,
1963 MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
1964 MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
1965 /* Clear all ai bits for next iteration */
1966 mvpp2_prs_sram_ai_update(&pe, 0,
1967 MVPP2_PRS_SRAM_AI_MASK);
1968 /* If packet is tagged continue check vlans */
1969 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
1970 } else {
1971 /* Set result info bits to 'no vlans' */
1972 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
1973 MVPP2_PRS_RI_VLAN_MASK);
1974 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
1975 }
1976
1977 /* Mask all ports */
1978 mvpp2_prs_tcam_port_map_set(&pe, 0);
1979 }
1980
1981 /* Update port mask */
1982 mvpp2_prs_tcam_port_set(&pe, port, add);
1983
1984 mvpp2_prs_hw_write(priv, &pe);
1985}
1986
1987/* Set entry for dsa ethertype */
1988static void mvpp2_prs_dsa_tag_ethertype_set(struct mvpp2 *priv, int port,
1989 bool add, bool tagged, bool extend)
1990{
1991 struct mvpp2_prs_entry pe;
1992 int tid, shift, port_mask;
1993
1994 if (extend) {
1995 tid = tagged ? MVPP2_PE_ETYPE_EDSA_TAGGED :
1996 MVPP2_PE_ETYPE_EDSA_UNTAGGED;
1997 port_mask = 0;
1998 shift = 8;
1999 } else {
2000 tid = tagged ? MVPP2_PE_ETYPE_DSA_TAGGED :
2001 MVPP2_PE_ETYPE_DSA_UNTAGGED;
2002 port_mask = MVPP2_PRS_PORT_MASK;
2003 shift = 4;
2004 }
2005
2006 if (priv->prs_shadow[tid].valid) {
2007 /* Entry exist - update port only */
2008 pe.index = tid;
2009 mvpp2_prs_hw_read(priv, &pe);
2010 } else {
2011 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002012 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002013 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
2014 pe.index = tid;
2015
2016 /* Set ethertype */
2017 mvpp2_prs_match_etype(&pe, 0, ETH_P_EDSA);
2018 mvpp2_prs_match_etype(&pe, 2, 0);
2019
2020 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DSA_MASK,
2021 MVPP2_PRS_RI_DSA_MASK);
2022 /* Shift ethertype + 2 byte reserved + tag*/
2023 mvpp2_prs_sram_shift_set(&pe, 2 + MVPP2_ETH_TYPE_LEN + shift,
2024 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2025
2026 /* Update shadow table */
2027 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
2028
2029 if (tagged) {
2030 /* Set tagged bit in DSA tag */
2031 mvpp2_prs_tcam_data_byte_set(&pe,
2032 MVPP2_ETH_TYPE_LEN + 2 + 3,
2033 MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
2034 MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
2035 /* Clear all ai bits for next iteration */
2036 mvpp2_prs_sram_ai_update(&pe, 0,
2037 MVPP2_PRS_SRAM_AI_MASK);
2038 /* If packet is tagged continue check vlans */
2039 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2040 } else {
2041 /* Set result info bits to 'no vlans' */
2042 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
2043 MVPP2_PRS_RI_VLAN_MASK);
2044 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2045 }
2046 /* Mask/unmask all ports, depending on dsa type */
2047 mvpp2_prs_tcam_port_map_set(&pe, port_mask);
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/* Search for existing single/triple vlan entry */
2057static struct mvpp2_prs_entry *mvpp2_prs_vlan_find(struct mvpp2 *priv,
2058 unsigned short tpid, int ai)
2059{
2060 struct mvpp2_prs_entry *pe;
2061 int tid;
2062
2063 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2064 if (!pe)
2065 return NULL;
2066 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2067
2068 /* Go through the all entries with MVPP2_PRS_LU_VLAN */
2069 for (tid = MVPP2_PE_FIRST_FREE_TID;
2070 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
2071 unsigned int ri_bits, ai_bits;
2072 bool match;
2073
2074 if (!priv->prs_shadow[tid].valid ||
2075 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
2076 continue;
2077
2078 pe->index = tid;
2079
2080 mvpp2_prs_hw_read(priv, pe);
2081 match = mvpp2_prs_tcam_data_cmp(pe, 0, swab16(tpid));
2082 if (!match)
2083 continue;
2084
2085 /* Get vlan type */
2086 ri_bits = mvpp2_prs_sram_ri_get(pe);
2087 ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
2088
2089 /* Get current ai value from tcam */
2090 ai_bits = mvpp2_prs_tcam_ai_get(pe);
2091 /* Clear double vlan bit */
2092 ai_bits &= ~MVPP2_PRS_DBL_VLAN_AI_BIT;
2093
2094 if (ai != ai_bits)
2095 continue;
2096
2097 if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
2098 ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
2099 return pe;
2100 }
2101 kfree(pe);
2102
2103 return NULL;
2104}
2105
2106/* Add/update single/triple vlan entry */
2107static int mvpp2_prs_vlan_add(struct mvpp2 *priv, unsigned short tpid, int ai,
2108 unsigned int port_map)
2109{
2110 struct mvpp2_prs_entry *pe;
2111 int tid_aux, tid;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302112 int ret = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002113
2114 pe = mvpp2_prs_vlan_find(priv, tpid, ai);
2115
2116 if (!pe) {
2117 /* Create new tcam entry */
2118 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_LAST_FREE_TID,
2119 MVPP2_PE_FIRST_FREE_TID);
2120 if (tid < 0)
2121 return tid;
2122
2123 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2124 if (!pe)
2125 return -ENOMEM;
2126
2127 /* Get last double vlan tid */
2128 for (tid_aux = MVPP2_PE_LAST_FREE_TID;
2129 tid_aux >= MVPP2_PE_FIRST_FREE_TID; tid_aux--) {
2130 unsigned int ri_bits;
2131
2132 if (!priv->prs_shadow[tid_aux].valid ||
2133 priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
2134 continue;
2135
2136 pe->index = tid_aux;
2137 mvpp2_prs_hw_read(priv, pe);
2138 ri_bits = mvpp2_prs_sram_ri_get(pe);
2139 if ((ri_bits & MVPP2_PRS_RI_VLAN_MASK) ==
2140 MVPP2_PRS_RI_VLAN_DOUBLE)
2141 break;
2142 }
2143
Sudip Mukherjee43737472014-11-01 16:59:34 +05302144 if (tid <= tid_aux) {
2145 ret = -EINVAL;
Markus Elfringf9fd0e32017-04-17 13:50:35 +02002146 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302147 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002148
Markus Elfringbd6aaf52017-04-17 10:40:32 +02002149 memset(pe, 0, sizeof(*pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002150 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2151 pe->index = tid;
2152
2153 mvpp2_prs_match_etype(pe, 0, tpid);
2154
2155 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_L2);
2156 /* Shift 4 bytes - skip 1 vlan tag */
2157 mvpp2_prs_sram_shift_set(pe, MVPP2_VLAN_TAG_LEN,
2158 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2159 /* Clear all ai bits for next iteration */
2160 mvpp2_prs_sram_ai_update(pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2161
2162 if (ai == MVPP2_PRS_SINGLE_VLAN_AI) {
2163 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_SINGLE,
2164 MVPP2_PRS_RI_VLAN_MASK);
2165 } else {
2166 ai |= MVPP2_PRS_DBL_VLAN_AI_BIT;
2167 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_TRIPLE,
2168 MVPP2_PRS_RI_VLAN_MASK);
2169 }
2170 mvpp2_prs_tcam_ai_update(pe, ai, MVPP2_PRS_SRAM_AI_MASK);
2171
2172 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_VLAN);
2173 }
2174 /* Update ports' mask */
2175 mvpp2_prs_tcam_port_map_set(pe, port_map);
2176
2177 mvpp2_prs_hw_write(priv, pe);
Markus Elfringf9fd0e32017-04-17 13:50:35 +02002178free_pe:
Marcin Wojtas3f518502014-07-10 16:52:13 -03002179 kfree(pe);
2180
Sudip Mukherjee43737472014-11-01 16:59:34 +05302181 return ret;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002182}
2183
2184/* Get first free double vlan ai number */
2185static int mvpp2_prs_double_vlan_ai_free_get(struct mvpp2 *priv)
2186{
2187 int i;
2188
2189 for (i = 1; i < MVPP2_PRS_DBL_VLANS_MAX; i++) {
2190 if (!priv->prs_double_vlans[i])
2191 return i;
2192 }
2193
2194 return -EINVAL;
2195}
2196
2197/* Search for existing double vlan entry */
2198static struct mvpp2_prs_entry *mvpp2_prs_double_vlan_find(struct mvpp2 *priv,
2199 unsigned short tpid1,
2200 unsigned short tpid2)
2201{
2202 struct mvpp2_prs_entry *pe;
2203 int tid;
2204
2205 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2206 if (!pe)
2207 return NULL;
2208 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2209
2210 /* Go through the all entries with MVPP2_PRS_LU_VLAN */
2211 for (tid = MVPP2_PE_FIRST_FREE_TID;
2212 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
2213 unsigned int ri_mask;
2214 bool match;
2215
2216 if (!priv->prs_shadow[tid].valid ||
2217 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
2218 continue;
2219
2220 pe->index = tid;
2221 mvpp2_prs_hw_read(priv, pe);
2222
2223 match = mvpp2_prs_tcam_data_cmp(pe, 0, swab16(tpid1))
2224 && mvpp2_prs_tcam_data_cmp(pe, 4, swab16(tpid2));
2225
2226 if (!match)
2227 continue;
2228
2229 ri_mask = mvpp2_prs_sram_ri_get(pe) & MVPP2_PRS_RI_VLAN_MASK;
2230 if (ri_mask == MVPP2_PRS_RI_VLAN_DOUBLE)
2231 return pe;
2232 }
2233 kfree(pe);
2234
2235 return NULL;
2236}
2237
2238/* Add or update double vlan entry */
2239static int mvpp2_prs_double_vlan_add(struct mvpp2 *priv, unsigned short tpid1,
2240 unsigned short tpid2,
2241 unsigned int port_map)
2242{
2243 struct mvpp2_prs_entry *pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302244 int tid_aux, tid, ai, ret = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002245
2246 pe = mvpp2_prs_double_vlan_find(priv, tpid1, tpid2);
2247
2248 if (!pe) {
2249 /* Create new tcam entry */
2250 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2251 MVPP2_PE_LAST_FREE_TID);
2252 if (tid < 0)
2253 return tid;
2254
2255 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2256 if (!pe)
2257 return -ENOMEM;
2258
2259 /* Set ai value for new double vlan entry */
2260 ai = mvpp2_prs_double_vlan_ai_free_get(priv);
Sudip Mukherjee43737472014-11-01 16:59:34 +05302261 if (ai < 0) {
2262 ret = ai;
Markus Elfringc9a7e122017-04-17 13:03:49 +02002263 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302264 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002265
2266 /* Get first single/triple vlan tid */
2267 for (tid_aux = MVPP2_PE_FIRST_FREE_TID;
2268 tid_aux <= MVPP2_PE_LAST_FREE_TID; tid_aux++) {
2269 unsigned int ri_bits;
2270
2271 if (!priv->prs_shadow[tid_aux].valid ||
2272 priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
2273 continue;
2274
2275 pe->index = tid_aux;
2276 mvpp2_prs_hw_read(priv, pe);
2277 ri_bits = mvpp2_prs_sram_ri_get(pe);
2278 ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
2279 if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
2280 ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
2281 break;
2282 }
2283
Sudip Mukherjee43737472014-11-01 16:59:34 +05302284 if (tid >= tid_aux) {
2285 ret = -ERANGE;
Markus Elfringc9a7e122017-04-17 13:03:49 +02002286 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302287 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002288
Markus Elfringbd6aaf52017-04-17 10:40:32 +02002289 memset(pe, 0, sizeof(*pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002290 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2291 pe->index = tid;
2292
2293 priv->prs_double_vlans[ai] = true;
2294
2295 mvpp2_prs_match_etype(pe, 0, tpid1);
2296 mvpp2_prs_match_etype(pe, 4, tpid2);
2297
2298 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_VLAN);
2299 /* Shift 8 bytes - skip 2 vlan tags */
2300 mvpp2_prs_sram_shift_set(pe, 2 * MVPP2_VLAN_TAG_LEN,
2301 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2302 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_DOUBLE,
2303 MVPP2_PRS_RI_VLAN_MASK);
2304 mvpp2_prs_sram_ai_update(pe, ai | MVPP2_PRS_DBL_VLAN_AI_BIT,
2305 MVPP2_PRS_SRAM_AI_MASK);
2306
2307 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_VLAN);
2308 }
2309
2310 /* Update ports' mask */
2311 mvpp2_prs_tcam_port_map_set(pe, port_map);
2312 mvpp2_prs_hw_write(priv, pe);
Markus Elfringc9a7e122017-04-17 13:03:49 +02002313free_pe:
Marcin Wojtas3f518502014-07-10 16:52:13 -03002314 kfree(pe);
Sudip Mukherjee43737472014-11-01 16:59:34 +05302315 return ret;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002316}
2317
2318/* IPv4 header parsing for fragmentation and L4 offset */
2319static int mvpp2_prs_ip4_proto(struct mvpp2 *priv, unsigned short proto,
2320 unsigned int ri, unsigned int ri_mask)
2321{
2322 struct mvpp2_prs_entry pe;
2323 int tid;
2324
2325 if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
2326 (proto != IPPROTO_IGMP))
2327 return -EINVAL;
2328
Stefan Chulskiaff3da32017-09-25 14:59:46 +02002329 /* Not fragmented packet */
Marcin Wojtas3f518502014-07-10 16:52:13 -03002330 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2331 MVPP2_PE_LAST_FREE_TID);
2332 if (tid < 0)
2333 return tid;
2334
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002335 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002336 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
2337 pe.index = tid;
2338
2339 /* Set next lu to IPv4 */
2340 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2341 mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2342 /* Set L4 offset */
2343 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
2344 sizeof(struct iphdr) - 4,
2345 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2346 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
2347 MVPP2_PRS_IPV4_DIP_AI_BIT);
Stefan Chulskiaff3da32017-09-25 14:59:46 +02002348 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask | MVPP2_PRS_RI_IP_FRAG_MASK);
2349
2350 mvpp2_prs_tcam_data_byte_set(&pe, 2, 0x00,
2351 MVPP2_PRS_TCAM_PROTO_MASK_L);
2352 mvpp2_prs_tcam_data_byte_set(&pe, 3, 0x00,
2353 MVPP2_PRS_TCAM_PROTO_MASK);
Marcin Wojtas3f518502014-07-10 16:52:13 -03002354
2355 mvpp2_prs_tcam_data_byte_set(&pe, 5, proto, MVPP2_PRS_TCAM_PROTO_MASK);
2356 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
2357 /* Unmask all ports */
2358 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2359
2360 /* Update shadow table and hw entry */
2361 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2362 mvpp2_prs_hw_write(priv, &pe);
2363
Stefan Chulskiaff3da32017-09-25 14:59:46 +02002364 /* Fragmented packet */
Marcin Wojtas3f518502014-07-10 16:52:13 -03002365 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2366 MVPP2_PE_LAST_FREE_TID);
2367 if (tid < 0)
2368 return tid;
2369
2370 pe.index = tid;
2371 /* Clear ri before updating */
2372 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2373 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2374 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
2375
Stefan Chulskiaff3da32017-09-25 14:59:46 +02002376 mvpp2_prs_sram_ri_update(&pe, ri | MVPP2_PRS_RI_IP_FRAG_TRUE,
2377 ri_mask | MVPP2_PRS_RI_IP_FRAG_MASK);
2378
2379 mvpp2_prs_tcam_data_byte_set(&pe, 2, 0x00, 0x0);
2380 mvpp2_prs_tcam_data_byte_set(&pe, 3, 0x00, 0x0);
Marcin Wojtas3f518502014-07-10 16:52:13 -03002381
2382 /* Update shadow table and hw entry */
2383 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2384 mvpp2_prs_hw_write(priv, &pe);
2385
2386 return 0;
2387}
2388
2389/* IPv4 L3 multicast or broadcast */
2390static int mvpp2_prs_ip4_cast(struct mvpp2 *priv, unsigned short l3_cast)
2391{
2392 struct mvpp2_prs_entry pe;
2393 int mask, tid;
2394
2395 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2396 MVPP2_PE_LAST_FREE_TID);
2397 if (tid < 0)
2398 return tid;
2399
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002400 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002401 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
2402 pe.index = tid;
2403
2404 switch (l3_cast) {
2405 case MVPP2_PRS_L3_MULTI_CAST:
2406 mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV4_MC,
2407 MVPP2_PRS_IPV4_MC_MASK);
2408 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
2409 MVPP2_PRS_RI_L3_ADDR_MASK);
2410 break;
2411 case MVPP2_PRS_L3_BROAD_CAST:
2412 mask = MVPP2_PRS_IPV4_BC_MASK;
2413 mvpp2_prs_tcam_data_byte_set(&pe, 0, mask, mask);
2414 mvpp2_prs_tcam_data_byte_set(&pe, 1, mask, mask);
2415 mvpp2_prs_tcam_data_byte_set(&pe, 2, mask, mask);
2416 mvpp2_prs_tcam_data_byte_set(&pe, 3, mask, mask);
2417 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_BCAST,
2418 MVPP2_PRS_RI_L3_ADDR_MASK);
2419 break;
2420 default:
2421 return -EINVAL;
2422 }
2423
2424 /* Finished: go to flowid generation */
2425 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2426 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2427
2428 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
2429 MVPP2_PRS_IPV4_DIP_AI_BIT);
2430 /* Unmask all ports */
2431 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2432
2433 /* Update shadow table and hw entry */
2434 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2435 mvpp2_prs_hw_write(priv, &pe);
2436
2437 return 0;
2438}
2439
2440/* Set entries for protocols over IPv6 */
2441static int mvpp2_prs_ip6_proto(struct mvpp2 *priv, unsigned short proto,
2442 unsigned int ri, unsigned int ri_mask)
2443{
2444 struct mvpp2_prs_entry pe;
2445 int tid;
2446
2447 if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
2448 (proto != IPPROTO_ICMPV6) && (proto != IPPROTO_IPIP))
2449 return -EINVAL;
2450
2451 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2452 MVPP2_PE_LAST_FREE_TID);
2453 if (tid < 0)
2454 return tid;
2455
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002456 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002457 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2458 pe.index = tid;
2459
2460 /* Finished: go to flowid generation */
2461 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2462 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2463 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
2464 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
2465 sizeof(struct ipv6hdr) - 6,
2466 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2467
2468 mvpp2_prs_tcam_data_byte_set(&pe, 0, proto, MVPP2_PRS_TCAM_PROTO_MASK);
2469 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
2470 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2471 /* Unmask all ports */
2472 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2473
2474 /* Write HW */
2475 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
2476 mvpp2_prs_hw_write(priv, &pe);
2477
2478 return 0;
2479}
2480
2481/* IPv6 L3 multicast entry */
2482static int mvpp2_prs_ip6_cast(struct mvpp2 *priv, unsigned short l3_cast)
2483{
2484 struct mvpp2_prs_entry pe;
2485 int tid;
2486
2487 if (l3_cast != MVPP2_PRS_L3_MULTI_CAST)
2488 return -EINVAL;
2489
2490 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2491 MVPP2_PE_LAST_FREE_TID);
2492 if (tid < 0)
2493 return tid;
2494
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002495 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002496 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2497 pe.index = tid;
2498
2499 /* Finished: go to flowid generation */
2500 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2501 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
2502 MVPP2_PRS_RI_L3_ADDR_MASK);
2503 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
2504 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2505 /* Shift back to IPv6 NH */
2506 mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2507
2508 mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV6_MC,
2509 MVPP2_PRS_IPV6_MC_MASK);
2510 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2511 /* Unmask all ports */
2512 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2513
2514 /* Update shadow table and hw entry */
2515 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
2516 mvpp2_prs_hw_write(priv, &pe);
2517
2518 return 0;
2519}
2520
2521/* Parser per-port initialization */
2522static void mvpp2_prs_hw_port_init(struct mvpp2 *priv, int port, int lu_first,
2523 int lu_max, int offset)
2524{
2525 u32 val;
2526
2527 /* Set lookup ID */
2528 val = mvpp2_read(priv, MVPP2_PRS_INIT_LOOKUP_REG);
2529 val &= ~MVPP2_PRS_PORT_LU_MASK(port);
2530 val |= MVPP2_PRS_PORT_LU_VAL(port, lu_first);
2531 mvpp2_write(priv, MVPP2_PRS_INIT_LOOKUP_REG, val);
2532
2533 /* Set maximum number of loops for packet received from port */
2534 val = mvpp2_read(priv, MVPP2_PRS_MAX_LOOP_REG(port));
2535 val &= ~MVPP2_PRS_MAX_LOOP_MASK(port);
2536 val |= MVPP2_PRS_MAX_LOOP_VAL(port, lu_max);
2537 mvpp2_write(priv, MVPP2_PRS_MAX_LOOP_REG(port), val);
2538
2539 /* Set initial offset for packet header extraction for the first
2540 * searching loop
2541 */
2542 val = mvpp2_read(priv, MVPP2_PRS_INIT_OFFS_REG(port));
2543 val &= ~MVPP2_PRS_INIT_OFF_MASK(port);
2544 val |= MVPP2_PRS_INIT_OFF_VAL(port, offset);
2545 mvpp2_write(priv, MVPP2_PRS_INIT_OFFS_REG(port), val);
2546}
2547
2548/* Default flow entries initialization for all ports */
2549static void mvpp2_prs_def_flow_init(struct mvpp2 *priv)
2550{
2551 struct mvpp2_prs_entry pe;
2552 int port;
2553
2554 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002555 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002556 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2557 pe.index = MVPP2_PE_FIRST_DEFAULT_FLOW - port;
2558
2559 /* Mask all ports */
2560 mvpp2_prs_tcam_port_map_set(&pe, 0);
2561
2562 /* Set flow ID*/
2563 mvpp2_prs_sram_ai_update(&pe, port, MVPP2_PRS_FLOW_ID_MASK);
2564 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
2565
2566 /* Update shadow table and hw entry */
2567 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_FLOWS);
2568 mvpp2_prs_hw_write(priv, &pe);
2569 }
2570}
2571
2572/* Set default entry for Marvell Header field */
2573static void mvpp2_prs_mh_init(struct mvpp2 *priv)
2574{
2575 struct mvpp2_prs_entry pe;
2576
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002577 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002578
2579 pe.index = MVPP2_PE_MH_DEFAULT;
2580 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MH);
2581 mvpp2_prs_sram_shift_set(&pe, MVPP2_MH_SIZE,
2582 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2583 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_MAC);
2584
2585 /* Unmask all ports */
2586 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2587
2588 /* Update shadow table and hw entry */
2589 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MH);
2590 mvpp2_prs_hw_write(priv, &pe);
2591}
2592
2593/* Set default entires (place holder) for promiscuous, non-promiscuous and
2594 * multicast MAC addresses
2595 */
2596static void mvpp2_prs_mac_init(struct mvpp2 *priv)
2597{
2598 struct mvpp2_prs_entry pe;
2599
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002600 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002601
2602 /* Non-promiscuous mode for all ports - DROP unknown packets */
2603 pe.index = MVPP2_PE_MAC_NON_PROMISCUOUS;
2604 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
2605
2606 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
2607 MVPP2_PRS_RI_DROP_MASK);
2608 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2609 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2610
2611 /* Unmask all ports */
2612 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2613
2614 /* Update shadow table and hw entry */
2615 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2616 mvpp2_prs_hw_write(priv, &pe);
2617
2618 /* place holders only - no ports */
2619 mvpp2_prs_mac_drop_all_set(priv, 0, false);
2620 mvpp2_prs_mac_promisc_set(priv, 0, false);
Antoine Tenart20746d72017-10-24 11:41:27 +02002621 mvpp2_prs_mac_multi_set(priv, 0, MVPP2_PE_MAC_MC_ALL, false);
2622 mvpp2_prs_mac_multi_set(priv, 0, MVPP2_PE_MAC_MC_IP6, false);
Marcin Wojtas3f518502014-07-10 16:52:13 -03002623}
2624
2625/* Set default entries for various types of dsa packets */
2626static void mvpp2_prs_dsa_init(struct mvpp2 *priv)
2627{
2628 struct mvpp2_prs_entry pe;
2629
2630 /* None tagged EDSA entry - place holder */
2631 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
2632 MVPP2_PRS_EDSA);
2633
2634 /* Tagged EDSA entry - place holder */
2635 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
2636
2637 /* None tagged DSA entry - place holder */
2638 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
2639 MVPP2_PRS_DSA);
2640
2641 /* Tagged DSA entry - place holder */
2642 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
2643
2644 /* None tagged EDSA ethertype entry - place holder*/
2645 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
2646 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
2647
2648 /* Tagged EDSA ethertype entry - place holder*/
2649 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
2650 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
2651
2652 /* None tagged DSA ethertype entry */
2653 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
2654 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
2655
2656 /* Tagged DSA ethertype entry */
2657 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
2658 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
2659
2660 /* Set default entry, in case DSA or EDSA tag not found */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002661 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002662 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
2663 pe.index = MVPP2_PE_DSA_DEFAULT;
2664 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2665
2666 /* Shift 0 bytes */
2667 mvpp2_prs_sram_shift_set(&pe, 0, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2668 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2669
2670 /* Clear all sram ai bits for next iteration */
2671 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2672
2673 /* Unmask all ports */
2674 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2675
2676 mvpp2_prs_hw_write(priv, &pe);
2677}
2678
2679/* Match basic ethertypes */
2680static int mvpp2_prs_etype_init(struct mvpp2 *priv)
2681{
2682 struct mvpp2_prs_entry pe;
2683 int tid;
2684
2685 /* Ethertype: PPPoE */
2686 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2687 MVPP2_PE_LAST_FREE_TID);
2688 if (tid < 0)
2689 return tid;
2690
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002691 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002692 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2693 pe.index = tid;
2694
2695 mvpp2_prs_match_etype(&pe, 0, ETH_P_PPP_SES);
2696
2697 mvpp2_prs_sram_shift_set(&pe, MVPP2_PPPOE_HDR_SIZE,
2698 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2699 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2700 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_PPPOE_MASK,
2701 MVPP2_PRS_RI_PPPOE_MASK);
2702
2703 /* Update shadow table and hw entry */
2704 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2705 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2706 priv->prs_shadow[pe.index].finish = false;
2707 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_PPPOE_MASK,
2708 MVPP2_PRS_RI_PPPOE_MASK);
2709 mvpp2_prs_hw_write(priv, &pe);
2710
2711 /* Ethertype: ARP */
2712 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2713 MVPP2_PE_LAST_FREE_TID);
2714 if (tid < 0)
2715 return tid;
2716
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002717 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002718 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2719 pe.index = tid;
2720
2721 mvpp2_prs_match_etype(&pe, 0, ETH_P_ARP);
2722
2723 /* Generate flow in the next iteration*/
2724 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2725 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2726 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_ARP,
2727 MVPP2_PRS_RI_L3_PROTO_MASK);
2728 /* Set L3 offset */
2729 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2730 MVPP2_ETH_TYPE_LEN,
2731 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2732
2733 /* Update shadow table and hw entry */
2734 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2735 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2736 priv->prs_shadow[pe.index].finish = true;
2737 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_ARP,
2738 MVPP2_PRS_RI_L3_PROTO_MASK);
2739 mvpp2_prs_hw_write(priv, &pe);
2740
2741 /* Ethertype: LBTD */
2742 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2743 MVPP2_PE_LAST_FREE_TID);
2744 if (tid < 0)
2745 return tid;
2746
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002747 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002748 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2749 pe.index = tid;
2750
2751 mvpp2_prs_match_etype(&pe, 0, MVPP2_IP_LBDT_TYPE);
2752
2753 /* Generate flow in the next iteration*/
2754 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2755 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2756 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2757 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2758 MVPP2_PRS_RI_CPU_CODE_MASK |
2759 MVPP2_PRS_RI_UDF3_MASK);
2760 /* Set L3 offset */
2761 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2762 MVPP2_ETH_TYPE_LEN,
2763 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2764
2765 /* Update shadow table and hw entry */
2766 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2767 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2768 priv->prs_shadow[pe.index].finish = true;
2769 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2770 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2771 MVPP2_PRS_RI_CPU_CODE_MASK |
2772 MVPP2_PRS_RI_UDF3_MASK);
2773 mvpp2_prs_hw_write(priv, &pe);
2774
2775 /* Ethertype: IPv4 without options */
2776 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2777 MVPP2_PE_LAST_FREE_TID);
2778 if (tid < 0)
2779 return tid;
2780
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002781 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002782 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2783 pe.index = tid;
2784
2785 mvpp2_prs_match_etype(&pe, 0, ETH_P_IP);
2786 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2787 MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
2788 MVPP2_PRS_IPV4_HEAD_MASK |
2789 MVPP2_PRS_IPV4_IHL_MASK);
2790
2791 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2792 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
2793 MVPP2_PRS_RI_L3_PROTO_MASK);
2794 /* Skip eth_type + 4 bytes of IP header */
2795 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
2796 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2797 /* Set L3 offset */
2798 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2799 MVPP2_ETH_TYPE_LEN,
2800 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2801
2802 /* Update shadow table and hw entry */
2803 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2804 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2805 priv->prs_shadow[pe.index].finish = false;
2806 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4,
2807 MVPP2_PRS_RI_L3_PROTO_MASK);
2808 mvpp2_prs_hw_write(priv, &pe);
2809
2810 /* Ethertype: IPv4 with options */
2811 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2812 MVPP2_PE_LAST_FREE_TID);
2813 if (tid < 0)
2814 return tid;
2815
2816 pe.index = tid;
2817
2818 /* Clear tcam data before updating */
2819 pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(MVPP2_ETH_TYPE_LEN)] = 0x0;
2820 pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(MVPP2_ETH_TYPE_LEN)] = 0x0;
2821
2822 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2823 MVPP2_PRS_IPV4_HEAD,
2824 MVPP2_PRS_IPV4_HEAD_MASK);
2825
2826 /* Clear ri before updating */
2827 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2828 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2829 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
2830 MVPP2_PRS_RI_L3_PROTO_MASK);
2831
2832 /* Update shadow table and hw entry */
2833 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2834 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2835 priv->prs_shadow[pe.index].finish = false;
2836 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4_OPT,
2837 MVPP2_PRS_RI_L3_PROTO_MASK);
2838 mvpp2_prs_hw_write(priv, &pe);
2839
2840 /* Ethertype: IPv6 without options */
2841 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2842 MVPP2_PE_LAST_FREE_TID);
2843 if (tid < 0)
2844 return tid;
2845
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002846 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002847 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2848 pe.index = tid;
2849
2850 mvpp2_prs_match_etype(&pe, 0, ETH_P_IPV6);
2851
2852 /* Skip DIP of IPV6 header */
2853 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 8 +
2854 MVPP2_MAX_L3_ADDR_SIZE,
2855 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2856 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2857 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
2858 MVPP2_PRS_RI_L3_PROTO_MASK);
2859 /* Set L3 offset */
2860 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2861 MVPP2_ETH_TYPE_LEN,
2862 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2863
2864 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2865 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2866 priv->prs_shadow[pe.index].finish = false;
2867 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP6,
2868 MVPP2_PRS_RI_L3_PROTO_MASK);
2869 mvpp2_prs_hw_write(priv, &pe);
2870
2871 /* Default entry for MVPP2_PRS_LU_L2 - Unknown ethtype */
2872 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2873 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2874 pe.index = MVPP2_PE_ETH_TYPE_UN;
2875
2876 /* Unmask all ports */
2877 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2878
2879 /* Generate flow in the next iteration*/
2880 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2881 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2882 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
2883 MVPP2_PRS_RI_L3_PROTO_MASK);
2884 /* Set L3 offset even it's unknown L3 */
2885 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2886 MVPP2_ETH_TYPE_LEN,
2887 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2888
2889 /* Update shadow table and hw entry */
2890 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2891 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2892 priv->prs_shadow[pe.index].finish = true;
2893 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_UN,
2894 MVPP2_PRS_RI_L3_PROTO_MASK);
2895 mvpp2_prs_hw_write(priv, &pe);
2896
2897 return 0;
2898}
2899
2900/* Configure vlan entries and detect up to 2 successive VLAN tags.
2901 * Possible options:
2902 * 0x8100, 0x88A8
2903 * 0x8100, 0x8100
2904 * 0x8100
2905 * 0x88A8
2906 */
2907static int mvpp2_prs_vlan_init(struct platform_device *pdev, struct mvpp2 *priv)
2908{
2909 struct mvpp2_prs_entry pe;
2910 int err;
2911
2912 priv->prs_double_vlans = devm_kcalloc(&pdev->dev, sizeof(bool),
2913 MVPP2_PRS_DBL_VLANS_MAX,
2914 GFP_KERNEL);
2915 if (!priv->prs_double_vlans)
2916 return -ENOMEM;
2917
2918 /* Double VLAN: 0x8100, 0x88A8 */
2919 err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021AD,
2920 MVPP2_PRS_PORT_MASK);
2921 if (err)
2922 return err;
2923
2924 /* Double VLAN: 0x8100, 0x8100 */
2925 err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021Q,
2926 MVPP2_PRS_PORT_MASK);
2927 if (err)
2928 return err;
2929
2930 /* Single VLAN: 0x88a8 */
2931 err = mvpp2_prs_vlan_add(priv, ETH_P_8021AD, MVPP2_PRS_SINGLE_VLAN_AI,
2932 MVPP2_PRS_PORT_MASK);
2933 if (err)
2934 return err;
2935
2936 /* Single VLAN: 0x8100 */
2937 err = mvpp2_prs_vlan_add(priv, ETH_P_8021Q, MVPP2_PRS_SINGLE_VLAN_AI,
2938 MVPP2_PRS_PORT_MASK);
2939 if (err)
2940 return err;
2941
2942 /* Set default double vlan entry */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002943 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002944 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2945 pe.index = MVPP2_PE_VLAN_DBL;
2946
2947 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2948 /* Clear ai for next iterations */
2949 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2950 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_DOUBLE,
2951 MVPP2_PRS_RI_VLAN_MASK);
2952
2953 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_DBL_VLAN_AI_BIT,
2954 MVPP2_PRS_DBL_VLAN_AI_BIT);
2955 /* Unmask all ports */
2956 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2957
2958 /* Update shadow table and hw entry */
2959 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
2960 mvpp2_prs_hw_write(priv, &pe);
2961
2962 /* Set default vlan none entry */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002963 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002964 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2965 pe.index = MVPP2_PE_VLAN_NONE;
2966
2967 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2968 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
2969 MVPP2_PRS_RI_VLAN_MASK);
2970
2971 /* Unmask all ports */
2972 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2973
2974 /* Update shadow table and hw entry */
2975 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
2976 mvpp2_prs_hw_write(priv, &pe);
2977
2978 return 0;
2979}
2980
2981/* Set entries for PPPoE ethertype */
2982static int mvpp2_prs_pppoe_init(struct mvpp2 *priv)
2983{
2984 struct mvpp2_prs_entry pe;
2985 int tid;
2986
2987 /* IPv4 over PPPoE with options */
2988 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2989 MVPP2_PE_LAST_FREE_TID);
2990 if (tid < 0)
2991 return tid;
2992
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002993 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002994 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2995 pe.index = tid;
2996
2997 mvpp2_prs_match_etype(&pe, 0, PPP_IP);
2998
2999 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
3000 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
3001 MVPP2_PRS_RI_L3_PROTO_MASK);
3002 /* Skip eth_type + 4 bytes of IP header */
3003 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
3004 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3005 /* Set L3 offset */
3006 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
3007 MVPP2_ETH_TYPE_LEN,
3008 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3009
3010 /* Update shadow table and hw entry */
3011 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3012 mvpp2_prs_hw_write(priv, &pe);
3013
3014 /* IPv4 over PPPoE without options */
3015 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3016 MVPP2_PE_LAST_FREE_TID);
3017 if (tid < 0)
3018 return tid;
3019
3020 pe.index = tid;
3021
3022 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
3023 MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
3024 MVPP2_PRS_IPV4_HEAD_MASK |
3025 MVPP2_PRS_IPV4_IHL_MASK);
3026
3027 /* Clear ri before updating */
3028 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
3029 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
3030 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
3031 MVPP2_PRS_RI_L3_PROTO_MASK);
3032
3033 /* Update shadow table and hw entry */
3034 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3035 mvpp2_prs_hw_write(priv, &pe);
3036
3037 /* IPv6 over PPPoE */
3038 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3039 MVPP2_PE_LAST_FREE_TID);
3040 if (tid < 0)
3041 return tid;
3042
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003043 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003044 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
3045 pe.index = tid;
3046
3047 mvpp2_prs_match_etype(&pe, 0, PPP_IPV6);
3048
3049 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
3050 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
3051 MVPP2_PRS_RI_L3_PROTO_MASK);
3052 /* Skip eth_type + 4 bytes of IPv6 header */
3053 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
3054 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3055 /* Set L3 offset */
3056 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
3057 MVPP2_ETH_TYPE_LEN,
3058 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3059
3060 /* Update shadow table and hw entry */
3061 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3062 mvpp2_prs_hw_write(priv, &pe);
3063
3064 /* Non-IP over PPPoE */
3065 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3066 MVPP2_PE_LAST_FREE_TID);
3067 if (tid < 0)
3068 return tid;
3069
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003070 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003071 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
3072 pe.index = tid;
3073
3074 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
3075 MVPP2_PRS_RI_L3_PROTO_MASK);
3076
3077 /* Finished: go to flowid generation */
3078 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3079 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3080 /* Set L3 offset even if it's unknown L3 */
3081 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
3082 MVPP2_ETH_TYPE_LEN,
3083 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3084
3085 /* Update shadow table and hw entry */
3086 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3087 mvpp2_prs_hw_write(priv, &pe);
3088
3089 return 0;
3090}
3091
3092/* Initialize entries for IPv4 */
3093static int mvpp2_prs_ip4_init(struct mvpp2 *priv)
3094{
3095 struct mvpp2_prs_entry pe;
3096 int err;
3097
3098 /* Set entries for TCP, UDP and IGMP over IPv4 */
3099 err = mvpp2_prs_ip4_proto(priv, IPPROTO_TCP, MVPP2_PRS_RI_L4_TCP,
3100 MVPP2_PRS_RI_L4_PROTO_MASK);
3101 if (err)
3102 return err;
3103
3104 err = mvpp2_prs_ip4_proto(priv, IPPROTO_UDP, MVPP2_PRS_RI_L4_UDP,
3105 MVPP2_PRS_RI_L4_PROTO_MASK);
3106 if (err)
3107 return err;
3108
3109 err = mvpp2_prs_ip4_proto(priv, IPPROTO_IGMP,
3110 MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
3111 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
3112 MVPP2_PRS_RI_CPU_CODE_MASK |
3113 MVPP2_PRS_RI_UDF3_MASK);
3114 if (err)
3115 return err;
3116
3117 /* IPv4 Broadcast */
3118 err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_BROAD_CAST);
3119 if (err)
3120 return err;
3121
3122 /* IPv4 Multicast */
3123 err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
3124 if (err)
3125 return err;
3126
3127 /* Default IPv4 entry for unknown protocols */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003128 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003129 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
3130 pe.index = MVPP2_PE_IP4_PROTO_UN;
3131
3132 /* Set next lu to IPv4 */
3133 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
3134 mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3135 /* Set L4 offset */
3136 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
3137 sizeof(struct iphdr) - 4,
3138 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3139 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
3140 MVPP2_PRS_IPV4_DIP_AI_BIT);
3141 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3142 MVPP2_PRS_RI_L4_PROTO_MASK);
3143
3144 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_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_IP4);
3150 mvpp2_prs_hw_write(priv, &pe);
3151
3152 /* Default IPv4 entry for unicast address */
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_IP4);
3155 pe.index = MVPP2_PE_IP4_ADDR_UN;
3156
3157 /* Finished: go to flowid generation */
3158 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3159 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3160 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
3161 MVPP2_PRS_RI_L3_ADDR_MASK);
3162
3163 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
3164 MVPP2_PRS_IPV4_DIP_AI_BIT);
3165 /* Unmask all ports */
3166 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3167
3168 /* Update shadow table and hw entry */
3169 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3170 mvpp2_prs_hw_write(priv, &pe);
3171
3172 return 0;
3173}
3174
3175/* Initialize entries for IPv6 */
3176static int mvpp2_prs_ip6_init(struct mvpp2 *priv)
3177{
3178 struct mvpp2_prs_entry pe;
3179 int tid, err;
3180
3181 /* Set entries for TCP, UDP and ICMP over IPv6 */
3182 err = mvpp2_prs_ip6_proto(priv, IPPROTO_TCP,
3183 MVPP2_PRS_RI_L4_TCP,
3184 MVPP2_PRS_RI_L4_PROTO_MASK);
3185 if (err)
3186 return err;
3187
3188 err = mvpp2_prs_ip6_proto(priv, IPPROTO_UDP,
3189 MVPP2_PRS_RI_L4_UDP,
3190 MVPP2_PRS_RI_L4_PROTO_MASK);
3191 if (err)
3192 return err;
3193
3194 err = mvpp2_prs_ip6_proto(priv, IPPROTO_ICMPV6,
3195 MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
3196 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
3197 MVPP2_PRS_RI_CPU_CODE_MASK |
3198 MVPP2_PRS_RI_UDF3_MASK);
3199 if (err)
3200 return err;
3201
3202 /* IPv4 is the last header. This is similar case as 6-TCP or 17-UDP */
3203 /* Result Info: UDF7=1, DS lite */
3204 err = mvpp2_prs_ip6_proto(priv, IPPROTO_IPIP,
3205 MVPP2_PRS_RI_UDF7_IP6_LITE,
3206 MVPP2_PRS_RI_UDF7_MASK);
3207 if (err)
3208 return err;
3209
3210 /* IPv6 multicast */
3211 err = mvpp2_prs_ip6_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
3212 if (err)
3213 return err;
3214
3215 /* Entry for checking hop limit */
3216 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3217 MVPP2_PE_LAST_FREE_TID);
3218 if (tid < 0)
3219 return tid;
3220
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003221 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003222 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3223 pe.index = tid;
3224
3225 /* Finished: go to flowid generation */
3226 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3227 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3228 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN |
3229 MVPP2_PRS_RI_DROP_MASK,
3230 MVPP2_PRS_RI_L3_PROTO_MASK |
3231 MVPP2_PRS_RI_DROP_MASK);
3232
3233 mvpp2_prs_tcam_data_byte_set(&pe, 1, 0x00, MVPP2_PRS_IPV6_HOP_MASK);
3234 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3235 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3236
3237 /* Update shadow table and hw entry */
3238 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3239 mvpp2_prs_hw_write(priv, &pe);
3240
3241 /* Default IPv6 entry for unknown protocols */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003242 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003243 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3244 pe.index = MVPP2_PE_IP6_PROTO_UN;
3245
3246 /* Finished: go to flowid generation */
3247 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3248 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3249 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3250 MVPP2_PRS_RI_L4_PROTO_MASK);
3251 /* Set L4 offset relatively to our current place */
3252 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
3253 sizeof(struct ipv6hdr) - 4,
3254 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3255
3256 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3257 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3258 /* Unmask all ports */
3259 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3260
3261 /* Update shadow table and hw entry */
3262 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3263 mvpp2_prs_hw_write(priv, &pe);
3264
3265 /* Default IPv6 entry for unknown ext protocols */
3266 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3267 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3268 pe.index = MVPP2_PE_IP6_EXT_PROTO_UN;
3269
3270 /* Finished: go to flowid generation */
3271 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3272 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3273 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3274 MVPP2_PRS_RI_L4_PROTO_MASK);
3275
3276 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_EXT_AI_BIT,
3277 MVPP2_PRS_IPV6_EXT_AI_BIT);
3278 /* Unmask all ports */
3279 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3280
3281 /* Update shadow table and hw entry */
3282 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3283 mvpp2_prs_hw_write(priv, &pe);
3284
3285 /* Default IPv6 entry for unicast address */
3286 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3287 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3288 pe.index = MVPP2_PE_IP6_ADDR_UN;
3289
3290 /* Finished: go to IPv6 again */
3291 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
3292 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
3293 MVPP2_PRS_RI_L3_ADDR_MASK);
3294 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3295 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3296 /* Shift back to IPV6 NH */
3297 mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3298
3299 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3300 /* Unmask all ports */
3301 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3302
3303 /* Update shadow table and hw entry */
3304 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
3305 mvpp2_prs_hw_write(priv, &pe);
3306
3307 return 0;
3308}
3309
3310/* Parser default initialization */
3311static int mvpp2_prs_default_init(struct platform_device *pdev,
3312 struct mvpp2 *priv)
3313{
3314 int err, index, i;
3315
3316 /* Enable tcam table */
3317 mvpp2_write(priv, MVPP2_PRS_TCAM_CTRL_REG, MVPP2_PRS_TCAM_EN_MASK);
3318
3319 /* Clear all tcam and sram entries */
3320 for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++) {
3321 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
3322 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
3323 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), 0);
3324
3325 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, index);
3326 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
3327 mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), 0);
3328 }
3329
3330 /* Invalidate all tcam entries */
3331 for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++)
3332 mvpp2_prs_hw_inv(priv, index);
3333
3334 priv->prs_shadow = devm_kcalloc(&pdev->dev, MVPP2_PRS_TCAM_SRAM_SIZE,
Markus Elfring37df25e2017-04-17 09:12:34 +02003335 sizeof(*priv->prs_shadow),
Marcin Wojtas3f518502014-07-10 16:52:13 -03003336 GFP_KERNEL);
3337 if (!priv->prs_shadow)
3338 return -ENOMEM;
3339
3340 /* Always start from lookup = 0 */
3341 for (index = 0; index < MVPP2_MAX_PORTS; index++)
3342 mvpp2_prs_hw_port_init(priv, index, MVPP2_PRS_LU_MH,
3343 MVPP2_PRS_PORT_LU_MAX, 0);
3344
3345 mvpp2_prs_def_flow_init(priv);
3346
3347 mvpp2_prs_mh_init(priv);
3348
3349 mvpp2_prs_mac_init(priv);
3350
3351 mvpp2_prs_dsa_init(priv);
3352
3353 err = mvpp2_prs_etype_init(priv);
3354 if (err)
3355 return err;
3356
3357 err = mvpp2_prs_vlan_init(pdev, priv);
3358 if (err)
3359 return err;
3360
3361 err = mvpp2_prs_pppoe_init(priv);
3362 if (err)
3363 return err;
3364
3365 err = mvpp2_prs_ip6_init(priv);
3366 if (err)
3367 return err;
3368
3369 err = mvpp2_prs_ip4_init(priv);
3370 if (err)
3371 return err;
3372
3373 return 0;
3374}
3375
3376/* Compare MAC DA with tcam entry data */
3377static bool mvpp2_prs_mac_range_equals(struct mvpp2_prs_entry *pe,
3378 const u8 *da, unsigned char *mask)
3379{
3380 unsigned char tcam_byte, tcam_mask;
3381 int index;
3382
3383 for (index = 0; index < ETH_ALEN; index++) {
3384 mvpp2_prs_tcam_data_byte_get(pe, index, &tcam_byte, &tcam_mask);
3385 if (tcam_mask != mask[index])
3386 return false;
3387
3388 if ((tcam_mask & tcam_byte) != (da[index] & mask[index]))
3389 return false;
3390 }
3391
3392 return true;
3393}
3394
3395/* Find tcam entry with matched pair <MAC DA, port> */
3396static struct mvpp2_prs_entry *
3397mvpp2_prs_mac_da_range_find(struct mvpp2 *priv, int pmap, const u8 *da,
3398 unsigned char *mask, int udf_type)
3399{
3400 struct mvpp2_prs_entry *pe;
3401 int tid;
3402
Antoine Tenart239dd4e2017-10-24 11:41:28 +02003403 pe = kzalloc(sizeof(*pe), GFP_ATOMIC);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003404 if (!pe)
3405 return NULL;
3406 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
3407
3408 /* Go through the all entires with MVPP2_PRS_LU_MAC */
3409 for (tid = MVPP2_PE_FIRST_FREE_TID;
3410 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
3411 unsigned int entry_pmap;
3412
3413 if (!priv->prs_shadow[tid].valid ||
3414 (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
3415 (priv->prs_shadow[tid].udf != udf_type))
3416 continue;
3417
3418 pe->index = tid;
3419 mvpp2_prs_hw_read(priv, pe);
3420 entry_pmap = mvpp2_prs_tcam_port_map_get(pe);
3421
3422 if (mvpp2_prs_mac_range_equals(pe, da, mask) &&
3423 entry_pmap == pmap)
3424 return pe;
3425 }
3426 kfree(pe);
3427
3428 return NULL;
3429}
3430
3431/* Update parser's mac da entry */
3432static int mvpp2_prs_mac_da_accept(struct mvpp2 *priv, int port,
3433 const u8 *da, bool add)
3434{
3435 struct mvpp2_prs_entry *pe;
3436 unsigned int pmap, len, ri;
3437 unsigned char mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3438 int tid;
3439
3440 /* Scan TCAM and see if entry with this <MAC DA, port> already exist */
3441 pe = mvpp2_prs_mac_da_range_find(priv, (1 << port), da, mask,
3442 MVPP2_PRS_UDF_MAC_DEF);
3443
3444 /* No such entry */
3445 if (!pe) {
3446 if (!add)
3447 return 0;
3448
3449 /* Create new TCAM entry */
3450 /* Find first range mac entry*/
3451 for (tid = MVPP2_PE_FIRST_FREE_TID;
3452 tid <= MVPP2_PE_LAST_FREE_TID; tid++)
3453 if (priv->prs_shadow[tid].valid &&
3454 (priv->prs_shadow[tid].lu == MVPP2_PRS_LU_MAC) &&
3455 (priv->prs_shadow[tid].udf ==
3456 MVPP2_PRS_UDF_MAC_RANGE))
3457 break;
3458
3459 /* Go through the all entries from first to last */
3460 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3461 tid - 1);
3462 if (tid < 0)
3463 return tid;
3464
Antoine Tenart239dd4e2017-10-24 11:41:28 +02003465 pe = kzalloc(sizeof(*pe), GFP_ATOMIC);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003466 if (!pe)
Amitoj Kaur Chawlac2bb7bc2016-02-04 19:25:26 +05303467 return -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003468 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
3469 pe->index = tid;
3470
3471 /* Mask all ports */
3472 mvpp2_prs_tcam_port_map_set(pe, 0);
3473 }
3474
3475 /* Update port mask */
3476 mvpp2_prs_tcam_port_set(pe, port, add);
3477
3478 /* Invalidate the entry if no ports are left enabled */
3479 pmap = mvpp2_prs_tcam_port_map_get(pe);
3480 if (pmap == 0) {
3481 if (add) {
3482 kfree(pe);
Amitoj Kaur Chawlac2bb7bc2016-02-04 19:25:26 +05303483 return -EINVAL;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003484 }
3485 mvpp2_prs_hw_inv(priv, pe->index);
3486 priv->prs_shadow[pe->index].valid = false;
3487 kfree(pe);
3488 return 0;
3489 }
3490
3491 /* Continue - set next lookup */
3492 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_DSA);
3493
3494 /* Set match on DA */
3495 len = ETH_ALEN;
3496 while (len--)
3497 mvpp2_prs_tcam_data_byte_set(pe, len, da[len], 0xff);
3498
3499 /* Set result info bits */
3500 if (is_broadcast_ether_addr(da))
3501 ri = MVPP2_PRS_RI_L2_BCAST;
3502 else if (is_multicast_ether_addr(da))
3503 ri = MVPP2_PRS_RI_L2_MCAST;
3504 else
3505 ri = MVPP2_PRS_RI_L2_UCAST | MVPP2_PRS_RI_MAC_ME_MASK;
3506
3507 mvpp2_prs_sram_ri_update(pe, ri, MVPP2_PRS_RI_L2_CAST_MASK |
3508 MVPP2_PRS_RI_MAC_ME_MASK);
3509 mvpp2_prs_shadow_ri_set(priv, pe->index, ri, MVPP2_PRS_RI_L2_CAST_MASK |
3510 MVPP2_PRS_RI_MAC_ME_MASK);
3511
3512 /* Shift to ethertype */
3513 mvpp2_prs_sram_shift_set(pe, 2 * ETH_ALEN,
3514 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3515
3516 /* Update shadow table and hw entry */
3517 priv->prs_shadow[pe->index].udf = MVPP2_PRS_UDF_MAC_DEF;
3518 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_MAC);
3519 mvpp2_prs_hw_write(priv, pe);
3520
3521 kfree(pe);
3522
3523 return 0;
3524}
3525
3526static int mvpp2_prs_update_mac_da(struct net_device *dev, const u8 *da)
3527{
3528 struct mvpp2_port *port = netdev_priv(dev);
3529 int err;
3530
3531 /* Remove old parser entry */
3532 err = mvpp2_prs_mac_da_accept(port->priv, port->id, dev->dev_addr,
3533 false);
3534 if (err)
3535 return err;
3536
3537 /* Add new parser entry */
3538 err = mvpp2_prs_mac_da_accept(port->priv, port->id, da, true);
3539 if (err)
3540 return err;
3541
3542 /* Set addr in the device */
3543 ether_addr_copy(dev->dev_addr, da);
3544
3545 return 0;
3546}
3547
3548/* Delete all port's multicast simple (not range) entries */
3549static void mvpp2_prs_mcast_del_all(struct mvpp2 *priv, int port)
3550{
3551 struct mvpp2_prs_entry pe;
3552 int index, tid;
3553
3554 for (tid = MVPP2_PE_FIRST_FREE_TID;
3555 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
3556 unsigned char da[ETH_ALEN], da_mask[ETH_ALEN];
3557
3558 if (!priv->prs_shadow[tid].valid ||
3559 (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
3560 (priv->prs_shadow[tid].udf != MVPP2_PRS_UDF_MAC_DEF))
3561 continue;
3562
3563 /* Only simple mac entries */
3564 pe.index = tid;
3565 mvpp2_prs_hw_read(priv, &pe);
3566
3567 /* Read mac addr from entry */
3568 for (index = 0; index < ETH_ALEN; index++)
3569 mvpp2_prs_tcam_data_byte_get(&pe, index, &da[index],
3570 &da_mask[index]);
3571
3572 if (is_multicast_ether_addr(da) && !is_broadcast_ether_addr(da))
3573 /* Delete this entry */
3574 mvpp2_prs_mac_da_accept(priv, port, da, false);
3575 }
3576}
3577
3578static int mvpp2_prs_tag_mode_set(struct mvpp2 *priv, int port, int type)
3579{
3580 switch (type) {
3581 case MVPP2_TAG_TYPE_EDSA:
3582 /* Add port to EDSA entries */
3583 mvpp2_prs_dsa_tag_set(priv, port, true,
3584 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3585 mvpp2_prs_dsa_tag_set(priv, port, true,
3586 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3587 /* Remove port from DSA entries */
3588 mvpp2_prs_dsa_tag_set(priv, port, false,
3589 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3590 mvpp2_prs_dsa_tag_set(priv, port, false,
3591 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3592 break;
3593
3594 case MVPP2_TAG_TYPE_DSA:
3595 /* Add port to DSA entries */
3596 mvpp2_prs_dsa_tag_set(priv, port, true,
3597 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3598 mvpp2_prs_dsa_tag_set(priv, port, true,
3599 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3600 /* Remove port from EDSA entries */
3601 mvpp2_prs_dsa_tag_set(priv, port, false,
3602 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3603 mvpp2_prs_dsa_tag_set(priv, port, false,
3604 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3605 break;
3606
3607 case MVPP2_TAG_TYPE_MH:
3608 case MVPP2_TAG_TYPE_NONE:
3609 /* Remove port form EDSA and DSA entries */
3610 mvpp2_prs_dsa_tag_set(priv, port, false,
3611 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3612 mvpp2_prs_dsa_tag_set(priv, port, false,
3613 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3614 mvpp2_prs_dsa_tag_set(priv, port, false,
3615 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3616 mvpp2_prs_dsa_tag_set(priv, port, false,
3617 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3618 break;
3619
3620 default:
3621 if ((type < 0) || (type > MVPP2_TAG_TYPE_EDSA))
3622 return -EINVAL;
3623 }
3624
3625 return 0;
3626}
3627
3628/* Set prs flow for the port */
3629static int mvpp2_prs_def_flow(struct mvpp2_port *port)
3630{
3631 struct mvpp2_prs_entry *pe;
3632 int tid;
3633
3634 pe = mvpp2_prs_flow_find(port->priv, port->id);
3635
3636 /* Such entry not exist */
3637 if (!pe) {
3638 /* Go through the all entires from last to first */
3639 tid = mvpp2_prs_tcam_first_free(port->priv,
3640 MVPP2_PE_LAST_FREE_TID,
3641 MVPP2_PE_FIRST_FREE_TID);
3642 if (tid < 0)
3643 return tid;
3644
3645 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
3646 if (!pe)
3647 return -ENOMEM;
3648
3649 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
3650 pe->index = tid;
3651
3652 /* Set flow ID*/
3653 mvpp2_prs_sram_ai_update(pe, port->id, MVPP2_PRS_FLOW_ID_MASK);
3654 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
3655
3656 /* Update shadow table */
3657 mvpp2_prs_shadow_set(port->priv, pe->index, MVPP2_PRS_LU_FLOWS);
3658 }
3659
3660 mvpp2_prs_tcam_port_map_set(pe, (1 << port->id));
3661 mvpp2_prs_hw_write(port->priv, pe);
3662 kfree(pe);
3663
3664 return 0;
3665}
3666
3667/* Classifier configuration routines */
3668
3669/* Update classification flow table registers */
3670static void mvpp2_cls_flow_write(struct mvpp2 *priv,
3671 struct mvpp2_cls_flow_entry *fe)
3672{
3673 mvpp2_write(priv, MVPP2_CLS_FLOW_INDEX_REG, fe->index);
3674 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL0_REG, fe->data[0]);
3675 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL1_REG, fe->data[1]);
3676 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL2_REG, fe->data[2]);
3677}
3678
3679/* Update classification lookup table register */
3680static void mvpp2_cls_lookup_write(struct mvpp2 *priv,
3681 struct mvpp2_cls_lookup_entry *le)
3682{
3683 u32 val;
3684
3685 val = (le->way << MVPP2_CLS_LKP_INDEX_WAY_OFFS) | le->lkpid;
3686 mvpp2_write(priv, MVPP2_CLS_LKP_INDEX_REG, val);
3687 mvpp2_write(priv, MVPP2_CLS_LKP_TBL_REG, le->data);
3688}
3689
3690/* Classifier default initialization */
3691static void mvpp2_cls_init(struct mvpp2 *priv)
3692{
3693 struct mvpp2_cls_lookup_entry le;
3694 struct mvpp2_cls_flow_entry fe;
3695 int index;
3696
3697 /* Enable classifier */
3698 mvpp2_write(priv, MVPP2_CLS_MODE_REG, MVPP2_CLS_MODE_ACTIVE_MASK);
3699
3700 /* Clear classifier flow table */
Arnd Bergmanne8f967c2016-11-24 17:28:12 +01003701 memset(&fe.data, 0, sizeof(fe.data));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003702 for (index = 0; index < MVPP2_CLS_FLOWS_TBL_SIZE; index++) {
3703 fe.index = index;
3704 mvpp2_cls_flow_write(priv, &fe);
3705 }
3706
3707 /* Clear classifier lookup table */
3708 le.data = 0;
3709 for (index = 0; index < MVPP2_CLS_LKP_TBL_SIZE; index++) {
3710 le.lkpid = index;
3711 le.way = 0;
3712 mvpp2_cls_lookup_write(priv, &le);
3713
3714 le.way = 1;
3715 mvpp2_cls_lookup_write(priv, &le);
3716 }
3717}
3718
3719static void mvpp2_cls_port_config(struct mvpp2_port *port)
3720{
3721 struct mvpp2_cls_lookup_entry le;
3722 u32 val;
3723
3724 /* Set way for the port */
3725 val = mvpp2_read(port->priv, MVPP2_CLS_PORT_WAY_REG);
3726 val &= ~MVPP2_CLS_PORT_WAY_MASK(port->id);
3727 mvpp2_write(port->priv, MVPP2_CLS_PORT_WAY_REG, val);
3728
3729 /* Pick the entry to be accessed in lookup ID decoding table
3730 * according to the way and lkpid.
3731 */
3732 le.lkpid = port->id;
3733 le.way = 0;
3734 le.data = 0;
3735
3736 /* Set initial CPU queue for receiving packets */
3737 le.data &= ~MVPP2_CLS_LKP_TBL_RXQ_MASK;
3738 le.data |= port->first_rxq;
3739
3740 /* Disable classification engines */
3741 le.data &= ~MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK;
3742
3743 /* Update lookup ID table entry */
3744 mvpp2_cls_lookup_write(port->priv, &le);
3745}
3746
3747/* Set CPU queue number for oversize packets */
3748static void mvpp2_cls_oversize_rxq_set(struct mvpp2_port *port)
3749{
3750 u32 val;
3751
3752 mvpp2_write(port->priv, MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port->id),
3753 port->first_rxq & MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK);
3754
3755 mvpp2_write(port->priv, MVPP2_CLS_SWFWD_P2HQ_REG(port->id),
3756 (port->first_rxq >> MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS));
3757
3758 val = mvpp2_read(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG);
3759 val |= MVPP2_CLS_SWFWD_PCTRL_MASK(port->id);
3760 mvpp2_write(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG, val);
3761}
3762
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003763static void *mvpp2_frag_alloc(const struct mvpp2_bm_pool *pool)
3764{
3765 if (likely(pool->frag_size <= PAGE_SIZE))
3766 return netdev_alloc_frag(pool->frag_size);
3767 else
3768 return kmalloc(pool->frag_size, GFP_ATOMIC);
3769}
3770
3771static void mvpp2_frag_free(const struct mvpp2_bm_pool *pool, void *data)
3772{
3773 if (likely(pool->frag_size <= PAGE_SIZE))
3774 skb_free_frag(data);
3775 else
3776 kfree(data);
3777}
3778
Marcin Wojtas3f518502014-07-10 16:52:13 -03003779/* Buffer Manager configuration routines */
3780
3781/* Create pool */
3782static int mvpp2_bm_pool_create(struct platform_device *pdev,
3783 struct mvpp2 *priv,
3784 struct mvpp2_bm_pool *bm_pool, int size)
3785{
Marcin Wojtas3f518502014-07-10 16:52:13 -03003786 u32 val;
3787
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003788 /* Number of buffer pointers must be a multiple of 16, as per
3789 * hardware constraints
3790 */
3791 if (!IS_ALIGNED(size, 16))
3792 return -EINVAL;
3793
3794 /* PPv2.1 needs 8 bytes per buffer pointer, PPv2.2 needs 16
3795 * bytes per buffer pointer
3796 */
3797 if (priv->hw_version == MVPP21)
3798 bm_pool->size_bytes = 2 * sizeof(u32) * size;
3799 else
3800 bm_pool->size_bytes = 2 * sizeof(u64) * size;
3801
3802 bm_pool->virt_addr = dma_alloc_coherent(&pdev->dev, bm_pool->size_bytes,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003803 &bm_pool->dma_addr,
Marcin Wojtas3f518502014-07-10 16:52:13 -03003804 GFP_KERNEL);
3805 if (!bm_pool->virt_addr)
3806 return -ENOMEM;
3807
Thomas Petazzonid3158802017-02-21 11:28:13 +01003808 if (!IS_ALIGNED((unsigned long)bm_pool->virt_addr,
3809 MVPP2_BM_POOL_PTR_ALIGN)) {
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003810 dma_free_coherent(&pdev->dev, bm_pool->size_bytes,
3811 bm_pool->virt_addr, bm_pool->dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003812 dev_err(&pdev->dev, "BM pool %d is not %d bytes aligned\n",
3813 bm_pool->id, MVPP2_BM_POOL_PTR_ALIGN);
3814 return -ENOMEM;
3815 }
3816
3817 mvpp2_write(priv, MVPP2_BM_POOL_BASE_REG(bm_pool->id),
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003818 lower_32_bits(bm_pool->dma_addr));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003819 mvpp2_write(priv, MVPP2_BM_POOL_SIZE_REG(bm_pool->id), size);
3820
3821 val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
3822 val |= MVPP2_BM_START_MASK;
3823 mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
3824
3825 bm_pool->type = MVPP2_BM_FREE;
3826 bm_pool->size = size;
3827 bm_pool->pkt_size = 0;
3828 bm_pool->buf_num = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003829
3830 return 0;
3831}
3832
3833/* Set pool buffer size */
3834static void mvpp2_bm_pool_bufsize_set(struct mvpp2 *priv,
3835 struct mvpp2_bm_pool *bm_pool,
3836 int buf_size)
3837{
3838 u32 val;
3839
3840 bm_pool->buf_size = buf_size;
3841
3842 val = ALIGN(buf_size, 1 << MVPP2_POOL_BUF_SIZE_OFFSET);
3843 mvpp2_write(priv, MVPP2_POOL_BUF_SIZE_REG(bm_pool->id), val);
3844}
3845
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003846static void mvpp2_bm_bufs_get_addrs(struct device *dev, struct mvpp2 *priv,
3847 struct mvpp2_bm_pool *bm_pool,
3848 dma_addr_t *dma_addr,
3849 phys_addr_t *phys_addr)
3850{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02003851 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01003852
3853 *dma_addr = mvpp2_percpu_read(priv, cpu,
3854 MVPP2_BM_PHY_ALLOC_REG(bm_pool->id));
3855 *phys_addr = mvpp2_percpu_read(priv, cpu, MVPP2_BM_VIRT_ALLOC_REG);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003856
3857 if (priv->hw_version == MVPP22) {
3858 u32 val;
3859 u32 dma_addr_highbits, phys_addr_highbits;
3860
Thomas Petazzonia7868412017-03-07 16:53:13 +01003861 val = mvpp2_percpu_read(priv, cpu, MVPP22_BM_ADDR_HIGH_ALLOC);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003862 dma_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_PHYS_MASK);
3863 phys_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_VIRT_MASK) >>
3864 MVPP22_BM_ADDR_HIGH_VIRT_SHIFT;
3865
3866 if (sizeof(dma_addr_t) == 8)
3867 *dma_addr |= (u64)dma_addr_highbits << 32;
3868
3869 if (sizeof(phys_addr_t) == 8)
3870 *phys_addr |= (u64)phys_addr_highbits << 32;
3871 }
Thomas Petazzonia704bb52017-06-10 23:18:22 +02003872
3873 put_cpu();
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003874}
3875
Ezequiel Garcia7861f122014-07-21 13:48:14 -03003876/* Free all buffers from the pool */
Marcin Wojtas4229d502015-12-03 15:20:50 +01003877static void mvpp2_bm_bufs_free(struct device *dev, struct mvpp2 *priv,
3878 struct mvpp2_bm_pool *bm_pool)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003879{
3880 int i;
3881
Ezequiel Garcia7861f122014-07-21 13:48:14 -03003882 for (i = 0; i < bm_pool->buf_num; i++) {
Thomas Petazzoni20396132017-03-07 16:53:00 +01003883 dma_addr_t buf_dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003884 phys_addr_t buf_phys_addr;
3885 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003886
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003887 mvpp2_bm_bufs_get_addrs(dev, priv, bm_pool,
3888 &buf_dma_addr, &buf_phys_addr);
Marcin Wojtas4229d502015-12-03 15:20:50 +01003889
Thomas Petazzoni20396132017-03-07 16:53:00 +01003890 dma_unmap_single(dev, buf_dma_addr,
Marcin Wojtas4229d502015-12-03 15:20:50 +01003891 bm_pool->buf_size, DMA_FROM_DEVICE);
3892
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003893 data = (void *)phys_to_virt(buf_phys_addr);
3894 if (!data)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003895 break;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003896
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003897 mvpp2_frag_free(bm_pool, data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003898 }
3899
3900 /* Update BM driver with number of buffers removed from pool */
3901 bm_pool->buf_num -= i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003902}
3903
3904/* Cleanup pool */
3905static int mvpp2_bm_pool_destroy(struct platform_device *pdev,
3906 struct mvpp2 *priv,
3907 struct mvpp2_bm_pool *bm_pool)
3908{
Marcin Wojtas3f518502014-07-10 16:52:13 -03003909 u32 val;
3910
Marcin Wojtas4229d502015-12-03 15:20:50 +01003911 mvpp2_bm_bufs_free(&pdev->dev, priv, bm_pool);
Ezequiel Garciad74c96c2014-07-21 13:48:13 -03003912 if (bm_pool->buf_num) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03003913 WARN(1, "cannot free all buffers in pool %d\n", bm_pool->id);
3914 return 0;
3915 }
3916
3917 val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
3918 val |= MVPP2_BM_STOP_MASK;
3919 mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
3920
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003921 dma_free_coherent(&pdev->dev, bm_pool->size_bytes,
Marcin Wojtas3f518502014-07-10 16:52:13 -03003922 bm_pool->virt_addr,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003923 bm_pool->dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003924 return 0;
3925}
3926
3927static int mvpp2_bm_pools_init(struct platform_device *pdev,
3928 struct mvpp2 *priv)
3929{
3930 int i, err, size;
3931 struct mvpp2_bm_pool *bm_pool;
3932
3933 /* Create all pools with maximum size */
3934 size = MVPP2_BM_POOL_SIZE_MAX;
3935 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
3936 bm_pool = &priv->bm_pools[i];
3937 bm_pool->id = i;
3938 err = mvpp2_bm_pool_create(pdev, priv, bm_pool, size);
3939 if (err)
3940 goto err_unroll_pools;
3941 mvpp2_bm_pool_bufsize_set(priv, bm_pool, 0);
3942 }
3943 return 0;
3944
3945err_unroll_pools:
3946 dev_err(&pdev->dev, "failed to create BM pool %d, size %d\n", i, size);
3947 for (i = i - 1; i >= 0; i--)
3948 mvpp2_bm_pool_destroy(pdev, priv, &priv->bm_pools[i]);
3949 return err;
3950}
3951
3952static int mvpp2_bm_init(struct platform_device *pdev, struct mvpp2 *priv)
3953{
3954 int i, err;
3955
3956 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
3957 /* Mask BM all interrupts */
3958 mvpp2_write(priv, MVPP2_BM_INTR_MASK_REG(i), 0);
3959 /* Clear BM cause register */
3960 mvpp2_write(priv, MVPP2_BM_INTR_CAUSE_REG(i), 0);
3961 }
3962
3963 /* Allocate and initialize BM pools */
3964 priv->bm_pools = devm_kcalloc(&pdev->dev, MVPP2_BM_POOLS_NUM,
Markus Elfring81f915e2017-04-17 09:06:33 +02003965 sizeof(*priv->bm_pools), GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003966 if (!priv->bm_pools)
3967 return -ENOMEM;
3968
3969 err = mvpp2_bm_pools_init(pdev, priv);
3970 if (err < 0)
3971 return err;
3972 return 0;
3973}
3974
3975/* Attach long pool to rxq */
3976static void mvpp2_rxq_long_pool_set(struct mvpp2_port *port,
3977 int lrxq, int long_pool)
3978{
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003979 u32 val, mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003980 int prxq;
3981
3982 /* Get queue physical ID */
3983 prxq = port->rxqs[lrxq]->id;
3984
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003985 if (port->priv->hw_version == MVPP21)
3986 mask = MVPP21_RXQ_POOL_LONG_MASK;
3987 else
3988 mask = MVPP22_RXQ_POOL_LONG_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003989
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003990 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
3991 val &= ~mask;
3992 val |= (long_pool << MVPP2_RXQ_POOL_LONG_OFFS) & mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003993 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
3994}
3995
3996/* Attach short pool to rxq */
3997static void mvpp2_rxq_short_pool_set(struct mvpp2_port *port,
3998 int lrxq, int short_pool)
3999{
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004000 u32 val, mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004001 int prxq;
4002
4003 /* Get queue physical ID */
4004 prxq = port->rxqs[lrxq]->id;
4005
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004006 if (port->priv->hw_version == MVPP21)
4007 mask = MVPP21_RXQ_POOL_SHORT_MASK;
4008 else
4009 mask = MVPP22_RXQ_POOL_SHORT_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004010
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004011 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
4012 val &= ~mask;
4013 val |= (short_pool << MVPP2_RXQ_POOL_SHORT_OFFS) & mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004014 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
4015}
4016
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004017static void *mvpp2_buf_alloc(struct mvpp2_port *port,
4018 struct mvpp2_bm_pool *bm_pool,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004019 dma_addr_t *buf_dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004020 phys_addr_t *buf_phys_addr,
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004021 gfp_t gfp_mask)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004022{
Thomas Petazzoni20396132017-03-07 16:53:00 +01004023 dma_addr_t dma_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004024 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004025
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004026 data = mvpp2_frag_alloc(bm_pool);
4027 if (!data)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004028 return NULL;
4029
Thomas Petazzoni20396132017-03-07 16:53:00 +01004030 dma_addr = dma_map_single(port->dev->dev.parent, data,
4031 MVPP2_RX_BUF_SIZE(bm_pool->pkt_size),
4032 DMA_FROM_DEVICE);
4033 if (unlikely(dma_mapping_error(port->dev->dev.parent, dma_addr))) {
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004034 mvpp2_frag_free(bm_pool, data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004035 return NULL;
4036 }
Thomas Petazzoni20396132017-03-07 16:53:00 +01004037 *buf_dma_addr = dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004038 *buf_phys_addr = virt_to_phys(data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004039
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004040 return data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004041}
4042
Marcin Wojtas3f518502014-07-10 16:52:13 -03004043/* Release buffer to BM */
4044static inline void mvpp2_bm_pool_put(struct mvpp2_port *port, int pool,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004045 dma_addr_t buf_dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004046 phys_addr_t buf_phys_addr)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004047{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004048 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01004049
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004050 if (port->priv->hw_version == MVPP22) {
4051 u32 val = 0;
4052
4053 if (sizeof(dma_addr_t) == 8)
4054 val |= upper_32_bits(buf_dma_addr) &
4055 MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK;
4056
4057 if (sizeof(phys_addr_t) == 8)
4058 val |= (upper_32_bits(buf_phys_addr)
4059 << MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT) &
4060 MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK;
4061
Thomas Petazzonia7868412017-03-07 16:53:13 +01004062 mvpp2_percpu_write(port->priv, cpu,
4063 MVPP22_BM_ADDR_HIGH_RLS_REG, val);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004064 }
4065
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004066 /* MVPP2_BM_VIRT_RLS_REG is not interpreted by HW, and simply
4067 * returned in the "cookie" field of the RX
4068 * descriptor. Instead of storing the virtual address, we
4069 * store the physical address
4070 */
Thomas Petazzonia7868412017-03-07 16:53:13 +01004071 mvpp2_percpu_write(port->priv, cpu,
4072 MVPP2_BM_VIRT_RLS_REG, buf_phys_addr);
4073 mvpp2_percpu_write(port->priv, cpu,
4074 MVPP2_BM_PHY_RLS_REG(pool), buf_dma_addr);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004075
4076 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03004077}
4078
Marcin Wojtas3f518502014-07-10 16:52:13 -03004079/* Allocate buffers for the pool */
4080static int mvpp2_bm_bufs_add(struct mvpp2_port *port,
4081 struct mvpp2_bm_pool *bm_pool, int buf_num)
4082{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004083 int i, buf_size, total_size;
Thomas Petazzoni20396132017-03-07 16:53:00 +01004084 dma_addr_t dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004085 phys_addr_t phys_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004086 void *buf;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004087
4088 buf_size = MVPP2_RX_BUF_SIZE(bm_pool->pkt_size);
4089 total_size = MVPP2_RX_TOTAL_SIZE(buf_size);
4090
4091 if (buf_num < 0 ||
4092 (buf_num + bm_pool->buf_num > bm_pool->size)) {
4093 netdev_err(port->dev,
4094 "cannot allocate %d buffers for pool %d\n",
4095 buf_num, bm_pool->id);
4096 return 0;
4097 }
4098
Marcin Wojtas3f518502014-07-10 16:52:13 -03004099 for (i = 0; i < buf_num; i++) {
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004100 buf = mvpp2_buf_alloc(port, bm_pool, &dma_addr,
4101 &phys_addr, GFP_KERNEL);
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004102 if (!buf)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004103 break;
4104
Thomas Petazzoni20396132017-03-07 16:53:00 +01004105 mvpp2_bm_pool_put(port, bm_pool->id, dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004106 phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004107 }
4108
4109 /* Update BM driver with number of buffers added to pool */
4110 bm_pool->buf_num += i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004111
4112 netdev_dbg(port->dev,
4113 "%s pool %d: pkt_size=%4d, buf_size=%4d, total_size=%4d\n",
4114 bm_pool->type == MVPP2_BM_SWF_SHORT ? "short" : " long",
4115 bm_pool->id, bm_pool->pkt_size, buf_size, total_size);
4116
4117 netdev_dbg(port->dev,
4118 "%s pool %d: %d of %d buffers added\n",
4119 bm_pool->type == MVPP2_BM_SWF_SHORT ? "short" : " long",
4120 bm_pool->id, i, buf_num);
4121 return i;
4122}
4123
4124/* Notify the driver that BM pool is being used as specific type and return the
4125 * pool pointer on success
4126 */
4127static struct mvpp2_bm_pool *
4128mvpp2_bm_pool_use(struct mvpp2_port *port, int pool, enum mvpp2_bm_type type,
4129 int pkt_size)
4130{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004131 struct mvpp2_bm_pool *new_pool = &port->priv->bm_pools[pool];
4132 int num;
4133
4134 if (new_pool->type != MVPP2_BM_FREE && new_pool->type != type) {
4135 netdev_err(port->dev, "mixing pool types is forbidden\n");
4136 return NULL;
4137 }
4138
Marcin Wojtas3f518502014-07-10 16:52:13 -03004139 if (new_pool->type == MVPP2_BM_FREE)
4140 new_pool->type = type;
4141
4142 /* Allocate buffers in case BM pool is used as long pool, but packet
4143 * size doesn't match MTU or BM pool hasn't being used yet
4144 */
4145 if (((type == MVPP2_BM_SWF_LONG) && (pkt_size > new_pool->pkt_size)) ||
4146 (new_pool->pkt_size == 0)) {
4147 int pkts_num;
4148
4149 /* Set default buffer number or free all the buffers in case
4150 * the pool is not empty
4151 */
4152 pkts_num = new_pool->buf_num;
4153 if (pkts_num == 0)
4154 pkts_num = type == MVPP2_BM_SWF_LONG ?
4155 MVPP2_BM_LONG_BUF_NUM :
4156 MVPP2_BM_SHORT_BUF_NUM;
4157 else
Marcin Wojtas4229d502015-12-03 15:20:50 +01004158 mvpp2_bm_bufs_free(port->dev->dev.parent,
4159 port->priv, new_pool);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004160
4161 new_pool->pkt_size = pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004162 new_pool->frag_size =
4163 SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
4164 MVPP2_SKB_SHINFO_SIZE;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004165
4166 /* Allocate buffers for this pool */
4167 num = mvpp2_bm_bufs_add(port, new_pool, pkts_num);
4168 if (num != pkts_num) {
4169 WARN(1, "pool %d: %d of %d allocated\n",
4170 new_pool->id, num, pkts_num);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004171 return NULL;
4172 }
4173 }
4174
4175 mvpp2_bm_pool_bufsize_set(port->priv, new_pool,
4176 MVPP2_RX_BUF_SIZE(new_pool->pkt_size));
4177
Marcin Wojtas3f518502014-07-10 16:52:13 -03004178 return new_pool;
4179}
4180
4181/* Initialize pools for swf */
4182static int mvpp2_swf_bm_pool_init(struct mvpp2_port *port)
4183{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004184 int rxq;
4185
4186 if (!port->pool_long) {
4187 port->pool_long =
4188 mvpp2_bm_pool_use(port, MVPP2_BM_SWF_LONG_POOL(port->id),
4189 MVPP2_BM_SWF_LONG,
4190 port->pkt_size);
4191 if (!port->pool_long)
4192 return -ENOMEM;
4193
Marcin Wojtas3f518502014-07-10 16:52:13 -03004194 port->pool_long->port_map |= (1 << port->id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004195
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004196 for (rxq = 0; rxq < port->nrxqs; rxq++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004197 mvpp2_rxq_long_pool_set(port, rxq, port->pool_long->id);
4198 }
4199
4200 if (!port->pool_short) {
4201 port->pool_short =
4202 mvpp2_bm_pool_use(port, MVPP2_BM_SWF_SHORT_POOL,
4203 MVPP2_BM_SWF_SHORT,
4204 MVPP2_BM_SHORT_PKT_SIZE);
4205 if (!port->pool_short)
4206 return -ENOMEM;
4207
Marcin Wojtas3f518502014-07-10 16:52:13 -03004208 port->pool_short->port_map |= (1 << port->id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004209
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004210 for (rxq = 0; rxq < port->nrxqs; rxq++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004211 mvpp2_rxq_short_pool_set(port, rxq,
4212 port->pool_short->id);
4213 }
4214
4215 return 0;
4216}
4217
4218static int mvpp2_bm_update_mtu(struct net_device *dev, int mtu)
4219{
4220 struct mvpp2_port *port = netdev_priv(dev);
4221 struct mvpp2_bm_pool *port_pool = port->pool_long;
4222 int num, pkts_num = port_pool->buf_num;
4223 int pkt_size = MVPP2_RX_PKT_SIZE(mtu);
4224
4225 /* Update BM pool with new buffer size */
Marcin Wojtas4229d502015-12-03 15:20:50 +01004226 mvpp2_bm_bufs_free(dev->dev.parent, port->priv, port_pool);
Ezequiel Garciad74c96c2014-07-21 13:48:13 -03004227 if (port_pool->buf_num) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004228 WARN(1, "cannot free all buffers in pool %d\n", port_pool->id);
4229 return -EIO;
4230 }
4231
4232 port_pool->pkt_size = pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004233 port_pool->frag_size = SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
4234 MVPP2_SKB_SHINFO_SIZE;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004235 num = mvpp2_bm_bufs_add(port, port_pool, pkts_num);
4236 if (num != pkts_num) {
4237 WARN(1, "pool %d: %d of %d allocated\n",
4238 port_pool->id, num, pkts_num);
4239 return -EIO;
4240 }
4241
4242 mvpp2_bm_pool_bufsize_set(port->priv, port_pool,
4243 MVPP2_RX_BUF_SIZE(port_pool->pkt_size));
4244 dev->mtu = mtu;
4245 netdev_update_features(dev);
4246 return 0;
4247}
4248
4249static inline void mvpp2_interrupts_enable(struct mvpp2_port *port)
4250{
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004251 int i, sw_thread_mask = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004252
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004253 for (i = 0; i < port->nqvecs; i++)
4254 sw_thread_mask |= port->qvecs[i].sw_thread_mask;
4255
Marcin Wojtas3f518502014-07-10 16:52:13 -03004256 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004257 MVPP2_ISR_ENABLE_INTERRUPT(sw_thread_mask));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004258}
4259
4260static inline void mvpp2_interrupts_disable(struct mvpp2_port *port)
4261{
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004262 int i, sw_thread_mask = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004263
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004264 for (i = 0; i < port->nqvecs; i++)
4265 sw_thread_mask |= port->qvecs[i].sw_thread_mask;
4266
Marcin Wojtas3f518502014-07-10 16:52:13 -03004267 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004268 MVPP2_ISR_DISABLE_INTERRUPT(sw_thread_mask));
4269}
4270
4271static inline void mvpp2_qvec_interrupt_enable(struct mvpp2_queue_vector *qvec)
4272{
4273 struct mvpp2_port *port = qvec->port;
4274
4275 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
4276 MVPP2_ISR_ENABLE_INTERRUPT(qvec->sw_thread_mask));
4277}
4278
4279static inline void mvpp2_qvec_interrupt_disable(struct mvpp2_queue_vector *qvec)
4280{
4281 struct mvpp2_port *port = qvec->port;
4282
4283 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
4284 MVPP2_ISR_DISABLE_INTERRUPT(qvec->sw_thread_mask));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004285}
4286
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004287/* Mask the current CPU's Rx/Tx interrupts
4288 * Called by on_each_cpu(), guaranteed to run with migration disabled,
4289 * using smp_processor_id() is OK.
4290 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004291static void mvpp2_interrupts_mask(void *arg)
4292{
4293 struct mvpp2_port *port = arg;
4294
Thomas Petazzonia7868412017-03-07 16:53:13 +01004295 mvpp2_percpu_write(port->priv, smp_processor_id(),
4296 MVPP2_ISR_RX_TX_MASK_REG(port->id), 0);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004297}
4298
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004299/* Unmask the current CPU's Rx/Tx interrupts.
4300 * Called by on_each_cpu(), guaranteed to run with migration disabled,
4301 * using smp_processor_id() is OK.
4302 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004303static void mvpp2_interrupts_unmask(void *arg)
4304{
4305 struct mvpp2_port *port = arg;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02004306 u32 val;
4307
4308 val = MVPP2_CAUSE_MISC_SUM_MASK |
4309 MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
4310 if (port->has_tx_irqs)
4311 val |= MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004312
Thomas Petazzonia7868412017-03-07 16:53:13 +01004313 mvpp2_percpu_write(port->priv, smp_processor_id(),
Thomas Petazzoni213f4282017-08-03 10:42:00 +02004314 MVPP2_ISR_RX_TX_MASK_REG(port->id), val);
4315}
4316
4317static void
4318mvpp2_shared_interrupt_mask_unmask(struct mvpp2_port *port, bool mask)
4319{
4320 u32 val;
4321 int i;
4322
4323 if (port->priv->hw_version != MVPP22)
4324 return;
4325
4326 if (mask)
4327 val = 0;
4328 else
4329 val = MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
4330
4331 for (i = 0; i < port->nqvecs; i++) {
4332 struct mvpp2_queue_vector *v = port->qvecs + i;
4333
4334 if (v->type != MVPP2_QUEUE_VECTOR_SHARED)
4335 continue;
4336
4337 mvpp2_percpu_write(port->priv, v->sw_thread_id,
4338 MVPP2_ISR_RX_TX_MASK_REG(port->id), val);
4339 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004340}
4341
4342/* Port configuration routines */
4343
Antoine Ténartf84bf382017-08-22 19:08:27 +02004344static void mvpp22_gop_init_rgmii(struct mvpp2_port *port)
4345{
4346 struct mvpp2 *priv = port->priv;
4347 u32 val;
4348
4349 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
4350 val |= GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT;
4351 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
4352
4353 regmap_read(priv->sysctrl_base, GENCONF_CTRL0, &val);
4354 if (port->gop_id == 2)
4355 val |= GENCONF_CTRL0_PORT0_RGMII | GENCONF_CTRL0_PORT1_RGMII;
4356 else if (port->gop_id == 3)
4357 val |= GENCONF_CTRL0_PORT1_RGMII_MII;
4358 regmap_write(priv->sysctrl_base, GENCONF_CTRL0, val);
4359}
4360
4361static void mvpp22_gop_init_sgmii(struct mvpp2_port *port)
4362{
4363 struct mvpp2 *priv = port->priv;
4364 u32 val;
4365
4366 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
4367 val |= GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT |
4368 GENCONF_PORT_CTRL0_RX_DATA_SAMPLE;
4369 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
4370
4371 if (port->gop_id > 1) {
4372 regmap_read(priv->sysctrl_base, GENCONF_CTRL0, &val);
4373 if (port->gop_id == 2)
4374 val &= ~GENCONF_CTRL0_PORT0_RGMII;
4375 else if (port->gop_id == 3)
4376 val &= ~GENCONF_CTRL0_PORT1_RGMII_MII;
4377 regmap_write(priv->sysctrl_base, GENCONF_CTRL0, val);
4378 }
4379}
4380
4381static void mvpp22_gop_init_10gkr(struct mvpp2_port *port)
4382{
4383 struct mvpp2 *priv = port->priv;
4384 void __iomem *mpcs = priv->iface_base + MVPP22_MPCS_BASE(port->gop_id);
4385 void __iomem *xpcs = priv->iface_base + MVPP22_XPCS_BASE(port->gop_id);
4386 u32 val;
4387
4388 /* XPCS */
4389 val = readl(xpcs + MVPP22_XPCS_CFG0);
4390 val &= ~(MVPP22_XPCS_CFG0_PCS_MODE(0x3) |
4391 MVPP22_XPCS_CFG0_ACTIVE_LANE(0x3));
4392 val |= MVPP22_XPCS_CFG0_ACTIVE_LANE(2);
4393 writel(val, xpcs + MVPP22_XPCS_CFG0);
4394
4395 /* MPCS */
4396 val = readl(mpcs + MVPP22_MPCS_CTRL);
4397 val &= ~MVPP22_MPCS_CTRL_FWD_ERR_CONN;
4398 writel(val, mpcs + MVPP22_MPCS_CTRL);
4399
4400 val = readl(mpcs + MVPP22_MPCS_CLK_RESET);
4401 val &= ~(MVPP22_MPCS_CLK_RESET_DIV_RATIO(0x7) | MAC_CLK_RESET_MAC |
4402 MAC_CLK_RESET_SD_RX | MAC_CLK_RESET_SD_TX);
4403 val |= MVPP22_MPCS_CLK_RESET_DIV_RATIO(1);
4404 writel(val, mpcs + MVPP22_MPCS_CLK_RESET);
4405
4406 val &= ~MVPP22_MPCS_CLK_RESET_DIV_SET;
4407 val |= MAC_CLK_RESET_MAC | MAC_CLK_RESET_SD_RX | MAC_CLK_RESET_SD_TX;
4408 writel(val, mpcs + MVPP22_MPCS_CLK_RESET);
4409}
4410
4411static int mvpp22_gop_init(struct mvpp2_port *port)
4412{
4413 struct mvpp2 *priv = port->priv;
4414 u32 val;
4415
4416 if (!priv->sysctrl_base)
4417 return 0;
4418
4419 switch (port->phy_interface) {
4420 case PHY_INTERFACE_MODE_RGMII:
4421 case PHY_INTERFACE_MODE_RGMII_ID:
4422 case PHY_INTERFACE_MODE_RGMII_RXID:
4423 case PHY_INTERFACE_MODE_RGMII_TXID:
4424 if (port->gop_id == 0)
4425 goto invalid_conf;
4426 mvpp22_gop_init_rgmii(port);
4427 break;
4428 case PHY_INTERFACE_MODE_SGMII:
4429 mvpp22_gop_init_sgmii(port);
4430 break;
4431 case PHY_INTERFACE_MODE_10GKR:
4432 if (port->gop_id != 0)
4433 goto invalid_conf;
4434 mvpp22_gop_init_10gkr(port);
4435 break;
4436 default:
4437 goto unsupported_conf;
4438 }
4439
4440 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL1, &val);
4441 val |= GENCONF_PORT_CTRL1_RESET(port->gop_id) |
4442 GENCONF_PORT_CTRL1_EN(port->gop_id);
4443 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL1, val);
4444
4445 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
4446 val |= GENCONF_PORT_CTRL0_CLK_DIV_PHASE_CLR;
4447 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
4448
4449 regmap_read(priv->sysctrl_base, GENCONF_SOFT_RESET1, &val);
4450 val |= GENCONF_SOFT_RESET1_GOP;
4451 regmap_write(priv->sysctrl_base, GENCONF_SOFT_RESET1, val);
4452
4453unsupported_conf:
4454 return 0;
4455
4456invalid_conf:
4457 netdev_err(port->dev, "Invalid port configuration\n");
4458 return -EINVAL;
4459}
4460
Antoine Tenartfd3651b2017-09-01 11:04:54 +02004461static void mvpp22_gop_unmask_irq(struct mvpp2_port *port)
4462{
4463 u32 val;
4464
4465 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
4466 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4467 /* Enable the GMAC link status irq for this port */
4468 val = readl(port->base + MVPP22_GMAC_INT_SUM_MASK);
4469 val |= MVPP22_GMAC_INT_SUM_MASK_LINK_STAT;
4470 writel(val, port->base + MVPP22_GMAC_INT_SUM_MASK);
4471 }
4472
4473 if (port->gop_id == 0) {
4474 /* Enable the XLG/GIG irqs for this port */
4475 val = readl(port->base + MVPP22_XLG_EXT_INT_MASK);
4476 if (port->phy_interface == PHY_INTERFACE_MODE_10GKR)
4477 val |= MVPP22_XLG_EXT_INT_MASK_XLG;
4478 else
4479 val |= MVPP22_XLG_EXT_INT_MASK_GIG;
4480 writel(val, port->base + MVPP22_XLG_EXT_INT_MASK);
4481 }
4482}
4483
4484static void mvpp22_gop_mask_irq(struct mvpp2_port *port)
4485{
4486 u32 val;
4487
4488 if (port->gop_id == 0) {
4489 val = readl(port->base + MVPP22_XLG_EXT_INT_MASK);
4490 val &= ~(MVPP22_XLG_EXT_INT_MASK_XLG |
4491 MVPP22_XLG_EXT_INT_MASK_GIG);
4492 writel(val, port->base + MVPP22_XLG_EXT_INT_MASK);
4493 }
4494
4495 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
4496 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4497 val = readl(port->base + MVPP22_GMAC_INT_SUM_MASK);
4498 val &= ~MVPP22_GMAC_INT_SUM_MASK_LINK_STAT;
4499 writel(val, port->base + MVPP22_GMAC_INT_SUM_MASK);
4500 }
4501}
4502
4503static void mvpp22_gop_setup_irq(struct mvpp2_port *port)
4504{
4505 u32 val;
4506
4507 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
4508 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4509 val = readl(port->base + MVPP22_GMAC_INT_MASK);
4510 val |= MVPP22_GMAC_INT_MASK_LINK_STAT;
4511 writel(val, port->base + MVPP22_GMAC_INT_MASK);
4512 }
4513
4514 if (port->gop_id == 0) {
4515 val = readl(port->base + MVPP22_XLG_INT_MASK);
4516 val |= MVPP22_XLG_INT_MASK_LINK;
4517 writel(val, port->base + MVPP22_XLG_INT_MASK);
4518 }
4519
4520 mvpp22_gop_unmask_irq(port);
4521}
4522
Antoine Tenart542897d2017-08-30 10:29:15 +02004523static int mvpp22_comphy_init(struct mvpp2_port *port)
4524{
4525 enum phy_mode mode;
4526 int ret;
4527
4528 if (!port->comphy)
4529 return 0;
4530
4531 switch (port->phy_interface) {
4532 case PHY_INTERFACE_MODE_SGMII:
4533 mode = PHY_MODE_SGMII;
4534 break;
4535 case PHY_INTERFACE_MODE_10GKR:
4536 mode = PHY_MODE_10GKR;
4537 break;
4538 default:
4539 return -EINVAL;
4540 }
4541
4542 ret = phy_set_mode(port->comphy, mode);
4543 if (ret)
4544 return ret;
4545
4546 return phy_power_on(port->comphy);
4547}
4548
Antoine Ténart39193572017-08-22 19:08:24 +02004549static void mvpp2_port_mii_gmac_configure_mode(struct mvpp2_port *port)
4550{
4551 u32 val;
4552
4553 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4554 val = readl(port->base + MVPP22_GMAC_CTRL_4_REG);
4555 val |= MVPP22_CTRL4_SYNC_BYPASS_DIS | MVPP22_CTRL4_DP_CLK_SEL |
4556 MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
4557 val &= ~MVPP22_CTRL4_EXT_PIN_GMII_SEL;
4558 writel(val, port->base + MVPP22_GMAC_CTRL_4_REG);
4559
4560 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4561 val |= MVPP2_GMAC_DISABLE_PADDING;
4562 val &= ~MVPP2_GMAC_FLOW_CTRL_MASK;
4563 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
Antoine Tenart1df22702017-09-01 11:04:52 +02004564 } else if (phy_interface_mode_is_rgmii(port->phy_interface)) {
Antoine Ténart39193572017-08-22 19:08:24 +02004565 val = readl(port->base + MVPP22_GMAC_CTRL_4_REG);
4566 val |= MVPP22_CTRL4_EXT_PIN_GMII_SEL |
4567 MVPP22_CTRL4_SYNC_BYPASS_DIS |
4568 MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
4569 val &= ~MVPP22_CTRL4_DP_CLK_SEL;
4570 writel(val, port->base + MVPP22_GMAC_CTRL_4_REG);
4571
4572 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4573 val &= ~MVPP2_GMAC_DISABLE_PADDING;
4574 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4575 }
4576
4577 /* The port is connected to a copper PHY */
4578 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4579 val &= ~MVPP2_GMAC_PORT_TYPE_MASK;
4580 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4581
4582 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4583 val |= MVPP2_GMAC_IN_BAND_AUTONEG_BYPASS |
4584 MVPP2_GMAC_AN_SPEED_EN | MVPP2_GMAC_FLOW_CTRL_AUTONEG |
4585 MVPP2_GMAC_AN_DUPLEX_EN;
4586 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
4587 val |= MVPP2_GMAC_IN_BAND_AUTONEG;
4588 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4589}
4590
4591static void mvpp2_port_mii_gmac_configure(struct mvpp2_port *port)
4592{
4593 u32 val;
4594
4595 /* Force link down */
4596 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4597 val &= ~MVPP2_GMAC_FORCE_LINK_PASS;
4598 val |= MVPP2_GMAC_FORCE_LINK_DOWN;
4599 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4600
4601 /* Set the GMAC in a reset state */
4602 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4603 val |= MVPP2_GMAC_PORT_RESET_MASK;
4604 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4605
4606 /* Configure the PCS and in-band AN */
4607 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4608 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4609 val |= MVPP2_GMAC_INBAND_AN_MASK | MVPP2_GMAC_PCS_ENABLE_MASK;
Antoine Tenart1df22702017-09-01 11:04:52 +02004610 } else if (phy_interface_mode_is_rgmii(port->phy_interface)) {
Antoine Ténart39193572017-08-22 19:08:24 +02004611 val &= ~MVPP2_GMAC_PCS_ENABLE_MASK;
Antoine Ténart39193572017-08-22 19:08:24 +02004612 }
4613 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4614
4615 mvpp2_port_mii_gmac_configure_mode(port);
4616
4617 /* Unset the GMAC reset state */
4618 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4619 val &= ~MVPP2_GMAC_PORT_RESET_MASK;
4620 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4621
4622 /* Stop forcing link down */
4623 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4624 val &= ~MVPP2_GMAC_FORCE_LINK_DOWN;
4625 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4626}
4627
Antoine Ténart77321952017-08-22 19:08:25 +02004628static void mvpp2_port_mii_xlg_configure(struct mvpp2_port *port)
4629{
4630 u32 val;
4631
4632 if (port->gop_id != 0)
4633 return;
4634
4635 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
4636 val |= MVPP22_XLG_CTRL0_RX_FLOW_CTRL_EN;
4637 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
4638
4639 val = readl(port->base + MVPP22_XLG_CTRL4_REG);
4640 val &= ~MVPP22_XLG_CTRL4_MACMODSELECT_GMAC;
4641 val |= MVPP22_XLG_CTRL4_FWD_FC | MVPP22_XLG_CTRL4_FWD_PFC;
4642 writel(val, port->base + MVPP22_XLG_CTRL4_REG);
4643}
4644
Thomas Petazzoni26975822017-03-07 16:53:14 +01004645static void mvpp22_port_mii_set(struct mvpp2_port *port)
4646{
4647 u32 val;
4648
Thomas Petazzoni26975822017-03-07 16:53:14 +01004649 /* Only GOP port 0 has an XLG MAC */
4650 if (port->gop_id == 0) {
4651 val = readl(port->base + MVPP22_XLG_CTRL3_REG);
4652 val &= ~MVPP22_XLG_CTRL3_MACMODESELECT_MASK;
Antoine Ténart725757a2017-06-12 16:01:39 +02004653
4654 if (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
4655 port->phy_interface == PHY_INTERFACE_MODE_10GKR)
4656 val |= MVPP22_XLG_CTRL3_MACMODESELECT_10G;
4657 else
4658 val |= MVPP22_XLG_CTRL3_MACMODESELECT_GMAC;
4659
Thomas Petazzoni26975822017-03-07 16:53:14 +01004660 writel(val, port->base + MVPP22_XLG_CTRL3_REG);
4661 }
Thomas Petazzoni26975822017-03-07 16:53:14 +01004662}
4663
Marcin Wojtas3f518502014-07-10 16:52:13 -03004664static void mvpp2_port_mii_set(struct mvpp2_port *port)
4665{
Thomas Petazzoni26975822017-03-07 16:53:14 +01004666 if (port->priv->hw_version == MVPP22)
4667 mvpp22_port_mii_set(port);
4668
Antoine Tenart1df22702017-09-01 11:04:52 +02004669 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
Antoine Ténart39193572017-08-22 19:08:24 +02004670 port->phy_interface == PHY_INTERFACE_MODE_SGMII)
4671 mvpp2_port_mii_gmac_configure(port);
Antoine Ténart77321952017-08-22 19:08:25 +02004672 else if (port->phy_interface == PHY_INTERFACE_MODE_10GKR)
4673 mvpp2_port_mii_xlg_configure(port);
Marcin Wojtas08a23752014-07-21 13:48:12 -03004674}
4675
4676static void mvpp2_port_fc_adv_enable(struct mvpp2_port *port)
4677{
4678 u32 val;
4679
4680 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4681 val |= MVPP2_GMAC_FC_ADV_EN;
4682 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004683}
4684
4685static void mvpp2_port_enable(struct mvpp2_port *port)
4686{
4687 u32 val;
4688
Antoine Ténart725757a2017-06-12 16:01:39 +02004689 /* Only GOP port 0 has an XLG MAC */
4690 if (port->gop_id == 0 &&
4691 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
4692 port->phy_interface == PHY_INTERFACE_MODE_10GKR)) {
4693 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
4694 val |= MVPP22_XLG_CTRL0_PORT_EN |
4695 MVPP22_XLG_CTRL0_MAC_RESET_DIS;
4696 val &= ~MVPP22_XLG_CTRL0_MIB_CNT_DIS;
4697 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
4698 } else {
4699 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4700 val |= MVPP2_GMAC_PORT_EN_MASK;
4701 val |= MVPP2_GMAC_MIB_CNTR_EN_MASK;
4702 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4703 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004704}
4705
4706static void mvpp2_port_disable(struct mvpp2_port *port)
4707{
4708 u32 val;
4709
Antoine Ténart725757a2017-06-12 16:01:39 +02004710 /* Only GOP port 0 has an XLG MAC */
4711 if (port->gop_id == 0 &&
4712 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
4713 port->phy_interface == PHY_INTERFACE_MODE_10GKR)) {
4714 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
4715 val &= ~(MVPP22_XLG_CTRL0_PORT_EN |
4716 MVPP22_XLG_CTRL0_MAC_RESET_DIS);
4717 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
4718 } else {
4719 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4720 val &= ~(MVPP2_GMAC_PORT_EN_MASK);
4721 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4722 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004723}
4724
4725/* Set IEEE 802.3x Flow Control Xon Packet Transmission Mode */
4726static void mvpp2_port_periodic_xon_disable(struct mvpp2_port *port)
4727{
4728 u32 val;
4729
4730 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG) &
4731 ~MVPP2_GMAC_PERIODIC_XON_EN_MASK;
4732 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
4733}
4734
4735/* Configure loopback port */
4736static void mvpp2_port_loopback_set(struct mvpp2_port *port)
4737{
4738 u32 val;
4739
4740 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
4741
4742 if (port->speed == 1000)
4743 val |= MVPP2_GMAC_GMII_LB_EN_MASK;
4744 else
4745 val &= ~MVPP2_GMAC_GMII_LB_EN_MASK;
4746
4747 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
4748 val |= MVPP2_GMAC_PCS_LB_EN_MASK;
4749 else
4750 val &= ~MVPP2_GMAC_PCS_LB_EN_MASK;
4751
4752 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
4753}
4754
4755static void mvpp2_port_reset(struct mvpp2_port *port)
4756{
4757 u32 val;
4758
4759 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
4760 ~MVPP2_GMAC_PORT_RESET_MASK;
4761 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4762
4763 while (readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
4764 MVPP2_GMAC_PORT_RESET_MASK)
4765 continue;
4766}
4767
4768/* Change maximum receive size of the port */
4769static inline void mvpp2_gmac_max_rx_size_set(struct mvpp2_port *port)
4770{
4771 u32 val;
4772
4773 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4774 val &= ~MVPP2_GMAC_MAX_RX_SIZE_MASK;
4775 val |= (((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
4776 MVPP2_GMAC_MAX_RX_SIZE_OFFS);
4777 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4778}
4779
Stefan Chulski76eb1b12017-08-22 19:08:26 +02004780/* Change maximum receive size of the port */
4781static inline void mvpp2_xlg_max_rx_size_set(struct mvpp2_port *port)
4782{
4783 u32 val;
4784
4785 val = readl(port->base + MVPP22_XLG_CTRL1_REG);
4786 val &= ~MVPP22_XLG_CTRL1_FRAMESIZELIMIT_MASK;
4787 val |= ((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
Antoine Ténartec15ecd2017-08-25 15:24:46 +02004788 MVPP22_XLG_CTRL1_FRAMESIZELIMIT_OFFS;
Stefan Chulski76eb1b12017-08-22 19:08:26 +02004789 writel(val, port->base + MVPP22_XLG_CTRL1_REG);
4790}
4791
Marcin Wojtas3f518502014-07-10 16:52:13 -03004792/* Set defaults to the MVPP2 port */
4793static void mvpp2_defaults_set(struct mvpp2_port *port)
4794{
4795 int tx_port_num, val, queue, ptxq, lrxq;
4796
Thomas Petazzoni3d9017d2017-03-07 16:53:11 +01004797 if (port->priv->hw_version == MVPP21) {
4798 /* Configure port to loopback if needed */
4799 if (port->flags & MVPP2_F_LOOPBACK)
4800 mvpp2_port_loopback_set(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004801
Thomas Petazzoni3d9017d2017-03-07 16:53:11 +01004802 /* Update TX FIFO MIN Threshold */
4803 val = readl(port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
4804 val &= ~MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK;
4805 /* Min. TX threshold must be less than minimal packet length */
4806 val |= MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(64 - 4 - 2);
4807 writel(val, port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
4808 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004809
4810 /* Disable Legacy WRR, Disable EJP, Release from reset */
4811 tx_port_num = mvpp2_egress_port(port);
4812 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG,
4813 tx_port_num);
4814 mvpp2_write(port->priv, MVPP2_TXP_SCHED_CMD_1_REG, 0);
4815
4816 /* Close bandwidth for all queues */
4817 for (queue = 0; queue < MVPP2_MAX_TXQ; queue++) {
4818 ptxq = mvpp2_txq_phys(port->id, queue);
4819 mvpp2_write(port->priv,
4820 MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(ptxq), 0);
4821 }
4822
4823 /* Set refill period to 1 usec, refill tokens
4824 * and bucket size to maximum
4825 */
4826 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PERIOD_REG,
4827 port->priv->tclk / USEC_PER_SEC);
4828 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_REFILL_REG);
4829 val &= ~MVPP2_TXP_REFILL_PERIOD_ALL_MASK;
4830 val |= MVPP2_TXP_REFILL_PERIOD_MASK(1);
4831 val |= MVPP2_TXP_REFILL_TOKENS_ALL_MASK;
4832 mvpp2_write(port->priv, MVPP2_TXP_SCHED_REFILL_REG, val);
4833 val = MVPP2_TXP_TOKEN_SIZE_MAX;
4834 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
4835
4836 /* Set MaximumLowLatencyPacketSize value to 256 */
4837 mvpp2_write(port->priv, MVPP2_RX_CTRL_REG(port->id),
4838 MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK |
4839 MVPP2_RX_LOW_LATENCY_PKT_SIZE(256));
4840
4841 /* Enable Rx cache snoop */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004842 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004843 queue = port->rxqs[lrxq]->id;
4844 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
4845 val |= MVPP2_SNOOP_PKT_SIZE_MASK |
4846 MVPP2_SNOOP_BUF_HDR_MASK;
4847 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
4848 }
4849
4850 /* At default, mask all interrupts to all present cpus */
4851 mvpp2_interrupts_disable(port);
4852}
4853
4854/* Enable/disable receiving packets */
4855static void mvpp2_ingress_enable(struct mvpp2_port *port)
4856{
4857 u32 val;
4858 int lrxq, queue;
4859
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004860 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004861 queue = port->rxqs[lrxq]->id;
4862 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
4863 val &= ~MVPP2_RXQ_DISABLE_MASK;
4864 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
4865 }
4866}
4867
4868static void mvpp2_ingress_disable(struct mvpp2_port *port)
4869{
4870 u32 val;
4871 int lrxq, queue;
4872
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004873 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004874 queue = port->rxqs[lrxq]->id;
4875 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
4876 val |= MVPP2_RXQ_DISABLE_MASK;
4877 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
4878 }
4879}
4880
4881/* Enable transmit via physical egress queue
4882 * - HW starts take descriptors from DRAM
4883 */
4884static void mvpp2_egress_enable(struct mvpp2_port *port)
4885{
4886 u32 qmap;
4887 int queue;
4888 int tx_port_num = mvpp2_egress_port(port);
4889
4890 /* Enable all initialized TXs. */
4891 qmap = 0;
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004892 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004893 struct mvpp2_tx_queue *txq = port->txqs[queue];
4894
Markus Elfringdbbb2f02017-04-17 14:07:52 +02004895 if (txq->descs)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004896 qmap |= (1 << queue);
4897 }
4898
4899 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
4900 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG, qmap);
4901}
4902
4903/* Disable transmit via physical egress queue
4904 * - HW doesn't take descriptors from DRAM
4905 */
4906static void mvpp2_egress_disable(struct mvpp2_port *port)
4907{
4908 u32 reg_data;
4909 int delay;
4910 int tx_port_num = mvpp2_egress_port(port);
4911
4912 /* Issue stop command for active channels only */
4913 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
4914 reg_data = (mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG)) &
4915 MVPP2_TXP_SCHED_ENQ_MASK;
4916 if (reg_data != 0)
4917 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG,
4918 (reg_data << MVPP2_TXP_SCHED_DISQ_OFFSET));
4919
4920 /* Wait for all Tx activity to terminate. */
4921 delay = 0;
4922 do {
4923 if (delay >= MVPP2_TX_DISABLE_TIMEOUT_MSEC) {
4924 netdev_warn(port->dev,
4925 "Tx stop timed out, status=0x%08x\n",
4926 reg_data);
4927 break;
4928 }
4929 mdelay(1);
4930 delay++;
4931
4932 /* Check port TX Command register that all
4933 * Tx queues are stopped
4934 */
4935 reg_data = mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG);
4936 } while (reg_data & MVPP2_TXP_SCHED_ENQ_MASK);
4937}
4938
4939/* Rx descriptors helper methods */
4940
4941/* Get number of Rx descriptors occupied by received packets */
4942static inline int
4943mvpp2_rxq_received(struct mvpp2_port *port, int rxq_id)
4944{
4945 u32 val = mvpp2_read(port->priv, MVPP2_RXQ_STATUS_REG(rxq_id));
4946
4947 return val & MVPP2_RXQ_OCCUPIED_MASK;
4948}
4949
4950/* Update Rx queue status with the number of occupied and available
4951 * Rx descriptor slots.
4952 */
4953static inline void
4954mvpp2_rxq_status_update(struct mvpp2_port *port, int rxq_id,
4955 int used_count, int free_count)
4956{
4957 /* Decrement the number of used descriptors and increment count
4958 * increment the number of free descriptors.
4959 */
4960 u32 val = used_count | (free_count << MVPP2_RXQ_NUM_NEW_OFFSET);
4961
4962 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_UPDATE_REG(rxq_id), val);
4963}
4964
4965/* Get pointer to next RX descriptor to be processed by SW */
4966static inline struct mvpp2_rx_desc *
4967mvpp2_rxq_next_desc_get(struct mvpp2_rx_queue *rxq)
4968{
4969 int rx_desc = rxq->next_desc_to_proc;
4970
4971 rxq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(rxq, rx_desc);
4972 prefetch(rxq->descs + rxq->next_desc_to_proc);
4973 return rxq->descs + rx_desc;
4974}
4975
4976/* Set rx queue offset */
4977static void mvpp2_rxq_offset_set(struct mvpp2_port *port,
4978 int prxq, int offset)
4979{
4980 u32 val;
4981
4982 /* Convert offset from bytes to units of 32 bytes */
4983 offset = offset >> 5;
4984
4985 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
4986 val &= ~MVPP2_RXQ_PACKET_OFFSET_MASK;
4987
4988 /* Offset is in */
4989 val |= ((offset << MVPP2_RXQ_PACKET_OFFSET_OFFS) &
4990 MVPP2_RXQ_PACKET_OFFSET_MASK);
4991
4992 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
4993}
4994
Marcin Wojtas3f518502014-07-10 16:52:13 -03004995/* Tx descriptors helper methods */
4996
Marcin Wojtas3f518502014-07-10 16:52:13 -03004997/* Get pointer to next Tx descriptor to be processed (send) by HW */
4998static struct mvpp2_tx_desc *
4999mvpp2_txq_next_desc_get(struct mvpp2_tx_queue *txq)
5000{
5001 int tx_desc = txq->next_desc_to_proc;
5002
5003 txq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(txq, tx_desc);
5004 return txq->descs + tx_desc;
5005}
5006
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005007/* Update HW with number of aggregated Tx descriptors to be sent
5008 *
5009 * Called only from mvpp2_tx(), so migration is disabled, using
5010 * smp_processor_id() is OK.
5011 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03005012static void mvpp2_aggr_txq_pend_desc_add(struct mvpp2_port *port, int pending)
5013{
5014 /* aggregated access - relevant TXQ number is written in TX desc */
Thomas Petazzonia7868412017-03-07 16:53:13 +01005015 mvpp2_percpu_write(port->priv, smp_processor_id(),
5016 MVPP2_AGGR_TXQ_UPDATE_REG, pending);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005017}
5018
5019
5020/* Check if there are enough free descriptors in aggregated txq.
5021 * If not, update the number of occupied descriptors and repeat the check.
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005022 *
5023 * Called only from mvpp2_tx(), so migration is disabled, using
5024 * smp_processor_id() is OK.
Marcin Wojtas3f518502014-07-10 16:52:13 -03005025 */
5026static int mvpp2_aggr_desc_num_check(struct mvpp2 *priv,
5027 struct mvpp2_tx_queue *aggr_txq, int num)
5028{
5029 if ((aggr_txq->count + num) > aggr_txq->size) {
5030 /* Update number of occupied aggregated Tx descriptors */
5031 int cpu = smp_processor_id();
5032 u32 val = mvpp2_read(priv, MVPP2_AGGR_TXQ_STATUS_REG(cpu));
5033
5034 aggr_txq->count = val & MVPP2_AGGR_TXQ_PENDING_MASK;
5035 }
5036
5037 if ((aggr_txq->count + num) > aggr_txq->size)
5038 return -ENOMEM;
5039
5040 return 0;
5041}
5042
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005043/* Reserved Tx descriptors allocation request
5044 *
5045 * Called only from mvpp2_txq_reserved_desc_num_proc(), itself called
5046 * only by mvpp2_tx(), so migration is disabled, using
5047 * smp_processor_id() is OK.
5048 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03005049static int mvpp2_txq_alloc_reserved_desc(struct mvpp2 *priv,
5050 struct mvpp2_tx_queue *txq, int num)
5051{
5052 u32 val;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005053 int cpu = smp_processor_id();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005054
5055 val = (txq->id << MVPP2_TXQ_RSVD_REQ_Q_OFFSET) | num;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005056 mvpp2_percpu_write(priv, cpu, MVPP2_TXQ_RSVD_REQ_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005057
Thomas Petazzonia7868412017-03-07 16:53:13 +01005058 val = mvpp2_percpu_read(priv, cpu, MVPP2_TXQ_RSVD_RSLT_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005059
5060 return val & MVPP2_TXQ_RSVD_RSLT_MASK;
5061}
5062
5063/* Check if there are enough reserved descriptors for transmission.
5064 * If not, request chunk of reserved descriptors and check again.
5065 */
5066static int mvpp2_txq_reserved_desc_num_proc(struct mvpp2 *priv,
5067 struct mvpp2_tx_queue *txq,
5068 struct mvpp2_txq_pcpu *txq_pcpu,
5069 int num)
5070{
5071 int req, cpu, desc_count;
5072
5073 if (txq_pcpu->reserved_num >= num)
5074 return 0;
5075
5076 /* Not enough descriptors reserved! Update the reserved descriptor
5077 * count and check again.
5078 */
5079
5080 desc_count = 0;
5081 /* Compute total of used descriptors */
5082 for_each_present_cpu(cpu) {
5083 struct mvpp2_txq_pcpu *txq_pcpu_aux;
5084
5085 txq_pcpu_aux = per_cpu_ptr(txq->pcpu, cpu);
5086 desc_count += txq_pcpu_aux->count;
5087 desc_count += txq_pcpu_aux->reserved_num;
5088 }
5089
5090 req = max(MVPP2_CPU_DESC_CHUNK, num - txq_pcpu->reserved_num);
5091 desc_count += req;
5092
5093 if (desc_count >
5094 (txq->size - (num_present_cpus() * MVPP2_CPU_DESC_CHUNK)))
5095 return -ENOMEM;
5096
5097 txq_pcpu->reserved_num += mvpp2_txq_alloc_reserved_desc(priv, txq, req);
5098
5099 /* OK, the descriptor cound has been updated: check again. */
5100 if (txq_pcpu->reserved_num < num)
5101 return -ENOMEM;
5102 return 0;
5103}
5104
5105/* Release the last allocated Tx descriptor. Useful to handle DMA
5106 * mapping failures in the Tx path.
5107 */
5108static void mvpp2_txq_desc_put(struct mvpp2_tx_queue *txq)
5109{
5110 if (txq->next_desc_to_proc == 0)
5111 txq->next_desc_to_proc = txq->last_desc - 1;
5112 else
5113 txq->next_desc_to_proc--;
5114}
5115
5116/* Set Tx descriptors fields relevant for CSUM calculation */
5117static u32 mvpp2_txq_desc_csum(int l3_offs, int l3_proto,
5118 int ip_hdr_len, int l4_proto)
5119{
5120 u32 command;
5121
5122 /* fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
5123 * G_L4_chk, L4_type required only for checksum calculation
5124 */
5125 command = (l3_offs << MVPP2_TXD_L3_OFF_SHIFT);
5126 command |= (ip_hdr_len << MVPP2_TXD_IP_HLEN_SHIFT);
5127 command |= MVPP2_TXD_IP_CSUM_DISABLE;
5128
5129 if (l3_proto == swab16(ETH_P_IP)) {
5130 command &= ~MVPP2_TXD_IP_CSUM_DISABLE; /* enable IPv4 csum */
5131 command &= ~MVPP2_TXD_L3_IP6; /* enable IPv4 */
5132 } else {
5133 command |= MVPP2_TXD_L3_IP6; /* enable IPv6 */
5134 }
5135
5136 if (l4_proto == IPPROTO_TCP) {
5137 command &= ~MVPP2_TXD_L4_UDP; /* enable TCP */
5138 command &= ~MVPP2_TXD_L4_CSUM_FRAG; /* generate L4 csum */
5139 } else if (l4_proto == IPPROTO_UDP) {
5140 command |= MVPP2_TXD_L4_UDP; /* enable UDP */
5141 command &= ~MVPP2_TXD_L4_CSUM_FRAG; /* generate L4 csum */
5142 } else {
5143 command |= MVPP2_TXD_L4_CSUM_NOT;
5144 }
5145
5146 return command;
5147}
5148
5149/* Get number of sent descriptors and decrement counter.
5150 * The number of sent descriptors is returned.
5151 * Per-CPU access
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005152 *
5153 * Called only from mvpp2_txq_done(), called from mvpp2_tx()
5154 * (migration disabled) and from the TX completion tasklet (migration
5155 * disabled) so using smp_processor_id() is OK.
Marcin Wojtas3f518502014-07-10 16:52:13 -03005156 */
5157static inline int mvpp2_txq_sent_desc_proc(struct mvpp2_port *port,
5158 struct mvpp2_tx_queue *txq)
5159{
5160 u32 val;
5161
5162 /* Reading status reg resets transmitted descriptor counter */
Thomas Petazzonia7868412017-03-07 16:53:13 +01005163 val = mvpp2_percpu_read(port->priv, smp_processor_id(),
5164 MVPP2_TXQ_SENT_REG(txq->id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005165
5166 return (val & MVPP2_TRANSMITTED_COUNT_MASK) >>
5167 MVPP2_TRANSMITTED_COUNT_OFFSET;
5168}
5169
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005170/* Called through on_each_cpu(), so runs on all CPUs, with migration
5171 * disabled, therefore using smp_processor_id() is OK.
5172 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03005173static void mvpp2_txq_sent_counter_clear(void *arg)
5174{
5175 struct mvpp2_port *port = arg;
5176 int queue;
5177
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005178 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005179 int id = port->txqs[queue]->id;
5180
Thomas Petazzonia7868412017-03-07 16:53:13 +01005181 mvpp2_percpu_read(port->priv, smp_processor_id(),
5182 MVPP2_TXQ_SENT_REG(id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005183 }
5184}
5185
5186/* Set max sizes for Tx queues */
5187static void mvpp2_txp_max_tx_size_set(struct mvpp2_port *port)
5188{
5189 u32 val, size, mtu;
5190 int txq, tx_port_num;
5191
5192 mtu = port->pkt_size * 8;
5193 if (mtu > MVPP2_TXP_MTU_MAX)
5194 mtu = MVPP2_TXP_MTU_MAX;
5195
5196 /* WA for wrong Token bucket update: Set MTU value = 3*real MTU value */
5197 mtu = 3 * mtu;
5198
5199 /* Indirect access to registers */
5200 tx_port_num = mvpp2_egress_port(port);
5201 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
5202
5203 /* Set MTU */
5204 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_MTU_REG);
5205 val &= ~MVPP2_TXP_MTU_MAX;
5206 val |= mtu;
5207 mvpp2_write(port->priv, MVPP2_TXP_SCHED_MTU_REG, val);
5208
5209 /* TXP token size and all TXQs token size must be larger that MTU */
5210 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG);
5211 size = val & MVPP2_TXP_TOKEN_SIZE_MAX;
5212 if (size < mtu) {
5213 size = mtu;
5214 val &= ~MVPP2_TXP_TOKEN_SIZE_MAX;
5215 val |= size;
5216 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
5217 }
5218
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005219 for (txq = 0; txq < port->ntxqs; txq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005220 val = mvpp2_read(port->priv,
5221 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq));
5222 size = val & MVPP2_TXQ_TOKEN_SIZE_MAX;
5223
5224 if (size < mtu) {
5225 size = mtu;
5226 val &= ~MVPP2_TXQ_TOKEN_SIZE_MAX;
5227 val |= size;
5228 mvpp2_write(port->priv,
5229 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq),
5230 val);
5231 }
5232 }
5233}
5234
5235/* Set the number of packets that will be received before Rx interrupt
5236 * will be generated by HW.
5237 */
5238static void mvpp2_rx_pkts_coal_set(struct mvpp2_port *port,
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01005239 struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005240{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005241 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005242
Thomas Petazzonif8b0d5f2017-02-21 11:28:03 +01005243 if (rxq->pkts_coal > MVPP2_OCCUPIED_THRESH_MASK)
5244 rxq->pkts_coal = MVPP2_OCCUPIED_THRESH_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005245
Thomas Petazzonia7868412017-03-07 16:53:13 +01005246 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
5247 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_THRESH_REG,
5248 rxq->pkts_coal);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005249
5250 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005251}
5252
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005253/* For some reason in the LSP this is done on each CPU. Why ? */
5254static void mvpp2_tx_pkts_coal_set(struct mvpp2_port *port,
5255 struct mvpp2_tx_queue *txq)
5256{
5257 int cpu = get_cpu();
5258 u32 val;
5259
5260 if (txq->done_pkts_coal > MVPP2_TXQ_THRESH_MASK)
5261 txq->done_pkts_coal = MVPP2_TXQ_THRESH_MASK;
5262
5263 val = (txq->done_pkts_coal << MVPP2_TXQ_THRESH_OFFSET);
5264 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5265 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_THRESH_REG, val);
5266
5267 put_cpu();
5268}
5269
Thomas Petazzoniab426762017-02-21 11:28:04 +01005270static u32 mvpp2_usec_to_cycles(u32 usec, unsigned long clk_hz)
5271{
5272 u64 tmp = (u64)clk_hz * usec;
5273
5274 do_div(tmp, USEC_PER_SEC);
5275
5276 return tmp > U32_MAX ? U32_MAX : tmp;
5277}
5278
5279static u32 mvpp2_cycles_to_usec(u32 cycles, unsigned long clk_hz)
5280{
5281 u64 tmp = (u64)cycles * USEC_PER_SEC;
5282
5283 do_div(tmp, clk_hz);
5284
5285 return tmp > U32_MAX ? U32_MAX : tmp;
5286}
5287
Marcin Wojtas3f518502014-07-10 16:52:13 -03005288/* Set the time delay in usec before Rx interrupt */
5289static void mvpp2_rx_time_coal_set(struct mvpp2_port *port,
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01005290 struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005291{
Thomas Petazzoniab426762017-02-21 11:28:04 +01005292 unsigned long freq = port->priv->tclk;
5293 u32 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005294
Thomas Petazzoniab426762017-02-21 11:28:04 +01005295 if (val > MVPP2_MAX_ISR_RX_THRESHOLD) {
5296 rxq->time_coal =
5297 mvpp2_cycles_to_usec(MVPP2_MAX_ISR_RX_THRESHOLD, freq);
5298
5299 /* re-evaluate to get actual register value */
5300 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
5301 }
5302
Marcin Wojtas3f518502014-07-10 16:52:13 -03005303 mvpp2_write(port->priv, MVPP2_ISR_RX_THRESHOLD_REG(rxq->id), val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005304}
5305
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005306static void mvpp2_tx_time_coal_set(struct mvpp2_port *port)
5307{
5308 unsigned long freq = port->priv->tclk;
5309 u32 val = mvpp2_usec_to_cycles(port->tx_time_coal, freq);
5310
5311 if (val > MVPP2_MAX_ISR_TX_THRESHOLD) {
5312 port->tx_time_coal =
5313 mvpp2_cycles_to_usec(MVPP2_MAX_ISR_TX_THRESHOLD, freq);
5314
5315 /* re-evaluate to get actual register value */
5316 val = mvpp2_usec_to_cycles(port->tx_time_coal, freq);
5317 }
5318
5319 mvpp2_write(port->priv, MVPP2_ISR_TX_THRESHOLD_REG(port->id), val);
5320}
5321
Marcin Wojtas3f518502014-07-10 16:52:13 -03005322/* Free Tx queue skbuffs */
5323static void mvpp2_txq_bufs_free(struct mvpp2_port *port,
5324 struct mvpp2_tx_queue *txq,
5325 struct mvpp2_txq_pcpu *txq_pcpu, int num)
5326{
5327 int i;
5328
5329 for (i = 0; i < num; i++) {
Thomas Petazzoni83544912016-12-21 11:28:49 +01005330 struct mvpp2_txq_pcpu_buf *tx_buf =
5331 txq_pcpu->buffs + txq_pcpu->txq_get_index;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005332
Antoine Tenart20920262017-10-23 15:24:30 +02005333 if (!IS_TSO_HEADER(txq_pcpu, tx_buf->dma))
5334 dma_unmap_single(port->dev->dev.parent, tx_buf->dma,
5335 tx_buf->size, DMA_TO_DEVICE);
Thomas Petazzoni36fb7432017-02-21 11:28:05 +01005336 if (tx_buf->skb)
5337 dev_kfree_skb_any(tx_buf->skb);
5338
5339 mvpp2_txq_inc_get(txq_pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005340 }
5341}
5342
5343static inline struct mvpp2_rx_queue *mvpp2_get_rx_queue(struct mvpp2_port *port,
5344 u32 cause)
5345{
5346 int queue = fls(cause) - 1;
5347
5348 return port->rxqs[queue];
5349}
5350
5351static inline struct mvpp2_tx_queue *mvpp2_get_tx_queue(struct mvpp2_port *port,
5352 u32 cause)
5353{
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005354 int queue = fls(cause) - 1;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005355
5356 return port->txqs[queue];
5357}
5358
5359/* Handle end of transmission */
5360static void mvpp2_txq_done(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
5361 struct mvpp2_txq_pcpu *txq_pcpu)
5362{
5363 struct netdev_queue *nq = netdev_get_tx_queue(port->dev, txq->log_id);
5364 int tx_done;
5365
5366 if (txq_pcpu->cpu != smp_processor_id())
5367 netdev_err(port->dev, "wrong cpu on the end of Tx processing\n");
5368
5369 tx_done = mvpp2_txq_sent_desc_proc(port, txq);
5370 if (!tx_done)
5371 return;
5372 mvpp2_txq_bufs_free(port, txq, txq_pcpu, tx_done);
5373
5374 txq_pcpu->count -= tx_done;
5375
5376 if (netif_tx_queue_stopped(nq))
5377 if (txq_pcpu->size - txq_pcpu->count >= MAX_SKB_FRAGS + 1)
5378 netif_tx_wake_queue(nq);
5379}
5380
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005381static unsigned int mvpp2_tx_done(struct mvpp2_port *port, u32 cause,
5382 int cpu)
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005383{
5384 struct mvpp2_tx_queue *txq;
5385 struct mvpp2_txq_pcpu *txq_pcpu;
5386 unsigned int tx_todo = 0;
5387
5388 while (cause) {
5389 txq = mvpp2_get_tx_queue(port, cause);
5390 if (!txq)
5391 break;
5392
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005393 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005394
5395 if (txq_pcpu->count) {
5396 mvpp2_txq_done(port, txq, txq_pcpu);
5397 tx_todo += txq_pcpu->count;
5398 }
5399
5400 cause &= ~(1 << txq->log_id);
5401 }
5402 return tx_todo;
5403}
5404
Marcin Wojtas3f518502014-07-10 16:52:13 -03005405/* Rx/Tx queue initialization/cleanup methods */
5406
5407/* Allocate and initialize descriptors for aggr TXQ */
5408static int mvpp2_aggr_txq_init(struct platform_device *pdev,
Antoine Ténart85affd72017-08-23 09:46:55 +02005409 struct mvpp2_tx_queue *aggr_txq, int cpu,
Marcin Wojtas3f518502014-07-10 16:52:13 -03005410 struct mvpp2 *priv)
5411{
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005412 u32 txq_dma;
5413
Marcin Wojtas3f518502014-07-10 16:52:13 -03005414 /* Allocate memory for TX descriptors */
5415 aggr_txq->descs = dma_alloc_coherent(&pdev->dev,
Antoine Ténart85affd72017-08-23 09:46:55 +02005416 MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005417 &aggr_txq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005418 if (!aggr_txq->descs)
5419 return -ENOMEM;
5420
Marcin Wojtas3f518502014-07-10 16:52:13 -03005421 aggr_txq->last_desc = aggr_txq->size - 1;
5422
5423 /* Aggr TXQ no reset WA */
5424 aggr_txq->next_desc_to_proc = mvpp2_read(priv,
5425 MVPP2_AGGR_TXQ_INDEX_REG(cpu));
5426
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005427 /* Set Tx descriptors queue starting address indirect
5428 * access
5429 */
5430 if (priv->hw_version == MVPP21)
5431 txq_dma = aggr_txq->descs_dma;
5432 else
5433 txq_dma = aggr_txq->descs_dma >>
5434 MVPP22_AGGR_TXQ_DESC_ADDR_OFFS;
5435
5436 mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu), txq_dma);
Antoine Ténart85affd72017-08-23 09:46:55 +02005437 mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu),
5438 MVPP2_AGGR_TXQ_SIZE);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005439
5440 return 0;
5441}
5442
5443/* Create a specified Rx queue */
5444static int mvpp2_rxq_init(struct mvpp2_port *port,
5445 struct mvpp2_rx_queue *rxq)
5446
5447{
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005448 u32 rxq_dma;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005449 int cpu;
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005450
Marcin Wojtas3f518502014-07-10 16:52:13 -03005451 rxq->size = port->rx_ring_size;
5452
5453 /* Allocate memory for RX descriptors */
5454 rxq->descs = dma_alloc_coherent(port->dev->dev.parent,
5455 rxq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005456 &rxq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005457 if (!rxq->descs)
5458 return -ENOMEM;
5459
Marcin Wojtas3f518502014-07-10 16:52:13 -03005460 rxq->last_desc = rxq->size - 1;
5461
5462 /* Zero occupied and non-occupied counters - direct access */
5463 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
5464
5465 /* Set Rx descriptors queue starting address - indirect access */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005466 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005467 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005468 if (port->priv->hw_version == MVPP21)
5469 rxq_dma = rxq->descs_dma;
5470 else
5471 rxq_dma = rxq->descs_dma >> MVPP22_DESC_ADDR_OFFS;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005472 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_ADDR_REG, rxq_dma);
5473 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_SIZE_REG, rxq->size);
5474 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_INDEX_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005475 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005476
5477 /* Set Offset */
5478 mvpp2_rxq_offset_set(port, rxq->id, NET_SKB_PAD);
5479
5480 /* Set coalescing pkts and time */
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01005481 mvpp2_rx_pkts_coal_set(port, rxq);
5482 mvpp2_rx_time_coal_set(port, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005483
5484 /* Add number of descriptors ready for receiving packets */
5485 mvpp2_rxq_status_update(port, rxq->id, 0, rxq->size);
5486
5487 return 0;
5488}
5489
5490/* Push packets received by the RXQ to BM pool */
5491static void mvpp2_rxq_drop_pkts(struct mvpp2_port *port,
5492 struct mvpp2_rx_queue *rxq)
5493{
5494 int rx_received, i;
5495
5496 rx_received = mvpp2_rxq_received(port, rxq->id);
5497 if (!rx_received)
5498 return;
5499
5500 for (i = 0; i < rx_received; i++) {
5501 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005502 u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
5503 int pool;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005504
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005505 pool = (status & MVPP2_RXD_BM_POOL_ID_MASK) >>
5506 MVPP2_RXD_BM_POOL_ID_OFFS;
5507
Thomas Petazzoni7d7627b2017-06-22 14:23:20 +02005508 mvpp2_bm_pool_put(port, pool,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005509 mvpp2_rxdesc_dma_addr_get(port, rx_desc),
5510 mvpp2_rxdesc_cookie_get(port, rx_desc));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005511 }
5512 mvpp2_rxq_status_update(port, rxq->id, rx_received, rx_received);
5513}
5514
5515/* Cleanup Rx queue */
5516static void mvpp2_rxq_deinit(struct mvpp2_port *port,
5517 struct mvpp2_rx_queue *rxq)
5518{
Thomas Petazzonia7868412017-03-07 16:53:13 +01005519 int cpu;
5520
Marcin Wojtas3f518502014-07-10 16:52:13 -03005521 mvpp2_rxq_drop_pkts(port, rxq);
5522
5523 if (rxq->descs)
5524 dma_free_coherent(port->dev->dev.parent,
5525 rxq->size * MVPP2_DESC_ALIGNED_SIZE,
5526 rxq->descs,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005527 rxq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005528
5529 rxq->descs = NULL;
5530 rxq->last_desc = 0;
5531 rxq->next_desc_to_proc = 0;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005532 rxq->descs_dma = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005533
5534 /* Clear Rx descriptors queue starting address and size;
5535 * free descriptor number
5536 */
5537 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005538 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005539 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
5540 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_ADDR_REG, 0);
5541 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_SIZE_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005542 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005543}
5544
5545/* Create and initialize a Tx queue */
5546static int mvpp2_txq_init(struct mvpp2_port *port,
5547 struct mvpp2_tx_queue *txq)
5548{
5549 u32 val;
5550 int cpu, desc, desc_per_txq, tx_port_num;
5551 struct mvpp2_txq_pcpu *txq_pcpu;
5552
5553 txq->size = port->tx_ring_size;
5554
5555 /* Allocate memory for Tx descriptors */
5556 txq->descs = dma_alloc_coherent(port->dev->dev.parent,
5557 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005558 &txq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005559 if (!txq->descs)
5560 return -ENOMEM;
5561
Marcin Wojtas3f518502014-07-10 16:52:13 -03005562 txq->last_desc = txq->size - 1;
5563
5564 /* Set Tx descriptors queue starting address - indirect access */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005565 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005566 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5567 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_ADDR_REG,
5568 txq->descs_dma);
5569 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_SIZE_REG,
5570 txq->size & MVPP2_TXQ_DESC_SIZE_MASK);
5571 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_INDEX_REG, 0);
5572 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_RSVD_CLR_REG,
5573 txq->id << MVPP2_TXQ_RSVD_CLR_OFFSET);
5574 val = mvpp2_percpu_read(port->priv, cpu, MVPP2_TXQ_PENDING_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005575 val &= ~MVPP2_TXQ_PENDING_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005576 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PENDING_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005577
5578 /* Calculate base address in prefetch buffer. We reserve 16 descriptors
5579 * for each existing TXQ.
5580 * TCONTS for PON port must be continuous from 0 to MVPP2_MAX_TCONT
5581 * GBE ports assumed to be continious from 0 to MVPP2_MAX_PORTS
5582 */
5583 desc_per_txq = 16;
5584 desc = (port->id * MVPP2_MAX_TXQ * desc_per_txq) +
5585 (txq->log_id * desc_per_txq);
5586
Thomas Petazzonia7868412017-03-07 16:53:13 +01005587 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG,
5588 MVPP2_PREF_BUF_PTR(desc) | MVPP2_PREF_BUF_SIZE_16 |
5589 MVPP2_PREF_BUF_THRESH(desc_per_txq / 2));
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005590 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005591
5592 /* WRR / EJP configuration - indirect access */
5593 tx_port_num = mvpp2_egress_port(port);
5594 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
5595
5596 val = mvpp2_read(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id));
5597 val &= ~MVPP2_TXQ_REFILL_PERIOD_ALL_MASK;
5598 val |= MVPP2_TXQ_REFILL_PERIOD_MASK(1);
5599 val |= MVPP2_TXQ_REFILL_TOKENS_ALL_MASK;
5600 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id), val);
5601
5602 val = MVPP2_TXQ_TOKEN_SIZE_MAX;
5603 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq->log_id),
5604 val);
5605
5606 for_each_present_cpu(cpu) {
5607 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
5608 txq_pcpu->size = txq->size;
Markus Elfring02c91ec2017-04-17 08:09:07 +02005609 txq_pcpu->buffs = kmalloc_array(txq_pcpu->size,
5610 sizeof(*txq_pcpu->buffs),
5611 GFP_KERNEL);
Thomas Petazzoni83544912016-12-21 11:28:49 +01005612 if (!txq_pcpu->buffs)
Markus Elfring20b1e162017-04-17 12:58:33 +02005613 goto cleanup;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005614
5615 txq_pcpu->count = 0;
5616 txq_pcpu->reserved_num = 0;
5617 txq_pcpu->txq_put_index = 0;
5618 txq_pcpu->txq_get_index = 0;
Antoine Ténart186cd4d2017-08-23 09:46:56 +02005619
5620 txq_pcpu->tso_headers =
5621 dma_alloc_coherent(port->dev->dev.parent,
Yan Markman822eaf72017-10-23 15:24:29 +02005622 txq_pcpu->size * TSO_HEADER_SIZE,
Antoine Ténart186cd4d2017-08-23 09:46:56 +02005623 &txq_pcpu->tso_headers_dma,
5624 GFP_KERNEL);
5625 if (!txq_pcpu->tso_headers)
5626 goto cleanup;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005627 }
5628
5629 return 0;
Markus Elfring20b1e162017-04-17 12:58:33 +02005630cleanup:
Marcin Wojtas71ce3912015-08-06 19:00:29 +02005631 for_each_present_cpu(cpu) {
5632 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Thomas Petazzoni83544912016-12-21 11:28:49 +01005633 kfree(txq_pcpu->buffs);
Antoine Ténart186cd4d2017-08-23 09:46:56 +02005634
5635 dma_free_coherent(port->dev->dev.parent,
Yan Markman822eaf72017-10-23 15:24:29 +02005636 txq_pcpu->size * TSO_HEADER_SIZE,
Antoine Ténart186cd4d2017-08-23 09:46:56 +02005637 txq_pcpu->tso_headers,
5638 txq_pcpu->tso_headers_dma);
Marcin Wojtas71ce3912015-08-06 19:00:29 +02005639 }
5640
5641 dma_free_coherent(port->dev->dev.parent,
5642 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005643 txq->descs, txq->descs_dma);
Marcin Wojtas71ce3912015-08-06 19:00:29 +02005644
5645 return -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005646}
5647
5648/* Free allocated TXQ resources */
5649static void mvpp2_txq_deinit(struct mvpp2_port *port,
5650 struct mvpp2_tx_queue *txq)
5651{
5652 struct mvpp2_txq_pcpu *txq_pcpu;
5653 int cpu;
5654
5655 for_each_present_cpu(cpu) {
5656 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Thomas Petazzoni83544912016-12-21 11:28:49 +01005657 kfree(txq_pcpu->buffs);
Antoine Ténart186cd4d2017-08-23 09:46:56 +02005658
5659 dma_free_coherent(port->dev->dev.parent,
Yan Markman822eaf72017-10-23 15:24:29 +02005660 txq_pcpu->size * TSO_HEADER_SIZE,
Antoine Ténart186cd4d2017-08-23 09:46:56 +02005661 txq_pcpu->tso_headers,
5662 txq_pcpu->tso_headers_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005663 }
5664
5665 if (txq->descs)
5666 dma_free_coherent(port->dev->dev.parent,
5667 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005668 txq->descs, txq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005669
5670 txq->descs = NULL;
5671 txq->last_desc = 0;
5672 txq->next_desc_to_proc = 0;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005673 txq->descs_dma = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005674
5675 /* Set minimum bandwidth for disabled TXQs */
5676 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(txq->id), 0);
5677
5678 /* Set Tx descriptors queue starting address and size */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005679 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005680 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5681 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_ADDR_REG, 0);
5682 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_SIZE_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005683 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005684}
5685
5686/* Cleanup Tx ports */
5687static void mvpp2_txq_clean(struct mvpp2_port *port, struct mvpp2_tx_queue *txq)
5688{
5689 struct mvpp2_txq_pcpu *txq_pcpu;
5690 int delay, pending, cpu;
5691 u32 val;
5692
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005693 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005694 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5695 val = mvpp2_percpu_read(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005696 val |= MVPP2_TXQ_DRAIN_EN_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005697 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005698
5699 /* The napi queue has been stopped so wait for all packets
5700 * to be transmitted.
5701 */
5702 delay = 0;
5703 do {
5704 if (delay >= MVPP2_TX_PENDING_TIMEOUT_MSEC) {
5705 netdev_warn(port->dev,
5706 "port %d: cleaning queue %d timed out\n",
5707 port->id, txq->log_id);
5708 break;
5709 }
5710 mdelay(1);
5711 delay++;
5712
Thomas Petazzonia7868412017-03-07 16:53:13 +01005713 pending = mvpp2_percpu_read(port->priv, cpu,
5714 MVPP2_TXQ_PENDING_REG);
5715 pending &= MVPP2_TXQ_PENDING_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005716 } while (pending);
5717
5718 val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005719 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG, val);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005720 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005721
5722 for_each_present_cpu(cpu) {
5723 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
5724
5725 /* Release all packets */
5726 mvpp2_txq_bufs_free(port, txq, txq_pcpu, txq_pcpu->count);
5727
5728 /* Reset queue */
5729 txq_pcpu->count = 0;
5730 txq_pcpu->txq_put_index = 0;
5731 txq_pcpu->txq_get_index = 0;
5732 }
5733}
5734
5735/* Cleanup all Tx queues */
5736static void mvpp2_cleanup_txqs(struct mvpp2_port *port)
5737{
5738 struct mvpp2_tx_queue *txq;
5739 int queue;
5740 u32 val;
5741
5742 val = mvpp2_read(port->priv, MVPP2_TX_PORT_FLUSH_REG);
5743
5744 /* Reset Tx ports and delete Tx queues */
5745 val |= MVPP2_TX_PORT_FLUSH_MASK(port->id);
5746 mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
5747
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005748 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005749 txq = port->txqs[queue];
5750 mvpp2_txq_clean(port, txq);
5751 mvpp2_txq_deinit(port, txq);
5752 }
5753
5754 on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
5755
5756 val &= ~MVPP2_TX_PORT_FLUSH_MASK(port->id);
5757 mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
5758}
5759
5760/* Cleanup all Rx queues */
5761static void mvpp2_cleanup_rxqs(struct mvpp2_port *port)
5762{
5763 int queue;
5764
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005765 for (queue = 0; queue < port->nrxqs; queue++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005766 mvpp2_rxq_deinit(port, port->rxqs[queue]);
5767}
5768
5769/* Init all Rx queues for port */
5770static int mvpp2_setup_rxqs(struct mvpp2_port *port)
5771{
5772 int queue, err;
5773
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005774 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005775 err = mvpp2_rxq_init(port, port->rxqs[queue]);
5776 if (err)
5777 goto err_cleanup;
5778 }
5779 return 0;
5780
5781err_cleanup:
5782 mvpp2_cleanup_rxqs(port);
5783 return err;
5784}
5785
5786/* Init all tx queues for port */
5787static int mvpp2_setup_txqs(struct mvpp2_port *port)
5788{
5789 struct mvpp2_tx_queue *txq;
5790 int queue, err;
5791
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005792 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005793 txq = port->txqs[queue];
5794 err = mvpp2_txq_init(port, txq);
5795 if (err)
5796 goto err_cleanup;
5797 }
5798
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005799 if (port->has_tx_irqs) {
5800 mvpp2_tx_time_coal_set(port);
5801 for (queue = 0; queue < port->ntxqs; queue++) {
5802 txq = port->txqs[queue];
5803 mvpp2_tx_pkts_coal_set(port, txq);
5804 }
5805 }
5806
Marcin Wojtas3f518502014-07-10 16:52:13 -03005807 on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
5808 return 0;
5809
5810err_cleanup:
5811 mvpp2_cleanup_txqs(port);
5812 return err;
5813}
5814
5815/* The callback for per-port interrupt */
5816static irqreturn_t mvpp2_isr(int irq, void *dev_id)
5817{
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02005818 struct mvpp2_queue_vector *qv = dev_id;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005819
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02005820 mvpp2_qvec_interrupt_disable(qv);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005821
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02005822 napi_schedule(&qv->napi);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005823
5824 return IRQ_HANDLED;
5825}
5826
Antoine Tenartfd3651b2017-09-01 11:04:54 +02005827/* Per-port interrupt for link status changes */
5828static irqreturn_t mvpp2_link_status_isr(int irq, void *dev_id)
5829{
5830 struct mvpp2_port *port = (struct mvpp2_port *)dev_id;
5831 struct net_device *dev = port->dev;
5832 bool event = false, link = false;
5833 u32 val;
5834
5835 mvpp22_gop_mask_irq(port);
5836
5837 if (port->gop_id == 0 &&
5838 port->phy_interface == PHY_INTERFACE_MODE_10GKR) {
5839 val = readl(port->base + MVPP22_XLG_INT_STAT);
5840 if (val & MVPP22_XLG_INT_STAT_LINK) {
5841 event = true;
5842 val = readl(port->base + MVPP22_XLG_STATUS);
5843 if (val & MVPP22_XLG_STATUS_LINK_UP)
5844 link = true;
5845 }
5846 } else if (phy_interface_mode_is_rgmii(port->phy_interface) ||
5847 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
5848 val = readl(port->base + MVPP22_GMAC_INT_STAT);
5849 if (val & MVPP22_GMAC_INT_STAT_LINK) {
5850 event = true;
5851 val = readl(port->base + MVPP2_GMAC_STATUS0);
5852 if (val & MVPP2_GMAC_STATUS0_LINK_UP)
5853 link = true;
5854 }
5855 }
5856
5857 if (!netif_running(dev) || !event)
5858 goto handled;
5859
5860 if (link) {
5861 mvpp2_interrupts_enable(port);
5862
5863 mvpp2_egress_enable(port);
5864 mvpp2_ingress_enable(port);
5865 netif_carrier_on(dev);
5866 netif_tx_wake_all_queues(dev);
5867 } else {
5868 netif_tx_stop_all_queues(dev);
5869 netif_carrier_off(dev);
5870 mvpp2_ingress_disable(port);
5871 mvpp2_egress_disable(port);
5872
5873 mvpp2_interrupts_disable(port);
5874 }
5875
5876handled:
5877 mvpp22_gop_unmask_irq(port);
5878 return IRQ_HANDLED;
5879}
5880
Antoine Tenart65a2c092017-08-30 10:29:18 +02005881static void mvpp2_gmac_set_autoneg(struct mvpp2_port *port,
5882 struct phy_device *phydev)
5883{
5884 u32 val;
5885
5886 if (port->phy_interface != PHY_INTERFACE_MODE_RGMII &&
5887 port->phy_interface != PHY_INTERFACE_MODE_RGMII_ID &&
5888 port->phy_interface != PHY_INTERFACE_MODE_RGMII_RXID &&
5889 port->phy_interface != PHY_INTERFACE_MODE_RGMII_TXID &&
5890 port->phy_interface != PHY_INTERFACE_MODE_SGMII)
5891 return;
5892
5893 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5894 val &= ~(MVPP2_GMAC_CONFIG_MII_SPEED |
5895 MVPP2_GMAC_CONFIG_GMII_SPEED |
5896 MVPP2_GMAC_CONFIG_FULL_DUPLEX |
5897 MVPP2_GMAC_AN_SPEED_EN |
5898 MVPP2_GMAC_AN_DUPLEX_EN);
5899
5900 if (phydev->duplex)
5901 val |= MVPP2_GMAC_CONFIG_FULL_DUPLEX;
5902
5903 if (phydev->speed == SPEED_1000)
5904 val |= MVPP2_GMAC_CONFIG_GMII_SPEED;
5905 else if (phydev->speed == SPEED_100)
5906 val |= MVPP2_GMAC_CONFIG_MII_SPEED;
5907
5908 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
Antoine Tenart65a2c092017-08-30 10:29:18 +02005909}
5910
Marcin Wojtas3f518502014-07-10 16:52:13 -03005911/* Adjust link */
5912static void mvpp2_link_event(struct net_device *dev)
5913{
5914 struct mvpp2_port *port = netdev_priv(dev);
Philippe Reynes8e072692016-06-28 00:08:11 +02005915 struct phy_device *phydev = dev->phydev;
Antoine Tenart89273bc2017-08-30 10:29:19 +02005916 bool link_reconfigured = false;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005917 u32 val;
5918
5919 if (phydev->link) {
Antoine Tenart89273bc2017-08-30 10:29:19 +02005920 if (port->phy_interface != phydev->interface && port->comphy) {
5921 /* disable current port for reconfiguration */
5922 mvpp2_interrupts_disable(port);
5923 netif_carrier_off(port->dev);
5924 mvpp2_port_disable(port);
5925 phy_power_off(port->comphy);
5926
5927 /* comphy reconfiguration */
5928 port->phy_interface = phydev->interface;
5929 mvpp22_comphy_init(port);
5930
5931 /* gop/mac reconfiguration */
5932 mvpp22_gop_init(port);
5933 mvpp2_port_mii_set(port);
5934
5935 link_reconfigured = true;
5936 }
5937
Marcin Wojtas3f518502014-07-10 16:52:13 -03005938 if ((port->speed != phydev->speed) ||
5939 (port->duplex != phydev->duplex)) {
Antoine Tenart65a2c092017-08-30 10:29:18 +02005940 mvpp2_gmac_set_autoneg(port, phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005941
5942 port->duplex = phydev->duplex;
5943 port->speed = phydev->speed;
5944 }
5945 }
5946
Antoine Tenart89273bc2017-08-30 10:29:19 +02005947 if (phydev->link != port->link || link_reconfigured) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005948 port->link = phydev->link;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005949
Marcin Wojtas3f518502014-07-10 16:52:13 -03005950 if (phydev->link) {
Antoine Tenart65a2c092017-08-30 10:29:18 +02005951 if (port->phy_interface == PHY_INTERFACE_MODE_RGMII ||
5952 port->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
5953 port->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID ||
5954 port->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID ||
5955 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
5956 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5957 val |= (MVPP2_GMAC_FORCE_LINK_PASS |
5958 MVPP2_GMAC_FORCE_LINK_DOWN);
5959 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5960 }
Antoine Tenartf55744a2017-08-30 10:29:17 +02005961
5962 mvpp2_interrupts_enable(port);
5963 mvpp2_port_enable(port);
5964
Marcin Wojtas3f518502014-07-10 16:52:13 -03005965 mvpp2_egress_enable(port);
5966 mvpp2_ingress_enable(port);
Antoine Tenartf55744a2017-08-30 10:29:17 +02005967 netif_carrier_on(dev);
5968 netif_tx_wake_all_queues(dev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005969 } else {
Antoine Tenart968b2112017-08-30 10:29:16 +02005970 port->duplex = -1;
5971 port->speed = 0;
5972
Antoine Tenartf55744a2017-08-30 10:29:17 +02005973 netif_tx_stop_all_queues(dev);
5974 netif_carrier_off(dev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005975 mvpp2_ingress_disable(port);
5976 mvpp2_egress_disable(port);
Antoine Tenartf55744a2017-08-30 10:29:17 +02005977
5978 mvpp2_port_disable(port);
5979 mvpp2_interrupts_disable(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005980 }
Antoine Tenart968b2112017-08-30 10:29:16 +02005981
Marcin Wojtas3f518502014-07-10 16:52:13 -03005982 phy_print_status(phydev);
5983 }
5984}
5985
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005986static void mvpp2_timer_set(struct mvpp2_port_pcpu *port_pcpu)
5987{
5988 ktime_t interval;
5989
5990 if (!port_pcpu->timer_scheduled) {
5991 port_pcpu->timer_scheduled = true;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01005992 interval = MVPP2_TXDONE_HRTIMER_PERIOD_NS;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005993 hrtimer_start(&port_pcpu->tx_done_timer, interval,
5994 HRTIMER_MODE_REL_PINNED);
5995 }
5996}
5997
5998static void mvpp2_tx_proc_cb(unsigned long data)
5999{
6000 struct net_device *dev = (struct net_device *)data;
6001 struct mvpp2_port *port = netdev_priv(dev);
6002 struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
6003 unsigned int tx_todo, cause;
6004
6005 if (!netif_running(dev))
6006 return;
6007 port_pcpu->timer_scheduled = false;
6008
6009 /* Process all the Tx queues */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006010 cause = (1 << port->ntxqs) - 1;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006011 tx_todo = mvpp2_tx_done(port, cause, smp_processor_id());
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006012
6013 /* Set the timer in case not all the packets were processed */
6014 if (tx_todo)
6015 mvpp2_timer_set(port_pcpu);
6016}
6017
6018static enum hrtimer_restart mvpp2_hr_timer_cb(struct hrtimer *timer)
6019{
6020 struct mvpp2_port_pcpu *port_pcpu = container_of(timer,
6021 struct mvpp2_port_pcpu,
6022 tx_done_timer);
6023
6024 tasklet_schedule(&port_pcpu->tx_done_tasklet);
6025
6026 return HRTIMER_NORESTART;
6027}
6028
Marcin Wojtas3f518502014-07-10 16:52:13 -03006029/* Main RX/TX processing routines */
6030
6031/* Display more error info */
6032static void mvpp2_rx_error(struct mvpp2_port *port,
6033 struct mvpp2_rx_desc *rx_desc)
6034{
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006035 u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
6036 size_t sz = mvpp2_rxdesc_size_get(port, rx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006037
6038 switch (status & MVPP2_RXD_ERR_CODE_MASK) {
6039 case MVPP2_RXD_ERR_CRC:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006040 netdev_err(port->dev, "bad rx status %08x (crc error), size=%zu\n",
6041 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006042 break;
6043 case MVPP2_RXD_ERR_OVERRUN:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006044 netdev_err(port->dev, "bad rx status %08x (overrun error), size=%zu\n",
6045 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006046 break;
6047 case MVPP2_RXD_ERR_RESOURCE:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006048 netdev_err(port->dev, "bad rx status %08x (resource error), size=%zu\n",
6049 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006050 break;
6051 }
6052}
6053
6054/* Handle RX checksum offload */
6055static void mvpp2_rx_csum(struct mvpp2_port *port, u32 status,
6056 struct sk_buff *skb)
6057{
6058 if (((status & MVPP2_RXD_L3_IP4) &&
6059 !(status & MVPP2_RXD_IP4_HEADER_ERR)) ||
6060 (status & MVPP2_RXD_L3_IP6))
6061 if (((status & MVPP2_RXD_L4_UDP) ||
6062 (status & MVPP2_RXD_L4_TCP)) &&
6063 (status & MVPP2_RXD_L4_CSUM_OK)) {
6064 skb->csum = 0;
6065 skb->ip_summed = CHECKSUM_UNNECESSARY;
6066 return;
6067 }
6068
6069 skb->ip_summed = CHECKSUM_NONE;
6070}
6071
6072/* Reuse skb if possible, or allocate a new skb and add it to BM pool */
6073static int mvpp2_rx_refill(struct mvpp2_port *port,
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006074 struct mvpp2_bm_pool *bm_pool, int pool)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006075{
Thomas Petazzoni20396132017-03-07 16:53:00 +01006076 dma_addr_t dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01006077 phys_addr_t phys_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006078 void *buf;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006079
Marcin Wojtas3f518502014-07-10 16:52:13 -03006080 /* No recycle or too many buffers are in use, so allocate a new skb */
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01006081 buf = mvpp2_buf_alloc(port, bm_pool, &dma_addr, &phys_addr,
6082 GFP_ATOMIC);
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006083 if (!buf)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006084 return -ENOMEM;
6085
Thomas Petazzoni7d7627b2017-06-22 14:23:20 +02006086 mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
Thomas Petazzoni7ef7e1d2017-02-21 11:28:07 +01006087
Marcin Wojtas3f518502014-07-10 16:52:13 -03006088 return 0;
6089}
6090
6091/* Handle tx checksum */
6092static u32 mvpp2_skb_tx_csum(struct mvpp2_port *port, struct sk_buff *skb)
6093{
6094 if (skb->ip_summed == CHECKSUM_PARTIAL) {
6095 int ip_hdr_len = 0;
6096 u8 l4_proto;
6097
6098 if (skb->protocol == htons(ETH_P_IP)) {
6099 struct iphdr *ip4h = ip_hdr(skb);
6100
6101 /* Calculate IPv4 checksum and L4 checksum */
6102 ip_hdr_len = ip4h->ihl;
6103 l4_proto = ip4h->protocol;
6104 } else if (skb->protocol == htons(ETH_P_IPV6)) {
6105 struct ipv6hdr *ip6h = ipv6_hdr(skb);
6106
6107 /* Read l4_protocol from one of IPv6 extra headers */
6108 if (skb_network_header_len(skb) > 0)
6109 ip_hdr_len = (skb_network_header_len(skb) >> 2);
6110 l4_proto = ip6h->nexthdr;
6111 } else {
6112 return MVPP2_TXD_L4_CSUM_NOT;
6113 }
6114
6115 return mvpp2_txq_desc_csum(skb_network_offset(skb),
6116 skb->protocol, ip_hdr_len, l4_proto);
6117 }
6118
6119 return MVPP2_TXD_L4_CSUM_NOT | MVPP2_TXD_IP_CSUM_DISABLE;
6120}
6121
Marcin Wojtas3f518502014-07-10 16:52:13 -03006122/* Main rx processing */
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006123static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi,
6124 int rx_todo, struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006125{
6126 struct net_device *dev = port->dev;
Marcin Wojtasb5015852015-12-03 15:20:51 +01006127 int rx_received;
6128 int rx_done = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006129 u32 rcvd_pkts = 0;
6130 u32 rcvd_bytes = 0;
6131
6132 /* Get number of received packets and clamp the to-do */
6133 rx_received = mvpp2_rxq_received(port, rxq->id);
6134 if (rx_todo > rx_received)
6135 rx_todo = rx_received;
6136
Marcin Wojtasb5015852015-12-03 15:20:51 +01006137 while (rx_done < rx_todo) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006138 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
6139 struct mvpp2_bm_pool *bm_pool;
6140 struct sk_buff *skb;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006141 unsigned int frag_size;
Thomas Petazzoni20396132017-03-07 16:53:00 +01006142 dma_addr_t dma_addr;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006143 phys_addr_t phys_addr;
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006144 u32 rx_status;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006145 int pool, rx_bytes, err;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006146 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006147
Marcin Wojtasb5015852015-12-03 15:20:51 +01006148 rx_done++;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006149 rx_status = mvpp2_rxdesc_status_get(port, rx_desc);
6150 rx_bytes = mvpp2_rxdesc_size_get(port, rx_desc);
6151 rx_bytes -= MVPP2_MH_SIZE;
6152 dma_addr = mvpp2_rxdesc_dma_addr_get(port, rx_desc);
6153 phys_addr = mvpp2_rxdesc_cookie_get(port, rx_desc);
6154 data = (void *)phys_to_virt(phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006155
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006156 pool = (rx_status & MVPP2_RXD_BM_POOL_ID_MASK) >>
6157 MVPP2_RXD_BM_POOL_ID_OFFS;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006158 bm_pool = &port->priv->bm_pools[pool];
Marcin Wojtas3f518502014-07-10 16:52:13 -03006159
6160 /* In case of an error, release the requested buffer pointer
6161 * to the Buffer Manager. This request process is controlled
6162 * by the hardware, and the information about the buffer is
6163 * comprised by the RX descriptor.
6164 */
6165 if (rx_status & MVPP2_RXD_ERR_SUMMARY) {
Markus Elfring8a524882017-04-17 10:52:02 +02006166err_drop_frame:
Marcin Wojtas3f518502014-07-10 16:52:13 -03006167 dev->stats.rx_errors++;
6168 mvpp2_rx_error(port, rx_desc);
Marcin Wojtasb5015852015-12-03 15:20:51 +01006169 /* Return the buffer to the pool */
Thomas Petazzoni7d7627b2017-06-22 14:23:20 +02006170 mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006171 continue;
6172 }
6173
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006174 if (bm_pool->frag_size > PAGE_SIZE)
6175 frag_size = 0;
6176 else
6177 frag_size = bm_pool->frag_size;
6178
6179 skb = build_skb(data, frag_size);
6180 if (!skb) {
6181 netdev_warn(port->dev, "skb build failed\n");
6182 goto err_drop_frame;
6183 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006184
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006185 err = mvpp2_rx_refill(port, bm_pool, pool);
Marcin Wojtasb5015852015-12-03 15:20:51 +01006186 if (err) {
6187 netdev_err(port->dev, "failed to refill BM pools\n");
6188 goto err_drop_frame;
6189 }
6190
Thomas Petazzoni20396132017-03-07 16:53:00 +01006191 dma_unmap_single(dev->dev.parent, dma_addr,
Marcin Wojtas4229d502015-12-03 15:20:50 +01006192 bm_pool->buf_size, DMA_FROM_DEVICE);
6193
Marcin Wojtas3f518502014-07-10 16:52:13 -03006194 rcvd_pkts++;
6195 rcvd_bytes += rx_bytes;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006196
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006197 skb_reserve(skb, MVPP2_MH_SIZE + NET_SKB_PAD);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006198 skb_put(skb, rx_bytes);
6199 skb->protocol = eth_type_trans(skb, dev);
6200 mvpp2_rx_csum(port, rx_status, skb);
6201
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006202 napi_gro_receive(napi, skb);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006203 }
6204
6205 if (rcvd_pkts) {
6206 struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
6207
6208 u64_stats_update_begin(&stats->syncp);
6209 stats->rx_packets += rcvd_pkts;
6210 stats->rx_bytes += rcvd_bytes;
6211 u64_stats_update_end(&stats->syncp);
6212 }
6213
6214 /* Update Rx queue management counters */
6215 wmb();
Marcin Wojtasb5015852015-12-03 15:20:51 +01006216 mvpp2_rxq_status_update(port, rxq->id, rx_done, rx_done);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006217
6218 return rx_todo;
6219}
6220
6221static inline void
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006222tx_desc_unmap_put(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006223 struct mvpp2_tx_desc *desc)
6224{
Antoine Tenart20920262017-10-23 15:24:30 +02006225 struct mvpp2_txq_pcpu *txq_pcpu = this_cpu_ptr(txq->pcpu);
6226
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006227 dma_addr_t buf_dma_addr =
6228 mvpp2_txdesc_dma_addr_get(port, desc);
6229 size_t buf_sz =
6230 mvpp2_txdesc_size_get(port, desc);
Antoine Tenart20920262017-10-23 15:24:30 +02006231 if (!IS_TSO_HEADER(txq_pcpu, buf_dma_addr))
6232 dma_unmap_single(port->dev->dev.parent, buf_dma_addr,
6233 buf_sz, DMA_TO_DEVICE);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006234 mvpp2_txq_desc_put(txq);
6235}
6236
6237/* Handle tx fragmentation processing */
6238static int mvpp2_tx_frag_process(struct mvpp2_port *port, struct sk_buff *skb,
6239 struct mvpp2_tx_queue *aggr_txq,
6240 struct mvpp2_tx_queue *txq)
6241{
6242 struct mvpp2_txq_pcpu *txq_pcpu = this_cpu_ptr(txq->pcpu);
6243 struct mvpp2_tx_desc *tx_desc;
6244 int i;
Thomas Petazzoni20396132017-03-07 16:53:00 +01006245 dma_addr_t buf_dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006246
6247 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
6248 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6249 void *addr = page_address(frag->page.p) + frag->page_offset;
6250
6251 tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006252 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6253 mvpp2_txdesc_size_set(port, tx_desc, frag->size);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006254
Thomas Petazzoni20396132017-03-07 16:53:00 +01006255 buf_dma_addr = dma_map_single(port->dev->dev.parent, addr,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006256 frag->size,
6257 DMA_TO_DEVICE);
Thomas Petazzoni20396132017-03-07 16:53:00 +01006258 if (dma_mapping_error(port->dev->dev.parent, buf_dma_addr)) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006259 mvpp2_txq_desc_put(txq);
Markus Elfring32bae632017-04-17 11:36:34 +02006260 goto cleanup;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006261 }
6262
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006263 mvpp2_txdesc_offset_set(port, tx_desc,
6264 buf_dma_addr & MVPP2_TX_DESC_ALIGN);
6265 mvpp2_txdesc_dma_addr_set(port, tx_desc,
6266 buf_dma_addr & ~MVPP2_TX_DESC_ALIGN);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006267
6268 if (i == (skb_shinfo(skb)->nr_frags - 1)) {
6269 /* Last descriptor */
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006270 mvpp2_txdesc_cmd_set(port, tx_desc,
6271 MVPP2_TXD_L_DESC);
6272 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006273 } else {
6274 /* Descriptor in the middle: Not First, Not Last */
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006275 mvpp2_txdesc_cmd_set(port, tx_desc, 0);
6276 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006277 }
6278 }
6279
6280 return 0;
Markus Elfring32bae632017-04-17 11:36:34 +02006281cleanup:
Marcin Wojtas3f518502014-07-10 16:52:13 -03006282 /* Release all descriptors that were used to map fragments of
6283 * this packet, as well as the corresponding DMA mappings
6284 */
6285 for (i = i - 1; i >= 0; i--) {
6286 tx_desc = txq->descs + i;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006287 tx_desc_unmap_put(port, txq, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006288 }
6289
6290 return -ENOMEM;
6291}
6292
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006293static inline void mvpp2_tso_put_hdr(struct sk_buff *skb,
6294 struct net_device *dev,
6295 struct mvpp2_tx_queue *txq,
6296 struct mvpp2_tx_queue *aggr_txq,
6297 struct mvpp2_txq_pcpu *txq_pcpu,
6298 int hdr_sz)
6299{
6300 struct mvpp2_port *port = netdev_priv(dev);
6301 struct mvpp2_tx_desc *tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
6302 dma_addr_t addr;
6303
6304 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6305 mvpp2_txdesc_size_set(port, tx_desc, hdr_sz);
6306
6307 addr = txq_pcpu->tso_headers_dma +
6308 txq_pcpu->txq_put_index * TSO_HEADER_SIZE;
6309 mvpp2_txdesc_offset_set(port, tx_desc, addr & MVPP2_TX_DESC_ALIGN);
6310 mvpp2_txdesc_dma_addr_set(port, tx_desc, addr & ~MVPP2_TX_DESC_ALIGN);
6311
6312 mvpp2_txdesc_cmd_set(port, tx_desc, mvpp2_skb_tx_csum(port, skb) |
6313 MVPP2_TXD_F_DESC |
6314 MVPP2_TXD_PADDING_DISABLE);
6315 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
6316}
6317
6318static inline int mvpp2_tso_put_data(struct sk_buff *skb,
6319 struct net_device *dev, struct tso_t *tso,
6320 struct mvpp2_tx_queue *txq,
6321 struct mvpp2_tx_queue *aggr_txq,
6322 struct mvpp2_txq_pcpu *txq_pcpu,
6323 int sz, bool left, bool last)
6324{
6325 struct mvpp2_port *port = netdev_priv(dev);
6326 struct mvpp2_tx_desc *tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
6327 dma_addr_t buf_dma_addr;
6328
6329 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6330 mvpp2_txdesc_size_set(port, tx_desc, sz);
6331
6332 buf_dma_addr = dma_map_single(dev->dev.parent, tso->data, sz,
6333 DMA_TO_DEVICE);
6334 if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
6335 mvpp2_txq_desc_put(txq);
6336 return -ENOMEM;
6337 }
6338
6339 mvpp2_txdesc_offset_set(port, tx_desc,
6340 buf_dma_addr & MVPP2_TX_DESC_ALIGN);
6341 mvpp2_txdesc_dma_addr_set(port, tx_desc,
6342 buf_dma_addr & ~MVPP2_TX_DESC_ALIGN);
6343
6344 if (!left) {
6345 mvpp2_txdesc_cmd_set(port, tx_desc, MVPP2_TXD_L_DESC);
6346 if (last) {
6347 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
6348 return 0;
6349 }
6350 } else {
6351 mvpp2_txdesc_cmd_set(port, tx_desc, 0);
6352 }
6353
6354 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
6355 return 0;
6356}
6357
6358static int mvpp2_tx_tso(struct sk_buff *skb, struct net_device *dev,
6359 struct mvpp2_tx_queue *txq,
6360 struct mvpp2_tx_queue *aggr_txq,
6361 struct mvpp2_txq_pcpu *txq_pcpu)
6362{
6363 struct mvpp2_port *port = netdev_priv(dev);
6364 struct tso_t tso;
6365 int hdr_sz = skb_transport_offset(skb) + tcp_hdrlen(skb);
6366 int i, len, descs = 0;
6367
6368 /* Check number of available descriptors */
6369 if (mvpp2_aggr_desc_num_check(port->priv, aggr_txq,
6370 tso_count_descs(skb)) ||
6371 mvpp2_txq_reserved_desc_num_proc(port->priv, txq, txq_pcpu,
6372 tso_count_descs(skb)))
6373 return 0;
6374
6375 tso_start(skb, &tso);
6376 len = skb->len - hdr_sz;
6377 while (len > 0) {
6378 int left = min_t(int, skb_shinfo(skb)->gso_size, len);
6379 char *hdr = txq_pcpu->tso_headers +
6380 txq_pcpu->txq_put_index * TSO_HEADER_SIZE;
6381
6382 len -= left;
6383 descs++;
6384
6385 tso_build_hdr(skb, hdr, &tso, left, len == 0);
6386 mvpp2_tso_put_hdr(skb, dev, txq, aggr_txq, txq_pcpu, hdr_sz);
6387
6388 while (left > 0) {
6389 int sz = min_t(int, tso.size, left);
6390 left -= sz;
6391 descs++;
6392
6393 if (mvpp2_tso_put_data(skb, dev, &tso, txq, aggr_txq,
6394 txq_pcpu, sz, left, len == 0))
6395 goto release;
6396 tso_build_data(skb, &tso, sz);
6397 }
6398 }
6399
6400 return descs;
6401
6402release:
6403 for (i = descs - 1; i >= 0; i--) {
6404 struct mvpp2_tx_desc *tx_desc = txq->descs + i;
6405 tx_desc_unmap_put(port, txq, tx_desc);
6406 }
6407 return 0;
6408}
6409
Marcin Wojtas3f518502014-07-10 16:52:13 -03006410/* Main tx processing */
6411static int mvpp2_tx(struct sk_buff *skb, struct net_device *dev)
6412{
6413 struct mvpp2_port *port = netdev_priv(dev);
6414 struct mvpp2_tx_queue *txq, *aggr_txq;
6415 struct mvpp2_txq_pcpu *txq_pcpu;
6416 struct mvpp2_tx_desc *tx_desc;
Thomas Petazzoni20396132017-03-07 16:53:00 +01006417 dma_addr_t buf_dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006418 int frags = 0;
6419 u16 txq_id;
6420 u32 tx_cmd;
6421
6422 txq_id = skb_get_queue_mapping(skb);
6423 txq = port->txqs[txq_id];
6424 txq_pcpu = this_cpu_ptr(txq->pcpu);
6425 aggr_txq = &port->priv->aggr_txqs[smp_processor_id()];
6426
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006427 if (skb_is_gso(skb)) {
6428 frags = mvpp2_tx_tso(skb, dev, txq, aggr_txq, txq_pcpu);
6429 goto out;
6430 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006431 frags = skb_shinfo(skb)->nr_frags + 1;
6432
6433 /* Check number of available descriptors */
6434 if (mvpp2_aggr_desc_num_check(port->priv, aggr_txq, frags) ||
6435 mvpp2_txq_reserved_desc_num_proc(port->priv, txq,
6436 txq_pcpu, frags)) {
6437 frags = 0;
6438 goto out;
6439 }
6440
6441 /* Get a descriptor for the first part of the packet */
6442 tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006443 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6444 mvpp2_txdesc_size_set(port, tx_desc, skb_headlen(skb));
Marcin Wojtas3f518502014-07-10 16:52:13 -03006445
Thomas Petazzoni20396132017-03-07 16:53:00 +01006446 buf_dma_addr = dma_map_single(dev->dev.parent, skb->data,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006447 skb_headlen(skb), DMA_TO_DEVICE);
Thomas Petazzoni20396132017-03-07 16:53:00 +01006448 if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006449 mvpp2_txq_desc_put(txq);
6450 frags = 0;
6451 goto out;
6452 }
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006453
6454 mvpp2_txdesc_offset_set(port, tx_desc,
6455 buf_dma_addr & MVPP2_TX_DESC_ALIGN);
6456 mvpp2_txdesc_dma_addr_set(port, tx_desc,
6457 buf_dma_addr & ~MVPP2_TX_DESC_ALIGN);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006458
6459 tx_cmd = mvpp2_skb_tx_csum(port, skb);
6460
6461 if (frags == 1) {
6462 /* First and Last descriptor */
6463 tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_L_DESC;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006464 mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
6465 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006466 } else {
6467 /* First but not Last */
6468 tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_PADDING_DISABLE;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006469 mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
6470 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006471
6472 /* Continue with other skb fragments */
6473 if (mvpp2_tx_frag_process(port, skb, aggr_txq, txq)) {
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006474 tx_desc_unmap_put(port, txq, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006475 frags = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006476 }
6477 }
6478
Marcin Wojtas3f518502014-07-10 16:52:13 -03006479out:
6480 if (frags > 0) {
6481 struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006482 struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
6483
6484 txq_pcpu->reserved_num -= frags;
6485 txq_pcpu->count += frags;
6486 aggr_txq->count += frags;
6487
6488 /* Enable transmit */
6489 wmb();
6490 mvpp2_aggr_txq_pend_desc_add(port, frags);
6491
6492 if (txq_pcpu->size - txq_pcpu->count < MAX_SKB_FRAGS + 1)
6493 netif_tx_stop_queue(nq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006494
6495 u64_stats_update_begin(&stats->syncp);
6496 stats->tx_packets++;
6497 stats->tx_bytes += skb->len;
6498 u64_stats_update_end(&stats->syncp);
6499 } else {
6500 dev->stats.tx_dropped++;
6501 dev_kfree_skb_any(skb);
6502 }
6503
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006504 /* Finalize TX processing */
Antoine Tenart082297e2017-10-23 15:24:31 +02006505 if (!port->has_tx_irqs && txq_pcpu->count >= txq->done_pkts_coal)
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006506 mvpp2_txq_done(port, txq, txq_pcpu);
6507
6508 /* Set the timer in case not all frags were processed */
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006509 if (!port->has_tx_irqs && txq_pcpu->count <= frags &&
6510 txq_pcpu->count > 0) {
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006511 struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
6512
6513 mvpp2_timer_set(port_pcpu);
6514 }
6515
Marcin Wojtas3f518502014-07-10 16:52:13 -03006516 return NETDEV_TX_OK;
6517}
6518
6519static inline void mvpp2_cause_error(struct net_device *dev, int cause)
6520{
6521 if (cause & MVPP2_CAUSE_FCS_ERR_MASK)
6522 netdev_err(dev, "FCS error\n");
6523 if (cause & MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK)
6524 netdev_err(dev, "rx fifo overrun error\n");
6525 if (cause & MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK)
6526 netdev_err(dev, "tx fifo underrun error\n");
6527}
6528
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006529static int mvpp2_poll(struct napi_struct *napi, int budget)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006530{
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006531 u32 cause_rx_tx, cause_rx, cause_tx, cause_misc;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006532 int rx_done = 0;
6533 struct mvpp2_port *port = netdev_priv(napi->dev);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006534 struct mvpp2_queue_vector *qv;
Thomas Petazzonia7868412017-03-07 16:53:13 +01006535 int cpu = smp_processor_id();
Marcin Wojtas3f518502014-07-10 16:52:13 -03006536
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006537 qv = container_of(napi, struct mvpp2_queue_vector, napi);
6538
Marcin Wojtas3f518502014-07-10 16:52:13 -03006539 /* Rx/Tx cause register
6540 *
6541 * Bits 0-15: each bit indicates received packets on the Rx queue
6542 * (bit 0 is for Rx queue 0).
6543 *
6544 * Bits 16-23: each bit indicates transmitted packets on the Tx queue
6545 * (bit 16 is for Tx queue 0).
6546 *
6547 * Each CPU has its own Rx/Tx cause register
6548 */
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006549 cause_rx_tx = mvpp2_percpu_read(port->priv, qv->sw_thread_id,
Thomas Petazzonia7868412017-03-07 16:53:13 +01006550 MVPP2_ISR_RX_TX_CAUSE_REG(port->id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03006551
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006552 cause_misc = cause_rx_tx & MVPP2_CAUSE_MISC_SUM_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006553 if (cause_misc) {
6554 mvpp2_cause_error(port->dev, cause_misc);
6555
6556 /* Clear the cause register */
6557 mvpp2_write(port->priv, MVPP2_ISR_MISC_CAUSE_REG, 0);
Thomas Petazzonia7868412017-03-07 16:53:13 +01006558 mvpp2_percpu_write(port->priv, cpu,
6559 MVPP2_ISR_RX_TX_CAUSE_REG(port->id),
6560 cause_rx_tx & ~MVPP2_CAUSE_MISC_SUM_MASK);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006561 }
6562
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006563 cause_tx = cause_rx_tx & MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
6564 if (cause_tx) {
6565 cause_tx >>= MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_OFFSET;
6566 mvpp2_tx_done(port, cause_tx, qv->sw_thread_id);
6567 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006568
6569 /* Process RX packets */
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006570 cause_rx = cause_rx_tx & MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
6571 cause_rx <<= qv->first_rxq;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006572 cause_rx |= qv->pending_cause_rx;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006573 while (cause_rx && budget > 0) {
6574 int count;
6575 struct mvpp2_rx_queue *rxq;
6576
6577 rxq = mvpp2_get_rx_queue(port, cause_rx);
6578 if (!rxq)
6579 break;
6580
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006581 count = mvpp2_rx(port, napi, budget, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006582 rx_done += count;
6583 budget -= count;
6584 if (budget > 0) {
6585 /* Clear the bit associated to this Rx queue
6586 * so that next iteration will continue from
6587 * the next Rx queue.
6588 */
6589 cause_rx &= ~(1 << rxq->logic_rxq);
6590 }
6591 }
6592
6593 if (budget > 0) {
6594 cause_rx = 0;
Eric Dumazet6ad20162017-01-30 08:22:01 -08006595 napi_complete_done(napi, rx_done);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006596
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006597 mvpp2_qvec_interrupt_enable(qv);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006598 }
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006599 qv->pending_cause_rx = cause_rx;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006600 return rx_done;
6601}
6602
6603/* Set hw internals when starting port */
6604static void mvpp2_start_dev(struct mvpp2_port *port)
6605{
Philippe Reynes8e072692016-06-28 00:08:11 +02006606 struct net_device *ndev = port->dev;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006607 int i;
Philippe Reynes8e072692016-06-28 00:08:11 +02006608
Stefan Chulski76eb1b12017-08-22 19:08:26 +02006609 if (port->gop_id == 0 &&
6610 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
6611 port->phy_interface == PHY_INTERFACE_MODE_10GKR))
6612 mvpp2_xlg_max_rx_size_set(port);
6613 else
6614 mvpp2_gmac_max_rx_size_set(port);
6615
Marcin Wojtas3f518502014-07-10 16:52:13 -03006616 mvpp2_txp_max_tx_size_set(port);
6617
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006618 for (i = 0; i < port->nqvecs; i++)
6619 napi_enable(&port->qvecs[i].napi);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006620
6621 /* Enable interrupts on all CPUs */
6622 mvpp2_interrupts_enable(port);
6623
Antoine Tenart542897d2017-08-30 10:29:15 +02006624 if (port->priv->hw_version == MVPP22) {
6625 mvpp22_comphy_init(port);
Antoine Ténartf84bf382017-08-22 19:08:27 +02006626 mvpp22_gop_init(port);
Antoine Tenart542897d2017-08-30 10:29:15 +02006627 }
Antoine Ténartf84bf382017-08-22 19:08:27 +02006628
Antoine Ténart2055d622017-08-22 19:08:23 +02006629 mvpp2_port_mii_set(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006630 mvpp2_port_enable(port);
Antoine Tenart5997c862017-09-01 11:04:53 +02006631 if (ndev->phydev)
6632 phy_start(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006633 netif_tx_start_all_queues(port->dev);
6634}
6635
6636/* Set hw internals when stopping port */
6637static void mvpp2_stop_dev(struct mvpp2_port *port)
6638{
Philippe Reynes8e072692016-06-28 00:08:11 +02006639 struct net_device *ndev = port->dev;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006640 int i;
Philippe Reynes8e072692016-06-28 00:08:11 +02006641
Marcin Wojtas3f518502014-07-10 16:52:13 -03006642 /* Stop new packets from arriving to RXQs */
6643 mvpp2_ingress_disable(port);
6644
6645 mdelay(10);
6646
6647 /* Disable interrupts on all CPUs */
6648 mvpp2_interrupts_disable(port);
6649
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006650 for (i = 0; i < port->nqvecs; i++)
6651 napi_disable(&port->qvecs[i].napi);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006652
6653 netif_carrier_off(port->dev);
6654 netif_tx_stop_all_queues(port->dev);
6655
6656 mvpp2_egress_disable(port);
6657 mvpp2_port_disable(port);
Antoine Tenart5997c862017-09-01 11:04:53 +02006658 if (ndev->phydev)
6659 phy_stop(ndev->phydev);
Antoine Tenart542897d2017-08-30 10:29:15 +02006660 phy_power_off(port->comphy);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006661}
6662
Marcin Wojtas3f518502014-07-10 16:52:13 -03006663static int mvpp2_check_ringparam_valid(struct net_device *dev,
6664 struct ethtool_ringparam *ring)
6665{
6666 u16 new_rx_pending = ring->rx_pending;
6667 u16 new_tx_pending = ring->tx_pending;
6668
6669 if (ring->rx_pending == 0 || ring->tx_pending == 0)
6670 return -EINVAL;
6671
6672 if (ring->rx_pending > MVPP2_MAX_RXD)
6673 new_rx_pending = MVPP2_MAX_RXD;
6674 else if (!IS_ALIGNED(ring->rx_pending, 16))
6675 new_rx_pending = ALIGN(ring->rx_pending, 16);
6676
6677 if (ring->tx_pending > MVPP2_MAX_TXD)
6678 new_tx_pending = MVPP2_MAX_TXD;
6679 else if (!IS_ALIGNED(ring->tx_pending, 32))
6680 new_tx_pending = ALIGN(ring->tx_pending, 32);
6681
6682 if (ring->rx_pending != new_rx_pending) {
6683 netdev_info(dev, "illegal Rx ring size value %d, round to %d\n",
6684 ring->rx_pending, new_rx_pending);
6685 ring->rx_pending = new_rx_pending;
6686 }
6687
6688 if (ring->tx_pending != new_tx_pending) {
6689 netdev_info(dev, "illegal Tx ring size value %d, round to %d\n",
6690 ring->tx_pending, new_tx_pending);
6691 ring->tx_pending = new_tx_pending;
6692 }
6693
6694 return 0;
6695}
6696
Thomas Petazzoni26975822017-03-07 16:53:14 +01006697static void mvpp21_get_mac_address(struct mvpp2_port *port, unsigned char *addr)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006698{
6699 u32 mac_addr_l, mac_addr_m, mac_addr_h;
6700
6701 mac_addr_l = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
6702 mac_addr_m = readl(port->priv->lms_base + MVPP2_SRC_ADDR_MIDDLE);
6703 mac_addr_h = readl(port->priv->lms_base + MVPP2_SRC_ADDR_HIGH);
6704 addr[0] = (mac_addr_h >> 24) & 0xFF;
6705 addr[1] = (mac_addr_h >> 16) & 0xFF;
6706 addr[2] = (mac_addr_h >> 8) & 0xFF;
6707 addr[3] = mac_addr_h & 0xFF;
6708 addr[4] = mac_addr_m & 0xFF;
6709 addr[5] = (mac_addr_l >> MVPP2_GMAC_SA_LOW_OFFS) & 0xFF;
6710}
6711
6712static int mvpp2_phy_connect(struct mvpp2_port *port)
6713{
6714 struct phy_device *phy_dev;
6715
Antoine Tenart5997c862017-09-01 11:04:53 +02006716 /* No PHY is attached */
6717 if (!port->phy_node)
6718 return 0;
6719
Marcin Wojtas3f518502014-07-10 16:52:13 -03006720 phy_dev = of_phy_connect(port->dev, port->phy_node, mvpp2_link_event, 0,
6721 port->phy_interface);
6722 if (!phy_dev) {
6723 netdev_err(port->dev, "cannot connect to phy\n");
6724 return -ENODEV;
6725 }
6726 phy_dev->supported &= PHY_GBIT_FEATURES;
6727 phy_dev->advertising = phy_dev->supported;
6728
Marcin Wojtas3f518502014-07-10 16:52:13 -03006729 port->link = 0;
6730 port->duplex = 0;
6731 port->speed = 0;
6732
6733 return 0;
6734}
6735
6736static void mvpp2_phy_disconnect(struct mvpp2_port *port)
6737{
Philippe Reynes8e072692016-06-28 00:08:11 +02006738 struct net_device *ndev = port->dev;
6739
Antoine Tenart5997c862017-09-01 11:04:53 +02006740 if (!ndev->phydev)
6741 return;
6742
Philippe Reynes8e072692016-06-28 00:08:11 +02006743 phy_disconnect(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006744}
6745
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006746static int mvpp2_irqs_init(struct mvpp2_port *port)
6747{
6748 int err, i;
6749
6750 for (i = 0; i < port->nqvecs; i++) {
6751 struct mvpp2_queue_vector *qv = port->qvecs + i;
6752
6753 err = request_irq(qv->irq, mvpp2_isr, 0, port->dev->name, qv);
6754 if (err)
6755 goto err;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006756
6757 if (qv->type == MVPP2_QUEUE_VECTOR_PRIVATE)
6758 irq_set_affinity_hint(qv->irq,
6759 cpumask_of(qv->sw_thread_id));
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006760 }
6761
6762 return 0;
6763err:
6764 for (i = 0; i < port->nqvecs; i++) {
6765 struct mvpp2_queue_vector *qv = port->qvecs + i;
6766
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006767 irq_set_affinity_hint(qv->irq, NULL);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006768 free_irq(qv->irq, qv);
6769 }
6770
6771 return err;
6772}
6773
6774static void mvpp2_irqs_deinit(struct mvpp2_port *port)
6775{
6776 int i;
6777
6778 for (i = 0; i < port->nqvecs; i++) {
6779 struct mvpp2_queue_vector *qv = port->qvecs + i;
6780
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006781 irq_set_affinity_hint(qv->irq, NULL);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006782 free_irq(qv->irq, qv);
6783 }
6784}
6785
Marcin Wojtas3f518502014-07-10 16:52:13 -03006786static int mvpp2_open(struct net_device *dev)
6787{
6788 struct mvpp2_port *port = netdev_priv(dev);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02006789 struct mvpp2 *priv = port->priv;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006790 unsigned char mac_bcast[ETH_ALEN] = {
6791 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
6792 int err;
6793
6794 err = mvpp2_prs_mac_da_accept(port->priv, port->id, mac_bcast, true);
6795 if (err) {
6796 netdev_err(dev, "mvpp2_prs_mac_da_accept BC failed\n");
6797 return err;
6798 }
6799 err = mvpp2_prs_mac_da_accept(port->priv, port->id,
6800 dev->dev_addr, true);
6801 if (err) {
6802 netdev_err(dev, "mvpp2_prs_mac_da_accept MC failed\n");
6803 return err;
6804 }
6805 err = mvpp2_prs_tag_mode_set(port->priv, port->id, MVPP2_TAG_TYPE_MH);
6806 if (err) {
6807 netdev_err(dev, "mvpp2_prs_tag_mode_set failed\n");
6808 return err;
6809 }
6810 err = mvpp2_prs_def_flow(port);
6811 if (err) {
6812 netdev_err(dev, "mvpp2_prs_def_flow failed\n");
6813 return err;
6814 }
6815
6816 /* Allocate the Rx/Tx queues */
6817 err = mvpp2_setup_rxqs(port);
6818 if (err) {
6819 netdev_err(port->dev, "cannot allocate Rx queues\n");
6820 return err;
6821 }
6822
6823 err = mvpp2_setup_txqs(port);
6824 if (err) {
6825 netdev_err(port->dev, "cannot allocate Tx queues\n");
6826 goto err_cleanup_rxqs;
6827 }
6828
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006829 err = mvpp2_irqs_init(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006830 if (err) {
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006831 netdev_err(port->dev, "cannot init IRQs\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03006832 goto err_cleanup_txqs;
6833 }
6834
Antoine Tenartfd3651b2017-09-01 11:04:54 +02006835 if (priv->hw_version == MVPP22 && !port->phy_node && port->link_irq) {
6836 err = request_irq(port->link_irq, mvpp2_link_status_isr, 0,
6837 dev->name, port);
6838 if (err) {
6839 netdev_err(port->dev, "cannot request link IRQ %d\n",
6840 port->link_irq);
6841 goto err_free_irq;
6842 }
6843
6844 mvpp22_gop_setup_irq(port);
6845 }
6846
Marcin Wojtas3f518502014-07-10 16:52:13 -03006847 /* In default link is down */
6848 netif_carrier_off(port->dev);
6849
6850 err = mvpp2_phy_connect(port);
6851 if (err < 0)
Antoine Tenartfd3651b2017-09-01 11:04:54 +02006852 goto err_free_link_irq;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006853
6854 /* Unmask interrupts on all CPUs */
6855 on_each_cpu(mvpp2_interrupts_unmask, port, 1);
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006856 mvpp2_shared_interrupt_mask_unmask(port, false);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006857
6858 mvpp2_start_dev(port);
6859
6860 return 0;
6861
Antoine Tenartfd3651b2017-09-01 11:04:54 +02006862err_free_link_irq:
6863 if (priv->hw_version == MVPP22 && !port->phy_node && port->link_irq)
6864 free_irq(port->link_irq, port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006865err_free_irq:
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006866 mvpp2_irqs_deinit(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006867err_cleanup_txqs:
6868 mvpp2_cleanup_txqs(port);
6869err_cleanup_rxqs:
6870 mvpp2_cleanup_rxqs(port);
6871 return err;
6872}
6873
6874static int mvpp2_stop(struct net_device *dev)
6875{
6876 struct mvpp2_port *port = netdev_priv(dev);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006877 struct mvpp2_port_pcpu *port_pcpu;
Antoine Tenartfd3651b2017-09-01 11:04:54 +02006878 struct mvpp2 *priv = port->priv;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006879 int cpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006880
6881 mvpp2_stop_dev(port);
6882 mvpp2_phy_disconnect(port);
6883
6884 /* Mask interrupts on all CPUs */
6885 on_each_cpu(mvpp2_interrupts_mask, port, 1);
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006886 mvpp2_shared_interrupt_mask_unmask(port, true);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006887
Antoine Tenartfd3651b2017-09-01 11:04:54 +02006888 if (priv->hw_version == MVPP22 && !port->phy_node && port->link_irq)
6889 free_irq(port->link_irq, port);
6890
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006891 mvpp2_irqs_deinit(port);
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006892 if (!port->has_tx_irqs) {
6893 for_each_present_cpu(cpu) {
6894 port_pcpu = per_cpu_ptr(port->pcpu, cpu);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006895
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006896 hrtimer_cancel(&port_pcpu->tx_done_timer);
6897 port_pcpu->timer_scheduled = false;
6898 tasklet_kill(&port_pcpu->tx_done_tasklet);
6899 }
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006900 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006901 mvpp2_cleanup_rxqs(port);
6902 mvpp2_cleanup_txqs(port);
6903
6904 return 0;
6905}
6906
6907static void mvpp2_set_rx_mode(struct net_device *dev)
6908{
6909 struct mvpp2_port *port = netdev_priv(dev);
6910 struct mvpp2 *priv = port->priv;
6911 struct netdev_hw_addr *ha;
6912 int id = port->id;
6913 bool allmulti = dev->flags & IFF_ALLMULTI;
6914
6915 mvpp2_prs_mac_promisc_set(priv, id, dev->flags & IFF_PROMISC);
6916 mvpp2_prs_mac_multi_set(priv, id, MVPP2_PE_MAC_MC_ALL, allmulti);
6917 mvpp2_prs_mac_multi_set(priv, id, MVPP2_PE_MAC_MC_IP6, allmulti);
6918
6919 /* Remove all port->id's mcast enries */
6920 mvpp2_prs_mcast_del_all(priv, id);
6921
6922 if (allmulti && !netdev_mc_empty(dev)) {
6923 netdev_for_each_mc_addr(ha, dev)
6924 mvpp2_prs_mac_da_accept(priv, id, ha->addr, true);
6925 }
6926}
6927
6928static int mvpp2_set_mac_address(struct net_device *dev, void *p)
6929{
6930 struct mvpp2_port *port = netdev_priv(dev);
6931 const struct sockaddr *addr = p;
6932 int err;
6933
6934 if (!is_valid_ether_addr(addr->sa_data)) {
6935 err = -EADDRNOTAVAIL;
Markus Elfringc1175542017-04-17 11:10:47 +02006936 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006937 }
6938
6939 if (!netif_running(dev)) {
6940 err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
6941 if (!err)
6942 return 0;
6943 /* Reconfigure parser to accept the original MAC address */
6944 err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
6945 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02006946 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006947 }
6948
6949 mvpp2_stop_dev(port);
6950
6951 err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
6952 if (!err)
6953 goto out_start;
6954
6955 /* Reconfigure parser accept the original MAC address */
6956 err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
6957 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02006958 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006959out_start:
6960 mvpp2_start_dev(port);
6961 mvpp2_egress_enable(port);
6962 mvpp2_ingress_enable(port);
6963 return 0;
Markus Elfringc1175542017-04-17 11:10:47 +02006964log_error:
Markus Elfringdfd42402017-04-17 11:20:41 +02006965 netdev_err(dev, "failed to change MAC address\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03006966 return err;
6967}
6968
6969static int mvpp2_change_mtu(struct net_device *dev, int mtu)
6970{
6971 struct mvpp2_port *port = netdev_priv(dev);
6972 int err;
6973
Jarod Wilson57779872016-10-17 15:54:06 -04006974 if (!IS_ALIGNED(MVPP2_RX_PKT_SIZE(mtu), 8)) {
6975 netdev_info(dev, "illegal MTU value %d, round to %d\n", mtu,
6976 ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8));
6977 mtu = ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006978 }
6979
6980 if (!netif_running(dev)) {
6981 err = mvpp2_bm_update_mtu(dev, mtu);
6982 if (!err) {
6983 port->pkt_size = MVPP2_RX_PKT_SIZE(mtu);
6984 return 0;
6985 }
6986
6987 /* Reconfigure BM to the original MTU */
6988 err = mvpp2_bm_update_mtu(dev, dev->mtu);
6989 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02006990 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006991 }
6992
6993 mvpp2_stop_dev(port);
6994
6995 err = mvpp2_bm_update_mtu(dev, mtu);
6996 if (!err) {
6997 port->pkt_size = MVPP2_RX_PKT_SIZE(mtu);
6998 goto out_start;
6999 }
7000
7001 /* Reconfigure BM to the original MTU */
7002 err = mvpp2_bm_update_mtu(dev, dev->mtu);
7003 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02007004 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007005
7006out_start:
7007 mvpp2_start_dev(port);
7008 mvpp2_egress_enable(port);
7009 mvpp2_ingress_enable(port);
7010
7011 return 0;
Markus Elfringc1175542017-04-17 11:10:47 +02007012log_error:
Markus Elfringdfd42402017-04-17 11:20:41 +02007013 netdev_err(dev, "failed to change MTU\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03007014 return err;
7015}
7016
stephen hemmingerbc1f4472017-01-06 19:12:52 -08007017static void
Marcin Wojtas3f518502014-07-10 16:52:13 -03007018mvpp2_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
7019{
7020 struct mvpp2_port *port = netdev_priv(dev);
7021 unsigned int start;
7022 int cpu;
7023
7024 for_each_possible_cpu(cpu) {
7025 struct mvpp2_pcpu_stats *cpu_stats;
7026 u64 rx_packets;
7027 u64 rx_bytes;
7028 u64 tx_packets;
7029 u64 tx_bytes;
7030
7031 cpu_stats = per_cpu_ptr(port->stats, cpu);
7032 do {
7033 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
7034 rx_packets = cpu_stats->rx_packets;
7035 rx_bytes = cpu_stats->rx_bytes;
7036 tx_packets = cpu_stats->tx_packets;
7037 tx_bytes = cpu_stats->tx_bytes;
7038 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
7039
7040 stats->rx_packets += rx_packets;
7041 stats->rx_bytes += rx_bytes;
7042 stats->tx_packets += tx_packets;
7043 stats->tx_bytes += tx_bytes;
7044 }
7045
7046 stats->rx_errors = dev->stats.rx_errors;
7047 stats->rx_dropped = dev->stats.rx_dropped;
7048 stats->tx_dropped = dev->stats.tx_dropped;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007049}
7050
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007051static int mvpp2_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
7052{
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007053 int ret;
7054
Philippe Reynes8e072692016-06-28 00:08:11 +02007055 if (!dev->phydev)
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007056 return -ENOTSUPP;
7057
Philippe Reynes8e072692016-06-28 00:08:11 +02007058 ret = phy_mii_ioctl(dev->phydev, ifr, cmd);
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007059 if (!ret)
7060 mvpp2_link_event(dev);
7061
7062 return ret;
7063}
7064
Marcin Wojtas3f518502014-07-10 16:52:13 -03007065/* Ethtool methods */
7066
Marcin Wojtas3f518502014-07-10 16:52:13 -03007067/* Set interrupt coalescing for ethtools */
7068static int mvpp2_ethtool_set_coalesce(struct net_device *dev,
7069 struct ethtool_coalesce *c)
7070{
7071 struct mvpp2_port *port = netdev_priv(dev);
7072 int queue;
7073
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007074 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007075 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
7076
7077 rxq->time_coal = c->rx_coalesce_usecs;
7078 rxq->pkts_coal = c->rx_max_coalesced_frames;
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01007079 mvpp2_rx_pkts_coal_set(port, rxq);
7080 mvpp2_rx_time_coal_set(port, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007081 }
7082
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007083 if (port->has_tx_irqs) {
7084 port->tx_time_coal = c->tx_coalesce_usecs;
7085 mvpp2_tx_time_coal_set(port);
7086 }
7087
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007088 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007089 struct mvpp2_tx_queue *txq = port->txqs[queue];
7090
7091 txq->done_pkts_coal = c->tx_max_coalesced_frames;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007092
7093 if (port->has_tx_irqs)
7094 mvpp2_tx_pkts_coal_set(port, txq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007095 }
7096
Marcin Wojtas3f518502014-07-10 16:52:13 -03007097 return 0;
7098}
7099
7100/* get coalescing for ethtools */
7101static int mvpp2_ethtool_get_coalesce(struct net_device *dev,
7102 struct ethtool_coalesce *c)
7103{
7104 struct mvpp2_port *port = netdev_priv(dev);
7105
7106 c->rx_coalesce_usecs = port->rxqs[0]->time_coal;
7107 c->rx_max_coalesced_frames = port->rxqs[0]->pkts_coal;
7108 c->tx_max_coalesced_frames = port->txqs[0]->done_pkts_coal;
7109 return 0;
7110}
7111
7112static void mvpp2_ethtool_get_drvinfo(struct net_device *dev,
7113 struct ethtool_drvinfo *drvinfo)
7114{
7115 strlcpy(drvinfo->driver, MVPP2_DRIVER_NAME,
7116 sizeof(drvinfo->driver));
7117 strlcpy(drvinfo->version, MVPP2_DRIVER_VERSION,
7118 sizeof(drvinfo->version));
7119 strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
7120 sizeof(drvinfo->bus_info));
7121}
7122
7123static void mvpp2_ethtool_get_ringparam(struct net_device *dev,
7124 struct ethtool_ringparam *ring)
7125{
7126 struct mvpp2_port *port = netdev_priv(dev);
7127
7128 ring->rx_max_pending = MVPP2_MAX_RXD;
7129 ring->tx_max_pending = MVPP2_MAX_TXD;
7130 ring->rx_pending = port->rx_ring_size;
7131 ring->tx_pending = port->tx_ring_size;
7132}
7133
7134static int mvpp2_ethtool_set_ringparam(struct net_device *dev,
7135 struct ethtool_ringparam *ring)
7136{
7137 struct mvpp2_port *port = netdev_priv(dev);
7138 u16 prev_rx_ring_size = port->rx_ring_size;
7139 u16 prev_tx_ring_size = port->tx_ring_size;
7140 int err;
7141
7142 err = mvpp2_check_ringparam_valid(dev, ring);
7143 if (err)
7144 return err;
7145
7146 if (!netif_running(dev)) {
7147 port->rx_ring_size = ring->rx_pending;
7148 port->tx_ring_size = ring->tx_pending;
7149 return 0;
7150 }
7151
7152 /* The interface is running, so we have to force a
7153 * reallocation of the queues
7154 */
7155 mvpp2_stop_dev(port);
7156 mvpp2_cleanup_rxqs(port);
7157 mvpp2_cleanup_txqs(port);
7158
7159 port->rx_ring_size = ring->rx_pending;
7160 port->tx_ring_size = ring->tx_pending;
7161
7162 err = mvpp2_setup_rxqs(port);
7163 if (err) {
7164 /* Reallocate Rx queues with the original ring size */
7165 port->rx_ring_size = prev_rx_ring_size;
7166 ring->rx_pending = prev_rx_ring_size;
7167 err = mvpp2_setup_rxqs(port);
7168 if (err)
7169 goto err_out;
7170 }
7171 err = mvpp2_setup_txqs(port);
7172 if (err) {
7173 /* Reallocate Tx queues with the original ring size */
7174 port->tx_ring_size = prev_tx_ring_size;
7175 ring->tx_pending = prev_tx_ring_size;
7176 err = mvpp2_setup_txqs(port);
7177 if (err)
7178 goto err_clean_rxqs;
7179 }
7180
7181 mvpp2_start_dev(port);
7182 mvpp2_egress_enable(port);
7183 mvpp2_ingress_enable(port);
7184
7185 return 0;
7186
7187err_clean_rxqs:
7188 mvpp2_cleanup_rxqs(port);
7189err_out:
Markus Elfringdfd42402017-04-17 11:20:41 +02007190 netdev_err(dev, "failed to change ring parameters");
Marcin Wojtas3f518502014-07-10 16:52:13 -03007191 return err;
7192}
7193
7194/* Device ops */
7195
7196static const struct net_device_ops mvpp2_netdev_ops = {
7197 .ndo_open = mvpp2_open,
7198 .ndo_stop = mvpp2_stop,
7199 .ndo_start_xmit = mvpp2_tx,
7200 .ndo_set_rx_mode = mvpp2_set_rx_mode,
7201 .ndo_set_mac_address = mvpp2_set_mac_address,
7202 .ndo_change_mtu = mvpp2_change_mtu,
7203 .ndo_get_stats64 = mvpp2_get_stats64,
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007204 .ndo_do_ioctl = mvpp2_ioctl,
Marcin Wojtas3f518502014-07-10 16:52:13 -03007205};
7206
7207static const struct ethtool_ops mvpp2_eth_tool_ops = {
Florian Fainelli00606c42016-11-15 11:19:48 -08007208 .nway_reset = phy_ethtool_nway_reset,
Marcin Wojtas3f518502014-07-10 16:52:13 -03007209 .get_link = ethtool_op_get_link,
Marcin Wojtas3f518502014-07-10 16:52:13 -03007210 .set_coalesce = mvpp2_ethtool_set_coalesce,
7211 .get_coalesce = mvpp2_ethtool_get_coalesce,
7212 .get_drvinfo = mvpp2_ethtool_get_drvinfo,
7213 .get_ringparam = mvpp2_ethtool_get_ringparam,
7214 .set_ringparam = mvpp2_ethtool_set_ringparam,
Philippe Reynesfb773e92016-06-28 00:08:12 +02007215 .get_link_ksettings = phy_ethtool_get_link_ksettings,
7216 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Marcin Wojtas3f518502014-07-10 16:52:13 -03007217};
7218
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007219/* Used for PPv2.1, or PPv2.2 with the old Device Tree binding that
7220 * had a single IRQ defined per-port.
7221 */
7222static int mvpp2_simple_queue_vectors_init(struct mvpp2_port *port,
7223 struct device_node *port_node)
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007224{
7225 struct mvpp2_queue_vector *v = &port->qvecs[0];
7226
7227 v->first_rxq = 0;
7228 v->nrxqs = port->nrxqs;
7229 v->type = MVPP2_QUEUE_VECTOR_SHARED;
7230 v->sw_thread_id = 0;
7231 v->sw_thread_mask = *cpumask_bits(cpu_online_mask);
7232 v->port = port;
7233 v->irq = irq_of_parse_and_map(port_node, 0);
7234 if (v->irq <= 0)
7235 return -EINVAL;
7236 netif_napi_add(port->dev, &v->napi, mvpp2_poll,
7237 NAPI_POLL_WEIGHT);
7238
7239 port->nqvecs = 1;
7240
7241 return 0;
7242}
7243
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007244static int mvpp2_multi_queue_vectors_init(struct mvpp2_port *port,
7245 struct device_node *port_node)
7246{
7247 struct mvpp2_queue_vector *v;
7248 int i, ret;
7249
7250 port->nqvecs = num_possible_cpus();
7251 if (queue_mode == MVPP2_QDIST_SINGLE_MODE)
7252 port->nqvecs += 1;
7253
7254 for (i = 0; i < port->nqvecs; i++) {
7255 char irqname[16];
7256
7257 v = port->qvecs + i;
7258
7259 v->port = port;
7260 v->type = MVPP2_QUEUE_VECTOR_PRIVATE;
7261 v->sw_thread_id = i;
7262 v->sw_thread_mask = BIT(i);
7263
7264 snprintf(irqname, sizeof(irqname), "tx-cpu%d", i);
7265
7266 if (queue_mode == MVPP2_QDIST_MULTI_MODE) {
7267 v->first_rxq = i * MVPP2_DEFAULT_RXQ;
7268 v->nrxqs = MVPP2_DEFAULT_RXQ;
7269 } else if (queue_mode == MVPP2_QDIST_SINGLE_MODE &&
7270 i == (port->nqvecs - 1)) {
7271 v->first_rxq = 0;
7272 v->nrxqs = port->nrxqs;
7273 v->type = MVPP2_QUEUE_VECTOR_SHARED;
7274 strncpy(irqname, "rx-shared", sizeof(irqname));
7275 }
7276
7277 v->irq = of_irq_get_byname(port_node, irqname);
7278 if (v->irq <= 0) {
7279 ret = -EINVAL;
7280 goto err;
7281 }
7282
7283 netif_napi_add(port->dev, &v->napi, mvpp2_poll,
7284 NAPI_POLL_WEIGHT);
7285 }
7286
7287 return 0;
7288
7289err:
7290 for (i = 0; i < port->nqvecs; i++)
7291 irq_dispose_mapping(port->qvecs[i].irq);
7292 return ret;
7293}
7294
7295static int mvpp2_queue_vectors_init(struct mvpp2_port *port,
7296 struct device_node *port_node)
7297{
7298 if (port->has_tx_irqs)
7299 return mvpp2_multi_queue_vectors_init(port, port_node);
7300 else
7301 return mvpp2_simple_queue_vectors_init(port, port_node);
7302}
7303
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007304static void mvpp2_queue_vectors_deinit(struct mvpp2_port *port)
7305{
7306 int i;
7307
7308 for (i = 0; i < port->nqvecs; i++)
7309 irq_dispose_mapping(port->qvecs[i].irq);
7310}
7311
7312/* Configure Rx queue group interrupt for this port */
7313static void mvpp2_rx_irqs_setup(struct mvpp2_port *port)
7314{
7315 struct mvpp2 *priv = port->priv;
7316 u32 val;
7317 int i;
7318
7319 if (priv->hw_version == MVPP21) {
7320 mvpp2_write(priv, MVPP21_ISR_RXQ_GROUP_REG(port->id),
7321 port->nrxqs);
7322 return;
7323 }
7324
7325 /* Handle the more complicated PPv2.2 case */
7326 for (i = 0; i < port->nqvecs; i++) {
7327 struct mvpp2_queue_vector *qv = port->qvecs + i;
7328
7329 if (!qv->nrxqs)
7330 continue;
7331
7332 val = qv->sw_thread_id;
7333 val |= port->id << MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_OFFSET;
7334 mvpp2_write(priv, MVPP22_ISR_RXQ_GROUP_INDEX_REG, val);
7335
7336 val = qv->first_rxq;
7337 val |= qv->nrxqs << MVPP22_ISR_RXQ_SUB_GROUP_SIZE_OFFSET;
7338 mvpp2_write(priv, MVPP22_ISR_RXQ_SUB_GROUP_CONFIG_REG, val);
7339 }
7340}
7341
Marcin Wojtas3f518502014-07-10 16:52:13 -03007342/* Initialize port HW */
7343static int mvpp2_port_init(struct mvpp2_port *port)
7344{
7345 struct device *dev = port->dev->dev.parent;
7346 struct mvpp2 *priv = port->priv;
7347 struct mvpp2_txq_pcpu *txq_pcpu;
7348 int queue, cpu, err;
7349
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007350 /* Checks for hardware constraints */
7351 if (port->first_rxq + port->nrxqs >
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01007352 MVPP2_MAX_PORTS * priv->max_port_rxqs)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007353 return -EINVAL;
7354
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007355 if (port->nrxqs % 4 || (port->nrxqs > priv->max_port_rxqs) ||
7356 (port->ntxqs > MVPP2_MAX_TXQ))
7357 return -EINVAL;
7358
Marcin Wojtas3f518502014-07-10 16:52:13 -03007359 /* Disable port */
7360 mvpp2_egress_disable(port);
7361 mvpp2_port_disable(port);
7362
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007363 port->tx_time_coal = MVPP2_TXDONE_COAL_USEC;
7364
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007365 port->txqs = devm_kcalloc(dev, port->ntxqs, sizeof(*port->txqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03007366 GFP_KERNEL);
7367 if (!port->txqs)
7368 return -ENOMEM;
7369
7370 /* Associate physical Tx queues to this port and initialize.
7371 * The mapping is predefined.
7372 */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007373 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007374 int queue_phy_id = mvpp2_txq_phys(port->id, queue);
7375 struct mvpp2_tx_queue *txq;
7376
7377 txq = devm_kzalloc(dev, sizeof(*txq), GFP_KERNEL);
Christophe Jaillet177c8d12017-02-19 10:19:57 +01007378 if (!txq) {
7379 err = -ENOMEM;
7380 goto err_free_percpu;
7381 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03007382
7383 txq->pcpu = alloc_percpu(struct mvpp2_txq_pcpu);
7384 if (!txq->pcpu) {
7385 err = -ENOMEM;
7386 goto err_free_percpu;
7387 }
7388
7389 txq->id = queue_phy_id;
7390 txq->log_id = queue;
7391 txq->done_pkts_coal = MVPP2_TXDONE_COAL_PKTS_THRESH;
7392 for_each_present_cpu(cpu) {
7393 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
7394 txq_pcpu->cpu = cpu;
7395 }
7396
7397 port->txqs[queue] = txq;
7398 }
7399
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007400 port->rxqs = devm_kcalloc(dev, port->nrxqs, sizeof(*port->rxqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03007401 GFP_KERNEL);
7402 if (!port->rxqs) {
7403 err = -ENOMEM;
7404 goto err_free_percpu;
7405 }
7406
7407 /* Allocate and initialize Rx queue for this port */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007408 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007409 struct mvpp2_rx_queue *rxq;
7410
7411 /* Map physical Rx queue to port's logical Rx queue */
7412 rxq = devm_kzalloc(dev, sizeof(*rxq), GFP_KERNEL);
Jisheng Zhangd82b0c22016-03-31 17:01:23 +08007413 if (!rxq) {
7414 err = -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007415 goto err_free_percpu;
Jisheng Zhangd82b0c22016-03-31 17:01:23 +08007416 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03007417 /* Map this Rx queue to a physical queue */
7418 rxq->id = port->first_rxq + queue;
7419 rxq->port = port->id;
7420 rxq->logic_rxq = queue;
7421
7422 port->rxqs[queue] = rxq;
7423 }
7424
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007425 mvpp2_rx_irqs_setup(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007426
7427 /* Create Rx descriptor rings */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007428 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007429 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
7430
7431 rxq->size = port->rx_ring_size;
7432 rxq->pkts_coal = MVPP2_RX_COAL_PKTS;
7433 rxq->time_coal = MVPP2_RX_COAL_USEC;
7434 }
7435
7436 mvpp2_ingress_disable(port);
7437
7438 /* Port default configuration */
7439 mvpp2_defaults_set(port);
7440
7441 /* Port's classifier configuration */
7442 mvpp2_cls_oversize_rxq_set(port);
7443 mvpp2_cls_port_config(port);
7444
7445 /* Provide an initial Rx packet size */
7446 port->pkt_size = MVPP2_RX_PKT_SIZE(port->dev->mtu);
7447
7448 /* Initialize pools for swf */
7449 err = mvpp2_swf_bm_pool_init(port);
7450 if (err)
7451 goto err_free_percpu;
7452
7453 return 0;
7454
7455err_free_percpu:
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007456 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007457 if (!port->txqs[queue])
7458 continue;
7459 free_percpu(port->txqs[queue]->pcpu);
7460 }
7461 return err;
7462}
7463
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007464/* Checks if the port DT description has the TX interrupts
7465 * described. On PPv2.1, there are no such interrupts. On PPv2.2,
7466 * there are available, but we need to keep support for old DTs.
7467 */
7468static bool mvpp2_port_has_tx_irqs(struct mvpp2 *priv,
7469 struct device_node *port_node)
7470{
7471 char *irqs[5] = { "rx-shared", "tx-cpu0", "tx-cpu1",
7472 "tx-cpu2", "tx-cpu3" };
7473 int ret, i;
7474
7475 if (priv->hw_version == MVPP21)
7476 return false;
7477
7478 for (i = 0; i < 5; i++) {
7479 ret = of_property_match_string(port_node, "interrupt-names",
7480 irqs[i]);
7481 if (ret < 0)
7482 return false;
7483 }
7484
7485 return true;
7486}
7487
Antoine Tenart3ba8c812017-09-02 11:06:47 +02007488static void mvpp2_port_copy_mac_addr(struct net_device *dev, struct mvpp2 *priv,
7489 struct device_node *port_node,
7490 char **mac_from)
7491{
7492 struct mvpp2_port *port = netdev_priv(dev);
7493 char hw_mac_addr[ETH_ALEN] = {0};
7494 const char *dt_mac_addr;
7495
7496 dt_mac_addr = of_get_mac_address(port_node);
7497 if (dt_mac_addr && is_valid_ether_addr(dt_mac_addr)) {
7498 *mac_from = "device tree";
7499 ether_addr_copy(dev->dev_addr, dt_mac_addr);
Antoine Tenart688cbaf2017-09-02 11:06:49 +02007500 return;
Antoine Tenart3ba8c812017-09-02 11:06:47 +02007501 }
Antoine Tenart688cbaf2017-09-02 11:06:49 +02007502
7503 if (priv->hw_version == MVPP21) {
7504 mvpp21_get_mac_address(port, hw_mac_addr);
7505 if (is_valid_ether_addr(hw_mac_addr)) {
7506 *mac_from = "hardware";
7507 ether_addr_copy(dev->dev_addr, hw_mac_addr);
7508 return;
7509 }
7510 }
7511
7512 *mac_from = "random";
7513 eth_hw_addr_random(dev);
Antoine Tenart3ba8c812017-09-02 11:06:47 +02007514}
7515
Marcin Wojtas3f518502014-07-10 16:52:13 -03007516/* Ports initialization */
7517static int mvpp2_port_probe(struct platform_device *pdev,
7518 struct device_node *port_node,
Yan Markman6bf69a12017-09-25 14:59:47 +02007519 struct mvpp2 *priv, int index)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007520{
7521 struct device_node *phy_node;
Antoine Tenart542897d2017-08-30 10:29:15 +02007522 struct phy *comphy;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007523 struct mvpp2_port *port;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007524 struct mvpp2_port_pcpu *port_pcpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007525 struct net_device *dev;
7526 struct resource *res;
Antoine Tenart3ba8c812017-09-02 11:06:47 +02007527 char *mac_from = "";
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007528 unsigned int ntxqs, nrxqs;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007529 bool has_tx_irqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007530 u32 id;
7531 int features;
7532 int phy_mode;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007533 int err, i, cpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007534
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007535 has_tx_irqs = mvpp2_port_has_tx_irqs(priv, port_node);
7536
7537 if (!has_tx_irqs)
7538 queue_mode = MVPP2_QDIST_SINGLE_MODE;
7539
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007540 ntxqs = MVPP2_MAX_TXQ;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007541 if (priv->hw_version == MVPP22 && queue_mode == MVPP2_QDIST_MULTI_MODE)
7542 nrxqs = MVPP2_DEFAULT_RXQ * num_possible_cpus();
7543 else
7544 nrxqs = MVPP2_DEFAULT_RXQ;
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007545
7546 dev = alloc_etherdev_mqs(sizeof(*port), ntxqs, nrxqs);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007547 if (!dev)
7548 return -ENOMEM;
7549
7550 phy_node = of_parse_phandle(port_node, "phy", 0);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007551 phy_mode = of_get_phy_mode(port_node);
7552 if (phy_mode < 0) {
7553 dev_err(&pdev->dev, "incorrect phy mode\n");
7554 err = phy_mode;
7555 goto err_free_netdev;
7556 }
7557
Antoine Tenart542897d2017-08-30 10:29:15 +02007558 comphy = devm_of_phy_get(&pdev->dev, port_node, NULL);
7559 if (IS_ERR(comphy)) {
7560 if (PTR_ERR(comphy) == -EPROBE_DEFER) {
7561 err = -EPROBE_DEFER;
7562 goto err_free_netdev;
7563 }
7564 comphy = NULL;
7565 }
7566
Marcin Wojtas3f518502014-07-10 16:52:13 -03007567 if (of_property_read_u32(port_node, "port-id", &id)) {
7568 err = -EINVAL;
7569 dev_err(&pdev->dev, "missing port-id value\n");
7570 goto err_free_netdev;
7571 }
7572
7573 dev->tx_queue_len = MVPP2_MAX_TXD;
7574 dev->watchdog_timeo = 5 * HZ;
7575 dev->netdev_ops = &mvpp2_netdev_ops;
7576 dev->ethtool_ops = &mvpp2_eth_tool_ops;
7577
7578 port = netdev_priv(dev);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007579 port->dev = dev;
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007580 port->ntxqs = ntxqs;
7581 port->nrxqs = nrxqs;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007582 port->priv = priv;
7583 port->has_tx_irqs = has_tx_irqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007584
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007585 err = mvpp2_queue_vectors_init(port, port_node);
7586 if (err)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007587 goto err_free_netdev;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007588
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007589 port->link_irq = of_irq_get_byname(port_node, "link");
7590 if (port->link_irq == -EPROBE_DEFER) {
7591 err = -EPROBE_DEFER;
7592 goto err_deinit_qvecs;
7593 }
7594 if (port->link_irq <= 0)
7595 /* the link irq is optional */
7596 port->link_irq = 0;
7597
Marcin Wojtas3f518502014-07-10 16:52:13 -03007598 if (of_property_read_bool(port_node, "marvell,loopback"))
7599 port->flags |= MVPP2_F_LOOPBACK;
7600
Marcin Wojtas3f518502014-07-10 16:52:13 -03007601 port->id = id;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01007602 if (priv->hw_version == MVPP21)
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007603 port->first_rxq = port->id * port->nrxqs;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01007604 else
7605 port->first_rxq = port->id * priv->max_port_rxqs;
7606
Marcin Wojtas3f518502014-07-10 16:52:13 -03007607 port->phy_node = phy_node;
7608 port->phy_interface = phy_mode;
Antoine Tenart542897d2017-08-30 10:29:15 +02007609 port->comphy = comphy;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007610
Thomas Petazzonia7868412017-03-07 16:53:13 +01007611 if (priv->hw_version == MVPP21) {
7612 res = platform_get_resource(pdev, IORESOURCE_MEM, 2 + id);
7613 port->base = devm_ioremap_resource(&pdev->dev, res);
7614 if (IS_ERR(port->base)) {
7615 err = PTR_ERR(port->base);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007616 goto err_free_irq;
Thomas Petazzonia7868412017-03-07 16:53:13 +01007617 }
7618 } else {
7619 if (of_property_read_u32(port_node, "gop-port-id",
7620 &port->gop_id)) {
7621 err = -EINVAL;
7622 dev_err(&pdev->dev, "missing gop-port-id value\n");
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007623 goto err_deinit_qvecs;
Thomas Petazzonia7868412017-03-07 16:53:13 +01007624 }
7625
7626 port->base = priv->iface_base + MVPP22_GMAC_BASE(port->gop_id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007627 }
7628
7629 /* Alloc per-cpu stats */
7630 port->stats = netdev_alloc_pcpu_stats(struct mvpp2_pcpu_stats);
7631 if (!port->stats) {
7632 err = -ENOMEM;
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007633 goto err_free_irq;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007634 }
7635
Antoine Tenart3ba8c812017-09-02 11:06:47 +02007636 mvpp2_port_copy_mac_addr(dev, priv, port_node, &mac_from);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007637
7638 port->tx_ring_size = MVPP2_MAX_TXD;
7639 port->rx_ring_size = MVPP2_MAX_RXD;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007640 SET_NETDEV_DEV(dev, &pdev->dev);
7641
7642 err = mvpp2_port_init(port);
7643 if (err < 0) {
7644 dev_err(&pdev->dev, "failed to init port %d\n", id);
7645 goto err_free_stats;
7646 }
Thomas Petazzoni26975822017-03-07 16:53:14 +01007647
Thomas Petazzoni26975822017-03-07 16:53:14 +01007648 mvpp2_port_periodic_xon_disable(port);
7649
7650 if (priv->hw_version == MVPP21)
7651 mvpp2_port_fc_adv_enable(port);
7652
7653 mvpp2_port_reset(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007654
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007655 port->pcpu = alloc_percpu(struct mvpp2_port_pcpu);
7656 if (!port->pcpu) {
7657 err = -ENOMEM;
7658 goto err_free_txq_pcpu;
7659 }
7660
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007661 if (!port->has_tx_irqs) {
7662 for_each_present_cpu(cpu) {
7663 port_pcpu = per_cpu_ptr(port->pcpu, cpu);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007664
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007665 hrtimer_init(&port_pcpu->tx_done_timer, CLOCK_MONOTONIC,
7666 HRTIMER_MODE_REL_PINNED);
7667 port_pcpu->tx_done_timer.function = mvpp2_hr_timer_cb;
7668 port_pcpu->timer_scheduled = false;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007669
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007670 tasklet_init(&port_pcpu->tx_done_tasklet,
7671 mvpp2_tx_proc_cb,
7672 (unsigned long)dev);
7673 }
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007674 }
7675
Antoine Ténart186cd4d2017-08-23 09:46:56 +02007676 features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007677 dev->features = features | NETIF_F_RXCSUM;
7678 dev->hw_features |= features | NETIF_F_RXCSUM | NETIF_F_GRO;
7679 dev->vlan_features |= features;
7680
Jarod Wilson57779872016-10-17 15:54:06 -04007681 /* MTU range: 68 - 9676 */
7682 dev->min_mtu = ETH_MIN_MTU;
7683 /* 9676 == 9700 - 20 and rounding to 8 */
7684 dev->max_mtu = 9676;
7685
Marcin Wojtas3f518502014-07-10 16:52:13 -03007686 err = register_netdev(dev);
7687 if (err < 0) {
7688 dev_err(&pdev->dev, "failed to register netdev\n");
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007689 goto err_free_port_pcpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007690 }
7691 netdev_info(dev, "Using %s mac address %pM\n", mac_from, dev->dev_addr);
7692
Yan Markman6bf69a12017-09-25 14:59:47 +02007693 priv->port_list[index] = port;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007694 return 0;
7695
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007696err_free_port_pcpu:
7697 free_percpu(port->pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007698err_free_txq_pcpu:
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007699 for (i = 0; i < port->ntxqs; i++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007700 free_percpu(port->txqs[i]->pcpu);
7701err_free_stats:
7702 free_percpu(port->stats);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007703err_free_irq:
7704 if (port->link_irq)
7705 irq_dispose_mapping(port->link_irq);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007706err_deinit_qvecs:
7707 mvpp2_queue_vectors_deinit(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007708err_free_netdev:
Peter Chenccb80392016-08-01 15:02:37 +08007709 of_node_put(phy_node);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007710 free_netdev(dev);
7711 return err;
7712}
7713
7714/* Ports removal routine */
7715static void mvpp2_port_remove(struct mvpp2_port *port)
7716{
7717 int i;
7718
7719 unregister_netdev(port->dev);
Peter Chenccb80392016-08-01 15:02:37 +08007720 of_node_put(port->phy_node);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007721 free_percpu(port->pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007722 free_percpu(port->stats);
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007723 for (i = 0; i < port->ntxqs; i++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007724 free_percpu(port->txqs[i]->pcpu);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007725 mvpp2_queue_vectors_deinit(port);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007726 if (port->link_irq)
7727 irq_dispose_mapping(port->link_irq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007728 free_netdev(port->dev);
7729}
7730
7731/* Initialize decoding windows */
7732static void mvpp2_conf_mbus_windows(const struct mbus_dram_target_info *dram,
7733 struct mvpp2 *priv)
7734{
7735 u32 win_enable;
7736 int i;
7737
7738 for (i = 0; i < 6; i++) {
7739 mvpp2_write(priv, MVPP2_WIN_BASE(i), 0);
7740 mvpp2_write(priv, MVPP2_WIN_SIZE(i), 0);
7741
7742 if (i < 4)
7743 mvpp2_write(priv, MVPP2_WIN_REMAP(i), 0);
7744 }
7745
7746 win_enable = 0;
7747
7748 for (i = 0; i < dram->num_cs; i++) {
7749 const struct mbus_dram_window *cs = dram->cs + i;
7750
7751 mvpp2_write(priv, MVPP2_WIN_BASE(i),
7752 (cs->base & 0xffff0000) | (cs->mbus_attr << 8) |
7753 dram->mbus_dram_target_id);
7754
7755 mvpp2_write(priv, MVPP2_WIN_SIZE(i),
7756 (cs->size - 1) & 0xffff0000);
7757
7758 win_enable |= (1 << i);
7759 }
7760
7761 mvpp2_write(priv, MVPP2_BASE_ADDR_ENABLE, win_enable);
7762}
7763
7764/* Initialize Rx FIFO's */
7765static void mvpp2_rx_fifo_init(struct mvpp2 *priv)
7766{
7767 int port;
7768
7769 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
7770 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
Antoine Tenart2d1d7df2017-10-30 11:23:28 +01007771 MVPP2_RX_FIFO_PORT_DATA_SIZE_4KB);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007772 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
Antoine Tenart2d1d7df2017-10-30 11:23:28 +01007773 MVPP2_RX_FIFO_PORT_ATTR_SIZE_4KB);
7774 }
7775
7776 mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
7777 MVPP2_RX_FIFO_PORT_MIN_PKT);
7778 mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
7779}
7780
7781static void mvpp22_rx_fifo_init(struct mvpp2 *priv)
7782{
7783 int port;
7784
7785 /* The FIFO size parameters are set depending on the maximum speed a
7786 * given port can handle:
7787 * - Port 0: 10Gbps
7788 * - Port 1: 2.5Gbps
7789 * - Ports 2 and 3: 1Gbps
7790 */
7791
7792 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(0),
7793 MVPP2_RX_FIFO_PORT_DATA_SIZE_32KB);
7794 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(0),
7795 MVPP2_RX_FIFO_PORT_ATTR_SIZE_32KB);
7796
7797 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(1),
7798 MVPP2_RX_FIFO_PORT_DATA_SIZE_8KB);
7799 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(1),
7800 MVPP2_RX_FIFO_PORT_ATTR_SIZE_8KB);
7801
7802 for (port = 2; port < MVPP2_MAX_PORTS; port++) {
7803 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
7804 MVPP2_RX_FIFO_PORT_DATA_SIZE_4KB);
7805 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
7806 MVPP2_RX_FIFO_PORT_ATTR_SIZE_4KB);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007807 }
7808
7809 mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
7810 MVPP2_RX_FIFO_PORT_MIN_PKT);
7811 mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
7812}
7813
Thomas Petazzoni6763ce32017-03-07 16:53:15 +01007814static void mvpp2_axi_init(struct mvpp2 *priv)
7815{
7816 u32 val, rdval, wrval;
7817
7818 mvpp2_write(priv, MVPP22_BM_ADDR_HIGH_RLS_REG, 0x0);
7819
7820 /* AXI Bridge Configuration */
7821
7822 rdval = MVPP22_AXI_CODE_CACHE_RD_CACHE
7823 << MVPP22_AXI_ATTR_CACHE_OFFS;
7824 rdval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
7825 << MVPP22_AXI_ATTR_DOMAIN_OFFS;
7826
7827 wrval = MVPP22_AXI_CODE_CACHE_WR_CACHE
7828 << MVPP22_AXI_ATTR_CACHE_OFFS;
7829 wrval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
7830 << MVPP22_AXI_ATTR_DOMAIN_OFFS;
7831
7832 /* BM */
7833 mvpp2_write(priv, MVPP22_AXI_BM_WR_ATTR_REG, wrval);
7834 mvpp2_write(priv, MVPP22_AXI_BM_RD_ATTR_REG, rdval);
7835
7836 /* Descriptors */
7837 mvpp2_write(priv, MVPP22_AXI_AGGRQ_DESCR_RD_ATTR_REG, rdval);
7838 mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_WR_ATTR_REG, wrval);
7839 mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_RD_ATTR_REG, rdval);
7840 mvpp2_write(priv, MVPP22_AXI_RXQ_DESCR_WR_ATTR_REG, wrval);
7841
7842 /* Buffer Data */
7843 mvpp2_write(priv, MVPP22_AXI_TX_DATA_RD_ATTR_REG, rdval);
7844 mvpp2_write(priv, MVPP22_AXI_RX_DATA_WR_ATTR_REG, wrval);
7845
7846 val = MVPP22_AXI_CODE_CACHE_NON_CACHE
7847 << MVPP22_AXI_CODE_CACHE_OFFS;
7848 val |= MVPP22_AXI_CODE_DOMAIN_SYSTEM
7849 << MVPP22_AXI_CODE_DOMAIN_OFFS;
7850 mvpp2_write(priv, MVPP22_AXI_RD_NORMAL_CODE_REG, val);
7851 mvpp2_write(priv, MVPP22_AXI_WR_NORMAL_CODE_REG, val);
7852
7853 val = MVPP22_AXI_CODE_CACHE_RD_CACHE
7854 << MVPP22_AXI_CODE_CACHE_OFFS;
7855 val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
7856 << MVPP22_AXI_CODE_DOMAIN_OFFS;
7857
7858 mvpp2_write(priv, MVPP22_AXI_RD_SNOOP_CODE_REG, val);
7859
7860 val = MVPP22_AXI_CODE_CACHE_WR_CACHE
7861 << MVPP22_AXI_CODE_CACHE_OFFS;
7862 val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
7863 << MVPP22_AXI_CODE_DOMAIN_OFFS;
7864
7865 mvpp2_write(priv, MVPP22_AXI_WR_SNOOP_CODE_REG, val);
7866}
7867
Marcin Wojtas3f518502014-07-10 16:52:13 -03007868/* Initialize network controller common part HW */
7869static int mvpp2_init(struct platform_device *pdev, struct mvpp2 *priv)
7870{
7871 const struct mbus_dram_target_info *dram_target_info;
7872 int err, i;
Marcin Wojtas08a23752014-07-21 13:48:12 -03007873 u32 val;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007874
Marcin Wojtas3f518502014-07-10 16:52:13 -03007875 /* MBUS windows configuration */
7876 dram_target_info = mv_mbus_dram_info();
7877 if (dram_target_info)
7878 mvpp2_conf_mbus_windows(dram_target_info, priv);
7879
Thomas Petazzoni6763ce32017-03-07 16:53:15 +01007880 if (priv->hw_version == MVPP22)
7881 mvpp2_axi_init(priv);
7882
Marcin Wojtas08a23752014-07-21 13:48:12 -03007883 /* Disable HW PHY polling */
Thomas Petazzoni26975822017-03-07 16:53:14 +01007884 if (priv->hw_version == MVPP21) {
7885 val = readl(priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
7886 val |= MVPP2_PHY_AN_STOP_SMI0_MASK;
7887 writel(val, priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
7888 } else {
7889 val = readl(priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
7890 val &= ~MVPP22_SMI_POLLING_EN;
7891 writel(val, priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
7892 }
Marcin Wojtas08a23752014-07-21 13:48:12 -03007893
Marcin Wojtas3f518502014-07-10 16:52:13 -03007894 /* Allocate and initialize aggregated TXQs */
7895 priv->aggr_txqs = devm_kcalloc(&pdev->dev, num_present_cpus(),
Markus Elfringd7ce3ce2017-04-17 08:48:23 +02007896 sizeof(*priv->aggr_txqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03007897 GFP_KERNEL);
7898 if (!priv->aggr_txqs)
7899 return -ENOMEM;
7900
7901 for_each_present_cpu(i) {
7902 priv->aggr_txqs[i].id = i;
7903 priv->aggr_txqs[i].size = MVPP2_AGGR_TXQ_SIZE;
Antoine Ténart85affd72017-08-23 09:46:55 +02007904 err = mvpp2_aggr_txq_init(pdev, &priv->aggr_txqs[i], i, priv);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007905 if (err < 0)
7906 return err;
7907 }
7908
7909 /* Rx Fifo Init */
Antoine Tenart2d1d7df2017-10-30 11:23:28 +01007910 if (priv->hw_version == MVPP21)
7911 mvpp2_rx_fifo_init(priv);
7912 else
7913 mvpp22_rx_fifo_init(priv);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007914
Thomas Petazzoni26975822017-03-07 16:53:14 +01007915 if (priv->hw_version == MVPP21)
7916 writel(MVPP2_EXT_GLOBAL_CTRL_DEFAULT,
7917 priv->lms_base + MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007918
7919 /* Allow cache snoop when transmiting packets */
7920 mvpp2_write(priv, MVPP2_TX_SNOOP_REG, 0x1);
7921
7922 /* Buffer Manager initialization */
7923 err = mvpp2_bm_init(pdev, priv);
7924 if (err < 0)
7925 return err;
7926
7927 /* Parser default initialization */
7928 err = mvpp2_prs_default_init(pdev, priv);
7929 if (err < 0)
7930 return err;
7931
7932 /* Classifier default initialization */
7933 mvpp2_cls_init(priv);
7934
7935 return 0;
7936}
7937
7938static int mvpp2_probe(struct platform_device *pdev)
7939{
7940 struct device_node *dn = pdev->dev.of_node;
7941 struct device_node *port_node;
7942 struct mvpp2 *priv;
7943 struct resource *res;
Thomas Petazzonia7868412017-03-07 16:53:13 +01007944 void __iomem *base;
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02007945 int port_count, i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007946 int err;
7947
Markus Elfring0b92e592017-04-17 08:38:32 +02007948 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007949 if (!priv)
7950 return -ENOMEM;
7951
Thomas Petazzonifaca9242017-03-07 16:53:06 +01007952 priv->hw_version =
7953 (unsigned long)of_device_get_match_data(&pdev->dev);
7954
Marcin Wojtas3f518502014-07-10 16:52:13 -03007955 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thomas Petazzonia7868412017-03-07 16:53:13 +01007956 base = devm_ioremap_resource(&pdev->dev, res);
7957 if (IS_ERR(base))
7958 return PTR_ERR(base);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007959
Thomas Petazzonia7868412017-03-07 16:53:13 +01007960 if (priv->hw_version == MVPP21) {
7961 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
7962 priv->lms_base = devm_ioremap_resource(&pdev->dev, res);
7963 if (IS_ERR(priv->lms_base))
7964 return PTR_ERR(priv->lms_base);
7965 } else {
7966 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
7967 priv->iface_base = devm_ioremap_resource(&pdev->dev, res);
7968 if (IS_ERR(priv->iface_base))
7969 return PTR_ERR(priv->iface_base);
Antoine Ténartf84bf382017-08-22 19:08:27 +02007970
7971 priv->sysctrl_base =
7972 syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
7973 "marvell,system-controller");
7974 if (IS_ERR(priv->sysctrl_base))
7975 /* The system controller regmap is optional for dt
7976 * compatibility reasons. When not provided, the
7977 * configuration of the GoP relies on the
7978 * firmware/bootloader.
7979 */
7980 priv->sysctrl_base = NULL;
Thomas Petazzonia7868412017-03-07 16:53:13 +01007981 }
7982
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02007983 for (i = 0; i < MVPP2_MAX_THREADS; i++) {
Thomas Petazzonia7868412017-03-07 16:53:13 +01007984 u32 addr_space_sz;
7985
7986 addr_space_sz = (priv->hw_version == MVPP21 ?
7987 MVPP21_ADDR_SPACE_SZ : MVPP22_ADDR_SPACE_SZ);
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02007988 priv->swth_base[i] = base + i * addr_space_sz;
Thomas Petazzonia7868412017-03-07 16:53:13 +01007989 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03007990
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01007991 if (priv->hw_version == MVPP21)
7992 priv->max_port_rxqs = 8;
7993 else
7994 priv->max_port_rxqs = 32;
7995
Marcin Wojtas3f518502014-07-10 16:52:13 -03007996 priv->pp_clk = devm_clk_get(&pdev->dev, "pp_clk");
7997 if (IS_ERR(priv->pp_clk))
7998 return PTR_ERR(priv->pp_clk);
7999 err = clk_prepare_enable(priv->pp_clk);
8000 if (err < 0)
8001 return err;
8002
8003 priv->gop_clk = devm_clk_get(&pdev->dev, "gop_clk");
8004 if (IS_ERR(priv->gop_clk)) {
8005 err = PTR_ERR(priv->gop_clk);
8006 goto err_pp_clk;
8007 }
8008 err = clk_prepare_enable(priv->gop_clk);
8009 if (err < 0)
8010 goto err_pp_clk;
8011
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008012 if (priv->hw_version == MVPP22) {
8013 priv->mg_clk = devm_clk_get(&pdev->dev, "mg_clk");
8014 if (IS_ERR(priv->mg_clk)) {
8015 err = PTR_ERR(priv->mg_clk);
8016 goto err_gop_clk;
8017 }
8018
8019 err = clk_prepare_enable(priv->mg_clk);
8020 if (err < 0)
8021 goto err_gop_clk;
Gregory CLEMENT4792ea02017-09-29 14:27:39 +02008022
8023 priv->axi_clk = devm_clk_get(&pdev->dev, "axi_clk");
8024 if (IS_ERR(priv->axi_clk)) {
8025 err = PTR_ERR(priv->axi_clk);
8026 if (err == -EPROBE_DEFER)
8027 goto err_gop_clk;
8028 priv->axi_clk = NULL;
8029 } else {
8030 err = clk_prepare_enable(priv->axi_clk);
8031 if (err < 0)
8032 goto err_gop_clk;
8033 }
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008034 }
8035
Marcin Wojtas3f518502014-07-10 16:52:13 -03008036 /* Get system's tclk rate */
8037 priv->tclk = clk_get_rate(priv->pp_clk);
8038
Thomas Petazzoni2067e0a2017-03-07 16:53:19 +01008039 if (priv->hw_version == MVPP22) {
8040 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(40));
8041 if (err)
8042 goto err_mg_clk;
8043 /* Sadly, the BM pools all share the same register to
8044 * store the high 32 bits of their address. So they
8045 * must all have the same high 32 bits, which forces
8046 * us to restrict coherent memory to DMA_BIT_MASK(32).
8047 */
8048 err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
8049 if (err)
8050 goto err_mg_clk;
8051 }
8052
Marcin Wojtas3f518502014-07-10 16:52:13 -03008053 /* Initialize network controller */
8054 err = mvpp2_init(pdev, priv);
8055 if (err < 0) {
8056 dev_err(&pdev->dev, "failed to initialize controller\n");
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008057 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008058 }
8059
8060 port_count = of_get_available_child_count(dn);
8061 if (port_count == 0) {
8062 dev_err(&pdev->dev, "no ports enabled\n");
Wei Yongjun575a1932014-07-20 22:02:43 +08008063 err = -ENODEV;
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008064 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008065 }
8066
8067 priv->port_list = devm_kcalloc(&pdev->dev, port_count,
Markus Elfring0b92e592017-04-17 08:38:32 +02008068 sizeof(*priv->port_list),
8069 GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008070 if (!priv->port_list) {
8071 err = -ENOMEM;
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008072 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008073 }
8074
8075 /* Initialize ports */
Yan Markman6bf69a12017-09-25 14:59:47 +02008076 i = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008077 for_each_available_child_of_node(dn, port_node) {
Yan Markman6bf69a12017-09-25 14:59:47 +02008078 err = mvpp2_port_probe(pdev, port_node, priv, i);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008079 if (err < 0)
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008080 goto err_mg_clk;
Yan Markman6bf69a12017-09-25 14:59:47 +02008081 i++;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008082 }
8083
8084 platform_set_drvdata(pdev, priv);
8085 return 0;
8086
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008087err_mg_clk:
Gregory CLEMENT4792ea02017-09-29 14:27:39 +02008088 clk_disable_unprepare(priv->axi_clk);
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008089 if (priv->hw_version == MVPP22)
8090 clk_disable_unprepare(priv->mg_clk);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008091err_gop_clk:
8092 clk_disable_unprepare(priv->gop_clk);
8093err_pp_clk:
8094 clk_disable_unprepare(priv->pp_clk);
8095 return err;
8096}
8097
8098static int mvpp2_remove(struct platform_device *pdev)
8099{
8100 struct mvpp2 *priv = platform_get_drvdata(pdev);
8101 struct device_node *dn = pdev->dev.of_node;
8102 struct device_node *port_node;
8103 int i = 0;
8104
8105 for_each_available_child_of_node(dn, port_node) {
8106 if (priv->port_list[i])
8107 mvpp2_port_remove(priv->port_list[i]);
8108 i++;
8109 }
8110
8111 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
8112 struct mvpp2_bm_pool *bm_pool = &priv->bm_pools[i];
8113
8114 mvpp2_bm_pool_destroy(pdev, priv, bm_pool);
8115 }
8116
8117 for_each_present_cpu(i) {
8118 struct mvpp2_tx_queue *aggr_txq = &priv->aggr_txqs[i];
8119
8120 dma_free_coherent(&pdev->dev,
8121 MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
8122 aggr_txq->descs,
Thomas Petazzoni20396132017-03-07 16:53:00 +01008123 aggr_txq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008124 }
8125
Gregory CLEMENT4792ea02017-09-29 14:27:39 +02008126 clk_disable_unprepare(priv->axi_clk);
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008127 clk_disable_unprepare(priv->mg_clk);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008128 clk_disable_unprepare(priv->pp_clk);
8129 clk_disable_unprepare(priv->gop_clk);
8130
8131 return 0;
8132}
8133
8134static const struct of_device_id mvpp2_match[] = {
Thomas Petazzonifaca9242017-03-07 16:53:06 +01008135 {
8136 .compatible = "marvell,armada-375-pp2",
8137 .data = (void *)MVPP21,
8138 },
Thomas Petazzonifc5e1552017-03-07 16:53:20 +01008139 {
8140 .compatible = "marvell,armada-7k-pp22",
8141 .data = (void *)MVPP22,
8142 },
Marcin Wojtas3f518502014-07-10 16:52:13 -03008143 { }
8144};
8145MODULE_DEVICE_TABLE(of, mvpp2_match);
8146
8147static struct platform_driver mvpp2_driver = {
8148 .probe = mvpp2_probe,
8149 .remove = mvpp2_remove,
8150 .driver = {
8151 .name = MVPP2_DRIVER_NAME,
8152 .of_match_table = mvpp2_match,
8153 },
8154};
8155
8156module_platform_driver(mvpp2_driver);
8157
8158MODULE_DESCRIPTION("Marvell PPv2 Ethernet Driver - www.marvell.com");
8159MODULE_AUTHOR("Marcin Wojtas <mw@semihalf.com>");
Ezequiel Garciac6340992014-07-14 10:34:47 -03008160MODULE_LICENSE("GPL v2");