blob: fea9ae5b70bab35bafb761f1689895b9fe566d9a [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>
31#include <linux/clk.h>
Marcin Wojtasedc660f2015-08-06 19:00:30 +020032#include <linux/hrtimer.h>
33#include <linux/ktime.h>
Antoine Ténartf84bf382017-08-22 19:08:27 +020034#include <linux/regmap.h>
Marcin Wojtas3f518502014-07-10 16:52:13 -030035#include <uapi/linux/ppp_defs.h>
36#include <net/ip.h>
37#include <net/ipv6.h>
Antoine Ténart186cd4d2017-08-23 09:46:56 +020038#include <net/tso.h>
Marcin Wojtas3f518502014-07-10 16:52:13 -030039
40/* RX Fifo Registers */
41#define MVPP2_RX_DATA_FIFO_SIZE_REG(port) (0x00 + 4 * (port))
42#define MVPP2_RX_ATTR_FIFO_SIZE_REG(port) (0x20 + 4 * (port))
43#define MVPP2_RX_MIN_PKT_SIZE_REG 0x60
44#define MVPP2_RX_FIFO_INIT_REG 0x64
45
46/* RX DMA Top Registers */
47#define MVPP2_RX_CTRL_REG(port) (0x140 + 4 * (port))
48#define MVPP2_RX_LOW_LATENCY_PKT_SIZE(s) (((s) & 0xfff) << 16)
49#define MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK BIT(31)
50#define MVPP2_POOL_BUF_SIZE_REG(pool) (0x180 + 4 * (pool))
51#define MVPP2_POOL_BUF_SIZE_OFFSET 5
52#define MVPP2_RXQ_CONFIG_REG(rxq) (0x800 + 4 * (rxq))
53#define MVPP2_SNOOP_PKT_SIZE_MASK 0x1ff
54#define MVPP2_SNOOP_BUF_HDR_MASK BIT(9)
55#define MVPP2_RXQ_POOL_SHORT_OFFS 20
Thomas Petazzoni5eac8922017-03-07 16:53:10 +010056#define MVPP21_RXQ_POOL_SHORT_MASK 0x700000
57#define MVPP22_RXQ_POOL_SHORT_MASK 0xf00000
Marcin Wojtas3f518502014-07-10 16:52:13 -030058#define MVPP2_RXQ_POOL_LONG_OFFS 24
Thomas Petazzoni5eac8922017-03-07 16:53:10 +010059#define MVPP21_RXQ_POOL_LONG_MASK 0x7000000
60#define MVPP22_RXQ_POOL_LONG_MASK 0xf000000
Marcin Wojtas3f518502014-07-10 16:52:13 -030061#define MVPP2_RXQ_PACKET_OFFSET_OFFS 28
62#define MVPP2_RXQ_PACKET_OFFSET_MASK 0x70000000
63#define MVPP2_RXQ_DISABLE_MASK BIT(31)
64
65/* Parser Registers */
66#define MVPP2_PRS_INIT_LOOKUP_REG 0x1000
67#define MVPP2_PRS_PORT_LU_MAX 0xf
68#define MVPP2_PRS_PORT_LU_MASK(port) (0xff << ((port) * 4))
69#define MVPP2_PRS_PORT_LU_VAL(port, val) ((val) << ((port) * 4))
70#define MVPP2_PRS_INIT_OFFS_REG(port) (0x1004 + ((port) & 4))
71#define MVPP2_PRS_INIT_OFF_MASK(port) (0x3f << (((port) % 4) * 8))
72#define MVPP2_PRS_INIT_OFF_VAL(port, val) ((val) << (((port) % 4) * 8))
73#define MVPP2_PRS_MAX_LOOP_REG(port) (0x100c + ((port) & 4))
74#define MVPP2_PRS_MAX_LOOP_MASK(port) (0xff << (((port) % 4) * 8))
75#define MVPP2_PRS_MAX_LOOP_VAL(port, val) ((val) << (((port) % 4) * 8))
76#define MVPP2_PRS_TCAM_IDX_REG 0x1100
77#define MVPP2_PRS_TCAM_DATA_REG(idx) (0x1104 + (idx) * 4)
78#define MVPP2_PRS_TCAM_INV_MASK BIT(31)
79#define MVPP2_PRS_SRAM_IDX_REG 0x1200
80#define MVPP2_PRS_SRAM_DATA_REG(idx) (0x1204 + (idx) * 4)
81#define MVPP2_PRS_TCAM_CTRL_REG 0x1230
82#define MVPP2_PRS_TCAM_EN_MASK BIT(0)
83
84/* Classifier Registers */
85#define MVPP2_CLS_MODE_REG 0x1800
86#define MVPP2_CLS_MODE_ACTIVE_MASK BIT(0)
87#define MVPP2_CLS_PORT_WAY_REG 0x1810
88#define MVPP2_CLS_PORT_WAY_MASK(port) (1 << (port))
89#define MVPP2_CLS_LKP_INDEX_REG 0x1814
90#define MVPP2_CLS_LKP_INDEX_WAY_OFFS 6
91#define MVPP2_CLS_LKP_TBL_REG 0x1818
92#define MVPP2_CLS_LKP_TBL_RXQ_MASK 0xff
93#define MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK BIT(25)
94#define MVPP2_CLS_FLOW_INDEX_REG 0x1820
95#define MVPP2_CLS_FLOW_TBL0_REG 0x1824
96#define MVPP2_CLS_FLOW_TBL1_REG 0x1828
97#define MVPP2_CLS_FLOW_TBL2_REG 0x182c
98#define MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port) (0x1980 + ((port) * 4))
99#define MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS 3
100#define MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK 0x7
101#define MVPP2_CLS_SWFWD_P2HQ_REG(port) (0x19b0 + ((port) * 4))
102#define MVPP2_CLS_SWFWD_PCTRL_REG 0x19d0
103#define MVPP2_CLS_SWFWD_PCTRL_MASK(port) (1 << (port))
104
105/* Descriptor Manager Top Registers */
106#define MVPP2_RXQ_NUM_REG 0x2040
107#define MVPP2_RXQ_DESC_ADDR_REG 0x2044
Thomas Petazzonib02f31f2017-03-07 16:53:12 +0100108#define MVPP22_DESC_ADDR_OFFS 8
Marcin Wojtas3f518502014-07-10 16:52:13 -0300109#define MVPP2_RXQ_DESC_SIZE_REG 0x2048
110#define MVPP2_RXQ_DESC_SIZE_MASK 0x3ff0
111#define MVPP2_RXQ_STATUS_UPDATE_REG(rxq) (0x3000 + 4 * (rxq))
112#define MVPP2_RXQ_NUM_PROCESSED_OFFSET 0
113#define MVPP2_RXQ_NUM_NEW_OFFSET 16
114#define MVPP2_RXQ_STATUS_REG(rxq) (0x3400 + 4 * (rxq))
115#define MVPP2_RXQ_OCCUPIED_MASK 0x3fff
116#define MVPP2_RXQ_NON_OCCUPIED_OFFSET 16
117#define MVPP2_RXQ_NON_OCCUPIED_MASK 0x3fff0000
118#define MVPP2_RXQ_THRESH_REG 0x204c
119#define MVPP2_OCCUPIED_THRESH_OFFSET 0
120#define MVPP2_OCCUPIED_THRESH_MASK 0x3fff
121#define MVPP2_RXQ_INDEX_REG 0x2050
122#define MVPP2_TXQ_NUM_REG 0x2080
123#define MVPP2_TXQ_DESC_ADDR_REG 0x2084
124#define MVPP2_TXQ_DESC_SIZE_REG 0x2088
125#define MVPP2_TXQ_DESC_SIZE_MASK 0x3ff0
Thomas Petazzoni213f4282017-08-03 10:42:00 +0200126#define MVPP2_TXQ_THRESH_REG 0x2094
127#define MVPP2_TXQ_THRESH_OFFSET 16
128#define MVPP2_TXQ_THRESH_MASK 0x3fff
Marcin Wojtas3f518502014-07-10 16:52:13 -0300129#define MVPP2_AGGR_TXQ_UPDATE_REG 0x2090
Marcin Wojtas3f518502014-07-10 16:52:13 -0300130#define MVPP2_TXQ_INDEX_REG 0x2098
131#define MVPP2_TXQ_PREF_BUF_REG 0x209c
132#define MVPP2_PREF_BUF_PTR(desc) ((desc) & 0xfff)
133#define MVPP2_PREF_BUF_SIZE_4 (BIT(12) | BIT(13))
134#define MVPP2_PREF_BUF_SIZE_16 (BIT(12) | BIT(14))
135#define MVPP2_PREF_BUF_THRESH(val) ((val) << 17)
136#define MVPP2_TXQ_DRAIN_EN_MASK BIT(31)
137#define MVPP2_TXQ_PENDING_REG 0x20a0
138#define MVPP2_TXQ_PENDING_MASK 0x3fff
139#define MVPP2_TXQ_INT_STATUS_REG 0x20a4
140#define MVPP2_TXQ_SENT_REG(txq) (0x3c00 + 4 * (txq))
141#define MVPP2_TRANSMITTED_COUNT_OFFSET 16
142#define MVPP2_TRANSMITTED_COUNT_MASK 0x3fff0000
143#define MVPP2_TXQ_RSVD_REQ_REG 0x20b0
144#define MVPP2_TXQ_RSVD_REQ_Q_OFFSET 16
145#define MVPP2_TXQ_RSVD_RSLT_REG 0x20b4
146#define MVPP2_TXQ_RSVD_RSLT_MASK 0x3fff
147#define MVPP2_TXQ_RSVD_CLR_REG 0x20b8
148#define MVPP2_TXQ_RSVD_CLR_OFFSET 16
149#define MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu) (0x2100 + 4 * (cpu))
Thomas Petazzonib02f31f2017-03-07 16:53:12 +0100150#define MVPP22_AGGR_TXQ_DESC_ADDR_OFFS 8
Marcin Wojtas3f518502014-07-10 16:52:13 -0300151#define MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu) (0x2140 + 4 * (cpu))
152#define MVPP2_AGGR_TXQ_DESC_SIZE_MASK 0x3ff0
153#define MVPP2_AGGR_TXQ_STATUS_REG(cpu) (0x2180 + 4 * (cpu))
154#define MVPP2_AGGR_TXQ_PENDING_MASK 0x3fff
155#define MVPP2_AGGR_TXQ_INDEX_REG(cpu) (0x21c0 + 4 * (cpu))
156
157/* MBUS bridge registers */
158#define MVPP2_WIN_BASE(w) (0x4000 + ((w) << 2))
159#define MVPP2_WIN_SIZE(w) (0x4020 + ((w) << 2))
160#define MVPP2_WIN_REMAP(w) (0x4040 + ((w) << 2))
161#define MVPP2_BASE_ADDR_ENABLE 0x4060
162
Thomas Petazzoni6763ce32017-03-07 16:53:15 +0100163/* AXI Bridge Registers */
164#define MVPP22_AXI_BM_WR_ATTR_REG 0x4100
165#define MVPP22_AXI_BM_RD_ATTR_REG 0x4104
166#define MVPP22_AXI_AGGRQ_DESCR_RD_ATTR_REG 0x4110
167#define MVPP22_AXI_TXQ_DESCR_WR_ATTR_REG 0x4114
168#define MVPP22_AXI_TXQ_DESCR_RD_ATTR_REG 0x4118
169#define MVPP22_AXI_RXQ_DESCR_WR_ATTR_REG 0x411c
170#define MVPP22_AXI_RX_DATA_WR_ATTR_REG 0x4120
171#define MVPP22_AXI_TX_DATA_RD_ATTR_REG 0x4130
172#define MVPP22_AXI_RD_NORMAL_CODE_REG 0x4150
173#define MVPP22_AXI_RD_SNOOP_CODE_REG 0x4154
174#define MVPP22_AXI_WR_NORMAL_CODE_REG 0x4160
175#define MVPP22_AXI_WR_SNOOP_CODE_REG 0x4164
176
177/* Values for AXI Bridge registers */
178#define MVPP22_AXI_ATTR_CACHE_OFFS 0
179#define MVPP22_AXI_ATTR_DOMAIN_OFFS 12
180
181#define MVPP22_AXI_CODE_CACHE_OFFS 0
182#define MVPP22_AXI_CODE_DOMAIN_OFFS 4
183
184#define MVPP22_AXI_CODE_CACHE_NON_CACHE 0x3
185#define MVPP22_AXI_CODE_CACHE_WR_CACHE 0x7
186#define MVPP22_AXI_CODE_CACHE_RD_CACHE 0xb
187
188#define MVPP22_AXI_CODE_DOMAIN_OUTER_DOM 2
189#define MVPP22_AXI_CODE_DOMAIN_SYSTEM 3
190
Marcin Wojtas3f518502014-07-10 16:52:13 -0300191/* Interrupt Cause and Mask registers */
Thomas Petazzoni213f4282017-08-03 10:42:00 +0200192#define MVPP2_ISR_TX_THRESHOLD_REG(port) (0x5140 + 4 * (port))
193#define MVPP2_MAX_ISR_TX_THRESHOLD 0xfffff0
194
Marcin Wojtas3f518502014-07-10 16:52:13 -0300195#define MVPP2_ISR_RX_THRESHOLD_REG(rxq) (0x5200 + 4 * (rxq))
Thomas Petazzoniab426762017-02-21 11:28:04 +0100196#define MVPP2_MAX_ISR_RX_THRESHOLD 0xfffff0
Thomas Petazzonieb1e93a2017-08-03 10:41:55 +0200197#define MVPP21_ISR_RXQ_GROUP_REG(port) (0x5400 + 4 * (port))
Thomas Petazzonia73fef12017-03-07 16:53:16 +0100198
Antoine Ténart81b66302017-08-22 19:08:21 +0200199#define MVPP22_ISR_RXQ_GROUP_INDEX_REG 0x5400
Thomas Petazzonia73fef12017-03-07 16:53:16 +0100200#define MVPP22_ISR_RXQ_GROUP_INDEX_SUBGROUP_MASK 0xf
Antoine Ténart81b66302017-08-22 19:08:21 +0200201#define MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_MASK 0x380
202#define MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_OFFSET 7
Thomas Petazzonia73fef12017-03-07 16:53:16 +0100203
204#define MVPP22_ISR_RXQ_GROUP_INDEX_SUBGROUP_MASK 0xf
Antoine Ténart81b66302017-08-22 19:08:21 +0200205#define MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_MASK 0x380
Thomas Petazzonia73fef12017-03-07 16:53:16 +0100206
Antoine Ténart81b66302017-08-22 19:08:21 +0200207#define MVPP22_ISR_RXQ_SUB_GROUP_CONFIG_REG 0x5404
208#define MVPP22_ISR_RXQ_SUB_GROUP_STARTQ_MASK 0x1f
209#define MVPP22_ISR_RXQ_SUB_GROUP_SIZE_MASK 0xf00
210#define MVPP22_ISR_RXQ_SUB_GROUP_SIZE_OFFSET 8
Thomas Petazzonia73fef12017-03-07 16:53:16 +0100211
Marcin Wojtas3f518502014-07-10 16:52:13 -0300212#define MVPP2_ISR_ENABLE_REG(port) (0x5420 + 4 * (port))
213#define MVPP2_ISR_ENABLE_INTERRUPT(mask) ((mask) & 0xffff)
214#define MVPP2_ISR_DISABLE_INTERRUPT(mask) (((mask) << 16) & 0xffff0000)
215#define MVPP2_ISR_RX_TX_CAUSE_REG(port) (0x5480 + 4 * (port))
216#define MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK 0xffff
217#define MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK 0xff0000
Thomas Petazzoni213f4282017-08-03 10:42:00 +0200218#define MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_OFFSET 16
Marcin Wojtas3f518502014-07-10 16:52:13 -0300219#define MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK BIT(24)
220#define MVPP2_CAUSE_FCS_ERR_MASK BIT(25)
221#define MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK BIT(26)
222#define MVPP2_CAUSE_TX_EXCEPTION_SUM_MASK BIT(29)
223#define MVPP2_CAUSE_RX_EXCEPTION_SUM_MASK BIT(30)
224#define MVPP2_CAUSE_MISC_SUM_MASK BIT(31)
225#define MVPP2_ISR_RX_TX_MASK_REG(port) (0x54a0 + 4 * (port))
226#define MVPP2_ISR_PON_RX_TX_MASK_REG 0x54bc
227#define MVPP2_PON_CAUSE_RXQ_OCCUP_DESC_ALL_MASK 0xffff
228#define MVPP2_PON_CAUSE_TXP_OCCUP_DESC_ALL_MASK 0x3fc00000
229#define MVPP2_PON_CAUSE_MISC_SUM_MASK BIT(31)
230#define MVPP2_ISR_MISC_CAUSE_REG 0x55b0
231
232/* Buffer Manager registers */
233#define MVPP2_BM_POOL_BASE_REG(pool) (0x6000 + ((pool) * 4))
234#define MVPP2_BM_POOL_BASE_ADDR_MASK 0xfffff80
235#define MVPP2_BM_POOL_SIZE_REG(pool) (0x6040 + ((pool) * 4))
236#define MVPP2_BM_POOL_SIZE_MASK 0xfff0
237#define MVPP2_BM_POOL_READ_PTR_REG(pool) (0x6080 + ((pool) * 4))
238#define MVPP2_BM_POOL_GET_READ_PTR_MASK 0xfff0
239#define MVPP2_BM_POOL_PTRS_NUM_REG(pool) (0x60c0 + ((pool) * 4))
240#define MVPP2_BM_POOL_PTRS_NUM_MASK 0xfff0
241#define MVPP2_BM_BPPI_READ_PTR_REG(pool) (0x6100 + ((pool) * 4))
242#define MVPP2_BM_BPPI_PTRS_NUM_REG(pool) (0x6140 + ((pool) * 4))
243#define MVPP2_BM_BPPI_PTR_NUM_MASK 0x7ff
244#define MVPP2_BM_BPPI_PREFETCH_FULL_MASK BIT(16)
245#define MVPP2_BM_POOL_CTRL_REG(pool) (0x6200 + ((pool) * 4))
246#define MVPP2_BM_START_MASK BIT(0)
247#define MVPP2_BM_STOP_MASK BIT(1)
248#define MVPP2_BM_STATE_MASK BIT(4)
249#define MVPP2_BM_LOW_THRESH_OFFS 8
250#define MVPP2_BM_LOW_THRESH_MASK 0x7f00
251#define MVPP2_BM_LOW_THRESH_VALUE(val) ((val) << \
252 MVPP2_BM_LOW_THRESH_OFFS)
253#define MVPP2_BM_HIGH_THRESH_OFFS 16
254#define MVPP2_BM_HIGH_THRESH_MASK 0x7f0000
255#define MVPP2_BM_HIGH_THRESH_VALUE(val) ((val) << \
256 MVPP2_BM_HIGH_THRESH_OFFS)
257#define MVPP2_BM_INTR_CAUSE_REG(pool) (0x6240 + ((pool) * 4))
258#define MVPP2_BM_RELEASED_DELAY_MASK BIT(0)
259#define MVPP2_BM_ALLOC_FAILED_MASK BIT(1)
260#define MVPP2_BM_BPPE_EMPTY_MASK BIT(2)
261#define MVPP2_BM_BPPE_FULL_MASK BIT(3)
262#define MVPP2_BM_AVAILABLE_BP_LOW_MASK BIT(4)
263#define MVPP2_BM_INTR_MASK_REG(pool) (0x6280 + ((pool) * 4))
264#define MVPP2_BM_PHY_ALLOC_REG(pool) (0x6400 + ((pool) * 4))
265#define MVPP2_BM_PHY_ALLOC_GRNTD_MASK BIT(0)
266#define MVPP2_BM_VIRT_ALLOC_REG 0x6440
Thomas Petazzonid01524d2017-03-07 16:53:09 +0100267#define MVPP22_BM_ADDR_HIGH_ALLOC 0x6444
268#define MVPP22_BM_ADDR_HIGH_PHYS_MASK 0xff
269#define MVPP22_BM_ADDR_HIGH_VIRT_MASK 0xff00
270#define MVPP22_BM_ADDR_HIGH_VIRT_SHIFT 8
Marcin Wojtas3f518502014-07-10 16:52:13 -0300271#define MVPP2_BM_PHY_RLS_REG(pool) (0x6480 + ((pool) * 4))
272#define MVPP2_BM_PHY_RLS_MC_BUFF_MASK BIT(0)
273#define MVPP2_BM_PHY_RLS_PRIO_EN_MASK BIT(1)
274#define MVPP2_BM_PHY_RLS_GRNTD_MASK BIT(2)
275#define MVPP2_BM_VIRT_RLS_REG 0x64c0
Thomas Petazzonid01524d2017-03-07 16:53:09 +0100276#define MVPP22_BM_ADDR_HIGH_RLS_REG 0x64c4
277#define MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK 0xff
Antoine Ténart81b66302017-08-22 19:08:21 +0200278#define MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK 0xff00
Thomas Petazzonid01524d2017-03-07 16:53:09 +0100279#define MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT 8
Marcin Wojtas3f518502014-07-10 16:52:13 -0300280
281/* TX Scheduler registers */
282#define MVPP2_TXP_SCHED_PORT_INDEX_REG 0x8000
283#define MVPP2_TXP_SCHED_Q_CMD_REG 0x8004
284#define MVPP2_TXP_SCHED_ENQ_MASK 0xff
285#define MVPP2_TXP_SCHED_DISQ_OFFSET 8
286#define MVPP2_TXP_SCHED_CMD_1_REG 0x8010
287#define MVPP2_TXP_SCHED_PERIOD_REG 0x8018
288#define MVPP2_TXP_SCHED_MTU_REG 0x801c
289#define MVPP2_TXP_MTU_MAX 0x7FFFF
290#define MVPP2_TXP_SCHED_REFILL_REG 0x8020
291#define MVPP2_TXP_REFILL_TOKENS_ALL_MASK 0x7ffff
292#define MVPP2_TXP_REFILL_PERIOD_ALL_MASK 0x3ff00000
293#define MVPP2_TXP_REFILL_PERIOD_MASK(v) ((v) << 20)
294#define MVPP2_TXP_SCHED_TOKEN_SIZE_REG 0x8024
295#define MVPP2_TXP_TOKEN_SIZE_MAX 0xffffffff
296#define MVPP2_TXQ_SCHED_REFILL_REG(q) (0x8040 + ((q) << 2))
297#define MVPP2_TXQ_REFILL_TOKENS_ALL_MASK 0x7ffff
298#define MVPP2_TXQ_REFILL_PERIOD_ALL_MASK 0x3ff00000
299#define MVPP2_TXQ_REFILL_PERIOD_MASK(v) ((v) << 20)
300#define MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(q) (0x8060 + ((q) << 2))
301#define MVPP2_TXQ_TOKEN_SIZE_MAX 0x7fffffff
302#define MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(q) (0x8080 + ((q) << 2))
303#define MVPP2_TXQ_TOKEN_CNTR_MAX 0xffffffff
304
305/* TX general registers */
306#define MVPP2_TX_SNOOP_REG 0x8800
307#define MVPP2_TX_PORT_FLUSH_REG 0x8810
308#define MVPP2_TX_PORT_FLUSH_MASK(port) (1 << (port))
309
310/* LMS registers */
311#define MVPP2_SRC_ADDR_MIDDLE 0x24
312#define MVPP2_SRC_ADDR_HIGH 0x28
Marcin Wojtas08a23752014-07-21 13:48:12 -0300313#define MVPP2_PHY_AN_CFG0_REG 0x34
314#define MVPP2_PHY_AN_STOP_SMI0_MASK BIT(7)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300315#define MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG 0x305c
Thomas Petazzoni31d76772017-02-21 11:28:10 +0100316#define MVPP2_EXT_GLOBAL_CTRL_DEFAULT 0x27
Marcin Wojtas3f518502014-07-10 16:52:13 -0300317
318/* Per-port registers */
319#define MVPP2_GMAC_CTRL_0_REG 0x0
Antoine Ténart81b66302017-08-22 19:08:21 +0200320#define MVPP2_GMAC_PORT_EN_MASK BIT(0)
Antoine Ténart39193572017-08-22 19:08:24 +0200321#define MVPP2_GMAC_PORT_TYPE_MASK BIT(1)
Antoine Ténart81b66302017-08-22 19:08:21 +0200322#define MVPP2_GMAC_MAX_RX_SIZE_OFFS 2
323#define MVPP2_GMAC_MAX_RX_SIZE_MASK 0x7ffc
324#define MVPP2_GMAC_MIB_CNTR_EN_MASK BIT(15)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300325#define MVPP2_GMAC_CTRL_1_REG 0x4
Antoine Ténart81b66302017-08-22 19:08:21 +0200326#define MVPP2_GMAC_PERIODIC_XON_EN_MASK BIT(1)
327#define MVPP2_GMAC_GMII_LB_EN_MASK BIT(5)
328#define MVPP2_GMAC_PCS_LB_EN_BIT 6
329#define MVPP2_GMAC_PCS_LB_EN_MASK BIT(6)
330#define MVPP2_GMAC_SA_LOW_OFFS 7
Marcin Wojtas3f518502014-07-10 16:52:13 -0300331#define MVPP2_GMAC_CTRL_2_REG 0x8
Antoine Ténart81b66302017-08-22 19:08:21 +0200332#define MVPP2_GMAC_INBAND_AN_MASK BIT(0)
Antoine Ténart39193572017-08-22 19:08:24 +0200333#define MVPP2_GMAC_FLOW_CTRL_MASK GENMASK(2, 1)
Antoine Ténart81b66302017-08-22 19:08:21 +0200334#define MVPP2_GMAC_PCS_ENABLE_MASK BIT(3)
335#define MVPP2_GMAC_PORT_RGMII_MASK BIT(4)
Antoine Ténart39193572017-08-22 19:08:24 +0200336#define MVPP2_GMAC_DISABLE_PADDING BIT(5)
Antoine Ténart81b66302017-08-22 19:08:21 +0200337#define MVPP2_GMAC_PORT_RESET_MASK BIT(6)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300338#define MVPP2_GMAC_AUTONEG_CONFIG 0xc
Antoine Ténart81b66302017-08-22 19:08:21 +0200339#define MVPP2_GMAC_FORCE_LINK_DOWN BIT(0)
340#define MVPP2_GMAC_FORCE_LINK_PASS BIT(1)
Antoine Ténart39193572017-08-22 19:08:24 +0200341#define MVPP2_GMAC_IN_BAND_AUTONEG BIT(2)
342#define MVPP2_GMAC_IN_BAND_AUTONEG_BYPASS BIT(3)
Antoine Ténart81b66302017-08-22 19:08:21 +0200343#define MVPP2_GMAC_CONFIG_MII_SPEED BIT(5)
344#define MVPP2_GMAC_CONFIG_GMII_SPEED BIT(6)
345#define MVPP2_GMAC_AN_SPEED_EN BIT(7)
346#define MVPP2_GMAC_FC_ADV_EN BIT(9)
Antoine Ténart39193572017-08-22 19:08:24 +0200347#define MVPP2_GMAC_FLOW_CTRL_AUTONEG BIT(11)
Antoine Ténart81b66302017-08-22 19:08:21 +0200348#define MVPP2_GMAC_CONFIG_FULL_DUPLEX BIT(12)
349#define MVPP2_GMAC_AN_DUPLEX_EN BIT(13)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300350#define MVPP2_GMAC_PORT_FIFO_CFG_1_REG 0x1c
Antoine Ténart81b66302017-08-22 19:08:21 +0200351#define MVPP2_GMAC_TX_FIFO_MIN_TH_OFFS 6
352#define MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK 0x1fc0
353#define MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(v) (((v) << 6) & \
Marcin Wojtas3f518502014-07-10 16:52:13 -0300354 MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK)
Thomas Petazzoni26975822017-03-07 16:53:14 +0100355#define MVPP22_GMAC_CTRL_4_REG 0x90
Antoine Ténart81b66302017-08-22 19:08:21 +0200356#define MVPP22_CTRL4_EXT_PIN_GMII_SEL BIT(0)
357#define MVPP22_CTRL4_DP_CLK_SEL BIT(5)
Antoine Ténart1068ec72017-08-22 19:08:22 +0200358#define MVPP22_CTRL4_SYNC_BYPASS_DIS BIT(6)
Antoine Ténart81b66302017-08-22 19:08:21 +0200359#define MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE BIT(7)
Thomas Petazzoni26975822017-03-07 16:53:14 +0100360
361/* Per-port XGMAC registers. PPv2.2 only, only for GOP port 0,
362 * relative to port->base.
363 */
Antoine Ténart725757a2017-06-12 16:01:39 +0200364#define MVPP22_XLG_CTRL0_REG 0x100
Antoine Ténart81b66302017-08-22 19:08:21 +0200365#define MVPP22_XLG_CTRL0_PORT_EN BIT(0)
366#define MVPP22_XLG_CTRL0_MAC_RESET_DIS BIT(1)
Antoine Ténart77321952017-08-22 19:08:25 +0200367#define MVPP22_XLG_CTRL0_RX_FLOW_CTRL_EN BIT(7)
Antoine Ténart81b66302017-08-22 19:08:21 +0200368#define MVPP22_XLG_CTRL0_MIB_CNT_DIS BIT(14)
Stefan Chulski76eb1b12017-08-22 19:08:26 +0200369#define MVPP22_XLG_CTRL1_REG 0x104
Antoine Ténartec15ecd2017-08-25 15:24:46 +0200370#define MVPP22_XLG_CTRL1_FRAMESIZELIMIT_OFFS 0
Stefan Chulski76eb1b12017-08-22 19:08:26 +0200371#define MVPP22_XLG_CTRL1_FRAMESIZELIMIT_MASK 0x1fff
Thomas Petazzoni26975822017-03-07 16:53:14 +0100372#define MVPP22_XLG_CTRL3_REG 0x11c
Antoine Ténart81b66302017-08-22 19:08:21 +0200373#define MVPP22_XLG_CTRL3_MACMODESELECT_MASK (7 << 13)
374#define MVPP22_XLG_CTRL3_MACMODESELECT_GMAC (0 << 13)
375#define MVPP22_XLG_CTRL3_MACMODESELECT_10G (1 << 13)
Thomas Petazzoni26975822017-03-07 16:53:14 +0100376
Antoine Ténart77321952017-08-22 19:08:25 +0200377#define MVPP22_XLG_CTRL4_REG 0x184
378#define MVPP22_XLG_CTRL4_FWD_FC BIT(5)
379#define MVPP22_XLG_CTRL4_FWD_PFC BIT(6)
380#define MVPP22_XLG_CTRL4_MACMODSELECT_GMAC BIT(12)
381
Thomas Petazzoni26975822017-03-07 16:53:14 +0100382/* SMI registers. PPv2.2 only, relative to priv->iface_base. */
383#define MVPP22_SMI_MISC_CFG_REG 0x1204
Antoine Ténart81b66302017-08-22 19:08:21 +0200384#define MVPP22_SMI_POLLING_EN BIT(10)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300385
Thomas Petazzonia7868412017-03-07 16:53:13 +0100386#define MVPP22_GMAC_BASE(port) (0x7000 + (port) * 0x1000 + 0xe00)
387
Marcin Wojtas3f518502014-07-10 16:52:13 -0300388#define MVPP2_CAUSE_TXQ_SENT_DESC_ALL_MASK 0xff
389
390/* Descriptor ring Macros */
391#define MVPP2_QUEUE_NEXT_DESC(q, index) \
392 (((index) < (q)->last_desc) ? ((index) + 1) : 0)
393
Antoine Ténartf84bf382017-08-22 19:08:27 +0200394/* XPCS registers. PPv2.2 only */
395#define MVPP22_MPCS_BASE(port) (0x7000 + (port) * 0x1000)
396#define MVPP22_MPCS_CTRL 0x14
397#define MVPP22_MPCS_CTRL_FWD_ERR_CONN BIT(10)
398#define MVPP22_MPCS_CLK_RESET 0x14c
399#define MAC_CLK_RESET_SD_TX BIT(0)
400#define MAC_CLK_RESET_SD_RX BIT(1)
401#define MAC_CLK_RESET_MAC BIT(2)
402#define MVPP22_MPCS_CLK_RESET_DIV_RATIO(n) ((n) << 4)
403#define MVPP22_MPCS_CLK_RESET_DIV_SET BIT(11)
404
405/* XPCS registers. PPv2.2 only */
406#define MVPP22_XPCS_BASE(port) (0x7400 + (port) * 0x1000)
407#define MVPP22_XPCS_CFG0 0x0
408#define MVPP22_XPCS_CFG0_PCS_MODE(n) ((n) << 3)
409#define MVPP22_XPCS_CFG0_ACTIVE_LANE(n) ((n) << 5)
410
411/* System controller registers. Accessed through a regmap. */
412#define GENCONF_SOFT_RESET1 0x1108
413#define GENCONF_SOFT_RESET1_GOP BIT(6)
414#define GENCONF_PORT_CTRL0 0x1110
415#define GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT BIT(1)
416#define GENCONF_PORT_CTRL0_RX_DATA_SAMPLE BIT(29)
417#define GENCONF_PORT_CTRL0_CLK_DIV_PHASE_CLR BIT(31)
418#define GENCONF_PORT_CTRL1 0x1114
419#define GENCONF_PORT_CTRL1_EN(p) BIT(p)
420#define GENCONF_PORT_CTRL1_RESET(p) (BIT(p) << 28)
421#define GENCONF_CTRL0 0x1120
422#define GENCONF_CTRL0_PORT0_RGMII BIT(0)
423#define GENCONF_CTRL0_PORT1_RGMII_MII BIT(1)
424#define GENCONF_CTRL0_PORT1_RGMII BIT(2)
425
Marcin Wojtas3f518502014-07-10 16:52:13 -0300426/* Various constants */
427
428/* Coalescing */
429#define MVPP2_TXDONE_COAL_PKTS_THRESH 15
Marcin Wojtasedc660f2015-08-06 19:00:30 +0200430#define MVPP2_TXDONE_HRTIMER_PERIOD_NS 1000000UL
Thomas Petazzoni213f4282017-08-03 10:42:00 +0200431#define MVPP2_TXDONE_COAL_USEC 1000
Marcin Wojtas3f518502014-07-10 16:52:13 -0300432#define MVPP2_RX_COAL_PKTS 32
433#define MVPP2_RX_COAL_USEC 100
434
435/* The two bytes Marvell header. Either contains a special value used
436 * by Marvell switches when a specific hardware mode is enabled (not
437 * supported by this driver) or is filled automatically by zeroes on
438 * the RX side. Those two bytes being at the front of the Ethernet
439 * header, they allow to have the IP header aligned on a 4 bytes
440 * boundary automatically: the hardware skips those two bytes on its
441 * own.
442 */
443#define MVPP2_MH_SIZE 2
444#define MVPP2_ETH_TYPE_LEN 2
445#define MVPP2_PPPOE_HDR_SIZE 8
446#define MVPP2_VLAN_TAG_LEN 4
447
448/* Lbtd 802.3 type */
449#define MVPP2_IP_LBDT_TYPE 0xfffa
450
Marcin Wojtas3f518502014-07-10 16:52:13 -0300451#define MVPP2_TX_CSUM_MAX_SIZE 9800
452
453/* Timeout constants */
454#define MVPP2_TX_DISABLE_TIMEOUT_MSEC 1000
455#define MVPP2_TX_PENDING_TIMEOUT_MSEC 1000
456
457#define MVPP2_TX_MTU_MAX 0x7ffff
458
459/* Maximum number of T-CONTs of PON port */
460#define MVPP2_MAX_TCONT 16
461
462/* Maximum number of supported ports */
463#define MVPP2_MAX_PORTS 4
464
465/* Maximum number of TXQs used by single port */
466#define MVPP2_MAX_TXQ 8
467
Marcin Wojtas3f518502014-07-10 16:52:13 -0300468/* Dfault number of RXQs in use */
469#define MVPP2_DEFAULT_RXQ 4
470
Marcin Wojtas3f518502014-07-10 16:52:13 -0300471/* Max number of Rx descriptors */
472#define MVPP2_MAX_RXD 128
473
474/* Max number of Tx descriptors */
475#define MVPP2_MAX_TXD 1024
476
477/* Amount of Tx descriptors that can be reserved at once by CPU */
478#define MVPP2_CPU_DESC_CHUNK 64
479
480/* Max number of Tx descriptors in each aggregated queue */
481#define MVPP2_AGGR_TXQ_SIZE 256
482
483/* Descriptor aligned size */
484#define MVPP2_DESC_ALIGNED_SIZE 32
485
486/* Descriptor alignment mask */
487#define MVPP2_TX_DESC_ALIGN (MVPP2_DESC_ALIGNED_SIZE - 1)
488
489/* RX FIFO constants */
490#define MVPP2_RX_FIFO_PORT_DATA_SIZE 0x2000
491#define MVPP2_RX_FIFO_PORT_ATTR_SIZE 0x80
492#define MVPP2_RX_FIFO_PORT_MIN_PKT 0x80
493
494/* RX buffer constants */
495#define MVPP2_SKB_SHINFO_SIZE \
496 SKB_DATA_ALIGN(sizeof(struct skb_shared_info))
497
498#define MVPP2_RX_PKT_SIZE(mtu) \
499 ALIGN((mtu) + MVPP2_MH_SIZE + MVPP2_VLAN_TAG_LEN + \
Jisheng Zhang4a0a12d2016-04-01 17:11:05 +0800500 ETH_HLEN + ETH_FCS_LEN, cache_line_size())
Marcin Wojtas3f518502014-07-10 16:52:13 -0300501
502#define MVPP2_RX_BUF_SIZE(pkt_size) ((pkt_size) + NET_SKB_PAD)
503#define MVPP2_RX_TOTAL_SIZE(buf_size) ((buf_size) + MVPP2_SKB_SHINFO_SIZE)
504#define MVPP2_RX_MAX_PKT_SIZE(total_size) \
505 ((total_size) - NET_SKB_PAD - MVPP2_SKB_SHINFO_SIZE)
506
507#define MVPP2_BIT_TO_BYTE(bit) ((bit) / 8)
508
509/* IPv6 max L3 address size */
510#define MVPP2_MAX_L3_ADDR_SIZE 16
511
512/* Port flags */
513#define MVPP2_F_LOOPBACK BIT(0)
514
515/* Marvell tag types */
516enum mvpp2_tag_type {
517 MVPP2_TAG_TYPE_NONE = 0,
518 MVPP2_TAG_TYPE_MH = 1,
519 MVPP2_TAG_TYPE_DSA = 2,
520 MVPP2_TAG_TYPE_EDSA = 3,
521 MVPP2_TAG_TYPE_VLAN = 4,
522 MVPP2_TAG_TYPE_LAST = 5
523};
524
525/* Parser constants */
526#define MVPP2_PRS_TCAM_SRAM_SIZE 256
527#define MVPP2_PRS_TCAM_WORDS 6
528#define MVPP2_PRS_SRAM_WORDS 4
529#define MVPP2_PRS_FLOW_ID_SIZE 64
530#define MVPP2_PRS_FLOW_ID_MASK 0x3f
531#define MVPP2_PRS_TCAM_ENTRY_INVALID 1
532#define MVPP2_PRS_TCAM_DSA_TAGGED_BIT BIT(5)
533#define MVPP2_PRS_IPV4_HEAD 0x40
534#define MVPP2_PRS_IPV4_HEAD_MASK 0xf0
535#define MVPP2_PRS_IPV4_MC 0xe0
536#define MVPP2_PRS_IPV4_MC_MASK 0xf0
537#define MVPP2_PRS_IPV4_BC_MASK 0xff
538#define MVPP2_PRS_IPV4_IHL 0x5
539#define MVPP2_PRS_IPV4_IHL_MASK 0xf
540#define MVPP2_PRS_IPV6_MC 0xff
541#define MVPP2_PRS_IPV6_MC_MASK 0xff
542#define MVPP2_PRS_IPV6_HOP_MASK 0xff
543#define MVPP2_PRS_TCAM_PROTO_MASK 0xff
544#define MVPP2_PRS_TCAM_PROTO_MASK_L 0x3f
545#define MVPP2_PRS_DBL_VLANS_MAX 100
546
547/* Tcam structure:
548 * - lookup ID - 4 bits
549 * - port ID - 1 byte
550 * - additional information - 1 byte
551 * - header data - 8 bytes
552 * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(5)->(0).
553 */
554#define MVPP2_PRS_AI_BITS 8
555#define MVPP2_PRS_PORT_MASK 0xff
556#define MVPP2_PRS_LU_MASK 0xf
557#define MVPP2_PRS_TCAM_DATA_BYTE(offs) \
558 (((offs) - ((offs) % 2)) * 2 + ((offs) % 2))
559#define MVPP2_PRS_TCAM_DATA_BYTE_EN(offs) \
560 (((offs) * 2) - ((offs) % 2) + 2)
561#define MVPP2_PRS_TCAM_AI_BYTE 16
562#define MVPP2_PRS_TCAM_PORT_BYTE 17
563#define MVPP2_PRS_TCAM_LU_BYTE 20
564#define MVPP2_PRS_TCAM_EN_OFFS(offs) ((offs) + 2)
565#define MVPP2_PRS_TCAM_INV_WORD 5
566/* Tcam entries ID */
567#define MVPP2_PE_DROP_ALL 0
568#define MVPP2_PE_FIRST_FREE_TID 1
569#define MVPP2_PE_LAST_FREE_TID (MVPP2_PRS_TCAM_SRAM_SIZE - 31)
570#define MVPP2_PE_IP6_EXT_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 30)
571#define MVPP2_PE_MAC_MC_IP6 (MVPP2_PRS_TCAM_SRAM_SIZE - 29)
572#define MVPP2_PE_IP6_ADDR_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 28)
573#define MVPP2_PE_IP4_ADDR_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 27)
574#define MVPP2_PE_LAST_DEFAULT_FLOW (MVPP2_PRS_TCAM_SRAM_SIZE - 26)
575#define MVPP2_PE_FIRST_DEFAULT_FLOW (MVPP2_PRS_TCAM_SRAM_SIZE - 19)
576#define MVPP2_PE_EDSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 18)
577#define MVPP2_PE_EDSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 17)
578#define MVPP2_PE_DSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 16)
579#define MVPP2_PE_DSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 15)
580#define MVPP2_PE_ETYPE_EDSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 14)
581#define MVPP2_PE_ETYPE_EDSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 13)
582#define MVPP2_PE_ETYPE_DSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 12)
583#define MVPP2_PE_ETYPE_DSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 11)
584#define MVPP2_PE_MH_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 10)
585#define MVPP2_PE_DSA_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 9)
586#define MVPP2_PE_IP6_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 8)
587#define MVPP2_PE_IP4_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 7)
588#define MVPP2_PE_ETH_TYPE_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 6)
589#define MVPP2_PE_VLAN_DBL (MVPP2_PRS_TCAM_SRAM_SIZE - 5)
590#define MVPP2_PE_VLAN_NONE (MVPP2_PRS_TCAM_SRAM_SIZE - 4)
591#define MVPP2_PE_MAC_MC_ALL (MVPP2_PRS_TCAM_SRAM_SIZE - 3)
592#define MVPP2_PE_MAC_PROMISCUOUS (MVPP2_PRS_TCAM_SRAM_SIZE - 2)
593#define MVPP2_PE_MAC_NON_PROMISCUOUS (MVPP2_PRS_TCAM_SRAM_SIZE - 1)
594
595/* Sram structure
596 * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(3)->(0).
597 */
598#define MVPP2_PRS_SRAM_RI_OFFS 0
599#define MVPP2_PRS_SRAM_RI_WORD 0
600#define MVPP2_PRS_SRAM_RI_CTRL_OFFS 32
601#define MVPP2_PRS_SRAM_RI_CTRL_WORD 1
602#define MVPP2_PRS_SRAM_RI_CTRL_BITS 32
603#define MVPP2_PRS_SRAM_SHIFT_OFFS 64
604#define MVPP2_PRS_SRAM_SHIFT_SIGN_BIT 72
605#define MVPP2_PRS_SRAM_UDF_OFFS 73
606#define MVPP2_PRS_SRAM_UDF_BITS 8
607#define MVPP2_PRS_SRAM_UDF_MASK 0xff
608#define MVPP2_PRS_SRAM_UDF_SIGN_BIT 81
609#define MVPP2_PRS_SRAM_UDF_TYPE_OFFS 82
610#define MVPP2_PRS_SRAM_UDF_TYPE_MASK 0x7
611#define MVPP2_PRS_SRAM_UDF_TYPE_L3 1
612#define MVPP2_PRS_SRAM_UDF_TYPE_L4 4
613#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS 85
614#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK 0x3
615#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD 1
616#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP4_ADD 2
617#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP6_ADD 3
618#define MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS 87
619#define MVPP2_PRS_SRAM_OP_SEL_UDF_BITS 2
620#define MVPP2_PRS_SRAM_OP_SEL_UDF_MASK 0x3
621#define MVPP2_PRS_SRAM_OP_SEL_UDF_ADD 0
622#define MVPP2_PRS_SRAM_OP_SEL_UDF_IP4_ADD 2
623#define MVPP2_PRS_SRAM_OP_SEL_UDF_IP6_ADD 3
624#define MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS 89
625#define MVPP2_PRS_SRAM_AI_OFFS 90
626#define MVPP2_PRS_SRAM_AI_CTRL_OFFS 98
627#define MVPP2_PRS_SRAM_AI_CTRL_BITS 8
628#define MVPP2_PRS_SRAM_AI_MASK 0xff
629#define MVPP2_PRS_SRAM_NEXT_LU_OFFS 106
630#define MVPP2_PRS_SRAM_NEXT_LU_MASK 0xf
631#define MVPP2_PRS_SRAM_LU_DONE_BIT 110
632#define MVPP2_PRS_SRAM_LU_GEN_BIT 111
633
634/* Sram result info bits assignment */
635#define MVPP2_PRS_RI_MAC_ME_MASK 0x1
636#define MVPP2_PRS_RI_DSA_MASK 0x2
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100637#define MVPP2_PRS_RI_VLAN_MASK (BIT(2) | BIT(3))
638#define MVPP2_PRS_RI_VLAN_NONE 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300639#define MVPP2_PRS_RI_VLAN_SINGLE BIT(2)
640#define MVPP2_PRS_RI_VLAN_DOUBLE BIT(3)
641#define MVPP2_PRS_RI_VLAN_TRIPLE (BIT(2) | BIT(3))
642#define MVPP2_PRS_RI_CPU_CODE_MASK 0x70
643#define MVPP2_PRS_RI_CPU_CODE_RX_SPEC BIT(4)
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100644#define MVPP2_PRS_RI_L2_CAST_MASK (BIT(9) | BIT(10))
645#define MVPP2_PRS_RI_L2_UCAST 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300646#define MVPP2_PRS_RI_L2_MCAST BIT(9)
647#define MVPP2_PRS_RI_L2_BCAST BIT(10)
648#define MVPP2_PRS_RI_PPPOE_MASK 0x800
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100649#define MVPP2_PRS_RI_L3_PROTO_MASK (BIT(12) | BIT(13) | BIT(14))
650#define MVPP2_PRS_RI_L3_UN 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300651#define MVPP2_PRS_RI_L3_IP4 BIT(12)
652#define MVPP2_PRS_RI_L3_IP4_OPT BIT(13)
653#define MVPP2_PRS_RI_L3_IP4_OTHER (BIT(12) | BIT(13))
654#define MVPP2_PRS_RI_L3_IP6 BIT(14)
655#define MVPP2_PRS_RI_L3_IP6_EXT (BIT(12) | BIT(14))
656#define MVPP2_PRS_RI_L3_ARP (BIT(13) | BIT(14))
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100657#define MVPP2_PRS_RI_L3_ADDR_MASK (BIT(15) | BIT(16))
658#define MVPP2_PRS_RI_L3_UCAST 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300659#define MVPP2_PRS_RI_L3_MCAST BIT(15)
660#define MVPP2_PRS_RI_L3_BCAST (BIT(15) | BIT(16))
661#define MVPP2_PRS_RI_IP_FRAG_MASK 0x20000
662#define MVPP2_PRS_RI_UDF3_MASK 0x300000
663#define MVPP2_PRS_RI_UDF3_RX_SPECIAL BIT(21)
664#define MVPP2_PRS_RI_L4_PROTO_MASK 0x1c00000
665#define MVPP2_PRS_RI_L4_TCP BIT(22)
666#define MVPP2_PRS_RI_L4_UDP BIT(23)
667#define MVPP2_PRS_RI_L4_OTHER (BIT(22) | BIT(23))
668#define MVPP2_PRS_RI_UDF7_MASK 0x60000000
669#define MVPP2_PRS_RI_UDF7_IP6_LITE BIT(29)
670#define MVPP2_PRS_RI_DROP_MASK 0x80000000
671
672/* Sram additional info bits assignment */
673#define MVPP2_PRS_IPV4_DIP_AI_BIT BIT(0)
674#define MVPP2_PRS_IPV6_NO_EXT_AI_BIT BIT(0)
675#define MVPP2_PRS_IPV6_EXT_AI_BIT BIT(1)
676#define MVPP2_PRS_IPV6_EXT_AH_AI_BIT BIT(2)
677#define MVPP2_PRS_IPV6_EXT_AH_LEN_AI_BIT BIT(3)
678#define MVPP2_PRS_IPV6_EXT_AH_L4_AI_BIT BIT(4)
679#define MVPP2_PRS_SINGLE_VLAN_AI 0
680#define MVPP2_PRS_DBL_VLAN_AI_BIT BIT(7)
681
682/* DSA/EDSA type */
683#define MVPP2_PRS_TAGGED true
684#define MVPP2_PRS_UNTAGGED false
685#define MVPP2_PRS_EDSA true
686#define MVPP2_PRS_DSA false
687
688/* MAC entries, shadow udf */
689enum mvpp2_prs_udf {
690 MVPP2_PRS_UDF_MAC_DEF,
691 MVPP2_PRS_UDF_MAC_RANGE,
692 MVPP2_PRS_UDF_L2_DEF,
693 MVPP2_PRS_UDF_L2_DEF_COPY,
694 MVPP2_PRS_UDF_L2_USER,
695};
696
697/* Lookup ID */
698enum mvpp2_prs_lookup {
699 MVPP2_PRS_LU_MH,
700 MVPP2_PRS_LU_MAC,
701 MVPP2_PRS_LU_DSA,
702 MVPP2_PRS_LU_VLAN,
703 MVPP2_PRS_LU_L2,
704 MVPP2_PRS_LU_PPPOE,
705 MVPP2_PRS_LU_IP4,
706 MVPP2_PRS_LU_IP6,
707 MVPP2_PRS_LU_FLOWS,
708 MVPP2_PRS_LU_LAST,
709};
710
711/* L3 cast enum */
712enum mvpp2_prs_l3_cast {
713 MVPP2_PRS_L3_UNI_CAST,
714 MVPP2_PRS_L3_MULTI_CAST,
715 MVPP2_PRS_L3_BROAD_CAST
716};
717
718/* Classifier constants */
719#define MVPP2_CLS_FLOWS_TBL_SIZE 512
720#define MVPP2_CLS_FLOWS_TBL_DATA_WORDS 3
721#define MVPP2_CLS_LKP_TBL_SIZE 64
722
723/* BM constants */
724#define MVPP2_BM_POOLS_NUM 8
725#define MVPP2_BM_LONG_BUF_NUM 1024
726#define MVPP2_BM_SHORT_BUF_NUM 2048
727#define MVPP2_BM_POOL_SIZE_MAX (16*1024 - MVPP2_BM_POOL_PTR_ALIGN/4)
728#define MVPP2_BM_POOL_PTR_ALIGN 128
729#define MVPP2_BM_SWF_LONG_POOL(port) ((port > 2) ? 2 : port)
730#define MVPP2_BM_SWF_SHORT_POOL 3
731
732/* BM cookie (32 bits) definition */
733#define MVPP2_BM_COOKIE_POOL_OFFS 8
734#define MVPP2_BM_COOKIE_CPU_OFFS 24
735
736/* BM short pool packet size
737 * These value assure that for SWF the total number
738 * of bytes allocated for each buffer will be 512
739 */
740#define MVPP2_BM_SHORT_PKT_SIZE MVPP2_RX_MAX_PKT_SIZE(512)
741
Thomas Petazzonia7868412017-03-07 16:53:13 +0100742#define MVPP21_ADDR_SPACE_SZ 0
743#define MVPP22_ADDR_SPACE_SZ SZ_64K
744
Thomas Petazzonidf089aa2017-08-03 10:41:58 +0200745#define MVPP2_MAX_THREADS 8
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +0200746#define MVPP2_MAX_QVECS MVPP2_MAX_THREADS
Thomas Petazzonia7868412017-03-07 16:53:13 +0100747
Marcin Wojtas3f518502014-07-10 16:52:13 -0300748enum mvpp2_bm_type {
749 MVPP2_BM_FREE,
750 MVPP2_BM_SWF_LONG,
751 MVPP2_BM_SWF_SHORT
752};
753
754/* Definitions */
755
756/* Shared Packet Processor resources */
757struct mvpp2 {
758 /* Shared registers' base addresses */
Marcin Wojtas3f518502014-07-10 16:52:13 -0300759 void __iomem *lms_base;
Thomas Petazzonia7868412017-03-07 16:53:13 +0100760 void __iomem *iface_base;
761
Thomas Petazzonidf089aa2017-08-03 10:41:58 +0200762 /* On PPv2.2, each "software thread" can access the base
763 * register through a separate address space, each 64 KB apart
764 * from each other. Typically, such address spaces will be
765 * used per CPU.
Thomas Petazzonia7868412017-03-07 16:53:13 +0100766 */
Thomas Petazzonidf089aa2017-08-03 10:41:58 +0200767 void __iomem *swth_base[MVPP2_MAX_THREADS];
Marcin Wojtas3f518502014-07-10 16:52:13 -0300768
Antoine Ténartf84bf382017-08-22 19:08:27 +0200769 /* On PPv2.2, some port control registers are located into the system
770 * controller space. These registers are accessible through a regmap.
771 */
772 struct regmap *sysctrl_base;
773
Marcin Wojtas3f518502014-07-10 16:52:13 -0300774 /* Common clocks */
775 struct clk *pp_clk;
776 struct clk *gop_clk;
Thomas Petazzonifceb55d2017-03-07 16:53:18 +0100777 struct clk *mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300778
779 /* List of pointers to port structures */
780 struct mvpp2_port **port_list;
781
782 /* Aggregated TXQs */
783 struct mvpp2_tx_queue *aggr_txqs;
784
785 /* BM pools */
786 struct mvpp2_bm_pool *bm_pools;
787
788 /* PRS shadow table */
789 struct mvpp2_prs_shadow *prs_shadow;
790 /* PRS auxiliary table for double vlan entries control */
791 bool *prs_double_vlans;
792
793 /* Tclk value */
794 u32 tclk;
Thomas Petazzonifaca9242017-03-07 16:53:06 +0100795
796 /* HW version */
797 enum { MVPP21, MVPP22 } hw_version;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +0100798
799 /* Maximum number of RXQs per port */
800 unsigned int max_port_rxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300801};
802
803struct mvpp2_pcpu_stats {
804 struct u64_stats_sync syncp;
805 u64 rx_packets;
806 u64 rx_bytes;
807 u64 tx_packets;
808 u64 tx_bytes;
809};
810
Marcin Wojtasedc660f2015-08-06 19:00:30 +0200811/* Per-CPU port control */
812struct mvpp2_port_pcpu {
813 struct hrtimer tx_done_timer;
814 bool timer_scheduled;
815 /* Tasklet for egress finalization */
816 struct tasklet_struct tx_done_tasklet;
817};
818
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +0200819struct mvpp2_queue_vector {
820 int irq;
821 struct napi_struct napi;
822 enum { MVPP2_QUEUE_VECTOR_SHARED, MVPP2_QUEUE_VECTOR_PRIVATE } type;
823 int sw_thread_id;
824 u16 sw_thread_mask;
825 int first_rxq;
826 int nrxqs;
827 u32 pending_cause_rx;
828 struct mvpp2_port *port;
829};
830
Marcin Wojtas3f518502014-07-10 16:52:13 -0300831struct mvpp2_port {
832 u8 id;
833
Thomas Petazzonia7868412017-03-07 16:53:13 +0100834 /* Index of the port from the "group of ports" complex point
835 * of view
836 */
837 int gop_id;
838
Marcin Wojtas3f518502014-07-10 16:52:13 -0300839 struct mvpp2 *priv;
840
841 /* Per-port registers' base address */
842 void __iomem *base;
843
844 struct mvpp2_rx_queue **rxqs;
Thomas Petazzoni09f83972017-08-03 10:41:57 +0200845 unsigned int nrxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300846 struct mvpp2_tx_queue **txqs;
Thomas Petazzoni09f83972017-08-03 10:41:57 +0200847 unsigned int ntxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300848 struct net_device *dev;
849
850 int pkt_size;
851
Marcin Wojtasedc660f2015-08-06 19:00:30 +0200852 /* Per-CPU port control */
853 struct mvpp2_port_pcpu __percpu *pcpu;
854
Marcin Wojtas3f518502014-07-10 16:52:13 -0300855 /* Flags */
856 unsigned long flags;
857
858 u16 tx_ring_size;
859 u16 rx_ring_size;
860 struct mvpp2_pcpu_stats __percpu *stats;
861
Marcin Wojtas3f518502014-07-10 16:52:13 -0300862 phy_interface_t phy_interface;
863 struct device_node *phy_node;
864 unsigned int link;
865 unsigned int duplex;
866 unsigned int speed;
867
868 struct mvpp2_bm_pool *pool_long;
869 struct mvpp2_bm_pool *pool_short;
870
871 /* Index of first port's physical RXQ */
872 u8 first_rxq;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +0200873
874 struct mvpp2_queue_vector qvecs[MVPP2_MAX_QVECS];
875 unsigned int nqvecs;
Thomas Petazzoni213f4282017-08-03 10:42:00 +0200876 bool has_tx_irqs;
877
878 u32 tx_time_coal;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300879};
880
881/* The mvpp2_tx_desc and mvpp2_rx_desc structures describe the
882 * layout of the transmit and reception DMA descriptors, and their
883 * layout is therefore defined by the hardware design
884 */
885
886#define MVPP2_TXD_L3_OFF_SHIFT 0
887#define MVPP2_TXD_IP_HLEN_SHIFT 8
888#define MVPP2_TXD_L4_CSUM_FRAG BIT(13)
889#define MVPP2_TXD_L4_CSUM_NOT BIT(14)
890#define MVPP2_TXD_IP_CSUM_DISABLE BIT(15)
891#define MVPP2_TXD_PADDING_DISABLE BIT(23)
892#define MVPP2_TXD_L4_UDP BIT(24)
893#define MVPP2_TXD_L3_IP6 BIT(26)
894#define MVPP2_TXD_L_DESC BIT(28)
895#define MVPP2_TXD_F_DESC BIT(29)
896
897#define MVPP2_RXD_ERR_SUMMARY BIT(15)
898#define MVPP2_RXD_ERR_CODE_MASK (BIT(13) | BIT(14))
899#define MVPP2_RXD_ERR_CRC 0x0
900#define MVPP2_RXD_ERR_OVERRUN BIT(13)
901#define MVPP2_RXD_ERR_RESOURCE (BIT(13) | BIT(14))
902#define MVPP2_RXD_BM_POOL_ID_OFFS 16
903#define MVPP2_RXD_BM_POOL_ID_MASK (BIT(16) | BIT(17) | BIT(18))
904#define MVPP2_RXD_HWF_SYNC BIT(21)
905#define MVPP2_RXD_L4_CSUM_OK BIT(22)
906#define MVPP2_RXD_IP4_HEADER_ERR BIT(24)
907#define MVPP2_RXD_L4_TCP BIT(25)
908#define MVPP2_RXD_L4_UDP BIT(26)
909#define MVPP2_RXD_L3_IP4 BIT(28)
910#define MVPP2_RXD_L3_IP6 BIT(30)
911#define MVPP2_RXD_BUF_HDR BIT(31)
912
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100913/* HW TX descriptor for PPv2.1 */
914struct mvpp21_tx_desc {
Marcin Wojtas3f518502014-07-10 16:52:13 -0300915 u32 command; /* Options used by HW for packet transmitting.*/
916 u8 packet_offset; /* the offset from the buffer beginning */
917 u8 phys_txq; /* destination queue ID */
918 u16 data_size; /* data size of transmitted packet in bytes */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100919 u32 buf_dma_addr; /* physical addr of transmitted buffer */
Marcin Wojtas3f518502014-07-10 16:52:13 -0300920 u32 buf_cookie; /* cookie for access to TX buffer in tx path */
921 u32 reserved1[3]; /* hw_cmd (for future use, BM, PON, PNC) */
922 u32 reserved2; /* reserved (for future use) */
923};
924
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100925/* HW RX descriptor for PPv2.1 */
926struct mvpp21_rx_desc {
Marcin Wojtas3f518502014-07-10 16:52:13 -0300927 u32 status; /* info about received packet */
928 u16 reserved1; /* parser_info (for future use, PnC) */
929 u16 data_size; /* size of received packet in bytes */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100930 u32 buf_dma_addr; /* physical address of the buffer */
Marcin Wojtas3f518502014-07-10 16:52:13 -0300931 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
932 u16 reserved2; /* gem_port_id (for future use, PON) */
933 u16 reserved3; /* csum_l4 (for future use, PnC) */
934 u8 reserved4; /* bm_qset (for future use, BM) */
935 u8 reserved5;
936 u16 reserved6; /* classify_info (for future use, PnC) */
937 u32 reserved7; /* flow_id (for future use, PnC) */
938 u32 reserved8;
939};
940
Thomas Petazzonie7c53592017-03-07 16:53:08 +0100941/* HW TX descriptor for PPv2.2 */
942struct mvpp22_tx_desc {
943 u32 command;
944 u8 packet_offset;
945 u8 phys_txq;
946 u16 data_size;
947 u64 reserved1;
948 u64 buf_dma_addr_ptp;
949 u64 buf_cookie_misc;
950};
951
952/* HW RX descriptor for PPv2.2 */
953struct mvpp22_rx_desc {
954 u32 status;
955 u16 reserved1;
956 u16 data_size;
957 u32 reserved2;
958 u32 reserved3;
959 u64 buf_dma_addr_key_hash;
960 u64 buf_cookie_misc;
961};
962
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100963/* Opaque type used by the driver to manipulate the HW TX and RX
964 * descriptors
965 */
966struct mvpp2_tx_desc {
967 union {
968 struct mvpp21_tx_desc pp21;
Thomas Petazzonie7c53592017-03-07 16:53:08 +0100969 struct mvpp22_tx_desc pp22;
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100970 };
971};
972
973struct mvpp2_rx_desc {
974 union {
975 struct mvpp21_rx_desc pp21;
Thomas Petazzonie7c53592017-03-07 16:53:08 +0100976 struct mvpp22_rx_desc pp22;
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100977 };
978};
979
Thomas Petazzoni83544912016-12-21 11:28:49 +0100980struct mvpp2_txq_pcpu_buf {
981 /* Transmitted SKB */
982 struct sk_buff *skb;
983
984 /* Physical address of transmitted buffer */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100985 dma_addr_t dma;
Thomas Petazzoni83544912016-12-21 11:28:49 +0100986
987 /* Size transmitted */
988 size_t size;
989};
990
Marcin Wojtas3f518502014-07-10 16:52:13 -0300991/* Per-CPU Tx queue control */
992struct mvpp2_txq_pcpu {
993 int cpu;
994
995 /* Number of Tx DMA descriptors in the descriptor ring */
996 int size;
997
998 /* Number of currently used Tx DMA descriptor in the
999 * descriptor ring
1000 */
1001 int count;
1002
1003 /* Number of Tx DMA descriptors reserved for each CPU */
1004 int reserved_num;
1005
Thomas Petazzoni83544912016-12-21 11:28:49 +01001006 /* Infos about transmitted buffers */
1007 struct mvpp2_txq_pcpu_buf *buffs;
Marcin Wojtas71ce3912015-08-06 19:00:29 +02001008
Marcin Wojtas3f518502014-07-10 16:52:13 -03001009 /* Index of last TX DMA descriptor that was inserted */
1010 int txq_put_index;
1011
1012 /* Index of the TX DMA descriptor to be cleaned up */
1013 int txq_get_index;
Antoine Ténart186cd4d2017-08-23 09:46:56 +02001014
1015 /* DMA buffer for TSO headers */
1016 char *tso_headers;
1017 dma_addr_t tso_headers_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001018};
1019
1020struct mvpp2_tx_queue {
1021 /* Physical number of this Tx queue */
1022 u8 id;
1023
1024 /* Logical number of this Tx queue */
1025 u8 log_id;
1026
1027 /* Number of Tx DMA descriptors in the descriptor ring */
1028 int size;
1029
1030 /* Number of currently used Tx DMA descriptor in the descriptor ring */
1031 int count;
1032
1033 /* Per-CPU control of physical Tx queues */
1034 struct mvpp2_txq_pcpu __percpu *pcpu;
1035
Marcin Wojtas3f518502014-07-10 16:52:13 -03001036 u32 done_pkts_coal;
1037
1038 /* Virtual address of thex Tx DMA descriptors array */
1039 struct mvpp2_tx_desc *descs;
1040
1041 /* DMA address of the Tx DMA descriptors array */
Thomas Petazzoni20396132017-03-07 16:53:00 +01001042 dma_addr_t descs_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001043
1044 /* Index of the last Tx DMA descriptor */
1045 int last_desc;
1046
1047 /* Index of the next Tx DMA descriptor to process */
1048 int next_desc_to_proc;
1049};
1050
1051struct mvpp2_rx_queue {
1052 /* RX queue number, in the range 0-31 for physical RXQs */
1053 u8 id;
1054
1055 /* Num of rx descriptors in the rx descriptor ring */
1056 int size;
1057
1058 u32 pkts_coal;
1059 u32 time_coal;
1060
1061 /* Virtual address of the RX DMA descriptors array */
1062 struct mvpp2_rx_desc *descs;
1063
1064 /* DMA address of the RX DMA descriptors array */
Thomas Petazzoni20396132017-03-07 16:53:00 +01001065 dma_addr_t descs_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001066
1067 /* Index of the last RX DMA descriptor */
1068 int last_desc;
1069
1070 /* Index of the next RX DMA descriptor to process */
1071 int next_desc_to_proc;
1072
1073 /* ID of port to which physical RXQ is mapped */
1074 int port;
1075
1076 /* Port's logic RXQ number to which physical RXQ is mapped */
1077 int logic_rxq;
1078};
1079
1080union mvpp2_prs_tcam_entry {
1081 u32 word[MVPP2_PRS_TCAM_WORDS];
1082 u8 byte[MVPP2_PRS_TCAM_WORDS * 4];
1083};
1084
1085union mvpp2_prs_sram_entry {
1086 u32 word[MVPP2_PRS_SRAM_WORDS];
1087 u8 byte[MVPP2_PRS_SRAM_WORDS * 4];
1088};
1089
1090struct mvpp2_prs_entry {
1091 u32 index;
1092 union mvpp2_prs_tcam_entry tcam;
1093 union mvpp2_prs_sram_entry sram;
1094};
1095
1096struct mvpp2_prs_shadow {
1097 bool valid;
1098 bool finish;
1099
1100 /* Lookup ID */
1101 int lu;
1102
1103 /* User defined offset */
1104 int udf;
1105
1106 /* Result info */
1107 u32 ri;
1108 u32 ri_mask;
1109};
1110
1111struct mvpp2_cls_flow_entry {
1112 u32 index;
1113 u32 data[MVPP2_CLS_FLOWS_TBL_DATA_WORDS];
1114};
1115
1116struct mvpp2_cls_lookup_entry {
1117 u32 lkpid;
1118 u32 way;
1119 u32 data;
1120};
1121
1122struct mvpp2_bm_pool {
1123 /* Pool number in the range 0-7 */
1124 int id;
1125 enum mvpp2_bm_type type;
1126
1127 /* Buffer Pointers Pool External (BPPE) size */
1128 int size;
Thomas Petazzonid01524d2017-03-07 16:53:09 +01001129 /* BPPE size in bytes */
1130 int size_bytes;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001131 /* Number of buffers for this pool */
1132 int buf_num;
1133 /* Pool buffer size */
1134 int buf_size;
1135 /* Packet size */
1136 int pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01001137 int frag_size;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001138
1139 /* BPPE virtual base address */
1140 u32 *virt_addr;
Thomas Petazzoni20396132017-03-07 16:53:00 +01001141 /* BPPE DMA base address */
1142 dma_addr_t dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001143
1144 /* Ports using BM pool */
1145 u32 port_map;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001146};
1147
Thomas Petazzoni213f4282017-08-03 10:42:00 +02001148/* Queue modes */
1149#define MVPP2_QDIST_SINGLE_MODE 0
1150#define MVPP2_QDIST_MULTI_MODE 1
1151
1152static int queue_mode = MVPP2_QDIST_SINGLE_MODE;
1153
1154module_param(queue_mode, int, 0444);
1155MODULE_PARM_DESC(queue_mode, "Set queue_mode (single=0, multi=1)");
1156
Marcin Wojtas3f518502014-07-10 16:52:13 -03001157#define MVPP2_DRIVER_NAME "mvpp2"
1158#define MVPP2_DRIVER_VERSION "1.0"
1159
1160/* Utility/helper methods */
1161
1162static void mvpp2_write(struct mvpp2 *priv, u32 offset, u32 data)
1163{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001164 writel(data, priv->swth_base[0] + offset);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001165}
1166
1167static u32 mvpp2_read(struct mvpp2 *priv, u32 offset)
1168{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001169 return readl(priv->swth_base[0] + offset);
Thomas Petazzonia7868412017-03-07 16:53:13 +01001170}
1171
1172/* These accessors should be used to access:
1173 *
1174 * - per-CPU registers, where each CPU has its own copy of the
1175 * register.
1176 *
1177 * MVPP2_BM_VIRT_ALLOC_REG
1178 * MVPP2_BM_ADDR_HIGH_ALLOC
1179 * MVPP22_BM_ADDR_HIGH_RLS_REG
1180 * MVPP2_BM_VIRT_RLS_REG
1181 * MVPP2_ISR_RX_TX_CAUSE_REG
1182 * MVPP2_ISR_RX_TX_MASK_REG
1183 * MVPP2_TXQ_NUM_REG
1184 * MVPP2_AGGR_TXQ_UPDATE_REG
1185 * MVPP2_TXQ_RSVD_REQ_REG
1186 * MVPP2_TXQ_RSVD_RSLT_REG
1187 * MVPP2_TXQ_SENT_REG
1188 * MVPP2_RXQ_NUM_REG
1189 *
1190 * - global registers that must be accessed through a specific CPU
1191 * window, because they are related to an access to a per-CPU
1192 * register
1193 *
1194 * MVPP2_BM_PHY_ALLOC_REG (related to MVPP2_BM_VIRT_ALLOC_REG)
1195 * MVPP2_BM_PHY_RLS_REG (related to MVPP2_BM_VIRT_RLS_REG)
1196 * MVPP2_RXQ_THRESH_REG (related to MVPP2_RXQ_NUM_REG)
1197 * MVPP2_RXQ_DESC_ADDR_REG (related to MVPP2_RXQ_NUM_REG)
1198 * MVPP2_RXQ_DESC_SIZE_REG (related to MVPP2_RXQ_NUM_REG)
1199 * MVPP2_RXQ_INDEX_REG (related to MVPP2_RXQ_NUM_REG)
1200 * MVPP2_TXQ_PENDING_REG (related to MVPP2_TXQ_NUM_REG)
1201 * MVPP2_TXQ_DESC_ADDR_REG (related to MVPP2_TXQ_NUM_REG)
1202 * MVPP2_TXQ_DESC_SIZE_REG (related to MVPP2_TXQ_NUM_REG)
1203 * MVPP2_TXQ_INDEX_REG (related to MVPP2_TXQ_NUM_REG)
1204 * MVPP2_TXQ_PENDING_REG (related to MVPP2_TXQ_NUM_REG)
1205 * MVPP2_TXQ_PREF_BUF_REG (related to MVPP2_TXQ_NUM_REG)
1206 * MVPP2_TXQ_PREF_BUF_REG (related to MVPP2_TXQ_NUM_REG)
1207 */
1208static void mvpp2_percpu_write(struct mvpp2 *priv, int cpu,
1209 u32 offset, u32 data)
1210{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001211 writel(data, priv->swth_base[cpu] + offset);
Thomas Petazzonia7868412017-03-07 16:53:13 +01001212}
1213
1214static u32 mvpp2_percpu_read(struct mvpp2 *priv, int cpu,
1215 u32 offset)
1216{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001217 return readl(priv->swth_base[cpu] + offset);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001218}
1219
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001220static dma_addr_t mvpp2_txdesc_dma_addr_get(struct mvpp2_port *port,
1221 struct mvpp2_tx_desc *tx_desc)
1222{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001223 if (port->priv->hw_version == MVPP21)
1224 return tx_desc->pp21.buf_dma_addr;
1225 else
1226 return tx_desc->pp22.buf_dma_addr_ptp & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001227}
1228
1229static void mvpp2_txdesc_dma_addr_set(struct mvpp2_port *port,
1230 struct mvpp2_tx_desc *tx_desc,
1231 dma_addr_t dma_addr)
1232{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001233 if (port->priv->hw_version == MVPP21) {
1234 tx_desc->pp21.buf_dma_addr = dma_addr;
1235 } else {
1236 u64 val = (u64)dma_addr;
1237
1238 tx_desc->pp22.buf_dma_addr_ptp &= ~GENMASK_ULL(40, 0);
1239 tx_desc->pp22.buf_dma_addr_ptp |= val;
1240 }
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001241}
1242
1243static size_t mvpp2_txdesc_size_get(struct mvpp2_port *port,
1244 struct mvpp2_tx_desc *tx_desc)
1245{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001246 if (port->priv->hw_version == MVPP21)
1247 return tx_desc->pp21.data_size;
1248 else
1249 return tx_desc->pp22.data_size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001250}
1251
1252static void mvpp2_txdesc_size_set(struct mvpp2_port *port,
1253 struct mvpp2_tx_desc *tx_desc,
1254 size_t size)
1255{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001256 if (port->priv->hw_version == MVPP21)
1257 tx_desc->pp21.data_size = size;
1258 else
1259 tx_desc->pp22.data_size = size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001260}
1261
1262static void mvpp2_txdesc_txq_set(struct mvpp2_port *port,
1263 struct mvpp2_tx_desc *tx_desc,
1264 unsigned int txq)
1265{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001266 if (port->priv->hw_version == MVPP21)
1267 tx_desc->pp21.phys_txq = txq;
1268 else
1269 tx_desc->pp22.phys_txq = txq;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001270}
1271
1272static void mvpp2_txdesc_cmd_set(struct mvpp2_port *port,
1273 struct mvpp2_tx_desc *tx_desc,
1274 unsigned int command)
1275{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001276 if (port->priv->hw_version == MVPP21)
1277 tx_desc->pp21.command = command;
1278 else
1279 tx_desc->pp22.command = command;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001280}
1281
1282static void mvpp2_txdesc_offset_set(struct mvpp2_port *port,
1283 struct mvpp2_tx_desc *tx_desc,
1284 unsigned int offset)
1285{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001286 if (port->priv->hw_version == MVPP21)
1287 tx_desc->pp21.packet_offset = offset;
1288 else
1289 tx_desc->pp22.packet_offset = offset;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001290}
1291
1292static unsigned int mvpp2_txdesc_offset_get(struct mvpp2_port *port,
1293 struct mvpp2_tx_desc *tx_desc)
1294{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001295 if (port->priv->hw_version == MVPP21)
1296 return tx_desc->pp21.packet_offset;
1297 else
1298 return tx_desc->pp22.packet_offset;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001299}
1300
1301static dma_addr_t mvpp2_rxdesc_dma_addr_get(struct mvpp2_port *port,
1302 struct mvpp2_rx_desc *rx_desc)
1303{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001304 if (port->priv->hw_version == MVPP21)
1305 return rx_desc->pp21.buf_dma_addr;
1306 else
1307 return rx_desc->pp22.buf_dma_addr_key_hash & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001308}
1309
1310static unsigned long mvpp2_rxdesc_cookie_get(struct mvpp2_port *port,
1311 struct mvpp2_rx_desc *rx_desc)
1312{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001313 if (port->priv->hw_version == MVPP21)
1314 return rx_desc->pp21.buf_cookie;
1315 else
1316 return rx_desc->pp22.buf_cookie_misc & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001317}
1318
1319static size_t mvpp2_rxdesc_size_get(struct mvpp2_port *port,
1320 struct mvpp2_rx_desc *rx_desc)
1321{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001322 if (port->priv->hw_version == MVPP21)
1323 return rx_desc->pp21.data_size;
1324 else
1325 return rx_desc->pp22.data_size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001326}
1327
1328static u32 mvpp2_rxdesc_status_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.status;
1333 else
1334 return rx_desc->pp22.status;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001335}
1336
Marcin Wojtas3f518502014-07-10 16:52:13 -03001337static void mvpp2_txq_inc_get(struct mvpp2_txq_pcpu *txq_pcpu)
1338{
1339 txq_pcpu->txq_get_index++;
1340 if (txq_pcpu->txq_get_index == txq_pcpu->size)
1341 txq_pcpu->txq_get_index = 0;
1342}
1343
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001344static void mvpp2_txq_inc_put(struct mvpp2_port *port,
1345 struct mvpp2_txq_pcpu *txq_pcpu,
Marcin Wojtas71ce3912015-08-06 19:00:29 +02001346 struct sk_buff *skb,
1347 struct mvpp2_tx_desc *tx_desc)
Marcin Wojtas3f518502014-07-10 16:52:13 -03001348{
Thomas Petazzoni83544912016-12-21 11:28:49 +01001349 struct mvpp2_txq_pcpu_buf *tx_buf =
1350 txq_pcpu->buffs + txq_pcpu->txq_put_index;
1351 tx_buf->skb = skb;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001352 tx_buf->size = mvpp2_txdesc_size_get(port, tx_desc);
1353 tx_buf->dma = mvpp2_txdesc_dma_addr_get(port, tx_desc) +
1354 mvpp2_txdesc_offset_get(port, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001355 txq_pcpu->txq_put_index++;
1356 if (txq_pcpu->txq_put_index == txq_pcpu->size)
1357 txq_pcpu->txq_put_index = 0;
1358}
1359
1360/* Get number of physical egress port */
1361static inline int mvpp2_egress_port(struct mvpp2_port *port)
1362{
1363 return MVPP2_MAX_TCONT + port->id;
1364}
1365
1366/* Get number of physical TXQ */
1367static inline int mvpp2_txq_phys(int port, int txq)
1368{
1369 return (MVPP2_MAX_TCONT + port) * MVPP2_MAX_TXQ + txq;
1370}
1371
1372/* Parser configuration routines */
1373
1374/* Update parser tcam and sram hw entries */
1375static int mvpp2_prs_hw_write(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1376{
1377 int i;
1378
1379 if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1380 return -EINVAL;
1381
1382 /* Clear entry invalidation bit */
1383 pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] &= ~MVPP2_PRS_TCAM_INV_MASK;
1384
1385 /* Write tcam index - indirect access */
1386 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1387 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1388 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), pe->tcam.word[i]);
1389
1390 /* Write sram index - indirect access */
1391 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1392 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1393 mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), pe->sram.word[i]);
1394
1395 return 0;
1396}
1397
1398/* Read tcam entry from hw */
1399static int mvpp2_prs_hw_read(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1400{
1401 int i;
1402
1403 if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1404 return -EINVAL;
1405
1406 /* Write tcam index - indirect access */
1407 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1408
1409 pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] = mvpp2_read(priv,
1410 MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD));
1411 if (pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] & MVPP2_PRS_TCAM_INV_MASK)
1412 return MVPP2_PRS_TCAM_ENTRY_INVALID;
1413
1414 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1415 pe->tcam.word[i] = mvpp2_read(priv, MVPP2_PRS_TCAM_DATA_REG(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 pe->sram.word[i] = mvpp2_read(priv, MVPP2_PRS_SRAM_DATA_REG(i));
1421
1422 return 0;
1423}
1424
1425/* Invalidate tcam hw entry */
1426static void mvpp2_prs_hw_inv(struct mvpp2 *priv, int index)
1427{
1428 /* Write index - indirect access */
1429 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
1430 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD),
1431 MVPP2_PRS_TCAM_INV_MASK);
1432}
1433
1434/* Enable shadow table entry and set its lookup ID */
1435static void mvpp2_prs_shadow_set(struct mvpp2 *priv, int index, int lu)
1436{
1437 priv->prs_shadow[index].valid = true;
1438 priv->prs_shadow[index].lu = lu;
1439}
1440
1441/* Update ri fields in shadow table entry */
1442static void mvpp2_prs_shadow_ri_set(struct mvpp2 *priv, int index,
1443 unsigned int ri, unsigned int ri_mask)
1444{
1445 priv->prs_shadow[index].ri_mask = ri_mask;
1446 priv->prs_shadow[index].ri = ri;
1447}
1448
1449/* Update lookup field in tcam sw entry */
1450static void mvpp2_prs_tcam_lu_set(struct mvpp2_prs_entry *pe, unsigned int lu)
1451{
1452 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_LU_BYTE);
1453
1454 pe->tcam.byte[MVPP2_PRS_TCAM_LU_BYTE] = lu;
1455 pe->tcam.byte[enable_off] = MVPP2_PRS_LU_MASK;
1456}
1457
1458/* Update mask for single port in tcam sw entry */
1459static void mvpp2_prs_tcam_port_set(struct mvpp2_prs_entry *pe,
1460 unsigned int port, bool add)
1461{
1462 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1463
1464 if (add)
1465 pe->tcam.byte[enable_off] &= ~(1 << port);
1466 else
1467 pe->tcam.byte[enable_off] |= 1 << port;
1468}
1469
1470/* Update port map in tcam sw entry */
1471static void mvpp2_prs_tcam_port_map_set(struct mvpp2_prs_entry *pe,
1472 unsigned int ports)
1473{
1474 unsigned char port_mask = MVPP2_PRS_PORT_MASK;
1475 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1476
1477 pe->tcam.byte[MVPP2_PRS_TCAM_PORT_BYTE] = 0;
1478 pe->tcam.byte[enable_off] &= ~port_mask;
1479 pe->tcam.byte[enable_off] |= ~ports & MVPP2_PRS_PORT_MASK;
1480}
1481
1482/* Obtain port map from tcam sw entry */
1483static unsigned int mvpp2_prs_tcam_port_map_get(struct mvpp2_prs_entry *pe)
1484{
1485 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1486
1487 return ~(pe->tcam.byte[enable_off]) & MVPP2_PRS_PORT_MASK;
1488}
1489
1490/* Set byte of data and its enable bits in tcam sw entry */
1491static void mvpp2_prs_tcam_data_byte_set(struct mvpp2_prs_entry *pe,
1492 unsigned int offs, unsigned char byte,
1493 unsigned char enable)
1494{
1495 pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)] = byte;
1496 pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)] = enable;
1497}
1498
1499/* Get byte of data and its enable bits from tcam sw entry */
1500static void mvpp2_prs_tcam_data_byte_get(struct mvpp2_prs_entry *pe,
1501 unsigned int offs, unsigned char *byte,
1502 unsigned char *enable)
1503{
1504 *byte = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)];
1505 *enable = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)];
1506}
1507
1508/* Compare tcam data bytes with a pattern */
1509static bool mvpp2_prs_tcam_data_cmp(struct mvpp2_prs_entry *pe, int offs,
1510 u16 data)
1511{
1512 int off = MVPP2_PRS_TCAM_DATA_BYTE(offs);
1513 u16 tcam_data;
1514
1515 tcam_data = (8 << pe->tcam.byte[off + 1]) | pe->tcam.byte[off];
1516 if (tcam_data != data)
1517 return false;
1518 return true;
1519}
1520
1521/* Update ai bits in tcam sw entry */
1522static void mvpp2_prs_tcam_ai_update(struct mvpp2_prs_entry *pe,
1523 unsigned int bits, unsigned int enable)
1524{
1525 int i, ai_idx = MVPP2_PRS_TCAM_AI_BYTE;
1526
1527 for (i = 0; i < MVPP2_PRS_AI_BITS; i++) {
1528
1529 if (!(enable & BIT(i)))
1530 continue;
1531
1532 if (bits & BIT(i))
1533 pe->tcam.byte[ai_idx] |= 1 << i;
1534 else
1535 pe->tcam.byte[ai_idx] &= ~(1 << i);
1536 }
1537
1538 pe->tcam.byte[MVPP2_PRS_TCAM_EN_OFFS(ai_idx)] |= enable;
1539}
1540
1541/* Get ai bits from tcam sw entry */
1542static int mvpp2_prs_tcam_ai_get(struct mvpp2_prs_entry *pe)
1543{
1544 return pe->tcam.byte[MVPP2_PRS_TCAM_AI_BYTE];
1545}
1546
1547/* Set ethertype in tcam sw entry */
1548static void mvpp2_prs_match_etype(struct mvpp2_prs_entry *pe, int offset,
1549 unsigned short ethertype)
1550{
1551 mvpp2_prs_tcam_data_byte_set(pe, offset + 0, ethertype >> 8, 0xff);
1552 mvpp2_prs_tcam_data_byte_set(pe, offset + 1, ethertype & 0xff, 0xff);
1553}
1554
1555/* Set bits in sram sw entry */
1556static void mvpp2_prs_sram_bits_set(struct mvpp2_prs_entry *pe, int bit_num,
1557 int val)
1558{
1559 pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] |= (val << (bit_num % 8));
1560}
1561
1562/* Clear bits in sram sw entry */
1563static void mvpp2_prs_sram_bits_clear(struct mvpp2_prs_entry *pe, int bit_num,
1564 int val)
1565{
1566 pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] &= ~(val << (bit_num % 8));
1567}
1568
1569/* Update ri bits in sram sw entry */
1570static void mvpp2_prs_sram_ri_update(struct mvpp2_prs_entry *pe,
1571 unsigned int bits, unsigned int mask)
1572{
1573 unsigned int i;
1574
1575 for (i = 0; i < MVPP2_PRS_SRAM_RI_CTRL_BITS; i++) {
1576 int ri_off = MVPP2_PRS_SRAM_RI_OFFS;
1577
1578 if (!(mask & BIT(i)))
1579 continue;
1580
1581 if (bits & BIT(i))
1582 mvpp2_prs_sram_bits_set(pe, ri_off + i, 1);
1583 else
1584 mvpp2_prs_sram_bits_clear(pe, ri_off + i, 1);
1585
1586 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_RI_CTRL_OFFS + i, 1);
1587 }
1588}
1589
1590/* Obtain ri bits from sram sw entry */
1591static int mvpp2_prs_sram_ri_get(struct mvpp2_prs_entry *pe)
1592{
1593 return pe->sram.word[MVPP2_PRS_SRAM_RI_WORD];
1594}
1595
1596/* Update ai bits in sram sw entry */
1597static void mvpp2_prs_sram_ai_update(struct mvpp2_prs_entry *pe,
1598 unsigned int bits, unsigned int mask)
1599{
1600 unsigned int i;
1601 int ai_off = MVPP2_PRS_SRAM_AI_OFFS;
1602
1603 for (i = 0; i < MVPP2_PRS_SRAM_AI_CTRL_BITS; i++) {
1604
1605 if (!(mask & BIT(i)))
1606 continue;
1607
1608 if (bits & BIT(i))
1609 mvpp2_prs_sram_bits_set(pe, ai_off + i, 1);
1610 else
1611 mvpp2_prs_sram_bits_clear(pe, ai_off + i, 1);
1612
1613 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_AI_CTRL_OFFS + i, 1);
1614 }
1615}
1616
1617/* Read ai bits from sram sw entry */
1618static int mvpp2_prs_sram_ai_get(struct mvpp2_prs_entry *pe)
1619{
1620 u8 bits;
1621 int ai_off = MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_AI_OFFS);
1622 int ai_en_off = ai_off + 1;
1623 int ai_shift = MVPP2_PRS_SRAM_AI_OFFS % 8;
1624
1625 bits = (pe->sram.byte[ai_off] >> ai_shift) |
1626 (pe->sram.byte[ai_en_off] << (8 - ai_shift));
1627
1628 return bits;
1629}
1630
1631/* In sram sw entry set lookup ID field of the tcam key to be used in the next
1632 * lookup interation
1633 */
1634static void mvpp2_prs_sram_next_lu_set(struct mvpp2_prs_entry *pe,
1635 unsigned int lu)
1636{
1637 int sram_next_off = MVPP2_PRS_SRAM_NEXT_LU_OFFS;
1638
1639 mvpp2_prs_sram_bits_clear(pe, sram_next_off,
1640 MVPP2_PRS_SRAM_NEXT_LU_MASK);
1641 mvpp2_prs_sram_bits_set(pe, sram_next_off, lu);
1642}
1643
1644/* In the sram sw entry set sign and value of the next lookup offset
1645 * and the offset value generated to the classifier
1646 */
1647static void mvpp2_prs_sram_shift_set(struct mvpp2_prs_entry *pe, int shift,
1648 unsigned int op)
1649{
1650 /* Set sign */
1651 if (shift < 0) {
1652 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1653 shift = 0 - shift;
1654 } else {
1655 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1656 }
1657
1658 /* Set value */
1659 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_SHIFT_OFFS)] =
1660 (unsigned char)shift;
1661
1662 /* Reset and set operation */
1663 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS,
1664 MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK);
1665 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS, op);
1666
1667 /* Set base offset as current */
1668 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1669}
1670
1671/* In the sram sw entry set sign and value of the user defined offset
1672 * generated to the classifier
1673 */
1674static void mvpp2_prs_sram_offset_set(struct mvpp2_prs_entry *pe,
1675 unsigned int type, int offset,
1676 unsigned int op)
1677{
1678 /* Set sign */
1679 if (offset < 0) {
1680 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1681 offset = 0 - offset;
1682 } else {
1683 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1684 }
1685
1686 /* Set value */
1687 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_OFFS,
1688 MVPP2_PRS_SRAM_UDF_MASK);
1689 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_OFFS, offset);
1690 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1691 MVPP2_PRS_SRAM_UDF_BITS)] &=
1692 ~(MVPP2_PRS_SRAM_UDF_MASK >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1693 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1694 MVPP2_PRS_SRAM_UDF_BITS)] |=
1695 (offset >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1696
1697 /* Set offset type */
1698 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS,
1699 MVPP2_PRS_SRAM_UDF_TYPE_MASK);
1700 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS, type);
1701
1702 /* Set offset operation */
1703 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS,
1704 MVPP2_PRS_SRAM_OP_SEL_UDF_MASK);
1705 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS, op);
1706
1707 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1708 MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] &=
1709 ~(MVPP2_PRS_SRAM_OP_SEL_UDF_MASK >>
1710 (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1711
1712 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1713 MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] |=
1714 (op >> (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1715
1716 /* Set base offset as current */
1717 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1718}
1719
1720/* Find parser flow entry */
1721static struct mvpp2_prs_entry *mvpp2_prs_flow_find(struct mvpp2 *priv, int flow)
1722{
1723 struct mvpp2_prs_entry *pe;
1724 int tid;
1725
1726 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
1727 if (!pe)
1728 return NULL;
1729 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
1730
1731 /* Go through the all entires with MVPP2_PRS_LU_FLOWS */
1732 for (tid = MVPP2_PRS_TCAM_SRAM_SIZE - 1; tid >= 0; tid--) {
1733 u8 bits;
1734
1735 if (!priv->prs_shadow[tid].valid ||
1736 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_FLOWS)
1737 continue;
1738
1739 pe->index = tid;
1740 mvpp2_prs_hw_read(priv, pe);
1741 bits = mvpp2_prs_sram_ai_get(pe);
1742
1743 /* Sram store classification lookup ID in AI bits [5:0] */
1744 if ((bits & MVPP2_PRS_FLOW_ID_MASK) == flow)
1745 return pe;
1746 }
1747 kfree(pe);
1748
1749 return NULL;
1750}
1751
1752/* Return first free tcam index, seeking from start to end */
1753static int mvpp2_prs_tcam_first_free(struct mvpp2 *priv, unsigned char start,
1754 unsigned char end)
1755{
1756 int tid;
1757
1758 if (start > end)
1759 swap(start, end);
1760
1761 if (end >= MVPP2_PRS_TCAM_SRAM_SIZE)
1762 end = MVPP2_PRS_TCAM_SRAM_SIZE - 1;
1763
1764 for (tid = start; tid <= end; tid++) {
1765 if (!priv->prs_shadow[tid].valid)
1766 return tid;
1767 }
1768
1769 return -EINVAL;
1770}
1771
1772/* Enable/disable dropping all mac da's */
1773static void mvpp2_prs_mac_drop_all_set(struct mvpp2 *priv, int port, bool add)
1774{
1775 struct mvpp2_prs_entry pe;
1776
1777 if (priv->prs_shadow[MVPP2_PE_DROP_ALL].valid) {
1778 /* Entry exist - update port only */
1779 pe.index = MVPP2_PE_DROP_ALL;
1780 mvpp2_prs_hw_read(priv, &pe);
1781 } else {
1782 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001783 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001784 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1785 pe.index = MVPP2_PE_DROP_ALL;
1786
1787 /* Non-promiscuous mode for all ports - DROP unknown packets */
1788 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
1789 MVPP2_PRS_RI_DROP_MASK);
1790
1791 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1792 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1793
1794 /* Update shadow table */
1795 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1796
1797 /* Mask all ports */
1798 mvpp2_prs_tcam_port_map_set(&pe, 0);
1799 }
1800
1801 /* Update port mask */
1802 mvpp2_prs_tcam_port_set(&pe, port, add);
1803
1804 mvpp2_prs_hw_write(priv, &pe);
1805}
1806
1807/* Set port to promiscuous mode */
1808static void mvpp2_prs_mac_promisc_set(struct mvpp2 *priv, int port, bool add)
1809{
1810 struct mvpp2_prs_entry pe;
1811
Joe Perchesdbedd442015-03-06 20:49:12 -08001812 /* Promiscuous mode - Accept unknown packets */
Marcin Wojtas3f518502014-07-10 16:52:13 -03001813
1814 if (priv->prs_shadow[MVPP2_PE_MAC_PROMISCUOUS].valid) {
1815 /* Entry exist - update port only */
1816 pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1817 mvpp2_prs_hw_read(priv, &pe);
1818 } else {
1819 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001820 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001821 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1822 pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1823
1824 /* Continue - set next lookup */
1825 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
1826
1827 /* Set result info bits */
1828 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_UCAST,
1829 MVPP2_PRS_RI_L2_CAST_MASK);
1830
1831 /* Shift to ethertype */
1832 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
1833 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1834
1835 /* Mask all ports */
1836 mvpp2_prs_tcam_port_map_set(&pe, 0);
1837
1838 /* Update shadow table */
1839 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1840 }
1841
1842 /* Update port mask */
1843 mvpp2_prs_tcam_port_set(&pe, port, add);
1844
1845 mvpp2_prs_hw_write(priv, &pe);
1846}
1847
1848/* Accept multicast */
1849static void mvpp2_prs_mac_multi_set(struct mvpp2 *priv, int port, int index,
1850 bool add)
1851{
1852 struct mvpp2_prs_entry pe;
1853 unsigned char da_mc;
1854
1855 /* Ethernet multicast address first byte is
1856 * 0x01 for IPv4 and 0x33 for IPv6
1857 */
1858 da_mc = (index == MVPP2_PE_MAC_MC_ALL) ? 0x01 : 0x33;
1859
1860 if (priv->prs_shadow[index].valid) {
1861 /* Entry exist - update port only */
1862 pe.index = index;
1863 mvpp2_prs_hw_read(priv, &pe);
1864 } else {
1865 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001866 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001867 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1868 pe.index = index;
1869
1870 /* Continue - set next lookup */
1871 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
1872
1873 /* Set result info bits */
1874 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_MCAST,
1875 MVPP2_PRS_RI_L2_CAST_MASK);
1876
1877 /* Update tcam entry data first byte */
1878 mvpp2_prs_tcam_data_byte_set(&pe, 0, da_mc, 0xff);
1879
1880 /* Shift to ethertype */
1881 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
1882 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1883
1884 /* Mask all ports */
1885 mvpp2_prs_tcam_port_map_set(&pe, 0);
1886
1887 /* Update shadow table */
1888 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1889 }
1890
1891 /* Update port mask */
1892 mvpp2_prs_tcam_port_set(&pe, port, add);
1893
1894 mvpp2_prs_hw_write(priv, &pe);
1895}
1896
1897/* Set entry for dsa packets */
1898static void mvpp2_prs_dsa_tag_set(struct mvpp2 *priv, int port, bool add,
1899 bool tagged, bool extend)
1900{
1901 struct mvpp2_prs_entry pe;
1902 int tid, shift;
1903
1904 if (extend) {
1905 tid = tagged ? MVPP2_PE_EDSA_TAGGED : MVPP2_PE_EDSA_UNTAGGED;
1906 shift = 8;
1907 } else {
1908 tid = tagged ? MVPP2_PE_DSA_TAGGED : MVPP2_PE_DSA_UNTAGGED;
1909 shift = 4;
1910 }
1911
1912 if (priv->prs_shadow[tid].valid) {
1913 /* Entry exist - update port only */
1914 pe.index = tid;
1915 mvpp2_prs_hw_read(priv, &pe);
1916 } else {
1917 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001918 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001919 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
1920 pe.index = tid;
1921
1922 /* Shift 4 bytes if DSA tag or 8 bytes in case of EDSA tag*/
1923 mvpp2_prs_sram_shift_set(&pe, shift,
1924 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1925
1926 /* Update shadow table */
1927 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
1928
1929 if (tagged) {
1930 /* Set tagged bit in DSA tag */
1931 mvpp2_prs_tcam_data_byte_set(&pe, 0,
1932 MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
1933 MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
1934 /* Clear all ai bits for next iteration */
1935 mvpp2_prs_sram_ai_update(&pe, 0,
1936 MVPP2_PRS_SRAM_AI_MASK);
1937 /* If packet is tagged continue check vlans */
1938 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
1939 } else {
1940 /* Set result info bits to 'no vlans' */
1941 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
1942 MVPP2_PRS_RI_VLAN_MASK);
1943 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
1944 }
1945
1946 /* Mask all ports */
1947 mvpp2_prs_tcam_port_map_set(&pe, 0);
1948 }
1949
1950 /* Update port mask */
1951 mvpp2_prs_tcam_port_set(&pe, port, add);
1952
1953 mvpp2_prs_hw_write(priv, &pe);
1954}
1955
1956/* Set entry for dsa ethertype */
1957static void mvpp2_prs_dsa_tag_ethertype_set(struct mvpp2 *priv, int port,
1958 bool add, bool tagged, bool extend)
1959{
1960 struct mvpp2_prs_entry pe;
1961 int tid, shift, port_mask;
1962
1963 if (extend) {
1964 tid = tagged ? MVPP2_PE_ETYPE_EDSA_TAGGED :
1965 MVPP2_PE_ETYPE_EDSA_UNTAGGED;
1966 port_mask = 0;
1967 shift = 8;
1968 } else {
1969 tid = tagged ? MVPP2_PE_ETYPE_DSA_TAGGED :
1970 MVPP2_PE_ETYPE_DSA_UNTAGGED;
1971 port_mask = MVPP2_PRS_PORT_MASK;
1972 shift = 4;
1973 }
1974
1975 if (priv->prs_shadow[tid].valid) {
1976 /* Entry exist - update port only */
1977 pe.index = tid;
1978 mvpp2_prs_hw_read(priv, &pe);
1979 } else {
1980 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001981 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001982 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
1983 pe.index = tid;
1984
1985 /* Set ethertype */
1986 mvpp2_prs_match_etype(&pe, 0, ETH_P_EDSA);
1987 mvpp2_prs_match_etype(&pe, 2, 0);
1988
1989 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DSA_MASK,
1990 MVPP2_PRS_RI_DSA_MASK);
1991 /* Shift ethertype + 2 byte reserved + tag*/
1992 mvpp2_prs_sram_shift_set(&pe, 2 + MVPP2_ETH_TYPE_LEN + shift,
1993 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1994
1995 /* Update shadow table */
1996 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
1997
1998 if (tagged) {
1999 /* Set tagged bit in DSA tag */
2000 mvpp2_prs_tcam_data_byte_set(&pe,
2001 MVPP2_ETH_TYPE_LEN + 2 + 3,
2002 MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
2003 MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
2004 /* Clear all ai bits for next iteration */
2005 mvpp2_prs_sram_ai_update(&pe, 0,
2006 MVPP2_PRS_SRAM_AI_MASK);
2007 /* If packet is tagged continue check vlans */
2008 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2009 } else {
2010 /* Set result info bits to 'no vlans' */
2011 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
2012 MVPP2_PRS_RI_VLAN_MASK);
2013 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2014 }
2015 /* Mask/unmask all ports, depending on dsa type */
2016 mvpp2_prs_tcam_port_map_set(&pe, port_mask);
2017 }
2018
2019 /* Update port mask */
2020 mvpp2_prs_tcam_port_set(&pe, port, add);
2021
2022 mvpp2_prs_hw_write(priv, &pe);
2023}
2024
2025/* Search for existing single/triple vlan entry */
2026static struct mvpp2_prs_entry *mvpp2_prs_vlan_find(struct mvpp2 *priv,
2027 unsigned short tpid, int ai)
2028{
2029 struct mvpp2_prs_entry *pe;
2030 int tid;
2031
2032 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2033 if (!pe)
2034 return NULL;
2035 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2036
2037 /* Go through the all entries with MVPP2_PRS_LU_VLAN */
2038 for (tid = MVPP2_PE_FIRST_FREE_TID;
2039 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
2040 unsigned int ri_bits, ai_bits;
2041 bool match;
2042
2043 if (!priv->prs_shadow[tid].valid ||
2044 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
2045 continue;
2046
2047 pe->index = tid;
2048
2049 mvpp2_prs_hw_read(priv, pe);
2050 match = mvpp2_prs_tcam_data_cmp(pe, 0, swab16(tpid));
2051 if (!match)
2052 continue;
2053
2054 /* Get vlan type */
2055 ri_bits = mvpp2_prs_sram_ri_get(pe);
2056 ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
2057
2058 /* Get current ai value from tcam */
2059 ai_bits = mvpp2_prs_tcam_ai_get(pe);
2060 /* Clear double vlan bit */
2061 ai_bits &= ~MVPP2_PRS_DBL_VLAN_AI_BIT;
2062
2063 if (ai != ai_bits)
2064 continue;
2065
2066 if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
2067 ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
2068 return pe;
2069 }
2070 kfree(pe);
2071
2072 return NULL;
2073}
2074
2075/* Add/update single/triple vlan entry */
2076static int mvpp2_prs_vlan_add(struct mvpp2 *priv, unsigned short tpid, int ai,
2077 unsigned int port_map)
2078{
2079 struct mvpp2_prs_entry *pe;
2080 int tid_aux, tid;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302081 int ret = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002082
2083 pe = mvpp2_prs_vlan_find(priv, tpid, ai);
2084
2085 if (!pe) {
2086 /* Create new tcam entry */
2087 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_LAST_FREE_TID,
2088 MVPP2_PE_FIRST_FREE_TID);
2089 if (tid < 0)
2090 return tid;
2091
2092 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2093 if (!pe)
2094 return -ENOMEM;
2095
2096 /* Get last double vlan tid */
2097 for (tid_aux = MVPP2_PE_LAST_FREE_TID;
2098 tid_aux >= MVPP2_PE_FIRST_FREE_TID; tid_aux--) {
2099 unsigned int ri_bits;
2100
2101 if (!priv->prs_shadow[tid_aux].valid ||
2102 priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
2103 continue;
2104
2105 pe->index = tid_aux;
2106 mvpp2_prs_hw_read(priv, pe);
2107 ri_bits = mvpp2_prs_sram_ri_get(pe);
2108 if ((ri_bits & MVPP2_PRS_RI_VLAN_MASK) ==
2109 MVPP2_PRS_RI_VLAN_DOUBLE)
2110 break;
2111 }
2112
Sudip Mukherjee43737472014-11-01 16:59:34 +05302113 if (tid <= tid_aux) {
2114 ret = -EINVAL;
Markus Elfringf9fd0e32017-04-17 13:50:35 +02002115 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302116 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002117
Markus Elfringbd6aaf52017-04-17 10:40:32 +02002118 memset(pe, 0, sizeof(*pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002119 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2120 pe->index = tid;
2121
2122 mvpp2_prs_match_etype(pe, 0, tpid);
2123
2124 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_L2);
2125 /* Shift 4 bytes - skip 1 vlan tag */
2126 mvpp2_prs_sram_shift_set(pe, MVPP2_VLAN_TAG_LEN,
2127 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2128 /* Clear all ai bits for next iteration */
2129 mvpp2_prs_sram_ai_update(pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2130
2131 if (ai == MVPP2_PRS_SINGLE_VLAN_AI) {
2132 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_SINGLE,
2133 MVPP2_PRS_RI_VLAN_MASK);
2134 } else {
2135 ai |= MVPP2_PRS_DBL_VLAN_AI_BIT;
2136 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_TRIPLE,
2137 MVPP2_PRS_RI_VLAN_MASK);
2138 }
2139 mvpp2_prs_tcam_ai_update(pe, ai, MVPP2_PRS_SRAM_AI_MASK);
2140
2141 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_VLAN);
2142 }
2143 /* Update ports' mask */
2144 mvpp2_prs_tcam_port_map_set(pe, port_map);
2145
2146 mvpp2_prs_hw_write(priv, pe);
Markus Elfringf9fd0e32017-04-17 13:50:35 +02002147free_pe:
Marcin Wojtas3f518502014-07-10 16:52:13 -03002148 kfree(pe);
2149
Sudip Mukherjee43737472014-11-01 16:59:34 +05302150 return ret;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002151}
2152
2153/* Get first free double vlan ai number */
2154static int mvpp2_prs_double_vlan_ai_free_get(struct mvpp2 *priv)
2155{
2156 int i;
2157
2158 for (i = 1; i < MVPP2_PRS_DBL_VLANS_MAX; i++) {
2159 if (!priv->prs_double_vlans[i])
2160 return i;
2161 }
2162
2163 return -EINVAL;
2164}
2165
2166/* Search for existing double vlan entry */
2167static struct mvpp2_prs_entry *mvpp2_prs_double_vlan_find(struct mvpp2 *priv,
2168 unsigned short tpid1,
2169 unsigned short tpid2)
2170{
2171 struct mvpp2_prs_entry *pe;
2172 int tid;
2173
2174 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2175 if (!pe)
2176 return NULL;
2177 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2178
2179 /* Go through the all entries with MVPP2_PRS_LU_VLAN */
2180 for (tid = MVPP2_PE_FIRST_FREE_TID;
2181 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
2182 unsigned int ri_mask;
2183 bool match;
2184
2185 if (!priv->prs_shadow[tid].valid ||
2186 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
2187 continue;
2188
2189 pe->index = tid;
2190 mvpp2_prs_hw_read(priv, pe);
2191
2192 match = mvpp2_prs_tcam_data_cmp(pe, 0, swab16(tpid1))
2193 && mvpp2_prs_tcam_data_cmp(pe, 4, swab16(tpid2));
2194
2195 if (!match)
2196 continue;
2197
2198 ri_mask = mvpp2_prs_sram_ri_get(pe) & MVPP2_PRS_RI_VLAN_MASK;
2199 if (ri_mask == MVPP2_PRS_RI_VLAN_DOUBLE)
2200 return pe;
2201 }
2202 kfree(pe);
2203
2204 return NULL;
2205}
2206
2207/* Add or update double vlan entry */
2208static int mvpp2_prs_double_vlan_add(struct mvpp2 *priv, unsigned short tpid1,
2209 unsigned short tpid2,
2210 unsigned int port_map)
2211{
2212 struct mvpp2_prs_entry *pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302213 int tid_aux, tid, ai, ret = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002214
2215 pe = mvpp2_prs_double_vlan_find(priv, tpid1, tpid2);
2216
2217 if (!pe) {
2218 /* Create new tcam entry */
2219 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2220 MVPP2_PE_LAST_FREE_TID);
2221 if (tid < 0)
2222 return tid;
2223
2224 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2225 if (!pe)
2226 return -ENOMEM;
2227
2228 /* Set ai value for new double vlan entry */
2229 ai = mvpp2_prs_double_vlan_ai_free_get(priv);
Sudip Mukherjee43737472014-11-01 16:59:34 +05302230 if (ai < 0) {
2231 ret = ai;
Markus Elfringc9a7e122017-04-17 13:03:49 +02002232 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302233 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002234
2235 /* Get first single/triple vlan tid */
2236 for (tid_aux = MVPP2_PE_FIRST_FREE_TID;
2237 tid_aux <= MVPP2_PE_LAST_FREE_TID; tid_aux++) {
2238 unsigned int ri_bits;
2239
2240 if (!priv->prs_shadow[tid_aux].valid ||
2241 priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
2242 continue;
2243
2244 pe->index = tid_aux;
2245 mvpp2_prs_hw_read(priv, pe);
2246 ri_bits = mvpp2_prs_sram_ri_get(pe);
2247 ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
2248 if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
2249 ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
2250 break;
2251 }
2252
Sudip Mukherjee43737472014-11-01 16:59:34 +05302253 if (tid >= tid_aux) {
2254 ret = -ERANGE;
Markus Elfringc9a7e122017-04-17 13:03:49 +02002255 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302256 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002257
Markus Elfringbd6aaf52017-04-17 10:40:32 +02002258 memset(pe, 0, sizeof(*pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002259 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2260 pe->index = tid;
2261
2262 priv->prs_double_vlans[ai] = true;
2263
2264 mvpp2_prs_match_etype(pe, 0, tpid1);
2265 mvpp2_prs_match_etype(pe, 4, tpid2);
2266
2267 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_VLAN);
2268 /* Shift 8 bytes - skip 2 vlan tags */
2269 mvpp2_prs_sram_shift_set(pe, 2 * MVPP2_VLAN_TAG_LEN,
2270 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2271 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_DOUBLE,
2272 MVPP2_PRS_RI_VLAN_MASK);
2273 mvpp2_prs_sram_ai_update(pe, ai | MVPP2_PRS_DBL_VLAN_AI_BIT,
2274 MVPP2_PRS_SRAM_AI_MASK);
2275
2276 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_VLAN);
2277 }
2278
2279 /* Update ports' mask */
2280 mvpp2_prs_tcam_port_map_set(pe, port_map);
2281 mvpp2_prs_hw_write(priv, pe);
Markus Elfringc9a7e122017-04-17 13:03:49 +02002282free_pe:
Marcin Wojtas3f518502014-07-10 16:52:13 -03002283 kfree(pe);
Sudip Mukherjee43737472014-11-01 16:59:34 +05302284 return ret;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002285}
2286
2287/* IPv4 header parsing for fragmentation and L4 offset */
2288static int mvpp2_prs_ip4_proto(struct mvpp2 *priv, unsigned short proto,
2289 unsigned int ri, unsigned int ri_mask)
2290{
2291 struct mvpp2_prs_entry pe;
2292 int tid;
2293
2294 if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
2295 (proto != IPPROTO_IGMP))
2296 return -EINVAL;
2297
2298 /* Fragmented packet */
2299 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2300 MVPP2_PE_LAST_FREE_TID);
2301 if (tid < 0)
2302 return tid;
2303
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002304 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002305 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
2306 pe.index = tid;
2307
2308 /* Set next lu to IPv4 */
2309 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2310 mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2311 /* Set L4 offset */
2312 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
2313 sizeof(struct iphdr) - 4,
2314 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2315 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
2316 MVPP2_PRS_IPV4_DIP_AI_BIT);
2317 mvpp2_prs_sram_ri_update(&pe, ri | MVPP2_PRS_RI_IP_FRAG_MASK,
2318 ri_mask | MVPP2_PRS_RI_IP_FRAG_MASK);
2319
2320 mvpp2_prs_tcam_data_byte_set(&pe, 5, proto, MVPP2_PRS_TCAM_PROTO_MASK);
2321 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
2322 /* Unmask all ports */
2323 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2324
2325 /* Update shadow table and hw entry */
2326 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2327 mvpp2_prs_hw_write(priv, &pe);
2328
2329 /* Not fragmented packet */
2330 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2331 MVPP2_PE_LAST_FREE_TID);
2332 if (tid < 0)
2333 return tid;
2334
2335 pe.index = tid;
2336 /* Clear ri before updating */
2337 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2338 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2339 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
2340
2341 mvpp2_prs_tcam_data_byte_set(&pe, 2, 0x00, MVPP2_PRS_TCAM_PROTO_MASK_L);
2342 mvpp2_prs_tcam_data_byte_set(&pe, 3, 0x00, MVPP2_PRS_TCAM_PROTO_MASK);
2343
2344 /* Update shadow table and hw entry */
2345 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2346 mvpp2_prs_hw_write(priv, &pe);
2347
2348 return 0;
2349}
2350
2351/* IPv4 L3 multicast or broadcast */
2352static int mvpp2_prs_ip4_cast(struct mvpp2 *priv, unsigned short l3_cast)
2353{
2354 struct mvpp2_prs_entry pe;
2355 int mask, tid;
2356
2357 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2358 MVPP2_PE_LAST_FREE_TID);
2359 if (tid < 0)
2360 return tid;
2361
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002362 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002363 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
2364 pe.index = tid;
2365
2366 switch (l3_cast) {
2367 case MVPP2_PRS_L3_MULTI_CAST:
2368 mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV4_MC,
2369 MVPP2_PRS_IPV4_MC_MASK);
2370 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
2371 MVPP2_PRS_RI_L3_ADDR_MASK);
2372 break;
2373 case MVPP2_PRS_L3_BROAD_CAST:
2374 mask = MVPP2_PRS_IPV4_BC_MASK;
2375 mvpp2_prs_tcam_data_byte_set(&pe, 0, mask, mask);
2376 mvpp2_prs_tcam_data_byte_set(&pe, 1, mask, mask);
2377 mvpp2_prs_tcam_data_byte_set(&pe, 2, mask, mask);
2378 mvpp2_prs_tcam_data_byte_set(&pe, 3, mask, mask);
2379 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_BCAST,
2380 MVPP2_PRS_RI_L3_ADDR_MASK);
2381 break;
2382 default:
2383 return -EINVAL;
2384 }
2385
2386 /* Finished: go to flowid generation */
2387 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2388 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2389
2390 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
2391 MVPP2_PRS_IPV4_DIP_AI_BIT);
2392 /* Unmask all ports */
2393 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2394
2395 /* Update shadow table and hw entry */
2396 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2397 mvpp2_prs_hw_write(priv, &pe);
2398
2399 return 0;
2400}
2401
2402/* Set entries for protocols over IPv6 */
2403static int mvpp2_prs_ip6_proto(struct mvpp2 *priv, unsigned short proto,
2404 unsigned int ri, unsigned int ri_mask)
2405{
2406 struct mvpp2_prs_entry pe;
2407 int tid;
2408
2409 if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
2410 (proto != IPPROTO_ICMPV6) && (proto != IPPROTO_IPIP))
2411 return -EINVAL;
2412
2413 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2414 MVPP2_PE_LAST_FREE_TID);
2415 if (tid < 0)
2416 return tid;
2417
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002418 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002419 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2420 pe.index = tid;
2421
2422 /* Finished: go to flowid generation */
2423 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2424 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2425 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
2426 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
2427 sizeof(struct ipv6hdr) - 6,
2428 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2429
2430 mvpp2_prs_tcam_data_byte_set(&pe, 0, proto, MVPP2_PRS_TCAM_PROTO_MASK);
2431 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
2432 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2433 /* Unmask all ports */
2434 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2435
2436 /* Write HW */
2437 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
2438 mvpp2_prs_hw_write(priv, &pe);
2439
2440 return 0;
2441}
2442
2443/* IPv6 L3 multicast entry */
2444static int mvpp2_prs_ip6_cast(struct mvpp2 *priv, unsigned short l3_cast)
2445{
2446 struct mvpp2_prs_entry pe;
2447 int tid;
2448
2449 if (l3_cast != MVPP2_PRS_L3_MULTI_CAST)
2450 return -EINVAL;
2451
2452 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2453 MVPP2_PE_LAST_FREE_TID);
2454 if (tid < 0)
2455 return tid;
2456
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002457 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002458 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2459 pe.index = tid;
2460
2461 /* Finished: go to flowid generation */
2462 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2463 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
2464 MVPP2_PRS_RI_L3_ADDR_MASK);
2465 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
2466 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2467 /* Shift back to IPv6 NH */
2468 mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2469
2470 mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV6_MC,
2471 MVPP2_PRS_IPV6_MC_MASK);
2472 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2473 /* Unmask all ports */
2474 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2475
2476 /* Update shadow table and hw entry */
2477 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
2478 mvpp2_prs_hw_write(priv, &pe);
2479
2480 return 0;
2481}
2482
2483/* Parser per-port initialization */
2484static void mvpp2_prs_hw_port_init(struct mvpp2 *priv, int port, int lu_first,
2485 int lu_max, int offset)
2486{
2487 u32 val;
2488
2489 /* Set lookup ID */
2490 val = mvpp2_read(priv, MVPP2_PRS_INIT_LOOKUP_REG);
2491 val &= ~MVPP2_PRS_PORT_LU_MASK(port);
2492 val |= MVPP2_PRS_PORT_LU_VAL(port, lu_first);
2493 mvpp2_write(priv, MVPP2_PRS_INIT_LOOKUP_REG, val);
2494
2495 /* Set maximum number of loops for packet received from port */
2496 val = mvpp2_read(priv, MVPP2_PRS_MAX_LOOP_REG(port));
2497 val &= ~MVPP2_PRS_MAX_LOOP_MASK(port);
2498 val |= MVPP2_PRS_MAX_LOOP_VAL(port, lu_max);
2499 mvpp2_write(priv, MVPP2_PRS_MAX_LOOP_REG(port), val);
2500
2501 /* Set initial offset for packet header extraction for the first
2502 * searching loop
2503 */
2504 val = mvpp2_read(priv, MVPP2_PRS_INIT_OFFS_REG(port));
2505 val &= ~MVPP2_PRS_INIT_OFF_MASK(port);
2506 val |= MVPP2_PRS_INIT_OFF_VAL(port, offset);
2507 mvpp2_write(priv, MVPP2_PRS_INIT_OFFS_REG(port), val);
2508}
2509
2510/* Default flow entries initialization for all ports */
2511static void mvpp2_prs_def_flow_init(struct mvpp2 *priv)
2512{
2513 struct mvpp2_prs_entry pe;
2514 int port;
2515
2516 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002517 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002518 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2519 pe.index = MVPP2_PE_FIRST_DEFAULT_FLOW - port;
2520
2521 /* Mask all ports */
2522 mvpp2_prs_tcam_port_map_set(&pe, 0);
2523
2524 /* Set flow ID*/
2525 mvpp2_prs_sram_ai_update(&pe, port, MVPP2_PRS_FLOW_ID_MASK);
2526 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
2527
2528 /* Update shadow table and hw entry */
2529 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_FLOWS);
2530 mvpp2_prs_hw_write(priv, &pe);
2531 }
2532}
2533
2534/* Set default entry for Marvell Header field */
2535static void mvpp2_prs_mh_init(struct mvpp2 *priv)
2536{
2537 struct mvpp2_prs_entry pe;
2538
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002539 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002540
2541 pe.index = MVPP2_PE_MH_DEFAULT;
2542 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MH);
2543 mvpp2_prs_sram_shift_set(&pe, MVPP2_MH_SIZE,
2544 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2545 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_MAC);
2546
2547 /* Unmask all ports */
2548 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2549
2550 /* Update shadow table and hw entry */
2551 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MH);
2552 mvpp2_prs_hw_write(priv, &pe);
2553}
2554
2555/* Set default entires (place holder) for promiscuous, non-promiscuous and
2556 * multicast MAC addresses
2557 */
2558static void mvpp2_prs_mac_init(struct mvpp2 *priv)
2559{
2560 struct mvpp2_prs_entry pe;
2561
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002562 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002563
2564 /* Non-promiscuous mode for all ports - DROP unknown packets */
2565 pe.index = MVPP2_PE_MAC_NON_PROMISCUOUS;
2566 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
2567
2568 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
2569 MVPP2_PRS_RI_DROP_MASK);
2570 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2571 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2572
2573 /* Unmask all ports */
2574 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2575
2576 /* Update shadow table and hw entry */
2577 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2578 mvpp2_prs_hw_write(priv, &pe);
2579
2580 /* place holders only - no ports */
2581 mvpp2_prs_mac_drop_all_set(priv, 0, false);
2582 mvpp2_prs_mac_promisc_set(priv, 0, false);
2583 mvpp2_prs_mac_multi_set(priv, MVPP2_PE_MAC_MC_ALL, 0, false);
2584 mvpp2_prs_mac_multi_set(priv, MVPP2_PE_MAC_MC_IP6, 0, false);
2585}
2586
2587/* Set default entries for various types of dsa packets */
2588static void mvpp2_prs_dsa_init(struct mvpp2 *priv)
2589{
2590 struct mvpp2_prs_entry pe;
2591
2592 /* None tagged EDSA entry - place holder */
2593 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
2594 MVPP2_PRS_EDSA);
2595
2596 /* Tagged EDSA entry - place holder */
2597 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
2598
2599 /* None tagged DSA entry - place holder */
2600 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
2601 MVPP2_PRS_DSA);
2602
2603 /* Tagged DSA entry - place holder */
2604 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
2605
2606 /* None tagged EDSA ethertype entry - place holder*/
2607 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
2608 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
2609
2610 /* Tagged EDSA ethertype entry - place holder*/
2611 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
2612 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
2613
2614 /* None tagged DSA ethertype entry */
2615 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
2616 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
2617
2618 /* Tagged DSA ethertype entry */
2619 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
2620 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
2621
2622 /* Set default entry, in case DSA or EDSA tag not found */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002623 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002624 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
2625 pe.index = MVPP2_PE_DSA_DEFAULT;
2626 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2627
2628 /* Shift 0 bytes */
2629 mvpp2_prs_sram_shift_set(&pe, 0, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2630 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2631
2632 /* Clear all sram ai bits for next iteration */
2633 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2634
2635 /* Unmask all ports */
2636 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2637
2638 mvpp2_prs_hw_write(priv, &pe);
2639}
2640
2641/* Match basic ethertypes */
2642static int mvpp2_prs_etype_init(struct mvpp2 *priv)
2643{
2644 struct mvpp2_prs_entry pe;
2645 int tid;
2646
2647 /* Ethertype: PPPoE */
2648 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2649 MVPP2_PE_LAST_FREE_TID);
2650 if (tid < 0)
2651 return tid;
2652
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002653 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002654 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2655 pe.index = tid;
2656
2657 mvpp2_prs_match_etype(&pe, 0, ETH_P_PPP_SES);
2658
2659 mvpp2_prs_sram_shift_set(&pe, MVPP2_PPPOE_HDR_SIZE,
2660 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2661 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2662 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_PPPOE_MASK,
2663 MVPP2_PRS_RI_PPPOE_MASK);
2664
2665 /* Update shadow table and hw entry */
2666 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2667 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2668 priv->prs_shadow[pe.index].finish = false;
2669 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_PPPOE_MASK,
2670 MVPP2_PRS_RI_PPPOE_MASK);
2671 mvpp2_prs_hw_write(priv, &pe);
2672
2673 /* Ethertype: ARP */
2674 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2675 MVPP2_PE_LAST_FREE_TID);
2676 if (tid < 0)
2677 return tid;
2678
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002679 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002680 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2681 pe.index = tid;
2682
2683 mvpp2_prs_match_etype(&pe, 0, ETH_P_ARP);
2684
2685 /* Generate flow in the next iteration*/
2686 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2687 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2688 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_ARP,
2689 MVPP2_PRS_RI_L3_PROTO_MASK);
2690 /* Set L3 offset */
2691 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2692 MVPP2_ETH_TYPE_LEN,
2693 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2694
2695 /* Update shadow table and hw entry */
2696 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2697 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2698 priv->prs_shadow[pe.index].finish = true;
2699 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_ARP,
2700 MVPP2_PRS_RI_L3_PROTO_MASK);
2701 mvpp2_prs_hw_write(priv, &pe);
2702
2703 /* Ethertype: LBTD */
2704 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2705 MVPP2_PE_LAST_FREE_TID);
2706 if (tid < 0)
2707 return tid;
2708
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002709 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002710 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2711 pe.index = tid;
2712
2713 mvpp2_prs_match_etype(&pe, 0, MVPP2_IP_LBDT_TYPE);
2714
2715 /* Generate flow in the next iteration*/
2716 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2717 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2718 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2719 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2720 MVPP2_PRS_RI_CPU_CODE_MASK |
2721 MVPP2_PRS_RI_UDF3_MASK);
2722 /* Set L3 offset */
2723 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2724 MVPP2_ETH_TYPE_LEN,
2725 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2726
2727 /* Update shadow table and hw entry */
2728 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2729 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2730 priv->prs_shadow[pe.index].finish = true;
2731 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2732 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2733 MVPP2_PRS_RI_CPU_CODE_MASK |
2734 MVPP2_PRS_RI_UDF3_MASK);
2735 mvpp2_prs_hw_write(priv, &pe);
2736
2737 /* Ethertype: IPv4 without options */
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, ETH_P_IP);
2748 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2749 MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
2750 MVPP2_PRS_IPV4_HEAD_MASK |
2751 MVPP2_PRS_IPV4_IHL_MASK);
2752
2753 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2754 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
2755 MVPP2_PRS_RI_L3_PROTO_MASK);
2756 /* Skip eth_type + 4 bytes of IP header */
2757 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
2758 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2759 /* Set L3 offset */
2760 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2761 MVPP2_ETH_TYPE_LEN,
2762 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2763
2764 /* Update shadow table and hw entry */
2765 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2766 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2767 priv->prs_shadow[pe.index].finish = false;
2768 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4,
2769 MVPP2_PRS_RI_L3_PROTO_MASK);
2770 mvpp2_prs_hw_write(priv, &pe);
2771
2772 /* Ethertype: IPv4 with options */
2773 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2774 MVPP2_PE_LAST_FREE_TID);
2775 if (tid < 0)
2776 return tid;
2777
2778 pe.index = tid;
2779
2780 /* Clear tcam data before updating */
2781 pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(MVPP2_ETH_TYPE_LEN)] = 0x0;
2782 pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(MVPP2_ETH_TYPE_LEN)] = 0x0;
2783
2784 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2785 MVPP2_PRS_IPV4_HEAD,
2786 MVPP2_PRS_IPV4_HEAD_MASK);
2787
2788 /* Clear ri before updating */
2789 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2790 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2791 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
2792 MVPP2_PRS_RI_L3_PROTO_MASK);
2793
2794 /* Update shadow table and hw entry */
2795 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2796 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2797 priv->prs_shadow[pe.index].finish = false;
2798 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4_OPT,
2799 MVPP2_PRS_RI_L3_PROTO_MASK);
2800 mvpp2_prs_hw_write(priv, &pe);
2801
2802 /* Ethertype: IPv6 without options */
2803 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2804 MVPP2_PE_LAST_FREE_TID);
2805 if (tid < 0)
2806 return tid;
2807
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002808 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002809 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2810 pe.index = tid;
2811
2812 mvpp2_prs_match_etype(&pe, 0, ETH_P_IPV6);
2813
2814 /* Skip DIP of IPV6 header */
2815 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 8 +
2816 MVPP2_MAX_L3_ADDR_SIZE,
2817 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2818 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2819 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
2820 MVPP2_PRS_RI_L3_PROTO_MASK);
2821 /* Set L3 offset */
2822 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2823 MVPP2_ETH_TYPE_LEN,
2824 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2825
2826 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2827 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2828 priv->prs_shadow[pe.index].finish = false;
2829 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP6,
2830 MVPP2_PRS_RI_L3_PROTO_MASK);
2831 mvpp2_prs_hw_write(priv, &pe);
2832
2833 /* Default entry for MVPP2_PRS_LU_L2 - Unknown ethtype */
2834 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2835 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2836 pe.index = MVPP2_PE_ETH_TYPE_UN;
2837
2838 /* Unmask all ports */
2839 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2840
2841 /* Generate flow in the next iteration*/
2842 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2843 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2844 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
2845 MVPP2_PRS_RI_L3_PROTO_MASK);
2846 /* Set L3 offset even it's unknown L3 */
2847 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2848 MVPP2_ETH_TYPE_LEN,
2849 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2850
2851 /* Update shadow table and hw entry */
2852 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2853 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2854 priv->prs_shadow[pe.index].finish = true;
2855 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_UN,
2856 MVPP2_PRS_RI_L3_PROTO_MASK);
2857 mvpp2_prs_hw_write(priv, &pe);
2858
2859 return 0;
2860}
2861
2862/* Configure vlan entries and detect up to 2 successive VLAN tags.
2863 * Possible options:
2864 * 0x8100, 0x88A8
2865 * 0x8100, 0x8100
2866 * 0x8100
2867 * 0x88A8
2868 */
2869static int mvpp2_prs_vlan_init(struct platform_device *pdev, struct mvpp2 *priv)
2870{
2871 struct mvpp2_prs_entry pe;
2872 int err;
2873
2874 priv->prs_double_vlans = devm_kcalloc(&pdev->dev, sizeof(bool),
2875 MVPP2_PRS_DBL_VLANS_MAX,
2876 GFP_KERNEL);
2877 if (!priv->prs_double_vlans)
2878 return -ENOMEM;
2879
2880 /* Double VLAN: 0x8100, 0x88A8 */
2881 err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021AD,
2882 MVPP2_PRS_PORT_MASK);
2883 if (err)
2884 return err;
2885
2886 /* Double VLAN: 0x8100, 0x8100 */
2887 err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021Q,
2888 MVPP2_PRS_PORT_MASK);
2889 if (err)
2890 return err;
2891
2892 /* Single VLAN: 0x88a8 */
2893 err = mvpp2_prs_vlan_add(priv, ETH_P_8021AD, MVPP2_PRS_SINGLE_VLAN_AI,
2894 MVPP2_PRS_PORT_MASK);
2895 if (err)
2896 return err;
2897
2898 /* Single VLAN: 0x8100 */
2899 err = mvpp2_prs_vlan_add(priv, ETH_P_8021Q, MVPP2_PRS_SINGLE_VLAN_AI,
2900 MVPP2_PRS_PORT_MASK);
2901 if (err)
2902 return err;
2903
2904 /* Set default double vlan entry */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002905 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002906 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2907 pe.index = MVPP2_PE_VLAN_DBL;
2908
2909 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2910 /* Clear ai for next iterations */
2911 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2912 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_DOUBLE,
2913 MVPP2_PRS_RI_VLAN_MASK);
2914
2915 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_DBL_VLAN_AI_BIT,
2916 MVPP2_PRS_DBL_VLAN_AI_BIT);
2917 /* Unmask all ports */
2918 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2919
2920 /* Update shadow table and hw entry */
2921 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
2922 mvpp2_prs_hw_write(priv, &pe);
2923
2924 /* Set default vlan none entry */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002925 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002926 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2927 pe.index = MVPP2_PE_VLAN_NONE;
2928
2929 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2930 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
2931 MVPP2_PRS_RI_VLAN_MASK);
2932
2933 /* Unmask all ports */
2934 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2935
2936 /* Update shadow table and hw entry */
2937 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
2938 mvpp2_prs_hw_write(priv, &pe);
2939
2940 return 0;
2941}
2942
2943/* Set entries for PPPoE ethertype */
2944static int mvpp2_prs_pppoe_init(struct mvpp2 *priv)
2945{
2946 struct mvpp2_prs_entry pe;
2947 int tid;
2948
2949 /* IPv4 over PPPoE with options */
2950 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2951 MVPP2_PE_LAST_FREE_TID);
2952 if (tid < 0)
2953 return tid;
2954
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002955 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002956 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2957 pe.index = tid;
2958
2959 mvpp2_prs_match_etype(&pe, 0, PPP_IP);
2960
2961 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2962 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
2963 MVPP2_PRS_RI_L3_PROTO_MASK);
2964 /* Skip eth_type + 4 bytes of IP header */
2965 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
2966 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2967 /* Set L3 offset */
2968 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2969 MVPP2_ETH_TYPE_LEN,
2970 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2971
2972 /* Update shadow table and hw entry */
2973 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
2974 mvpp2_prs_hw_write(priv, &pe);
2975
2976 /* IPv4 over PPPoE without options */
2977 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2978 MVPP2_PE_LAST_FREE_TID);
2979 if (tid < 0)
2980 return tid;
2981
2982 pe.index = tid;
2983
2984 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2985 MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
2986 MVPP2_PRS_IPV4_HEAD_MASK |
2987 MVPP2_PRS_IPV4_IHL_MASK);
2988
2989 /* Clear ri before updating */
2990 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2991 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2992 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
2993 MVPP2_PRS_RI_L3_PROTO_MASK);
2994
2995 /* Update shadow table and hw entry */
2996 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
2997 mvpp2_prs_hw_write(priv, &pe);
2998
2999 /* IPv6 over PPPoE */
3000 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3001 MVPP2_PE_LAST_FREE_TID);
3002 if (tid < 0)
3003 return tid;
3004
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003005 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003006 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
3007 pe.index = tid;
3008
3009 mvpp2_prs_match_etype(&pe, 0, PPP_IPV6);
3010
3011 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
3012 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
3013 MVPP2_PRS_RI_L3_PROTO_MASK);
3014 /* Skip eth_type + 4 bytes of IPv6 header */
3015 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
3016 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3017 /* Set L3 offset */
3018 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
3019 MVPP2_ETH_TYPE_LEN,
3020 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3021
3022 /* Update shadow table and hw entry */
3023 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3024 mvpp2_prs_hw_write(priv, &pe);
3025
3026 /* Non-IP over PPPoE */
3027 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3028 MVPP2_PE_LAST_FREE_TID);
3029 if (tid < 0)
3030 return tid;
3031
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003032 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003033 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
3034 pe.index = tid;
3035
3036 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
3037 MVPP2_PRS_RI_L3_PROTO_MASK);
3038
3039 /* Finished: go to flowid generation */
3040 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3041 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3042 /* Set L3 offset even if it's unknown L3 */
3043 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
3044 MVPP2_ETH_TYPE_LEN,
3045 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3046
3047 /* Update shadow table and hw entry */
3048 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3049 mvpp2_prs_hw_write(priv, &pe);
3050
3051 return 0;
3052}
3053
3054/* Initialize entries for IPv4 */
3055static int mvpp2_prs_ip4_init(struct mvpp2 *priv)
3056{
3057 struct mvpp2_prs_entry pe;
3058 int err;
3059
3060 /* Set entries for TCP, UDP and IGMP over IPv4 */
3061 err = mvpp2_prs_ip4_proto(priv, IPPROTO_TCP, MVPP2_PRS_RI_L4_TCP,
3062 MVPP2_PRS_RI_L4_PROTO_MASK);
3063 if (err)
3064 return err;
3065
3066 err = mvpp2_prs_ip4_proto(priv, IPPROTO_UDP, MVPP2_PRS_RI_L4_UDP,
3067 MVPP2_PRS_RI_L4_PROTO_MASK);
3068 if (err)
3069 return err;
3070
3071 err = mvpp2_prs_ip4_proto(priv, IPPROTO_IGMP,
3072 MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
3073 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
3074 MVPP2_PRS_RI_CPU_CODE_MASK |
3075 MVPP2_PRS_RI_UDF3_MASK);
3076 if (err)
3077 return err;
3078
3079 /* IPv4 Broadcast */
3080 err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_BROAD_CAST);
3081 if (err)
3082 return err;
3083
3084 /* IPv4 Multicast */
3085 err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
3086 if (err)
3087 return err;
3088
3089 /* Default IPv4 entry for unknown protocols */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003090 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003091 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
3092 pe.index = MVPP2_PE_IP4_PROTO_UN;
3093
3094 /* Set next lu to IPv4 */
3095 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
3096 mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3097 /* Set L4 offset */
3098 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
3099 sizeof(struct iphdr) - 4,
3100 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3101 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
3102 MVPP2_PRS_IPV4_DIP_AI_BIT);
3103 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3104 MVPP2_PRS_RI_L4_PROTO_MASK);
3105
3106 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
3107 /* Unmask all ports */
3108 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3109
3110 /* Update shadow table and hw entry */
3111 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3112 mvpp2_prs_hw_write(priv, &pe);
3113
3114 /* Default IPv4 entry for unicast address */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003115 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003116 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
3117 pe.index = MVPP2_PE_IP4_ADDR_UN;
3118
3119 /* Finished: go to flowid generation */
3120 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3121 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3122 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
3123 MVPP2_PRS_RI_L3_ADDR_MASK);
3124
3125 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
3126 MVPP2_PRS_IPV4_DIP_AI_BIT);
3127 /* Unmask all ports */
3128 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3129
3130 /* Update shadow table and hw entry */
3131 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3132 mvpp2_prs_hw_write(priv, &pe);
3133
3134 return 0;
3135}
3136
3137/* Initialize entries for IPv6 */
3138static int mvpp2_prs_ip6_init(struct mvpp2 *priv)
3139{
3140 struct mvpp2_prs_entry pe;
3141 int tid, err;
3142
3143 /* Set entries for TCP, UDP and ICMP over IPv6 */
3144 err = mvpp2_prs_ip6_proto(priv, IPPROTO_TCP,
3145 MVPP2_PRS_RI_L4_TCP,
3146 MVPP2_PRS_RI_L4_PROTO_MASK);
3147 if (err)
3148 return err;
3149
3150 err = mvpp2_prs_ip6_proto(priv, IPPROTO_UDP,
3151 MVPP2_PRS_RI_L4_UDP,
3152 MVPP2_PRS_RI_L4_PROTO_MASK);
3153 if (err)
3154 return err;
3155
3156 err = mvpp2_prs_ip6_proto(priv, IPPROTO_ICMPV6,
3157 MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
3158 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
3159 MVPP2_PRS_RI_CPU_CODE_MASK |
3160 MVPP2_PRS_RI_UDF3_MASK);
3161 if (err)
3162 return err;
3163
3164 /* IPv4 is the last header. This is similar case as 6-TCP or 17-UDP */
3165 /* Result Info: UDF7=1, DS lite */
3166 err = mvpp2_prs_ip6_proto(priv, IPPROTO_IPIP,
3167 MVPP2_PRS_RI_UDF7_IP6_LITE,
3168 MVPP2_PRS_RI_UDF7_MASK);
3169 if (err)
3170 return err;
3171
3172 /* IPv6 multicast */
3173 err = mvpp2_prs_ip6_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
3174 if (err)
3175 return err;
3176
3177 /* Entry for checking hop limit */
3178 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3179 MVPP2_PE_LAST_FREE_TID);
3180 if (tid < 0)
3181 return tid;
3182
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003183 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003184 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3185 pe.index = tid;
3186
3187 /* Finished: go to flowid generation */
3188 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3189 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3190 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN |
3191 MVPP2_PRS_RI_DROP_MASK,
3192 MVPP2_PRS_RI_L3_PROTO_MASK |
3193 MVPP2_PRS_RI_DROP_MASK);
3194
3195 mvpp2_prs_tcam_data_byte_set(&pe, 1, 0x00, MVPP2_PRS_IPV6_HOP_MASK);
3196 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3197 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3198
3199 /* Update shadow table and hw entry */
3200 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3201 mvpp2_prs_hw_write(priv, &pe);
3202
3203 /* Default IPv6 entry for unknown protocols */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003204 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003205 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3206 pe.index = MVPP2_PE_IP6_PROTO_UN;
3207
3208 /* Finished: go to flowid generation */
3209 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3210 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3211 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3212 MVPP2_PRS_RI_L4_PROTO_MASK);
3213 /* Set L4 offset relatively to our current place */
3214 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
3215 sizeof(struct ipv6hdr) - 4,
3216 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3217
3218 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3219 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3220 /* Unmask all ports */
3221 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3222
3223 /* Update shadow table and hw entry */
3224 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3225 mvpp2_prs_hw_write(priv, &pe);
3226
3227 /* Default IPv6 entry for unknown ext protocols */
3228 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3229 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3230 pe.index = MVPP2_PE_IP6_EXT_PROTO_UN;
3231
3232 /* Finished: go to flowid generation */
3233 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3234 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3235 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3236 MVPP2_PRS_RI_L4_PROTO_MASK);
3237
3238 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_EXT_AI_BIT,
3239 MVPP2_PRS_IPV6_EXT_AI_BIT);
3240 /* Unmask all ports */
3241 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3242
3243 /* Update shadow table and hw entry */
3244 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3245 mvpp2_prs_hw_write(priv, &pe);
3246
3247 /* Default IPv6 entry for unicast address */
3248 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3249 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3250 pe.index = MVPP2_PE_IP6_ADDR_UN;
3251
3252 /* Finished: go to IPv6 again */
3253 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
3254 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
3255 MVPP2_PRS_RI_L3_ADDR_MASK);
3256 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3257 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3258 /* Shift back to IPV6 NH */
3259 mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3260
3261 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3262 /* Unmask all ports */
3263 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3264
3265 /* Update shadow table and hw entry */
3266 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
3267 mvpp2_prs_hw_write(priv, &pe);
3268
3269 return 0;
3270}
3271
3272/* Parser default initialization */
3273static int mvpp2_prs_default_init(struct platform_device *pdev,
3274 struct mvpp2 *priv)
3275{
3276 int err, index, i;
3277
3278 /* Enable tcam table */
3279 mvpp2_write(priv, MVPP2_PRS_TCAM_CTRL_REG, MVPP2_PRS_TCAM_EN_MASK);
3280
3281 /* Clear all tcam and sram entries */
3282 for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++) {
3283 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
3284 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
3285 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), 0);
3286
3287 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, index);
3288 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
3289 mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), 0);
3290 }
3291
3292 /* Invalidate all tcam entries */
3293 for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++)
3294 mvpp2_prs_hw_inv(priv, index);
3295
3296 priv->prs_shadow = devm_kcalloc(&pdev->dev, MVPP2_PRS_TCAM_SRAM_SIZE,
Markus Elfring37df25e2017-04-17 09:12:34 +02003297 sizeof(*priv->prs_shadow),
Marcin Wojtas3f518502014-07-10 16:52:13 -03003298 GFP_KERNEL);
3299 if (!priv->prs_shadow)
3300 return -ENOMEM;
3301
3302 /* Always start from lookup = 0 */
3303 for (index = 0; index < MVPP2_MAX_PORTS; index++)
3304 mvpp2_prs_hw_port_init(priv, index, MVPP2_PRS_LU_MH,
3305 MVPP2_PRS_PORT_LU_MAX, 0);
3306
3307 mvpp2_prs_def_flow_init(priv);
3308
3309 mvpp2_prs_mh_init(priv);
3310
3311 mvpp2_prs_mac_init(priv);
3312
3313 mvpp2_prs_dsa_init(priv);
3314
3315 err = mvpp2_prs_etype_init(priv);
3316 if (err)
3317 return err;
3318
3319 err = mvpp2_prs_vlan_init(pdev, priv);
3320 if (err)
3321 return err;
3322
3323 err = mvpp2_prs_pppoe_init(priv);
3324 if (err)
3325 return err;
3326
3327 err = mvpp2_prs_ip6_init(priv);
3328 if (err)
3329 return err;
3330
3331 err = mvpp2_prs_ip4_init(priv);
3332 if (err)
3333 return err;
3334
3335 return 0;
3336}
3337
3338/* Compare MAC DA with tcam entry data */
3339static bool mvpp2_prs_mac_range_equals(struct mvpp2_prs_entry *pe,
3340 const u8 *da, unsigned char *mask)
3341{
3342 unsigned char tcam_byte, tcam_mask;
3343 int index;
3344
3345 for (index = 0; index < ETH_ALEN; index++) {
3346 mvpp2_prs_tcam_data_byte_get(pe, index, &tcam_byte, &tcam_mask);
3347 if (tcam_mask != mask[index])
3348 return false;
3349
3350 if ((tcam_mask & tcam_byte) != (da[index] & mask[index]))
3351 return false;
3352 }
3353
3354 return true;
3355}
3356
3357/* Find tcam entry with matched pair <MAC DA, port> */
3358static struct mvpp2_prs_entry *
3359mvpp2_prs_mac_da_range_find(struct mvpp2 *priv, int pmap, const u8 *da,
3360 unsigned char *mask, int udf_type)
3361{
3362 struct mvpp2_prs_entry *pe;
3363 int tid;
3364
3365 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
3366 if (!pe)
3367 return NULL;
3368 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
3369
3370 /* Go through the all entires with MVPP2_PRS_LU_MAC */
3371 for (tid = MVPP2_PE_FIRST_FREE_TID;
3372 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
3373 unsigned int entry_pmap;
3374
3375 if (!priv->prs_shadow[tid].valid ||
3376 (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
3377 (priv->prs_shadow[tid].udf != udf_type))
3378 continue;
3379
3380 pe->index = tid;
3381 mvpp2_prs_hw_read(priv, pe);
3382 entry_pmap = mvpp2_prs_tcam_port_map_get(pe);
3383
3384 if (mvpp2_prs_mac_range_equals(pe, da, mask) &&
3385 entry_pmap == pmap)
3386 return pe;
3387 }
3388 kfree(pe);
3389
3390 return NULL;
3391}
3392
3393/* Update parser's mac da entry */
3394static int mvpp2_prs_mac_da_accept(struct mvpp2 *priv, int port,
3395 const u8 *da, bool add)
3396{
3397 struct mvpp2_prs_entry *pe;
3398 unsigned int pmap, len, ri;
3399 unsigned char mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3400 int tid;
3401
3402 /* Scan TCAM and see if entry with this <MAC DA, port> already exist */
3403 pe = mvpp2_prs_mac_da_range_find(priv, (1 << port), da, mask,
3404 MVPP2_PRS_UDF_MAC_DEF);
3405
3406 /* No such entry */
3407 if (!pe) {
3408 if (!add)
3409 return 0;
3410
3411 /* Create new TCAM entry */
3412 /* Find first range mac entry*/
3413 for (tid = MVPP2_PE_FIRST_FREE_TID;
3414 tid <= MVPP2_PE_LAST_FREE_TID; tid++)
3415 if (priv->prs_shadow[tid].valid &&
3416 (priv->prs_shadow[tid].lu == MVPP2_PRS_LU_MAC) &&
3417 (priv->prs_shadow[tid].udf ==
3418 MVPP2_PRS_UDF_MAC_RANGE))
3419 break;
3420
3421 /* Go through the all entries from first to last */
3422 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3423 tid - 1);
3424 if (tid < 0)
3425 return tid;
3426
3427 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
3428 if (!pe)
Amitoj Kaur Chawlac2bb7bc2016-02-04 19:25:26 +05303429 return -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003430 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
3431 pe->index = tid;
3432
3433 /* Mask all ports */
3434 mvpp2_prs_tcam_port_map_set(pe, 0);
3435 }
3436
3437 /* Update port mask */
3438 mvpp2_prs_tcam_port_set(pe, port, add);
3439
3440 /* Invalidate the entry if no ports are left enabled */
3441 pmap = mvpp2_prs_tcam_port_map_get(pe);
3442 if (pmap == 0) {
3443 if (add) {
3444 kfree(pe);
Amitoj Kaur Chawlac2bb7bc2016-02-04 19:25:26 +05303445 return -EINVAL;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003446 }
3447 mvpp2_prs_hw_inv(priv, pe->index);
3448 priv->prs_shadow[pe->index].valid = false;
3449 kfree(pe);
3450 return 0;
3451 }
3452
3453 /* Continue - set next lookup */
3454 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_DSA);
3455
3456 /* Set match on DA */
3457 len = ETH_ALEN;
3458 while (len--)
3459 mvpp2_prs_tcam_data_byte_set(pe, len, da[len], 0xff);
3460
3461 /* Set result info bits */
3462 if (is_broadcast_ether_addr(da))
3463 ri = MVPP2_PRS_RI_L2_BCAST;
3464 else if (is_multicast_ether_addr(da))
3465 ri = MVPP2_PRS_RI_L2_MCAST;
3466 else
3467 ri = MVPP2_PRS_RI_L2_UCAST | MVPP2_PRS_RI_MAC_ME_MASK;
3468
3469 mvpp2_prs_sram_ri_update(pe, ri, MVPP2_PRS_RI_L2_CAST_MASK |
3470 MVPP2_PRS_RI_MAC_ME_MASK);
3471 mvpp2_prs_shadow_ri_set(priv, pe->index, ri, MVPP2_PRS_RI_L2_CAST_MASK |
3472 MVPP2_PRS_RI_MAC_ME_MASK);
3473
3474 /* Shift to ethertype */
3475 mvpp2_prs_sram_shift_set(pe, 2 * ETH_ALEN,
3476 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3477
3478 /* Update shadow table and hw entry */
3479 priv->prs_shadow[pe->index].udf = MVPP2_PRS_UDF_MAC_DEF;
3480 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_MAC);
3481 mvpp2_prs_hw_write(priv, pe);
3482
3483 kfree(pe);
3484
3485 return 0;
3486}
3487
3488static int mvpp2_prs_update_mac_da(struct net_device *dev, const u8 *da)
3489{
3490 struct mvpp2_port *port = netdev_priv(dev);
3491 int err;
3492
3493 /* Remove old parser entry */
3494 err = mvpp2_prs_mac_da_accept(port->priv, port->id, dev->dev_addr,
3495 false);
3496 if (err)
3497 return err;
3498
3499 /* Add new parser entry */
3500 err = mvpp2_prs_mac_da_accept(port->priv, port->id, da, true);
3501 if (err)
3502 return err;
3503
3504 /* Set addr in the device */
3505 ether_addr_copy(dev->dev_addr, da);
3506
3507 return 0;
3508}
3509
3510/* Delete all port's multicast simple (not range) entries */
3511static void mvpp2_prs_mcast_del_all(struct mvpp2 *priv, int port)
3512{
3513 struct mvpp2_prs_entry pe;
3514 int index, tid;
3515
3516 for (tid = MVPP2_PE_FIRST_FREE_TID;
3517 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
3518 unsigned char da[ETH_ALEN], da_mask[ETH_ALEN];
3519
3520 if (!priv->prs_shadow[tid].valid ||
3521 (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
3522 (priv->prs_shadow[tid].udf != MVPP2_PRS_UDF_MAC_DEF))
3523 continue;
3524
3525 /* Only simple mac entries */
3526 pe.index = tid;
3527 mvpp2_prs_hw_read(priv, &pe);
3528
3529 /* Read mac addr from entry */
3530 for (index = 0; index < ETH_ALEN; index++)
3531 mvpp2_prs_tcam_data_byte_get(&pe, index, &da[index],
3532 &da_mask[index]);
3533
3534 if (is_multicast_ether_addr(da) && !is_broadcast_ether_addr(da))
3535 /* Delete this entry */
3536 mvpp2_prs_mac_da_accept(priv, port, da, false);
3537 }
3538}
3539
3540static int mvpp2_prs_tag_mode_set(struct mvpp2 *priv, int port, int type)
3541{
3542 switch (type) {
3543 case MVPP2_TAG_TYPE_EDSA:
3544 /* Add port to EDSA entries */
3545 mvpp2_prs_dsa_tag_set(priv, port, true,
3546 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3547 mvpp2_prs_dsa_tag_set(priv, port, true,
3548 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3549 /* Remove port from DSA entries */
3550 mvpp2_prs_dsa_tag_set(priv, port, false,
3551 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3552 mvpp2_prs_dsa_tag_set(priv, port, false,
3553 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3554 break;
3555
3556 case MVPP2_TAG_TYPE_DSA:
3557 /* Add port to DSA entries */
3558 mvpp2_prs_dsa_tag_set(priv, port, true,
3559 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3560 mvpp2_prs_dsa_tag_set(priv, port, true,
3561 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3562 /* Remove port from EDSA entries */
3563 mvpp2_prs_dsa_tag_set(priv, port, false,
3564 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3565 mvpp2_prs_dsa_tag_set(priv, port, false,
3566 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3567 break;
3568
3569 case MVPP2_TAG_TYPE_MH:
3570 case MVPP2_TAG_TYPE_NONE:
3571 /* Remove port form EDSA and DSA entries */
3572 mvpp2_prs_dsa_tag_set(priv, port, false,
3573 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3574 mvpp2_prs_dsa_tag_set(priv, port, false,
3575 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3576 mvpp2_prs_dsa_tag_set(priv, port, false,
3577 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3578 mvpp2_prs_dsa_tag_set(priv, port, false,
3579 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3580 break;
3581
3582 default:
3583 if ((type < 0) || (type > MVPP2_TAG_TYPE_EDSA))
3584 return -EINVAL;
3585 }
3586
3587 return 0;
3588}
3589
3590/* Set prs flow for the port */
3591static int mvpp2_prs_def_flow(struct mvpp2_port *port)
3592{
3593 struct mvpp2_prs_entry *pe;
3594 int tid;
3595
3596 pe = mvpp2_prs_flow_find(port->priv, port->id);
3597
3598 /* Such entry not exist */
3599 if (!pe) {
3600 /* Go through the all entires from last to first */
3601 tid = mvpp2_prs_tcam_first_free(port->priv,
3602 MVPP2_PE_LAST_FREE_TID,
3603 MVPP2_PE_FIRST_FREE_TID);
3604 if (tid < 0)
3605 return tid;
3606
3607 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
3608 if (!pe)
3609 return -ENOMEM;
3610
3611 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
3612 pe->index = tid;
3613
3614 /* Set flow ID*/
3615 mvpp2_prs_sram_ai_update(pe, port->id, MVPP2_PRS_FLOW_ID_MASK);
3616 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
3617
3618 /* Update shadow table */
3619 mvpp2_prs_shadow_set(port->priv, pe->index, MVPP2_PRS_LU_FLOWS);
3620 }
3621
3622 mvpp2_prs_tcam_port_map_set(pe, (1 << port->id));
3623 mvpp2_prs_hw_write(port->priv, pe);
3624 kfree(pe);
3625
3626 return 0;
3627}
3628
3629/* Classifier configuration routines */
3630
3631/* Update classification flow table registers */
3632static void mvpp2_cls_flow_write(struct mvpp2 *priv,
3633 struct mvpp2_cls_flow_entry *fe)
3634{
3635 mvpp2_write(priv, MVPP2_CLS_FLOW_INDEX_REG, fe->index);
3636 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL0_REG, fe->data[0]);
3637 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL1_REG, fe->data[1]);
3638 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL2_REG, fe->data[2]);
3639}
3640
3641/* Update classification lookup table register */
3642static void mvpp2_cls_lookup_write(struct mvpp2 *priv,
3643 struct mvpp2_cls_lookup_entry *le)
3644{
3645 u32 val;
3646
3647 val = (le->way << MVPP2_CLS_LKP_INDEX_WAY_OFFS) | le->lkpid;
3648 mvpp2_write(priv, MVPP2_CLS_LKP_INDEX_REG, val);
3649 mvpp2_write(priv, MVPP2_CLS_LKP_TBL_REG, le->data);
3650}
3651
3652/* Classifier default initialization */
3653static void mvpp2_cls_init(struct mvpp2 *priv)
3654{
3655 struct mvpp2_cls_lookup_entry le;
3656 struct mvpp2_cls_flow_entry fe;
3657 int index;
3658
3659 /* Enable classifier */
3660 mvpp2_write(priv, MVPP2_CLS_MODE_REG, MVPP2_CLS_MODE_ACTIVE_MASK);
3661
3662 /* Clear classifier flow table */
Arnd Bergmanne8f967c2016-11-24 17:28:12 +01003663 memset(&fe.data, 0, sizeof(fe.data));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003664 for (index = 0; index < MVPP2_CLS_FLOWS_TBL_SIZE; index++) {
3665 fe.index = index;
3666 mvpp2_cls_flow_write(priv, &fe);
3667 }
3668
3669 /* Clear classifier lookup table */
3670 le.data = 0;
3671 for (index = 0; index < MVPP2_CLS_LKP_TBL_SIZE; index++) {
3672 le.lkpid = index;
3673 le.way = 0;
3674 mvpp2_cls_lookup_write(priv, &le);
3675
3676 le.way = 1;
3677 mvpp2_cls_lookup_write(priv, &le);
3678 }
3679}
3680
3681static void mvpp2_cls_port_config(struct mvpp2_port *port)
3682{
3683 struct mvpp2_cls_lookup_entry le;
3684 u32 val;
3685
3686 /* Set way for the port */
3687 val = mvpp2_read(port->priv, MVPP2_CLS_PORT_WAY_REG);
3688 val &= ~MVPP2_CLS_PORT_WAY_MASK(port->id);
3689 mvpp2_write(port->priv, MVPP2_CLS_PORT_WAY_REG, val);
3690
3691 /* Pick the entry to be accessed in lookup ID decoding table
3692 * according to the way and lkpid.
3693 */
3694 le.lkpid = port->id;
3695 le.way = 0;
3696 le.data = 0;
3697
3698 /* Set initial CPU queue for receiving packets */
3699 le.data &= ~MVPP2_CLS_LKP_TBL_RXQ_MASK;
3700 le.data |= port->first_rxq;
3701
3702 /* Disable classification engines */
3703 le.data &= ~MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK;
3704
3705 /* Update lookup ID table entry */
3706 mvpp2_cls_lookup_write(port->priv, &le);
3707}
3708
3709/* Set CPU queue number for oversize packets */
3710static void mvpp2_cls_oversize_rxq_set(struct mvpp2_port *port)
3711{
3712 u32 val;
3713
3714 mvpp2_write(port->priv, MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port->id),
3715 port->first_rxq & MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK);
3716
3717 mvpp2_write(port->priv, MVPP2_CLS_SWFWD_P2HQ_REG(port->id),
3718 (port->first_rxq >> MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS));
3719
3720 val = mvpp2_read(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG);
3721 val |= MVPP2_CLS_SWFWD_PCTRL_MASK(port->id);
3722 mvpp2_write(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG, val);
3723}
3724
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003725static void *mvpp2_frag_alloc(const struct mvpp2_bm_pool *pool)
3726{
3727 if (likely(pool->frag_size <= PAGE_SIZE))
3728 return netdev_alloc_frag(pool->frag_size);
3729 else
3730 return kmalloc(pool->frag_size, GFP_ATOMIC);
3731}
3732
3733static void mvpp2_frag_free(const struct mvpp2_bm_pool *pool, void *data)
3734{
3735 if (likely(pool->frag_size <= PAGE_SIZE))
3736 skb_free_frag(data);
3737 else
3738 kfree(data);
3739}
3740
Marcin Wojtas3f518502014-07-10 16:52:13 -03003741/* Buffer Manager configuration routines */
3742
3743/* Create pool */
3744static int mvpp2_bm_pool_create(struct platform_device *pdev,
3745 struct mvpp2 *priv,
3746 struct mvpp2_bm_pool *bm_pool, int size)
3747{
Marcin Wojtas3f518502014-07-10 16:52:13 -03003748 u32 val;
3749
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003750 /* Number of buffer pointers must be a multiple of 16, as per
3751 * hardware constraints
3752 */
3753 if (!IS_ALIGNED(size, 16))
3754 return -EINVAL;
3755
3756 /* PPv2.1 needs 8 bytes per buffer pointer, PPv2.2 needs 16
3757 * bytes per buffer pointer
3758 */
3759 if (priv->hw_version == MVPP21)
3760 bm_pool->size_bytes = 2 * sizeof(u32) * size;
3761 else
3762 bm_pool->size_bytes = 2 * sizeof(u64) * size;
3763
3764 bm_pool->virt_addr = dma_alloc_coherent(&pdev->dev, bm_pool->size_bytes,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003765 &bm_pool->dma_addr,
Marcin Wojtas3f518502014-07-10 16:52:13 -03003766 GFP_KERNEL);
3767 if (!bm_pool->virt_addr)
3768 return -ENOMEM;
3769
Thomas Petazzonid3158802017-02-21 11:28:13 +01003770 if (!IS_ALIGNED((unsigned long)bm_pool->virt_addr,
3771 MVPP2_BM_POOL_PTR_ALIGN)) {
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003772 dma_free_coherent(&pdev->dev, bm_pool->size_bytes,
3773 bm_pool->virt_addr, bm_pool->dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003774 dev_err(&pdev->dev, "BM pool %d is not %d bytes aligned\n",
3775 bm_pool->id, MVPP2_BM_POOL_PTR_ALIGN);
3776 return -ENOMEM;
3777 }
3778
3779 mvpp2_write(priv, MVPP2_BM_POOL_BASE_REG(bm_pool->id),
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003780 lower_32_bits(bm_pool->dma_addr));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003781 mvpp2_write(priv, MVPP2_BM_POOL_SIZE_REG(bm_pool->id), size);
3782
3783 val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
3784 val |= MVPP2_BM_START_MASK;
3785 mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
3786
3787 bm_pool->type = MVPP2_BM_FREE;
3788 bm_pool->size = size;
3789 bm_pool->pkt_size = 0;
3790 bm_pool->buf_num = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003791
3792 return 0;
3793}
3794
3795/* Set pool buffer size */
3796static void mvpp2_bm_pool_bufsize_set(struct mvpp2 *priv,
3797 struct mvpp2_bm_pool *bm_pool,
3798 int buf_size)
3799{
3800 u32 val;
3801
3802 bm_pool->buf_size = buf_size;
3803
3804 val = ALIGN(buf_size, 1 << MVPP2_POOL_BUF_SIZE_OFFSET);
3805 mvpp2_write(priv, MVPP2_POOL_BUF_SIZE_REG(bm_pool->id), val);
3806}
3807
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003808static void mvpp2_bm_bufs_get_addrs(struct device *dev, struct mvpp2 *priv,
3809 struct mvpp2_bm_pool *bm_pool,
3810 dma_addr_t *dma_addr,
3811 phys_addr_t *phys_addr)
3812{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02003813 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01003814
3815 *dma_addr = mvpp2_percpu_read(priv, cpu,
3816 MVPP2_BM_PHY_ALLOC_REG(bm_pool->id));
3817 *phys_addr = mvpp2_percpu_read(priv, cpu, MVPP2_BM_VIRT_ALLOC_REG);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003818
3819 if (priv->hw_version == MVPP22) {
3820 u32 val;
3821 u32 dma_addr_highbits, phys_addr_highbits;
3822
Thomas Petazzonia7868412017-03-07 16:53:13 +01003823 val = mvpp2_percpu_read(priv, cpu, MVPP22_BM_ADDR_HIGH_ALLOC);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003824 dma_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_PHYS_MASK);
3825 phys_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_VIRT_MASK) >>
3826 MVPP22_BM_ADDR_HIGH_VIRT_SHIFT;
3827
3828 if (sizeof(dma_addr_t) == 8)
3829 *dma_addr |= (u64)dma_addr_highbits << 32;
3830
3831 if (sizeof(phys_addr_t) == 8)
3832 *phys_addr |= (u64)phys_addr_highbits << 32;
3833 }
Thomas Petazzonia704bb52017-06-10 23:18:22 +02003834
3835 put_cpu();
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003836}
3837
Ezequiel Garcia7861f122014-07-21 13:48:14 -03003838/* Free all buffers from the pool */
Marcin Wojtas4229d502015-12-03 15:20:50 +01003839static void mvpp2_bm_bufs_free(struct device *dev, struct mvpp2 *priv,
3840 struct mvpp2_bm_pool *bm_pool)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003841{
3842 int i;
3843
Ezequiel Garcia7861f122014-07-21 13:48:14 -03003844 for (i = 0; i < bm_pool->buf_num; i++) {
Thomas Petazzoni20396132017-03-07 16:53:00 +01003845 dma_addr_t buf_dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003846 phys_addr_t buf_phys_addr;
3847 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003848
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003849 mvpp2_bm_bufs_get_addrs(dev, priv, bm_pool,
3850 &buf_dma_addr, &buf_phys_addr);
Marcin Wojtas4229d502015-12-03 15:20:50 +01003851
Thomas Petazzoni20396132017-03-07 16:53:00 +01003852 dma_unmap_single(dev, buf_dma_addr,
Marcin Wojtas4229d502015-12-03 15:20:50 +01003853 bm_pool->buf_size, DMA_FROM_DEVICE);
3854
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003855 data = (void *)phys_to_virt(buf_phys_addr);
3856 if (!data)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003857 break;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003858
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003859 mvpp2_frag_free(bm_pool, data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003860 }
3861
3862 /* Update BM driver with number of buffers removed from pool */
3863 bm_pool->buf_num -= i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003864}
3865
3866/* Cleanup pool */
3867static int mvpp2_bm_pool_destroy(struct platform_device *pdev,
3868 struct mvpp2 *priv,
3869 struct mvpp2_bm_pool *bm_pool)
3870{
Marcin Wojtas3f518502014-07-10 16:52:13 -03003871 u32 val;
3872
Marcin Wojtas4229d502015-12-03 15:20:50 +01003873 mvpp2_bm_bufs_free(&pdev->dev, priv, bm_pool);
Ezequiel Garciad74c96c2014-07-21 13:48:13 -03003874 if (bm_pool->buf_num) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03003875 WARN(1, "cannot free all buffers in pool %d\n", bm_pool->id);
3876 return 0;
3877 }
3878
3879 val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
3880 val |= MVPP2_BM_STOP_MASK;
3881 mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
3882
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003883 dma_free_coherent(&pdev->dev, bm_pool->size_bytes,
Marcin Wojtas3f518502014-07-10 16:52:13 -03003884 bm_pool->virt_addr,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003885 bm_pool->dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003886 return 0;
3887}
3888
3889static int mvpp2_bm_pools_init(struct platform_device *pdev,
3890 struct mvpp2 *priv)
3891{
3892 int i, err, size;
3893 struct mvpp2_bm_pool *bm_pool;
3894
3895 /* Create all pools with maximum size */
3896 size = MVPP2_BM_POOL_SIZE_MAX;
3897 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
3898 bm_pool = &priv->bm_pools[i];
3899 bm_pool->id = i;
3900 err = mvpp2_bm_pool_create(pdev, priv, bm_pool, size);
3901 if (err)
3902 goto err_unroll_pools;
3903 mvpp2_bm_pool_bufsize_set(priv, bm_pool, 0);
3904 }
3905 return 0;
3906
3907err_unroll_pools:
3908 dev_err(&pdev->dev, "failed to create BM pool %d, size %d\n", i, size);
3909 for (i = i - 1; i >= 0; i--)
3910 mvpp2_bm_pool_destroy(pdev, priv, &priv->bm_pools[i]);
3911 return err;
3912}
3913
3914static int mvpp2_bm_init(struct platform_device *pdev, struct mvpp2 *priv)
3915{
3916 int i, err;
3917
3918 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
3919 /* Mask BM all interrupts */
3920 mvpp2_write(priv, MVPP2_BM_INTR_MASK_REG(i), 0);
3921 /* Clear BM cause register */
3922 mvpp2_write(priv, MVPP2_BM_INTR_CAUSE_REG(i), 0);
3923 }
3924
3925 /* Allocate and initialize BM pools */
3926 priv->bm_pools = devm_kcalloc(&pdev->dev, MVPP2_BM_POOLS_NUM,
Markus Elfring81f915e2017-04-17 09:06:33 +02003927 sizeof(*priv->bm_pools), GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003928 if (!priv->bm_pools)
3929 return -ENOMEM;
3930
3931 err = mvpp2_bm_pools_init(pdev, priv);
3932 if (err < 0)
3933 return err;
3934 return 0;
3935}
3936
3937/* Attach long pool to rxq */
3938static void mvpp2_rxq_long_pool_set(struct mvpp2_port *port,
3939 int lrxq, int long_pool)
3940{
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003941 u32 val, mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003942 int prxq;
3943
3944 /* Get queue physical ID */
3945 prxq = port->rxqs[lrxq]->id;
3946
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003947 if (port->priv->hw_version == MVPP21)
3948 mask = MVPP21_RXQ_POOL_LONG_MASK;
3949 else
3950 mask = MVPP22_RXQ_POOL_LONG_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003951
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003952 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
3953 val &= ~mask;
3954 val |= (long_pool << MVPP2_RXQ_POOL_LONG_OFFS) & mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003955 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
3956}
3957
3958/* Attach short pool to rxq */
3959static void mvpp2_rxq_short_pool_set(struct mvpp2_port *port,
3960 int lrxq, int short_pool)
3961{
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003962 u32 val, mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003963 int prxq;
3964
3965 /* Get queue physical ID */
3966 prxq = port->rxqs[lrxq]->id;
3967
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003968 if (port->priv->hw_version == MVPP21)
3969 mask = MVPP21_RXQ_POOL_SHORT_MASK;
3970 else
3971 mask = MVPP22_RXQ_POOL_SHORT_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003972
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003973 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
3974 val &= ~mask;
3975 val |= (short_pool << MVPP2_RXQ_POOL_SHORT_OFFS) & mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003976 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
3977}
3978
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003979static void *mvpp2_buf_alloc(struct mvpp2_port *port,
3980 struct mvpp2_bm_pool *bm_pool,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003981 dma_addr_t *buf_dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003982 phys_addr_t *buf_phys_addr,
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003983 gfp_t gfp_mask)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003984{
Thomas Petazzoni20396132017-03-07 16:53:00 +01003985 dma_addr_t dma_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003986 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003987
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003988 data = mvpp2_frag_alloc(bm_pool);
3989 if (!data)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003990 return NULL;
3991
Thomas Petazzoni20396132017-03-07 16:53:00 +01003992 dma_addr = dma_map_single(port->dev->dev.parent, data,
3993 MVPP2_RX_BUF_SIZE(bm_pool->pkt_size),
3994 DMA_FROM_DEVICE);
3995 if (unlikely(dma_mapping_error(port->dev->dev.parent, dma_addr))) {
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003996 mvpp2_frag_free(bm_pool, data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003997 return NULL;
3998 }
Thomas Petazzoni20396132017-03-07 16:53:00 +01003999 *buf_dma_addr = dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004000 *buf_phys_addr = virt_to_phys(data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004001
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004002 return data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004003}
4004
Marcin Wojtas3f518502014-07-10 16:52:13 -03004005/* Release buffer to BM */
4006static inline void mvpp2_bm_pool_put(struct mvpp2_port *port, int pool,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004007 dma_addr_t buf_dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004008 phys_addr_t buf_phys_addr)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004009{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004010 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01004011
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004012 if (port->priv->hw_version == MVPP22) {
4013 u32 val = 0;
4014
4015 if (sizeof(dma_addr_t) == 8)
4016 val |= upper_32_bits(buf_dma_addr) &
4017 MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK;
4018
4019 if (sizeof(phys_addr_t) == 8)
4020 val |= (upper_32_bits(buf_phys_addr)
4021 << MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT) &
4022 MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK;
4023
Thomas Petazzonia7868412017-03-07 16:53:13 +01004024 mvpp2_percpu_write(port->priv, cpu,
4025 MVPP22_BM_ADDR_HIGH_RLS_REG, val);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004026 }
4027
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004028 /* MVPP2_BM_VIRT_RLS_REG is not interpreted by HW, and simply
4029 * returned in the "cookie" field of the RX
4030 * descriptor. Instead of storing the virtual address, we
4031 * store the physical address
4032 */
Thomas Petazzonia7868412017-03-07 16:53:13 +01004033 mvpp2_percpu_write(port->priv, cpu,
4034 MVPP2_BM_VIRT_RLS_REG, buf_phys_addr);
4035 mvpp2_percpu_write(port->priv, cpu,
4036 MVPP2_BM_PHY_RLS_REG(pool), buf_dma_addr);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004037
4038 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03004039}
4040
Marcin Wojtas3f518502014-07-10 16:52:13 -03004041/* Allocate buffers for the pool */
4042static int mvpp2_bm_bufs_add(struct mvpp2_port *port,
4043 struct mvpp2_bm_pool *bm_pool, int buf_num)
4044{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004045 int i, buf_size, total_size;
Thomas Petazzoni20396132017-03-07 16:53:00 +01004046 dma_addr_t dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004047 phys_addr_t phys_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004048 void *buf;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004049
4050 buf_size = MVPP2_RX_BUF_SIZE(bm_pool->pkt_size);
4051 total_size = MVPP2_RX_TOTAL_SIZE(buf_size);
4052
4053 if (buf_num < 0 ||
4054 (buf_num + bm_pool->buf_num > bm_pool->size)) {
4055 netdev_err(port->dev,
4056 "cannot allocate %d buffers for pool %d\n",
4057 buf_num, bm_pool->id);
4058 return 0;
4059 }
4060
Marcin Wojtas3f518502014-07-10 16:52:13 -03004061 for (i = 0; i < buf_num; i++) {
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004062 buf = mvpp2_buf_alloc(port, bm_pool, &dma_addr,
4063 &phys_addr, GFP_KERNEL);
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004064 if (!buf)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004065 break;
4066
Thomas Petazzoni20396132017-03-07 16:53:00 +01004067 mvpp2_bm_pool_put(port, bm_pool->id, dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004068 phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004069 }
4070
4071 /* Update BM driver with number of buffers added to pool */
4072 bm_pool->buf_num += i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004073
4074 netdev_dbg(port->dev,
4075 "%s pool %d: pkt_size=%4d, buf_size=%4d, total_size=%4d\n",
4076 bm_pool->type == MVPP2_BM_SWF_SHORT ? "short" : " long",
4077 bm_pool->id, bm_pool->pkt_size, buf_size, total_size);
4078
4079 netdev_dbg(port->dev,
4080 "%s pool %d: %d of %d buffers added\n",
4081 bm_pool->type == MVPP2_BM_SWF_SHORT ? "short" : " long",
4082 bm_pool->id, i, buf_num);
4083 return i;
4084}
4085
4086/* Notify the driver that BM pool is being used as specific type and return the
4087 * pool pointer on success
4088 */
4089static struct mvpp2_bm_pool *
4090mvpp2_bm_pool_use(struct mvpp2_port *port, int pool, enum mvpp2_bm_type type,
4091 int pkt_size)
4092{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004093 struct mvpp2_bm_pool *new_pool = &port->priv->bm_pools[pool];
4094 int num;
4095
4096 if (new_pool->type != MVPP2_BM_FREE && new_pool->type != type) {
4097 netdev_err(port->dev, "mixing pool types is forbidden\n");
4098 return NULL;
4099 }
4100
Marcin Wojtas3f518502014-07-10 16:52:13 -03004101 if (new_pool->type == MVPP2_BM_FREE)
4102 new_pool->type = type;
4103
4104 /* Allocate buffers in case BM pool is used as long pool, but packet
4105 * size doesn't match MTU or BM pool hasn't being used yet
4106 */
4107 if (((type == MVPP2_BM_SWF_LONG) && (pkt_size > new_pool->pkt_size)) ||
4108 (new_pool->pkt_size == 0)) {
4109 int pkts_num;
4110
4111 /* Set default buffer number or free all the buffers in case
4112 * the pool is not empty
4113 */
4114 pkts_num = new_pool->buf_num;
4115 if (pkts_num == 0)
4116 pkts_num = type == MVPP2_BM_SWF_LONG ?
4117 MVPP2_BM_LONG_BUF_NUM :
4118 MVPP2_BM_SHORT_BUF_NUM;
4119 else
Marcin Wojtas4229d502015-12-03 15:20:50 +01004120 mvpp2_bm_bufs_free(port->dev->dev.parent,
4121 port->priv, new_pool);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004122
4123 new_pool->pkt_size = pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004124 new_pool->frag_size =
4125 SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
4126 MVPP2_SKB_SHINFO_SIZE;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004127
4128 /* Allocate buffers for this pool */
4129 num = mvpp2_bm_bufs_add(port, new_pool, pkts_num);
4130 if (num != pkts_num) {
4131 WARN(1, "pool %d: %d of %d allocated\n",
4132 new_pool->id, num, pkts_num);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004133 return NULL;
4134 }
4135 }
4136
4137 mvpp2_bm_pool_bufsize_set(port->priv, new_pool,
4138 MVPP2_RX_BUF_SIZE(new_pool->pkt_size));
4139
Marcin Wojtas3f518502014-07-10 16:52:13 -03004140 return new_pool;
4141}
4142
4143/* Initialize pools for swf */
4144static int mvpp2_swf_bm_pool_init(struct mvpp2_port *port)
4145{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004146 int rxq;
4147
4148 if (!port->pool_long) {
4149 port->pool_long =
4150 mvpp2_bm_pool_use(port, MVPP2_BM_SWF_LONG_POOL(port->id),
4151 MVPP2_BM_SWF_LONG,
4152 port->pkt_size);
4153 if (!port->pool_long)
4154 return -ENOMEM;
4155
Marcin Wojtas3f518502014-07-10 16:52:13 -03004156 port->pool_long->port_map |= (1 << port->id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004157
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004158 for (rxq = 0; rxq < port->nrxqs; rxq++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004159 mvpp2_rxq_long_pool_set(port, rxq, port->pool_long->id);
4160 }
4161
4162 if (!port->pool_short) {
4163 port->pool_short =
4164 mvpp2_bm_pool_use(port, MVPP2_BM_SWF_SHORT_POOL,
4165 MVPP2_BM_SWF_SHORT,
4166 MVPP2_BM_SHORT_PKT_SIZE);
4167 if (!port->pool_short)
4168 return -ENOMEM;
4169
Marcin Wojtas3f518502014-07-10 16:52:13 -03004170 port->pool_short->port_map |= (1 << port->id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004171
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004172 for (rxq = 0; rxq < port->nrxqs; rxq++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004173 mvpp2_rxq_short_pool_set(port, rxq,
4174 port->pool_short->id);
4175 }
4176
4177 return 0;
4178}
4179
4180static int mvpp2_bm_update_mtu(struct net_device *dev, int mtu)
4181{
4182 struct mvpp2_port *port = netdev_priv(dev);
4183 struct mvpp2_bm_pool *port_pool = port->pool_long;
4184 int num, pkts_num = port_pool->buf_num;
4185 int pkt_size = MVPP2_RX_PKT_SIZE(mtu);
4186
4187 /* Update BM pool with new buffer size */
Marcin Wojtas4229d502015-12-03 15:20:50 +01004188 mvpp2_bm_bufs_free(dev->dev.parent, port->priv, port_pool);
Ezequiel Garciad74c96c2014-07-21 13:48:13 -03004189 if (port_pool->buf_num) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004190 WARN(1, "cannot free all buffers in pool %d\n", port_pool->id);
4191 return -EIO;
4192 }
4193
4194 port_pool->pkt_size = pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004195 port_pool->frag_size = SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
4196 MVPP2_SKB_SHINFO_SIZE;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004197 num = mvpp2_bm_bufs_add(port, port_pool, pkts_num);
4198 if (num != pkts_num) {
4199 WARN(1, "pool %d: %d of %d allocated\n",
4200 port_pool->id, num, pkts_num);
4201 return -EIO;
4202 }
4203
4204 mvpp2_bm_pool_bufsize_set(port->priv, port_pool,
4205 MVPP2_RX_BUF_SIZE(port_pool->pkt_size));
4206 dev->mtu = mtu;
4207 netdev_update_features(dev);
4208 return 0;
4209}
4210
4211static inline void mvpp2_interrupts_enable(struct mvpp2_port *port)
4212{
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004213 int i, sw_thread_mask = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004214
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004215 for (i = 0; i < port->nqvecs; i++)
4216 sw_thread_mask |= port->qvecs[i].sw_thread_mask;
4217
Marcin Wojtas3f518502014-07-10 16:52:13 -03004218 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004219 MVPP2_ISR_ENABLE_INTERRUPT(sw_thread_mask));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004220}
4221
4222static inline void mvpp2_interrupts_disable(struct mvpp2_port *port)
4223{
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004224 int i, sw_thread_mask = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004225
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004226 for (i = 0; i < port->nqvecs; i++)
4227 sw_thread_mask |= port->qvecs[i].sw_thread_mask;
4228
Marcin Wojtas3f518502014-07-10 16:52:13 -03004229 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004230 MVPP2_ISR_DISABLE_INTERRUPT(sw_thread_mask));
4231}
4232
4233static inline void mvpp2_qvec_interrupt_enable(struct mvpp2_queue_vector *qvec)
4234{
4235 struct mvpp2_port *port = qvec->port;
4236
4237 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
4238 MVPP2_ISR_ENABLE_INTERRUPT(qvec->sw_thread_mask));
4239}
4240
4241static inline void mvpp2_qvec_interrupt_disable(struct mvpp2_queue_vector *qvec)
4242{
4243 struct mvpp2_port *port = qvec->port;
4244
4245 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
4246 MVPP2_ISR_DISABLE_INTERRUPT(qvec->sw_thread_mask));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004247}
4248
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004249/* Mask the current CPU's Rx/Tx interrupts
4250 * Called by on_each_cpu(), guaranteed to run with migration disabled,
4251 * using smp_processor_id() is OK.
4252 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004253static void mvpp2_interrupts_mask(void *arg)
4254{
4255 struct mvpp2_port *port = arg;
4256
Thomas Petazzonia7868412017-03-07 16:53:13 +01004257 mvpp2_percpu_write(port->priv, smp_processor_id(),
4258 MVPP2_ISR_RX_TX_MASK_REG(port->id), 0);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004259}
4260
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004261/* Unmask the current CPU's Rx/Tx interrupts.
4262 * Called by on_each_cpu(), guaranteed to run with migration disabled,
4263 * using smp_processor_id() is OK.
4264 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004265static void mvpp2_interrupts_unmask(void *arg)
4266{
4267 struct mvpp2_port *port = arg;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02004268 u32 val;
4269
4270 val = MVPP2_CAUSE_MISC_SUM_MASK |
4271 MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
4272 if (port->has_tx_irqs)
4273 val |= MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004274
Thomas Petazzonia7868412017-03-07 16:53:13 +01004275 mvpp2_percpu_write(port->priv, smp_processor_id(),
Thomas Petazzoni213f4282017-08-03 10:42:00 +02004276 MVPP2_ISR_RX_TX_MASK_REG(port->id), val);
4277}
4278
4279static void
4280mvpp2_shared_interrupt_mask_unmask(struct mvpp2_port *port, bool mask)
4281{
4282 u32 val;
4283 int i;
4284
4285 if (port->priv->hw_version != MVPP22)
4286 return;
4287
4288 if (mask)
4289 val = 0;
4290 else
4291 val = MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
4292
4293 for (i = 0; i < port->nqvecs; i++) {
4294 struct mvpp2_queue_vector *v = port->qvecs + i;
4295
4296 if (v->type != MVPP2_QUEUE_VECTOR_SHARED)
4297 continue;
4298
4299 mvpp2_percpu_write(port->priv, v->sw_thread_id,
4300 MVPP2_ISR_RX_TX_MASK_REG(port->id), val);
4301 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004302}
4303
4304/* Port configuration routines */
4305
Antoine Ténartf84bf382017-08-22 19:08:27 +02004306static void mvpp22_gop_init_rgmii(struct mvpp2_port *port)
4307{
4308 struct mvpp2 *priv = port->priv;
4309 u32 val;
4310
4311 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
4312 val |= GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT;
4313 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
4314
4315 regmap_read(priv->sysctrl_base, GENCONF_CTRL0, &val);
4316 if (port->gop_id == 2)
4317 val |= GENCONF_CTRL0_PORT0_RGMII | GENCONF_CTRL0_PORT1_RGMII;
4318 else if (port->gop_id == 3)
4319 val |= GENCONF_CTRL0_PORT1_RGMII_MII;
4320 regmap_write(priv->sysctrl_base, GENCONF_CTRL0, val);
4321}
4322
4323static void mvpp22_gop_init_sgmii(struct mvpp2_port *port)
4324{
4325 struct mvpp2 *priv = port->priv;
4326 u32 val;
4327
4328 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
4329 val |= GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT |
4330 GENCONF_PORT_CTRL0_RX_DATA_SAMPLE;
4331 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
4332
4333 if (port->gop_id > 1) {
4334 regmap_read(priv->sysctrl_base, GENCONF_CTRL0, &val);
4335 if (port->gop_id == 2)
4336 val &= ~GENCONF_CTRL0_PORT0_RGMII;
4337 else if (port->gop_id == 3)
4338 val &= ~GENCONF_CTRL0_PORT1_RGMII_MII;
4339 regmap_write(priv->sysctrl_base, GENCONF_CTRL0, val);
4340 }
4341}
4342
4343static void mvpp22_gop_init_10gkr(struct mvpp2_port *port)
4344{
4345 struct mvpp2 *priv = port->priv;
4346 void __iomem *mpcs = priv->iface_base + MVPP22_MPCS_BASE(port->gop_id);
4347 void __iomem *xpcs = priv->iface_base + MVPP22_XPCS_BASE(port->gop_id);
4348 u32 val;
4349
4350 /* XPCS */
4351 val = readl(xpcs + MVPP22_XPCS_CFG0);
4352 val &= ~(MVPP22_XPCS_CFG0_PCS_MODE(0x3) |
4353 MVPP22_XPCS_CFG0_ACTIVE_LANE(0x3));
4354 val |= MVPP22_XPCS_CFG0_ACTIVE_LANE(2);
4355 writel(val, xpcs + MVPP22_XPCS_CFG0);
4356
4357 /* MPCS */
4358 val = readl(mpcs + MVPP22_MPCS_CTRL);
4359 val &= ~MVPP22_MPCS_CTRL_FWD_ERR_CONN;
4360 writel(val, mpcs + MVPP22_MPCS_CTRL);
4361
4362 val = readl(mpcs + MVPP22_MPCS_CLK_RESET);
4363 val &= ~(MVPP22_MPCS_CLK_RESET_DIV_RATIO(0x7) | MAC_CLK_RESET_MAC |
4364 MAC_CLK_RESET_SD_RX | MAC_CLK_RESET_SD_TX);
4365 val |= MVPP22_MPCS_CLK_RESET_DIV_RATIO(1);
4366 writel(val, mpcs + MVPP22_MPCS_CLK_RESET);
4367
4368 val &= ~MVPP22_MPCS_CLK_RESET_DIV_SET;
4369 val |= MAC_CLK_RESET_MAC | MAC_CLK_RESET_SD_RX | MAC_CLK_RESET_SD_TX;
4370 writel(val, mpcs + MVPP22_MPCS_CLK_RESET);
4371}
4372
4373static int mvpp22_gop_init(struct mvpp2_port *port)
4374{
4375 struct mvpp2 *priv = port->priv;
4376 u32 val;
4377
4378 if (!priv->sysctrl_base)
4379 return 0;
4380
4381 switch (port->phy_interface) {
4382 case PHY_INTERFACE_MODE_RGMII:
4383 case PHY_INTERFACE_MODE_RGMII_ID:
4384 case PHY_INTERFACE_MODE_RGMII_RXID:
4385 case PHY_INTERFACE_MODE_RGMII_TXID:
4386 if (port->gop_id == 0)
4387 goto invalid_conf;
4388 mvpp22_gop_init_rgmii(port);
4389 break;
4390 case PHY_INTERFACE_MODE_SGMII:
4391 mvpp22_gop_init_sgmii(port);
4392 break;
4393 case PHY_INTERFACE_MODE_10GKR:
4394 if (port->gop_id != 0)
4395 goto invalid_conf;
4396 mvpp22_gop_init_10gkr(port);
4397 break;
4398 default:
4399 goto unsupported_conf;
4400 }
4401
4402 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL1, &val);
4403 val |= GENCONF_PORT_CTRL1_RESET(port->gop_id) |
4404 GENCONF_PORT_CTRL1_EN(port->gop_id);
4405 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL1, val);
4406
4407 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
4408 val |= GENCONF_PORT_CTRL0_CLK_DIV_PHASE_CLR;
4409 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
4410
4411 regmap_read(priv->sysctrl_base, GENCONF_SOFT_RESET1, &val);
4412 val |= GENCONF_SOFT_RESET1_GOP;
4413 regmap_write(priv->sysctrl_base, GENCONF_SOFT_RESET1, val);
4414
4415unsupported_conf:
4416 return 0;
4417
4418invalid_conf:
4419 netdev_err(port->dev, "Invalid port configuration\n");
4420 return -EINVAL;
4421}
4422
Antoine Ténart39193572017-08-22 19:08:24 +02004423static void mvpp2_port_mii_gmac_configure_mode(struct mvpp2_port *port)
4424{
4425 u32 val;
4426
4427 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4428 val = readl(port->base + MVPP22_GMAC_CTRL_4_REG);
4429 val |= MVPP22_CTRL4_SYNC_BYPASS_DIS | MVPP22_CTRL4_DP_CLK_SEL |
4430 MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
4431 val &= ~MVPP22_CTRL4_EXT_PIN_GMII_SEL;
4432 writel(val, port->base + MVPP22_GMAC_CTRL_4_REG);
4433
4434 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4435 val |= MVPP2_GMAC_DISABLE_PADDING;
4436 val &= ~MVPP2_GMAC_FLOW_CTRL_MASK;
4437 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4438 } else if (port->phy_interface == PHY_INTERFACE_MODE_RGMII ||
4439 port->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
4440 port->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID ||
4441 port->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID) {
4442 val = readl(port->base + MVPP22_GMAC_CTRL_4_REG);
4443 val |= MVPP22_CTRL4_EXT_PIN_GMII_SEL |
4444 MVPP22_CTRL4_SYNC_BYPASS_DIS |
4445 MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
4446 val &= ~MVPP22_CTRL4_DP_CLK_SEL;
4447 writel(val, port->base + MVPP22_GMAC_CTRL_4_REG);
4448
4449 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4450 val &= ~MVPP2_GMAC_DISABLE_PADDING;
4451 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4452 }
4453
4454 /* The port is connected to a copper PHY */
4455 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4456 val &= ~MVPP2_GMAC_PORT_TYPE_MASK;
4457 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4458
4459 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4460 val |= MVPP2_GMAC_IN_BAND_AUTONEG_BYPASS |
4461 MVPP2_GMAC_AN_SPEED_EN | MVPP2_GMAC_FLOW_CTRL_AUTONEG |
4462 MVPP2_GMAC_AN_DUPLEX_EN;
4463 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
4464 val |= MVPP2_GMAC_IN_BAND_AUTONEG;
4465 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4466}
4467
4468static void mvpp2_port_mii_gmac_configure(struct mvpp2_port *port)
4469{
4470 u32 val;
4471
4472 /* Force link down */
4473 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4474 val &= ~MVPP2_GMAC_FORCE_LINK_PASS;
4475 val |= MVPP2_GMAC_FORCE_LINK_DOWN;
4476 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4477
4478 /* Set the GMAC in a reset state */
4479 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4480 val |= MVPP2_GMAC_PORT_RESET_MASK;
4481 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4482
4483 /* Configure the PCS and in-band AN */
4484 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4485 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4486 val |= MVPP2_GMAC_INBAND_AN_MASK | MVPP2_GMAC_PCS_ENABLE_MASK;
4487 } else if (port->phy_interface == PHY_INTERFACE_MODE_RGMII ||
4488 port->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
4489 port->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID ||
4490 port->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID) {
4491 val &= ~MVPP2_GMAC_PCS_ENABLE_MASK;
4492 val |= MVPP2_GMAC_PORT_RGMII_MASK;
4493 }
4494 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4495
4496 mvpp2_port_mii_gmac_configure_mode(port);
4497
4498 /* Unset the GMAC reset state */
4499 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4500 val &= ~MVPP2_GMAC_PORT_RESET_MASK;
4501 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4502
4503 /* Stop forcing link down */
4504 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4505 val &= ~MVPP2_GMAC_FORCE_LINK_DOWN;
4506 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4507}
4508
Antoine Ténart77321952017-08-22 19:08:25 +02004509static void mvpp2_port_mii_xlg_configure(struct mvpp2_port *port)
4510{
4511 u32 val;
4512
4513 if (port->gop_id != 0)
4514 return;
4515
4516 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
4517 val |= MVPP22_XLG_CTRL0_RX_FLOW_CTRL_EN;
4518 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
4519
4520 val = readl(port->base + MVPP22_XLG_CTRL4_REG);
4521 val &= ~MVPP22_XLG_CTRL4_MACMODSELECT_GMAC;
4522 val |= MVPP22_XLG_CTRL4_FWD_FC | MVPP22_XLG_CTRL4_FWD_PFC;
4523 writel(val, port->base + MVPP22_XLG_CTRL4_REG);
4524}
4525
Thomas Petazzoni26975822017-03-07 16:53:14 +01004526static void mvpp22_port_mii_set(struct mvpp2_port *port)
4527{
4528 u32 val;
4529
Thomas Petazzoni26975822017-03-07 16:53:14 +01004530 /* Only GOP port 0 has an XLG MAC */
4531 if (port->gop_id == 0) {
4532 val = readl(port->base + MVPP22_XLG_CTRL3_REG);
4533 val &= ~MVPP22_XLG_CTRL3_MACMODESELECT_MASK;
Antoine Ténart725757a2017-06-12 16:01:39 +02004534
4535 if (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
4536 port->phy_interface == PHY_INTERFACE_MODE_10GKR)
4537 val |= MVPP22_XLG_CTRL3_MACMODESELECT_10G;
4538 else
4539 val |= MVPP22_XLG_CTRL3_MACMODESELECT_GMAC;
4540
Thomas Petazzoni26975822017-03-07 16:53:14 +01004541 writel(val, port->base + MVPP22_XLG_CTRL3_REG);
4542 }
Thomas Petazzoni26975822017-03-07 16:53:14 +01004543}
4544
Marcin Wojtas3f518502014-07-10 16:52:13 -03004545static void mvpp2_port_mii_set(struct mvpp2_port *port)
4546{
Thomas Petazzoni26975822017-03-07 16:53:14 +01004547 if (port->priv->hw_version == MVPP22)
4548 mvpp22_port_mii_set(port);
4549
Antoine Ténart39193572017-08-22 19:08:24 +02004550 if (port->phy_interface == PHY_INTERFACE_MODE_RGMII ||
4551 port->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
4552 port->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID ||
4553 port->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID ||
4554 port->phy_interface == PHY_INTERFACE_MODE_SGMII)
4555 mvpp2_port_mii_gmac_configure(port);
Antoine Ténart77321952017-08-22 19:08:25 +02004556 else if (port->phy_interface == PHY_INTERFACE_MODE_10GKR)
4557 mvpp2_port_mii_xlg_configure(port);
Marcin Wojtas08a23752014-07-21 13:48:12 -03004558}
4559
4560static void mvpp2_port_fc_adv_enable(struct mvpp2_port *port)
4561{
4562 u32 val;
4563
4564 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4565 val |= MVPP2_GMAC_FC_ADV_EN;
4566 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004567}
4568
4569static void mvpp2_port_enable(struct mvpp2_port *port)
4570{
4571 u32 val;
4572
Antoine Ténart725757a2017-06-12 16:01:39 +02004573 /* Only GOP port 0 has an XLG MAC */
4574 if (port->gop_id == 0 &&
4575 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
4576 port->phy_interface == PHY_INTERFACE_MODE_10GKR)) {
4577 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
4578 val |= MVPP22_XLG_CTRL0_PORT_EN |
4579 MVPP22_XLG_CTRL0_MAC_RESET_DIS;
4580 val &= ~MVPP22_XLG_CTRL0_MIB_CNT_DIS;
4581 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
4582 } else {
4583 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4584 val |= MVPP2_GMAC_PORT_EN_MASK;
4585 val |= MVPP2_GMAC_MIB_CNTR_EN_MASK;
4586 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4587 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004588}
4589
4590static void mvpp2_port_disable(struct mvpp2_port *port)
4591{
4592 u32 val;
4593
Antoine Ténart725757a2017-06-12 16:01:39 +02004594 /* Only GOP port 0 has an XLG MAC */
4595 if (port->gop_id == 0 &&
4596 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
4597 port->phy_interface == PHY_INTERFACE_MODE_10GKR)) {
4598 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
4599 val &= ~(MVPP22_XLG_CTRL0_PORT_EN |
4600 MVPP22_XLG_CTRL0_MAC_RESET_DIS);
4601 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
4602 } else {
4603 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4604 val &= ~(MVPP2_GMAC_PORT_EN_MASK);
4605 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4606 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004607}
4608
4609/* Set IEEE 802.3x Flow Control Xon Packet Transmission Mode */
4610static void mvpp2_port_periodic_xon_disable(struct mvpp2_port *port)
4611{
4612 u32 val;
4613
4614 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG) &
4615 ~MVPP2_GMAC_PERIODIC_XON_EN_MASK;
4616 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
4617}
4618
4619/* Configure loopback port */
4620static void mvpp2_port_loopback_set(struct mvpp2_port *port)
4621{
4622 u32 val;
4623
4624 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
4625
4626 if (port->speed == 1000)
4627 val |= MVPP2_GMAC_GMII_LB_EN_MASK;
4628 else
4629 val &= ~MVPP2_GMAC_GMII_LB_EN_MASK;
4630
4631 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
4632 val |= MVPP2_GMAC_PCS_LB_EN_MASK;
4633 else
4634 val &= ~MVPP2_GMAC_PCS_LB_EN_MASK;
4635
4636 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
4637}
4638
4639static void mvpp2_port_reset(struct mvpp2_port *port)
4640{
4641 u32 val;
4642
4643 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
4644 ~MVPP2_GMAC_PORT_RESET_MASK;
4645 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4646
4647 while (readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
4648 MVPP2_GMAC_PORT_RESET_MASK)
4649 continue;
4650}
4651
4652/* Change maximum receive size of the port */
4653static inline void mvpp2_gmac_max_rx_size_set(struct mvpp2_port *port)
4654{
4655 u32 val;
4656
4657 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4658 val &= ~MVPP2_GMAC_MAX_RX_SIZE_MASK;
4659 val |= (((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
4660 MVPP2_GMAC_MAX_RX_SIZE_OFFS);
4661 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4662}
4663
Stefan Chulski76eb1b12017-08-22 19:08:26 +02004664/* Change maximum receive size of the port */
4665static inline void mvpp2_xlg_max_rx_size_set(struct mvpp2_port *port)
4666{
4667 u32 val;
4668
4669 val = readl(port->base + MVPP22_XLG_CTRL1_REG);
4670 val &= ~MVPP22_XLG_CTRL1_FRAMESIZELIMIT_MASK;
4671 val |= ((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
Antoine Ténartec15ecd2017-08-25 15:24:46 +02004672 MVPP22_XLG_CTRL1_FRAMESIZELIMIT_OFFS;
Stefan Chulski76eb1b12017-08-22 19:08:26 +02004673 writel(val, port->base + MVPP22_XLG_CTRL1_REG);
4674}
4675
Marcin Wojtas3f518502014-07-10 16:52:13 -03004676/* Set defaults to the MVPP2 port */
4677static void mvpp2_defaults_set(struct mvpp2_port *port)
4678{
4679 int tx_port_num, val, queue, ptxq, lrxq;
4680
Thomas Petazzoni3d9017d2017-03-07 16:53:11 +01004681 if (port->priv->hw_version == MVPP21) {
4682 /* Configure port to loopback if needed */
4683 if (port->flags & MVPP2_F_LOOPBACK)
4684 mvpp2_port_loopback_set(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004685
Thomas Petazzoni3d9017d2017-03-07 16:53:11 +01004686 /* Update TX FIFO MIN Threshold */
4687 val = readl(port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
4688 val &= ~MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK;
4689 /* Min. TX threshold must be less than minimal packet length */
4690 val |= MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(64 - 4 - 2);
4691 writel(val, port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
4692 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004693
4694 /* Disable Legacy WRR, Disable EJP, Release from reset */
4695 tx_port_num = mvpp2_egress_port(port);
4696 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG,
4697 tx_port_num);
4698 mvpp2_write(port->priv, MVPP2_TXP_SCHED_CMD_1_REG, 0);
4699
4700 /* Close bandwidth for all queues */
4701 for (queue = 0; queue < MVPP2_MAX_TXQ; queue++) {
4702 ptxq = mvpp2_txq_phys(port->id, queue);
4703 mvpp2_write(port->priv,
4704 MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(ptxq), 0);
4705 }
4706
4707 /* Set refill period to 1 usec, refill tokens
4708 * and bucket size to maximum
4709 */
4710 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PERIOD_REG,
4711 port->priv->tclk / USEC_PER_SEC);
4712 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_REFILL_REG);
4713 val &= ~MVPP2_TXP_REFILL_PERIOD_ALL_MASK;
4714 val |= MVPP2_TXP_REFILL_PERIOD_MASK(1);
4715 val |= MVPP2_TXP_REFILL_TOKENS_ALL_MASK;
4716 mvpp2_write(port->priv, MVPP2_TXP_SCHED_REFILL_REG, val);
4717 val = MVPP2_TXP_TOKEN_SIZE_MAX;
4718 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
4719
4720 /* Set MaximumLowLatencyPacketSize value to 256 */
4721 mvpp2_write(port->priv, MVPP2_RX_CTRL_REG(port->id),
4722 MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK |
4723 MVPP2_RX_LOW_LATENCY_PKT_SIZE(256));
4724
4725 /* Enable Rx cache snoop */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004726 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004727 queue = port->rxqs[lrxq]->id;
4728 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
4729 val |= MVPP2_SNOOP_PKT_SIZE_MASK |
4730 MVPP2_SNOOP_BUF_HDR_MASK;
4731 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
4732 }
4733
4734 /* At default, mask all interrupts to all present cpus */
4735 mvpp2_interrupts_disable(port);
4736}
4737
4738/* Enable/disable receiving packets */
4739static void mvpp2_ingress_enable(struct mvpp2_port *port)
4740{
4741 u32 val;
4742 int lrxq, queue;
4743
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004744 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004745 queue = port->rxqs[lrxq]->id;
4746 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
4747 val &= ~MVPP2_RXQ_DISABLE_MASK;
4748 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
4749 }
4750}
4751
4752static void mvpp2_ingress_disable(struct mvpp2_port *port)
4753{
4754 u32 val;
4755 int lrxq, queue;
4756
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004757 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004758 queue = port->rxqs[lrxq]->id;
4759 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
4760 val |= MVPP2_RXQ_DISABLE_MASK;
4761 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
4762 }
4763}
4764
4765/* Enable transmit via physical egress queue
4766 * - HW starts take descriptors from DRAM
4767 */
4768static void mvpp2_egress_enable(struct mvpp2_port *port)
4769{
4770 u32 qmap;
4771 int queue;
4772 int tx_port_num = mvpp2_egress_port(port);
4773
4774 /* Enable all initialized TXs. */
4775 qmap = 0;
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004776 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004777 struct mvpp2_tx_queue *txq = port->txqs[queue];
4778
Markus Elfringdbbb2f02017-04-17 14:07:52 +02004779 if (txq->descs)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004780 qmap |= (1 << queue);
4781 }
4782
4783 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
4784 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG, qmap);
4785}
4786
4787/* Disable transmit via physical egress queue
4788 * - HW doesn't take descriptors from DRAM
4789 */
4790static void mvpp2_egress_disable(struct mvpp2_port *port)
4791{
4792 u32 reg_data;
4793 int delay;
4794 int tx_port_num = mvpp2_egress_port(port);
4795
4796 /* Issue stop command for active channels only */
4797 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
4798 reg_data = (mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG)) &
4799 MVPP2_TXP_SCHED_ENQ_MASK;
4800 if (reg_data != 0)
4801 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG,
4802 (reg_data << MVPP2_TXP_SCHED_DISQ_OFFSET));
4803
4804 /* Wait for all Tx activity to terminate. */
4805 delay = 0;
4806 do {
4807 if (delay >= MVPP2_TX_DISABLE_TIMEOUT_MSEC) {
4808 netdev_warn(port->dev,
4809 "Tx stop timed out, status=0x%08x\n",
4810 reg_data);
4811 break;
4812 }
4813 mdelay(1);
4814 delay++;
4815
4816 /* Check port TX Command register that all
4817 * Tx queues are stopped
4818 */
4819 reg_data = mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG);
4820 } while (reg_data & MVPP2_TXP_SCHED_ENQ_MASK);
4821}
4822
4823/* Rx descriptors helper methods */
4824
4825/* Get number of Rx descriptors occupied by received packets */
4826static inline int
4827mvpp2_rxq_received(struct mvpp2_port *port, int rxq_id)
4828{
4829 u32 val = mvpp2_read(port->priv, MVPP2_RXQ_STATUS_REG(rxq_id));
4830
4831 return val & MVPP2_RXQ_OCCUPIED_MASK;
4832}
4833
4834/* Update Rx queue status with the number of occupied and available
4835 * Rx descriptor slots.
4836 */
4837static inline void
4838mvpp2_rxq_status_update(struct mvpp2_port *port, int rxq_id,
4839 int used_count, int free_count)
4840{
4841 /* Decrement the number of used descriptors and increment count
4842 * increment the number of free descriptors.
4843 */
4844 u32 val = used_count | (free_count << MVPP2_RXQ_NUM_NEW_OFFSET);
4845
4846 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_UPDATE_REG(rxq_id), val);
4847}
4848
4849/* Get pointer to next RX descriptor to be processed by SW */
4850static inline struct mvpp2_rx_desc *
4851mvpp2_rxq_next_desc_get(struct mvpp2_rx_queue *rxq)
4852{
4853 int rx_desc = rxq->next_desc_to_proc;
4854
4855 rxq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(rxq, rx_desc);
4856 prefetch(rxq->descs + rxq->next_desc_to_proc);
4857 return rxq->descs + rx_desc;
4858}
4859
4860/* Set rx queue offset */
4861static void mvpp2_rxq_offset_set(struct mvpp2_port *port,
4862 int prxq, int offset)
4863{
4864 u32 val;
4865
4866 /* Convert offset from bytes to units of 32 bytes */
4867 offset = offset >> 5;
4868
4869 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
4870 val &= ~MVPP2_RXQ_PACKET_OFFSET_MASK;
4871
4872 /* Offset is in */
4873 val |= ((offset << MVPP2_RXQ_PACKET_OFFSET_OFFS) &
4874 MVPP2_RXQ_PACKET_OFFSET_MASK);
4875
4876 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
4877}
4878
Marcin Wojtas3f518502014-07-10 16:52:13 -03004879/* Tx descriptors helper methods */
4880
Marcin Wojtas3f518502014-07-10 16:52:13 -03004881/* Get pointer to next Tx descriptor to be processed (send) by HW */
4882static struct mvpp2_tx_desc *
4883mvpp2_txq_next_desc_get(struct mvpp2_tx_queue *txq)
4884{
4885 int tx_desc = txq->next_desc_to_proc;
4886
4887 txq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(txq, tx_desc);
4888 return txq->descs + tx_desc;
4889}
4890
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004891/* Update HW with number of aggregated Tx descriptors to be sent
4892 *
4893 * Called only from mvpp2_tx(), so migration is disabled, using
4894 * smp_processor_id() is OK.
4895 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004896static void mvpp2_aggr_txq_pend_desc_add(struct mvpp2_port *port, int pending)
4897{
4898 /* aggregated access - relevant TXQ number is written in TX desc */
Thomas Petazzonia7868412017-03-07 16:53:13 +01004899 mvpp2_percpu_write(port->priv, smp_processor_id(),
4900 MVPP2_AGGR_TXQ_UPDATE_REG, pending);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004901}
4902
4903
4904/* Check if there are enough free descriptors in aggregated txq.
4905 * If not, update the number of occupied descriptors and repeat the check.
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004906 *
4907 * Called only from mvpp2_tx(), so migration is disabled, using
4908 * smp_processor_id() is OK.
Marcin Wojtas3f518502014-07-10 16:52:13 -03004909 */
4910static int mvpp2_aggr_desc_num_check(struct mvpp2 *priv,
4911 struct mvpp2_tx_queue *aggr_txq, int num)
4912{
4913 if ((aggr_txq->count + num) > aggr_txq->size) {
4914 /* Update number of occupied aggregated Tx descriptors */
4915 int cpu = smp_processor_id();
4916 u32 val = mvpp2_read(priv, MVPP2_AGGR_TXQ_STATUS_REG(cpu));
4917
4918 aggr_txq->count = val & MVPP2_AGGR_TXQ_PENDING_MASK;
4919 }
4920
4921 if ((aggr_txq->count + num) > aggr_txq->size)
4922 return -ENOMEM;
4923
4924 return 0;
4925}
4926
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004927/* Reserved Tx descriptors allocation request
4928 *
4929 * Called only from mvpp2_txq_reserved_desc_num_proc(), itself called
4930 * only by mvpp2_tx(), so migration is disabled, using
4931 * smp_processor_id() is OK.
4932 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004933static int mvpp2_txq_alloc_reserved_desc(struct mvpp2 *priv,
4934 struct mvpp2_tx_queue *txq, int num)
4935{
4936 u32 val;
Thomas Petazzonia7868412017-03-07 16:53:13 +01004937 int cpu = smp_processor_id();
Marcin Wojtas3f518502014-07-10 16:52:13 -03004938
4939 val = (txq->id << MVPP2_TXQ_RSVD_REQ_Q_OFFSET) | num;
Thomas Petazzonia7868412017-03-07 16:53:13 +01004940 mvpp2_percpu_write(priv, cpu, MVPP2_TXQ_RSVD_REQ_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004941
Thomas Petazzonia7868412017-03-07 16:53:13 +01004942 val = mvpp2_percpu_read(priv, cpu, MVPP2_TXQ_RSVD_RSLT_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004943
4944 return val & MVPP2_TXQ_RSVD_RSLT_MASK;
4945}
4946
4947/* Check if there are enough reserved descriptors for transmission.
4948 * If not, request chunk of reserved descriptors and check again.
4949 */
4950static int mvpp2_txq_reserved_desc_num_proc(struct mvpp2 *priv,
4951 struct mvpp2_tx_queue *txq,
4952 struct mvpp2_txq_pcpu *txq_pcpu,
4953 int num)
4954{
4955 int req, cpu, desc_count;
4956
4957 if (txq_pcpu->reserved_num >= num)
4958 return 0;
4959
4960 /* Not enough descriptors reserved! Update the reserved descriptor
4961 * count and check again.
4962 */
4963
4964 desc_count = 0;
4965 /* Compute total of used descriptors */
4966 for_each_present_cpu(cpu) {
4967 struct mvpp2_txq_pcpu *txq_pcpu_aux;
4968
4969 txq_pcpu_aux = per_cpu_ptr(txq->pcpu, cpu);
4970 desc_count += txq_pcpu_aux->count;
4971 desc_count += txq_pcpu_aux->reserved_num;
4972 }
4973
4974 req = max(MVPP2_CPU_DESC_CHUNK, num - txq_pcpu->reserved_num);
4975 desc_count += req;
4976
4977 if (desc_count >
4978 (txq->size - (num_present_cpus() * MVPP2_CPU_DESC_CHUNK)))
4979 return -ENOMEM;
4980
4981 txq_pcpu->reserved_num += mvpp2_txq_alloc_reserved_desc(priv, txq, req);
4982
4983 /* OK, the descriptor cound has been updated: check again. */
4984 if (txq_pcpu->reserved_num < num)
4985 return -ENOMEM;
4986 return 0;
4987}
4988
4989/* Release the last allocated Tx descriptor. Useful to handle DMA
4990 * mapping failures in the Tx path.
4991 */
4992static void mvpp2_txq_desc_put(struct mvpp2_tx_queue *txq)
4993{
4994 if (txq->next_desc_to_proc == 0)
4995 txq->next_desc_to_proc = txq->last_desc - 1;
4996 else
4997 txq->next_desc_to_proc--;
4998}
4999
5000/* Set Tx descriptors fields relevant for CSUM calculation */
5001static u32 mvpp2_txq_desc_csum(int l3_offs, int l3_proto,
5002 int ip_hdr_len, int l4_proto)
5003{
5004 u32 command;
5005
5006 /* fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
5007 * G_L4_chk, L4_type required only for checksum calculation
5008 */
5009 command = (l3_offs << MVPP2_TXD_L3_OFF_SHIFT);
5010 command |= (ip_hdr_len << MVPP2_TXD_IP_HLEN_SHIFT);
5011 command |= MVPP2_TXD_IP_CSUM_DISABLE;
5012
5013 if (l3_proto == swab16(ETH_P_IP)) {
5014 command &= ~MVPP2_TXD_IP_CSUM_DISABLE; /* enable IPv4 csum */
5015 command &= ~MVPP2_TXD_L3_IP6; /* enable IPv4 */
5016 } else {
5017 command |= MVPP2_TXD_L3_IP6; /* enable IPv6 */
5018 }
5019
5020 if (l4_proto == IPPROTO_TCP) {
5021 command &= ~MVPP2_TXD_L4_UDP; /* enable TCP */
5022 command &= ~MVPP2_TXD_L4_CSUM_FRAG; /* generate L4 csum */
5023 } else if (l4_proto == IPPROTO_UDP) {
5024 command |= MVPP2_TXD_L4_UDP; /* enable UDP */
5025 command &= ~MVPP2_TXD_L4_CSUM_FRAG; /* generate L4 csum */
5026 } else {
5027 command |= MVPP2_TXD_L4_CSUM_NOT;
5028 }
5029
5030 return command;
5031}
5032
5033/* Get number of sent descriptors and decrement counter.
5034 * The number of sent descriptors is returned.
5035 * Per-CPU access
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005036 *
5037 * Called only from mvpp2_txq_done(), called from mvpp2_tx()
5038 * (migration disabled) and from the TX completion tasklet (migration
5039 * disabled) so using smp_processor_id() is OK.
Marcin Wojtas3f518502014-07-10 16:52:13 -03005040 */
5041static inline int mvpp2_txq_sent_desc_proc(struct mvpp2_port *port,
5042 struct mvpp2_tx_queue *txq)
5043{
5044 u32 val;
5045
5046 /* Reading status reg resets transmitted descriptor counter */
Thomas Petazzonia7868412017-03-07 16:53:13 +01005047 val = mvpp2_percpu_read(port->priv, smp_processor_id(),
5048 MVPP2_TXQ_SENT_REG(txq->id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005049
5050 return (val & MVPP2_TRANSMITTED_COUNT_MASK) >>
5051 MVPP2_TRANSMITTED_COUNT_OFFSET;
5052}
5053
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005054/* Called through on_each_cpu(), so runs on all CPUs, with migration
5055 * disabled, therefore using smp_processor_id() is OK.
5056 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03005057static void mvpp2_txq_sent_counter_clear(void *arg)
5058{
5059 struct mvpp2_port *port = arg;
5060 int queue;
5061
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005062 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005063 int id = port->txqs[queue]->id;
5064
Thomas Petazzonia7868412017-03-07 16:53:13 +01005065 mvpp2_percpu_read(port->priv, smp_processor_id(),
5066 MVPP2_TXQ_SENT_REG(id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005067 }
5068}
5069
5070/* Set max sizes for Tx queues */
5071static void mvpp2_txp_max_tx_size_set(struct mvpp2_port *port)
5072{
5073 u32 val, size, mtu;
5074 int txq, tx_port_num;
5075
5076 mtu = port->pkt_size * 8;
5077 if (mtu > MVPP2_TXP_MTU_MAX)
5078 mtu = MVPP2_TXP_MTU_MAX;
5079
5080 /* WA for wrong Token bucket update: Set MTU value = 3*real MTU value */
5081 mtu = 3 * mtu;
5082
5083 /* Indirect access to registers */
5084 tx_port_num = mvpp2_egress_port(port);
5085 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
5086
5087 /* Set MTU */
5088 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_MTU_REG);
5089 val &= ~MVPP2_TXP_MTU_MAX;
5090 val |= mtu;
5091 mvpp2_write(port->priv, MVPP2_TXP_SCHED_MTU_REG, val);
5092
5093 /* TXP token size and all TXQs token size must be larger that MTU */
5094 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG);
5095 size = val & MVPP2_TXP_TOKEN_SIZE_MAX;
5096 if (size < mtu) {
5097 size = mtu;
5098 val &= ~MVPP2_TXP_TOKEN_SIZE_MAX;
5099 val |= size;
5100 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
5101 }
5102
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005103 for (txq = 0; txq < port->ntxqs; txq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005104 val = mvpp2_read(port->priv,
5105 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq));
5106 size = val & MVPP2_TXQ_TOKEN_SIZE_MAX;
5107
5108 if (size < mtu) {
5109 size = mtu;
5110 val &= ~MVPP2_TXQ_TOKEN_SIZE_MAX;
5111 val |= size;
5112 mvpp2_write(port->priv,
5113 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq),
5114 val);
5115 }
5116 }
5117}
5118
5119/* Set the number of packets that will be received before Rx interrupt
5120 * will be generated by HW.
5121 */
5122static void mvpp2_rx_pkts_coal_set(struct mvpp2_port *port,
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01005123 struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005124{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005125 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005126
Thomas Petazzonif8b0d5f2017-02-21 11:28:03 +01005127 if (rxq->pkts_coal > MVPP2_OCCUPIED_THRESH_MASK)
5128 rxq->pkts_coal = MVPP2_OCCUPIED_THRESH_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005129
Thomas Petazzonia7868412017-03-07 16:53:13 +01005130 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
5131 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_THRESH_REG,
5132 rxq->pkts_coal);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005133
5134 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005135}
5136
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005137/* For some reason in the LSP this is done on each CPU. Why ? */
5138static void mvpp2_tx_pkts_coal_set(struct mvpp2_port *port,
5139 struct mvpp2_tx_queue *txq)
5140{
5141 int cpu = get_cpu();
5142 u32 val;
5143
5144 if (txq->done_pkts_coal > MVPP2_TXQ_THRESH_MASK)
5145 txq->done_pkts_coal = MVPP2_TXQ_THRESH_MASK;
5146
5147 val = (txq->done_pkts_coal << MVPP2_TXQ_THRESH_OFFSET);
5148 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5149 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_THRESH_REG, val);
5150
5151 put_cpu();
5152}
5153
Thomas Petazzoniab426762017-02-21 11:28:04 +01005154static u32 mvpp2_usec_to_cycles(u32 usec, unsigned long clk_hz)
5155{
5156 u64 tmp = (u64)clk_hz * usec;
5157
5158 do_div(tmp, USEC_PER_SEC);
5159
5160 return tmp > U32_MAX ? U32_MAX : tmp;
5161}
5162
5163static u32 mvpp2_cycles_to_usec(u32 cycles, unsigned long clk_hz)
5164{
5165 u64 tmp = (u64)cycles * USEC_PER_SEC;
5166
5167 do_div(tmp, clk_hz);
5168
5169 return tmp > U32_MAX ? U32_MAX : tmp;
5170}
5171
Marcin Wojtas3f518502014-07-10 16:52:13 -03005172/* Set the time delay in usec before Rx interrupt */
5173static void mvpp2_rx_time_coal_set(struct mvpp2_port *port,
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01005174 struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005175{
Thomas Petazzoniab426762017-02-21 11:28:04 +01005176 unsigned long freq = port->priv->tclk;
5177 u32 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005178
Thomas Petazzoniab426762017-02-21 11:28:04 +01005179 if (val > MVPP2_MAX_ISR_RX_THRESHOLD) {
5180 rxq->time_coal =
5181 mvpp2_cycles_to_usec(MVPP2_MAX_ISR_RX_THRESHOLD, freq);
5182
5183 /* re-evaluate to get actual register value */
5184 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
5185 }
5186
Marcin Wojtas3f518502014-07-10 16:52:13 -03005187 mvpp2_write(port->priv, MVPP2_ISR_RX_THRESHOLD_REG(rxq->id), val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005188}
5189
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005190static void mvpp2_tx_time_coal_set(struct mvpp2_port *port)
5191{
5192 unsigned long freq = port->priv->tclk;
5193 u32 val = mvpp2_usec_to_cycles(port->tx_time_coal, freq);
5194
5195 if (val > MVPP2_MAX_ISR_TX_THRESHOLD) {
5196 port->tx_time_coal =
5197 mvpp2_cycles_to_usec(MVPP2_MAX_ISR_TX_THRESHOLD, freq);
5198
5199 /* re-evaluate to get actual register value */
5200 val = mvpp2_usec_to_cycles(port->tx_time_coal, freq);
5201 }
5202
5203 mvpp2_write(port->priv, MVPP2_ISR_TX_THRESHOLD_REG(port->id), val);
5204}
5205
Marcin Wojtas3f518502014-07-10 16:52:13 -03005206/* Free Tx queue skbuffs */
5207static void mvpp2_txq_bufs_free(struct mvpp2_port *port,
5208 struct mvpp2_tx_queue *txq,
5209 struct mvpp2_txq_pcpu *txq_pcpu, int num)
5210{
5211 int i;
5212
5213 for (i = 0; i < num; i++) {
Thomas Petazzoni83544912016-12-21 11:28:49 +01005214 struct mvpp2_txq_pcpu_buf *tx_buf =
5215 txq_pcpu->buffs + txq_pcpu->txq_get_index;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005216
Thomas Petazzoni20396132017-03-07 16:53:00 +01005217 dma_unmap_single(port->dev->dev.parent, tx_buf->dma,
Thomas Petazzoni83544912016-12-21 11:28:49 +01005218 tx_buf->size, DMA_TO_DEVICE);
Thomas Petazzoni36fb7432017-02-21 11:28:05 +01005219 if (tx_buf->skb)
5220 dev_kfree_skb_any(tx_buf->skb);
5221
5222 mvpp2_txq_inc_get(txq_pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005223 }
5224}
5225
5226static inline struct mvpp2_rx_queue *mvpp2_get_rx_queue(struct mvpp2_port *port,
5227 u32 cause)
5228{
5229 int queue = fls(cause) - 1;
5230
5231 return port->rxqs[queue];
5232}
5233
5234static inline struct mvpp2_tx_queue *mvpp2_get_tx_queue(struct mvpp2_port *port,
5235 u32 cause)
5236{
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005237 int queue = fls(cause) - 1;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005238
5239 return port->txqs[queue];
5240}
5241
5242/* Handle end of transmission */
5243static void mvpp2_txq_done(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
5244 struct mvpp2_txq_pcpu *txq_pcpu)
5245{
5246 struct netdev_queue *nq = netdev_get_tx_queue(port->dev, txq->log_id);
5247 int tx_done;
5248
5249 if (txq_pcpu->cpu != smp_processor_id())
5250 netdev_err(port->dev, "wrong cpu on the end of Tx processing\n");
5251
5252 tx_done = mvpp2_txq_sent_desc_proc(port, txq);
5253 if (!tx_done)
5254 return;
5255 mvpp2_txq_bufs_free(port, txq, txq_pcpu, tx_done);
5256
5257 txq_pcpu->count -= tx_done;
5258
5259 if (netif_tx_queue_stopped(nq))
5260 if (txq_pcpu->size - txq_pcpu->count >= MAX_SKB_FRAGS + 1)
5261 netif_tx_wake_queue(nq);
5262}
5263
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005264static unsigned int mvpp2_tx_done(struct mvpp2_port *port, u32 cause,
5265 int cpu)
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005266{
5267 struct mvpp2_tx_queue *txq;
5268 struct mvpp2_txq_pcpu *txq_pcpu;
5269 unsigned int tx_todo = 0;
5270
5271 while (cause) {
5272 txq = mvpp2_get_tx_queue(port, cause);
5273 if (!txq)
5274 break;
5275
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005276 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005277
5278 if (txq_pcpu->count) {
5279 mvpp2_txq_done(port, txq, txq_pcpu);
5280 tx_todo += txq_pcpu->count;
5281 }
5282
5283 cause &= ~(1 << txq->log_id);
5284 }
5285 return tx_todo;
5286}
5287
Marcin Wojtas3f518502014-07-10 16:52:13 -03005288/* Rx/Tx queue initialization/cleanup methods */
5289
5290/* Allocate and initialize descriptors for aggr TXQ */
5291static int mvpp2_aggr_txq_init(struct platform_device *pdev,
Antoine Ténart85affd72017-08-23 09:46:55 +02005292 struct mvpp2_tx_queue *aggr_txq, int cpu,
Marcin Wojtas3f518502014-07-10 16:52:13 -03005293 struct mvpp2 *priv)
5294{
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005295 u32 txq_dma;
5296
Marcin Wojtas3f518502014-07-10 16:52:13 -03005297 /* Allocate memory for TX descriptors */
5298 aggr_txq->descs = dma_alloc_coherent(&pdev->dev,
Antoine Ténart85affd72017-08-23 09:46:55 +02005299 MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005300 &aggr_txq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005301 if (!aggr_txq->descs)
5302 return -ENOMEM;
5303
Marcin Wojtas3f518502014-07-10 16:52:13 -03005304 aggr_txq->last_desc = aggr_txq->size - 1;
5305
5306 /* Aggr TXQ no reset WA */
5307 aggr_txq->next_desc_to_proc = mvpp2_read(priv,
5308 MVPP2_AGGR_TXQ_INDEX_REG(cpu));
5309
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005310 /* Set Tx descriptors queue starting address indirect
5311 * access
5312 */
5313 if (priv->hw_version == MVPP21)
5314 txq_dma = aggr_txq->descs_dma;
5315 else
5316 txq_dma = aggr_txq->descs_dma >>
5317 MVPP22_AGGR_TXQ_DESC_ADDR_OFFS;
5318
5319 mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu), txq_dma);
Antoine Ténart85affd72017-08-23 09:46:55 +02005320 mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu),
5321 MVPP2_AGGR_TXQ_SIZE);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005322
5323 return 0;
5324}
5325
5326/* Create a specified Rx queue */
5327static int mvpp2_rxq_init(struct mvpp2_port *port,
5328 struct mvpp2_rx_queue *rxq)
5329
5330{
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005331 u32 rxq_dma;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005332 int cpu;
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005333
Marcin Wojtas3f518502014-07-10 16:52:13 -03005334 rxq->size = port->rx_ring_size;
5335
5336 /* Allocate memory for RX descriptors */
5337 rxq->descs = dma_alloc_coherent(port->dev->dev.parent,
5338 rxq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005339 &rxq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005340 if (!rxq->descs)
5341 return -ENOMEM;
5342
Marcin Wojtas3f518502014-07-10 16:52:13 -03005343 rxq->last_desc = rxq->size - 1;
5344
5345 /* Zero occupied and non-occupied counters - direct access */
5346 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
5347
5348 /* Set Rx descriptors queue starting address - indirect access */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005349 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005350 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005351 if (port->priv->hw_version == MVPP21)
5352 rxq_dma = rxq->descs_dma;
5353 else
5354 rxq_dma = rxq->descs_dma >> MVPP22_DESC_ADDR_OFFS;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005355 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_ADDR_REG, rxq_dma);
5356 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_SIZE_REG, rxq->size);
5357 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_INDEX_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005358 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005359
5360 /* Set Offset */
5361 mvpp2_rxq_offset_set(port, rxq->id, NET_SKB_PAD);
5362
5363 /* Set coalescing pkts and time */
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01005364 mvpp2_rx_pkts_coal_set(port, rxq);
5365 mvpp2_rx_time_coal_set(port, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005366
5367 /* Add number of descriptors ready for receiving packets */
5368 mvpp2_rxq_status_update(port, rxq->id, 0, rxq->size);
5369
5370 return 0;
5371}
5372
5373/* Push packets received by the RXQ to BM pool */
5374static void mvpp2_rxq_drop_pkts(struct mvpp2_port *port,
5375 struct mvpp2_rx_queue *rxq)
5376{
5377 int rx_received, i;
5378
5379 rx_received = mvpp2_rxq_received(port, rxq->id);
5380 if (!rx_received)
5381 return;
5382
5383 for (i = 0; i < rx_received; i++) {
5384 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005385 u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
5386 int pool;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005387
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005388 pool = (status & MVPP2_RXD_BM_POOL_ID_MASK) >>
5389 MVPP2_RXD_BM_POOL_ID_OFFS;
5390
Thomas Petazzoni7d7627b2017-06-22 14:23:20 +02005391 mvpp2_bm_pool_put(port, pool,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005392 mvpp2_rxdesc_dma_addr_get(port, rx_desc),
5393 mvpp2_rxdesc_cookie_get(port, rx_desc));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005394 }
5395 mvpp2_rxq_status_update(port, rxq->id, rx_received, rx_received);
5396}
5397
5398/* Cleanup Rx queue */
5399static void mvpp2_rxq_deinit(struct mvpp2_port *port,
5400 struct mvpp2_rx_queue *rxq)
5401{
Thomas Petazzonia7868412017-03-07 16:53:13 +01005402 int cpu;
5403
Marcin Wojtas3f518502014-07-10 16:52:13 -03005404 mvpp2_rxq_drop_pkts(port, rxq);
5405
5406 if (rxq->descs)
5407 dma_free_coherent(port->dev->dev.parent,
5408 rxq->size * MVPP2_DESC_ALIGNED_SIZE,
5409 rxq->descs,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005410 rxq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005411
5412 rxq->descs = NULL;
5413 rxq->last_desc = 0;
5414 rxq->next_desc_to_proc = 0;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005415 rxq->descs_dma = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005416
5417 /* Clear Rx descriptors queue starting address and size;
5418 * free descriptor number
5419 */
5420 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005421 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005422 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
5423 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_ADDR_REG, 0);
5424 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_SIZE_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005425 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005426}
5427
5428/* Create and initialize a Tx queue */
5429static int mvpp2_txq_init(struct mvpp2_port *port,
5430 struct mvpp2_tx_queue *txq)
5431{
5432 u32 val;
5433 int cpu, desc, desc_per_txq, tx_port_num;
5434 struct mvpp2_txq_pcpu *txq_pcpu;
5435
5436 txq->size = port->tx_ring_size;
5437
5438 /* Allocate memory for Tx descriptors */
5439 txq->descs = dma_alloc_coherent(port->dev->dev.parent,
5440 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005441 &txq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005442 if (!txq->descs)
5443 return -ENOMEM;
5444
Marcin Wojtas3f518502014-07-10 16:52:13 -03005445 txq->last_desc = txq->size - 1;
5446
5447 /* Set Tx descriptors queue starting address - indirect access */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005448 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005449 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5450 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_ADDR_REG,
5451 txq->descs_dma);
5452 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_SIZE_REG,
5453 txq->size & MVPP2_TXQ_DESC_SIZE_MASK);
5454 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_INDEX_REG, 0);
5455 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_RSVD_CLR_REG,
5456 txq->id << MVPP2_TXQ_RSVD_CLR_OFFSET);
5457 val = mvpp2_percpu_read(port->priv, cpu, MVPP2_TXQ_PENDING_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005458 val &= ~MVPP2_TXQ_PENDING_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005459 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PENDING_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005460
5461 /* Calculate base address in prefetch buffer. We reserve 16 descriptors
5462 * for each existing TXQ.
5463 * TCONTS for PON port must be continuous from 0 to MVPP2_MAX_TCONT
5464 * GBE ports assumed to be continious from 0 to MVPP2_MAX_PORTS
5465 */
5466 desc_per_txq = 16;
5467 desc = (port->id * MVPP2_MAX_TXQ * desc_per_txq) +
5468 (txq->log_id * desc_per_txq);
5469
Thomas Petazzonia7868412017-03-07 16:53:13 +01005470 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG,
5471 MVPP2_PREF_BUF_PTR(desc) | MVPP2_PREF_BUF_SIZE_16 |
5472 MVPP2_PREF_BUF_THRESH(desc_per_txq / 2));
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005473 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005474
5475 /* WRR / EJP configuration - indirect access */
5476 tx_port_num = mvpp2_egress_port(port);
5477 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
5478
5479 val = mvpp2_read(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id));
5480 val &= ~MVPP2_TXQ_REFILL_PERIOD_ALL_MASK;
5481 val |= MVPP2_TXQ_REFILL_PERIOD_MASK(1);
5482 val |= MVPP2_TXQ_REFILL_TOKENS_ALL_MASK;
5483 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id), val);
5484
5485 val = MVPP2_TXQ_TOKEN_SIZE_MAX;
5486 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq->log_id),
5487 val);
5488
5489 for_each_present_cpu(cpu) {
5490 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
5491 txq_pcpu->size = txq->size;
Markus Elfring02c91ec2017-04-17 08:09:07 +02005492 txq_pcpu->buffs = kmalloc_array(txq_pcpu->size,
5493 sizeof(*txq_pcpu->buffs),
5494 GFP_KERNEL);
Thomas Petazzoni83544912016-12-21 11:28:49 +01005495 if (!txq_pcpu->buffs)
Markus Elfring20b1e162017-04-17 12:58:33 +02005496 goto cleanup;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005497
5498 txq_pcpu->count = 0;
5499 txq_pcpu->reserved_num = 0;
5500 txq_pcpu->txq_put_index = 0;
5501 txq_pcpu->txq_get_index = 0;
Antoine Ténart186cd4d2017-08-23 09:46:56 +02005502
5503 txq_pcpu->tso_headers =
5504 dma_alloc_coherent(port->dev->dev.parent,
5505 MVPP2_AGGR_TXQ_SIZE * TSO_HEADER_SIZE,
5506 &txq_pcpu->tso_headers_dma,
5507 GFP_KERNEL);
5508 if (!txq_pcpu->tso_headers)
5509 goto cleanup;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005510 }
5511
5512 return 0;
Markus Elfring20b1e162017-04-17 12:58:33 +02005513cleanup:
Marcin Wojtas71ce3912015-08-06 19:00:29 +02005514 for_each_present_cpu(cpu) {
5515 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Thomas Petazzoni83544912016-12-21 11:28:49 +01005516 kfree(txq_pcpu->buffs);
Antoine Ténart186cd4d2017-08-23 09:46:56 +02005517
5518 dma_free_coherent(port->dev->dev.parent,
5519 MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
5520 txq_pcpu->tso_headers,
5521 txq_pcpu->tso_headers_dma);
Marcin Wojtas71ce3912015-08-06 19:00:29 +02005522 }
5523
5524 dma_free_coherent(port->dev->dev.parent,
5525 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005526 txq->descs, txq->descs_dma);
Marcin Wojtas71ce3912015-08-06 19:00:29 +02005527
5528 return -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005529}
5530
5531/* Free allocated TXQ resources */
5532static void mvpp2_txq_deinit(struct mvpp2_port *port,
5533 struct mvpp2_tx_queue *txq)
5534{
5535 struct mvpp2_txq_pcpu *txq_pcpu;
5536 int cpu;
5537
5538 for_each_present_cpu(cpu) {
5539 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Thomas Petazzoni83544912016-12-21 11:28:49 +01005540 kfree(txq_pcpu->buffs);
Antoine Ténart186cd4d2017-08-23 09:46:56 +02005541
5542 dma_free_coherent(port->dev->dev.parent,
5543 MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
5544 txq_pcpu->tso_headers,
5545 txq_pcpu->tso_headers_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005546 }
5547
5548 if (txq->descs)
5549 dma_free_coherent(port->dev->dev.parent,
5550 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005551 txq->descs, txq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005552
5553 txq->descs = NULL;
5554 txq->last_desc = 0;
5555 txq->next_desc_to_proc = 0;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005556 txq->descs_dma = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005557
5558 /* Set minimum bandwidth for disabled TXQs */
5559 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(txq->id), 0);
5560
5561 /* Set Tx descriptors queue starting address and size */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005562 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005563 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5564 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_ADDR_REG, 0);
5565 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_SIZE_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005566 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005567}
5568
5569/* Cleanup Tx ports */
5570static void mvpp2_txq_clean(struct mvpp2_port *port, struct mvpp2_tx_queue *txq)
5571{
5572 struct mvpp2_txq_pcpu *txq_pcpu;
5573 int delay, pending, cpu;
5574 u32 val;
5575
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005576 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005577 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5578 val = mvpp2_percpu_read(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005579 val |= MVPP2_TXQ_DRAIN_EN_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005580 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005581
5582 /* The napi queue has been stopped so wait for all packets
5583 * to be transmitted.
5584 */
5585 delay = 0;
5586 do {
5587 if (delay >= MVPP2_TX_PENDING_TIMEOUT_MSEC) {
5588 netdev_warn(port->dev,
5589 "port %d: cleaning queue %d timed out\n",
5590 port->id, txq->log_id);
5591 break;
5592 }
5593 mdelay(1);
5594 delay++;
5595
Thomas Petazzonia7868412017-03-07 16:53:13 +01005596 pending = mvpp2_percpu_read(port->priv, cpu,
5597 MVPP2_TXQ_PENDING_REG);
5598 pending &= MVPP2_TXQ_PENDING_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005599 } while (pending);
5600
5601 val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005602 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG, val);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005603 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005604
5605 for_each_present_cpu(cpu) {
5606 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
5607
5608 /* Release all packets */
5609 mvpp2_txq_bufs_free(port, txq, txq_pcpu, txq_pcpu->count);
5610
5611 /* Reset queue */
5612 txq_pcpu->count = 0;
5613 txq_pcpu->txq_put_index = 0;
5614 txq_pcpu->txq_get_index = 0;
5615 }
5616}
5617
5618/* Cleanup all Tx queues */
5619static void mvpp2_cleanup_txqs(struct mvpp2_port *port)
5620{
5621 struct mvpp2_tx_queue *txq;
5622 int queue;
5623 u32 val;
5624
5625 val = mvpp2_read(port->priv, MVPP2_TX_PORT_FLUSH_REG);
5626
5627 /* Reset Tx ports and delete Tx queues */
5628 val |= MVPP2_TX_PORT_FLUSH_MASK(port->id);
5629 mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
5630
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005631 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005632 txq = port->txqs[queue];
5633 mvpp2_txq_clean(port, txq);
5634 mvpp2_txq_deinit(port, txq);
5635 }
5636
5637 on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
5638
5639 val &= ~MVPP2_TX_PORT_FLUSH_MASK(port->id);
5640 mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
5641}
5642
5643/* Cleanup all Rx queues */
5644static void mvpp2_cleanup_rxqs(struct mvpp2_port *port)
5645{
5646 int queue;
5647
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005648 for (queue = 0; queue < port->nrxqs; queue++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005649 mvpp2_rxq_deinit(port, port->rxqs[queue]);
5650}
5651
5652/* Init all Rx queues for port */
5653static int mvpp2_setup_rxqs(struct mvpp2_port *port)
5654{
5655 int queue, err;
5656
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005657 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005658 err = mvpp2_rxq_init(port, port->rxqs[queue]);
5659 if (err)
5660 goto err_cleanup;
5661 }
5662 return 0;
5663
5664err_cleanup:
5665 mvpp2_cleanup_rxqs(port);
5666 return err;
5667}
5668
5669/* Init all tx queues for port */
5670static int mvpp2_setup_txqs(struct mvpp2_port *port)
5671{
5672 struct mvpp2_tx_queue *txq;
5673 int queue, err;
5674
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005675 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005676 txq = port->txqs[queue];
5677 err = mvpp2_txq_init(port, txq);
5678 if (err)
5679 goto err_cleanup;
5680 }
5681
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005682 if (port->has_tx_irqs) {
5683 mvpp2_tx_time_coal_set(port);
5684 for (queue = 0; queue < port->ntxqs; queue++) {
5685 txq = port->txqs[queue];
5686 mvpp2_tx_pkts_coal_set(port, txq);
5687 }
5688 }
5689
Marcin Wojtas3f518502014-07-10 16:52:13 -03005690 on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
5691 return 0;
5692
5693err_cleanup:
5694 mvpp2_cleanup_txqs(port);
5695 return err;
5696}
5697
5698/* The callback for per-port interrupt */
5699static irqreturn_t mvpp2_isr(int irq, void *dev_id)
5700{
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02005701 struct mvpp2_queue_vector *qv = dev_id;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005702
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02005703 mvpp2_qvec_interrupt_disable(qv);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005704
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02005705 napi_schedule(&qv->napi);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005706
5707 return IRQ_HANDLED;
5708}
5709
5710/* Adjust link */
5711static void mvpp2_link_event(struct net_device *dev)
5712{
5713 struct mvpp2_port *port = netdev_priv(dev);
Philippe Reynes8e072692016-06-28 00:08:11 +02005714 struct phy_device *phydev = dev->phydev;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005715 int status_change = 0;
5716 u32 val;
5717
5718 if (phydev->link) {
5719 if ((port->speed != phydev->speed) ||
5720 (port->duplex != phydev->duplex)) {
5721 u32 val;
5722
5723 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5724 val &= ~(MVPP2_GMAC_CONFIG_MII_SPEED |
5725 MVPP2_GMAC_CONFIG_GMII_SPEED |
5726 MVPP2_GMAC_CONFIG_FULL_DUPLEX |
5727 MVPP2_GMAC_AN_SPEED_EN |
5728 MVPP2_GMAC_AN_DUPLEX_EN);
5729
5730 if (phydev->duplex)
5731 val |= MVPP2_GMAC_CONFIG_FULL_DUPLEX;
5732
5733 if (phydev->speed == SPEED_1000)
5734 val |= MVPP2_GMAC_CONFIG_GMII_SPEED;
Thomas Petazzoni2add5112014-07-27 23:21:35 +02005735 else if (phydev->speed == SPEED_100)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005736 val |= MVPP2_GMAC_CONFIG_MII_SPEED;
5737
5738 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5739
5740 port->duplex = phydev->duplex;
5741 port->speed = phydev->speed;
5742 }
5743 }
5744
5745 if (phydev->link != port->link) {
5746 if (!phydev->link) {
5747 port->duplex = -1;
5748 port->speed = 0;
5749 }
5750
5751 port->link = phydev->link;
5752 status_change = 1;
5753 }
5754
5755 if (status_change) {
5756 if (phydev->link) {
5757 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5758 val |= (MVPP2_GMAC_FORCE_LINK_PASS |
5759 MVPP2_GMAC_FORCE_LINK_DOWN);
5760 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5761 mvpp2_egress_enable(port);
5762 mvpp2_ingress_enable(port);
5763 } else {
5764 mvpp2_ingress_disable(port);
5765 mvpp2_egress_disable(port);
5766 }
5767 phy_print_status(phydev);
5768 }
5769}
5770
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005771static void mvpp2_timer_set(struct mvpp2_port_pcpu *port_pcpu)
5772{
5773 ktime_t interval;
5774
5775 if (!port_pcpu->timer_scheduled) {
5776 port_pcpu->timer_scheduled = true;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01005777 interval = MVPP2_TXDONE_HRTIMER_PERIOD_NS;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005778 hrtimer_start(&port_pcpu->tx_done_timer, interval,
5779 HRTIMER_MODE_REL_PINNED);
5780 }
5781}
5782
5783static void mvpp2_tx_proc_cb(unsigned long data)
5784{
5785 struct net_device *dev = (struct net_device *)data;
5786 struct mvpp2_port *port = netdev_priv(dev);
5787 struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
5788 unsigned int tx_todo, cause;
5789
5790 if (!netif_running(dev))
5791 return;
5792 port_pcpu->timer_scheduled = false;
5793
5794 /* Process all the Tx queues */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005795 cause = (1 << port->ntxqs) - 1;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005796 tx_todo = mvpp2_tx_done(port, cause, smp_processor_id());
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005797
5798 /* Set the timer in case not all the packets were processed */
5799 if (tx_todo)
5800 mvpp2_timer_set(port_pcpu);
5801}
5802
5803static enum hrtimer_restart mvpp2_hr_timer_cb(struct hrtimer *timer)
5804{
5805 struct mvpp2_port_pcpu *port_pcpu = container_of(timer,
5806 struct mvpp2_port_pcpu,
5807 tx_done_timer);
5808
5809 tasklet_schedule(&port_pcpu->tx_done_tasklet);
5810
5811 return HRTIMER_NORESTART;
5812}
5813
Marcin Wojtas3f518502014-07-10 16:52:13 -03005814/* Main RX/TX processing routines */
5815
5816/* Display more error info */
5817static void mvpp2_rx_error(struct mvpp2_port *port,
5818 struct mvpp2_rx_desc *rx_desc)
5819{
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005820 u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
5821 size_t sz = mvpp2_rxdesc_size_get(port, rx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005822
5823 switch (status & MVPP2_RXD_ERR_CODE_MASK) {
5824 case MVPP2_RXD_ERR_CRC:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005825 netdev_err(port->dev, "bad rx status %08x (crc error), size=%zu\n",
5826 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005827 break;
5828 case MVPP2_RXD_ERR_OVERRUN:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005829 netdev_err(port->dev, "bad rx status %08x (overrun error), size=%zu\n",
5830 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005831 break;
5832 case MVPP2_RXD_ERR_RESOURCE:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005833 netdev_err(port->dev, "bad rx status %08x (resource error), size=%zu\n",
5834 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005835 break;
5836 }
5837}
5838
5839/* Handle RX checksum offload */
5840static void mvpp2_rx_csum(struct mvpp2_port *port, u32 status,
5841 struct sk_buff *skb)
5842{
5843 if (((status & MVPP2_RXD_L3_IP4) &&
5844 !(status & MVPP2_RXD_IP4_HEADER_ERR)) ||
5845 (status & MVPP2_RXD_L3_IP6))
5846 if (((status & MVPP2_RXD_L4_UDP) ||
5847 (status & MVPP2_RXD_L4_TCP)) &&
5848 (status & MVPP2_RXD_L4_CSUM_OK)) {
5849 skb->csum = 0;
5850 skb->ip_summed = CHECKSUM_UNNECESSARY;
5851 return;
5852 }
5853
5854 skb->ip_summed = CHECKSUM_NONE;
5855}
5856
5857/* Reuse skb if possible, or allocate a new skb and add it to BM pool */
5858static int mvpp2_rx_refill(struct mvpp2_port *port,
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005859 struct mvpp2_bm_pool *bm_pool, int pool)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005860{
Thomas Petazzoni20396132017-03-07 16:53:00 +01005861 dma_addr_t dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01005862 phys_addr_t phys_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005863 void *buf;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005864
Marcin Wojtas3f518502014-07-10 16:52:13 -03005865 /* No recycle or too many buffers are in use, so allocate a new skb */
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01005866 buf = mvpp2_buf_alloc(port, bm_pool, &dma_addr, &phys_addr,
5867 GFP_ATOMIC);
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005868 if (!buf)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005869 return -ENOMEM;
5870
Thomas Petazzoni7d7627b2017-06-22 14:23:20 +02005871 mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
Thomas Petazzoni7ef7e1d2017-02-21 11:28:07 +01005872
Marcin Wojtas3f518502014-07-10 16:52:13 -03005873 return 0;
5874}
5875
5876/* Handle tx checksum */
5877static u32 mvpp2_skb_tx_csum(struct mvpp2_port *port, struct sk_buff *skb)
5878{
5879 if (skb->ip_summed == CHECKSUM_PARTIAL) {
5880 int ip_hdr_len = 0;
5881 u8 l4_proto;
5882
5883 if (skb->protocol == htons(ETH_P_IP)) {
5884 struct iphdr *ip4h = ip_hdr(skb);
5885
5886 /* Calculate IPv4 checksum and L4 checksum */
5887 ip_hdr_len = ip4h->ihl;
5888 l4_proto = ip4h->protocol;
5889 } else if (skb->protocol == htons(ETH_P_IPV6)) {
5890 struct ipv6hdr *ip6h = ipv6_hdr(skb);
5891
5892 /* Read l4_protocol from one of IPv6 extra headers */
5893 if (skb_network_header_len(skb) > 0)
5894 ip_hdr_len = (skb_network_header_len(skb) >> 2);
5895 l4_proto = ip6h->nexthdr;
5896 } else {
5897 return MVPP2_TXD_L4_CSUM_NOT;
5898 }
5899
5900 return mvpp2_txq_desc_csum(skb_network_offset(skb),
5901 skb->protocol, ip_hdr_len, l4_proto);
5902 }
5903
5904 return MVPP2_TXD_L4_CSUM_NOT | MVPP2_TXD_IP_CSUM_DISABLE;
5905}
5906
Marcin Wojtas3f518502014-07-10 16:52:13 -03005907/* Main rx processing */
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02005908static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi,
5909 int rx_todo, struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005910{
5911 struct net_device *dev = port->dev;
Marcin Wojtasb5015852015-12-03 15:20:51 +01005912 int rx_received;
5913 int rx_done = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005914 u32 rcvd_pkts = 0;
5915 u32 rcvd_bytes = 0;
5916
5917 /* Get number of received packets and clamp the to-do */
5918 rx_received = mvpp2_rxq_received(port, rxq->id);
5919 if (rx_todo > rx_received)
5920 rx_todo = rx_received;
5921
Marcin Wojtasb5015852015-12-03 15:20:51 +01005922 while (rx_done < rx_todo) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005923 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
5924 struct mvpp2_bm_pool *bm_pool;
5925 struct sk_buff *skb;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005926 unsigned int frag_size;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005927 dma_addr_t dma_addr;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005928 phys_addr_t phys_addr;
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005929 u32 rx_status;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005930 int pool, rx_bytes, err;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005931 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005932
Marcin Wojtasb5015852015-12-03 15:20:51 +01005933 rx_done++;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005934 rx_status = mvpp2_rxdesc_status_get(port, rx_desc);
5935 rx_bytes = mvpp2_rxdesc_size_get(port, rx_desc);
5936 rx_bytes -= MVPP2_MH_SIZE;
5937 dma_addr = mvpp2_rxdesc_dma_addr_get(port, rx_desc);
5938 phys_addr = mvpp2_rxdesc_cookie_get(port, rx_desc);
5939 data = (void *)phys_to_virt(phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005940
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005941 pool = (rx_status & MVPP2_RXD_BM_POOL_ID_MASK) >>
5942 MVPP2_RXD_BM_POOL_ID_OFFS;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005943 bm_pool = &port->priv->bm_pools[pool];
Marcin Wojtas3f518502014-07-10 16:52:13 -03005944
5945 /* In case of an error, release the requested buffer pointer
5946 * to the Buffer Manager. This request process is controlled
5947 * by the hardware, and the information about the buffer is
5948 * comprised by the RX descriptor.
5949 */
5950 if (rx_status & MVPP2_RXD_ERR_SUMMARY) {
Markus Elfring8a524882017-04-17 10:52:02 +02005951err_drop_frame:
Marcin Wojtas3f518502014-07-10 16:52:13 -03005952 dev->stats.rx_errors++;
5953 mvpp2_rx_error(port, rx_desc);
Marcin Wojtasb5015852015-12-03 15:20:51 +01005954 /* Return the buffer to the pool */
Thomas Petazzoni7d7627b2017-06-22 14:23:20 +02005955 mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005956 continue;
5957 }
5958
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005959 if (bm_pool->frag_size > PAGE_SIZE)
5960 frag_size = 0;
5961 else
5962 frag_size = bm_pool->frag_size;
5963
5964 skb = build_skb(data, frag_size);
5965 if (!skb) {
5966 netdev_warn(port->dev, "skb build failed\n");
5967 goto err_drop_frame;
5968 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03005969
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005970 err = mvpp2_rx_refill(port, bm_pool, pool);
Marcin Wojtasb5015852015-12-03 15:20:51 +01005971 if (err) {
5972 netdev_err(port->dev, "failed to refill BM pools\n");
5973 goto err_drop_frame;
5974 }
5975
Thomas Petazzoni20396132017-03-07 16:53:00 +01005976 dma_unmap_single(dev->dev.parent, dma_addr,
Marcin Wojtas4229d502015-12-03 15:20:50 +01005977 bm_pool->buf_size, DMA_FROM_DEVICE);
5978
Marcin Wojtas3f518502014-07-10 16:52:13 -03005979 rcvd_pkts++;
5980 rcvd_bytes += rx_bytes;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005981
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005982 skb_reserve(skb, MVPP2_MH_SIZE + NET_SKB_PAD);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005983 skb_put(skb, rx_bytes);
5984 skb->protocol = eth_type_trans(skb, dev);
5985 mvpp2_rx_csum(port, rx_status, skb);
5986
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02005987 napi_gro_receive(napi, skb);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005988 }
5989
5990 if (rcvd_pkts) {
5991 struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
5992
5993 u64_stats_update_begin(&stats->syncp);
5994 stats->rx_packets += rcvd_pkts;
5995 stats->rx_bytes += rcvd_bytes;
5996 u64_stats_update_end(&stats->syncp);
5997 }
5998
5999 /* Update Rx queue management counters */
6000 wmb();
Marcin Wojtasb5015852015-12-03 15:20:51 +01006001 mvpp2_rxq_status_update(port, rxq->id, rx_done, rx_done);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006002
6003 return rx_todo;
6004}
6005
6006static inline void
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006007tx_desc_unmap_put(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006008 struct mvpp2_tx_desc *desc)
6009{
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006010 dma_addr_t buf_dma_addr =
6011 mvpp2_txdesc_dma_addr_get(port, desc);
6012 size_t buf_sz =
6013 mvpp2_txdesc_size_get(port, desc);
6014 dma_unmap_single(port->dev->dev.parent, buf_dma_addr,
6015 buf_sz, DMA_TO_DEVICE);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006016 mvpp2_txq_desc_put(txq);
6017}
6018
6019/* Handle tx fragmentation processing */
6020static int mvpp2_tx_frag_process(struct mvpp2_port *port, struct sk_buff *skb,
6021 struct mvpp2_tx_queue *aggr_txq,
6022 struct mvpp2_tx_queue *txq)
6023{
6024 struct mvpp2_txq_pcpu *txq_pcpu = this_cpu_ptr(txq->pcpu);
6025 struct mvpp2_tx_desc *tx_desc;
6026 int i;
Thomas Petazzoni20396132017-03-07 16:53:00 +01006027 dma_addr_t buf_dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006028
6029 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
6030 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6031 void *addr = page_address(frag->page.p) + frag->page_offset;
6032
6033 tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006034 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6035 mvpp2_txdesc_size_set(port, tx_desc, frag->size);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006036
Thomas Petazzoni20396132017-03-07 16:53:00 +01006037 buf_dma_addr = dma_map_single(port->dev->dev.parent, addr,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006038 frag->size,
6039 DMA_TO_DEVICE);
Thomas Petazzoni20396132017-03-07 16:53:00 +01006040 if (dma_mapping_error(port->dev->dev.parent, buf_dma_addr)) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006041 mvpp2_txq_desc_put(txq);
Markus Elfring32bae632017-04-17 11:36:34 +02006042 goto cleanup;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006043 }
6044
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006045 mvpp2_txdesc_offset_set(port, tx_desc,
6046 buf_dma_addr & MVPP2_TX_DESC_ALIGN);
6047 mvpp2_txdesc_dma_addr_set(port, tx_desc,
6048 buf_dma_addr & ~MVPP2_TX_DESC_ALIGN);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006049
6050 if (i == (skb_shinfo(skb)->nr_frags - 1)) {
6051 /* Last descriptor */
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006052 mvpp2_txdesc_cmd_set(port, tx_desc,
6053 MVPP2_TXD_L_DESC);
6054 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006055 } else {
6056 /* Descriptor in the middle: Not First, Not Last */
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006057 mvpp2_txdesc_cmd_set(port, tx_desc, 0);
6058 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006059 }
6060 }
6061
6062 return 0;
Markus Elfring32bae632017-04-17 11:36:34 +02006063cleanup:
Marcin Wojtas3f518502014-07-10 16:52:13 -03006064 /* Release all descriptors that were used to map fragments of
6065 * this packet, as well as the corresponding DMA mappings
6066 */
6067 for (i = i - 1; i >= 0; i--) {
6068 tx_desc = txq->descs + i;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006069 tx_desc_unmap_put(port, txq, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006070 }
6071
6072 return -ENOMEM;
6073}
6074
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006075static inline void mvpp2_tso_put_hdr(struct sk_buff *skb,
6076 struct net_device *dev,
6077 struct mvpp2_tx_queue *txq,
6078 struct mvpp2_tx_queue *aggr_txq,
6079 struct mvpp2_txq_pcpu *txq_pcpu,
6080 int hdr_sz)
6081{
6082 struct mvpp2_port *port = netdev_priv(dev);
6083 struct mvpp2_tx_desc *tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
6084 dma_addr_t addr;
6085
6086 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6087 mvpp2_txdesc_size_set(port, tx_desc, hdr_sz);
6088
6089 addr = txq_pcpu->tso_headers_dma +
6090 txq_pcpu->txq_put_index * TSO_HEADER_SIZE;
6091 mvpp2_txdesc_offset_set(port, tx_desc, addr & MVPP2_TX_DESC_ALIGN);
6092 mvpp2_txdesc_dma_addr_set(port, tx_desc, addr & ~MVPP2_TX_DESC_ALIGN);
6093
6094 mvpp2_txdesc_cmd_set(port, tx_desc, mvpp2_skb_tx_csum(port, skb) |
6095 MVPP2_TXD_F_DESC |
6096 MVPP2_TXD_PADDING_DISABLE);
6097 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
6098}
6099
6100static inline int mvpp2_tso_put_data(struct sk_buff *skb,
6101 struct net_device *dev, struct tso_t *tso,
6102 struct mvpp2_tx_queue *txq,
6103 struct mvpp2_tx_queue *aggr_txq,
6104 struct mvpp2_txq_pcpu *txq_pcpu,
6105 int sz, bool left, bool last)
6106{
6107 struct mvpp2_port *port = netdev_priv(dev);
6108 struct mvpp2_tx_desc *tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
6109 dma_addr_t buf_dma_addr;
6110
6111 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6112 mvpp2_txdesc_size_set(port, tx_desc, sz);
6113
6114 buf_dma_addr = dma_map_single(dev->dev.parent, tso->data, sz,
6115 DMA_TO_DEVICE);
6116 if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
6117 mvpp2_txq_desc_put(txq);
6118 return -ENOMEM;
6119 }
6120
6121 mvpp2_txdesc_offset_set(port, tx_desc,
6122 buf_dma_addr & MVPP2_TX_DESC_ALIGN);
6123 mvpp2_txdesc_dma_addr_set(port, tx_desc,
6124 buf_dma_addr & ~MVPP2_TX_DESC_ALIGN);
6125
6126 if (!left) {
6127 mvpp2_txdesc_cmd_set(port, tx_desc, MVPP2_TXD_L_DESC);
6128 if (last) {
6129 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
6130 return 0;
6131 }
6132 } else {
6133 mvpp2_txdesc_cmd_set(port, tx_desc, 0);
6134 }
6135
6136 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
6137 return 0;
6138}
6139
6140static int mvpp2_tx_tso(struct sk_buff *skb, struct net_device *dev,
6141 struct mvpp2_tx_queue *txq,
6142 struct mvpp2_tx_queue *aggr_txq,
6143 struct mvpp2_txq_pcpu *txq_pcpu)
6144{
6145 struct mvpp2_port *port = netdev_priv(dev);
6146 struct tso_t tso;
6147 int hdr_sz = skb_transport_offset(skb) + tcp_hdrlen(skb);
6148 int i, len, descs = 0;
6149
6150 /* Check number of available descriptors */
6151 if (mvpp2_aggr_desc_num_check(port->priv, aggr_txq,
6152 tso_count_descs(skb)) ||
6153 mvpp2_txq_reserved_desc_num_proc(port->priv, txq, txq_pcpu,
6154 tso_count_descs(skb)))
6155 return 0;
6156
6157 tso_start(skb, &tso);
6158 len = skb->len - hdr_sz;
6159 while (len > 0) {
6160 int left = min_t(int, skb_shinfo(skb)->gso_size, len);
6161 char *hdr = txq_pcpu->tso_headers +
6162 txq_pcpu->txq_put_index * TSO_HEADER_SIZE;
6163
6164 len -= left;
6165 descs++;
6166
6167 tso_build_hdr(skb, hdr, &tso, left, len == 0);
6168 mvpp2_tso_put_hdr(skb, dev, txq, aggr_txq, txq_pcpu, hdr_sz);
6169
6170 while (left > 0) {
6171 int sz = min_t(int, tso.size, left);
6172 left -= sz;
6173 descs++;
6174
6175 if (mvpp2_tso_put_data(skb, dev, &tso, txq, aggr_txq,
6176 txq_pcpu, sz, left, len == 0))
6177 goto release;
6178 tso_build_data(skb, &tso, sz);
6179 }
6180 }
6181
6182 return descs;
6183
6184release:
6185 for (i = descs - 1; i >= 0; i--) {
6186 struct mvpp2_tx_desc *tx_desc = txq->descs + i;
6187 tx_desc_unmap_put(port, txq, tx_desc);
6188 }
6189 return 0;
6190}
6191
Marcin Wojtas3f518502014-07-10 16:52:13 -03006192/* Main tx processing */
6193static int mvpp2_tx(struct sk_buff *skb, struct net_device *dev)
6194{
6195 struct mvpp2_port *port = netdev_priv(dev);
6196 struct mvpp2_tx_queue *txq, *aggr_txq;
6197 struct mvpp2_txq_pcpu *txq_pcpu;
6198 struct mvpp2_tx_desc *tx_desc;
Thomas Petazzoni20396132017-03-07 16:53:00 +01006199 dma_addr_t buf_dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006200 int frags = 0;
6201 u16 txq_id;
6202 u32 tx_cmd;
6203
6204 txq_id = skb_get_queue_mapping(skb);
6205 txq = port->txqs[txq_id];
6206 txq_pcpu = this_cpu_ptr(txq->pcpu);
6207 aggr_txq = &port->priv->aggr_txqs[smp_processor_id()];
6208
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006209 if (skb_is_gso(skb)) {
6210 frags = mvpp2_tx_tso(skb, dev, txq, aggr_txq, txq_pcpu);
6211 goto out;
6212 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006213 frags = skb_shinfo(skb)->nr_frags + 1;
6214
6215 /* Check number of available descriptors */
6216 if (mvpp2_aggr_desc_num_check(port->priv, aggr_txq, frags) ||
6217 mvpp2_txq_reserved_desc_num_proc(port->priv, txq,
6218 txq_pcpu, frags)) {
6219 frags = 0;
6220 goto out;
6221 }
6222
6223 /* Get a descriptor for the first part of the packet */
6224 tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006225 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6226 mvpp2_txdesc_size_set(port, tx_desc, skb_headlen(skb));
Marcin Wojtas3f518502014-07-10 16:52:13 -03006227
Thomas Petazzoni20396132017-03-07 16:53:00 +01006228 buf_dma_addr = dma_map_single(dev->dev.parent, skb->data,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006229 skb_headlen(skb), DMA_TO_DEVICE);
Thomas Petazzoni20396132017-03-07 16:53:00 +01006230 if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006231 mvpp2_txq_desc_put(txq);
6232 frags = 0;
6233 goto out;
6234 }
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006235
6236 mvpp2_txdesc_offset_set(port, tx_desc,
6237 buf_dma_addr & MVPP2_TX_DESC_ALIGN);
6238 mvpp2_txdesc_dma_addr_set(port, tx_desc,
6239 buf_dma_addr & ~MVPP2_TX_DESC_ALIGN);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006240
6241 tx_cmd = mvpp2_skb_tx_csum(port, skb);
6242
6243 if (frags == 1) {
6244 /* First and Last descriptor */
6245 tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_L_DESC;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006246 mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
6247 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006248 } else {
6249 /* First but not Last */
6250 tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_PADDING_DISABLE;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006251 mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
6252 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006253
6254 /* Continue with other skb fragments */
6255 if (mvpp2_tx_frag_process(port, skb, aggr_txq, txq)) {
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006256 tx_desc_unmap_put(port, txq, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006257 frags = 0;
6258 goto out;
6259 }
6260 }
6261
Marcin Wojtas3f518502014-07-10 16:52:13 -03006262out:
6263 if (frags > 0) {
6264 struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006265 struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
6266
6267 txq_pcpu->reserved_num -= frags;
6268 txq_pcpu->count += frags;
6269 aggr_txq->count += frags;
6270
6271 /* Enable transmit */
6272 wmb();
6273 mvpp2_aggr_txq_pend_desc_add(port, frags);
6274
6275 if (txq_pcpu->size - txq_pcpu->count < MAX_SKB_FRAGS + 1)
6276 netif_tx_stop_queue(nq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006277
6278 u64_stats_update_begin(&stats->syncp);
6279 stats->tx_packets++;
6280 stats->tx_bytes += skb->len;
6281 u64_stats_update_end(&stats->syncp);
6282 } else {
6283 dev->stats.tx_dropped++;
6284 dev_kfree_skb_any(skb);
6285 }
6286
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006287 /* Finalize TX processing */
6288 if (txq_pcpu->count >= txq->done_pkts_coal)
6289 mvpp2_txq_done(port, txq, txq_pcpu);
6290
6291 /* Set the timer in case not all frags were processed */
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006292 if (!port->has_tx_irqs && txq_pcpu->count <= frags &&
6293 txq_pcpu->count > 0) {
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006294 struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
6295
6296 mvpp2_timer_set(port_pcpu);
6297 }
6298
Marcin Wojtas3f518502014-07-10 16:52:13 -03006299 return NETDEV_TX_OK;
6300}
6301
6302static inline void mvpp2_cause_error(struct net_device *dev, int cause)
6303{
6304 if (cause & MVPP2_CAUSE_FCS_ERR_MASK)
6305 netdev_err(dev, "FCS error\n");
6306 if (cause & MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK)
6307 netdev_err(dev, "rx fifo overrun error\n");
6308 if (cause & MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK)
6309 netdev_err(dev, "tx fifo underrun error\n");
6310}
6311
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006312static int mvpp2_poll(struct napi_struct *napi, int budget)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006313{
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006314 u32 cause_rx_tx, cause_rx, cause_tx, cause_misc;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006315 int rx_done = 0;
6316 struct mvpp2_port *port = netdev_priv(napi->dev);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006317 struct mvpp2_queue_vector *qv;
Thomas Petazzonia7868412017-03-07 16:53:13 +01006318 int cpu = smp_processor_id();
Marcin Wojtas3f518502014-07-10 16:52:13 -03006319
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006320 qv = container_of(napi, struct mvpp2_queue_vector, napi);
6321
Marcin Wojtas3f518502014-07-10 16:52:13 -03006322 /* Rx/Tx cause register
6323 *
6324 * Bits 0-15: each bit indicates received packets on the Rx queue
6325 * (bit 0 is for Rx queue 0).
6326 *
6327 * Bits 16-23: each bit indicates transmitted packets on the Tx queue
6328 * (bit 16 is for Tx queue 0).
6329 *
6330 * Each CPU has its own Rx/Tx cause register
6331 */
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006332 cause_rx_tx = mvpp2_percpu_read(port->priv, qv->sw_thread_id,
Thomas Petazzonia7868412017-03-07 16:53:13 +01006333 MVPP2_ISR_RX_TX_CAUSE_REG(port->id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03006334
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006335 cause_misc = cause_rx_tx & MVPP2_CAUSE_MISC_SUM_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006336 if (cause_misc) {
6337 mvpp2_cause_error(port->dev, cause_misc);
6338
6339 /* Clear the cause register */
6340 mvpp2_write(port->priv, MVPP2_ISR_MISC_CAUSE_REG, 0);
Thomas Petazzonia7868412017-03-07 16:53:13 +01006341 mvpp2_percpu_write(port->priv, cpu,
6342 MVPP2_ISR_RX_TX_CAUSE_REG(port->id),
6343 cause_rx_tx & ~MVPP2_CAUSE_MISC_SUM_MASK);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006344 }
6345
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006346 cause_tx = cause_rx_tx & MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
6347 if (cause_tx) {
6348 cause_tx >>= MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_OFFSET;
6349 mvpp2_tx_done(port, cause_tx, qv->sw_thread_id);
6350 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006351
6352 /* Process RX packets */
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006353 cause_rx = cause_rx_tx & MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
6354 cause_rx <<= qv->first_rxq;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006355 cause_rx |= qv->pending_cause_rx;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006356 while (cause_rx && budget > 0) {
6357 int count;
6358 struct mvpp2_rx_queue *rxq;
6359
6360 rxq = mvpp2_get_rx_queue(port, cause_rx);
6361 if (!rxq)
6362 break;
6363
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006364 count = mvpp2_rx(port, napi, budget, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006365 rx_done += count;
6366 budget -= count;
6367 if (budget > 0) {
6368 /* Clear the bit associated to this Rx queue
6369 * so that next iteration will continue from
6370 * the next Rx queue.
6371 */
6372 cause_rx &= ~(1 << rxq->logic_rxq);
6373 }
6374 }
6375
6376 if (budget > 0) {
6377 cause_rx = 0;
Eric Dumazet6ad20162017-01-30 08:22:01 -08006378 napi_complete_done(napi, rx_done);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006379
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006380 mvpp2_qvec_interrupt_enable(qv);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006381 }
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006382 qv->pending_cause_rx = cause_rx;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006383 return rx_done;
6384}
6385
6386/* Set hw internals when starting port */
6387static void mvpp2_start_dev(struct mvpp2_port *port)
6388{
Philippe Reynes8e072692016-06-28 00:08:11 +02006389 struct net_device *ndev = port->dev;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006390 int i;
Philippe Reynes8e072692016-06-28 00:08:11 +02006391
Stefan Chulski76eb1b12017-08-22 19:08:26 +02006392 if (port->gop_id == 0 &&
6393 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
6394 port->phy_interface == PHY_INTERFACE_MODE_10GKR))
6395 mvpp2_xlg_max_rx_size_set(port);
6396 else
6397 mvpp2_gmac_max_rx_size_set(port);
6398
Marcin Wojtas3f518502014-07-10 16:52:13 -03006399 mvpp2_txp_max_tx_size_set(port);
6400
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006401 for (i = 0; i < port->nqvecs; i++)
6402 napi_enable(&port->qvecs[i].napi);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006403
6404 /* Enable interrupts on all CPUs */
6405 mvpp2_interrupts_enable(port);
6406
Antoine Ténartf84bf382017-08-22 19:08:27 +02006407 if (port->priv->hw_version == MVPP22)
6408 mvpp22_gop_init(port);
6409
Antoine Ténart2055d622017-08-22 19:08:23 +02006410 mvpp2_port_mii_set(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006411 mvpp2_port_enable(port);
Philippe Reynes8e072692016-06-28 00:08:11 +02006412 phy_start(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006413 netif_tx_start_all_queues(port->dev);
6414}
6415
6416/* Set hw internals when stopping port */
6417static void mvpp2_stop_dev(struct mvpp2_port *port)
6418{
Philippe Reynes8e072692016-06-28 00:08:11 +02006419 struct net_device *ndev = port->dev;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006420 int i;
Philippe Reynes8e072692016-06-28 00:08:11 +02006421
Marcin Wojtas3f518502014-07-10 16:52:13 -03006422 /* Stop new packets from arriving to RXQs */
6423 mvpp2_ingress_disable(port);
6424
6425 mdelay(10);
6426
6427 /* Disable interrupts on all CPUs */
6428 mvpp2_interrupts_disable(port);
6429
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006430 for (i = 0; i < port->nqvecs; i++)
6431 napi_disable(&port->qvecs[i].napi);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006432
6433 netif_carrier_off(port->dev);
6434 netif_tx_stop_all_queues(port->dev);
6435
6436 mvpp2_egress_disable(port);
6437 mvpp2_port_disable(port);
Philippe Reynes8e072692016-06-28 00:08:11 +02006438 phy_stop(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006439}
6440
Marcin Wojtas3f518502014-07-10 16:52:13 -03006441static int mvpp2_check_ringparam_valid(struct net_device *dev,
6442 struct ethtool_ringparam *ring)
6443{
6444 u16 new_rx_pending = ring->rx_pending;
6445 u16 new_tx_pending = ring->tx_pending;
6446
6447 if (ring->rx_pending == 0 || ring->tx_pending == 0)
6448 return -EINVAL;
6449
6450 if (ring->rx_pending > MVPP2_MAX_RXD)
6451 new_rx_pending = MVPP2_MAX_RXD;
6452 else if (!IS_ALIGNED(ring->rx_pending, 16))
6453 new_rx_pending = ALIGN(ring->rx_pending, 16);
6454
6455 if (ring->tx_pending > MVPP2_MAX_TXD)
6456 new_tx_pending = MVPP2_MAX_TXD;
6457 else if (!IS_ALIGNED(ring->tx_pending, 32))
6458 new_tx_pending = ALIGN(ring->tx_pending, 32);
6459
6460 if (ring->rx_pending != new_rx_pending) {
6461 netdev_info(dev, "illegal Rx ring size value %d, round to %d\n",
6462 ring->rx_pending, new_rx_pending);
6463 ring->rx_pending = new_rx_pending;
6464 }
6465
6466 if (ring->tx_pending != new_tx_pending) {
6467 netdev_info(dev, "illegal Tx ring size value %d, round to %d\n",
6468 ring->tx_pending, new_tx_pending);
6469 ring->tx_pending = new_tx_pending;
6470 }
6471
6472 return 0;
6473}
6474
Thomas Petazzoni26975822017-03-07 16:53:14 +01006475static void mvpp21_get_mac_address(struct mvpp2_port *port, unsigned char *addr)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006476{
6477 u32 mac_addr_l, mac_addr_m, mac_addr_h;
6478
6479 mac_addr_l = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
6480 mac_addr_m = readl(port->priv->lms_base + MVPP2_SRC_ADDR_MIDDLE);
6481 mac_addr_h = readl(port->priv->lms_base + MVPP2_SRC_ADDR_HIGH);
6482 addr[0] = (mac_addr_h >> 24) & 0xFF;
6483 addr[1] = (mac_addr_h >> 16) & 0xFF;
6484 addr[2] = (mac_addr_h >> 8) & 0xFF;
6485 addr[3] = mac_addr_h & 0xFF;
6486 addr[4] = mac_addr_m & 0xFF;
6487 addr[5] = (mac_addr_l >> MVPP2_GMAC_SA_LOW_OFFS) & 0xFF;
6488}
6489
6490static int mvpp2_phy_connect(struct mvpp2_port *port)
6491{
6492 struct phy_device *phy_dev;
6493
6494 phy_dev = of_phy_connect(port->dev, port->phy_node, mvpp2_link_event, 0,
6495 port->phy_interface);
6496 if (!phy_dev) {
6497 netdev_err(port->dev, "cannot connect to phy\n");
6498 return -ENODEV;
6499 }
6500 phy_dev->supported &= PHY_GBIT_FEATURES;
6501 phy_dev->advertising = phy_dev->supported;
6502
Marcin Wojtas3f518502014-07-10 16:52:13 -03006503 port->link = 0;
6504 port->duplex = 0;
6505 port->speed = 0;
6506
6507 return 0;
6508}
6509
6510static void mvpp2_phy_disconnect(struct mvpp2_port *port)
6511{
Philippe Reynes8e072692016-06-28 00:08:11 +02006512 struct net_device *ndev = port->dev;
6513
6514 phy_disconnect(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006515}
6516
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006517static int mvpp2_irqs_init(struct mvpp2_port *port)
6518{
6519 int err, i;
6520
6521 for (i = 0; i < port->nqvecs; i++) {
6522 struct mvpp2_queue_vector *qv = port->qvecs + i;
6523
6524 err = request_irq(qv->irq, mvpp2_isr, 0, port->dev->name, qv);
6525 if (err)
6526 goto err;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006527
6528 if (qv->type == MVPP2_QUEUE_VECTOR_PRIVATE)
6529 irq_set_affinity_hint(qv->irq,
6530 cpumask_of(qv->sw_thread_id));
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006531 }
6532
6533 return 0;
6534err:
6535 for (i = 0; i < port->nqvecs; i++) {
6536 struct mvpp2_queue_vector *qv = port->qvecs + i;
6537
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006538 irq_set_affinity_hint(qv->irq, NULL);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006539 free_irq(qv->irq, qv);
6540 }
6541
6542 return err;
6543}
6544
6545static void mvpp2_irqs_deinit(struct mvpp2_port *port)
6546{
6547 int i;
6548
6549 for (i = 0; i < port->nqvecs; i++) {
6550 struct mvpp2_queue_vector *qv = port->qvecs + i;
6551
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006552 irq_set_affinity_hint(qv->irq, NULL);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006553 free_irq(qv->irq, qv);
6554 }
6555}
6556
Marcin Wojtas3f518502014-07-10 16:52:13 -03006557static int mvpp2_open(struct net_device *dev)
6558{
6559 struct mvpp2_port *port = netdev_priv(dev);
6560 unsigned char mac_bcast[ETH_ALEN] = {
6561 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
6562 int err;
6563
6564 err = mvpp2_prs_mac_da_accept(port->priv, port->id, mac_bcast, true);
6565 if (err) {
6566 netdev_err(dev, "mvpp2_prs_mac_da_accept BC failed\n");
6567 return err;
6568 }
6569 err = mvpp2_prs_mac_da_accept(port->priv, port->id,
6570 dev->dev_addr, true);
6571 if (err) {
6572 netdev_err(dev, "mvpp2_prs_mac_da_accept MC failed\n");
6573 return err;
6574 }
6575 err = mvpp2_prs_tag_mode_set(port->priv, port->id, MVPP2_TAG_TYPE_MH);
6576 if (err) {
6577 netdev_err(dev, "mvpp2_prs_tag_mode_set failed\n");
6578 return err;
6579 }
6580 err = mvpp2_prs_def_flow(port);
6581 if (err) {
6582 netdev_err(dev, "mvpp2_prs_def_flow failed\n");
6583 return err;
6584 }
6585
6586 /* Allocate the Rx/Tx queues */
6587 err = mvpp2_setup_rxqs(port);
6588 if (err) {
6589 netdev_err(port->dev, "cannot allocate Rx queues\n");
6590 return err;
6591 }
6592
6593 err = mvpp2_setup_txqs(port);
6594 if (err) {
6595 netdev_err(port->dev, "cannot allocate Tx queues\n");
6596 goto err_cleanup_rxqs;
6597 }
6598
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006599 err = mvpp2_irqs_init(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006600 if (err) {
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006601 netdev_err(port->dev, "cannot init IRQs\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03006602 goto err_cleanup_txqs;
6603 }
6604
6605 /* In default link is down */
6606 netif_carrier_off(port->dev);
6607
6608 err = mvpp2_phy_connect(port);
6609 if (err < 0)
6610 goto err_free_irq;
6611
6612 /* Unmask interrupts on all CPUs */
6613 on_each_cpu(mvpp2_interrupts_unmask, port, 1);
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006614 mvpp2_shared_interrupt_mask_unmask(port, false);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006615
6616 mvpp2_start_dev(port);
6617
6618 return 0;
6619
6620err_free_irq:
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006621 mvpp2_irqs_deinit(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006622err_cleanup_txqs:
6623 mvpp2_cleanup_txqs(port);
6624err_cleanup_rxqs:
6625 mvpp2_cleanup_rxqs(port);
6626 return err;
6627}
6628
6629static int mvpp2_stop(struct net_device *dev)
6630{
6631 struct mvpp2_port *port = netdev_priv(dev);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006632 struct mvpp2_port_pcpu *port_pcpu;
6633 int cpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006634
6635 mvpp2_stop_dev(port);
6636 mvpp2_phy_disconnect(port);
6637
6638 /* Mask interrupts on all CPUs */
6639 on_each_cpu(mvpp2_interrupts_mask, port, 1);
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006640 mvpp2_shared_interrupt_mask_unmask(port, true);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006641
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006642 mvpp2_irqs_deinit(port);
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006643 if (!port->has_tx_irqs) {
6644 for_each_present_cpu(cpu) {
6645 port_pcpu = per_cpu_ptr(port->pcpu, cpu);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006646
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006647 hrtimer_cancel(&port_pcpu->tx_done_timer);
6648 port_pcpu->timer_scheduled = false;
6649 tasklet_kill(&port_pcpu->tx_done_tasklet);
6650 }
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006651 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006652 mvpp2_cleanup_rxqs(port);
6653 mvpp2_cleanup_txqs(port);
6654
6655 return 0;
6656}
6657
6658static void mvpp2_set_rx_mode(struct net_device *dev)
6659{
6660 struct mvpp2_port *port = netdev_priv(dev);
6661 struct mvpp2 *priv = port->priv;
6662 struct netdev_hw_addr *ha;
6663 int id = port->id;
6664 bool allmulti = dev->flags & IFF_ALLMULTI;
6665
6666 mvpp2_prs_mac_promisc_set(priv, id, dev->flags & IFF_PROMISC);
6667 mvpp2_prs_mac_multi_set(priv, id, MVPP2_PE_MAC_MC_ALL, allmulti);
6668 mvpp2_prs_mac_multi_set(priv, id, MVPP2_PE_MAC_MC_IP6, allmulti);
6669
6670 /* Remove all port->id's mcast enries */
6671 mvpp2_prs_mcast_del_all(priv, id);
6672
6673 if (allmulti && !netdev_mc_empty(dev)) {
6674 netdev_for_each_mc_addr(ha, dev)
6675 mvpp2_prs_mac_da_accept(priv, id, ha->addr, true);
6676 }
6677}
6678
6679static int mvpp2_set_mac_address(struct net_device *dev, void *p)
6680{
6681 struct mvpp2_port *port = netdev_priv(dev);
6682 const struct sockaddr *addr = p;
6683 int err;
6684
6685 if (!is_valid_ether_addr(addr->sa_data)) {
6686 err = -EADDRNOTAVAIL;
Markus Elfringc1175542017-04-17 11:10:47 +02006687 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006688 }
6689
6690 if (!netif_running(dev)) {
6691 err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
6692 if (!err)
6693 return 0;
6694 /* Reconfigure parser to accept the original MAC address */
6695 err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
6696 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02006697 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006698 }
6699
6700 mvpp2_stop_dev(port);
6701
6702 err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
6703 if (!err)
6704 goto out_start;
6705
6706 /* Reconfigure parser accept the original MAC address */
6707 err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
6708 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02006709 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006710out_start:
6711 mvpp2_start_dev(port);
6712 mvpp2_egress_enable(port);
6713 mvpp2_ingress_enable(port);
6714 return 0;
Markus Elfringc1175542017-04-17 11:10:47 +02006715log_error:
Markus Elfringdfd42402017-04-17 11:20:41 +02006716 netdev_err(dev, "failed to change MAC address\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03006717 return err;
6718}
6719
6720static int mvpp2_change_mtu(struct net_device *dev, int mtu)
6721{
6722 struct mvpp2_port *port = netdev_priv(dev);
6723 int err;
6724
Jarod Wilson57779872016-10-17 15:54:06 -04006725 if (!IS_ALIGNED(MVPP2_RX_PKT_SIZE(mtu), 8)) {
6726 netdev_info(dev, "illegal MTU value %d, round to %d\n", mtu,
6727 ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8));
6728 mtu = ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006729 }
6730
6731 if (!netif_running(dev)) {
6732 err = mvpp2_bm_update_mtu(dev, mtu);
6733 if (!err) {
6734 port->pkt_size = MVPP2_RX_PKT_SIZE(mtu);
6735 return 0;
6736 }
6737
6738 /* Reconfigure BM to the original MTU */
6739 err = mvpp2_bm_update_mtu(dev, dev->mtu);
6740 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02006741 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006742 }
6743
6744 mvpp2_stop_dev(port);
6745
6746 err = mvpp2_bm_update_mtu(dev, mtu);
6747 if (!err) {
6748 port->pkt_size = MVPP2_RX_PKT_SIZE(mtu);
6749 goto out_start;
6750 }
6751
6752 /* Reconfigure BM to the original MTU */
6753 err = mvpp2_bm_update_mtu(dev, dev->mtu);
6754 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02006755 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006756
6757out_start:
6758 mvpp2_start_dev(port);
6759 mvpp2_egress_enable(port);
6760 mvpp2_ingress_enable(port);
6761
6762 return 0;
Markus Elfringc1175542017-04-17 11:10:47 +02006763log_error:
Markus Elfringdfd42402017-04-17 11:20:41 +02006764 netdev_err(dev, "failed to change MTU\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03006765 return err;
6766}
6767
stephen hemmingerbc1f4472017-01-06 19:12:52 -08006768static void
Marcin Wojtas3f518502014-07-10 16:52:13 -03006769mvpp2_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
6770{
6771 struct mvpp2_port *port = netdev_priv(dev);
6772 unsigned int start;
6773 int cpu;
6774
6775 for_each_possible_cpu(cpu) {
6776 struct mvpp2_pcpu_stats *cpu_stats;
6777 u64 rx_packets;
6778 u64 rx_bytes;
6779 u64 tx_packets;
6780 u64 tx_bytes;
6781
6782 cpu_stats = per_cpu_ptr(port->stats, cpu);
6783 do {
6784 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
6785 rx_packets = cpu_stats->rx_packets;
6786 rx_bytes = cpu_stats->rx_bytes;
6787 tx_packets = cpu_stats->tx_packets;
6788 tx_bytes = cpu_stats->tx_bytes;
6789 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
6790
6791 stats->rx_packets += rx_packets;
6792 stats->rx_bytes += rx_bytes;
6793 stats->tx_packets += tx_packets;
6794 stats->tx_bytes += tx_bytes;
6795 }
6796
6797 stats->rx_errors = dev->stats.rx_errors;
6798 stats->rx_dropped = dev->stats.rx_dropped;
6799 stats->tx_dropped = dev->stats.tx_dropped;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006800}
6801
Thomas Petazzonibd695a52014-07-27 23:21:36 +02006802static int mvpp2_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
6803{
Thomas Petazzonibd695a52014-07-27 23:21:36 +02006804 int ret;
6805
Philippe Reynes8e072692016-06-28 00:08:11 +02006806 if (!dev->phydev)
Thomas Petazzonibd695a52014-07-27 23:21:36 +02006807 return -ENOTSUPP;
6808
Philippe Reynes8e072692016-06-28 00:08:11 +02006809 ret = phy_mii_ioctl(dev->phydev, ifr, cmd);
Thomas Petazzonibd695a52014-07-27 23:21:36 +02006810 if (!ret)
6811 mvpp2_link_event(dev);
6812
6813 return ret;
6814}
6815
Marcin Wojtas3f518502014-07-10 16:52:13 -03006816/* Ethtool methods */
6817
Marcin Wojtas3f518502014-07-10 16:52:13 -03006818/* Set interrupt coalescing for ethtools */
6819static int mvpp2_ethtool_set_coalesce(struct net_device *dev,
6820 struct ethtool_coalesce *c)
6821{
6822 struct mvpp2_port *port = netdev_priv(dev);
6823 int queue;
6824
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006825 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006826 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
6827
6828 rxq->time_coal = c->rx_coalesce_usecs;
6829 rxq->pkts_coal = c->rx_max_coalesced_frames;
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01006830 mvpp2_rx_pkts_coal_set(port, rxq);
6831 mvpp2_rx_time_coal_set(port, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006832 }
6833
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006834 if (port->has_tx_irqs) {
6835 port->tx_time_coal = c->tx_coalesce_usecs;
6836 mvpp2_tx_time_coal_set(port);
6837 }
6838
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006839 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006840 struct mvpp2_tx_queue *txq = port->txqs[queue];
6841
6842 txq->done_pkts_coal = c->tx_max_coalesced_frames;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006843
6844 if (port->has_tx_irqs)
6845 mvpp2_tx_pkts_coal_set(port, txq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006846 }
6847
Marcin Wojtas3f518502014-07-10 16:52:13 -03006848 return 0;
6849}
6850
6851/* get coalescing for ethtools */
6852static int mvpp2_ethtool_get_coalesce(struct net_device *dev,
6853 struct ethtool_coalesce *c)
6854{
6855 struct mvpp2_port *port = netdev_priv(dev);
6856
6857 c->rx_coalesce_usecs = port->rxqs[0]->time_coal;
6858 c->rx_max_coalesced_frames = port->rxqs[0]->pkts_coal;
6859 c->tx_max_coalesced_frames = port->txqs[0]->done_pkts_coal;
6860 return 0;
6861}
6862
6863static void mvpp2_ethtool_get_drvinfo(struct net_device *dev,
6864 struct ethtool_drvinfo *drvinfo)
6865{
6866 strlcpy(drvinfo->driver, MVPP2_DRIVER_NAME,
6867 sizeof(drvinfo->driver));
6868 strlcpy(drvinfo->version, MVPP2_DRIVER_VERSION,
6869 sizeof(drvinfo->version));
6870 strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
6871 sizeof(drvinfo->bus_info));
6872}
6873
6874static void mvpp2_ethtool_get_ringparam(struct net_device *dev,
6875 struct ethtool_ringparam *ring)
6876{
6877 struct mvpp2_port *port = netdev_priv(dev);
6878
6879 ring->rx_max_pending = MVPP2_MAX_RXD;
6880 ring->tx_max_pending = MVPP2_MAX_TXD;
6881 ring->rx_pending = port->rx_ring_size;
6882 ring->tx_pending = port->tx_ring_size;
6883}
6884
6885static int mvpp2_ethtool_set_ringparam(struct net_device *dev,
6886 struct ethtool_ringparam *ring)
6887{
6888 struct mvpp2_port *port = netdev_priv(dev);
6889 u16 prev_rx_ring_size = port->rx_ring_size;
6890 u16 prev_tx_ring_size = port->tx_ring_size;
6891 int err;
6892
6893 err = mvpp2_check_ringparam_valid(dev, ring);
6894 if (err)
6895 return err;
6896
6897 if (!netif_running(dev)) {
6898 port->rx_ring_size = ring->rx_pending;
6899 port->tx_ring_size = ring->tx_pending;
6900 return 0;
6901 }
6902
6903 /* The interface is running, so we have to force a
6904 * reallocation of the queues
6905 */
6906 mvpp2_stop_dev(port);
6907 mvpp2_cleanup_rxqs(port);
6908 mvpp2_cleanup_txqs(port);
6909
6910 port->rx_ring_size = ring->rx_pending;
6911 port->tx_ring_size = ring->tx_pending;
6912
6913 err = mvpp2_setup_rxqs(port);
6914 if (err) {
6915 /* Reallocate Rx queues with the original ring size */
6916 port->rx_ring_size = prev_rx_ring_size;
6917 ring->rx_pending = prev_rx_ring_size;
6918 err = mvpp2_setup_rxqs(port);
6919 if (err)
6920 goto err_out;
6921 }
6922 err = mvpp2_setup_txqs(port);
6923 if (err) {
6924 /* Reallocate Tx queues with the original ring size */
6925 port->tx_ring_size = prev_tx_ring_size;
6926 ring->tx_pending = prev_tx_ring_size;
6927 err = mvpp2_setup_txqs(port);
6928 if (err)
6929 goto err_clean_rxqs;
6930 }
6931
6932 mvpp2_start_dev(port);
6933 mvpp2_egress_enable(port);
6934 mvpp2_ingress_enable(port);
6935
6936 return 0;
6937
6938err_clean_rxqs:
6939 mvpp2_cleanup_rxqs(port);
6940err_out:
Markus Elfringdfd42402017-04-17 11:20:41 +02006941 netdev_err(dev, "failed to change ring parameters");
Marcin Wojtas3f518502014-07-10 16:52:13 -03006942 return err;
6943}
6944
6945/* Device ops */
6946
6947static const struct net_device_ops mvpp2_netdev_ops = {
6948 .ndo_open = mvpp2_open,
6949 .ndo_stop = mvpp2_stop,
6950 .ndo_start_xmit = mvpp2_tx,
6951 .ndo_set_rx_mode = mvpp2_set_rx_mode,
6952 .ndo_set_mac_address = mvpp2_set_mac_address,
6953 .ndo_change_mtu = mvpp2_change_mtu,
6954 .ndo_get_stats64 = mvpp2_get_stats64,
Thomas Petazzonibd695a52014-07-27 23:21:36 +02006955 .ndo_do_ioctl = mvpp2_ioctl,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006956};
6957
6958static const struct ethtool_ops mvpp2_eth_tool_ops = {
Florian Fainelli00606c42016-11-15 11:19:48 -08006959 .nway_reset = phy_ethtool_nway_reset,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006960 .get_link = ethtool_op_get_link,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006961 .set_coalesce = mvpp2_ethtool_set_coalesce,
6962 .get_coalesce = mvpp2_ethtool_get_coalesce,
6963 .get_drvinfo = mvpp2_ethtool_get_drvinfo,
6964 .get_ringparam = mvpp2_ethtool_get_ringparam,
6965 .set_ringparam = mvpp2_ethtool_set_ringparam,
Philippe Reynesfb773e92016-06-28 00:08:12 +02006966 .get_link_ksettings = phy_ethtool_get_link_ksettings,
6967 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006968};
6969
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006970/* Used for PPv2.1, or PPv2.2 with the old Device Tree binding that
6971 * had a single IRQ defined per-port.
6972 */
6973static int mvpp2_simple_queue_vectors_init(struct mvpp2_port *port,
6974 struct device_node *port_node)
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006975{
6976 struct mvpp2_queue_vector *v = &port->qvecs[0];
6977
6978 v->first_rxq = 0;
6979 v->nrxqs = port->nrxqs;
6980 v->type = MVPP2_QUEUE_VECTOR_SHARED;
6981 v->sw_thread_id = 0;
6982 v->sw_thread_mask = *cpumask_bits(cpu_online_mask);
6983 v->port = port;
6984 v->irq = irq_of_parse_and_map(port_node, 0);
6985 if (v->irq <= 0)
6986 return -EINVAL;
6987 netif_napi_add(port->dev, &v->napi, mvpp2_poll,
6988 NAPI_POLL_WEIGHT);
6989
6990 port->nqvecs = 1;
6991
6992 return 0;
6993}
6994
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006995static int mvpp2_multi_queue_vectors_init(struct mvpp2_port *port,
6996 struct device_node *port_node)
6997{
6998 struct mvpp2_queue_vector *v;
6999 int i, ret;
7000
7001 port->nqvecs = num_possible_cpus();
7002 if (queue_mode == MVPP2_QDIST_SINGLE_MODE)
7003 port->nqvecs += 1;
7004
7005 for (i = 0; i < port->nqvecs; i++) {
7006 char irqname[16];
7007
7008 v = port->qvecs + i;
7009
7010 v->port = port;
7011 v->type = MVPP2_QUEUE_VECTOR_PRIVATE;
7012 v->sw_thread_id = i;
7013 v->sw_thread_mask = BIT(i);
7014
7015 snprintf(irqname, sizeof(irqname), "tx-cpu%d", i);
7016
7017 if (queue_mode == MVPP2_QDIST_MULTI_MODE) {
7018 v->first_rxq = i * MVPP2_DEFAULT_RXQ;
7019 v->nrxqs = MVPP2_DEFAULT_RXQ;
7020 } else if (queue_mode == MVPP2_QDIST_SINGLE_MODE &&
7021 i == (port->nqvecs - 1)) {
7022 v->first_rxq = 0;
7023 v->nrxqs = port->nrxqs;
7024 v->type = MVPP2_QUEUE_VECTOR_SHARED;
7025 strncpy(irqname, "rx-shared", sizeof(irqname));
7026 }
7027
7028 v->irq = of_irq_get_byname(port_node, irqname);
7029 if (v->irq <= 0) {
7030 ret = -EINVAL;
7031 goto err;
7032 }
7033
7034 netif_napi_add(port->dev, &v->napi, mvpp2_poll,
7035 NAPI_POLL_WEIGHT);
7036 }
7037
7038 return 0;
7039
7040err:
7041 for (i = 0; i < port->nqvecs; i++)
7042 irq_dispose_mapping(port->qvecs[i].irq);
7043 return ret;
7044}
7045
7046static int mvpp2_queue_vectors_init(struct mvpp2_port *port,
7047 struct device_node *port_node)
7048{
7049 if (port->has_tx_irqs)
7050 return mvpp2_multi_queue_vectors_init(port, port_node);
7051 else
7052 return mvpp2_simple_queue_vectors_init(port, port_node);
7053}
7054
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007055static void mvpp2_queue_vectors_deinit(struct mvpp2_port *port)
7056{
7057 int i;
7058
7059 for (i = 0; i < port->nqvecs; i++)
7060 irq_dispose_mapping(port->qvecs[i].irq);
7061}
7062
7063/* Configure Rx queue group interrupt for this port */
7064static void mvpp2_rx_irqs_setup(struct mvpp2_port *port)
7065{
7066 struct mvpp2 *priv = port->priv;
7067 u32 val;
7068 int i;
7069
7070 if (priv->hw_version == MVPP21) {
7071 mvpp2_write(priv, MVPP21_ISR_RXQ_GROUP_REG(port->id),
7072 port->nrxqs);
7073 return;
7074 }
7075
7076 /* Handle the more complicated PPv2.2 case */
7077 for (i = 0; i < port->nqvecs; i++) {
7078 struct mvpp2_queue_vector *qv = port->qvecs + i;
7079
7080 if (!qv->nrxqs)
7081 continue;
7082
7083 val = qv->sw_thread_id;
7084 val |= port->id << MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_OFFSET;
7085 mvpp2_write(priv, MVPP22_ISR_RXQ_GROUP_INDEX_REG, val);
7086
7087 val = qv->first_rxq;
7088 val |= qv->nrxqs << MVPP22_ISR_RXQ_SUB_GROUP_SIZE_OFFSET;
7089 mvpp2_write(priv, MVPP22_ISR_RXQ_SUB_GROUP_CONFIG_REG, val);
7090 }
7091}
7092
Marcin Wojtas3f518502014-07-10 16:52:13 -03007093/* Initialize port HW */
7094static int mvpp2_port_init(struct mvpp2_port *port)
7095{
7096 struct device *dev = port->dev->dev.parent;
7097 struct mvpp2 *priv = port->priv;
7098 struct mvpp2_txq_pcpu *txq_pcpu;
7099 int queue, cpu, err;
7100
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007101 /* Checks for hardware constraints */
7102 if (port->first_rxq + port->nrxqs >
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01007103 MVPP2_MAX_PORTS * priv->max_port_rxqs)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007104 return -EINVAL;
7105
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007106 if (port->nrxqs % 4 || (port->nrxqs > priv->max_port_rxqs) ||
7107 (port->ntxqs > MVPP2_MAX_TXQ))
7108 return -EINVAL;
7109
Marcin Wojtas3f518502014-07-10 16:52:13 -03007110 /* Disable port */
7111 mvpp2_egress_disable(port);
7112 mvpp2_port_disable(port);
7113
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007114 port->tx_time_coal = MVPP2_TXDONE_COAL_USEC;
7115
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007116 port->txqs = devm_kcalloc(dev, port->ntxqs, sizeof(*port->txqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03007117 GFP_KERNEL);
7118 if (!port->txqs)
7119 return -ENOMEM;
7120
7121 /* Associate physical Tx queues to this port and initialize.
7122 * The mapping is predefined.
7123 */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007124 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007125 int queue_phy_id = mvpp2_txq_phys(port->id, queue);
7126 struct mvpp2_tx_queue *txq;
7127
7128 txq = devm_kzalloc(dev, sizeof(*txq), GFP_KERNEL);
Christophe Jaillet177c8d12017-02-19 10:19:57 +01007129 if (!txq) {
7130 err = -ENOMEM;
7131 goto err_free_percpu;
7132 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03007133
7134 txq->pcpu = alloc_percpu(struct mvpp2_txq_pcpu);
7135 if (!txq->pcpu) {
7136 err = -ENOMEM;
7137 goto err_free_percpu;
7138 }
7139
7140 txq->id = queue_phy_id;
7141 txq->log_id = queue;
7142 txq->done_pkts_coal = MVPP2_TXDONE_COAL_PKTS_THRESH;
7143 for_each_present_cpu(cpu) {
7144 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
7145 txq_pcpu->cpu = cpu;
7146 }
7147
7148 port->txqs[queue] = txq;
7149 }
7150
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007151 port->rxqs = devm_kcalloc(dev, port->nrxqs, sizeof(*port->rxqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03007152 GFP_KERNEL);
7153 if (!port->rxqs) {
7154 err = -ENOMEM;
7155 goto err_free_percpu;
7156 }
7157
7158 /* Allocate and initialize Rx queue for this port */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007159 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007160 struct mvpp2_rx_queue *rxq;
7161
7162 /* Map physical Rx queue to port's logical Rx queue */
7163 rxq = devm_kzalloc(dev, sizeof(*rxq), GFP_KERNEL);
Jisheng Zhangd82b0c22016-03-31 17:01:23 +08007164 if (!rxq) {
7165 err = -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007166 goto err_free_percpu;
Jisheng Zhangd82b0c22016-03-31 17:01:23 +08007167 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03007168 /* Map this Rx queue to a physical queue */
7169 rxq->id = port->first_rxq + queue;
7170 rxq->port = port->id;
7171 rxq->logic_rxq = queue;
7172
7173 port->rxqs[queue] = rxq;
7174 }
7175
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007176 mvpp2_rx_irqs_setup(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007177
7178 /* Create Rx descriptor rings */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007179 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007180 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
7181
7182 rxq->size = port->rx_ring_size;
7183 rxq->pkts_coal = MVPP2_RX_COAL_PKTS;
7184 rxq->time_coal = MVPP2_RX_COAL_USEC;
7185 }
7186
7187 mvpp2_ingress_disable(port);
7188
7189 /* Port default configuration */
7190 mvpp2_defaults_set(port);
7191
7192 /* Port's classifier configuration */
7193 mvpp2_cls_oversize_rxq_set(port);
7194 mvpp2_cls_port_config(port);
7195
7196 /* Provide an initial Rx packet size */
7197 port->pkt_size = MVPP2_RX_PKT_SIZE(port->dev->mtu);
7198
7199 /* Initialize pools for swf */
7200 err = mvpp2_swf_bm_pool_init(port);
7201 if (err)
7202 goto err_free_percpu;
7203
7204 return 0;
7205
7206err_free_percpu:
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007207 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007208 if (!port->txqs[queue])
7209 continue;
7210 free_percpu(port->txqs[queue]->pcpu);
7211 }
7212 return err;
7213}
7214
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007215/* Checks if the port DT description has the TX interrupts
7216 * described. On PPv2.1, there are no such interrupts. On PPv2.2,
7217 * there are available, but we need to keep support for old DTs.
7218 */
7219static bool mvpp2_port_has_tx_irqs(struct mvpp2 *priv,
7220 struct device_node *port_node)
7221{
7222 char *irqs[5] = { "rx-shared", "tx-cpu0", "tx-cpu1",
7223 "tx-cpu2", "tx-cpu3" };
7224 int ret, i;
7225
7226 if (priv->hw_version == MVPP21)
7227 return false;
7228
7229 for (i = 0; i < 5; i++) {
7230 ret = of_property_match_string(port_node, "interrupt-names",
7231 irqs[i]);
7232 if (ret < 0)
7233 return false;
7234 }
7235
7236 return true;
7237}
7238
Marcin Wojtas3f518502014-07-10 16:52:13 -03007239/* Ports initialization */
7240static int mvpp2_port_probe(struct platform_device *pdev,
7241 struct device_node *port_node,
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01007242 struct mvpp2 *priv)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007243{
7244 struct device_node *phy_node;
7245 struct mvpp2_port *port;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007246 struct mvpp2_port_pcpu *port_pcpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007247 struct net_device *dev;
7248 struct resource *res;
7249 const char *dt_mac_addr;
7250 const char *mac_from;
7251 char hw_mac_addr[ETH_ALEN];
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007252 unsigned int ntxqs, nrxqs;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007253 bool has_tx_irqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007254 u32 id;
7255 int features;
7256 int phy_mode;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007257 int err, i, cpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007258
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007259 has_tx_irqs = mvpp2_port_has_tx_irqs(priv, port_node);
7260
7261 if (!has_tx_irqs)
7262 queue_mode = MVPP2_QDIST_SINGLE_MODE;
7263
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007264 ntxqs = MVPP2_MAX_TXQ;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007265 if (priv->hw_version == MVPP22 && queue_mode == MVPP2_QDIST_MULTI_MODE)
7266 nrxqs = MVPP2_DEFAULT_RXQ * num_possible_cpus();
7267 else
7268 nrxqs = MVPP2_DEFAULT_RXQ;
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007269
7270 dev = alloc_etherdev_mqs(sizeof(*port), ntxqs, nrxqs);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007271 if (!dev)
7272 return -ENOMEM;
7273
7274 phy_node = of_parse_phandle(port_node, "phy", 0);
7275 if (!phy_node) {
7276 dev_err(&pdev->dev, "missing phy\n");
7277 err = -ENODEV;
7278 goto err_free_netdev;
7279 }
7280
7281 phy_mode = of_get_phy_mode(port_node);
7282 if (phy_mode < 0) {
7283 dev_err(&pdev->dev, "incorrect phy mode\n");
7284 err = phy_mode;
7285 goto err_free_netdev;
7286 }
7287
7288 if (of_property_read_u32(port_node, "port-id", &id)) {
7289 err = -EINVAL;
7290 dev_err(&pdev->dev, "missing port-id value\n");
7291 goto err_free_netdev;
7292 }
7293
7294 dev->tx_queue_len = MVPP2_MAX_TXD;
7295 dev->watchdog_timeo = 5 * HZ;
7296 dev->netdev_ops = &mvpp2_netdev_ops;
7297 dev->ethtool_ops = &mvpp2_eth_tool_ops;
7298
7299 port = netdev_priv(dev);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007300 port->dev = dev;
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007301 port->ntxqs = ntxqs;
7302 port->nrxqs = nrxqs;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007303 port->priv = priv;
7304 port->has_tx_irqs = has_tx_irqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007305
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007306 err = mvpp2_queue_vectors_init(port, port_node);
7307 if (err)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007308 goto err_free_netdev;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007309
7310 if (of_property_read_bool(port_node, "marvell,loopback"))
7311 port->flags |= MVPP2_F_LOOPBACK;
7312
Marcin Wojtas3f518502014-07-10 16:52:13 -03007313 port->id = id;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01007314 if (priv->hw_version == MVPP21)
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007315 port->first_rxq = port->id * port->nrxqs;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01007316 else
7317 port->first_rxq = port->id * priv->max_port_rxqs;
7318
Marcin Wojtas3f518502014-07-10 16:52:13 -03007319 port->phy_node = phy_node;
7320 port->phy_interface = phy_mode;
7321
Thomas Petazzonia7868412017-03-07 16:53:13 +01007322 if (priv->hw_version == MVPP21) {
7323 res = platform_get_resource(pdev, IORESOURCE_MEM, 2 + id);
7324 port->base = devm_ioremap_resource(&pdev->dev, res);
7325 if (IS_ERR(port->base)) {
7326 err = PTR_ERR(port->base);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007327 goto err_deinit_qvecs;
Thomas Petazzonia7868412017-03-07 16:53:13 +01007328 }
7329 } else {
7330 if (of_property_read_u32(port_node, "gop-port-id",
7331 &port->gop_id)) {
7332 err = -EINVAL;
7333 dev_err(&pdev->dev, "missing gop-port-id value\n");
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007334 goto err_deinit_qvecs;
Thomas Petazzonia7868412017-03-07 16:53:13 +01007335 }
7336
7337 port->base = priv->iface_base + MVPP22_GMAC_BASE(port->gop_id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007338 }
7339
7340 /* Alloc per-cpu stats */
7341 port->stats = netdev_alloc_pcpu_stats(struct mvpp2_pcpu_stats);
7342 if (!port->stats) {
7343 err = -ENOMEM;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007344 goto err_deinit_qvecs;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007345 }
7346
7347 dt_mac_addr = of_get_mac_address(port_node);
7348 if (dt_mac_addr && is_valid_ether_addr(dt_mac_addr)) {
7349 mac_from = "device tree";
7350 ether_addr_copy(dev->dev_addr, dt_mac_addr);
7351 } else {
Thomas Petazzoni26975822017-03-07 16:53:14 +01007352 if (priv->hw_version == MVPP21)
7353 mvpp21_get_mac_address(port, hw_mac_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007354 if (is_valid_ether_addr(hw_mac_addr)) {
7355 mac_from = "hardware";
7356 ether_addr_copy(dev->dev_addr, hw_mac_addr);
7357 } else {
7358 mac_from = "random";
7359 eth_hw_addr_random(dev);
7360 }
7361 }
7362
7363 port->tx_ring_size = MVPP2_MAX_TXD;
7364 port->rx_ring_size = MVPP2_MAX_RXD;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007365 SET_NETDEV_DEV(dev, &pdev->dev);
7366
7367 err = mvpp2_port_init(port);
7368 if (err < 0) {
7369 dev_err(&pdev->dev, "failed to init port %d\n", id);
7370 goto err_free_stats;
7371 }
Thomas Petazzoni26975822017-03-07 16:53:14 +01007372
Thomas Petazzoni26975822017-03-07 16:53:14 +01007373 mvpp2_port_periodic_xon_disable(port);
7374
7375 if (priv->hw_version == MVPP21)
7376 mvpp2_port_fc_adv_enable(port);
7377
7378 mvpp2_port_reset(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007379
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007380 port->pcpu = alloc_percpu(struct mvpp2_port_pcpu);
7381 if (!port->pcpu) {
7382 err = -ENOMEM;
7383 goto err_free_txq_pcpu;
7384 }
7385
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007386 if (!port->has_tx_irqs) {
7387 for_each_present_cpu(cpu) {
7388 port_pcpu = per_cpu_ptr(port->pcpu, cpu);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007389
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007390 hrtimer_init(&port_pcpu->tx_done_timer, CLOCK_MONOTONIC,
7391 HRTIMER_MODE_REL_PINNED);
7392 port_pcpu->tx_done_timer.function = mvpp2_hr_timer_cb;
7393 port_pcpu->timer_scheduled = false;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007394
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007395 tasklet_init(&port_pcpu->tx_done_tasklet,
7396 mvpp2_tx_proc_cb,
7397 (unsigned long)dev);
7398 }
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007399 }
7400
Antoine Ténart186cd4d2017-08-23 09:46:56 +02007401 features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007402 dev->features = features | NETIF_F_RXCSUM;
7403 dev->hw_features |= features | NETIF_F_RXCSUM | NETIF_F_GRO;
7404 dev->vlan_features |= features;
7405
Jarod Wilson57779872016-10-17 15:54:06 -04007406 /* MTU range: 68 - 9676 */
7407 dev->min_mtu = ETH_MIN_MTU;
7408 /* 9676 == 9700 - 20 and rounding to 8 */
7409 dev->max_mtu = 9676;
7410
Marcin Wojtas3f518502014-07-10 16:52:13 -03007411 err = register_netdev(dev);
7412 if (err < 0) {
7413 dev_err(&pdev->dev, "failed to register netdev\n");
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007414 goto err_free_port_pcpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007415 }
7416 netdev_info(dev, "Using %s mac address %pM\n", mac_from, dev->dev_addr);
7417
Marcin Wojtas3f518502014-07-10 16:52:13 -03007418 priv->port_list[id] = port;
7419 return 0;
7420
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007421err_free_port_pcpu:
7422 free_percpu(port->pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007423err_free_txq_pcpu:
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007424 for (i = 0; i < port->ntxqs; i++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007425 free_percpu(port->txqs[i]->pcpu);
7426err_free_stats:
7427 free_percpu(port->stats);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007428err_deinit_qvecs:
7429 mvpp2_queue_vectors_deinit(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007430err_free_netdev:
Peter Chenccb80392016-08-01 15:02:37 +08007431 of_node_put(phy_node);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007432 free_netdev(dev);
7433 return err;
7434}
7435
7436/* Ports removal routine */
7437static void mvpp2_port_remove(struct mvpp2_port *port)
7438{
7439 int i;
7440
7441 unregister_netdev(port->dev);
Peter Chenccb80392016-08-01 15:02:37 +08007442 of_node_put(port->phy_node);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007443 free_percpu(port->pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007444 free_percpu(port->stats);
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007445 for (i = 0; i < port->ntxqs; i++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007446 free_percpu(port->txqs[i]->pcpu);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007447 mvpp2_queue_vectors_deinit(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007448 free_netdev(port->dev);
7449}
7450
7451/* Initialize decoding windows */
7452static void mvpp2_conf_mbus_windows(const struct mbus_dram_target_info *dram,
7453 struct mvpp2 *priv)
7454{
7455 u32 win_enable;
7456 int i;
7457
7458 for (i = 0; i < 6; i++) {
7459 mvpp2_write(priv, MVPP2_WIN_BASE(i), 0);
7460 mvpp2_write(priv, MVPP2_WIN_SIZE(i), 0);
7461
7462 if (i < 4)
7463 mvpp2_write(priv, MVPP2_WIN_REMAP(i), 0);
7464 }
7465
7466 win_enable = 0;
7467
7468 for (i = 0; i < dram->num_cs; i++) {
7469 const struct mbus_dram_window *cs = dram->cs + i;
7470
7471 mvpp2_write(priv, MVPP2_WIN_BASE(i),
7472 (cs->base & 0xffff0000) | (cs->mbus_attr << 8) |
7473 dram->mbus_dram_target_id);
7474
7475 mvpp2_write(priv, MVPP2_WIN_SIZE(i),
7476 (cs->size - 1) & 0xffff0000);
7477
7478 win_enable |= (1 << i);
7479 }
7480
7481 mvpp2_write(priv, MVPP2_BASE_ADDR_ENABLE, win_enable);
7482}
7483
7484/* Initialize Rx FIFO's */
7485static void mvpp2_rx_fifo_init(struct mvpp2 *priv)
7486{
7487 int port;
7488
7489 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
7490 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
7491 MVPP2_RX_FIFO_PORT_DATA_SIZE);
7492 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
7493 MVPP2_RX_FIFO_PORT_ATTR_SIZE);
7494 }
7495
7496 mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
7497 MVPP2_RX_FIFO_PORT_MIN_PKT);
7498 mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
7499}
7500
Thomas Petazzoni6763ce32017-03-07 16:53:15 +01007501static void mvpp2_axi_init(struct mvpp2 *priv)
7502{
7503 u32 val, rdval, wrval;
7504
7505 mvpp2_write(priv, MVPP22_BM_ADDR_HIGH_RLS_REG, 0x0);
7506
7507 /* AXI Bridge Configuration */
7508
7509 rdval = MVPP22_AXI_CODE_CACHE_RD_CACHE
7510 << MVPP22_AXI_ATTR_CACHE_OFFS;
7511 rdval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
7512 << MVPP22_AXI_ATTR_DOMAIN_OFFS;
7513
7514 wrval = MVPP22_AXI_CODE_CACHE_WR_CACHE
7515 << MVPP22_AXI_ATTR_CACHE_OFFS;
7516 wrval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
7517 << MVPP22_AXI_ATTR_DOMAIN_OFFS;
7518
7519 /* BM */
7520 mvpp2_write(priv, MVPP22_AXI_BM_WR_ATTR_REG, wrval);
7521 mvpp2_write(priv, MVPP22_AXI_BM_RD_ATTR_REG, rdval);
7522
7523 /* Descriptors */
7524 mvpp2_write(priv, MVPP22_AXI_AGGRQ_DESCR_RD_ATTR_REG, rdval);
7525 mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_WR_ATTR_REG, wrval);
7526 mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_RD_ATTR_REG, rdval);
7527 mvpp2_write(priv, MVPP22_AXI_RXQ_DESCR_WR_ATTR_REG, wrval);
7528
7529 /* Buffer Data */
7530 mvpp2_write(priv, MVPP22_AXI_TX_DATA_RD_ATTR_REG, rdval);
7531 mvpp2_write(priv, MVPP22_AXI_RX_DATA_WR_ATTR_REG, wrval);
7532
7533 val = MVPP22_AXI_CODE_CACHE_NON_CACHE
7534 << MVPP22_AXI_CODE_CACHE_OFFS;
7535 val |= MVPP22_AXI_CODE_DOMAIN_SYSTEM
7536 << MVPP22_AXI_CODE_DOMAIN_OFFS;
7537 mvpp2_write(priv, MVPP22_AXI_RD_NORMAL_CODE_REG, val);
7538 mvpp2_write(priv, MVPP22_AXI_WR_NORMAL_CODE_REG, val);
7539
7540 val = MVPP22_AXI_CODE_CACHE_RD_CACHE
7541 << MVPP22_AXI_CODE_CACHE_OFFS;
7542 val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
7543 << MVPP22_AXI_CODE_DOMAIN_OFFS;
7544
7545 mvpp2_write(priv, MVPP22_AXI_RD_SNOOP_CODE_REG, val);
7546
7547 val = MVPP22_AXI_CODE_CACHE_WR_CACHE
7548 << MVPP22_AXI_CODE_CACHE_OFFS;
7549 val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
7550 << MVPP22_AXI_CODE_DOMAIN_OFFS;
7551
7552 mvpp2_write(priv, MVPP22_AXI_WR_SNOOP_CODE_REG, val);
7553}
7554
Marcin Wojtas3f518502014-07-10 16:52:13 -03007555/* Initialize network controller common part HW */
7556static int mvpp2_init(struct platform_device *pdev, struct mvpp2 *priv)
7557{
7558 const struct mbus_dram_target_info *dram_target_info;
7559 int err, i;
Marcin Wojtas08a23752014-07-21 13:48:12 -03007560 u32 val;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007561
Marcin Wojtas3f518502014-07-10 16:52:13 -03007562 /* MBUS windows configuration */
7563 dram_target_info = mv_mbus_dram_info();
7564 if (dram_target_info)
7565 mvpp2_conf_mbus_windows(dram_target_info, priv);
7566
Thomas Petazzoni6763ce32017-03-07 16:53:15 +01007567 if (priv->hw_version == MVPP22)
7568 mvpp2_axi_init(priv);
7569
Marcin Wojtas08a23752014-07-21 13:48:12 -03007570 /* Disable HW PHY polling */
Thomas Petazzoni26975822017-03-07 16:53:14 +01007571 if (priv->hw_version == MVPP21) {
7572 val = readl(priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
7573 val |= MVPP2_PHY_AN_STOP_SMI0_MASK;
7574 writel(val, priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
7575 } else {
7576 val = readl(priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
7577 val &= ~MVPP22_SMI_POLLING_EN;
7578 writel(val, priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
7579 }
Marcin Wojtas08a23752014-07-21 13:48:12 -03007580
Marcin Wojtas3f518502014-07-10 16:52:13 -03007581 /* Allocate and initialize aggregated TXQs */
7582 priv->aggr_txqs = devm_kcalloc(&pdev->dev, num_present_cpus(),
Markus Elfringd7ce3ce2017-04-17 08:48:23 +02007583 sizeof(*priv->aggr_txqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03007584 GFP_KERNEL);
7585 if (!priv->aggr_txqs)
7586 return -ENOMEM;
7587
7588 for_each_present_cpu(i) {
7589 priv->aggr_txqs[i].id = i;
7590 priv->aggr_txqs[i].size = MVPP2_AGGR_TXQ_SIZE;
Antoine Ténart85affd72017-08-23 09:46:55 +02007591 err = mvpp2_aggr_txq_init(pdev, &priv->aggr_txqs[i], i, priv);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007592 if (err < 0)
7593 return err;
7594 }
7595
7596 /* Rx Fifo Init */
7597 mvpp2_rx_fifo_init(priv);
7598
Thomas Petazzoni26975822017-03-07 16:53:14 +01007599 if (priv->hw_version == MVPP21)
7600 writel(MVPP2_EXT_GLOBAL_CTRL_DEFAULT,
7601 priv->lms_base + MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007602
7603 /* Allow cache snoop when transmiting packets */
7604 mvpp2_write(priv, MVPP2_TX_SNOOP_REG, 0x1);
7605
7606 /* Buffer Manager initialization */
7607 err = mvpp2_bm_init(pdev, priv);
7608 if (err < 0)
7609 return err;
7610
7611 /* Parser default initialization */
7612 err = mvpp2_prs_default_init(pdev, priv);
7613 if (err < 0)
7614 return err;
7615
7616 /* Classifier default initialization */
7617 mvpp2_cls_init(priv);
7618
7619 return 0;
7620}
7621
7622static int mvpp2_probe(struct platform_device *pdev)
7623{
7624 struct device_node *dn = pdev->dev.of_node;
7625 struct device_node *port_node;
7626 struct mvpp2 *priv;
7627 struct resource *res;
Thomas Petazzonia7868412017-03-07 16:53:13 +01007628 void __iomem *base;
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02007629 int port_count, i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007630 int err;
7631
Markus Elfring0b92e592017-04-17 08:38:32 +02007632 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007633 if (!priv)
7634 return -ENOMEM;
7635
Thomas Petazzonifaca9242017-03-07 16:53:06 +01007636 priv->hw_version =
7637 (unsigned long)of_device_get_match_data(&pdev->dev);
7638
Marcin Wojtas3f518502014-07-10 16:52:13 -03007639 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thomas Petazzonia7868412017-03-07 16:53:13 +01007640 base = devm_ioremap_resource(&pdev->dev, res);
7641 if (IS_ERR(base))
7642 return PTR_ERR(base);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007643
Thomas Petazzonia7868412017-03-07 16:53:13 +01007644 if (priv->hw_version == MVPP21) {
7645 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
7646 priv->lms_base = devm_ioremap_resource(&pdev->dev, res);
7647 if (IS_ERR(priv->lms_base))
7648 return PTR_ERR(priv->lms_base);
7649 } else {
7650 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
7651 priv->iface_base = devm_ioremap_resource(&pdev->dev, res);
7652 if (IS_ERR(priv->iface_base))
7653 return PTR_ERR(priv->iface_base);
Antoine Ténartf84bf382017-08-22 19:08:27 +02007654
7655 priv->sysctrl_base =
7656 syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
7657 "marvell,system-controller");
7658 if (IS_ERR(priv->sysctrl_base))
7659 /* The system controller regmap is optional for dt
7660 * compatibility reasons. When not provided, the
7661 * configuration of the GoP relies on the
7662 * firmware/bootloader.
7663 */
7664 priv->sysctrl_base = NULL;
Thomas Petazzonia7868412017-03-07 16:53:13 +01007665 }
7666
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02007667 for (i = 0; i < MVPP2_MAX_THREADS; i++) {
Thomas Petazzonia7868412017-03-07 16:53:13 +01007668 u32 addr_space_sz;
7669
7670 addr_space_sz = (priv->hw_version == MVPP21 ?
7671 MVPP21_ADDR_SPACE_SZ : MVPP22_ADDR_SPACE_SZ);
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02007672 priv->swth_base[i] = base + i * addr_space_sz;
Thomas Petazzonia7868412017-03-07 16:53:13 +01007673 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03007674
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01007675 if (priv->hw_version == MVPP21)
7676 priv->max_port_rxqs = 8;
7677 else
7678 priv->max_port_rxqs = 32;
7679
Marcin Wojtas3f518502014-07-10 16:52:13 -03007680 priv->pp_clk = devm_clk_get(&pdev->dev, "pp_clk");
7681 if (IS_ERR(priv->pp_clk))
7682 return PTR_ERR(priv->pp_clk);
7683 err = clk_prepare_enable(priv->pp_clk);
7684 if (err < 0)
7685 return err;
7686
7687 priv->gop_clk = devm_clk_get(&pdev->dev, "gop_clk");
7688 if (IS_ERR(priv->gop_clk)) {
7689 err = PTR_ERR(priv->gop_clk);
7690 goto err_pp_clk;
7691 }
7692 err = clk_prepare_enable(priv->gop_clk);
7693 if (err < 0)
7694 goto err_pp_clk;
7695
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01007696 if (priv->hw_version == MVPP22) {
7697 priv->mg_clk = devm_clk_get(&pdev->dev, "mg_clk");
7698 if (IS_ERR(priv->mg_clk)) {
7699 err = PTR_ERR(priv->mg_clk);
7700 goto err_gop_clk;
7701 }
7702
7703 err = clk_prepare_enable(priv->mg_clk);
7704 if (err < 0)
7705 goto err_gop_clk;
7706 }
7707
Marcin Wojtas3f518502014-07-10 16:52:13 -03007708 /* Get system's tclk rate */
7709 priv->tclk = clk_get_rate(priv->pp_clk);
7710
Thomas Petazzoni2067e0a2017-03-07 16:53:19 +01007711 if (priv->hw_version == MVPP22) {
7712 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(40));
7713 if (err)
7714 goto err_mg_clk;
7715 /* Sadly, the BM pools all share the same register to
7716 * store the high 32 bits of their address. So they
7717 * must all have the same high 32 bits, which forces
7718 * us to restrict coherent memory to DMA_BIT_MASK(32).
7719 */
7720 err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
7721 if (err)
7722 goto err_mg_clk;
7723 }
7724
Marcin Wojtas3f518502014-07-10 16:52:13 -03007725 /* Initialize network controller */
7726 err = mvpp2_init(pdev, priv);
7727 if (err < 0) {
7728 dev_err(&pdev->dev, "failed to initialize controller\n");
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01007729 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007730 }
7731
7732 port_count = of_get_available_child_count(dn);
7733 if (port_count == 0) {
7734 dev_err(&pdev->dev, "no ports enabled\n");
Wei Yongjun575a1932014-07-20 22:02:43 +08007735 err = -ENODEV;
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01007736 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007737 }
7738
7739 priv->port_list = devm_kcalloc(&pdev->dev, port_count,
Markus Elfring0b92e592017-04-17 08:38:32 +02007740 sizeof(*priv->port_list),
7741 GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007742 if (!priv->port_list) {
7743 err = -ENOMEM;
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01007744 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007745 }
7746
7747 /* Initialize ports */
Marcin Wojtas3f518502014-07-10 16:52:13 -03007748 for_each_available_child_of_node(dn, port_node) {
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01007749 err = mvpp2_port_probe(pdev, port_node, priv);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007750 if (err < 0)
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01007751 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007752 }
7753
7754 platform_set_drvdata(pdev, priv);
7755 return 0;
7756
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01007757err_mg_clk:
7758 if (priv->hw_version == MVPP22)
7759 clk_disable_unprepare(priv->mg_clk);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007760err_gop_clk:
7761 clk_disable_unprepare(priv->gop_clk);
7762err_pp_clk:
7763 clk_disable_unprepare(priv->pp_clk);
7764 return err;
7765}
7766
7767static int mvpp2_remove(struct platform_device *pdev)
7768{
7769 struct mvpp2 *priv = platform_get_drvdata(pdev);
7770 struct device_node *dn = pdev->dev.of_node;
7771 struct device_node *port_node;
7772 int i = 0;
7773
7774 for_each_available_child_of_node(dn, port_node) {
7775 if (priv->port_list[i])
7776 mvpp2_port_remove(priv->port_list[i]);
7777 i++;
7778 }
7779
7780 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
7781 struct mvpp2_bm_pool *bm_pool = &priv->bm_pools[i];
7782
7783 mvpp2_bm_pool_destroy(pdev, priv, bm_pool);
7784 }
7785
7786 for_each_present_cpu(i) {
7787 struct mvpp2_tx_queue *aggr_txq = &priv->aggr_txqs[i];
7788
7789 dma_free_coherent(&pdev->dev,
7790 MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
7791 aggr_txq->descs,
Thomas Petazzoni20396132017-03-07 16:53:00 +01007792 aggr_txq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007793 }
7794
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01007795 clk_disable_unprepare(priv->mg_clk);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007796 clk_disable_unprepare(priv->pp_clk);
7797 clk_disable_unprepare(priv->gop_clk);
7798
7799 return 0;
7800}
7801
7802static const struct of_device_id mvpp2_match[] = {
Thomas Petazzonifaca9242017-03-07 16:53:06 +01007803 {
7804 .compatible = "marvell,armada-375-pp2",
7805 .data = (void *)MVPP21,
7806 },
Thomas Petazzonifc5e1552017-03-07 16:53:20 +01007807 {
7808 .compatible = "marvell,armada-7k-pp22",
7809 .data = (void *)MVPP22,
7810 },
Marcin Wojtas3f518502014-07-10 16:52:13 -03007811 { }
7812};
7813MODULE_DEVICE_TABLE(of, mvpp2_match);
7814
7815static struct platform_driver mvpp2_driver = {
7816 .probe = mvpp2_probe,
7817 .remove = mvpp2_remove,
7818 .driver = {
7819 .name = MVPP2_DRIVER_NAME,
7820 .of_match_table = mvpp2_match,
7821 },
7822};
7823
7824module_platform_driver(mvpp2_driver);
7825
7826MODULE_DESCRIPTION("Marvell PPv2 Ethernet Driver - www.marvell.com");
7827MODULE_AUTHOR("Marcin Wojtas <mw@semihalf.com>");
Ezequiel Garciac6340992014-07-14 10:34:47 -03007828MODULE_LICENSE("GPL v2");