blob: c8ce652a4dd787ed68f151c63e256f0ed5037988 [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;
Gregory CLEMENT4792ea02017-09-29 14:27:39 +0200796 struct clk *axi_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300797
798 /* List of pointers to port structures */
799 struct mvpp2_port **port_list;
800
801 /* Aggregated TXQs */
802 struct mvpp2_tx_queue *aggr_txqs;
803
804 /* BM pools */
805 struct mvpp2_bm_pool *bm_pools;
806
807 /* PRS shadow table */
808 struct mvpp2_prs_shadow *prs_shadow;
809 /* PRS auxiliary table for double vlan entries control */
810 bool *prs_double_vlans;
811
812 /* Tclk value */
813 u32 tclk;
Thomas Petazzonifaca9242017-03-07 16:53:06 +0100814
815 /* HW version */
816 enum { MVPP21, MVPP22 } hw_version;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +0100817
818 /* Maximum number of RXQs per port */
819 unsigned int max_port_rxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300820};
821
822struct mvpp2_pcpu_stats {
823 struct u64_stats_sync syncp;
824 u64 rx_packets;
825 u64 rx_bytes;
826 u64 tx_packets;
827 u64 tx_bytes;
828};
829
Marcin Wojtasedc660f2015-08-06 19:00:30 +0200830/* Per-CPU port control */
831struct mvpp2_port_pcpu {
832 struct hrtimer tx_done_timer;
833 bool timer_scheduled;
834 /* Tasklet for egress finalization */
835 struct tasklet_struct tx_done_tasklet;
836};
837
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +0200838struct mvpp2_queue_vector {
839 int irq;
840 struct napi_struct napi;
841 enum { MVPP2_QUEUE_VECTOR_SHARED, MVPP2_QUEUE_VECTOR_PRIVATE } type;
842 int sw_thread_id;
843 u16 sw_thread_mask;
844 int first_rxq;
845 int nrxqs;
846 u32 pending_cause_rx;
847 struct mvpp2_port *port;
848};
849
Marcin Wojtas3f518502014-07-10 16:52:13 -0300850struct mvpp2_port {
851 u8 id;
852
Thomas Petazzonia7868412017-03-07 16:53:13 +0100853 /* Index of the port from the "group of ports" complex point
854 * of view
855 */
856 int gop_id;
857
Antoine Tenartfd3651b2017-09-01 11:04:54 +0200858 int link_irq;
859
Marcin Wojtas3f518502014-07-10 16:52:13 -0300860 struct mvpp2 *priv;
861
862 /* Per-port registers' base address */
863 void __iomem *base;
864
865 struct mvpp2_rx_queue **rxqs;
Thomas Petazzoni09f83972017-08-03 10:41:57 +0200866 unsigned int nrxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300867 struct mvpp2_tx_queue **txqs;
Thomas Petazzoni09f83972017-08-03 10:41:57 +0200868 unsigned int ntxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300869 struct net_device *dev;
870
871 int pkt_size;
872
Marcin Wojtasedc660f2015-08-06 19:00:30 +0200873 /* Per-CPU port control */
874 struct mvpp2_port_pcpu __percpu *pcpu;
875
Marcin Wojtas3f518502014-07-10 16:52:13 -0300876 /* Flags */
877 unsigned long flags;
878
879 u16 tx_ring_size;
880 u16 rx_ring_size;
881 struct mvpp2_pcpu_stats __percpu *stats;
882
Marcin Wojtas3f518502014-07-10 16:52:13 -0300883 phy_interface_t phy_interface;
884 struct device_node *phy_node;
Antoine Tenart542897d2017-08-30 10:29:15 +0200885 struct phy *comphy;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300886 unsigned int link;
887 unsigned int duplex;
888 unsigned int speed;
889
890 struct mvpp2_bm_pool *pool_long;
891 struct mvpp2_bm_pool *pool_short;
892
893 /* Index of first port's physical RXQ */
894 u8 first_rxq;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +0200895
896 struct mvpp2_queue_vector qvecs[MVPP2_MAX_QVECS];
897 unsigned int nqvecs;
Thomas Petazzoni213f4282017-08-03 10:42:00 +0200898 bool has_tx_irqs;
899
900 u32 tx_time_coal;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300901};
902
903/* The mvpp2_tx_desc and mvpp2_rx_desc structures describe the
904 * layout of the transmit and reception DMA descriptors, and their
905 * layout is therefore defined by the hardware design
906 */
907
908#define MVPP2_TXD_L3_OFF_SHIFT 0
909#define MVPP2_TXD_IP_HLEN_SHIFT 8
910#define MVPP2_TXD_L4_CSUM_FRAG BIT(13)
911#define MVPP2_TXD_L4_CSUM_NOT BIT(14)
912#define MVPP2_TXD_IP_CSUM_DISABLE BIT(15)
913#define MVPP2_TXD_PADDING_DISABLE BIT(23)
914#define MVPP2_TXD_L4_UDP BIT(24)
915#define MVPP2_TXD_L3_IP6 BIT(26)
916#define MVPP2_TXD_L_DESC BIT(28)
917#define MVPP2_TXD_F_DESC BIT(29)
918
919#define MVPP2_RXD_ERR_SUMMARY BIT(15)
920#define MVPP2_RXD_ERR_CODE_MASK (BIT(13) | BIT(14))
921#define MVPP2_RXD_ERR_CRC 0x0
922#define MVPP2_RXD_ERR_OVERRUN BIT(13)
923#define MVPP2_RXD_ERR_RESOURCE (BIT(13) | BIT(14))
924#define MVPP2_RXD_BM_POOL_ID_OFFS 16
925#define MVPP2_RXD_BM_POOL_ID_MASK (BIT(16) | BIT(17) | BIT(18))
926#define MVPP2_RXD_HWF_SYNC BIT(21)
927#define MVPP2_RXD_L4_CSUM_OK BIT(22)
928#define MVPP2_RXD_IP4_HEADER_ERR BIT(24)
929#define MVPP2_RXD_L4_TCP BIT(25)
930#define MVPP2_RXD_L4_UDP BIT(26)
931#define MVPP2_RXD_L3_IP4 BIT(28)
932#define MVPP2_RXD_L3_IP6 BIT(30)
933#define MVPP2_RXD_BUF_HDR BIT(31)
934
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100935/* HW TX descriptor for PPv2.1 */
936struct mvpp21_tx_desc {
Marcin Wojtas3f518502014-07-10 16:52:13 -0300937 u32 command; /* Options used by HW for packet transmitting.*/
938 u8 packet_offset; /* the offset from the buffer beginning */
939 u8 phys_txq; /* destination queue ID */
940 u16 data_size; /* data size of transmitted packet in bytes */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100941 u32 buf_dma_addr; /* physical addr of transmitted buffer */
Marcin Wojtas3f518502014-07-10 16:52:13 -0300942 u32 buf_cookie; /* cookie for access to TX buffer in tx path */
943 u32 reserved1[3]; /* hw_cmd (for future use, BM, PON, PNC) */
944 u32 reserved2; /* reserved (for future use) */
945};
946
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100947/* HW RX descriptor for PPv2.1 */
948struct mvpp21_rx_desc {
Marcin Wojtas3f518502014-07-10 16:52:13 -0300949 u32 status; /* info about received packet */
950 u16 reserved1; /* parser_info (for future use, PnC) */
951 u16 data_size; /* size of received packet in bytes */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100952 u32 buf_dma_addr; /* physical address of the buffer */
Marcin Wojtas3f518502014-07-10 16:52:13 -0300953 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
954 u16 reserved2; /* gem_port_id (for future use, PON) */
955 u16 reserved3; /* csum_l4 (for future use, PnC) */
956 u8 reserved4; /* bm_qset (for future use, BM) */
957 u8 reserved5;
958 u16 reserved6; /* classify_info (for future use, PnC) */
959 u32 reserved7; /* flow_id (for future use, PnC) */
960 u32 reserved8;
961};
962
Thomas Petazzonie7c53592017-03-07 16:53:08 +0100963/* HW TX descriptor for PPv2.2 */
964struct mvpp22_tx_desc {
965 u32 command;
966 u8 packet_offset;
967 u8 phys_txq;
968 u16 data_size;
969 u64 reserved1;
970 u64 buf_dma_addr_ptp;
971 u64 buf_cookie_misc;
972};
973
974/* HW RX descriptor for PPv2.2 */
975struct mvpp22_rx_desc {
976 u32 status;
977 u16 reserved1;
978 u16 data_size;
979 u32 reserved2;
980 u32 reserved3;
981 u64 buf_dma_addr_key_hash;
982 u64 buf_cookie_misc;
983};
984
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100985/* Opaque type used by the driver to manipulate the HW TX and RX
986 * descriptors
987 */
988struct mvpp2_tx_desc {
989 union {
990 struct mvpp21_tx_desc pp21;
Thomas Petazzonie7c53592017-03-07 16:53:08 +0100991 struct mvpp22_tx_desc pp22;
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100992 };
993};
994
995struct mvpp2_rx_desc {
996 union {
997 struct mvpp21_rx_desc pp21;
Thomas Petazzonie7c53592017-03-07 16:53:08 +0100998 struct mvpp22_rx_desc pp22;
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100999 };
1000};
1001
Thomas Petazzoni83544912016-12-21 11:28:49 +01001002struct mvpp2_txq_pcpu_buf {
1003 /* Transmitted SKB */
1004 struct sk_buff *skb;
1005
1006 /* Physical address of transmitted buffer */
Thomas Petazzoni20396132017-03-07 16:53:00 +01001007 dma_addr_t dma;
Thomas Petazzoni83544912016-12-21 11:28:49 +01001008
1009 /* Size transmitted */
1010 size_t size;
1011};
1012
Marcin Wojtas3f518502014-07-10 16:52:13 -03001013/* Per-CPU Tx queue control */
1014struct mvpp2_txq_pcpu {
1015 int cpu;
1016
1017 /* Number of Tx DMA descriptors in the descriptor ring */
1018 int size;
1019
1020 /* Number of currently used Tx DMA descriptor in the
1021 * descriptor ring
1022 */
1023 int count;
1024
1025 /* Number of Tx DMA descriptors reserved for each CPU */
1026 int reserved_num;
1027
Thomas Petazzoni83544912016-12-21 11:28:49 +01001028 /* Infos about transmitted buffers */
1029 struct mvpp2_txq_pcpu_buf *buffs;
Marcin Wojtas71ce3912015-08-06 19:00:29 +02001030
Marcin Wojtas3f518502014-07-10 16:52:13 -03001031 /* Index of last TX DMA descriptor that was inserted */
1032 int txq_put_index;
1033
1034 /* Index of the TX DMA descriptor to be cleaned up */
1035 int txq_get_index;
Antoine Ténart186cd4d2017-08-23 09:46:56 +02001036
1037 /* DMA buffer for TSO headers */
1038 char *tso_headers;
1039 dma_addr_t tso_headers_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001040};
1041
1042struct mvpp2_tx_queue {
1043 /* Physical number of this Tx queue */
1044 u8 id;
1045
1046 /* Logical number of this Tx queue */
1047 u8 log_id;
1048
1049 /* Number of Tx DMA descriptors in the descriptor ring */
1050 int size;
1051
1052 /* Number of currently used Tx DMA descriptor in the descriptor ring */
1053 int count;
1054
1055 /* Per-CPU control of physical Tx queues */
1056 struct mvpp2_txq_pcpu __percpu *pcpu;
1057
Marcin Wojtas3f518502014-07-10 16:52:13 -03001058 u32 done_pkts_coal;
1059
1060 /* Virtual address of thex Tx DMA descriptors array */
1061 struct mvpp2_tx_desc *descs;
1062
1063 /* DMA address of the Tx DMA descriptors array */
Thomas Petazzoni20396132017-03-07 16:53:00 +01001064 dma_addr_t descs_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001065
1066 /* Index of the last Tx DMA descriptor */
1067 int last_desc;
1068
1069 /* Index of the next Tx DMA descriptor to process */
1070 int next_desc_to_proc;
1071};
1072
1073struct mvpp2_rx_queue {
1074 /* RX queue number, in the range 0-31 for physical RXQs */
1075 u8 id;
1076
1077 /* Num of rx descriptors in the rx descriptor ring */
1078 int size;
1079
1080 u32 pkts_coal;
1081 u32 time_coal;
1082
1083 /* Virtual address of the RX DMA descriptors array */
1084 struct mvpp2_rx_desc *descs;
1085
1086 /* DMA address of the RX DMA descriptors array */
Thomas Petazzoni20396132017-03-07 16:53:00 +01001087 dma_addr_t descs_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001088
1089 /* Index of the last RX DMA descriptor */
1090 int last_desc;
1091
1092 /* Index of the next RX DMA descriptor to process */
1093 int next_desc_to_proc;
1094
1095 /* ID of port to which physical RXQ is mapped */
1096 int port;
1097
1098 /* Port's logic RXQ number to which physical RXQ is mapped */
1099 int logic_rxq;
1100};
1101
1102union mvpp2_prs_tcam_entry {
1103 u32 word[MVPP2_PRS_TCAM_WORDS];
1104 u8 byte[MVPP2_PRS_TCAM_WORDS * 4];
1105};
1106
1107union mvpp2_prs_sram_entry {
1108 u32 word[MVPP2_PRS_SRAM_WORDS];
1109 u8 byte[MVPP2_PRS_SRAM_WORDS * 4];
1110};
1111
1112struct mvpp2_prs_entry {
1113 u32 index;
1114 union mvpp2_prs_tcam_entry tcam;
1115 union mvpp2_prs_sram_entry sram;
1116};
1117
1118struct mvpp2_prs_shadow {
1119 bool valid;
1120 bool finish;
1121
1122 /* Lookup ID */
1123 int lu;
1124
1125 /* User defined offset */
1126 int udf;
1127
1128 /* Result info */
1129 u32 ri;
1130 u32 ri_mask;
1131};
1132
1133struct mvpp2_cls_flow_entry {
1134 u32 index;
1135 u32 data[MVPP2_CLS_FLOWS_TBL_DATA_WORDS];
1136};
1137
1138struct mvpp2_cls_lookup_entry {
1139 u32 lkpid;
1140 u32 way;
1141 u32 data;
1142};
1143
1144struct mvpp2_bm_pool {
1145 /* Pool number in the range 0-7 */
1146 int id;
1147 enum mvpp2_bm_type type;
1148
1149 /* Buffer Pointers Pool External (BPPE) size */
1150 int size;
Thomas Petazzonid01524d2017-03-07 16:53:09 +01001151 /* BPPE size in bytes */
1152 int size_bytes;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001153 /* Number of buffers for this pool */
1154 int buf_num;
1155 /* Pool buffer size */
1156 int buf_size;
1157 /* Packet size */
1158 int pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01001159 int frag_size;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001160
1161 /* BPPE virtual base address */
1162 u32 *virt_addr;
Thomas Petazzoni20396132017-03-07 16:53:00 +01001163 /* BPPE DMA base address */
1164 dma_addr_t dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001165
1166 /* Ports using BM pool */
1167 u32 port_map;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001168};
1169
Antoine Tenart20920262017-10-23 15:24:30 +02001170#define IS_TSO_HEADER(txq_pcpu, addr) \
1171 ((addr) >= (txq_pcpu)->tso_headers_dma && \
1172 (addr) < (txq_pcpu)->tso_headers_dma + \
1173 (txq_pcpu)->size * TSO_HEADER_SIZE)
1174
Thomas Petazzoni213f4282017-08-03 10:42:00 +02001175/* Queue modes */
1176#define MVPP2_QDIST_SINGLE_MODE 0
1177#define MVPP2_QDIST_MULTI_MODE 1
1178
1179static int queue_mode = MVPP2_QDIST_SINGLE_MODE;
1180
1181module_param(queue_mode, int, 0444);
1182MODULE_PARM_DESC(queue_mode, "Set queue_mode (single=0, multi=1)");
1183
Marcin Wojtas3f518502014-07-10 16:52:13 -03001184#define MVPP2_DRIVER_NAME "mvpp2"
1185#define MVPP2_DRIVER_VERSION "1.0"
1186
1187/* Utility/helper methods */
1188
1189static void mvpp2_write(struct mvpp2 *priv, u32 offset, u32 data)
1190{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001191 writel(data, priv->swth_base[0] + offset);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001192}
1193
1194static u32 mvpp2_read(struct mvpp2 *priv, u32 offset)
1195{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001196 return readl(priv->swth_base[0] + offset);
Thomas Petazzonia7868412017-03-07 16:53:13 +01001197}
1198
1199/* These accessors should be used to access:
1200 *
1201 * - per-CPU registers, where each CPU has its own copy of the
1202 * register.
1203 *
1204 * MVPP2_BM_VIRT_ALLOC_REG
1205 * MVPP2_BM_ADDR_HIGH_ALLOC
1206 * MVPP22_BM_ADDR_HIGH_RLS_REG
1207 * MVPP2_BM_VIRT_RLS_REG
1208 * MVPP2_ISR_RX_TX_CAUSE_REG
1209 * MVPP2_ISR_RX_TX_MASK_REG
1210 * MVPP2_TXQ_NUM_REG
1211 * MVPP2_AGGR_TXQ_UPDATE_REG
1212 * MVPP2_TXQ_RSVD_REQ_REG
1213 * MVPP2_TXQ_RSVD_RSLT_REG
1214 * MVPP2_TXQ_SENT_REG
1215 * MVPP2_RXQ_NUM_REG
1216 *
1217 * - global registers that must be accessed through a specific CPU
1218 * window, because they are related to an access to a per-CPU
1219 * register
1220 *
1221 * MVPP2_BM_PHY_ALLOC_REG (related to MVPP2_BM_VIRT_ALLOC_REG)
1222 * MVPP2_BM_PHY_RLS_REG (related to MVPP2_BM_VIRT_RLS_REG)
1223 * MVPP2_RXQ_THRESH_REG (related to MVPP2_RXQ_NUM_REG)
1224 * MVPP2_RXQ_DESC_ADDR_REG (related to MVPP2_RXQ_NUM_REG)
1225 * MVPP2_RXQ_DESC_SIZE_REG (related to MVPP2_RXQ_NUM_REG)
1226 * MVPP2_RXQ_INDEX_REG (related to MVPP2_RXQ_NUM_REG)
1227 * MVPP2_TXQ_PENDING_REG (related to MVPP2_TXQ_NUM_REG)
1228 * MVPP2_TXQ_DESC_ADDR_REG (related to MVPP2_TXQ_NUM_REG)
1229 * MVPP2_TXQ_DESC_SIZE_REG (related to MVPP2_TXQ_NUM_REG)
1230 * MVPP2_TXQ_INDEX_REG (related to MVPP2_TXQ_NUM_REG)
1231 * MVPP2_TXQ_PENDING_REG (related to MVPP2_TXQ_NUM_REG)
1232 * MVPP2_TXQ_PREF_BUF_REG (related to MVPP2_TXQ_NUM_REG)
1233 * MVPP2_TXQ_PREF_BUF_REG (related to MVPP2_TXQ_NUM_REG)
1234 */
1235static void mvpp2_percpu_write(struct mvpp2 *priv, int cpu,
1236 u32 offset, u32 data)
1237{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001238 writel(data, priv->swth_base[cpu] + offset);
Thomas Petazzonia7868412017-03-07 16:53:13 +01001239}
1240
1241static u32 mvpp2_percpu_read(struct mvpp2 *priv, int cpu,
1242 u32 offset)
1243{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001244 return readl(priv->swth_base[cpu] + offset);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001245}
1246
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001247static dma_addr_t mvpp2_txdesc_dma_addr_get(struct mvpp2_port *port,
1248 struct mvpp2_tx_desc *tx_desc)
1249{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001250 if (port->priv->hw_version == MVPP21)
1251 return tx_desc->pp21.buf_dma_addr;
1252 else
1253 return tx_desc->pp22.buf_dma_addr_ptp & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001254}
1255
1256static void mvpp2_txdesc_dma_addr_set(struct mvpp2_port *port,
1257 struct mvpp2_tx_desc *tx_desc,
1258 dma_addr_t dma_addr)
1259{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001260 if (port->priv->hw_version == MVPP21) {
1261 tx_desc->pp21.buf_dma_addr = dma_addr;
1262 } else {
1263 u64 val = (u64)dma_addr;
1264
1265 tx_desc->pp22.buf_dma_addr_ptp &= ~GENMASK_ULL(40, 0);
1266 tx_desc->pp22.buf_dma_addr_ptp |= val;
1267 }
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001268}
1269
1270static size_t mvpp2_txdesc_size_get(struct mvpp2_port *port,
1271 struct mvpp2_tx_desc *tx_desc)
1272{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001273 if (port->priv->hw_version == MVPP21)
1274 return tx_desc->pp21.data_size;
1275 else
1276 return tx_desc->pp22.data_size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001277}
1278
1279static void mvpp2_txdesc_size_set(struct mvpp2_port *port,
1280 struct mvpp2_tx_desc *tx_desc,
1281 size_t size)
1282{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001283 if (port->priv->hw_version == MVPP21)
1284 tx_desc->pp21.data_size = size;
1285 else
1286 tx_desc->pp22.data_size = size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001287}
1288
1289static void mvpp2_txdesc_txq_set(struct mvpp2_port *port,
1290 struct mvpp2_tx_desc *tx_desc,
1291 unsigned int txq)
1292{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001293 if (port->priv->hw_version == MVPP21)
1294 tx_desc->pp21.phys_txq = txq;
1295 else
1296 tx_desc->pp22.phys_txq = txq;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001297}
1298
1299static void mvpp2_txdesc_cmd_set(struct mvpp2_port *port,
1300 struct mvpp2_tx_desc *tx_desc,
1301 unsigned int command)
1302{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001303 if (port->priv->hw_version == MVPP21)
1304 tx_desc->pp21.command = command;
1305 else
1306 tx_desc->pp22.command = command;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001307}
1308
1309static void mvpp2_txdesc_offset_set(struct mvpp2_port *port,
1310 struct mvpp2_tx_desc *tx_desc,
1311 unsigned int offset)
1312{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001313 if (port->priv->hw_version == MVPP21)
1314 tx_desc->pp21.packet_offset = offset;
1315 else
1316 tx_desc->pp22.packet_offset = offset;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001317}
1318
1319static unsigned int mvpp2_txdesc_offset_get(struct mvpp2_port *port,
1320 struct mvpp2_tx_desc *tx_desc)
1321{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001322 if (port->priv->hw_version == MVPP21)
1323 return tx_desc->pp21.packet_offset;
1324 else
1325 return tx_desc->pp22.packet_offset;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001326}
1327
1328static dma_addr_t mvpp2_rxdesc_dma_addr_get(struct mvpp2_port *port,
1329 struct mvpp2_rx_desc *rx_desc)
1330{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001331 if (port->priv->hw_version == MVPP21)
1332 return rx_desc->pp21.buf_dma_addr;
1333 else
1334 return rx_desc->pp22.buf_dma_addr_key_hash & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001335}
1336
1337static unsigned long mvpp2_rxdesc_cookie_get(struct mvpp2_port *port,
1338 struct mvpp2_rx_desc *rx_desc)
1339{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001340 if (port->priv->hw_version == MVPP21)
1341 return rx_desc->pp21.buf_cookie;
1342 else
1343 return rx_desc->pp22.buf_cookie_misc & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001344}
1345
1346static size_t mvpp2_rxdesc_size_get(struct mvpp2_port *port,
1347 struct mvpp2_rx_desc *rx_desc)
1348{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001349 if (port->priv->hw_version == MVPP21)
1350 return rx_desc->pp21.data_size;
1351 else
1352 return rx_desc->pp22.data_size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001353}
1354
1355static u32 mvpp2_rxdesc_status_get(struct mvpp2_port *port,
1356 struct mvpp2_rx_desc *rx_desc)
1357{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001358 if (port->priv->hw_version == MVPP21)
1359 return rx_desc->pp21.status;
1360 else
1361 return rx_desc->pp22.status;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001362}
1363
Marcin Wojtas3f518502014-07-10 16:52:13 -03001364static void mvpp2_txq_inc_get(struct mvpp2_txq_pcpu *txq_pcpu)
1365{
1366 txq_pcpu->txq_get_index++;
1367 if (txq_pcpu->txq_get_index == txq_pcpu->size)
1368 txq_pcpu->txq_get_index = 0;
1369}
1370
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001371static void mvpp2_txq_inc_put(struct mvpp2_port *port,
1372 struct mvpp2_txq_pcpu *txq_pcpu,
Marcin Wojtas71ce3912015-08-06 19:00:29 +02001373 struct sk_buff *skb,
1374 struct mvpp2_tx_desc *tx_desc)
Marcin Wojtas3f518502014-07-10 16:52:13 -03001375{
Thomas Petazzoni83544912016-12-21 11:28:49 +01001376 struct mvpp2_txq_pcpu_buf *tx_buf =
1377 txq_pcpu->buffs + txq_pcpu->txq_put_index;
1378 tx_buf->skb = skb;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001379 tx_buf->size = mvpp2_txdesc_size_get(port, tx_desc);
1380 tx_buf->dma = mvpp2_txdesc_dma_addr_get(port, tx_desc) +
1381 mvpp2_txdesc_offset_get(port, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001382 txq_pcpu->txq_put_index++;
1383 if (txq_pcpu->txq_put_index == txq_pcpu->size)
1384 txq_pcpu->txq_put_index = 0;
1385}
1386
1387/* Get number of physical egress port */
1388static inline int mvpp2_egress_port(struct mvpp2_port *port)
1389{
1390 return MVPP2_MAX_TCONT + port->id;
1391}
1392
1393/* Get number of physical TXQ */
1394static inline int mvpp2_txq_phys(int port, int txq)
1395{
1396 return (MVPP2_MAX_TCONT + port) * MVPP2_MAX_TXQ + txq;
1397}
1398
1399/* Parser configuration routines */
1400
1401/* Update parser tcam and sram hw entries */
1402static int mvpp2_prs_hw_write(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1403{
1404 int i;
1405
1406 if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1407 return -EINVAL;
1408
1409 /* Clear entry invalidation bit */
1410 pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] &= ~MVPP2_PRS_TCAM_INV_MASK;
1411
1412 /* Write tcam index - indirect access */
1413 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1414 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1415 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), pe->tcam.word[i]);
1416
1417 /* Write sram index - indirect access */
1418 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1419 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1420 mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), pe->sram.word[i]);
1421
1422 return 0;
1423}
1424
1425/* Read tcam entry from hw */
1426static int mvpp2_prs_hw_read(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1427{
1428 int i;
1429
1430 if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1431 return -EINVAL;
1432
1433 /* Write tcam index - indirect access */
1434 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1435
1436 pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] = mvpp2_read(priv,
1437 MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD));
1438 if (pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] & MVPP2_PRS_TCAM_INV_MASK)
1439 return MVPP2_PRS_TCAM_ENTRY_INVALID;
1440
1441 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1442 pe->tcam.word[i] = mvpp2_read(priv, MVPP2_PRS_TCAM_DATA_REG(i));
1443
1444 /* Write sram index - indirect access */
1445 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1446 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1447 pe->sram.word[i] = mvpp2_read(priv, MVPP2_PRS_SRAM_DATA_REG(i));
1448
1449 return 0;
1450}
1451
1452/* Invalidate tcam hw entry */
1453static void mvpp2_prs_hw_inv(struct mvpp2 *priv, int index)
1454{
1455 /* Write index - indirect access */
1456 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
1457 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD),
1458 MVPP2_PRS_TCAM_INV_MASK);
1459}
1460
1461/* Enable shadow table entry and set its lookup ID */
1462static void mvpp2_prs_shadow_set(struct mvpp2 *priv, int index, int lu)
1463{
1464 priv->prs_shadow[index].valid = true;
1465 priv->prs_shadow[index].lu = lu;
1466}
1467
1468/* Update ri fields in shadow table entry */
1469static void mvpp2_prs_shadow_ri_set(struct mvpp2 *priv, int index,
1470 unsigned int ri, unsigned int ri_mask)
1471{
1472 priv->prs_shadow[index].ri_mask = ri_mask;
1473 priv->prs_shadow[index].ri = ri;
1474}
1475
1476/* Update lookup field in tcam sw entry */
1477static void mvpp2_prs_tcam_lu_set(struct mvpp2_prs_entry *pe, unsigned int lu)
1478{
1479 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_LU_BYTE);
1480
1481 pe->tcam.byte[MVPP2_PRS_TCAM_LU_BYTE] = lu;
1482 pe->tcam.byte[enable_off] = MVPP2_PRS_LU_MASK;
1483}
1484
1485/* Update mask for single port in tcam sw entry */
1486static void mvpp2_prs_tcam_port_set(struct mvpp2_prs_entry *pe,
1487 unsigned int port, bool add)
1488{
1489 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1490
1491 if (add)
1492 pe->tcam.byte[enable_off] &= ~(1 << port);
1493 else
1494 pe->tcam.byte[enable_off] |= 1 << port;
1495}
1496
1497/* Update port map in tcam sw entry */
1498static void mvpp2_prs_tcam_port_map_set(struct mvpp2_prs_entry *pe,
1499 unsigned int ports)
1500{
1501 unsigned char port_mask = MVPP2_PRS_PORT_MASK;
1502 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1503
1504 pe->tcam.byte[MVPP2_PRS_TCAM_PORT_BYTE] = 0;
1505 pe->tcam.byte[enable_off] &= ~port_mask;
1506 pe->tcam.byte[enable_off] |= ~ports & MVPP2_PRS_PORT_MASK;
1507}
1508
1509/* Obtain port map from tcam sw entry */
1510static unsigned int mvpp2_prs_tcam_port_map_get(struct mvpp2_prs_entry *pe)
1511{
1512 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1513
1514 return ~(pe->tcam.byte[enable_off]) & MVPP2_PRS_PORT_MASK;
1515}
1516
1517/* Set byte of data and its enable bits in tcam sw entry */
1518static void mvpp2_prs_tcam_data_byte_set(struct mvpp2_prs_entry *pe,
1519 unsigned int offs, unsigned char byte,
1520 unsigned char enable)
1521{
1522 pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)] = byte;
1523 pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)] = enable;
1524}
1525
1526/* Get byte of data and its enable bits from tcam sw entry */
1527static void mvpp2_prs_tcam_data_byte_get(struct mvpp2_prs_entry *pe,
1528 unsigned int offs, unsigned char *byte,
1529 unsigned char *enable)
1530{
1531 *byte = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)];
1532 *enable = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)];
1533}
1534
1535/* Compare tcam data bytes with a pattern */
1536static bool mvpp2_prs_tcam_data_cmp(struct mvpp2_prs_entry *pe, int offs,
1537 u16 data)
1538{
1539 int off = MVPP2_PRS_TCAM_DATA_BYTE(offs);
1540 u16 tcam_data;
1541
Antoine Tenartef4816f2017-10-24 11:41:26 +02001542 tcam_data = (pe->tcam.byte[off + 1] << 8) | pe->tcam.byte[off];
Marcin Wojtas3f518502014-07-10 16:52:13 -03001543 if (tcam_data != data)
1544 return false;
1545 return true;
1546}
1547
1548/* Update ai bits in tcam sw entry */
1549static void mvpp2_prs_tcam_ai_update(struct mvpp2_prs_entry *pe,
1550 unsigned int bits, unsigned int enable)
1551{
1552 int i, ai_idx = MVPP2_PRS_TCAM_AI_BYTE;
1553
1554 for (i = 0; i < MVPP2_PRS_AI_BITS; i++) {
1555
1556 if (!(enable & BIT(i)))
1557 continue;
1558
1559 if (bits & BIT(i))
1560 pe->tcam.byte[ai_idx] |= 1 << i;
1561 else
1562 pe->tcam.byte[ai_idx] &= ~(1 << i);
1563 }
1564
1565 pe->tcam.byte[MVPP2_PRS_TCAM_EN_OFFS(ai_idx)] |= enable;
1566}
1567
1568/* Get ai bits from tcam sw entry */
1569static int mvpp2_prs_tcam_ai_get(struct mvpp2_prs_entry *pe)
1570{
1571 return pe->tcam.byte[MVPP2_PRS_TCAM_AI_BYTE];
1572}
1573
1574/* Set ethertype in tcam sw entry */
1575static void mvpp2_prs_match_etype(struct mvpp2_prs_entry *pe, int offset,
1576 unsigned short ethertype)
1577{
1578 mvpp2_prs_tcam_data_byte_set(pe, offset + 0, ethertype >> 8, 0xff);
1579 mvpp2_prs_tcam_data_byte_set(pe, offset + 1, ethertype & 0xff, 0xff);
1580}
1581
1582/* Set bits in sram sw entry */
1583static void mvpp2_prs_sram_bits_set(struct mvpp2_prs_entry *pe, int bit_num,
1584 int val)
1585{
1586 pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] |= (val << (bit_num % 8));
1587}
1588
1589/* Clear bits in sram sw entry */
1590static void mvpp2_prs_sram_bits_clear(struct mvpp2_prs_entry *pe, int bit_num,
1591 int val)
1592{
1593 pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] &= ~(val << (bit_num % 8));
1594}
1595
1596/* Update ri bits in sram sw entry */
1597static void mvpp2_prs_sram_ri_update(struct mvpp2_prs_entry *pe,
1598 unsigned int bits, unsigned int mask)
1599{
1600 unsigned int i;
1601
1602 for (i = 0; i < MVPP2_PRS_SRAM_RI_CTRL_BITS; i++) {
1603 int ri_off = MVPP2_PRS_SRAM_RI_OFFS;
1604
1605 if (!(mask & BIT(i)))
1606 continue;
1607
1608 if (bits & BIT(i))
1609 mvpp2_prs_sram_bits_set(pe, ri_off + i, 1);
1610 else
1611 mvpp2_prs_sram_bits_clear(pe, ri_off + i, 1);
1612
1613 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_RI_CTRL_OFFS + i, 1);
1614 }
1615}
1616
1617/* Obtain ri bits from sram sw entry */
1618static int mvpp2_prs_sram_ri_get(struct mvpp2_prs_entry *pe)
1619{
1620 return pe->sram.word[MVPP2_PRS_SRAM_RI_WORD];
1621}
1622
1623/* Update ai bits in sram sw entry */
1624static void mvpp2_prs_sram_ai_update(struct mvpp2_prs_entry *pe,
1625 unsigned int bits, unsigned int mask)
1626{
1627 unsigned int i;
1628 int ai_off = MVPP2_PRS_SRAM_AI_OFFS;
1629
1630 for (i = 0; i < MVPP2_PRS_SRAM_AI_CTRL_BITS; i++) {
1631
1632 if (!(mask & BIT(i)))
1633 continue;
1634
1635 if (bits & BIT(i))
1636 mvpp2_prs_sram_bits_set(pe, ai_off + i, 1);
1637 else
1638 mvpp2_prs_sram_bits_clear(pe, ai_off + i, 1);
1639
1640 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_AI_CTRL_OFFS + i, 1);
1641 }
1642}
1643
1644/* Read ai bits from sram sw entry */
1645static int mvpp2_prs_sram_ai_get(struct mvpp2_prs_entry *pe)
1646{
1647 u8 bits;
1648 int ai_off = MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_AI_OFFS);
1649 int ai_en_off = ai_off + 1;
1650 int ai_shift = MVPP2_PRS_SRAM_AI_OFFS % 8;
1651
1652 bits = (pe->sram.byte[ai_off] >> ai_shift) |
1653 (pe->sram.byte[ai_en_off] << (8 - ai_shift));
1654
1655 return bits;
1656}
1657
1658/* In sram sw entry set lookup ID field of the tcam key to be used in the next
1659 * lookup interation
1660 */
1661static void mvpp2_prs_sram_next_lu_set(struct mvpp2_prs_entry *pe,
1662 unsigned int lu)
1663{
1664 int sram_next_off = MVPP2_PRS_SRAM_NEXT_LU_OFFS;
1665
1666 mvpp2_prs_sram_bits_clear(pe, sram_next_off,
1667 MVPP2_PRS_SRAM_NEXT_LU_MASK);
1668 mvpp2_prs_sram_bits_set(pe, sram_next_off, lu);
1669}
1670
1671/* In the sram sw entry set sign and value of the next lookup offset
1672 * and the offset value generated to the classifier
1673 */
1674static void mvpp2_prs_sram_shift_set(struct mvpp2_prs_entry *pe, int shift,
1675 unsigned int op)
1676{
1677 /* Set sign */
1678 if (shift < 0) {
1679 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1680 shift = 0 - shift;
1681 } else {
1682 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1683 }
1684
1685 /* Set value */
1686 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_SHIFT_OFFS)] =
1687 (unsigned char)shift;
1688
1689 /* Reset and set operation */
1690 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS,
1691 MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK);
1692 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS, op);
1693
1694 /* Set base offset as current */
1695 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1696}
1697
1698/* In the sram sw entry set sign and value of the user defined offset
1699 * generated to the classifier
1700 */
1701static void mvpp2_prs_sram_offset_set(struct mvpp2_prs_entry *pe,
1702 unsigned int type, int offset,
1703 unsigned int op)
1704{
1705 /* Set sign */
1706 if (offset < 0) {
1707 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1708 offset = 0 - offset;
1709 } else {
1710 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1711 }
1712
1713 /* Set value */
1714 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_OFFS,
1715 MVPP2_PRS_SRAM_UDF_MASK);
1716 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_OFFS, offset);
1717 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1718 MVPP2_PRS_SRAM_UDF_BITS)] &=
1719 ~(MVPP2_PRS_SRAM_UDF_MASK >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1720 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1721 MVPP2_PRS_SRAM_UDF_BITS)] |=
1722 (offset >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1723
1724 /* Set offset type */
1725 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS,
1726 MVPP2_PRS_SRAM_UDF_TYPE_MASK);
1727 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS, type);
1728
1729 /* Set offset operation */
1730 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS,
1731 MVPP2_PRS_SRAM_OP_SEL_UDF_MASK);
1732 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS, op);
1733
1734 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1735 MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] &=
1736 ~(MVPP2_PRS_SRAM_OP_SEL_UDF_MASK >>
1737 (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1738
1739 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1740 MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] |=
1741 (op >> (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1742
1743 /* Set base offset as current */
1744 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1745}
1746
1747/* Find parser flow entry */
1748static struct mvpp2_prs_entry *mvpp2_prs_flow_find(struct mvpp2 *priv, int flow)
1749{
1750 struct mvpp2_prs_entry *pe;
1751 int tid;
1752
1753 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
1754 if (!pe)
1755 return NULL;
1756 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
1757
1758 /* Go through the all entires with MVPP2_PRS_LU_FLOWS */
1759 for (tid = MVPP2_PRS_TCAM_SRAM_SIZE - 1; tid >= 0; tid--) {
1760 u8 bits;
1761
1762 if (!priv->prs_shadow[tid].valid ||
1763 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_FLOWS)
1764 continue;
1765
1766 pe->index = tid;
1767 mvpp2_prs_hw_read(priv, pe);
1768 bits = mvpp2_prs_sram_ai_get(pe);
1769
1770 /* Sram store classification lookup ID in AI bits [5:0] */
1771 if ((bits & MVPP2_PRS_FLOW_ID_MASK) == flow)
1772 return pe;
1773 }
1774 kfree(pe);
1775
1776 return NULL;
1777}
1778
1779/* Return first free tcam index, seeking from start to end */
1780static int mvpp2_prs_tcam_first_free(struct mvpp2 *priv, unsigned char start,
1781 unsigned char end)
1782{
1783 int tid;
1784
1785 if (start > end)
1786 swap(start, end);
1787
1788 if (end >= MVPP2_PRS_TCAM_SRAM_SIZE)
1789 end = MVPP2_PRS_TCAM_SRAM_SIZE - 1;
1790
1791 for (tid = start; tid <= end; tid++) {
1792 if (!priv->prs_shadow[tid].valid)
1793 return tid;
1794 }
1795
1796 return -EINVAL;
1797}
1798
1799/* Enable/disable dropping all mac da's */
1800static void mvpp2_prs_mac_drop_all_set(struct mvpp2 *priv, int port, bool add)
1801{
1802 struct mvpp2_prs_entry pe;
1803
1804 if (priv->prs_shadow[MVPP2_PE_DROP_ALL].valid) {
1805 /* Entry exist - update port only */
1806 pe.index = MVPP2_PE_DROP_ALL;
1807 mvpp2_prs_hw_read(priv, &pe);
1808 } else {
1809 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001810 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001811 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1812 pe.index = MVPP2_PE_DROP_ALL;
1813
1814 /* Non-promiscuous mode for all ports - DROP unknown packets */
1815 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
1816 MVPP2_PRS_RI_DROP_MASK);
1817
1818 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1819 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1820
1821 /* Update shadow table */
1822 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1823
1824 /* Mask all ports */
1825 mvpp2_prs_tcam_port_map_set(&pe, 0);
1826 }
1827
1828 /* Update port mask */
1829 mvpp2_prs_tcam_port_set(&pe, port, add);
1830
1831 mvpp2_prs_hw_write(priv, &pe);
1832}
1833
1834/* Set port to promiscuous mode */
1835static void mvpp2_prs_mac_promisc_set(struct mvpp2 *priv, int port, bool add)
1836{
1837 struct mvpp2_prs_entry pe;
1838
Joe Perchesdbedd442015-03-06 20:49:12 -08001839 /* Promiscuous mode - Accept unknown packets */
Marcin Wojtas3f518502014-07-10 16:52:13 -03001840
1841 if (priv->prs_shadow[MVPP2_PE_MAC_PROMISCUOUS].valid) {
1842 /* Entry exist - update port only */
1843 pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1844 mvpp2_prs_hw_read(priv, &pe);
1845 } else {
1846 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001847 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001848 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1849 pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1850
1851 /* Continue - set next lookup */
1852 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
1853
1854 /* Set result info bits */
1855 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_UCAST,
1856 MVPP2_PRS_RI_L2_CAST_MASK);
1857
1858 /* Shift to ethertype */
1859 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
1860 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1861
1862 /* Mask all ports */
1863 mvpp2_prs_tcam_port_map_set(&pe, 0);
1864
1865 /* Update shadow table */
1866 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1867 }
1868
1869 /* Update port mask */
1870 mvpp2_prs_tcam_port_set(&pe, port, add);
1871
1872 mvpp2_prs_hw_write(priv, &pe);
1873}
1874
1875/* Accept multicast */
1876static void mvpp2_prs_mac_multi_set(struct mvpp2 *priv, int port, int index,
1877 bool add)
1878{
1879 struct mvpp2_prs_entry pe;
1880 unsigned char da_mc;
1881
1882 /* Ethernet multicast address first byte is
1883 * 0x01 for IPv4 and 0x33 for IPv6
1884 */
1885 da_mc = (index == MVPP2_PE_MAC_MC_ALL) ? 0x01 : 0x33;
1886
1887 if (priv->prs_shadow[index].valid) {
1888 /* Entry exist - update port only */
1889 pe.index = index;
1890 mvpp2_prs_hw_read(priv, &pe);
1891 } else {
1892 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001893 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001894 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1895 pe.index = index;
1896
1897 /* Continue - set next lookup */
1898 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
1899
1900 /* Set result info bits */
1901 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_MCAST,
1902 MVPP2_PRS_RI_L2_CAST_MASK);
1903
1904 /* Update tcam entry data first byte */
1905 mvpp2_prs_tcam_data_byte_set(&pe, 0, da_mc, 0xff);
1906
1907 /* Shift to ethertype */
1908 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
1909 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1910
1911 /* Mask all ports */
1912 mvpp2_prs_tcam_port_map_set(&pe, 0);
1913
1914 /* Update shadow table */
1915 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1916 }
1917
1918 /* Update port mask */
1919 mvpp2_prs_tcam_port_set(&pe, port, add);
1920
1921 mvpp2_prs_hw_write(priv, &pe);
1922}
1923
1924/* Set entry for dsa packets */
1925static void mvpp2_prs_dsa_tag_set(struct mvpp2 *priv, int port, bool add,
1926 bool tagged, bool extend)
1927{
1928 struct mvpp2_prs_entry pe;
1929 int tid, shift;
1930
1931 if (extend) {
1932 tid = tagged ? MVPP2_PE_EDSA_TAGGED : MVPP2_PE_EDSA_UNTAGGED;
1933 shift = 8;
1934 } else {
1935 tid = tagged ? MVPP2_PE_DSA_TAGGED : MVPP2_PE_DSA_UNTAGGED;
1936 shift = 4;
1937 }
1938
1939 if (priv->prs_shadow[tid].valid) {
1940 /* Entry exist - update port only */
1941 pe.index = tid;
1942 mvpp2_prs_hw_read(priv, &pe);
1943 } else {
1944 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001945 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001946 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
1947 pe.index = tid;
1948
1949 /* Shift 4 bytes if DSA tag or 8 bytes in case of EDSA tag*/
1950 mvpp2_prs_sram_shift_set(&pe, shift,
1951 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1952
1953 /* Update shadow table */
1954 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
1955
1956 if (tagged) {
1957 /* Set tagged bit in DSA tag */
1958 mvpp2_prs_tcam_data_byte_set(&pe, 0,
1959 MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
1960 MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
1961 /* Clear all ai bits for next iteration */
1962 mvpp2_prs_sram_ai_update(&pe, 0,
1963 MVPP2_PRS_SRAM_AI_MASK);
1964 /* If packet is tagged continue check vlans */
1965 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
1966 } else {
1967 /* Set result info bits to 'no vlans' */
1968 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
1969 MVPP2_PRS_RI_VLAN_MASK);
1970 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
1971 }
1972
1973 /* Mask all ports */
1974 mvpp2_prs_tcam_port_map_set(&pe, 0);
1975 }
1976
1977 /* Update port mask */
1978 mvpp2_prs_tcam_port_set(&pe, port, add);
1979
1980 mvpp2_prs_hw_write(priv, &pe);
1981}
1982
1983/* Set entry for dsa ethertype */
1984static void mvpp2_prs_dsa_tag_ethertype_set(struct mvpp2 *priv, int port,
1985 bool add, bool tagged, bool extend)
1986{
1987 struct mvpp2_prs_entry pe;
1988 int tid, shift, port_mask;
1989
1990 if (extend) {
1991 tid = tagged ? MVPP2_PE_ETYPE_EDSA_TAGGED :
1992 MVPP2_PE_ETYPE_EDSA_UNTAGGED;
1993 port_mask = 0;
1994 shift = 8;
1995 } else {
1996 tid = tagged ? MVPP2_PE_ETYPE_DSA_TAGGED :
1997 MVPP2_PE_ETYPE_DSA_UNTAGGED;
1998 port_mask = MVPP2_PRS_PORT_MASK;
1999 shift = 4;
2000 }
2001
2002 if (priv->prs_shadow[tid].valid) {
2003 /* Entry exist - update port only */
2004 pe.index = tid;
2005 mvpp2_prs_hw_read(priv, &pe);
2006 } else {
2007 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002008 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002009 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
2010 pe.index = tid;
2011
2012 /* Set ethertype */
2013 mvpp2_prs_match_etype(&pe, 0, ETH_P_EDSA);
2014 mvpp2_prs_match_etype(&pe, 2, 0);
2015
2016 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DSA_MASK,
2017 MVPP2_PRS_RI_DSA_MASK);
2018 /* Shift ethertype + 2 byte reserved + tag*/
2019 mvpp2_prs_sram_shift_set(&pe, 2 + MVPP2_ETH_TYPE_LEN + shift,
2020 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2021
2022 /* Update shadow table */
2023 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
2024
2025 if (tagged) {
2026 /* Set tagged bit in DSA tag */
2027 mvpp2_prs_tcam_data_byte_set(&pe,
2028 MVPP2_ETH_TYPE_LEN + 2 + 3,
2029 MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
2030 MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
2031 /* Clear all ai bits for next iteration */
2032 mvpp2_prs_sram_ai_update(&pe, 0,
2033 MVPP2_PRS_SRAM_AI_MASK);
2034 /* If packet is tagged continue check vlans */
2035 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2036 } else {
2037 /* Set result info bits to 'no vlans' */
2038 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
2039 MVPP2_PRS_RI_VLAN_MASK);
2040 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2041 }
2042 /* Mask/unmask all ports, depending on dsa type */
2043 mvpp2_prs_tcam_port_map_set(&pe, port_mask);
2044 }
2045
2046 /* Update port mask */
2047 mvpp2_prs_tcam_port_set(&pe, port, add);
2048
2049 mvpp2_prs_hw_write(priv, &pe);
2050}
2051
2052/* Search for existing single/triple vlan entry */
2053static struct mvpp2_prs_entry *mvpp2_prs_vlan_find(struct mvpp2 *priv,
2054 unsigned short tpid, int ai)
2055{
2056 struct mvpp2_prs_entry *pe;
2057 int tid;
2058
2059 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2060 if (!pe)
2061 return NULL;
2062 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2063
2064 /* Go through the all entries with MVPP2_PRS_LU_VLAN */
2065 for (tid = MVPP2_PE_FIRST_FREE_TID;
2066 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
2067 unsigned int ri_bits, ai_bits;
2068 bool match;
2069
2070 if (!priv->prs_shadow[tid].valid ||
2071 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
2072 continue;
2073
2074 pe->index = tid;
2075
2076 mvpp2_prs_hw_read(priv, pe);
2077 match = mvpp2_prs_tcam_data_cmp(pe, 0, swab16(tpid));
2078 if (!match)
2079 continue;
2080
2081 /* Get vlan type */
2082 ri_bits = mvpp2_prs_sram_ri_get(pe);
2083 ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
2084
2085 /* Get current ai value from tcam */
2086 ai_bits = mvpp2_prs_tcam_ai_get(pe);
2087 /* Clear double vlan bit */
2088 ai_bits &= ~MVPP2_PRS_DBL_VLAN_AI_BIT;
2089
2090 if (ai != ai_bits)
2091 continue;
2092
2093 if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
2094 ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
2095 return pe;
2096 }
2097 kfree(pe);
2098
2099 return NULL;
2100}
2101
2102/* Add/update single/triple vlan entry */
2103static int mvpp2_prs_vlan_add(struct mvpp2 *priv, unsigned short tpid, int ai,
2104 unsigned int port_map)
2105{
2106 struct mvpp2_prs_entry *pe;
2107 int tid_aux, tid;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302108 int ret = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002109
2110 pe = mvpp2_prs_vlan_find(priv, tpid, ai);
2111
2112 if (!pe) {
2113 /* Create new tcam entry */
2114 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_LAST_FREE_TID,
2115 MVPP2_PE_FIRST_FREE_TID);
2116 if (tid < 0)
2117 return tid;
2118
2119 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2120 if (!pe)
2121 return -ENOMEM;
2122
2123 /* Get last double vlan tid */
2124 for (tid_aux = MVPP2_PE_LAST_FREE_TID;
2125 tid_aux >= MVPP2_PE_FIRST_FREE_TID; tid_aux--) {
2126 unsigned int ri_bits;
2127
2128 if (!priv->prs_shadow[tid_aux].valid ||
2129 priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
2130 continue;
2131
2132 pe->index = tid_aux;
2133 mvpp2_prs_hw_read(priv, pe);
2134 ri_bits = mvpp2_prs_sram_ri_get(pe);
2135 if ((ri_bits & MVPP2_PRS_RI_VLAN_MASK) ==
2136 MVPP2_PRS_RI_VLAN_DOUBLE)
2137 break;
2138 }
2139
Sudip Mukherjee43737472014-11-01 16:59:34 +05302140 if (tid <= tid_aux) {
2141 ret = -EINVAL;
Markus Elfringf9fd0e32017-04-17 13:50:35 +02002142 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302143 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002144
Markus Elfringbd6aaf52017-04-17 10:40:32 +02002145 memset(pe, 0, sizeof(*pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002146 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2147 pe->index = tid;
2148
2149 mvpp2_prs_match_etype(pe, 0, tpid);
2150
2151 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_L2);
2152 /* Shift 4 bytes - skip 1 vlan tag */
2153 mvpp2_prs_sram_shift_set(pe, MVPP2_VLAN_TAG_LEN,
2154 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2155 /* Clear all ai bits for next iteration */
2156 mvpp2_prs_sram_ai_update(pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2157
2158 if (ai == MVPP2_PRS_SINGLE_VLAN_AI) {
2159 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_SINGLE,
2160 MVPP2_PRS_RI_VLAN_MASK);
2161 } else {
2162 ai |= MVPP2_PRS_DBL_VLAN_AI_BIT;
2163 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_TRIPLE,
2164 MVPP2_PRS_RI_VLAN_MASK);
2165 }
2166 mvpp2_prs_tcam_ai_update(pe, ai, MVPP2_PRS_SRAM_AI_MASK);
2167
2168 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_VLAN);
2169 }
2170 /* Update ports' mask */
2171 mvpp2_prs_tcam_port_map_set(pe, port_map);
2172
2173 mvpp2_prs_hw_write(priv, pe);
Markus Elfringf9fd0e32017-04-17 13:50:35 +02002174free_pe:
Marcin Wojtas3f518502014-07-10 16:52:13 -03002175 kfree(pe);
2176
Sudip Mukherjee43737472014-11-01 16:59:34 +05302177 return ret;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002178}
2179
2180/* Get first free double vlan ai number */
2181static int mvpp2_prs_double_vlan_ai_free_get(struct mvpp2 *priv)
2182{
2183 int i;
2184
2185 for (i = 1; i < MVPP2_PRS_DBL_VLANS_MAX; i++) {
2186 if (!priv->prs_double_vlans[i])
2187 return i;
2188 }
2189
2190 return -EINVAL;
2191}
2192
2193/* Search for existing double vlan entry */
2194static struct mvpp2_prs_entry *mvpp2_prs_double_vlan_find(struct mvpp2 *priv,
2195 unsigned short tpid1,
2196 unsigned short tpid2)
2197{
2198 struct mvpp2_prs_entry *pe;
2199 int tid;
2200
2201 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2202 if (!pe)
2203 return NULL;
2204 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2205
2206 /* Go through the all entries with MVPP2_PRS_LU_VLAN */
2207 for (tid = MVPP2_PE_FIRST_FREE_TID;
2208 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
2209 unsigned int ri_mask;
2210 bool match;
2211
2212 if (!priv->prs_shadow[tid].valid ||
2213 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
2214 continue;
2215
2216 pe->index = tid;
2217 mvpp2_prs_hw_read(priv, pe);
2218
2219 match = mvpp2_prs_tcam_data_cmp(pe, 0, swab16(tpid1))
2220 && mvpp2_prs_tcam_data_cmp(pe, 4, swab16(tpid2));
2221
2222 if (!match)
2223 continue;
2224
2225 ri_mask = mvpp2_prs_sram_ri_get(pe) & MVPP2_PRS_RI_VLAN_MASK;
2226 if (ri_mask == MVPP2_PRS_RI_VLAN_DOUBLE)
2227 return pe;
2228 }
2229 kfree(pe);
2230
2231 return NULL;
2232}
2233
2234/* Add or update double vlan entry */
2235static int mvpp2_prs_double_vlan_add(struct mvpp2 *priv, unsigned short tpid1,
2236 unsigned short tpid2,
2237 unsigned int port_map)
2238{
2239 struct mvpp2_prs_entry *pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302240 int tid_aux, tid, ai, ret = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002241
2242 pe = mvpp2_prs_double_vlan_find(priv, tpid1, tpid2);
2243
2244 if (!pe) {
2245 /* Create new tcam entry */
2246 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2247 MVPP2_PE_LAST_FREE_TID);
2248 if (tid < 0)
2249 return tid;
2250
2251 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2252 if (!pe)
2253 return -ENOMEM;
2254
2255 /* Set ai value for new double vlan entry */
2256 ai = mvpp2_prs_double_vlan_ai_free_get(priv);
Sudip Mukherjee43737472014-11-01 16:59:34 +05302257 if (ai < 0) {
2258 ret = ai;
Markus Elfringc9a7e122017-04-17 13:03:49 +02002259 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302260 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002261
2262 /* Get first single/triple vlan tid */
2263 for (tid_aux = MVPP2_PE_FIRST_FREE_TID;
2264 tid_aux <= MVPP2_PE_LAST_FREE_TID; tid_aux++) {
2265 unsigned int ri_bits;
2266
2267 if (!priv->prs_shadow[tid_aux].valid ||
2268 priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
2269 continue;
2270
2271 pe->index = tid_aux;
2272 mvpp2_prs_hw_read(priv, pe);
2273 ri_bits = mvpp2_prs_sram_ri_get(pe);
2274 ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
2275 if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
2276 ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
2277 break;
2278 }
2279
Sudip Mukherjee43737472014-11-01 16:59:34 +05302280 if (tid >= tid_aux) {
2281 ret = -ERANGE;
Markus Elfringc9a7e122017-04-17 13:03:49 +02002282 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302283 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002284
Markus Elfringbd6aaf52017-04-17 10:40:32 +02002285 memset(pe, 0, sizeof(*pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002286 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2287 pe->index = tid;
2288
2289 priv->prs_double_vlans[ai] = true;
2290
2291 mvpp2_prs_match_etype(pe, 0, tpid1);
2292 mvpp2_prs_match_etype(pe, 4, tpid2);
2293
2294 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_VLAN);
2295 /* Shift 8 bytes - skip 2 vlan tags */
2296 mvpp2_prs_sram_shift_set(pe, 2 * MVPP2_VLAN_TAG_LEN,
2297 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2298 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_DOUBLE,
2299 MVPP2_PRS_RI_VLAN_MASK);
2300 mvpp2_prs_sram_ai_update(pe, ai | MVPP2_PRS_DBL_VLAN_AI_BIT,
2301 MVPP2_PRS_SRAM_AI_MASK);
2302
2303 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_VLAN);
2304 }
2305
2306 /* Update ports' mask */
2307 mvpp2_prs_tcam_port_map_set(pe, port_map);
2308 mvpp2_prs_hw_write(priv, pe);
Markus Elfringc9a7e122017-04-17 13:03:49 +02002309free_pe:
Marcin Wojtas3f518502014-07-10 16:52:13 -03002310 kfree(pe);
Sudip Mukherjee43737472014-11-01 16:59:34 +05302311 return ret;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002312}
2313
2314/* IPv4 header parsing for fragmentation and L4 offset */
2315static int mvpp2_prs_ip4_proto(struct mvpp2 *priv, unsigned short proto,
2316 unsigned int ri, unsigned int ri_mask)
2317{
2318 struct mvpp2_prs_entry pe;
2319 int tid;
2320
2321 if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
2322 (proto != IPPROTO_IGMP))
2323 return -EINVAL;
2324
Stefan Chulskiaff3da32017-09-25 14:59:46 +02002325 /* Not fragmented packet */
Marcin Wojtas3f518502014-07-10 16:52:13 -03002326 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2327 MVPP2_PE_LAST_FREE_TID);
2328 if (tid < 0)
2329 return tid;
2330
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002331 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002332 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
2333 pe.index = tid;
2334
2335 /* Set next lu to IPv4 */
2336 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2337 mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2338 /* Set L4 offset */
2339 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
2340 sizeof(struct iphdr) - 4,
2341 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2342 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
2343 MVPP2_PRS_IPV4_DIP_AI_BIT);
Stefan Chulskiaff3da32017-09-25 14:59:46 +02002344 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask | MVPP2_PRS_RI_IP_FRAG_MASK);
2345
2346 mvpp2_prs_tcam_data_byte_set(&pe, 2, 0x00,
2347 MVPP2_PRS_TCAM_PROTO_MASK_L);
2348 mvpp2_prs_tcam_data_byte_set(&pe, 3, 0x00,
2349 MVPP2_PRS_TCAM_PROTO_MASK);
Marcin Wojtas3f518502014-07-10 16:52:13 -03002350
2351 mvpp2_prs_tcam_data_byte_set(&pe, 5, proto, MVPP2_PRS_TCAM_PROTO_MASK);
2352 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
2353 /* Unmask all ports */
2354 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2355
2356 /* Update shadow table and hw entry */
2357 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2358 mvpp2_prs_hw_write(priv, &pe);
2359
Stefan Chulskiaff3da32017-09-25 14:59:46 +02002360 /* Fragmented packet */
Marcin Wojtas3f518502014-07-10 16:52:13 -03002361 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2362 MVPP2_PE_LAST_FREE_TID);
2363 if (tid < 0)
2364 return tid;
2365
2366 pe.index = tid;
2367 /* Clear ri before updating */
2368 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2369 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2370 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
2371
Stefan Chulskiaff3da32017-09-25 14:59:46 +02002372 mvpp2_prs_sram_ri_update(&pe, ri | MVPP2_PRS_RI_IP_FRAG_TRUE,
2373 ri_mask | MVPP2_PRS_RI_IP_FRAG_MASK);
2374
2375 mvpp2_prs_tcam_data_byte_set(&pe, 2, 0x00, 0x0);
2376 mvpp2_prs_tcam_data_byte_set(&pe, 3, 0x00, 0x0);
Marcin Wojtas3f518502014-07-10 16:52:13 -03002377
2378 /* Update shadow table and hw entry */
2379 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2380 mvpp2_prs_hw_write(priv, &pe);
2381
2382 return 0;
2383}
2384
2385/* IPv4 L3 multicast or broadcast */
2386static int mvpp2_prs_ip4_cast(struct mvpp2 *priv, unsigned short l3_cast)
2387{
2388 struct mvpp2_prs_entry pe;
2389 int mask, tid;
2390
2391 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2392 MVPP2_PE_LAST_FREE_TID);
2393 if (tid < 0)
2394 return tid;
2395
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002396 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002397 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
2398 pe.index = tid;
2399
2400 switch (l3_cast) {
2401 case MVPP2_PRS_L3_MULTI_CAST:
2402 mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV4_MC,
2403 MVPP2_PRS_IPV4_MC_MASK);
2404 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
2405 MVPP2_PRS_RI_L3_ADDR_MASK);
2406 break;
2407 case MVPP2_PRS_L3_BROAD_CAST:
2408 mask = MVPP2_PRS_IPV4_BC_MASK;
2409 mvpp2_prs_tcam_data_byte_set(&pe, 0, mask, mask);
2410 mvpp2_prs_tcam_data_byte_set(&pe, 1, mask, mask);
2411 mvpp2_prs_tcam_data_byte_set(&pe, 2, mask, mask);
2412 mvpp2_prs_tcam_data_byte_set(&pe, 3, mask, mask);
2413 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_BCAST,
2414 MVPP2_PRS_RI_L3_ADDR_MASK);
2415 break;
2416 default:
2417 return -EINVAL;
2418 }
2419
2420 /* Finished: go to flowid generation */
2421 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2422 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2423
2424 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
2425 MVPP2_PRS_IPV4_DIP_AI_BIT);
2426 /* Unmask all ports */
2427 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2428
2429 /* Update shadow table and hw entry */
2430 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2431 mvpp2_prs_hw_write(priv, &pe);
2432
2433 return 0;
2434}
2435
2436/* Set entries for protocols over IPv6 */
2437static int mvpp2_prs_ip6_proto(struct mvpp2 *priv, unsigned short proto,
2438 unsigned int ri, unsigned int ri_mask)
2439{
2440 struct mvpp2_prs_entry pe;
2441 int tid;
2442
2443 if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
2444 (proto != IPPROTO_ICMPV6) && (proto != IPPROTO_IPIP))
2445 return -EINVAL;
2446
2447 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2448 MVPP2_PE_LAST_FREE_TID);
2449 if (tid < 0)
2450 return tid;
2451
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002452 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002453 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2454 pe.index = tid;
2455
2456 /* Finished: go to flowid generation */
2457 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2458 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2459 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
2460 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
2461 sizeof(struct ipv6hdr) - 6,
2462 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2463
2464 mvpp2_prs_tcam_data_byte_set(&pe, 0, proto, MVPP2_PRS_TCAM_PROTO_MASK);
2465 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
2466 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2467 /* Unmask all ports */
2468 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2469
2470 /* Write HW */
2471 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
2472 mvpp2_prs_hw_write(priv, &pe);
2473
2474 return 0;
2475}
2476
2477/* IPv6 L3 multicast entry */
2478static int mvpp2_prs_ip6_cast(struct mvpp2 *priv, unsigned short l3_cast)
2479{
2480 struct mvpp2_prs_entry pe;
2481 int tid;
2482
2483 if (l3_cast != MVPP2_PRS_L3_MULTI_CAST)
2484 return -EINVAL;
2485
2486 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2487 MVPP2_PE_LAST_FREE_TID);
2488 if (tid < 0)
2489 return tid;
2490
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002491 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002492 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2493 pe.index = tid;
2494
2495 /* Finished: go to flowid generation */
2496 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2497 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
2498 MVPP2_PRS_RI_L3_ADDR_MASK);
2499 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
2500 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2501 /* Shift back to IPv6 NH */
2502 mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2503
2504 mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV6_MC,
2505 MVPP2_PRS_IPV6_MC_MASK);
2506 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2507 /* Unmask all ports */
2508 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2509
2510 /* Update shadow table and hw entry */
2511 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
2512 mvpp2_prs_hw_write(priv, &pe);
2513
2514 return 0;
2515}
2516
2517/* Parser per-port initialization */
2518static void mvpp2_prs_hw_port_init(struct mvpp2 *priv, int port, int lu_first,
2519 int lu_max, int offset)
2520{
2521 u32 val;
2522
2523 /* Set lookup ID */
2524 val = mvpp2_read(priv, MVPP2_PRS_INIT_LOOKUP_REG);
2525 val &= ~MVPP2_PRS_PORT_LU_MASK(port);
2526 val |= MVPP2_PRS_PORT_LU_VAL(port, lu_first);
2527 mvpp2_write(priv, MVPP2_PRS_INIT_LOOKUP_REG, val);
2528
2529 /* Set maximum number of loops for packet received from port */
2530 val = mvpp2_read(priv, MVPP2_PRS_MAX_LOOP_REG(port));
2531 val &= ~MVPP2_PRS_MAX_LOOP_MASK(port);
2532 val |= MVPP2_PRS_MAX_LOOP_VAL(port, lu_max);
2533 mvpp2_write(priv, MVPP2_PRS_MAX_LOOP_REG(port), val);
2534
2535 /* Set initial offset for packet header extraction for the first
2536 * searching loop
2537 */
2538 val = mvpp2_read(priv, MVPP2_PRS_INIT_OFFS_REG(port));
2539 val &= ~MVPP2_PRS_INIT_OFF_MASK(port);
2540 val |= MVPP2_PRS_INIT_OFF_VAL(port, offset);
2541 mvpp2_write(priv, MVPP2_PRS_INIT_OFFS_REG(port), val);
2542}
2543
2544/* Default flow entries initialization for all ports */
2545static void mvpp2_prs_def_flow_init(struct mvpp2 *priv)
2546{
2547 struct mvpp2_prs_entry pe;
2548 int port;
2549
2550 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002551 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002552 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2553 pe.index = MVPP2_PE_FIRST_DEFAULT_FLOW - port;
2554
2555 /* Mask all ports */
2556 mvpp2_prs_tcam_port_map_set(&pe, 0);
2557
2558 /* Set flow ID*/
2559 mvpp2_prs_sram_ai_update(&pe, port, MVPP2_PRS_FLOW_ID_MASK);
2560 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
2561
2562 /* Update shadow table and hw entry */
2563 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_FLOWS);
2564 mvpp2_prs_hw_write(priv, &pe);
2565 }
2566}
2567
2568/* Set default entry for Marvell Header field */
2569static void mvpp2_prs_mh_init(struct mvpp2 *priv)
2570{
2571 struct mvpp2_prs_entry pe;
2572
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002573 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002574
2575 pe.index = MVPP2_PE_MH_DEFAULT;
2576 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MH);
2577 mvpp2_prs_sram_shift_set(&pe, MVPP2_MH_SIZE,
2578 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2579 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_MAC);
2580
2581 /* Unmask all ports */
2582 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2583
2584 /* Update shadow table and hw entry */
2585 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MH);
2586 mvpp2_prs_hw_write(priv, &pe);
2587}
2588
2589/* Set default entires (place holder) for promiscuous, non-promiscuous and
2590 * multicast MAC addresses
2591 */
2592static void mvpp2_prs_mac_init(struct mvpp2 *priv)
2593{
2594 struct mvpp2_prs_entry pe;
2595
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002596 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002597
2598 /* Non-promiscuous mode for all ports - DROP unknown packets */
2599 pe.index = MVPP2_PE_MAC_NON_PROMISCUOUS;
2600 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
2601
2602 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
2603 MVPP2_PRS_RI_DROP_MASK);
2604 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2605 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2606
2607 /* Unmask all ports */
2608 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2609
2610 /* Update shadow table and hw entry */
2611 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2612 mvpp2_prs_hw_write(priv, &pe);
2613
2614 /* place holders only - no ports */
2615 mvpp2_prs_mac_drop_all_set(priv, 0, false);
2616 mvpp2_prs_mac_promisc_set(priv, 0, false);
Antoine Tenart20746d72017-10-24 11:41:27 +02002617 mvpp2_prs_mac_multi_set(priv, 0, MVPP2_PE_MAC_MC_ALL, false);
2618 mvpp2_prs_mac_multi_set(priv, 0, MVPP2_PE_MAC_MC_IP6, false);
Marcin Wojtas3f518502014-07-10 16:52:13 -03002619}
2620
2621/* Set default entries for various types of dsa packets */
2622static void mvpp2_prs_dsa_init(struct mvpp2 *priv)
2623{
2624 struct mvpp2_prs_entry pe;
2625
2626 /* None tagged EDSA entry - place holder */
2627 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
2628 MVPP2_PRS_EDSA);
2629
2630 /* Tagged EDSA entry - place holder */
2631 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
2632
2633 /* None tagged DSA entry - place holder */
2634 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
2635 MVPP2_PRS_DSA);
2636
2637 /* Tagged DSA entry - place holder */
2638 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
2639
2640 /* None tagged EDSA ethertype entry - place holder*/
2641 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
2642 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
2643
2644 /* Tagged EDSA ethertype entry - place holder*/
2645 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
2646 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
2647
2648 /* None tagged DSA ethertype entry */
2649 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
2650 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
2651
2652 /* Tagged DSA ethertype entry */
2653 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
2654 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
2655
2656 /* Set default entry, in case DSA or EDSA tag not found */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002657 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002658 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
2659 pe.index = MVPP2_PE_DSA_DEFAULT;
2660 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2661
2662 /* Shift 0 bytes */
2663 mvpp2_prs_sram_shift_set(&pe, 0, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2664 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2665
2666 /* Clear all sram ai bits for next iteration */
2667 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2668
2669 /* Unmask all ports */
2670 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2671
2672 mvpp2_prs_hw_write(priv, &pe);
2673}
2674
2675/* Match basic ethertypes */
2676static int mvpp2_prs_etype_init(struct mvpp2 *priv)
2677{
2678 struct mvpp2_prs_entry pe;
2679 int tid;
2680
2681 /* Ethertype: PPPoE */
2682 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2683 MVPP2_PE_LAST_FREE_TID);
2684 if (tid < 0)
2685 return tid;
2686
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002687 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002688 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2689 pe.index = tid;
2690
2691 mvpp2_prs_match_etype(&pe, 0, ETH_P_PPP_SES);
2692
2693 mvpp2_prs_sram_shift_set(&pe, MVPP2_PPPOE_HDR_SIZE,
2694 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2695 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2696 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_PPPOE_MASK,
2697 MVPP2_PRS_RI_PPPOE_MASK);
2698
2699 /* Update shadow table and hw entry */
2700 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2701 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2702 priv->prs_shadow[pe.index].finish = false;
2703 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_PPPOE_MASK,
2704 MVPP2_PRS_RI_PPPOE_MASK);
2705 mvpp2_prs_hw_write(priv, &pe);
2706
2707 /* Ethertype: ARP */
2708 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2709 MVPP2_PE_LAST_FREE_TID);
2710 if (tid < 0)
2711 return tid;
2712
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002713 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002714 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2715 pe.index = tid;
2716
2717 mvpp2_prs_match_etype(&pe, 0, ETH_P_ARP);
2718
2719 /* Generate flow in the next iteration*/
2720 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2721 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2722 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_ARP,
2723 MVPP2_PRS_RI_L3_PROTO_MASK);
2724 /* Set L3 offset */
2725 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2726 MVPP2_ETH_TYPE_LEN,
2727 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2728
2729 /* Update shadow table and hw entry */
2730 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2731 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2732 priv->prs_shadow[pe.index].finish = true;
2733 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_ARP,
2734 MVPP2_PRS_RI_L3_PROTO_MASK);
2735 mvpp2_prs_hw_write(priv, &pe);
2736
2737 /* Ethertype: LBTD */
2738 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2739 MVPP2_PE_LAST_FREE_TID);
2740 if (tid < 0)
2741 return tid;
2742
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002743 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002744 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2745 pe.index = tid;
2746
2747 mvpp2_prs_match_etype(&pe, 0, MVPP2_IP_LBDT_TYPE);
2748
2749 /* Generate flow in the next iteration*/
2750 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2751 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2752 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2753 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2754 MVPP2_PRS_RI_CPU_CODE_MASK |
2755 MVPP2_PRS_RI_UDF3_MASK);
2756 /* Set L3 offset */
2757 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2758 MVPP2_ETH_TYPE_LEN,
2759 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2760
2761 /* Update shadow table and hw entry */
2762 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2763 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2764 priv->prs_shadow[pe.index].finish = true;
2765 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2766 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2767 MVPP2_PRS_RI_CPU_CODE_MASK |
2768 MVPP2_PRS_RI_UDF3_MASK);
2769 mvpp2_prs_hw_write(priv, &pe);
2770
2771 /* Ethertype: IPv4 without options */
2772 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2773 MVPP2_PE_LAST_FREE_TID);
2774 if (tid < 0)
2775 return tid;
2776
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002777 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002778 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2779 pe.index = tid;
2780
2781 mvpp2_prs_match_etype(&pe, 0, ETH_P_IP);
2782 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2783 MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
2784 MVPP2_PRS_IPV4_HEAD_MASK |
2785 MVPP2_PRS_IPV4_IHL_MASK);
2786
2787 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2788 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
2789 MVPP2_PRS_RI_L3_PROTO_MASK);
2790 /* Skip eth_type + 4 bytes of IP header */
2791 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
2792 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2793 /* Set L3 offset */
2794 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2795 MVPP2_ETH_TYPE_LEN,
2796 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2797
2798 /* Update shadow table and hw entry */
2799 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2800 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2801 priv->prs_shadow[pe.index].finish = false;
2802 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4,
2803 MVPP2_PRS_RI_L3_PROTO_MASK);
2804 mvpp2_prs_hw_write(priv, &pe);
2805
2806 /* Ethertype: IPv4 with options */
2807 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2808 MVPP2_PE_LAST_FREE_TID);
2809 if (tid < 0)
2810 return tid;
2811
2812 pe.index = tid;
2813
2814 /* Clear tcam data before updating */
2815 pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(MVPP2_ETH_TYPE_LEN)] = 0x0;
2816 pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(MVPP2_ETH_TYPE_LEN)] = 0x0;
2817
2818 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2819 MVPP2_PRS_IPV4_HEAD,
2820 MVPP2_PRS_IPV4_HEAD_MASK);
2821
2822 /* Clear ri before updating */
2823 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2824 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2825 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
2826 MVPP2_PRS_RI_L3_PROTO_MASK);
2827
2828 /* Update shadow table and hw entry */
2829 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2830 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2831 priv->prs_shadow[pe.index].finish = false;
2832 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4_OPT,
2833 MVPP2_PRS_RI_L3_PROTO_MASK);
2834 mvpp2_prs_hw_write(priv, &pe);
2835
2836 /* Ethertype: IPv6 without options */
2837 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2838 MVPP2_PE_LAST_FREE_TID);
2839 if (tid < 0)
2840 return tid;
2841
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002842 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002843 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2844 pe.index = tid;
2845
2846 mvpp2_prs_match_etype(&pe, 0, ETH_P_IPV6);
2847
2848 /* Skip DIP of IPV6 header */
2849 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 8 +
2850 MVPP2_MAX_L3_ADDR_SIZE,
2851 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2852 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2853 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
2854 MVPP2_PRS_RI_L3_PROTO_MASK);
2855 /* Set L3 offset */
2856 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2857 MVPP2_ETH_TYPE_LEN,
2858 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2859
2860 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2861 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2862 priv->prs_shadow[pe.index].finish = false;
2863 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP6,
2864 MVPP2_PRS_RI_L3_PROTO_MASK);
2865 mvpp2_prs_hw_write(priv, &pe);
2866
2867 /* Default entry for MVPP2_PRS_LU_L2 - Unknown ethtype */
2868 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2869 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2870 pe.index = MVPP2_PE_ETH_TYPE_UN;
2871
2872 /* Unmask all ports */
2873 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2874
2875 /* Generate flow in the next iteration*/
2876 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2877 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2878 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
2879 MVPP2_PRS_RI_L3_PROTO_MASK);
2880 /* Set L3 offset even it's unknown L3 */
2881 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2882 MVPP2_ETH_TYPE_LEN,
2883 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2884
2885 /* Update shadow table and hw entry */
2886 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2887 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2888 priv->prs_shadow[pe.index].finish = true;
2889 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_UN,
2890 MVPP2_PRS_RI_L3_PROTO_MASK);
2891 mvpp2_prs_hw_write(priv, &pe);
2892
2893 return 0;
2894}
2895
2896/* Configure vlan entries and detect up to 2 successive VLAN tags.
2897 * Possible options:
2898 * 0x8100, 0x88A8
2899 * 0x8100, 0x8100
2900 * 0x8100
2901 * 0x88A8
2902 */
2903static int mvpp2_prs_vlan_init(struct platform_device *pdev, struct mvpp2 *priv)
2904{
2905 struct mvpp2_prs_entry pe;
2906 int err;
2907
2908 priv->prs_double_vlans = devm_kcalloc(&pdev->dev, sizeof(bool),
2909 MVPP2_PRS_DBL_VLANS_MAX,
2910 GFP_KERNEL);
2911 if (!priv->prs_double_vlans)
2912 return -ENOMEM;
2913
2914 /* Double VLAN: 0x8100, 0x88A8 */
2915 err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021AD,
2916 MVPP2_PRS_PORT_MASK);
2917 if (err)
2918 return err;
2919
2920 /* Double VLAN: 0x8100, 0x8100 */
2921 err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021Q,
2922 MVPP2_PRS_PORT_MASK);
2923 if (err)
2924 return err;
2925
2926 /* Single VLAN: 0x88a8 */
2927 err = mvpp2_prs_vlan_add(priv, ETH_P_8021AD, MVPP2_PRS_SINGLE_VLAN_AI,
2928 MVPP2_PRS_PORT_MASK);
2929 if (err)
2930 return err;
2931
2932 /* Single VLAN: 0x8100 */
2933 err = mvpp2_prs_vlan_add(priv, ETH_P_8021Q, MVPP2_PRS_SINGLE_VLAN_AI,
2934 MVPP2_PRS_PORT_MASK);
2935 if (err)
2936 return err;
2937
2938 /* Set default double vlan entry */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002939 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002940 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2941 pe.index = MVPP2_PE_VLAN_DBL;
2942
2943 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2944 /* Clear ai for next iterations */
2945 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2946 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_DOUBLE,
2947 MVPP2_PRS_RI_VLAN_MASK);
2948
2949 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_DBL_VLAN_AI_BIT,
2950 MVPP2_PRS_DBL_VLAN_AI_BIT);
2951 /* Unmask all ports */
2952 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2953
2954 /* Update shadow table and hw entry */
2955 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
2956 mvpp2_prs_hw_write(priv, &pe);
2957
2958 /* Set default vlan none entry */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002959 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002960 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2961 pe.index = MVPP2_PE_VLAN_NONE;
2962
2963 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2964 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
2965 MVPP2_PRS_RI_VLAN_MASK);
2966
2967 /* Unmask all ports */
2968 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2969
2970 /* Update shadow table and hw entry */
2971 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
2972 mvpp2_prs_hw_write(priv, &pe);
2973
2974 return 0;
2975}
2976
2977/* Set entries for PPPoE ethertype */
2978static int mvpp2_prs_pppoe_init(struct mvpp2 *priv)
2979{
2980 struct mvpp2_prs_entry pe;
2981 int tid;
2982
2983 /* IPv4 over PPPoE with options */
2984 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2985 MVPP2_PE_LAST_FREE_TID);
2986 if (tid < 0)
2987 return tid;
2988
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002989 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002990 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2991 pe.index = tid;
2992
2993 mvpp2_prs_match_etype(&pe, 0, PPP_IP);
2994
2995 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2996 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
2997 MVPP2_PRS_RI_L3_PROTO_MASK);
2998 /* Skip eth_type + 4 bytes of IP header */
2999 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
3000 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3001 /* Set L3 offset */
3002 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
3003 MVPP2_ETH_TYPE_LEN,
3004 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3005
3006 /* Update shadow table and hw entry */
3007 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3008 mvpp2_prs_hw_write(priv, &pe);
3009
3010 /* IPv4 over PPPoE without options */
3011 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3012 MVPP2_PE_LAST_FREE_TID);
3013 if (tid < 0)
3014 return tid;
3015
3016 pe.index = tid;
3017
3018 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
3019 MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
3020 MVPP2_PRS_IPV4_HEAD_MASK |
3021 MVPP2_PRS_IPV4_IHL_MASK);
3022
3023 /* Clear ri before updating */
3024 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
3025 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
3026 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
3027 MVPP2_PRS_RI_L3_PROTO_MASK);
3028
3029 /* Update shadow table and hw entry */
3030 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3031 mvpp2_prs_hw_write(priv, &pe);
3032
3033 /* IPv6 over PPPoE */
3034 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3035 MVPP2_PE_LAST_FREE_TID);
3036 if (tid < 0)
3037 return tid;
3038
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003039 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003040 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
3041 pe.index = tid;
3042
3043 mvpp2_prs_match_etype(&pe, 0, PPP_IPV6);
3044
3045 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
3046 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
3047 MVPP2_PRS_RI_L3_PROTO_MASK);
3048 /* Skip eth_type + 4 bytes of IPv6 header */
3049 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
3050 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3051 /* Set L3 offset */
3052 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
3053 MVPP2_ETH_TYPE_LEN,
3054 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3055
3056 /* Update shadow table and hw entry */
3057 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3058 mvpp2_prs_hw_write(priv, &pe);
3059
3060 /* Non-IP over PPPoE */
3061 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3062 MVPP2_PE_LAST_FREE_TID);
3063 if (tid < 0)
3064 return tid;
3065
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003066 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003067 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
3068 pe.index = tid;
3069
3070 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
3071 MVPP2_PRS_RI_L3_PROTO_MASK);
3072
3073 /* Finished: go to flowid generation */
3074 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3075 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3076 /* Set L3 offset even if it's unknown L3 */
3077 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
3078 MVPP2_ETH_TYPE_LEN,
3079 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3080
3081 /* Update shadow table and hw entry */
3082 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3083 mvpp2_prs_hw_write(priv, &pe);
3084
3085 return 0;
3086}
3087
3088/* Initialize entries for IPv4 */
3089static int mvpp2_prs_ip4_init(struct mvpp2 *priv)
3090{
3091 struct mvpp2_prs_entry pe;
3092 int err;
3093
3094 /* Set entries for TCP, UDP and IGMP over IPv4 */
3095 err = mvpp2_prs_ip4_proto(priv, IPPROTO_TCP, MVPP2_PRS_RI_L4_TCP,
3096 MVPP2_PRS_RI_L4_PROTO_MASK);
3097 if (err)
3098 return err;
3099
3100 err = mvpp2_prs_ip4_proto(priv, IPPROTO_UDP, MVPP2_PRS_RI_L4_UDP,
3101 MVPP2_PRS_RI_L4_PROTO_MASK);
3102 if (err)
3103 return err;
3104
3105 err = mvpp2_prs_ip4_proto(priv, IPPROTO_IGMP,
3106 MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
3107 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
3108 MVPP2_PRS_RI_CPU_CODE_MASK |
3109 MVPP2_PRS_RI_UDF3_MASK);
3110 if (err)
3111 return err;
3112
3113 /* IPv4 Broadcast */
3114 err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_BROAD_CAST);
3115 if (err)
3116 return err;
3117
3118 /* IPv4 Multicast */
3119 err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
3120 if (err)
3121 return err;
3122
3123 /* Default IPv4 entry for unknown protocols */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003124 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003125 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
3126 pe.index = MVPP2_PE_IP4_PROTO_UN;
3127
3128 /* Set next lu to IPv4 */
3129 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
3130 mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3131 /* Set L4 offset */
3132 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
3133 sizeof(struct iphdr) - 4,
3134 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3135 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
3136 MVPP2_PRS_IPV4_DIP_AI_BIT);
3137 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3138 MVPP2_PRS_RI_L4_PROTO_MASK);
3139
3140 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
3141 /* Unmask all ports */
3142 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3143
3144 /* Update shadow table and hw entry */
3145 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3146 mvpp2_prs_hw_write(priv, &pe);
3147
3148 /* Default IPv4 entry for unicast address */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003149 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003150 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
3151 pe.index = MVPP2_PE_IP4_ADDR_UN;
3152
3153 /* Finished: go to flowid generation */
3154 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3155 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3156 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
3157 MVPP2_PRS_RI_L3_ADDR_MASK);
3158
3159 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
3160 MVPP2_PRS_IPV4_DIP_AI_BIT);
3161 /* Unmask all ports */
3162 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3163
3164 /* Update shadow table and hw entry */
3165 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3166 mvpp2_prs_hw_write(priv, &pe);
3167
3168 return 0;
3169}
3170
3171/* Initialize entries for IPv6 */
3172static int mvpp2_prs_ip6_init(struct mvpp2 *priv)
3173{
3174 struct mvpp2_prs_entry pe;
3175 int tid, err;
3176
3177 /* Set entries for TCP, UDP and ICMP over IPv6 */
3178 err = mvpp2_prs_ip6_proto(priv, IPPROTO_TCP,
3179 MVPP2_PRS_RI_L4_TCP,
3180 MVPP2_PRS_RI_L4_PROTO_MASK);
3181 if (err)
3182 return err;
3183
3184 err = mvpp2_prs_ip6_proto(priv, IPPROTO_UDP,
3185 MVPP2_PRS_RI_L4_UDP,
3186 MVPP2_PRS_RI_L4_PROTO_MASK);
3187 if (err)
3188 return err;
3189
3190 err = mvpp2_prs_ip6_proto(priv, IPPROTO_ICMPV6,
3191 MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
3192 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
3193 MVPP2_PRS_RI_CPU_CODE_MASK |
3194 MVPP2_PRS_RI_UDF3_MASK);
3195 if (err)
3196 return err;
3197
3198 /* IPv4 is the last header. This is similar case as 6-TCP or 17-UDP */
3199 /* Result Info: UDF7=1, DS lite */
3200 err = mvpp2_prs_ip6_proto(priv, IPPROTO_IPIP,
3201 MVPP2_PRS_RI_UDF7_IP6_LITE,
3202 MVPP2_PRS_RI_UDF7_MASK);
3203 if (err)
3204 return err;
3205
3206 /* IPv6 multicast */
3207 err = mvpp2_prs_ip6_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
3208 if (err)
3209 return err;
3210
3211 /* Entry for checking hop limit */
3212 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3213 MVPP2_PE_LAST_FREE_TID);
3214 if (tid < 0)
3215 return tid;
3216
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003217 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003218 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3219 pe.index = tid;
3220
3221 /* Finished: go to flowid generation */
3222 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3223 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3224 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN |
3225 MVPP2_PRS_RI_DROP_MASK,
3226 MVPP2_PRS_RI_L3_PROTO_MASK |
3227 MVPP2_PRS_RI_DROP_MASK);
3228
3229 mvpp2_prs_tcam_data_byte_set(&pe, 1, 0x00, MVPP2_PRS_IPV6_HOP_MASK);
3230 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3231 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3232
3233 /* Update shadow table and hw entry */
3234 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3235 mvpp2_prs_hw_write(priv, &pe);
3236
3237 /* Default IPv6 entry for unknown protocols */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003238 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003239 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3240 pe.index = MVPP2_PE_IP6_PROTO_UN;
3241
3242 /* Finished: go to flowid generation */
3243 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3244 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3245 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3246 MVPP2_PRS_RI_L4_PROTO_MASK);
3247 /* Set L4 offset relatively to our current place */
3248 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
3249 sizeof(struct ipv6hdr) - 4,
3250 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3251
3252 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3253 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3254 /* Unmask all ports */
3255 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3256
3257 /* Update shadow table and hw entry */
3258 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3259 mvpp2_prs_hw_write(priv, &pe);
3260
3261 /* Default IPv6 entry for unknown ext protocols */
3262 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3263 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3264 pe.index = MVPP2_PE_IP6_EXT_PROTO_UN;
3265
3266 /* Finished: go to flowid generation */
3267 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3268 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3269 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3270 MVPP2_PRS_RI_L4_PROTO_MASK);
3271
3272 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_EXT_AI_BIT,
3273 MVPP2_PRS_IPV6_EXT_AI_BIT);
3274 /* Unmask all ports */
3275 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3276
3277 /* Update shadow table and hw entry */
3278 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3279 mvpp2_prs_hw_write(priv, &pe);
3280
3281 /* Default IPv6 entry for unicast address */
3282 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3283 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3284 pe.index = MVPP2_PE_IP6_ADDR_UN;
3285
3286 /* Finished: go to IPv6 again */
3287 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
3288 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
3289 MVPP2_PRS_RI_L3_ADDR_MASK);
3290 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3291 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3292 /* Shift back to IPV6 NH */
3293 mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3294
3295 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3296 /* Unmask all ports */
3297 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3298
3299 /* Update shadow table and hw entry */
3300 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
3301 mvpp2_prs_hw_write(priv, &pe);
3302
3303 return 0;
3304}
3305
3306/* Parser default initialization */
3307static int mvpp2_prs_default_init(struct platform_device *pdev,
3308 struct mvpp2 *priv)
3309{
3310 int err, index, i;
3311
3312 /* Enable tcam table */
3313 mvpp2_write(priv, MVPP2_PRS_TCAM_CTRL_REG, MVPP2_PRS_TCAM_EN_MASK);
3314
3315 /* Clear all tcam and sram entries */
3316 for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++) {
3317 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
3318 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
3319 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), 0);
3320
3321 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, index);
3322 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
3323 mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), 0);
3324 }
3325
3326 /* Invalidate all tcam entries */
3327 for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++)
3328 mvpp2_prs_hw_inv(priv, index);
3329
3330 priv->prs_shadow = devm_kcalloc(&pdev->dev, MVPP2_PRS_TCAM_SRAM_SIZE,
Markus Elfring37df25e2017-04-17 09:12:34 +02003331 sizeof(*priv->prs_shadow),
Marcin Wojtas3f518502014-07-10 16:52:13 -03003332 GFP_KERNEL);
3333 if (!priv->prs_shadow)
3334 return -ENOMEM;
3335
3336 /* Always start from lookup = 0 */
3337 for (index = 0; index < MVPP2_MAX_PORTS; index++)
3338 mvpp2_prs_hw_port_init(priv, index, MVPP2_PRS_LU_MH,
3339 MVPP2_PRS_PORT_LU_MAX, 0);
3340
3341 mvpp2_prs_def_flow_init(priv);
3342
3343 mvpp2_prs_mh_init(priv);
3344
3345 mvpp2_prs_mac_init(priv);
3346
3347 mvpp2_prs_dsa_init(priv);
3348
3349 err = mvpp2_prs_etype_init(priv);
3350 if (err)
3351 return err;
3352
3353 err = mvpp2_prs_vlan_init(pdev, priv);
3354 if (err)
3355 return err;
3356
3357 err = mvpp2_prs_pppoe_init(priv);
3358 if (err)
3359 return err;
3360
3361 err = mvpp2_prs_ip6_init(priv);
3362 if (err)
3363 return err;
3364
3365 err = mvpp2_prs_ip4_init(priv);
3366 if (err)
3367 return err;
3368
3369 return 0;
3370}
3371
3372/* Compare MAC DA with tcam entry data */
3373static bool mvpp2_prs_mac_range_equals(struct mvpp2_prs_entry *pe,
3374 const u8 *da, unsigned char *mask)
3375{
3376 unsigned char tcam_byte, tcam_mask;
3377 int index;
3378
3379 for (index = 0; index < ETH_ALEN; index++) {
3380 mvpp2_prs_tcam_data_byte_get(pe, index, &tcam_byte, &tcam_mask);
3381 if (tcam_mask != mask[index])
3382 return false;
3383
3384 if ((tcam_mask & tcam_byte) != (da[index] & mask[index]))
3385 return false;
3386 }
3387
3388 return true;
3389}
3390
3391/* Find tcam entry with matched pair <MAC DA, port> */
3392static struct mvpp2_prs_entry *
3393mvpp2_prs_mac_da_range_find(struct mvpp2 *priv, int pmap, const u8 *da,
3394 unsigned char *mask, int udf_type)
3395{
3396 struct mvpp2_prs_entry *pe;
3397 int tid;
3398
3399 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
3400 if (!pe)
3401 return NULL;
3402 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
3403
3404 /* Go through the all entires with MVPP2_PRS_LU_MAC */
3405 for (tid = MVPP2_PE_FIRST_FREE_TID;
3406 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
3407 unsigned int entry_pmap;
3408
3409 if (!priv->prs_shadow[tid].valid ||
3410 (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
3411 (priv->prs_shadow[tid].udf != udf_type))
3412 continue;
3413
3414 pe->index = tid;
3415 mvpp2_prs_hw_read(priv, pe);
3416 entry_pmap = mvpp2_prs_tcam_port_map_get(pe);
3417
3418 if (mvpp2_prs_mac_range_equals(pe, da, mask) &&
3419 entry_pmap == pmap)
3420 return pe;
3421 }
3422 kfree(pe);
3423
3424 return NULL;
3425}
3426
3427/* Update parser's mac da entry */
3428static int mvpp2_prs_mac_da_accept(struct mvpp2 *priv, int port,
3429 const u8 *da, bool add)
3430{
3431 struct mvpp2_prs_entry *pe;
3432 unsigned int pmap, len, ri;
3433 unsigned char mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3434 int tid;
3435
3436 /* Scan TCAM and see if entry with this <MAC DA, port> already exist */
3437 pe = mvpp2_prs_mac_da_range_find(priv, (1 << port), da, mask,
3438 MVPP2_PRS_UDF_MAC_DEF);
3439
3440 /* No such entry */
3441 if (!pe) {
3442 if (!add)
3443 return 0;
3444
3445 /* Create new TCAM entry */
3446 /* Find first range mac entry*/
3447 for (tid = MVPP2_PE_FIRST_FREE_TID;
3448 tid <= MVPP2_PE_LAST_FREE_TID; tid++)
3449 if (priv->prs_shadow[tid].valid &&
3450 (priv->prs_shadow[tid].lu == MVPP2_PRS_LU_MAC) &&
3451 (priv->prs_shadow[tid].udf ==
3452 MVPP2_PRS_UDF_MAC_RANGE))
3453 break;
3454
3455 /* Go through the all entries from first to last */
3456 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3457 tid - 1);
3458 if (tid < 0)
3459 return tid;
3460
3461 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
3462 if (!pe)
Amitoj Kaur Chawlac2bb7bc2016-02-04 19:25:26 +05303463 return -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003464 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
3465 pe->index = tid;
3466
3467 /* Mask all ports */
3468 mvpp2_prs_tcam_port_map_set(pe, 0);
3469 }
3470
3471 /* Update port mask */
3472 mvpp2_prs_tcam_port_set(pe, port, add);
3473
3474 /* Invalidate the entry if no ports are left enabled */
3475 pmap = mvpp2_prs_tcam_port_map_get(pe);
3476 if (pmap == 0) {
3477 if (add) {
3478 kfree(pe);
Amitoj Kaur Chawlac2bb7bc2016-02-04 19:25:26 +05303479 return -EINVAL;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003480 }
3481 mvpp2_prs_hw_inv(priv, pe->index);
3482 priv->prs_shadow[pe->index].valid = false;
3483 kfree(pe);
3484 return 0;
3485 }
3486
3487 /* Continue - set next lookup */
3488 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_DSA);
3489
3490 /* Set match on DA */
3491 len = ETH_ALEN;
3492 while (len--)
3493 mvpp2_prs_tcam_data_byte_set(pe, len, da[len], 0xff);
3494
3495 /* Set result info bits */
3496 if (is_broadcast_ether_addr(da))
3497 ri = MVPP2_PRS_RI_L2_BCAST;
3498 else if (is_multicast_ether_addr(da))
3499 ri = MVPP2_PRS_RI_L2_MCAST;
3500 else
3501 ri = MVPP2_PRS_RI_L2_UCAST | MVPP2_PRS_RI_MAC_ME_MASK;
3502
3503 mvpp2_prs_sram_ri_update(pe, ri, MVPP2_PRS_RI_L2_CAST_MASK |
3504 MVPP2_PRS_RI_MAC_ME_MASK);
3505 mvpp2_prs_shadow_ri_set(priv, pe->index, ri, MVPP2_PRS_RI_L2_CAST_MASK |
3506 MVPP2_PRS_RI_MAC_ME_MASK);
3507
3508 /* Shift to ethertype */
3509 mvpp2_prs_sram_shift_set(pe, 2 * ETH_ALEN,
3510 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3511
3512 /* Update shadow table and hw entry */
3513 priv->prs_shadow[pe->index].udf = MVPP2_PRS_UDF_MAC_DEF;
3514 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_MAC);
3515 mvpp2_prs_hw_write(priv, pe);
3516
3517 kfree(pe);
3518
3519 return 0;
3520}
3521
3522static int mvpp2_prs_update_mac_da(struct net_device *dev, const u8 *da)
3523{
3524 struct mvpp2_port *port = netdev_priv(dev);
3525 int err;
3526
3527 /* Remove old parser entry */
3528 err = mvpp2_prs_mac_da_accept(port->priv, port->id, dev->dev_addr,
3529 false);
3530 if (err)
3531 return err;
3532
3533 /* Add new parser entry */
3534 err = mvpp2_prs_mac_da_accept(port->priv, port->id, da, true);
3535 if (err)
3536 return err;
3537
3538 /* Set addr in the device */
3539 ether_addr_copy(dev->dev_addr, da);
3540
3541 return 0;
3542}
3543
3544/* Delete all port's multicast simple (not range) entries */
3545static void mvpp2_prs_mcast_del_all(struct mvpp2 *priv, int port)
3546{
3547 struct mvpp2_prs_entry pe;
3548 int index, tid;
3549
3550 for (tid = MVPP2_PE_FIRST_FREE_TID;
3551 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
3552 unsigned char da[ETH_ALEN], da_mask[ETH_ALEN];
3553
3554 if (!priv->prs_shadow[tid].valid ||
3555 (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
3556 (priv->prs_shadow[tid].udf != MVPP2_PRS_UDF_MAC_DEF))
3557 continue;
3558
3559 /* Only simple mac entries */
3560 pe.index = tid;
3561 mvpp2_prs_hw_read(priv, &pe);
3562
3563 /* Read mac addr from entry */
3564 for (index = 0; index < ETH_ALEN; index++)
3565 mvpp2_prs_tcam_data_byte_get(&pe, index, &da[index],
3566 &da_mask[index]);
3567
3568 if (is_multicast_ether_addr(da) && !is_broadcast_ether_addr(da))
3569 /* Delete this entry */
3570 mvpp2_prs_mac_da_accept(priv, port, da, false);
3571 }
3572}
3573
3574static int mvpp2_prs_tag_mode_set(struct mvpp2 *priv, int port, int type)
3575{
3576 switch (type) {
3577 case MVPP2_TAG_TYPE_EDSA:
3578 /* Add port to EDSA entries */
3579 mvpp2_prs_dsa_tag_set(priv, port, true,
3580 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3581 mvpp2_prs_dsa_tag_set(priv, port, true,
3582 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3583 /* Remove port from DSA entries */
3584 mvpp2_prs_dsa_tag_set(priv, port, false,
3585 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3586 mvpp2_prs_dsa_tag_set(priv, port, false,
3587 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3588 break;
3589
3590 case MVPP2_TAG_TYPE_DSA:
3591 /* Add port to DSA entries */
3592 mvpp2_prs_dsa_tag_set(priv, port, true,
3593 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3594 mvpp2_prs_dsa_tag_set(priv, port, true,
3595 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3596 /* Remove port from EDSA entries */
3597 mvpp2_prs_dsa_tag_set(priv, port, false,
3598 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3599 mvpp2_prs_dsa_tag_set(priv, port, false,
3600 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3601 break;
3602
3603 case MVPP2_TAG_TYPE_MH:
3604 case MVPP2_TAG_TYPE_NONE:
3605 /* Remove port form EDSA and DSA entries */
3606 mvpp2_prs_dsa_tag_set(priv, port, false,
3607 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3608 mvpp2_prs_dsa_tag_set(priv, port, false,
3609 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3610 mvpp2_prs_dsa_tag_set(priv, port, false,
3611 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3612 mvpp2_prs_dsa_tag_set(priv, port, false,
3613 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3614 break;
3615
3616 default:
3617 if ((type < 0) || (type > MVPP2_TAG_TYPE_EDSA))
3618 return -EINVAL;
3619 }
3620
3621 return 0;
3622}
3623
3624/* Set prs flow for the port */
3625static int mvpp2_prs_def_flow(struct mvpp2_port *port)
3626{
3627 struct mvpp2_prs_entry *pe;
3628 int tid;
3629
3630 pe = mvpp2_prs_flow_find(port->priv, port->id);
3631
3632 /* Such entry not exist */
3633 if (!pe) {
3634 /* Go through the all entires from last to first */
3635 tid = mvpp2_prs_tcam_first_free(port->priv,
3636 MVPP2_PE_LAST_FREE_TID,
3637 MVPP2_PE_FIRST_FREE_TID);
3638 if (tid < 0)
3639 return tid;
3640
3641 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
3642 if (!pe)
3643 return -ENOMEM;
3644
3645 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
3646 pe->index = tid;
3647
3648 /* Set flow ID*/
3649 mvpp2_prs_sram_ai_update(pe, port->id, MVPP2_PRS_FLOW_ID_MASK);
3650 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
3651
3652 /* Update shadow table */
3653 mvpp2_prs_shadow_set(port->priv, pe->index, MVPP2_PRS_LU_FLOWS);
3654 }
3655
3656 mvpp2_prs_tcam_port_map_set(pe, (1 << port->id));
3657 mvpp2_prs_hw_write(port->priv, pe);
3658 kfree(pe);
3659
3660 return 0;
3661}
3662
3663/* Classifier configuration routines */
3664
3665/* Update classification flow table registers */
3666static void mvpp2_cls_flow_write(struct mvpp2 *priv,
3667 struct mvpp2_cls_flow_entry *fe)
3668{
3669 mvpp2_write(priv, MVPP2_CLS_FLOW_INDEX_REG, fe->index);
3670 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL0_REG, fe->data[0]);
3671 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL1_REG, fe->data[1]);
3672 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL2_REG, fe->data[2]);
3673}
3674
3675/* Update classification lookup table register */
3676static void mvpp2_cls_lookup_write(struct mvpp2 *priv,
3677 struct mvpp2_cls_lookup_entry *le)
3678{
3679 u32 val;
3680
3681 val = (le->way << MVPP2_CLS_LKP_INDEX_WAY_OFFS) | le->lkpid;
3682 mvpp2_write(priv, MVPP2_CLS_LKP_INDEX_REG, val);
3683 mvpp2_write(priv, MVPP2_CLS_LKP_TBL_REG, le->data);
3684}
3685
3686/* Classifier default initialization */
3687static void mvpp2_cls_init(struct mvpp2 *priv)
3688{
3689 struct mvpp2_cls_lookup_entry le;
3690 struct mvpp2_cls_flow_entry fe;
3691 int index;
3692
3693 /* Enable classifier */
3694 mvpp2_write(priv, MVPP2_CLS_MODE_REG, MVPP2_CLS_MODE_ACTIVE_MASK);
3695
3696 /* Clear classifier flow table */
Arnd Bergmanne8f967c2016-11-24 17:28:12 +01003697 memset(&fe.data, 0, sizeof(fe.data));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003698 for (index = 0; index < MVPP2_CLS_FLOWS_TBL_SIZE; index++) {
3699 fe.index = index;
3700 mvpp2_cls_flow_write(priv, &fe);
3701 }
3702
3703 /* Clear classifier lookup table */
3704 le.data = 0;
3705 for (index = 0; index < MVPP2_CLS_LKP_TBL_SIZE; index++) {
3706 le.lkpid = index;
3707 le.way = 0;
3708 mvpp2_cls_lookup_write(priv, &le);
3709
3710 le.way = 1;
3711 mvpp2_cls_lookup_write(priv, &le);
3712 }
3713}
3714
3715static void mvpp2_cls_port_config(struct mvpp2_port *port)
3716{
3717 struct mvpp2_cls_lookup_entry le;
3718 u32 val;
3719
3720 /* Set way for the port */
3721 val = mvpp2_read(port->priv, MVPP2_CLS_PORT_WAY_REG);
3722 val &= ~MVPP2_CLS_PORT_WAY_MASK(port->id);
3723 mvpp2_write(port->priv, MVPP2_CLS_PORT_WAY_REG, val);
3724
3725 /* Pick the entry to be accessed in lookup ID decoding table
3726 * according to the way and lkpid.
3727 */
3728 le.lkpid = port->id;
3729 le.way = 0;
3730 le.data = 0;
3731
3732 /* Set initial CPU queue for receiving packets */
3733 le.data &= ~MVPP2_CLS_LKP_TBL_RXQ_MASK;
3734 le.data |= port->first_rxq;
3735
3736 /* Disable classification engines */
3737 le.data &= ~MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK;
3738
3739 /* Update lookup ID table entry */
3740 mvpp2_cls_lookup_write(port->priv, &le);
3741}
3742
3743/* Set CPU queue number for oversize packets */
3744static void mvpp2_cls_oversize_rxq_set(struct mvpp2_port *port)
3745{
3746 u32 val;
3747
3748 mvpp2_write(port->priv, MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port->id),
3749 port->first_rxq & MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK);
3750
3751 mvpp2_write(port->priv, MVPP2_CLS_SWFWD_P2HQ_REG(port->id),
3752 (port->first_rxq >> MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS));
3753
3754 val = mvpp2_read(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG);
3755 val |= MVPP2_CLS_SWFWD_PCTRL_MASK(port->id);
3756 mvpp2_write(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG, val);
3757}
3758
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003759static void *mvpp2_frag_alloc(const struct mvpp2_bm_pool *pool)
3760{
3761 if (likely(pool->frag_size <= PAGE_SIZE))
3762 return netdev_alloc_frag(pool->frag_size);
3763 else
3764 return kmalloc(pool->frag_size, GFP_ATOMIC);
3765}
3766
3767static void mvpp2_frag_free(const struct mvpp2_bm_pool *pool, void *data)
3768{
3769 if (likely(pool->frag_size <= PAGE_SIZE))
3770 skb_free_frag(data);
3771 else
3772 kfree(data);
3773}
3774
Marcin Wojtas3f518502014-07-10 16:52:13 -03003775/* Buffer Manager configuration routines */
3776
3777/* Create pool */
3778static int mvpp2_bm_pool_create(struct platform_device *pdev,
3779 struct mvpp2 *priv,
3780 struct mvpp2_bm_pool *bm_pool, int size)
3781{
Marcin Wojtas3f518502014-07-10 16:52:13 -03003782 u32 val;
3783
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003784 /* Number of buffer pointers must be a multiple of 16, as per
3785 * hardware constraints
3786 */
3787 if (!IS_ALIGNED(size, 16))
3788 return -EINVAL;
3789
3790 /* PPv2.1 needs 8 bytes per buffer pointer, PPv2.2 needs 16
3791 * bytes per buffer pointer
3792 */
3793 if (priv->hw_version == MVPP21)
3794 bm_pool->size_bytes = 2 * sizeof(u32) * size;
3795 else
3796 bm_pool->size_bytes = 2 * sizeof(u64) * size;
3797
3798 bm_pool->virt_addr = dma_alloc_coherent(&pdev->dev, bm_pool->size_bytes,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003799 &bm_pool->dma_addr,
Marcin Wojtas3f518502014-07-10 16:52:13 -03003800 GFP_KERNEL);
3801 if (!bm_pool->virt_addr)
3802 return -ENOMEM;
3803
Thomas Petazzonid3158802017-02-21 11:28:13 +01003804 if (!IS_ALIGNED((unsigned long)bm_pool->virt_addr,
3805 MVPP2_BM_POOL_PTR_ALIGN)) {
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003806 dma_free_coherent(&pdev->dev, bm_pool->size_bytes,
3807 bm_pool->virt_addr, bm_pool->dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003808 dev_err(&pdev->dev, "BM pool %d is not %d bytes aligned\n",
3809 bm_pool->id, MVPP2_BM_POOL_PTR_ALIGN);
3810 return -ENOMEM;
3811 }
3812
3813 mvpp2_write(priv, MVPP2_BM_POOL_BASE_REG(bm_pool->id),
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003814 lower_32_bits(bm_pool->dma_addr));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003815 mvpp2_write(priv, MVPP2_BM_POOL_SIZE_REG(bm_pool->id), size);
3816
3817 val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
3818 val |= MVPP2_BM_START_MASK;
3819 mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
3820
3821 bm_pool->type = MVPP2_BM_FREE;
3822 bm_pool->size = size;
3823 bm_pool->pkt_size = 0;
3824 bm_pool->buf_num = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003825
3826 return 0;
3827}
3828
3829/* Set pool buffer size */
3830static void mvpp2_bm_pool_bufsize_set(struct mvpp2 *priv,
3831 struct mvpp2_bm_pool *bm_pool,
3832 int buf_size)
3833{
3834 u32 val;
3835
3836 bm_pool->buf_size = buf_size;
3837
3838 val = ALIGN(buf_size, 1 << MVPP2_POOL_BUF_SIZE_OFFSET);
3839 mvpp2_write(priv, MVPP2_POOL_BUF_SIZE_REG(bm_pool->id), val);
3840}
3841
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003842static void mvpp2_bm_bufs_get_addrs(struct device *dev, struct mvpp2 *priv,
3843 struct mvpp2_bm_pool *bm_pool,
3844 dma_addr_t *dma_addr,
3845 phys_addr_t *phys_addr)
3846{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02003847 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01003848
3849 *dma_addr = mvpp2_percpu_read(priv, cpu,
3850 MVPP2_BM_PHY_ALLOC_REG(bm_pool->id));
3851 *phys_addr = mvpp2_percpu_read(priv, cpu, MVPP2_BM_VIRT_ALLOC_REG);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003852
3853 if (priv->hw_version == MVPP22) {
3854 u32 val;
3855 u32 dma_addr_highbits, phys_addr_highbits;
3856
Thomas Petazzonia7868412017-03-07 16:53:13 +01003857 val = mvpp2_percpu_read(priv, cpu, MVPP22_BM_ADDR_HIGH_ALLOC);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003858 dma_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_PHYS_MASK);
3859 phys_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_VIRT_MASK) >>
3860 MVPP22_BM_ADDR_HIGH_VIRT_SHIFT;
3861
3862 if (sizeof(dma_addr_t) == 8)
3863 *dma_addr |= (u64)dma_addr_highbits << 32;
3864
3865 if (sizeof(phys_addr_t) == 8)
3866 *phys_addr |= (u64)phys_addr_highbits << 32;
3867 }
Thomas Petazzonia704bb52017-06-10 23:18:22 +02003868
3869 put_cpu();
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003870}
3871
Ezequiel Garcia7861f122014-07-21 13:48:14 -03003872/* Free all buffers from the pool */
Marcin Wojtas4229d502015-12-03 15:20:50 +01003873static void mvpp2_bm_bufs_free(struct device *dev, struct mvpp2 *priv,
3874 struct mvpp2_bm_pool *bm_pool)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003875{
3876 int i;
3877
Ezequiel Garcia7861f122014-07-21 13:48:14 -03003878 for (i = 0; i < bm_pool->buf_num; i++) {
Thomas Petazzoni20396132017-03-07 16:53:00 +01003879 dma_addr_t buf_dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003880 phys_addr_t buf_phys_addr;
3881 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003882
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003883 mvpp2_bm_bufs_get_addrs(dev, priv, bm_pool,
3884 &buf_dma_addr, &buf_phys_addr);
Marcin Wojtas4229d502015-12-03 15:20:50 +01003885
Thomas Petazzoni20396132017-03-07 16:53:00 +01003886 dma_unmap_single(dev, buf_dma_addr,
Marcin Wojtas4229d502015-12-03 15:20:50 +01003887 bm_pool->buf_size, DMA_FROM_DEVICE);
3888
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003889 data = (void *)phys_to_virt(buf_phys_addr);
3890 if (!data)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003891 break;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003892
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003893 mvpp2_frag_free(bm_pool, data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003894 }
3895
3896 /* Update BM driver with number of buffers removed from pool */
3897 bm_pool->buf_num -= i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003898}
3899
3900/* Cleanup pool */
3901static int mvpp2_bm_pool_destroy(struct platform_device *pdev,
3902 struct mvpp2 *priv,
3903 struct mvpp2_bm_pool *bm_pool)
3904{
Marcin Wojtas3f518502014-07-10 16:52:13 -03003905 u32 val;
3906
Marcin Wojtas4229d502015-12-03 15:20:50 +01003907 mvpp2_bm_bufs_free(&pdev->dev, priv, bm_pool);
Ezequiel Garciad74c96c2014-07-21 13:48:13 -03003908 if (bm_pool->buf_num) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03003909 WARN(1, "cannot free all buffers in pool %d\n", bm_pool->id);
3910 return 0;
3911 }
3912
3913 val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
3914 val |= MVPP2_BM_STOP_MASK;
3915 mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
3916
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003917 dma_free_coherent(&pdev->dev, bm_pool->size_bytes,
Marcin Wojtas3f518502014-07-10 16:52:13 -03003918 bm_pool->virt_addr,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003919 bm_pool->dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003920 return 0;
3921}
3922
3923static int mvpp2_bm_pools_init(struct platform_device *pdev,
3924 struct mvpp2 *priv)
3925{
3926 int i, err, size;
3927 struct mvpp2_bm_pool *bm_pool;
3928
3929 /* Create all pools with maximum size */
3930 size = MVPP2_BM_POOL_SIZE_MAX;
3931 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
3932 bm_pool = &priv->bm_pools[i];
3933 bm_pool->id = i;
3934 err = mvpp2_bm_pool_create(pdev, priv, bm_pool, size);
3935 if (err)
3936 goto err_unroll_pools;
3937 mvpp2_bm_pool_bufsize_set(priv, bm_pool, 0);
3938 }
3939 return 0;
3940
3941err_unroll_pools:
3942 dev_err(&pdev->dev, "failed to create BM pool %d, size %d\n", i, size);
3943 for (i = i - 1; i >= 0; i--)
3944 mvpp2_bm_pool_destroy(pdev, priv, &priv->bm_pools[i]);
3945 return err;
3946}
3947
3948static int mvpp2_bm_init(struct platform_device *pdev, struct mvpp2 *priv)
3949{
3950 int i, err;
3951
3952 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
3953 /* Mask BM all interrupts */
3954 mvpp2_write(priv, MVPP2_BM_INTR_MASK_REG(i), 0);
3955 /* Clear BM cause register */
3956 mvpp2_write(priv, MVPP2_BM_INTR_CAUSE_REG(i), 0);
3957 }
3958
3959 /* Allocate and initialize BM pools */
3960 priv->bm_pools = devm_kcalloc(&pdev->dev, MVPP2_BM_POOLS_NUM,
Markus Elfring81f915e2017-04-17 09:06:33 +02003961 sizeof(*priv->bm_pools), GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003962 if (!priv->bm_pools)
3963 return -ENOMEM;
3964
3965 err = mvpp2_bm_pools_init(pdev, priv);
3966 if (err < 0)
3967 return err;
3968 return 0;
3969}
3970
3971/* Attach long pool to rxq */
3972static void mvpp2_rxq_long_pool_set(struct mvpp2_port *port,
3973 int lrxq, int long_pool)
3974{
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003975 u32 val, mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003976 int prxq;
3977
3978 /* Get queue physical ID */
3979 prxq = port->rxqs[lrxq]->id;
3980
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003981 if (port->priv->hw_version == MVPP21)
3982 mask = MVPP21_RXQ_POOL_LONG_MASK;
3983 else
3984 mask = MVPP22_RXQ_POOL_LONG_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003985
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003986 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
3987 val &= ~mask;
3988 val |= (long_pool << MVPP2_RXQ_POOL_LONG_OFFS) & mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003989 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
3990}
3991
3992/* Attach short pool to rxq */
3993static void mvpp2_rxq_short_pool_set(struct mvpp2_port *port,
3994 int lrxq, int short_pool)
3995{
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003996 u32 val, mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003997 int prxq;
3998
3999 /* Get queue physical ID */
4000 prxq = port->rxqs[lrxq]->id;
4001
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004002 if (port->priv->hw_version == MVPP21)
4003 mask = MVPP21_RXQ_POOL_SHORT_MASK;
4004 else
4005 mask = MVPP22_RXQ_POOL_SHORT_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004006
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004007 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
4008 val &= ~mask;
4009 val |= (short_pool << MVPP2_RXQ_POOL_SHORT_OFFS) & mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004010 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
4011}
4012
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004013static void *mvpp2_buf_alloc(struct mvpp2_port *port,
4014 struct mvpp2_bm_pool *bm_pool,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004015 dma_addr_t *buf_dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004016 phys_addr_t *buf_phys_addr,
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004017 gfp_t gfp_mask)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004018{
Thomas Petazzoni20396132017-03-07 16:53:00 +01004019 dma_addr_t dma_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004020 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004021
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004022 data = mvpp2_frag_alloc(bm_pool);
4023 if (!data)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004024 return NULL;
4025
Thomas Petazzoni20396132017-03-07 16:53:00 +01004026 dma_addr = dma_map_single(port->dev->dev.parent, data,
4027 MVPP2_RX_BUF_SIZE(bm_pool->pkt_size),
4028 DMA_FROM_DEVICE);
4029 if (unlikely(dma_mapping_error(port->dev->dev.parent, dma_addr))) {
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004030 mvpp2_frag_free(bm_pool, data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004031 return NULL;
4032 }
Thomas Petazzoni20396132017-03-07 16:53:00 +01004033 *buf_dma_addr = dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004034 *buf_phys_addr = virt_to_phys(data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004035
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004036 return data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004037}
4038
Marcin Wojtas3f518502014-07-10 16:52:13 -03004039/* Release buffer to BM */
4040static inline void mvpp2_bm_pool_put(struct mvpp2_port *port, int pool,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004041 dma_addr_t buf_dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004042 phys_addr_t buf_phys_addr)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004043{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004044 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01004045
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004046 if (port->priv->hw_version == MVPP22) {
4047 u32 val = 0;
4048
4049 if (sizeof(dma_addr_t) == 8)
4050 val |= upper_32_bits(buf_dma_addr) &
4051 MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK;
4052
4053 if (sizeof(phys_addr_t) == 8)
4054 val |= (upper_32_bits(buf_phys_addr)
4055 << MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT) &
4056 MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK;
4057
Thomas Petazzonia7868412017-03-07 16:53:13 +01004058 mvpp2_percpu_write(port->priv, cpu,
4059 MVPP22_BM_ADDR_HIGH_RLS_REG, val);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004060 }
4061
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004062 /* MVPP2_BM_VIRT_RLS_REG is not interpreted by HW, and simply
4063 * returned in the "cookie" field of the RX
4064 * descriptor. Instead of storing the virtual address, we
4065 * store the physical address
4066 */
Thomas Petazzonia7868412017-03-07 16:53:13 +01004067 mvpp2_percpu_write(port->priv, cpu,
4068 MVPP2_BM_VIRT_RLS_REG, buf_phys_addr);
4069 mvpp2_percpu_write(port->priv, cpu,
4070 MVPP2_BM_PHY_RLS_REG(pool), buf_dma_addr);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004071
4072 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03004073}
4074
Marcin Wojtas3f518502014-07-10 16:52:13 -03004075/* Allocate buffers for the pool */
4076static int mvpp2_bm_bufs_add(struct mvpp2_port *port,
4077 struct mvpp2_bm_pool *bm_pool, int buf_num)
4078{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004079 int i, buf_size, total_size;
Thomas Petazzoni20396132017-03-07 16:53:00 +01004080 dma_addr_t dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004081 phys_addr_t phys_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004082 void *buf;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004083
4084 buf_size = MVPP2_RX_BUF_SIZE(bm_pool->pkt_size);
4085 total_size = MVPP2_RX_TOTAL_SIZE(buf_size);
4086
4087 if (buf_num < 0 ||
4088 (buf_num + bm_pool->buf_num > bm_pool->size)) {
4089 netdev_err(port->dev,
4090 "cannot allocate %d buffers for pool %d\n",
4091 buf_num, bm_pool->id);
4092 return 0;
4093 }
4094
Marcin Wojtas3f518502014-07-10 16:52:13 -03004095 for (i = 0; i < buf_num; i++) {
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004096 buf = mvpp2_buf_alloc(port, bm_pool, &dma_addr,
4097 &phys_addr, GFP_KERNEL);
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004098 if (!buf)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004099 break;
4100
Thomas Petazzoni20396132017-03-07 16:53:00 +01004101 mvpp2_bm_pool_put(port, bm_pool->id, dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004102 phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004103 }
4104
4105 /* Update BM driver with number of buffers added to pool */
4106 bm_pool->buf_num += i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004107
4108 netdev_dbg(port->dev,
4109 "%s pool %d: pkt_size=%4d, buf_size=%4d, total_size=%4d\n",
4110 bm_pool->type == MVPP2_BM_SWF_SHORT ? "short" : " long",
4111 bm_pool->id, bm_pool->pkt_size, buf_size, total_size);
4112
4113 netdev_dbg(port->dev,
4114 "%s pool %d: %d of %d buffers added\n",
4115 bm_pool->type == MVPP2_BM_SWF_SHORT ? "short" : " long",
4116 bm_pool->id, i, buf_num);
4117 return i;
4118}
4119
4120/* Notify the driver that BM pool is being used as specific type and return the
4121 * pool pointer on success
4122 */
4123static struct mvpp2_bm_pool *
4124mvpp2_bm_pool_use(struct mvpp2_port *port, int pool, enum mvpp2_bm_type type,
4125 int pkt_size)
4126{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004127 struct mvpp2_bm_pool *new_pool = &port->priv->bm_pools[pool];
4128 int num;
4129
4130 if (new_pool->type != MVPP2_BM_FREE && new_pool->type != type) {
4131 netdev_err(port->dev, "mixing pool types is forbidden\n");
4132 return NULL;
4133 }
4134
Marcin Wojtas3f518502014-07-10 16:52:13 -03004135 if (new_pool->type == MVPP2_BM_FREE)
4136 new_pool->type = type;
4137
4138 /* Allocate buffers in case BM pool is used as long pool, but packet
4139 * size doesn't match MTU or BM pool hasn't being used yet
4140 */
4141 if (((type == MVPP2_BM_SWF_LONG) && (pkt_size > new_pool->pkt_size)) ||
4142 (new_pool->pkt_size == 0)) {
4143 int pkts_num;
4144
4145 /* Set default buffer number or free all the buffers in case
4146 * the pool is not empty
4147 */
4148 pkts_num = new_pool->buf_num;
4149 if (pkts_num == 0)
4150 pkts_num = type == MVPP2_BM_SWF_LONG ?
4151 MVPP2_BM_LONG_BUF_NUM :
4152 MVPP2_BM_SHORT_BUF_NUM;
4153 else
Marcin Wojtas4229d502015-12-03 15:20:50 +01004154 mvpp2_bm_bufs_free(port->dev->dev.parent,
4155 port->priv, new_pool);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004156
4157 new_pool->pkt_size = pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004158 new_pool->frag_size =
4159 SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
4160 MVPP2_SKB_SHINFO_SIZE;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004161
4162 /* Allocate buffers for this pool */
4163 num = mvpp2_bm_bufs_add(port, new_pool, pkts_num);
4164 if (num != pkts_num) {
4165 WARN(1, "pool %d: %d of %d allocated\n",
4166 new_pool->id, num, pkts_num);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004167 return NULL;
4168 }
4169 }
4170
4171 mvpp2_bm_pool_bufsize_set(port->priv, new_pool,
4172 MVPP2_RX_BUF_SIZE(new_pool->pkt_size));
4173
Marcin Wojtas3f518502014-07-10 16:52:13 -03004174 return new_pool;
4175}
4176
4177/* Initialize pools for swf */
4178static int mvpp2_swf_bm_pool_init(struct mvpp2_port *port)
4179{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004180 int rxq;
4181
4182 if (!port->pool_long) {
4183 port->pool_long =
4184 mvpp2_bm_pool_use(port, MVPP2_BM_SWF_LONG_POOL(port->id),
4185 MVPP2_BM_SWF_LONG,
4186 port->pkt_size);
4187 if (!port->pool_long)
4188 return -ENOMEM;
4189
Marcin Wojtas3f518502014-07-10 16:52:13 -03004190 port->pool_long->port_map |= (1 << port->id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004191
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004192 for (rxq = 0; rxq < port->nrxqs; rxq++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004193 mvpp2_rxq_long_pool_set(port, rxq, port->pool_long->id);
4194 }
4195
4196 if (!port->pool_short) {
4197 port->pool_short =
4198 mvpp2_bm_pool_use(port, MVPP2_BM_SWF_SHORT_POOL,
4199 MVPP2_BM_SWF_SHORT,
4200 MVPP2_BM_SHORT_PKT_SIZE);
4201 if (!port->pool_short)
4202 return -ENOMEM;
4203
Marcin Wojtas3f518502014-07-10 16:52:13 -03004204 port->pool_short->port_map |= (1 << port->id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004205
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004206 for (rxq = 0; rxq < port->nrxqs; rxq++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004207 mvpp2_rxq_short_pool_set(port, rxq,
4208 port->pool_short->id);
4209 }
4210
4211 return 0;
4212}
4213
4214static int mvpp2_bm_update_mtu(struct net_device *dev, int mtu)
4215{
4216 struct mvpp2_port *port = netdev_priv(dev);
4217 struct mvpp2_bm_pool *port_pool = port->pool_long;
4218 int num, pkts_num = port_pool->buf_num;
4219 int pkt_size = MVPP2_RX_PKT_SIZE(mtu);
4220
4221 /* Update BM pool with new buffer size */
Marcin Wojtas4229d502015-12-03 15:20:50 +01004222 mvpp2_bm_bufs_free(dev->dev.parent, port->priv, port_pool);
Ezequiel Garciad74c96c2014-07-21 13:48:13 -03004223 if (port_pool->buf_num) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004224 WARN(1, "cannot free all buffers in pool %d\n", port_pool->id);
4225 return -EIO;
4226 }
4227
4228 port_pool->pkt_size = pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004229 port_pool->frag_size = SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
4230 MVPP2_SKB_SHINFO_SIZE;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004231 num = mvpp2_bm_bufs_add(port, port_pool, pkts_num);
4232 if (num != pkts_num) {
4233 WARN(1, "pool %d: %d of %d allocated\n",
4234 port_pool->id, num, pkts_num);
4235 return -EIO;
4236 }
4237
4238 mvpp2_bm_pool_bufsize_set(port->priv, port_pool,
4239 MVPP2_RX_BUF_SIZE(port_pool->pkt_size));
4240 dev->mtu = mtu;
4241 netdev_update_features(dev);
4242 return 0;
4243}
4244
4245static inline void mvpp2_interrupts_enable(struct mvpp2_port *port)
4246{
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004247 int i, sw_thread_mask = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004248
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004249 for (i = 0; i < port->nqvecs; i++)
4250 sw_thread_mask |= port->qvecs[i].sw_thread_mask;
4251
Marcin Wojtas3f518502014-07-10 16:52:13 -03004252 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004253 MVPP2_ISR_ENABLE_INTERRUPT(sw_thread_mask));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004254}
4255
4256static inline void mvpp2_interrupts_disable(struct mvpp2_port *port)
4257{
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004258 int i, sw_thread_mask = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004259
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004260 for (i = 0; i < port->nqvecs; i++)
4261 sw_thread_mask |= port->qvecs[i].sw_thread_mask;
4262
Marcin Wojtas3f518502014-07-10 16:52:13 -03004263 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004264 MVPP2_ISR_DISABLE_INTERRUPT(sw_thread_mask));
4265}
4266
4267static inline void mvpp2_qvec_interrupt_enable(struct mvpp2_queue_vector *qvec)
4268{
4269 struct mvpp2_port *port = qvec->port;
4270
4271 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
4272 MVPP2_ISR_ENABLE_INTERRUPT(qvec->sw_thread_mask));
4273}
4274
4275static inline void mvpp2_qvec_interrupt_disable(struct mvpp2_queue_vector *qvec)
4276{
4277 struct mvpp2_port *port = qvec->port;
4278
4279 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
4280 MVPP2_ISR_DISABLE_INTERRUPT(qvec->sw_thread_mask));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004281}
4282
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004283/* Mask the current CPU's Rx/Tx interrupts
4284 * Called by on_each_cpu(), guaranteed to run with migration disabled,
4285 * using smp_processor_id() is OK.
4286 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004287static void mvpp2_interrupts_mask(void *arg)
4288{
4289 struct mvpp2_port *port = arg;
4290
Thomas Petazzonia7868412017-03-07 16:53:13 +01004291 mvpp2_percpu_write(port->priv, smp_processor_id(),
4292 MVPP2_ISR_RX_TX_MASK_REG(port->id), 0);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004293}
4294
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004295/* Unmask the current CPU's Rx/Tx interrupts.
4296 * Called by on_each_cpu(), guaranteed to run with migration disabled,
4297 * using smp_processor_id() is OK.
4298 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004299static void mvpp2_interrupts_unmask(void *arg)
4300{
4301 struct mvpp2_port *port = arg;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02004302 u32 val;
4303
4304 val = MVPP2_CAUSE_MISC_SUM_MASK |
4305 MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
4306 if (port->has_tx_irqs)
4307 val |= MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004308
Thomas Petazzonia7868412017-03-07 16:53:13 +01004309 mvpp2_percpu_write(port->priv, smp_processor_id(),
Thomas Petazzoni213f4282017-08-03 10:42:00 +02004310 MVPP2_ISR_RX_TX_MASK_REG(port->id), val);
4311}
4312
4313static void
4314mvpp2_shared_interrupt_mask_unmask(struct mvpp2_port *port, bool mask)
4315{
4316 u32 val;
4317 int i;
4318
4319 if (port->priv->hw_version != MVPP22)
4320 return;
4321
4322 if (mask)
4323 val = 0;
4324 else
4325 val = MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
4326
4327 for (i = 0; i < port->nqvecs; i++) {
4328 struct mvpp2_queue_vector *v = port->qvecs + i;
4329
4330 if (v->type != MVPP2_QUEUE_VECTOR_SHARED)
4331 continue;
4332
4333 mvpp2_percpu_write(port->priv, v->sw_thread_id,
4334 MVPP2_ISR_RX_TX_MASK_REG(port->id), val);
4335 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004336}
4337
4338/* Port configuration routines */
4339
Antoine Ténartf84bf382017-08-22 19:08:27 +02004340static void mvpp22_gop_init_rgmii(struct mvpp2_port *port)
4341{
4342 struct mvpp2 *priv = port->priv;
4343 u32 val;
4344
4345 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
4346 val |= GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT;
4347 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
4348
4349 regmap_read(priv->sysctrl_base, GENCONF_CTRL0, &val);
4350 if (port->gop_id == 2)
4351 val |= GENCONF_CTRL0_PORT0_RGMII | GENCONF_CTRL0_PORT1_RGMII;
4352 else if (port->gop_id == 3)
4353 val |= GENCONF_CTRL0_PORT1_RGMII_MII;
4354 regmap_write(priv->sysctrl_base, GENCONF_CTRL0, val);
4355}
4356
4357static void mvpp22_gop_init_sgmii(struct mvpp2_port *port)
4358{
4359 struct mvpp2 *priv = port->priv;
4360 u32 val;
4361
4362 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
4363 val |= GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT |
4364 GENCONF_PORT_CTRL0_RX_DATA_SAMPLE;
4365 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
4366
4367 if (port->gop_id > 1) {
4368 regmap_read(priv->sysctrl_base, GENCONF_CTRL0, &val);
4369 if (port->gop_id == 2)
4370 val &= ~GENCONF_CTRL0_PORT0_RGMII;
4371 else if (port->gop_id == 3)
4372 val &= ~GENCONF_CTRL0_PORT1_RGMII_MII;
4373 regmap_write(priv->sysctrl_base, GENCONF_CTRL0, val);
4374 }
4375}
4376
4377static void mvpp22_gop_init_10gkr(struct mvpp2_port *port)
4378{
4379 struct mvpp2 *priv = port->priv;
4380 void __iomem *mpcs = priv->iface_base + MVPP22_MPCS_BASE(port->gop_id);
4381 void __iomem *xpcs = priv->iface_base + MVPP22_XPCS_BASE(port->gop_id);
4382 u32 val;
4383
4384 /* XPCS */
4385 val = readl(xpcs + MVPP22_XPCS_CFG0);
4386 val &= ~(MVPP22_XPCS_CFG0_PCS_MODE(0x3) |
4387 MVPP22_XPCS_CFG0_ACTIVE_LANE(0x3));
4388 val |= MVPP22_XPCS_CFG0_ACTIVE_LANE(2);
4389 writel(val, xpcs + MVPP22_XPCS_CFG0);
4390
4391 /* MPCS */
4392 val = readl(mpcs + MVPP22_MPCS_CTRL);
4393 val &= ~MVPP22_MPCS_CTRL_FWD_ERR_CONN;
4394 writel(val, mpcs + MVPP22_MPCS_CTRL);
4395
4396 val = readl(mpcs + MVPP22_MPCS_CLK_RESET);
4397 val &= ~(MVPP22_MPCS_CLK_RESET_DIV_RATIO(0x7) | MAC_CLK_RESET_MAC |
4398 MAC_CLK_RESET_SD_RX | MAC_CLK_RESET_SD_TX);
4399 val |= MVPP22_MPCS_CLK_RESET_DIV_RATIO(1);
4400 writel(val, mpcs + MVPP22_MPCS_CLK_RESET);
4401
4402 val &= ~MVPP22_MPCS_CLK_RESET_DIV_SET;
4403 val |= MAC_CLK_RESET_MAC | MAC_CLK_RESET_SD_RX | MAC_CLK_RESET_SD_TX;
4404 writel(val, mpcs + MVPP22_MPCS_CLK_RESET);
4405}
4406
4407static int mvpp22_gop_init(struct mvpp2_port *port)
4408{
4409 struct mvpp2 *priv = port->priv;
4410 u32 val;
4411
4412 if (!priv->sysctrl_base)
4413 return 0;
4414
4415 switch (port->phy_interface) {
4416 case PHY_INTERFACE_MODE_RGMII:
4417 case PHY_INTERFACE_MODE_RGMII_ID:
4418 case PHY_INTERFACE_MODE_RGMII_RXID:
4419 case PHY_INTERFACE_MODE_RGMII_TXID:
4420 if (port->gop_id == 0)
4421 goto invalid_conf;
4422 mvpp22_gop_init_rgmii(port);
4423 break;
4424 case PHY_INTERFACE_MODE_SGMII:
4425 mvpp22_gop_init_sgmii(port);
4426 break;
4427 case PHY_INTERFACE_MODE_10GKR:
4428 if (port->gop_id != 0)
4429 goto invalid_conf;
4430 mvpp22_gop_init_10gkr(port);
4431 break;
4432 default:
4433 goto unsupported_conf;
4434 }
4435
4436 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL1, &val);
4437 val |= GENCONF_PORT_CTRL1_RESET(port->gop_id) |
4438 GENCONF_PORT_CTRL1_EN(port->gop_id);
4439 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL1, val);
4440
4441 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
4442 val |= GENCONF_PORT_CTRL0_CLK_DIV_PHASE_CLR;
4443 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
4444
4445 regmap_read(priv->sysctrl_base, GENCONF_SOFT_RESET1, &val);
4446 val |= GENCONF_SOFT_RESET1_GOP;
4447 regmap_write(priv->sysctrl_base, GENCONF_SOFT_RESET1, val);
4448
4449unsupported_conf:
4450 return 0;
4451
4452invalid_conf:
4453 netdev_err(port->dev, "Invalid port configuration\n");
4454 return -EINVAL;
4455}
4456
Antoine Tenartfd3651b2017-09-01 11:04:54 +02004457static void mvpp22_gop_unmask_irq(struct mvpp2_port *port)
4458{
4459 u32 val;
4460
4461 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
4462 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4463 /* Enable the GMAC link status irq for this port */
4464 val = readl(port->base + MVPP22_GMAC_INT_SUM_MASK);
4465 val |= MVPP22_GMAC_INT_SUM_MASK_LINK_STAT;
4466 writel(val, port->base + MVPP22_GMAC_INT_SUM_MASK);
4467 }
4468
4469 if (port->gop_id == 0) {
4470 /* Enable the XLG/GIG irqs for this port */
4471 val = readl(port->base + MVPP22_XLG_EXT_INT_MASK);
4472 if (port->phy_interface == PHY_INTERFACE_MODE_10GKR)
4473 val |= MVPP22_XLG_EXT_INT_MASK_XLG;
4474 else
4475 val |= MVPP22_XLG_EXT_INT_MASK_GIG;
4476 writel(val, port->base + MVPP22_XLG_EXT_INT_MASK);
4477 }
4478}
4479
4480static void mvpp22_gop_mask_irq(struct mvpp2_port *port)
4481{
4482 u32 val;
4483
4484 if (port->gop_id == 0) {
4485 val = readl(port->base + MVPP22_XLG_EXT_INT_MASK);
4486 val &= ~(MVPP22_XLG_EXT_INT_MASK_XLG |
4487 MVPP22_XLG_EXT_INT_MASK_GIG);
4488 writel(val, port->base + MVPP22_XLG_EXT_INT_MASK);
4489 }
4490
4491 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
4492 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4493 val = readl(port->base + MVPP22_GMAC_INT_SUM_MASK);
4494 val &= ~MVPP22_GMAC_INT_SUM_MASK_LINK_STAT;
4495 writel(val, port->base + MVPP22_GMAC_INT_SUM_MASK);
4496 }
4497}
4498
4499static void mvpp22_gop_setup_irq(struct mvpp2_port *port)
4500{
4501 u32 val;
4502
4503 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
4504 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4505 val = readl(port->base + MVPP22_GMAC_INT_MASK);
4506 val |= MVPP22_GMAC_INT_MASK_LINK_STAT;
4507 writel(val, port->base + MVPP22_GMAC_INT_MASK);
4508 }
4509
4510 if (port->gop_id == 0) {
4511 val = readl(port->base + MVPP22_XLG_INT_MASK);
4512 val |= MVPP22_XLG_INT_MASK_LINK;
4513 writel(val, port->base + MVPP22_XLG_INT_MASK);
4514 }
4515
4516 mvpp22_gop_unmask_irq(port);
4517}
4518
Antoine Tenart542897d2017-08-30 10:29:15 +02004519static int mvpp22_comphy_init(struct mvpp2_port *port)
4520{
4521 enum phy_mode mode;
4522 int ret;
4523
4524 if (!port->comphy)
4525 return 0;
4526
4527 switch (port->phy_interface) {
4528 case PHY_INTERFACE_MODE_SGMII:
4529 mode = PHY_MODE_SGMII;
4530 break;
4531 case PHY_INTERFACE_MODE_10GKR:
4532 mode = PHY_MODE_10GKR;
4533 break;
4534 default:
4535 return -EINVAL;
4536 }
4537
4538 ret = phy_set_mode(port->comphy, mode);
4539 if (ret)
4540 return ret;
4541
4542 return phy_power_on(port->comphy);
4543}
4544
Antoine Ténart39193572017-08-22 19:08:24 +02004545static void mvpp2_port_mii_gmac_configure_mode(struct mvpp2_port *port)
4546{
4547 u32 val;
4548
4549 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4550 val = readl(port->base + MVPP22_GMAC_CTRL_4_REG);
4551 val |= MVPP22_CTRL4_SYNC_BYPASS_DIS | MVPP22_CTRL4_DP_CLK_SEL |
4552 MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
4553 val &= ~MVPP22_CTRL4_EXT_PIN_GMII_SEL;
4554 writel(val, port->base + MVPP22_GMAC_CTRL_4_REG);
4555
4556 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4557 val |= MVPP2_GMAC_DISABLE_PADDING;
4558 val &= ~MVPP2_GMAC_FLOW_CTRL_MASK;
4559 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
Antoine Tenart1df22702017-09-01 11:04:52 +02004560 } else if (phy_interface_mode_is_rgmii(port->phy_interface)) {
Antoine Ténart39193572017-08-22 19:08:24 +02004561 val = readl(port->base + MVPP22_GMAC_CTRL_4_REG);
4562 val |= MVPP22_CTRL4_EXT_PIN_GMII_SEL |
4563 MVPP22_CTRL4_SYNC_BYPASS_DIS |
4564 MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
4565 val &= ~MVPP22_CTRL4_DP_CLK_SEL;
4566 writel(val, port->base + MVPP22_GMAC_CTRL_4_REG);
4567
4568 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4569 val &= ~MVPP2_GMAC_DISABLE_PADDING;
4570 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4571 }
4572
4573 /* The port is connected to a copper PHY */
4574 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4575 val &= ~MVPP2_GMAC_PORT_TYPE_MASK;
4576 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4577
4578 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4579 val |= MVPP2_GMAC_IN_BAND_AUTONEG_BYPASS |
4580 MVPP2_GMAC_AN_SPEED_EN | MVPP2_GMAC_FLOW_CTRL_AUTONEG |
4581 MVPP2_GMAC_AN_DUPLEX_EN;
4582 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
4583 val |= MVPP2_GMAC_IN_BAND_AUTONEG;
4584 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4585}
4586
4587static void mvpp2_port_mii_gmac_configure(struct mvpp2_port *port)
4588{
4589 u32 val;
4590
4591 /* Force link down */
4592 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4593 val &= ~MVPP2_GMAC_FORCE_LINK_PASS;
4594 val |= MVPP2_GMAC_FORCE_LINK_DOWN;
4595 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4596
4597 /* Set the GMAC in a reset state */
4598 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4599 val |= MVPP2_GMAC_PORT_RESET_MASK;
4600 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4601
4602 /* Configure the PCS and in-band AN */
4603 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4604 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4605 val |= MVPP2_GMAC_INBAND_AN_MASK | MVPP2_GMAC_PCS_ENABLE_MASK;
Antoine Tenart1df22702017-09-01 11:04:52 +02004606 } else if (phy_interface_mode_is_rgmii(port->phy_interface)) {
Antoine Ténart39193572017-08-22 19:08:24 +02004607 val &= ~MVPP2_GMAC_PCS_ENABLE_MASK;
Antoine Ténart39193572017-08-22 19:08:24 +02004608 }
4609 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4610
4611 mvpp2_port_mii_gmac_configure_mode(port);
4612
4613 /* Unset the GMAC reset state */
4614 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4615 val &= ~MVPP2_GMAC_PORT_RESET_MASK;
4616 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4617
4618 /* Stop forcing link down */
4619 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4620 val &= ~MVPP2_GMAC_FORCE_LINK_DOWN;
4621 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4622}
4623
Antoine Ténart77321952017-08-22 19:08:25 +02004624static void mvpp2_port_mii_xlg_configure(struct mvpp2_port *port)
4625{
4626 u32 val;
4627
4628 if (port->gop_id != 0)
4629 return;
4630
4631 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
4632 val |= MVPP22_XLG_CTRL0_RX_FLOW_CTRL_EN;
4633 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
4634
4635 val = readl(port->base + MVPP22_XLG_CTRL4_REG);
4636 val &= ~MVPP22_XLG_CTRL4_MACMODSELECT_GMAC;
4637 val |= MVPP22_XLG_CTRL4_FWD_FC | MVPP22_XLG_CTRL4_FWD_PFC;
4638 writel(val, port->base + MVPP22_XLG_CTRL4_REG);
4639}
4640
Thomas Petazzoni26975822017-03-07 16:53:14 +01004641static void mvpp22_port_mii_set(struct mvpp2_port *port)
4642{
4643 u32 val;
4644
Thomas Petazzoni26975822017-03-07 16:53:14 +01004645 /* Only GOP port 0 has an XLG MAC */
4646 if (port->gop_id == 0) {
4647 val = readl(port->base + MVPP22_XLG_CTRL3_REG);
4648 val &= ~MVPP22_XLG_CTRL3_MACMODESELECT_MASK;
Antoine Ténart725757a2017-06-12 16:01:39 +02004649
4650 if (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
4651 port->phy_interface == PHY_INTERFACE_MODE_10GKR)
4652 val |= MVPP22_XLG_CTRL3_MACMODESELECT_10G;
4653 else
4654 val |= MVPP22_XLG_CTRL3_MACMODESELECT_GMAC;
4655
Thomas Petazzoni26975822017-03-07 16:53:14 +01004656 writel(val, port->base + MVPP22_XLG_CTRL3_REG);
4657 }
Thomas Petazzoni26975822017-03-07 16:53:14 +01004658}
4659
Marcin Wojtas3f518502014-07-10 16:52:13 -03004660static void mvpp2_port_mii_set(struct mvpp2_port *port)
4661{
Thomas Petazzoni26975822017-03-07 16:53:14 +01004662 if (port->priv->hw_version == MVPP22)
4663 mvpp22_port_mii_set(port);
4664
Antoine Tenart1df22702017-09-01 11:04:52 +02004665 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
Antoine Ténart39193572017-08-22 19:08:24 +02004666 port->phy_interface == PHY_INTERFACE_MODE_SGMII)
4667 mvpp2_port_mii_gmac_configure(port);
Antoine Ténart77321952017-08-22 19:08:25 +02004668 else if (port->phy_interface == PHY_INTERFACE_MODE_10GKR)
4669 mvpp2_port_mii_xlg_configure(port);
Marcin Wojtas08a23752014-07-21 13:48:12 -03004670}
4671
4672static void mvpp2_port_fc_adv_enable(struct mvpp2_port *port)
4673{
4674 u32 val;
4675
4676 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4677 val |= MVPP2_GMAC_FC_ADV_EN;
4678 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004679}
4680
4681static void mvpp2_port_enable(struct mvpp2_port *port)
4682{
4683 u32 val;
4684
Antoine Ténart725757a2017-06-12 16:01:39 +02004685 /* Only GOP port 0 has an XLG MAC */
4686 if (port->gop_id == 0 &&
4687 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
4688 port->phy_interface == PHY_INTERFACE_MODE_10GKR)) {
4689 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
4690 val |= MVPP22_XLG_CTRL0_PORT_EN |
4691 MVPP22_XLG_CTRL0_MAC_RESET_DIS;
4692 val &= ~MVPP22_XLG_CTRL0_MIB_CNT_DIS;
4693 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
4694 } else {
4695 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4696 val |= MVPP2_GMAC_PORT_EN_MASK;
4697 val |= MVPP2_GMAC_MIB_CNTR_EN_MASK;
4698 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4699 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004700}
4701
4702static void mvpp2_port_disable(struct mvpp2_port *port)
4703{
4704 u32 val;
4705
Antoine Ténart725757a2017-06-12 16:01:39 +02004706 /* Only GOP port 0 has an XLG MAC */
4707 if (port->gop_id == 0 &&
4708 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
4709 port->phy_interface == PHY_INTERFACE_MODE_10GKR)) {
4710 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
4711 val &= ~(MVPP22_XLG_CTRL0_PORT_EN |
4712 MVPP22_XLG_CTRL0_MAC_RESET_DIS);
4713 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
4714 } else {
4715 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4716 val &= ~(MVPP2_GMAC_PORT_EN_MASK);
4717 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4718 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004719}
4720
4721/* Set IEEE 802.3x Flow Control Xon Packet Transmission Mode */
4722static void mvpp2_port_periodic_xon_disable(struct mvpp2_port *port)
4723{
4724 u32 val;
4725
4726 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG) &
4727 ~MVPP2_GMAC_PERIODIC_XON_EN_MASK;
4728 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
4729}
4730
4731/* Configure loopback port */
4732static void mvpp2_port_loopback_set(struct mvpp2_port *port)
4733{
4734 u32 val;
4735
4736 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
4737
4738 if (port->speed == 1000)
4739 val |= MVPP2_GMAC_GMII_LB_EN_MASK;
4740 else
4741 val &= ~MVPP2_GMAC_GMII_LB_EN_MASK;
4742
4743 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
4744 val |= MVPP2_GMAC_PCS_LB_EN_MASK;
4745 else
4746 val &= ~MVPP2_GMAC_PCS_LB_EN_MASK;
4747
4748 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
4749}
4750
4751static void mvpp2_port_reset(struct mvpp2_port *port)
4752{
4753 u32 val;
4754
4755 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
4756 ~MVPP2_GMAC_PORT_RESET_MASK;
4757 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4758
4759 while (readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
4760 MVPP2_GMAC_PORT_RESET_MASK)
4761 continue;
4762}
4763
4764/* Change maximum receive size of the port */
4765static inline void mvpp2_gmac_max_rx_size_set(struct mvpp2_port *port)
4766{
4767 u32 val;
4768
4769 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4770 val &= ~MVPP2_GMAC_MAX_RX_SIZE_MASK;
4771 val |= (((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
4772 MVPP2_GMAC_MAX_RX_SIZE_OFFS);
4773 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4774}
4775
Stefan Chulski76eb1b12017-08-22 19:08:26 +02004776/* Change maximum receive size of the port */
4777static inline void mvpp2_xlg_max_rx_size_set(struct mvpp2_port *port)
4778{
4779 u32 val;
4780
4781 val = readl(port->base + MVPP22_XLG_CTRL1_REG);
4782 val &= ~MVPP22_XLG_CTRL1_FRAMESIZELIMIT_MASK;
4783 val |= ((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
Antoine Ténartec15ecd2017-08-25 15:24:46 +02004784 MVPP22_XLG_CTRL1_FRAMESIZELIMIT_OFFS;
Stefan Chulski76eb1b12017-08-22 19:08:26 +02004785 writel(val, port->base + MVPP22_XLG_CTRL1_REG);
4786}
4787
Marcin Wojtas3f518502014-07-10 16:52:13 -03004788/* Set defaults to the MVPP2 port */
4789static void mvpp2_defaults_set(struct mvpp2_port *port)
4790{
4791 int tx_port_num, val, queue, ptxq, lrxq;
4792
Thomas Petazzoni3d9017d2017-03-07 16:53:11 +01004793 if (port->priv->hw_version == MVPP21) {
4794 /* Configure port to loopback if needed */
4795 if (port->flags & MVPP2_F_LOOPBACK)
4796 mvpp2_port_loopback_set(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004797
Thomas Petazzoni3d9017d2017-03-07 16:53:11 +01004798 /* Update TX FIFO MIN Threshold */
4799 val = readl(port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
4800 val &= ~MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK;
4801 /* Min. TX threshold must be less than minimal packet length */
4802 val |= MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(64 - 4 - 2);
4803 writel(val, port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
4804 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004805
4806 /* Disable Legacy WRR, Disable EJP, Release from reset */
4807 tx_port_num = mvpp2_egress_port(port);
4808 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG,
4809 tx_port_num);
4810 mvpp2_write(port->priv, MVPP2_TXP_SCHED_CMD_1_REG, 0);
4811
4812 /* Close bandwidth for all queues */
4813 for (queue = 0; queue < MVPP2_MAX_TXQ; queue++) {
4814 ptxq = mvpp2_txq_phys(port->id, queue);
4815 mvpp2_write(port->priv,
4816 MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(ptxq), 0);
4817 }
4818
4819 /* Set refill period to 1 usec, refill tokens
4820 * and bucket size to maximum
4821 */
4822 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PERIOD_REG,
4823 port->priv->tclk / USEC_PER_SEC);
4824 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_REFILL_REG);
4825 val &= ~MVPP2_TXP_REFILL_PERIOD_ALL_MASK;
4826 val |= MVPP2_TXP_REFILL_PERIOD_MASK(1);
4827 val |= MVPP2_TXP_REFILL_TOKENS_ALL_MASK;
4828 mvpp2_write(port->priv, MVPP2_TXP_SCHED_REFILL_REG, val);
4829 val = MVPP2_TXP_TOKEN_SIZE_MAX;
4830 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
4831
4832 /* Set MaximumLowLatencyPacketSize value to 256 */
4833 mvpp2_write(port->priv, MVPP2_RX_CTRL_REG(port->id),
4834 MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK |
4835 MVPP2_RX_LOW_LATENCY_PKT_SIZE(256));
4836
4837 /* Enable Rx cache snoop */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004838 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004839 queue = port->rxqs[lrxq]->id;
4840 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
4841 val |= MVPP2_SNOOP_PKT_SIZE_MASK |
4842 MVPP2_SNOOP_BUF_HDR_MASK;
4843 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
4844 }
4845
4846 /* At default, mask all interrupts to all present cpus */
4847 mvpp2_interrupts_disable(port);
4848}
4849
4850/* Enable/disable receiving packets */
4851static void mvpp2_ingress_enable(struct mvpp2_port *port)
4852{
4853 u32 val;
4854 int lrxq, queue;
4855
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004856 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004857 queue = port->rxqs[lrxq]->id;
4858 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
4859 val &= ~MVPP2_RXQ_DISABLE_MASK;
4860 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
4861 }
4862}
4863
4864static void mvpp2_ingress_disable(struct mvpp2_port *port)
4865{
4866 u32 val;
4867 int lrxq, queue;
4868
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004869 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004870 queue = port->rxqs[lrxq]->id;
4871 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
4872 val |= MVPP2_RXQ_DISABLE_MASK;
4873 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
4874 }
4875}
4876
4877/* Enable transmit via physical egress queue
4878 * - HW starts take descriptors from DRAM
4879 */
4880static void mvpp2_egress_enable(struct mvpp2_port *port)
4881{
4882 u32 qmap;
4883 int queue;
4884 int tx_port_num = mvpp2_egress_port(port);
4885
4886 /* Enable all initialized TXs. */
4887 qmap = 0;
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004888 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004889 struct mvpp2_tx_queue *txq = port->txqs[queue];
4890
Markus Elfringdbbb2f02017-04-17 14:07:52 +02004891 if (txq->descs)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004892 qmap |= (1 << queue);
4893 }
4894
4895 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
4896 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG, qmap);
4897}
4898
4899/* Disable transmit via physical egress queue
4900 * - HW doesn't take descriptors from DRAM
4901 */
4902static void mvpp2_egress_disable(struct mvpp2_port *port)
4903{
4904 u32 reg_data;
4905 int delay;
4906 int tx_port_num = mvpp2_egress_port(port);
4907
4908 /* Issue stop command for active channels only */
4909 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
4910 reg_data = (mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG)) &
4911 MVPP2_TXP_SCHED_ENQ_MASK;
4912 if (reg_data != 0)
4913 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG,
4914 (reg_data << MVPP2_TXP_SCHED_DISQ_OFFSET));
4915
4916 /* Wait for all Tx activity to terminate. */
4917 delay = 0;
4918 do {
4919 if (delay >= MVPP2_TX_DISABLE_TIMEOUT_MSEC) {
4920 netdev_warn(port->dev,
4921 "Tx stop timed out, status=0x%08x\n",
4922 reg_data);
4923 break;
4924 }
4925 mdelay(1);
4926 delay++;
4927
4928 /* Check port TX Command register that all
4929 * Tx queues are stopped
4930 */
4931 reg_data = mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG);
4932 } while (reg_data & MVPP2_TXP_SCHED_ENQ_MASK);
4933}
4934
4935/* Rx descriptors helper methods */
4936
4937/* Get number of Rx descriptors occupied by received packets */
4938static inline int
4939mvpp2_rxq_received(struct mvpp2_port *port, int rxq_id)
4940{
4941 u32 val = mvpp2_read(port->priv, MVPP2_RXQ_STATUS_REG(rxq_id));
4942
4943 return val & MVPP2_RXQ_OCCUPIED_MASK;
4944}
4945
4946/* Update Rx queue status with the number of occupied and available
4947 * Rx descriptor slots.
4948 */
4949static inline void
4950mvpp2_rxq_status_update(struct mvpp2_port *port, int rxq_id,
4951 int used_count, int free_count)
4952{
4953 /* Decrement the number of used descriptors and increment count
4954 * increment the number of free descriptors.
4955 */
4956 u32 val = used_count | (free_count << MVPP2_RXQ_NUM_NEW_OFFSET);
4957
4958 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_UPDATE_REG(rxq_id), val);
4959}
4960
4961/* Get pointer to next RX descriptor to be processed by SW */
4962static inline struct mvpp2_rx_desc *
4963mvpp2_rxq_next_desc_get(struct mvpp2_rx_queue *rxq)
4964{
4965 int rx_desc = rxq->next_desc_to_proc;
4966
4967 rxq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(rxq, rx_desc);
4968 prefetch(rxq->descs + rxq->next_desc_to_proc);
4969 return rxq->descs + rx_desc;
4970}
4971
4972/* Set rx queue offset */
4973static void mvpp2_rxq_offset_set(struct mvpp2_port *port,
4974 int prxq, int offset)
4975{
4976 u32 val;
4977
4978 /* Convert offset from bytes to units of 32 bytes */
4979 offset = offset >> 5;
4980
4981 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
4982 val &= ~MVPP2_RXQ_PACKET_OFFSET_MASK;
4983
4984 /* Offset is in */
4985 val |= ((offset << MVPP2_RXQ_PACKET_OFFSET_OFFS) &
4986 MVPP2_RXQ_PACKET_OFFSET_MASK);
4987
4988 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
4989}
4990
Marcin Wojtas3f518502014-07-10 16:52:13 -03004991/* Tx descriptors helper methods */
4992
Marcin Wojtas3f518502014-07-10 16:52:13 -03004993/* Get pointer to next Tx descriptor to be processed (send) by HW */
4994static struct mvpp2_tx_desc *
4995mvpp2_txq_next_desc_get(struct mvpp2_tx_queue *txq)
4996{
4997 int tx_desc = txq->next_desc_to_proc;
4998
4999 txq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(txq, tx_desc);
5000 return txq->descs + tx_desc;
5001}
5002
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005003/* Update HW with number of aggregated Tx descriptors to be sent
5004 *
5005 * Called only from mvpp2_tx(), so migration is disabled, using
5006 * smp_processor_id() is OK.
5007 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03005008static void mvpp2_aggr_txq_pend_desc_add(struct mvpp2_port *port, int pending)
5009{
5010 /* aggregated access - relevant TXQ number is written in TX desc */
Thomas Petazzonia7868412017-03-07 16:53:13 +01005011 mvpp2_percpu_write(port->priv, smp_processor_id(),
5012 MVPP2_AGGR_TXQ_UPDATE_REG, pending);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005013}
5014
5015
5016/* Check if there are enough free descriptors in aggregated txq.
5017 * If not, update the number of occupied descriptors and repeat the check.
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005018 *
5019 * Called only from mvpp2_tx(), so migration is disabled, using
5020 * smp_processor_id() is OK.
Marcin Wojtas3f518502014-07-10 16:52:13 -03005021 */
5022static int mvpp2_aggr_desc_num_check(struct mvpp2 *priv,
5023 struct mvpp2_tx_queue *aggr_txq, int num)
5024{
5025 if ((aggr_txq->count + num) > aggr_txq->size) {
5026 /* Update number of occupied aggregated Tx descriptors */
5027 int cpu = smp_processor_id();
5028 u32 val = mvpp2_read(priv, MVPP2_AGGR_TXQ_STATUS_REG(cpu));
5029
5030 aggr_txq->count = val & MVPP2_AGGR_TXQ_PENDING_MASK;
5031 }
5032
5033 if ((aggr_txq->count + num) > aggr_txq->size)
5034 return -ENOMEM;
5035
5036 return 0;
5037}
5038
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005039/* Reserved Tx descriptors allocation request
5040 *
5041 * Called only from mvpp2_txq_reserved_desc_num_proc(), itself called
5042 * only by mvpp2_tx(), so migration is disabled, using
5043 * smp_processor_id() is OK.
5044 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03005045static int mvpp2_txq_alloc_reserved_desc(struct mvpp2 *priv,
5046 struct mvpp2_tx_queue *txq, int num)
5047{
5048 u32 val;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005049 int cpu = smp_processor_id();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005050
5051 val = (txq->id << MVPP2_TXQ_RSVD_REQ_Q_OFFSET) | num;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005052 mvpp2_percpu_write(priv, cpu, MVPP2_TXQ_RSVD_REQ_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005053
Thomas Petazzonia7868412017-03-07 16:53:13 +01005054 val = mvpp2_percpu_read(priv, cpu, MVPP2_TXQ_RSVD_RSLT_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005055
5056 return val & MVPP2_TXQ_RSVD_RSLT_MASK;
5057}
5058
5059/* Check if there are enough reserved descriptors for transmission.
5060 * If not, request chunk of reserved descriptors and check again.
5061 */
5062static int mvpp2_txq_reserved_desc_num_proc(struct mvpp2 *priv,
5063 struct mvpp2_tx_queue *txq,
5064 struct mvpp2_txq_pcpu *txq_pcpu,
5065 int num)
5066{
5067 int req, cpu, desc_count;
5068
5069 if (txq_pcpu->reserved_num >= num)
5070 return 0;
5071
5072 /* Not enough descriptors reserved! Update the reserved descriptor
5073 * count and check again.
5074 */
5075
5076 desc_count = 0;
5077 /* Compute total of used descriptors */
5078 for_each_present_cpu(cpu) {
5079 struct mvpp2_txq_pcpu *txq_pcpu_aux;
5080
5081 txq_pcpu_aux = per_cpu_ptr(txq->pcpu, cpu);
5082 desc_count += txq_pcpu_aux->count;
5083 desc_count += txq_pcpu_aux->reserved_num;
5084 }
5085
5086 req = max(MVPP2_CPU_DESC_CHUNK, num - txq_pcpu->reserved_num);
5087 desc_count += req;
5088
5089 if (desc_count >
5090 (txq->size - (num_present_cpus() * MVPP2_CPU_DESC_CHUNK)))
5091 return -ENOMEM;
5092
5093 txq_pcpu->reserved_num += mvpp2_txq_alloc_reserved_desc(priv, txq, req);
5094
5095 /* OK, the descriptor cound has been updated: check again. */
5096 if (txq_pcpu->reserved_num < num)
5097 return -ENOMEM;
5098 return 0;
5099}
5100
5101/* Release the last allocated Tx descriptor. Useful to handle DMA
5102 * mapping failures in the Tx path.
5103 */
5104static void mvpp2_txq_desc_put(struct mvpp2_tx_queue *txq)
5105{
5106 if (txq->next_desc_to_proc == 0)
5107 txq->next_desc_to_proc = txq->last_desc - 1;
5108 else
5109 txq->next_desc_to_proc--;
5110}
5111
5112/* Set Tx descriptors fields relevant for CSUM calculation */
5113static u32 mvpp2_txq_desc_csum(int l3_offs, int l3_proto,
5114 int ip_hdr_len, int l4_proto)
5115{
5116 u32 command;
5117
5118 /* fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
5119 * G_L4_chk, L4_type required only for checksum calculation
5120 */
5121 command = (l3_offs << MVPP2_TXD_L3_OFF_SHIFT);
5122 command |= (ip_hdr_len << MVPP2_TXD_IP_HLEN_SHIFT);
5123 command |= MVPP2_TXD_IP_CSUM_DISABLE;
5124
5125 if (l3_proto == swab16(ETH_P_IP)) {
5126 command &= ~MVPP2_TXD_IP_CSUM_DISABLE; /* enable IPv4 csum */
5127 command &= ~MVPP2_TXD_L3_IP6; /* enable IPv4 */
5128 } else {
5129 command |= MVPP2_TXD_L3_IP6; /* enable IPv6 */
5130 }
5131
5132 if (l4_proto == IPPROTO_TCP) {
5133 command &= ~MVPP2_TXD_L4_UDP; /* enable TCP */
5134 command &= ~MVPP2_TXD_L4_CSUM_FRAG; /* generate L4 csum */
5135 } else if (l4_proto == IPPROTO_UDP) {
5136 command |= MVPP2_TXD_L4_UDP; /* enable UDP */
5137 command &= ~MVPP2_TXD_L4_CSUM_FRAG; /* generate L4 csum */
5138 } else {
5139 command |= MVPP2_TXD_L4_CSUM_NOT;
5140 }
5141
5142 return command;
5143}
5144
5145/* Get number of sent descriptors and decrement counter.
5146 * The number of sent descriptors is returned.
5147 * Per-CPU access
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005148 *
5149 * Called only from mvpp2_txq_done(), called from mvpp2_tx()
5150 * (migration disabled) and from the TX completion tasklet (migration
5151 * disabled) so using smp_processor_id() is OK.
Marcin Wojtas3f518502014-07-10 16:52:13 -03005152 */
5153static inline int mvpp2_txq_sent_desc_proc(struct mvpp2_port *port,
5154 struct mvpp2_tx_queue *txq)
5155{
5156 u32 val;
5157
5158 /* Reading status reg resets transmitted descriptor counter */
Thomas Petazzonia7868412017-03-07 16:53:13 +01005159 val = mvpp2_percpu_read(port->priv, smp_processor_id(),
5160 MVPP2_TXQ_SENT_REG(txq->id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005161
5162 return (val & MVPP2_TRANSMITTED_COUNT_MASK) >>
5163 MVPP2_TRANSMITTED_COUNT_OFFSET;
5164}
5165
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005166/* Called through on_each_cpu(), so runs on all CPUs, with migration
5167 * disabled, therefore using smp_processor_id() is OK.
5168 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03005169static void mvpp2_txq_sent_counter_clear(void *arg)
5170{
5171 struct mvpp2_port *port = arg;
5172 int queue;
5173
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005174 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005175 int id = port->txqs[queue]->id;
5176
Thomas Petazzonia7868412017-03-07 16:53:13 +01005177 mvpp2_percpu_read(port->priv, smp_processor_id(),
5178 MVPP2_TXQ_SENT_REG(id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005179 }
5180}
5181
5182/* Set max sizes for Tx queues */
5183static void mvpp2_txp_max_tx_size_set(struct mvpp2_port *port)
5184{
5185 u32 val, size, mtu;
5186 int txq, tx_port_num;
5187
5188 mtu = port->pkt_size * 8;
5189 if (mtu > MVPP2_TXP_MTU_MAX)
5190 mtu = MVPP2_TXP_MTU_MAX;
5191
5192 /* WA for wrong Token bucket update: Set MTU value = 3*real MTU value */
5193 mtu = 3 * mtu;
5194
5195 /* Indirect access to registers */
5196 tx_port_num = mvpp2_egress_port(port);
5197 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
5198
5199 /* Set MTU */
5200 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_MTU_REG);
5201 val &= ~MVPP2_TXP_MTU_MAX;
5202 val |= mtu;
5203 mvpp2_write(port->priv, MVPP2_TXP_SCHED_MTU_REG, val);
5204
5205 /* TXP token size and all TXQs token size must be larger that MTU */
5206 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG);
5207 size = val & MVPP2_TXP_TOKEN_SIZE_MAX;
5208 if (size < mtu) {
5209 size = mtu;
5210 val &= ~MVPP2_TXP_TOKEN_SIZE_MAX;
5211 val |= size;
5212 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
5213 }
5214
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005215 for (txq = 0; txq < port->ntxqs; txq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005216 val = mvpp2_read(port->priv,
5217 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq));
5218 size = val & MVPP2_TXQ_TOKEN_SIZE_MAX;
5219
5220 if (size < mtu) {
5221 size = mtu;
5222 val &= ~MVPP2_TXQ_TOKEN_SIZE_MAX;
5223 val |= size;
5224 mvpp2_write(port->priv,
5225 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq),
5226 val);
5227 }
5228 }
5229}
5230
5231/* Set the number of packets that will be received before Rx interrupt
5232 * will be generated by HW.
5233 */
5234static void mvpp2_rx_pkts_coal_set(struct mvpp2_port *port,
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01005235 struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005236{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005237 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005238
Thomas Petazzonif8b0d5f2017-02-21 11:28:03 +01005239 if (rxq->pkts_coal > MVPP2_OCCUPIED_THRESH_MASK)
5240 rxq->pkts_coal = MVPP2_OCCUPIED_THRESH_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005241
Thomas Petazzonia7868412017-03-07 16:53:13 +01005242 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
5243 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_THRESH_REG,
5244 rxq->pkts_coal);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005245
5246 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005247}
5248
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005249/* For some reason in the LSP this is done on each CPU. Why ? */
5250static void mvpp2_tx_pkts_coal_set(struct mvpp2_port *port,
5251 struct mvpp2_tx_queue *txq)
5252{
5253 int cpu = get_cpu();
5254 u32 val;
5255
5256 if (txq->done_pkts_coal > MVPP2_TXQ_THRESH_MASK)
5257 txq->done_pkts_coal = MVPP2_TXQ_THRESH_MASK;
5258
5259 val = (txq->done_pkts_coal << MVPP2_TXQ_THRESH_OFFSET);
5260 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5261 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_THRESH_REG, val);
5262
5263 put_cpu();
5264}
5265
Thomas Petazzoniab426762017-02-21 11:28:04 +01005266static u32 mvpp2_usec_to_cycles(u32 usec, unsigned long clk_hz)
5267{
5268 u64 tmp = (u64)clk_hz * usec;
5269
5270 do_div(tmp, USEC_PER_SEC);
5271
5272 return tmp > U32_MAX ? U32_MAX : tmp;
5273}
5274
5275static u32 mvpp2_cycles_to_usec(u32 cycles, unsigned long clk_hz)
5276{
5277 u64 tmp = (u64)cycles * USEC_PER_SEC;
5278
5279 do_div(tmp, clk_hz);
5280
5281 return tmp > U32_MAX ? U32_MAX : tmp;
5282}
5283
Marcin Wojtas3f518502014-07-10 16:52:13 -03005284/* Set the time delay in usec before Rx interrupt */
5285static void mvpp2_rx_time_coal_set(struct mvpp2_port *port,
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01005286 struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005287{
Thomas Petazzoniab426762017-02-21 11:28:04 +01005288 unsigned long freq = port->priv->tclk;
5289 u32 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005290
Thomas Petazzoniab426762017-02-21 11:28:04 +01005291 if (val > MVPP2_MAX_ISR_RX_THRESHOLD) {
5292 rxq->time_coal =
5293 mvpp2_cycles_to_usec(MVPP2_MAX_ISR_RX_THRESHOLD, freq);
5294
5295 /* re-evaluate to get actual register value */
5296 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
5297 }
5298
Marcin Wojtas3f518502014-07-10 16:52:13 -03005299 mvpp2_write(port->priv, MVPP2_ISR_RX_THRESHOLD_REG(rxq->id), val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005300}
5301
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005302static void mvpp2_tx_time_coal_set(struct mvpp2_port *port)
5303{
5304 unsigned long freq = port->priv->tclk;
5305 u32 val = mvpp2_usec_to_cycles(port->tx_time_coal, freq);
5306
5307 if (val > MVPP2_MAX_ISR_TX_THRESHOLD) {
5308 port->tx_time_coal =
5309 mvpp2_cycles_to_usec(MVPP2_MAX_ISR_TX_THRESHOLD, freq);
5310
5311 /* re-evaluate to get actual register value */
5312 val = mvpp2_usec_to_cycles(port->tx_time_coal, freq);
5313 }
5314
5315 mvpp2_write(port->priv, MVPP2_ISR_TX_THRESHOLD_REG(port->id), val);
5316}
5317
Marcin Wojtas3f518502014-07-10 16:52:13 -03005318/* Free Tx queue skbuffs */
5319static void mvpp2_txq_bufs_free(struct mvpp2_port *port,
5320 struct mvpp2_tx_queue *txq,
5321 struct mvpp2_txq_pcpu *txq_pcpu, int num)
5322{
5323 int i;
5324
5325 for (i = 0; i < num; i++) {
Thomas Petazzoni83544912016-12-21 11:28:49 +01005326 struct mvpp2_txq_pcpu_buf *tx_buf =
5327 txq_pcpu->buffs + txq_pcpu->txq_get_index;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005328
Antoine Tenart20920262017-10-23 15:24:30 +02005329 if (!IS_TSO_HEADER(txq_pcpu, tx_buf->dma))
5330 dma_unmap_single(port->dev->dev.parent, tx_buf->dma,
5331 tx_buf->size, DMA_TO_DEVICE);
Thomas Petazzoni36fb7432017-02-21 11:28:05 +01005332 if (tx_buf->skb)
5333 dev_kfree_skb_any(tx_buf->skb);
5334
5335 mvpp2_txq_inc_get(txq_pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005336 }
5337}
5338
5339static inline struct mvpp2_rx_queue *mvpp2_get_rx_queue(struct mvpp2_port *port,
5340 u32 cause)
5341{
5342 int queue = fls(cause) - 1;
5343
5344 return port->rxqs[queue];
5345}
5346
5347static inline struct mvpp2_tx_queue *mvpp2_get_tx_queue(struct mvpp2_port *port,
5348 u32 cause)
5349{
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005350 int queue = fls(cause) - 1;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005351
5352 return port->txqs[queue];
5353}
5354
5355/* Handle end of transmission */
5356static void mvpp2_txq_done(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
5357 struct mvpp2_txq_pcpu *txq_pcpu)
5358{
5359 struct netdev_queue *nq = netdev_get_tx_queue(port->dev, txq->log_id);
5360 int tx_done;
5361
5362 if (txq_pcpu->cpu != smp_processor_id())
5363 netdev_err(port->dev, "wrong cpu on the end of Tx processing\n");
5364
5365 tx_done = mvpp2_txq_sent_desc_proc(port, txq);
5366 if (!tx_done)
5367 return;
5368 mvpp2_txq_bufs_free(port, txq, txq_pcpu, tx_done);
5369
5370 txq_pcpu->count -= tx_done;
5371
5372 if (netif_tx_queue_stopped(nq))
5373 if (txq_pcpu->size - txq_pcpu->count >= MAX_SKB_FRAGS + 1)
5374 netif_tx_wake_queue(nq);
5375}
5376
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005377static unsigned int mvpp2_tx_done(struct mvpp2_port *port, u32 cause,
5378 int cpu)
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005379{
5380 struct mvpp2_tx_queue *txq;
5381 struct mvpp2_txq_pcpu *txq_pcpu;
5382 unsigned int tx_todo = 0;
5383
5384 while (cause) {
5385 txq = mvpp2_get_tx_queue(port, cause);
5386 if (!txq)
5387 break;
5388
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005389 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005390
5391 if (txq_pcpu->count) {
5392 mvpp2_txq_done(port, txq, txq_pcpu);
5393 tx_todo += txq_pcpu->count;
5394 }
5395
5396 cause &= ~(1 << txq->log_id);
5397 }
5398 return tx_todo;
5399}
5400
Marcin Wojtas3f518502014-07-10 16:52:13 -03005401/* Rx/Tx queue initialization/cleanup methods */
5402
5403/* Allocate and initialize descriptors for aggr TXQ */
5404static int mvpp2_aggr_txq_init(struct platform_device *pdev,
Antoine Ténart85affd72017-08-23 09:46:55 +02005405 struct mvpp2_tx_queue *aggr_txq, int cpu,
Marcin Wojtas3f518502014-07-10 16:52:13 -03005406 struct mvpp2 *priv)
5407{
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005408 u32 txq_dma;
5409
Marcin Wojtas3f518502014-07-10 16:52:13 -03005410 /* Allocate memory for TX descriptors */
5411 aggr_txq->descs = dma_alloc_coherent(&pdev->dev,
Antoine Ténart85affd72017-08-23 09:46:55 +02005412 MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005413 &aggr_txq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005414 if (!aggr_txq->descs)
5415 return -ENOMEM;
5416
Marcin Wojtas3f518502014-07-10 16:52:13 -03005417 aggr_txq->last_desc = aggr_txq->size - 1;
5418
5419 /* Aggr TXQ no reset WA */
5420 aggr_txq->next_desc_to_proc = mvpp2_read(priv,
5421 MVPP2_AGGR_TXQ_INDEX_REG(cpu));
5422
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005423 /* Set Tx descriptors queue starting address indirect
5424 * access
5425 */
5426 if (priv->hw_version == MVPP21)
5427 txq_dma = aggr_txq->descs_dma;
5428 else
5429 txq_dma = aggr_txq->descs_dma >>
5430 MVPP22_AGGR_TXQ_DESC_ADDR_OFFS;
5431
5432 mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu), txq_dma);
Antoine Ténart85affd72017-08-23 09:46:55 +02005433 mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu),
5434 MVPP2_AGGR_TXQ_SIZE);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005435
5436 return 0;
5437}
5438
5439/* Create a specified Rx queue */
5440static int mvpp2_rxq_init(struct mvpp2_port *port,
5441 struct mvpp2_rx_queue *rxq)
5442
5443{
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005444 u32 rxq_dma;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005445 int cpu;
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005446
Marcin Wojtas3f518502014-07-10 16:52:13 -03005447 rxq->size = port->rx_ring_size;
5448
5449 /* Allocate memory for RX descriptors */
5450 rxq->descs = dma_alloc_coherent(port->dev->dev.parent,
5451 rxq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005452 &rxq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005453 if (!rxq->descs)
5454 return -ENOMEM;
5455
Marcin Wojtas3f518502014-07-10 16:52:13 -03005456 rxq->last_desc = rxq->size - 1;
5457
5458 /* Zero occupied and non-occupied counters - direct access */
5459 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
5460
5461 /* Set Rx descriptors queue starting address - indirect access */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005462 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005463 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005464 if (port->priv->hw_version == MVPP21)
5465 rxq_dma = rxq->descs_dma;
5466 else
5467 rxq_dma = rxq->descs_dma >> MVPP22_DESC_ADDR_OFFS;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005468 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_ADDR_REG, rxq_dma);
5469 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_SIZE_REG, rxq->size);
5470 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_INDEX_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005471 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005472
5473 /* Set Offset */
5474 mvpp2_rxq_offset_set(port, rxq->id, NET_SKB_PAD);
5475
5476 /* Set coalescing pkts and time */
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01005477 mvpp2_rx_pkts_coal_set(port, rxq);
5478 mvpp2_rx_time_coal_set(port, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005479
5480 /* Add number of descriptors ready for receiving packets */
5481 mvpp2_rxq_status_update(port, rxq->id, 0, rxq->size);
5482
5483 return 0;
5484}
5485
5486/* Push packets received by the RXQ to BM pool */
5487static void mvpp2_rxq_drop_pkts(struct mvpp2_port *port,
5488 struct mvpp2_rx_queue *rxq)
5489{
5490 int rx_received, i;
5491
5492 rx_received = mvpp2_rxq_received(port, rxq->id);
5493 if (!rx_received)
5494 return;
5495
5496 for (i = 0; i < rx_received; i++) {
5497 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005498 u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
5499 int pool;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005500
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005501 pool = (status & MVPP2_RXD_BM_POOL_ID_MASK) >>
5502 MVPP2_RXD_BM_POOL_ID_OFFS;
5503
Thomas Petazzoni7d7627b2017-06-22 14:23:20 +02005504 mvpp2_bm_pool_put(port, pool,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005505 mvpp2_rxdesc_dma_addr_get(port, rx_desc),
5506 mvpp2_rxdesc_cookie_get(port, rx_desc));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005507 }
5508 mvpp2_rxq_status_update(port, rxq->id, rx_received, rx_received);
5509}
5510
5511/* Cleanup Rx queue */
5512static void mvpp2_rxq_deinit(struct mvpp2_port *port,
5513 struct mvpp2_rx_queue *rxq)
5514{
Thomas Petazzonia7868412017-03-07 16:53:13 +01005515 int cpu;
5516
Marcin Wojtas3f518502014-07-10 16:52:13 -03005517 mvpp2_rxq_drop_pkts(port, rxq);
5518
5519 if (rxq->descs)
5520 dma_free_coherent(port->dev->dev.parent,
5521 rxq->size * MVPP2_DESC_ALIGNED_SIZE,
5522 rxq->descs,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005523 rxq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005524
5525 rxq->descs = NULL;
5526 rxq->last_desc = 0;
5527 rxq->next_desc_to_proc = 0;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005528 rxq->descs_dma = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005529
5530 /* Clear Rx descriptors queue starting address and size;
5531 * free descriptor number
5532 */
5533 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005534 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005535 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
5536 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_ADDR_REG, 0);
5537 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_SIZE_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005538 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005539}
5540
5541/* Create and initialize a Tx queue */
5542static int mvpp2_txq_init(struct mvpp2_port *port,
5543 struct mvpp2_tx_queue *txq)
5544{
5545 u32 val;
5546 int cpu, desc, desc_per_txq, tx_port_num;
5547 struct mvpp2_txq_pcpu *txq_pcpu;
5548
5549 txq->size = port->tx_ring_size;
5550
5551 /* Allocate memory for Tx descriptors */
5552 txq->descs = dma_alloc_coherent(port->dev->dev.parent,
5553 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005554 &txq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005555 if (!txq->descs)
5556 return -ENOMEM;
5557
Marcin Wojtas3f518502014-07-10 16:52:13 -03005558 txq->last_desc = txq->size - 1;
5559
5560 /* Set Tx descriptors queue starting address - indirect access */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005561 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005562 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5563 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_ADDR_REG,
5564 txq->descs_dma);
5565 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_SIZE_REG,
5566 txq->size & MVPP2_TXQ_DESC_SIZE_MASK);
5567 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_INDEX_REG, 0);
5568 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_RSVD_CLR_REG,
5569 txq->id << MVPP2_TXQ_RSVD_CLR_OFFSET);
5570 val = mvpp2_percpu_read(port->priv, cpu, MVPP2_TXQ_PENDING_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005571 val &= ~MVPP2_TXQ_PENDING_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005572 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PENDING_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005573
5574 /* Calculate base address in prefetch buffer. We reserve 16 descriptors
5575 * for each existing TXQ.
5576 * TCONTS for PON port must be continuous from 0 to MVPP2_MAX_TCONT
5577 * GBE ports assumed to be continious from 0 to MVPP2_MAX_PORTS
5578 */
5579 desc_per_txq = 16;
5580 desc = (port->id * MVPP2_MAX_TXQ * desc_per_txq) +
5581 (txq->log_id * desc_per_txq);
5582
Thomas Petazzonia7868412017-03-07 16:53:13 +01005583 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG,
5584 MVPP2_PREF_BUF_PTR(desc) | MVPP2_PREF_BUF_SIZE_16 |
5585 MVPP2_PREF_BUF_THRESH(desc_per_txq / 2));
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005586 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005587
5588 /* WRR / EJP configuration - indirect access */
5589 tx_port_num = mvpp2_egress_port(port);
5590 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
5591
5592 val = mvpp2_read(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id));
5593 val &= ~MVPP2_TXQ_REFILL_PERIOD_ALL_MASK;
5594 val |= MVPP2_TXQ_REFILL_PERIOD_MASK(1);
5595 val |= MVPP2_TXQ_REFILL_TOKENS_ALL_MASK;
5596 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id), val);
5597
5598 val = MVPP2_TXQ_TOKEN_SIZE_MAX;
5599 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq->log_id),
5600 val);
5601
5602 for_each_present_cpu(cpu) {
5603 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
5604 txq_pcpu->size = txq->size;
Markus Elfring02c91ec2017-04-17 08:09:07 +02005605 txq_pcpu->buffs = kmalloc_array(txq_pcpu->size,
5606 sizeof(*txq_pcpu->buffs),
5607 GFP_KERNEL);
Thomas Petazzoni83544912016-12-21 11:28:49 +01005608 if (!txq_pcpu->buffs)
Markus Elfring20b1e162017-04-17 12:58:33 +02005609 goto cleanup;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005610
5611 txq_pcpu->count = 0;
5612 txq_pcpu->reserved_num = 0;
5613 txq_pcpu->txq_put_index = 0;
5614 txq_pcpu->txq_get_index = 0;
Antoine Ténart186cd4d2017-08-23 09:46:56 +02005615
5616 txq_pcpu->tso_headers =
5617 dma_alloc_coherent(port->dev->dev.parent,
Yan Markman822eaf72017-10-23 15:24:29 +02005618 txq_pcpu->size * TSO_HEADER_SIZE,
Antoine Ténart186cd4d2017-08-23 09:46:56 +02005619 &txq_pcpu->tso_headers_dma,
5620 GFP_KERNEL);
5621 if (!txq_pcpu->tso_headers)
5622 goto cleanup;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005623 }
5624
5625 return 0;
Markus Elfring20b1e162017-04-17 12:58:33 +02005626cleanup:
Marcin Wojtas71ce3912015-08-06 19:00:29 +02005627 for_each_present_cpu(cpu) {
5628 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Thomas Petazzoni83544912016-12-21 11:28:49 +01005629 kfree(txq_pcpu->buffs);
Antoine Ténart186cd4d2017-08-23 09:46:56 +02005630
5631 dma_free_coherent(port->dev->dev.parent,
Yan Markman822eaf72017-10-23 15:24:29 +02005632 txq_pcpu->size * TSO_HEADER_SIZE,
Antoine Ténart186cd4d2017-08-23 09:46:56 +02005633 txq_pcpu->tso_headers,
5634 txq_pcpu->tso_headers_dma);
Marcin Wojtas71ce3912015-08-06 19:00:29 +02005635 }
5636
5637 dma_free_coherent(port->dev->dev.parent,
5638 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005639 txq->descs, txq->descs_dma);
Marcin Wojtas71ce3912015-08-06 19:00:29 +02005640
5641 return -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005642}
5643
5644/* Free allocated TXQ resources */
5645static void mvpp2_txq_deinit(struct mvpp2_port *port,
5646 struct mvpp2_tx_queue *txq)
5647{
5648 struct mvpp2_txq_pcpu *txq_pcpu;
5649 int cpu;
5650
5651 for_each_present_cpu(cpu) {
5652 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Thomas Petazzoni83544912016-12-21 11:28:49 +01005653 kfree(txq_pcpu->buffs);
Antoine Ténart186cd4d2017-08-23 09:46:56 +02005654
5655 dma_free_coherent(port->dev->dev.parent,
Yan Markman822eaf72017-10-23 15:24:29 +02005656 txq_pcpu->size * TSO_HEADER_SIZE,
Antoine Ténart186cd4d2017-08-23 09:46:56 +02005657 txq_pcpu->tso_headers,
5658 txq_pcpu->tso_headers_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005659 }
5660
5661 if (txq->descs)
5662 dma_free_coherent(port->dev->dev.parent,
5663 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005664 txq->descs, txq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005665
5666 txq->descs = NULL;
5667 txq->last_desc = 0;
5668 txq->next_desc_to_proc = 0;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005669 txq->descs_dma = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005670
5671 /* Set minimum bandwidth for disabled TXQs */
5672 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(txq->id), 0);
5673
5674 /* Set Tx descriptors queue starting address and size */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005675 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005676 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5677 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_ADDR_REG, 0);
5678 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_SIZE_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005679 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005680}
5681
5682/* Cleanup Tx ports */
5683static void mvpp2_txq_clean(struct mvpp2_port *port, struct mvpp2_tx_queue *txq)
5684{
5685 struct mvpp2_txq_pcpu *txq_pcpu;
5686 int delay, pending, cpu;
5687 u32 val;
5688
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005689 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005690 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5691 val = mvpp2_percpu_read(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005692 val |= MVPP2_TXQ_DRAIN_EN_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005693 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005694
5695 /* The napi queue has been stopped so wait for all packets
5696 * to be transmitted.
5697 */
5698 delay = 0;
5699 do {
5700 if (delay >= MVPP2_TX_PENDING_TIMEOUT_MSEC) {
5701 netdev_warn(port->dev,
5702 "port %d: cleaning queue %d timed out\n",
5703 port->id, txq->log_id);
5704 break;
5705 }
5706 mdelay(1);
5707 delay++;
5708
Thomas Petazzonia7868412017-03-07 16:53:13 +01005709 pending = mvpp2_percpu_read(port->priv, cpu,
5710 MVPP2_TXQ_PENDING_REG);
5711 pending &= MVPP2_TXQ_PENDING_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005712 } while (pending);
5713
5714 val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005715 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG, val);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005716 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005717
5718 for_each_present_cpu(cpu) {
5719 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
5720
5721 /* Release all packets */
5722 mvpp2_txq_bufs_free(port, txq, txq_pcpu, txq_pcpu->count);
5723
5724 /* Reset queue */
5725 txq_pcpu->count = 0;
5726 txq_pcpu->txq_put_index = 0;
5727 txq_pcpu->txq_get_index = 0;
5728 }
5729}
5730
5731/* Cleanup all Tx queues */
5732static void mvpp2_cleanup_txqs(struct mvpp2_port *port)
5733{
5734 struct mvpp2_tx_queue *txq;
5735 int queue;
5736 u32 val;
5737
5738 val = mvpp2_read(port->priv, MVPP2_TX_PORT_FLUSH_REG);
5739
5740 /* Reset Tx ports and delete Tx queues */
5741 val |= MVPP2_TX_PORT_FLUSH_MASK(port->id);
5742 mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
5743
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005744 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005745 txq = port->txqs[queue];
5746 mvpp2_txq_clean(port, txq);
5747 mvpp2_txq_deinit(port, txq);
5748 }
5749
5750 on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
5751
5752 val &= ~MVPP2_TX_PORT_FLUSH_MASK(port->id);
5753 mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
5754}
5755
5756/* Cleanup all Rx queues */
5757static void mvpp2_cleanup_rxqs(struct mvpp2_port *port)
5758{
5759 int queue;
5760
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005761 for (queue = 0; queue < port->nrxqs; queue++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005762 mvpp2_rxq_deinit(port, port->rxqs[queue]);
5763}
5764
5765/* Init all Rx queues for port */
5766static int mvpp2_setup_rxqs(struct mvpp2_port *port)
5767{
5768 int queue, err;
5769
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005770 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005771 err = mvpp2_rxq_init(port, port->rxqs[queue]);
5772 if (err)
5773 goto err_cleanup;
5774 }
5775 return 0;
5776
5777err_cleanup:
5778 mvpp2_cleanup_rxqs(port);
5779 return err;
5780}
5781
5782/* Init all tx queues for port */
5783static int mvpp2_setup_txqs(struct mvpp2_port *port)
5784{
5785 struct mvpp2_tx_queue *txq;
5786 int queue, err;
5787
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005788 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005789 txq = port->txqs[queue];
5790 err = mvpp2_txq_init(port, txq);
5791 if (err)
5792 goto err_cleanup;
5793 }
5794
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005795 if (port->has_tx_irqs) {
5796 mvpp2_tx_time_coal_set(port);
5797 for (queue = 0; queue < port->ntxqs; queue++) {
5798 txq = port->txqs[queue];
5799 mvpp2_tx_pkts_coal_set(port, txq);
5800 }
5801 }
5802
Marcin Wojtas3f518502014-07-10 16:52:13 -03005803 on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
5804 return 0;
5805
5806err_cleanup:
5807 mvpp2_cleanup_txqs(port);
5808 return err;
5809}
5810
5811/* The callback for per-port interrupt */
5812static irqreturn_t mvpp2_isr(int irq, void *dev_id)
5813{
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02005814 struct mvpp2_queue_vector *qv = dev_id;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005815
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02005816 mvpp2_qvec_interrupt_disable(qv);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005817
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02005818 napi_schedule(&qv->napi);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005819
5820 return IRQ_HANDLED;
5821}
5822
Antoine Tenartfd3651b2017-09-01 11:04:54 +02005823/* Per-port interrupt for link status changes */
5824static irqreturn_t mvpp2_link_status_isr(int irq, void *dev_id)
5825{
5826 struct mvpp2_port *port = (struct mvpp2_port *)dev_id;
5827 struct net_device *dev = port->dev;
5828 bool event = false, link = false;
5829 u32 val;
5830
5831 mvpp22_gop_mask_irq(port);
5832
5833 if (port->gop_id == 0 &&
5834 port->phy_interface == PHY_INTERFACE_MODE_10GKR) {
5835 val = readl(port->base + MVPP22_XLG_INT_STAT);
5836 if (val & MVPP22_XLG_INT_STAT_LINK) {
5837 event = true;
5838 val = readl(port->base + MVPP22_XLG_STATUS);
5839 if (val & MVPP22_XLG_STATUS_LINK_UP)
5840 link = true;
5841 }
5842 } else if (phy_interface_mode_is_rgmii(port->phy_interface) ||
5843 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
5844 val = readl(port->base + MVPP22_GMAC_INT_STAT);
5845 if (val & MVPP22_GMAC_INT_STAT_LINK) {
5846 event = true;
5847 val = readl(port->base + MVPP2_GMAC_STATUS0);
5848 if (val & MVPP2_GMAC_STATUS0_LINK_UP)
5849 link = true;
5850 }
5851 }
5852
5853 if (!netif_running(dev) || !event)
5854 goto handled;
5855
5856 if (link) {
5857 mvpp2_interrupts_enable(port);
5858
5859 mvpp2_egress_enable(port);
5860 mvpp2_ingress_enable(port);
5861 netif_carrier_on(dev);
5862 netif_tx_wake_all_queues(dev);
5863 } else {
5864 netif_tx_stop_all_queues(dev);
5865 netif_carrier_off(dev);
5866 mvpp2_ingress_disable(port);
5867 mvpp2_egress_disable(port);
5868
5869 mvpp2_interrupts_disable(port);
5870 }
5871
5872handled:
5873 mvpp22_gop_unmask_irq(port);
5874 return IRQ_HANDLED;
5875}
5876
Antoine Tenart65a2c092017-08-30 10:29:18 +02005877static void mvpp2_gmac_set_autoneg(struct mvpp2_port *port,
5878 struct phy_device *phydev)
5879{
5880 u32 val;
5881
5882 if (port->phy_interface != PHY_INTERFACE_MODE_RGMII &&
5883 port->phy_interface != PHY_INTERFACE_MODE_RGMII_ID &&
5884 port->phy_interface != PHY_INTERFACE_MODE_RGMII_RXID &&
5885 port->phy_interface != PHY_INTERFACE_MODE_RGMII_TXID &&
5886 port->phy_interface != PHY_INTERFACE_MODE_SGMII)
5887 return;
5888
5889 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5890 val &= ~(MVPP2_GMAC_CONFIG_MII_SPEED |
5891 MVPP2_GMAC_CONFIG_GMII_SPEED |
5892 MVPP2_GMAC_CONFIG_FULL_DUPLEX |
5893 MVPP2_GMAC_AN_SPEED_EN |
5894 MVPP2_GMAC_AN_DUPLEX_EN);
5895
5896 if (phydev->duplex)
5897 val |= MVPP2_GMAC_CONFIG_FULL_DUPLEX;
5898
5899 if (phydev->speed == SPEED_1000)
5900 val |= MVPP2_GMAC_CONFIG_GMII_SPEED;
5901 else if (phydev->speed == SPEED_100)
5902 val |= MVPP2_GMAC_CONFIG_MII_SPEED;
5903
5904 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
Antoine Tenart65a2c092017-08-30 10:29:18 +02005905}
5906
Marcin Wojtas3f518502014-07-10 16:52:13 -03005907/* Adjust link */
5908static void mvpp2_link_event(struct net_device *dev)
5909{
5910 struct mvpp2_port *port = netdev_priv(dev);
Philippe Reynes8e072692016-06-28 00:08:11 +02005911 struct phy_device *phydev = dev->phydev;
Antoine Tenart89273bc2017-08-30 10:29:19 +02005912 bool link_reconfigured = false;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005913 u32 val;
5914
5915 if (phydev->link) {
Antoine Tenart89273bc2017-08-30 10:29:19 +02005916 if (port->phy_interface != phydev->interface && port->comphy) {
5917 /* disable current port for reconfiguration */
5918 mvpp2_interrupts_disable(port);
5919 netif_carrier_off(port->dev);
5920 mvpp2_port_disable(port);
5921 phy_power_off(port->comphy);
5922
5923 /* comphy reconfiguration */
5924 port->phy_interface = phydev->interface;
5925 mvpp22_comphy_init(port);
5926
5927 /* gop/mac reconfiguration */
5928 mvpp22_gop_init(port);
5929 mvpp2_port_mii_set(port);
5930
5931 link_reconfigured = true;
5932 }
5933
Marcin Wojtas3f518502014-07-10 16:52:13 -03005934 if ((port->speed != phydev->speed) ||
5935 (port->duplex != phydev->duplex)) {
Antoine Tenart65a2c092017-08-30 10:29:18 +02005936 mvpp2_gmac_set_autoneg(port, phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005937
5938 port->duplex = phydev->duplex;
5939 port->speed = phydev->speed;
5940 }
5941 }
5942
Antoine Tenart89273bc2017-08-30 10:29:19 +02005943 if (phydev->link != port->link || link_reconfigured) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005944 port->link = phydev->link;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005945
Marcin Wojtas3f518502014-07-10 16:52:13 -03005946 if (phydev->link) {
Antoine Tenart65a2c092017-08-30 10:29:18 +02005947 if (port->phy_interface == PHY_INTERFACE_MODE_RGMII ||
5948 port->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
5949 port->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID ||
5950 port->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID ||
5951 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
5952 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5953 val |= (MVPP2_GMAC_FORCE_LINK_PASS |
5954 MVPP2_GMAC_FORCE_LINK_DOWN);
5955 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5956 }
Antoine Tenartf55744a2017-08-30 10:29:17 +02005957
5958 mvpp2_interrupts_enable(port);
5959 mvpp2_port_enable(port);
5960
Marcin Wojtas3f518502014-07-10 16:52:13 -03005961 mvpp2_egress_enable(port);
5962 mvpp2_ingress_enable(port);
Antoine Tenartf55744a2017-08-30 10:29:17 +02005963 netif_carrier_on(dev);
5964 netif_tx_wake_all_queues(dev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005965 } else {
Antoine Tenart968b2112017-08-30 10:29:16 +02005966 port->duplex = -1;
5967 port->speed = 0;
5968
Antoine Tenartf55744a2017-08-30 10:29:17 +02005969 netif_tx_stop_all_queues(dev);
5970 netif_carrier_off(dev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005971 mvpp2_ingress_disable(port);
5972 mvpp2_egress_disable(port);
Antoine Tenartf55744a2017-08-30 10:29:17 +02005973
5974 mvpp2_port_disable(port);
5975 mvpp2_interrupts_disable(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005976 }
Antoine Tenart968b2112017-08-30 10:29:16 +02005977
Marcin Wojtas3f518502014-07-10 16:52:13 -03005978 phy_print_status(phydev);
5979 }
5980}
5981
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005982static void mvpp2_timer_set(struct mvpp2_port_pcpu *port_pcpu)
5983{
5984 ktime_t interval;
5985
5986 if (!port_pcpu->timer_scheduled) {
5987 port_pcpu->timer_scheduled = true;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01005988 interval = MVPP2_TXDONE_HRTIMER_PERIOD_NS;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005989 hrtimer_start(&port_pcpu->tx_done_timer, interval,
5990 HRTIMER_MODE_REL_PINNED);
5991 }
5992}
5993
5994static void mvpp2_tx_proc_cb(unsigned long data)
5995{
5996 struct net_device *dev = (struct net_device *)data;
5997 struct mvpp2_port *port = netdev_priv(dev);
5998 struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
5999 unsigned int tx_todo, cause;
6000
6001 if (!netif_running(dev))
6002 return;
6003 port_pcpu->timer_scheduled = false;
6004
6005 /* Process all the Tx queues */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006006 cause = (1 << port->ntxqs) - 1;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006007 tx_todo = mvpp2_tx_done(port, cause, smp_processor_id());
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006008
6009 /* Set the timer in case not all the packets were processed */
6010 if (tx_todo)
6011 mvpp2_timer_set(port_pcpu);
6012}
6013
6014static enum hrtimer_restart mvpp2_hr_timer_cb(struct hrtimer *timer)
6015{
6016 struct mvpp2_port_pcpu *port_pcpu = container_of(timer,
6017 struct mvpp2_port_pcpu,
6018 tx_done_timer);
6019
6020 tasklet_schedule(&port_pcpu->tx_done_tasklet);
6021
6022 return HRTIMER_NORESTART;
6023}
6024
Marcin Wojtas3f518502014-07-10 16:52:13 -03006025/* Main RX/TX processing routines */
6026
6027/* Display more error info */
6028static void mvpp2_rx_error(struct mvpp2_port *port,
6029 struct mvpp2_rx_desc *rx_desc)
6030{
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006031 u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
6032 size_t sz = mvpp2_rxdesc_size_get(port, rx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006033
6034 switch (status & MVPP2_RXD_ERR_CODE_MASK) {
6035 case MVPP2_RXD_ERR_CRC:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006036 netdev_err(port->dev, "bad rx status %08x (crc error), size=%zu\n",
6037 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006038 break;
6039 case MVPP2_RXD_ERR_OVERRUN:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006040 netdev_err(port->dev, "bad rx status %08x (overrun error), size=%zu\n",
6041 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006042 break;
6043 case MVPP2_RXD_ERR_RESOURCE:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006044 netdev_err(port->dev, "bad rx status %08x (resource error), size=%zu\n",
6045 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006046 break;
6047 }
6048}
6049
6050/* Handle RX checksum offload */
6051static void mvpp2_rx_csum(struct mvpp2_port *port, u32 status,
6052 struct sk_buff *skb)
6053{
6054 if (((status & MVPP2_RXD_L3_IP4) &&
6055 !(status & MVPP2_RXD_IP4_HEADER_ERR)) ||
6056 (status & MVPP2_RXD_L3_IP6))
6057 if (((status & MVPP2_RXD_L4_UDP) ||
6058 (status & MVPP2_RXD_L4_TCP)) &&
6059 (status & MVPP2_RXD_L4_CSUM_OK)) {
6060 skb->csum = 0;
6061 skb->ip_summed = CHECKSUM_UNNECESSARY;
6062 return;
6063 }
6064
6065 skb->ip_summed = CHECKSUM_NONE;
6066}
6067
6068/* Reuse skb if possible, or allocate a new skb and add it to BM pool */
6069static int mvpp2_rx_refill(struct mvpp2_port *port,
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006070 struct mvpp2_bm_pool *bm_pool, int pool)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006071{
Thomas Petazzoni20396132017-03-07 16:53:00 +01006072 dma_addr_t dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01006073 phys_addr_t phys_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006074 void *buf;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006075
Marcin Wojtas3f518502014-07-10 16:52:13 -03006076 /* No recycle or too many buffers are in use, so allocate a new skb */
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01006077 buf = mvpp2_buf_alloc(port, bm_pool, &dma_addr, &phys_addr,
6078 GFP_ATOMIC);
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006079 if (!buf)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006080 return -ENOMEM;
6081
Thomas Petazzoni7d7627b2017-06-22 14:23:20 +02006082 mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
Thomas Petazzoni7ef7e1d2017-02-21 11:28:07 +01006083
Marcin Wojtas3f518502014-07-10 16:52:13 -03006084 return 0;
6085}
6086
6087/* Handle tx checksum */
6088static u32 mvpp2_skb_tx_csum(struct mvpp2_port *port, struct sk_buff *skb)
6089{
6090 if (skb->ip_summed == CHECKSUM_PARTIAL) {
6091 int ip_hdr_len = 0;
6092 u8 l4_proto;
6093
6094 if (skb->protocol == htons(ETH_P_IP)) {
6095 struct iphdr *ip4h = ip_hdr(skb);
6096
6097 /* Calculate IPv4 checksum and L4 checksum */
6098 ip_hdr_len = ip4h->ihl;
6099 l4_proto = ip4h->protocol;
6100 } else if (skb->protocol == htons(ETH_P_IPV6)) {
6101 struct ipv6hdr *ip6h = ipv6_hdr(skb);
6102
6103 /* Read l4_protocol from one of IPv6 extra headers */
6104 if (skb_network_header_len(skb) > 0)
6105 ip_hdr_len = (skb_network_header_len(skb) >> 2);
6106 l4_proto = ip6h->nexthdr;
6107 } else {
6108 return MVPP2_TXD_L4_CSUM_NOT;
6109 }
6110
6111 return mvpp2_txq_desc_csum(skb_network_offset(skb),
6112 skb->protocol, ip_hdr_len, l4_proto);
6113 }
6114
6115 return MVPP2_TXD_L4_CSUM_NOT | MVPP2_TXD_IP_CSUM_DISABLE;
6116}
6117
Marcin Wojtas3f518502014-07-10 16:52:13 -03006118/* Main rx processing */
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006119static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi,
6120 int rx_todo, struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006121{
6122 struct net_device *dev = port->dev;
Marcin Wojtasb5015852015-12-03 15:20:51 +01006123 int rx_received;
6124 int rx_done = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006125 u32 rcvd_pkts = 0;
6126 u32 rcvd_bytes = 0;
6127
6128 /* Get number of received packets and clamp the to-do */
6129 rx_received = mvpp2_rxq_received(port, rxq->id);
6130 if (rx_todo > rx_received)
6131 rx_todo = rx_received;
6132
Marcin Wojtasb5015852015-12-03 15:20:51 +01006133 while (rx_done < rx_todo) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006134 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
6135 struct mvpp2_bm_pool *bm_pool;
6136 struct sk_buff *skb;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006137 unsigned int frag_size;
Thomas Petazzoni20396132017-03-07 16:53:00 +01006138 dma_addr_t dma_addr;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006139 phys_addr_t phys_addr;
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006140 u32 rx_status;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006141 int pool, rx_bytes, err;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006142 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006143
Marcin Wojtasb5015852015-12-03 15:20:51 +01006144 rx_done++;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006145 rx_status = mvpp2_rxdesc_status_get(port, rx_desc);
6146 rx_bytes = mvpp2_rxdesc_size_get(port, rx_desc);
6147 rx_bytes -= MVPP2_MH_SIZE;
6148 dma_addr = mvpp2_rxdesc_dma_addr_get(port, rx_desc);
6149 phys_addr = mvpp2_rxdesc_cookie_get(port, rx_desc);
6150 data = (void *)phys_to_virt(phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006151
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006152 pool = (rx_status & MVPP2_RXD_BM_POOL_ID_MASK) >>
6153 MVPP2_RXD_BM_POOL_ID_OFFS;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006154 bm_pool = &port->priv->bm_pools[pool];
Marcin Wojtas3f518502014-07-10 16:52:13 -03006155
6156 /* In case of an error, release the requested buffer pointer
6157 * to the Buffer Manager. This request process is controlled
6158 * by the hardware, and the information about the buffer is
6159 * comprised by the RX descriptor.
6160 */
6161 if (rx_status & MVPP2_RXD_ERR_SUMMARY) {
Markus Elfring8a524882017-04-17 10:52:02 +02006162err_drop_frame:
Marcin Wojtas3f518502014-07-10 16:52:13 -03006163 dev->stats.rx_errors++;
6164 mvpp2_rx_error(port, rx_desc);
Marcin Wojtasb5015852015-12-03 15:20:51 +01006165 /* Return the buffer to the pool */
Thomas Petazzoni7d7627b2017-06-22 14:23:20 +02006166 mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006167 continue;
6168 }
6169
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006170 if (bm_pool->frag_size > PAGE_SIZE)
6171 frag_size = 0;
6172 else
6173 frag_size = bm_pool->frag_size;
6174
6175 skb = build_skb(data, frag_size);
6176 if (!skb) {
6177 netdev_warn(port->dev, "skb build failed\n");
6178 goto err_drop_frame;
6179 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006180
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006181 err = mvpp2_rx_refill(port, bm_pool, pool);
Marcin Wojtasb5015852015-12-03 15:20:51 +01006182 if (err) {
6183 netdev_err(port->dev, "failed to refill BM pools\n");
6184 goto err_drop_frame;
6185 }
6186
Thomas Petazzoni20396132017-03-07 16:53:00 +01006187 dma_unmap_single(dev->dev.parent, dma_addr,
Marcin Wojtas4229d502015-12-03 15:20:50 +01006188 bm_pool->buf_size, DMA_FROM_DEVICE);
6189
Marcin Wojtas3f518502014-07-10 16:52:13 -03006190 rcvd_pkts++;
6191 rcvd_bytes += rx_bytes;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006192
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006193 skb_reserve(skb, MVPP2_MH_SIZE + NET_SKB_PAD);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006194 skb_put(skb, rx_bytes);
6195 skb->protocol = eth_type_trans(skb, dev);
6196 mvpp2_rx_csum(port, rx_status, skb);
6197
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006198 napi_gro_receive(napi, skb);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006199 }
6200
6201 if (rcvd_pkts) {
6202 struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
6203
6204 u64_stats_update_begin(&stats->syncp);
6205 stats->rx_packets += rcvd_pkts;
6206 stats->rx_bytes += rcvd_bytes;
6207 u64_stats_update_end(&stats->syncp);
6208 }
6209
6210 /* Update Rx queue management counters */
6211 wmb();
Marcin Wojtasb5015852015-12-03 15:20:51 +01006212 mvpp2_rxq_status_update(port, rxq->id, rx_done, rx_done);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006213
6214 return rx_todo;
6215}
6216
6217static inline void
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006218tx_desc_unmap_put(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006219 struct mvpp2_tx_desc *desc)
6220{
Antoine Tenart20920262017-10-23 15:24:30 +02006221 struct mvpp2_txq_pcpu *txq_pcpu = this_cpu_ptr(txq->pcpu);
6222
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006223 dma_addr_t buf_dma_addr =
6224 mvpp2_txdesc_dma_addr_get(port, desc);
6225 size_t buf_sz =
6226 mvpp2_txdesc_size_get(port, desc);
Antoine Tenart20920262017-10-23 15:24:30 +02006227 if (!IS_TSO_HEADER(txq_pcpu, buf_dma_addr))
6228 dma_unmap_single(port->dev->dev.parent, buf_dma_addr,
6229 buf_sz, DMA_TO_DEVICE);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006230 mvpp2_txq_desc_put(txq);
6231}
6232
6233/* Handle tx fragmentation processing */
6234static int mvpp2_tx_frag_process(struct mvpp2_port *port, struct sk_buff *skb,
6235 struct mvpp2_tx_queue *aggr_txq,
6236 struct mvpp2_tx_queue *txq)
6237{
6238 struct mvpp2_txq_pcpu *txq_pcpu = this_cpu_ptr(txq->pcpu);
6239 struct mvpp2_tx_desc *tx_desc;
6240 int i;
Thomas Petazzoni20396132017-03-07 16:53:00 +01006241 dma_addr_t buf_dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006242
6243 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
6244 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6245 void *addr = page_address(frag->page.p) + frag->page_offset;
6246
6247 tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006248 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6249 mvpp2_txdesc_size_set(port, tx_desc, frag->size);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006250
Thomas Petazzoni20396132017-03-07 16:53:00 +01006251 buf_dma_addr = dma_map_single(port->dev->dev.parent, addr,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006252 frag->size,
6253 DMA_TO_DEVICE);
Thomas Petazzoni20396132017-03-07 16:53:00 +01006254 if (dma_mapping_error(port->dev->dev.parent, buf_dma_addr)) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006255 mvpp2_txq_desc_put(txq);
Markus Elfring32bae632017-04-17 11:36:34 +02006256 goto cleanup;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006257 }
6258
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006259 mvpp2_txdesc_offset_set(port, tx_desc,
6260 buf_dma_addr & MVPP2_TX_DESC_ALIGN);
6261 mvpp2_txdesc_dma_addr_set(port, tx_desc,
6262 buf_dma_addr & ~MVPP2_TX_DESC_ALIGN);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006263
6264 if (i == (skb_shinfo(skb)->nr_frags - 1)) {
6265 /* Last descriptor */
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006266 mvpp2_txdesc_cmd_set(port, tx_desc,
6267 MVPP2_TXD_L_DESC);
6268 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006269 } else {
6270 /* Descriptor in the middle: Not First, Not Last */
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006271 mvpp2_txdesc_cmd_set(port, tx_desc, 0);
6272 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006273 }
6274 }
6275
6276 return 0;
Markus Elfring32bae632017-04-17 11:36:34 +02006277cleanup:
Marcin Wojtas3f518502014-07-10 16:52:13 -03006278 /* Release all descriptors that were used to map fragments of
6279 * this packet, as well as the corresponding DMA mappings
6280 */
6281 for (i = i - 1; i >= 0; i--) {
6282 tx_desc = txq->descs + i;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006283 tx_desc_unmap_put(port, txq, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006284 }
6285
6286 return -ENOMEM;
6287}
6288
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006289static inline void mvpp2_tso_put_hdr(struct sk_buff *skb,
6290 struct net_device *dev,
6291 struct mvpp2_tx_queue *txq,
6292 struct mvpp2_tx_queue *aggr_txq,
6293 struct mvpp2_txq_pcpu *txq_pcpu,
6294 int hdr_sz)
6295{
6296 struct mvpp2_port *port = netdev_priv(dev);
6297 struct mvpp2_tx_desc *tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
6298 dma_addr_t addr;
6299
6300 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6301 mvpp2_txdesc_size_set(port, tx_desc, hdr_sz);
6302
6303 addr = txq_pcpu->tso_headers_dma +
6304 txq_pcpu->txq_put_index * TSO_HEADER_SIZE;
6305 mvpp2_txdesc_offset_set(port, tx_desc, addr & MVPP2_TX_DESC_ALIGN);
6306 mvpp2_txdesc_dma_addr_set(port, tx_desc, addr & ~MVPP2_TX_DESC_ALIGN);
6307
6308 mvpp2_txdesc_cmd_set(port, tx_desc, mvpp2_skb_tx_csum(port, skb) |
6309 MVPP2_TXD_F_DESC |
6310 MVPP2_TXD_PADDING_DISABLE);
6311 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
6312}
6313
6314static inline int mvpp2_tso_put_data(struct sk_buff *skb,
6315 struct net_device *dev, struct tso_t *tso,
6316 struct mvpp2_tx_queue *txq,
6317 struct mvpp2_tx_queue *aggr_txq,
6318 struct mvpp2_txq_pcpu *txq_pcpu,
6319 int sz, bool left, bool last)
6320{
6321 struct mvpp2_port *port = netdev_priv(dev);
6322 struct mvpp2_tx_desc *tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
6323 dma_addr_t buf_dma_addr;
6324
6325 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6326 mvpp2_txdesc_size_set(port, tx_desc, sz);
6327
6328 buf_dma_addr = dma_map_single(dev->dev.parent, tso->data, sz,
6329 DMA_TO_DEVICE);
6330 if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
6331 mvpp2_txq_desc_put(txq);
6332 return -ENOMEM;
6333 }
6334
6335 mvpp2_txdesc_offset_set(port, tx_desc,
6336 buf_dma_addr & MVPP2_TX_DESC_ALIGN);
6337 mvpp2_txdesc_dma_addr_set(port, tx_desc,
6338 buf_dma_addr & ~MVPP2_TX_DESC_ALIGN);
6339
6340 if (!left) {
6341 mvpp2_txdesc_cmd_set(port, tx_desc, MVPP2_TXD_L_DESC);
6342 if (last) {
6343 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
6344 return 0;
6345 }
6346 } else {
6347 mvpp2_txdesc_cmd_set(port, tx_desc, 0);
6348 }
6349
6350 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
6351 return 0;
6352}
6353
6354static int mvpp2_tx_tso(struct sk_buff *skb, struct net_device *dev,
6355 struct mvpp2_tx_queue *txq,
6356 struct mvpp2_tx_queue *aggr_txq,
6357 struct mvpp2_txq_pcpu *txq_pcpu)
6358{
6359 struct mvpp2_port *port = netdev_priv(dev);
6360 struct tso_t tso;
6361 int hdr_sz = skb_transport_offset(skb) + tcp_hdrlen(skb);
6362 int i, len, descs = 0;
6363
6364 /* Check number of available descriptors */
6365 if (mvpp2_aggr_desc_num_check(port->priv, aggr_txq,
6366 tso_count_descs(skb)) ||
6367 mvpp2_txq_reserved_desc_num_proc(port->priv, txq, txq_pcpu,
6368 tso_count_descs(skb)))
6369 return 0;
6370
6371 tso_start(skb, &tso);
6372 len = skb->len - hdr_sz;
6373 while (len > 0) {
6374 int left = min_t(int, skb_shinfo(skb)->gso_size, len);
6375 char *hdr = txq_pcpu->tso_headers +
6376 txq_pcpu->txq_put_index * TSO_HEADER_SIZE;
6377
6378 len -= left;
6379 descs++;
6380
6381 tso_build_hdr(skb, hdr, &tso, left, len == 0);
6382 mvpp2_tso_put_hdr(skb, dev, txq, aggr_txq, txq_pcpu, hdr_sz);
6383
6384 while (left > 0) {
6385 int sz = min_t(int, tso.size, left);
6386 left -= sz;
6387 descs++;
6388
6389 if (mvpp2_tso_put_data(skb, dev, &tso, txq, aggr_txq,
6390 txq_pcpu, sz, left, len == 0))
6391 goto release;
6392 tso_build_data(skb, &tso, sz);
6393 }
6394 }
6395
6396 return descs;
6397
6398release:
6399 for (i = descs - 1; i >= 0; i--) {
6400 struct mvpp2_tx_desc *tx_desc = txq->descs + i;
6401 tx_desc_unmap_put(port, txq, tx_desc);
6402 }
6403 return 0;
6404}
6405
Marcin Wojtas3f518502014-07-10 16:52:13 -03006406/* Main tx processing */
6407static int mvpp2_tx(struct sk_buff *skb, struct net_device *dev)
6408{
6409 struct mvpp2_port *port = netdev_priv(dev);
6410 struct mvpp2_tx_queue *txq, *aggr_txq;
6411 struct mvpp2_txq_pcpu *txq_pcpu;
6412 struct mvpp2_tx_desc *tx_desc;
Thomas Petazzoni20396132017-03-07 16:53:00 +01006413 dma_addr_t buf_dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006414 int frags = 0;
6415 u16 txq_id;
6416 u32 tx_cmd;
6417
6418 txq_id = skb_get_queue_mapping(skb);
6419 txq = port->txqs[txq_id];
6420 txq_pcpu = this_cpu_ptr(txq->pcpu);
6421 aggr_txq = &port->priv->aggr_txqs[smp_processor_id()];
6422
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006423 if (skb_is_gso(skb)) {
6424 frags = mvpp2_tx_tso(skb, dev, txq, aggr_txq, txq_pcpu);
6425 goto out;
6426 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006427 frags = skb_shinfo(skb)->nr_frags + 1;
6428
6429 /* Check number of available descriptors */
6430 if (mvpp2_aggr_desc_num_check(port->priv, aggr_txq, frags) ||
6431 mvpp2_txq_reserved_desc_num_proc(port->priv, txq,
6432 txq_pcpu, frags)) {
6433 frags = 0;
6434 goto out;
6435 }
6436
6437 /* Get a descriptor for the first part of the packet */
6438 tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006439 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6440 mvpp2_txdesc_size_set(port, tx_desc, skb_headlen(skb));
Marcin Wojtas3f518502014-07-10 16:52:13 -03006441
Thomas Petazzoni20396132017-03-07 16:53:00 +01006442 buf_dma_addr = dma_map_single(dev->dev.parent, skb->data,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006443 skb_headlen(skb), DMA_TO_DEVICE);
Thomas Petazzoni20396132017-03-07 16:53:00 +01006444 if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006445 mvpp2_txq_desc_put(txq);
6446 frags = 0;
6447 goto out;
6448 }
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006449
6450 mvpp2_txdesc_offset_set(port, tx_desc,
6451 buf_dma_addr & MVPP2_TX_DESC_ALIGN);
6452 mvpp2_txdesc_dma_addr_set(port, tx_desc,
6453 buf_dma_addr & ~MVPP2_TX_DESC_ALIGN);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006454
6455 tx_cmd = mvpp2_skb_tx_csum(port, skb);
6456
6457 if (frags == 1) {
6458 /* First and Last descriptor */
6459 tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_L_DESC;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006460 mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
6461 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006462 } else {
6463 /* First but not Last */
6464 tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_PADDING_DISABLE;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006465 mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
6466 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006467
6468 /* Continue with other skb fragments */
6469 if (mvpp2_tx_frag_process(port, skb, aggr_txq, txq)) {
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006470 tx_desc_unmap_put(port, txq, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006471 frags = 0;
6472 goto out;
6473 }
6474 }
6475
Marcin Wojtas3f518502014-07-10 16:52:13 -03006476out:
6477 if (frags > 0) {
6478 struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006479 struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
6480
6481 txq_pcpu->reserved_num -= frags;
6482 txq_pcpu->count += frags;
6483 aggr_txq->count += frags;
6484
6485 /* Enable transmit */
6486 wmb();
6487 mvpp2_aggr_txq_pend_desc_add(port, frags);
6488
6489 if (txq_pcpu->size - txq_pcpu->count < MAX_SKB_FRAGS + 1)
6490 netif_tx_stop_queue(nq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006491
6492 u64_stats_update_begin(&stats->syncp);
6493 stats->tx_packets++;
6494 stats->tx_bytes += skb->len;
6495 u64_stats_update_end(&stats->syncp);
6496 } else {
6497 dev->stats.tx_dropped++;
6498 dev_kfree_skb_any(skb);
6499 }
6500
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006501 /* Finalize TX processing */
Antoine Tenart082297e2017-10-23 15:24:31 +02006502 if (!port->has_tx_irqs && txq_pcpu->count >= txq->done_pkts_coal)
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006503 mvpp2_txq_done(port, txq, txq_pcpu);
6504
6505 /* Set the timer in case not all frags were processed */
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006506 if (!port->has_tx_irqs && txq_pcpu->count <= frags &&
6507 txq_pcpu->count > 0) {
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006508 struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
6509
6510 mvpp2_timer_set(port_pcpu);
6511 }
6512
Marcin Wojtas3f518502014-07-10 16:52:13 -03006513 return NETDEV_TX_OK;
6514}
6515
6516static inline void mvpp2_cause_error(struct net_device *dev, int cause)
6517{
6518 if (cause & MVPP2_CAUSE_FCS_ERR_MASK)
6519 netdev_err(dev, "FCS error\n");
6520 if (cause & MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK)
6521 netdev_err(dev, "rx fifo overrun error\n");
6522 if (cause & MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK)
6523 netdev_err(dev, "tx fifo underrun error\n");
6524}
6525
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006526static int mvpp2_poll(struct napi_struct *napi, int budget)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006527{
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006528 u32 cause_rx_tx, cause_rx, cause_tx, cause_misc;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006529 int rx_done = 0;
6530 struct mvpp2_port *port = netdev_priv(napi->dev);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006531 struct mvpp2_queue_vector *qv;
Thomas Petazzonia7868412017-03-07 16:53:13 +01006532 int cpu = smp_processor_id();
Marcin Wojtas3f518502014-07-10 16:52:13 -03006533
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006534 qv = container_of(napi, struct mvpp2_queue_vector, napi);
6535
Marcin Wojtas3f518502014-07-10 16:52:13 -03006536 /* Rx/Tx cause register
6537 *
6538 * Bits 0-15: each bit indicates received packets on the Rx queue
6539 * (bit 0 is for Rx queue 0).
6540 *
6541 * Bits 16-23: each bit indicates transmitted packets on the Tx queue
6542 * (bit 16 is for Tx queue 0).
6543 *
6544 * Each CPU has its own Rx/Tx cause register
6545 */
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006546 cause_rx_tx = mvpp2_percpu_read(port->priv, qv->sw_thread_id,
Thomas Petazzonia7868412017-03-07 16:53:13 +01006547 MVPP2_ISR_RX_TX_CAUSE_REG(port->id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03006548
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006549 cause_misc = cause_rx_tx & MVPP2_CAUSE_MISC_SUM_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006550 if (cause_misc) {
6551 mvpp2_cause_error(port->dev, cause_misc);
6552
6553 /* Clear the cause register */
6554 mvpp2_write(port->priv, MVPP2_ISR_MISC_CAUSE_REG, 0);
Thomas Petazzonia7868412017-03-07 16:53:13 +01006555 mvpp2_percpu_write(port->priv, cpu,
6556 MVPP2_ISR_RX_TX_CAUSE_REG(port->id),
6557 cause_rx_tx & ~MVPP2_CAUSE_MISC_SUM_MASK);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006558 }
6559
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006560 cause_tx = cause_rx_tx & MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
6561 if (cause_tx) {
6562 cause_tx >>= MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_OFFSET;
6563 mvpp2_tx_done(port, cause_tx, qv->sw_thread_id);
6564 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006565
6566 /* Process RX packets */
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006567 cause_rx = cause_rx_tx & MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
6568 cause_rx <<= qv->first_rxq;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006569 cause_rx |= qv->pending_cause_rx;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006570 while (cause_rx && budget > 0) {
6571 int count;
6572 struct mvpp2_rx_queue *rxq;
6573
6574 rxq = mvpp2_get_rx_queue(port, cause_rx);
6575 if (!rxq)
6576 break;
6577
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006578 count = mvpp2_rx(port, napi, budget, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006579 rx_done += count;
6580 budget -= count;
6581 if (budget > 0) {
6582 /* Clear the bit associated to this Rx queue
6583 * so that next iteration will continue from
6584 * the next Rx queue.
6585 */
6586 cause_rx &= ~(1 << rxq->logic_rxq);
6587 }
6588 }
6589
6590 if (budget > 0) {
6591 cause_rx = 0;
Eric Dumazet6ad20162017-01-30 08:22:01 -08006592 napi_complete_done(napi, rx_done);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006593
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006594 mvpp2_qvec_interrupt_enable(qv);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006595 }
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006596 qv->pending_cause_rx = cause_rx;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006597 return rx_done;
6598}
6599
6600/* Set hw internals when starting port */
6601static void mvpp2_start_dev(struct mvpp2_port *port)
6602{
Philippe Reynes8e072692016-06-28 00:08:11 +02006603 struct net_device *ndev = port->dev;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006604 int i;
Philippe Reynes8e072692016-06-28 00:08:11 +02006605
Stefan Chulski76eb1b12017-08-22 19:08:26 +02006606 if (port->gop_id == 0 &&
6607 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
6608 port->phy_interface == PHY_INTERFACE_MODE_10GKR))
6609 mvpp2_xlg_max_rx_size_set(port);
6610 else
6611 mvpp2_gmac_max_rx_size_set(port);
6612
Marcin Wojtas3f518502014-07-10 16:52:13 -03006613 mvpp2_txp_max_tx_size_set(port);
6614
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006615 for (i = 0; i < port->nqvecs; i++)
6616 napi_enable(&port->qvecs[i].napi);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006617
6618 /* Enable interrupts on all CPUs */
6619 mvpp2_interrupts_enable(port);
6620
Antoine Tenart542897d2017-08-30 10:29:15 +02006621 if (port->priv->hw_version == MVPP22) {
6622 mvpp22_comphy_init(port);
Antoine Ténartf84bf382017-08-22 19:08:27 +02006623 mvpp22_gop_init(port);
Antoine Tenart542897d2017-08-30 10:29:15 +02006624 }
Antoine Ténartf84bf382017-08-22 19:08:27 +02006625
Antoine Ténart2055d622017-08-22 19:08:23 +02006626 mvpp2_port_mii_set(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006627 mvpp2_port_enable(port);
Antoine Tenart5997c862017-09-01 11:04:53 +02006628 if (ndev->phydev)
6629 phy_start(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006630 netif_tx_start_all_queues(port->dev);
6631}
6632
6633/* Set hw internals when stopping port */
6634static void mvpp2_stop_dev(struct mvpp2_port *port)
6635{
Philippe Reynes8e072692016-06-28 00:08:11 +02006636 struct net_device *ndev = port->dev;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006637 int i;
Philippe Reynes8e072692016-06-28 00:08:11 +02006638
Marcin Wojtas3f518502014-07-10 16:52:13 -03006639 /* Stop new packets from arriving to RXQs */
6640 mvpp2_ingress_disable(port);
6641
6642 mdelay(10);
6643
6644 /* Disable interrupts on all CPUs */
6645 mvpp2_interrupts_disable(port);
6646
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006647 for (i = 0; i < port->nqvecs; i++)
6648 napi_disable(&port->qvecs[i].napi);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006649
6650 netif_carrier_off(port->dev);
6651 netif_tx_stop_all_queues(port->dev);
6652
6653 mvpp2_egress_disable(port);
6654 mvpp2_port_disable(port);
Antoine Tenart5997c862017-09-01 11:04:53 +02006655 if (ndev->phydev)
6656 phy_stop(ndev->phydev);
Antoine Tenart542897d2017-08-30 10:29:15 +02006657 phy_power_off(port->comphy);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006658}
6659
Marcin Wojtas3f518502014-07-10 16:52:13 -03006660static int mvpp2_check_ringparam_valid(struct net_device *dev,
6661 struct ethtool_ringparam *ring)
6662{
6663 u16 new_rx_pending = ring->rx_pending;
6664 u16 new_tx_pending = ring->tx_pending;
6665
6666 if (ring->rx_pending == 0 || ring->tx_pending == 0)
6667 return -EINVAL;
6668
6669 if (ring->rx_pending > MVPP2_MAX_RXD)
6670 new_rx_pending = MVPP2_MAX_RXD;
6671 else if (!IS_ALIGNED(ring->rx_pending, 16))
6672 new_rx_pending = ALIGN(ring->rx_pending, 16);
6673
6674 if (ring->tx_pending > MVPP2_MAX_TXD)
6675 new_tx_pending = MVPP2_MAX_TXD;
6676 else if (!IS_ALIGNED(ring->tx_pending, 32))
6677 new_tx_pending = ALIGN(ring->tx_pending, 32);
6678
6679 if (ring->rx_pending != new_rx_pending) {
6680 netdev_info(dev, "illegal Rx ring size value %d, round to %d\n",
6681 ring->rx_pending, new_rx_pending);
6682 ring->rx_pending = new_rx_pending;
6683 }
6684
6685 if (ring->tx_pending != new_tx_pending) {
6686 netdev_info(dev, "illegal Tx ring size value %d, round to %d\n",
6687 ring->tx_pending, new_tx_pending);
6688 ring->tx_pending = new_tx_pending;
6689 }
6690
6691 return 0;
6692}
6693
Thomas Petazzoni26975822017-03-07 16:53:14 +01006694static void mvpp21_get_mac_address(struct mvpp2_port *port, unsigned char *addr)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006695{
6696 u32 mac_addr_l, mac_addr_m, mac_addr_h;
6697
6698 mac_addr_l = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
6699 mac_addr_m = readl(port->priv->lms_base + MVPP2_SRC_ADDR_MIDDLE);
6700 mac_addr_h = readl(port->priv->lms_base + MVPP2_SRC_ADDR_HIGH);
6701 addr[0] = (mac_addr_h >> 24) & 0xFF;
6702 addr[1] = (mac_addr_h >> 16) & 0xFF;
6703 addr[2] = (mac_addr_h >> 8) & 0xFF;
6704 addr[3] = mac_addr_h & 0xFF;
6705 addr[4] = mac_addr_m & 0xFF;
6706 addr[5] = (mac_addr_l >> MVPP2_GMAC_SA_LOW_OFFS) & 0xFF;
6707}
6708
6709static int mvpp2_phy_connect(struct mvpp2_port *port)
6710{
6711 struct phy_device *phy_dev;
6712
Antoine Tenart5997c862017-09-01 11:04:53 +02006713 /* No PHY is attached */
6714 if (!port->phy_node)
6715 return 0;
6716
Marcin Wojtas3f518502014-07-10 16:52:13 -03006717 phy_dev = of_phy_connect(port->dev, port->phy_node, mvpp2_link_event, 0,
6718 port->phy_interface);
6719 if (!phy_dev) {
6720 netdev_err(port->dev, "cannot connect to phy\n");
6721 return -ENODEV;
6722 }
6723 phy_dev->supported &= PHY_GBIT_FEATURES;
6724 phy_dev->advertising = phy_dev->supported;
6725
Marcin Wojtas3f518502014-07-10 16:52:13 -03006726 port->link = 0;
6727 port->duplex = 0;
6728 port->speed = 0;
6729
6730 return 0;
6731}
6732
6733static void mvpp2_phy_disconnect(struct mvpp2_port *port)
6734{
Philippe Reynes8e072692016-06-28 00:08:11 +02006735 struct net_device *ndev = port->dev;
6736
Antoine Tenart5997c862017-09-01 11:04:53 +02006737 if (!ndev->phydev)
6738 return;
6739
Philippe Reynes8e072692016-06-28 00:08:11 +02006740 phy_disconnect(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006741}
6742
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006743static int mvpp2_irqs_init(struct mvpp2_port *port)
6744{
6745 int err, i;
6746
6747 for (i = 0; i < port->nqvecs; i++) {
6748 struct mvpp2_queue_vector *qv = port->qvecs + i;
6749
6750 err = request_irq(qv->irq, mvpp2_isr, 0, port->dev->name, qv);
6751 if (err)
6752 goto err;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006753
6754 if (qv->type == MVPP2_QUEUE_VECTOR_PRIVATE)
6755 irq_set_affinity_hint(qv->irq,
6756 cpumask_of(qv->sw_thread_id));
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006757 }
6758
6759 return 0;
6760err:
6761 for (i = 0; i < port->nqvecs; i++) {
6762 struct mvpp2_queue_vector *qv = port->qvecs + i;
6763
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006764 irq_set_affinity_hint(qv->irq, NULL);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006765 free_irq(qv->irq, qv);
6766 }
6767
6768 return err;
6769}
6770
6771static void mvpp2_irqs_deinit(struct mvpp2_port *port)
6772{
6773 int i;
6774
6775 for (i = 0; i < port->nqvecs; i++) {
6776 struct mvpp2_queue_vector *qv = port->qvecs + i;
6777
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006778 irq_set_affinity_hint(qv->irq, NULL);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006779 free_irq(qv->irq, qv);
6780 }
6781}
6782
Marcin Wojtas3f518502014-07-10 16:52:13 -03006783static int mvpp2_open(struct net_device *dev)
6784{
6785 struct mvpp2_port *port = netdev_priv(dev);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02006786 struct mvpp2 *priv = port->priv;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006787 unsigned char mac_bcast[ETH_ALEN] = {
6788 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
6789 int err;
6790
6791 err = mvpp2_prs_mac_da_accept(port->priv, port->id, mac_bcast, true);
6792 if (err) {
6793 netdev_err(dev, "mvpp2_prs_mac_da_accept BC failed\n");
6794 return err;
6795 }
6796 err = mvpp2_prs_mac_da_accept(port->priv, port->id,
6797 dev->dev_addr, true);
6798 if (err) {
6799 netdev_err(dev, "mvpp2_prs_mac_da_accept MC failed\n");
6800 return err;
6801 }
6802 err = mvpp2_prs_tag_mode_set(port->priv, port->id, MVPP2_TAG_TYPE_MH);
6803 if (err) {
6804 netdev_err(dev, "mvpp2_prs_tag_mode_set failed\n");
6805 return err;
6806 }
6807 err = mvpp2_prs_def_flow(port);
6808 if (err) {
6809 netdev_err(dev, "mvpp2_prs_def_flow failed\n");
6810 return err;
6811 }
6812
6813 /* Allocate the Rx/Tx queues */
6814 err = mvpp2_setup_rxqs(port);
6815 if (err) {
6816 netdev_err(port->dev, "cannot allocate Rx queues\n");
6817 return err;
6818 }
6819
6820 err = mvpp2_setup_txqs(port);
6821 if (err) {
6822 netdev_err(port->dev, "cannot allocate Tx queues\n");
6823 goto err_cleanup_rxqs;
6824 }
6825
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006826 err = mvpp2_irqs_init(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006827 if (err) {
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006828 netdev_err(port->dev, "cannot init IRQs\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03006829 goto err_cleanup_txqs;
6830 }
6831
Antoine Tenartfd3651b2017-09-01 11:04:54 +02006832 if (priv->hw_version == MVPP22 && !port->phy_node && port->link_irq) {
6833 err = request_irq(port->link_irq, mvpp2_link_status_isr, 0,
6834 dev->name, port);
6835 if (err) {
6836 netdev_err(port->dev, "cannot request link IRQ %d\n",
6837 port->link_irq);
6838 goto err_free_irq;
6839 }
6840
6841 mvpp22_gop_setup_irq(port);
6842 }
6843
Marcin Wojtas3f518502014-07-10 16:52:13 -03006844 /* In default link is down */
6845 netif_carrier_off(port->dev);
6846
6847 err = mvpp2_phy_connect(port);
6848 if (err < 0)
Antoine Tenartfd3651b2017-09-01 11:04:54 +02006849 goto err_free_link_irq;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006850
6851 /* Unmask interrupts on all CPUs */
6852 on_each_cpu(mvpp2_interrupts_unmask, port, 1);
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006853 mvpp2_shared_interrupt_mask_unmask(port, false);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006854
6855 mvpp2_start_dev(port);
6856
6857 return 0;
6858
Antoine Tenartfd3651b2017-09-01 11:04:54 +02006859err_free_link_irq:
6860 if (priv->hw_version == MVPP22 && !port->phy_node && port->link_irq)
6861 free_irq(port->link_irq, port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006862err_free_irq:
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006863 mvpp2_irqs_deinit(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006864err_cleanup_txqs:
6865 mvpp2_cleanup_txqs(port);
6866err_cleanup_rxqs:
6867 mvpp2_cleanup_rxqs(port);
6868 return err;
6869}
6870
6871static int mvpp2_stop(struct net_device *dev)
6872{
6873 struct mvpp2_port *port = netdev_priv(dev);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006874 struct mvpp2_port_pcpu *port_pcpu;
Antoine Tenartfd3651b2017-09-01 11:04:54 +02006875 struct mvpp2 *priv = port->priv;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006876 int cpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006877
6878 mvpp2_stop_dev(port);
6879 mvpp2_phy_disconnect(port);
6880
6881 /* Mask interrupts on all CPUs */
6882 on_each_cpu(mvpp2_interrupts_mask, port, 1);
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006883 mvpp2_shared_interrupt_mask_unmask(port, true);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006884
Antoine Tenartfd3651b2017-09-01 11:04:54 +02006885 if (priv->hw_version == MVPP22 && !port->phy_node && port->link_irq)
6886 free_irq(port->link_irq, port);
6887
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006888 mvpp2_irqs_deinit(port);
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006889 if (!port->has_tx_irqs) {
6890 for_each_present_cpu(cpu) {
6891 port_pcpu = per_cpu_ptr(port->pcpu, cpu);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006892
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006893 hrtimer_cancel(&port_pcpu->tx_done_timer);
6894 port_pcpu->timer_scheduled = false;
6895 tasklet_kill(&port_pcpu->tx_done_tasklet);
6896 }
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006897 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006898 mvpp2_cleanup_rxqs(port);
6899 mvpp2_cleanup_txqs(port);
6900
6901 return 0;
6902}
6903
6904static void mvpp2_set_rx_mode(struct net_device *dev)
6905{
6906 struct mvpp2_port *port = netdev_priv(dev);
6907 struct mvpp2 *priv = port->priv;
6908 struct netdev_hw_addr *ha;
6909 int id = port->id;
6910 bool allmulti = dev->flags & IFF_ALLMULTI;
6911
6912 mvpp2_prs_mac_promisc_set(priv, id, dev->flags & IFF_PROMISC);
6913 mvpp2_prs_mac_multi_set(priv, id, MVPP2_PE_MAC_MC_ALL, allmulti);
6914 mvpp2_prs_mac_multi_set(priv, id, MVPP2_PE_MAC_MC_IP6, allmulti);
6915
6916 /* Remove all port->id's mcast enries */
6917 mvpp2_prs_mcast_del_all(priv, id);
6918
6919 if (allmulti && !netdev_mc_empty(dev)) {
6920 netdev_for_each_mc_addr(ha, dev)
6921 mvpp2_prs_mac_da_accept(priv, id, ha->addr, true);
6922 }
6923}
6924
6925static int mvpp2_set_mac_address(struct net_device *dev, void *p)
6926{
6927 struct mvpp2_port *port = netdev_priv(dev);
6928 const struct sockaddr *addr = p;
6929 int err;
6930
6931 if (!is_valid_ether_addr(addr->sa_data)) {
6932 err = -EADDRNOTAVAIL;
Markus Elfringc1175542017-04-17 11:10:47 +02006933 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006934 }
6935
6936 if (!netif_running(dev)) {
6937 err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
6938 if (!err)
6939 return 0;
6940 /* Reconfigure parser to accept the original MAC address */
6941 err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
6942 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02006943 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006944 }
6945
6946 mvpp2_stop_dev(port);
6947
6948 err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
6949 if (!err)
6950 goto out_start;
6951
6952 /* Reconfigure parser accept the original MAC address */
6953 err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
6954 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02006955 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006956out_start:
6957 mvpp2_start_dev(port);
6958 mvpp2_egress_enable(port);
6959 mvpp2_ingress_enable(port);
6960 return 0;
Markus Elfringc1175542017-04-17 11:10:47 +02006961log_error:
Markus Elfringdfd42402017-04-17 11:20:41 +02006962 netdev_err(dev, "failed to change MAC address\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03006963 return err;
6964}
6965
6966static int mvpp2_change_mtu(struct net_device *dev, int mtu)
6967{
6968 struct mvpp2_port *port = netdev_priv(dev);
6969 int err;
6970
Jarod Wilson57779872016-10-17 15:54:06 -04006971 if (!IS_ALIGNED(MVPP2_RX_PKT_SIZE(mtu), 8)) {
6972 netdev_info(dev, "illegal MTU value %d, round to %d\n", mtu,
6973 ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8));
6974 mtu = ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006975 }
6976
6977 if (!netif_running(dev)) {
6978 err = mvpp2_bm_update_mtu(dev, mtu);
6979 if (!err) {
6980 port->pkt_size = MVPP2_RX_PKT_SIZE(mtu);
6981 return 0;
6982 }
6983
6984 /* Reconfigure BM to the original MTU */
6985 err = mvpp2_bm_update_mtu(dev, dev->mtu);
6986 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02006987 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006988 }
6989
6990 mvpp2_stop_dev(port);
6991
6992 err = mvpp2_bm_update_mtu(dev, mtu);
6993 if (!err) {
6994 port->pkt_size = MVPP2_RX_PKT_SIZE(mtu);
6995 goto out_start;
6996 }
6997
6998 /* Reconfigure BM to the original MTU */
6999 err = mvpp2_bm_update_mtu(dev, dev->mtu);
7000 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02007001 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007002
7003out_start:
7004 mvpp2_start_dev(port);
7005 mvpp2_egress_enable(port);
7006 mvpp2_ingress_enable(port);
7007
7008 return 0;
Markus Elfringc1175542017-04-17 11:10:47 +02007009log_error:
Markus Elfringdfd42402017-04-17 11:20:41 +02007010 netdev_err(dev, "failed to change MTU\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03007011 return err;
7012}
7013
stephen hemmingerbc1f4472017-01-06 19:12:52 -08007014static void
Marcin Wojtas3f518502014-07-10 16:52:13 -03007015mvpp2_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
7016{
7017 struct mvpp2_port *port = netdev_priv(dev);
7018 unsigned int start;
7019 int cpu;
7020
7021 for_each_possible_cpu(cpu) {
7022 struct mvpp2_pcpu_stats *cpu_stats;
7023 u64 rx_packets;
7024 u64 rx_bytes;
7025 u64 tx_packets;
7026 u64 tx_bytes;
7027
7028 cpu_stats = per_cpu_ptr(port->stats, cpu);
7029 do {
7030 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
7031 rx_packets = cpu_stats->rx_packets;
7032 rx_bytes = cpu_stats->rx_bytes;
7033 tx_packets = cpu_stats->tx_packets;
7034 tx_bytes = cpu_stats->tx_bytes;
7035 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
7036
7037 stats->rx_packets += rx_packets;
7038 stats->rx_bytes += rx_bytes;
7039 stats->tx_packets += tx_packets;
7040 stats->tx_bytes += tx_bytes;
7041 }
7042
7043 stats->rx_errors = dev->stats.rx_errors;
7044 stats->rx_dropped = dev->stats.rx_dropped;
7045 stats->tx_dropped = dev->stats.tx_dropped;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007046}
7047
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007048static int mvpp2_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
7049{
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007050 int ret;
7051
Philippe Reynes8e072692016-06-28 00:08:11 +02007052 if (!dev->phydev)
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007053 return -ENOTSUPP;
7054
Philippe Reynes8e072692016-06-28 00:08:11 +02007055 ret = phy_mii_ioctl(dev->phydev, ifr, cmd);
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007056 if (!ret)
7057 mvpp2_link_event(dev);
7058
7059 return ret;
7060}
7061
Marcin Wojtas3f518502014-07-10 16:52:13 -03007062/* Ethtool methods */
7063
Marcin Wojtas3f518502014-07-10 16:52:13 -03007064/* Set interrupt coalescing for ethtools */
7065static int mvpp2_ethtool_set_coalesce(struct net_device *dev,
7066 struct ethtool_coalesce *c)
7067{
7068 struct mvpp2_port *port = netdev_priv(dev);
7069 int queue;
7070
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007071 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007072 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
7073
7074 rxq->time_coal = c->rx_coalesce_usecs;
7075 rxq->pkts_coal = c->rx_max_coalesced_frames;
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01007076 mvpp2_rx_pkts_coal_set(port, rxq);
7077 mvpp2_rx_time_coal_set(port, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007078 }
7079
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007080 if (port->has_tx_irqs) {
7081 port->tx_time_coal = c->tx_coalesce_usecs;
7082 mvpp2_tx_time_coal_set(port);
7083 }
7084
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007085 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007086 struct mvpp2_tx_queue *txq = port->txqs[queue];
7087
7088 txq->done_pkts_coal = c->tx_max_coalesced_frames;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007089
7090 if (port->has_tx_irqs)
7091 mvpp2_tx_pkts_coal_set(port, txq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007092 }
7093
Marcin Wojtas3f518502014-07-10 16:52:13 -03007094 return 0;
7095}
7096
7097/* get coalescing for ethtools */
7098static int mvpp2_ethtool_get_coalesce(struct net_device *dev,
7099 struct ethtool_coalesce *c)
7100{
7101 struct mvpp2_port *port = netdev_priv(dev);
7102
7103 c->rx_coalesce_usecs = port->rxqs[0]->time_coal;
7104 c->rx_max_coalesced_frames = port->rxqs[0]->pkts_coal;
7105 c->tx_max_coalesced_frames = port->txqs[0]->done_pkts_coal;
7106 return 0;
7107}
7108
7109static void mvpp2_ethtool_get_drvinfo(struct net_device *dev,
7110 struct ethtool_drvinfo *drvinfo)
7111{
7112 strlcpy(drvinfo->driver, MVPP2_DRIVER_NAME,
7113 sizeof(drvinfo->driver));
7114 strlcpy(drvinfo->version, MVPP2_DRIVER_VERSION,
7115 sizeof(drvinfo->version));
7116 strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
7117 sizeof(drvinfo->bus_info));
7118}
7119
7120static void mvpp2_ethtool_get_ringparam(struct net_device *dev,
7121 struct ethtool_ringparam *ring)
7122{
7123 struct mvpp2_port *port = netdev_priv(dev);
7124
7125 ring->rx_max_pending = MVPP2_MAX_RXD;
7126 ring->tx_max_pending = MVPP2_MAX_TXD;
7127 ring->rx_pending = port->rx_ring_size;
7128 ring->tx_pending = port->tx_ring_size;
7129}
7130
7131static int mvpp2_ethtool_set_ringparam(struct net_device *dev,
7132 struct ethtool_ringparam *ring)
7133{
7134 struct mvpp2_port *port = netdev_priv(dev);
7135 u16 prev_rx_ring_size = port->rx_ring_size;
7136 u16 prev_tx_ring_size = port->tx_ring_size;
7137 int err;
7138
7139 err = mvpp2_check_ringparam_valid(dev, ring);
7140 if (err)
7141 return err;
7142
7143 if (!netif_running(dev)) {
7144 port->rx_ring_size = ring->rx_pending;
7145 port->tx_ring_size = ring->tx_pending;
7146 return 0;
7147 }
7148
7149 /* The interface is running, so we have to force a
7150 * reallocation of the queues
7151 */
7152 mvpp2_stop_dev(port);
7153 mvpp2_cleanup_rxqs(port);
7154 mvpp2_cleanup_txqs(port);
7155
7156 port->rx_ring_size = ring->rx_pending;
7157 port->tx_ring_size = ring->tx_pending;
7158
7159 err = mvpp2_setup_rxqs(port);
7160 if (err) {
7161 /* Reallocate Rx queues with the original ring size */
7162 port->rx_ring_size = prev_rx_ring_size;
7163 ring->rx_pending = prev_rx_ring_size;
7164 err = mvpp2_setup_rxqs(port);
7165 if (err)
7166 goto err_out;
7167 }
7168 err = mvpp2_setup_txqs(port);
7169 if (err) {
7170 /* Reallocate Tx queues with the original ring size */
7171 port->tx_ring_size = prev_tx_ring_size;
7172 ring->tx_pending = prev_tx_ring_size;
7173 err = mvpp2_setup_txqs(port);
7174 if (err)
7175 goto err_clean_rxqs;
7176 }
7177
7178 mvpp2_start_dev(port);
7179 mvpp2_egress_enable(port);
7180 mvpp2_ingress_enable(port);
7181
7182 return 0;
7183
7184err_clean_rxqs:
7185 mvpp2_cleanup_rxqs(port);
7186err_out:
Markus Elfringdfd42402017-04-17 11:20:41 +02007187 netdev_err(dev, "failed to change ring parameters");
Marcin Wojtas3f518502014-07-10 16:52:13 -03007188 return err;
7189}
7190
7191/* Device ops */
7192
7193static const struct net_device_ops mvpp2_netdev_ops = {
7194 .ndo_open = mvpp2_open,
7195 .ndo_stop = mvpp2_stop,
7196 .ndo_start_xmit = mvpp2_tx,
7197 .ndo_set_rx_mode = mvpp2_set_rx_mode,
7198 .ndo_set_mac_address = mvpp2_set_mac_address,
7199 .ndo_change_mtu = mvpp2_change_mtu,
7200 .ndo_get_stats64 = mvpp2_get_stats64,
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007201 .ndo_do_ioctl = mvpp2_ioctl,
Marcin Wojtas3f518502014-07-10 16:52:13 -03007202};
7203
7204static const struct ethtool_ops mvpp2_eth_tool_ops = {
Florian Fainelli00606c42016-11-15 11:19:48 -08007205 .nway_reset = phy_ethtool_nway_reset,
Marcin Wojtas3f518502014-07-10 16:52:13 -03007206 .get_link = ethtool_op_get_link,
Marcin Wojtas3f518502014-07-10 16:52:13 -03007207 .set_coalesce = mvpp2_ethtool_set_coalesce,
7208 .get_coalesce = mvpp2_ethtool_get_coalesce,
7209 .get_drvinfo = mvpp2_ethtool_get_drvinfo,
7210 .get_ringparam = mvpp2_ethtool_get_ringparam,
7211 .set_ringparam = mvpp2_ethtool_set_ringparam,
Philippe Reynesfb773e92016-06-28 00:08:12 +02007212 .get_link_ksettings = phy_ethtool_get_link_ksettings,
7213 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Marcin Wojtas3f518502014-07-10 16:52:13 -03007214};
7215
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007216/* Used for PPv2.1, or PPv2.2 with the old Device Tree binding that
7217 * had a single IRQ defined per-port.
7218 */
7219static int mvpp2_simple_queue_vectors_init(struct mvpp2_port *port,
7220 struct device_node *port_node)
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007221{
7222 struct mvpp2_queue_vector *v = &port->qvecs[0];
7223
7224 v->first_rxq = 0;
7225 v->nrxqs = port->nrxqs;
7226 v->type = MVPP2_QUEUE_VECTOR_SHARED;
7227 v->sw_thread_id = 0;
7228 v->sw_thread_mask = *cpumask_bits(cpu_online_mask);
7229 v->port = port;
7230 v->irq = irq_of_parse_and_map(port_node, 0);
7231 if (v->irq <= 0)
7232 return -EINVAL;
7233 netif_napi_add(port->dev, &v->napi, mvpp2_poll,
7234 NAPI_POLL_WEIGHT);
7235
7236 port->nqvecs = 1;
7237
7238 return 0;
7239}
7240
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007241static int mvpp2_multi_queue_vectors_init(struct mvpp2_port *port,
7242 struct device_node *port_node)
7243{
7244 struct mvpp2_queue_vector *v;
7245 int i, ret;
7246
7247 port->nqvecs = num_possible_cpus();
7248 if (queue_mode == MVPP2_QDIST_SINGLE_MODE)
7249 port->nqvecs += 1;
7250
7251 for (i = 0; i < port->nqvecs; i++) {
7252 char irqname[16];
7253
7254 v = port->qvecs + i;
7255
7256 v->port = port;
7257 v->type = MVPP2_QUEUE_VECTOR_PRIVATE;
7258 v->sw_thread_id = i;
7259 v->sw_thread_mask = BIT(i);
7260
7261 snprintf(irqname, sizeof(irqname), "tx-cpu%d", i);
7262
7263 if (queue_mode == MVPP2_QDIST_MULTI_MODE) {
7264 v->first_rxq = i * MVPP2_DEFAULT_RXQ;
7265 v->nrxqs = MVPP2_DEFAULT_RXQ;
7266 } else if (queue_mode == MVPP2_QDIST_SINGLE_MODE &&
7267 i == (port->nqvecs - 1)) {
7268 v->first_rxq = 0;
7269 v->nrxqs = port->nrxqs;
7270 v->type = MVPP2_QUEUE_VECTOR_SHARED;
7271 strncpy(irqname, "rx-shared", sizeof(irqname));
7272 }
7273
7274 v->irq = of_irq_get_byname(port_node, irqname);
7275 if (v->irq <= 0) {
7276 ret = -EINVAL;
7277 goto err;
7278 }
7279
7280 netif_napi_add(port->dev, &v->napi, mvpp2_poll,
7281 NAPI_POLL_WEIGHT);
7282 }
7283
7284 return 0;
7285
7286err:
7287 for (i = 0; i < port->nqvecs; i++)
7288 irq_dispose_mapping(port->qvecs[i].irq);
7289 return ret;
7290}
7291
7292static int mvpp2_queue_vectors_init(struct mvpp2_port *port,
7293 struct device_node *port_node)
7294{
7295 if (port->has_tx_irqs)
7296 return mvpp2_multi_queue_vectors_init(port, port_node);
7297 else
7298 return mvpp2_simple_queue_vectors_init(port, port_node);
7299}
7300
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007301static void mvpp2_queue_vectors_deinit(struct mvpp2_port *port)
7302{
7303 int i;
7304
7305 for (i = 0; i < port->nqvecs; i++)
7306 irq_dispose_mapping(port->qvecs[i].irq);
7307}
7308
7309/* Configure Rx queue group interrupt for this port */
7310static void mvpp2_rx_irqs_setup(struct mvpp2_port *port)
7311{
7312 struct mvpp2 *priv = port->priv;
7313 u32 val;
7314 int i;
7315
7316 if (priv->hw_version == MVPP21) {
7317 mvpp2_write(priv, MVPP21_ISR_RXQ_GROUP_REG(port->id),
7318 port->nrxqs);
7319 return;
7320 }
7321
7322 /* Handle the more complicated PPv2.2 case */
7323 for (i = 0; i < port->nqvecs; i++) {
7324 struct mvpp2_queue_vector *qv = port->qvecs + i;
7325
7326 if (!qv->nrxqs)
7327 continue;
7328
7329 val = qv->sw_thread_id;
7330 val |= port->id << MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_OFFSET;
7331 mvpp2_write(priv, MVPP22_ISR_RXQ_GROUP_INDEX_REG, val);
7332
7333 val = qv->first_rxq;
7334 val |= qv->nrxqs << MVPP22_ISR_RXQ_SUB_GROUP_SIZE_OFFSET;
7335 mvpp2_write(priv, MVPP22_ISR_RXQ_SUB_GROUP_CONFIG_REG, val);
7336 }
7337}
7338
Marcin Wojtas3f518502014-07-10 16:52:13 -03007339/* Initialize port HW */
7340static int mvpp2_port_init(struct mvpp2_port *port)
7341{
7342 struct device *dev = port->dev->dev.parent;
7343 struct mvpp2 *priv = port->priv;
7344 struct mvpp2_txq_pcpu *txq_pcpu;
7345 int queue, cpu, err;
7346
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007347 /* Checks for hardware constraints */
7348 if (port->first_rxq + port->nrxqs >
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01007349 MVPP2_MAX_PORTS * priv->max_port_rxqs)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007350 return -EINVAL;
7351
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007352 if (port->nrxqs % 4 || (port->nrxqs > priv->max_port_rxqs) ||
7353 (port->ntxqs > MVPP2_MAX_TXQ))
7354 return -EINVAL;
7355
Marcin Wojtas3f518502014-07-10 16:52:13 -03007356 /* Disable port */
7357 mvpp2_egress_disable(port);
7358 mvpp2_port_disable(port);
7359
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007360 port->tx_time_coal = MVPP2_TXDONE_COAL_USEC;
7361
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007362 port->txqs = devm_kcalloc(dev, port->ntxqs, sizeof(*port->txqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03007363 GFP_KERNEL);
7364 if (!port->txqs)
7365 return -ENOMEM;
7366
7367 /* Associate physical Tx queues to this port and initialize.
7368 * The mapping is predefined.
7369 */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007370 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007371 int queue_phy_id = mvpp2_txq_phys(port->id, queue);
7372 struct mvpp2_tx_queue *txq;
7373
7374 txq = devm_kzalloc(dev, sizeof(*txq), GFP_KERNEL);
Christophe Jaillet177c8d12017-02-19 10:19:57 +01007375 if (!txq) {
7376 err = -ENOMEM;
7377 goto err_free_percpu;
7378 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03007379
7380 txq->pcpu = alloc_percpu(struct mvpp2_txq_pcpu);
7381 if (!txq->pcpu) {
7382 err = -ENOMEM;
7383 goto err_free_percpu;
7384 }
7385
7386 txq->id = queue_phy_id;
7387 txq->log_id = queue;
7388 txq->done_pkts_coal = MVPP2_TXDONE_COAL_PKTS_THRESH;
7389 for_each_present_cpu(cpu) {
7390 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
7391 txq_pcpu->cpu = cpu;
7392 }
7393
7394 port->txqs[queue] = txq;
7395 }
7396
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007397 port->rxqs = devm_kcalloc(dev, port->nrxqs, sizeof(*port->rxqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03007398 GFP_KERNEL);
7399 if (!port->rxqs) {
7400 err = -ENOMEM;
7401 goto err_free_percpu;
7402 }
7403
7404 /* Allocate and initialize Rx queue for this port */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007405 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007406 struct mvpp2_rx_queue *rxq;
7407
7408 /* Map physical Rx queue to port's logical Rx queue */
7409 rxq = devm_kzalloc(dev, sizeof(*rxq), GFP_KERNEL);
Jisheng Zhangd82b0c22016-03-31 17:01:23 +08007410 if (!rxq) {
7411 err = -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007412 goto err_free_percpu;
Jisheng Zhangd82b0c22016-03-31 17:01:23 +08007413 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03007414 /* Map this Rx queue to a physical queue */
7415 rxq->id = port->first_rxq + queue;
7416 rxq->port = port->id;
7417 rxq->logic_rxq = queue;
7418
7419 port->rxqs[queue] = rxq;
7420 }
7421
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007422 mvpp2_rx_irqs_setup(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007423
7424 /* Create Rx descriptor rings */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007425 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007426 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
7427
7428 rxq->size = port->rx_ring_size;
7429 rxq->pkts_coal = MVPP2_RX_COAL_PKTS;
7430 rxq->time_coal = MVPP2_RX_COAL_USEC;
7431 }
7432
7433 mvpp2_ingress_disable(port);
7434
7435 /* Port default configuration */
7436 mvpp2_defaults_set(port);
7437
7438 /* Port's classifier configuration */
7439 mvpp2_cls_oversize_rxq_set(port);
7440 mvpp2_cls_port_config(port);
7441
7442 /* Provide an initial Rx packet size */
7443 port->pkt_size = MVPP2_RX_PKT_SIZE(port->dev->mtu);
7444
7445 /* Initialize pools for swf */
7446 err = mvpp2_swf_bm_pool_init(port);
7447 if (err)
7448 goto err_free_percpu;
7449
7450 return 0;
7451
7452err_free_percpu:
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007453 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007454 if (!port->txqs[queue])
7455 continue;
7456 free_percpu(port->txqs[queue]->pcpu);
7457 }
7458 return err;
7459}
7460
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007461/* Checks if the port DT description has the TX interrupts
7462 * described. On PPv2.1, there are no such interrupts. On PPv2.2,
7463 * there are available, but we need to keep support for old DTs.
7464 */
7465static bool mvpp2_port_has_tx_irqs(struct mvpp2 *priv,
7466 struct device_node *port_node)
7467{
7468 char *irqs[5] = { "rx-shared", "tx-cpu0", "tx-cpu1",
7469 "tx-cpu2", "tx-cpu3" };
7470 int ret, i;
7471
7472 if (priv->hw_version == MVPP21)
7473 return false;
7474
7475 for (i = 0; i < 5; i++) {
7476 ret = of_property_match_string(port_node, "interrupt-names",
7477 irqs[i]);
7478 if (ret < 0)
7479 return false;
7480 }
7481
7482 return true;
7483}
7484
Antoine Tenart3ba8c812017-09-02 11:06:47 +02007485static void mvpp2_port_copy_mac_addr(struct net_device *dev, struct mvpp2 *priv,
7486 struct device_node *port_node,
7487 char **mac_from)
7488{
7489 struct mvpp2_port *port = netdev_priv(dev);
7490 char hw_mac_addr[ETH_ALEN] = {0};
7491 const char *dt_mac_addr;
7492
7493 dt_mac_addr = of_get_mac_address(port_node);
7494 if (dt_mac_addr && is_valid_ether_addr(dt_mac_addr)) {
7495 *mac_from = "device tree";
7496 ether_addr_copy(dev->dev_addr, dt_mac_addr);
Antoine Tenart688cbaf2017-09-02 11:06:49 +02007497 return;
Antoine Tenart3ba8c812017-09-02 11:06:47 +02007498 }
Antoine Tenart688cbaf2017-09-02 11:06:49 +02007499
7500 if (priv->hw_version == MVPP21) {
7501 mvpp21_get_mac_address(port, hw_mac_addr);
7502 if (is_valid_ether_addr(hw_mac_addr)) {
7503 *mac_from = "hardware";
7504 ether_addr_copy(dev->dev_addr, hw_mac_addr);
7505 return;
7506 }
7507 }
7508
7509 *mac_from = "random";
7510 eth_hw_addr_random(dev);
Antoine Tenart3ba8c812017-09-02 11:06:47 +02007511}
7512
Marcin Wojtas3f518502014-07-10 16:52:13 -03007513/* Ports initialization */
7514static int mvpp2_port_probe(struct platform_device *pdev,
7515 struct device_node *port_node,
Yan Markman6bf69a12017-09-25 14:59:47 +02007516 struct mvpp2 *priv, int index)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007517{
7518 struct device_node *phy_node;
Antoine Tenart542897d2017-08-30 10:29:15 +02007519 struct phy *comphy;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007520 struct mvpp2_port *port;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007521 struct mvpp2_port_pcpu *port_pcpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007522 struct net_device *dev;
7523 struct resource *res;
Antoine Tenart3ba8c812017-09-02 11:06:47 +02007524 char *mac_from = "";
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007525 unsigned int ntxqs, nrxqs;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007526 bool has_tx_irqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007527 u32 id;
7528 int features;
7529 int phy_mode;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007530 int err, i, cpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007531
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007532 has_tx_irqs = mvpp2_port_has_tx_irqs(priv, port_node);
7533
7534 if (!has_tx_irqs)
7535 queue_mode = MVPP2_QDIST_SINGLE_MODE;
7536
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007537 ntxqs = MVPP2_MAX_TXQ;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007538 if (priv->hw_version == MVPP22 && queue_mode == MVPP2_QDIST_MULTI_MODE)
7539 nrxqs = MVPP2_DEFAULT_RXQ * num_possible_cpus();
7540 else
7541 nrxqs = MVPP2_DEFAULT_RXQ;
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007542
7543 dev = alloc_etherdev_mqs(sizeof(*port), ntxqs, nrxqs);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007544 if (!dev)
7545 return -ENOMEM;
7546
7547 phy_node = of_parse_phandle(port_node, "phy", 0);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007548 phy_mode = of_get_phy_mode(port_node);
7549 if (phy_mode < 0) {
7550 dev_err(&pdev->dev, "incorrect phy mode\n");
7551 err = phy_mode;
7552 goto err_free_netdev;
7553 }
7554
Antoine Tenart542897d2017-08-30 10:29:15 +02007555 comphy = devm_of_phy_get(&pdev->dev, port_node, NULL);
7556 if (IS_ERR(comphy)) {
7557 if (PTR_ERR(comphy) == -EPROBE_DEFER) {
7558 err = -EPROBE_DEFER;
7559 goto err_free_netdev;
7560 }
7561 comphy = NULL;
7562 }
7563
Marcin Wojtas3f518502014-07-10 16:52:13 -03007564 if (of_property_read_u32(port_node, "port-id", &id)) {
7565 err = -EINVAL;
7566 dev_err(&pdev->dev, "missing port-id value\n");
7567 goto err_free_netdev;
7568 }
7569
7570 dev->tx_queue_len = MVPP2_MAX_TXD;
7571 dev->watchdog_timeo = 5 * HZ;
7572 dev->netdev_ops = &mvpp2_netdev_ops;
7573 dev->ethtool_ops = &mvpp2_eth_tool_ops;
7574
7575 port = netdev_priv(dev);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007576 port->dev = dev;
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007577 port->ntxqs = ntxqs;
7578 port->nrxqs = nrxqs;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007579 port->priv = priv;
7580 port->has_tx_irqs = has_tx_irqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007581
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007582 err = mvpp2_queue_vectors_init(port, port_node);
7583 if (err)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007584 goto err_free_netdev;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007585
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007586 port->link_irq = of_irq_get_byname(port_node, "link");
7587 if (port->link_irq == -EPROBE_DEFER) {
7588 err = -EPROBE_DEFER;
7589 goto err_deinit_qvecs;
7590 }
7591 if (port->link_irq <= 0)
7592 /* the link irq is optional */
7593 port->link_irq = 0;
7594
Marcin Wojtas3f518502014-07-10 16:52:13 -03007595 if (of_property_read_bool(port_node, "marvell,loopback"))
7596 port->flags |= MVPP2_F_LOOPBACK;
7597
Marcin Wojtas3f518502014-07-10 16:52:13 -03007598 port->id = id;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01007599 if (priv->hw_version == MVPP21)
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007600 port->first_rxq = port->id * port->nrxqs;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01007601 else
7602 port->first_rxq = port->id * priv->max_port_rxqs;
7603
Marcin Wojtas3f518502014-07-10 16:52:13 -03007604 port->phy_node = phy_node;
7605 port->phy_interface = phy_mode;
Antoine Tenart542897d2017-08-30 10:29:15 +02007606 port->comphy = comphy;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007607
Thomas Petazzonia7868412017-03-07 16:53:13 +01007608 if (priv->hw_version == MVPP21) {
7609 res = platform_get_resource(pdev, IORESOURCE_MEM, 2 + id);
7610 port->base = devm_ioremap_resource(&pdev->dev, res);
7611 if (IS_ERR(port->base)) {
7612 err = PTR_ERR(port->base);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007613 goto err_free_irq;
Thomas Petazzonia7868412017-03-07 16:53:13 +01007614 }
7615 } else {
7616 if (of_property_read_u32(port_node, "gop-port-id",
7617 &port->gop_id)) {
7618 err = -EINVAL;
7619 dev_err(&pdev->dev, "missing gop-port-id value\n");
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007620 goto err_deinit_qvecs;
Thomas Petazzonia7868412017-03-07 16:53:13 +01007621 }
7622
7623 port->base = priv->iface_base + MVPP22_GMAC_BASE(port->gop_id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007624 }
7625
7626 /* Alloc per-cpu stats */
7627 port->stats = netdev_alloc_pcpu_stats(struct mvpp2_pcpu_stats);
7628 if (!port->stats) {
7629 err = -ENOMEM;
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007630 goto err_free_irq;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007631 }
7632
Antoine Tenart3ba8c812017-09-02 11:06:47 +02007633 mvpp2_port_copy_mac_addr(dev, priv, port_node, &mac_from);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007634
7635 port->tx_ring_size = MVPP2_MAX_TXD;
7636 port->rx_ring_size = MVPP2_MAX_RXD;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007637 SET_NETDEV_DEV(dev, &pdev->dev);
7638
7639 err = mvpp2_port_init(port);
7640 if (err < 0) {
7641 dev_err(&pdev->dev, "failed to init port %d\n", id);
7642 goto err_free_stats;
7643 }
Thomas Petazzoni26975822017-03-07 16:53:14 +01007644
Thomas Petazzoni26975822017-03-07 16:53:14 +01007645 mvpp2_port_periodic_xon_disable(port);
7646
7647 if (priv->hw_version == MVPP21)
7648 mvpp2_port_fc_adv_enable(port);
7649
7650 mvpp2_port_reset(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007651
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007652 port->pcpu = alloc_percpu(struct mvpp2_port_pcpu);
7653 if (!port->pcpu) {
7654 err = -ENOMEM;
7655 goto err_free_txq_pcpu;
7656 }
7657
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007658 if (!port->has_tx_irqs) {
7659 for_each_present_cpu(cpu) {
7660 port_pcpu = per_cpu_ptr(port->pcpu, cpu);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007661
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007662 hrtimer_init(&port_pcpu->tx_done_timer, CLOCK_MONOTONIC,
7663 HRTIMER_MODE_REL_PINNED);
7664 port_pcpu->tx_done_timer.function = mvpp2_hr_timer_cb;
7665 port_pcpu->timer_scheduled = false;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007666
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007667 tasklet_init(&port_pcpu->tx_done_tasklet,
7668 mvpp2_tx_proc_cb,
7669 (unsigned long)dev);
7670 }
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007671 }
7672
Antoine Ténart186cd4d2017-08-23 09:46:56 +02007673 features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007674 dev->features = features | NETIF_F_RXCSUM;
7675 dev->hw_features |= features | NETIF_F_RXCSUM | NETIF_F_GRO;
7676 dev->vlan_features |= features;
7677
Jarod Wilson57779872016-10-17 15:54:06 -04007678 /* MTU range: 68 - 9676 */
7679 dev->min_mtu = ETH_MIN_MTU;
7680 /* 9676 == 9700 - 20 and rounding to 8 */
7681 dev->max_mtu = 9676;
7682
Marcin Wojtas3f518502014-07-10 16:52:13 -03007683 err = register_netdev(dev);
7684 if (err < 0) {
7685 dev_err(&pdev->dev, "failed to register netdev\n");
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007686 goto err_free_port_pcpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007687 }
7688 netdev_info(dev, "Using %s mac address %pM\n", mac_from, dev->dev_addr);
7689
Yan Markman6bf69a12017-09-25 14:59:47 +02007690 priv->port_list[index] = port;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007691 return 0;
7692
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007693err_free_port_pcpu:
7694 free_percpu(port->pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007695err_free_txq_pcpu:
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007696 for (i = 0; i < port->ntxqs; i++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007697 free_percpu(port->txqs[i]->pcpu);
7698err_free_stats:
7699 free_percpu(port->stats);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007700err_free_irq:
7701 if (port->link_irq)
7702 irq_dispose_mapping(port->link_irq);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007703err_deinit_qvecs:
7704 mvpp2_queue_vectors_deinit(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007705err_free_netdev:
Peter Chenccb80392016-08-01 15:02:37 +08007706 of_node_put(phy_node);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007707 free_netdev(dev);
7708 return err;
7709}
7710
7711/* Ports removal routine */
7712static void mvpp2_port_remove(struct mvpp2_port *port)
7713{
7714 int i;
7715
7716 unregister_netdev(port->dev);
Peter Chenccb80392016-08-01 15:02:37 +08007717 of_node_put(port->phy_node);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007718 free_percpu(port->pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007719 free_percpu(port->stats);
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007720 for (i = 0; i < port->ntxqs; i++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007721 free_percpu(port->txqs[i]->pcpu);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007722 mvpp2_queue_vectors_deinit(port);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007723 if (port->link_irq)
7724 irq_dispose_mapping(port->link_irq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007725 free_netdev(port->dev);
7726}
7727
7728/* Initialize decoding windows */
7729static void mvpp2_conf_mbus_windows(const struct mbus_dram_target_info *dram,
7730 struct mvpp2 *priv)
7731{
7732 u32 win_enable;
7733 int i;
7734
7735 for (i = 0; i < 6; i++) {
7736 mvpp2_write(priv, MVPP2_WIN_BASE(i), 0);
7737 mvpp2_write(priv, MVPP2_WIN_SIZE(i), 0);
7738
7739 if (i < 4)
7740 mvpp2_write(priv, MVPP2_WIN_REMAP(i), 0);
7741 }
7742
7743 win_enable = 0;
7744
7745 for (i = 0; i < dram->num_cs; i++) {
7746 const struct mbus_dram_window *cs = dram->cs + i;
7747
7748 mvpp2_write(priv, MVPP2_WIN_BASE(i),
7749 (cs->base & 0xffff0000) | (cs->mbus_attr << 8) |
7750 dram->mbus_dram_target_id);
7751
7752 mvpp2_write(priv, MVPP2_WIN_SIZE(i),
7753 (cs->size - 1) & 0xffff0000);
7754
7755 win_enable |= (1 << i);
7756 }
7757
7758 mvpp2_write(priv, MVPP2_BASE_ADDR_ENABLE, win_enable);
7759}
7760
7761/* Initialize Rx FIFO's */
7762static void mvpp2_rx_fifo_init(struct mvpp2 *priv)
7763{
7764 int port;
7765
7766 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
7767 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
7768 MVPP2_RX_FIFO_PORT_DATA_SIZE);
7769 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
7770 MVPP2_RX_FIFO_PORT_ATTR_SIZE);
7771 }
7772
7773 mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
7774 MVPP2_RX_FIFO_PORT_MIN_PKT);
7775 mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
7776}
7777
Thomas Petazzoni6763ce32017-03-07 16:53:15 +01007778static void mvpp2_axi_init(struct mvpp2 *priv)
7779{
7780 u32 val, rdval, wrval;
7781
7782 mvpp2_write(priv, MVPP22_BM_ADDR_HIGH_RLS_REG, 0x0);
7783
7784 /* AXI Bridge Configuration */
7785
7786 rdval = MVPP22_AXI_CODE_CACHE_RD_CACHE
7787 << MVPP22_AXI_ATTR_CACHE_OFFS;
7788 rdval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
7789 << MVPP22_AXI_ATTR_DOMAIN_OFFS;
7790
7791 wrval = MVPP22_AXI_CODE_CACHE_WR_CACHE
7792 << MVPP22_AXI_ATTR_CACHE_OFFS;
7793 wrval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
7794 << MVPP22_AXI_ATTR_DOMAIN_OFFS;
7795
7796 /* BM */
7797 mvpp2_write(priv, MVPP22_AXI_BM_WR_ATTR_REG, wrval);
7798 mvpp2_write(priv, MVPP22_AXI_BM_RD_ATTR_REG, rdval);
7799
7800 /* Descriptors */
7801 mvpp2_write(priv, MVPP22_AXI_AGGRQ_DESCR_RD_ATTR_REG, rdval);
7802 mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_WR_ATTR_REG, wrval);
7803 mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_RD_ATTR_REG, rdval);
7804 mvpp2_write(priv, MVPP22_AXI_RXQ_DESCR_WR_ATTR_REG, wrval);
7805
7806 /* Buffer Data */
7807 mvpp2_write(priv, MVPP22_AXI_TX_DATA_RD_ATTR_REG, rdval);
7808 mvpp2_write(priv, MVPP22_AXI_RX_DATA_WR_ATTR_REG, wrval);
7809
7810 val = MVPP22_AXI_CODE_CACHE_NON_CACHE
7811 << MVPP22_AXI_CODE_CACHE_OFFS;
7812 val |= MVPP22_AXI_CODE_DOMAIN_SYSTEM
7813 << MVPP22_AXI_CODE_DOMAIN_OFFS;
7814 mvpp2_write(priv, MVPP22_AXI_RD_NORMAL_CODE_REG, val);
7815 mvpp2_write(priv, MVPP22_AXI_WR_NORMAL_CODE_REG, val);
7816
7817 val = MVPP22_AXI_CODE_CACHE_RD_CACHE
7818 << MVPP22_AXI_CODE_CACHE_OFFS;
7819 val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
7820 << MVPP22_AXI_CODE_DOMAIN_OFFS;
7821
7822 mvpp2_write(priv, MVPP22_AXI_RD_SNOOP_CODE_REG, val);
7823
7824 val = MVPP22_AXI_CODE_CACHE_WR_CACHE
7825 << MVPP22_AXI_CODE_CACHE_OFFS;
7826 val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
7827 << MVPP22_AXI_CODE_DOMAIN_OFFS;
7828
7829 mvpp2_write(priv, MVPP22_AXI_WR_SNOOP_CODE_REG, val);
7830}
7831
Marcin Wojtas3f518502014-07-10 16:52:13 -03007832/* Initialize network controller common part HW */
7833static int mvpp2_init(struct platform_device *pdev, struct mvpp2 *priv)
7834{
7835 const struct mbus_dram_target_info *dram_target_info;
7836 int err, i;
Marcin Wojtas08a23752014-07-21 13:48:12 -03007837 u32 val;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007838
Marcin Wojtas3f518502014-07-10 16:52:13 -03007839 /* MBUS windows configuration */
7840 dram_target_info = mv_mbus_dram_info();
7841 if (dram_target_info)
7842 mvpp2_conf_mbus_windows(dram_target_info, priv);
7843
Thomas Petazzoni6763ce32017-03-07 16:53:15 +01007844 if (priv->hw_version == MVPP22)
7845 mvpp2_axi_init(priv);
7846
Marcin Wojtas08a23752014-07-21 13:48:12 -03007847 /* Disable HW PHY polling */
Thomas Petazzoni26975822017-03-07 16:53:14 +01007848 if (priv->hw_version == MVPP21) {
7849 val = readl(priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
7850 val |= MVPP2_PHY_AN_STOP_SMI0_MASK;
7851 writel(val, priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
7852 } else {
7853 val = readl(priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
7854 val &= ~MVPP22_SMI_POLLING_EN;
7855 writel(val, priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
7856 }
Marcin Wojtas08a23752014-07-21 13:48:12 -03007857
Marcin Wojtas3f518502014-07-10 16:52:13 -03007858 /* Allocate and initialize aggregated TXQs */
7859 priv->aggr_txqs = devm_kcalloc(&pdev->dev, num_present_cpus(),
Markus Elfringd7ce3ce2017-04-17 08:48:23 +02007860 sizeof(*priv->aggr_txqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03007861 GFP_KERNEL);
7862 if (!priv->aggr_txqs)
7863 return -ENOMEM;
7864
7865 for_each_present_cpu(i) {
7866 priv->aggr_txqs[i].id = i;
7867 priv->aggr_txqs[i].size = MVPP2_AGGR_TXQ_SIZE;
Antoine Ténart85affd72017-08-23 09:46:55 +02007868 err = mvpp2_aggr_txq_init(pdev, &priv->aggr_txqs[i], i, priv);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007869 if (err < 0)
7870 return err;
7871 }
7872
7873 /* Rx Fifo Init */
7874 mvpp2_rx_fifo_init(priv);
7875
Thomas Petazzoni26975822017-03-07 16:53:14 +01007876 if (priv->hw_version == MVPP21)
7877 writel(MVPP2_EXT_GLOBAL_CTRL_DEFAULT,
7878 priv->lms_base + MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007879
7880 /* Allow cache snoop when transmiting packets */
7881 mvpp2_write(priv, MVPP2_TX_SNOOP_REG, 0x1);
7882
7883 /* Buffer Manager initialization */
7884 err = mvpp2_bm_init(pdev, priv);
7885 if (err < 0)
7886 return err;
7887
7888 /* Parser default initialization */
7889 err = mvpp2_prs_default_init(pdev, priv);
7890 if (err < 0)
7891 return err;
7892
7893 /* Classifier default initialization */
7894 mvpp2_cls_init(priv);
7895
7896 return 0;
7897}
7898
7899static int mvpp2_probe(struct platform_device *pdev)
7900{
7901 struct device_node *dn = pdev->dev.of_node;
7902 struct device_node *port_node;
7903 struct mvpp2 *priv;
7904 struct resource *res;
Thomas Petazzonia7868412017-03-07 16:53:13 +01007905 void __iomem *base;
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02007906 int port_count, i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007907 int err;
7908
Markus Elfring0b92e592017-04-17 08:38:32 +02007909 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007910 if (!priv)
7911 return -ENOMEM;
7912
Thomas Petazzonifaca9242017-03-07 16:53:06 +01007913 priv->hw_version =
7914 (unsigned long)of_device_get_match_data(&pdev->dev);
7915
Marcin Wojtas3f518502014-07-10 16:52:13 -03007916 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thomas Petazzonia7868412017-03-07 16:53:13 +01007917 base = devm_ioremap_resource(&pdev->dev, res);
7918 if (IS_ERR(base))
7919 return PTR_ERR(base);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007920
Thomas Petazzonia7868412017-03-07 16:53:13 +01007921 if (priv->hw_version == MVPP21) {
7922 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
7923 priv->lms_base = devm_ioremap_resource(&pdev->dev, res);
7924 if (IS_ERR(priv->lms_base))
7925 return PTR_ERR(priv->lms_base);
7926 } else {
7927 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
7928 priv->iface_base = devm_ioremap_resource(&pdev->dev, res);
7929 if (IS_ERR(priv->iface_base))
7930 return PTR_ERR(priv->iface_base);
Antoine Ténartf84bf382017-08-22 19:08:27 +02007931
7932 priv->sysctrl_base =
7933 syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
7934 "marvell,system-controller");
7935 if (IS_ERR(priv->sysctrl_base))
7936 /* The system controller regmap is optional for dt
7937 * compatibility reasons. When not provided, the
7938 * configuration of the GoP relies on the
7939 * firmware/bootloader.
7940 */
7941 priv->sysctrl_base = NULL;
Thomas Petazzonia7868412017-03-07 16:53:13 +01007942 }
7943
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02007944 for (i = 0; i < MVPP2_MAX_THREADS; i++) {
Thomas Petazzonia7868412017-03-07 16:53:13 +01007945 u32 addr_space_sz;
7946
7947 addr_space_sz = (priv->hw_version == MVPP21 ?
7948 MVPP21_ADDR_SPACE_SZ : MVPP22_ADDR_SPACE_SZ);
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02007949 priv->swth_base[i] = base + i * addr_space_sz;
Thomas Petazzonia7868412017-03-07 16:53:13 +01007950 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03007951
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01007952 if (priv->hw_version == MVPP21)
7953 priv->max_port_rxqs = 8;
7954 else
7955 priv->max_port_rxqs = 32;
7956
Marcin Wojtas3f518502014-07-10 16:52:13 -03007957 priv->pp_clk = devm_clk_get(&pdev->dev, "pp_clk");
7958 if (IS_ERR(priv->pp_clk))
7959 return PTR_ERR(priv->pp_clk);
7960 err = clk_prepare_enable(priv->pp_clk);
7961 if (err < 0)
7962 return err;
7963
7964 priv->gop_clk = devm_clk_get(&pdev->dev, "gop_clk");
7965 if (IS_ERR(priv->gop_clk)) {
7966 err = PTR_ERR(priv->gop_clk);
7967 goto err_pp_clk;
7968 }
7969 err = clk_prepare_enable(priv->gop_clk);
7970 if (err < 0)
7971 goto err_pp_clk;
7972
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01007973 if (priv->hw_version == MVPP22) {
7974 priv->mg_clk = devm_clk_get(&pdev->dev, "mg_clk");
7975 if (IS_ERR(priv->mg_clk)) {
7976 err = PTR_ERR(priv->mg_clk);
7977 goto err_gop_clk;
7978 }
7979
7980 err = clk_prepare_enable(priv->mg_clk);
7981 if (err < 0)
7982 goto err_gop_clk;
Gregory CLEMENT4792ea02017-09-29 14:27:39 +02007983
7984 priv->axi_clk = devm_clk_get(&pdev->dev, "axi_clk");
7985 if (IS_ERR(priv->axi_clk)) {
7986 err = PTR_ERR(priv->axi_clk);
7987 if (err == -EPROBE_DEFER)
7988 goto err_gop_clk;
7989 priv->axi_clk = NULL;
7990 } else {
7991 err = clk_prepare_enable(priv->axi_clk);
7992 if (err < 0)
7993 goto err_gop_clk;
7994 }
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01007995 }
7996
Marcin Wojtas3f518502014-07-10 16:52:13 -03007997 /* Get system's tclk rate */
7998 priv->tclk = clk_get_rate(priv->pp_clk);
7999
Thomas Petazzoni2067e0a2017-03-07 16:53:19 +01008000 if (priv->hw_version == MVPP22) {
8001 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(40));
8002 if (err)
8003 goto err_mg_clk;
8004 /* Sadly, the BM pools all share the same register to
8005 * store the high 32 bits of their address. So they
8006 * must all have the same high 32 bits, which forces
8007 * us to restrict coherent memory to DMA_BIT_MASK(32).
8008 */
8009 err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
8010 if (err)
8011 goto err_mg_clk;
8012 }
8013
Marcin Wojtas3f518502014-07-10 16:52:13 -03008014 /* Initialize network controller */
8015 err = mvpp2_init(pdev, priv);
8016 if (err < 0) {
8017 dev_err(&pdev->dev, "failed to initialize controller\n");
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008018 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008019 }
8020
8021 port_count = of_get_available_child_count(dn);
8022 if (port_count == 0) {
8023 dev_err(&pdev->dev, "no ports enabled\n");
Wei Yongjun575a1932014-07-20 22:02:43 +08008024 err = -ENODEV;
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008025 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008026 }
8027
8028 priv->port_list = devm_kcalloc(&pdev->dev, port_count,
Markus Elfring0b92e592017-04-17 08:38:32 +02008029 sizeof(*priv->port_list),
8030 GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008031 if (!priv->port_list) {
8032 err = -ENOMEM;
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008033 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008034 }
8035
8036 /* Initialize ports */
Yan Markman6bf69a12017-09-25 14:59:47 +02008037 i = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008038 for_each_available_child_of_node(dn, port_node) {
Yan Markman6bf69a12017-09-25 14:59:47 +02008039 err = mvpp2_port_probe(pdev, port_node, priv, i);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008040 if (err < 0)
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008041 goto err_mg_clk;
Yan Markman6bf69a12017-09-25 14:59:47 +02008042 i++;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008043 }
8044
8045 platform_set_drvdata(pdev, priv);
8046 return 0;
8047
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008048err_mg_clk:
Gregory CLEMENT4792ea02017-09-29 14:27:39 +02008049 clk_disable_unprepare(priv->axi_clk);
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008050 if (priv->hw_version == MVPP22)
8051 clk_disable_unprepare(priv->mg_clk);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008052err_gop_clk:
8053 clk_disable_unprepare(priv->gop_clk);
8054err_pp_clk:
8055 clk_disable_unprepare(priv->pp_clk);
8056 return err;
8057}
8058
8059static int mvpp2_remove(struct platform_device *pdev)
8060{
8061 struct mvpp2 *priv = platform_get_drvdata(pdev);
8062 struct device_node *dn = pdev->dev.of_node;
8063 struct device_node *port_node;
8064 int i = 0;
8065
8066 for_each_available_child_of_node(dn, port_node) {
8067 if (priv->port_list[i])
8068 mvpp2_port_remove(priv->port_list[i]);
8069 i++;
8070 }
8071
8072 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
8073 struct mvpp2_bm_pool *bm_pool = &priv->bm_pools[i];
8074
8075 mvpp2_bm_pool_destroy(pdev, priv, bm_pool);
8076 }
8077
8078 for_each_present_cpu(i) {
8079 struct mvpp2_tx_queue *aggr_txq = &priv->aggr_txqs[i];
8080
8081 dma_free_coherent(&pdev->dev,
8082 MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
8083 aggr_txq->descs,
Thomas Petazzoni20396132017-03-07 16:53:00 +01008084 aggr_txq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008085 }
8086
Gregory CLEMENT4792ea02017-09-29 14:27:39 +02008087 clk_disable_unprepare(priv->axi_clk);
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008088 clk_disable_unprepare(priv->mg_clk);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008089 clk_disable_unprepare(priv->pp_clk);
8090 clk_disable_unprepare(priv->gop_clk);
8091
8092 return 0;
8093}
8094
8095static const struct of_device_id mvpp2_match[] = {
Thomas Petazzonifaca9242017-03-07 16:53:06 +01008096 {
8097 .compatible = "marvell,armada-375-pp2",
8098 .data = (void *)MVPP21,
8099 },
Thomas Petazzonifc5e1552017-03-07 16:53:20 +01008100 {
8101 .compatible = "marvell,armada-7k-pp22",
8102 .data = (void *)MVPP22,
8103 },
Marcin Wojtas3f518502014-07-10 16:52:13 -03008104 { }
8105};
8106MODULE_DEVICE_TABLE(of, mvpp2_match);
8107
8108static struct platform_driver mvpp2_driver = {
8109 .probe = mvpp2_probe,
8110 .remove = mvpp2_remove,
8111 .driver = {
8112 .name = MVPP2_DRIVER_NAME,
8113 .of_match_table = mvpp2_match,
8114 },
8115};
8116
8117module_platform_driver(mvpp2_driver);
8118
8119MODULE_DESCRIPTION("Marvell PPv2 Ethernet Driver - www.marvell.com");
8120MODULE_AUTHOR("Marcin Wojtas <mw@semihalf.com>");
Ezequiel Garciac6340992014-07-14 10:34:47 -03008121MODULE_LICENSE("GPL v2");