blob: 1610555647201d13bdda2087ad3f19f85c2513d1 [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 */
507#define MVPP2_RX_FIFO_PORT_DATA_SIZE 0x2000
508#define MVPP2_RX_FIFO_PORT_ATTR_SIZE 0x80
509#define MVPP2_RX_FIFO_PORT_MIN_PKT 0x80
510
511/* RX buffer constants */
512#define MVPP2_SKB_SHINFO_SIZE \
513 SKB_DATA_ALIGN(sizeof(struct skb_shared_info))
514
515#define MVPP2_RX_PKT_SIZE(mtu) \
516 ALIGN((mtu) + MVPP2_MH_SIZE + MVPP2_VLAN_TAG_LEN + \
Jisheng Zhang4a0a12d2016-04-01 17:11:05 +0800517 ETH_HLEN + ETH_FCS_LEN, cache_line_size())
Marcin Wojtas3f518502014-07-10 16:52:13 -0300518
519#define MVPP2_RX_BUF_SIZE(pkt_size) ((pkt_size) + NET_SKB_PAD)
520#define MVPP2_RX_TOTAL_SIZE(buf_size) ((buf_size) + MVPP2_SKB_SHINFO_SIZE)
521#define MVPP2_RX_MAX_PKT_SIZE(total_size) \
522 ((total_size) - NET_SKB_PAD - MVPP2_SKB_SHINFO_SIZE)
523
524#define MVPP2_BIT_TO_BYTE(bit) ((bit) / 8)
525
526/* IPv6 max L3 address size */
527#define MVPP2_MAX_L3_ADDR_SIZE 16
528
529/* Port flags */
530#define MVPP2_F_LOOPBACK BIT(0)
531
532/* Marvell tag types */
533enum mvpp2_tag_type {
534 MVPP2_TAG_TYPE_NONE = 0,
535 MVPP2_TAG_TYPE_MH = 1,
536 MVPP2_TAG_TYPE_DSA = 2,
537 MVPP2_TAG_TYPE_EDSA = 3,
538 MVPP2_TAG_TYPE_VLAN = 4,
539 MVPP2_TAG_TYPE_LAST = 5
540};
541
542/* Parser constants */
543#define MVPP2_PRS_TCAM_SRAM_SIZE 256
544#define MVPP2_PRS_TCAM_WORDS 6
545#define MVPP2_PRS_SRAM_WORDS 4
546#define MVPP2_PRS_FLOW_ID_SIZE 64
547#define MVPP2_PRS_FLOW_ID_MASK 0x3f
548#define MVPP2_PRS_TCAM_ENTRY_INVALID 1
549#define MVPP2_PRS_TCAM_DSA_TAGGED_BIT BIT(5)
550#define MVPP2_PRS_IPV4_HEAD 0x40
551#define MVPP2_PRS_IPV4_HEAD_MASK 0xf0
552#define MVPP2_PRS_IPV4_MC 0xe0
553#define MVPP2_PRS_IPV4_MC_MASK 0xf0
554#define MVPP2_PRS_IPV4_BC_MASK 0xff
555#define MVPP2_PRS_IPV4_IHL 0x5
556#define MVPP2_PRS_IPV4_IHL_MASK 0xf
557#define MVPP2_PRS_IPV6_MC 0xff
558#define MVPP2_PRS_IPV6_MC_MASK 0xff
559#define MVPP2_PRS_IPV6_HOP_MASK 0xff
560#define MVPP2_PRS_TCAM_PROTO_MASK 0xff
561#define MVPP2_PRS_TCAM_PROTO_MASK_L 0x3f
562#define MVPP2_PRS_DBL_VLANS_MAX 100
563
564/* Tcam structure:
565 * - lookup ID - 4 bits
566 * - port ID - 1 byte
567 * - additional information - 1 byte
568 * - header data - 8 bytes
569 * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(5)->(0).
570 */
571#define MVPP2_PRS_AI_BITS 8
572#define MVPP2_PRS_PORT_MASK 0xff
573#define MVPP2_PRS_LU_MASK 0xf
574#define MVPP2_PRS_TCAM_DATA_BYTE(offs) \
575 (((offs) - ((offs) % 2)) * 2 + ((offs) % 2))
576#define MVPP2_PRS_TCAM_DATA_BYTE_EN(offs) \
577 (((offs) * 2) - ((offs) % 2) + 2)
578#define MVPP2_PRS_TCAM_AI_BYTE 16
579#define MVPP2_PRS_TCAM_PORT_BYTE 17
580#define MVPP2_PRS_TCAM_LU_BYTE 20
581#define MVPP2_PRS_TCAM_EN_OFFS(offs) ((offs) + 2)
582#define MVPP2_PRS_TCAM_INV_WORD 5
583/* Tcam entries ID */
584#define MVPP2_PE_DROP_ALL 0
585#define MVPP2_PE_FIRST_FREE_TID 1
586#define MVPP2_PE_LAST_FREE_TID (MVPP2_PRS_TCAM_SRAM_SIZE - 31)
587#define MVPP2_PE_IP6_EXT_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 30)
588#define MVPP2_PE_MAC_MC_IP6 (MVPP2_PRS_TCAM_SRAM_SIZE - 29)
589#define MVPP2_PE_IP6_ADDR_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 28)
590#define MVPP2_PE_IP4_ADDR_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 27)
591#define MVPP2_PE_LAST_DEFAULT_FLOW (MVPP2_PRS_TCAM_SRAM_SIZE - 26)
592#define MVPP2_PE_FIRST_DEFAULT_FLOW (MVPP2_PRS_TCAM_SRAM_SIZE - 19)
593#define MVPP2_PE_EDSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 18)
594#define MVPP2_PE_EDSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 17)
595#define MVPP2_PE_DSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 16)
596#define MVPP2_PE_DSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 15)
597#define MVPP2_PE_ETYPE_EDSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 14)
598#define MVPP2_PE_ETYPE_EDSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 13)
599#define MVPP2_PE_ETYPE_DSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 12)
600#define MVPP2_PE_ETYPE_DSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 11)
601#define MVPP2_PE_MH_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 10)
602#define MVPP2_PE_DSA_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 9)
603#define MVPP2_PE_IP6_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 8)
604#define MVPP2_PE_IP4_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 7)
605#define MVPP2_PE_ETH_TYPE_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 6)
606#define MVPP2_PE_VLAN_DBL (MVPP2_PRS_TCAM_SRAM_SIZE - 5)
607#define MVPP2_PE_VLAN_NONE (MVPP2_PRS_TCAM_SRAM_SIZE - 4)
608#define MVPP2_PE_MAC_MC_ALL (MVPP2_PRS_TCAM_SRAM_SIZE - 3)
609#define MVPP2_PE_MAC_PROMISCUOUS (MVPP2_PRS_TCAM_SRAM_SIZE - 2)
610#define MVPP2_PE_MAC_NON_PROMISCUOUS (MVPP2_PRS_TCAM_SRAM_SIZE - 1)
611
612/* Sram structure
613 * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(3)->(0).
614 */
615#define MVPP2_PRS_SRAM_RI_OFFS 0
616#define MVPP2_PRS_SRAM_RI_WORD 0
617#define MVPP2_PRS_SRAM_RI_CTRL_OFFS 32
618#define MVPP2_PRS_SRAM_RI_CTRL_WORD 1
619#define MVPP2_PRS_SRAM_RI_CTRL_BITS 32
620#define MVPP2_PRS_SRAM_SHIFT_OFFS 64
621#define MVPP2_PRS_SRAM_SHIFT_SIGN_BIT 72
622#define MVPP2_PRS_SRAM_UDF_OFFS 73
623#define MVPP2_PRS_SRAM_UDF_BITS 8
624#define MVPP2_PRS_SRAM_UDF_MASK 0xff
625#define MVPP2_PRS_SRAM_UDF_SIGN_BIT 81
626#define MVPP2_PRS_SRAM_UDF_TYPE_OFFS 82
627#define MVPP2_PRS_SRAM_UDF_TYPE_MASK 0x7
628#define MVPP2_PRS_SRAM_UDF_TYPE_L3 1
629#define MVPP2_PRS_SRAM_UDF_TYPE_L4 4
630#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS 85
631#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK 0x3
632#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD 1
633#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP4_ADD 2
634#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP6_ADD 3
635#define MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS 87
636#define MVPP2_PRS_SRAM_OP_SEL_UDF_BITS 2
637#define MVPP2_PRS_SRAM_OP_SEL_UDF_MASK 0x3
638#define MVPP2_PRS_SRAM_OP_SEL_UDF_ADD 0
639#define MVPP2_PRS_SRAM_OP_SEL_UDF_IP4_ADD 2
640#define MVPP2_PRS_SRAM_OP_SEL_UDF_IP6_ADD 3
641#define MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS 89
642#define MVPP2_PRS_SRAM_AI_OFFS 90
643#define MVPP2_PRS_SRAM_AI_CTRL_OFFS 98
644#define MVPP2_PRS_SRAM_AI_CTRL_BITS 8
645#define MVPP2_PRS_SRAM_AI_MASK 0xff
646#define MVPP2_PRS_SRAM_NEXT_LU_OFFS 106
647#define MVPP2_PRS_SRAM_NEXT_LU_MASK 0xf
648#define MVPP2_PRS_SRAM_LU_DONE_BIT 110
649#define MVPP2_PRS_SRAM_LU_GEN_BIT 111
650
651/* Sram result info bits assignment */
652#define MVPP2_PRS_RI_MAC_ME_MASK 0x1
653#define MVPP2_PRS_RI_DSA_MASK 0x2
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100654#define MVPP2_PRS_RI_VLAN_MASK (BIT(2) | BIT(3))
655#define MVPP2_PRS_RI_VLAN_NONE 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300656#define MVPP2_PRS_RI_VLAN_SINGLE BIT(2)
657#define MVPP2_PRS_RI_VLAN_DOUBLE BIT(3)
658#define MVPP2_PRS_RI_VLAN_TRIPLE (BIT(2) | BIT(3))
659#define MVPP2_PRS_RI_CPU_CODE_MASK 0x70
660#define MVPP2_PRS_RI_CPU_CODE_RX_SPEC BIT(4)
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100661#define MVPP2_PRS_RI_L2_CAST_MASK (BIT(9) | BIT(10))
662#define MVPP2_PRS_RI_L2_UCAST 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300663#define MVPP2_PRS_RI_L2_MCAST BIT(9)
664#define MVPP2_PRS_RI_L2_BCAST BIT(10)
665#define MVPP2_PRS_RI_PPPOE_MASK 0x800
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100666#define MVPP2_PRS_RI_L3_PROTO_MASK (BIT(12) | BIT(13) | BIT(14))
667#define MVPP2_PRS_RI_L3_UN 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300668#define MVPP2_PRS_RI_L3_IP4 BIT(12)
669#define MVPP2_PRS_RI_L3_IP4_OPT BIT(13)
670#define MVPP2_PRS_RI_L3_IP4_OTHER (BIT(12) | BIT(13))
671#define MVPP2_PRS_RI_L3_IP6 BIT(14)
672#define MVPP2_PRS_RI_L3_IP6_EXT (BIT(12) | BIT(14))
673#define MVPP2_PRS_RI_L3_ARP (BIT(13) | BIT(14))
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100674#define MVPP2_PRS_RI_L3_ADDR_MASK (BIT(15) | BIT(16))
675#define MVPP2_PRS_RI_L3_UCAST 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300676#define MVPP2_PRS_RI_L3_MCAST BIT(15)
677#define MVPP2_PRS_RI_L3_BCAST (BIT(15) | BIT(16))
678#define MVPP2_PRS_RI_IP_FRAG_MASK 0x20000
Stefan Chulskiaff3da32017-09-25 14:59:46 +0200679#define MVPP2_PRS_RI_IP_FRAG_TRUE BIT(17)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300680#define MVPP2_PRS_RI_UDF3_MASK 0x300000
681#define MVPP2_PRS_RI_UDF3_RX_SPECIAL BIT(21)
682#define MVPP2_PRS_RI_L4_PROTO_MASK 0x1c00000
683#define MVPP2_PRS_RI_L4_TCP BIT(22)
684#define MVPP2_PRS_RI_L4_UDP BIT(23)
685#define MVPP2_PRS_RI_L4_OTHER (BIT(22) | BIT(23))
686#define MVPP2_PRS_RI_UDF7_MASK 0x60000000
687#define MVPP2_PRS_RI_UDF7_IP6_LITE BIT(29)
688#define MVPP2_PRS_RI_DROP_MASK 0x80000000
689
690/* Sram additional info bits assignment */
691#define MVPP2_PRS_IPV4_DIP_AI_BIT BIT(0)
692#define MVPP2_PRS_IPV6_NO_EXT_AI_BIT BIT(0)
693#define MVPP2_PRS_IPV6_EXT_AI_BIT BIT(1)
694#define MVPP2_PRS_IPV6_EXT_AH_AI_BIT BIT(2)
695#define MVPP2_PRS_IPV6_EXT_AH_LEN_AI_BIT BIT(3)
696#define MVPP2_PRS_IPV6_EXT_AH_L4_AI_BIT BIT(4)
697#define MVPP2_PRS_SINGLE_VLAN_AI 0
698#define MVPP2_PRS_DBL_VLAN_AI_BIT BIT(7)
699
700/* DSA/EDSA type */
701#define MVPP2_PRS_TAGGED true
702#define MVPP2_PRS_UNTAGGED false
703#define MVPP2_PRS_EDSA true
704#define MVPP2_PRS_DSA false
705
706/* MAC entries, shadow udf */
707enum mvpp2_prs_udf {
708 MVPP2_PRS_UDF_MAC_DEF,
709 MVPP2_PRS_UDF_MAC_RANGE,
710 MVPP2_PRS_UDF_L2_DEF,
711 MVPP2_PRS_UDF_L2_DEF_COPY,
712 MVPP2_PRS_UDF_L2_USER,
713};
714
715/* Lookup ID */
716enum mvpp2_prs_lookup {
717 MVPP2_PRS_LU_MH,
718 MVPP2_PRS_LU_MAC,
719 MVPP2_PRS_LU_DSA,
720 MVPP2_PRS_LU_VLAN,
721 MVPP2_PRS_LU_L2,
722 MVPP2_PRS_LU_PPPOE,
723 MVPP2_PRS_LU_IP4,
724 MVPP2_PRS_LU_IP6,
725 MVPP2_PRS_LU_FLOWS,
726 MVPP2_PRS_LU_LAST,
727};
728
729/* L3 cast enum */
730enum mvpp2_prs_l3_cast {
731 MVPP2_PRS_L3_UNI_CAST,
732 MVPP2_PRS_L3_MULTI_CAST,
733 MVPP2_PRS_L3_BROAD_CAST
734};
735
736/* Classifier constants */
737#define MVPP2_CLS_FLOWS_TBL_SIZE 512
738#define MVPP2_CLS_FLOWS_TBL_DATA_WORDS 3
739#define MVPP2_CLS_LKP_TBL_SIZE 64
740
741/* BM constants */
742#define MVPP2_BM_POOLS_NUM 8
743#define MVPP2_BM_LONG_BUF_NUM 1024
744#define MVPP2_BM_SHORT_BUF_NUM 2048
745#define MVPP2_BM_POOL_SIZE_MAX (16*1024 - MVPP2_BM_POOL_PTR_ALIGN/4)
746#define MVPP2_BM_POOL_PTR_ALIGN 128
747#define MVPP2_BM_SWF_LONG_POOL(port) ((port > 2) ? 2 : port)
748#define MVPP2_BM_SWF_SHORT_POOL 3
749
750/* BM cookie (32 bits) definition */
751#define MVPP2_BM_COOKIE_POOL_OFFS 8
752#define MVPP2_BM_COOKIE_CPU_OFFS 24
753
754/* BM short pool packet size
755 * These value assure that for SWF the total number
756 * of bytes allocated for each buffer will be 512
757 */
758#define MVPP2_BM_SHORT_PKT_SIZE MVPP2_RX_MAX_PKT_SIZE(512)
759
Thomas Petazzonia7868412017-03-07 16:53:13 +0100760#define MVPP21_ADDR_SPACE_SZ 0
761#define MVPP22_ADDR_SPACE_SZ SZ_64K
762
Thomas Petazzonidf089aa2017-08-03 10:41:58 +0200763#define MVPP2_MAX_THREADS 8
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +0200764#define MVPP2_MAX_QVECS MVPP2_MAX_THREADS
Thomas Petazzonia7868412017-03-07 16:53:13 +0100765
Marcin Wojtas3f518502014-07-10 16:52:13 -0300766enum mvpp2_bm_type {
767 MVPP2_BM_FREE,
768 MVPP2_BM_SWF_LONG,
769 MVPP2_BM_SWF_SHORT
770};
771
772/* Definitions */
773
774/* Shared Packet Processor resources */
775struct mvpp2 {
776 /* Shared registers' base addresses */
Marcin Wojtas3f518502014-07-10 16:52:13 -0300777 void __iomem *lms_base;
Thomas Petazzonia7868412017-03-07 16:53:13 +0100778 void __iomem *iface_base;
779
Thomas Petazzonidf089aa2017-08-03 10:41:58 +0200780 /* On PPv2.2, each "software thread" can access the base
781 * register through a separate address space, each 64 KB apart
782 * from each other. Typically, such address spaces will be
783 * used per CPU.
Thomas Petazzonia7868412017-03-07 16:53:13 +0100784 */
Thomas Petazzonidf089aa2017-08-03 10:41:58 +0200785 void __iomem *swth_base[MVPP2_MAX_THREADS];
Marcin Wojtas3f518502014-07-10 16:52:13 -0300786
Antoine Ténartf84bf382017-08-22 19:08:27 +0200787 /* On PPv2.2, some port control registers are located into the system
788 * controller space. These registers are accessible through a regmap.
789 */
790 struct regmap *sysctrl_base;
791
Marcin Wojtas3f518502014-07-10 16:52:13 -0300792 /* Common clocks */
793 struct clk *pp_clk;
794 struct clk *gop_clk;
Thomas Petazzonifceb55d2017-03-07 16:53:18 +0100795 struct clk *mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300796
797 /* List of pointers to port structures */
798 struct mvpp2_port **port_list;
799
800 /* Aggregated TXQs */
801 struct mvpp2_tx_queue *aggr_txqs;
802
803 /* BM pools */
804 struct mvpp2_bm_pool *bm_pools;
805
806 /* PRS shadow table */
807 struct mvpp2_prs_shadow *prs_shadow;
808 /* PRS auxiliary table for double vlan entries control */
809 bool *prs_double_vlans;
810
811 /* Tclk value */
812 u32 tclk;
Thomas Petazzonifaca9242017-03-07 16:53:06 +0100813
814 /* HW version */
815 enum { MVPP21, MVPP22 } hw_version;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +0100816
817 /* Maximum number of RXQs per port */
818 unsigned int max_port_rxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300819};
820
821struct mvpp2_pcpu_stats {
822 struct u64_stats_sync syncp;
823 u64 rx_packets;
824 u64 rx_bytes;
825 u64 tx_packets;
826 u64 tx_bytes;
827};
828
Marcin Wojtasedc660f2015-08-06 19:00:30 +0200829/* Per-CPU port control */
830struct mvpp2_port_pcpu {
831 struct hrtimer tx_done_timer;
832 bool timer_scheduled;
833 /* Tasklet for egress finalization */
834 struct tasklet_struct tx_done_tasklet;
835};
836
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +0200837struct mvpp2_queue_vector {
838 int irq;
839 struct napi_struct napi;
840 enum { MVPP2_QUEUE_VECTOR_SHARED, MVPP2_QUEUE_VECTOR_PRIVATE } type;
841 int sw_thread_id;
842 u16 sw_thread_mask;
843 int first_rxq;
844 int nrxqs;
845 u32 pending_cause_rx;
846 struct mvpp2_port *port;
847};
848
Marcin Wojtas3f518502014-07-10 16:52:13 -0300849struct mvpp2_port {
850 u8 id;
851
Thomas Petazzonia7868412017-03-07 16:53:13 +0100852 /* Index of the port from the "group of ports" complex point
853 * of view
854 */
855 int gop_id;
856
Antoine Tenartfd3651b2017-09-01 11:04:54 +0200857 int link_irq;
858
Marcin Wojtas3f518502014-07-10 16:52:13 -0300859 struct mvpp2 *priv;
860
861 /* Per-port registers' base address */
862 void __iomem *base;
863
864 struct mvpp2_rx_queue **rxqs;
Thomas Petazzoni09f83972017-08-03 10:41:57 +0200865 unsigned int nrxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300866 struct mvpp2_tx_queue **txqs;
Thomas Petazzoni09f83972017-08-03 10:41:57 +0200867 unsigned int ntxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300868 struct net_device *dev;
869
870 int pkt_size;
871
Marcin Wojtasedc660f2015-08-06 19:00:30 +0200872 /* Per-CPU port control */
873 struct mvpp2_port_pcpu __percpu *pcpu;
874
Marcin Wojtas3f518502014-07-10 16:52:13 -0300875 /* Flags */
876 unsigned long flags;
877
878 u16 tx_ring_size;
879 u16 rx_ring_size;
880 struct mvpp2_pcpu_stats __percpu *stats;
881
Marcin Wojtas3f518502014-07-10 16:52:13 -0300882 phy_interface_t phy_interface;
883 struct device_node *phy_node;
Antoine Tenart542897d2017-08-30 10:29:15 +0200884 struct phy *comphy;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300885 unsigned int link;
886 unsigned int duplex;
887 unsigned int speed;
888
889 struct mvpp2_bm_pool *pool_long;
890 struct mvpp2_bm_pool *pool_short;
891
892 /* Index of first port's physical RXQ */
893 u8 first_rxq;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +0200894
895 struct mvpp2_queue_vector qvecs[MVPP2_MAX_QVECS];
896 unsigned int nqvecs;
Thomas Petazzoni213f4282017-08-03 10:42:00 +0200897 bool has_tx_irqs;
898
899 u32 tx_time_coal;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300900};
901
902/* The mvpp2_tx_desc and mvpp2_rx_desc structures describe the
903 * layout of the transmit and reception DMA descriptors, and their
904 * layout is therefore defined by the hardware design
905 */
906
907#define MVPP2_TXD_L3_OFF_SHIFT 0
908#define MVPP2_TXD_IP_HLEN_SHIFT 8
909#define MVPP2_TXD_L4_CSUM_FRAG BIT(13)
910#define MVPP2_TXD_L4_CSUM_NOT BIT(14)
911#define MVPP2_TXD_IP_CSUM_DISABLE BIT(15)
912#define MVPP2_TXD_PADDING_DISABLE BIT(23)
913#define MVPP2_TXD_L4_UDP BIT(24)
914#define MVPP2_TXD_L3_IP6 BIT(26)
915#define MVPP2_TXD_L_DESC BIT(28)
916#define MVPP2_TXD_F_DESC BIT(29)
917
918#define MVPP2_RXD_ERR_SUMMARY BIT(15)
919#define MVPP2_RXD_ERR_CODE_MASK (BIT(13) | BIT(14))
920#define MVPP2_RXD_ERR_CRC 0x0
921#define MVPP2_RXD_ERR_OVERRUN BIT(13)
922#define MVPP2_RXD_ERR_RESOURCE (BIT(13) | BIT(14))
923#define MVPP2_RXD_BM_POOL_ID_OFFS 16
924#define MVPP2_RXD_BM_POOL_ID_MASK (BIT(16) | BIT(17) | BIT(18))
925#define MVPP2_RXD_HWF_SYNC BIT(21)
926#define MVPP2_RXD_L4_CSUM_OK BIT(22)
927#define MVPP2_RXD_IP4_HEADER_ERR BIT(24)
928#define MVPP2_RXD_L4_TCP BIT(25)
929#define MVPP2_RXD_L4_UDP BIT(26)
930#define MVPP2_RXD_L3_IP4 BIT(28)
931#define MVPP2_RXD_L3_IP6 BIT(30)
932#define MVPP2_RXD_BUF_HDR BIT(31)
933
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100934/* HW TX descriptor for PPv2.1 */
935struct mvpp21_tx_desc {
Marcin Wojtas3f518502014-07-10 16:52:13 -0300936 u32 command; /* Options used by HW for packet transmitting.*/
937 u8 packet_offset; /* the offset from the buffer beginning */
938 u8 phys_txq; /* destination queue ID */
939 u16 data_size; /* data size of transmitted packet in bytes */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100940 u32 buf_dma_addr; /* physical addr of transmitted buffer */
Marcin Wojtas3f518502014-07-10 16:52:13 -0300941 u32 buf_cookie; /* cookie for access to TX buffer in tx path */
942 u32 reserved1[3]; /* hw_cmd (for future use, BM, PON, PNC) */
943 u32 reserved2; /* reserved (for future use) */
944};
945
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100946/* HW RX descriptor for PPv2.1 */
947struct mvpp21_rx_desc {
Marcin Wojtas3f518502014-07-10 16:52:13 -0300948 u32 status; /* info about received packet */
949 u16 reserved1; /* parser_info (for future use, PnC) */
950 u16 data_size; /* size of received packet in bytes */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100951 u32 buf_dma_addr; /* physical address of the buffer */
Marcin Wojtas3f518502014-07-10 16:52:13 -0300952 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
953 u16 reserved2; /* gem_port_id (for future use, PON) */
954 u16 reserved3; /* csum_l4 (for future use, PnC) */
955 u8 reserved4; /* bm_qset (for future use, BM) */
956 u8 reserved5;
957 u16 reserved6; /* classify_info (for future use, PnC) */
958 u32 reserved7; /* flow_id (for future use, PnC) */
959 u32 reserved8;
960};
961
Thomas Petazzonie7c53592017-03-07 16:53:08 +0100962/* HW TX descriptor for PPv2.2 */
963struct mvpp22_tx_desc {
964 u32 command;
965 u8 packet_offset;
966 u8 phys_txq;
967 u16 data_size;
968 u64 reserved1;
969 u64 buf_dma_addr_ptp;
970 u64 buf_cookie_misc;
971};
972
973/* HW RX descriptor for PPv2.2 */
974struct mvpp22_rx_desc {
975 u32 status;
976 u16 reserved1;
977 u16 data_size;
978 u32 reserved2;
979 u32 reserved3;
980 u64 buf_dma_addr_key_hash;
981 u64 buf_cookie_misc;
982};
983
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100984/* Opaque type used by the driver to manipulate the HW TX and RX
985 * descriptors
986 */
987struct mvpp2_tx_desc {
988 union {
989 struct mvpp21_tx_desc pp21;
Thomas Petazzonie7c53592017-03-07 16:53:08 +0100990 struct mvpp22_tx_desc pp22;
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100991 };
992};
993
994struct mvpp2_rx_desc {
995 union {
996 struct mvpp21_rx_desc pp21;
Thomas Petazzonie7c53592017-03-07 16:53:08 +0100997 struct mvpp22_rx_desc pp22;
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100998 };
999};
1000
Thomas Petazzoni83544912016-12-21 11:28:49 +01001001struct mvpp2_txq_pcpu_buf {
1002 /* Transmitted SKB */
1003 struct sk_buff *skb;
1004
1005 /* Physical address of transmitted buffer */
Thomas Petazzoni20396132017-03-07 16:53:00 +01001006 dma_addr_t dma;
Thomas Petazzoni83544912016-12-21 11:28:49 +01001007
1008 /* Size transmitted */
1009 size_t size;
1010};
1011
Marcin Wojtas3f518502014-07-10 16:52:13 -03001012/* Per-CPU Tx queue control */
1013struct mvpp2_txq_pcpu {
1014 int cpu;
1015
1016 /* Number of Tx DMA descriptors in the descriptor ring */
1017 int size;
1018
1019 /* Number of currently used Tx DMA descriptor in the
1020 * descriptor ring
1021 */
1022 int count;
1023
1024 /* Number of Tx DMA descriptors reserved for each CPU */
1025 int reserved_num;
1026
Thomas Petazzoni83544912016-12-21 11:28:49 +01001027 /* Infos about transmitted buffers */
1028 struct mvpp2_txq_pcpu_buf *buffs;
Marcin Wojtas71ce3912015-08-06 19:00:29 +02001029
Marcin Wojtas3f518502014-07-10 16:52:13 -03001030 /* Index of last TX DMA descriptor that was inserted */
1031 int txq_put_index;
1032
1033 /* Index of the TX DMA descriptor to be cleaned up */
1034 int txq_get_index;
Antoine Ténart186cd4d2017-08-23 09:46:56 +02001035
1036 /* DMA buffer for TSO headers */
1037 char *tso_headers;
1038 dma_addr_t tso_headers_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001039};
1040
1041struct mvpp2_tx_queue {
1042 /* Physical number of this Tx queue */
1043 u8 id;
1044
1045 /* Logical number of this Tx queue */
1046 u8 log_id;
1047
1048 /* Number of Tx DMA descriptors in the descriptor ring */
1049 int size;
1050
1051 /* Number of currently used Tx DMA descriptor in the descriptor ring */
1052 int count;
1053
1054 /* Per-CPU control of physical Tx queues */
1055 struct mvpp2_txq_pcpu __percpu *pcpu;
1056
Marcin Wojtas3f518502014-07-10 16:52:13 -03001057 u32 done_pkts_coal;
1058
1059 /* Virtual address of thex Tx DMA descriptors array */
1060 struct mvpp2_tx_desc *descs;
1061
1062 /* DMA address of the Tx DMA descriptors array */
Thomas Petazzoni20396132017-03-07 16:53:00 +01001063 dma_addr_t descs_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001064
1065 /* Index of the last Tx DMA descriptor */
1066 int last_desc;
1067
1068 /* Index of the next Tx DMA descriptor to process */
1069 int next_desc_to_proc;
1070};
1071
1072struct mvpp2_rx_queue {
1073 /* RX queue number, in the range 0-31 for physical RXQs */
1074 u8 id;
1075
1076 /* Num of rx descriptors in the rx descriptor ring */
1077 int size;
1078
1079 u32 pkts_coal;
1080 u32 time_coal;
1081
1082 /* Virtual address of the RX DMA descriptors array */
1083 struct mvpp2_rx_desc *descs;
1084
1085 /* DMA address of the RX DMA descriptors array */
Thomas Petazzoni20396132017-03-07 16:53:00 +01001086 dma_addr_t descs_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001087
1088 /* Index of the last RX DMA descriptor */
1089 int last_desc;
1090
1091 /* Index of the next RX DMA descriptor to process */
1092 int next_desc_to_proc;
1093
1094 /* ID of port to which physical RXQ is mapped */
1095 int port;
1096
1097 /* Port's logic RXQ number to which physical RXQ is mapped */
1098 int logic_rxq;
1099};
1100
1101union mvpp2_prs_tcam_entry {
1102 u32 word[MVPP2_PRS_TCAM_WORDS];
1103 u8 byte[MVPP2_PRS_TCAM_WORDS * 4];
1104};
1105
1106union mvpp2_prs_sram_entry {
1107 u32 word[MVPP2_PRS_SRAM_WORDS];
1108 u8 byte[MVPP2_PRS_SRAM_WORDS * 4];
1109};
1110
1111struct mvpp2_prs_entry {
1112 u32 index;
1113 union mvpp2_prs_tcam_entry tcam;
1114 union mvpp2_prs_sram_entry sram;
1115};
1116
1117struct mvpp2_prs_shadow {
1118 bool valid;
1119 bool finish;
1120
1121 /* Lookup ID */
1122 int lu;
1123
1124 /* User defined offset */
1125 int udf;
1126
1127 /* Result info */
1128 u32 ri;
1129 u32 ri_mask;
1130};
1131
1132struct mvpp2_cls_flow_entry {
1133 u32 index;
1134 u32 data[MVPP2_CLS_FLOWS_TBL_DATA_WORDS];
1135};
1136
1137struct mvpp2_cls_lookup_entry {
1138 u32 lkpid;
1139 u32 way;
1140 u32 data;
1141};
1142
1143struct mvpp2_bm_pool {
1144 /* Pool number in the range 0-7 */
1145 int id;
1146 enum mvpp2_bm_type type;
1147
1148 /* Buffer Pointers Pool External (BPPE) size */
1149 int size;
Thomas Petazzonid01524d2017-03-07 16:53:09 +01001150 /* BPPE size in bytes */
1151 int size_bytes;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001152 /* Number of buffers for this pool */
1153 int buf_num;
1154 /* Pool buffer size */
1155 int buf_size;
1156 /* Packet size */
1157 int pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01001158 int frag_size;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001159
1160 /* BPPE virtual base address */
1161 u32 *virt_addr;
Thomas Petazzoni20396132017-03-07 16:53:00 +01001162 /* BPPE DMA base address */
1163 dma_addr_t dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001164
1165 /* Ports using BM pool */
1166 u32 port_map;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001167};
1168
Thomas Petazzoni213f4282017-08-03 10:42:00 +02001169/* Queue modes */
1170#define MVPP2_QDIST_SINGLE_MODE 0
1171#define MVPP2_QDIST_MULTI_MODE 1
1172
1173static int queue_mode = MVPP2_QDIST_SINGLE_MODE;
1174
1175module_param(queue_mode, int, 0444);
1176MODULE_PARM_DESC(queue_mode, "Set queue_mode (single=0, multi=1)");
1177
Marcin Wojtas3f518502014-07-10 16:52:13 -03001178#define MVPP2_DRIVER_NAME "mvpp2"
1179#define MVPP2_DRIVER_VERSION "1.0"
1180
1181/* Utility/helper methods */
1182
1183static void mvpp2_write(struct mvpp2 *priv, u32 offset, u32 data)
1184{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001185 writel(data, priv->swth_base[0] + offset);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001186}
1187
1188static u32 mvpp2_read(struct mvpp2 *priv, u32 offset)
1189{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001190 return readl(priv->swth_base[0] + offset);
Thomas Petazzonia7868412017-03-07 16:53:13 +01001191}
1192
1193/* These accessors should be used to access:
1194 *
1195 * - per-CPU registers, where each CPU has its own copy of the
1196 * register.
1197 *
1198 * MVPP2_BM_VIRT_ALLOC_REG
1199 * MVPP2_BM_ADDR_HIGH_ALLOC
1200 * MVPP22_BM_ADDR_HIGH_RLS_REG
1201 * MVPP2_BM_VIRT_RLS_REG
1202 * MVPP2_ISR_RX_TX_CAUSE_REG
1203 * MVPP2_ISR_RX_TX_MASK_REG
1204 * MVPP2_TXQ_NUM_REG
1205 * MVPP2_AGGR_TXQ_UPDATE_REG
1206 * MVPP2_TXQ_RSVD_REQ_REG
1207 * MVPP2_TXQ_RSVD_RSLT_REG
1208 * MVPP2_TXQ_SENT_REG
1209 * MVPP2_RXQ_NUM_REG
1210 *
1211 * - global registers that must be accessed through a specific CPU
1212 * window, because they are related to an access to a per-CPU
1213 * register
1214 *
1215 * MVPP2_BM_PHY_ALLOC_REG (related to MVPP2_BM_VIRT_ALLOC_REG)
1216 * MVPP2_BM_PHY_RLS_REG (related to MVPP2_BM_VIRT_RLS_REG)
1217 * MVPP2_RXQ_THRESH_REG (related to MVPP2_RXQ_NUM_REG)
1218 * MVPP2_RXQ_DESC_ADDR_REG (related to MVPP2_RXQ_NUM_REG)
1219 * MVPP2_RXQ_DESC_SIZE_REG (related to MVPP2_RXQ_NUM_REG)
1220 * MVPP2_RXQ_INDEX_REG (related to MVPP2_RXQ_NUM_REG)
1221 * MVPP2_TXQ_PENDING_REG (related to MVPP2_TXQ_NUM_REG)
1222 * MVPP2_TXQ_DESC_ADDR_REG (related to MVPP2_TXQ_NUM_REG)
1223 * MVPP2_TXQ_DESC_SIZE_REG (related to MVPP2_TXQ_NUM_REG)
1224 * MVPP2_TXQ_INDEX_REG (related to MVPP2_TXQ_NUM_REG)
1225 * MVPP2_TXQ_PENDING_REG (related to MVPP2_TXQ_NUM_REG)
1226 * MVPP2_TXQ_PREF_BUF_REG (related to MVPP2_TXQ_NUM_REG)
1227 * MVPP2_TXQ_PREF_BUF_REG (related to MVPP2_TXQ_NUM_REG)
1228 */
1229static void mvpp2_percpu_write(struct mvpp2 *priv, int cpu,
1230 u32 offset, u32 data)
1231{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001232 writel(data, priv->swth_base[cpu] + offset);
Thomas Petazzonia7868412017-03-07 16:53:13 +01001233}
1234
1235static u32 mvpp2_percpu_read(struct mvpp2 *priv, int cpu,
1236 u32 offset)
1237{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001238 return readl(priv->swth_base[cpu] + offset);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001239}
1240
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001241static dma_addr_t mvpp2_txdesc_dma_addr_get(struct mvpp2_port *port,
1242 struct mvpp2_tx_desc *tx_desc)
1243{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001244 if (port->priv->hw_version == MVPP21)
1245 return tx_desc->pp21.buf_dma_addr;
1246 else
1247 return tx_desc->pp22.buf_dma_addr_ptp & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001248}
1249
1250static void mvpp2_txdesc_dma_addr_set(struct mvpp2_port *port,
1251 struct mvpp2_tx_desc *tx_desc,
1252 dma_addr_t dma_addr)
1253{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001254 if (port->priv->hw_version == MVPP21) {
1255 tx_desc->pp21.buf_dma_addr = dma_addr;
1256 } else {
1257 u64 val = (u64)dma_addr;
1258
1259 tx_desc->pp22.buf_dma_addr_ptp &= ~GENMASK_ULL(40, 0);
1260 tx_desc->pp22.buf_dma_addr_ptp |= val;
1261 }
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001262}
1263
1264static size_t mvpp2_txdesc_size_get(struct mvpp2_port *port,
1265 struct mvpp2_tx_desc *tx_desc)
1266{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001267 if (port->priv->hw_version == MVPP21)
1268 return tx_desc->pp21.data_size;
1269 else
1270 return tx_desc->pp22.data_size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001271}
1272
1273static void mvpp2_txdesc_size_set(struct mvpp2_port *port,
1274 struct mvpp2_tx_desc *tx_desc,
1275 size_t size)
1276{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001277 if (port->priv->hw_version == MVPP21)
1278 tx_desc->pp21.data_size = size;
1279 else
1280 tx_desc->pp22.data_size = size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001281}
1282
1283static void mvpp2_txdesc_txq_set(struct mvpp2_port *port,
1284 struct mvpp2_tx_desc *tx_desc,
1285 unsigned int txq)
1286{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001287 if (port->priv->hw_version == MVPP21)
1288 tx_desc->pp21.phys_txq = txq;
1289 else
1290 tx_desc->pp22.phys_txq = txq;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001291}
1292
1293static void mvpp2_txdesc_cmd_set(struct mvpp2_port *port,
1294 struct mvpp2_tx_desc *tx_desc,
1295 unsigned int command)
1296{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001297 if (port->priv->hw_version == MVPP21)
1298 tx_desc->pp21.command = command;
1299 else
1300 tx_desc->pp22.command = command;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001301}
1302
1303static void mvpp2_txdesc_offset_set(struct mvpp2_port *port,
1304 struct mvpp2_tx_desc *tx_desc,
1305 unsigned int offset)
1306{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001307 if (port->priv->hw_version == MVPP21)
1308 tx_desc->pp21.packet_offset = offset;
1309 else
1310 tx_desc->pp22.packet_offset = offset;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001311}
1312
1313static unsigned int mvpp2_txdesc_offset_get(struct mvpp2_port *port,
1314 struct mvpp2_tx_desc *tx_desc)
1315{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001316 if (port->priv->hw_version == MVPP21)
1317 return tx_desc->pp21.packet_offset;
1318 else
1319 return tx_desc->pp22.packet_offset;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001320}
1321
1322static dma_addr_t mvpp2_rxdesc_dma_addr_get(struct mvpp2_port *port,
1323 struct mvpp2_rx_desc *rx_desc)
1324{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001325 if (port->priv->hw_version == MVPP21)
1326 return rx_desc->pp21.buf_dma_addr;
1327 else
1328 return rx_desc->pp22.buf_dma_addr_key_hash & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001329}
1330
1331static unsigned long mvpp2_rxdesc_cookie_get(struct mvpp2_port *port,
1332 struct mvpp2_rx_desc *rx_desc)
1333{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001334 if (port->priv->hw_version == MVPP21)
1335 return rx_desc->pp21.buf_cookie;
1336 else
1337 return rx_desc->pp22.buf_cookie_misc & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001338}
1339
1340static size_t mvpp2_rxdesc_size_get(struct mvpp2_port *port,
1341 struct mvpp2_rx_desc *rx_desc)
1342{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001343 if (port->priv->hw_version == MVPP21)
1344 return rx_desc->pp21.data_size;
1345 else
1346 return rx_desc->pp22.data_size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001347}
1348
1349static u32 mvpp2_rxdesc_status_get(struct mvpp2_port *port,
1350 struct mvpp2_rx_desc *rx_desc)
1351{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001352 if (port->priv->hw_version == MVPP21)
1353 return rx_desc->pp21.status;
1354 else
1355 return rx_desc->pp22.status;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001356}
1357
Marcin Wojtas3f518502014-07-10 16:52:13 -03001358static void mvpp2_txq_inc_get(struct mvpp2_txq_pcpu *txq_pcpu)
1359{
1360 txq_pcpu->txq_get_index++;
1361 if (txq_pcpu->txq_get_index == txq_pcpu->size)
1362 txq_pcpu->txq_get_index = 0;
1363}
1364
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001365static void mvpp2_txq_inc_put(struct mvpp2_port *port,
1366 struct mvpp2_txq_pcpu *txq_pcpu,
Marcin Wojtas71ce3912015-08-06 19:00:29 +02001367 struct sk_buff *skb,
1368 struct mvpp2_tx_desc *tx_desc)
Marcin Wojtas3f518502014-07-10 16:52:13 -03001369{
Thomas Petazzoni83544912016-12-21 11:28:49 +01001370 struct mvpp2_txq_pcpu_buf *tx_buf =
1371 txq_pcpu->buffs + txq_pcpu->txq_put_index;
1372 tx_buf->skb = skb;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001373 tx_buf->size = mvpp2_txdesc_size_get(port, tx_desc);
1374 tx_buf->dma = mvpp2_txdesc_dma_addr_get(port, tx_desc) +
1375 mvpp2_txdesc_offset_get(port, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001376 txq_pcpu->txq_put_index++;
1377 if (txq_pcpu->txq_put_index == txq_pcpu->size)
1378 txq_pcpu->txq_put_index = 0;
1379}
1380
1381/* Get number of physical egress port */
1382static inline int mvpp2_egress_port(struct mvpp2_port *port)
1383{
1384 return MVPP2_MAX_TCONT + port->id;
1385}
1386
1387/* Get number of physical TXQ */
1388static inline int mvpp2_txq_phys(int port, int txq)
1389{
1390 return (MVPP2_MAX_TCONT + port) * MVPP2_MAX_TXQ + txq;
1391}
1392
1393/* Parser configuration routines */
1394
1395/* Update parser tcam and sram hw entries */
1396static int mvpp2_prs_hw_write(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1397{
1398 int i;
1399
1400 if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1401 return -EINVAL;
1402
1403 /* Clear entry invalidation bit */
1404 pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] &= ~MVPP2_PRS_TCAM_INV_MASK;
1405
1406 /* Write tcam index - indirect access */
1407 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1408 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1409 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), pe->tcam.word[i]);
1410
1411 /* Write sram index - indirect access */
1412 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1413 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1414 mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), pe->sram.word[i]);
1415
1416 return 0;
1417}
1418
1419/* Read tcam entry from hw */
1420static int mvpp2_prs_hw_read(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1421{
1422 int i;
1423
1424 if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1425 return -EINVAL;
1426
1427 /* Write tcam index - indirect access */
1428 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1429
1430 pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] = mvpp2_read(priv,
1431 MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD));
1432 if (pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] & MVPP2_PRS_TCAM_INV_MASK)
1433 return MVPP2_PRS_TCAM_ENTRY_INVALID;
1434
1435 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1436 pe->tcam.word[i] = mvpp2_read(priv, MVPP2_PRS_TCAM_DATA_REG(i));
1437
1438 /* Write sram index - indirect access */
1439 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1440 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1441 pe->sram.word[i] = mvpp2_read(priv, MVPP2_PRS_SRAM_DATA_REG(i));
1442
1443 return 0;
1444}
1445
1446/* Invalidate tcam hw entry */
1447static void mvpp2_prs_hw_inv(struct mvpp2 *priv, int index)
1448{
1449 /* Write index - indirect access */
1450 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
1451 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD),
1452 MVPP2_PRS_TCAM_INV_MASK);
1453}
1454
1455/* Enable shadow table entry and set its lookup ID */
1456static void mvpp2_prs_shadow_set(struct mvpp2 *priv, int index, int lu)
1457{
1458 priv->prs_shadow[index].valid = true;
1459 priv->prs_shadow[index].lu = lu;
1460}
1461
1462/* Update ri fields in shadow table entry */
1463static void mvpp2_prs_shadow_ri_set(struct mvpp2 *priv, int index,
1464 unsigned int ri, unsigned int ri_mask)
1465{
1466 priv->prs_shadow[index].ri_mask = ri_mask;
1467 priv->prs_shadow[index].ri = ri;
1468}
1469
1470/* Update lookup field in tcam sw entry */
1471static void mvpp2_prs_tcam_lu_set(struct mvpp2_prs_entry *pe, unsigned int lu)
1472{
1473 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_LU_BYTE);
1474
1475 pe->tcam.byte[MVPP2_PRS_TCAM_LU_BYTE] = lu;
1476 pe->tcam.byte[enable_off] = MVPP2_PRS_LU_MASK;
1477}
1478
1479/* Update mask for single port in tcam sw entry */
1480static void mvpp2_prs_tcam_port_set(struct mvpp2_prs_entry *pe,
1481 unsigned int port, bool add)
1482{
1483 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1484
1485 if (add)
1486 pe->tcam.byte[enable_off] &= ~(1 << port);
1487 else
1488 pe->tcam.byte[enable_off] |= 1 << port;
1489}
1490
1491/* Update port map in tcam sw entry */
1492static void mvpp2_prs_tcam_port_map_set(struct mvpp2_prs_entry *pe,
1493 unsigned int ports)
1494{
1495 unsigned char port_mask = MVPP2_PRS_PORT_MASK;
1496 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1497
1498 pe->tcam.byte[MVPP2_PRS_TCAM_PORT_BYTE] = 0;
1499 pe->tcam.byte[enable_off] &= ~port_mask;
1500 pe->tcam.byte[enable_off] |= ~ports & MVPP2_PRS_PORT_MASK;
1501}
1502
1503/* Obtain port map from tcam sw entry */
1504static unsigned int mvpp2_prs_tcam_port_map_get(struct mvpp2_prs_entry *pe)
1505{
1506 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1507
1508 return ~(pe->tcam.byte[enable_off]) & MVPP2_PRS_PORT_MASK;
1509}
1510
1511/* Set byte of data and its enable bits in tcam sw entry */
1512static void mvpp2_prs_tcam_data_byte_set(struct mvpp2_prs_entry *pe,
1513 unsigned int offs, unsigned char byte,
1514 unsigned char enable)
1515{
1516 pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)] = byte;
1517 pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)] = enable;
1518}
1519
1520/* Get byte of data and its enable bits from tcam sw entry */
1521static void mvpp2_prs_tcam_data_byte_get(struct mvpp2_prs_entry *pe,
1522 unsigned int offs, unsigned char *byte,
1523 unsigned char *enable)
1524{
1525 *byte = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)];
1526 *enable = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)];
1527}
1528
1529/* Compare tcam data bytes with a pattern */
1530static bool mvpp2_prs_tcam_data_cmp(struct mvpp2_prs_entry *pe, int offs,
1531 u16 data)
1532{
1533 int off = MVPP2_PRS_TCAM_DATA_BYTE(offs);
1534 u16 tcam_data;
1535
1536 tcam_data = (8 << pe->tcam.byte[off + 1]) | pe->tcam.byte[off];
1537 if (tcam_data != data)
1538 return false;
1539 return true;
1540}
1541
1542/* Update ai bits in tcam sw entry */
1543static void mvpp2_prs_tcam_ai_update(struct mvpp2_prs_entry *pe,
1544 unsigned int bits, unsigned int enable)
1545{
1546 int i, ai_idx = MVPP2_PRS_TCAM_AI_BYTE;
1547
1548 for (i = 0; i < MVPP2_PRS_AI_BITS; i++) {
1549
1550 if (!(enable & BIT(i)))
1551 continue;
1552
1553 if (bits & BIT(i))
1554 pe->tcam.byte[ai_idx] |= 1 << i;
1555 else
1556 pe->tcam.byte[ai_idx] &= ~(1 << i);
1557 }
1558
1559 pe->tcam.byte[MVPP2_PRS_TCAM_EN_OFFS(ai_idx)] |= enable;
1560}
1561
1562/* Get ai bits from tcam sw entry */
1563static int mvpp2_prs_tcam_ai_get(struct mvpp2_prs_entry *pe)
1564{
1565 return pe->tcam.byte[MVPP2_PRS_TCAM_AI_BYTE];
1566}
1567
1568/* Set ethertype in tcam sw entry */
1569static void mvpp2_prs_match_etype(struct mvpp2_prs_entry *pe, int offset,
1570 unsigned short ethertype)
1571{
1572 mvpp2_prs_tcam_data_byte_set(pe, offset + 0, ethertype >> 8, 0xff);
1573 mvpp2_prs_tcam_data_byte_set(pe, offset + 1, ethertype & 0xff, 0xff);
1574}
1575
1576/* Set bits in sram sw entry */
1577static void mvpp2_prs_sram_bits_set(struct mvpp2_prs_entry *pe, int bit_num,
1578 int val)
1579{
1580 pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] |= (val << (bit_num % 8));
1581}
1582
1583/* Clear bits in sram sw entry */
1584static void mvpp2_prs_sram_bits_clear(struct mvpp2_prs_entry *pe, int bit_num,
1585 int val)
1586{
1587 pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] &= ~(val << (bit_num % 8));
1588}
1589
1590/* Update ri bits in sram sw entry */
1591static void mvpp2_prs_sram_ri_update(struct mvpp2_prs_entry *pe,
1592 unsigned int bits, unsigned int mask)
1593{
1594 unsigned int i;
1595
1596 for (i = 0; i < MVPP2_PRS_SRAM_RI_CTRL_BITS; i++) {
1597 int ri_off = MVPP2_PRS_SRAM_RI_OFFS;
1598
1599 if (!(mask & BIT(i)))
1600 continue;
1601
1602 if (bits & BIT(i))
1603 mvpp2_prs_sram_bits_set(pe, ri_off + i, 1);
1604 else
1605 mvpp2_prs_sram_bits_clear(pe, ri_off + i, 1);
1606
1607 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_RI_CTRL_OFFS + i, 1);
1608 }
1609}
1610
1611/* Obtain ri bits from sram sw entry */
1612static int mvpp2_prs_sram_ri_get(struct mvpp2_prs_entry *pe)
1613{
1614 return pe->sram.word[MVPP2_PRS_SRAM_RI_WORD];
1615}
1616
1617/* Update ai bits in sram sw entry */
1618static void mvpp2_prs_sram_ai_update(struct mvpp2_prs_entry *pe,
1619 unsigned int bits, unsigned int mask)
1620{
1621 unsigned int i;
1622 int ai_off = MVPP2_PRS_SRAM_AI_OFFS;
1623
1624 for (i = 0; i < MVPP2_PRS_SRAM_AI_CTRL_BITS; i++) {
1625
1626 if (!(mask & BIT(i)))
1627 continue;
1628
1629 if (bits & BIT(i))
1630 mvpp2_prs_sram_bits_set(pe, ai_off + i, 1);
1631 else
1632 mvpp2_prs_sram_bits_clear(pe, ai_off + i, 1);
1633
1634 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_AI_CTRL_OFFS + i, 1);
1635 }
1636}
1637
1638/* Read ai bits from sram sw entry */
1639static int mvpp2_prs_sram_ai_get(struct mvpp2_prs_entry *pe)
1640{
1641 u8 bits;
1642 int ai_off = MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_AI_OFFS);
1643 int ai_en_off = ai_off + 1;
1644 int ai_shift = MVPP2_PRS_SRAM_AI_OFFS % 8;
1645
1646 bits = (pe->sram.byte[ai_off] >> ai_shift) |
1647 (pe->sram.byte[ai_en_off] << (8 - ai_shift));
1648
1649 return bits;
1650}
1651
1652/* In sram sw entry set lookup ID field of the tcam key to be used in the next
1653 * lookup interation
1654 */
1655static void mvpp2_prs_sram_next_lu_set(struct mvpp2_prs_entry *pe,
1656 unsigned int lu)
1657{
1658 int sram_next_off = MVPP2_PRS_SRAM_NEXT_LU_OFFS;
1659
1660 mvpp2_prs_sram_bits_clear(pe, sram_next_off,
1661 MVPP2_PRS_SRAM_NEXT_LU_MASK);
1662 mvpp2_prs_sram_bits_set(pe, sram_next_off, lu);
1663}
1664
1665/* In the sram sw entry set sign and value of the next lookup offset
1666 * and the offset value generated to the classifier
1667 */
1668static void mvpp2_prs_sram_shift_set(struct mvpp2_prs_entry *pe, int shift,
1669 unsigned int op)
1670{
1671 /* Set sign */
1672 if (shift < 0) {
1673 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1674 shift = 0 - shift;
1675 } else {
1676 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1677 }
1678
1679 /* Set value */
1680 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_SHIFT_OFFS)] =
1681 (unsigned char)shift;
1682
1683 /* Reset and set operation */
1684 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS,
1685 MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK);
1686 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS, op);
1687
1688 /* Set base offset as current */
1689 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1690}
1691
1692/* In the sram sw entry set sign and value of the user defined offset
1693 * generated to the classifier
1694 */
1695static void mvpp2_prs_sram_offset_set(struct mvpp2_prs_entry *pe,
1696 unsigned int type, int offset,
1697 unsigned int op)
1698{
1699 /* Set sign */
1700 if (offset < 0) {
1701 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1702 offset = 0 - offset;
1703 } else {
1704 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1705 }
1706
1707 /* Set value */
1708 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_OFFS,
1709 MVPP2_PRS_SRAM_UDF_MASK);
1710 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_OFFS, offset);
1711 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1712 MVPP2_PRS_SRAM_UDF_BITS)] &=
1713 ~(MVPP2_PRS_SRAM_UDF_MASK >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1714 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1715 MVPP2_PRS_SRAM_UDF_BITS)] |=
1716 (offset >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1717
1718 /* Set offset type */
1719 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS,
1720 MVPP2_PRS_SRAM_UDF_TYPE_MASK);
1721 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS, type);
1722
1723 /* Set offset operation */
1724 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS,
1725 MVPP2_PRS_SRAM_OP_SEL_UDF_MASK);
1726 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS, op);
1727
1728 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1729 MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] &=
1730 ~(MVPP2_PRS_SRAM_OP_SEL_UDF_MASK >>
1731 (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1732
1733 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1734 MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] |=
1735 (op >> (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1736
1737 /* Set base offset as current */
1738 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1739}
1740
1741/* Find parser flow entry */
1742static struct mvpp2_prs_entry *mvpp2_prs_flow_find(struct mvpp2 *priv, int flow)
1743{
1744 struct mvpp2_prs_entry *pe;
1745 int tid;
1746
1747 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
1748 if (!pe)
1749 return NULL;
1750 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
1751
1752 /* Go through the all entires with MVPP2_PRS_LU_FLOWS */
1753 for (tid = MVPP2_PRS_TCAM_SRAM_SIZE - 1; tid >= 0; tid--) {
1754 u8 bits;
1755
1756 if (!priv->prs_shadow[tid].valid ||
1757 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_FLOWS)
1758 continue;
1759
1760 pe->index = tid;
1761 mvpp2_prs_hw_read(priv, pe);
1762 bits = mvpp2_prs_sram_ai_get(pe);
1763
1764 /* Sram store classification lookup ID in AI bits [5:0] */
1765 if ((bits & MVPP2_PRS_FLOW_ID_MASK) == flow)
1766 return pe;
1767 }
1768 kfree(pe);
1769
1770 return NULL;
1771}
1772
1773/* Return first free tcam index, seeking from start to end */
1774static int mvpp2_prs_tcam_first_free(struct mvpp2 *priv, unsigned char start,
1775 unsigned char end)
1776{
1777 int tid;
1778
1779 if (start > end)
1780 swap(start, end);
1781
1782 if (end >= MVPP2_PRS_TCAM_SRAM_SIZE)
1783 end = MVPP2_PRS_TCAM_SRAM_SIZE - 1;
1784
1785 for (tid = start; tid <= end; tid++) {
1786 if (!priv->prs_shadow[tid].valid)
1787 return tid;
1788 }
1789
1790 return -EINVAL;
1791}
1792
1793/* Enable/disable dropping all mac da's */
1794static void mvpp2_prs_mac_drop_all_set(struct mvpp2 *priv, int port, bool add)
1795{
1796 struct mvpp2_prs_entry pe;
1797
1798 if (priv->prs_shadow[MVPP2_PE_DROP_ALL].valid) {
1799 /* Entry exist - update port only */
1800 pe.index = MVPP2_PE_DROP_ALL;
1801 mvpp2_prs_hw_read(priv, &pe);
1802 } else {
1803 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001804 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001805 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1806 pe.index = MVPP2_PE_DROP_ALL;
1807
1808 /* Non-promiscuous mode for all ports - DROP unknown packets */
1809 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
1810 MVPP2_PRS_RI_DROP_MASK);
1811
1812 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1813 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1814
1815 /* Update shadow table */
1816 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1817
1818 /* Mask all ports */
1819 mvpp2_prs_tcam_port_map_set(&pe, 0);
1820 }
1821
1822 /* Update port mask */
1823 mvpp2_prs_tcam_port_set(&pe, port, add);
1824
1825 mvpp2_prs_hw_write(priv, &pe);
1826}
1827
1828/* Set port to promiscuous mode */
1829static void mvpp2_prs_mac_promisc_set(struct mvpp2 *priv, int port, bool add)
1830{
1831 struct mvpp2_prs_entry pe;
1832
Joe Perchesdbedd442015-03-06 20:49:12 -08001833 /* Promiscuous mode - Accept unknown packets */
Marcin Wojtas3f518502014-07-10 16:52:13 -03001834
1835 if (priv->prs_shadow[MVPP2_PE_MAC_PROMISCUOUS].valid) {
1836 /* Entry exist - update port only */
1837 pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1838 mvpp2_prs_hw_read(priv, &pe);
1839 } else {
1840 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001841 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001842 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1843 pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1844
1845 /* Continue - set next lookup */
1846 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
1847
1848 /* Set result info bits */
1849 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_UCAST,
1850 MVPP2_PRS_RI_L2_CAST_MASK);
1851
1852 /* Shift to ethertype */
1853 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
1854 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1855
1856 /* Mask all ports */
1857 mvpp2_prs_tcam_port_map_set(&pe, 0);
1858
1859 /* Update shadow table */
1860 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1861 }
1862
1863 /* Update port mask */
1864 mvpp2_prs_tcam_port_set(&pe, port, add);
1865
1866 mvpp2_prs_hw_write(priv, &pe);
1867}
1868
1869/* Accept multicast */
1870static void mvpp2_prs_mac_multi_set(struct mvpp2 *priv, int port, int index,
1871 bool add)
1872{
1873 struct mvpp2_prs_entry pe;
1874 unsigned char da_mc;
1875
1876 /* Ethernet multicast address first byte is
1877 * 0x01 for IPv4 and 0x33 for IPv6
1878 */
1879 da_mc = (index == MVPP2_PE_MAC_MC_ALL) ? 0x01 : 0x33;
1880
1881 if (priv->prs_shadow[index].valid) {
1882 /* Entry exist - update port only */
1883 pe.index = index;
1884 mvpp2_prs_hw_read(priv, &pe);
1885 } else {
1886 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001887 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001888 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1889 pe.index = index;
1890
1891 /* Continue - set next lookup */
1892 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
1893
1894 /* Set result info bits */
1895 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_MCAST,
1896 MVPP2_PRS_RI_L2_CAST_MASK);
1897
1898 /* Update tcam entry data first byte */
1899 mvpp2_prs_tcam_data_byte_set(&pe, 0, da_mc, 0xff);
1900
1901 /* Shift to ethertype */
1902 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
1903 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1904
1905 /* Mask all ports */
1906 mvpp2_prs_tcam_port_map_set(&pe, 0);
1907
1908 /* Update shadow table */
1909 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1910 }
1911
1912 /* Update port mask */
1913 mvpp2_prs_tcam_port_set(&pe, port, add);
1914
1915 mvpp2_prs_hw_write(priv, &pe);
1916}
1917
1918/* Set entry for dsa packets */
1919static void mvpp2_prs_dsa_tag_set(struct mvpp2 *priv, int port, bool add,
1920 bool tagged, bool extend)
1921{
1922 struct mvpp2_prs_entry pe;
1923 int tid, shift;
1924
1925 if (extend) {
1926 tid = tagged ? MVPP2_PE_EDSA_TAGGED : MVPP2_PE_EDSA_UNTAGGED;
1927 shift = 8;
1928 } else {
1929 tid = tagged ? MVPP2_PE_DSA_TAGGED : MVPP2_PE_DSA_UNTAGGED;
1930 shift = 4;
1931 }
1932
1933 if (priv->prs_shadow[tid].valid) {
1934 /* Entry exist - update port only */
1935 pe.index = tid;
1936 mvpp2_prs_hw_read(priv, &pe);
1937 } else {
1938 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001939 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001940 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
1941 pe.index = tid;
1942
1943 /* Shift 4 bytes if DSA tag or 8 bytes in case of EDSA tag*/
1944 mvpp2_prs_sram_shift_set(&pe, shift,
1945 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1946
1947 /* Update shadow table */
1948 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
1949
1950 if (tagged) {
1951 /* Set tagged bit in DSA tag */
1952 mvpp2_prs_tcam_data_byte_set(&pe, 0,
1953 MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
1954 MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
1955 /* Clear all ai bits for next iteration */
1956 mvpp2_prs_sram_ai_update(&pe, 0,
1957 MVPP2_PRS_SRAM_AI_MASK);
1958 /* If packet is tagged continue check vlans */
1959 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
1960 } else {
1961 /* Set result info bits to 'no vlans' */
1962 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
1963 MVPP2_PRS_RI_VLAN_MASK);
1964 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
1965 }
1966
1967 /* Mask all ports */
1968 mvpp2_prs_tcam_port_map_set(&pe, 0);
1969 }
1970
1971 /* Update port mask */
1972 mvpp2_prs_tcam_port_set(&pe, port, add);
1973
1974 mvpp2_prs_hw_write(priv, &pe);
1975}
1976
1977/* Set entry for dsa ethertype */
1978static void mvpp2_prs_dsa_tag_ethertype_set(struct mvpp2 *priv, int port,
1979 bool add, bool tagged, bool extend)
1980{
1981 struct mvpp2_prs_entry pe;
1982 int tid, shift, port_mask;
1983
1984 if (extend) {
1985 tid = tagged ? MVPP2_PE_ETYPE_EDSA_TAGGED :
1986 MVPP2_PE_ETYPE_EDSA_UNTAGGED;
1987 port_mask = 0;
1988 shift = 8;
1989 } else {
1990 tid = tagged ? MVPP2_PE_ETYPE_DSA_TAGGED :
1991 MVPP2_PE_ETYPE_DSA_UNTAGGED;
1992 port_mask = MVPP2_PRS_PORT_MASK;
1993 shift = 4;
1994 }
1995
1996 if (priv->prs_shadow[tid].valid) {
1997 /* Entry exist - update port only */
1998 pe.index = tid;
1999 mvpp2_prs_hw_read(priv, &pe);
2000 } else {
2001 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002002 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002003 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
2004 pe.index = tid;
2005
2006 /* Set ethertype */
2007 mvpp2_prs_match_etype(&pe, 0, ETH_P_EDSA);
2008 mvpp2_prs_match_etype(&pe, 2, 0);
2009
2010 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DSA_MASK,
2011 MVPP2_PRS_RI_DSA_MASK);
2012 /* Shift ethertype + 2 byte reserved + tag*/
2013 mvpp2_prs_sram_shift_set(&pe, 2 + MVPP2_ETH_TYPE_LEN + shift,
2014 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2015
2016 /* Update shadow table */
2017 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
2018
2019 if (tagged) {
2020 /* Set tagged bit in DSA tag */
2021 mvpp2_prs_tcam_data_byte_set(&pe,
2022 MVPP2_ETH_TYPE_LEN + 2 + 3,
2023 MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
2024 MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
2025 /* Clear all ai bits for next iteration */
2026 mvpp2_prs_sram_ai_update(&pe, 0,
2027 MVPP2_PRS_SRAM_AI_MASK);
2028 /* If packet is tagged continue check vlans */
2029 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2030 } else {
2031 /* Set result info bits to 'no vlans' */
2032 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
2033 MVPP2_PRS_RI_VLAN_MASK);
2034 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2035 }
2036 /* Mask/unmask all ports, depending on dsa type */
2037 mvpp2_prs_tcam_port_map_set(&pe, port_mask);
2038 }
2039
2040 /* Update port mask */
2041 mvpp2_prs_tcam_port_set(&pe, port, add);
2042
2043 mvpp2_prs_hw_write(priv, &pe);
2044}
2045
2046/* Search for existing single/triple vlan entry */
2047static struct mvpp2_prs_entry *mvpp2_prs_vlan_find(struct mvpp2 *priv,
2048 unsigned short tpid, int ai)
2049{
2050 struct mvpp2_prs_entry *pe;
2051 int tid;
2052
2053 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2054 if (!pe)
2055 return NULL;
2056 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2057
2058 /* Go through the all entries with MVPP2_PRS_LU_VLAN */
2059 for (tid = MVPP2_PE_FIRST_FREE_TID;
2060 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
2061 unsigned int ri_bits, ai_bits;
2062 bool match;
2063
2064 if (!priv->prs_shadow[tid].valid ||
2065 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
2066 continue;
2067
2068 pe->index = tid;
2069
2070 mvpp2_prs_hw_read(priv, pe);
2071 match = mvpp2_prs_tcam_data_cmp(pe, 0, swab16(tpid));
2072 if (!match)
2073 continue;
2074
2075 /* Get vlan type */
2076 ri_bits = mvpp2_prs_sram_ri_get(pe);
2077 ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
2078
2079 /* Get current ai value from tcam */
2080 ai_bits = mvpp2_prs_tcam_ai_get(pe);
2081 /* Clear double vlan bit */
2082 ai_bits &= ~MVPP2_PRS_DBL_VLAN_AI_BIT;
2083
2084 if (ai != ai_bits)
2085 continue;
2086
2087 if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
2088 ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
2089 return pe;
2090 }
2091 kfree(pe);
2092
2093 return NULL;
2094}
2095
2096/* Add/update single/triple vlan entry */
2097static int mvpp2_prs_vlan_add(struct mvpp2 *priv, unsigned short tpid, int ai,
2098 unsigned int port_map)
2099{
2100 struct mvpp2_prs_entry *pe;
2101 int tid_aux, tid;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302102 int ret = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002103
2104 pe = mvpp2_prs_vlan_find(priv, tpid, ai);
2105
2106 if (!pe) {
2107 /* Create new tcam entry */
2108 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_LAST_FREE_TID,
2109 MVPP2_PE_FIRST_FREE_TID);
2110 if (tid < 0)
2111 return tid;
2112
2113 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2114 if (!pe)
2115 return -ENOMEM;
2116
2117 /* Get last double vlan tid */
2118 for (tid_aux = MVPP2_PE_LAST_FREE_TID;
2119 tid_aux >= MVPP2_PE_FIRST_FREE_TID; tid_aux--) {
2120 unsigned int ri_bits;
2121
2122 if (!priv->prs_shadow[tid_aux].valid ||
2123 priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
2124 continue;
2125
2126 pe->index = tid_aux;
2127 mvpp2_prs_hw_read(priv, pe);
2128 ri_bits = mvpp2_prs_sram_ri_get(pe);
2129 if ((ri_bits & MVPP2_PRS_RI_VLAN_MASK) ==
2130 MVPP2_PRS_RI_VLAN_DOUBLE)
2131 break;
2132 }
2133
Sudip Mukherjee43737472014-11-01 16:59:34 +05302134 if (tid <= tid_aux) {
2135 ret = -EINVAL;
Markus Elfringf9fd0e32017-04-17 13:50:35 +02002136 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302137 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002138
Markus Elfringbd6aaf52017-04-17 10:40:32 +02002139 memset(pe, 0, sizeof(*pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002140 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2141 pe->index = tid;
2142
2143 mvpp2_prs_match_etype(pe, 0, tpid);
2144
2145 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_L2);
2146 /* Shift 4 bytes - skip 1 vlan tag */
2147 mvpp2_prs_sram_shift_set(pe, MVPP2_VLAN_TAG_LEN,
2148 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2149 /* Clear all ai bits for next iteration */
2150 mvpp2_prs_sram_ai_update(pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2151
2152 if (ai == MVPP2_PRS_SINGLE_VLAN_AI) {
2153 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_SINGLE,
2154 MVPP2_PRS_RI_VLAN_MASK);
2155 } else {
2156 ai |= MVPP2_PRS_DBL_VLAN_AI_BIT;
2157 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_TRIPLE,
2158 MVPP2_PRS_RI_VLAN_MASK);
2159 }
2160 mvpp2_prs_tcam_ai_update(pe, ai, MVPP2_PRS_SRAM_AI_MASK);
2161
2162 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_VLAN);
2163 }
2164 /* Update ports' mask */
2165 mvpp2_prs_tcam_port_map_set(pe, port_map);
2166
2167 mvpp2_prs_hw_write(priv, pe);
Markus Elfringf9fd0e32017-04-17 13:50:35 +02002168free_pe:
Marcin Wojtas3f518502014-07-10 16:52:13 -03002169 kfree(pe);
2170
Sudip Mukherjee43737472014-11-01 16:59:34 +05302171 return ret;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002172}
2173
2174/* Get first free double vlan ai number */
2175static int mvpp2_prs_double_vlan_ai_free_get(struct mvpp2 *priv)
2176{
2177 int i;
2178
2179 for (i = 1; i < MVPP2_PRS_DBL_VLANS_MAX; i++) {
2180 if (!priv->prs_double_vlans[i])
2181 return i;
2182 }
2183
2184 return -EINVAL;
2185}
2186
2187/* Search for existing double vlan entry */
2188static struct mvpp2_prs_entry *mvpp2_prs_double_vlan_find(struct mvpp2 *priv,
2189 unsigned short tpid1,
2190 unsigned short tpid2)
2191{
2192 struct mvpp2_prs_entry *pe;
2193 int tid;
2194
2195 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2196 if (!pe)
2197 return NULL;
2198 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2199
2200 /* Go through the all entries with MVPP2_PRS_LU_VLAN */
2201 for (tid = MVPP2_PE_FIRST_FREE_TID;
2202 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
2203 unsigned int ri_mask;
2204 bool match;
2205
2206 if (!priv->prs_shadow[tid].valid ||
2207 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
2208 continue;
2209
2210 pe->index = tid;
2211 mvpp2_prs_hw_read(priv, pe);
2212
2213 match = mvpp2_prs_tcam_data_cmp(pe, 0, swab16(tpid1))
2214 && mvpp2_prs_tcam_data_cmp(pe, 4, swab16(tpid2));
2215
2216 if (!match)
2217 continue;
2218
2219 ri_mask = mvpp2_prs_sram_ri_get(pe) & MVPP2_PRS_RI_VLAN_MASK;
2220 if (ri_mask == MVPP2_PRS_RI_VLAN_DOUBLE)
2221 return pe;
2222 }
2223 kfree(pe);
2224
2225 return NULL;
2226}
2227
2228/* Add or update double vlan entry */
2229static int mvpp2_prs_double_vlan_add(struct mvpp2 *priv, unsigned short tpid1,
2230 unsigned short tpid2,
2231 unsigned int port_map)
2232{
2233 struct mvpp2_prs_entry *pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302234 int tid_aux, tid, ai, ret = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002235
2236 pe = mvpp2_prs_double_vlan_find(priv, tpid1, tpid2);
2237
2238 if (!pe) {
2239 /* Create new tcam entry */
2240 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2241 MVPP2_PE_LAST_FREE_TID);
2242 if (tid < 0)
2243 return tid;
2244
2245 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2246 if (!pe)
2247 return -ENOMEM;
2248
2249 /* Set ai value for new double vlan entry */
2250 ai = mvpp2_prs_double_vlan_ai_free_get(priv);
Sudip Mukherjee43737472014-11-01 16:59:34 +05302251 if (ai < 0) {
2252 ret = ai;
Markus Elfringc9a7e122017-04-17 13:03:49 +02002253 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302254 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002255
2256 /* Get first single/triple vlan tid */
2257 for (tid_aux = MVPP2_PE_FIRST_FREE_TID;
2258 tid_aux <= MVPP2_PE_LAST_FREE_TID; tid_aux++) {
2259 unsigned int ri_bits;
2260
2261 if (!priv->prs_shadow[tid_aux].valid ||
2262 priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
2263 continue;
2264
2265 pe->index = tid_aux;
2266 mvpp2_prs_hw_read(priv, pe);
2267 ri_bits = mvpp2_prs_sram_ri_get(pe);
2268 ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
2269 if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
2270 ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
2271 break;
2272 }
2273
Sudip Mukherjee43737472014-11-01 16:59:34 +05302274 if (tid >= tid_aux) {
2275 ret = -ERANGE;
Markus Elfringc9a7e122017-04-17 13:03:49 +02002276 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302277 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002278
Markus Elfringbd6aaf52017-04-17 10:40:32 +02002279 memset(pe, 0, sizeof(*pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002280 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2281 pe->index = tid;
2282
2283 priv->prs_double_vlans[ai] = true;
2284
2285 mvpp2_prs_match_etype(pe, 0, tpid1);
2286 mvpp2_prs_match_etype(pe, 4, tpid2);
2287
2288 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_VLAN);
2289 /* Shift 8 bytes - skip 2 vlan tags */
2290 mvpp2_prs_sram_shift_set(pe, 2 * MVPP2_VLAN_TAG_LEN,
2291 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2292 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_DOUBLE,
2293 MVPP2_PRS_RI_VLAN_MASK);
2294 mvpp2_prs_sram_ai_update(pe, ai | MVPP2_PRS_DBL_VLAN_AI_BIT,
2295 MVPP2_PRS_SRAM_AI_MASK);
2296
2297 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_VLAN);
2298 }
2299
2300 /* Update ports' mask */
2301 mvpp2_prs_tcam_port_map_set(pe, port_map);
2302 mvpp2_prs_hw_write(priv, pe);
Markus Elfringc9a7e122017-04-17 13:03:49 +02002303free_pe:
Marcin Wojtas3f518502014-07-10 16:52:13 -03002304 kfree(pe);
Sudip Mukherjee43737472014-11-01 16:59:34 +05302305 return ret;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002306}
2307
2308/* IPv4 header parsing for fragmentation and L4 offset */
2309static int mvpp2_prs_ip4_proto(struct mvpp2 *priv, unsigned short proto,
2310 unsigned int ri, unsigned int ri_mask)
2311{
2312 struct mvpp2_prs_entry pe;
2313 int tid;
2314
2315 if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
2316 (proto != IPPROTO_IGMP))
2317 return -EINVAL;
2318
Stefan Chulskiaff3da32017-09-25 14:59:46 +02002319 /* Not fragmented packet */
Marcin Wojtas3f518502014-07-10 16:52:13 -03002320 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2321 MVPP2_PE_LAST_FREE_TID);
2322 if (tid < 0)
2323 return tid;
2324
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002325 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002326 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
2327 pe.index = tid;
2328
2329 /* Set next lu to IPv4 */
2330 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2331 mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2332 /* Set L4 offset */
2333 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
2334 sizeof(struct iphdr) - 4,
2335 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2336 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
2337 MVPP2_PRS_IPV4_DIP_AI_BIT);
Stefan Chulskiaff3da32017-09-25 14:59:46 +02002338 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask | MVPP2_PRS_RI_IP_FRAG_MASK);
2339
2340 mvpp2_prs_tcam_data_byte_set(&pe, 2, 0x00,
2341 MVPP2_PRS_TCAM_PROTO_MASK_L);
2342 mvpp2_prs_tcam_data_byte_set(&pe, 3, 0x00,
2343 MVPP2_PRS_TCAM_PROTO_MASK);
Marcin Wojtas3f518502014-07-10 16:52:13 -03002344
2345 mvpp2_prs_tcam_data_byte_set(&pe, 5, proto, MVPP2_PRS_TCAM_PROTO_MASK);
2346 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
2347 /* Unmask all ports */
2348 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2349
2350 /* Update shadow table and hw entry */
2351 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2352 mvpp2_prs_hw_write(priv, &pe);
2353
Stefan Chulskiaff3da32017-09-25 14:59:46 +02002354 /* Fragmented packet */
Marcin Wojtas3f518502014-07-10 16:52:13 -03002355 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2356 MVPP2_PE_LAST_FREE_TID);
2357 if (tid < 0)
2358 return tid;
2359
2360 pe.index = tid;
2361 /* Clear ri before updating */
2362 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2363 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2364 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
2365
Stefan Chulskiaff3da32017-09-25 14:59:46 +02002366 mvpp2_prs_sram_ri_update(&pe, ri | MVPP2_PRS_RI_IP_FRAG_TRUE,
2367 ri_mask | MVPP2_PRS_RI_IP_FRAG_MASK);
2368
2369 mvpp2_prs_tcam_data_byte_set(&pe, 2, 0x00, 0x0);
2370 mvpp2_prs_tcam_data_byte_set(&pe, 3, 0x00, 0x0);
Marcin Wojtas3f518502014-07-10 16:52:13 -03002371
2372 /* Update shadow table and hw entry */
2373 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2374 mvpp2_prs_hw_write(priv, &pe);
2375
2376 return 0;
2377}
2378
2379/* IPv4 L3 multicast or broadcast */
2380static int mvpp2_prs_ip4_cast(struct mvpp2 *priv, unsigned short l3_cast)
2381{
2382 struct mvpp2_prs_entry pe;
2383 int mask, tid;
2384
2385 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2386 MVPP2_PE_LAST_FREE_TID);
2387 if (tid < 0)
2388 return tid;
2389
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002390 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002391 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
2392 pe.index = tid;
2393
2394 switch (l3_cast) {
2395 case MVPP2_PRS_L3_MULTI_CAST:
2396 mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV4_MC,
2397 MVPP2_PRS_IPV4_MC_MASK);
2398 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
2399 MVPP2_PRS_RI_L3_ADDR_MASK);
2400 break;
2401 case MVPP2_PRS_L3_BROAD_CAST:
2402 mask = MVPP2_PRS_IPV4_BC_MASK;
2403 mvpp2_prs_tcam_data_byte_set(&pe, 0, mask, mask);
2404 mvpp2_prs_tcam_data_byte_set(&pe, 1, mask, mask);
2405 mvpp2_prs_tcam_data_byte_set(&pe, 2, mask, mask);
2406 mvpp2_prs_tcam_data_byte_set(&pe, 3, mask, mask);
2407 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_BCAST,
2408 MVPP2_PRS_RI_L3_ADDR_MASK);
2409 break;
2410 default:
2411 return -EINVAL;
2412 }
2413
2414 /* Finished: go to flowid generation */
2415 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2416 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2417
2418 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
2419 MVPP2_PRS_IPV4_DIP_AI_BIT);
2420 /* Unmask all ports */
2421 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2422
2423 /* Update shadow table and hw entry */
2424 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2425 mvpp2_prs_hw_write(priv, &pe);
2426
2427 return 0;
2428}
2429
2430/* Set entries for protocols over IPv6 */
2431static int mvpp2_prs_ip6_proto(struct mvpp2 *priv, unsigned short proto,
2432 unsigned int ri, unsigned int ri_mask)
2433{
2434 struct mvpp2_prs_entry pe;
2435 int tid;
2436
2437 if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
2438 (proto != IPPROTO_ICMPV6) && (proto != IPPROTO_IPIP))
2439 return -EINVAL;
2440
2441 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2442 MVPP2_PE_LAST_FREE_TID);
2443 if (tid < 0)
2444 return tid;
2445
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002446 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002447 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2448 pe.index = tid;
2449
2450 /* Finished: go to flowid generation */
2451 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2452 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2453 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
2454 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
2455 sizeof(struct ipv6hdr) - 6,
2456 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2457
2458 mvpp2_prs_tcam_data_byte_set(&pe, 0, proto, MVPP2_PRS_TCAM_PROTO_MASK);
2459 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
2460 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2461 /* Unmask all ports */
2462 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2463
2464 /* Write HW */
2465 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
2466 mvpp2_prs_hw_write(priv, &pe);
2467
2468 return 0;
2469}
2470
2471/* IPv6 L3 multicast entry */
2472static int mvpp2_prs_ip6_cast(struct mvpp2 *priv, unsigned short l3_cast)
2473{
2474 struct mvpp2_prs_entry pe;
2475 int tid;
2476
2477 if (l3_cast != MVPP2_PRS_L3_MULTI_CAST)
2478 return -EINVAL;
2479
2480 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2481 MVPP2_PE_LAST_FREE_TID);
2482 if (tid < 0)
2483 return tid;
2484
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002485 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002486 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2487 pe.index = tid;
2488
2489 /* Finished: go to flowid generation */
2490 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2491 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
2492 MVPP2_PRS_RI_L3_ADDR_MASK);
2493 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
2494 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2495 /* Shift back to IPv6 NH */
2496 mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2497
2498 mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV6_MC,
2499 MVPP2_PRS_IPV6_MC_MASK);
2500 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2501 /* Unmask all ports */
2502 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2503
2504 /* Update shadow table and hw entry */
2505 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
2506 mvpp2_prs_hw_write(priv, &pe);
2507
2508 return 0;
2509}
2510
2511/* Parser per-port initialization */
2512static void mvpp2_prs_hw_port_init(struct mvpp2 *priv, int port, int lu_first,
2513 int lu_max, int offset)
2514{
2515 u32 val;
2516
2517 /* Set lookup ID */
2518 val = mvpp2_read(priv, MVPP2_PRS_INIT_LOOKUP_REG);
2519 val &= ~MVPP2_PRS_PORT_LU_MASK(port);
2520 val |= MVPP2_PRS_PORT_LU_VAL(port, lu_first);
2521 mvpp2_write(priv, MVPP2_PRS_INIT_LOOKUP_REG, val);
2522
2523 /* Set maximum number of loops for packet received from port */
2524 val = mvpp2_read(priv, MVPP2_PRS_MAX_LOOP_REG(port));
2525 val &= ~MVPP2_PRS_MAX_LOOP_MASK(port);
2526 val |= MVPP2_PRS_MAX_LOOP_VAL(port, lu_max);
2527 mvpp2_write(priv, MVPP2_PRS_MAX_LOOP_REG(port), val);
2528
2529 /* Set initial offset for packet header extraction for the first
2530 * searching loop
2531 */
2532 val = mvpp2_read(priv, MVPP2_PRS_INIT_OFFS_REG(port));
2533 val &= ~MVPP2_PRS_INIT_OFF_MASK(port);
2534 val |= MVPP2_PRS_INIT_OFF_VAL(port, offset);
2535 mvpp2_write(priv, MVPP2_PRS_INIT_OFFS_REG(port), val);
2536}
2537
2538/* Default flow entries initialization for all ports */
2539static void mvpp2_prs_def_flow_init(struct mvpp2 *priv)
2540{
2541 struct mvpp2_prs_entry pe;
2542 int port;
2543
2544 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002545 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002546 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2547 pe.index = MVPP2_PE_FIRST_DEFAULT_FLOW - port;
2548
2549 /* Mask all ports */
2550 mvpp2_prs_tcam_port_map_set(&pe, 0);
2551
2552 /* Set flow ID*/
2553 mvpp2_prs_sram_ai_update(&pe, port, MVPP2_PRS_FLOW_ID_MASK);
2554 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
2555
2556 /* Update shadow table and hw entry */
2557 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_FLOWS);
2558 mvpp2_prs_hw_write(priv, &pe);
2559 }
2560}
2561
2562/* Set default entry for Marvell Header field */
2563static void mvpp2_prs_mh_init(struct mvpp2 *priv)
2564{
2565 struct mvpp2_prs_entry pe;
2566
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002567 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002568
2569 pe.index = MVPP2_PE_MH_DEFAULT;
2570 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MH);
2571 mvpp2_prs_sram_shift_set(&pe, MVPP2_MH_SIZE,
2572 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2573 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_MAC);
2574
2575 /* Unmask all ports */
2576 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2577
2578 /* Update shadow table and hw entry */
2579 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MH);
2580 mvpp2_prs_hw_write(priv, &pe);
2581}
2582
2583/* Set default entires (place holder) for promiscuous, non-promiscuous and
2584 * multicast MAC addresses
2585 */
2586static void mvpp2_prs_mac_init(struct mvpp2 *priv)
2587{
2588 struct mvpp2_prs_entry pe;
2589
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002590 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002591
2592 /* Non-promiscuous mode for all ports - DROP unknown packets */
2593 pe.index = MVPP2_PE_MAC_NON_PROMISCUOUS;
2594 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
2595
2596 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
2597 MVPP2_PRS_RI_DROP_MASK);
2598 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2599 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2600
2601 /* Unmask all ports */
2602 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2603
2604 /* Update shadow table and hw entry */
2605 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2606 mvpp2_prs_hw_write(priv, &pe);
2607
2608 /* place holders only - no ports */
2609 mvpp2_prs_mac_drop_all_set(priv, 0, false);
2610 mvpp2_prs_mac_promisc_set(priv, 0, false);
2611 mvpp2_prs_mac_multi_set(priv, MVPP2_PE_MAC_MC_ALL, 0, false);
2612 mvpp2_prs_mac_multi_set(priv, MVPP2_PE_MAC_MC_IP6, 0, false);
2613}
2614
2615/* Set default entries for various types of dsa packets */
2616static void mvpp2_prs_dsa_init(struct mvpp2 *priv)
2617{
2618 struct mvpp2_prs_entry pe;
2619
2620 /* None tagged EDSA entry - place holder */
2621 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
2622 MVPP2_PRS_EDSA);
2623
2624 /* Tagged EDSA entry - place holder */
2625 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
2626
2627 /* None tagged DSA entry - place holder */
2628 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
2629 MVPP2_PRS_DSA);
2630
2631 /* Tagged DSA entry - place holder */
2632 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
2633
2634 /* None tagged EDSA ethertype entry - place holder*/
2635 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
2636 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
2637
2638 /* Tagged EDSA ethertype entry - place holder*/
2639 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
2640 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
2641
2642 /* None tagged DSA ethertype entry */
2643 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
2644 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
2645
2646 /* Tagged DSA ethertype entry */
2647 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
2648 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
2649
2650 /* Set default entry, in case DSA or EDSA tag not found */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002651 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002652 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
2653 pe.index = MVPP2_PE_DSA_DEFAULT;
2654 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2655
2656 /* Shift 0 bytes */
2657 mvpp2_prs_sram_shift_set(&pe, 0, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2658 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2659
2660 /* Clear all sram ai bits for next iteration */
2661 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2662
2663 /* Unmask all ports */
2664 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2665
2666 mvpp2_prs_hw_write(priv, &pe);
2667}
2668
2669/* Match basic ethertypes */
2670static int mvpp2_prs_etype_init(struct mvpp2 *priv)
2671{
2672 struct mvpp2_prs_entry pe;
2673 int tid;
2674
2675 /* Ethertype: PPPoE */
2676 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2677 MVPP2_PE_LAST_FREE_TID);
2678 if (tid < 0)
2679 return tid;
2680
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002681 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002682 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2683 pe.index = tid;
2684
2685 mvpp2_prs_match_etype(&pe, 0, ETH_P_PPP_SES);
2686
2687 mvpp2_prs_sram_shift_set(&pe, MVPP2_PPPOE_HDR_SIZE,
2688 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2689 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2690 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_PPPOE_MASK,
2691 MVPP2_PRS_RI_PPPOE_MASK);
2692
2693 /* Update shadow table and hw entry */
2694 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2695 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2696 priv->prs_shadow[pe.index].finish = false;
2697 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_PPPOE_MASK,
2698 MVPP2_PRS_RI_PPPOE_MASK);
2699 mvpp2_prs_hw_write(priv, &pe);
2700
2701 /* Ethertype: ARP */
2702 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2703 MVPP2_PE_LAST_FREE_TID);
2704 if (tid < 0)
2705 return tid;
2706
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002707 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002708 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2709 pe.index = tid;
2710
2711 mvpp2_prs_match_etype(&pe, 0, ETH_P_ARP);
2712
2713 /* Generate flow in the next iteration*/
2714 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2715 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2716 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_ARP,
2717 MVPP2_PRS_RI_L3_PROTO_MASK);
2718 /* Set L3 offset */
2719 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2720 MVPP2_ETH_TYPE_LEN,
2721 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2722
2723 /* Update shadow table and hw entry */
2724 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2725 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2726 priv->prs_shadow[pe.index].finish = true;
2727 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_ARP,
2728 MVPP2_PRS_RI_L3_PROTO_MASK);
2729 mvpp2_prs_hw_write(priv, &pe);
2730
2731 /* Ethertype: LBTD */
2732 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2733 MVPP2_PE_LAST_FREE_TID);
2734 if (tid < 0)
2735 return tid;
2736
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002737 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002738 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2739 pe.index = tid;
2740
2741 mvpp2_prs_match_etype(&pe, 0, MVPP2_IP_LBDT_TYPE);
2742
2743 /* Generate flow in the next iteration*/
2744 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2745 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2746 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2747 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2748 MVPP2_PRS_RI_CPU_CODE_MASK |
2749 MVPP2_PRS_RI_UDF3_MASK);
2750 /* Set L3 offset */
2751 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2752 MVPP2_ETH_TYPE_LEN,
2753 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2754
2755 /* Update shadow table and hw entry */
2756 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2757 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2758 priv->prs_shadow[pe.index].finish = true;
2759 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2760 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2761 MVPP2_PRS_RI_CPU_CODE_MASK |
2762 MVPP2_PRS_RI_UDF3_MASK);
2763 mvpp2_prs_hw_write(priv, &pe);
2764
2765 /* Ethertype: IPv4 without options */
2766 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2767 MVPP2_PE_LAST_FREE_TID);
2768 if (tid < 0)
2769 return tid;
2770
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002771 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002772 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2773 pe.index = tid;
2774
2775 mvpp2_prs_match_etype(&pe, 0, ETH_P_IP);
2776 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2777 MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
2778 MVPP2_PRS_IPV4_HEAD_MASK |
2779 MVPP2_PRS_IPV4_IHL_MASK);
2780
2781 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2782 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
2783 MVPP2_PRS_RI_L3_PROTO_MASK);
2784 /* Skip eth_type + 4 bytes of IP header */
2785 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
2786 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2787 /* Set L3 offset */
2788 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2789 MVPP2_ETH_TYPE_LEN,
2790 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2791
2792 /* Update shadow table and hw entry */
2793 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2794 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2795 priv->prs_shadow[pe.index].finish = false;
2796 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4,
2797 MVPP2_PRS_RI_L3_PROTO_MASK);
2798 mvpp2_prs_hw_write(priv, &pe);
2799
2800 /* Ethertype: IPv4 with options */
2801 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2802 MVPP2_PE_LAST_FREE_TID);
2803 if (tid < 0)
2804 return tid;
2805
2806 pe.index = tid;
2807
2808 /* Clear tcam data before updating */
2809 pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(MVPP2_ETH_TYPE_LEN)] = 0x0;
2810 pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(MVPP2_ETH_TYPE_LEN)] = 0x0;
2811
2812 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2813 MVPP2_PRS_IPV4_HEAD,
2814 MVPP2_PRS_IPV4_HEAD_MASK);
2815
2816 /* Clear ri before updating */
2817 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2818 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2819 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
2820 MVPP2_PRS_RI_L3_PROTO_MASK);
2821
2822 /* Update shadow table and hw entry */
2823 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2824 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2825 priv->prs_shadow[pe.index].finish = false;
2826 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4_OPT,
2827 MVPP2_PRS_RI_L3_PROTO_MASK);
2828 mvpp2_prs_hw_write(priv, &pe);
2829
2830 /* Ethertype: IPv6 without options */
2831 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2832 MVPP2_PE_LAST_FREE_TID);
2833 if (tid < 0)
2834 return tid;
2835
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002836 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002837 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2838 pe.index = tid;
2839
2840 mvpp2_prs_match_etype(&pe, 0, ETH_P_IPV6);
2841
2842 /* Skip DIP of IPV6 header */
2843 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 8 +
2844 MVPP2_MAX_L3_ADDR_SIZE,
2845 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2846 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2847 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
2848 MVPP2_PRS_RI_L3_PROTO_MASK);
2849 /* Set L3 offset */
2850 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2851 MVPP2_ETH_TYPE_LEN,
2852 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2853
2854 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2855 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2856 priv->prs_shadow[pe.index].finish = false;
2857 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP6,
2858 MVPP2_PRS_RI_L3_PROTO_MASK);
2859 mvpp2_prs_hw_write(priv, &pe);
2860
2861 /* Default entry for MVPP2_PRS_LU_L2 - Unknown ethtype */
2862 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2863 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2864 pe.index = MVPP2_PE_ETH_TYPE_UN;
2865
2866 /* Unmask all ports */
2867 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2868
2869 /* Generate flow in the next iteration*/
2870 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2871 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2872 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
2873 MVPP2_PRS_RI_L3_PROTO_MASK);
2874 /* Set L3 offset even it's unknown L3 */
2875 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2876 MVPP2_ETH_TYPE_LEN,
2877 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2878
2879 /* Update shadow table and hw entry */
2880 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2881 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2882 priv->prs_shadow[pe.index].finish = true;
2883 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_UN,
2884 MVPP2_PRS_RI_L3_PROTO_MASK);
2885 mvpp2_prs_hw_write(priv, &pe);
2886
2887 return 0;
2888}
2889
2890/* Configure vlan entries and detect up to 2 successive VLAN tags.
2891 * Possible options:
2892 * 0x8100, 0x88A8
2893 * 0x8100, 0x8100
2894 * 0x8100
2895 * 0x88A8
2896 */
2897static int mvpp2_prs_vlan_init(struct platform_device *pdev, struct mvpp2 *priv)
2898{
2899 struct mvpp2_prs_entry pe;
2900 int err;
2901
2902 priv->prs_double_vlans = devm_kcalloc(&pdev->dev, sizeof(bool),
2903 MVPP2_PRS_DBL_VLANS_MAX,
2904 GFP_KERNEL);
2905 if (!priv->prs_double_vlans)
2906 return -ENOMEM;
2907
2908 /* Double VLAN: 0x8100, 0x88A8 */
2909 err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021AD,
2910 MVPP2_PRS_PORT_MASK);
2911 if (err)
2912 return err;
2913
2914 /* Double VLAN: 0x8100, 0x8100 */
2915 err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021Q,
2916 MVPP2_PRS_PORT_MASK);
2917 if (err)
2918 return err;
2919
2920 /* Single VLAN: 0x88a8 */
2921 err = mvpp2_prs_vlan_add(priv, ETH_P_8021AD, MVPP2_PRS_SINGLE_VLAN_AI,
2922 MVPP2_PRS_PORT_MASK);
2923 if (err)
2924 return err;
2925
2926 /* Single VLAN: 0x8100 */
2927 err = mvpp2_prs_vlan_add(priv, ETH_P_8021Q, MVPP2_PRS_SINGLE_VLAN_AI,
2928 MVPP2_PRS_PORT_MASK);
2929 if (err)
2930 return err;
2931
2932 /* Set default double vlan entry */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002933 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002934 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2935 pe.index = MVPP2_PE_VLAN_DBL;
2936
2937 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2938 /* Clear ai for next iterations */
2939 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2940 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_DOUBLE,
2941 MVPP2_PRS_RI_VLAN_MASK);
2942
2943 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_DBL_VLAN_AI_BIT,
2944 MVPP2_PRS_DBL_VLAN_AI_BIT);
2945 /* Unmask all ports */
2946 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2947
2948 /* Update shadow table and hw entry */
2949 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
2950 mvpp2_prs_hw_write(priv, &pe);
2951
2952 /* Set default vlan none entry */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002953 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002954 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2955 pe.index = MVPP2_PE_VLAN_NONE;
2956
2957 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2958 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
2959 MVPP2_PRS_RI_VLAN_MASK);
2960
2961 /* Unmask all ports */
2962 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2963
2964 /* Update shadow table and hw entry */
2965 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
2966 mvpp2_prs_hw_write(priv, &pe);
2967
2968 return 0;
2969}
2970
2971/* Set entries for PPPoE ethertype */
2972static int mvpp2_prs_pppoe_init(struct mvpp2 *priv)
2973{
2974 struct mvpp2_prs_entry pe;
2975 int tid;
2976
2977 /* IPv4 over PPPoE with options */
2978 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2979 MVPP2_PE_LAST_FREE_TID);
2980 if (tid < 0)
2981 return tid;
2982
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002983 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002984 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2985 pe.index = tid;
2986
2987 mvpp2_prs_match_etype(&pe, 0, PPP_IP);
2988
2989 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2990 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
2991 MVPP2_PRS_RI_L3_PROTO_MASK);
2992 /* Skip eth_type + 4 bytes of IP header */
2993 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
2994 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2995 /* Set L3 offset */
2996 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2997 MVPP2_ETH_TYPE_LEN,
2998 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2999
3000 /* Update shadow table and hw entry */
3001 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3002 mvpp2_prs_hw_write(priv, &pe);
3003
3004 /* IPv4 over PPPoE without options */
3005 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3006 MVPP2_PE_LAST_FREE_TID);
3007 if (tid < 0)
3008 return tid;
3009
3010 pe.index = tid;
3011
3012 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
3013 MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
3014 MVPP2_PRS_IPV4_HEAD_MASK |
3015 MVPP2_PRS_IPV4_IHL_MASK);
3016
3017 /* Clear ri before updating */
3018 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
3019 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
3020 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
3021 MVPP2_PRS_RI_L3_PROTO_MASK);
3022
3023 /* Update shadow table and hw entry */
3024 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3025 mvpp2_prs_hw_write(priv, &pe);
3026
3027 /* IPv6 over PPPoE */
3028 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3029 MVPP2_PE_LAST_FREE_TID);
3030 if (tid < 0)
3031 return tid;
3032
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003033 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003034 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
3035 pe.index = tid;
3036
3037 mvpp2_prs_match_etype(&pe, 0, PPP_IPV6);
3038
3039 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
3040 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
3041 MVPP2_PRS_RI_L3_PROTO_MASK);
3042 /* Skip eth_type + 4 bytes of IPv6 header */
3043 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
3044 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3045 /* Set L3 offset */
3046 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
3047 MVPP2_ETH_TYPE_LEN,
3048 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3049
3050 /* Update shadow table and hw entry */
3051 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3052 mvpp2_prs_hw_write(priv, &pe);
3053
3054 /* Non-IP over PPPoE */
3055 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3056 MVPP2_PE_LAST_FREE_TID);
3057 if (tid < 0)
3058 return tid;
3059
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003060 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003061 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
3062 pe.index = tid;
3063
3064 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
3065 MVPP2_PRS_RI_L3_PROTO_MASK);
3066
3067 /* Finished: go to flowid generation */
3068 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3069 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3070 /* Set L3 offset even if it's unknown L3 */
3071 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
3072 MVPP2_ETH_TYPE_LEN,
3073 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3074
3075 /* Update shadow table and hw entry */
3076 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3077 mvpp2_prs_hw_write(priv, &pe);
3078
3079 return 0;
3080}
3081
3082/* Initialize entries for IPv4 */
3083static int mvpp2_prs_ip4_init(struct mvpp2 *priv)
3084{
3085 struct mvpp2_prs_entry pe;
3086 int err;
3087
3088 /* Set entries for TCP, UDP and IGMP over IPv4 */
3089 err = mvpp2_prs_ip4_proto(priv, IPPROTO_TCP, MVPP2_PRS_RI_L4_TCP,
3090 MVPP2_PRS_RI_L4_PROTO_MASK);
3091 if (err)
3092 return err;
3093
3094 err = mvpp2_prs_ip4_proto(priv, IPPROTO_UDP, MVPP2_PRS_RI_L4_UDP,
3095 MVPP2_PRS_RI_L4_PROTO_MASK);
3096 if (err)
3097 return err;
3098
3099 err = mvpp2_prs_ip4_proto(priv, IPPROTO_IGMP,
3100 MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
3101 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
3102 MVPP2_PRS_RI_CPU_CODE_MASK |
3103 MVPP2_PRS_RI_UDF3_MASK);
3104 if (err)
3105 return err;
3106
3107 /* IPv4 Broadcast */
3108 err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_BROAD_CAST);
3109 if (err)
3110 return err;
3111
3112 /* IPv4 Multicast */
3113 err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
3114 if (err)
3115 return err;
3116
3117 /* Default IPv4 entry for unknown protocols */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003118 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003119 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
3120 pe.index = MVPP2_PE_IP4_PROTO_UN;
3121
3122 /* Set next lu to IPv4 */
3123 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
3124 mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3125 /* Set L4 offset */
3126 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
3127 sizeof(struct iphdr) - 4,
3128 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3129 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
3130 MVPP2_PRS_IPV4_DIP_AI_BIT);
3131 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3132 MVPP2_PRS_RI_L4_PROTO_MASK);
3133
3134 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
3135 /* Unmask all ports */
3136 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3137
3138 /* Update shadow table and hw entry */
3139 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3140 mvpp2_prs_hw_write(priv, &pe);
3141
3142 /* Default IPv4 entry for unicast address */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003143 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003144 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
3145 pe.index = MVPP2_PE_IP4_ADDR_UN;
3146
3147 /* Finished: go to flowid generation */
3148 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3149 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3150 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
3151 MVPP2_PRS_RI_L3_ADDR_MASK);
3152
3153 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
3154 MVPP2_PRS_IPV4_DIP_AI_BIT);
3155 /* Unmask all ports */
3156 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3157
3158 /* Update shadow table and hw entry */
3159 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3160 mvpp2_prs_hw_write(priv, &pe);
3161
3162 return 0;
3163}
3164
3165/* Initialize entries for IPv6 */
3166static int mvpp2_prs_ip6_init(struct mvpp2 *priv)
3167{
3168 struct mvpp2_prs_entry pe;
3169 int tid, err;
3170
3171 /* Set entries for TCP, UDP and ICMP over IPv6 */
3172 err = mvpp2_prs_ip6_proto(priv, IPPROTO_TCP,
3173 MVPP2_PRS_RI_L4_TCP,
3174 MVPP2_PRS_RI_L4_PROTO_MASK);
3175 if (err)
3176 return err;
3177
3178 err = mvpp2_prs_ip6_proto(priv, IPPROTO_UDP,
3179 MVPP2_PRS_RI_L4_UDP,
3180 MVPP2_PRS_RI_L4_PROTO_MASK);
3181 if (err)
3182 return err;
3183
3184 err = mvpp2_prs_ip6_proto(priv, IPPROTO_ICMPV6,
3185 MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
3186 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
3187 MVPP2_PRS_RI_CPU_CODE_MASK |
3188 MVPP2_PRS_RI_UDF3_MASK);
3189 if (err)
3190 return err;
3191
3192 /* IPv4 is the last header. This is similar case as 6-TCP or 17-UDP */
3193 /* Result Info: UDF7=1, DS lite */
3194 err = mvpp2_prs_ip6_proto(priv, IPPROTO_IPIP,
3195 MVPP2_PRS_RI_UDF7_IP6_LITE,
3196 MVPP2_PRS_RI_UDF7_MASK);
3197 if (err)
3198 return err;
3199
3200 /* IPv6 multicast */
3201 err = mvpp2_prs_ip6_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
3202 if (err)
3203 return err;
3204
3205 /* Entry for checking hop limit */
3206 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3207 MVPP2_PE_LAST_FREE_TID);
3208 if (tid < 0)
3209 return tid;
3210
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003211 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003212 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3213 pe.index = tid;
3214
3215 /* Finished: go to flowid generation */
3216 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3217 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3218 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN |
3219 MVPP2_PRS_RI_DROP_MASK,
3220 MVPP2_PRS_RI_L3_PROTO_MASK |
3221 MVPP2_PRS_RI_DROP_MASK);
3222
3223 mvpp2_prs_tcam_data_byte_set(&pe, 1, 0x00, MVPP2_PRS_IPV6_HOP_MASK);
3224 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3225 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3226
3227 /* Update shadow table and hw entry */
3228 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3229 mvpp2_prs_hw_write(priv, &pe);
3230
3231 /* Default IPv6 entry for unknown protocols */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003232 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003233 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3234 pe.index = MVPP2_PE_IP6_PROTO_UN;
3235
3236 /* Finished: go to flowid generation */
3237 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3238 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3239 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3240 MVPP2_PRS_RI_L4_PROTO_MASK);
3241 /* Set L4 offset relatively to our current place */
3242 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
3243 sizeof(struct ipv6hdr) - 4,
3244 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3245
3246 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3247 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3248 /* Unmask all ports */
3249 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3250
3251 /* Update shadow table and hw entry */
3252 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3253 mvpp2_prs_hw_write(priv, &pe);
3254
3255 /* Default IPv6 entry for unknown ext protocols */
3256 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3257 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3258 pe.index = MVPP2_PE_IP6_EXT_PROTO_UN;
3259
3260 /* Finished: go to flowid generation */
3261 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3262 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3263 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3264 MVPP2_PRS_RI_L4_PROTO_MASK);
3265
3266 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_EXT_AI_BIT,
3267 MVPP2_PRS_IPV6_EXT_AI_BIT);
3268 /* Unmask all ports */
3269 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3270
3271 /* Update shadow table and hw entry */
3272 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3273 mvpp2_prs_hw_write(priv, &pe);
3274
3275 /* Default IPv6 entry for unicast address */
3276 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3277 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3278 pe.index = MVPP2_PE_IP6_ADDR_UN;
3279
3280 /* Finished: go to IPv6 again */
3281 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
3282 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
3283 MVPP2_PRS_RI_L3_ADDR_MASK);
3284 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3285 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3286 /* Shift back to IPV6 NH */
3287 mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3288
3289 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3290 /* Unmask all ports */
3291 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3292
3293 /* Update shadow table and hw entry */
3294 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
3295 mvpp2_prs_hw_write(priv, &pe);
3296
3297 return 0;
3298}
3299
3300/* Parser default initialization */
3301static int mvpp2_prs_default_init(struct platform_device *pdev,
3302 struct mvpp2 *priv)
3303{
3304 int err, index, i;
3305
3306 /* Enable tcam table */
3307 mvpp2_write(priv, MVPP2_PRS_TCAM_CTRL_REG, MVPP2_PRS_TCAM_EN_MASK);
3308
3309 /* Clear all tcam and sram entries */
3310 for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++) {
3311 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
3312 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
3313 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), 0);
3314
3315 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, index);
3316 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
3317 mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), 0);
3318 }
3319
3320 /* Invalidate all tcam entries */
3321 for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++)
3322 mvpp2_prs_hw_inv(priv, index);
3323
3324 priv->prs_shadow = devm_kcalloc(&pdev->dev, MVPP2_PRS_TCAM_SRAM_SIZE,
Markus Elfring37df25e2017-04-17 09:12:34 +02003325 sizeof(*priv->prs_shadow),
Marcin Wojtas3f518502014-07-10 16:52:13 -03003326 GFP_KERNEL);
3327 if (!priv->prs_shadow)
3328 return -ENOMEM;
3329
3330 /* Always start from lookup = 0 */
3331 for (index = 0; index < MVPP2_MAX_PORTS; index++)
3332 mvpp2_prs_hw_port_init(priv, index, MVPP2_PRS_LU_MH,
3333 MVPP2_PRS_PORT_LU_MAX, 0);
3334
3335 mvpp2_prs_def_flow_init(priv);
3336
3337 mvpp2_prs_mh_init(priv);
3338
3339 mvpp2_prs_mac_init(priv);
3340
3341 mvpp2_prs_dsa_init(priv);
3342
3343 err = mvpp2_prs_etype_init(priv);
3344 if (err)
3345 return err;
3346
3347 err = mvpp2_prs_vlan_init(pdev, priv);
3348 if (err)
3349 return err;
3350
3351 err = mvpp2_prs_pppoe_init(priv);
3352 if (err)
3353 return err;
3354
3355 err = mvpp2_prs_ip6_init(priv);
3356 if (err)
3357 return err;
3358
3359 err = mvpp2_prs_ip4_init(priv);
3360 if (err)
3361 return err;
3362
3363 return 0;
3364}
3365
3366/* Compare MAC DA with tcam entry data */
3367static bool mvpp2_prs_mac_range_equals(struct mvpp2_prs_entry *pe,
3368 const u8 *da, unsigned char *mask)
3369{
3370 unsigned char tcam_byte, tcam_mask;
3371 int index;
3372
3373 for (index = 0; index < ETH_ALEN; index++) {
3374 mvpp2_prs_tcam_data_byte_get(pe, index, &tcam_byte, &tcam_mask);
3375 if (tcam_mask != mask[index])
3376 return false;
3377
3378 if ((tcam_mask & tcam_byte) != (da[index] & mask[index]))
3379 return false;
3380 }
3381
3382 return true;
3383}
3384
3385/* Find tcam entry with matched pair <MAC DA, port> */
3386static struct mvpp2_prs_entry *
3387mvpp2_prs_mac_da_range_find(struct mvpp2 *priv, int pmap, const u8 *da,
3388 unsigned char *mask, int udf_type)
3389{
3390 struct mvpp2_prs_entry *pe;
3391 int tid;
3392
3393 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
3394 if (!pe)
3395 return NULL;
3396 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
3397
3398 /* Go through the all entires with MVPP2_PRS_LU_MAC */
3399 for (tid = MVPP2_PE_FIRST_FREE_TID;
3400 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
3401 unsigned int entry_pmap;
3402
3403 if (!priv->prs_shadow[tid].valid ||
3404 (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
3405 (priv->prs_shadow[tid].udf != udf_type))
3406 continue;
3407
3408 pe->index = tid;
3409 mvpp2_prs_hw_read(priv, pe);
3410 entry_pmap = mvpp2_prs_tcam_port_map_get(pe);
3411
3412 if (mvpp2_prs_mac_range_equals(pe, da, mask) &&
3413 entry_pmap == pmap)
3414 return pe;
3415 }
3416 kfree(pe);
3417
3418 return NULL;
3419}
3420
3421/* Update parser's mac da entry */
3422static int mvpp2_prs_mac_da_accept(struct mvpp2 *priv, int port,
3423 const u8 *da, bool add)
3424{
3425 struct mvpp2_prs_entry *pe;
3426 unsigned int pmap, len, ri;
3427 unsigned char mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3428 int tid;
3429
3430 /* Scan TCAM and see if entry with this <MAC DA, port> already exist */
3431 pe = mvpp2_prs_mac_da_range_find(priv, (1 << port), da, mask,
3432 MVPP2_PRS_UDF_MAC_DEF);
3433
3434 /* No such entry */
3435 if (!pe) {
3436 if (!add)
3437 return 0;
3438
3439 /* Create new TCAM entry */
3440 /* Find first range mac entry*/
3441 for (tid = MVPP2_PE_FIRST_FREE_TID;
3442 tid <= MVPP2_PE_LAST_FREE_TID; tid++)
3443 if (priv->prs_shadow[tid].valid &&
3444 (priv->prs_shadow[tid].lu == MVPP2_PRS_LU_MAC) &&
3445 (priv->prs_shadow[tid].udf ==
3446 MVPP2_PRS_UDF_MAC_RANGE))
3447 break;
3448
3449 /* Go through the all entries from first to last */
3450 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3451 tid - 1);
3452 if (tid < 0)
3453 return tid;
3454
3455 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
3456 if (!pe)
Amitoj Kaur Chawlac2bb7bc2016-02-04 19:25:26 +05303457 return -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003458 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
3459 pe->index = tid;
3460
3461 /* Mask all ports */
3462 mvpp2_prs_tcam_port_map_set(pe, 0);
3463 }
3464
3465 /* Update port mask */
3466 mvpp2_prs_tcam_port_set(pe, port, add);
3467
3468 /* Invalidate the entry if no ports are left enabled */
3469 pmap = mvpp2_prs_tcam_port_map_get(pe);
3470 if (pmap == 0) {
3471 if (add) {
3472 kfree(pe);
Amitoj Kaur Chawlac2bb7bc2016-02-04 19:25:26 +05303473 return -EINVAL;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003474 }
3475 mvpp2_prs_hw_inv(priv, pe->index);
3476 priv->prs_shadow[pe->index].valid = false;
3477 kfree(pe);
3478 return 0;
3479 }
3480
3481 /* Continue - set next lookup */
3482 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_DSA);
3483
3484 /* Set match on DA */
3485 len = ETH_ALEN;
3486 while (len--)
3487 mvpp2_prs_tcam_data_byte_set(pe, len, da[len], 0xff);
3488
3489 /* Set result info bits */
3490 if (is_broadcast_ether_addr(da))
3491 ri = MVPP2_PRS_RI_L2_BCAST;
3492 else if (is_multicast_ether_addr(da))
3493 ri = MVPP2_PRS_RI_L2_MCAST;
3494 else
3495 ri = MVPP2_PRS_RI_L2_UCAST | MVPP2_PRS_RI_MAC_ME_MASK;
3496
3497 mvpp2_prs_sram_ri_update(pe, ri, MVPP2_PRS_RI_L2_CAST_MASK |
3498 MVPP2_PRS_RI_MAC_ME_MASK);
3499 mvpp2_prs_shadow_ri_set(priv, pe->index, ri, MVPP2_PRS_RI_L2_CAST_MASK |
3500 MVPP2_PRS_RI_MAC_ME_MASK);
3501
3502 /* Shift to ethertype */
3503 mvpp2_prs_sram_shift_set(pe, 2 * ETH_ALEN,
3504 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3505
3506 /* Update shadow table and hw entry */
3507 priv->prs_shadow[pe->index].udf = MVPP2_PRS_UDF_MAC_DEF;
3508 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_MAC);
3509 mvpp2_prs_hw_write(priv, pe);
3510
3511 kfree(pe);
3512
3513 return 0;
3514}
3515
3516static int mvpp2_prs_update_mac_da(struct net_device *dev, const u8 *da)
3517{
3518 struct mvpp2_port *port = netdev_priv(dev);
3519 int err;
3520
3521 /* Remove old parser entry */
3522 err = mvpp2_prs_mac_da_accept(port->priv, port->id, dev->dev_addr,
3523 false);
3524 if (err)
3525 return err;
3526
3527 /* Add new parser entry */
3528 err = mvpp2_prs_mac_da_accept(port->priv, port->id, da, true);
3529 if (err)
3530 return err;
3531
3532 /* Set addr in the device */
3533 ether_addr_copy(dev->dev_addr, da);
3534
3535 return 0;
3536}
3537
3538/* Delete all port's multicast simple (not range) entries */
3539static void mvpp2_prs_mcast_del_all(struct mvpp2 *priv, int port)
3540{
3541 struct mvpp2_prs_entry pe;
3542 int index, tid;
3543
3544 for (tid = MVPP2_PE_FIRST_FREE_TID;
3545 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
3546 unsigned char da[ETH_ALEN], da_mask[ETH_ALEN];
3547
3548 if (!priv->prs_shadow[tid].valid ||
3549 (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
3550 (priv->prs_shadow[tid].udf != MVPP2_PRS_UDF_MAC_DEF))
3551 continue;
3552
3553 /* Only simple mac entries */
3554 pe.index = tid;
3555 mvpp2_prs_hw_read(priv, &pe);
3556
3557 /* Read mac addr from entry */
3558 for (index = 0; index < ETH_ALEN; index++)
3559 mvpp2_prs_tcam_data_byte_get(&pe, index, &da[index],
3560 &da_mask[index]);
3561
3562 if (is_multicast_ether_addr(da) && !is_broadcast_ether_addr(da))
3563 /* Delete this entry */
3564 mvpp2_prs_mac_da_accept(priv, port, da, false);
3565 }
3566}
3567
3568static int mvpp2_prs_tag_mode_set(struct mvpp2 *priv, int port, int type)
3569{
3570 switch (type) {
3571 case MVPP2_TAG_TYPE_EDSA:
3572 /* Add port to EDSA entries */
3573 mvpp2_prs_dsa_tag_set(priv, port, true,
3574 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3575 mvpp2_prs_dsa_tag_set(priv, port, true,
3576 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3577 /* Remove port from DSA entries */
3578 mvpp2_prs_dsa_tag_set(priv, port, false,
3579 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3580 mvpp2_prs_dsa_tag_set(priv, port, false,
3581 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3582 break;
3583
3584 case MVPP2_TAG_TYPE_DSA:
3585 /* Add port to DSA entries */
3586 mvpp2_prs_dsa_tag_set(priv, port, true,
3587 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3588 mvpp2_prs_dsa_tag_set(priv, port, true,
3589 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3590 /* Remove port from EDSA entries */
3591 mvpp2_prs_dsa_tag_set(priv, port, false,
3592 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3593 mvpp2_prs_dsa_tag_set(priv, port, false,
3594 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3595 break;
3596
3597 case MVPP2_TAG_TYPE_MH:
3598 case MVPP2_TAG_TYPE_NONE:
3599 /* Remove port form EDSA and DSA entries */
3600 mvpp2_prs_dsa_tag_set(priv, port, false,
3601 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3602 mvpp2_prs_dsa_tag_set(priv, port, false,
3603 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3604 mvpp2_prs_dsa_tag_set(priv, port, false,
3605 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3606 mvpp2_prs_dsa_tag_set(priv, port, false,
3607 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3608 break;
3609
3610 default:
3611 if ((type < 0) || (type > MVPP2_TAG_TYPE_EDSA))
3612 return -EINVAL;
3613 }
3614
3615 return 0;
3616}
3617
3618/* Set prs flow for the port */
3619static int mvpp2_prs_def_flow(struct mvpp2_port *port)
3620{
3621 struct mvpp2_prs_entry *pe;
3622 int tid;
3623
3624 pe = mvpp2_prs_flow_find(port->priv, port->id);
3625
3626 /* Such entry not exist */
3627 if (!pe) {
3628 /* Go through the all entires from last to first */
3629 tid = mvpp2_prs_tcam_first_free(port->priv,
3630 MVPP2_PE_LAST_FREE_TID,
3631 MVPP2_PE_FIRST_FREE_TID);
3632 if (tid < 0)
3633 return tid;
3634
3635 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
3636 if (!pe)
3637 return -ENOMEM;
3638
3639 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
3640 pe->index = tid;
3641
3642 /* Set flow ID*/
3643 mvpp2_prs_sram_ai_update(pe, port->id, MVPP2_PRS_FLOW_ID_MASK);
3644 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
3645
3646 /* Update shadow table */
3647 mvpp2_prs_shadow_set(port->priv, pe->index, MVPP2_PRS_LU_FLOWS);
3648 }
3649
3650 mvpp2_prs_tcam_port_map_set(pe, (1 << port->id));
3651 mvpp2_prs_hw_write(port->priv, pe);
3652 kfree(pe);
3653
3654 return 0;
3655}
3656
3657/* Classifier configuration routines */
3658
3659/* Update classification flow table registers */
3660static void mvpp2_cls_flow_write(struct mvpp2 *priv,
3661 struct mvpp2_cls_flow_entry *fe)
3662{
3663 mvpp2_write(priv, MVPP2_CLS_FLOW_INDEX_REG, fe->index);
3664 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL0_REG, fe->data[0]);
3665 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL1_REG, fe->data[1]);
3666 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL2_REG, fe->data[2]);
3667}
3668
3669/* Update classification lookup table register */
3670static void mvpp2_cls_lookup_write(struct mvpp2 *priv,
3671 struct mvpp2_cls_lookup_entry *le)
3672{
3673 u32 val;
3674
3675 val = (le->way << MVPP2_CLS_LKP_INDEX_WAY_OFFS) | le->lkpid;
3676 mvpp2_write(priv, MVPP2_CLS_LKP_INDEX_REG, val);
3677 mvpp2_write(priv, MVPP2_CLS_LKP_TBL_REG, le->data);
3678}
3679
3680/* Classifier default initialization */
3681static void mvpp2_cls_init(struct mvpp2 *priv)
3682{
3683 struct mvpp2_cls_lookup_entry le;
3684 struct mvpp2_cls_flow_entry fe;
3685 int index;
3686
3687 /* Enable classifier */
3688 mvpp2_write(priv, MVPP2_CLS_MODE_REG, MVPP2_CLS_MODE_ACTIVE_MASK);
3689
3690 /* Clear classifier flow table */
Arnd Bergmanne8f967c2016-11-24 17:28:12 +01003691 memset(&fe.data, 0, sizeof(fe.data));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003692 for (index = 0; index < MVPP2_CLS_FLOWS_TBL_SIZE; index++) {
3693 fe.index = index;
3694 mvpp2_cls_flow_write(priv, &fe);
3695 }
3696
3697 /* Clear classifier lookup table */
3698 le.data = 0;
3699 for (index = 0; index < MVPP2_CLS_LKP_TBL_SIZE; index++) {
3700 le.lkpid = index;
3701 le.way = 0;
3702 mvpp2_cls_lookup_write(priv, &le);
3703
3704 le.way = 1;
3705 mvpp2_cls_lookup_write(priv, &le);
3706 }
3707}
3708
3709static void mvpp2_cls_port_config(struct mvpp2_port *port)
3710{
3711 struct mvpp2_cls_lookup_entry le;
3712 u32 val;
3713
3714 /* Set way for the port */
3715 val = mvpp2_read(port->priv, MVPP2_CLS_PORT_WAY_REG);
3716 val &= ~MVPP2_CLS_PORT_WAY_MASK(port->id);
3717 mvpp2_write(port->priv, MVPP2_CLS_PORT_WAY_REG, val);
3718
3719 /* Pick the entry to be accessed in lookup ID decoding table
3720 * according to the way and lkpid.
3721 */
3722 le.lkpid = port->id;
3723 le.way = 0;
3724 le.data = 0;
3725
3726 /* Set initial CPU queue for receiving packets */
3727 le.data &= ~MVPP2_CLS_LKP_TBL_RXQ_MASK;
3728 le.data |= port->first_rxq;
3729
3730 /* Disable classification engines */
3731 le.data &= ~MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK;
3732
3733 /* Update lookup ID table entry */
3734 mvpp2_cls_lookup_write(port->priv, &le);
3735}
3736
3737/* Set CPU queue number for oversize packets */
3738static void mvpp2_cls_oversize_rxq_set(struct mvpp2_port *port)
3739{
3740 u32 val;
3741
3742 mvpp2_write(port->priv, MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port->id),
3743 port->first_rxq & MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK);
3744
3745 mvpp2_write(port->priv, MVPP2_CLS_SWFWD_P2HQ_REG(port->id),
3746 (port->first_rxq >> MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS));
3747
3748 val = mvpp2_read(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG);
3749 val |= MVPP2_CLS_SWFWD_PCTRL_MASK(port->id);
3750 mvpp2_write(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG, val);
3751}
3752
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003753static void *mvpp2_frag_alloc(const struct mvpp2_bm_pool *pool)
3754{
3755 if (likely(pool->frag_size <= PAGE_SIZE))
3756 return netdev_alloc_frag(pool->frag_size);
3757 else
3758 return kmalloc(pool->frag_size, GFP_ATOMIC);
3759}
3760
3761static void mvpp2_frag_free(const struct mvpp2_bm_pool *pool, void *data)
3762{
3763 if (likely(pool->frag_size <= PAGE_SIZE))
3764 skb_free_frag(data);
3765 else
3766 kfree(data);
3767}
3768
Marcin Wojtas3f518502014-07-10 16:52:13 -03003769/* Buffer Manager configuration routines */
3770
3771/* Create pool */
3772static int mvpp2_bm_pool_create(struct platform_device *pdev,
3773 struct mvpp2 *priv,
3774 struct mvpp2_bm_pool *bm_pool, int size)
3775{
Marcin Wojtas3f518502014-07-10 16:52:13 -03003776 u32 val;
3777
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003778 /* Number of buffer pointers must be a multiple of 16, as per
3779 * hardware constraints
3780 */
3781 if (!IS_ALIGNED(size, 16))
3782 return -EINVAL;
3783
3784 /* PPv2.1 needs 8 bytes per buffer pointer, PPv2.2 needs 16
3785 * bytes per buffer pointer
3786 */
3787 if (priv->hw_version == MVPP21)
3788 bm_pool->size_bytes = 2 * sizeof(u32) * size;
3789 else
3790 bm_pool->size_bytes = 2 * sizeof(u64) * size;
3791
3792 bm_pool->virt_addr = dma_alloc_coherent(&pdev->dev, bm_pool->size_bytes,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003793 &bm_pool->dma_addr,
Marcin Wojtas3f518502014-07-10 16:52:13 -03003794 GFP_KERNEL);
3795 if (!bm_pool->virt_addr)
3796 return -ENOMEM;
3797
Thomas Petazzonid3158802017-02-21 11:28:13 +01003798 if (!IS_ALIGNED((unsigned long)bm_pool->virt_addr,
3799 MVPP2_BM_POOL_PTR_ALIGN)) {
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003800 dma_free_coherent(&pdev->dev, bm_pool->size_bytes,
3801 bm_pool->virt_addr, bm_pool->dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003802 dev_err(&pdev->dev, "BM pool %d is not %d bytes aligned\n",
3803 bm_pool->id, MVPP2_BM_POOL_PTR_ALIGN);
3804 return -ENOMEM;
3805 }
3806
3807 mvpp2_write(priv, MVPP2_BM_POOL_BASE_REG(bm_pool->id),
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003808 lower_32_bits(bm_pool->dma_addr));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003809 mvpp2_write(priv, MVPP2_BM_POOL_SIZE_REG(bm_pool->id), size);
3810
3811 val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
3812 val |= MVPP2_BM_START_MASK;
3813 mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
3814
3815 bm_pool->type = MVPP2_BM_FREE;
3816 bm_pool->size = size;
3817 bm_pool->pkt_size = 0;
3818 bm_pool->buf_num = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003819
3820 return 0;
3821}
3822
3823/* Set pool buffer size */
3824static void mvpp2_bm_pool_bufsize_set(struct mvpp2 *priv,
3825 struct mvpp2_bm_pool *bm_pool,
3826 int buf_size)
3827{
3828 u32 val;
3829
3830 bm_pool->buf_size = buf_size;
3831
3832 val = ALIGN(buf_size, 1 << MVPP2_POOL_BUF_SIZE_OFFSET);
3833 mvpp2_write(priv, MVPP2_POOL_BUF_SIZE_REG(bm_pool->id), val);
3834}
3835
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003836static void mvpp2_bm_bufs_get_addrs(struct device *dev, struct mvpp2 *priv,
3837 struct mvpp2_bm_pool *bm_pool,
3838 dma_addr_t *dma_addr,
3839 phys_addr_t *phys_addr)
3840{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02003841 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01003842
3843 *dma_addr = mvpp2_percpu_read(priv, cpu,
3844 MVPP2_BM_PHY_ALLOC_REG(bm_pool->id));
3845 *phys_addr = mvpp2_percpu_read(priv, cpu, MVPP2_BM_VIRT_ALLOC_REG);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003846
3847 if (priv->hw_version == MVPP22) {
3848 u32 val;
3849 u32 dma_addr_highbits, phys_addr_highbits;
3850
Thomas Petazzonia7868412017-03-07 16:53:13 +01003851 val = mvpp2_percpu_read(priv, cpu, MVPP22_BM_ADDR_HIGH_ALLOC);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003852 dma_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_PHYS_MASK);
3853 phys_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_VIRT_MASK) >>
3854 MVPP22_BM_ADDR_HIGH_VIRT_SHIFT;
3855
3856 if (sizeof(dma_addr_t) == 8)
3857 *dma_addr |= (u64)dma_addr_highbits << 32;
3858
3859 if (sizeof(phys_addr_t) == 8)
3860 *phys_addr |= (u64)phys_addr_highbits << 32;
3861 }
Thomas Petazzonia704bb52017-06-10 23:18:22 +02003862
3863 put_cpu();
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003864}
3865
Ezequiel Garcia7861f122014-07-21 13:48:14 -03003866/* Free all buffers from the pool */
Marcin Wojtas4229d502015-12-03 15:20:50 +01003867static void mvpp2_bm_bufs_free(struct device *dev, struct mvpp2 *priv,
3868 struct mvpp2_bm_pool *bm_pool)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003869{
3870 int i;
3871
Ezequiel Garcia7861f122014-07-21 13:48:14 -03003872 for (i = 0; i < bm_pool->buf_num; i++) {
Thomas Petazzoni20396132017-03-07 16:53:00 +01003873 dma_addr_t buf_dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003874 phys_addr_t buf_phys_addr;
3875 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003876
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003877 mvpp2_bm_bufs_get_addrs(dev, priv, bm_pool,
3878 &buf_dma_addr, &buf_phys_addr);
Marcin Wojtas4229d502015-12-03 15:20:50 +01003879
Thomas Petazzoni20396132017-03-07 16:53:00 +01003880 dma_unmap_single(dev, buf_dma_addr,
Marcin Wojtas4229d502015-12-03 15:20:50 +01003881 bm_pool->buf_size, DMA_FROM_DEVICE);
3882
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003883 data = (void *)phys_to_virt(buf_phys_addr);
3884 if (!data)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003885 break;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003886
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003887 mvpp2_frag_free(bm_pool, data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003888 }
3889
3890 /* Update BM driver with number of buffers removed from pool */
3891 bm_pool->buf_num -= i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003892}
3893
3894/* Cleanup pool */
3895static int mvpp2_bm_pool_destroy(struct platform_device *pdev,
3896 struct mvpp2 *priv,
3897 struct mvpp2_bm_pool *bm_pool)
3898{
Marcin Wojtas3f518502014-07-10 16:52:13 -03003899 u32 val;
3900
Marcin Wojtas4229d502015-12-03 15:20:50 +01003901 mvpp2_bm_bufs_free(&pdev->dev, priv, bm_pool);
Ezequiel Garciad74c96c2014-07-21 13:48:13 -03003902 if (bm_pool->buf_num) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03003903 WARN(1, "cannot free all buffers in pool %d\n", bm_pool->id);
3904 return 0;
3905 }
3906
3907 val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
3908 val |= MVPP2_BM_STOP_MASK;
3909 mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
3910
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003911 dma_free_coherent(&pdev->dev, bm_pool->size_bytes,
Marcin Wojtas3f518502014-07-10 16:52:13 -03003912 bm_pool->virt_addr,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003913 bm_pool->dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003914 return 0;
3915}
3916
3917static int mvpp2_bm_pools_init(struct platform_device *pdev,
3918 struct mvpp2 *priv)
3919{
3920 int i, err, size;
3921 struct mvpp2_bm_pool *bm_pool;
3922
3923 /* Create all pools with maximum size */
3924 size = MVPP2_BM_POOL_SIZE_MAX;
3925 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
3926 bm_pool = &priv->bm_pools[i];
3927 bm_pool->id = i;
3928 err = mvpp2_bm_pool_create(pdev, priv, bm_pool, size);
3929 if (err)
3930 goto err_unroll_pools;
3931 mvpp2_bm_pool_bufsize_set(priv, bm_pool, 0);
3932 }
3933 return 0;
3934
3935err_unroll_pools:
3936 dev_err(&pdev->dev, "failed to create BM pool %d, size %d\n", i, size);
3937 for (i = i - 1; i >= 0; i--)
3938 mvpp2_bm_pool_destroy(pdev, priv, &priv->bm_pools[i]);
3939 return err;
3940}
3941
3942static int mvpp2_bm_init(struct platform_device *pdev, struct mvpp2 *priv)
3943{
3944 int i, err;
3945
3946 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
3947 /* Mask BM all interrupts */
3948 mvpp2_write(priv, MVPP2_BM_INTR_MASK_REG(i), 0);
3949 /* Clear BM cause register */
3950 mvpp2_write(priv, MVPP2_BM_INTR_CAUSE_REG(i), 0);
3951 }
3952
3953 /* Allocate and initialize BM pools */
3954 priv->bm_pools = devm_kcalloc(&pdev->dev, MVPP2_BM_POOLS_NUM,
Markus Elfring81f915e2017-04-17 09:06:33 +02003955 sizeof(*priv->bm_pools), GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003956 if (!priv->bm_pools)
3957 return -ENOMEM;
3958
3959 err = mvpp2_bm_pools_init(pdev, priv);
3960 if (err < 0)
3961 return err;
3962 return 0;
3963}
3964
3965/* Attach long pool to rxq */
3966static void mvpp2_rxq_long_pool_set(struct mvpp2_port *port,
3967 int lrxq, int long_pool)
3968{
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003969 u32 val, mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003970 int prxq;
3971
3972 /* Get queue physical ID */
3973 prxq = port->rxqs[lrxq]->id;
3974
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003975 if (port->priv->hw_version == MVPP21)
3976 mask = MVPP21_RXQ_POOL_LONG_MASK;
3977 else
3978 mask = MVPP22_RXQ_POOL_LONG_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003979
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003980 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
3981 val &= ~mask;
3982 val |= (long_pool << MVPP2_RXQ_POOL_LONG_OFFS) & mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003983 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
3984}
3985
3986/* Attach short pool to rxq */
3987static void mvpp2_rxq_short_pool_set(struct mvpp2_port *port,
3988 int lrxq, int short_pool)
3989{
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003990 u32 val, mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003991 int prxq;
3992
3993 /* Get queue physical ID */
3994 prxq = port->rxqs[lrxq]->id;
3995
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003996 if (port->priv->hw_version == MVPP21)
3997 mask = MVPP21_RXQ_POOL_SHORT_MASK;
3998 else
3999 mask = MVPP22_RXQ_POOL_SHORT_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004000
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004001 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
4002 val &= ~mask;
4003 val |= (short_pool << MVPP2_RXQ_POOL_SHORT_OFFS) & mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004004 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
4005}
4006
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004007static void *mvpp2_buf_alloc(struct mvpp2_port *port,
4008 struct mvpp2_bm_pool *bm_pool,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004009 dma_addr_t *buf_dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004010 phys_addr_t *buf_phys_addr,
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004011 gfp_t gfp_mask)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004012{
Thomas Petazzoni20396132017-03-07 16:53:00 +01004013 dma_addr_t dma_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004014 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004015
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004016 data = mvpp2_frag_alloc(bm_pool);
4017 if (!data)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004018 return NULL;
4019
Thomas Petazzoni20396132017-03-07 16:53:00 +01004020 dma_addr = dma_map_single(port->dev->dev.parent, data,
4021 MVPP2_RX_BUF_SIZE(bm_pool->pkt_size),
4022 DMA_FROM_DEVICE);
4023 if (unlikely(dma_mapping_error(port->dev->dev.parent, dma_addr))) {
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004024 mvpp2_frag_free(bm_pool, data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004025 return NULL;
4026 }
Thomas Petazzoni20396132017-03-07 16:53:00 +01004027 *buf_dma_addr = dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004028 *buf_phys_addr = virt_to_phys(data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004029
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004030 return data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004031}
4032
Marcin Wojtas3f518502014-07-10 16:52:13 -03004033/* Release buffer to BM */
4034static inline void mvpp2_bm_pool_put(struct mvpp2_port *port, int pool,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004035 dma_addr_t buf_dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004036 phys_addr_t buf_phys_addr)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004037{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004038 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01004039
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004040 if (port->priv->hw_version == MVPP22) {
4041 u32 val = 0;
4042
4043 if (sizeof(dma_addr_t) == 8)
4044 val |= upper_32_bits(buf_dma_addr) &
4045 MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK;
4046
4047 if (sizeof(phys_addr_t) == 8)
4048 val |= (upper_32_bits(buf_phys_addr)
4049 << MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT) &
4050 MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK;
4051
Thomas Petazzonia7868412017-03-07 16:53:13 +01004052 mvpp2_percpu_write(port->priv, cpu,
4053 MVPP22_BM_ADDR_HIGH_RLS_REG, val);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004054 }
4055
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004056 /* MVPP2_BM_VIRT_RLS_REG is not interpreted by HW, and simply
4057 * returned in the "cookie" field of the RX
4058 * descriptor. Instead of storing the virtual address, we
4059 * store the physical address
4060 */
Thomas Petazzonia7868412017-03-07 16:53:13 +01004061 mvpp2_percpu_write(port->priv, cpu,
4062 MVPP2_BM_VIRT_RLS_REG, buf_phys_addr);
4063 mvpp2_percpu_write(port->priv, cpu,
4064 MVPP2_BM_PHY_RLS_REG(pool), buf_dma_addr);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004065
4066 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03004067}
4068
Marcin Wojtas3f518502014-07-10 16:52:13 -03004069/* Allocate buffers for the pool */
4070static int mvpp2_bm_bufs_add(struct mvpp2_port *port,
4071 struct mvpp2_bm_pool *bm_pool, int buf_num)
4072{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004073 int i, buf_size, total_size;
Thomas Petazzoni20396132017-03-07 16:53:00 +01004074 dma_addr_t dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004075 phys_addr_t phys_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004076 void *buf;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004077
4078 buf_size = MVPP2_RX_BUF_SIZE(bm_pool->pkt_size);
4079 total_size = MVPP2_RX_TOTAL_SIZE(buf_size);
4080
4081 if (buf_num < 0 ||
4082 (buf_num + bm_pool->buf_num > bm_pool->size)) {
4083 netdev_err(port->dev,
4084 "cannot allocate %d buffers for pool %d\n",
4085 buf_num, bm_pool->id);
4086 return 0;
4087 }
4088
Marcin Wojtas3f518502014-07-10 16:52:13 -03004089 for (i = 0; i < buf_num; i++) {
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004090 buf = mvpp2_buf_alloc(port, bm_pool, &dma_addr,
4091 &phys_addr, GFP_KERNEL);
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004092 if (!buf)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004093 break;
4094
Thomas Petazzoni20396132017-03-07 16:53:00 +01004095 mvpp2_bm_pool_put(port, bm_pool->id, dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004096 phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004097 }
4098
4099 /* Update BM driver with number of buffers added to pool */
4100 bm_pool->buf_num += i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004101
4102 netdev_dbg(port->dev,
4103 "%s pool %d: pkt_size=%4d, buf_size=%4d, total_size=%4d\n",
4104 bm_pool->type == MVPP2_BM_SWF_SHORT ? "short" : " long",
4105 bm_pool->id, bm_pool->pkt_size, buf_size, total_size);
4106
4107 netdev_dbg(port->dev,
4108 "%s pool %d: %d of %d buffers added\n",
4109 bm_pool->type == MVPP2_BM_SWF_SHORT ? "short" : " long",
4110 bm_pool->id, i, buf_num);
4111 return i;
4112}
4113
4114/* Notify the driver that BM pool is being used as specific type and return the
4115 * pool pointer on success
4116 */
4117static struct mvpp2_bm_pool *
4118mvpp2_bm_pool_use(struct mvpp2_port *port, int pool, enum mvpp2_bm_type type,
4119 int pkt_size)
4120{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004121 struct mvpp2_bm_pool *new_pool = &port->priv->bm_pools[pool];
4122 int num;
4123
4124 if (new_pool->type != MVPP2_BM_FREE && new_pool->type != type) {
4125 netdev_err(port->dev, "mixing pool types is forbidden\n");
4126 return NULL;
4127 }
4128
Marcin Wojtas3f518502014-07-10 16:52:13 -03004129 if (new_pool->type == MVPP2_BM_FREE)
4130 new_pool->type = type;
4131
4132 /* Allocate buffers in case BM pool is used as long pool, but packet
4133 * size doesn't match MTU or BM pool hasn't being used yet
4134 */
4135 if (((type == MVPP2_BM_SWF_LONG) && (pkt_size > new_pool->pkt_size)) ||
4136 (new_pool->pkt_size == 0)) {
4137 int pkts_num;
4138
4139 /* Set default buffer number or free all the buffers in case
4140 * the pool is not empty
4141 */
4142 pkts_num = new_pool->buf_num;
4143 if (pkts_num == 0)
4144 pkts_num = type == MVPP2_BM_SWF_LONG ?
4145 MVPP2_BM_LONG_BUF_NUM :
4146 MVPP2_BM_SHORT_BUF_NUM;
4147 else
Marcin Wojtas4229d502015-12-03 15:20:50 +01004148 mvpp2_bm_bufs_free(port->dev->dev.parent,
4149 port->priv, new_pool);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004150
4151 new_pool->pkt_size = pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004152 new_pool->frag_size =
4153 SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
4154 MVPP2_SKB_SHINFO_SIZE;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004155
4156 /* Allocate buffers for this pool */
4157 num = mvpp2_bm_bufs_add(port, new_pool, pkts_num);
4158 if (num != pkts_num) {
4159 WARN(1, "pool %d: %d of %d allocated\n",
4160 new_pool->id, num, pkts_num);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004161 return NULL;
4162 }
4163 }
4164
4165 mvpp2_bm_pool_bufsize_set(port->priv, new_pool,
4166 MVPP2_RX_BUF_SIZE(new_pool->pkt_size));
4167
Marcin Wojtas3f518502014-07-10 16:52:13 -03004168 return new_pool;
4169}
4170
4171/* Initialize pools for swf */
4172static int mvpp2_swf_bm_pool_init(struct mvpp2_port *port)
4173{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004174 int rxq;
4175
4176 if (!port->pool_long) {
4177 port->pool_long =
4178 mvpp2_bm_pool_use(port, MVPP2_BM_SWF_LONG_POOL(port->id),
4179 MVPP2_BM_SWF_LONG,
4180 port->pkt_size);
4181 if (!port->pool_long)
4182 return -ENOMEM;
4183
Marcin Wojtas3f518502014-07-10 16:52:13 -03004184 port->pool_long->port_map |= (1 << port->id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004185
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004186 for (rxq = 0; rxq < port->nrxqs; rxq++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004187 mvpp2_rxq_long_pool_set(port, rxq, port->pool_long->id);
4188 }
4189
4190 if (!port->pool_short) {
4191 port->pool_short =
4192 mvpp2_bm_pool_use(port, MVPP2_BM_SWF_SHORT_POOL,
4193 MVPP2_BM_SWF_SHORT,
4194 MVPP2_BM_SHORT_PKT_SIZE);
4195 if (!port->pool_short)
4196 return -ENOMEM;
4197
Marcin Wojtas3f518502014-07-10 16:52:13 -03004198 port->pool_short->port_map |= (1 << port->id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004199
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004200 for (rxq = 0; rxq < port->nrxqs; rxq++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004201 mvpp2_rxq_short_pool_set(port, rxq,
4202 port->pool_short->id);
4203 }
4204
4205 return 0;
4206}
4207
4208static int mvpp2_bm_update_mtu(struct net_device *dev, int mtu)
4209{
4210 struct mvpp2_port *port = netdev_priv(dev);
4211 struct mvpp2_bm_pool *port_pool = port->pool_long;
4212 int num, pkts_num = port_pool->buf_num;
4213 int pkt_size = MVPP2_RX_PKT_SIZE(mtu);
4214
4215 /* Update BM pool with new buffer size */
Marcin Wojtas4229d502015-12-03 15:20:50 +01004216 mvpp2_bm_bufs_free(dev->dev.parent, port->priv, port_pool);
Ezequiel Garciad74c96c2014-07-21 13:48:13 -03004217 if (port_pool->buf_num) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004218 WARN(1, "cannot free all buffers in pool %d\n", port_pool->id);
4219 return -EIO;
4220 }
4221
4222 port_pool->pkt_size = pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004223 port_pool->frag_size = SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
4224 MVPP2_SKB_SHINFO_SIZE;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004225 num = mvpp2_bm_bufs_add(port, port_pool, pkts_num);
4226 if (num != pkts_num) {
4227 WARN(1, "pool %d: %d of %d allocated\n",
4228 port_pool->id, num, pkts_num);
4229 return -EIO;
4230 }
4231
4232 mvpp2_bm_pool_bufsize_set(port->priv, port_pool,
4233 MVPP2_RX_BUF_SIZE(port_pool->pkt_size));
4234 dev->mtu = mtu;
4235 netdev_update_features(dev);
4236 return 0;
4237}
4238
4239static inline void mvpp2_interrupts_enable(struct mvpp2_port *port)
4240{
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004241 int i, sw_thread_mask = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004242
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004243 for (i = 0; i < port->nqvecs; i++)
4244 sw_thread_mask |= port->qvecs[i].sw_thread_mask;
4245
Marcin Wojtas3f518502014-07-10 16:52:13 -03004246 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004247 MVPP2_ISR_ENABLE_INTERRUPT(sw_thread_mask));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004248}
4249
4250static inline void mvpp2_interrupts_disable(struct mvpp2_port *port)
4251{
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004252 int i, sw_thread_mask = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004253
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004254 for (i = 0; i < port->nqvecs; i++)
4255 sw_thread_mask |= port->qvecs[i].sw_thread_mask;
4256
Marcin Wojtas3f518502014-07-10 16:52:13 -03004257 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004258 MVPP2_ISR_DISABLE_INTERRUPT(sw_thread_mask));
4259}
4260
4261static inline void mvpp2_qvec_interrupt_enable(struct mvpp2_queue_vector *qvec)
4262{
4263 struct mvpp2_port *port = qvec->port;
4264
4265 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
4266 MVPP2_ISR_ENABLE_INTERRUPT(qvec->sw_thread_mask));
4267}
4268
4269static inline void mvpp2_qvec_interrupt_disable(struct mvpp2_queue_vector *qvec)
4270{
4271 struct mvpp2_port *port = qvec->port;
4272
4273 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
4274 MVPP2_ISR_DISABLE_INTERRUPT(qvec->sw_thread_mask));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004275}
4276
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004277/* Mask the current CPU's Rx/Tx interrupts
4278 * Called by on_each_cpu(), guaranteed to run with migration disabled,
4279 * using smp_processor_id() is OK.
4280 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004281static void mvpp2_interrupts_mask(void *arg)
4282{
4283 struct mvpp2_port *port = arg;
4284
Thomas Petazzonia7868412017-03-07 16:53:13 +01004285 mvpp2_percpu_write(port->priv, smp_processor_id(),
4286 MVPP2_ISR_RX_TX_MASK_REG(port->id), 0);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004287}
4288
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004289/* Unmask the current CPU's Rx/Tx interrupts.
4290 * Called by on_each_cpu(), guaranteed to run with migration disabled,
4291 * using smp_processor_id() is OK.
4292 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004293static void mvpp2_interrupts_unmask(void *arg)
4294{
4295 struct mvpp2_port *port = arg;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02004296 u32 val;
4297
4298 val = MVPP2_CAUSE_MISC_SUM_MASK |
4299 MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
4300 if (port->has_tx_irqs)
4301 val |= MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004302
Thomas Petazzonia7868412017-03-07 16:53:13 +01004303 mvpp2_percpu_write(port->priv, smp_processor_id(),
Thomas Petazzoni213f4282017-08-03 10:42:00 +02004304 MVPP2_ISR_RX_TX_MASK_REG(port->id), val);
4305}
4306
4307static void
4308mvpp2_shared_interrupt_mask_unmask(struct mvpp2_port *port, bool mask)
4309{
4310 u32 val;
4311 int i;
4312
4313 if (port->priv->hw_version != MVPP22)
4314 return;
4315
4316 if (mask)
4317 val = 0;
4318 else
4319 val = MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
4320
4321 for (i = 0; i < port->nqvecs; i++) {
4322 struct mvpp2_queue_vector *v = port->qvecs + i;
4323
4324 if (v->type != MVPP2_QUEUE_VECTOR_SHARED)
4325 continue;
4326
4327 mvpp2_percpu_write(port->priv, v->sw_thread_id,
4328 MVPP2_ISR_RX_TX_MASK_REG(port->id), val);
4329 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004330}
4331
4332/* Port configuration routines */
4333
Antoine Ténartf84bf382017-08-22 19:08:27 +02004334static void mvpp22_gop_init_rgmii(struct mvpp2_port *port)
4335{
4336 struct mvpp2 *priv = port->priv;
4337 u32 val;
4338
4339 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
4340 val |= GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT;
4341 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
4342
4343 regmap_read(priv->sysctrl_base, GENCONF_CTRL0, &val);
4344 if (port->gop_id == 2)
4345 val |= GENCONF_CTRL0_PORT0_RGMII | GENCONF_CTRL0_PORT1_RGMII;
4346 else if (port->gop_id == 3)
4347 val |= GENCONF_CTRL0_PORT1_RGMII_MII;
4348 regmap_write(priv->sysctrl_base, GENCONF_CTRL0, val);
4349}
4350
4351static void mvpp22_gop_init_sgmii(struct mvpp2_port *port)
4352{
4353 struct mvpp2 *priv = port->priv;
4354 u32 val;
4355
4356 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
4357 val |= GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT |
4358 GENCONF_PORT_CTRL0_RX_DATA_SAMPLE;
4359 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
4360
4361 if (port->gop_id > 1) {
4362 regmap_read(priv->sysctrl_base, GENCONF_CTRL0, &val);
4363 if (port->gop_id == 2)
4364 val &= ~GENCONF_CTRL0_PORT0_RGMII;
4365 else if (port->gop_id == 3)
4366 val &= ~GENCONF_CTRL0_PORT1_RGMII_MII;
4367 regmap_write(priv->sysctrl_base, GENCONF_CTRL0, val);
4368 }
4369}
4370
4371static void mvpp22_gop_init_10gkr(struct mvpp2_port *port)
4372{
4373 struct mvpp2 *priv = port->priv;
4374 void __iomem *mpcs = priv->iface_base + MVPP22_MPCS_BASE(port->gop_id);
4375 void __iomem *xpcs = priv->iface_base + MVPP22_XPCS_BASE(port->gop_id);
4376 u32 val;
4377
4378 /* XPCS */
4379 val = readl(xpcs + MVPP22_XPCS_CFG0);
4380 val &= ~(MVPP22_XPCS_CFG0_PCS_MODE(0x3) |
4381 MVPP22_XPCS_CFG0_ACTIVE_LANE(0x3));
4382 val |= MVPP22_XPCS_CFG0_ACTIVE_LANE(2);
4383 writel(val, xpcs + MVPP22_XPCS_CFG0);
4384
4385 /* MPCS */
4386 val = readl(mpcs + MVPP22_MPCS_CTRL);
4387 val &= ~MVPP22_MPCS_CTRL_FWD_ERR_CONN;
4388 writel(val, mpcs + MVPP22_MPCS_CTRL);
4389
4390 val = readl(mpcs + MVPP22_MPCS_CLK_RESET);
4391 val &= ~(MVPP22_MPCS_CLK_RESET_DIV_RATIO(0x7) | MAC_CLK_RESET_MAC |
4392 MAC_CLK_RESET_SD_RX | MAC_CLK_RESET_SD_TX);
4393 val |= MVPP22_MPCS_CLK_RESET_DIV_RATIO(1);
4394 writel(val, mpcs + MVPP22_MPCS_CLK_RESET);
4395
4396 val &= ~MVPP22_MPCS_CLK_RESET_DIV_SET;
4397 val |= MAC_CLK_RESET_MAC | MAC_CLK_RESET_SD_RX | MAC_CLK_RESET_SD_TX;
4398 writel(val, mpcs + MVPP22_MPCS_CLK_RESET);
4399}
4400
4401static int mvpp22_gop_init(struct mvpp2_port *port)
4402{
4403 struct mvpp2 *priv = port->priv;
4404 u32 val;
4405
4406 if (!priv->sysctrl_base)
4407 return 0;
4408
4409 switch (port->phy_interface) {
4410 case PHY_INTERFACE_MODE_RGMII:
4411 case PHY_INTERFACE_MODE_RGMII_ID:
4412 case PHY_INTERFACE_MODE_RGMII_RXID:
4413 case PHY_INTERFACE_MODE_RGMII_TXID:
4414 if (port->gop_id == 0)
4415 goto invalid_conf;
4416 mvpp22_gop_init_rgmii(port);
4417 break;
4418 case PHY_INTERFACE_MODE_SGMII:
4419 mvpp22_gop_init_sgmii(port);
4420 break;
4421 case PHY_INTERFACE_MODE_10GKR:
4422 if (port->gop_id != 0)
4423 goto invalid_conf;
4424 mvpp22_gop_init_10gkr(port);
4425 break;
4426 default:
4427 goto unsupported_conf;
4428 }
4429
4430 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL1, &val);
4431 val |= GENCONF_PORT_CTRL1_RESET(port->gop_id) |
4432 GENCONF_PORT_CTRL1_EN(port->gop_id);
4433 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL1, val);
4434
4435 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
4436 val |= GENCONF_PORT_CTRL0_CLK_DIV_PHASE_CLR;
4437 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
4438
4439 regmap_read(priv->sysctrl_base, GENCONF_SOFT_RESET1, &val);
4440 val |= GENCONF_SOFT_RESET1_GOP;
4441 regmap_write(priv->sysctrl_base, GENCONF_SOFT_RESET1, val);
4442
4443unsupported_conf:
4444 return 0;
4445
4446invalid_conf:
4447 netdev_err(port->dev, "Invalid port configuration\n");
4448 return -EINVAL;
4449}
4450
Antoine Tenartfd3651b2017-09-01 11:04:54 +02004451static void mvpp22_gop_unmask_irq(struct mvpp2_port *port)
4452{
4453 u32 val;
4454
4455 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
4456 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4457 /* Enable the GMAC link status irq for this port */
4458 val = readl(port->base + MVPP22_GMAC_INT_SUM_MASK);
4459 val |= MVPP22_GMAC_INT_SUM_MASK_LINK_STAT;
4460 writel(val, port->base + MVPP22_GMAC_INT_SUM_MASK);
4461 }
4462
4463 if (port->gop_id == 0) {
4464 /* Enable the XLG/GIG irqs for this port */
4465 val = readl(port->base + MVPP22_XLG_EXT_INT_MASK);
4466 if (port->phy_interface == PHY_INTERFACE_MODE_10GKR)
4467 val |= MVPP22_XLG_EXT_INT_MASK_XLG;
4468 else
4469 val |= MVPP22_XLG_EXT_INT_MASK_GIG;
4470 writel(val, port->base + MVPP22_XLG_EXT_INT_MASK);
4471 }
4472}
4473
4474static void mvpp22_gop_mask_irq(struct mvpp2_port *port)
4475{
4476 u32 val;
4477
4478 if (port->gop_id == 0) {
4479 val = readl(port->base + MVPP22_XLG_EXT_INT_MASK);
4480 val &= ~(MVPP22_XLG_EXT_INT_MASK_XLG |
4481 MVPP22_XLG_EXT_INT_MASK_GIG);
4482 writel(val, port->base + MVPP22_XLG_EXT_INT_MASK);
4483 }
4484
4485 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
4486 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4487 val = readl(port->base + MVPP22_GMAC_INT_SUM_MASK);
4488 val &= ~MVPP22_GMAC_INT_SUM_MASK_LINK_STAT;
4489 writel(val, port->base + MVPP22_GMAC_INT_SUM_MASK);
4490 }
4491}
4492
4493static void mvpp22_gop_setup_irq(struct mvpp2_port *port)
4494{
4495 u32 val;
4496
4497 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
4498 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4499 val = readl(port->base + MVPP22_GMAC_INT_MASK);
4500 val |= MVPP22_GMAC_INT_MASK_LINK_STAT;
4501 writel(val, port->base + MVPP22_GMAC_INT_MASK);
4502 }
4503
4504 if (port->gop_id == 0) {
4505 val = readl(port->base + MVPP22_XLG_INT_MASK);
4506 val |= MVPP22_XLG_INT_MASK_LINK;
4507 writel(val, port->base + MVPP22_XLG_INT_MASK);
4508 }
4509
4510 mvpp22_gop_unmask_irq(port);
4511}
4512
Antoine Tenart542897d2017-08-30 10:29:15 +02004513static int mvpp22_comphy_init(struct mvpp2_port *port)
4514{
4515 enum phy_mode mode;
4516 int ret;
4517
4518 if (!port->comphy)
4519 return 0;
4520
4521 switch (port->phy_interface) {
4522 case PHY_INTERFACE_MODE_SGMII:
4523 mode = PHY_MODE_SGMII;
4524 break;
4525 case PHY_INTERFACE_MODE_10GKR:
4526 mode = PHY_MODE_10GKR;
4527 break;
4528 default:
4529 return -EINVAL;
4530 }
4531
4532 ret = phy_set_mode(port->comphy, mode);
4533 if (ret)
4534 return ret;
4535
4536 return phy_power_on(port->comphy);
4537}
4538
Antoine Ténart39193572017-08-22 19:08:24 +02004539static void mvpp2_port_mii_gmac_configure_mode(struct mvpp2_port *port)
4540{
4541 u32 val;
4542
4543 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4544 val = readl(port->base + MVPP22_GMAC_CTRL_4_REG);
4545 val |= MVPP22_CTRL4_SYNC_BYPASS_DIS | MVPP22_CTRL4_DP_CLK_SEL |
4546 MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
4547 val &= ~MVPP22_CTRL4_EXT_PIN_GMII_SEL;
4548 writel(val, port->base + MVPP22_GMAC_CTRL_4_REG);
4549
4550 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4551 val |= MVPP2_GMAC_DISABLE_PADDING;
4552 val &= ~MVPP2_GMAC_FLOW_CTRL_MASK;
4553 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
Antoine Tenart1df22702017-09-01 11:04:52 +02004554 } else if (phy_interface_mode_is_rgmii(port->phy_interface)) {
Antoine Ténart39193572017-08-22 19:08:24 +02004555 val = readl(port->base + MVPP22_GMAC_CTRL_4_REG);
4556 val |= MVPP22_CTRL4_EXT_PIN_GMII_SEL |
4557 MVPP22_CTRL4_SYNC_BYPASS_DIS |
4558 MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
4559 val &= ~MVPP22_CTRL4_DP_CLK_SEL;
4560 writel(val, port->base + MVPP22_GMAC_CTRL_4_REG);
4561
4562 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4563 val &= ~MVPP2_GMAC_DISABLE_PADDING;
4564 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4565 }
4566
4567 /* The port is connected to a copper PHY */
4568 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4569 val &= ~MVPP2_GMAC_PORT_TYPE_MASK;
4570 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4571
4572 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4573 val |= MVPP2_GMAC_IN_BAND_AUTONEG_BYPASS |
4574 MVPP2_GMAC_AN_SPEED_EN | MVPP2_GMAC_FLOW_CTRL_AUTONEG |
4575 MVPP2_GMAC_AN_DUPLEX_EN;
4576 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
4577 val |= MVPP2_GMAC_IN_BAND_AUTONEG;
4578 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4579}
4580
4581static void mvpp2_port_mii_gmac_configure(struct mvpp2_port *port)
4582{
4583 u32 val;
4584
4585 /* Force link down */
4586 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4587 val &= ~MVPP2_GMAC_FORCE_LINK_PASS;
4588 val |= MVPP2_GMAC_FORCE_LINK_DOWN;
4589 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4590
4591 /* Set the GMAC in a reset state */
4592 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4593 val |= MVPP2_GMAC_PORT_RESET_MASK;
4594 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4595
4596 /* Configure the PCS and in-band AN */
4597 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4598 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4599 val |= MVPP2_GMAC_INBAND_AN_MASK | MVPP2_GMAC_PCS_ENABLE_MASK;
Antoine Tenart1df22702017-09-01 11:04:52 +02004600 } else if (phy_interface_mode_is_rgmii(port->phy_interface)) {
Antoine Ténart39193572017-08-22 19:08:24 +02004601 val &= ~MVPP2_GMAC_PCS_ENABLE_MASK;
Antoine Ténart39193572017-08-22 19:08:24 +02004602 }
4603 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4604
4605 mvpp2_port_mii_gmac_configure_mode(port);
4606
4607 /* Unset the GMAC reset state */
4608 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4609 val &= ~MVPP2_GMAC_PORT_RESET_MASK;
4610 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4611
4612 /* Stop forcing link down */
4613 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4614 val &= ~MVPP2_GMAC_FORCE_LINK_DOWN;
4615 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4616}
4617
Antoine Ténart77321952017-08-22 19:08:25 +02004618static void mvpp2_port_mii_xlg_configure(struct mvpp2_port *port)
4619{
4620 u32 val;
4621
4622 if (port->gop_id != 0)
4623 return;
4624
4625 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
4626 val |= MVPP22_XLG_CTRL0_RX_FLOW_CTRL_EN;
4627 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
4628
4629 val = readl(port->base + MVPP22_XLG_CTRL4_REG);
4630 val &= ~MVPP22_XLG_CTRL4_MACMODSELECT_GMAC;
4631 val |= MVPP22_XLG_CTRL4_FWD_FC | MVPP22_XLG_CTRL4_FWD_PFC;
4632 writel(val, port->base + MVPP22_XLG_CTRL4_REG);
4633}
4634
Thomas Petazzoni26975822017-03-07 16:53:14 +01004635static void mvpp22_port_mii_set(struct mvpp2_port *port)
4636{
4637 u32 val;
4638
Thomas Petazzoni26975822017-03-07 16:53:14 +01004639 /* Only GOP port 0 has an XLG MAC */
4640 if (port->gop_id == 0) {
4641 val = readl(port->base + MVPP22_XLG_CTRL3_REG);
4642 val &= ~MVPP22_XLG_CTRL3_MACMODESELECT_MASK;
Antoine Ténart725757a2017-06-12 16:01:39 +02004643
4644 if (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
4645 port->phy_interface == PHY_INTERFACE_MODE_10GKR)
4646 val |= MVPP22_XLG_CTRL3_MACMODESELECT_10G;
4647 else
4648 val |= MVPP22_XLG_CTRL3_MACMODESELECT_GMAC;
4649
Thomas Petazzoni26975822017-03-07 16:53:14 +01004650 writel(val, port->base + MVPP22_XLG_CTRL3_REG);
4651 }
Thomas Petazzoni26975822017-03-07 16:53:14 +01004652}
4653
Marcin Wojtas3f518502014-07-10 16:52:13 -03004654static void mvpp2_port_mii_set(struct mvpp2_port *port)
4655{
Thomas Petazzoni26975822017-03-07 16:53:14 +01004656 if (port->priv->hw_version == MVPP22)
4657 mvpp22_port_mii_set(port);
4658
Antoine Tenart1df22702017-09-01 11:04:52 +02004659 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
Antoine Ténart39193572017-08-22 19:08:24 +02004660 port->phy_interface == PHY_INTERFACE_MODE_SGMII)
4661 mvpp2_port_mii_gmac_configure(port);
Antoine Ténart77321952017-08-22 19:08:25 +02004662 else if (port->phy_interface == PHY_INTERFACE_MODE_10GKR)
4663 mvpp2_port_mii_xlg_configure(port);
Marcin Wojtas08a23752014-07-21 13:48:12 -03004664}
4665
4666static void mvpp2_port_fc_adv_enable(struct mvpp2_port *port)
4667{
4668 u32 val;
4669
4670 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4671 val |= MVPP2_GMAC_FC_ADV_EN;
4672 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004673}
4674
4675static void mvpp2_port_enable(struct mvpp2_port *port)
4676{
4677 u32 val;
4678
Antoine Ténart725757a2017-06-12 16:01:39 +02004679 /* Only GOP port 0 has an XLG MAC */
4680 if (port->gop_id == 0 &&
4681 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
4682 port->phy_interface == PHY_INTERFACE_MODE_10GKR)) {
4683 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
4684 val |= MVPP22_XLG_CTRL0_PORT_EN |
4685 MVPP22_XLG_CTRL0_MAC_RESET_DIS;
4686 val &= ~MVPP22_XLG_CTRL0_MIB_CNT_DIS;
4687 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
4688 } else {
4689 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4690 val |= MVPP2_GMAC_PORT_EN_MASK;
4691 val |= MVPP2_GMAC_MIB_CNTR_EN_MASK;
4692 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4693 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004694}
4695
4696static void mvpp2_port_disable(struct mvpp2_port *port)
4697{
4698 u32 val;
4699
Antoine Ténart725757a2017-06-12 16:01:39 +02004700 /* Only GOP port 0 has an XLG MAC */
4701 if (port->gop_id == 0 &&
4702 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
4703 port->phy_interface == PHY_INTERFACE_MODE_10GKR)) {
4704 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
4705 val &= ~(MVPP22_XLG_CTRL0_PORT_EN |
4706 MVPP22_XLG_CTRL0_MAC_RESET_DIS);
4707 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
4708 } else {
4709 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4710 val &= ~(MVPP2_GMAC_PORT_EN_MASK);
4711 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4712 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004713}
4714
4715/* Set IEEE 802.3x Flow Control Xon Packet Transmission Mode */
4716static void mvpp2_port_periodic_xon_disable(struct mvpp2_port *port)
4717{
4718 u32 val;
4719
4720 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG) &
4721 ~MVPP2_GMAC_PERIODIC_XON_EN_MASK;
4722 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
4723}
4724
4725/* Configure loopback port */
4726static void mvpp2_port_loopback_set(struct mvpp2_port *port)
4727{
4728 u32 val;
4729
4730 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
4731
4732 if (port->speed == 1000)
4733 val |= MVPP2_GMAC_GMII_LB_EN_MASK;
4734 else
4735 val &= ~MVPP2_GMAC_GMII_LB_EN_MASK;
4736
4737 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
4738 val |= MVPP2_GMAC_PCS_LB_EN_MASK;
4739 else
4740 val &= ~MVPP2_GMAC_PCS_LB_EN_MASK;
4741
4742 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
4743}
4744
4745static void mvpp2_port_reset(struct mvpp2_port *port)
4746{
4747 u32 val;
4748
4749 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
4750 ~MVPP2_GMAC_PORT_RESET_MASK;
4751 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4752
4753 while (readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
4754 MVPP2_GMAC_PORT_RESET_MASK)
4755 continue;
4756}
4757
4758/* Change maximum receive size of the port */
4759static inline void mvpp2_gmac_max_rx_size_set(struct mvpp2_port *port)
4760{
4761 u32 val;
4762
4763 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4764 val &= ~MVPP2_GMAC_MAX_RX_SIZE_MASK;
4765 val |= (((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
4766 MVPP2_GMAC_MAX_RX_SIZE_OFFS);
4767 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4768}
4769
Stefan Chulski76eb1b12017-08-22 19:08:26 +02004770/* Change maximum receive size of the port */
4771static inline void mvpp2_xlg_max_rx_size_set(struct mvpp2_port *port)
4772{
4773 u32 val;
4774
4775 val = readl(port->base + MVPP22_XLG_CTRL1_REG);
4776 val &= ~MVPP22_XLG_CTRL1_FRAMESIZELIMIT_MASK;
4777 val |= ((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
Antoine Ténartec15ecd2017-08-25 15:24:46 +02004778 MVPP22_XLG_CTRL1_FRAMESIZELIMIT_OFFS;
Stefan Chulski76eb1b12017-08-22 19:08:26 +02004779 writel(val, port->base + MVPP22_XLG_CTRL1_REG);
4780}
4781
Marcin Wojtas3f518502014-07-10 16:52:13 -03004782/* Set defaults to the MVPP2 port */
4783static void mvpp2_defaults_set(struct mvpp2_port *port)
4784{
4785 int tx_port_num, val, queue, ptxq, lrxq;
4786
Thomas Petazzoni3d9017d2017-03-07 16:53:11 +01004787 if (port->priv->hw_version == MVPP21) {
4788 /* Configure port to loopback if needed */
4789 if (port->flags & MVPP2_F_LOOPBACK)
4790 mvpp2_port_loopback_set(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004791
Thomas Petazzoni3d9017d2017-03-07 16:53:11 +01004792 /* Update TX FIFO MIN Threshold */
4793 val = readl(port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
4794 val &= ~MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK;
4795 /* Min. TX threshold must be less than minimal packet length */
4796 val |= MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(64 - 4 - 2);
4797 writel(val, port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
4798 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004799
4800 /* Disable Legacy WRR, Disable EJP, Release from reset */
4801 tx_port_num = mvpp2_egress_port(port);
4802 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG,
4803 tx_port_num);
4804 mvpp2_write(port->priv, MVPP2_TXP_SCHED_CMD_1_REG, 0);
4805
4806 /* Close bandwidth for all queues */
4807 for (queue = 0; queue < MVPP2_MAX_TXQ; queue++) {
4808 ptxq = mvpp2_txq_phys(port->id, queue);
4809 mvpp2_write(port->priv,
4810 MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(ptxq), 0);
4811 }
4812
4813 /* Set refill period to 1 usec, refill tokens
4814 * and bucket size to maximum
4815 */
4816 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PERIOD_REG,
4817 port->priv->tclk / USEC_PER_SEC);
4818 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_REFILL_REG);
4819 val &= ~MVPP2_TXP_REFILL_PERIOD_ALL_MASK;
4820 val |= MVPP2_TXP_REFILL_PERIOD_MASK(1);
4821 val |= MVPP2_TXP_REFILL_TOKENS_ALL_MASK;
4822 mvpp2_write(port->priv, MVPP2_TXP_SCHED_REFILL_REG, val);
4823 val = MVPP2_TXP_TOKEN_SIZE_MAX;
4824 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
4825
4826 /* Set MaximumLowLatencyPacketSize value to 256 */
4827 mvpp2_write(port->priv, MVPP2_RX_CTRL_REG(port->id),
4828 MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK |
4829 MVPP2_RX_LOW_LATENCY_PKT_SIZE(256));
4830
4831 /* Enable Rx cache snoop */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004832 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004833 queue = port->rxqs[lrxq]->id;
4834 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
4835 val |= MVPP2_SNOOP_PKT_SIZE_MASK |
4836 MVPP2_SNOOP_BUF_HDR_MASK;
4837 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
4838 }
4839
4840 /* At default, mask all interrupts to all present cpus */
4841 mvpp2_interrupts_disable(port);
4842}
4843
4844/* Enable/disable receiving packets */
4845static void mvpp2_ingress_enable(struct mvpp2_port *port)
4846{
4847 u32 val;
4848 int lrxq, queue;
4849
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004850 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004851 queue = port->rxqs[lrxq]->id;
4852 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
4853 val &= ~MVPP2_RXQ_DISABLE_MASK;
4854 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
4855 }
4856}
4857
4858static void mvpp2_ingress_disable(struct mvpp2_port *port)
4859{
4860 u32 val;
4861 int lrxq, queue;
4862
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004863 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004864 queue = port->rxqs[lrxq]->id;
4865 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
4866 val |= MVPP2_RXQ_DISABLE_MASK;
4867 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
4868 }
4869}
4870
4871/* Enable transmit via physical egress queue
4872 * - HW starts take descriptors from DRAM
4873 */
4874static void mvpp2_egress_enable(struct mvpp2_port *port)
4875{
4876 u32 qmap;
4877 int queue;
4878 int tx_port_num = mvpp2_egress_port(port);
4879
4880 /* Enable all initialized TXs. */
4881 qmap = 0;
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004882 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004883 struct mvpp2_tx_queue *txq = port->txqs[queue];
4884
Markus Elfringdbbb2f02017-04-17 14:07:52 +02004885 if (txq->descs)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004886 qmap |= (1 << queue);
4887 }
4888
4889 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
4890 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG, qmap);
4891}
4892
4893/* Disable transmit via physical egress queue
4894 * - HW doesn't take descriptors from DRAM
4895 */
4896static void mvpp2_egress_disable(struct mvpp2_port *port)
4897{
4898 u32 reg_data;
4899 int delay;
4900 int tx_port_num = mvpp2_egress_port(port);
4901
4902 /* Issue stop command for active channels only */
4903 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
4904 reg_data = (mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG)) &
4905 MVPP2_TXP_SCHED_ENQ_MASK;
4906 if (reg_data != 0)
4907 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG,
4908 (reg_data << MVPP2_TXP_SCHED_DISQ_OFFSET));
4909
4910 /* Wait for all Tx activity to terminate. */
4911 delay = 0;
4912 do {
4913 if (delay >= MVPP2_TX_DISABLE_TIMEOUT_MSEC) {
4914 netdev_warn(port->dev,
4915 "Tx stop timed out, status=0x%08x\n",
4916 reg_data);
4917 break;
4918 }
4919 mdelay(1);
4920 delay++;
4921
4922 /* Check port TX Command register that all
4923 * Tx queues are stopped
4924 */
4925 reg_data = mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG);
4926 } while (reg_data & MVPP2_TXP_SCHED_ENQ_MASK);
4927}
4928
4929/* Rx descriptors helper methods */
4930
4931/* Get number of Rx descriptors occupied by received packets */
4932static inline int
4933mvpp2_rxq_received(struct mvpp2_port *port, int rxq_id)
4934{
4935 u32 val = mvpp2_read(port->priv, MVPP2_RXQ_STATUS_REG(rxq_id));
4936
4937 return val & MVPP2_RXQ_OCCUPIED_MASK;
4938}
4939
4940/* Update Rx queue status with the number of occupied and available
4941 * Rx descriptor slots.
4942 */
4943static inline void
4944mvpp2_rxq_status_update(struct mvpp2_port *port, int rxq_id,
4945 int used_count, int free_count)
4946{
4947 /* Decrement the number of used descriptors and increment count
4948 * increment the number of free descriptors.
4949 */
4950 u32 val = used_count | (free_count << MVPP2_RXQ_NUM_NEW_OFFSET);
4951
4952 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_UPDATE_REG(rxq_id), val);
4953}
4954
4955/* Get pointer to next RX descriptor to be processed by SW */
4956static inline struct mvpp2_rx_desc *
4957mvpp2_rxq_next_desc_get(struct mvpp2_rx_queue *rxq)
4958{
4959 int rx_desc = rxq->next_desc_to_proc;
4960
4961 rxq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(rxq, rx_desc);
4962 prefetch(rxq->descs + rxq->next_desc_to_proc);
4963 return rxq->descs + rx_desc;
4964}
4965
4966/* Set rx queue offset */
4967static void mvpp2_rxq_offset_set(struct mvpp2_port *port,
4968 int prxq, int offset)
4969{
4970 u32 val;
4971
4972 /* Convert offset from bytes to units of 32 bytes */
4973 offset = offset >> 5;
4974
4975 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
4976 val &= ~MVPP2_RXQ_PACKET_OFFSET_MASK;
4977
4978 /* Offset is in */
4979 val |= ((offset << MVPP2_RXQ_PACKET_OFFSET_OFFS) &
4980 MVPP2_RXQ_PACKET_OFFSET_MASK);
4981
4982 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
4983}
4984
Marcin Wojtas3f518502014-07-10 16:52:13 -03004985/* Tx descriptors helper methods */
4986
Marcin Wojtas3f518502014-07-10 16:52:13 -03004987/* Get pointer to next Tx descriptor to be processed (send) by HW */
4988static struct mvpp2_tx_desc *
4989mvpp2_txq_next_desc_get(struct mvpp2_tx_queue *txq)
4990{
4991 int tx_desc = txq->next_desc_to_proc;
4992
4993 txq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(txq, tx_desc);
4994 return txq->descs + tx_desc;
4995}
4996
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004997/* Update HW with number of aggregated Tx descriptors to be sent
4998 *
4999 * Called only from mvpp2_tx(), so migration is disabled, using
5000 * smp_processor_id() is OK.
5001 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03005002static void mvpp2_aggr_txq_pend_desc_add(struct mvpp2_port *port, int pending)
5003{
5004 /* aggregated access - relevant TXQ number is written in TX desc */
Thomas Petazzonia7868412017-03-07 16:53:13 +01005005 mvpp2_percpu_write(port->priv, smp_processor_id(),
5006 MVPP2_AGGR_TXQ_UPDATE_REG, pending);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005007}
5008
5009
5010/* Check if there are enough free descriptors in aggregated txq.
5011 * If not, update the number of occupied descriptors and repeat the check.
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005012 *
5013 * Called only from mvpp2_tx(), so migration is disabled, using
5014 * smp_processor_id() is OK.
Marcin Wojtas3f518502014-07-10 16:52:13 -03005015 */
5016static int mvpp2_aggr_desc_num_check(struct mvpp2 *priv,
5017 struct mvpp2_tx_queue *aggr_txq, int num)
5018{
5019 if ((aggr_txq->count + num) > aggr_txq->size) {
5020 /* Update number of occupied aggregated Tx descriptors */
5021 int cpu = smp_processor_id();
5022 u32 val = mvpp2_read(priv, MVPP2_AGGR_TXQ_STATUS_REG(cpu));
5023
5024 aggr_txq->count = val & MVPP2_AGGR_TXQ_PENDING_MASK;
5025 }
5026
5027 if ((aggr_txq->count + num) > aggr_txq->size)
5028 return -ENOMEM;
5029
5030 return 0;
5031}
5032
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005033/* Reserved Tx descriptors allocation request
5034 *
5035 * Called only from mvpp2_txq_reserved_desc_num_proc(), itself called
5036 * only by mvpp2_tx(), so migration is disabled, using
5037 * smp_processor_id() is OK.
5038 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03005039static int mvpp2_txq_alloc_reserved_desc(struct mvpp2 *priv,
5040 struct mvpp2_tx_queue *txq, int num)
5041{
5042 u32 val;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005043 int cpu = smp_processor_id();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005044
5045 val = (txq->id << MVPP2_TXQ_RSVD_REQ_Q_OFFSET) | num;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005046 mvpp2_percpu_write(priv, cpu, MVPP2_TXQ_RSVD_REQ_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005047
Thomas Petazzonia7868412017-03-07 16:53:13 +01005048 val = mvpp2_percpu_read(priv, cpu, MVPP2_TXQ_RSVD_RSLT_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005049
5050 return val & MVPP2_TXQ_RSVD_RSLT_MASK;
5051}
5052
5053/* Check if there are enough reserved descriptors for transmission.
5054 * If not, request chunk of reserved descriptors and check again.
5055 */
5056static int mvpp2_txq_reserved_desc_num_proc(struct mvpp2 *priv,
5057 struct mvpp2_tx_queue *txq,
5058 struct mvpp2_txq_pcpu *txq_pcpu,
5059 int num)
5060{
5061 int req, cpu, desc_count;
5062
5063 if (txq_pcpu->reserved_num >= num)
5064 return 0;
5065
5066 /* Not enough descriptors reserved! Update the reserved descriptor
5067 * count and check again.
5068 */
5069
5070 desc_count = 0;
5071 /* Compute total of used descriptors */
5072 for_each_present_cpu(cpu) {
5073 struct mvpp2_txq_pcpu *txq_pcpu_aux;
5074
5075 txq_pcpu_aux = per_cpu_ptr(txq->pcpu, cpu);
5076 desc_count += txq_pcpu_aux->count;
5077 desc_count += txq_pcpu_aux->reserved_num;
5078 }
5079
5080 req = max(MVPP2_CPU_DESC_CHUNK, num - txq_pcpu->reserved_num);
5081 desc_count += req;
5082
5083 if (desc_count >
5084 (txq->size - (num_present_cpus() * MVPP2_CPU_DESC_CHUNK)))
5085 return -ENOMEM;
5086
5087 txq_pcpu->reserved_num += mvpp2_txq_alloc_reserved_desc(priv, txq, req);
5088
5089 /* OK, the descriptor cound has been updated: check again. */
5090 if (txq_pcpu->reserved_num < num)
5091 return -ENOMEM;
5092 return 0;
5093}
5094
5095/* Release the last allocated Tx descriptor. Useful to handle DMA
5096 * mapping failures in the Tx path.
5097 */
5098static void mvpp2_txq_desc_put(struct mvpp2_tx_queue *txq)
5099{
5100 if (txq->next_desc_to_proc == 0)
5101 txq->next_desc_to_proc = txq->last_desc - 1;
5102 else
5103 txq->next_desc_to_proc--;
5104}
5105
5106/* Set Tx descriptors fields relevant for CSUM calculation */
5107static u32 mvpp2_txq_desc_csum(int l3_offs, int l3_proto,
5108 int ip_hdr_len, int l4_proto)
5109{
5110 u32 command;
5111
5112 /* fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
5113 * G_L4_chk, L4_type required only for checksum calculation
5114 */
5115 command = (l3_offs << MVPP2_TXD_L3_OFF_SHIFT);
5116 command |= (ip_hdr_len << MVPP2_TXD_IP_HLEN_SHIFT);
5117 command |= MVPP2_TXD_IP_CSUM_DISABLE;
5118
5119 if (l3_proto == swab16(ETH_P_IP)) {
5120 command &= ~MVPP2_TXD_IP_CSUM_DISABLE; /* enable IPv4 csum */
5121 command &= ~MVPP2_TXD_L3_IP6; /* enable IPv4 */
5122 } else {
5123 command |= MVPP2_TXD_L3_IP6; /* enable IPv6 */
5124 }
5125
5126 if (l4_proto == IPPROTO_TCP) {
5127 command &= ~MVPP2_TXD_L4_UDP; /* enable TCP */
5128 command &= ~MVPP2_TXD_L4_CSUM_FRAG; /* generate L4 csum */
5129 } else if (l4_proto == IPPROTO_UDP) {
5130 command |= MVPP2_TXD_L4_UDP; /* enable UDP */
5131 command &= ~MVPP2_TXD_L4_CSUM_FRAG; /* generate L4 csum */
5132 } else {
5133 command |= MVPP2_TXD_L4_CSUM_NOT;
5134 }
5135
5136 return command;
5137}
5138
5139/* Get number of sent descriptors and decrement counter.
5140 * The number of sent descriptors is returned.
5141 * Per-CPU access
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005142 *
5143 * Called only from mvpp2_txq_done(), called from mvpp2_tx()
5144 * (migration disabled) and from the TX completion tasklet (migration
5145 * disabled) so using smp_processor_id() is OK.
Marcin Wojtas3f518502014-07-10 16:52:13 -03005146 */
5147static inline int mvpp2_txq_sent_desc_proc(struct mvpp2_port *port,
5148 struct mvpp2_tx_queue *txq)
5149{
5150 u32 val;
5151
5152 /* Reading status reg resets transmitted descriptor counter */
Thomas Petazzonia7868412017-03-07 16:53:13 +01005153 val = mvpp2_percpu_read(port->priv, smp_processor_id(),
5154 MVPP2_TXQ_SENT_REG(txq->id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005155
5156 return (val & MVPP2_TRANSMITTED_COUNT_MASK) >>
5157 MVPP2_TRANSMITTED_COUNT_OFFSET;
5158}
5159
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005160/* Called through on_each_cpu(), so runs on all CPUs, with migration
5161 * disabled, therefore using smp_processor_id() is OK.
5162 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03005163static void mvpp2_txq_sent_counter_clear(void *arg)
5164{
5165 struct mvpp2_port *port = arg;
5166 int queue;
5167
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005168 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005169 int id = port->txqs[queue]->id;
5170
Thomas Petazzonia7868412017-03-07 16:53:13 +01005171 mvpp2_percpu_read(port->priv, smp_processor_id(),
5172 MVPP2_TXQ_SENT_REG(id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005173 }
5174}
5175
5176/* Set max sizes for Tx queues */
5177static void mvpp2_txp_max_tx_size_set(struct mvpp2_port *port)
5178{
5179 u32 val, size, mtu;
5180 int txq, tx_port_num;
5181
5182 mtu = port->pkt_size * 8;
5183 if (mtu > MVPP2_TXP_MTU_MAX)
5184 mtu = MVPP2_TXP_MTU_MAX;
5185
5186 /* WA for wrong Token bucket update: Set MTU value = 3*real MTU value */
5187 mtu = 3 * mtu;
5188
5189 /* Indirect access to registers */
5190 tx_port_num = mvpp2_egress_port(port);
5191 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
5192
5193 /* Set MTU */
5194 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_MTU_REG);
5195 val &= ~MVPP2_TXP_MTU_MAX;
5196 val |= mtu;
5197 mvpp2_write(port->priv, MVPP2_TXP_SCHED_MTU_REG, val);
5198
5199 /* TXP token size and all TXQs token size must be larger that MTU */
5200 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG);
5201 size = val & MVPP2_TXP_TOKEN_SIZE_MAX;
5202 if (size < mtu) {
5203 size = mtu;
5204 val &= ~MVPP2_TXP_TOKEN_SIZE_MAX;
5205 val |= size;
5206 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
5207 }
5208
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005209 for (txq = 0; txq < port->ntxqs; txq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005210 val = mvpp2_read(port->priv,
5211 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq));
5212 size = val & MVPP2_TXQ_TOKEN_SIZE_MAX;
5213
5214 if (size < mtu) {
5215 size = mtu;
5216 val &= ~MVPP2_TXQ_TOKEN_SIZE_MAX;
5217 val |= size;
5218 mvpp2_write(port->priv,
5219 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq),
5220 val);
5221 }
5222 }
5223}
5224
5225/* Set the number of packets that will be received before Rx interrupt
5226 * will be generated by HW.
5227 */
5228static void mvpp2_rx_pkts_coal_set(struct mvpp2_port *port,
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01005229 struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005230{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005231 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005232
Thomas Petazzonif8b0d5f2017-02-21 11:28:03 +01005233 if (rxq->pkts_coal > MVPP2_OCCUPIED_THRESH_MASK)
5234 rxq->pkts_coal = MVPP2_OCCUPIED_THRESH_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005235
Thomas Petazzonia7868412017-03-07 16:53:13 +01005236 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
5237 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_THRESH_REG,
5238 rxq->pkts_coal);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005239
5240 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005241}
5242
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005243/* For some reason in the LSP this is done on each CPU. Why ? */
5244static void mvpp2_tx_pkts_coal_set(struct mvpp2_port *port,
5245 struct mvpp2_tx_queue *txq)
5246{
5247 int cpu = get_cpu();
5248 u32 val;
5249
5250 if (txq->done_pkts_coal > MVPP2_TXQ_THRESH_MASK)
5251 txq->done_pkts_coal = MVPP2_TXQ_THRESH_MASK;
5252
5253 val = (txq->done_pkts_coal << MVPP2_TXQ_THRESH_OFFSET);
5254 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5255 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_THRESH_REG, val);
5256
5257 put_cpu();
5258}
5259
Thomas Petazzoniab426762017-02-21 11:28:04 +01005260static u32 mvpp2_usec_to_cycles(u32 usec, unsigned long clk_hz)
5261{
5262 u64 tmp = (u64)clk_hz * usec;
5263
5264 do_div(tmp, USEC_PER_SEC);
5265
5266 return tmp > U32_MAX ? U32_MAX : tmp;
5267}
5268
5269static u32 mvpp2_cycles_to_usec(u32 cycles, unsigned long clk_hz)
5270{
5271 u64 tmp = (u64)cycles * USEC_PER_SEC;
5272
5273 do_div(tmp, clk_hz);
5274
5275 return tmp > U32_MAX ? U32_MAX : tmp;
5276}
5277
Marcin Wojtas3f518502014-07-10 16:52:13 -03005278/* Set the time delay in usec before Rx interrupt */
5279static void mvpp2_rx_time_coal_set(struct mvpp2_port *port,
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01005280 struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005281{
Thomas Petazzoniab426762017-02-21 11:28:04 +01005282 unsigned long freq = port->priv->tclk;
5283 u32 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005284
Thomas Petazzoniab426762017-02-21 11:28:04 +01005285 if (val > MVPP2_MAX_ISR_RX_THRESHOLD) {
5286 rxq->time_coal =
5287 mvpp2_cycles_to_usec(MVPP2_MAX_ISR_RX_THRESHOLD, freq);
5288
5289 /* re-evaluate to get actual register value */
5290 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
5291 }
5292
Marcin Wojtas3f518502014-07-10 16:52:13 -03005293 mvpp2_write(port->priv, MVPP2_ISR_RX_THRESHOLD_REG(rxq->id), val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005294}
5295
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005296static void mvpp2_tx_time_coal_set(struct mvpp2_port *port)
5297{
5298 unsigned long freq = port->priv->tclk;
5299 u32 val = mvpp2_usec_to_cycles(port->tx_time_coal, freq);
5300
5301 if (val > MVPP2_MAX_ISR_TX_THRESHOLD) {
5302 port->tx_time_coal =
5303 mvpp2_cycles_to_usec(MVPP2_MAX_ISR_TX_THRESHOLD, freq);
5304
5305 /* re-evaluate to get actual register value */
5306 val = mvpp2_usec_to_cycles(port->tx_time_coal, freq);
5307 }
5308
5309 mvpp2_write(port->priv, MVPP2_ISR_TX_THRESHOLD_REG(port->id), val);
5310}
5311
Marcin Wojtas3f518502014-07-10 16:52:13 -03005312/* Free Tx queue skbuffs */
5313static void mvpp2_txq_bufs_free(struct mvpp2_port *port,
5314 struct mvpp2_tx_queue *txq,
5315 struct mvpp2_txq_pcpu *txq_pcpu, int num)
5316{
5317 int i;
5318
5319 for (i = 0; i < num; i++) {
Thomas Petazzoni83544912016-12-21 11:28:49 +01005320 struct mvpp2_txq_pcpu_buf *tx_buf =
5321 txq_pcpu->buffs + txq_pcpu->txq_get_index;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005322
Thomas Petazzoni20396132017-03-07 16:53:00 +01005323 dma_unmap_single(port->dev->dev.parent, tx_buf->dma,
Thomas Petazzoni83544912016-12-21 11:28:49 +01005324 tx_buf->size, DMA_TO_DEVICE);
Thomas Petazzoni36fb7432017-02-21 11:28:05 +01005325 if (tx_buf->skb)
5326 dev_kfree_skb_any(tx_buf->skb);
5327
5328 mvpp2_txq_inc_get(txq_pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005329 }
5330}
5331
5332static inline struct mvpp2_rx_queue *mvpp2_get_rx_queue(struct mvpp2_port *port,
5333 u32 cause)
5334{
5335 int queue = fls(cause) - 1;
5336
5337 return port->rxqs[queue];
5338}
5339
5340static inline struct mvpp2_tx_queue *mvpp2_get_tx_queue(struct mvpp2_port *port,
5341 u32 cause)
5342{
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005343 int queue = fls(cause) - 1;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005344
5345 return port->txqs[queue];
5346}
5347
5348/* Handle end of transmission */
5349static void mvpp2_txq_done(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
5350 struct mvpp2_txq_pcpu *txq_pcpu)
5351{
5352 struct netdev_queue *nq = netdev_get_tx_queue(port->dev, txq->log_id);
5353 int tx_done;
5354
5355 if (txq_pcpu->cpu != smp_processor_id())
5356 netdev_err(port->dev, "wrong cpu on the end of Tx processing\n");
5357
5358 tx_done = mvpp2_txq_sent_desc_proc(port, txq);
5359 if (!tx_done)
5360 return;
5361 mvpp2_txq_bufs_free(port, txq, txq_pcpu, tx_done);
5362
5363 txq_pcpu->count -= tx_done;
5364
5365 if (netif_tx_queue_stopped(nq))
5366 if (txq_pcpu->size - txq_pcpu->count >= MAX_SKB_FRAGS + 1)
5367 netif_tx_wake_queue(nq);
5368}
5369
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005370static unsigned int mvpp2_tx_done(struct mvpp2_port *port, u32 cause,
5371 int cpu)
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005372{
5373 struct mvpp2_tx_queue *txq;
5374 struct mvpp2_txq_pcpu *txq_pcpu;
5375 unsigned int tx_todo = 0;
5376
5377 while (cause) {
5378 txq = mvpp2_get_tx_queue(port, cause);
5379 if (!txq)
5380 break;
5381
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005382 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005383
5384 if (txq_pcpu->count) {
5385 mvpp2_txq_done(port, txq, txq_pcpu);
5386 tx_todo += txq_pcpu->count;
5387 }
5388
5389 cause &= ~(1 << txq->log_id);
5390 }
5391 return tx_todo;
5392}
5393
Marcin Wojtas3f518502014-07-10 16:52:13 -03005394/* Rx/Tx queue initialization/cleanup methods */
5395
5396/* Allocate and initialize descriptors for aggr TXQ */
5397static int mvpp2_aggr_txq_init(struct platform_device *pdev,
Antoine Ténart85affd72017-08-23 09:46:55 +02005398 struct mvpp2_tx_queue *aggr_txq, int cpu,
Marcin Wojtas3f518502014-07-10 16:52:13 -03005399 struct mvpp2 *priv)
5400{
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005401 u32 txq_dma;
5402
Marcin Wojtas3f518502014-07-10 16:52:13 -03005403 /* Allocate memory for TX descriptors */
5404 aggr_txq->descs = dma_alloc_coherent(&pdev->dev,
Antoine Ténart85affd72017-08-23 09:46:55 +02005405 MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005406 &aggr_txq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005407 if (!aggr_txq->descs)
5408 return -ENOMEM;
5409
Marcin Wojtas3f518502014-07-10 16:52:13 -03005410 aggr_txq->last_desc = aggr_txq->size - 1;
5411
5412 /* Aggr TXQ no reset WA */
5413 aggr_txq->next_desc_to_proc = mvpp2_read(priv,
5414 MVPP2_AGGR_TXQ_INDEX_REG(cpu));
5415
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005416 /* Set Tx descriptors queue starting address indirect
5417 * access
5418 */
5419 if (priv->hw_version == MVPP21)
5420 txq_dma = aggr_txq->descs_dma;
5421 else
5422 txq_dma = aggr_txq->descs_dma >>
5423 MVPP22_AGGR_TXQ_DESC_ADDR_OFFS;
5424
5425 mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu), txq_dma);
Antoine Ténart85affd72017-08-23 09:46:55 +02005426 mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu),
5427 MVPP2_AGGR_TXQ_SIZE);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005428
5429 return 0;
5430}
5431
5432/* Create a specified Rx queue */
5433static int mvpp2_rxq_init(struct mvpp2_port *port,
5434 struct mvpp2_rx_queue *rxq)
5435
5436{
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005437 u32 rxq_dma;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005438 int cpu;
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005439
Marcin Wojtas3f518502014-07-10 16:52:13 -03005440 rxq->size = port->rx_ring_size;
5441
5442 /* Allocate memory for RX descriptors */
5443 rxq->descs = dma_alloc_coherent(port->dev->dev.parent,
5444 rxq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005445 &rxq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005446 if (!rxq->descs)
5447 return -ENOMEM;
5448
Marcin Wojtas3f518502014-07-10 16:52:13 -03005449 rxq->last_desc = rxq->size - 1;
5450
5451 /* Zero occupied and non-occupied counters - direct access */
5452 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
5453
5454 /* Set Rx descriptors queue starting address - indirect access */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005455 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005456 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005457 if (port->priv->hw_version == MVPP21)
5458 rxq_dma = rxq->descs_dma;
5459 else
5460 rxq_dma = rxq->descs_dma >> MVPP22_DESC_ADDR_OFFS;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005461 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_ADDR_REG, rxq_dma);
5462 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_SIZE_REG, rxq->size);
5463 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_INDEX_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005464 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005465
5466 /* Set Offset */
5467 mvpp2_rxq_offset_set(port, rxq->id, NET_SKB_PAD);
5468
5469 /* Set coalescing pkts and time */
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01005470 mvpp2_rx_pkts_coal_set(port, rxq);
5471 mvpp2_rx_time_coal_set(port, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005472
5473 /* Add number of descriptors ready for receiving packets */
5474 mvpp2_rxq_status_update(port, rxq->id, 0, rxq->size);
5475
5476 return 0;
5477}
5478
5479/* Push packets received by the RXQ to BM pool */
5480static void mvpp2_rxq_drop_pkts(struct mvpp2_port *port,
5481 struct mvpp2_rx_queue *rxq)
5482{
5483 int rx_received, i;
5484
5485 rx_received = mvpp2_rxq_received(port, rxq->id);
5486 if (!rx_received)
5487 return;
5488
5489 for (i = 0; i < rx_received; i++) {
5490 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005491 u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
5492 int pool;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005493
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005494 pool = (status & MVPP2_RXD_BM_POOL_ID_MASK) >>
5495 MVPP2_RXD_BM_POOL_ID_OFFS;
5496
Thomas Petazzoni7d7627b2017-06-22 14:23:20 +02005497 mvpp2_bm_pool_put(port, pool,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005498 mvpp2_rxdesc_dma_addr_get(port, rx_desc),
5499 mvpp2_rxdesc_cookie_get(port, rx_desc));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005500 }
5501 mvpp2_rxq_status_update(port, rxq->id, rx_received, rx_received);
5502}
5503
5504/* Cleanup Rx queue */
5505static void mvpp2_rxq_deinit(struct mvpp2_port *port,
5506 struct mvpp2_rx_queue *rxq)
5507{
Thomas Petazzonia7868412017-03-07 16:53:13 +01005508 int cpu;
5509
Marcin Wojtas3f518502014-07-10 16:52:13 -03005510 mvpp2_rxq_drop_pkts(port, rxq);
5511
5512 if (rxq->descs)
5513 dma_free_coherent(port->dev->dev.parent,
5514 rxq->size * MVPP2_DESC_ALIGNED_SIZE,
5515 rxq->descs,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005516 rxq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005517
5518 rxq->descs = NULL;
5519 rxq->last_desc = 0;
5520 rxq->next_desc_to_proc = 0;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005521 rxq->descs_dma = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005522
5523 /* Clear Rx descriptors queue starting address and size;
5524 * free descriptor number
5525 */
5526 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005527 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005528 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
5529 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_ADDR_REG, 0);
5530 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_SIZE_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005531 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005532}
5533
5534/* Create and initialize a Tx queue */
5535static int mvpp2_txq_init(struct mvpp2_port *port,
5536 struct mvpp2_tx_queue *txq)
5537{
5538 u32 val;
5539 int cpu, desc, desc_per_txq, tx_port_num;
5540 struct mvpp2_txq_pcpu *txq_pcpu;
5541
5542 txq->size = port->tx_ring_size;
5543
5544 /* Allocate memory for Tx descriptors */
5545 txq->descs = dma_alloc_coherent(port->dev->dev.parent,
5546 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005547 &txq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005548 if (!txq->descs)
5549 return -ENOMEM;
5550
Marcin Wojtas3f518502014-07-10 16:52:13 -03005551 txq->last_desc = txq->size - 1;
5552
5553 /* Set Tx descriptors queue starting address - indirect access */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005554 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005555 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5556 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_ADDR_REG,
5557 txq->descs_dma);
5558 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_SIZE_REG,
5559 txq->size & MVPP2_TXQ_DESC_SIZE_MASK);
5560 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_INDEX_REG, 0);
5561 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_RSVD_CLR_REG,
5562 txq->id << MVPP2_TXQ_RSVD_CLR_OFFSET);
5563 val = mvpp2_percpu_read(port->priv, cpu, MVPP2_TXQ_PENDING_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005564 val &= ~MVPP2_TXQ_PENDING_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005565 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PENDING_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005566
5567 /* Calculate base address in prefetch buffer. We reserve 16 descriptors
5568 * for each existing TXQ.
5569 * TCONTS for PON port must be continuous from 0 to MVPP2_MAX_TCONT
5570 * GBE ports assumed to be continious from 0 to MVPP2_MAX_PORTS
5571 */
5572 desc_per_txq = 16;
5573 desc = (port->id * MVPP2_MAX_TXQ * desc_per_txq) +
5574 (txq->log_id * desc_per_txq);
5575
Thomas Petazzonia7868412017-03-07 16:53:13 +01005576 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG,
5577 MVPP2_PREF_BUF_PTR(desc) | MVPP2_PREF_BUF_SIZE_16 |
5578 MVPP2_PREF_BUF_THRESH(desc_per_txq / 2));
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005579 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005580
5581 /* WRR / EJP configuration - indirect access */
5582 tx_port_num = mvpp2_egress_port(port);
5583 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
5584
5585 val = mvpp2_read(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id));
5586 val &= ~MVPP2_TXQ_REFILL_PERIOD_ALL_MASK;
5587 val |= MVPP2_TXQ_REFILL_PERIOD_MASK(1);
5588 val |= MVPP2_TXQ_REFILL_TOKENS_ALL_MASK;
5589 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id), val);
5590
5591 val = MVPP2_TXQ_TOKEN_SIZE_MAX;
5592 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq->log_id),
5593 val);
5594
5595 for_each_present_cpu(cpu) {
5596 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
5597 txq_pcpu->size = txq->size;
Markus Elfring02c91ec2017-04-17 08:09:07 +02005598 txq_pcpu->buffs = kmalloc_array(txq_pcpu->size,
5599 sizeof(*txq_pcpu->buffs),
5600 GFP_KERNEL);
Thomas Petazzoni83544912016-12-21 11:28:49 +01005601 if (!txq_pcpu->buffs)
Markus Elfring20b1e162017-04-17 12:58:33 +02005602 goto cleanup;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005603
5604 txq_pcpu->count = 0;
5605 txq_pcpu->reserved_num = 0;
5606 txq_pcpu->txq_put_index = 0;
5607 txq_pcpu->txq_get_index = 0;
Antoine Ténart186cd4d2017-08-23 09:46:56 +02005608
5609 txq_pcpu->tso_headers =
5610 dma_alloc_coherent(port->dev->dev.parent,
5611 MVPP2_AGGR_TXQ_SIZE * TSO_HEADER_SIZE,
5612 &txq_pcpu->tso_headers_dma,
5613 GFP_KERNEL);
5614 if (!txq_pcpu->tso_headers)
5615 goto cleanup;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005616 }
5617
5618 return 0;
Markus Elfring20b1e162017-04-17 12:58:33 +02005619cleanup:
Marcin Wojtas71ce3912015-08-06 19:00:29 +02005620 for_each_present_cpu(cpu) {
5621 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Thomas Petazzoni83544912016-12-21 11:28:49 +01005622 kfree(txq_pcpu->buffs);
Antoine Ténart186cd4d2017-08-23 09:46:56 +02005623
5624 dma_free_coherent(port->dev->dev.parent,
5625 MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
5626 txq_pcpu->tso_headers,
5627 txq_pcpu->tso_headers_dma);
Marcin Wojtas71ce3912015-08-06 19:00:29 +02005628 }
5629
5630 dma_free_coherent(port->dev->dev.parent,
5631 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005632 txq->descs, txq->descs_dma);
Marcin Wojtas71ce3912015-08-06 19:00:29 +02005633
5634 return -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005635}
5636
5637/* Free allocated TXQ resources */
5638static void mvpp2_txq_deinit(struct mvpp2_port *port,
5639 struct mvpp2_tx_queue *txq)
5640{
5641 struct mvpp2_txq_pcpu *txq_pcpu;
5642 int cpu;
5643
5644 for_each_present_cpu(cpu) {
5645 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Thomas Petazzoni83544912016-12-21 11:28:49 +01005646 kfree(txq_pcpu->buffs);
Antoine Ténart186cd4d2017-08-23 09:46:56 +02005647
5648 dma_free_coherent(port->dev->dev.parent,
5649 MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
5650 txq_pcpu->tso_headers,
5651 txq_pcpu->tso_headers_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005652 }
5653
5654 if (txq->descs)
5655 dma_free_coherent(port->dev->dev.parent,
5656 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005657 txq->descs, txq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005658
5659 txq->descs = NULL;
5660 txq->last_desc = 0;
5661 txq->next_desc_to_proc = 0;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005662 txq->descs_dma = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005663
5664 /* Set minimum bandwidth for disabled TXQs */
5665 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(txq->id), 0);
5666
5667 /* Set Tx descriptors queue starting address and size */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005668 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005669 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5670 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_ADDR_REG, 0);
5671 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_SIZE_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005672 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005673}
5674
5675/* Cleanup Tx ports */
5676static void mvpp2_txq_clean(struct mvpp2_port *port, struct mvpp2_tx_queue *txq)
5677{
5678 struct mvpp2_txq_pcpu *txq_pcpu;
5679 int delay, pending, cpu;
5680 u32 val;
5681
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005682 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005683 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5684 val = mvpp2_percpu_read(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005685 val |= MVPP2_TXQ_DRAIN_EN_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005686 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005687
5688 /* The napi queue has been stopped so wait for all packets
5689 * to be transmitted.
5690 */
5691 delay = 0;
5692 do {
5693 if (delay >= MVPP2_TX_PENDING_TIMEOUT_MSEC) {
5694 netdev_warn(port->dev,
5695 "port %d: cleaning queue %d timed out\n",
5696 port->id, txq->log_id);
5697 break;
5698 }
5699 mdelay(1);
5700 delay++;
5701
Thomas Petazzonia7868412017-03-07 16:53:13 +01005702 pending = mvpp2_percpu_read(port->priv, cpu,
5703 MVPP2_TXQ_PENDING_REG);
5704 pending &= MVPP2_TXQ_PENDING_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005705 } while (pending);
5706
5707 val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005708 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG, val);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005709 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005710
5711 for_each_present_cpu(cpu) {
5712 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
5713
5714 /* Release all packets */
5715 mvpp2_txq_bufs_free(port, txq, txq_pcpu, txq_pcpu->count);
5716
5717 /* Reset queue */
5718 txq_pcpu->count = 0;
5719 txq_pcpu->txq_put_index = 0;
5720 txq_pcpu->txq_get_index = 0;
5721 }
5722}
5723
5724/* Cleanup all Tx queues */
5725static void mvpp2_cleanup_txqs(struct mvpp2_port *port)
5726{
5727 struct mvpp2_tx_queue *txq;
5728 int queue;
5729 u32 val;
5730
5731 val = mvpp2_read(port->priv, MVPP2_TX_PORT_FLUSH_REG);
5732
5733 /* Reset Tx ports and delete Tx queues */
5734 val |= MVPP2_TX_PORT_FLUSH_MASK(port->id);
5735 mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
5736
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005737 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005738 txq = port->txqs[queue];
5739 mvpp2_txq_clean(port, txq);
5740 mvpp2_txq_deinit(port, txq);
5741 }
5742
5743 on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
5744
5745 val &= ~MVPP2_TX_PORT_FLUSH_MASK(port->id);
5746 mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
5747}
5748
5749/* Cleanup all Rx queues */
5750static void mvpp2_cleanup_rxqs(struct mvpp2_port *port)
5751{
5752 int queue;
5753
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005754 for (queue = 0; queue < port->nrxqs; queue++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005755 mvpp2_rxq_deinit(port, port->rxqs[queue]);
5756}
5757
5758/* Init all Rx queues for port */
5759static int mvpp2_setup_rxqs(struct mvpp2_port *port)
5760{
5761 int queue, err;
5762
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005763 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005764 err = mvpp2_rxq_init(port, port->rxqs[queue]);
5765 if (err)
5766 goto err_cleanup;
5767 }
5768 return 0;
5769
5770err_cleanup:
5771 mvpp2_cleanup_rxqs(port);
5772 return err;
5773}
5774
5775/* Init all tx queues for port */
5776static int mvpp2_setup_txqs(struct mvpp2_port *port)
5777{
5778 struct mvpp2_tx_queue *txq;
5779 int queue, err;
5780
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005781 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005782 txq = port->txqs[queue];
5783 err = mvpp2_txq_init(port, txq);
5784 if (err)
5785 goto err_cleanup;
5786 }
5787
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005788 if (port->has_tx_irqs) {
5789 mvpp2_tx_time_coal_set(port);
5790 for (queue = 0; queue < port->ntxqs; queue++) {
5791 txq = port->txqs[queue];
5792 mvpp2_tx_pkts_coal_set(port, txq);
5793 }
5794 }
5795
Marcin Wojtas3f518502014-07-10 16:52:13 -03005796 on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
5797 return 0;
5798
5799err_cleanup:
5800 mvpp2_cleanup_txqs(port);
5801 return err;
5802}
5803
5804/* The callback for per-port interrupt */
5805static irqreturn_t mvpp2_isr(int irq, void *dev_id)
5806{
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02005807 struct mvpp2_queue_vector *qv = dev_id;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005808
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02005809 mvpp2_qvec_interrupt_disable(qv);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005810
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02005811 napi_schedule(&qv->napi);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005812
5813 return IRQ_HANDLED;
5814}
5815
Antoine Tenartfd3651b2017-09-01 11:04:54 +02005816/* Per-port interrupt for link status changes */
5817static irqreturn_t mvpp2_link_status_isr(int irq, void *dev_id)
5818{
5819 struct mvpp2_port *port = (struct mvpp2_port *)dev_id;
5820 struct net_device *dev = port->dev;
5821 bool event = false, link = false;
5822 u32 val;
5823
5824 mvpp22_gop_mask_irq(port);
5825
5826 if (port->gop_id == 0 &&
5827 port->phy_interface == PHY_INTERFACE_MODE_10GKR) {
5828 val = readl(port->base + MVPP22_XLG_INT_STAT);
5829 if (val & MVPP22_XLG_INT_STAT_LINK) {
5830 event = true;
5831 val = readl(port->base + MVPP22_XLG_STATUS);
5832 if (val & MVPP22_XLG_STATUS_LINK_UP)
5833 link = true;
5834 }
5835 } else if (phy_interface_mode_is_rgmii(port->phy_interface) ||
5836 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
5837 val = readl(port->base + MVPP22_GMAC_INT_STAT);
5838 if (val & MVPP22_GMAC_INT_STAT_LINK) {
5839 event = true;
5840 val = readl(port->base + MVPP2_GMAC_STATUS0);
5841 if (val & MVPP2_GMAC_STATUS0_LINK_UP)
5842 link = true;
5843 }
5844 }
5845
5846 if (!netif_running(dev) || !event)
5847 goto handled;
5848
5849 if (link) {
5850 mvpp2_interrupts_enable(port);
5851
5852 mvpp2_egress_enable(port);
5853 mvpp2_ingress_enable(port);
5854 netif_carrier_on(dev);
5855 netif_tx_wake_all_queues(dev);
5856 } else {
5857 netif_tx_stop_all_queues(dev);
5858 netif_carrier_off(dev);
5859 mvpp2_ingress_disable(port);
5860 mvpp2_egress_disable(port);
5861
5862 mvpp2_interrupts_disable(port);
5863 }
5864
5865handled:
5866 mvpp22_gop_unmask_irq(port);
5867 return IRQ_HANDLED;
5868}
5869
Antoine Tenart65a2c092017-08-30 10:29:18 +02005870static void mvpp2_gmac_set_autoneg(struct mvpp2_port *port,
5871 struct phy_device *phydev)
5872{
5873 u32 val;
5874
5875 if (port->phy_interface != PHY_INTERFACE_MODE_RGMII &&
5876 port->phy_interface != PHY_INTERFACE_MODE_RGMII_ID &&
5877 port->phy_interface != PHY_INTERFACE_MODE_RGMII_RXID &&
5878 port->phy_interface != PHY_INTERFACE_MODE_RGMII_TXID &&
5879 port->phy_interface != PHY_INTERFACE_MODE_SGMII)
5880 return;
5881
5882 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5883 val &= ~(MVPP2_GMAC_CONFIG_MII_SPEED |
5884 MVPP2_GMAC_CONFIG_GMII_SPEED |
5885 MVPP2_GMAC_CONFIG_FULL_DUPLEX |
5886 MVPP2_GMAC_AN_SPEED_EN |
5887 MVPP2_GMAC_AN_DUPLEX_EN);
5888
5889 if (phydev->duplex)
5890 val |= MVPP2_GMAC_CONFIG_FULL_DUPLEX;
5891
5892 if (phydev->speed == SPEED_1000)
5893 val |= MVPP2_GMAC_CONFIG_GMII_SPEED;
5894 else if (phydev->speed == SPEED_100)
5895 val |= MVPP2_GMAC_CONFIG_MII_SPEED;
5896
5897 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
Antoine Tenart65a2c092017-08-30 10:29:18 +02005898}
5899
Marcin Wojtas3f518502014-07-10 16:52:13 -03005900/* Adjust link */
5901static void mvpp2_link_event(struct net_device *dev)
5902{
5903 struct mvpp2_port *port = netdev_priv(dev);
Philippe Reynes8e072692016-06-28 00:08:11 +02005904 struct phy_device *phydev = dev->phydev;
Antoine Tenart89273bc2017-08-30 10:29:19 +02005905 bool link_reconfigured = false;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005906 u32 val;
5907
5908 if (phydev->link) {
Antoine Tenart89273bc2017-08-30 10:29:19 +02005909 if (port->phy_interface != phydev->interface && port->comphy) {
5910 /* disable current port for reconfiguration */
5911 mvpp2_interrupts_disable(port);
5912 netif_carrier_off(port->dev);
5913 mvpp2_port_disable(port);
5914 phy_power_off(port->comphy);
5915
5916 /* comphy reconfiguration */
5917 port->phy_interface = phydev->interface;
5918 mvpp22_comphy_init(port);
5919
5920 /* gop/mac reconfiguration */
5921 mvpp22_gop_init(port);
5922 mvpp2_port_mii_set(port);
5923
5924 link_reconfigured = true;
5925 }
5926
Marcin Wojtas3f518502014-07-10 16:52:13 -03005927 if ((port->speed != phydev->speed) ||
5928 (port->duplex != phydev->duplex)) {
Antoine Tenart65a2c092017-08-30 10:29:18 +02005929 mvpp2_gmac_set_autoneg(port, phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005930
5931 port->duplex = phydev->duplex;
5932 port->speed = phydev->speed;
5933 }
5934 }
5935
Antoine Tenart89273bc2017-08-30 10:29:19 +02005936 if (phydev->link != port->link || link_reconfigured) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005937 port->link = phydev->link;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005938
Marcin Wojtas3f518502014-07-10 16:52:13 -03005939 if (phydev->link) {
Antoine Tenart65a2c092017-08-30 10:29:18 +02005940 if (port->phy_interface == PHY_INTERFACE_MODE_RGMII ||
5941 port->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
5942 port->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID ||
5943 port->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID ||
5944 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
5945 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5946 val |= (MVPP2_GMAC_FORCE_LINK_PASS |
5947 MVPP2_GMAC_FORCE_LINK_DOWN);
5948 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5949 }
Antoine Tenartf55744a2017-08-30 10:29:17 +02005950
5951 mvpp2_interrupts_enable(port);
5952 mvpp2_port_enable(port);
5953
Marcin Wojtas3f518502014-07-10 16:52:13 -03005954 mvpp2_egress_enable(port);
5955 mvpp2_ingress_enable(port);
Antoine Tenartf55744a2017-08-30 10:29:17 +02005956 netif_carrier_on(dev);
5957 netif_tx_wake_all_queues(dev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005958 } else {
Antoine Tenart968b2112017-08-30 10:29:16 +02005959 port->duplex = -1;
5960 port->speed = 0;
5961
Antoine Tenartf55744a2017-08-30 10:29:17 +02005962 netif_tx_stop_all_queues(dev);
5963 netif_carrier_off(dev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005964 mvpp2_ingress_disable(port);
5965 mvpp2_egress_disable(port);
Antoine Tenartf55744a2017-08-30 10:29:17 +02005966
5967 mvpp2_port_disable(port);
5968 mvpp2_interrupts_disable(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005969 }
Antoine Tenart968b2112017-08-30 10:29:16 +02005970
Marcin Wojtas3f518502014-07-10 16:52:13 -03005971 phy_print_status(phydev);
5972 }
5973}
5974
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005975static void mvpp2_timer_set(struct mvpp2_port_pcpu *port_pcpu)
5976{
5977 ktime_t interval;
5978
5979 if (!port_pcpu->timer_scheduled) {
5980 port_pcpu->timer_scheduled = true;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01005981 interval = MVPP2_TXDONE_HRTIMER_PERIOD_NS;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005982 hrtimer_start(&port_pcpu->tx_done_timer, interval,
5983 HRTIMER_MODE_REL_PINNED);
5984 }
5985}
5986
5987static void mvpp2_tx_proc_cb(unsigned long data)
5988{
5989 struct net_device *dev = (struct net_device *)data;
5990 struct mvpp2_port *port = netdev_priv(dev);
5991 struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
5992 unsigned int tx_todo, cause;
5993
5994 if (!netif_running(dev))
5995 return;
5996 port_pcpu->timer_scheduled = false;
5997
5998 /* Process all the Tx queues */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005999 cause = (1 << port->ntxqs) - 1;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006000 tx_todo = mvpp2_tx_done(port, cause, smp_processor_id());
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006001
6002 /* Set the timer in case not all the packets were processed */
6003 if (tx_todo)
6004 mvpp2_timer_set(port_pcpu);
6005}
6006
6007static enum hrtimer_restart mvpp2_hr_timer_cb(struct hrtimer *timer)
6008{
6009 struct mvpp2_port_pcpu *port_pcpu = container_of(timer,
6010 struct mvpp2_port_pcpu,
6011 tx_done_timer);
6012
6013 tasklet_schedule(&port_pcpu->tx_done_tasklet);
6014
6015 return HRTIMER_NORESTART;
6016}
6017
Marcin Wojtas3f518502014-07-10 16:52:13 -03006018/* Main RX/TX processing routines */
6019
6020/* Display more error info */
6021static void mvpp2_rx_error(struct mvpp2_port *port,
6022 struct mvpp2_rx_desc *rx_desc)
6023{
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006024 u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
6025 size_t sz = mvpp2_rxdesc_size_get(port, rx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006026
6027 switch (status & MVPP2_RXD_ERR_CODE_MASK) {
6028 case MVPP2_RXD_ERR_CRC:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006029 netdev_err(port->dev, "bad rx status %08x (crc error), size=%zu\n",
6030 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006031 break;
6032 case MVPP2_RXD_ERR_OVERRUN:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006033 netdev_err(port->dev, "bad rx status %08x (overrun error), size=%zu\n",
6034 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006035 break;
6036 case MVPP2_RXD_ERR_RESOURCE:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006037 netdev_err(port->dev, "bad rx status %08x (resource error), size=%zu\n",
6038 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006039 break;
6040 }
6041}
6042
6043/* Handle RX checksum offload */
6044static void mvpp2_rx_csum(struct mvpp2_port *port, u32 status,
6045 struct sk_buff *skb)
6046{
6047 if (((status & MVPP2_RXD_L3_IP4) &&
6048 !(status & MVPP2_RXD_IP4_HEADER_ERR)) ||
6049 (status & MVPP2_RXD_L3_IP6))
6050 if (((status & MVPP2_RXD_L4_UDP) ||
6051 (status & MVPP2_RXD_L4_TCP)) &&
6052 (status & MVPP2_RXD_L4_CSUM_OK)) {
6053 skb->csum = 0;
6054 skb->ip_summed = CHECKSUM_UNNECESSARY;
6055 return;
6056 }
6057
6058 skb->ip_summed = CHECKSUM_NONE;
6059}
6060
6061/* Reuse skb if possible, or allocate a new skb and add it to BM pool */
6062static int mvpp2_rx_refill(struct mvpp2_port *port,
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006063 struct mvpp2_bm_pool *bm_pool, int pool)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006064{
Thomas Petazzoni20396132017-03-07 16:53:00 +01006065 dma_addr_t dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01006066 phys_addr_t phys_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006067 void *buf;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006068
Marcin Wojtas3f518502014-07-10 16:52:13 -03006069 /* No recycle or too many buffers are in use, so allocate a new skb */
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01006070 buf = mvpp2_buf_alloc(port, bm_pool, &dma_addr, &phys_addr,
6071 GFP_ATOMIC);
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006072 if (!buf)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006073 return -ENOMEM;
6074
Thomas Petazzoni7d7627b2017-06-22 14:23:20 +02006075 mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
Thomas Petazzoni7ef7e1d2017-02-21 11:28:07 +01006076
Marcin Wojtas3f518502014-07-10 16:52:13 -03006077 return 0;
6078}
6079
6080/* Handle tx checksum */
6081static u32 mvpp2_skb_tx_csum(struct mvpp2_port *port, struct sk_buff *skb)
6082{
6083 if (skb->ip_summed == CHECKSUM_PARTIAL) {
6084 int ip_hdr_len = 0;
6085 u8 l4_proto;
6086
6087 if (skb->protocol == htons(ETH_P_IP)) {
6088 struct iphdr *ip4h = ip_hdr(skb);
6089
6090 /* Calculate IPv4 checksum and L4 checksum */
6091 ip_hdr_len = ip4h->ihl;
6092 l4_proto = ip4h->protocol;
6093 } else if (skb->protocol == htons(ETH_P_IPV6)) {
6094 struct ipv6hdr *ip6h = ipv6_hdr(skb);
6095
6096 /* Read l4_protocol from one of IPv6 extra headers */
6097 if (skb_network_header_len(skb) > 0)
6098 ip_hdr_len = (skb_network_header_len(skb) >> 2);
6099 l4_proto = ip6h->nexthdr;
6100 } else {
6101 return MVPP2_TXD_L4_CSUM_NOT;
6102 }
6103
6104 return mvpp2_txq_desc_csum(skb_network_offset(skb),
6105 skb->protocol, ip_hdr_len, l4_proto);
6106 }
6107
6108 return MVPP2_TXD_L4_CSUM_NOT | MVPP2_TXD_IP_CSUM_DISABLE;
6109}
6110
Marcin Wojtas3f518502014-07-10 16:52:13 -03006111/* Main rx processing */
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006112static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi,
6113 int rx_todo, struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006114{
6115 struct net_device *dev = port->dev;
Marcin Wojtasb5015852015-12-03 15:20:51 +01006116 int rx_received;
6117 int rx_done = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006118 u32 rcvd_pkts = 0;
6119 u32 rcvd_bytes = 0;
6120
6121 /* Get number of received packets and clamp the to-do */
6122 rx_received = mvpp2_rxq_received(port, rxq->id);
6123 if (rx_todo > rx_received)
6124 rx_todo = rx_received;
6125
Marcin Wojtasb5015852015-12-03 15:20:51 +01006126 while (rx_done < rx_todo) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006127 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
6128 struct mvpp2_bm_pool *bm_pool;
6129 struct sk_buff *skb;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006130 unsigned int frag_size;
Thomas Petazzoni20396132017-03-07 16:53:00 +01006131 dma_addr_t dma_addr;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006132 phys_addr_t phys_addr;
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006133 u32 rx_status;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006134 int pool, rx_bytes, err;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006135 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006136
Marcin Wojtasb5015852015-12-03 15:20:51 +01006137 rx_done++;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006138 rx_status = mvpp2_rxdesc_status_get(port, rx_desc);
6139 rx_bytes = mvpp2_rxdesc_size_get(port, rx_desc);
6140 rx_bytes -= MVPP2_MH_SIZE;
6141 dma_addr = mvpp2_rxdesc_dma_addr_get(port, rx_desc);
6142 phys_addr = mvpp2_rxdesc_cookie_get(port, rx_desc);
6143 data = (void *)phys_to_virt(phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006144
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006145 pool = (rx_status & MVPP2_RXD_BM_POOL_ID_MASK) >>
6146 MVPP2_RXD_BM_POOL_ID_OFFS;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006147 bm_pool = &port->priv->bm_pools[pool];
Marcin Wojtas3f518502014-07-10 16:52:13 -03006148
6149 /* In case of an error, release the requested buffer pointer
6150 * to the Buffer Manager. This request process is controlled
6151 * by the hardware, and the information about the buffer is
6152 * comprised by the RX descriptor.
6153 */
6154 if (rx_status & MVPP2_RXD_ERR_SUMMARY) {
Markus Elfring8a524882017-04-17 10:52:02 +02006155err_drop_frame:
Marcin Wojtas3f518502014-07-10 16:52:13 -03006156 dev->stats.rx_errors++;
6157 mvpp2_rx_error(port, rx_desc);
Marcin Wojtasb5015852015-12-03 15:20:51 +01006158 /* Return the buffer to the pool */
Thomas Petazzoni7d7627b2017-06-22 14:23:20 +02006159 mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006160 continue;
6161 }
6162
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006163 if (bm_pool->frag_size > PAGE_SIZE)
6164 frag_size = 0;
6165 else
6166 frag_size = bm_pool->frag_size;
6167
6168 skb = build_skb(data, frag_size);
6169 if (!skb) {
6170 netdev_warn(port->dev, "skb build failed\n");
6171 goto err_drop_frame;
6172 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006173
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006174 err = mvpp2_rx_refill(port, bm_pool, pool);
Marcin Wojtasb5015852015-12-03 15:20:51 +01006175 if (err) {
6176 netdev_err(port->dev, "failed to refill BM pools\n");
6177 goto err_drop_frame;
6178 }
6179
Thomas Petazzoni20396132017-03-07 16:53:00 +01006180 dma_unmap_single(dev->dev.parent, dma_addr,
Marcin Wojtas4229d502015-12-03 15:20:50 +01006181 bm_pool->buf_size, DMA_FROM_DEVICE);
6182
Marcin Wojtas3f518502014-07-10 16:52:13 -03006183 rcvd_pkts++;
6184 rcvd_bytes += rx_bytes;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006185
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006186 skb_reserve(skb, MVPP2_MH_SIZE + NET_SKB_PAD);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006187 skb_put(skb, rx_bytes);
6188 skb->protocol = eth_type_trans(skb, dev);
6189 mvpp2_rx_csum(port, rx_status, skb);
6190
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006191 napi_gro_receive(napi, skb);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006192 }
6193
6194 if (rcvd_pkts) {
6195 struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
6196
6197 u64_stats_update_begin(&stats->syncp);
6198 stats->rx_packets += rcvd_pkts;
6199 stats->rx_bytes += rcvd_bytes;
6200 u64_stats_update_end(&stats->syncp);
6201 }
6202
6203 /* Update Rx queue management counters */
6204 wmb();
Marcin Wojtasb5015852015-12-03 15:20:51 +01006205 mvpp2_rxq_status_update(port, rxq->id, rx_done, rx_done);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006206
6207 return rx_todo;
6208}
6209
6210static inline void
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006211tx_desc_unmap_put(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006212 struct mvpp2_tx_desc *desc)
6213{
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006214 dma_addr_t buf_dma_addr =
6215 mvpp2_txdesc_dma_addr_get(port, desc);
6216 size_t buf_sz =
6217 mvpp2_txdesc_size_get(port, desc);
6218 dma_unmap_single(port->dev->dev.parent, buf_dma_addr,
6219 buf_sz, DMA_TO_DEVICE);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006220 mvpp2_txq_desc_put(txq);
6221}
6222
6223/* Handle tx fragmentation processing */
6224static int mvpp2_tx_frag_process(struct mvpp2_port *port, struct sk_buff *skb,
6225 struct mvpp2_tx_queue *aggr_txq,
6226 struct mvpp2_tx_queue *txq)
6227{
6228 struct mvpp2_txq_pcpu *txq_pcpu = this_cpu_ptr(txq->pcpu);
6229 struct mvpp2_tx_desc *tx_desc;
6230 int i;
Thomas Petazzoni20396132017-03-07 16:53:00 +01006231 dma_addr_t buf_dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006232
6233 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
6234 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6235 void *addr = page_address(frag->page.p) + frag->page_offset;
6236
6237 tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006238 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6239 mvpp2_txdesc_size_set(port, tx_desc, frag->size);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006240
Thomas Petazzoni20396132017-03-07 16:53:00 +01006241 buf_dma_addr = dma_map_single(port->dev->dev.parent, addr,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006242 frag->size,
6243 DMA_TO_DEVICE);
Thomas Petazzoni20396132017-03-07 16:53:00 +01006244 if (dma_mapping_error(port->dev->dev.parent, buf_dma_addr)) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006245 mvpp2_txq_desc_put(txq);
Markus Elfring32bae632017-04-17 11:36:34 +02006246 goto cleanup;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006247 }
6248
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006249 mvpp2_txdesc_offset_set(port, tx_desc,
6250 buf_dma_addr & MVPP2_TX_DESC_ALIGN);
6251 mvpp2_txdesc_dma_addr_set(port, tx_desc,
6252 buf_dma_addr & ~MVPP2_TX_DESC_ALIGN);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006253
6254 if (i == (skb_shinfo(skb)->nr_frags - 1)) {
6255 /* Last descriptor */
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006256 mvpp2_txdesc_cmd_set(port, tx_desc,
6257 MVPP2_TXD_L_DESC);
6258 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006259 } else {
6260 /* Descriptor in the middle: Not First, Not Last */
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006261 mvpp2_txdesc_cmd_set(port, tx_desc, 0);
6262 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006263 }
6264 }
6265
6266 return 0;
Markus Elfring32bae632017-04-17 11:36:34 +02006267cleanup:
Marcin Wojtas3f518502014-07-10 16:52:13 -03006268 /* Release all descriptors that were used to map fragments of
6269 * this packet, as well as the corresponding DMA mappings
6270 */
6271 for (i = i - 1; i >= 0; i--) {
6272 tx_desc = txq->descs + i;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006273 tx_desc_unmap_put(port, txq, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006274 }
6275
6276 return -ENOMEM;
6277}
6278
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006279static inline void mvpp2_tso_put_hdr(struct sk_buff *skb,
6280 struct net_device *dev,
6281 struct mvpp2_tx_queue *txq,
6282 struct mvpp2_tx_queue *aggr_txq,
6283 struct mvpp2_txq_pcpu *txq_pcpu,
6284 int hdr_sz)
6285{
6286 struct mvpp2_port *port = netdev_priv(dev);
6287 struct mvpp2_tx_desc *tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
6288 dma_addr_t addr;
6289
6290 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6291 mvpp2_txdesc_size_set(port, tx_desc, hdr_sz);
6292
6293 addr = txq_pcpu->tso_headers_dma +
6294 txq_pcpu->txq_put_index * TSO_HEADER_SIZE;
6295 mvpp2_txdesc_offset_set(port, tx_desc, addr & MVPP2_TX_DESC_ALIGN);
6296 mvpp2_txdesc_dma_addr_set(port, tx_desc, addr & ~MVPP2_TX_DESC_ALIGN);
6297
6298 mvpp2_txdesc_cmd_set(port, tx_desc, mvpp2_skb_tx_csum(port, skb) |
6299 MVPP2_TXD_F_DESC |
6300 MVPP2_TXD_PADDING_DISABLE);
6301 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
6302}
6303
6304static inline int mvpp2_tso_put_data(struct sk_buff *skb,
6305 struct net_device *dev, struct tso_t *tso,
6306 struct mvpp2_tx_queue *txq,
6307 struct mvpp2_tx_queue *aggr_txq,
6308 struct mvpp2_txq_pcpu *txq_pcpu,
6309 int sz, bool left, bool last)
6310{
6311 struct mvpp2_port *port = netdev_priv(dev);
6312 struct mvpp2_tx_desc *tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
6313 dma_addr_t buf_dma_addr;
6314
6315 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6316 mvpp2_txdesc_size_set(port, tx_desc, sz);
6317
6318 buf_dma_addr = dma_map_single(dev->dev.parent, tso->data, sz,
6319 DMA_TO_DEVICE);
6320 if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
6321 mvpp2_txq_desc_put(txq);
6322 return -ENOMEM;
6323 }
6324
6325 mvpp2_txdesc_offset_set(port, tx_desc,
6326 buf_dma_addr & MVPP2_TX_DESC_ALIGN);
6327 mvpp2_txdesc_dma_addr_set(port, tx_desc,
6328 buf_dma_addr & ~MVPP2_TX_DESC_ALIGN);
6329
6330 if (!left) {
6331 mvpp2_txdesc_cmd_set(port, tx_desc, MVPP2_TXD_L_DESC);
6332 if (last) {
6333 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
6334 return 0;
6335 }
6336 } else {
6337 mvpp2_txdesc_cmd_set(port, tx_desc, 0);
6338 }
6339
6340 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
6341 return 0;
6342}
6343
6344static int mvpp2_tx_tso(struct sk_buff *skb, struct net_device *dev,
6345 struct mvpp2_tx_queue *txq,
6346 struct mvpp2_tx_queue *aggr_txq,
6347 struct mvpp2_txq_pcpu *txq_pcpu)
6348{
6349 struct mvpp2_port *port = netdev_priv(dev);
6350 struct tso_t tso;
6351 int hdr_sz = skb_transport_offset(skb) + tcp_hdrlen(skb);
6352 int i, len, descs = 0;
6353
6354 /* Check number of available descriptors */
6355 if (mvpp2_aggr_desc_num_check(port->priv, aggr_txq,
6356 tso_count_descs(skb)) ||
6357 mvpp2_txq_reserved_desc_num_proc(port->priv, txq, txq_pcpu,
6358 tso_count_descs(skb)))
6359 return 0;
6360
6361 tso_start(skb, &tso);
6362 len = skb->len - hdr_sz;
6363 while (len > 0) {
6364 int left = min_t(int, skb_shinfo(skb)->gso_size, len);
6365 char *hdr = txq_pcpu->tso_headers +
6366 txq_pcpu->txq_put_index * TSO_HEADER_SIZE;
6367
6368 len -= left;
6369 descs++;
6370
6371 tso_build_hdr(skb, hdr, &tso, left, len == 0);
6372 mvpp2_tso_put_hdr(skb, dev, txq, aggr_txq, txq_pcpu, hdr_sz);
6373
6374 while (left > 0) {
6375 int sz = min_t(int, tso.size, left);
6376 left -= sz;
6377 descs++;
6378
6379 if (mvpp2_tso_put_data(skb, dev, &tso, txq, aggr_txq,
6380 txq_pcpu, sz, left, len == 0))
6381 goto release;
6382 tso_build_data(skb, &tso, sz);
6383 }
6384 }
6385
6386 return descs;
6387
6388release:
6389 for (i = descs - 1; i >= 0; i--) {
6390 struct mvpp2_tx_desc *tx_desc = txq->descs + i;
6391 tx_desc_unmap_put(port, txq, tx_desc);
6392 }
6393 return 0;
6394}
6395
Marcin Wojtas3f518502014-07-10 16:52:13 -03006396/* Main tx processing */
6397static int mvpp2_tx(struct sk_buff *skb, struct net_device *dev)
6398{
6399 struct mvpp2_port *port = netdev_priv(dev);
6400 struct mvpp2_tx_queue *txq, *aggr_txq;
6401 struct mvpp2_txq_pcpu *txq_pcpu;
6402 struct mvpp2_tx_desc *tx_desc;
Thomas Petazzoni20396132017-03-07 16:53:00 +01006403 dma_addr_t buf_dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006404 int frags = 0;
6405 u16 txq_id;
6406 u32 tx_cmd;
6407
6408 txq_id = skb_get_queue_mapping(skb);
6409 txq = port->txqs[txq_id];
6410 txq_pcpu = this_cpu_ptr(txq->pcpu);
6411 aggr_txq = &port->priv->aggr_txqs[smp_processor_id()];
6412
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006413 if (skb_is_gso(skb)) {
6414 frags = mvpp2_tx_tso(skb, dev, txq, aggr_txq, txq_pcpu);
6415 goto out;
6416 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006417 frags = skb_shinfo(skb)->nr_frags + 1;
6418
6419 /* Check number of available descriptors */
6420 if (mvpp2_aggr_desc_num_check(port->priv, aggr_txq, frags) ||
6421 mvpp2_txq_reserved_desc_num_proc(port->priv, txq,
6422 txq_pcpu, frags)) {
6423 frags = 0;
6424 goto out;
6425 }
6426
6427 /* Get a descriptor for the first part of the packet */
6428 tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006429 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6430 mvpp2_txdesc_size_set(port, tx_desc, skb_headlen(skb));
Marcin Wojtas3f518502014-07-10 16:52:13 -03006431
Thomas Petazzoni20396132017-03-07 16:53:00 +01006432 buf_dma_addr = dma_map_single(dev->dev.parent, skb->data,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006433 skb_headlen(skb), DMA_TO_DEVICE);
Thomas Petazzoni20396132017-03-07 16:53:00 +01006434 if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006435 mvpp2_txq_desc_put(txq);
6436 frags = 0;
6437 goto out;
6438 }
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006439
6440 mvpp2_txdesc_offset_set(port, tx_desc,
6441 buf_dma_addr & MVPP2_TX_DESC_ALIGN);
6442 mvpp2_txdesc_dma_addr_set(port, tx_desc,
6443 buf_dma_addr & ~MVPP2_TX_DESC_ALIGN);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006444
6445 tx_cmd = mvpp2_skb_tx_csum(port, skb);
6446
6447 if (frags == 1) {
6448 /* First and Last descriptor */
6449 tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_L_DESC;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006450 mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
6451 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006452 } else {
6453 /* First but not Last */
6454 tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_PADDING_DISABLE;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006455 mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
6456 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006457
6458 /* Continue with other skb fragments */
6459 if (mvpp2_tx_frag_process(port, skb, aggr_txq, txq)) {
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006460 tx_desc_unmap_put(port, txq, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006461 frags = 0;
6462 goto out;
6463 }
6464 }
6465
Marcin Wojtas3f518502014-07-10 16:52:13 -03006466out:
6467 if (frags > 0) {
6468 struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006469 struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
6470
6471 txq_pcpu->reserved_num -= frags;
6472 txq_pcpu->count += frags;
6473 aggr_txq->count += frags;
6474
6475 /* Enable transmit */
6476 wmb();
6477 mvpp2_aggr_txq_pend_desc_add(port, frags);
6478
6479 if (txq_pcpu->size - txq_pcpu->count < MAX_SKB_FRAGS + 1)
6480 netif_tx_stop_queue(nq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006481
6482 u64_stats_update_begin(&stats->syncp);
6483 stats->tx_packets++;
6484 stats->tx_bytes += skb->len;
6485 u64_stats_update_end(&stats->syncp);
6486 } else {
6487 dev->stats.tx_dropped++;
6488 dev_kfree_skb_any(skb);
6489 }
6490
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006491 /* Finalize TX processing */
6492 if (txq_pcpu->count >= txq->done_pkts_coal)
6493 mvpp2_txq_done(port, txq, txq_pcpu);
6494
6495 /* Set the timer in case not all frags were processed */
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006496 if (!port->has_tx_irqs && txq_pcpu->count <= frags &&
6497 txq_pcpu->count > 0) {
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006498 struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
6499
6500 mvpp2_timer_set(port_pcpu);
6501 }
6502
Marcin Wojtas3f518502014-07-10 16:52:13 -03006503 return NETDEV_TX_OK;
6504}
6505
6506static inline void mvpp2_cause_error(struct net_device *dev, int cause)
6507{
6508 if (cause & MVPP2_CAUSE_FCS_ERR_MASK)
6509 netdev_err(dev, "FCS error\n");
6510 if (cause & MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK)
6511 netdev_err(dev, "rx fifo overrun error\n");
6512 if (cause & MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK)
6513 netdev_err(dev, "tx fifo underrun error\n");
6514}
6515
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006516static int mvpp2_poll(struct napi_struct *napi, int budget)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006517{
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006518 u32 cause_rx_tx, cause_rx, cause_tx, cause_misc;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006519 int rx_done = 0;
6520 struct mvpp2_port *port = netdev_priv(napi->dev);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006521 struct mvpp2_queue_vector *qv;
Thomas Petazzonia7868412017-03-07 16:53:13 +01006522 int cpu = smp_processor_id();
Marcin Wojtas3f518502014-07-10 16:52:13 -03006523
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006524 qv = container_of(napi, struct mvpp2_queue_vector, napi);
6525
Marcin Wojtas3f518502014-07-10 16:52:13 -03006526 /* Rx/Tx cause register
6527 *
6528 * Bits 0-15: each bit indicates received packets on the Rx queue
6529 * (bit 0 is for Rx queue 0).
6530 *
6531 * Bits 16-23: each bit indicates transmitted packets on the Tx queue
6532 * (bit 16 is for Tx queue 0).
6533 *
6534 * Each CPU has its own Rx/Tx cause register
6535 */
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006536 cause_rx_tx = mvpp2_percpu_read(port->priv, qv->sw_thread_id,
Thomas Petazzonia7868412017-03-07 16:53:13 +01006537 MVPP2_ISR_RX_TX_CAUSE_REG(port->id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03006538
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006539 cause_misc = cause_rx_tx & MVPP2_CAUSE_MISC_SUM_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006540 if (cause_misc) {
6541 mvpp2_cause_error(port->dev, cause_misc);
6542
6543 /* Clear the cause register */
6544 mvpp2_write(port->priv, MVPP2_ISR_MISC_CAUSE_REG, 0);
Thomas Petazzonia7868412017-03-07 16:53:13 +01006545 mvpp2_percpu_write(port->priv, cpu,
6546 MVPP2_ISR_RX_TX_CAUSE_REG(port->id),
6547 cause_rx_tx & ~MVPP2_CAUSE_MISC_SUM_MASK);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006548 }
6549
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006550 cause_tx = cause_rx_tx & MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
6551 if (cause_tx) {
6552 cause_tx >>= MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_OFFSET;
6553 mvpp2_tx_done(port, cause_tx, qv->sw_thread_id);
6554 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006555
6556 /* Process RX packets */
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006557 cause_rx = cause_rx_tx & MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
6558 cause_rx <<= qv->first_rxq;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006559 cause_rx |= qv->pending_cause_rx;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006560 while (cause_rx && budget > 0) {
6561 int count;
6562 struct mvpp2_rx_queue *rxq;
6563
6564 rxq = mvpp2_get_rx_queue(port, cause_rx);
6565 if (!rxq)
6566 break;
6567
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006568 count = mvpp2_rx(port, napi, budget, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006569 rx_done += count;
6570 budget -= count;
6571 if (budget > 0) {
6572 /* Clear the bit associated to this Rx queue
6573 * so that next iteration will continue from
6574 * the next Rx queue.
6575 */
6576 cause_rx &= ~(1 << rxq->logic_rxq);
6577 }
6578 }
6579
6580 if (budget > 0) {
6581 cause_rx = 0;
Eric Dumazet6ad20162017-01-30 08:22:01 -08006582 napi_complete_done(napi, rx_done);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006583
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006584 mvpp2_qvec_interrupt_enable(qv);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006585 }
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006586 qv->pending_cause_rx = cause_rx;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006587 return rx_done;
6588}
6589
6590/* Set hw internals when starting port */
6591static void mvpp2_start_dev(struct mvpp2_port *port)
6592{
Philippe Reynes8e072692016-06-28 00:08:11 +02006593 struct net_device *ndev = port->dev;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006594 int i;
Philippe Reynes8e072692016-06-28 00:08:11 +02006595
Stefan Chulski76eb1b12017-08-22 19:08:26 +02006596 if (port->gop_id == 0 &&
6597 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
6598 port->phy_interface == PHY_INTERFACE_MODE_10GKR))
6599 mvpp2_xlg_max_rx_size_set(port);
6600 else
6601 mvpp2_gmac_max_rx_size_set(port);
6602
Marcin Wojtas3f518502014-07-10 16:52:13 -03006603 mvpp2_txp_max_tx_size_set(port);
6604
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006605 for (i = 0; i < port->nqvecs; i++)
6606 napi_enable(&port->qvecs[i].napi);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006607
6608 /* Enable interrupts on all CPUs */
6609 mvpp2_interrupts_enable(port);
6610
Antoine Tenart542897d2017-08-30 10:29:15 +02006611 if (port->priv->hw_version == MVPP22) {
6612 mvpp22_comphy_init(port);
Antoine Ténartf84bf382017-08-22 19:08:27 +02006613 mvpp22_gop_init(port);
Antoine Tenart542897d2017-08-30 10:29:15 +02006614 }
Antoine Ténartf84bf382017-08-22 19:08:27 +02006615
Antoine Ténart2055d622017-08-22 19:08:23 +02006616 mvpp2_port_mii_set(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006617 mvpp2_port_enable(port);
Antoine Tenart5997c862017-09-01 11:04:53 +02006618 if (ndev->phydev)
6619 phy_start(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006620 netif_tx_start_all_queues(port->dev);
6621}
6622
6623/* Set hw internals when stopping port */
6624static void mvpp2_stop_dev(struct mvpp2_port *port)
6625{
Philippe Reynes8e072692016-06-28 00:08:11 +02006626 struct net_device *ndev = port->dev;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006627 int i;
Philippe Reynes8e072692016-06-28 00:08:11 +02006628
Marcin Wojtas3f518502014-07-10 16:52:13 -03006629 /* Stop new packets from arriving to RXQs */
6630 mvpp2_ingress_disable(port);
6631
6632 mdelay(10);
6633
6634 /* Disable interrupts on all CPUs */
6635 mvpp2_interrupts_disable(port);
6636
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006637 for (i = 0; i < port->nqvecs; i++)
6638 napi_disable(&port->qvecs[i].napi);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006639
6640 netif_carrier_off(port->dev);
6641 netif_tx_stop_all_queues(port->dev);
6642
6643 mvpp2_egress_disable(port);
6644 mvpp2_port_disable(port);
Antoine Tenart5997c862017-09-01 11:04:53 +02006645 if (ndev->phydev)
6646 phy_stop(ndev->phydev);
Antoine Tenart542897d2017-08-30 10:29:15 +02006647 phy_power_off(port->comphy);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006648}
6649
Marcin Wojtas3f518502014-07-10 16:52:13 -03006650static int mvpp2_check_ringparam_valid(struct net_device *dev,
6651 struct ethtool_ringparam *ring)
6652{
6653 u16 new_rx_pending = ring->rx_pending;
6654 u16 new_tx_pending = ring->tx_pending;
6655
6656 if (ring->rx_pending == 0 || ring->tx_pending == 0)
6657 return -EINVAL;
6658
6659 if (ring->rx_pending > MVPP2_MAX_RXD)
6660 new_rx_pending = MVPP2_MAX_RXD;
6661 else if (!IS_ALIGNED(ring->rx_pending, 16))
6662 new_rx_pending = ALIGN(ring->rx_pending, 16);
6663
6664 if (ring->tx_pending > MVPP2_MAX_TXD)
6665 new_tx_pending = MVPP2_MAX_TXD;
6666 else if (!IS_ALIGNED(ring->tx_pending, 32))
6667 new_tx_pending = ALIGN(ring->tx_pending, 32);
6668
6669 if (ring->rx_pending != new_rx_pending) {
6670 netdev_info(dev, "illegal Rx ring size value %d, round to %d\n",
6671 ring->rx_pending, new_rx_pending);
6672 ring->rx_pending = new_rx_pending;
6673 }
6674
6675 if (ring->tx_pending != new_tx_pending) {
6676 netdev_info(dev, "illegal Tx ring size value %d, round to %d\n",
6677 ring->tx_pending, new_tx_pending);
6678 ring->tx_pending = new_tx_pending;
6679 }
6680
6681 return 0;
6682}
6683
Thomas Petazzoni26975822017-03-07 16:53:14 +01006684static void mvpp21_get_mac_address(struct mvpp2_port *port, unsigned char *addr)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006685{
6686 u32 mac_addr_l, mac_addr_m, mac_addr_h;
6687
6688 mac_addr_l = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
6689 mac_addr_m = readl(port->priv->lms_base + MVPP2_SRC_ADDR_MIDDLE);
6690 mac_addr_h = readl(port->priv->lms_base + MVPP2_SRC_ADDR_HIGH);
6691 addr[0] = (mac_addr_h >> 24) & 0xFF;
6692 addr[1] = (mac_addr_h >> 16) & 0xFF;
6693 addr[2] = (mac_addr_h >> 8) & 0xFF;
6694 addr[3] = mac_addr_h & 0xFF;
6695 addr[4] = mac_addr_m & 0xFF;
6696 addr[5] = (mac_addr_l >> MVPP2_GMAC_SA_LOW_OFFS) & 0xFF;
6697}
6698
6699static int mvpp2_phy_connect(struct mvpp2_port *port)
6700{
6701 struct phy_device *phy_dev;
6702
Antoine Tenart5997c862017-09-01 11:04:53 +02006703 /* No PHY is attached */
6704 if (!port->phy_node)
6705 return 0;
6706
Marcin Wojtas3f518502014-07-10 16:52:13 -03006707 phy_dev = of_phy_connect(port->dev, port->phy_node, mvpp2_link_event, 0,
6708 port->phy_interface);
6709 if (!phy_dev) {
6710 netdev_err(port->dev, "cannot connect to phy\n");
6711 return -ENODEV;
6712 }
6713 phy_dev->supported &= PHY_GBIT_FEATURES;
6714 phy_dev->advertising = phy_dev->supported;
6715
Marcin Wojtas3f518502014-07-10 16:52:13 -03006716 port->link = 0;
6717 port->duplex = 0;
6718 port->speed = 0;
6719
6720 return 0;
6721}
6722
6723static void mvpp2_phy_disconnect(struct mvpp2_port *port)
6724{
Philippe Reynes8e072692016-06-28 00:08:11 +02006725 struct net_device *ndev = port->dev;
6726
Antoine Tenart5997c862017-09-01 11:04:53 +02006727 if (!ndev->phydev)
6728 return;
6729
Philippe Reynes8e072692016-06-28 00:08:11 +02006730 phy_disconnect(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006731}
6732
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006733static int mvpp2_irqs_init(struct mvpp2_port *port)
6734{
6735 int err, i;
6736
6737 for (i = 0; i < port->nqvecs; i++) {
6738 struct mvpp2_queue_vector *qv = port->qvecs + i;
6739
6740 err = request_irq(qv->irq, mvpp2_isr, 0, port->dev->name, qv);
6741 if (err)
6742 goto err;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006743
6744 if (qv->type == MVPP2_QUEUE_VECTOR_PRIVATE)
6745 irq_set_affinity_hint(qv->irq,
6746 cpumask_of(qv->sw_thread_id));
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006747 }
6748
6749 return 0;
6750err:
6751 for (i = 0; i < port->nqvecs; i++) {
6752 struct mvpp2_queue_vector *qv = port->qvecs + i;
6753
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006754 irq_set_affinity_hint(qv->irq, NULL);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006755 free_irq(qv->irq, qv);
6756 }
6757
6758 return err;
6759}
6760
6761static void mvpp2_irqs_deinit(struct mvpp2_port *port)
6762{
6763 int i;
6764
6765 for (i = 0; i < port->nqvecs; i++) {
6766 struct mvpp2_queue_vector *qv = port->qvecs + i;
6767
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006768 irq_set_affinity_hint(qv->irq, NULL);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006769 free_irq(qv->irq, qv);
6770 }
6771}
6772
Marcin Wojtas3f518502014-07-10 16:52:13 -03006773static int mvpp2_open(struct net_device *dev)
6774{
6775 struct mvpp2_port *port = netdev_priv(dev);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02006776 struct mvpp2 *priv = port->priv;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006777 unsigned char mac_bcast[ETH_ALEN] = {
6778 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
6779 int err;
6780
6781 err = mvpp2_prs_mac_da_accept(port->priv, port->id, mac_bcast, true);
6782 if (err) {
6783 netdev_err(dev, "mvpp2_prs_mac_da_accept BC failed\n");
6784 return err;
6785 }
6786 err = mvpp2_prs_mac_da_accept(port->priv, port->id,
6787 dev->dev_addr, true);
6788 if (err) {
6789 netdev_err(dev, "mvpp2_prs_mac_da_accept MC failed\n");
6790 return err;
6791 }
6792 err = mvpp2_prs_tag_mode_set(port->priv, port->id, MVPP2_TAG_TYPE_MH);
6793 if (err) {
6794 netdev_err(dev, "mvpp2_prs_tag_mode_set failed\n");
6795 return err;
6796 }
6797 err = mvpp2_prs_def_flow(port);
6798 if (err) {
6799 netdev_err(dev, "mvpp2_prs_def_flow failed\n");
6800 return err;
6801 }
6802
6803 /* Allocate the Rx/Tx queues */
6804 err = mvpp2_setup_rxqs(port);
6805 if (err) {
6806 netdev_err(port->dev, "cannot allocate Rx queues\n");
6807 return err;
6808 }
6809
6810 err = mvpp2_setup_txqs(port);
6811 if (err) {
6812 netdev_err(port->dev, "cannot allocate Tx queues\n");
6813 goto err_cleanup_rxqs;
6814 }
6815
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006816 err = mvpp2_irqs_init(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006817 if (err) {
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006818 netdev_err(port->dev, "cannot init IRQs\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03006819 goto err_cleanup_txqs;
6820 }
6821
Antoine Tenartfd3651b2017-09-01 11:04:54 +02006822 if (priv->hw_version == MVPP22 && !port->phy_node && port->link_irq) {
6823 err = request_irq(port->link_irq, mvpp2_link_status_isr, 0,
6824 dev->name, port);
6825 if (err) {
6826 netdev_err(port->dev, "cannot request link IRQ %d\n",
6827 port->link_irq);
6828 goto err_free_irq;
6829 }
6830
6831 mvpp22_gop_setup_irq(port);
6832 }
6833
Marcin Wojtas3f518502014-07-10 16:52:13 -03006834 /* In default link is down */
6835 netif_carrier_off(port->dev);
6836
6837 err = mvpp2_phy_connect(port);
6838 if (err < 0)
Antoine Tenartfd3651b2017-09-01 11:04:54 +02006839 goto err_free_link_irq;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006840
6841 /* Unmask interrupts on all CPUs */
6842 on_each_cpu(mvpp2_interrupts_unmask, port, 1);
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006843 mvpp2_shared_interrupt_mask_unmask(port, false);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006844
6845 mvpp2_start_dev(port);
6846
6847 return 0;
6848
Antoine Tenartfd3651b2017-09-01 11:04:54 +02006849err_free_link_irq:
6850 if (priv->hw_version == MVPP22 && !port->phy_node && port->link_irq)
6851 free_irq(port->link_irq, port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006852err_free_irq:
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006853 mvpp2_irqs_deinit(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006854err_cleanup_txqs:
6855 mvpp2_cleanup_txqs(port);
6856err_cleanup_rxqs:
6857 mvpp2_cleanup_rxqs(port);
6858 return err;
6859}
6860
6861static int mvpp2_stop(struct net_device *dev)
6862{
6863 struct mvpp2_port *port = netdev_priv(dev);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006864 struct mvpp2_port_pcpu *port_pcpu;
Antoine Tenartfd3651b2017-09-01 11:04:54 +02006865 struct mvpp2 *priv = port->priv;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006866 int cpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006867
6868 mvpp2_stop_dev(port);
6869 mvpp2_phy_disconnect(port);
6870
6871 /* Mask interrupts on all CPUs */
6872 on_each_cpu(mvpp2_interrupts_mask, port, 1);
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006873 mvpp2_shared_interrupt_mask_unmask(port, true);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006874
Antoine Tenartfd3651b2017-09-01 11:04:54 +02006875 if (priv->hw_version == MVPP22 && !port->phy_node && port->link_irq)
6876 free_irq(port->link_irq, port);
6877
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006878 mvpp2_irqs_deinit(port);
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006879 if (!port->has_tx_irqs) {
6880 for_each_present_cpu(cpu) {
6881 port_pcpu = per_cpu_ptr(port->pcpu, cpu);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006882
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006883 hrtimer_cancel(&port_pcpu->tx_done_timer);
6884 port_pcpu->timer_scheduled = false;
6885 tasklet_kill(&port_pcpu->tx_done_tasklet);
6886 }
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006887 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006888 mvpp2_cleanup_rxqs(port);
6889 mvpp2_cleanup_txqs(port);
6890
6891 return 0;
6892}
6893
6894static void mvpp2_set_rx_mode(struct net_device *dev)
6895{
6896 struct mvpp2_port *port = netdev_priv(dev);
6897 struct mvpp2 *priv = port->priv;
6898 struct netdev_hw_addr *ha;
6899 int id = port->id;
6900 bool allmulti = dev->flags & IFF_ALLMULTI;
6901
6902 mvpp2_prs_mac_promisc_set(priv, id, dev->flags & IFF_PROMISC);
6903 mvpp2_prs_mac_multi_set(priv, id, MVPP2_PE_MAC_MC_ALL, allmulti);
6904 mvpp2_prs_mac_multi_set(priv, id, MVPP2_PE_MAC_MC_IP6, allmulti);
6905
6906 /* Remove all port->id's mcast enries */
6907 mvpp2_prs_mcast_del_all(priv, id);
6908
6909 if (allmulti && !netdev_mc_empty(dev)) {
6910 netdev_for_each_mc_addr(ha, dev)
6911 mvpp2_prs_mac_da_accept(priv, id, ha->addr, true);
6912 }
6913}
6914
6915static int mvpp2_set_mac_address(struct net_device *dev, void *p)
6916{
6917 struct mvpp2_port *port = netdev_priv(dev);
6918 const struct sockaddr *addr = p;
6919 int err;
6920
6921 if (!is_valid_ether_addr(addr->sa_data)) {
6922 err = -EADDRNOTAVAIL;
Markus Elfringc1175542017-04-17 11:10:47 +02006923 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006924 }
6925
6926 if (!netif_running(dev)) {
6927 err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
6928 if (!err)
6929 return 0;
6930 /* Reconfigure parser to accept the original MAC address */
6931 err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
6932 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02006933 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006934 }
6935
6936 mvpp2_stop_dev(port);
6937
6938 err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
6939 if (!err)
6940 goto out_start;
6941
6942 /* Reconfigure parser accept the original MAC address */
6943 err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
6944 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02006945 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006946out_start:
6947 mvpp2_start_dev(port);
6948 mvpp2_egress_enable(port);
6949 mvpp2_ingress_enable(port);
6950 return 0;
Markus Elfringc1175542017-04-17 11:10:47 +02006951log_error:
Markus Elfringdfd42402017-04-17 11:20:41 +02006952 netdev_err(dev, "failed to change MAC address\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03006953 return err;
6954}
6955
6956static int mvpp2_change_mtu(struct net_device *dev, int mtu)
6957{
6958 struct mvpp2_port *port = netdev_priv(dev);
6959 int err;
6960
Jarod Wilson57779872016-10-17 15:54:06 -04006961 if (!IS_ALIGNED(MVPP2_RX_PKT_SIZE(mtu), 8)) {
6962 netdev_info(dev, "illegal MTU value %d, round to %d\n", mtu,
6963 ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8));
6964 mtu = ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006965 }
6966
6967 if (!netif_running(dev)) {
6968 err = mvpp2_bm_update_mtu(dev, mtu);
6969 if (!err) {
6970 port->pkt_size = MVPP2_RX_PKT_SIZE(mtu);
6971 return 0;
6972 }
6973
6974 /* Reconfigure BM to the original MTU */
6975 err = mvpp2_bm_update_mtu(dev, dev->mtu);
6976 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02006977 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006978 }
6979
6980 mvpp2_stop_dev(port);
6981
6982 err = mvpp2_bm_update_mtu(dev, mtu);
6983 if (!err) {
6984 port->pkt_size = MVPP2_RX_PKT_SIZE(mtu);
6985 goto out_start;
6986 }
6987
6988 /* Reconfigure BM to the original MTU */
6989 err = mvpp2_bm_update_mtu(dev, dev->mtu);
6990 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02006991 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006992
6993out_start:
6994 mvpp2_start_dev(port);
6995 mvpp2_egress_enable(port);
6996 mvpp2_ingress_enable(port);
6997
6998 return 0;
Markus Elfringc1175542017-04-17 11:10:47 +02006999log_error:
Markus Elfringdfd42402017-04-17 11:20:41 +02007000 netdev_err(dev, "failed to change MTU\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03007001 return err;
7002}
7003
stephen hemmingerbc1f4472017-01-06 19:12:52 -08007004static void
Marcin Wojtas3f518502014-07-10 16:52:13 -03007005mvpp2_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
7006{
7007 struct mvpp2_port *port = netdev_priv(dev);
7008 unsigned int start;
7009 int cpu;
7010
7011 for_each_possible_cpu(cpu) {
7012 struct mvpp2_pcpu_stats *cpu_stats;
7013 u64 rx_packets;
7014 u64 rx_bytes;
7015 u64 tx_packets;
7016 u64 tx_bytes;
7017
7018 cpu_stats = per_cpu_ptr(port->stats, cpu);
7019 do {
7020 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
7021 rx_packets = cpu_stats->rx_packets;
7022 rx_bytes = cpu_stats->rx_bytes;
7023 tx_packets = cpu_stats->tx_packets;
7024 tx_bytes = cpu_stats->tx_bytes;
7025 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
7026
7027 stats->rx_packets += rx_packets;
7028 stats->rx_bytes += rx_bytes;
7029 stats->tx_packets += tx_packets;
7030 stats->tx_bytes += tx_bytes;
7031 }
7032
7033 stats->rx_errors = dev->stats.rx_errors;
7034 stats->rx_dropped = dev->stats.rx_dropped;
7035 stats->tx_dropped = dev->stats.tx_dropped;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007036}
7037
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007038static int mvpp2_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
7039{
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007040 int ret;
7041
Philippe Reynes8e072692016-06-28 00:08:11 +02007042 if (!dev->phydev)
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007043 return -ENOTSUPP;
7044
Philippe Reynes8e072692016-06-28 00:08:11 +02007045 ret = phy_mii_ioctl(dev->phydev, ifr, cmd);
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007046 if (!ret)
7047 mvpp2_link_event(dev);
7048
7049 return ret;
7050}
7051
Marcin Wojtas3f518502014-07-10 16:52:13 -03007052/* Ethtool methods */
7053
Marcin Wojtas3f518502014-07-10 16:52:13 -03007054/* Set interrupt coalescing for ethtools */
7055static int mvpp2_ethtool_set_coalesce(struct net_device *dev,
7056 struct ethtool_coalesce *c)
7057{
7058 struct mvpp2_port *port = netdev_priv(dev);
7059 int queue;
7060
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007061 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007062 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
7063
7064 rxq->time_coal = c->rx_coalesce_usecs;
7065 rxq->pkts_coal = c->rx_max_coalesced_frames;
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01007066 mvpp2_rx_pkts_coal_set(port, rxq);
7067 mvpp2_rx_time_coal_set(port, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007068 }
7069
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007070 if (port->has_tx_irqs) {
7071 port->tx_time_coal = c->tx_coalesce_usecs;
7072 mvpp2_tx_time_coal_set(port);
7073 }
7074
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007075 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007076 struct mvpp2_tx_queue *txq = port->txqs[queue];
7077
7078 txq->done_pkts_coal = c->tx_max_coalesced_frames;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007079
7080 if (port->has_tx_irqs)
7081 mvpp2_tx_pkts_coal_set(port, txq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007082 }
7083
Marcin Wojtas3f518502014-07-10 16:52:13 -03007084 return 0;
7085}
7086
7087/* get coalescing for ethtools */
7088static int mvpp2_ethtool_get_coalesce(struct net_device *dev,
7089 struct ethtool_coalesce *c)
7090{
7091 struct mvpp2_port *port = netdev_priv(dev);
7092
7093 c->rx_coalesce_usecs = port->rxqs[0]->time_coal;
7094 c->rx_max_coalesced_frames = port->rxqs[0]->pkts_coal;
7095 c->tx_max_coalesced_frames = port->txqs[0]->done_pkts_coal;
7096 return 0;
7097}
7098
7099static void mvpp2_ethtool_get_drvinfo(struct net_device *dev,
7100 struct ethtool_drvinfo *drvinfo)
7101{
7102 strlcpy(drvinfo->driver, MVPP2_DRIVER_NAME,
7103 sizeof(drvinfo->driver));
7104 strlcpy(drvinfo->version, MVPP2_DRIVER_VERSION,
7105 sizeof(drvinfo->version));
7106 strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
7107 sizeof(drvinfo->bus_info));
7108}
7109
7110static void mvpp2_ethtool_get_ringparam(struct net_device *dev,
7111 struct ethtool_ringparam *ring)
7112{
7113 struct mvpp2_port *port = netdev_priv(dev);
7114
7115 ring->rx_max_pending = MVPP2_MAX_RXD;
7116 ring->tx_max_pending = MVPP2_MAX_TXD;
7117 ring->rx_pending = port->rx_ring_size;
7118 ring->tx_pending = port->tx_ring_size;
7119}
7120
7121static int mvpp2_ethtool_set_ringparam(struct net_device *dev,
7122 struct ethtool_ringparam *ring)
7123{
7124 struct mvpp2_port *port = netdev_priv(dev);
7125 u16 prev_rx_ring_size = port->rx_ring_size;
7126 u16 prev_tx_ring_size = port->tx_ring_size;
7127 int err;
7128
7129 err = mvpp2_check_ringparam_valid(dev, ring);
7130 if (err)
7131 return err;
7132
7133 if (!netif_running(dev)) {
7134 port->rx_ring_size = ring->rx_pending;
7135 port->tx_ring_size = ring->tx_pending;
7136 return 0;
7137 }
7138
7139 /* The interface is running, so we have to force a
7140 * reallocation of the queues
7141 */
7142 mvpp2_stop_dev(port);
7143 mvpp2_cleanup_rxqs(port);
7144 mvpp2_cleanup_txqs(port);
7145
7146 port->rx_ring_size = ring->rx_pending;
7147 port->tx_ring_size = ring->tx_pending;
7148
7149 err = mvpp2_setup_rxqs(port);
7150 if (err) {
7151 /* Reallocate Rx queues with the original ring size */
7152 port->rx_ring_size = prev_rx_ring_size;
7153 ring->rx_pending = prev_rx_ring_size;
7154 err = mvpp2_setup_rxqs(port);
7155 if (err)
7156 goto err_out;
7157 }
7158 err = mvpp2_setup_txqs(port);
7159 if (err) {
7160 /* Reallocate Tx queues with the original ring size */
7161 port->tx_ring_size = prev_tx_ring_size;
7162 ring->tx_pending = prev_tx_ring_size;
7163 err = mvpp2_setup_txqs(port);
7164 if (err)
7165 goto err_clean_rxqs;
7166 }
7167
7168 mvpp2_start_dev(port);
7169 mvpp2_egress_enable(port);
7170 mvpp2_ingress_enable(port);
7171
7172 return 0;
7173
7174err_clean_rxqs:
7175 mvpp2_cleanup_rxqs(port);
7176err_out:
Markus Elfringdfd42402017-04-17 11:20:41 +02007177 netdev_err(dev, "failed to change ring parameters");
Marcin Wojtas3f518502014-07-10 16:52:13 -03007178 return err;
7179}
7180
7181/* Device ops */
7182
7183static const struct net_device_ops mvpp2_netdev_ops = {
7184 .ndo_open = mvpp2_open,
7185 .ndo_stop = mvpp2_stop,
7186 .ndo_start_xmit = mvpp2_tx,
7187 .ndo_set_rx_mode = mvpp2_set_rx_mode,
7188 .ndo_set_mac_address = mvpp2_set_mac_address,
7189 .ndo_change_mtu = mvpp2_change_mtu,
7190 .ndo_get_stats64 = mvpp2_get_stats64,
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007191 .ndo_do_ioctl = mvpp2_ioctl,
Marcin Wojtas3f518502014-07-10 16:52:13 -03007192};
7193
7194static const struct ethtool_ops mvpp2_eth_tool_ops = {
Florian Fainelli00606c42016-11-15 11:19:48 -08007195 .nway_reset = phy_ethtool_nway_reset,
Marcin Wojtas3f518502014-07-10 16:52:13 -03007196 .get_link = ethtool_op_get_link,
Marcin Wojtas3f518502014-07-10 16:52:13 -03007197 .set_coalesce = mvpp2_ethtool_set_coalesce,
7198 .get_coalesce = mvpp2_ethtool_get_coalesce,
7199 .get_drvinfo = mvpp2_ethtool_get_drvinfo,
7200 .get_ringparam = mvpp2_ethtool_get_ringparam,
7201 .set_ringparam = mvpp2_ethtool_set_ringparam,
Philippe Reynesfb773e92016-06-28 00:08:12 +02007202 .get_link_ksettings = phy_ethtool_get_link_ksettings,
7203 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Marcin Wojtas3f518502014-07-10 16:52:13 -03007204};
7205
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007206/* Used for PPv2.1, or PPv2.2 with the old Device Tree binding that
7207 * had a single IRQ defined per-port.
7208 */
7209static int mvpp2_simple_queue_vectors_init(struct mvpp2_port *port,
7210 struct device_node *port_node)
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007211{
7212 struct mvpp2_queue_vector *v = &port->qvecs[0];
7213
7214 v->first_rxq = 0;
7215 v->nrxqs = port->nrxqs;
7216 v->type = MVPP2_QUEUE_VECTOR_SHARED;
7217 v->sw_thread_id = 0;
7218 v->sw_thread_mask = *cpumask_bits(cpu_online_mask);
7219 v->port = port;
7220 v->irq = irq_of_parse_and_map(port_node, 0);
7221 if (v->irq <= 0)
7222 return -EINVAL;
7223 netif_napi_add(port->dev, &v->napi, mvpp2_poll,
7224 NAPI_POLL_WEIGHT);
7225
7226 port->nqvecs = 1;
7227
7228 return 0;
7229}
7230
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007231static int mvpp2_multi_queue_vectors_init(struct mvpp2_port *port,
7232 struct device_node *port_node)
7233{
7234 struct mvpp2_queue_vector *v;
7235 int i, ret;
7236
7237 port->nqvecs = num_possible_cpus();
7238 if (queue_mode == MVPP2_QDIST_SINGLE_MODE)
7239 port->nqvecs += 1;
7240
7241 for (i = 0; i < port->nqvecs; i++) {
7242 char irqname[16];
7243
7244 v = port->qvecs + i;
7245
7246 v->port = port;
7247 v->type = MVPP2_QUEUE_VECTOR_PRIVATE;
7248 v->sw_thread_id = i;
7249 v->sw_thread_mask = BIT(i);
7250
7251 snprintf(irqname, sizeof(irqname), "tx-cpu%d", i);
7252
7253 if (queue_mode == MVPP2_QDIST_MULTI_MODE) {
7254 v->first_rxq = i * MVPP2_DEFAULT_RXQ;
7255 v->nrxqs = MVPP2_DEFAULT_RXQ;
7256 } else if (queue_mode == MVPP2_QDIST_SINGLE_MODE &&
7257 i == (port->nqvecs - 1)) {
7258 v->first_rxq = 0;
7259 v->nrxqs = port->nrxqs;
7260 v->type = MVPP2_QUEUE_VECTOR_SHARED;
7261 strncpy(irqname, "rx-shared", sizeof(irqname));
7262 }
7263
7264 v->irq = of_irq_get_byname(port_node, irqname);
7265 if (v->irq <= 0) {
7266 ret = -EINVAL;
7267 goto err;
7268 }
7269
7270 netif_napi_add(port->dev, &v->napi, mvpp2_poll,
7271 NAPI_POLL_WEIGHT);
7272 }
7273
7274 return 0;
7275
7276err:
7277 for (i = 0; i < port->nqvecs; i++)
7278 irq_dispose_mapping(port->qvecs[i].irq);
7279 return ret;
7280}
7281
7282static int mvpp2_queue_vectors_init(struct mvpp2_port *port,
7283 struct device_node *port_node)
7284{
7285 if (port->has_tx_irqs)
7286 return mvpp2_multi_queue_vectors_init(port, port_node);
7287 else
7288 return mvpp2_simple_queue_vectors_init(port, port_node);
7289}
7290
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007291static void mvpp2_queue_vectors_deinit(struct mvpp2_port *port)
7292{
7293 int i;
7294
7295 for (i = 0; i < port->nqvecs; i++)
7296 irq_dispose_mapping(port->qvecs[i].irq);
7297}
7298
7299/* Configure Rx queue group interrupt for this port */
7300static void mvpp2_rx_irqs_setup(struct mvpp2_port *port)
7301{
7302 struct mvpp2 *priv = port->priv;
7303 u32 val;
7304 int i;
7305
7306 if (priv->hw_version == MVPP21) {
7307 mvpp2_write(priv, MVPP21_ISR_RXQ_GROUP_REG(port->id),
7308 port->nrxqs);
7309 return;
7310 }
7311
7312 /* Handle the more complicated PPv2.2 case */
7313 for (i = 0; i < port->nqvecs; i++) {
7314 struct mvpp2_queue_vector *qv = port->qvecs + i;
7315
7316 if (!qv->nrxqs)
7317 continue;
7318
7319 val = qv->sw_thread_id;
7320 val |= port->id << MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_OFFSET;
7321 mvpp2_write(priv, MVPP22_ISR_RXQ_GROUP_INDEX_REG, val);
7322
7323 val = qv->first_rxq;
7324 val |= qv->nrxqs << MVPP22_ISR_RXQ_SUB_GROUP_SIZE_OFFSET;
7325 mvpp2_write(priv, MVPP22_ISR_RXQ_SUB_GROUP_CONFIG_REG, val);
7326 }
7327}
7328
Marcin Wojtas3f518502014-07-10 16:52:13 -03007329/* Initialize port HW */
7330static int mvpp2_port_init(struct mvpp2_port *port)
7331{
7332 struct device *dev = port->dev->dev.parent;
7333 struct mvpp2 *priv = port->priv;
7334 struct mvpp2_txq_pcpu *txq_pcpu;
7335 int queue, cpu, err;
7336
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007337 /* Checks for hardware constraints */
7338 if (port->first_rxq + port->nrxqs >
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01007339 MVPP2_MAX_PORTS * priv->max_port_rxqs)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007340 return -EINVAL;
7341
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007342 if (port->nrxqs % 4 || (port->nrxqs > priv->max_port_rxqs) ||
7343 (port->ntxqs > MVPP2_MAX_TXQ))
7344 return -EINVAL;
7345
Marcin Wojtas3f518502014-07-10 16:52:13 -03007346 /* Disable port */
7347 mvpp2_egress_disable(port);
7348 mvpp2_port_disable(port);
7349
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007350 port->tx_time_coal = MVPP2_TXDONE_COAL_USEC;
7351
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007352 port->txqs = devm_kcalloc(dev, port->ntxqs, sizeof(*port->txqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03007353 GFP_KERNEL);
7354 if (!port->txqs)
7355 return -ENOMEM;
7356
7357 /* Associate physical Tx queues to this port and initialize.
7358 * The mapping is predefined.
7359 */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007360 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007361 int queue_phy_id = mvpp2_txq_phys(port->id, queue);
7362 struct mvpp2_tx_queue *txq;
7363
7364 txq = devm_kzalloc(dev, sizeof(*txq), GFP_KERNEL);
Christophe Jaillet177c8d12017-02-19 10:19:57 +01007365 if (!txq) {
7366 err = -ENOMEM;
7367 goto err_free_percpu;
7368 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03007369
7370 txq->pcpu = alloc_percpu(struct mvpp2_txq_pcpu);
7371 if (!txq->pcpu) {
7372 err = -ENOMEM;
7373 goto err_free_percpu;
7374 }
7375
7376 txq->id = queue_phy_id;
7377 txq->log_id = queue;
7378 txq->done_pkts_coal = MVPP2_TXDONE_COAL_PKTS_THRESH;
7379 for_each_present_cpu(cpu) {
7380 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
7381 txq_pcpu->cpu = cpu;
7382 }
7383
7384 port->txqs[queue] = txq;
7385 }
7386
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007387 port->rxqs = devm_kcalloc(dev, port->nrxqs, sizeof(*port->rxqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03007388 GFP_KERNEL);
7389 if (!port->rxqs) {
7390 err = -ENOMEM;
7391 goto err_free_percpu;
7392 }
7393
7394 /* Allocate and initialize Rx queue for this port */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007395 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007396 struct mvpp2_rx_queue *rxq;
7397
7398 /* Map physical Rx queue to port's logical Rx queue */
7399 rxq = devm_kzalloc(dev, sizeof(*rxq), GFP_KERNEL);
Jisheng Zhangd82b0c22016-03-31 17:01:23 +08007400 if (!rxq) {
7401 err = -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007402 goto err_free_percpu;
Jisheng Zhangd82b0c22016-03-31 17:01:23 +08007403 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03007404 /* Map this Rx queue to a physical queue */
7405 rxq->id = port->first_rxq + queue;
7406 rxq->port = port->id;
7407 rxq->logic_rxq = queue;
7408
7409 port->rxqs[queue] = rxq;
7410 }
7411
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007412 mvpp2_rx_irqs_setup(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007413
7414 /* Create Rx descriptor rings */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007415 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007416 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
7417
7418 rxq->size = port->rx_ring_size;
7419 rxq->pkts_coal = MVPP2_RX_COAL_PKTS;
7420 rxq->time_coal = MVPP2_RX_COAL_USEC;
7421 }
7422
7423 mvpp2_ingress_disable(port);
7424
7425 /* Port default configuration */
7426 mvpp2_defaults_set(port);
7427
7428 /* Port's classifier configuration */
7429 mvpp2_cls_oversize_rxq_set(port);
7430 mvpp2_cls_port_config(port);
7431
7432 /* Provide an initial Rx packet size */
7433 port->pkt_size = MVPP2_RX_PKT_SIZE(port->dev->mtu);
7434
7435 /* Initialize pools for swf */
7436 err = mvpp2_swf_bm_pool_init(port);
7437 if (err)
7438 goto err_free_percpu;
7439
7440 return 0;
7441
7442err_free_percpu:
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007443 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007444 if (!port->txqs[queue])
7445 continue;
7446 free_percpu(port->txqs[queue]->pcpu);
7447 }
7448 return err;
7449}
7450
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007451/* Checks if the port DT description has the TX interrupts
7452 * described. On PPv2.1, there are no such interrupts. On PPv2.2,
7453 * there are available, but we need to keep support for old DTs.
7454 */
7455static bool mvpp2_port_has_tx_irqs(struct mvpp2 *priv,
7456 struct device_node *port_node)
7457{
7458 char *irqs[5] = { "rx-shared", "tx-cpu0", "tx-cpu1",
7459 "tx-cpu2", "tx-cpu3" };
7460 int ret, i;
7461
7462 if (priv->hw_version == MVPP21)
7463 return false;
7464
7465 for (i = 0; i < 5; i++) {
7466 ret = of_property_match_string(port_node, "interrupt-names",
7467 irqs[i]);
7468 if (ret < 0)
7469 return false;
7470 }
7471
7472 return true;
7473}
7474
Antoine Tenart3ba8c812017-09-02 11:06:47 +02007475static void mvpp2_port_copy_mac_addr(struct net_device *dev, struct mvpp2 *priv,
7476 struct device_node *port_node,
7477 char **mac_from)
7478{
7479 struct mvpp2_port *port = netdev_priv(dev);
7480 char hw_mac_addr[ETH_ALEN] = {0};
7481 const char *dt_mac_addr;
7482
7483 dt_mac_addr = of_get_mac_address(port_node);
7484 if (dt_mac_addr && is_valid_ether_addr(dt_mac_addr)) {
7485 *mac_from = "device tree";
7486 ether_addr_copy(dev->dev_addr, dt_mac_addr);
Antoine Tenart688cbaf2017-09-02 11:06:49 +02007487 return;
Antoine Tenart3ba8c812017-09-02 11:06:47 +02007488 }
Antoine Tenart688cbaf2017-09-02 11:06:49 +02007489
7490 if (priv->hw_version == MVPP21) {
7491 mvpp21_get_mac_address(port, hw_mac_addr);
7492 if (is_valid_ether_addr(hw_mac_addr)) {
7493 *mac_from = "hardware";
7494 ether_addr_copy(dev->dev_addr, hw_mac_addr);
7495 return;
7496 }
7497 }
7498
7499 *mac_from = "random";
7500 eth_hw_addr_random(dev);
Antoine Tenart3ba8c812017-09-02 11:06:47 +02007501}
7502
Marcin Wojtas3f518502014-07-10 16:52:13 -03007503/* Ports initialization */
7504static int mvpp2_port_probe(struct platform_device *pdev,
7505 struct device_node *port_node,
Yan Markman6bf69a12017-09-25 14:59:47 +02007506 struct mvpp2 *priv, int index)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007507{
7508 struct device_node *phy_node;
Antoine Tenart542897d2017-08-30 10:29:15 +02007509 struct phy *comphy;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007510 struct mvpp2_port *port;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007511 struct mvpp2_port_pcpu *port_pcpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007512 struct net_device *dev;
7513 struct resource *res;
Antoine Tenart3ba8c812017-09-02 11:06:47 +02007514 char *mac_from = "";
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007515 unsigned int ntxqs, nrxqs;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007516 bool has_tx_irqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007517 u32 id;
7518 int features;
7519 int phy_mode;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007520 int err, i, cpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007521
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007522 has_tx_irqs = mvpp2_port_has_tx_irqs(priv, port_node);
7523
7524 if (!has_tx_irqs)
7525 queue_mode = MVPP2_QDIST_SINGLE_MODE;
7526
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007527 ntxqs = MVPP2_MAX_TXQ;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007528 if (priv->hw_version == MVPP22 && queue_mode == MVPP2_QDIST_MULTI_MODE)
7529 nrxqs = MVPP2_DEFAULT_RXQ * num_possible_cpus();
7530 else
7531 nrxqs = MVPP2_DEFAULT_RXQ;
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007532
7533 dev = alloc_etherdev_mqs(sizeof(*port), ntxqs, nrxqs);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007534 if (!dev)
7535 return -ENOMEM;
7536
7537 phy_node = of_parse_phandle(port_node, "phy", 0);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007538 phy_mode = of_get_phy_mode(port_node);
7539 if (phy_mode < 0) {
7540 dev_err(&pdev->dev, "incorrect phy mode\n");
7541 err = phy_mode;
7542 goto err_free_netdev;
7543 }
7544
Antoine Tenart542897d2017-08-30 10:29:15 +02007545 comphy = devm_of_phy_get(&pdev->dev, port_node, NULL);
7546 if (IS_ERR(comphy)) {
7547 if (PTR_ERR(comphy) == -EPROBE_DEFER) {
7548 err = -EPROBE_DEFER;
7549 goto err_free_netdev;
7550 }
7551 comphy = NULL;
7552 }
7553
Marcin Wojtas3f518502014-07-10 16:52:13 -03007554 if (of_property_read_u32(port_node, "port-id", &id)) {
7555 err = -EINVAL;
7556 dev_err(&pdev->dev, "missing port-id value\n");
7557 goto err_free_netdev;
7558 }
7559
7560 dev->tx_queue_len = MVPP2_MAX_TXD;
7561 dev->watchdog_timeo = 5 * HZ;
7562 dev->netdev_ops = &mvpp2_netdev_ops;
7563 dev->ethtool_ops = &mvpp2_eth_tool_ops;
7564
7565 port = netdev_priv(dev);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007566 port->dev = dev;
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007567 port->ntxqs = ntxqs;
7568 port->nrxqs = nrxqs;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007569 port->priv = priv;
7570 port->has_tx_irqs = has_tx_irqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007571
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007572 err = mvpp2_queue_vectors_init(port, port_node);
7573 if (err)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007574 goto err_free_netdev;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007575
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007576 port->link_irq = of_irq_get_byname(port_node, "link");
7577 if (port->link_irq == -EPROBE_DEFER) {
7578 err = -EPROBE_DEFER;
7579 goto err_deinit_qvecs;
7580 }
7581 if (port->link_irq <= 0)
7582 /* the link irq is optional */
7583 port->link_irq = 0;
7584
Marcin Wojtas3f518502014-07-10 16:52:13 -03007585 if (of_property_read_bool(port_node, "marvell,loopback"))
7586 port->flags |= MVPP2_F_LOOPBACK;
7587
Marcin Wojtas3f518502014-07-10 16:52:13 -03007588 port->id = id;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01007589 if (priv->hw_version == MVPP21)
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007590 port->first_rxq = port->id * port->nrxqs;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01007591 else
7592 port->first_rxq = port->id * priv->max_port_rxqs;
7593
Marcin Wojtas3f518502014-07-10 16:52:13 -03007594 port->phy_node = phy_node;
7595 port->phy_interface = phy_mode;
Antoine Tenart542897d2017-08-30 10:29:15 +02007596 port->comphy = comphy;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007597
Thomas Petazzonia7868412017-03-07 16:53:13 +01007598 if (priv->hw_version == MVPP21) {
7599 res = platform_get_resource(pdev, IORESOURCE_MEM, 2 + id);
7600 port->base = devm_ioremap_resource(&pdev->dev, res);
7601 if (IS_ERR(port->base)) {
7602 err = PTR_ERR(port->base);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007603 goto err_free_irq;
Thomas Petazzonia7868412017-03-07 16:53:13 +01007604 }
7605 } else {
7606 if (of_property_read_u32(port_node, "gop-port-id",
7607 &port->gop_id)) {
7608 err = -EINVAL;
7609 dev_err(&pdev->dev, "missing gop-port-id value\n");
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007610 goto err_deinit_qvecs;
Thomas Petazzonia7868412017-03-07 16:53:13 +01007611 }
7612
7613 port->base = priv->iface_base + MVPP22_GMAC_BASE(port->gop_id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007614 }
7615
7616 /* Alloc per-cpu stats */
7617 port->stats = netdev_alloc_pcpu_stats(struct mvpp2_pcpu_stats);
7618 if (!port->stats) {
7619 err = -ENOMEM;
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007620 goto err_free_irq;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007621 }
7622
Antoine Tenart3ba8c812017-09-02 11:06:47 +02007623 mvpp2_port_copy_mac_addr(dev, priv, port_node, &mac_from);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007624
7625 port->tx_ring_size = MVPP2_MAX_TXD;
7626 port->rx_ring_size = MVPP2_MAX_RXD;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007627 SET_NETDEV_DEV(dev, &pdev->dev);
7628
7629 err = mvpp2_port_init(port);
7630 if (err < 0) {
7631 dev_err(&pdev->dev, "failed to init port %d\n", id);
7632 goto err_free_stats;
7633 }
Thomas Petazzoni26975822017-03-07 16:53:14 +01007634
Thomas Petazzoni26975822017-03-07 16:53:14 +01007635 mvpp2_port_periodic_xon_disable(port);
7636
7637 if (priv->hw_version == MVPP21)
7638 mvpp2_port_fc_adv_enable(port);
7639
7640 mvpp2_port_reset(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007641
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007642 port->pcpu = alloc_percpu(struct mvpp2_port_pcpu);
7643 if (!port->pcpu) {
7644 err = -ENOMEM;
7645 goto err_free_txq_pcpu;
7646 }
7647
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007648 if (!port->has_tx_irqs) {
7649 for_each_present_cpu(cpu) {
7650 port_pcpu = per_cpu_ptr(port->pcpu, cpu);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007651
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007652 hrtimer_init(&port_pcpu->tx_done_timer, CLOCK_MONOTONIC,
7653 HRTIMER_MODE_REL_PINNED);
7654 port_pcpu->tx_done_timer.function = mvpp2_hr_timer_cb;
7655 port_pcpu->timer_scheduled = false;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007656
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007657 tasklet_init(&port_pcpu->tx_done_tasklet,
7658 mvpp2_tx_proc_cb,
7659 (unsigned long)dev);
7660 }
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007661 }
7662
Antoine Ténart186cd4d2017-08-23 09:46:56 +02007663 features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007664 dev->features = features | NETIF_F_RXCSUM;
7665 dev->hw_features |= features | NETIF_F_RXCSUM | NETIF_F_GRO;
7666 dev->vlan_features |= features;
7667
Jarod Wilson57779872016-10-17 15:54:06 -04007668 /* MTU range: 68 - 9676 */
7669 dev->min_mtu = ETH_MIN_MTU;
7670 /* 9676 == 9700 - 20 and rounding to 8 */
7671 dev->max_mtu = 9676;
7672
Marcin Wojtas3f518502014-07-10 16:52:13 -03007673 err = register_netdev(dev);
7674 if (err < 0) {
7675 dev_err(&pdev->dev, "failed to register netdev\n");
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007676 goto err_free_port_pcpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007677 }
7678 netdev_info(dev, "Using %s mac address %pM\n", mac_from, dev->dev_addr);
7679
Yan Markman6bf69a12017-09-25 14:59:47 +02007680 priv->port_list[index] = port;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007681 return 0;
7682
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007683err_free_port_pcpu:
7684 free_percpu(port->pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007685err_free_txq_pcpu:
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007686 for (i = 0; i < port->ntxqs; i++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007687 free_percpu(port->txqs[i]->pcpu);
7688err_free_stats:
7689 free_percpu(port->stats);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007690err_free_irq:
7691 if (port->link_irq)
7692 irq_dispose_mapping(port->link_irq);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007693err_deinit_qvecs:
7694 mvpp2_queue_vectors_deinit(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007695err_free_netdev:
Peter Chenccb80392016-08-01 15:02:37 +08007696 of_node_put(phy_node);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007697 free_netdev(dev);
7698 return err;
7699}
7700
7701/* Ports removal routine */
7702static void mvpp2_port_remove(struct mvpp2_port *port)
7703{
7704 int i;
7705
7706 unregister_netdev(port->dev);
Peter Chenccb80392016-08-01 15:02:37 +08007707 of_node_put(port->phy_node);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007708 free_percpu(port->pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007709 free_percpu(port->stats);
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007710 for (i = 0; i < port->ntxqs; i++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007711 free_percpu(port->txqs[i]->pcpu);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007712 mvpp2_queue_vectors_deinit(port);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007713 if (port->link_irq)
7714 irq_dispose_mapping(port->link_irq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007715 free_netdev(port->dev);
7716}
7717
7718/* Initialize decoding windows */
7719static void mvpp2_conf_mbus_windows(const struct mbus_dram_target_info *dram,
7720 struct mvpp2 *priv)
7721{
7722 u32 win_enable;
7723 int i;
7724
7725 for (i = 0; i < 6; i++) {
7726 mvpp2_write(priv, MVPP2_WIN_BASE(i), 0);
7727 mvpp2_write(priv, MVPP2_WIN_SIZE(i), 0);
7728
7729 if (i < 4)
7730 mvpp2_write(priv, MVPP2_WIN_REMAP(i), 0);
7731 }
7732
7733 win_enable = 0;
7734
7735 for (i = 0; i < dram->num_cs; i++) {
7736 const struct mbus_dram_window *cs = dram->cs + i;
7737
7738 mvpp2_write(priv, MVPP2_WIN_BASE(i),
7739 (cs->base & 0xffff0000) | (cs->mbus_attr << 8) |
7740 dram->mbus_dram_target_id);
7741
7742 mvpp2_write(priv, MVPP2_WIN_SIZE(i),
7743 (cs->size - 1) & 0xffff0000);
7744
7745 win_enable |= (1 << i);
7746 }
7747
7748 mvpp2_write(priv, MVPP2_BASE_ADDR_ENABLE, win_enable);
7749}
7750
7751/* Initialize Rx FIFO's */
7752static void mvpp2_rx_fifo_init(struct mvpp2 *priv)
7753{
7754 int port;
7755
7756 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
7757 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
7758 MVPP2_RX_FIFO_PORT_DATA_SIZE);
7759 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
7760 MVPP2_RX_FIFO_PORT_ATTR_SIZE);
7761 }
7762
7763 mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
7764 MVPP2_RX_FIFO_PORT_MIN_PKT);
7765 mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
7766}
7767
Thomas Petazzoni6763ce32017-03-07 16:53:15 +01007768static void mvpp2_axi_init(struct mvpp2 *priv)
7769{
7770 u32 val, rdval, wrval;
7771
7772 mvpp2_write(priv, MVPP22_BM_ADDR_HIGH_RLS_REG, 0x0);
7773
7774 /* AXI Bridge Configuration */
7775
7776 rdval = MVPP22_AXI_CODE_CACHE_RD_CACHE
7777 << MVPP22_AXI_ATTR_CACHE_OFFS;
7778 rdval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
7779 << MVPP22_AXI_ATTR_DOMAIN_OFFS;
7780
7781 wrval = MVPP22_AXI_CODE_CACHE_WR_CACHE
7782 << MVPP22_AXI_ATTR_CACHE_OFFS;
7783 wrval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
7784 << MVPP22_AXI_ATTR_DOMAIN_OFFS;
7785
7786 /* BM */
7787 mvpp2_write(priv, MVPP22_AXI_BM_WR_ATTR_REG, wrval);
7788 mvpp2_write(priv, MVPP22_AXI_BM_RD_ATTR_REG, rdval);
7789
7790 /* Descriptors */
7791 mvpp2_write(priv, MVPP22_AXI_AGGRQ_DESCR_RD_ATTR_REG, rdval);
7792 mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_WR_ATTR_REG, wrval);
7793 mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_RD_ATTR_REG, rdval);
7794 mvpp2_write(priv, MVPP22_AXI_RXQ_DESCR_WR_ATTR_REG, wrval);
7795
7796 /* Buffer Data */
7797 mvpp2_write(priv, MVPP22_AXI_TX_DATA_RD_ATTR_REG, rdval);
7798 mvpp2_write(priv, MVPP22_AXI_RX_DATA_WR_ATTR_REG, wrval);
7799
7800 val = MVPP22_AXI_CODE_CACHE_NON_CACHE
7801 << MVPP22_AXI_CODE_CACHE_OFFS;
7802 val |= MVPP22_AXI_CODE_DOMAIN_SYSTEM
7803 << MVPP22_AXI_CODE_DOMAIN_OFFS;
7804 mvpp2_write(priv, MVPP22_AXI_RD_NORMAL_CODE_REG, val);
7805 mvpp2_write(priv, MVPP22_AXI_WR_NORMAL_CODE_REG, val);
7806
7807 val = MVPP22_AXI_CODE_CACHE_RD_CACHE
7808 << MVPP22_AXI_CODE_CACHE_OFFS;
7809 val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
7810 << MVPP22_AXI_CODE_DOMAIN_OFFS;
7811
7812 mvpp2_write(priv, MVPP22_AXI_RD_SNOOP_CODE_REG, val);
7813
7814 val = MVPP22_AXI_CODE_CACHE_WR_CACHE
7815 << MVPP22_AXI_CODE_CACHE_OFFS;
7816 val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
7817 << MVPP22_AXI_CODE_DOMAIN_OFFS;
7818
7819 mvpp2_write(priv, MVPP22_AXI_WR_SNOOP_CODE_REG, val);
7820}
7821
Marcin Wojtas3f518502014-07-10 16:52:13 -03007822/* Initialize network controller common part HW */
7823static int mvpp2_init(struct platform_device *pdev, struct mvpp2 *priv)
7824{
7825 const struct mbus_dram_target_info *dram_target_info;
7826 int err, i;
Marcin Wojtas08a23752014-07-21 13:48:12 -03007827 u32 val;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007828
Marcin Wojtas3f518502014-07-10 16:52:13 -03007829 /* MBUS windows configuration */
7830 dram_target_info = mv_mbus_dram_info();
7831 if (dram_target_info)
7832 mvpp2_conf_mbus_windows(dram_target_info, priv);
7833
Thomas Petazzoni6763ce32017-03-07 16:53:15 +01007834 if (priv->hw_version == MVPP22)
7835 mvpp2_axi_init(priv);
7836
Marcin Wojtas08a23752014-07-21 13:48:12 -03007837 /* Disable HW PHY polling */
Thomas Petazzoni26975822017-03-07 16:53:14 +01007838 if (priv->hw_version == MVPP21) {
7839 val = readl(priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
7840 val |= MVPP2_PHY_AN_STOP_SMI0_MASK;
7841 writel(val, priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
7842 } else {
7843 val = readl(priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
7844 val &= ~MVPP22_SMI_POLLING_EN;
7845 writel(val, priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
7846 }
Marcin Wojtas08a23752014-07-21 13:48:12 -03007847
Marcin Wojtas3f518502014-07-10 16:52:13 -03007848 /* Allocate and initialize aggregated TXQs */
7849 priv->aggr_txqs = devm_kcalloc(&pdev->dev, num_present_cpus(),
Markus Elfringd7ce3ce2017-04-17 08:48:23 +02007850 sizeof(*priv->aggr_txqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03007851 GFP_KERNEL);
7852 if (!priv->aggr_txqs)
7853 return -ENOMEM;
7854
7855 for_each_present_cpu(i) {
7856 priv->aggr_txqs[i].id = i;
7857 priv->aggr_txqs[i].size = MVPP2_AGGR_TXQ_SIZE;
Antoine Ténart85affd72017-08-23 09:46:55 +02007858 err = mvpp2_aggr_txq_init(pdev, &priv->aggr_txqs[i], i, priv);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007859 if (err < 0)
7860 return err;
7861 }
7862
7863 /* Rx Fifo Init */
7864 mvpp2_rx_fifo_init(priv);
7865
Thomas Petazzoni26975822017-03-07 16:53:14 +01007866 if (priv->hw_version == MVPP21)
7867 writel(MVPP2_EXT_GLOBAL_CTRL_DEFAULT,
7868 priv->lms_base + MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007869
7870 /* Allow cache snoop when transmiting packets */
7871 mvpp2_write(priv, MVPP2_TX_SNOOP_REG, 0x1);
7872
7873 /* Buffer Manager initialization */
7874 err = mvpp2_bm_init(pdev, priv);
7875 if (err < 0)
7876 return err;
7877
7878 /* Parser default initialization */
7879 err = mvpp2_prs_default_init(pdev, priv);
7880 if (err < 0)
7881 return err;
7882
7883 /* Classifier default initialization */
7884 mvpp2_cls_init(priv);
7885
7886 return 0;
7887}
7888
7889static int mvpp2_probe(struct platform_device *pdev)
7890{
7891 struct device_node *dn = pdev->dev.of_node;
7892 struct device_node *port_node;
7893 struct mvpp2 *priv;
7894 struct resource *res;
Thomas Petazzonia7868412017-03-07 16:53:13 +01007895 void __iomem *base;
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02007896 int port_count, i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007897 int err;
7898
Markus Elfring0b92e592017-04-17 08:38:32 +02007899 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007900 if (!priv)
7901 return -ENOMEM;
7902
Thomas Petazzonifaca9242017-03-07 16:53:06 +01007903 priv->hw_version =
7904 (unsigned long)of_device_get_match_data(&pdev->dev);
7905
Marcin Wojtas3f518502014-07-10 16:52:13 -03007906 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thomas Petazzonia7868412017-03-07 16:53:13 +01007907 base = devm_ioremap_resource(&pdev->dev, res);
7908 if (IS_ERR(base))
7909 return PTR_ERR(base);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007910
Thomas Petazzonia7868412017-03-07 16:53:13 +01007911 if (priv->hw_version == MVPP21) {
7912 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
7913 priv->lms_base = devm_ioremap_resource(&pdev->dev, res);
7914 if (IS_ERR(priv->lms_base))
7915 return PTR_ERR(priv->lms_base);
7916 } else {
7917 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
7918 priv->iface_base = devm_ioremap_resource(&pdev->dev, res);
7919 if (IS_ERR(priv->iface_base))
7920 return PTR_ERR(priv->iface_base);
Antoine Ténartf84bf382017-08-22 19:08:27 +02007921
7922 priv->sysctrl_base =
7923 syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
7924 "marvell,system-controller");
7925 if (IS_ERR(priv->sysctrl_base))
7926 /* The system controller regmap is optional for dt
7927 * compatibility reasons. When not provided, the
7928 * configuration of the GoP relies on the
7929 * firmware/bootloader.
7930 */
7931 priv->sysctrl_base = NULL;
Thomas Petazzonia7868412017-03-07 16:53:13 +01007932 }
7933
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02007934 for (i = 0; i < MVPP2_MAX_THREADS; i++) {
Thomas Petazzonia7868412017-03-07 16:53:13 +01007935 u32 addr_space_sz;
7936
7937 addr_space_sz = (priv->hw_version == MVPP21 ?
7938 MVPP21_ADDR_SPACE_SZ : MVPP22_ADDR_SPACE_SZ);
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02007939 priv->swth_base[i] = base + i * addr_space_sz;
Thomas Petazzonia7868412017-03-07 16:53:13 +01007940 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03007941
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01007942 if (priv->hw_version == MVPP21)
7943 priv->max_port_rxqs = 8;
7944 else
7945 priv->max_port_rxqs = 32;
7946
Marcin Wojtas3f518502014-07-10 16:52:13 -03007947 priv->pp_clk = devm_clk_get(&pdev->dev, "pp_clk");
7948 if (IS_ERR(priv->pp_clk))
7949 return PTR_ERR(priv->pp_clk);
7950 err = clk_prepare_enable(priv->pp_clk);
7951 if (err < 0)
7952 return err;
7953
7954 priv->gop_clk = devm_clk_get(&pdev->dev, "gop_clk");
7955 if (IS_ERR(priv->gop_clk)) {
7956 err = PTR_ERR(priv->gop_clk);
7957 goto err_pp_clk;
7958 }
7959 err = clk_prepare_enable(priv->gop_clk);
7960 if (err < 0)
7961 goto err_pp_clk;
7962
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01007963 if (priv->hw_version == MVPP22) {
7964 priv->mg_clk = devm_clk_get(&pdev->dev, "mg_clk");
7965 if (IS_ERR(priv->mg_clk)) {
7966 err = PTR_ERR(priv->mg_clk);
7967 goto err_gop_clk;
7968 }
7969
7970 err = clk_prepare_enable(priv->mg_clk);
7971 if (err < 0)
7972 goto err_gop_clk;
7973 }
7974
Marcin Wojtas3f518502014-07-10 16:52:13 -03007975 /* Get system's tclk rate */
7976 priv->tclk = clk_get_rate(priv->pp_clk);
7977
Thomas Petazzoni2067e0a2017-03-07 16:53:19 +01007978 if (priv->hw_version == MVPP22) {
7979 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(40));
7980 if (err)
7981 goto err_mg_clk;
7982 /* Sadly, the BM pools all share the same register to
7983 * store the high 32 bits of their address. So they
7984 * must all have the same high 32 bits, which forces
7985 * us to restrict coherent memory to DMA_BIT_MASK(32).
7986 */
7987 err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
7988 if (err)
7989 goto err_mg_clk;
7990 }
7991
Marcin Wojtas3f518502014-07-10 16:52:13 -03007992 /* Initialize network controller */
7993 err = mvpp2_init(pdev, priv);
7994 if (err < 0) {
7995 dev_err(&pdev->dev, "failed to initialize controller\n");
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01007996 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007997 }
7998
7999 port_count = of_get_available_child_count(dn);
8000 if (port_count == 0) {
8001 dev_err(&pdev->dev, "no ports enabled\n");
Wei Yongjun575a1932014-07-20 22:02:43 +08008002 err = -ENODEV;
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008003 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008004 }
8005
8006 priv->port_list = devm_kcalloc(&pdev->dev, port_count,
Markus Elfring0b92e592017-04-17 08:38:32 +02008007 sizeof(*priv->port_list),
8008 GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008009 if (!priv->port_list) {
8010 err = -ENOMEM;
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008011 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008012 }
8013
8014 /* Initialize ports */
Yan Markman6bf69a12017-09-25 14:59:47 +02008015 i = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008016 for_each_available_child_of_node(dn, port_node) {
Yan Markman6bf69a12017-09-25 14:59:47 +02008017 err = mvpp2_port_probe(pdev, port_node, priv, i);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008018 if (err < 0)
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008019 goto err_mg_clk;
Yan Markman6bf69a12017-09-25 14:59:47 +02008020 i++;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008021 }
8022
8023 platform_set_drvdata(pdev, priv);
8024 return 0;
8025
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008026err_mg_clk:
8027 if (priv->hw_version == MVPP22)
8028 clk_disable_unprepare(priv->mg_clk);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008029err_gop_clk:
8030 clk_disable_unprepare(priv->gop_clk);
8031err_pp_clk:
8032 clk_disable_unprepare(priv->pp_clk);
8033 return err;
8034}
8035
8036static int mvpp2_remove(struct platform_device *pdev)
8037{
8038 struct mvpp2 *priv = platform_get_drvdata(pdev);
8039 struct device_node *dn = pdev->dev.of_node;
8040 struct device_node *port_node;
8041 int i = 0;
8042
8043 for_each_available_child_of_node(dn, port_node) {
8044 if (priv->port_list[i])
8045 mvpp2_port_remove(priv->port_list[i]);
8046 i++;
8047 }
8048
8049 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
8050 struct mvpp2_bm_pool *bm_pool = &priv->bm_pools[i];
8051
8052 mvpp2_bm_pool_destroy(pdev, priv, bm_pool);
8053 }
8054
8055 for_each_present_cpu(i) {
8056 struct mvpp2_tx_queue *aggr_txq = &priv->aggr_txqs[i];
8057
8058 dma_free_coherent(&pdev->dev,
8059 MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
8060 aggr_txq->descs,
Thomas Petazzoni20396132017-03-07 16:53:00 +01008061 aggr_txq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008062 }
8063
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008064 clk_disable_unprepare(priv->mg_clk);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008065 clk_disable_unprepare(priv->pp_clk);
8066 clk_disable_unprepare(priv->gop_clk);
8067
8068 return 0;
8069}
8070
8071static const struct of_device_id mvpp2_match[] = {
Thomas Petazzonifaca9242017-03-07 16:53:06 +01008072 {
8073 .compatible = "marvell,armada-375-pp2",
8074 .data = (void *)MVPP21,
8075 },
Thomas Petazzonifc5e1552017-03-07 16:53:20 +01008076 {
8077 .compatible = "marvell,armada-7k-pp22",
8078 .data = (void *)MVPP22,
8079 },
Marcin Wojtas3f518502014-07-10 16:52:13 -03008080 { }
8081};
8082MODULE_DEVICE_TABLE(of, mvpp2_match);
8083
8084static struct platform_driver mvpp2_driver = {
8085 .probe = mvpp2_probe,
8086 .remove = mvpp2_remove,
8087 .driver = {
8088 .name = MVPP2_DRIVER_NAME,
8089 .of_match_table = mvpp2_match,
8090 },
8091};
8092
8093module_platform_driver(mvpp2_driver);
8094
8095MODULE_DESCRIPTION("Marvell PPv2 Ethernet Driver - www.marvell.com");
8096MODULE_AUTHOR("Marcin Wojtas <mw@semihalf.com>");
Ezequiel Garciac6340992014-07-14 10:34:47 -03008097MODULE_LICENSE("GPL v2");