blob: 7a6e6ae0a074cb88d41b4f98b2f8e23016b3e7ec [file] [log] [blame]
Marcin Wojtas3f518502014-07-10 16:52:13 -03001/*
2 * Driver for Marvell PPv2 network controller for Armada 375 SoC.
3 *
4 * Copyright (C) 2014 Marvell
5 *
6 * Marcin Wojtas <mw@semihalf.com>
7 *
8 * This file is licensed under the terms of the GNU General Public
9 * License version 2. This program is licensed "as is" without any
10 * warranty of any kind, whether express or implied.
11 */
12
13#include <linux/kernel.h>
14#include <linux/netdevice.h>
15#include <linux/etherdevice.h>
16#include <linux/platform_device.h>
17#include <linux/skbuff.h>
18#include <linux/inetdevice.h>
19#include <linux/mbus.h>
20#include <linux/module.h>
Antoine Ténartf84bf382017-08-22 19:08:27 +020021#include <linux/mfd/syscon.h>
Marcin Wojtas3f518502014-07-10 16:52:13 -030022#include <linux/interrupt.h>
23#include <linux/cpumask.h>
24#include <linux/of.h>
25#include <linux/of_irq.h>
26#include <linux/of_mdio.h>
27#include <linux/of_net.h>
28#include <linux/of_address.h>
Thomas Petazzonifaca9242017-03-07 16:53:06 +010029#include <linux/of_device.h>
Marcin Wojtas3f518502014-07-10 16:52:13 -030030#include <linux/phy.h>
Antoine Tenart542897d2017-08-30 10:29:15 +020031#include <linux/phy/phy.h>
Marcin Wojtas3f518502014-07-10 16:52:13 -030032#include <linux/clk.h>
Marcin Wojtasedc660f2015-08-06 19:00:30 +020033#include <linux/hrtimer.h>
34#include <linux/ktime.h>
Antoine Ténartf84bf382017-08-22 19:08:27 +020035#include <linux/regmap.h>
Marcin Wojtas3f518502014-07-10 16:52:13 -030036#include <uapi/linux/ppp_defs.h>
37#include <net/ip.h>
38#include <net/ipv6.h>
Antoine Ténart186cd4d2017-08-23 09:46:56 +020039#include <net/tso.h>
Marcin Wojtas3f518502014-07-10 16:52:13 -030040
Antoine Tenart7c10f972017-10-30 11:23:29 +010041/* Fifo Registers */
Marcin Wojtas3f518502014-07-10 16:52:13 -030042#define MVPP2_RX_DATA_FIFO_SIZE_REG(port) (0x00 + 4 * (port))
43#define MVPP2_RX_ATTR_FIFO_SIZE_REG(port) (0x20 + 4 * (port))
44#define MVPP2_RX_MIN_PKT_SIZE_REG 0x60
45#define MVPP2_RX_FIFO_INIT_REG 0x64
Antoine Tenart7c10f972017-10-30 11:23:29 +010046#define MVPP22_TX_FIFO_SIZE_REG(port) (0x8860 + 4 * (port))
Marcin Wojtas3f518502014-07-10 16:52:13 -030047
48/* RX DMA Top Registers */
49#define MVPP2_RX_CTRL_REG(port) (0x140 + 4 * (port))
50#define MVPP2_RX_LOW_LATENCY_PKT_SIZE(s) (((s) & 0xfff) << 16)
51#define MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK BIT(31)
52#define MVPP2_POOL_BUF_SIZE_REG(pool) (0x180 + 4 * (pool))
53#define MVPP2_POOL_BUF_SIZE_OFFSET 5
54#define MVPP2_RXQ_CONFIG_REG(rxq) (0x800 + 4 * (rxq))
55#define MVPP2_SNOOP_PKT_SIZE_MASK 0x1ff
56#define MVPP2_SNOOP_BUF_HDR_MASK BIT(9)
57#define MVPP2_RXQ_POOL_SHORT_OFFS 20
Thomas Petazzoni5eac8922017-03-07 16:53:10 +010058#define MVPP21_RXQ_POOL_SHORT_MASK 0x700000
59#define MVPP22_RXQ_POOL_SHORT_MASK 0xf00000
Marcin Wojtas3f518502014-07-10 16:52:13 -030060#define MVPP2_RXQ_POOL_LONG_OFFS 24
Thomas Petazzoni5eac8922017-03-07 16:53:10 +010061#define MVPP21_RXQ_POOL_LONG_MASK 0x7000000
62#define MVPP22_RXQ_POOL_LONG_MASK 0xf000000
Marcin Wojtas3f518502014-07-10 16:52:13 -030063#define MVPP2_RXQ_PACKET_OFFSET_OFFS 28
64#define MVPP2_RXQ_PACKET_OFFSET_MASK 0x70000000
65#define MVPP2_RXQ_DISABLE_MASK BIT(31)
66
67/* Parser Registers */
68#define MVPP2_PRS_INIT_LOOKUP_REG 0x1000
69#define MVPP2_PRS_PORT_LU_MAX 0xf
70#define MVPP2_PRS_PORT_LU_MASK(port) (0xff << ((port) * 4))
71#define MVPP2_PRS_PORT_LU_VAL(port, val) ((val) << ((port) * 4))
72#define MVPP2_PRS_INIT_OFFS_REG(port) (0x1004 + ((port) & 4))
73#define MVPP2_PRS_INIT_OFF_MASK(port) (0x3f << (((port) % 4) * 8))
74#define MVPP2_PRS_INIT_OFF_VAL(port, val) ((val) << (((port) % 4) * 8))
75#define MVPP2_PRS_MAX_LOOP_REG(port) (0x100c + ((port) & 4))
76#define MVPP2_PRS_MAX_LOOP_MASK(port) (0xff << (((port) % 4) * 8))
77#define MVPP2_PRS_MAX_LOOP_VAL(port, val) ((val) << (((port) % 4) * 8))
78#define MVPP2_PRS_TCAM_IDX_REG 0x1100
79#define MVPP2_PRS_TCAM_DATA_REG(idx) (0x1104 + (idx) * 4)
80#define MVPP2_PRS_TCAM_INV_MASK BIT(31)
81#define MVPP2_PRS_SRAM_IDX_REG 0x1200
82#define MVPP2_PRS_SRAM_DATA_REG(idx) (0x1204 + (idx) * 4)
83#define MVPP2_PRS_TCAM_CTRL_REG 0x1230
84#define MVPP2_PRS_TCAM_EN_MASK BIT(0)
85
86/* Classifier Registers */
87#define MVPP2_CLS_MODE_REG 0x1800
88#define MVPP2_CLS_MODE_ACTIVE_MASK BIT(0)
89#define MVPP2_CLS_PORT_WAY_REG 0x1810
90#define MVPP2_CLS_PORT_WAY_MASK(port) (1 << (port))
91#define MVPP2_CLS_LKP_INDEX_REG 0x1814
92#define MVPP2_CLS_LKP_INDEX_WAY_OFFS 6
93#define MVPP2_CLS_LKP_TBL_REG 0x1818
94#define MVPP2_CLS_LKP_TBL_RXQ_MASK 0xff
95#define MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK BIT(25)
96#define MVPP2_CLS_FLOW_INDEX_REG 0x1820
97#define MVPP2_CLS_FLOW_TBL0_REG 0x1824
98#define MVPP2_CLS_FLOW_TBL1_REG 0x1828
99#define MVPP2_CLS_FLOW_TBL2_REG 0x182c
100#define MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port) (0x1980 + ((port) * 4))
101#define MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS 3
102#define MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK 0x7
103#define MVPP2_CLS_SWFWD_P2HQ_REG(port) (0x19b0 + ((port) * 4))
104#define MVPP2_CLS_SWFWD_PCTRL_REG 0x19d0
105#define MVPP2_CLS_SWFWD_PCTRL_MASK(port) (1 << (port))
106
107/* Descriptor Manager Top Registers */
108#define MVPP2_RXQ_NUM_REG 0x2040
109#define MVPP2_RXQ_DESC_ADDR_REG 0x2044
Thomas Petazzonib02f31f2017-03-07 16:53:12 +0100110#define MVPP22_DESC_ADDR_OFFS 8
Marcin Wojtas3f518502014-07-10 16:52:13 -0300111#define MVPP2_RXQ_DESC_SIZE_REG 0x2048
112#define MVPP2_RXQ_DESC_SIZE_MASK 0x3ff0
113#define MVPP2_RXQ_STATUS_UPDATE_REG(rxq) (0x3000 + 4 * (rxq))
114#define MVPP2_RXQ_NUM_PROCESSED_OFFSET 0
115#define MVPP2_RXQ_NUM_NEW_OFFSET 16
116#define MVPP2_RXQ_STATUS_REG(rxq) (0x3400 + 4 * (rxq))
117#define MVPP2_RXQ_OCCUPIED_MASK 0x3fff
118#define MVPP2_RXQ_NON_OCCUPIED_OFFSET 16
119#define MVPP2_RXQ_NON_OCCUPIED_MASK 0x3fff0000
120#define MVPP2_RXQ_THRESH_REG 0x204c
121#define MVPP2_OCCUPIED_THRESH_OFFSET 0
122#define MVPP2_OCCUPIED_THRESH_MASK 0x3fff
123#define MVPP2_RXQ_INDEX_REG 0x2050
124#define MVPP2_TXQ_NUM_REG 0x2080
125#define MVPP2_TXQ_DESC_ADDR_REG 0x2084
126#define MVPP2_TXQ_DESC_SIZE_REG 0x2088
127#define MVPP2_TXQ_DESC_SIZE_MASK 0x3ff0
Thomas Petazzoni213f4282017-08-03 10:42:00 +0200128#define MVPP2_TXQ_THRESH_REG 0x2094
129#define MVPP2_TXQ_THRESH_OFFSET 16
130#define MVPP2_TXQ_THRESH_MASK 0x3fff
Marcin Wojtas3f518502014-07-10 16:52:13 -0300131#define MVPP2_AGGR_TXQ_UPDATE_REG 0x2090
Marcin Wojtas3f518502014-07-10 16:52:13 -0300132#define MVPP2_TXQ_INDEX_REG 0x2098
133#define MVPP2_TXQ_PREF_BUF_REG 0x209c
134#define MVPP2_PREF_BUF_PTR(desc) ((desc) & 0xfff)
135#define MVPP2_PREF_BUF_SIZE_4 (BIT(12) | BIT(13))
136#define MVPP2_PREF_BUF_SIZE_16 (BIT(12) | BIT(14))
137#define MVPP2_PREF_BUF_THRESH(val) ((val) << 17)
138#define MVPP2_TXQ_DRAIN_EN_MASK BIT(31)
139#define MVPP2_TXQ_PENDING_REG 0x20a0
140#define MVPP2_TXQ_PENDING_MASK 0x3fff
141#define MVPP2_TXQ_INT_STATUS_REG 0x20a4
142#define MVPP2_TXQ_SENT_REG(txq) (0x3c00 + 4 * (txq))
143#define MVPP2_TRANSMITTED_COUNT_OFFSET 16
144#define MVPP2_TRANSMITTED_COUNT_MASK 0x3fff0000
145#define MVPP2_TXQ_RSVD_REQ_REG 0x20b0
146#define MVPP2_TXQ_RSVD_REQ_Q_OFFSET 16
147#define MVPP2_TXQ_RSVD_RSLT_REG 0x20b4
148#define MVPP2_TXQ_RSVD_RSLT_MASK 0x3fff
149#define MVPP2_TXQ_RSVD_CLR_REG 0x20b8
150#define MVPP2_TXQ_RSVD_CLR_OFFSET 16
151#define MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu) (0x2100 + 4 * (cpu))
Thomas Petazzonib02f31f2017-03-07 16:53:12 +0100152#define MVPP22_AGGR_TXQ_DESC_ADDR_OFFS 8
Marcin Wojtas3f518502014-07-10 16:52:13 -0300153#define MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu) (0x2140 + 4 * (cpu))
154#define MVPP2_AGGR_TXQ_DESC_SIZE_MASK 0x3ff0
155#define MVPP2_AGGR_TXQ_STATUS_REG(cpu) (0x2180 + 4 * (cpu))
156#define MVPP2_AGGR_TXQ_PENDING_MASK 0x3fff
157#define MVPP2_AGGR_TXQ_INDEX_REG(cpu) (0x21c0 + 4 * (cpu))
158
159/* MBUS bridge registers */
160#define MVPP2_WIN_BASE(w) (0x4000 + ((w) << 2))
161#define MVPP2_WIN_SIZE(w) (0x4020 + ((w) << 2))
162#define MVPP2_WIN_REMAP(w) (0x4040 + ((w) << 2))
163#define MVPP2_BASE_ADDR_ENABLE 0x4060
164
Thomas Petazzoni6763ce32017-03-07 16:53:15 +0100165/* AXI Bridge Registers */
166#define MVPP22_AXI_BM_WR_ATTR_REG 0x4100
167#define MVPP22_AXI_BM_RD_ATTR_REG 0x4104
168#define MVPP22_AXI_AGGRQ_DESCR_RD_ATTR_REG 0x4110
169#define MVPP22_AXI_TXQ_DESCR_WR_ATTR_REG 0x4114
170#define MVPP22_AXI_TXQ_DESCR_RD_ATTR_REG 0x4118
171#define MVPP22_AXI_RXQ_DESCR_WR_ATTR_REG 0x411c
172#define MVPP22_AXI_RX_DATA_WR_ATTR_REG 0x4120
173#define MVPP22_AXI_TX_DATA_RD_ATTR_REG 0x4130
174#define MVPP22_AXI_RD_NORMAL_CODE_REG 0x4150
175#define MVPP22_AXI_RD_SNOOP_CODE_REG 0x4154
176#define MVPP22_AXI_WR_NORMAL_CODE_REG 0x4160
177#define MVPP22_AXI_WR_SNOOP_CODE_REG 0x4164
178
179/* Values for AXI Bridge registers */
180#define MVPP22_AXI_ATTR_CACHE_OFFS 0
181#define MVPP22_AXI_ATTR_DOMAIN_OFFS 12
182
183#define MVPP22_AXI_CODE_CACHE_OFFS 0
184#define MVPP22_AXI_CODE_DOMAIN_OFFS 4
185
186#define MVPP22_AXI_CODE_CACHE_NON_CACHE 0x3
187#define MVPP22_AXI_CODE_CACHE_WR_CACHE 0x7
188#define MVPP22_AXI_CODE_CACHE_RD_CACHE 0xb
189
190#define MVPP22_AXI_CODE_DOMAIN_OUTER_DOM 2
191#define MVPP22_AXI_CODE_DOMAIN_SYSTEM 3
192
Marcin Wojtas3f518502014-07-10 16:52:13 -0300193/* Interrupt Cause and Mask registers */
Thomas Petazzoni213f4282017-08-03 10:42:00 +0200194#define MVPP2_ISR_TX_THRESHOLD_REG(port) (0x5140 + 4 * (port))
195#define MVPP2_MAX_ISR_TX_THRESHOLD 0xfffff0
196
Marcin Wojtas3f518502014-07-10 16:52:13 -0300197#define MVPP2_ISR_RX_THRESHOLD_REG(rxq) (0x5200 + 4 * (rxq))
Thomas Petazzoniab426762017-02-21 11:28:04 +0100198#define MVPP2_MAX_ISR_RX_THRESHOLD 0xfffff0
Thomas Petazzonieb1e93a2017-08-03 10:41:55 +0200199#define MVPP21_ISR_RXQ_GROUP_REG(port) (0x5400 + 4 * (port))
Thomas Petazzonia73fef12017-03-07 16:53:16 +0100200
Antoine Ténart81b66302017-08-22 19:08:21 +0200201#define MVPP22_ISR_RXQ_GROUP_INDEX_REG 0x5400
Thomas Petazzonia73fef12017-03-07 16:53:16 +0100202#define MVPP22_ISR_RXQ_GROUP_INDEX_SUBGROUP_MASK 0xf
Antoine Ténart81b66302017-08-22 19:08:21 +0200203#define MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_MASK 0x380
204#define MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_OFFSET 7
Thomas Petazzonia73fef12017-03-07 16:53:16 +0100205
206#define MVPP22_ISR_RXQ_GROUP_INDEX_SUBGROUP_MASK 0xf
Antoine Ténart81b66302017-08-22 19:08:21 +0200207#define MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_MASK 0x380
Thomas Petazzonia73fef12017-03-07 16:53:16 +0100208
Antoine Ténart81b66302017-08-22 19:08:21 +0200209#define MVPP22_ISR_RXQ_SUB_GROUP_CONFIG_REG 0x5404
210#define MVPP22_ISR_RXQ_SUB_GROUP_STARTQ_MASK 0x1f
211#define MVPP22_ISR_RXQ_SUB_GROUP_SIZE_MASK 0xf00
212#define MVPP22_ISR_RXQ_SUB_GROUP_SIZE_OFFSET 8
Thomas Petazzonia73fef12017-03-07 16:53:16 +0100213
Marcin Wojtas3f518502014-07-10 16:52:13 -0300214#define MVPP2_ISR_ENABLE_REG(port) (0x5420 + 4 * (port))
215#define MVPP2_ISR_ENABLE_INTERRUPT(mask) ((mask) & 0xffff)
216#define MVPP2_ISR_DISABLE_INTERRUPT(mask) (((mask) << 16) & 0xffff0000)
217#define MVPP2_ISR_RX_TX_CAUSE_REG(port) (0x5480 + 4 * (port))
218#define MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK 0xffff
219#define MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK 0xff0000
Thomas Petazzoni213f4282017-08-03 10:42:00 +0200220#define MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_OFFSET 16
Marcin Wojtas3f518502014-07-10 16:52:13 -0300221#define MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK BIT(24)
222#define MVPP2_CAUSE_FCS_ERR_MASK BIT(25)
223#define MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK BIT(26)
224#define MVPP2_CAUSE_TX_EXCEPTION_SUM_MASK BIT(29)
225#define MVPP2_CAUSE_RX_EXCEPTION_SUM_MASK BIT(30)
226#define MVPP2_CAUSE_MISC_SUM_MASK BIT(31)
227#define MVPP2_ISR_RX_TX_MASK_REG(port) (0x54a0 + 4 * (port))
228#define MVPP2_ISR_PON_RX_TX_MASK_REG 0x54bc
229#define MVPP2_PON_CAUSE_RXQ_OCCUP_DESC_ALL_MASK 0xffff
230#define MVPP2_PON_CAUSE_TXP_OCCUP_DESC_ALL_MASK 0x3fc00000
231#define MVPP2_PON_CAUSE_MISC_SUM_MASK BIT(31)
232#define MVPP2_ISR_MISC_CAUSE_REG 0x55b0
233
234/* Buffer Manager registers */
235#define MVPP2_BM_POOL_BASE_REG(pool) (0x6000 + ((pool) * 4))
236#define MVPP2_BM_POOL_BASE_ADDR_MASK 0xfffff80
237#define MVPP2_BM_POOL_SIZE_REG(pool) (0x6040 + ((pool) * 4))
238#define MVPP2_BM_POOL_SIZE_MASK 0xfff0
239#define MVPP2_BM_POOL_READ_PTR_REG(pool) (0x6080 + ((pool) * 4))
240#define MVPP2_BM_POOL_GET_READ_PTR_MASK 0xfff0
241#define MVPP2_BM_POOL_PTRS_NUM_REG(pool) (0x60c0 + ((pool) * 4))
242#define MVPP2_BM_POOL_PTRS_NUM_MASK 0xfff0
243#define MVPP2_BM_BPPI_READ_PTR_REG(pool) (0x6100 + ((pool) * 4))
244#define MVPP2_BM_BPPI_PTRS_NUM_REG(pool) (0x6140 + ((pool) * 4))
245#define MVPP2_BM_BPPI_PTR_NUM_MASK 0x7ff
246#define MVPP2_BM_BPPI_PREFETCH_FULL_MASK BIT(16)
247#define MVPP2_BM_POOL_CTRL_REG(pool) (0x6200 + ((pool) * 4))
248#define MVPP2_BM_START_MASK BIT(0)
249#define MVPP2_BM_STOP_MASK BIT(1)
250#define MVPP2_BM_STATE_MASK BIT(4)
251#define MVPP2_BM_LOW_THRESH_OFFS 8
252#define MVPP2_BM_LOW_THRESH_MASK 0x7f00
253#define MVPP2_BM_LOW_THRESH_VALUE(val) ((val) << \
254 MVPP2_BM_LOW_THRESH_OFFS)
255#define MVPP2_BM_HIGH_THRESH_OFFS 16
256#define MVPP2_BM_HIGH_THRESH_MASK 0x7f0000
257#define MVPP2_BM_HIGH_THRESH_VALUE(val) ((val) << \
258 MVPP2_BM_HIGH_THRESH_OFFS)
259#define MVPP2_BM_INTR_CAUSE_REG(pool) (0x6240 + ((pool) * 4))
260#define MVPP2_BM_RELEASED_DELAY_MASK BIT(0)
261#define MVPP2_BM_ALLOC_FAILED_MASK BIT(1)
262#define MVPP2_BM_BPPE_EMPTY_MASK BIT(2)
263#define MVPP2_BM_BPPE_FULL_MASK BIT(3)
264#define MVPP2_BM_AVAILABLE_BP_LOW_MASK BIT(4)
265#define MVPP2_BM_INTR_MASK_REG(pool) (0x6280 + ((pool) * 4))
266#define MVPP2_BM_PHY_ALLOC_REG(pool) (0x6400 + ((pool) * 4))
267#define MVPP2_BM_PHY_ALLOC_GRNTD_MASK BIT(0)
268#define MVPP2_BM_VIRT_ALLOC_REG 0x6440
Thomas Petazzonid01524d2017-03-07 16:53:09 +0100269#define MVPP22_BM_ADDR_HIGH_ALLOC 0x6444
270#define MVPP22_BM_ADDR_HIGH_PHYS_MASK 0xff
271#define MVPP22_BM_ADDR_HIGH_VIRT_MASK 0xff00
272#define MVPP22_BM_ADDR_HIGH_VIRT_SHIFT 8
Marcin Wojtas3f518502014-07-10 16:52:13 -0300273#define MVPP2_BM_PHY_RLS_REG(pool) (0x6480 + ((pool) * 4))
274#define MVPP2_BM_PHY_RLS_MC_BUFF_MASK BIT(0)
275#define MVPP2_BM_PHY_RLS_PRIO_EN_MASK BIT(1)
276#define MVPP2_BM_PHY_RLS_GRNTD_MASK BIT(2)
277#define MVPP2_BM_VIRT_RLS_REG 0x64c0
Thomas Petazzonid01524d2017-03-07 16:53:09 +0100278#define MVPP22_BM_ADDR_HIGH_RLS_REG 0x64c4
279#define MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK 0xff
Antoine Ténart81b66302017-08-22 19:08:21 +0200280#define MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK 0xff00
Thomas Petazzonid01524d2017-03-07 16:53:09 +0100281#define MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT 8
Marcin Wojtas3f518502014-07-10 16:52:13 -0300282
283/* TX Scheduler registers */
284#define MVPP2_TXP_SCHED_PORT_INDEX_REG 0x8000
285#define MVPP2_TXP_SCHED_Q_CMD_REG 0x8004
286#define MVPP2_TXP_SCHED_ENQ_MASK 0xff
287#define MVPP2_TXP_SCHED_DISQ_OFFSET 8
288#define MVPP2_TXP_SCHED_CMD_1_REG 0x8010
289#define MVPP2_TXP_SCHED_PERIOD_REG 0x8018
290#define MVPP2_TXP_SCHED_MTU_REG 0x801c
291#define MVPP2_TXP_MTU_MAX 0x7FFFF
292#define MVPP2_TXP_SCHED_REFILL_REG 0x8020
293#define MVPP2_TXP_REFILL_TOKENS_ALL_MASK 0x7ffff
294#define MVPP2_TXP_REFILL_PERIOD_ALL_MASK 0x3ff00000
295#define MVPP2_TXP_REFILL_PERIOD_MASK(v) ((v) << 20)
296#define MVPP2_TXP_SCHED_TOKEN_SIZE_REG 0x8024
297#define MVPP2_TXP_TOKEN_SIZE_MAX 0xffffffff
298#define MVPP2_TXQ_SCHED_REFILL_REG(q) (0x8040 + ((q) << 2))
299#define MVPP2_TXQ_REFILL_TOKENS_ALL_MASK 0x7ffff
300#define MVPP2_TXQ_REFILL_PERIOD_ALL_MASK 0x3ff00000
301#define MVPP2_TXQ_REFILL_PERIOD_MASK(v) ((v) << 20)
302#define MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(q) (0x8060 + ((q) << 2))
303#define MVPP2_TXQ_TOKEN_SIZE_MAX 0x7fffffff
304#define MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(q) (0x8080 + ((q) << 2))
305#define MVPP2_TXQ_TOKEN_CNTR_MAX 0xffffffff
306
307/* TX general registers */
308#define MVPP2_TX_SNOOP_REG 0x8800
309#define MVPP2_TX_PORT_FLUSH_REG 0x8810
310#define MVPP2_TX_PORT_FLUSH_MASK(port) (1 << (port))
311
312/* LMS registers */
313#define MVPP2_SRC_ADDR_MIDDLE 0x24
314#define MVPP2_SRC_ADDR_HIGH 0x28
Marcin Wojtas08a23752014-07-21 13:48:12 -0300315#define MVPP2_PHY_AN_CFG0_REG 0x34
316#define MVPP2_PHY_AN_STOP_SMI0_MASK BIT(7)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300317#define MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG 0x305c
Thomas Petazzoni31d76772017-02-21 11:28:10 +0100318#define MVPP2_EXT_GLOBAL_CTRL_DEFAULT 0x27
Marcin Wojtas3f518502014-07-10 16:52:13 -0300319
320/* Per-port registers */
321#define MVPP2_GMAC_CTRL_0_REG 0x0
Antoine Ténart81b66302017-08-22 19:08:21 +0200322#define MVPP2_GMAC_PORT_EN_MASK BIT(0)
Antoine Ténart39193572017-08-22 19:08:24 +0200323#define MVPP2_GMAC_PORT_TYPE_MASK BIT(1)
Antoine Ténart81b66302017-08-22 19:08:21 +0200324#define MVPP2_GMAC_MAX_RX_SIZE_OFFS 2
325#define MVPP2_GMAC_MAX_RX_SIZE_MASK 0x7ffc
326#define MVPP2_GMAC_MIB_CNTR_EN_MASK BIT(15)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300327#define MVPP2_GMAC_CTRL_1_REG 0x4
Antoine Ténart81b66302017-08-22 19:08:21 +0200328#define MVPP2_GMAC_PERIODIC_XON_EN_MASK BIT(1)
329#define MVPP2_GMAC_GMII_LB_EN_MASK BIT(5)
330#define MVPP2_GMAC_PCS_LB_EN_BIT 6
331#define MVPP2_GMAC_PCS_LB_EN_MASK BIT(6)
332#define MVPP2_GMAC_SA_LOW_OFFS 7
Marcin Wojtas3f518502014-07-10 16:52:13 -0300333#define MVPP2_GMAC_CTRL_2_REG 0x8
Antoine Ténart81b66302017-08-22 19:08:21 +0200334#define MVPP2_GMAC_INBAND_AN_MASK BIT(0)
Antoine Ténart39193572017-08-22 19:08:24 +0200335#define MVPP2_GMAC_FLOW_CTRL_MASK GENMASK(2, 1)
Antoine Ténart81b66302017-08-22 19:08:21 +0200336#define MVPP2_GMAC_PCS_ENABLE_MASK BIT(3)
Antoine Tenartc7dfc8c2017-09-25 14:59:48 +0200337#define MVPP2_GMAC_INTERNAL_CLK_MASK BIT(4)
Antoine Ténart39193572017-08-22 19:08:24 +0200338#define MVPP2_GMAC_DISABLE_PADDING BIT(5)
Antoine Ténart81b66302017-08-22 19:08:21 +0200339#define MVPP2_GMAC_PORT_RESET_MASK BIT(6)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300340#define MVPP2_GMAC_AUTONEG_CONFIG 0xc
Antoine Ténart81b66302017-08-22 19:08:21 +0200341#define MVPP2_GMAC_FORCE_LINK_DOWN BIT(0)
342#define MVPP2_GMAC_FORCE_LINK_PASS BIT(1)
Antoine Ténart39193572017-08-22 19:08:24 +0200343#define MVPP2_GMAC_IN_BAND_AUTONEG BIT(2)
344#define MVPP2_GMAC_IN_BAND_AUTONEG_BYPASS BIT(3)
Antoine Ténart81b66302017-08-22 19:08:21 +0200345#define MVPP2_GMAC_CONFIG_MII_SPEED BIT(5)
346#define MVPP2_GMAC_CONFIG_GMII_SPEED BIT(6)
347#define MVPP2_GMAC_AN_SPEED_EN BIT(7)
348#define MVPP2_GMAC_FC_ADV_EN BIT(9)
Antoine Ténart39193572017-08-22 19:08:24 +0200349#define MVPP2_GMAC_FLOW_CTRL_AUTONEG BIT(11)
Antoine Ténart81b66302017-08-22 19:08:21 +0200350#define MVPP2_GMAC_CONFIG_FULL_DUPLEX BIT(12)
351#define MVPP2_GMAC_AN_DUPLEX_EN BIT(13)
Antoine Tenartfd3651b2017-09-01 11:04:54 +0200352#define MVPP2_GMAC_STATUS0 0x10
353#define MVPP2_GMAC_STATUS0_LINK_UP BIT(0)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300354#define MVPP2_GMAC_PORT_FIFO_CFG_1_REG 0x1c
Antoine Ténart81b66302017-08-22 19:08:21 +0200355#define MVPP2_GMAC_TX_FIFO_MIN_TH_OFFS 6
356#define MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK 0x1fc0
357#define MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(v) (((v) << 6) & \
Marcin Wojtas3f518502014-07-10 16:52:13 -0300358 MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK)
Antoine Tenartfd3651b2017-09-01 11:04:54 +0200359#define MVPP22_GMAC_INT_STAT 0x20
360#define MVPP22_GMAC_INT_STAT_LINK BIT(1)
361#define MVPP22_GMAC_INT_MASK 0x24
362#define MVPP22_GMAC_INT_MASK_LINK_STAT BIT(1)
Thomas Petazzoni26975822017-03-07 16:53:14 +0100363#define MVPP22_GMAC_CTRL_4_REG 0x90
Antoine Ténart81b66302017-08-22 19:08:21 +0200364#define MVPP22_CTRL4_EXT_PIN_GMII_SEL BIT(0)
365#define MVPP22_CTRL4_DP_CLK_SEL BIT(5)
Antoine Ténart1068ec72017-08-22 19:08:22 +0200366#define MVPP22_CTRL4_SYNC_BYPASS_DIS BIT(6)
Antoine Ténart81b66302017-08-22 19:08:21 +0200367#define MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE BIT(7)
Antoine Tenartfd3651b2017-09-01 11:04:54 +0200368#define MVPP22_GMAC_INT_SUM_MASK 0xa4
369#define MVPP22_GMAC_INT_SUM_MASK_LINK_STAT BIT(1)
Thomas Petazzoni26975822017-03-07 16:53:14 +0100370
371/* Per-port XGMAC registers. PPv2.2 only, only for GOP port 0,
372 * relative to port->base.
373 */
Antoine Ténart725757a2017-06-12 16:01:39 +0200374#define MVPP22_XLG_CTRL0_REG 0x100
Antoine Ténart81b66302017-08-22 19:08:21 +0200375#define MVPP22_XLG_CTRL0_PORT_EN BIT(0)
376#define MVPP22_XLG_CTRL0_MAC_RESET_DIS BIT(1)
Antoine Ténart77321952017-08-22 19:08:25 +0200377#define MVPP22_XLG_CTRL0_RX_FLOW_CTRL_EN BIT(7)
Antoine Ténart81b66302017-08-22 19:08:21 +0200378#define MVPP22_XLG_CTRL0_MIB_CNT_DIS BIT(14)
Stefan Chulski76eb1b12017-08-22 19:08:26 +0200379#define MVPP22_XLG_CTRL1_REG 0x104
Antoine Ténartec15ecd2017-08-25 15:24:46 +0200380#define MVPP22_XLG_CTRL1_FRAMESIZELIMIT_OFFS 0
Stefan Chulski76eb1b12017-08-22 19:08:26 +0200381#define MVPP22_XLG_CTRL1_FRAMESIZELIMIT_MASK 0x1fff
Antoine Tenartfd3651b2017-09-01 11:04:54 +0200382#define MVPP22_XLG_STATUS 0x10c
383#define MVPP22_XLG_STATUS_LINK_UP BIT(0)
384#define MVPP22_XLG_INT_STAT 0x114
385#define MVPP22_XLG_INT_STAT_LINK BIT(1)
386#define MVPP22_XLG_INT_MASK 0x118
387#define MVPP22_XLG_INT_MASK_LINK BIT(1)
Thomas Petazzoni26975822017-03-07 16:53:14 +0100388#define MVPP22_XLG_CTRL3_REG 0x11c
Antoine Ténart81b66302017-08-22 19:08:21 +0200389#define MVPP22_XLG_CTRL3_MACMODESELECT_MASK (7 << 13)
390#define MVPP22_XLG_CTRL3_MACMODESELECT_GMAC (0 << 13)
391#define MVPP22_XLG_CTRL3_MACMODESELECT_10G (1 << 13)
Antoine Tenartfd3651b2017-09-01 11:04:54 +0200392#define MVPP22_XLG_EXT_INT_MASK 0x15c
393#define MVPP22_XLG_EXT_INT_MASK_XLG BIT(1)
394#define MVPP22_XLG_EXT_INT_MASK_GIG BIT(2)
Antoine Ténart77321952017-08-22 19:08:25 +0200395#define MVPP22_XLG_CTRL4_REG 0x184
396#define MVPP22_XLG_CTRL4_FWD_FC BIT(5)
397#define MVPP22_XLG_CTRL4_FWD_PFC BIT(6)
398#define MVPP22_XLG_CTRL4_MACMODSELECT_GMAC BIT(12)
399
Thomas Petazzoni26975822017-03-07 16:53:14 +0100400/* SMI registers. PPv2.2 only, relative to priv->iface_base. */
401#define MVPP22_SMI_MISC_CFG_REG 0x1204
Antoine Ténart81b66302017-08-22 19:08:21 +0200402#define MVPP22_SMI_POLLING_EN BIT(10)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300403
Thomas Petazzonia7868412017-03-07 16:53:13 +0100404#define MVPP22_GMAC_BASE(port) (0x7000 + (port) * 0x1000 + 0xe00)
405
Marcin Wojtas3f518502014-07-10 16:52:13 -0300406#define MVPP2_CAUSE_TXQ_SENT_DESC_ALL_MASK 0xff
407
408/* Descriptor ring Macros */
409#define MVPP2_QUEUE_NEXT_DESC(q, index) \
410 (((index) < (q)->last_desc) ? ((index) + 1) : 0)
411
Antoine Ténartf84bf382017-08-22 19:08:27 +0200412/* XPCS registers. PPv2.2 only */
413#define MVPP22_MPCS_BASE(port) (0x7000 + (port) * 0x1000)
414#define MVPP22_MPCS_CTRL 0x14
415#define MVPP22_MPCS_CTRL_FWD_ERR_CONN BIT(10)
416#define MVPP22_MPCS_CLK_RESET 0x14c
417#define MAC_CLK_RESET_SD_TX BIT(0)
418#define MAC_CLK_RESET_SD_RX BIT(1)
419#define MAC_CLK_RESET_MAC BIT(2)
420#define MVPP22_MPCS_CLK_RESET_DIV_RATIO(n) ((n) << 4)
421#define MVPP22_MPCS_CLK_RESET_DIV_SET BIT(11)
422
423/* XPCS registers. PPv2.2 only */
424#define MVPP22_XPCS_BASE(port) (0x7400 + (port) * 0x1000)
425#define MVPP22_XPCS_CFG0 0x0
426#define MVPP22_XPCS_CFG0_PCS_MODE(n) ((n) << 3)
427#define MVPP22_XPCS_CFG0_ACTIVE_LANE(n) ((n) << 5)
428
429/* System controller registers. Accessed through a regmap. */
430#define GENCONF_SOFT_RESET1 0x1108
431#define GENCONF_SOFT_RESET1_GOP BIT(6)
432#define GENCONF_PORT_CTRL0 0x1110
433#define GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT BIT(1)
434#define GENCONF_PORT_CTRL0_RX_DATA_SAMPLE BIT(29)
435#define GENCONF_PORT_CTRL0_CLK_DIV_PHASE_CLR BIT(31)
436#define GENCONF_PORT_CTRL1 0x1114
437#define GENCONF_PORT_CTRL1_EN(p) BIT(p)
438#define GENCONF_PORT_CTRL1_RESET(p) (BIT(p) << 28)
439#define GENCONF_CTRL0 0x1120
440#define GENCONF_CTRL0_PORT0_RGMII BIT(0)
441#define GENCONF_CTRL0_PORT1_RGMII_MII BIT(1)
442#define GENCONF_CTRL0_PORT1_RGMII BIT(2)
443
Marcin Wojtas3f518502014-07-10 16:52:13 -0300444/* Various constants */
445
446/* Coalescing */
447#define MVPP2_TXDONE_COAL_PKTS_THRESH 15
Marcin Wojtasedc660f2015-08-06 19:00:30 +0200448#define MVPP2_TXDONE_HRTIMER_PERIOD_NS 1000000UL
Thomas Petazzoni213f4282017-08-03 10:42:00 +0200449#define MVPP2_TXDONE_COAL_USEC 1000
Marcin Wojtas3f518502014-07-10 16:52:13 -0300450#define MVPP2_RX_COAL_PKTS 32
451#define MVPP2_RX_COAL_USEC 100
452
453/* The two bytes Marvell header. Either contains a special value used
454 * by Marvell switches when a specific hardware mode is enabled (not
455 * supported by this driver) or is filled automatically by zeroes on
456 * the RX side. Those two bytes being at the front of the Ethernet
457 * header, they allow to have the IP header aligned on a 4 bytes
458 * boundary automatically: the hardware skips those two bytes on its
459 * own.
460 */
461#define MVPP2_MH_SIZE 2
462#define MVPP2_ETH_TYPE_LEN 2
463#define MVPP2_PPPOE_HDR_SIZE 8
464#define MVPP2_VLAN_TAG_LEN 4
465
466/* Lbtd 802.3 type */
467#define MVPP2_IP_LBDT_TYPE 0xfffa
468
Marcin Wojtas3f518502014-07-10 16:52:13 -0300469#define MVPP2_TX_CSUM_MAX_SIZE 9800
470
471/* Timeout constants */
472#define MVPP2_TX_DISABLE_TIMEOUT_MSEC 1000
473#define MVPP2_TX_PENDING_TIMEOUT_MSEC 1000
474
475#define MVPP2_TX_MTU_MAX 0x7ffff
476
477/* Maximum number of T-CONTs of PON port */
478#define MVPP2_MAX_TCONT 16
479
480/* Maximum number of supported ports */
481#define MVPP2_MAX_PORTS 4
482
483/* Maximum number of TXQs used by single port */
484#define MVPP2_MAX_TXQ 8
485
Marcin Wojtas3f518502014-07-10 16:52:13 -0300486/* Dfault number of RXQs in use */
487#define MVPP2_DEFAULT_RXQ 4
488
Marcin Wojtas3f518502014-07-10 16:52:13 -0300489/* Max number of Rx descriptors */
490#define MVPP2_MAX_RXD 128
491
492/* Max number of Tx descriptors */
493#define MVPP2_MAX_TXD 1024
494
495/* Amount of Tx descriptors that can be reserved at once by CPU */
496#define MVPP2_CPU_DESC_CHUNK 64
497
498/* Max number of Tx descriptors in each aggregated queue */
499#define MVPP2_AGGR_TXQ_SIZE 256
500
501/* Descriptor aligned size */
502#define MVPP2_DESC_ALIGNED_SIZE 32
503
504/* Descriptor alignment mask */
505#define MVPP2_TX_DESC_ALIGN (MVPP2_DESC_ALIGNED_SIZE - 1)
506
507/* RX FIFO constants */
Antoine Tenart2d1d7df2017-10-30 11:23:28 +0100508#define MVPP2_RX_FIFO_PORT_DATA_SIZE_32KB 0x8000
509#define MVPP2_RX_FIFO_PORT_DATA_SIZE_8KB 0x2000
510#define MVPP2_RX_FIFO_PORT_DATA_SIZE_4KB 0x1000
511#define MVPP2_RX_FIFO_PORT_ATTR_SIZE_32KB 0x200
512#define MVPP2_RX_FIFO_PORT_ATTR_SIZE_8KB 0x80
513#define MVPP2_RX_FIFO_PORT_ATTR_SIZE_4KB 0x40
514#define MVPP2_RX_FIFO_PORT_MIN_PKT 0x80
Marcin Wojtas3f518502014-07-10 16:52:13 -0300515
Antoine Tenart7c10f972017-10-30 11:23:29 +0100516/* TX FIFO constants */
517#define MVPP22_TX_FIFO_DATA_SIZE_10KB 0xa
518#define MVPP22_TX_FIFO_DATA_SIZE_3KB 0x3
519
Marcin Wojtas3f518502014-07-10 16:52:13 -0300520/* RX buffer constants */
521#define MVPP2_SKB_SHINFO_SIZE \
522 SKB_DATA_ALIGN(sizeof(struct skb_shared_info))
523
524#define MVPP2_RX_PKT_SIZE(mtu) \
525 ALIGN((mtu) + MVPP2_MH_SIZE + MVPP2_VLAN_TAG_LEN + \
Jisheng Zhang4a0a12d2016-04-01 17:11:05 +0800526 ETH_HLEN + ETH_FCS_LEN, cache_line_size())
Marcin Wojtas3f518502014-07-10 16:52:13 -0300527
528#define MVPP2_RX_BUF_SIZE(pkt_size) ((pkt_size) + NET_SKB_PAD)
529#define MVPP2_RX_TOTAL_SIZE(buf_size) ((buf_size) + MVPP2_SKB_SHINFO_SIZE)
530#define MVPP2_RX_MAX_PKT_SIZE(total_size) \
531 ((total_size) - NET_SKB_PAD - MVPP2_SKB_SHINFO_SIZE)
532
533#define MVPP2_BIT_TO_BYTE(bit) ((bit) / 8)
534
535/* IPv6 max L3 address size */
536#define MVPP2_MAX_L3_ADDR_SIZE 16
537
538/* Port flags */
539#define MVPP2_F_LOOPBACK BIT(0)
540
541/* Marvell tag types */
542enum mvpp2_tag_type {
543 MVPP2_TAG_TYPE_NONE = 0,
544 MVPP2_TAG_TYPE_MH = 1,
545 MVPP2_TAG_TYPE_DSA = 2,
546 MVPP2_TAG_TYPE_EDSA = 3,
547 MVPP2_TAG_TYPE_VLAN = 4,
548 MVPP2_TAG_TYPE_LAST = 5
549};
550
551/* Parser constants */
552#define MVPP2_PRS_TCAM_SRAM_SIZE 256
553#define MVPP2_PRS_TCAM_WORDS 6
554#define MVPP2_PRS_SRAM_WORDS 4
555#define MVPP2_PRS_FLOW_ID_SIZE 64
556#define MVPP2_PRS_FLOW_ID_MASK 0x3f
557#define MVPP2_PRS_TCAM_ENTRY_INVALID 1
558#define MVPP2_PRS_TCAM_DSA_TAGGED_BIT BIT(5)
559#define MVPP2_PRS_IPV4_HEAD 0x40
560#define MVPP2_PRS_IPV4_HEAD_MASK 0xf0
561#define MVPP2_PRS_IPV4_MC 0xe0
562#define MVPP2_PRS_IPV4_MC_MASK 0xf0
563#define MVPP2_PRS_IPV4_BC_MASK 0xff
564#define MVPP2_PRS_IPV4_IHL 0x5
565#define MVPP2_PRS_IPV4_IHL_MASK 0xf
566#define MVPP2_PRS_IPV6_MC 0xff
567#define MVPP2_PRS_IPV6_MC_MASK 0xff
568#define MVPP2_PRS_IPV6_HOP_MASK 0xff
569#define MVPP2_PRS_TCAM_PROTO_MASK 0xff
570#define MVPP2_PRS_TCAM_PROTO_MASK_L 0x3f
571#define MVPP2_PRS_DBL_VLANS_MAX 100
572
573/* Tcam structure:
574 * - lookup ID - 4 bits
575 * - port ID - 1 byte
576 * - additional information - 1 byte
577 * - header data - 8 bytes
578 * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(5)->(0).
579 */
580#define MVPP2_PRS_AI_BITS 8
581#define MVPP2_PRS_PORT_MASK 0xff
582#define MVPP2_PRS_LU_MASK 0xf
583#define MVPP2_PRS_TCAM_DATA_BYTE(offs) \
584 (((offs) - ((offs) % 2)) * 2 + ((offs) % 2))
585#define MVPP2_PRS_TCAM_DATA_BYTE_EN(offs) \
586 (((offs) * 2) - ((offs) % 2) + 2)
587#define MVPP2_PRS_TCAM_AI_BYTE 16
588#define MVPP2_PRS_TCAM_PORT_BYTE 17
589#define MVPP2_PRS_TCAM_LU_BYTE 20
590#define MVPP2_PRS_TCAM_EN_OFFS(offs) ((offs) + 2)
591#define MVPP2_PRS_TCAM_INV_WORD 5
592/* Tcam entries ID */
593#define MVPP2_PE_DROP_ALL 0
594#define MVPP2_PE_FIRST_FREE_TID 1
595#define MVPP2_PE_LAST_FREE_TID (MVPP2_PRS_TCAM_SRAM_SIZE - 31)
596#define MVPP2_PE_IP6_EXT_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 30)
597#define MVPP2_PE_MAC_MC_IP6 (MVPP2_PRS_TCAM_SRAM_SIZE - 29)
598#define MVPP2_PE_IP6_ADDR_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 28)
599#define MVPP2_PE_IP4_ADDR_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 27)
600#define MVPP2_PE_LAST_DEFAULT_FLOW (MVPP2_PRS_TCAM_SRAM_SIZE - 26)
601#define MVPP2_PE_FIRST_DEFAULT_FLOW (MVPP2_PRS_TCAM_SRAM_SIZE - 19)
602#define MVPP2_PE_EDSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 18)
603#define MVPP2_PE_EDSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 17)
604#define MVPP2_PE_DSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 16)
605#define MVPP2_PE_DSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 15)
606#define MVPP2_PE_ETYPE_EDSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 14)
607#define MVPP2_PE_ETYPE_EDSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 13)
608#define MVPP2_PE_ETYPE_DSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 12)
609#define MVPP2_PE_ETYPE_DSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 11)
610#define MVPP2_PE_MH_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 10)
611#define MVPP2_PE_DSA_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 9)
612#define MVPP2_PE_IP6_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 8)
613#define MVPP2_PE_IP4_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 7)
614#define MVPP2_PE_ETH_TYPE_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 6)
615#define MVPP2_PE_VLAN_DBL (MVPP2_PRS_TCAM_SRAM_SIZE - 5)
616#define MVPP2_PE_VLAN_NONE (MVPP2_PRS_TCAM_SRAM_SIZE - 4)
617#define MVPP2_PE_MAC_MC_ALL (MVPP2_PRS_TCAM_SRAM_SIZE - 3)
618#define MVPP2_PE_MAC_PROMISCUOUS (MVPP2_PRS_TCAM_SRAM_SIZE - 2)
619#define MVPP2_PE_MAC_NON_PROMISCUOUS (MVPP2_PRS_TCAM_SRAM_SIZE - 1)
620
621/* Sram structure
622 * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(3)->(0).
623 */
624#define MVPP2_PRS_SRAM_RI_OFFS 0
625#define MVPP2_PRS_SRAM_RI_WORD 0
626#define MVPP2_PRS_SRAM_RI_CTRL_OFFS 32
627#define MVPP2_PRS_SRAM_RI_CTRL_WORD 1
628#define MVPP2_PRS_SRAM_RI_CTRL_BITS 32
629#define MVPP2_PRS_SRAM_SHIFT_OFFS 64
630#define MVPP2_PRS_SRAM_SHIFT_SIGN_BIT 72
631#define MVPP2_PRS_SRAM_UDF_OFFS 73
632#define MVPP2_PRS_SRAM_UDF_BITS 8
633#define MVPP2_PRS_SRAM_UDF_MASK 0xff
634#define MVPP2_PRS_SRAM_UDF_SIGN_BIT 81
635#define MVPP2_PRS_SRAM_UDF_TYPE_OFFS 82
636#define MVPP2_PRS_SRAM_UDF_TYPE_MASK 0x7
637#define MVPP2_PRS_SRAM_UDF_TYPE_L3 1
638#define MVPP2_PRS_SRAM_UDF_TYPE_L4 4
639#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS 85
640#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK 0x3
641#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD 1
642#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP4_ADD 2
643#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP6_ADD 3
644#define MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS 87
645#define MVPP2_PRS_SRAM_OP_SEL_UDF_BITS 2
646#define MVPP2_PRS_SRAM_OP_SEL_UDF_MASK 0x3
647#define MVPP2_PRS_SRAM_OP_SEL_UDF_ADD 0
648#define MVPP2_PRS_SRAM_OP_SEL_UDF_IP4_ADD 2
649#define MVPP2_PRS_SRAM_OP_SEL_UDF_IP6_ADD 3
650#define MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS 89
651#define MVPP2_PRS_SRAM_AI_OFFS 90
652#define MVPP2_PRS_SRAM_AI_CTRL_OFFS 98
653#define MVPP2_PRS_SRAM_AI_CTRL_BITS 8
654#define MVPP2_PRS_SRAM_AI_MASK 0xff
655#define MVPP2_PRS_SRAM_NEXT_LU_OFFS 106
656#define MVPP2_PRS_SRAM_NEXT_LU_MASK 0xf
657#define MVPP2_PRS_SRAM_LU_DONE_BIT 110
658#define MVPP2_PRS_SRAM_LU_GEN_BIT 111
659
660/* Sram result info bits assignment */
661#define MVPP2_PRS_RI_MAC_ME_MASK 0x1
662#define MVPP2_PRS_RI_DSA_MASK 0x2
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100663#define MVPP2_PRS_RI_VLAN_MASK (BIT(2) | BIT(3))
664#define MVPP2_PRS_RI_VLAN_NONE 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300665#define MVPP2_PRS_RI_VLAN_SINGLE BIT(2)
666#define MVPP2_PRS_RI_VLAN_DOUBLE BIT(3)
667#define MVPP2_PRS_RI_VLAN_TRIPLE (BIT(2) | BIT(3))
668#define MVPP2_PRS_RI_CPU_CODE_MASK 0x70
669#define MVPP2_PRS_RI_CPU_CODE_RX_SPEC BIT(4)
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100670#define MVPP2_PRS_RI_L2_CAST_MASK (BIT(9) | BIT(10))
671#define MVPP2_PRS_RI_L2_UCAST 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300672#define MVPP2_PRS_RI_L2_MCAST BIT(9)
673#define MVPP2_PRS_RI_L2_BCAST BIT(10)
674#define MVPP2_PRS_RI_PPPOE_MASK 0x800
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100675#define MVPP2_PRS_RI_L3_PROTO_MASK (BIT(12) | BIT(13) | BIT(14))
676#define MVPP2_PRS_RI_L3_UN 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300677#define MVPP2_PRS_RI_L3_IP4 BIT(12)
678#define MVPP2_PRS_RI_L3_IP4_OPT BIT(13)
679#define MVPP2_PRS_RI_L3_IP4_OTHER (BIT(12) | BIT(13))
680#define MVPP2_PRS_RI_L3_IP6 BIT(14)
681#define MVPP2_PRS_RI_L3_IP6_EXT (BIT(12) | BIT(14))
682#define MVPP2_PRS_RI_L3_ARP (BIT(13) | BIT(14))
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100683#define MVPP2_PRS_RI_L3_ADDR_MASK (BIT(15) | BIT(16))
684#define MVPP2_PRS_RI_L3_UCAST 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300685#define MVPP2_PRS_RI_L3_MCAST BIT(15)
686#define MVPP2_PRS_RI_L3_BCAST (BIT(15) | BIT(16))
687#define MVPP2_PRS_RI_IP_FRAG_MASK 0x20000
Stefan Chulskiaff3da32017-09-25 14:59:46 +0200688#define MVPP2_PRS_RI_IP_FRAG_TRUE BIT(17)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300689#define MVPP2_PRS_RI_UDF3_MASK 0x300000
690#define MVPP2_PRS_RI_UDF3_RX_SPECIAL BIT(21)
691#define MVPP2_PRS_RI_L4_PROTO_MASK 0x1c00000
692#define MVPP2_PRS_RI_L4_TCP BIT(22)
693#define MVPP2_PRS_RI_L4_UDP BIT(23)
694#define MVPP2_PRS_RI_L4_OTHER (BIT(22) | BIT(23))
695#define MVPP2_PRS_RI_UDF7_MASK 0x60000000
696#define MVPP2_PRS_RI_UDF7_IP6_LITE BIT(29)
697#define MVPP2_PRS_RI_DROP_MASK 0x80000000
698
699/* Sram additional info bits assignment */
700#define MVPP2_PRS_IPV4_DIP_AI_BIT BIT(0)
701#define MVPP2_PRS_IPV6_NO_EXT_AI_BIT BIT(0)
702#define MVPP2_PRS_IPV6_EXT_AI_BIT BIT(1)
703#define MVPP2_PRS_IPV6_EXT_AH_AI_BIT BIT(2)
704#define MVPP2_PRS_IPV6_EXT_AH_LEN_AI_BIT BIT(3)
705#define MVPP2_PRS_IPV6_EXT_AH_L4_AI_BIT BIT(4)
706#define MVPP2_PRS_SINGLE_VLAN_AI 0
707#define MVPP2_PRS_DBL_VLAN_AI_BIT BIT(7)
708
709/* DSA/EDSA type */
710#define MVPP2_PRS_TAGGED true
711#define MVPP2_PRS_UNTAGGED false
712#define MVPP2_PRS_EDSA true
713#define MVPP2_PRS_DSA false
714
715/* MAC entries, shadow udf */
716enum mvpp2_prs_udf {
717 MVPP2_PRS_UDF_MAC_DEF,
718 MVPP2_PRS_UDF_MAC_RANGE,
719 MVPP2_PRS_UDF_L2_DEF,
720 MVPP2_PRS_UDF_L2_DEF_COPY,
721 MVPP2_PRS_UDF_L2_USER,
722};
723
724/* Lookup ID */
725enum mvpp2_prs_lookup {
726 MVPP2_PRS_LU_MH,
727 MVPP2_PRS_LU_MAC,
728 MVPP2_PRS_LU_DSA,
729 MVPP2_PRS_LU_VLAN,
730 MVPP2_PRS_LU_L2,
731 MVPP2_PRS_LU_PPPOE,
732 MVPP2_PRS_LU_IP4,
733 MVPP2_PRS_LU_IP6,
734 MVPP2_PRS_LU_FLOWS,
735 MVPP2_PRS_LU_LAST,
736};
737
738/* L3 cast enum */
739enum mvpp2_prs_l3_cast {
740 MVPP2_PRS_L3_UNI_CAST,
741 MVPP2_PRS_L3_MULTI_CAST,
742 MVPP2_PRS_L3_BROAD_CAST
743};
744
745/* Classifier constants */
746#define MVPP2_CLS_FLOWS_TBL_SIZE 512
747#define MVPP2_CLS_FLOWS_TBL_DATA_WORDS 3
748#define MVPP2_CLS_LKP_TBL_SIZE 64
749
750/* BM constants */
751#define MVPP2_BM_POOLS_NUM 8
752#define MVPP2_BM_LONG_BUF_NUM 1024
753#define MVPP2_BM_SHORT_BUF_NUM 2048
754#define MVPP2_BM_POOL_SIZE_MAX (16*1024 - MVPP2_BM_POOL_PTR_ALIGN/4)
755#define MVPP2_BM_POOL_PTR_ALIGN 128
756#define MVPP2_BM_SWF_LONG_POOL(port) ((port > 2) ? 2 : port)
757#define MVPP2_BM_SWF_SHORT_POOL 3
758
759/* BM cookie (32 bits) definition */
760#define MVPP2_BM_COOKIE_POOL_OFFS 8
761#define MVPP2_BM_COOKIE_CPU_OFFS 24
762
763/* BM short pool packet size
764 * These value assure that for SWF the total number
765 * of bytes allocated for each buffer will be 512
766 */
767#define MVPP2_BM_SHORT_PKT_SIZE MVPP2_RX_MAX_PKT_SIZE(512)
768
Thomas Petazzonia7868412017-03-07 16:53:13 +0100769#define MVPP21_ADDR_SPACE_SZ 0
770#define MVPP22_ADDR_SPACE_SZ SZ_64K
771
Thomas Petazzonidf089aa2017-08-03 10:41:58 +0200772#define MVPP2_MAX_THREADS 8
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +0200773#define MVPP2_MAX_QVECS MVPP2_MAX_THREADS
Thomas Petazzonia7868412017-03-07 16:53:13 +0100774
Marcin Wojtas3f518502014-07-10 16:52:13 -0300775enum mvpp2_bm_type {
776 MVPP2_BM_FREE,
777 MVPP2_BM_SWF_LONG,
778 MVPP2_BM_SWF_SHORT
779};
780
781/* Definitions */
782
783/* Shared Packet Processor resources */
784struct mvpp2 {
785 /* Shared registers' base addresses */
Marcin Wojtas3f518502014-07-10 16:52:13 -0300786 void __iomem *lms_base;
Thomas Petazzonia7868412017-03-07 16:53:13 +0100787 void __iomem *iface_base;
788
Thomas Petazzonidf089aa2017-08-03 10:41:58 +0200789 /* On PPv2.2, each "software thread" can access the base
790 * register through a separate address space, each 64 KB apart
791 * from each other. Typically, such address spaces will be
792 * used per CPU.
Thomas Petazzonia7868412017-03-07 16:53:13 +0100793 */
Thomas Petazzonidf089aa2017-08-03 10:41:58 +0200794 void __iomem *swth_base[MVPP2_MAX_THREADS];
Marcin Wojtas3f518502014-07-10 16:52:13 -0300795
Antoine Ténartf84bf382017-08-22 19:08:27 +0200796 /* On PPv2.2, some port control registers are located into the system
797 * controller space. These registers are accessible through a regmap.
798 */
799 struct regmap *sysctrl_base;
800
Marcin Wojtas3f518502014-07-10 16:52:13 -0300801 /* Common clocks */
802 struct clk *pp_clk;
803 struct clk *gop_clk;
Thomas Petazzonifceb55d2017-03-07 16:53:18 +0100804 struct clk *mg_clk;
Gregory CLEMENT4792ea02017-09-29 14:27:39 +0200805 struct clk *axi_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300806
807 /* List of pointers to port structures */
808 struct mvpp2_port **port_list;
809
810 /* Aggregated TXQs */
811 struct mvpp2_tx_queue *aggr_txqs;
812
813 /* BM pools */
814 struct mvpp2_bm_pool *bm_pools;
815
816 /* PRS shadow table */
817 struct mvpp2_prs_shadow *prs_shadow;
818 /* PRS auxiliary table for double vlan entries control */
819 bool *prs_double_vlans;
820
821 /* Tclk value */
822 u32 tclk;
Thomas Petazzonifaca9242017-03-07 16:53:06 +0100823
824 /* HW version */
825 enum { MVPP21, MVPP22 } hw_version;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +0100826
827 /* Maximum number of RXQs per port */
828 unsigned int max_port_rxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300829};
830
831struct mvpp2_pcpu_stats {
832 struct u64_stats_sync syncp;
833 u64 rx_packets;
834 u64 rx_bytes;
835 u64 tx_packets;
836 u64 tx_bytes;
837};
838
Marcin Wojtasedc660f2015-08-06 19:00:30 +0200839/* Per-CPU port control */
840struct mvpp2_port_pcpu {
841 struct hrtimer tx_done_timer;
842 bool timer_scheduled;
843 /* Tasklet for egress finalization */
844 struct tasklet_struct tx_done_tasklet;
845};
846
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +0200847struct mvpp2_queue_vector {
848 int irq;
849 struct napi_struct napi;
850 enum { MVPP2_QUEUE_VECTOR_SHARED, MVPP2_QUEUE_VECTOR_PRIVATE } type;
851 int sw_thread_id;
852 u16 sw_thread_mask;
853 int first_rxq;
854 int nrxqs;
855 u32 pending_cause_rx;
856 struct mvpp2_port *port;
857};
858
Marcin Wojtas3f518502014-07-10 16:52:13 -0300859struct mvpp2_port {
860 u8 id;
861
Thomas Petazzonia7868412017-03-07 16:53:13 +0100862 /* Index of the port from the "group of ports" complex point
863 * of view
864 */
865 int gop_id;
866
Antoine Tenartfd3651b2017-09-01 11:04:54 +0200867 int link_irq;
868
Marcin Wojtas3f518502014-07-10 16:52:13 -0300869 struct mvpp2 *priv;
870
871 /* Per-port registers' base address */
872 void __iomem *base;
873
874 struct mvpp2_rx_queue **rxqs;
Thomas Petazzoni09f83972017-08-03 10:41:57 +0200875 unsigned int nrxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300876 struct mvpp2_tx_queue **txqs;
Thomas Petazzoni09f83972017-08-03 10:41:57 +0200877 unsigned int ntxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300878 struct net_device *dev;
879
880 int pkt_size;
881
Marcin Wojtasedc660f2015-08-06 19:00:30 +0200882 /* Per-CPU port control */
883 struct mvpp2_port_pcpu __percpu *pcpu;
884
Marcin Wojtas3f518502014-07-10 16:52:13 -0300885 /* Flags */
886 unsigned long flags;
887
888 u16 tx_ring_size;
889 u16 rx_ring_size;
890 struct mvpp2_pcpu_stats __percpu *stats;
891
Marcin Wojtas3f518502014-07-10 16:52:13 -0300892 phy_interface_t phy_interface;
893 struct device_node *phy_node;
Antoine Tenart542897d2017-08-30 10:29:15 +0200894 struct phy *comphy;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300895 unsigned int link;
896 unsigned int duplex;
897 unsigned int speed;
898
899 struct mvpp2_bm_pool *pool_long;
900 struct mvpp2_bm_pool *pool_short;
901
902 /* Index of first port's physical RXQ */
903 u8 first_rxq;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +0200904
905 struct mvpp2_queue_vector qvecs[MVPP2_MAX_QVECS];
906 unsigned int nqvecs;
Thomas Petazzoni213f4282017-08-03 10:42:00 +0200907 bool has_tx_irqs;
908
909 u32 tx_time_coal;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300910};
911
912/* The mvpp2_tx_desc and mvpp2_rx_desc structures describe the
913 * layout of the transmit and reception DMA descriptors, and their
914 * layout is therefore defined by the hardware design
915 */
916
917#define MVPP2_TXD_L3_OFF_SHIFT 0
918#define MVPP2_TXD_IP_HLEN_SHIFT 8
919#define MVPP2_TXD_L4_CSUM_FRAG BIT(13)
920#define MVPP2_TXD_L4_CSUM_NOT BIT(14)
921#define MVPP2_TXD_IP_CSUM_DISABLE BIT(15)
922#define MVPP2_TXD_PADDING_DISABLE BIT(23)
923#define MVPP2_TXD_L4_UDP BIT(24)
924#define MVPP2_TXD_L3_IP6 BIT(26)
925#define MVPP2_TXD_L_DESC BIT(28)
926#define MVPP2_TXD_F_DESC BIT(29)
927
928#define MVPP2_RXD_ERR_SUMMARY BIT(15)
929#define MVPP2_RXD_ERR_CODE_MASK (BIT(13) | BIT(14))
930#define MVPP2_RXD_ERR_CRC 0x0
931#define MVPP2_RXD_ERR_OVERRUN BIT(13)
932#define MVPP2_RXD_ERR_RESOURCE (BIT(13) | BIT(14))
933#define MVPP2_RXD_BM_POOL_ID_OFFS 16
934#define MVPP2_RXD_BM_POOL_ID_MASK (BIT(16) | BIT(17) | BIT(18))
935#define MVPP2_RXD_HWF_SYNC BIT(21)
936#define MVPP2_RXD_L4_CSUM_OK BIT(22)
937#define MVPP2_RXD_IP4_HEADER_ERR BIT(24)
938#define MVPP2_RXD_L4_TCP BIT(25)
939#define MVPP2_RXD_L4_UDP BIT(26)
940#define MVPP2_RXD_L3_IP4 BIT(28)
941#define MVPP2_RXD_L3_IP6 BIT(30)
942#define MVPP2_RXD_BUF_HDR BIT(31)
943
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100944/* HW TX descriptor for PPv2.1 */
945struct mvpp21_tx_desc {
Marcin Wojtas3f518502014-07-10 16:52:13 -0300946 u32 command; /* Options used by HW for packet transmitting.*/
947 u8 packet_offset; /* the offset from the buffer beginning */
948 u8 phys_txq; /* destination queue ID */
949 u16 data_size; /* data size of transmitted packet in bytes */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100950 u32 buf_dma_addr; /* physical addr of transmitted buffer */
Marcin Wojtas3f518502014-07-10 16:52:13 -0300951 u32 buf_cookie; /* cookie for access to TX buffer in tx path */
952 u32 reserved1[3]; /* hw_cmd (for future use, BM, PON, PNC) */
953 u32 reserved2; /* reserved (for future use) */
954};
955
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100956/* HW RX descriptor for PPv2.1 */
957struct mvpp21_rx_desc {
Marcin Wojtas3f518502014-07-10 16:52:13 -0300958 u32 status; /* info about received packet */
959 u16 reserved1; /* parser_info (for future use, PnC) */
960 u16 data_size; /* size of received packet in bytes */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100961 u32 buf_dma_addr; /* physical address of the buffer */
Marcin Wojtas3f518502014-07-10 16:52:13 -0300962 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
963 u16 reserved2; /* gem_port_id (for future use, PON) */
964 u16 reserved3; /* csum_l4 (for future use, PnC) */
965 u8 reserved4; /* bm_qset (for future use, BM) */
966 u8 reserved5;
967 u16 reserved6; /* classify_info (for future use, PnC) */
968 u32 reserved7; /* flow_id (for future use, PnC) */
969 u32 reserved8;
970};
971
Thomas Petazzonie7c53592017-03-07 16:53:08 +0100972/* HW TX descriptor for PPv2.2 */
973struct mvpp22_tx_desc {
974 u32 command;
975 u8 packet_offset;
976 u8 phys_txq;
977 u16 data_size;
978 u64 reserved1;
979 u64 buf_dma_addr_ptp;
980 u64 buf_cookie_misc;
981};
982
983/* HW RX descriptor for PPv2.2 */
984struct mvpp22_rx_desc {
985 u32 status;
986 u16 reserved1;
987 u16 data_size;
988 u32 reserved2;
989 u32 reserved3;
990 u64 buf_dma_addr_key_hash;
991 u64 buf_cookie_misc;
992};
993
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100994/* Opaque type used by the driver to manipulate the HW TX and RX
995 * descriptors
996 */
997struct mvpp2_tx_desc {
998 union {
999 struct mvpp21_tx_desc pp21;
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001000 struct mvpp22_tx_desc pp22;
Thomas Petazzoni054f6372017-03-07 16:53:07 +01001001 };
1002};
1003
1004struct mvpp2_rx_desc {
1005 union {
1006 struct mvpp21_rx_desc pp21;
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001007 struct mvpp22_rx_desc pp22;
Thomas Petazzoni054f6372017-03-07 16:53:07 +01001008 };
1009};
1010
Thomas Petazzoni83544912016-12-21 11:28:49 +01001011struct mvpp2_txq_pcpu_buf {
1012 /* Transmitted SKB */
1013 struct sk_buff *skb;
1014
1015 /* Physical address of transmitted buffer */
Thomas Petazzoni20396132017-03-07 16:53:00 +01001016 dma_addr_t dma;
Thomas Petazzoni83544912016-12-21 11:28:49 +01001017
1018 /* Size transmitted */
1019 size_t size;
1020};
1021
Marcin Wojtas3f518502014-07-10 16:52:13 -03001022/* Per-CPU Tx queue control */
1023struct mvpp2_txq_pcpu {
1024 int cpu;
1025
1026 /* Number of Tx DMA descriptors in the descriptor ring */
1027 int size;
1028
1029 /* Number of currently used Tx DMA descriptor in the
1030 * descriptor ring
1031 */
1032 int count;
1033
1034 /* Number of Tx DMA descriptors reserved for each CPU */
1035 int reserved_num;
1036
Thomas Petazzoni83544912016-12-21 11:28:49 +01001037 /* Infos about transmitted buffers */
1038 struct mvpp2_txq_pcpu_buf *buffs;
Marcin Wojtas71ce3912015-08-06 19:00:29 +02001039
Marcin Wojtas3f518502014-07-10 16:52:13 -03001040 /* Index of last TX DMA descriptor that was inserted */
1041 int txq_put_index;
1042
1043 /* Index of the TX DMA descriptor to be cleaned up */
1044 int txq_get_index;
Antoine Ténart186cd4d2017-08-23 09:46:56 +02001045
1046 /* DMA buffer for TSO headers */
1047 char *tso_headers;
1048 dma_addr_t tso_headers_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001049};
1050
1051struct mvpp2_tx_queue {
1052 /* Physical number of this Tx queue */
1053 u8 id;
1054
1055 /* Logical number of this Tx queue */
1056 u8 log_id;
1057
1058 /* Number of Tx DMA descriptors in the descriptor ring */
1059 int size;
1060
1061 /* Number of currently used Tx DMA descriptor in the descriptor ring */
1062 int count;
1063
1064 /* Per-CPU control of physical Tx queues */
1065 struct mvpp2_txq_pcpu __percpu *pcpu;
1066
Marcin Wojtas3f518502014-07-10 16:52:13 -03001067 u32 done_pkts_coal;
1068
1069 /* Virtual address of thex Tx DMA descriptors array */
1070 struct mvpp2_tx_desc *descs;
1071
1072 /* DMA address of the Tx DMA descriptors array */
Thomas Petazzoni20396132017-03-07 16:53:00 +01001073 dma_addr_t descs_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001074
1075 /* Index of the last Tx DMA descriptor */
1076 int last_desc;
1077
1078 /* Index of the next Tx DMA descriptor to process */
1079 int next_desc_to_proc;
1080};
1081
1082struct mvpp2_rx_queue {
1083 /* RX queue number, in the range 0-31 for physical RXQs */
1084 u8 id;
1085
1086 /* Num of rx descriptors in the rx descriptor ring */
1087 int size;
1088
1089 u32 pkts_coal;
1090 u32 time_coal;
1091
1092 /* Virtual address of the RX DMA descriptors array */
1093 struct mvpp2_rx_desc *descs;
1094
1095 /* DMA address of the RX DMA descriptors array */
Thomas Petazzoni20396132017-03-07 16:53:00 +01001096 dma_addr_t descs_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001097
1098 /* Index of the last RX DMA descriptor */
1099 int last_desc;
1100
1101 /* Index of the next RX DMA descriptor to process */
1102 int next_desc_to_proc;
1103
1104 /* ID of port to which physical RXQ is mapped */
1105 int port;
1106
1107 /* Port's logic RXQ number to which physical RXQ is mapped */
1108 int logic_rxq;
1109};
1110
1111union mvpp2_prs_tcam_entry {
1112 u32 word[MVPP2_PRS_TCAM_WORDS];
1113 u8 byte[MVPP2_PRS_TCAM_WORDS * 4];
1114};
1115
1116union mvpp2_prs_sram_entry {
1117 u32 word[MVPP2_PRS_SRAM_WORDS];
1118 u8 byte[MVPP2_PRS_SRAM_WORDS * 4];
1119};
1120
1121struct mvpp2_prs_entry {
1122 u32 index;
1123 union mvpp2_prs_tcam_entry tcam;
1124 union mvpp2_prs_sram_entry sram;
1125};
1126
1127struct mvpp2_prs_shadow {
1128 bool valid;
1129 bool finish;
1130
1131 /* Lookup ID */
1132 int lu;
1133
1134 /* User defined offset */
1135 int udf;
1136
1137 /* Result info */
1138 u32 ri;
1139 u32 ri_mask;
1140};
1141
1142struct mvpp2_cls_flow_entry {
1143 u32 index;
1144 u32 data[MVPP2_CLS_FLOWS_TBL_DATA_WORDS];
1145};
1146
1147struct mvpp2_cls_lookup_entry {
1148 u32 lkpid;
1149 u32 way;
1150 u32 data;
1151};
1152
1153struct mvpp2_bm_pool {
1154 /* Pool number in the range 0-7 */
1155 int id;
1156 enum mvpp2_bm_type type;
1157
1158 /* Buffer Pointers Pool External (BPPE) size */
1159 int size;
Thomas Petazzonid01524d2017-03-07 16:53:09 +01001160 /* BPPE size in bytes */
1161 int size_bytes;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001162 /* Number of buffers for this pool */
1163 int buf_num;
1164 /* Pool buffer size */
1165 int buf_size;
1166 /* Packet size */
1167 int pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01001168 int frag_size;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001169
1170 /* BPPE virtual base address */
1171 u32 *virt_addr;
Thomas Petazzoni20396132017-03-07 16:53:00 +01001172 /* BPPE DMA base address */
1173 dma_addr_t dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001174
1175 /* Ports using BM pool */
1176 u32 port_map;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001177};
1178
Antoine Tenart20920262017-10-23 15:24:30 +02001179#define IS_TSO_HEADER(txq_pcpu, addr) \
1180 ((addr) >= (txq_pcpu)->tso_headers_dma && \
1181 (addr) < (txq_pcpu)->tso_headers_dma + \
1182 (txq_pcpu)->size * TSO_HEADER_SIZE)
1183
Thomas Petazzoni213f4282017-08-03 10:42:00 +02001184/* Queue modes */
1185#define MVPP2_QDIST_SINGLE_MODE 0
1186#define MVPP2_QDIST_MULTI_MODE 1
1187
1188static int queue_mode = MVPP2_QDIST_SINGLE_MODE;
1189
1190module_param(queue_mode, int, 0444);
1191MODULE_PARM_DESC(queue_mode, "Set queue_mode (single=0, multi=1)");
1192
Marcin Wojtas3f518502014-07-10 16:52:13 -03001193#define MVPP2_DRIVER_NAME "mvpp2"
1194#define MVPP2_DRIVER_VERSION "1.0"
1195
1196/* Utility/helper methods */
1197
1198static void mvpp2_write(struct mvpp2 *priv, u32 offset, u32 data)
1199{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001200 writel(data, priv->swth_base[0] + offset);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001201}
1202
1203static u32 mvpp2_read(struct mvpp2 *priv, u32 offset)
1204{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001205 return readl(priv->swth_base[0] + offset);
Thomas Petazzonia7868412017-03-07 16:53:13 +01001206}
1207
1208/* These accessors should be used to access:
1209 *
1210 * - per-CPU registers, where each CPU has its own copy of the
1211 * register.
1212 *
1213 * MVPP2_BM_VIRT_ALLOC_REG
1214 * MVPP2_BM_ADDR_HIGH_ALLOC
1215 * MVPP22_BM_ADDR_HIGH_RLS_REG
1216 * MVPP2_BM_VIRT_RLS_REG
1217 * MVPP2_ISR_RX_TX_CAUSE_REG
1218 * MVPP2_ISR_RX_TX_MASK_REG
1219 * MVPP2_TXQ_NUM_REG
1220 * MVPP2_AGGR_TXQ_UPDATE_REG
1221 * MVPP2_TXQ_RSVD_REQ_REG
1222 * MVPP2_TXQ_RSVD_RSLT_REG
1223 * MVPP2_TXQ_SENT_REG
1224 * MVPP2_RXQ_NUM_REG
1225 *
1226 * - global registers that must be accessed through a specific CPU
1227 * window, because they are related to an access to a per-CPU
1228 * register
1229 *
1230 * MVPP2_BM_PHY_ALLOC_REG (related to MVPP2_BM_VIRT_ALLOC_REG)
1231 * MVPP2_BM_PHY_RLS_REG (related to MVPP2_BM_VIRT_RLS_REG)
1232 * MVPP2_RXQ_THRESH_REG (related to MVPP2_RXQ_NUM_REG)
1233 * MVPP2_RXQ_DESC_ADDR_REG (related to MVPP2_RXQ_NUM_REG)
1234 * MVPP2_RXQ_DESC_SIZE_REG (related to MVPP2_RXQ_NUM_REG)
1235 * MVPP2_RXQ_INDEX_REG (related to MVPP2_RXQ_NUM_REG)
1236 * MVPP2_TXQ_PENDING_REG (related to MVPP2_TXQ_NUM_REG)
1237 * MVPP2_TXQ_DESC_ADDR_REG (related to MVPP2_TXQ_NUM_REG)
1238 * MVPP2_TXQ_DESC_SIZE_REG (related to MVPP2_TXQ_NUM_REG)
1239 * MVPP2_TXQ_INDEX_REG (related to MVPP2_TXQ_NUM_REG)
1240 * MVPP2_TXQ_PENDING_REG (related to MVPP2_TXQ_NUM_REG)
1241 * MVPP2_TXQ_PREF_BUF_REG (related to MVPP2_TXQ_NUM_REG)
1242 * MVPP2_TXQ_PREF_BUF_REG (related to MVPP2_TXQ_NUM_REG)
1243 */
1244static void mvpp2_percpu_write(struct mvpp2 *priv, int cpu,
1245 u32 offset, u32 data)
1246{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001247 writel(data, priv->swth_base[cpu] + offset);
Thomas Petazzonia7868412017-03-07 16:53:13 +01001248}
1249
1250static u32 mvpp2_percpu_read(struct mvpp2 *priv, int cpu,
1251 u32 offset)
1252{
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02001253 return readl(priv->swth_base[cpu] + offset);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001254}
1255
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001256static dma_addr_t mvpp2_txdesc_dma_addr_get(struct mvpp2_port *port,
1257 struct mvpp2_tx_desc *tx_desc)
1258{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001259 if (port->priv->hw_version == MVPP21)
1260 return tx_desc->pp21.buf_dma_addr;
1261 else
1262 return tx_desc->pp22.buf_dma_addr_ptp & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001263}
1264
1265static void mvpp2_txdesc_dma_addr_set(struct mvpp2_port *port,
1266 struct mvpp2_tx_desc *tx_desc,
1267 dma_addr_t dma_addr)
1268{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001269 if (port->priv->hw_version == MVPP21) {
1270 tx_desc->pp21.buf_dma_addr = dma_addr;
1271 } else {
1272 u64 val = (u64)dma_addr;
1273
1274 tx_desc->pp22.buf_dma_addr_ptp &= ~GENMASK_ULL(40, 0);
1275 tx_desc->pp22.buf_dma_addr_ptp |= val;
1276 }
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001277}
1278
1279static size_t mvpp2_txdesc_size_get(struct mvpp2_port *port,
1280 struct mvpp2_tx_desc *tx_desc)
1281{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001282 if (port->priv->hw_version == MVPP21)
1283 return tx_desc->pp21.data_size;
1284 else
1285 return tx_desc->pp22.data_size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001286}
1287
1288static void mvpp2_txdesc_size_set(struct mvpp2_port *port,
1289 struct mvpp2_tx_desc *tx_desc,
1290 size_t size)
1291{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001292 if (port->priv->hw_version == MVPP21)
1293 tx_desc->pp21.data_size = size;
1294 else
1295 tx_desc->pp22.data_size = size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001296}
1297
1298static void mvpp2_txdesc_txq_set(struct mvpp2_port *port,
1299 struct mvpp2_tx_desc *tx_desc,
1300 unsigned int txq)
1301{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001302 if (port->priv->hw_version == MVPP21)
1303 tx_desc->pp21.phys_txq = txq;
1304 else
1305 tx_desc->pp22.phys_txq = txq;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001306}
1307
1308static void mvpp2_txdesc_cmd_set(struct mvpp2_port *port,
1309 struct mvpp2_tx_desc *tx_desc,
1310 unsigned int command)
1311{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001312 if (port->priv->hw_version == MVPP21)
1313 tx_desc->pp21.command = command;
1314 else
1315 tx_desc->pp22.command = command;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001316}
1317
1318static void mvpp2_txdesc_offset_set(struct mvpp2_port *port,
1319 struct mvpp2_tx_desc *tx_desc,
1320 unsigned int offset)
1321{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001322 if (port->priv->hw_version == MVPP21)
1323 tx_desc->pp21.packet_offset = offset;
1324 else
1325 tx_desc->pp22.packet_offset = offset;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001326}
1327
1328static unsigned int mvpp2_txdesc_offset_get(struct mvpp2_port *port,
1329 struct mvpp2_tx_desc *tx_desc)
1330{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001331 if (port->priv->hw_version == MVPP21)
1332 return tx_desc->pp21.packet_offset;
1333 else
1334 return tx_desc->pp22.packet_offset;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001335}
1336
1337static dma_addr_t mvpp2_rxdesc_dma_addr_get(struct mvpp2_port *port,
1338 struct mvpp2_rx_desc *rx_desc)
1339{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001340 if (port->priv->hw_version == MVPP21)
1341 return rx_desc->pp21.buf_dma_addr;
1342 else
1343 return rx_desc->pp22.buf_dma_addr_key_hash & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001344}
1345
1346static unsigned long mvpp2_rxdesc_cookie_get(struct mvpp2_port *port,
1347 struct mvpp2_rx_desc *rx_desc)
1348{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001349 if (port->priv->hw_version == MVPP21)
1350 return rx_desc->pp21.buf_cookie;
1351 else
1352 return rx_desc->pp22.buf_cookie_misc & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001353}
1354
1355static size_t mvpp2_rxdesc_size_get(struct mvpp2_port *port,
1356 struct mvpp2_rx_desc *rx_desc)
1357{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001358 if (port->priv->hw_version == MVPP21)
1359 return rx_desc->pp21.data_size;
1360 else
1361 return rx_desc->pp22.data_size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001362}
1363
1364static u32 mvpp2_rxdesc_status_get(struct mvpp2_port *port,
1365 struct mvpp2_rx_desc *rx_desc)
1366{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001367 if (port->priv->hw_version == MVPP21)
1368 return rx_desc->pp21.status;
1369 else
1370 return rx_desc->pp22.status;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001371}
1372
Marcin Wojtas3f518502014-07-10 16:52:13 -03001373static void mvpp2_txq_inc_get(struct mvpp2_txq_pcpu *txq_pcpu)
1374{
1375 txq_pcpu->txq_get_index++;
1376 if (txq_pcpu->txq_get_index == txq_pcpu->size)
1377 txq_pcpu->txq_get_index = 0;
1378}
1379
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001380static void mvpp2_txq_inc_put(struct mvpp2_port *port,
1381 struct mvpp2_txq_pcpu *txq_pcpu,
Marcin Wojtas71ce3912015-08-06 19:00:29 +02001382 struct sk_buff *skb,
1383 struct mvpp2_tx_desc *tx_desc)
Marcin Wojtas3f518502014-07-10 16:52:13 -03001384{
Thomas Petazzoni83544912016-12-21 11:28:49 +01001385 struct mvpp2_txq_pcpu_buf *tx_buf =
1386 txq_pcpu->buffs + txq_pcpu->txq_put_index;
1387 tx_buf->skb = skb;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001388 tx_buf->size = mvpp2_txdesc_size_get(port, tx_desc);
1389 tx_buf->dma = mvpp2_txdesc_dma_addr_get(port, tx_desc) +
1390 mvpp2_txdesc_offset_get(port, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001391 txq_pcpu->txq_put_index++;
1392 if (txq_pcpu->txq_put_index == txq_pcpu->size)
1393 txq_pcpu->txq_put_index = 0;
1394}
1395
1396/* Get number of physical egress port */
1397static inline int mvpp2_egress_port(struct mvpp2_port *port)
1398{
1399 return MVPP2_MAX_TCONT + port->id;
1400}
1401
1402/* Get number of physical TXQ */
1403static inline int mvpp2_txq_phys(int port, int txq)
1404{
1405 return (MVPP2_MAX_TCONT + port) * MVPP2_MAX_TXQ + txq;
1406}
1407
1408/* Parser configuration routines */
1409
1410/* Update parser tcam and sram hw entries */
1411static int mvpp2_prs_hw_write(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1412{
1413 int i;
1414
1415 if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1416 return -EINVAL;
1417
1418 /* Clear entry invalidation bit */
1419 pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] &= ~MVPP2_PRS_TCAM_INV_MASK;
1420
1421 /* Write tcam index - indirect access */
1422 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1423 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1424 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), pe->tcam.word[i]);
1425
1426 /* Write sram index - indirect access */
1427 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1428 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1429 mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), pe->sram.word[i]);
1430
1431 return 0;
1432}
1433
1434/* Read tcam entry from hw */
1435static int mvpp2_prs_hw_read(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1436{
1437 int i;
1438
1439 if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1440 return -EINVAL;
1441
1442 /* Write tcam index - indirect access */
1443 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1444
1445 pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] = mvpp2_read(priv,
1446 MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD));
1447 if (pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] & MVPP2_PRS_TCAM_INV_MASK)
1448 return MVPP2_PRS_TCAM_ENTRY_INVALID;
1449
1450 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1451 pe->tcam.word[i] = mvpp2_read(priv, MVPP2_PRS_TCAM_DATA_REG(i));
1452
1453 /* Write sram index - indirect access */
1454 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1455 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1456 pe->sram.word[i] = mvpp2_read(priv, MVPP2_PRS_SRAM_DATA_REG(i));
1457
1458 return 0;
1459}
1460
1461/* Invalidate tcam hw entry */
1462static void mvpp2_prs_hw_inv(struct mvpp2 *priv, int index)
1463{
1464 /* Write index - indirect access */
1465 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
1466 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD),
1467 MVPP2_PRS_TCAM_INV_MASK);
1468}
1469
1470/* Enable shadow table entry and set its lookup ID */
1471static void mvpp2_prs_shadow_set(struct mvpp2 *priv, int index, int lu)
1472{
1473 priv->prs_shadow[index].valid = true;
1474 priv->prs_shadow[index].lu = lu;
1475}
1476
1477/* Update ri fields in shadow table entry */
1478static void mvpp2_prs_shadow_ri_set(struct mvpp2 *priv, int index,
1479 unsigned int ri, unsigned int ri_mask)
1480{
1481 priv->prs_shadow[index].ri_mask = ri_mask;
1482 priv->prs_shadow[index].ri = ri;
1483}
1484
1485/* Update lookup field in tcam sw entry */
1486static void mvpp2_prs_tcam_lu_set(struct mvpp2_prs_entry *pe, unsigned int lu)
1487{
1488 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_LU_BYTE);
1489
1490 pe->tcam.byte[MVPP2_PRS_TCAM_LU_BYTE] = lu;
1491 pe->tcam.byte[enable_off] = MVPP2_PRS_LU_MASK;
1492}
1493
1494/* Update mask for single port in tcam sw entry */
1495static void mvpp2_prs_tcam_port_set(struct mvpp2_prs_entry *pe,
1496 unsigned int port, bool add)
1497{
1498 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1499
1500 if (add)
1501 pe->tcam.byte[enable_off] &= ~(1 << port);
1502 else
1503 pe->tcam.byte[enable_off] |= 1 << port;
1504}
1505
1506/* Update port map in tcam sw entry */
1507static void mvpp2_prs_tcam_port_map_set(struct mvpp2_prs_entry *pe,
1508 unsigned int ports)
1509{
1510 unsigned char port_mask = MVPP2_PRS_PORT_MASK;
1511 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1512
1513 pe->tcam.byte[MVPP2_PRS_TCAM_PORT_BYTE] = 0;
1514 pe->tcam.byte[enable_off] &= ~port_mask;
1515 pe->tcam.byte[enable_off] |= ~ports & MVPP2_PRS_PORT_MASK;
1516}
1517
1518/* Obtain port map from tcam sw entry */
1519static unsigned int mvpp2_prs_tcam_port_map_get(struct mvpp2_prs_entry *pe)
1520{
1521 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1522
1523 return ~(pe->tcam.byte[enable_off]) & MVPP2_PRS_PORT_MASK;
1524}
1525
1526/* Set byte of data and its enable bits in tcam sw entry */
1527static void mvpp2_prs_tcam_data_byte_set(struct mvpp2_prs_entry *pe,
1528 unsigned int offs, unsigned char byte,
1529 unsigned char enable)
1530{
1531 pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)] = byte;
1532 pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)] = enable;
1533}
1534
1535/* Get byte of data and its enable bits from tcam sw entry */
1536static void mvpp2_prs_tcam_data_byte_get(struct mvpp2_prs_entry *pe,
1537 unsigned int offs, unsigned char *byte,
1538 unsigned char *enable)
1539{
1540 *byte = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)];
1541 *enable = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)];
1542}
1543
1544/* Compare tcam data bytes with a pattern */
1545static bool mvpp2_prs_tcam_data_cmp(struct mvpp2_prs_entry *pe, int offs,
1546 u16 data)
1547{
1548 int off = MVPP2_PRS_TCAM_DATA_BYTE(offs);
1549 u16 tcam_data;
1550
Antoine Tenartef4816f2017-10-24 11:41:26 +02001551 tcam_data = (pe->tcam.byte[off + 1] << 8) | pe->tcam.byte[off];
Marcin Wojtas3f518502014-07-10 16:52:13 -03001552 if (tcam_data != data)
1553 return false;
1554 return true;
1555}
1556
1557/* Update ai bits in tcam sw entry */
1558static void mvpp2_prs_tcam_ai_update(struct mvpp2_prs_entry *pe,
1559 unsigned int bits, unsigned int enable)
1560{
1561 int i, ai_idx = MVPP2_PRS_TCAM_AI_BYTE;
1562
1563 for (i = 0; i < MVPP2_PRS_AI_BITS; i++) {
1564
1565 if (!(enable & BIT(i)))
1566 continue;
1567
1568 if (bits & BIT(i))
1569 pe->tcam.byte[ai_idx] |= 1 << i;
1570 else
1571 pe->tcam.byte[ai_idx] &= ~(1 << i);
1572 }
1573
1574 pe->tcam.byte[MVPP2_PRS_TCAM_EN_OFFS(ai_idx)] |= enable;
1575}
1576
1577/* Get ai bits from tcam sw entry */
1578static int mvpp2_prs_tcam_ai_get(struct mvpp2_prs_entry *pe)
1579{
1580 return pe->tcam.byte[MVPP2_PRS_TCAM_AI_BYTE];
1581}
1582
1583/* Set ethertype in tcam sw entry */
1584static void mvpp2_prs_match_etype(struct mvpp2_prs_entry *pe, int offset,
1585 unsigned short ethertype)
1586{
1587 mvpp2_prs_tcam_data_byte_set(pe, offset + 0, ethertype >> 8, 0xff);
1588 mvpp2_prs_tcam_data_byte_set(pe, offset + 1, ethertype & 0xff, 0xff);
1589}
1590
1591/* Set bits in sram sw entry */
1592static void mvpp2_prs_sram_bits_set(struct mvpp2_prs_entry *pe, int bit_num,
1593 int val)
1594{
1595 pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] |= (val << (bit_num % 8));
1596}
1597
1598/* Clear bits in sram sw entry */
1599static void mvpp2_prs_sram_bits_clear(struct mvpp2_prs_entry *pe, int bit_num,
1600 int val)
1601{
1602 pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] &= ~(val << (bit_num % 8));
1603}
1604
1605/* Update ri bits in sram sw entry */
1606static void mvpp2_prs_sram_ri_update(struct mvpp2_prs_entry *pe,
1607 unsigned int bits, unsigned int mask)
1608{
1609 unsigned int i;
1610
1611 for (i = 0; i < MVPP2_PRS_SRAM_RI_CTRL_BITS; i++) {
1612 int ri_off = MVPP2_PRS_SRAM_RI_OFFS;
1613
1614 if (!(mask & BIT(i)))
1615 continue;
1616
1617 if (bits & BIT(i))
1618 mvpp2_prs_sram_bits_set(pe, ri_off + i, 1);
1619 else
1620 mvpp2_prs_sram_bits_clear(pe, ri_off + i, 1);
1621
1622 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_RI_CTRL_OFFS + i, 1);
1623 }
1624}
1625
1626/* Obtain ri bits from sram sw entry */
1627static int mvpp2_prs_sram_ri_get(struct mvpp2_prs_entry *pe)
1628{
1629 return pe->sram.word[MVPP2_PRS_SRAM_RI_WORD];
1630}
1631
1632/* Update ai bits in sram sw entry */
1633static void mvpp2_prs_sram_ai_update(struct mvpp2_prs_entry *pe,
1634 unsigned int bits, unsigned int mask)
1635{
1636 unsigned int i;
1637 int ai_off = MVPP2_PRS_SRAM_AI_OFFS;
1638
1639 for (i = 0; i < MVPP2_PRS_SRAM_AI_CTRL_BITS; i++) {
1640
1641 if (!(mask & BIT(i)))
1642 continue;
1643
1644 if (bits & BIT(i))
1645 mvpp2_prs_sram_bits_set(pe, ai_off + i, 1);
1646 else
1647 mvpp2_prs_sram_bits_clear(pe, ai_off + i, 1);
1648
1649 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_AI_CTRL_OFFS + i, 1);
1650 }
1651}
1652
1653/* Read ai bits from sram sw entry */
1654static int mvpp2_prs_sram_ai_get(struct mvpp2_prs_entry *pe)
1655{
1656 u8 bits;
1657 int ai_off = MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_AI_OFFS);
1658 int ai_en_off = ai_off + 1;
1659 int ai_shift = MVPP2_PRS_SRAM_AI_OFFS % 8;
1660
1661 bits = (pe->sram.byte[ai_off] >> ai_shift) |
1662 (pe->sram.byte[ai_en_off] << (8 - ai_shift));
1663
1664 return bits;
1665}
1666
1667/* In sram sw entry set lookup ID field of the tcam key to be used in the next
1668 * lookup interation
1669 */
1670static void mvpp2_prs_sram_next_lu_set(struct mvpp2_prs_entry *pe,
1671 unsigned int lu)
1672{
1673 int sram_next_off = MVPP2_PRS_SRAM_NEXT_LU_OFFS;
1674
1675 mvpp2_prs_sram_bits_clear(pe, sram_next_off,
1676 MVPP2_PRS_SRAM_NEXT_LU_MASK);
1677 mvpp2_prs_sram_bits_set(pe, sram_next_off, lu);
1678}
1679
1680/* In the sram sw entry set sign and value of the next lookup offset
1681 * and the offset value generated to the classifier
1682 */
1683static void mvpp2_prs_sram_shift_set(struct mvpp2_prs_entry *pe, int shift,
1684 unsigned int op)
1685{
1686 /* Set sign */
1687 if (shift < 0) {
1688 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1689 shift = 0 - shift;
1690 } else {
1691 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1692 }
1693
1694 /* Set value */
1695 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_SHIFT_OFFS)] =
1696 (unsigned char)shift;
1697
1698 /* Reset and set operation */
1699 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS,
1700 MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK);
1701 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS, op);
1702
1703 /* Set base offset as current */
1704 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1705}
1706
1707/* In the sram sw entry set sign and value of the user defined offset
1708 * generated to the classifier
1709 */
1710static void mvpp2_prs_sram_offset_set(struct mvpp2_prs_entry *pe,
1711 unsigned int type, int offset,
1712 unsigned int op)
1713{
1714 /* Set sign */
1715 if (offset < 0) {
1716 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1717 offset = 0 - offset;
1718 } else {
1719 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1720 }
1721
1722 /* Set value */
1723 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_OFFS,
1724 MVPP2_PRS_SRAM_UDF_MASK);
1725 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_OFFS, offset);
1726 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1727 MVPP2_PRS_SRAM_UDF_BITS)] &=
1728 ~(MVPP2_PRS_SRAM_UDF_MASK >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1729 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1730 MVPP2_PRS_SRAM_UDF_BITS)] |=
1731 (offset >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1732
1733 /* Set offset type */
1734 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS,
1735 MVPP2_PRS_SRAM_UDF_TYPE_MASK);
1736 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS, type);
1737
1738 /* Set offset operation */
1739 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS,
1740 MVPP2_PRS_SRAM_OP_SEL_UDF_MASK);
1741 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS, op);
1742
1743 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1744 MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] &=
1745 ~(MVPP2_PRS_SRAM_OP_SEL_UDF_MASK >>
1746 (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1747
1748 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1749 MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] |=
1750 (op >> (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1751
1752 /* Set base offset as current */
1753 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1754}
1755
1756/* Find parser flow entry */
1757static struct mvpp2_prs_entry *mvpp2_prs_flow_find(struct mvpp2 *priv, int flow)
1758{
1759 struct mvpp2_prs_entry *pe;
1760 int tid;
1761
1762 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
1763 if (!pe)
1764 return NULL;
1765 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
1766
1767 /* Go through the all entires with MVPP2_PRS_LU_FLOWS */
1768 for (tid = MVPP2_PRS_TCAM_SRAM_SIZE - 1; tid >= 0; tid--) {
1769 u8 bits;
1770
1771 if (!priv->prs_shadow[tid].valid ||
1772 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_FLOWS)
1773 continue;
1774
1775 pe->index = tid;
1776 mvpp2_prs_hw_read(priv, pe);
1777 bits = mvpp2_prs_sram_ai_get(pe);
1778
1779 /* Sram store classification lookup ID in AI bits [5:0] */
1780 if ((bits & MVPP2_PRS_FLOW_ID_MASK) == flow)
1781 return pe;
1782 }
1783 kfree(pe);
1784
1785 return NULL;
1786}
1787
1788/* Return first free tcam index, seeking from start to end */
1789static int mvpp2_prs_tcam_first_free(struct mvpp2 *priv, unsigned char start,
1790 unsigned char end)
1791{
1792 int tid;
1793
1794 if (start > end)
1795 swap(start, end);
1796
1797 if (end >= MVPP2_PRS_TCAM_SRAM_SIZE)
1798 end = MVPP2_PRS_TCAM_SRAM_SIZE - 1;
1799
1800 for (tid = start; tid <= end; tid++) {
1801 if (!priv->prs_shadow[tid].valid)
1802 return tid;
1803 }
1804
1805 return -EINVAL;
1806}
1807
1808/* Enable/disable dropping all mac da's */
1809static void mvpp2_prs_mac_drop_all_set(struct mvpp2 *priv, int port, bool add)
1810{
1811 struct mvpp2_prs_entry pe;
1812
1813 if (priv->prs_shadow[MVPP2_PE_DROP_ALL].valid) {
1814 /* Entry exist - update port only */
1815 pe.index = MVPP2_PE_DROP_ALL;
1816 mvpp2_prs_hw_read(priv, &pe);
1817 } else {
1818 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001819 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001820 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1821 pe.index = MVPP2_PE_DROP_ALL;
1822
1823 /* Non-promiscuous mode for all ports - DROP unknown packets */
1824 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
1825 MVPP2_PRS_RI_DROP_MASK);
1826
1827 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1828 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1829
1830 /* Update shadow table */
1831 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1832
1833 /* Mask all ports */
1834 mvpp2_prs_tcam_port_map_set(&pe, 0);
1835 }
1836
1837 /* Update port mask */
1838 mvpp2_prs_tcam_port_set(&pe, port, add);
1839
1840 mvpp2_prs_hw_write(priv, &pe);
1841}
1842
1843/* Set port to promiscuous mode */
1844static void mvpp2_prs_mac_promisc_set(struct mvpp2 *priv, int port, bool add)
1845{
1846 struct mvpp2_prs_entry pe;
1847
Joe Perchesdbedd442015-03-06 20:49:12 -08001848 /* Promiscuous mode - Accept unknown packets */
Marcin Wojtas3f518502014-07-10 16:52:13 -03001849
1850 if (priv->prs_shadow[MVPP2_PE_MAC_PROMISCUOUS].valid) {
1851 /* Entry exist - update port only */
1852 pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1853 mvpp2_prs_hw_read(priv, &pe);
1854 } else {
1855 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001856 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001857 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1858 pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1859
1860 /* Continue - set next lookup */
1861 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
1862
1863 /* Set result info bits */
1864 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_UCAST,
1865 MVPP2_PRS_RI_L2_CAST_MASK);
1866
1867 /* Shift to ethertype */
1868 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
1869 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1870
1871 /* Mask all ports */
1872 mvpp2_prs_tcam_port_map_set(&pe, 0);
1873
1874 /* Update shadow table */
1875 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1876 }
1877
1878 /* Update port mask */
1879 mvpp2_prs_tcam_port_set(&pe, port, add);
1880
1881 mvpp2_prs_hw_write(priv, &pe);
1882}
1883
1884/* Accept multicast */
1885static void mvpp2_prs_mac_multi_set(struct mvpp2 *priv, int port, int index,
1886 bool add)
1887{
1888 struct mvpp2_prs_entry pe;
1889 unsigned char da_mc;
1890
1891 /* Ethernet multicast address first byte is
1892 * 0x01 for IPv4 and 0x33 for IPv6
1893 */
1894 da_mc = (index == MVPP2_PE_MAC_MC_ALL) ? 0x01 : 0x33;
1895
1896 if (priv->prs_shadow[index].valid) {
1897 /* Entry exist - update port only */
1898 pe.index = index;
1899 mvpp2_prs_hw_read(priv, &pe);
1900 } else {
1901 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001902 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001903 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1904 pe.index = index;
1905
1906 /* Continue - set next lookup */
1907 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
1908
1909 /* Set result info bits */
1910 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_MCAST,
1911 MVPP2_PRS_RI_L2_CAST_MASK);
1912
1913 /* Update tcam entry data first byte */
1914 mvpp2_prs_tcam_data_byte_set(&pe, 0, da_mc, 0xff);
1915
1916 /* Shift to ethertype */
1917 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
1918 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1919
1920 /* Mask all ports */
1921 mvpp2_prs_tcam_port_map_set(&pe, 0);
1922
1923 /* Update shadow table */
1924 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1925 }
1926
1927 /* Update port mask */
1928 mvpp2_prs_tcam_port_set(&pe, port, add);
1929
1930 mvpp2_prs_hw_write(priv, &pe);
1931}
1932
1933/* Set entry for dsa packets */
1934static void mvpp2_prs_dsa_tag_set(struct mvpp2 *priv, int port, bool add,
1935 bool tagged, bool extend)
1936{
1937 struct mvpp2_prs_entry pe;
1938 int tid, shift;
1939
1940 if (extend) {
1941 tid = tagged ? MVPP2_PE_EDSA_TAGGED : MVPP2_PE_EDSA_UNTAGGED;
1942 shift = 8;
1943 } else {
1944 tid = tagged ? MVPP2_PE_DSA_TAGGED : MVPP2_PE_DSA_UNTAGGED;
1945 shift = 4;
1946 }
1947
1948 if (priv->prs_shadow[tid].valid) {
1949 /* Entry exist - update port only */
1950 pe.index = tid;
1951 mvpp2_prs_hw_read(priv, &pe);
1952 } else {
1953 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001954 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001955 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
1956 pe.index = tid;
1957
1958 /* Shift 4 bytes if DSA tag or 8 bytes in case of EDSA tag*/
1959 mvpp2_prs_sram_shift_set(&pe, shift,
1960 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1961
1962 /* Update shadow table */
1963 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
1964
1965 if (tagged) {
1966 /* Set tagged bit in DSA tag */
1967 mvpp2_prs_tcam_data_byte_set(&pe, 0,
1968 MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
1969 MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
1970 /* Clear all ai bits for next iteration */
1971 mvpp2_prs_sram_ai_update(&pe, 0,
1972 MVPP2_PRS_SRAM_AI_MASK);
1973 /* If packet is tagged continue check vlans */
1974 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
1975 } else {
1976 /* Set result info bits to 'no vlans' */
1977 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
1978 MVPP2_PRS_RI_VLAN_MASK);
1979 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
1980 }
1981
1982 /* Mask all ports */
1983 mvpp2_prs_tcam_port_map_set(&pe, 0);
1984 }
1985
1986 /* Update port mask */
1987 mvpp2_prs_tcam_port_set(&pe, port, add);
1988
1989 mvpp2_prs_hw_write(priv, &pe);
1990}
1991
1992/* Set entry for dsa ethertype */
1993static void mvpp2_prs_dsa_tag_ethertype_set(struct mvpp2 *priv, int port,
1994 bool add, bool tagged, bool extend)
1995{
1996 struct mvpp2_prs_entry pe;
1997 int tid, shift, port_mask;
1998
1999 if (extend) {
2000 tid = tagged ? MVPP2_PE_ETYPE_EDSA_TAGGED :
2001 MVPP2_PE_ETYPE_EDSA_UNTAGGED;
2002 port_mask = 0;
2003 shift = 8;
2004 } else {
2005 tid = tagged ? MVPP2_PE_ETYPE_DSA_TAGGED :
2006 MVPP2_PE_ETYPE_DSA_UNTAGGED;
2007 port_mask = MVPP2_PRS_PORT_MASK;
2008 shift = 4;
2009 }
2010
2011 if (priv->prs_shadow[tid].valid) {
2012 /* Entry exist - update port only */
2013 pe.index = tid;
2014 mvpp2_prs_hw_read(priv, &pe);
2015 } else {
2016 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002017 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002018 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
2019 pe.index = tid;
2020
2021 /* Set ethertype */
2022 mvpp2_prs_match_etype(&pe, 0, ETH_P_EDSA);
2023 mvpp2_prs_match_etype(&pe, 2, 0);
2024
2025 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DSA_MASK,
2026 MVPP2_PRS_RI_DSA_MASK);
2027 /* Shift ethertype + 2 byte reserved + tag*/
2028 mvpp2_prs_sram_shift_set(&pe, 2 + MVPP2_ETH_TYPE_LEN + shift,
2029 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2030
2031 /* Update shadow table */
2032 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
2033
2034 if (tagged) {
2035 /* Set tagged bit in DSA tag */
2036 mvpp2_prs_tcam_data_byte_set(&pe,
2037 MVPP2_ETH_TYPE_LEN + 2 + 3,
2038 MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
2039 MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
2040 /* Clear all ai bits for next iteration */
2041 mvpp2_prs_sram_ai_update(&pe, 0,
2042 MVPP2_PRS_SRAM_AI_MASK);
2043 /* If packet is tagged continue check vlans */
2044 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2045 } else {
2046 /* Set result info bits to 'no vlans' */
2047 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
2048 MVPP2_PRS_RI_VLAN_MASK);
2049 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2050 }
2051 /* Mask/unmask all ports, depending on dsa type */
2052 mvpp2_prs_tcam_port_map_set(&pe, port_mask);
2053 }
2054
2055 /* Update port mask */
2056 mvpp2_prs_tcam_port_set(&pe, port, add);
2057
2058 mvpp2_prs_hw_write(priv, &pe);
2059}
2060
2061/* Search for existing single/triple vlan entry */
2062static struct mvpp2_prs_entry *mvpp2_prs_vlan_find(struct mvpp2 *priv,
2063 unsigned short tpid, int ai)
2064{
2065 struct mvpp2_prs_entry *pe;
2066 int tid;
2067
2068 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2069 if (!pe)
2070 return NULL;
2071 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2072
2073 /* Go through the all entries with MVPP2_PRS_LU_VLAN */
2074 for (tid = MVPP2_PE_FIRST_FREE_TID;
2075 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
2076 unsigned int ri_bits, ai_bits;
2077 bool match;
2078
2079 if (!priv->prs_shadow[tid].valid ||
2080 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
2081 continue;
2082
2083 pe->index = tid;
2084
2085 mvpp2_prs_hw_read(priv, pe);
2086 match = mvpp2_prs_tcam_data_cmp(pe, 0, swab16(tpid));
2087 if (!match)
2088 continue;
2089
2090 /* Get vlan type */
2091 ri_bits = mvpp2_prs_sram_ri_get(pe);
2092 ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
2093
2094 /* Get current ai value from tcam */
2095 ai_bits = mvpp2_prs_tcam_ai_get(pe);
2096 /* Clear double vlan bit */
2097 ai_bits &= ~MVPP2_PRS_DBL_VLAN_AI_BIT;
2098
2099 if (ai != ai_bits)
2100 continue;
2101
2102 if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
2103 ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
2104 return pe;
2105 }
2106 kfree(pe);
2107
2108 return NULL;
2109}
2110
2111/* Add/update single/triple vlan entry */
2112static int mvpp2_prs_vlan_add(struct mvpp2 *priv, unsigned short tpid, int ai,
2113 unsigned int port_map)
2114{
2115 struct mvpp2_prs_entry *pe;
2116 int tid_aux, tid;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302117 int ret = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002118
2119 pe = mvpp2_prs_vlan_find(priv, tpid, ai);
2120
2121 if (!pe) {
2122 /* Create new tcam entry */
2123 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_LAST_FREE_TID,
2124 MVPP2_PE_FIRST_FREE_TID);
2125 if (tid < 0)
2126 return tid;
2127
2128 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2129 if (!pe)
2130 return -ENOMEM;
2131
2132 /* Get last double vlan tid */
2133 for (tid_aux = MVPP2_PE_LAST_FREE_TID;
2134 tid_aux >= MVPP2_PE_FIRST_FREE_TID; tid_aux--) {
2135 unsigned int ri_bits;
2136
2137 if (!priv->prs_shadow[tid_aux].valid ||
2138 priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
2139 continue;
2140
2141 pe->index = tid_aux;
2142 mvpp2_prs_hw_read(priv, pe);
2143 ri_bits = mvpp2_prs_sram_ri_get(pe);
2144 if ((ri_bits & MVPP2_PRS_RI_VLAN_MASK) ==
2145 MVPP2_PRS_RI_VLAN_DOUBLE)
2146 break;
2147 }
2148
Sudip Mukherjee43737472014-11-01 16:59:34 +05302149 if (tid <= tid_aux) {
2150 ret = -EINVAL;
Markus Elfringf9fd0e32017-04-17 13:50:35 +02002151 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302152 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002153
Markus Elfringbd6aaf52017-04-17 10:40:32 +02002154 memset(pe, 0, sizeof(*pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002155 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2156 pe->index = tid;
2157
2158 mvpp2_prs_match_etype(pe, 0, tpid);
2159
2160 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_L2);
2161 /* Shift 4 bytes - skip 1 vlan tag */
2162 mvpp2_prs_sram_shift_set(pe, MVPP2_VLAN_TAG_LEN,
2163 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2164 /* Clear all ai bits for next iteration */
2165 mvpp2_prs_sram_ai_update(pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2166
2167 if (ai == MVPP2_PRS_SINGLE_VLAN_AI) {
2168 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_SINGLE,
2169 MVPP2_PRS_RI_VLAN_MASK);
2170 } else {
2171 ai |= MVPP2_PRS_DBL_VLAN_AI_BIT;
2172 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_TRIPLE,
2173 MVPP2_PRS_RI_VLAN_MASK);
2174 }
2175 mvpp2_prs_tcam_ai_update(pe, ai, MVPP2_PRS_SRAM_AI_MASK);
2176
2177 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_VLAN);
2178 }
2179 /* Update ports' mask */
2180 mvpp2_prs_tcam_port_map_set(pe, port_map);
2181
2182 mvpp2_prs_hw_write(priv, pe);
Markus Elfringf9fd0e32017-04-17 13:50:35 +02002183free_pe:
Marcin Wojtas3f518502014-07-10 16:52:13 -03002184 kfree(pe);
2185
Sudip Mukherjee43737472014-11-01 16:59:34 +05302186 return ret;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002187}
2188
2189/* Get first free double vlan ai number */
2190static int mvpp2_prs_double_vlan_ai_free_get(struct mvpp2 *priv)
2191{
2192 int i;
2193
2194 for (i = 1; i < MVPP2_PRS_DBL_VLANS_MAX; i++) {
2195 if (!priv->prs_double_vlans[i])
2196 return i;
2197 }
2198
2199 return -EINVAL;
2200}
2201
2202/* Search for existing double vlan entry */
2203static struct mvpp2_prs_entry *mvpp2_prs_double_vlan_find(struct mvpp2 *priv,
2204 unsigned short tpid1,
2205 unsigned short tpid2)
2206{
2207 struct mvpp2_prs_entry *pe;
2208 int tid;
2209
2210 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2211 if (!pe)
2212 return NULL;
2213 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2214
2215 /* Go through the all entries with MVPP2_PRS_LU_VLAN */
2216 for (tid = MVPP2_PE_FIRST_FREE_TID;
2217 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
2218 unsigned int ri_mask;
2219 bool match;
2220
2221 if (!priv->prs_shadow[tid].valid ||
2222 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
2223 continue;
2224
2225 pe->index = tid;
2226 mvpp2_prs_hw_read(priv, pe);
2227
2228 match = mvpp2_prs_tcam_data_cmp(pe, 0, swab16(tpid1))
2229 && mvpp2_prs_tcam_data_cmp(pe, 4, swab16(tpid2));
2230
2231 if (!match)
2232 continue;
2233
2234 ri_mask = mvpp2_prs_sram_ri_get(pe) & MVPP2_PRS_RI_VLAN_MASK;
2235 if (ri_mask == MVPP2_PRS_RI_VLAN_DOUBLE)
2236 return pe;
2237 }
2238 kfree(pe);
2239
2240 return NULL;
2241}
2242
2243/* Add or update double vlan entry */
2244static int mvpp2_prs_double_vlan_add(struct mvpp2 *priv, unsigned short tpid1,
2245 unsigned short tpid2,
2246 unsigned int port_map)
2247{
2248 struct mvpp2_prs_entry *pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302249 int tid_aux, tid, ai, ret = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002250
2251 pe = mvpp2_prs_double_vlan_find(priv, tpid1, tpid2);
2252
2253 if (!pe) {
2254 /* Create new tcam entry */
2255 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2256 MVPP2_PE_LAST_FREE_TID);
2257 if (tid < 0)
2258 return tid;
2259
2260 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2261 if (!pe)
2262 return -ENOMEM;
2263
2264 /* Set ai value for new double vlan entry */
2265 ai = mvpp2_prs_double_vlan_ai_free_get(priv);
Sudip Mukherjee43737472014-11-01 16:59:34 +05302266 if (ai < 0) {
2267 ret = ai;
Markus Elfringc9a7e122017-04-17 13:03:49 +02002268 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302269 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002270
2271 /* Get first single/triple vlan tid */
2272 for (tid_aux = MVPP2_PE_FIRST_FREE_TID;
2273 tid_aux <= MVPP2_PE_LAST_FREE_TID; tid_aux++) {
2274 unsigned int ri_bits;
2275
2276 if (!priv->prs_shadow[tid_aux].valid ||
2277 priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
2278 continue;
2279
2280 pe->index = tid_aux;
2281 mvpp2_prs_hw_read(priv, pe);
2282 ri_bits = mvpp2_prs_sram_ri_get(pe);
2283 ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
2284 if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
2285 ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
2286 break;
2287 }
2288
Sudip Mukherjee43737472014-11-01 16:59:34 +05302289 if (tid >= tid_aux) {
2290 ret = -ERANGE;
Markus Elfringc9a7e122017-04-17 13:03:49 +02002291 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302292 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002293
Markus Elfringbd6aaf52017-04-17 10:40:32 +02002294 memset(pe, 0, sizeof(*pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002295 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2296 pe->index = tid;
2297
2298 priv->prs_double_vlans[ai] = true;
2299
2300 mvpp2_prs_match_etype(pe, 0, tpid1);
2301 mvpp2_prs_match_etype(pe, 4, tpid2);
2302
2303 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_VLAN);
2304 /* Shift 8 bytes - skip 2 vlan tags */
2305 mvpp2_prs_sram_shift_set(pe, 2 * MVPP2_VLAN_TAG_LEN,
2306 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2307 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_DOUBLE,
2308 MVPP2_PRS_RI_VLAN_MASK);
2309 mvpp2_prs_sram_ai_update(pe, ai | MVPP2_PRS_DBL_VLAN_AI_BIT,
2310 MVPP2_PRS_SRAM_AI_MASK);
2311
2312 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_VLAN);
2313 }
2314
2315 /* Update ports' mask */
2316 mvpp2_prs_tcam_port_map_set(pe, port_map);
2317 mvpp2_prs_hw_write(priv, pe);
Markus Elfringc9a7e122017-04-17 13:03:49 +02002318free_pe:
Marcin Wojtas3f518502014-07-10 16:52:13 -03002319 kfree(pe);
Sudip Mukherjee43737472014-11-01 16:59:34 +05302320 return ret;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002321}
2322
2323/* IPv4 header parsing for fragmentation and L4 offset */
2324static int mvpp2_prs_ip4_proto(struct mvpp2 *priv, unsigned short proto,
2325 unsigned int ri, unsigned int ri_mask)
2326{
2327 struct mvpp2_prs_entry pe;
2328 int tid;
2329
2330 if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
2331 (proto != IPPROTO_IGMP))
2332 return -EINVAL;
2333
Stefan Chulskiaff3da32017-09-25 14:59:46 +02002334 /* Not fragmented packet */
Marcin Wojtas3f518502014-07-10 16:52:13 -03002335 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2336 MVPP2_PE_LAST_FREE_TID);
2337 if (tid < 0)
2338 return tid;
2339
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002340 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002341 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
2342 pe.index = tid;
2343
2344 /* Set next lu to IPv4 */
2345 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2346 mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2347 /* Set L4 offset */
2348 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
2349 sizeof(struct iphdr) - 4,
2350 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2351 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
2352 MVPP2_PRS_IPV4_DIP_AI_BIT);
Stefan Chulskiaff3da32017-09-25 14:59:46 +02002353 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask | MVPP2_PRS_RI_IP_FRAG_MASK);
2354
2355 mvpp2_prs_tcam_data_byte_set(&pe, 2, 0x00,
2356 MVPP2_PRS_TCAM_PROTO_MASK_L);
2357 mvpp2_prs_tcam_data_byte_set(&pe, 3, 0x00,
2358 MVPP2_PRS_TCAM_PROTO_MASK);
Marcin Wojtas3f518502014-07-10 16:52:13 -03002359
2360 mvpp2_prs_tcam_data_byte_set(&pe, 5, proto, MVPP2_PRS_TCAM_PROTO_MASK);
2361 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
2362 /* Unmask all ports */
2363 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2364
2365 /* Update shadow table and hw entry */
2366 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2367 mvpp2_prs_hw_write(priv, &pe);
2368
Stefan Chulskiaff3da32017-09-25 14:59:46 +02002369 /* Fragmented packet */
Marcin Wojtas3f518502014-07-10 16:52:13 -03002370 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2371 MVPP2_PE_LAST_FREE_TID);
2372 if (tid < 0)
2373 return tid;
2374
2375 pe.index = tid;
2376 /* Clear ri before updating */
2377 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2378 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2379 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
2380
Stefan Chulskiaff3da32017-09-25 14:59:46 +02002381 mvpp2_prs_sram_ri_update(&pe, ri | MVPP2_PRS_RI_IP_FRAG_TRUE,
2382 ri_mask | MVPP2_PRS_RI_IP_FRAG_MASK);
2383
2384 mvpp2_prs_tcam_data_byte_set(&pe, 2, 0x00, 0x0);
2385 mvpp2_prs_tcam_data_byte_set(&pe, 3, 0x00, 0x0);
Marcin Wojtas3f518502014-07-10 16:52:13 -03002386
2387 /* Update shadow table and hw entry */
2388 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2389 mvpp2_prs_hw_write(priv, &pe);
2390
2391 return 0;
2392}
2393
2394/* IPv4 L3 multicast or broadcast */
2395static int mvpp2_prs_ip4_cast(struct mvpp2 *priv, unsigned short l3_cast)
2396{
2397 struct mvpp2_prs_entry pe;
2398 int mask, tid;
2399
2400 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2401 MVPP2_PE_LAST_FREE_TID);
2402 if (tid < 0)
2403 return tid;
2404
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002405 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002406 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
2407 pe.index = tid;
2408
2409 switch (l3_cast) {
2410 case MVPP2_PRS_L3_MULTI_CAST:
2411 mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV4_MC,
2412 MVPP2_PRS_IPV4_MC_MASK);
2413 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
2414 MVPP2_PRS_RI_L3_ADDR_MASK);
2415 break;
2416 case MVPP2_PRS_L3_BROAD_CAST:
2417 mask = MVPP2_PRS_IPV4_BC_MASK;
2418 mvpp2_prs_tcam_data_byte_set(&pe, 0, mask, mask);
2419 mvpp2_prs_tcam_data_byte_set(&pe, 1, mask, mask);
2420 mvpp2_prs_tcam_data_byte_set(&pe, 2, mask, mask);
2421 mvpp2_prs_tcam_data_byte_set(&pe, 3, mask, mask);
2422 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_BCAST,
2423 MVPP2_PRS_RI_L3_ADDR_MASK);
2424 break;
2425 default:
2426 return -EINVAL;
2427 }
2428
2429 /* Finished: go to flowid generation */
2430 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2431 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2432
2433 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
2434 MVPP2_PRS_IPV4_DIP_AI_BIT);
2435 /* Unmask all ports */
2436 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2437
2438 /* Update shadow table and hw entry */
2439 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2440 mvpp2_prs_hw_write(priv, &pe);
2441
2442 return 0;
2443}
2444
2445/* Set entries for protocols over IPv6 */
2446static int mvpp2_prs_ip6_proto(struct mvpp2 *priv, unsigned short proto,
2447 unsigned int ri, unsigned int ri_mask)
2448{
2449 struct mvpp2_prs_entry pe;
2450 int tid;
2451
2452 if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
2453 (proto != IPPROTO_ICMPV6) && (proto != IPPROTO_IPIP))
2454 return -EINVAL;
2455
2456 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2457 MVPP2_PE_LAST_FREE_TID);
2458 if (tid < 0)
2459 return tid;
2460
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002461 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002462 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2463 pe.index = tid;
2464
2465 /* Finished: go to flowid generation */
2466 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2467 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2468 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
2469 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
2470 sizeof(struct ipv6hdr) - 6,
2471 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2472
2473 mvpp2_prs_tcam_data_byte_set(&pe, 0, proto, MVPP2_PRS_TCAM_PROTO_MASK);
2474 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
2475 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2476 /* Unmask all ports */
2477 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2478
2479 /* Write HW */
2480 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
2481 mvpp2_prs_hw_write(priv, &pe);
2482
2483 return 0;
2484}
2485
2486/* IPv6 L3 multicast entry */
2487static int mvpp2_prs_ip6_cast(struct mvpp2 *priv, unsigned short l3_cast)
2488{
2489 struct mvpp2_prs_entry pe;
2490 int tid;
2491
2492 if (l3_cast != MVPP2_PRS_L3_MULTI_CAST)
2493 return -EINVAL;
2494
2495 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2496 MVPP2_PE_LAST_FREE_TID);
2497 if (tid < 0)
2498 return tid;
2499
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002500 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002501 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2502 pe.index = tid;
2503
2504 /* Finished: go to flowid generation */
2505 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2506 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
2507 MVPP2_PRS_RI_L3_ADDR_MASK);
2508 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
2509 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2510 /* Shift back to IPv6 NH */
2511 mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2512
2513 mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV6_MC,
2514 MVPP2_PRS_IPV6_MC_MASK);
2515 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2516 /* Unmask all ports */
2517 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2518
2519 /* Update shadow table and hw entry */
2520 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
2521 mvpp2_prs_hw_write(priv, &pe);
2522
2523 return 0;
2524}
2525
2526/* Parser per-port initialization */
2527static void mvpp2_prs_hw_port_init(struct mvpp2 *priv, int port, int lu_first,
2528 int lu_max, int offset)
2529{
2530 u32 val;
2531
2532 /* Set lookup ID */
2533 val = mvpp2_read(priv, MVPP2_PRS_INIT_LOOKUP_REG);
2534 val &= ~MVPP2_PRS_PORT_LU_MASK(port);
2535 val |= MVPP2_PRS_PORT_LU_VAL(port, lu_first);
2536 mvpp2_write(priv, MVPP2_PRS_INIT_LOOKUP_REG, val);
2537
2538 /* Set maximum number of loops for packet received from port */
2539 val = mvpp2_read(priv, MVPP2_PRS_MAX_LOOP_REG(port));
2540 val &= ~MVPP2_PRS_MAX_LOOP_MASK(port);
2541 val |= MVPP2_PRS_MAX_LOOP_VAL(port, lu_max);
2542 mvpp2_write(priv, MVPP2_PRS_MAX_LOOP_REG(port), val);
2543
2544 /* Set initial offset for packet header extraction for the first
2545 * searching loop
2546 */
2547 val = mvpp2_read(priv, MVPP2_PRS_INIT_OFFS_REG(port));
2548 val &= ~MVPP2_PRS_INIT_OFF_MASK(port);
2549 val |= MVPP2_PRS_INIT_OFF_VAL(port, offset);
2550 mvpp2_write(priv, MVPP2_PRS_INIT_OFFS_REG(port), val);
2551}
2552
2553/* Default flow entries initialization for all ports */
2554static void mvpp2_prs_def_flow_init(struct mvpp2 *priv)
2555{
2556 struct mvpp2_prs_entry pe;
2557 int port;
2558
2559 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002560 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002561 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2562 pe.index = MVPP2_PE_FIRST_DEFAULT_FLOW - port;
2563
2564 /* Mask all ports */
2565 mvpp2_prs_tcam_port_map_set(&pe, 0);
2566
2567 /* Set flow ID*/
2568 mvpp2_prs_sram_ai_update(&pe, port, MVPP2_PRS_FLOW_ID_MASK);
2569 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
2570
2571 /* Update shadow table and hw entry */
2572 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_FLOWS);
2573 mvpp2_prs_hw_write(priv, &pe);
2574 }
2575}
2576
2577/* Set default entry for Marvell Header field */
2578static void mvpp2_prs_mh_init(struct mvpp2 *priv)
2579{
2580 struct mvpp2_prs_entry pe;
2581
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002582 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002583
2584 pe.index = MVPP2_PE_MH_DEFAULT;
2585 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MH);
2586 mvpp2_prs_sram_shift_set(&pe, MVPP2_MH_SIZE,
2587 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2588 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_MAC);
2589
2590 /* Unmask all ports */
2591 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2592
2593 /* Update shadow table and hw entry */
2594 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MH);
2595 mvpp2_prs_hw_write(priv, &pe);
2596}
2597
2598/* Set default entires (place holder) for promiscuous, non-promiscuous and
2599 * multicast MAC addresses
2600 */
2601static void mvpp2_prs_mac_init(struct mvpp2 *priv)
2602{
2603 struct mvpp2_prs_entry pe;
2604
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002605 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002606
2607 /* Non-promiscuous mode for all ports - DROP unknown packets */
2608 pe.index = MVPP2_PE_MAC_NON_PROMISCUOUS;
2609 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
2610
2611 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
2612 MVPP2_PRS_RI_DROP_MASK);
2613 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2614 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2615
2616 /* Unmask all ports */
2617 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2618
2619 /* Update shadow table and hw entry */
2620 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2621 mvpp2_prs_hw_write(priv, &pe);
2622
2623 /* place holders only - no ports */
2624 mvpp2_prs_mac_drop_all_set(priv, 0, false);
2625 mvpp2_prs_mac_promisc_set(priv, 0, false);
Antoine Tenart20746d72017-10-24 11:41:27 +02002626 mvpp2_prs_mac_multi_set(priv, 0, MVPP2_PE_MAC_MC_ALL, false);
2627 mvpp2_prs_mac_multi_set(priv, 0, MVPP2_PE_MAC_MC_IP6, false);
Marcin Wojtas3f518502014-07-10 16:52:13 -03002628}
2629
2630/* Set default entries for various types of dsa packets */
2631static void mvpp2_prs_dsa_init(struct mvpp2 *priv)
2632{
2633 struct mvpp2_prs_entry pe;
2634
2635 /* None tagged EDSA entry - place holder */
2636 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
2637 MVPP2_PRS_EDSA);
2638
2639 /* Tagged EDSA entry - place holder */
2640 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
2641
2642 /* None tagged DSA entry - place holder */
2643 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
2644 MVPP2_PRS_DSA);
2645
2646 /* Tagged DSA entry - place holder */
2647 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
2648
2649 /* None tagged EDSA ethertype entry - place holder*/
2650 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
2651 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
2652
2653 /* Tagged EDSA ethertype entry - place holder*/
2654 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
2655 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
2656
2657 /* None tagged DSA ethertype entry */
2658 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
2659 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
2660
2661 /* Tagged DSA ethertype entry */
2662 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
2663 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
2664
2665 /* Set default entry, in case DSA or EDSA tag not found */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002666 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002667 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
2668 pe.index = MVPP2_PE_DSA_DEFAULT;
2669 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2670
2671 /* Shift 0 bytes */
2672 mvpp2_prs_sram_shift_set(&pe, 0, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2673 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2674
2675 /* Clear all sram ai bits for next iteration */
2676 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2677
2678 /* Unmask all ports */
2679 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2680
2681 mvpp2_prs_hw_write(priv, &pe);
2682}
2683
2684/* Match basic ethertypes */
2685static int mvpp2_prs_etype_init(struct mvpp2 *priv)
2686{
2687 struct mvpp2_prs_entry pe;
2688 int tid;
2689
2690 /* Ethertype: PPPoE */
2691 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2692 MVPP2_PE_LAST_FREE_TID);
2693 if (tid < 0)
2694 return tid;
2695
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002696 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002697 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2698 pe.index = tid;
2699
2700 mvpp2_prs_match_etype(&pe, 0, ETH_P_PPP_SES);
2701
2702 mvpp2_prs_sram_shift_set(&pe, MVPP2_PPPOE_HDR_SIZE,
2703 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2704 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2705 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_PPPOE_MASK,
2706 MVPP2_PRS_RI_PPPOE_MASK);
2707
2708 /* Update shadow table and hw entry */
2709 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2710 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2711 priv->prs_shadow[pe.index].finish = false;
2712 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_PPPOE_MASK,
2713 MVPP2_PRS_RI_PPPOE_MASK);
2714 mvpp2_prs_hw_write(priv, &pe);
2715
2716 /* Ethertype: ARP */
2717 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2718 MVPP2_PE_LAST_FREE_TID);
2719 if (tid < 0)
2720 return tid;
2721
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002722 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002723 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2724 pe.index = tid;
2725
2726 mvpp2_prs_match_etype(&pe, 0, ETH_P_ARP);
2727
2728 /* Generate flow in the next iteration*/
2729 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2730 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2731 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_ARP,
2732 MVPP2_PRS_RI_L3_PROTO_MASK);
2733 /* Set L3 offset */
2734 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2735 MVPP2_ETH_TYPE_LEN,
2736 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2737
2738 /* Update shadow table and hw entry */
2739 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2740 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2741 priv->prs_shadow[pe.index].finish = true;
2742 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_ARP,
2743 MVPP2_PRS_RI_L3_PROTO_MASK);
2744 mvpp2_prs_hw_write(priv, &pe);
2745
2746 /* Ethertype: LBTD */
2747 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2748 MVPP2_PE_LAST_FREE_TID);
2749 if (tid < 0)
2750 return tid;
2751
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002752 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002753 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2754 pe.index = tid;
2755
2756 mvpp2_prs_match_etype(&pe, 0, MVPP2_IP_LBDT_TYPE);
2757
2758 /* Generate flow in the next iteration*/
2759 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2760 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2761 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2762 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2763 MVPP2_PRS_RI_CPU_CODE_MASK |
2764 MVPP2_PRS_RI_UDF3_MASK);
2765 /* Set L3 offset */
2766 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2767 MVPP2_ETH_TYPE_LEN,
2768 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2769
2770 /* Update shadow table and hw entry */
2771 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2772 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2773 priv->prs_shadow[pe.index].finish = true;
2774 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2775 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2776 MVPP2_PRS_RI_CPU_CODE_MASK |
2777 MVPP2_PRS_RI_UDF3_MASK);
2778 mvpp2_prs_hw_write(priv, &pe);
2779
2780 /* Ethertype: IPv4 without options */
2781 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2782 MVPP2_PE_LAST_FREE_TID);
2783 if (tid < 0)
2784 return tid;
2785
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002786 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002787 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2788 pe.index = tid;
2789
2790 mvpp2_prs_match_etype(&pe, 0, ETH_P_IP);
2791 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2792 MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
2793 MVPP2_PRS_IPV4_HEAD_MASK |
2794 MVPP2_PRS_IPV4_IHL_MASK);
2795
2796 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2797 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
2798 MVPP2_PRS_RI_L3_PROTO_MASK);
2799 /* Skip eth_type + 4 bytes of IP header */
2800 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
2801 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2802 /* Set L3 offset */
2803 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2804 MVPP2_ETH_TYPE_LEN,
2805 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2806
2807 /* Update shadow table and hw entry */
2808 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2809 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2810 priv->prs_shadow[pe.index].finish = false;
2811 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4,
2812 MVPP2_PRS_RI_L3_PROTO_MASK);
2813 mvpp2_prs_hw_write(priv, &pe);
2814
2815 /* Ethertype: IPv4 with options */
2816 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2817 MVPP2_PE_LAST_FREE_TID);
2818 if (tid < 0)
2819 return tid;
2820
2821 pe.index = tid;
2822
2823 /* Clear tcam data before updating */
2824 pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(MVPP2_ETH_TYPE_LEN)] = 0x0;
2825 pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(MVPP2_ETH_TYPE_LEN)] = 0x0;
2826
2827 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2828 MVPP2_PRS_IPV4_HEAD,
2829 MVPP2_PRS_IPV4_HEAD_MASK);
2830
2831 /* Clear ri before updating */
2832 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2833 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2834 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
2835 MVPP2_PRS_RI_L3_PROTO_MASK);
2836
2837 /* Update shadow table and hw entry */
2838 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2839 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2840 priv->prs_shadow[pe.index].finish = false;
2841 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4_OPT,
2842 MVPP2_PRS_RI_L3_PROTO_MASK);
2843 mvpp2_prs_hw_write(priv, &pe);
2844
2845 /* Ethertype: IPv6 without options */
2846 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2847 MVPP2_PE_LAST_FREE_TID);
2848 if (tid < 0)
2849 return tid;
2850
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002851 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002852 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2853 pe.index = tid;
2854
2855 mvpp2_prs_match_etype(&pe, 0, ETH_P_IPV6);
2856
2857 /* Skip DIP of IPV6 header */
2858 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 8 +
2859 MVPP2_MAX_L3_ADDR_SIZE,
2860 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2861 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2862 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
2863 MVPP2_PRS_RI_L3_PROTO_MASK);
2864 /* Set L3 offset */
2865 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2866 MVPP2_ETH_TYPE_LEN,
2867 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2868
2869 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2870 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2871 priv->prs_shadow[pe.index].finish = false;
2872 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP6,
2873 MVPP2_PRS_RI_L3_PROTO_MASK);
2874 mvpp2_prs_hw_write(priv, &pe);
2875
2876 /* Default entry for MVPP2_PRS_LU_L2 - Unknown ethtype */
2877 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2878 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2879 pe.index = MVPP2_PE_ETH_TYPE_UN;
2880
2881 /* Unmask all ports */
2882 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2883
2884 /* Generate flow in the next iteration*/
2885 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2886 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2887 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
2888 MVPP2_PRS_RI_L3_PROTO_MASK);
2889 /* Set L3 offset even it's unknown L3 */
2890 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2891 MVPP2_ETH_TYPE_LEN,
2892 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2893
2894 /* Update shadow table and hw entry */
2895 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2896 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2897 priv->prs_shadow[pe.index].finish = true;
2898 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_UN,
2899 MVPP2_PRS_RI_L3_PROTO_MASK);
2900 mvpp2_prs_hw_write(priv, &pe);
2901
2902 return 0;
2903}
2904
2905/* Configure vlan entries and detect up to 2 successive VLAN tags.
2906 * Possible options:
2907 * 0x8100, 0x88A8
2908 * 0x8100, 0x8100
2909 * 0x8100
2910 * 0x88A8
2911 */
2912static int mvpp2_prs_vlan_init(struct platform_device *pdev, struct mvpp2 *priv)
2913{
2914 struct mvpp2_prs_entry pe;
2915 int err;
2916
2917 priv->prs_double_vlans = devm_kcalloc(&pdev->dev, sizeof(bool),
2918 MVPP2_PRS_DBL_VLANS_MAX,
2919 GFP_KERNEL);
2920 if (!priv->prs_double_vlans)
2921 return -ENOMEM;
2922
2923 /* Double VLAN: 0x8100, 0x88A8 */
2924 err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021AD,
2925 MVPP2_PRS_PORT_MASK);
2926 if (err)
2927 return err;
2928
2929 /* Double VLAN: 0x8100, 0x8100 */
2930 err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021Q,
2931 MVPP2_PRS_PORT_MASK);
2932 if (err)
2933 return err;
2934
2935 /* Single VLAN: 0x88a8 */
2936 err = mvpp2_prs_vlan_add(priv, ETH_P_8021AD, MVPP2_PRS_SINGLE_VLAN_AI,
2937 MVPP2_PRS_PORT_MASK);
2938 if (err)
2939 return err;
2940
2941 /* Single VLAN: 0x8100 */
2942 err = mvpp2_prs_vlan_add(priv, ETH_P_8021Q, MVPP2_PRS_SINGLE_VLAN_AI,
2943 MVPP2_PRS_PORT_MASK);
2944 if (err)
2945 return err;
2946
2947 /* Set default double vlan entry */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002948 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002949 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2950 pe.index = MVPP2_PE_VLAN_DBL;
2951
2952 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2953 /* Clear ai for next iterations */
2954 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2955 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_DOUBLE,
2956 MVPP2_PRS_RI_VLAN_MASK);
2957
2958 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_DBL_VLAN_AI_BIT,
2959 MVPP2_PRS_DBL_VLAN_AI_BIT);
2960 /* Unmask all ports */
2961 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2962
2963 /* Update shadow table and hw entry */
2964 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
2965 mvpp2_prs_hw_write(priv, &pe);
2966
2967 /* Set default vlan none entry */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002968 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002969 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2970 pe.index = MVPP2_PE_VLAN_NONE;
2971
2972 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2973 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
2974 MVPP2_PRS_RI_VLAN_MASK);
2975
2976 /* Unmask all ports */
2977 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2978
2979 /* Update shadow table and hw entry */
2980 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
2981 mvpp2_prs_hw_write(priv, &pe);
2982
2983 return 0;
2984}
2985
2986/* Set entries for PPPoE ethertype */
2987static int mvpp2_prs_pppoe_init(struct mvpp2 *priv)
2988{
2989 struct mvpp2_prs_entry pe;
2990 int tid;
2991
2992 /* IPv4 over PPPoE with options */
2993 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2994 MVPP2_PE_LAST_FREE_TID);
2995 if (tid < 0)
2996 return tid;
2997
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002998 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002999 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
3000 pe.index = tid;
3001
3002 mvpp2_prs_match_etype(&pe, 0, PPP_IP);
3003
3004 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
3005 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
3006 MVPP2_PRS_RI_L3_PROTO_MASK);
3007 /* Skip eth_type + 4 bytes of IP header */
3008 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
3009 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3010 /* Set L3 offset */
3011 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
3012 MVPP2_ETH_TYPE_LEN,
3013 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3014
3015 /* Update shadow table and hw entry */
3016 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3017 mvpp2_prs_hw_write(priv, &pe);
3018
3019 /* IPv4 over PPPoE without options */
3020 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3021 MVPP2_PE_LAST_FREE_TID);
3022 if (tid < 0)
3023 return tid;
3024
3025 pe.index = tid;
3026
3027 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
3028 MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
3029 MVPP2_PRS_IPV4_HEAD_MASK |
3030 MVPP2_PRS_IPV4_IHL_MASK);
3031
3032 /* Clear ri before updating */
3033 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
3034 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
3035 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
3036 MVPP2_PRS_RI_L3_PROTO_MASK);
3037
3038 /* Update shadow table and hw entry */
3039 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3040 mvpp2_prs_hw_write(priv, &pe);
3041
3042 /* IPv6 over PPPoE */
3043 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3044 MVPP2_PE_LAST_FREE_TID);
3045 if (tid < 0)
3046 return tid;
3047
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003048 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003049 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
3050 pe.index = tid;
3051
3052 mvpp2_prs_match_etype(&pe, 0, PPP_IPV6);
3053
3054 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
3055 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
3056 MVPP2_PRS_RI_L3_PROTO_MASK);
3057 /* Skip eth_type + 4 bytes of IPv6 header */
3058 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
3059 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3060 /* Set L3 offset */
3061 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
3062 MVPP2_ETH_TYPE_LEN,
3063 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3064
3065 /* Update shadow table and hw entry */
3066 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3067 mvpp2_prs_hw_write(priv, &pe);
3068
3069 /* Non-IP over PPPoE */
3070 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3071 MVPP2_PE_LAST_FREE_TID);
3072 if (tid < 0)
3073 return tid;
3074
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003075 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003076 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
3077 pe.index = tid;
3078
3079 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
3080 MVPP2_PRS_RI_L3_PROTO_MASK);
3081
3082 /* Finished: go to flowid generation */
3083 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3084 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3085 /* Set L3 offset even if it's unknown L3 */
3086 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
3087 MVPP2_ETH_TYPE_LEN,
3088 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3089
3090 /* Update shadow table and hw entry */
3091 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3092 mvpp2_prs_hw_write(priv, &pe);
3093
3094 return 0;
3095}
3096
3097/* Initialize entries for IPv4 */
3098static int mvpp2_prs_ip4_init(struct mvpp2 *priv)
3099{
3100 struct mvpp2_prs_entry pe;
3101 int err;
3102
3103 /* Set entries for TCP, UDP and IGMP over IPv4 */
3104 err = mvpp2_prs_ip4_proto(priv, IPPROTO_TCP, MVPP2_PRS_RI_L4_TCP,
3105 MVPP2_PRS_RI_L4_PROTO_MASK);
3106 if (err)
3107 return err;
3108
3109 err = mvpp2_prs_ip4_proto(priv, IPPROTO_UDP, MVPP2_PRS_RI_L4_UDP,
3110 MVPP2_PRS_RI_L4_PROTO_MASK);
3111 if (err)
3112 return err;
3113
3114 err = mvpp2_prs_ip4_proto(priv, IPPROTO_IGMP,
3115 MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
3116 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
3117 MVPP2_PRS_RI_CPU_CODE_MASK |
3118 MVPP2_PRS_RI_UDF3_MASK);
3119 if (err)
3120 return err;
3121
3122 /* IPv4 Broadcast */
3123 err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_BROAD_CAST);
3124 if (err)
3125 return err;
3126
3127 /* IPv4 Multicast */
3128 err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
3129 if (err)
3130 return err;
3131
3132 /* Default IPv4 entry for unknown protocols */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003133 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003134 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
3135 pe.index = MVPP2_PE_IP4_PROTO_UN;
3136
3137 /* Set next lu to IPv4 */
3138 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
3139 mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3140 /* Set L4 offset */
3141 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
3142 sizeof(struct iphdr) - 4,
3143 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3144 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
3145 MVPP2_PRS_IPV4_DIP_AI_BIT);
3146 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3147 MVPP2_PRS_RI_L4_PROTO_MASK);
3148
3149 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
3150 /* Unmask all ports */
3151 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3152
3153 /* Update shadow table and hw entry */
3154 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3155 mvpp2_prs_hw_write(priv, &pe);
3156
3157 /* Default IPv4 entry for unicast address */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003158 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003159 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
3160 pe.index = MVPP2_PE_IP4_ADDR_UN;
3161
3162 /* Finished: go to flowid generation */
3163 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3164 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3165 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
3166 MVPP2_PRS_RI_L3_ADDR_MASK);
3167
3168 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
3169 MVPP2_PRS_IPV4_DIP_AI_BIT);
3170 /* Unmask all ports */
3171 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3172
3173 /* Update shadow table and hw entry */
3174 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3175 mvpp2_prs_hw_write(priv, &pe);
3176
3177 return 0;
3178}
3179
3180/* Initialize entries for IPv6 */
3181static int mvpp2_prs_ip6_init(struct mvpp2 *priv)
3182{
3183 struct mvpp2_prs_entry pe;
3184 int tid, err;
3185
3186 /* Set entries for TCP, UDP and ICMP over IPv6 */
3187 err = mvpp2_prs_ip6_proto(priv, IPPROTO_TCP,
3188 MVPP2_PRS_RI_L4_TCP,
3189 MVPP2_PRS_RI_L4_PROTO_MASK);
3190 if (err)
3191 return err;
3192
3193 err = mvpp2_prs_ip6_proto(priv, IPPROTO_UDP,
3194 MVPP2_PRS_RI_L4_UDP,
3195 MVPP2_PRS_RI_L4_PROTO_MASK);
3196 if (err)
3197 return err;
3198
3199 err = mvpp2_prs_ip6_proto(priv, IPPROTO_ICMPV6,
3200 MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
3201 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
3202 MVPP2_PRS_RI_CPU_CODE_MASK |
3203 MVPP2_PRS_RI_UDF3_MASK);
3204 if (err)
3205 return err;
3206
3207 /* IPv4 is the last header. This is similar case as 6-TCP or 17-UDP */
3208 /* Result Info: UDF7=1, DS lite */
3209 err = mvpp2_prs_ip6_proto(priv, IPPROTO_IPIP,
3210 MVPP2_PRS_RI_UDF7_IP6_LITE,
3211 MVPP2_PRS_RI_UDF7_MASK);
3212 if (err)
3213 return err;
3214
3215 /* IPv6 multicast */
3216 err = mvpp2_prs_ip6_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
3217 if (err)
3218 return err;
3219
3220 /* Entry for checking hop limit */
3221 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3222 MVPP2_PE_LAST_FREE_TID);
3223 if (tid < 0)
3224 return tid;
3225
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003226 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003227 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3228 pe.index = tid;
3229
3230 /* Finished: go to flowid generation */
3231 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3232 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3233 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN |
3234 MVPP2_PRS_RI_DROP_MASK,
3235 MVPP2_PRS_RI_L3_PROTO_MASK |
3236 MVPP2_PRS_RI_DROP_MASK);
3237
3238 mvpp2_prs_tcam_data_byte_set(&pe, 1, 0x00, MVPP2_PRS_IPV6_HOP_MASK);
3239 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3240 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3241
3242 /* Update shadow table and hw entry */
3243 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3244 mvpp2_prs_hw_write(priv, &pe);
3245
3246 /* Default IPv6 entry for unknown protocols */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003247 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003248 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3249 pe.index = MVPP2_PE_IP6_PROTO_UN;
3250
3251 /* Finished: go to flowid generation */
3252 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3253 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3254 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3255 MVPP2_PRS_RI_L4_PROTO_MASK);
3256 /* Set L4 offset relatively to our current place */
3257 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
3258 sizeof(struct ipv6hdr) - 4,
3259 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3260
3261 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3262 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3263 /* Unmask all ports */
3264 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3265
3266 /* Update shadow table and hw entry */
3267 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3268 mvpp2_prs_hw_write(priv, &pe);
3269
3270 /* Default IPv6 entry for unknown ext protocols */
3271 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3272 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3273 pe.index = MVPP2_PE_IP6_EXT_PROTO_UN;
3274
3275 /* Finished: go to flowid generation */
3276 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3277 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3278 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3279 MVPP2_PRS_RI_L4_PROTO_MASK);
3280
3281 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_EXT_AI_BIT,
3282 MVPP2_PRS_IPV6_EXT_AI_BIT);
3283 /* Unmask all ports */
3284 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3285
3286 /* Update shadow table and hw entry */
3287 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3288 mvpp2_prs_hw_write(priv, &pe);
3289
3290 /* Default IPv6 entry for unicast address */
3291 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3292 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3293 pe.index = MVPP2_PE_IP6_ADDR_UN;
3294
3295 /* Finished: go to IPv6 again */
3296 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
3297 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
3298 MVPP2_PRS_RI_L3_ADDR_MASK);
3299 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3300 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3301 /* Shift back to IPV6 NH */
3302 mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3303
3304 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3305 /* Unmask all ports */
3306 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3307
3308 /* Update shadow table and hw entry */
3309 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
3310 mvpp2_prs_hw_write(priv, &pe);
3311
3312 return 0;
3313}
3314
3315/* Parser default initialization */
3316static int mvpp2_prs_default_init(struct platform_device *pdev,
3317 struct mvpp2 *priv)
3318{
3319 int err, index, i;
3320
3321 /* Enable tcam table */
3322 mvpp2_write(priv, MVPP2_PRS_TCAM_CTRL_REG, MVPP2_PRS_TCAM_EN_MASK);
3323
3324 /* Clear all tcam and sram entries */
3325 for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++) {
3326 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
3327 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
3328 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), 0);
3329
3330 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, index);
3331 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
3332 mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), 0);
3333 }
3334
3335 /* Invalidate all tcam entries */
3336 for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++)
3337 mvpp2_prs_hw_inv(priv, index);
3338
3339 priv->prs_shadow = devm_kcalloc(&pdev->dev, MVPP2_PRS_TCAM_SRAM_SIZE,
Markus Elfring37df25e2017-04-17 09:12:34 +02003340 sizeof(*priv->prs_shadow),
Marcin Wojtas3f518502014-07-10 16:52:13 -03003341 GFP_KERNEL);
3342 if (!priv->prs_shadow)
3343 return -ENOMEM;
3344
3345 /* Always start from lookup = 0 */
3346 for (index = 0; index < MVPP2_MAX_PORTS; index++)
3347 mvpp2_prs_hw_port_init(priv, index, MVPP2_PRS_LU_MH,
3348 MVPP2_PRS_PORT_LU_MAX, 0);
3349
3350 mvpp2_prs_def_flow_init(priv);
3351
3352 mvpp2_prs_mh_init(priv);
3353
3354 mvpp2_prs_mac_init(priv);
3355
3356 mvpp2_prs_dsa_init(priv);
3357
3358 err = mvpp2_prs_etype_init(priv);
3359 if (err)
3360 return err;
3361
3362 err = mvpp2_prs_vlan_init(pdev, priv);
3363 if (err)
3364 return err;
3365
3366 err = mvpp2_prs_pppoe_init(priv);
3367 if (err)
3368 return err;
3369
3370 err = mvpp2_prs_ip6_init(priv);
3371 if (err)
3372 return err;
3373
3374 err = mvpp2_prs_ip4_init(priv);
3375 if (err)
3376 return err;
3377
3378 return 0;
3379}
3380
3381/* Compare MAC DA with tcam entry data */
3382static bool mvpp2_prs_mac_range_equals(struct mvpp2_prs_entry *pe,
3383 const u8 *da, unsigned char *mask)
3384{
3385 unsigned char tcam_byte, tcam_mask;
3386 int index;
3387
3388 for (index = 0; index < ETH_ALEN; index++) {
3389 mvpp2_prs_tcam_data_byte_get(pe, index, &tcam_byte, &tcam_mask);
3390 if (tcam_mask != mask[index])
3391 return false;
3392
3393 if ((tcam_mask & tcam_byte) != (da[index] & mask[index]))
3394 return false;
3395 }
3396
3397 return true;
3398}
3399
3400/* Find tcam entry with matched pair <MAC DA, port> */
3401static struct mvpp2_prs_entry *
3402mvpp2_prs_mac_da_range_find(struct mvpp2 *priv, int pmap, const u8 *da,
3403 unsigned char *mask, int udf_type)
3404{
3405 struct mvpp2_prs_entry *pe;
3406 int tid;
3407
Antoine Tenart239dd4e2017-10-24 11:41:28 +02003408 pe = kzalloc(sizeof(*pe), GFP_ATOMIC);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003409 if (!pe)
3410 return NULL;
3411 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
3412
3413 /* Go through the all entires with MVPP2_PRS_LU_MAC */
3414 for (tid = MVPP2_PE_FIRST_FREE_TID;
3415 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
3416 unsigned int entry_pmap;
3417
3418 if (!priv->prs_shadow[tid].valid ||
3419 (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
3420 (priv->prs_shadow[tid].udf != udf_type))
3421 continue;
3422
3423 pe->index = tid;
3424 mvpp2_prs_hw_read(priv, pe);
3425 entry_pmap = mvpp2_prs_tcam_port_map_get(pe);
3426
3427 if (mvpp2_prs_mac_range_equals(pe, da, mask) &&
3428 entry_pmap == pmap)
3429 return pe;
3430 }
3431 kfree(pe);
3432
3433 return NULL;
3434}
3435
3436/* Update parser's mac da entry */
3437static int mvpp2_prs_mac_da_accept(struct mvpp2 *priv, int port,
3438 const u8 *da, bool add)
3439{
3440 struct mvpp2_prs_entry *pe;
3441 unsigned int pmap, len, ri;
3442 unsigned char mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3443 int tid;
3444
3445 /* Scan TCAM and see if entry with this <MAC DA, port> already exist */
3446 pe = mvpp2_prs_mac_da_range_find(priv, (1 << port), da, mask,
3447 MVPP2_PRS_UDF_MAC_DEF);
3448
3449 /* No such entry */
3450 if (!pe) {
3451 if (!add)
3452 return 0;
3453
3454 /* Create new TCAM entry */
3455 /* Find first range mac entry*/
3456 for (tid = MVPP2_PE_FIRST_FREE_TID;
3457 tid <= MVPP2_PE_LAST_FREE_TID; tid++)
3458 if (priv->prs_shadow[tid].valid &&
3459 (priv->prs_shadow[tid].lu == MVPP2_PRS_LU_MAC) &&
3460 (priv->prs_shadow[tid].udf ==
3461 MVPP2_PRS_UDF_MAC_RANGE))
3462 break;
3463
3464 /* Go through the all entries from first to last */
3465 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3466 tid - 1);
3467 if (tid < 0)
3468 return tid;
3469
Antoine Tenart239dd4e2017-10-24 11:41:28 +02003470 pe = kzalloc(sizeof(*pe), GFP_ATOMIC);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003471 if (!pe)
Amitoj Kaur Chawlac2bb7bc2016-02-04 19:25:26 +05303472 return -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003473 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
3474 pe->index = tid;
3475
3476 /* Mask all ports */
3477 mvpp2_prs_tcam_port_map_set(pe, 0);
3478 }
3479
3480 /* Update port mask */
3481 mvpp2_prs_tcam_port_set(pe, port, add);
3482
3483 /* Invalidate the entry if no ports are left enabled */
3484 pmap = mvpp2_prs_tcam_port_map_get(pe);
3485 if (pmap == 0) {
3486 if (add) {
3487 kfree(pe);
Amitoj Kaur Chawlac2bb7bc2016-02-04 19:25:26 +05303488 return -EINVAL;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003489 }
3490 mvpp2_prs_hw_inv(priv, pe->index);
3491 priv->prs_shadow[pe->index].valid = false;
3492 kfree(pe);
3493 return 0;
3494 }
3495
3496 /* Continue - set next lookup */
3497 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_DSA);
3498
3499 /* Set match on DA */
3500 len = ETH_ALEN;
3501 while (len--)
3502 mvpp2_prs_tcam_data_byte_set(pe, len, da[len], 0xff);
3503
3504 /* Set result info bits */
3505 if (is_broadcast_ether_addr(da))
3506 ri = MVPP2_PRS_RI_L2_BCAST;
3507 else if (is_multicast_ether_addr(da))
3508 ri = MVPP2_PRS_RI_L2_MCAST;
3509 else
3510 ri = MVPP2_PRS_RI_L2_UCAST | MVPP2_PRS_RI_MAC_ME_MASK;
3511
3512 mvpp2_prs_sram_ri_update(pe, ri, MVPP2_PRS_RI_L2_CAST_MASK |
3513 MVPP2_PRS_RI_MAC_ME_MASK);
3514 mvpp2_prs_shadow_ri_set(priv, pe->index, ri, MVPP2_PRS_RI_L2_CAST_MASK |
3515 MVPP2_PRS_RI_MAC_ME_MASK);
3516
3517 /* Shift to ethertype */
3518 mvpp2_prs_sram_shift_set(pe, 2 * ETH_ALEN,
3519 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3520
3521 /* Update shadow table and hw entry */
3522 priv->prs_shadow[pe->index].udf = MVPP2_PRS_UDF_MAC_DEF;
3523 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_MAC);
3524 mvpp2_prs_hw_write(priv, pe);
3525
3526 kfree(pe);
3527
3528 return 0;
3529}
3530
3531static int mvpp2_prs_update_mac_da(struct net_device *dev, const u8 *da)
3532{
3533 struct mvpp2_port *port = netdev_priv(dev);
3534 int err;
3535
3536 /* Remove old parser entry */
3537 err = mvpp2_prs_mac_da_accept(port->priv, port->id, dev->dev_addr,
3538 false);
3539 if (err)
3540 return err;
3541
3542 /* Add new parser entry */
3543 err = mvpp2_prs_mac_da_accept(port->priv, port->id, da, true);
3544 if (err)
3545 return err;
3546
3547 /* Set addr in the device */
3548 ether_addr_copy(dev->dev_addr, da);
3549
3550 return 0;
3551}
3552
3553/* Delete all port's multicast simple (not range) entries */
3554static void mvpp2_prs_mcast_del_all(struct mvpp2 *priv, int port)
3555{
3556 struct mvpp2_prs_entry pe;
3557 int index, tid;
3558
3559 for (tid = MVPP2_PE_FIRST_FREE_TID;
3560 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
3561 unsigned char da[ETH_ALEN], da_mask[ETH_ALEN];
3562
3563 if (!priv->prs_shadow[tid].valid ||
3564 (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
3565 (priv->prs_shadow[tid].udf != MVPP2_PRS_UDF_MAC_DEF))
3566 continue;
3567
3568 /* Only simple mac entries */
3569 pe.index = tid;
3570 mvpp2_prs_hw_read(priv, &pe);
3571
3572 /* Read mac addr from entry */
3573 for (index = 0; index < ETH_ALEN; index++)
3574 mvpp2_prs_tcam_data_byte_get(&pe, index, &da[index],
3575 &da_mask[index]);
3576
3577 if (is_multicast_ether_addr(da) && !is_broadcast_ether_addr(da))
3578 /* Delete this entry */
3579 mvpp2_prs_mac_da_accept(priv, port, da, false);
3580 }
3581}
3582
3583static int mvpp2_prs_tag_mode_set(struct mvpp2 *priv, int port, int type)
3584{
3585 switch (type) {
3586 case MVPP2_TAG_TYPE_EDSA:
3587 /* Add port to EDSA entries */
3588 mvpp2_prs_dsa_tag_set(priv, port, true,
3589 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3590 mvpp2_prs_dsa_tag_set(priv, port, true,
3591 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3592 /* Remove port from DSA entries */
3593 mvpp2_prs_dsa_tag_set(priv, port, false,
3594 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3595 mvpp2_prs_dsa_tag_set(priv, port, false,
3596 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3597 break;
3598
3599 case MVPP2_TAG_TYPE_DSA:
3600 /* Add port to DSA entries */
3601 mvpp2_prs_dsa_tag_set(priv, port, true,
3602 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3603 mvpp2_prs_dsa_tag_set(priv, port, true,
3604 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3605 /* Remove port from EDSA entries */
3606 mvpp2_prs_dsa_tag_set(priv, port, false,
3607 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3608 mvpp2_prs_dsa_tag_set(priv, port, false,
3609 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3610 break;
3611
3612 case MVPP2_TAG_TYPE_MH:
3613 case MVPP2_TAG_TYPE_NONE:
3614 /* Remove port form EDSA and DSA entries */
3615 mvpp2_prs_dsa_tag_set(priv, port, false,
3616 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3617 mvpp2_prs_dsa_tag_set(priv, port, false,
3618 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3619 mvpp2_prs_dsa_tag_set(priv, port, false,
3620 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3621 mvpp2_prs_dsa_tag_set(priv, port, false,
3622 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3623 break;
3624
3625 default:
3626 if ((type < 0) || (type > MVPP2_TAG_TYPE_EDSA))
3627 return -EINVAL;
3628 }
3629
3630 return 0;
3631}
3632
3633/* Set prs flow for the port */
3634static int mvpp2_prs_def_flow(struct mvpp2_port *port)
3635{
3636 struct mvpp2_prs_entry *pe;
3637 int tid;
3638
3639 pe = mvpp2_prs_flow_find(port->priv, port->id);
3640
3641 /* Such entry not exist */
3642 if (!pe) {
3643 /* Go through the all entires from last to first */
3644 tid = mvpp2_prs_tcam_first_free(port->priv,
3645 MVPP2_PE_LAST_FREE_TID,
3646 MVPP2_PE_FIRST_FREE_TID);
3647 if (tid < 0)
3648 return tid;
3649
3650 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
3651 if (!pe)
3652 return -ENOMEM;
3653
3654 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
3655 pe->index = tid;
3656
3657 /* Set flow ID*/
3658 mvpp2_prs_sram_ai_update(pe, port->id, MVPP2_PRS_FLOW_ID_MASK);
3659 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
3660
3661 /* Update shadow table */
3662 mvpp2_prs_shadow_set(port->priv, pe->index, MVPP2_PRS_LU_FLOWS);
3663 }
3664
3665 mvpp2_prs_tcam_port_map_set(pe, (1 << port->id));
3666 mvpp2_prs_hw_write(port->priv, pe);
3667 kfree(pe);
3668
3669 return 0;
3670}
3671
3672/* Classifier configuration routines */
3673
3674/* Update classification flow table registers */
3675static void mvpp2_cls_flow_write(struct mvpp2 *priv,
3676 struct mvpp2_cls_flow_entry *fe)
3677{
3678 mvpp2_write(priv, MVPP2_CLS_FLOW_INDEX_REG, fe->index);
3679 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL0_REG, fe->data[0]);
3680 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL1_REG, fe->data[1]);
3681 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL2_REG, fe->data[2]);
3682}
3683
3684/* Update classification lookup table register */
3685static void mvpp2_cls_lookup_write(struct mvpp2 *priv,
3686 struct mvpp2_cls_lookup_entry *le)
3687{
3688 u32 val;
3689
3690 val = (le->way << MVPP2_CLS_LKP_INDEX_WAY_OFFS) | le->lkpid;
3691 mvpp2_write(priv, MVPP2_CLS_LKP_INDEX_REG, val);
3692 mvpp2_write(priv, MVPP2_CLS_LKP_TBL_REG, le->data);
3693}
3694
3695/* Classifier default initialization */
3696static void mvpp2_cls_init(struct mvpp2 *priv)
3697{
3698 struct mvpp2_cls_lookup_entry le;
3699 struct mvpp2_cls_flow_entry fe;
3700 int index;
3701
3702 /* Enable classifier */
3703 mvpp2_write(priv, MVPP2_CLS_MODE_REG, MVPP2_CLS_MODE_ACTIVE_MASK);
3704
3705 /* Clear classifier flow table */
Arnd Bergmanne8f967c2016-11-24 17:28:12 +01003706 memset(&fe.data, 0, sizeof(fe.data));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003707 for (index = 0; index < MVPP2_CLS_FLOWS_TBL_SIZE; index++) {
3708 fe.index = index;
3709 mvpp2_cls_flow_write(priv, &fe);
3710 }
3711
3712 /* Clear classifier lookup table */
3713 le.data = 0;
3714 for (index = 0; index < MVPP2_CLS_LKP_TBL_SIZE; index++) {
3715 le.lkpid = index;
3716 le.way = 0;
3717 mvpp2_cls_lookup_write(priv, &le);
3718
3719 le.way = 1;
3720 mvpp2_cls_lookup_write(priv, &le);
3721 }
3722}
3723
3724static void mvpp2_cls_port_config(struct mvpp2_port *port)
3725{
3726 struct mvpp2_cls_lookup_entry le;
3727 u32 val;
3728
3729 /* Set way for the port */
3730 val = mvpp2_read(port->priv, MVPP2_CLS_PORT_WAY_REG);
3731 val &= ~MVPP2_CLS_PORT_WAY_MASK(port->id);
3732 mvpp2_write(port->priv, MVPP2_CLS_PORT_WAY_REG, val);
3733
3734 /* Pick the entry to be accessed in lookup ID decoding table
3735 * according to the way and lkpid.
3736 */
3737 le.lkpid = port->id;
3738 le.way = 0;
3739 le.data = 0;
3740
3741 /* Set initial CPU queue for receiving packets */
3742 le.data &= ~MVPP2_CLS_LKP_TBL_RXQ_MASK;
3743 le.data |= port->first_rxq;
3744
3745 /* Disable classification engines */
3746 le.data &= ~MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK;
3747
3748 /* Update lookup ID table entry */
3749 mvpp2_cls_lookup_write(port->priv, &le);
3750}
3751
3752/* Set CPU queue number for oversize packets */
3753static void mvpp2_cls_oversize_rxq_set(struct mvpp2_port *port)
3754{
3755 u32 val;
3756
3757 mvpp2_write(port->priv, MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port->id),
3758 port->first_rxq & MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK);
3759
3760 mvpp2_write(port->priv, MVPP2_CLS_SWFWD_P2HQ_REG(port->id),
3761 (port->first_rxq >> MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS));
3762
3763 val = mvpp2_read(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG);
3764 val |= MVPP2_CLS_SWFWD_PCTRL_MASK(port->id);
3765 mvpp2_write(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG, val);
3766}
3767
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003768static void *mvpp2_frag_alloc(const struct mvpp2_bm_pool *pool)
3769{
3770 if (likely(pool->frag_size <= PAGE_SIZE))
3771 return netdev_alloc_frag(pool->frag_size);
3772 else
3773 return kmalloc(pool->frag_size, GFP_ATOMIC);
3774}
3775
3776static void mvpp2_frag_free(const struct mvpp2_bm_pool *pool, void *data)
3777{
3778 if (likely(pool->frag_size <= PAGE_SIZE))
3779 skb_free_frag(data);
3780 else
3781 kfree(data);
3782}
3783
Marcin Wojtas3f518502014-07-10 16:52:13 -03003784/* Buffer Manager configuration routines */
3785
3786/* Create pool */
3787static int mvpp2_bm_pool_create(struct platform_device *pdev,
3788 struct mvpp2 *priv,
3789 struct mvpp2_bm_pool *bm_pool, int size)
3790{
Marcin Wojtas3f518502014-07-10 16:52:13 -03003791 u32 val;
3792
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003793 /* Number of buffer pointers must be a multiple of 16, as per
3794 * hardware constraints
3795 */
3796 if (!IS_ALIGNED(size, 16))
3797 return -EINVAL;
3798
3799 /* PPv2.1 needs 8 bytes per buffer pointer, PPv2.2 needs 16
3800 * bytes per buffer pointer
3801 */
3802 if (priv->hw_version == MVPP21)
3803 bm_pool->size_bytes = 2 * sizeof(u32) * size;
3804 else
3805 bm_pool->size_bytes = 2 * sizeof(u64) * size;
3806
3807 bm_pool->virt_addr = dma_alloc_coherent(&pdev->dev, bm_pool->size_bytes,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003808 &bm_pool->dma_addr,
Marcin Wojtas3f518502014-07-10 16:52:13 -03003809 GFP_KERNEL);
3810 if (!bm_pool->virt_addr)
3811 return -ENOMEM;
3812
Thomas Petazzonid3158802017-02-21 11:28:13 +01003813 if (!IS_ALIGNED((unsigned long)bm_pool->virt_addr,
3814 MVPP2_BM_POOL_PTR_ALIGN)) {
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003815 dma_free_coherent(&pdev->dev, bm_pool->size_bytes,
3816 bm_pool->virt_addr, bm_pool->dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003817 dev_err(&pdev->dev, "BM pool %d is not %d bytes aligned\n",
3818 bm_pool->id, MVPP2_BM_POOL_PTR_ALIGN);
3819 return -ENOMEM;
3820 }
3821
3822 mvpp2_write(priv, MVPP2_BM_POOL_BASE_REG(bm_pool->id),
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003823 lower_32_bits(bm_pool->dma_addr));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003824 mvpp2_write(priv, MVPP2_BM_POOL_SIZE_REG(bm_pool->id), size);
3825
3826 val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
3827 val |= MVPP2_BM_START_MASK;
3828 mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
3829
3830 bm_pool->type = MVPP2_BM_FREE;
3831 bm_pool->size = size;
3832 bm_pool->pkt_size = 0;
3833 bm_pool->buf_num = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003834
3835 return 0;
3836}
3837
3838/* Set pool buffer size */
3839static void mvpp2_bm_pool_bufsize_set(struct mvpp2 *priv,
3840 struct mvpp2_bm_pool *bm_pool,
3841 int buf_size)
3842{
3843 u32 val;
3844
3845 bm_pool->buf_size = buf_size;
3846
3847 val = ALIGN(buf_size, 1 << MVPP2_POOL_BUF_SIZE_OFFSET);
3848 mvpp2_write(priv, MVPP2_POOL_BUF_SIZE_REG(bm_pool->id), val);
3849}
3850
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003851static void mvpp2_bm_bufs_get_addrs(struct device *dev, struct mvpp2 *priv,
3852 struct mvpp2_bm_pool *bm_pool,
3853 dma_addr_t *dma_addr,
3854 phys_addr_t *phys_addr)
3855{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02003856 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01003857
3858 *dma_addr = mvpp2_percpu_read(priv, cpu,
3859 MVPP2_BM_PHY_ALLOC_REG(bm_pool->id));
3860 *phys_addr = mvpp2_percpu_read(priv, cpu, MVPP2_BM_VIRT_ALLOC_REG);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003861
3862 if (priv->hw_version == MVPP22) {
3863 u32 val;
3864 u32 dma_addr_highbits, phys_addr_highbits;
3865
Thomas Petazzonia7868412017-03-07 16:53:13 +01003866 val = mvpp2_percpu_read(priv, cpu, MVPP22_BM_ADDR_HIGH_ALLOC);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003867 dma_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_PHYS_MASK);
3868 phys_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_VIRT_MASK) >>
3869 MVPP22_BM_ADDR_HIGH_VIRT_SHIFT;
3870
3871 if (sizeof(dma_addr_t) == 8)
3872 *dma_addr |= (u64)dma_addr_highbits << 32;
3873
3874 if (sizeof(phys_addr_t) == 8)
3875 *phys_addr |= (u64)phys_addr_highbits << 32;
3876 }
Thomas Petazzonia704bb52017-06-10 23:18:22 +02003877
3878 put_cpu();
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003879}
3880
Ezequiel Garcia7861f122014-07-21 13:48:14 -03003881/* Free all buffers from the pool */
Marcin Wojtas4229d502015-12-03 15:20:50 +01003882static void mvpp2_bm_bufs_free(struct device *dev, struct mvpp2 *priv,
3883 struct mvpp2_bm_pool *bm_pool)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003884{
3885 int i;
3886
Ezequiel Garcia7861f122014-07-21 13:48:14 -03003887 for (i = 0; i < bm_pool->buf_num; i++) {
Thomas Petazzoni20396132017-03-07 16:53:00 +01003888 dma_addr_t buf_dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003889 phys_addr_t buf_phys_addr;
3890 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003891
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003892 mvpp2_bm_bufs_get_addrs(dev, priv, bm_pool,
3893 &buf_dma_addr, &buf_phys_addr);
Marcin Wojtas4229d502015-12-03 15:20:50 +01003894
Thomas Petazzoni20396132017-03-07 16:53:00 +01003895 dma_unmap_single(dev, buf_dma_addr,
Marcin Wojtas4229d502015-12-03 15:20:50 +01003896 bm_pool->buf_size, DMA_FROM_DEVICE);
3897
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003898 data = (void *)phys_to_virt(buf_phys_addr);
3899 if (!data)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003900 break;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003901
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003902 mvpp2_frag_free(bm_pool, data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003903 }
3904
3905 /* Update BM driver with number of buffers removed from pool */
3906 bm_pool->buf_num -= i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003907}
3908
3909/* Cleanup pool */
3910static int mvpp2_bm_pool_destroy(struct platform_device *pdev,
3911 struct mvpp2 *priv,
3912 struct mvpp2_bm_pool *bm_pool)
3913{
Marcin Wojtas3f518502014-07-10 16:52:13 -03003914 u32 val;
3915
Marcin Wojtas4229d502015-12-03 15:20:50 +01003916 mvpp2_bm_bufs_free(&pdev->dev, priv, bm_pool);
Ezequiel Garciad74c96c2014-07-21 13:48:13 -03003917 if (bm_pool->buf_num) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03003918 WARN(1, "cannot free all buffers in pool %d\n", bm_pool->id);
3919 return 0;
3920 }
3921
3922 val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
3923 val |= MVPP2_BM_STOP_MASK;
3924 mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
3925
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003926 dma_free_coherent(&pdev->dev, bm_pool->size_bytes,
Marcin Wojtas3f518502014-07-10 16:52:13 -03003927 bm_pool->virt_addr,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003928 bm_pool->dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003929 return 0;
3930}
3931
3932static int mvpp2_bm_pools_init(struct platform_device *pdev,
3933 struct mvpp2 *priv)
3934{
3935 int i, err, size;
3936 struct mvpp2_bm_pool *bm_pool;
3937
3938 /* Create all pools with maximum size */
3939 size = MVPP2_BM_POOL_SIZE_MAX;
3940 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
3941 bm_pool = &priv->bm_pools[i];
3942 bm_pool->id = i;
3943 err = mvpp2_bm_pool_create(pdev, priv, bm_pool, size);
3944 if (err)
3945 goto err_unroll_pools;
3946 mvpp2_bm_pool_bufsize_set(priv, bm_pool, 0);
3947 }
3948 return 0;
3949
3950err_unroll_pools:
3951 dev_err(&pdev->dev, "failed to create BM pool %d, size %d\n", i, size);
3952 for (i = i - 1; i >= 0; i--)
3953 mvpp2_bm_pool_destroy(pdev, priv, &priv->bm_pools[i]);
3954 return err;
3955}
3956
3957static int mvpp2_bm_init(struct platform_device *pdev, struct mvpp2 *priv)
3958{
3959 int i, err;
3960
3961 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
3962 /* Mask BM all interrupts */
3963 mvpp2_write(priv, MVPP2_BM_INTR_MASK_REG(i), 0);
3964 /* Clear BM cause register */
3965 mvpp2_write(priv, MVPP2_BM_INTR_CAUSE_REG(i), 0);
3966 }
3967
3968 /* Allocate and initialize BM pools */
3969 priv->bm_pools = devm_kcalloc(&pdev->dev, MVPP2_BM_POOLS_NUM,
Markus Elfring81f915e2017-04-17 09:06:33 +02003970 sizeof(*priv->bm_pools), GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003971 if (!priv->bm_pools)
3972 return -ENOMEM;
3973
3974 err = mvpp2_bm_pools_init(pdev, priv);
3975 if (err < 0)
3976 return err;
3977 return 0;
3978}
3979
3980/* Attach long pool to rxq */
3981static void mvpp2_rxq_long_pool_set(struct mvpp2_port *port,
3982 int lrxq, int long_pool)
3983{
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003984 u32 val, mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003985 int prxq;
3986
3987 /* Get queue physical ID */
3988 prxq = port->rxqs[lrxq]->id;
3989
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003990 if (port->priv->hw_version == MVPP21)
3991 mask = MVPP21_RXQ_POOL_LONG_MASK;
3992 else
3993 mask = MVPP22_RXQ_POOL_LONG_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003994
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003995 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
3996 val &= ~mask;
3997 val |= (long_pool << MVPP2_RXQ_POOL_LONG_OFFS) & mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003998 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
3999}
4000
4001/* Attach short pool to rxq */
4002static void mvpp2_rxq_short_pool_set(struct mvpp2_port *port,
4003 int lrxq, int short_pool)
4004{
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004005 u32 val, mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004006 int prxq;
4007
4008 /* Get queue physical ID */
4009 prxq = port->rxqs[lrxq]->id;
4010
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004011 if (port->priv->hw_version == MVPP21)
4012 mask = MVPP21_RXQ_POOL_SHORT_MASK;
4013 else
4014 mask = MVPP22_RXQ_POOL_SHORT_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004015
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01004016 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
4017 val &= ~mask;
4018 val |= (short_pool << MVPP2_RXQ_POOL_SHORT_OFFS) & mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004019 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
4020}
4021
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004022static void *mvpp2_buf_alloc(struct mvpp2_port *port,
4023 struct mvpp2_bm_pool *bm_pool,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004024 dma_addr_t *buf_dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004025 phys_addr_t *buf_phys_addr,
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004026 gfp_t gfp_mask)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004027{
Thomas Petazzoni20396132017-03-07 16:53:00 +01004028 dma_addr_t dma_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004029 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004030
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004031 data = mvpp2_frag_alloc(bm_pool);
4032 if (!data)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004033 return NULL;
4034
Thomas Petazzoni20396132017-03-07 16:53:00 +01004035 dma_addr = dma_map_single(port->dev->dev.parent, data,
4036 MVPP2_RX_BUF_SIZE(bm_pool->pkt_size),
4037 DMA_FROM_DEVICE);
4038 if (unlikely(dma_mapping_error(port->dev->dev.parent, dma_addr))) {
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004039 mvpp2_frag_free(bm_pool, data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004040 return NULL;
4041 }
Thomas Petazzoni20396132017-03-07 16:53:00 +01004042 *buf_dma_addr = dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004043 *buf_phys_addr = virt_to_phys(data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004044
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004045 return data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004046}
4047
Marcin Wojtas3f518502014-07-10 16:52:13 -03004048/* Release buffer to BM */
4049static inline void mvpp2_bm_pool_put(struct mvpp2_port *port, int pool,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004050 dma_addr_t buf_dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004051 phys_addr_t buf_phys_addr)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004052{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004053 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01004054
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004055 if (port->priv->hw_version == MVPP22) {
4056 u32 val = 0;
4057
4058 if (sizeof(dma_addr_t) == 8)
4059 val |= upper_32_bits(buf_dma_addr) &
4060 MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK;
4061
4062 if (sizeof(phys_addr_t) == 8)
4063 val |= (upper_32_bits(buf_phys_addr)
4064 << MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT) &
4065 MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK;
4066
Thomas Petazzonia7868412017-03-07 16:53:13 +01004067 mvpp2_percpu_write(port->priv, cpu,
4068 MVPP22_BM_ADDR_HIGH_RLS_REG, val);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01004069 }
4070
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004071 /* MVPP2_BM_VIRT_RLS_REG is not interpreted by HW, and simply
4072 * returned in the "cookie" field of the RX
4073 * descriptor. Instead of storing the virtual address, we
4074 * store the physical address
4075 */
Thomas Petazzonia7868412017-03-07 16:53:13 +01004076 mvpp2_percpu_write(port->priv, cpu,
4077 MVPP2_BM_VIRT_RLS_REG, buf_phys_addr);
4078 mvpp2_percpu_write(port->priv, cpu,
4079 MVPP2_BM_PHY_RLS_REG(pool), buf_dma_addr);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004080
4081 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03004082}
4083
Marcin Wojtas3f518502014-07-10 16:52:13 -03004084/* Allocate buffers for the pool */
4085static int mvpp2_bm_bufs_add(struct mvpp2_port *port,
4086 struct mvpp2_bm_pool *bm_pool, int buf_num)
4087{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004088 int i, buf_size, total_size;
Thomas Petazzoni20396132017-03-07 16:53:00 +01004089 dma_addr_t dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004090 phys_addr_t phys_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004091 void *buf;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004092
4093 buf_size = MVPP2_RX_BUF_SIZE(bm_pool->pkt_size);
4094 total_size = MVPP2_RX_TOTAL_SIZE(buf_size);
4095
4096 if (buf_num < 0 ||
4097 (buf_num + bm_pool->buf_num > bm_pool->size)) {
4098 netdev_err(port->dev,
4099 "cannot allocate %d buffers for pool %d\n",
4100 buf_num, bm_pool->id);
4101 return 0;
4102 }
4103
Marcin Wojtas3f518502014-07-10 16:52:13 -03004104 for (i = 0; i < buf_num; i++) {
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004105 buf = mvpp2_buf_alloc(port, bm_pool, &dma_addr,
4106 &phys_addr, GFP_KERNEL);
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004107 if (!buf)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004108 break;
4109
Thomas Petazzoni20396132017-03-07 16:53:00 +01004110 mvpp2_bm_pool_put(port, bm_pool->id, dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004111 phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004112 }
4113
4114 /* Update BM driver with number of buffers added to pool */
4115 bm_pool->buf_num += i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004116
4117 netdev_dbg(port->dev,
4118 "%s pool %d: pkt_size=%4d, buf_size=%4d, total_size=%4d\n",
4119 bm_pool->type == MVPP2_BM_SWF_SHORT ? "short" : " long",
4120 bm_pool->id, bm_pool->pkt_size, buf_size, total_size);
4121
4122 netdev_dbg(port->dev,
4123 "%s pool %d: %d of %d buffers added\n",
4124 bm_pool->type == MVPP2_BM_SWF_SHORT ? "short" : " long",
4125 bm_pool->id, i, buf_num);
4126 return i;
4127}
4128
4129/* Notify the driver that BM pool is being used as specific type and return the
4130 * pool pointer on success
4131 */
4132static struct mvpp2_bm_pool *
4133mvpp2_bm_pool_use(struct mvpp2_port *port, int pool, enum mvpp2_bm_type type,
4134 int pkt_size)
4135{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004136 struct mvpp2_bm_pool *new_pool = &port->priv->bm_pools[pool];
4137 int num;
4138
4139 if (new_pool->type != MVPP2_BM_FREE && new_pool->type != type) {
4140 netdev_err(port->dev, "mixing pool types is forbidden\n");
4141 return NULL;
4142 }
4143
Marcin Wojtas3f518502014-07-10 16:52:13 -03004144 if (new_pool->type == MVPP2_BM_FREE)
4145 new_pool->type = type;
4146
4147 /* Allocate buffers in case BM pool is used as long pool, but packet
4148 * size doesn't match MTU or BM pool hasn't being used yet
4149 */
4150 if (((type == MVPP2_BM_SWF_LONG) && (pkt_size > new_pool->pkt_size)) ||
4151 (new_pool->pkt_size == 0)) {
4152 int pkts_num;
4153
4154 /* Set default buffer number or free all the buffers in case
4155 * the pool is not empty
4156 */
4157 pkts_num = new_pool->buf_num;
4158 if (pkts_num == 0)
4159 pkts_num = type == MVPP2_BM_SWF_LONG ?
4160 MVPP2_BM_LONG_BUF_NUM :
4161 MVPP2_BM_SHORT_BUF_NUM;
4162 else
Marcin Wojtas4229d502015-12-03 15:20:50 +01004163 mvpp2_bm_bufs_free(port->dev->dev.parent,
4164 port->priv, new_pool);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004165
4166 new_pool->pkt_size = pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004167 new_pool->frag_size =
4168 SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
4169 MVPP2_SKB_SHINFO_SIZE;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004170
4171 /* Allocate buffers for this pool */
4172 num = mvpp2_bm_bufs_add(port, new_pool, pkts_num);
4173 if (num != pkts_num) {
4174 WARN(1, "pool %d: %d of %d allocated\n",
4175 new_pool->id, num, pkts_num);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004176 return NULL;
4177 }
4178 }
4179
4180 mvpp2_bm_pool_bufsize_set(port->priv, new_pool,
4181 MVPP2_RX_BUF_SIZE(new_pool->pkt_size));
4182
Marcin Wojtas3f518502014-07-10 16:52:13 -03004183 return new_pool;
4184}
4185
4186/* Initialize pools for swf */
4187static int mvpp2_swf_bm_pool_init(struct mvpp2_port *port)
4188{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004189 int rxq;
4190
4191 if (!port->pool_long) {
4192 port->pool_long =
4193 mvpp2_bm_pool_use(port, MVPP2_BM_SWF_LONG_POOL(port->id),
4194 MVPP2_BM_SWF_LONG,
4195 port->pkt_size);
4196 if (!port->pool_long)
4197 return -ENOMEM;
4198
Marcin Wojtas3f518502014-07-10 16:52:13 -03004199 port->pool_long->port_map |= (1 << port->id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004200
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004201 for (rxq = 0; rxq < port->nrxqs; rxq++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004202 mvpp2_rxq_long_pool_set(port, rxq, port->pool_long->id);
4203 }
4204
4205 if (!port->pool_short) {
4206 port->pool_short =
4207 mvpp2_bm_pool_use(port, MVPP2_BM_SWF_SHORT_POOL,
4208 MVPP2_BM_SWF_SHORT,
4209 MVPP2_BM_SHORT_PKT_SIZE);
4210 if (!port->pool_short)
4211 return -ENOMEM;
4212
Marcin Wojtas3f518502014-07-10 16:52:13 -03004213 port->pool_short->port_map |= (1 << port->id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004214
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004215 for (rxq = 0; rxq < port->nrxqs; rxq++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004216 mvpp2_rxq_short_pool_set(port, rxq,
4217 port->pool_short->id);
4218 }
4219
4220 return 0;
4221}
4222
4223static int mvpp2_bm_update_mtu(struct net_device *dev, int mtu)
4224{
4225 struct mvpp2_port *port = netdev_priv(dev);
4226 struct mvpp2_bm_pool *port_pool = port->pool_long;
4227 int num, pkts_num = port_pool->buf_num;
4228 int pkt_size = MVPP2_RX_PKT_SIZE(mtu);
4229
4230 /* Update BM pool with new buffer size */
Marcin Wojtas4229d502015-12-03 15:20:50 +01004231 mvpp2_bm_bufs_free(dev->dev.parent, port->priv, port_pool);
Ezequiel Garciad74c96c2014-07-21 13:48:13 -03004232 if (port_pool->buf_num) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004233 WARN(1, "cannot free all buffers in pool %d\n", port_pool->id);
4234 return -EIO;
4235 }
4236
4237 port_pool->pkt_size = pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004238 port_pool->frag_size = SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
4239 MVPP2_SKB_SHINFO_SIZE;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004240 num = mvpp2_bm_bufs_add(port, port_pool, pkts_num);
4241 if (num != pkts_num) {
4242 WARN(1, "pool %d: %d of %d allocated\n",
4243 port_pool->id, num, pkts_num);
4244 return -EIO;
4245 }
4246
4247 mvpp2_bm_pool_bufsize_set(port->priv, port_pool,
4248 MVPP2_RX_BUF_SIZE(port_pool->pkt_size));
4249 dev->mtu = mtu;
4250 netdev_update_features(dev);
4251 return 0;
4252}
4253
4254static inline void mvpp2_interrupts_enable(struct mvpp2_port *port)
4255{
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004256 int i, sw_thread_mask = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004257
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004258 for (i = 0; i < port->nqvecs; i++)
4259 sw_thread_mask |= port->qvecs[i].sw_thread_mask;
4260
Marcin Wojtas3f518502014-07-10 16:52:13 -03004261 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004262 MVPP2_ISR_ENABLE_INTERRUPT(sw_thread_mask));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004263}
4264
4265static inline void mvpp2_interrupts_disable(struct mvpp2_port *port)
4266{
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004267 int i, sw_thread_mask = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004268
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004269 for (i = 0; i < port->nqvecs; i++)
4270 sw_thread_mask |= port->qvecs[i].sw_thread_mask;
4271
Marcin Wojtas3f518502014-07-10 16:52:13 -03004272 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02004273 MVPP2_ISR_DISABLE_INTERRUPT(sw_thread_mask));
4274}
4275
4276static inline void mvpp2_qvec_interrupt_enable(struct mvpp2_queue_vector *qvec)
4277{
4278 struct mvpp2_port *port = qvec->port;
4279
4280 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
4281 MVPP2_ISR_ENABLE_INTERRUPT(qvec->sw_thread_mask));
4282}
4283
4284static inline void mvpp2_qvec_interrupt_disable(struct mvpp2_queue_vector *qvec)
4285{
4286 struct mvpp2_port *port = qvec->port;
4287
4288 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
4289 MVPP2_ISR_DISABLE_INTERRUPT(qvec->sw_thread_mask));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004290}
4291
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004292/* Mask the current CPU's Rx/Tx interrupts
4293 * Called by on_each_cpu(), guaranteed to run with migration disabled,
4294 * using smp_processor_id() is OK.
4295 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004296static void mvpp2_interrupts_mask(void *arg)
4297{
4298 struct mvpp2_port *port = arg;
4299
Thomas Petazzonia7868412017-03-07 16:53:13 +01004300 mvpp2_percpu_write(port->priv, smp_processor_id(),
4301 MVPP2_ISR_RX_TX_MASK_REG(port->id), 0);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004302}
4303
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004304/* Unmask the current CPU's Rx/Tx interrupts.
4305 * Called by on_each_cpu(), guaranteed to run with migration disabled,
4306 * using smp_processor_id() is OK.
4307 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004308static void mvpp2_interrupts_unmask(void *arg)
4309{
4310 struct mvpp2_port *port = arg;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02004311 u32 val;
4312
4313 val = MVPP2_CAUSE_MISC_SUM_MASK |
4314 MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
4315 if (port->has_tx_irqs)
4316 val |= MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004317
Thomas Petazzonia7868412017-03-07 16:53:13 +01004318 mvpp2_percpu_write(port->priv, smp_processor_id(),
Thomas Petazzoni213f4282017-08-03 10:42:00 +02004319 MVPP2_ISR_RX_TX_MASK_REG(port->id), val);
4320}
4321
4322static void
4323mvpp2_shared_interrupt_mask_unmask(struct mvpp2_port *port, bool mask)
4324{
4325 u32 val;
4326 int i;
4327
4328 if (port->priv->hw_version != MVPP22)
4329 return;
4330
4331 if (mask)
4332 val = 0;
4333 else
4334 val = MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
4335
4336 for (i = 0; i < port->nqvecs; i++) {
4337 struct mvpp2_queue_vector *v = port->qvecs + i;
4338
4339 if (v->type != MVPP2_QUEUE_VECTOR_SHARED)
4340 continue;
4341
4342 mvpp2_percpu_write(port->priv, v->sw_thread_id,
4343 MVPP2_ISR_RX_TX_MASK_REG(port->id), val);
4344 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004345}
4346
4347/* Port configuration routines */
4348
Antoine Ténartf84bf382017-08-22 19:08:27 +02004349static void mvpp22_gop_init_rgmii(struct mvpp2_port *port)
4350{
4351 struct mvpp2 *priv = port->priv;
4352 u32 val;
4353
4354 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
4355 val |= GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT;
4356 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
4357
4358 regmap_read(priv->sysctrl_base, GENCONF_CTRL0, &val);
4359 if (port->gop_id == 2)
4360 val |= GENCONF_CTRL0_PORT0_RGMII | GENCONF_CTRL0_PORT1_RGMII;
4361 else if (port->gop_id == 3)
4362 val |= GENCONF_CTRL0_PORT1_RGMII_MII;
4363 regmap_write(priv->sysctrl_base, GENCONF_CTRL0, val);
4364}
4365
4366static void mvpp22_gop_init_sgmii(struct mvpp2_port *port)
4367{
4368 struct mvpp2 *priv = port->priv;
4369 u32 val;
4370
4371 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
4372 val |= GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT |
4373 GENCONF_PORT_CTRL0_RX_DATA_SAMPLE;
4374 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
4375
4376 if (port->gop_id > 1) {
4377 regmap_read(priv->sysctrl_base, GENCONF_CTRL0, &val);
4378 if (port->gop_id == 2)
4379 val &= ~GENCONF_CTRL0_PORT0_RGMII;
4380 else if (port->gop_id == 3)
4381 val &= ~GENCONF_CTRL0_PORT1_RGMII_MII;
4382 regmap_write(priv->sysctrl_base, GENCONF_CTRL0, val);
4383 }
4384}
4385
4386static void mvpp22_gop_init_10gkr(struct mvpp2_port *port)
4387{
4388 struct mvpp2 *priv = port->priv;
4389 void __iomem *mpcs = priv->iface_base + MVPP22_MPCS_BASE(port->gop_id);
4390 void __iomem *xpcs = priv->iface_base + MVPP22_XPCS_BASE(port->gop_id);
4391 u32 val;
4392
4393 /* XPCS */
4394 val = readl(xpcs + MVPP22_XPCS_CFG0);
4395 val &= ~(MVPP22_XPCS_CFG0_PCS_MODE(0x3) |
4396 MVPP22_XPCS_CFG0_ACTIVE_LANE(0x3));
4397 val |= MVPP22_XPCS_CFG0_ACTIVE_LANE(2);
4398 writel(val, xpcs + MVPP22_XPCS_CFG0);
4399
4400 /* MPCS */
4401 val = readl(mpcs + MVPP22_MPCS_CTRL);
4402 val &= ~MVPP22_MPCS_CTRL_FWD_ERR_CONN;
4403 writel(val, mpcs + MVPP22_MPCS_CTRL);
4404
4405 val = readl(mpcs + MVPP22_MPCS_CLK_RESET);
4406 val &= ~(MVPP22_MPCS_CLK_RESET_DIV_RATIO(0x7) | MAC_CLK_RESET_MAC |
4407 MAC_CLK_RESET_SD_RX | MAC_CLK_RESET_SD_TX);
4408 val |= MVPP22_MPCS_CLK_RESET_DIV_RATIO(1);
4409 writel(val, mpcs + MVPP22_MPCS_CLK_RESET);
4410
4411 val &= ~MVPP22_MPCS_CLK_RESET_DIV_SET;
4412 val |= MAC_CLK_RESET_MAC | MAC_CLK_RESET_SD_RX | MAC_CLK_RESET_SD_TX;
4413 writel(val, mpcs + MVPP22_MPCS_CLK_RESET);
4414}
4415
4416static int mvpp22_gop_init(struct mvpp2_port *port)
4417{
4418 struct mvpp2 *priv = port->priv;
4419 u32 val;
4420
4421 if (!priv->sysctrl_base)
4422 return 0;
4423
4424 switch (port->phy_interface) {
4425 case PHY_INTERFACE_MODE_RGMII:
4426 case PHY_INTERFACE_MODE_RGMII_ID:
4427 case PHY_INTERFACE_MODE_RGMII_RXID:
4428 case PHY_INTERFACE_MODE_RGMII_TXID:
4429 if (port->gop_id == 0)
4430 goto invalid_conf;
4431 mvpp22_gop_init_rgmii(port);
4432 break;
4433 case PHY_INTERFACE_MODE_SGMII:
4434 mvpp22_gop_init_sgmii(port);
4435 break;
4436 case PHY_INTERFACE_MODE_10GKR:
4437 if (port->gop_id != 0)
4438 goto invalid_conf;
4439 mvpp22_gop_init_10gkr(port);
4440 break;
4441 default:
4442 goto unsupported_conf;
4443 }
4444
4445 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL1, &val);
4446 val |= GENCONF_PORT_CTRL1_RESET(port->gop_id) |
4447 GENCONF_PORT_CTRL1_EN(port->gop_id);
4448 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL1, val);
4449
4450 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
4451 val |= GENCONF_PORT_CTRL0_CLK_DIV_PHASE_CLR;
4452 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
4453
4454 regmap_read(priv->sysctrl_base, GENCONF_SOFT_RESET1, &val);
4455 val |= GENCONF_SOFT_RESET1_GOP;
4456 regmap_write(priv->sysctrl_base, GENCONF_SOFT_RESET1, val);
4457
4458unsupported_conf:
4459 return 0;
4460
4461invalid_conf:
4462 netdev_err(port->dev, "Invalid port configuration\n");
4463 return -EINVAL;
4464}
4465
Antoine Tenartfd3651b2017-09-01 11:04:54 +02004466static void mvpp22_gop_unmask_irq(struct mvpp2_port *port)
4467{
4468 u32 val;
4469
4470 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
4471 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4472 /* Enable the GMAC link status irq for this port */
4473 val = readl(port->base + MVPP22_GMAC_INT_SUM_MASK);
4474 val |= MVPP22_GMAC_INT_SUM_MASK_LINK_STAT;
4475 writel(val, port->base + MVPP22_GMAC_INT_SUM_MASK);
4476 }
4477
4478 if (port->gop_id == 0) {
4479 /* Enable the XLG/GIG irqs for this port */
4480 val = readl(port->base + MVPP22_XLG_EXT_INT_MASK);
4481 if (port->phy_interface == PHY_INTERFACE_MODE_10GKR)
4482 val |= MVPP22_XLG_EXT_INT_MASK_XLG;
4483 else
4484 val |= MVPP22_XLG_EXT_INT_MASK_GIG;
4485 writel(val, port->base + MVPP22_XLG_EXT_INT_MASK);
4486 }
4487}
4488
4489static void mvpp22_gop_mask_irq(struct mvpp2_port *port)
4490{
4491 u32 val;
4492
4493 if (port->gop_id == 0) {
4494 val = readl(port->base + MVPP22_XLG_EXT_INT_MASK);
4495 val &= ~(MVPP22_XLG_EXT_INT_MASK_XLG |
4496 MVPP22_XLG_EXT_INT_MASK_GIG);
4497 writel(val, port->base + MVPP22_XLG_EXT_INT_MASK);
4498 }
4499
4500 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
4501 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4502 val = readl(port->base + MVPP22_GMAC_INT_SUM_MASK);
4503 val &= ~MVPP22_GMAC_INT_SUM_MASK_LINK_STAT;
4504 writel(val, port->base + MVPP22_GMAC_INT_SUM_MASK);
4505 }
4506}
4507
4508static void mvpp22_gop_setup_irq(struct mvpp2_port *port)
4509{
4510 u32 val;
4511
4512 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
4513 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4514 val = readl(port->base + MVPP22_GMAC_INT_MASK);
4515 val |= MVPP22_GMAC_INT_MASK_LINK_STAT;
4516 writel(val, port->base + MVPP22_GMAC_INT_MASK);
4517 }
4518
4519 if (port->gop_id == 0) {
4520 val = readl(port->base + MVPP22_XLG_INT_MASK);
4521 val |= MVPP22_XLG_INT_MASK_LINK;
4522 writel(val, port->base + MVPP22_XLG_INT_MASK);
4523 }
4524
4525 mvpp22_gop_unmask_irq(port);
4526}
4527
Antoine Tenart542897d2017-08-30 10:29:15 +02004528static int mvpp22_comphy_init(struct mvpp2_port *port)
4529{
4530 enum phy_mode mode;
4531 int ret;
4532
4533 if (!port->comphy)
4534 return 0;
4535
4536 switch (port->phy_interface) {
4537 case PHY_INTERFACE_MODE_SGMII:
4538 mode = PHY_MODE_SGMII;
4539 break;
4540 case PHY_INTERFACE_MODE_10GKR:
4541 mode = PHY_MODE_10GKR;
4542 break;
4543 default:
4544 return -EINVAL;
4545 }
4546
4547 ret = phy_set_mode(port->comphy, mode);
4548 if (ret)
4549 return ret;
4550
4551 return phy_power_on(port->comphy);
4552}
4553
Antoine Ténart39193572017-08-22 19:08:24 +02004554static void mvpp2_port_mii_gmac_configure_mode(struct mvpp2_port *port)
4555{
4556 u32 val;
4557
4558 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4559 val = readl(port->base + MVPP22_GMAC_CTRL_4_REG);
4560 val |= MVPP22_CTRL4_SYNC_BYPASS_DIS | MVPP22_CTRL4_DP_CLK_SEL |
4561 MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
4562 val &= ~MVPP22_CTRL4_EXT_PIN_GMII_SEL;
4563 writel(val, port->base + MVPP22_GMAC_CTRL_4_REG);
4564
4565 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4566 val |= MVPP2_GMAC_DISABLE_PADDING;
4567 val &= ~MVPP2_GMAC_FLOW_CTRL_MASK;
4568 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
Antoine Tenart1df22702017-09-01 11:04:52 +02004569 } else if (phy_interface_mode_is_rgmii(port->phy_interface)) {
Antoine Ténart39193572017-08-22 19:08:24 +02004570 val = readl(port->base + MVPP22_GMAC_CTRL_4_REG);
4571 val |= MVPP22_CTRL4_EXT_PIN_GMII_SEL |
4572 MVPP22_CTRL4_SYNC_BYPASS_DIS |
4573 MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
4574 val &= ~MVPP22_CTRL4_DP_CLK_SEL;
4575 writel(val, port->base + MVPP22_GMAC_CTRL_4_REG);
4576
4577 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4578 val &= ~MVPP2_GMAC_DISABLE_PADDING;
4579 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4580 }
4581
4582 /* The port is connected to a copper PHY */
4583 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4584 val &= ~MVPP2_GMAC_PORT_TYPE_MASK;
4585 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4586
4587 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4588 val |= MVPP2_GMAC_IN_BAND_AUTONEG_BYPASS |
4589 MVPP2_GMAC_AN_SPEED_EN | MVPP2_GMAC_FLOW_CTRL_AUTONEG |
4590 MVPP2_GMAC_AN_DUPLEX_EN;
4591 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
4592 val |= MVPP2_GMAC_IN_BAND_AUTONEG;
4593 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4594}
4595
4596static void mvpp2_port_mii_gmac_configure(struct mvpp2_port *port)
4597{
4598 u32 val;
4599
4600 /* Force link down */
4601 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4602 val &= ~MVPP2_GMAC_FORCE_LINK_PASS;
4603 val |= MVPP2_GMAC_FORCE_LINK_DOWN;
4604 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4605
4606 /* Set the GMAC in a reset state */
4607 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4608 val |= MVPP2_GMAC_PORT_RESET_MASK;
4609 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4610
4611 /* Configure the PCS and in-band AN */
4612 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4613 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4614 val |= MVPP2_GMAC_INBAND_AN_MASK | MVPP2_GMAC_PCS_ENABLE_MASK;
Antoine Tenart1df22702017-09-01 11:04:52 +02004615 } else if (phy_interface_mode_is_rgmii(port->phy_interface)) {
Antoine Ténart39193572017-08-22 19:08:24 +02004616 val &= ~MVPP2_GMAC_PCS_ENABLE_MASK;
Antoine Ténart39193572017-08-22 19:08:24 +02004617 }
4618 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4619
4620 mvpp2_port_mii_gmac_configure_mode(port);
4621
4622 /* Unset the GMAC reset state */
4623 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4624 val &= ~MVPP2_GMAC_PORT_RESET_MASK;
4625 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4626
4627 /* Stop forcing link down */
4628 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4629 val &= ~MVPP2_GMAC_FORCE_LINK_DOWN;
4630 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4631}
4632
Antoine Ténart77321952017-08-22 19:08:25 +02004633static void mvpp2_port_mii_xlg_configure(struct mvpp2_port *port)
4634{
4635 u32 val;
4636
4637 if (port->gop_id != 0)
4638 return;
4639
4640 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
4641 val |= MVPP22_XLG_CTRL0_RX_FLOW_CTRL_EN;
4642 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
4643
4644 val = readl(port->base + MVPP22_XLG_CTRL4_REG);
4645 val &= ~MVPP22_XLG_CTRL4_MACMODSELECT_GMAC;
4646 val |= MVPP22_XLG_CTRL4_FWD_FC | MVPP22_XLG_CTRL4_FWD_PFC;
4647 writel(val, port->base + MVPP22_XLG_CTRL4_REG);
4648}
4649
Thomas Petazzoni26975822017-03-07 16:53:14 +01004650static void mvpp22_port_mii_set(struct mvpp2_port *port)
4651{
4652 u32 val;
4653
Thomas Petazzoni26975822017-03-07 16:53:14 +01004654 /* Only GOP port 0 has an XLG MAC */
4655 if (port->gop_id == 0) {
4656 val = readl(port->base + MVPP22_XLG_CTRL3_REG);
4657 val &= ~MVPP22_XLG_CTRL3_MACMODESELECT_MASK;
Antoine Ténart725757a2017-06-12 16:01:39 +02004658
4659 if (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
4660 port->phy_interface == PHY_INTERFACE_MODE_10GKR)
4661 val |= MVPP22_XLG_CTRL3_MACMODESELECT_10G;
4662 else
4663 val |= MVPP22_XLG_CTRL3_MACMODESELECT_GMAC;
4664
Thomas Petazzoni26975822017-03-07 16:53:14 +01004665 writel(val, port->base + MVPP22_XLG_CTRL3_REG);
4666 }
Thomas Petazzoni26975822017-03-07 16:53:14 +01004667}
4668
Marcin Wojtas3f518502014-07-10 16:52:13 -03004669static void mvpp2_port_mii_set(struct mvpp2_port *port)
4670{
Thomas Petazzoni26975822017-03-07 16:53:14 +01004671 if (port->priv->hw_version == MVPP22)
4672 mvpp22_port_mii_set(port);
4673
Antoine Tenart1df22702017-09-01 11:04:52 +02004674 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
Antoine Ténart39193572017-08-22 19:08:24 +02004675 port->phy_interface == PHY_INTERFACE_MODE_SGMII)
4676 mvpp2_port_mii_gmac_configure(port);
Antoine Ténart77321952017-08-22 19:08:25 +02004677 else if (port->phy_interface == PHY_INTERFACE_MODE_10GKR)
4678 mvpp2_port_mii_xlg_configure(port);
Marcin Wojtas08a23752014-07-21 13:48:12 -03004679}
4680
4681static void mvpp2_port_fc_adv_enable(struct mvpp2_port *port)
4682{
4683 u32 val;
4684
4685 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4686 val |= MVPP2_GMAC_FC_ADV_EN;
4687 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004688}
4689
4690static void mvpp2_port_enable(struct mvpp2_port *port)
4691{
4692 u32 val;
4693
Antoine Ténart725757a2017-06-12 16:01:39 +02004694 /* Only GOP port 0 has an XLG MAC */
4695 if (port->gop_id == 0 &&
4696 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
4697 port->phy_interface == PHY_INTERFACE_MODE_10GKR)) {
4698 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
4699 val |= MVPP22_XLG_CTRL0_PORT_EN |
4700 MVPP22_XLG_CTRL0_MAC_RESET_DIS;
4701 val &= ~MVPP22_XLG_CTRL0_MIB_CNT_DIS;
4702 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
4703 } else {
4704 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4705 val |= MVPP2_GMAC_PORT_EN_MASK;
4706 val |= MVPP2_GMAC_MIB_CNTR_EN_MASK;
4707 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4708 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004709}
4710
4711static void mvpp2_port_disable(struct mvpp2_port *port)
4712{
4713 u32 val;
4714
Antoine Ténart725757a2017-06-12 16:01:39 +02004715 /* Only GOP port 0 has an XLG MAC */
4716 if (port->gop_id == 0 &&
4717 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
4718 port->phy_interface == PHY_INTERFACE_MODE_10GKR)) {
4719 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
4720 val &= ~(MVPP22_XLG_CTRL0_PORT_EN |
4721 MVPP22_XLG_CTRL0_MAC_RESET_DIS);
4722 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
4723 } else {
4724 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4725 val &= ~(MVPP2_GMAC_PORT_EN_MASK);
4726 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4727 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004728}
4729
4730/* Set IEEE 802.3x Flow Control Xon Packet Transmission Mode */
4731static void mvpp2_port_periodic_xon_disable(struct mvpp2_port *port)
4732{
4733 u32 val;
4734
4735 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG) &
4736 ~MVPP2_GMAC_PERIODIC_XON_EN_MASK;
4737 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
4738}
4739
4740/* Configure loopback port */
4741static void mvpp2_port_loopback_set(struct mvpp2_port *port)
4742{
4743 u32 val;
4744
4745 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
4746
4747 if (port->speed == 1000)
4748 val |= MVPP2_GMAC_GMII_LB_EN_MASK;
4749 else
4750 val &= ~MVPP2_GMAC_GMII_LB_EN_MASK;
4751
4752 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
4753 val |= MVPP2_GMAC_PCS_LB_EN_MASK;
4754 else
4755 val &= ~MVPP2_GMAC_PCS_LB_EN_MASK;
4756
4757 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
4758}
4759
4760static void mvpp2_port_reset(struct mvpp2_port *port)
4761{
4762 u32 val;
4763
4764 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
4765 ~MVPP2_GMAC_PORT_RESET_MASK;
4766 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4767
4768 while (readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
4769 MVPP2_GMAC_PORT_RESET_MASK)
4770 continue;
4771}
4772
4773/* Change maximum receive size of the port */
4774static inline void mvpp2_gmac_max_rx_size_set(struct mvpp2_port *port)
4775{
4776 u32 val;
4777
4778 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4779 val &= ~MVPP2_GMAC_MAX_RX_SIZE_MASK;
4780 val |= (((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
4781 MVPP2_GMAC_MAX_RX_SIZE_OFFS);
4782 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4783}
4784
Stefan Chulski76eb1b12017-08-22 19:08:26 +02004785/* Change maximum receive size of the port */
4786static inline void mvpp2_xlg_max_rx_size_set(struct mvpp2_port *port)
4787{
4788 u32 val;
4789
4790 val = readl(port->base + MVPP22_XLG_CTRL1_REG);
4791 val &= ~MVPP22_XLG_CTRL1_FRAMESIZELIMIT_MASK;
4792 val |= ((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
Antoine Ténartec15ecd2017-08-25 15:24:46 +02004793 MVPP22_XLG_CTRL1_FRAMESIZELIMIT_OFFS;
Stefan Chulski76eb1b12017-08-22 19:08:26 +02004794 writel(val, port->base + MVPP22_XLG_CTRL1_REG);
4795}
4796
Marcin Wojtas3f518502014-07-10 16:52:13 -03004797/* Set defaults to the MVPP2 port */
4798static void mvpp2_defaults_set(struct mvpp2_port *port)
4799{
4800 int tx_port_num, val, queue, ptxq, lrxq;
4801
Thomas Petazzoni3d9017d2017-03-07 16:53:11 +01004802 if (port->priv->hw_version == MVPP21) {
4803 /* Configure port to loopback if needed */
4804 if (port->flags & MVPP2_F_LOOPBACK)
4805 mvpp2_port_loopback_set(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004806
Thomas Petazzoni3d9017d2017-03-07 16:53:11 +01004807 /* Update TX FIFO MIN Threshold */
4808 val = readl(port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
4809 val &= ~MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK;
4810 /* Min. TX threshold must be less than minimal packet length */
4811 val |= MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(64 - 4 - 2);
4812 writel(val, port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
4813 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004814
4815 /* Disable Legacy WRR, Disable EJP, Release from reset */
4816 tx_port_num = mvpp2_egress_port(port);
4817 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG,
4818 tx_port_num);
4819 mvpp2_write(port->priv, MVPP2_TXP_SCHED_CMD_1_REG, 0);
4820
4821 /* Close bandwidth for all queues */
4822 for (queue = 0; queue < MVPP2_MAX_TXQ; queue++) {
4823 ptxq = mvpp2_txq_phys(port->id, queue);
4824 mvpp2_write(port->priv,
4825 MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(ptxq), 0);
4826 }
4827
4828 /* Set refill period to 1 usec, refill tokens
4829 * and bucket size to maximum
4830 */
4831 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PERIOD_REG,
4832 port->priv->tclk / USEC_PER_SEC);
4833 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_REFILL_REG);
4834 val &= ~MVPP2_TXP_REFILL_PERIOD_ALL_MASK;
4835 val |= MVPP2_TXP_REFILL_PERIOD_MASK(1);
4836 val |= MVPP2_TXP_REFILL_TOKENS_ALL_MASK;
4837 mvpp2_write(port->priv, MVPP2_TXP_SCHED_REFILL_REG, val);
4838 val = MVPP2_TXP_TOKEN_SIZE_MAX;
4839 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
4840
4841 /* Set MaximumLowLatencyPacketSize value to 256 */
4842 mvpp2_write(port->priv, MVPP2_RX_CTRL_REG(port->id),
4843 MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK |
4844 MVPP2_RX_LOW_LATENCY_PKT_SIZE(256));
4845
4846 /* Enable Rx cache snoop */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004847 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004848 queue = port->rxqs[lrxq]->id;
4849 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
4850 val |= MVPP2_SNOOP_PKT_SIZE_MASK |
4851 MVPP2_SNOOP_BUF_HDR_MASK;
4852 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
4853 }
4854
4855 /* At default, mask all interrupts to all present cpus */
4856 mvpp2_interrupts_disable(port);
4857}
4858
4859/* Enable/disable receiving packets */
4860static void mvpp2_ingress_enable(struct mvpp2_port *port)
4861{
4862 u32 val;
4863 int lrxq, queue;
4864
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004865 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004866 queue = port->rxqs[lrxq]->id;
4867 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
4868 val &= ~MVPP2_RXQ_DISABLE_MASK;
4869 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
4870 }
4871}
4872
4873static void mvpp2_ingress_disable(struct mvpp2_port *port)
4874{
4875 u32 val;
4876 int lrxq, queue;
4877
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004878 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004879 queue = port->rxqs[lrxq]->id;
4880 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
4881 val |= MVPP2_RXQ_DISABLE_MASK;
4882 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
4883 }
4884}
4885
4886/* Enable transmit via physical egress queue
4887 * - HW starts take descriptors from DRAM
4888 */
4889static void mvpp2_egress_enable(struct mvpp2_port *port)
4890{
4891 u32 qmap;
4892 int queue;
4893 int tx_port_num = mvpp2_egress_port(port);
4894
4895 /* Enable all initialized TXs. */
4896 qmap = 0;
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004897 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004898 struct mvpp2_tx_queue *txq = port->txqs[queue];
4899
Markus Elfringdbbb2f02017-04-17 14:07:52 +02004900 if (txq->descs)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004901 qmap |= (1 << queue);
4902 }
4903
4904 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
4905 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG, qmap);
4906}
4907
4908/* Disable transmit via physical egress queue
4909 * - HW doesn't take descriptors from DRAM
4910 */
4911static void mvpp2_egress_disable(struct mvpp2_port *port)
4912{
4913 u32 reg_data;
4914 int delay;
4915 int tx_port_num = mvpp2_egress_port(port);
4916
4917 /* Issue stop command for active channels only */
4918 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
4919 reg_data = (mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG)) &
4920 MVPP2_TXP_SCHED_ENQ_MASK;
4921 if (reg_data != 0)
4922 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG,
4923 (reg_data << MVPP2_TXP_SCHED_DISQ_OFFSET));
4924
4925 /* Wait for all Tx activity to terminate. */
4926 delay = 0;
4927 do {
4928 if (delay >= MVPP2_TX_DISABLE_TIMEOUT_MSEC) {
4929 netdev_warn(port->dev,
4930 "Tx stop timed out, status=0x%08x\n",
4931 reg_data);
4932 break;
4933 }
4934 mdelay(1);
4935 delay++;
4936
4937 /* Check port TX Command register that all
4938 * Tx queues are stopped
4939 */
4940 reg_data = mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG);
4941 } while (reg_data & MVPP2_TXP_SCHED_ENQ_MASK);
4942}
4943
4944/* Rx descriptors helper methods */
4945
4946/* Get number of Rx descriptors occupied by received packets */
4947static inline int
4948mvpp2_rxq_received(struct mvpp2_port *port, int rxq_id)
4949{
4950 u32 val = mvpp2_read(port->priv, MVPP2_RXQ_STATUS_REG(rxq_id));
4951
4952 return val & MVPP2_RXQ_OCCUPIED_MASK;
4953}
4954
4955/* Update Rx queue status with the number of occupied and available
4956 * Rx descriptor slots.
4957 */
4958static inline void
4959mvpp2_rxq_status_update(struct mvpp2_port *port, int rxq_id,
4960 int used_count, int free_count)
4961{
4962 /* Decrement the number of used descriptors and increment count
4963 * increment the number of free descriptors.
4964 */
4965 u32 val = used_count | (free_count << MVPP2_RXQ_NUM_NEW_OFFSET);
4966
4967 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_UPDATE_REG(rxq_id), val);
4968}
4969
4970/* Get pointer to next RX descriptor to be processed by SW */
4971static inline struct mvpp2_rx_desc *
4972mvpp2_rxq_next_desc_get(struct mvpp2_rx_queue *rxq)
4973{
4974 int rx_desc = rxq->next_desc_to_proc;
4975
4976 rxq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(rxq, rx_desc);
4977 prefetch(rxq->descs + rxq->next_desc_to_proc);
4978 return rxq->descs + rx_desc;
4979}
4980
4981/* Set rx queue offset */
4982static void mvpp2_rxq_offset_set(struct mvpp2_port *port,
4983 int prxq, int offset)
4984{
4985 u32 val;
4986
4987 /* Convert offset from bytes to units of 32 bytes */
4988 offset = offset >> 5;
4989
4990 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
4991 val &= ~MVPP2_RXQ_PACKET_OFFSET_MASK;
4992
4993 /* Offset is in */
4994 val |= ((offset << MVPP2_RXQ_PACKET_OFFSET_OFFS) &
4995 MVPP2_RXQ_PACKET_OFFSET_MASK);
4996
4997 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
4998}
4999
Marcin Wojtas3f518502014-07-10 16:52:13 -03005000/* Tx descriptors helper methods */
5001
Marcin Wojtas3f518502014-07-10 16:52:13 -03005002/* Get pointer to next Tx descriptor to be processed (send) by HW */
5003static struct mvpp2_tx_desc *
5004mvpp2_txq_next_desc_get(struct mvpp2_tx_queue *txq)
5005{
5006 int tx_desc = txq->next_desc_to_proc;
5007
5008 txq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(txq, tx_desc);
5009 return txq->descs + tx_desc;
5010}
5011
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005012/* Update HW with number of aggregated Tx descriptors to be sent
5013 *
5014 * Called only from mvpp2_tx(), so migration is disabled, using
5015 * smp_processor_id() is OK.
5016 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03005017static void mvpp2_aggr_txq_pend_desc_add(struct mvpp2_port *port, int pending)
5018{
5019 /* aggregated access - relevant TXQ number is written in TX desc */
Thomas Petazzonia7868412017-03-07 16:53:13 +01005020 mvpp2_percpu_write(port->priv, smp_processor_id(),
5021 MVPP2_AGGR_TXQ_UPDATE_REG, pending);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005022}
5023
5024
5025/* Check if there are enough free descriptors in aggregated txq.
5026 * If not, update the number of occupied descriptors and repeat the check.
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005027 *
5028 * Called only from mvpp2_tx(), so migration is disabled, using
5029 * smp_processor_id() is OK.
Marcin Wojtas3f518502014-07-10 16:52:13 -03005030 */
5031static int mvpp2_aggr_desc_num_check(struct mvpp2 *priv,
5032 struct mvpp2_tx_queue *aggr_txq, int num)
5033{
5034 if ((aggr_txq->count + num) > aggr_txq->size) {
5035 /* Update number of occupied aggregated Tx descriptors */
5036 int cpu = smp_processor_id();
5037 u32 val = mvpp2_read(priv, MVPP2_AGGR_TXQ_STATUS_REG(cpu));
5038
5039 aggr_txq->count = val & MVPP2_AGGR_TXQ_PENDING_MASK;
5040 }
5041
5042 if ((aggr_txq->count + num) > aggr_txq->size)
5043 return -ENOMEM;
5044
5045 return 0;
5046}
5047
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005048/* Reserved Tx descriptors allocation request
5049 *
5050 * Called only from mvpp2_txq_reserved_desc_num_proc(), itself called
5051 * only by mvpp2_tx(), so migration is disabled, using
5052 * smp_processor_id() is OK.
5053 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03005054static int mvpp2_txq_alloc_reserved_desc(struct mvpp2 *priv,
5055 struct mvpp2_tx_queue *txq, int num)
5056{
5057 u32 val;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005058 int cpu = smp_processor_id();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005059
5060 val = (txq->id << MVPP2_TXQ_RSVD_REQ_Q_OFFSET) | num;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005061 mvpp2_percpu_write(priv, cpu, MVPP2_TXQ_RSVD_REQ_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005062
Thomas Petazzonia7868412017-03-07 16:53:13 +01005063 val = mvpp2_percpu_read(priv, cpu, MVPP2_TXQ_RSVD_RSLT_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005064
5065 return val & MVPP2_TXQ_RSVD_RSLT_MASK;
5066}
5067
5068/* Check if there are enough reserved descriptors for transmission.
5069 * If not, request chunk of reserved descriptors and check again.
5070 */
5071static int mvpp2_txq_reserved_desc_num_proc(struct mvpp2 *priv,
5072 struct mvpp2_tx_queue *txq,
5073 struct mvpp2_txq_pcpu *txq_pcpu,
5074 int num)
5075{
5076 int req, cpu, desc_count;
5077
5078 if (txq_pcpu->reserved_num >= num)
5079 return 0;
5080
5081 /* Not enough descriptors reserved! Update the reserved descriptor
5082 * count and check again.
5083 */
5084
5085 desc_count = 0;
5086 /* Compute total of used descriptors */
5087 for_each_present_cpu(cpu) {
5088 struct mvpp2_txq_pcpu *txq_pcpu_aux;
5089
5090 txq_pcpu_aux = per_cpu_ptr(txq->pcpu, cpu);
5091 desc_count += txq_pcpu_aux->count;
5092 desc_count += txq_pcpu_aux->reserved_num;
5093 }
5094
5095 req = max(MVPP2_CPU_DESC_CHUNK, num - txq_pcpu->reserved_num);
5096 desc_count += req;
5097
5098 if (desc_count >
5099 (txq->size - (num_present_cpus() * MVPP2_CPU_DESC_CHUNK)))
5100 return -ENOMEM;
5101
5102 txq_pcpu->reserved_num += mvpp2_txq_alloc_reserved_desc(priv, txq, req);
5103
5104 /* OK, the descriptor cound has been updated: check again. */
5105 if (txq_pcpu->reserved_num < num)
5106 return -ENOMEM;
5107 return 0;
5108}
5109
5110/* Release the last allocated Tx descriptor. Useful to handle DMA
5111 * mapping failures in the Tx path.
5112 */
5113static void mvpp2_txq_desc_put(struct mvpp2_tx_queue *txq)
5114{
5115 if (txq->next_desc_to_proc == 0)
5116 txq->next_desc_to_proc = txq->last_desc - 1;
5117 else
5118 txq->next_desc_to_proc--;
5119}
5120
5121/* Set Tx descriptors fields relevant for CSUM calculation */
5122static u32 mvpp2_txq_desc_csum(int l3_offs, int l3_proto,
5123 int ip_hdr_len, int l4_proto)
5124{
5125 u32 command;
5126
5127 /* fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
5128 * G_L4_chk, L4_type required only for checksum calculation
5129 */
5130 command = (l3_offs << MVPP2_TXD_L3_OFF_SHIFT);
5131 command |= (ip_hdr_len << MVPP2_TXD_IP_HLEN_SHIFT);
5132 command |= MVPP2_TXD_IP_CSUM_DISABLE;
5133
5134 if (l3_proto == swab16(ETH_P_IP)) {
5135 command &= ~MVPP2_TXD_IP_CSUM_DISABLE; /* enable IPv4 csum */
5136 command &= ~MVPP2_TXD_L3_IP6; /* enable IPv4 */
5137 } else {
5138 command |= MVPP2_TXD_L3_IP6; /* enable IPv6 */
5139 }
5140
5141 if (l4_proto == IPPROTO_TCP) {
5142 command &= ~MVPP2_TXD_L4_UDP; /* enable TCP */
5143 command &= ~MVPP2_TXD_L4_CSUM_FRAG; /* generate L4 csum */
5144 } else if (l4_proto == IPPROTO_UDP) {
5145 command |= MVPP2_TXD_L4_UDP; /* enable UDP */
5146 command &= ~MVPP2_TXD_L4_CSUM_FRAG; /* generate L4 csum */
5147 } else {
5148 command |= MVPP2_TXD_L4_CSUM_NOT;
5149 }
5150
5151 return command;
5152}
5153
5154/* Get number of sent descriptors and decrement counter.
5155 * The number of sent descriptors is returned.
5156 * Per-CPU access
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005157 *
5158 * Called only from mvpp2_txq_done(), called from mvpp2_tx()
5159 * (migration disabled) and from the TX completion tasklet (migration
5160 * disabled) so using smp_processor_id() is OK.
Marcin Wojtas3f518502014-07-10 16:52:13 -03005161 */
5162static inline int mvpp2_txq_sent_desc_proc(struct mvpp2_port *port,
5163 struct mvpp2_tx_queue *txq)
5164{
5165 u32 val;
5166
5167 /* Reading status reg resets transmitted descriptor counter */
Thomas Petazzonia7868412017-03-07 16:53:13 +01005168 val = mvpp2_percpu_read(port->priv, smp_processor_id(),
5169 MVPP2_TXQ_SENT_REG(txq->id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005170
5171 return (val & MVPP2_TRANSMITTED_COUNT_MASK) >>
5172 MVPP2_TRANSMITTED_COUNT_OFFSET;
5173}
5174
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02005175/* Called through on_each_cpu(), so runs on all CPUs, with migration
5176 * disabled, therefore using smp_processor_id() is OK.
5177 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03005178static void mvpp2_txq_sent_counter_clear(void *arg)
5179{
5180 struct mvpp2_port *port = arg;
5181 int queue;
5182
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005183 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005184 int id = port->txqs[queue]->id;
5185
Thomas Petazzonia7868412017-03-07 16:53:13 +01005186 mvpp2_percpu_read(port->priv, smp_processor_id(),
5187 MVPP2_TXQ_SENT_REG(id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005188 }
5189}
5190
5191/* Set max sizes for Tx queues */
5192static void mvpp2_txp_max_tx_size_set(struct mvpp2_port *port)
5193{
5194 u32 val, size, mtu;
5195 int txq, tx_port_num;
5196
5197 mtu = port->pkt_size * 8;
5198 if (mtu > MVPP2_TXP_MTU_MAX)
5199 mtu = MVPP2_TXP_MTU_MAX;
5200
5201 /* WA for wrong Token bucket update: Set MTU value = 3*real MTU value */
5202 mtu = 3 * mtu;
5203
5204 /* Indirect access to registers */
5205 tx_port_num = mvpp2_egress_port(port);
5206 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
5207
5208 /* Set MTU */
5209 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_MTU_REG);
5210 val &= ~MVPP2_TXP_MTU_MAX;
5211 val |= mtu;
5212 mvpp2_write(port->priv, MVPP2_TXP_SCHED_MTU_REG, val);
5213
5214 /* TXP token size and all TXQs token size must be larger that MTU */
5215 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG);
5216 size = val & MVPP2_TXP_TOKEN_SIZE_MAX;
5217 if (size < mtu) {
5218 size = mtu;
5219 val &= ~MVPP2_TXP_TOKEN_SIZE_MAX;
5220 val |= size;
5221 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
5222 }
5223
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005224 for (txq = 0; txq < port->ntxqs; txq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005225 val = mvpp2_read(port->priv,
5226 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq));
5227 size = val & MVPP2_TXQ_TOKEN_SIZE_MAX;
5228
5229 if (size < mtu) {
5230 size = mtu;
5231 val &= ~MVPP2_TXQ_TOKEN_SIZE_MAX;
5232 val |= size;
5233 mvpp2_write(port->priv,
5234 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq),
5235 val);
5236 }
5237 }
5238}
5239
5240/* Set the number of packets that will be received before Rx interrupt
5241 * will be generated by HW.
5242 */
5243static void mvpp2_rx_pkts_coal_set(struct mvpp2_port *port,
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01005244 struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005245{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005246 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005247
Thomas Petazzonif8b0d5f2017-02-21 11:28:03 +01005248 if (rxq->pkts_coal > MVPP2_OCCUPIED_THRESH_MASK)
5249 rxq->pkts_coal = MVPP2_OCCUPIED_THRESH_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005250
Thomas Petazzonia7868412017-03-07 16:53:13 +01005251 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
5252 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_THRESH_REG,
5253 rxq->pkts_coal);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005254
5255 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005256}
5257
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005258/* For some reason in the LSP this is done on each CPU. Why ? */
5259static void mvpp2_tx_pkts_coal_set(struct mvpp2_port *port,
5260 struct mvpp2_tx_queue *txq)
5261{
5262 int cpu = get_cpu();
5263 u32 val;
5264
5265 if (txq->done_pkts_coal > MVPP2_TXQ_THRESH_MASK)
5266 txq->done_pkts_coal = MVPP2_TXQ_THRESH_MASK;
5267
5268 val = (txq->done_pkts_coal << MVPP2_TXQ_THRESH_OFFSET);
5269 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5270 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_THRESH_REG, val);
5271
5272 put_cpu();
5273}
5274
Thomas Petazzoniab426762017-02-21 11:28:04 +01005275static u32 mvpp2_usec_to_cycles(u32 usec, unsigned long clk_hz)
5276{
5277 u64 tmp = (u64)clk_hz * usec;
5278
5279 do_div(tmp, USEC_PER_SEC);
5280
5281 return tmp > U32_MAX ? U32_MAX : tmp;
5282}
5283
5284static u32 mvpp2_cycles_to_usec(u32 cycles, unsigned long clk_hz)
5285{
5286 u64 tmp = (u64)cycles * USEC_PER_SEC;
5287
5288 do_div(tmp, clk_hz);
5289
5290 return tmp > U32_MAX ? U32_MAX : tmp;
5291}
5292
Marcin Wojtas3f518502014-07-10 16:52:13 -03005293/* Set the time delay in usec before Rx interrupt */
5294static void mvpp2_rx_time_coal_set(struct mvpp2_port *port,
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01005295 struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005296{
Thomas Petazzoniab426762017-02-21 11:28:04 +01005297 unsigned long freq = port->priv->tclk;
5298 u32 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005299
Thomas Petazzoniab426762017-02-21 11:28:04 +01005300 if (val > MVPP2_MAX_ISR_RX_THRESHOLD) {
5301 rxq->time_coal =
5302 mvpp2_cycles_to_usec(MVPP2_MAX_ISR_RX_THRESHOLD, freq);
5303
5304 /* re-evaluate to get actual register value */
5305 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
5306 }
5307
Marcin Wojtas3f518502014-07-10 16:52:13 -03005308 mvpp2_write(port->priv, MVPP2_ISR_RX_THRESHOLD_REG(rxq->id), val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005309}
5310
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005311static void mvpp2_tx_time_coal_set(struct mvpp2_port *port)
5312{
5313 unsigned long freq = port->priv->tclk;
5314 u32 val = mvpp2_usec_to_cycles(port->tx_time_coal, freq);
5315
5316 if (val > MVPP2_MAX_ISR_TX_THRESHOLD) {
5317 port->tx_time_coal =
5318 mvpp2_cycles_to_usec(MVPP2_MAX_ISR_TX_THRESHOLD, freq);
5319
5320 /* re-evaluate to get actual register value */
5321 val = mvpp2_usec_to_cycles(port->tx_time_coal, freq);
5322 }
5323
5324 mvpp2_write(port->priv, MVPP2_ISR_TX_THRESHOLD_REG(port->id), val);
5325}
5326
Marcin Wojtas3f518502014-07-10 16:52:13 -03005327/* Free Tx queue skbuffs */
5328static void mvpp2_txq_bufs_free(struct mvpp2_port *port,
5329 struct mvpp2_tx_queue *txq,
5330 struct mvpp2_txq_pcpu *txq_pcpu, int num)
5331{
5332 int i;
5333
5334 for (i = 0; i < num; i++) {
Thomas Petazzoni83544912016-12-21 11:28:49 +01005335 struct mvpp2_txq_pcpu_buf *tx_buf =
5336 txq_pcpu->buffs + txq_pcpu->txq_get_index;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005337
Antoine Tenart20920262017-10-23 15:24:30 +02005338 if (!IS_TSO_HEADER(txq_pcpu, tx_buf->dma))
5339 dma_unmap_single(port->dev->dev.parent, tx_buf->dma,
5340 tx_buf->size, DMA_TO_DEVICE);
Thomas Petazzoni36fb7432017-02-21 11:28:05 +01005341 if (tx_buf->skb)
5342 dev_kfree_skb_any(tx_buf->skb);
5343
5344 mvpp2_txq_inc_get(txq_pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005345 }
5346}
5347
5348static inline struct mvpp2_rx_queue *mvpp2_get_rx_queue(struct mvpp2_port *port,
5349 u32 cause)
5350{
5351 int queue = fls(cause) - 1;
5352
5353 return port->rxqs[queue];
5354}
5355
5356static inline struct mvpp2_tx_queue *mvpp2_get_tx_queue(struct mvpp2_port *port,
5357 u32 cause)
5358{
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005359 int queue = fls(cause) - 1;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005360
5361 return port->txqs[queue];
5362}
5363
5364/* Handle end of transmission */
5365static void mvpp2_txq_done(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
5366 struct mvpp2_txq_pcpu *txq_pcpu)
5367{
5368 struct netdev_queue *nq = netdev_get_tx_queue(port->dev, txq->log_id);
5369 int tx_done;
5370
5371 if (txq_pcpu->cpu != smp_processor_id())
5372 netdev_err(port->dev, "wrong cpu on the end of Tx processing\n");
5373
5374 tx_done = mvpp2_txq_sent_desc_proc(port, txq);
5375 if (!tx_done)
5376 return;
5377 mvpp2_txq_bufs_free(port, txq, txq_pcpu, tx_done);
5378
5379 txq_pcpu->count -= tx_done;
5380
5381 if (netif_tx_queue_stopped(nq))
5382 if (txq_pcpu->size - txq_pcpu->count >= MAX_SKB_FRAGS + 1)
5383 netif_tx_wake_queue(nq);
5384}
5385
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005386static unsigned int mvpp2_tx_done(struct mvpp2_port *port, u32 cause,
5387 int cpu)
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005388{
5389 struct mvpp2_tx_queue *txq;
5390 struct mvpp2_txq_pcpu *txq_pcpu;
5391 unsigned int tx_todo = 0;
5392
5393 while (cause) {
5394 txq = mvpp2_get_tx_queue(port, cause);
5395 if (!txq)
5396 break;
5397
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005398 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005399
5400 if (txq_pcpu->count) {
5401 mvpp2_txq_done(port, txq, txq_pcpu);
5402 tx_todo += txq_pcpu->count;
5403 }
5404
5405 cause &= ~(1 << txq->log_id);
5406 }
5407 return tx_todo;
5408}
5409
Marcin Wojtas3f518502014-07-10 16:52:13 -03005410/* Rx/Tx queue initialization/cleanup methods */
5411
5412/* Allocate and initialize descriptors for aggr TXQ */
5413static int mvpp2_aggr_txq_init(struct platform_device *pdev,
Antoine Ténart85affd72017-08-23 09:46:55 +02005414 struct mvpp2_tx_queue *aggr_txq, int cpu,
Marcin Wojtas3f518502014-07-10 16:52:13 -03005415 struct mvpp2 *priv)
5416{
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005417 u32 txq_dma;
5418
Marcin Wojtas3f518502014-07-10 16:52:13 -03005419 /* Allocate memory for TX descriptors */
5420 aggr_txq->descs = dma_alloc_coherent(&pdev->dev,
Antoine Ténart85affd72017-08-23 09:46:55 +02005421 MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005422 &aggr_txq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005423 if (!aggr_txq->descs)
5424 return -ENOMEM;
5425
Marcin Wojtas3f518502014-07-10 16:52:13 -03005426 aggr_txq->last_desc = aggr_txq->size - 1;
5427
5428 /* Aggr TXQ no reset WA */
5429 aggr_txq->next_desc_to_proc = mvpp2_read(priv,
5430 MVPP2_AGGR_TXQ_INDEX_REG(cpu));
5431
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005432 /* Set Tx descriptors queue starting address indirect
5433 * access
5434 */
5435 if (priv->hw_version == MVPP21)
5436 txq_dma = aggr_txq->descs_dma;
5437 else
5438 txq_dma = aggr_txq->descs_dma >>
5439 MVPP22_AGGR_TXQ_DESC_ADDR_OFFS;
5440
5441 mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu), txq_dma);
Antoine Ténart85affd72017-08-23 09:46:55 +02005442 mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu),
5443 MVPP2_AGGR_TXQ_SIZE);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005444
5445 return 0;
5446}
5447
5448/* Create a specified Rx queue */
5449static int mvpp2_rxq_init(struct mvpp2_port *port,
5450 struct mvpp2_rx_queue *rxq)
5451
5452{
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005453 u32 rxq_dma;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005454 int cpu;
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005455
Marcin Wojtas3f518502014-07-10 16:52:13 -03005456 rxq->size = port->rx_ring_size;
5457
5458 /* Allocate memory for RX descriptors */
5459 rxq->descs = dma_alloc_coherent(port->dev->dev.parent,
5460 rxq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005461 &rxq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005462 if (!rxq->descs)
5463 return -ENOMEM;
5464
Marcin Wojtas3f518502014-07-10 16:52:13 -03005465 rxq->last_desc = rxq->size - 1;
5466
5467 /* Zero occupied and non-occupied counters - direct access */
5468 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
5469
5470 /* Set Rx descriptors queue starting address - indirect access */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005471 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005472 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01005473 if (port->priv->hw_version == MVPP21)
5474 rxq_dma = rxq->descs_dma;
5475 else
5476 rxq_dma = rxq->descs_dma >> MVPP22_DESC_ADDR_OFFS;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005477 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_ADDR_REG, rxq_dma);
5478 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_SIZE_REG, rxq->size);
5479 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_INDEX_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005480 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005481
5482 /* Set Offset */
5483 mvpp2_rxq_offset_set(port, rxq->id, NET_SKB_PAD);
5484
5485 /* Set coalescing pkts and time */
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01005486 mvpp2_rx_pkts_coal_set(port, rxq);
5487 mvpp2_rx_time_coal_set(port, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005488
5489 /* Add number of descriptors ready for receiving packets */
5490 mvpp2_rxq_status_update(port, rxq->id, 0, rxq->size);
5491
5492 return 0;
5493}
5494
5495/* Push packets received by the RXQ to BM pool */
5496static void mvpp2_rxq_drop_pkts(struct mvpp2_port *port,
5497 struct mvpp2_rx_queue *rxq)
5498{
5499 int rx_received, i;
5500
5501 rx_received = mvpp2_rxq_received(port, rxq->id);
5502 if (!rx_received)
5503 return;
5504
5505 for (i = 0; i < rx_received; i++) {
5506 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005507 u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
5508 int pool;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005509
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005510 pool = (status & MVPP2_RXD_BM_POOL_ID_MASK) >>
5511 MVPP2_RXD_BM_POOL_ID_OFFS;
5512
Thomas Petazzoni7d7627b2017-06-22 14:23:20 +02005513 mvpp2_bm_pool_put(port, pool,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005514 mvpp2_rxdesc_dma_addr_get(port, rx_desc),
5515 mvpp2_rxdesc_cookie_get(port, rx_desc));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005516 }
5517 mvpp2_rxq_status_update(port, rxq->id, rx_received, rx_received);
5518}
5519
5520/* Cleanup Rx queue */
5521static void mvpp2_rxq_deinit(struct mvpp2_port *port,
5522 struct mvpp2_rx_queue *rxq)
5523{
Thomas Petazzonia7868412017-03-07 16:53:13 +01005524 int cpu;
5525
Marcin Wojtas3f518502014-07-10 16:52:13 -03005526 mvpp2_rxq_drop_pkts(port, rxq);
5527
5528 if (rxq->descs)
5529 dma_free_coherent(port->dev->dev.parent,
5530 rxq->size * MVPP2_DESC_ALIGNED_SIZE,
5531 rxq->descs,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005532 rxq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005533
5534 rxq->descs = NULL;
5535 rxq->last_desc = 0;
5536 rxq->next_desc_to_proc = 0;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005537 rxq->descs_dma = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005538
5539 /* Clear Rx descriptors queue starting address and size;
5540 * free descriptor number
5541 */
5542 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005543 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005544 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
5545 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_ADDR_REG, 0);
5546 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_SIZE_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005547 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005548}
5549
5550/* Create and initialize a Tx queue */
5551static int mvpp2_txq_init(struct mvpp2_port *port,
5552 struct mvpp2_tx_queue *txq)
5553{
5554 u32 val;
5555 int cpu, desc, desc_per_txq, tx_port_num;
5556 struct mvpp2_txq_pcpu *txq_pcpu;
5557
5558 txq->size = port->tx_ring_size;
5559
5560 /* Allocate memory for Tx descriptors */
5561 txq->descs = dma_alloc_coherent(port->dev->dev.parent,
5562 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005563 &txq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005564 if (!txq->descs)
5565 return -ENOMEM;
5566
Marcin Wojtas3f518502014-07-10 16:52:13 -03005567 txq->last_desc = txq->size - 1;
5568
5569 /* Set Tx descriptors queue starting address - indirect access */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005570 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005571 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5572 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_ADDR_REG,
5573 txq->descs_dma);
5574 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_SIZE_REG,
5575 txq->size & MVPP2_TXQ_DESC_SIZE_MASK);
5576 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_INDEX_REG, 0);
5577 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_RSVD_CLR_REG,
5578 txq->id << MVPP2_TXQ_RSVD_CLR_OFFSET);
5579 val = mvpp2_percpu_read(port->priv, cpu, MVPP2_TXQ_PENDING_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005580 val &= ~MVPP2_TXQ_PENDING_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005581 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PENDING_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005582
5583 /* Calculate base address in prefetch buffer. We reserve 16 descriptors
5584 * for each existing TXQ.
5585 * TCONTS for PON port must be continuous from 0 to MVPP2_MAX_TCONT
5586 * GBE ports assumed to be continious from 0 to MVPP2_MAX_PORTS
5587 */
5588 desc_per_txq = 16;
5589 desc = (port->id * MVPP2_MAX_TXQ * desc_per_txq) +
5590 (txq->log_id * desc_per_txq);
5591
Thomas Petazzonia7868412017-03-07 16:53:13 +01005592 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG,
5593 MVPP2_PREF_BUF_PTR(desc) | MVPP2_PREF_BUF_SIZE_16 |
5594 MVPP2_PREF_BUF_THRESH(desc_per_txq / 2));
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005595 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005596
5597 /* WRR / EJP configuration - indirect access */
5598 tx_port_num = mvpp2_egress_port(port);
5599 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
5600
5601 val = mvpp2_read(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id));
5602 val &= ~MVPP2_TXQ_REFILL_PERIOD_ALL_MASK;
5603 val |= MVPP2_TXQ_REFILL_PERIOD_MASK(1);
5604 val |= MVPP2_TXQ_REFILL_TOKENS_ALL_MASK;
5605 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id), val);
5606
5607 val = MVPP2_TXQ_TOKEN_SIZE_MAX;
5608 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq->log_id),
5609 val);
5610
5611 for_each_present_cpu(cpu) {
5612 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
5613 txq_pcpu->size = txq->size;
Markus Elfring02c91ec2017-04-17 08:09:07 +02005614 txq_pcpu->buffs = kmalloc_array(txq_pcpu->size,
5615 sizeof(*txq_pcpu->buffs),
5616 GFP_KERNEL);
Thomas Petazzoni83544912016-12-21 11:28:49 +01005617 if (!txq_pcpu->buffs)
Markus Elfring20b1e162017-04-17 12:58:33 +02005618 goto cleanup;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005619
5620 txq_pcpu->count = 0;
5621 txq_pcpu->reserved_num = 0;
5622 txq_pcpu->txq_put_index = 0;
5623 txq_pcpu->txq_get_index = 0;
Antoine Ténart186cd4d2017-08-23 09:46:56 +02005624
5625 txq_pcpu->tso_headers =
5626 dma_alloc_coherent(port->dev->dev.parent,
Yan Markman822eaf72017-10-23 15:24:29 +02005627 txq_pcpu->size * TSO_HEADER_SIZE,
Antoine Ténart186cd4d2017-08-23 09:46:56 +02005628 &txq_pcpu->tso_headers_dma,
5629 GFP_KERNEL);
5630 if (!txq_pcpu->tso_headers)
5631 goto cleanup;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005632 }
5633
5634 return 0;
Markus Elfring20b1e162017-04-17 12:58:33 +02005635cleanup:
Marcin Wojtas71ce3912015-08-06 19:00:29 +02005636 for_each_present_cpu(cpu) {
5637 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Thomas Petazzoni83544912016-12-21 11:28:49 +01005638 kfree(txq_pcpu->buffs);
Antoine Ténart186cd4d2017-08-23 09:46:56 +02005639
5640 dma_free_coherent(port->dev->dev.parent,
Yan Markman822eaf72017-10-23 15:24:29 +02005641 txq_pcpu->size * TSO_HEADER_SIZE,
Antoine Ténart186cd4d2017-08-23 09:46:56 +02005642 txq_pcpu->tso_headers,
5643 txq_pcpu->tso_headers_dma);
Marcin Wojtas71ce3912015-08-06 19:00:29 +02005644 }
5645
5646 dma_free_coherent(port->dev->dev.parent,
5647 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005648 txq->descs, txq->descs_dma);
Marcin Wojtas71ce3912015-08-06 19:00:29 +02005649
5650 return -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005651}
5652
5653/* Free allocated TXQ resources */
5654static void mvpp2_txq_deinit(struct mvpp2_port *port,
5655 struct mvpp2_tx_queue *txq)
5656{
5657 struct mvpp2_txq_pcpu *txq_pcpu;
5658 int cpu;
5659
5660 for_each_present_cpu(cpu) {
5661 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Thomas Petazzoni83544912016-12-21 11:28:49 +01005662 kfree(txq_pcpu->buffs);
Antoine Ténart186cd4d2017-08-23 09:46:56 +02005663
5664 dma_free_coherent(port->dev->dev.parent,
Yan Markman822eaf72017-10-23 15:24:29 +02005665 txq_pcpu->size * TSO_HEADER_SIZE,
Antoine Ténart186cd4d2017-08-23 09:46:56 +02005666 txq_pcpu->tso_headers,
5667 txq_pcpu->tso_headers_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005668 }
5669
5670 if (txq->descs)
5671 dma_free_coherent(port->dev->dev.parent,
5672 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005673 txq->descs, txq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005674
5675 txq->descs = NULL;
5676 txq->last_desc = 0;
5677 txq->next_desc_to_proc = 0;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005678 txq->descs_dma = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005679
5680 /* Set minimum bandwidth for disabled TXQs */
5681 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(txq->id), 0);
5682
5683 /* Set Tx descriptors queue starting address and size */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005684 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005685 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5686 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_ADDR_REG, 0);
5687 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_SIZE_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005688 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005689}
5690
5691/* Cleanup Tx ports */
5692static void mvpp2_txq_clean(struct mvpp2_port *port, struct mvpp2_tx_queue *txq)
5693{
5694 struct mvpp2_txq_pcpu *txq_pcpu;
5695 int delay, pending, cpu;
5696 u32 val;
5697
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005698 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005699 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5700 val = mvpp2_percpu_read(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005701 val |= MVPP2_TXQ_DRAIN_EN_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005702 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005703
5704 /* The napi queue has been stopped so wait for all packets
5705 * to be transmitted.
5706 */
5707 delay = 0;
5708 do {
5709 if (delay >= MVPP2_TX_PENDING_TIMEOUT_MSEC) {
5710 netdev_warn(port->dev,
5711 "port %d: cleaning queue %d timed out\n",
5712 port->id, txq->log_id);
5713 break;
5714 }
5715 mdelay(1);
5716 delay++;
5717
Thomas Petazzonia7868412017-03-07 16:53:13 +01005718 pending = mvpp2_percpu_read(port->priv, cpu,
5719 MVPP2_TXQ_PENDING_REG);
5720 pending &= MVPP2_TXQ_PENDING_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005721 } while (pending);
5722
5723 val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005724 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG, val);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005725 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005726
5727 for_each_present_cpu(cpu) {
5728 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
5729
5730 /* Release all packets */
5731 mvpp2_txq_bufs_free(port, txq, txq_pcpu, txq_pcpu->count);
5732
5733 /* Reset queue */
5734 txq_pcpu->count = 0;
5735 txq_pcpu->txq_put_index = 0;
5736 txq_pcpu->txq_get_index = 0;
5737 }
5738}
5739
5740/* Cleanup all Tx queues */
5741static void mvpp2_cleanup_txqs(struct mvpp2_port *port)
5742{
5743 struct mvpp2_tx_queue *txq;
5744 int queue;
5745 u32 val;
5746
5747 val = mvpp2_read(port->priv, MVPP2_TX_PORT_FLUSH_REG);
5748
5749 /* Reset Tx ports and delete Tx queues */
5750 val |= MVPP2_TX_PORT_FLUSH_MASK(port->id);
5751 mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
5752
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005753 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005754 txq = port->txqs[queue];
5755 mvpp2_txq_clean(port, txq);
5756 mvpp2_txq_deinit(port, txq);
5757 }
5758
5759 on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
5760
5761 val &= ~MVPP2_TX_PORT_FLUSH_MASK(port->id);
5762 mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
5763}
5764
5765/* Cleanup all Rx queues */
5766static void mvpp2_cleanup_rxqs(struct mvpp2_port *port)
5767{
5768 int queue;
5769
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005770 for (queue = 0; queue < port->nrxqs; queue++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005771 mvpp2_rxq_deinit(port, port->rxqs[queue]);
5772}
5773
5774/* Init all Rx queues for port */
5775static int mvpp2_setup_rxqs(struct mvpp2_port *port)
5776{
5777 int queue, err;
5778
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005779 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005780 err = mvpp2_rxq_init(port, port->rxqs[queue]);
5781 if (err)
5782 goto err_cleanup;
5783 }
5784 return 0;
5785
5786err_cleanup:
5787 mvpp2_cleanup_rxqs(port);
5788 return err;
5789}
5790
5791/* Init all tx queues for port */
5792static int mvpp2_setup_txqs(struct mvpp2_port *port)
5793{
5794 struct mvpp2_tx_queue *txq;
5795 int queue, err;
5796
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005797 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005798 txq = port->txqs[queue];
5799 err = mvpp2_txq_init(port, txq);
5800 if (err)
5801 goto err_cleanup;
5802 }
5803
Thomas Petazzoni213f4282017-08-03 10:42:00 +02005804 if (port->has_tx_irqs) {
5805 mvpp2_tx_time_coal_set(port);
5806 for (queue = 0; queue < port->ntxqs; queue++) {
5807 txq = port->txqs[queue];
5808 mvpp2_tx_pkts_coal_set(port, txq);
5809 }
5810 }
5811
Marcin Wojtas3f518502014-07-10 16:52:13 -03005812 on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
5813 return 0;
5814
5815err_cleanup:
5816 mvpp2_cleanup_txqs(port);
5817 return err;
5818}
5819
5820/* The callback for per-port interrupt */
5821static irqreturn_t mvpp2_isr(int irq, void *dev_id)
5822{
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02005823 struct mvpp2_queue_vector *qv = dev_id;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005824
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02005825 mvpp2_qvec_interrupt_disable(qv);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005826
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02005827 napi_schedule(&qv->napi);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005828
5829 return IRQ_HANDLED;
5830}
5831
Antoine Tenartfd3651b2017-09-01 11:04:54 +02005832/* Per-port interrupt for link status changes */
5833static irqreturn_t mvpp2_link_status_isr(int irq, void *dev_id)
5834{
5835 struct mvpp2_port *port = (struct mvpp2_port *)dev_id;
5836 struct net_device *dev = port->dev;
5837 bool event = false, link = false;
5838 u32 val;
5839
5840 mvpp22_gop_mask_irq(port);
5841
5842 if (port->gop_id == 0 &&
5843 port->phy_interface == PHY_INTERFACE_MODE_10GKR) {
5844 val = readl(port->base + MVPP22_XLG_INT_STAT);
5845 if (val & MVPP22_XLG_INT_STAT_LINK) {
5846 event = true;
5847 val = readl(port->base + MVPP22_XLG_STATUS);
5848 if (val & MVPP22_XLG_STATUS_LINK_UP)
5849 link = true;
5850 }
5851 } else if (phy_interface_mode_is_rgmii(port->phy_interface) ||
5852 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
5853 val = readl(port->base + MVPP22_GMAC_INT_STAT);
5854 if (val & MVPP22_GMAC_INT_STAT_LINK) {
5855 event = true;
5856 val = readl(port->base + MVPP2_GMAC_STATUS0);
5857 if (val & MVPP2_GMAC_STATUS0_LINK_UP)
5858 link = true;
5859 }
5860 }
5861
5862 if (!netif_running(dev) || !event)
5863 goto handled;
5864
5865 if (link) {
5866 mvpp2_interrupts_enable(port);
5867
5868 mvpp2_egress_enable(port);
5869 mvpp2_ingress_enable(port);
5870 netif_carrier_on(dev);
5871 netif_tx_wake_all_queues(dev);
5872 } else {
5873 netif_tx_stop_all_queues(dev);
5874 netif_carrier_off(dev);
5875 mvpp2_ingress_disable(port);
5876 mvpp2_egress_disable(port);
5877
5878 mvpp2_interrupts_disable(port);
5879 }
5880
5881handled:
5882 mvpp22_gop_unmask_irq(port);
5883 return IRQ_HANDLED;
5884}
5885
Antoine Tenart65a2c092017-08-30 10:29:18 +02005886static void mvpp2_gmac_set_autoneg(struct mvpp2_port *port,
5887 struct phy_device *phydev)
5888{
5889 u32 val;
5890
5891 if (port->phy_interface != PHY_INTERFACE_MODE_RGMII &&
5892 port->phy_interface != PHY_INTERFACE_MODE_RGMII_ID &&
5893 port->phy_interface != PHY_INTERFACE_MODE_RGMII_RXID &&
5894 port->phy_interface != PHY_INTERFACE_MODE_RGMII_TXID &&
5895 port->phy_interface != PHY_INTERFACE_MODE_SGMII)
5896 return;
5897
5898 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5899 val &= ~(MVPP2_GMAC_CONFIG_MII_SPEED |
5900 MVPP2_GMAC_CONFIG_GMII_SPEED |
5901 MVPP2_GMAC_CONFIG_FULL_DUPLEX |
5902 MVPP2_GMAC_AN_SPEED_EN |
5903 MVPP2_GMAC_AN_DUPLEX_EN);
5904
5905 if (phydev->duplex)
5906 val |= MVPP2_GMAC_CONFIG_FULL_DUPLEX;
5907
5908 if (phydev->speed == SPEED_1000)
5909 val |= MVPP2_GMAC_CONFIG_GMII_SPEED;
5910 else if (phydev->speed == SPEED_100)
5911 val |= MVPP2_GMAC_CONFIG_MII_SPEED;
5912
5913 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
Antoine Tenart65a2c092017-08-30 10:29:18 +02005914}
5915
Marcin Wojtas3f518502014-07-10 16:52:13 -03005916/* Adjust link */
5917static void mvpp2_link_event(struct net_device *dev)
5918{
5919 struct mvpp2_port *port = netdev_priv(dev);
Philippe Reynes8e072692016-06-28 00:08:11 +02005920 struct phy_device *phydev = dev->phydev;
Antoine Tenart89273bc2017-08-30 10:29:19 +02005921 bool link_reconfigured = false;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005922 u32 val;
5923
5924 if (phydev->link) {
Antoine Tenart89273bc2017-08-30 10:29:19 +02005925 if (port->phy_interface != phydev->interface && port->comphy) {
5926 /* disable current port for reconfiguration */
5927 mvpp2_interrupts_disable(port);
5928 netif_carrier_off(port->dev);
5929 mvpp2_port_disable(port);
5930 phy_power_off(port->comphy);
5931
5932 /* comphy reconfiguration */
5933 port->phy_interface = phydev->interface;
5934 mvpp22_comphy_init(port);
5935
5936 /* gop/mac reconfiguration */
5937 mvpp22_gop_init(port);
5938 mvpp2_port_mii_set(port);
5939
5940 link_reconfigured = true;
5941 }
5942
Marcin Wojtas3f518502014-07-10 16:52:13 -03005943 if ((port->speed != phydev->speed) ||
5944 (port->duplex != phydev->duplex)) {
Antoine Tenart65a2c092017-08-30 10:29:18 +02005945 mvpp2_gmac_set_autoneg(port, phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005946
5947 port->duplex = phydev->duplex;
5948 port->speed = phydev->speed;
5949 }
5950 }
5951
Antoine Tenart89273bc2017-08-30 10:29:19 +02005952 if (phydev->link != port->link || link_reconfigured) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005953 port->link = phydev->link;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005954
Marcin Wojtas3f518502014-07-10 16:52:13 -03005955 if (phydev->link) {
Antoine Tenart65a2c092017-08-30 10:29:18 +02005956 if (port->phy_interface == PHY_INTERFACE_MODE_RGMII ||
5957 port->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
5958 port->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID ||
5959 port->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID ||
5960 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
5961 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5962 val |= (MVPP2_GMAC_FORCE_LINK_PASS |
5963 MVPP2_GMAC_FORCE_LINK_DOWN);
5964 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5965 }
Antoine Tenartf55744a2017-08-30 10:29:17 +02005966
5967 mvpp2_interrupts_enable(port);
5968 mvpp2_port_enable(port);
5969
Marcin Wojtas3f518502014-07-10 16:52:13 -03005970 mvpp2_egress_enable(port);
5971 mvpp2_ingress_enable(port);
Antoine Tenartf55744a2017-08-30 10:29:17 +02005972 netif_carrier_on(dev);
5973 netif_tx_wake_all_queues(dev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005974 } else {
Antoine Tenart968b2112017-08-30 10:29:16 +02005975 port->duplex = -1;
5976 port->speed = 0;
5977
Antoine Tenartf55744a2017-08-30 10:29:17 +02005978 netif_tx_stop_all_queues(dev);
5979 netif_carrier_off(dev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005980 mvpp2_ingress_disable(port);
5981 mvpp2_egress_disable(port);
Antoine Tenartf55744a2017-08-30 10:29:17 +02005982
5983 mvpp2_port_disable(port);
5984 mvpp2_interrupts_disable(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005985 }
Antoine Tenart968b2112017-08-30 10:29:16 +02005986
Marcin Wojtas3f518502014-07-10 16:52:13 -03005987 phy_print_status(phydev);
5988 }
5989}
5990
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005991static void mvpp2_timer_set(struct mvpp2_port_pcpu *port_pcpu)
5992{
5993 ktime_t interval;
5994
5995 if (!port_pcpu->timer_scheduled) {
5996 port_pcpu->timer_scheduled = true;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01005997 interval = MVPP2_TXDONE_HRTIMER_PERIOD_NS;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005998 hrtimer_start(&port_pcpu->tx_done_timer, interval,
5999 HRTIMER_MODE_REL_PINNED);
6000 }
6001}
6002
6003static void mvpp2_tx_proc_cb(unsigned long data)
6004{
6005 struct net_device *dev = (struct net_device *)data;
6006 struct mvpp2_port *port = netdev_priv(dev);
6007 struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
6008 unsigned int tx_todo, cause;
6009
6010 if (!netif_running(dev))
6011 return;
6012 port_pcpu->timer_scheduled = false;
6013
6014 /* Process all the Tx queues */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006015 cause = (1 << port->ntxqs) - 1;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006016 tx_todo = mvpp2_tx_done(port, cause, smp_processor_id());
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006017
6018 /* Set the timer in case not all the packets were processed */
6019 if (tx_todo)
6020 mvpp2_timer_set(port_pcpu);
6021}
6022
6023static enum hrtimer_restart mvpp2_hr_timer_cb(struct hrtimer *timer)
6024{
6025 struct mvpp2_port_pcpu *port_pcpu = container_of(timer,
6026 struct mvpp2_port_pcpu,
6027 tx_done_timer);
6028
6029 tasklet_schedule(&port_pcpu->tx_done_tasklet);
6030
6031 return HRTIMER_NORESTART;
6032}
6033
Marcin Wojtas3f518502014-07-10 16:52:13 -03006034/* Main RX/TX processing routines */
6035
6036/* Display more error info */
6037static void mvpp2_rx_error(struct mvpp2_port *port,
6038 struct mvpp2_rx_desc *rx_desc)
6039{
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006040 u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
6041 size_t sz = mvpp2_rxdesc_size_get(port, rx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006042
6043 switch (status & MVPP2_RXD_ERR_CODE_MASK) {
6044 case MVPP2_RXD_ERR_CRC:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006045 netdev_err(port->dev, "bad rx status %08x (crc error), size=%zu\n",
6046 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006047 break;
6048 case MVPP2_RXD_ERR_OVERRUN:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006049 netdev_err(port->dev, "bad rx status %08x (overrun error), size=%zu\n",
6050 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006051 break;
6052 case MVPP2_RXD_ERR_RESOURCE:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006053 netdev_err(port->dev, "bad rx status %08x (resource error), size=%zu\n",
6054 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006055 break;
6056 }
6057}
6058
6059/* Handle RX checksum offload */
6060static void mvpp2_rx_csum(struct mvpp2_port *port, u32 status,
6061 struct sk_buff *skb)
6062{
6063 if (((status & MVPP2_RXD_L3_IP4) &&
6064 !(status & MVPP2_RXD_IP4_HEADER_ERR)) ||
6065 (status & MVPP2_RXD_L3_IP6))
6066 if (((status & MVPP2_RXD_L4_UDP) ||
6067 (status & MVPP2_RXD_L4_TCP)) &&
6068 (status & MVPP2_RXD_L4_CSUM_OK)) {
6069 skb->csum = 0;
6070 skb->ip_summed = CHECKSUM_UNNECESSARY;
6071 return;
6072 }
6073
6074 skb->ip_summed = CHECKSUM_NONE;
6075}
6076
6077/* Reuse skb if possible, or allocate a new skb and add it to BM pool */
6078static int mvpp2_rx_refill(struct mvpp2_port *port,
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006079 struct mvpp2_bm_pool *bm_pool, int pool)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006080{
Thomas Petazzoni20396132017-03-07 16:53:00 +01006081 dma_addr_t dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01006082 phys_addr_t phys_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006083 void *buf;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006084
Marcin Wojtas3f518502014-07-10 16:52:13 -03006085 /* No recycle or too many buffers are in use, so allocate a new skb */
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01006086 buf = mvpp2_buf_alloc(port, bm_pool, &dma_addr, &phys_addr,
6087 GFP_ATOMIC);
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006088 if (!buf)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006089 return -ENOMEM;
6090
Thomas Petazzoni7d7627b2017-06-22 14:23:20 +02006091 mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
Thomas Petazzoni7ef7e1d2017-02-21 11:28:07 +01006092
Marcin Wojtas3f518502014-07-10 16:52:13 -03006093 return 0;
6094}
6095
6096/* Handle tx checksum */
6097static u32 mvpp2_skb_tx_csum(struct mvpp2_port *port, struct sk_buff *skb)
6098{
6099 if (skb->ip_summed == CHECKSUM_PARTIAL) {
6100 int ip_hdr_len = 0;
6101 u8 l4_proto;
6102
6103 if (skb->protocol == htons(ETH_P_IP)) {
6104 struct iphdr *ip4h = ip_hdr(skb);
6105
6106 /* Calculate IPv4 checksum and L4 checksum */
6107 ip_hdr_len = ip4h->ihl;
6108 l4_proto = ip4h->protocol;
6109 } else if (skb->protocol == htons(ETH_P_IPV6)) {
6110 struct ipv6hdr *ip6h = ipv6_hdr(skb);
6111
6112 /* Read l4_protocol from one of IPv6 extra headers */
6113 if (skb_network_header_len(skb) > 0)
6114 ip_hdr_len = (skb_network_header_len(skb) >> 2);
6115 l4_proto = ip6h->nexthdr;
6116 } else {
6117 return MVPP2_TXD_L4_CSUM_NOT;
6118 }
6119
6120 return mvpp2_txq_desc_csum(skb_network_offset(skb),
6121 skb->protocol, ip_hdr_len, l4_proto);
6122 }
6123
6124 return MVPP2_TXD_L4_CSUM_NOT | MVPP2_TXD_IP_CSUM_DISABLE;
6125}
6126
Marcin Wojtas3f518502014-07-10 16:52:13 -03006127/* Main rx processing */
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006128static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi,
6129 int rx_todo, struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006130{
6131 struct net_device *dev = port->dev;
Marcin Wojtasb5015852015-12-03 15:20:51 +01006132 int rx_received;
6133 int rx_done = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006134 u32 rcvd_pkts = 0;
6135 u32 rcvd_bytes = 0;
6136
6137 /* Get number of received packets and clamp the to-do */
6138 rx_received = mvpp2_rxq_received(port, rxq->id);
6139 if (rx_todo > rx_received)
6140 rx_todo = rx_received;
6141
Marcin Wojtasb5015852015-12-03 15:20:51 +01006142 while (rx_done < rx_todo) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006143 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
6144 struct mvpp2_bm_pool *bm_pool;
6145 struct sk_buff *skb;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006146 unsigned int frag_size;
Thomas Petazzoni20396132017-03-07 16:53:00 +01006147 dma_addr_t dma_addr;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006148 phys_addr_t phys_addr;
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006149 u32 rx_status;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006150 int pool, rx_bytes, err;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006151 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006152
Marcin Wojtasb5015852015-12-03 15:20:51 +01006153 rx_done++;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006154 rx_status = mvpp2_rxdesc_status_get(port, rx_desc);
6155 rx_bytes = mvpp2_rxdesc_size_get(port, rx_desc);
6156 rx_bytes -= MVPP2_MH_SIZE;
6157 dma_addr = mvpp2_rxdesc_dma_addr_get(port, rx_desc);
6158 phys_addr = mvpp2_rxdesc_cookie_get(port, rx_desc);
6159 data = (void *)phys_to_virt(phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006160
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006161 pool = (rx_status & MVPP2_RXD_BM_POOL_ID_MASK) >>
6162 MVPP2_RXD_BM_POOL_ID_OFFS;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006163 bm_pool = &port->priv->bm_pools[pool];
Marcin Wojtas3f518502014-07-10 16:52:13 -03006164
6165 /* In case of an error, release the requested buffer pointer
6166 * to the Buffer Manager. This request process is controlled
6167 * by the hardware, and the information about the buffer is
6168 * comprised by the RX descriptor.
6169 */
6170 if (rx_status & MVPP2_RXD_ERR_SUMMARY) {
Markus Elfring8a524882017-04-17 10:52:02 +02006171err_drop_frame:
Marcin Wojtas3f518502014-07-10 16:52:13 -03006172 dev->stats.rx_errors++;
6173 mvpp2_rx_error(port, rx_desc);
Marcin Wojtasb5015852015-12-03 15:20:51 +01006174 /* Return the buffer to the pool */
Thomas Petazzoni7d7627b2017-06-22 14:23:20 +02006175 mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006176 continue;
6177 }
6178
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006179 if (bm_pool->frag_size > PAGE_SIZE)
6180 frag_size = 0;
6181 else
6182 frag_size = bm_pool->frag_size;
6183
6184 skb = build_skb(data, frag_size);
6185 if (!skb) {
6186 netdev_warn(port->dev, "skb build failed\n");
6187 goto err_drop_frame;
6188 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006189
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02006190 err = mvpp2_rx_refill(port, bm_pool, pool);
Marcin Wojtasb5015852015-12-03 15:20:51 +01006191 if (err) {
6192 netdev_err(port->dev, "failed to refill BM pools\n");
6193 goto err_drop_frame;
6194 }
6195
Thomas Petazzoni20396132017-03-07 16:53:00 +01006196 dma_unmap_single(dev->dev.parent, dma_addr,
Marcin Wojtas4229d502015-12-03 15:20:50 +01006197 bm_pool->buf_size, DMA_FROM_DEVICE);
6198
Marcin Wojtas3f518502014-07-10 16:52:13 -03006199 rcvd_pkts++;
6200 rcvd_bytes += rx_bytes;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006201
Thomas Petazzoni0e037282017-02-21 11:28:12 +01006202 skb_reserve(skb, MVPP2_MH_SIZE + NET_SKB_PAD);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006203 skb_put(skb, rx_bytes);
6204 skb->protocol = eth_type_trans(skb, dev);
6205 mvpp2_rx_csum(port, rx_status, skb);
6206
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006207 napi_gro_receive(napi, skb);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006208 }
6209
6210 if (rcvd_pkts) {
6211 struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
6212
6213 u64_stats_update_begin(&stats->syncp);
6214 stats->rx_packets += rcvd_pkts;
6215 stats->rx_bytes += rcvd_bytes;
6216 u64_stats_update_end(&stats->syncp);
6217 }
6218
6219 /* Update Rx queue management counters */
6220 wmb();
Marcin Wojtasb5015852015-12-03 15:20:51 +01006221 mvpp2_rxq_status_update(port, rxq->id, rx_done, rx_done);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006222
6223 return rx_todo;
6224}
6225
6226static inline void
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006227tx_desc_unmap_put(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006228 struct mvpp2_tx_desc *desc)
6229{
Antoine Tenart20920262017-10-23 15:24:30 +02006230 struct mvpp2_txq_pcpu *txq_pcpu = this_cpu_ptr(txq->pcpu);
6231
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006232 dma_addr_t buf_dma_addr =
6233 mvpp2_txdesc_dma_addr_get(port, desc);
6234 size_t buf_sz =
6235 mvpp2_txdesc_size_get(port, desc);
Antoine Tenart20920262017-10-23 15:24:30 +02006236 if (!IS_TSO_HEADER(txq_pcpu, buf_dma_addr))
6237 dma_unmap_single(port->dev->dev.parent, buf_dma_addr,
6238 buf_sz, DMA_TO_DEVICE);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006239 mvpp2_txq_desc_put(txq);
6240}
6241
6242/* Handle tx fragmentation processing */
6243static int mvpp2_tx_frag_process(struct mvpp2_port *port, struct sk_buff *skb,
6244 struct mvpp2_tx_queue *aggr_txq,
6245 struct mvpp2_tx_queue *txq)
6246{
6247 struct mvpp2_txq_pcpu *txq_pcpu = this_cpu_ptr(txq->pcpu);
6248 struct mvpp2_tx_desc *tx_desc;
6249 int i;
Thomas Petazzoni20396132017-03-07 16:53:00 +01006250 dma_addr_t buf_dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006251
6252 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
6253 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6254 void *addr = page_address(frag->page.p) + frag->page_offset;
6255
6256 tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006257 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6258 mvpp2_txdesc_size_set(port, tx_desc, frag->size);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006259
Thomas Petazzoni20396132017-03-07 16:53:00 +01006260 buf_dma_addr = dma_map_single(port->dev->dev.parent, addr,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006261 frag->size,
6262 DMA_TO_DEVICE);
Thomas Petazzoni20396132017-03-07 16:53:00 +01006263 if (dma_mapping_error(port->dev->dev.parent, buf_dma_addr)) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006264 mvpp2_txq_desc_put(txq);
Markus Elfring32bae632017-04-17 11:36:34 +02006265 goto cleanup;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006266 }
6267
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006268 mvpp2_txdesc_offset_set(port, tx_desc,
6269 buf_dma_addr & MVPP2_TX_DESC_ALIGN);
6270 mvpp2_txdesc_dma_addr_set(port, tx_desc,
6271 buf_dma_addr & ~MVPP2_TX_DESC_ALIGN);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006272
6273 if (i == (skb_shinfo(skb)->nr_frags - 1)) {
6274 /* Last descriptor */
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006275 mvpp2_txdesc_cmd_set(port, tx_desc,
6276 MVPP2_TXD_L_DESC);
6277 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006278 } else {
6279 /* Descriptor in the middle: Not First, Not Last */
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006280 mvpp2_txdesc_cmd_set(port, tx_desc, 0);
6281 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006282 }
6283 }
6284
6285 return 0;
Markus Elfring32bae632017-04-17 11:36:34 +02006286cleanup:
Marcin Wojtas3f518502014-07-10 16:52:13 -03006287 /* Release all descriptors that were used to map fragments of
6288 * this packet, as well as the corresponding DMA mappings
6289 */
6290 for (i = i - 1; i >= 0; i--) {
6291 tx_desc = txq->descs + i;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006292 tx_desc_unmap_put(port, txq, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006293 }
6294
6295 return -ENOMEM;
6296}
6297
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006298static inline void mvpp2_tso_put_hdr(struct sk_buff *skb,
6299 struct net_device *dev,
6300 struct mvpp2_tx_queue *txq,
6301 struct mvpp2_tx_queue *aggr_txq,
6302 struct mvpp2_txq_pcpu *txq_pcpu,
6303 int hdr_sz)
6304{
6305 struct mvpp2_port *port = netdev_priv(dev);
6306 struct mvpp2_tx_desc *tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
6307 dma_addr_t addr;
6308
6309 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6310 mvpp2_txdesc_size_set(port, tx_desc, hdr_sz);
6311
6312 addr = txq_pcpu->tso_headers_dma +
6313 txq_pcpu->txq_put_index * TSO_HEADER_SIZE;
6314 mvpp2_txdesc_offset_set(port, tx_desc, addr & MVPP2_TX_DESC_ALIGN);
6315 mvpp2_txdesc_dma_addr_set(port, tx_desc, addr & ~MVPP2_TX_DESC_ALIGN);
6316
6317 mvpp2_txdesc_cmd_set(port, tx_desc, mvpp2_skb_tx_csum(port, skb) |
6318 MVPP2_TXD_F_DESC |
6319 MVPP2_TXD_PADDING_DISABLE);
6320 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
6321}
6322
6323static inline int mvpp2_tso_put_data(struct sk_buff *skb,
6324 struct net_device *dev, struct tso_t *tso,
6325 struct mvpp2_tx_queue *txq,
6326 struct mvpp2_tx_queue *aggr_txq,
6327 struct mvpp2_txq_pcpu *txq_pcpu,
6328 int sz, bool left, bool last)
6329{
6330 struct mvpp2_port *port = netdev_priv(dev);
6331 struct mvpp2_tx_desc *tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
6332 dma_addr_t buf_dma_addr;
6333
6334 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6335 mvpp2_txdesc_size_set(port, tx_desc, sz);
6336
6337 buf_dma_addr = dma_map_single(dev->dev.parent, tso->data, sz,
6338 DMA_TO_DEVICE);
6339 if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
6340 mvpp2_txq_desc_put(txq);
6341 return -ENOMEM;
6342 }
6343
6344 mvpp2_txdesc_offset_set(port, tx_desc,
6345 buf_dma_addr & MVPP2_TX_DESC_ALIGN);
6346 mvpp2_txdesc_dma_addr_set(port, tx_desc,
6347 buf_dma_addr & ~MVPP2_TX_DESC_ALIGN);
6348
6349 if (!left) {
6350 mvpp2_txdesc_cmd_set(port, tx_desc, MVPP2_TXD_L_DESC);
6351 if (last) {
6352 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
6353 return 0;
6354 }
6355 } else {
6356 mvpp2_txdesc_cmd_set(port, tx_desc, 0);
6357 }
6358
6359 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
6360 return 0;
6361}
6362
6363static int mvpp2_tx_tso(struct sk_buff *skb, struct net_device *dev,
6364 struct mvpp2_tx_queue *txq,
6365 struct mvpp2_tx_queue *aggr_txq,
6366 struct mvpp2_txq_pcpu *txq_pcpu)
6367{
6368 struct mvpp2_port *port = netdev_priv(dev);
6369 struct tso_t tso;
6370 int hdr_sz = skb_transport_offset(skb) + tcp_hdrlen(skb);
6371 int i, len, descs = 0;
6372
6373 /* Check number of available descriptors */
6374 if (mvpp2_aggr_desc_num_check(port->priv, aggr_txq,
6375 tso_count_descs(skb)) ||
6376 mvpp2_txq_reserved_desc_num_proc(port->priv, txq, txq_pcpu,
6377 tso_count_descs(skb)))
6378 return 0;
6379
6380 tso_start(skb, &tso);
6381 len = skb->len - hdr_sz;
6382 while (len > 0) {
6383 int left = min_t(int, skb_shinfo(skb)->gso_size, len);
6384 char *hdr = txq_pcpu->tso_headers +
6385 txq_pcpu->txq_put_index * TSO_HEADER_SIZE;
6386
6387 len -= left;
6388 descs++;
6389
6390 tso_build_hdr(skb, hdr, &tso, left, len == 0);
6391 mvpp2_tso_put_hdr(skb, dev, txq, aggr_txq, txq_pcpu, hdr_sz);
6392
6393 while (left > 0) {
6394 int sz = min_t(int, tso.size, left);
6395 left -= sz;
6396 descs++;
6397
6398 if (mvpp2_tso_put_data(skb, dev, &tso, txq, aggr_txq,
6399 txq_pcpu, sz, left, len == 0))
6400 goto release;
6401 tso_build_data(skb, &tso, sz);
6402 }
6403 }
6404
6405 return descs;
6406
6407release:
6408 for (i = descs - 1; i >= 0; i--) {
6409 struct mvpp2_tx_desc *tx_desc = txq->descs + i;
6410 tx_desc_unmap_put(port, txq, tx_desc);
6411 }
6412 return 0;
6413}
6414
Marcin Wojtas3f518502014-07-10 16:52:13 -03006415/* Main tx processing */
6416static int mvpp2_tx(struct sk_buff *skb, struct net_device *dev)
6417{
6418 struct mvpp2_port *port = netdev_priv(dev);
6419 struct mvpp2_tx_queue *txq, *aggr_txq;
6420 struct mvpp2_txq_pcpu *txq_pcpu;
6421 struct mvpp2_tx_desc *tx_desc;
Thomas Petazzoni20396132017-03-07 16:53:00 +01006422 dma_addr_t buf_dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006423 int frags = 0;
6424 u16 txq_id;
6425 u32 tx_cmd;
6426
6427 txq_id = skb_get_queue_mapping(skb);
6428 txq = port->txqs[txq_id];
6429 txq_pcpu = this_cpu_ptr(txq->pcpu);
6430 aggr_txq = &port->priv->aggr_txqs[smp_processor_id()];
6431
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006432 if (skb_is_gso(skb)) {
6433 frags = mvpp2_tx_tso(skb, dev, txq, aggr_txq, txq_pcpu);
6434 goto out;
6435 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006436 frags = skb_shinfo(skb)->nr_frags + 1;
6437
6438 /* Check number of available descriptors */
6439 if (mvpp2_aggr_desc_num_check(port->priv, aggr_txq, frags) ||
6440 mvpp2_txq_reserved_desc_num_proc(port->priv, txq,
6441 txq_pcpu, frags)) {
6442 frags = 0;
6443 goto out;
6444 }
6445
6446 /* Get a descriptor for the first part of the packet */
6447 tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006448 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6449 mvpp2_txdesc_size_set(port, tx_desc, skb_headlen(skb));
Marcin Wojtas3f518502014-07-10 16:52:13 -03006450
Thomas Petazzoni20396132017-03-07 16:53:00 +01006451 buf_dma_addr = dma_map_single(dev->dev.parent, skb->data,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006452 skb_headlen(skb), DMA_TO_DEVICE);
Thomas Petazzoni20396132017-03-07 16:53:00 +01006453 if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006454 mvpp2_txq_desc_put(txq);
6455 frags = 0;
6456 goto out;
6457 }
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006458
6459 mvpp2_txdesc_offset_set(port, tx_desc,
6460 buf_dma_addr & MVPP2_TX_DESC_ALIGN);
6461 mvpp2_txdesc_dma_addr_set(port, tx_desc,
6462 buf_dma_addr & ~MVPP2_TX_DESC_ALIGN);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006463
6464 tx_cmd = mvpp2_skb_tx_csum(port, skb);
6465
6466 if (frags == 1) {
6467 /* First and Last descriptor */
6468 tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_L_DESC;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006469 mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
6470 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006471 } else {
6472 /* First but not Last */
6473 tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_PADDING_DISABLE;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006474 mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
6475 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006476
6477 /* Continue with other skb fragments */
6478 if (mvpp2_tx_frag_process(port, skb, aggr_txq, txq)) {
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01006479 tx_desc_unmap_put(port, txq, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006480 frags = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006481 }
6482 }
6483
Marcin Wojtas3f518502014-07-10 16:52:13 -03006484out:
6485 if (frags > 0) {
6486 struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
Antoine Ténart186cd4d2017-08-23 09:46:56 +02006487 struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
6488
6489 txq_pcpu->reserved_num -= frags;
6490 txq_pcpu->count += frags;
6491 aggr_txq->count += frags;
6492
6493 /* Enable transmit */
6494 wmb();
6495 mvpp2_aggr_txq_pend_desc_add(port, frags);
6496
6497 if (txq_pcpu->size - txq_pcpu->count < MAX_SKB_FRAGS + 1)
6498 netif_tx_stop_queue(nq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006499
6500 u64_stats_update_begin(&stats->syncp);
6501 stats->tx_packets++;
6502 stats->tx_bytes += skb->len;
6503 u64_stats_update_end(&stats->syncp);
6504 } else {
6505 dev->stats.tx_dropped++;
6506 dev_kfree_skb_any(skb);
6507 }
6508
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006509 /* Finalize TX processing */
Antoine Tenart082297e2017-10-23 15:24:31 +02006510 if (!port->has_tx_irqs && txq_pcpu->count >= txq->done_pkts_coal)
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006511 mvpp2_txq_done(port, txq, txq_pcpu);
6512
6513 /* Set the timer in case not all frags were processed */
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006514 if (!port->has_tx_irqs && txq_pcpu->count <= frags &&
6515 txq_pcpu->count > 0) {
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006516 struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
6517
6518 mvpp2_timer_set(port_pcpu);
6519 }
6520
Marcin Wojtas3f518502014-07-10 16:52:13 -03006521 return NETDEV_TX_OK;
6522}
6523
6524static inline void mvpp2_cause_error(struct net_device *dev, int cause)
6525{
6526 if (cause & MVPP2_CAUSE_FCS_ERR_MASK)
6527 netdev_err(dev, "FCS error\n");
6528 if (cause & MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK)
6529 netdev_err(dev, "rx fifo overrun error\n");
6530 if (cause & MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK)
6531 netdev_err(dev, "tx fifo underrun error\n");
6532}
6533
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006534static int mvpp2_poll(struct napi_struct *napi, int budget)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006535{
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006536 u32 cause_rx_tx, cause_rx, cause_tx, cause_misc;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006537 int rx_done = 0;
6538 struct mvpp2_port *port = netdev_priv(napi->dev);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006539 struct mvpp2_queue_vector *qv;
Thomas Petazzonia7868412017-03-07 16:53:13 +01006540 int cpu = smp_processor_id();
Marcin Wojtas3f518502014-07-10 16:52:13 -03006541
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006542 qv = container_of(napi, struct mvpp2_queue_vector, napi);
6543
Marcin Wojtas3f518502014-07-10 16:52:13 -03006544 /* Rx/Tx cause register
6545 *
6546 * Bits 0-15: each bit indicates received packets on the Rx queue
6547 * (bit 0 is for Rx queue 0).
6548 *
6549 * Bits 16-23: each bit indicates transmitted packets on the Tx queue
6550 * (bit 16 is for Tx queue 0).
6551 *
6552 * Each CPU has its own Rx/Tx cause register
6553 */
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006554 cause_rx_tx = mvpp2_percpu_read(port->priv, qv->sw_thread_id,
Thomas Petazzonia7868412017-03-07 16:53:13 +01006555 MVPP2_ISR_RX_TX_CAUSE_REG(port->id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03006556
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006557 cause_misc = cause_rx_tx & MVPP2_CAUSE_MISC_SUM_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006558 if (cause_misc) {
6559 mvpp2_cause_error(port->dev, cause_misc);
6560
6561 /* Clear the cause register */
6562 mvpp2_write(port->priv, MVPP2_ISR_MISC_CAUSE_REG, 0);
Thomas Petazzonia7868412017-03-07 16:53:13 +01006563 mvpp2_percpu_write(port->priv, cpu,
6564 MVPP2_ISR_RX_TX_CAUSE_REG(port->id),
6565 cause_rx_tx & ~MVPP2_CAUSE_MISC_SUM_MASK);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006566 }
6567
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006568 cause_tx = cause_rx_tx & MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
6569 if (cause_tx) {
6570 cause_tx >>= MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_OFFSET;
6571 mvpp2_tx_done(port, cause_tx, qv->sw_thread_id);
6572 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006573
6574 /* Process RX packets */
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006575 cause_rx = cause_rx_tx & MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
6576 cause_rx <<= qv->first_rxq;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006577 cause_rx |= qv->pending_cause_rx;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006578 while (cause_rx && budget > 0) {
6579 int count;
6580 struct mvpp2_rx_queue *rxq;
6581
6582 rxq = mvpp2_get_rx_queue(port, cause_rx);
6583 if (!rxq)
6584 break;
6585
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006586 count = mvpp2_rx(port, napi, budget, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006587 rx_done += count;
6588 budget -= count;
6589 if (budget > 0) {
6590 /* Clear the bit associated to this Rx queue
6591 * so that next iteration will continue from
6592 * the next Rx queue.
6593 */
6594 cause_rx &= ~(1 << rxq->logic_rxq);
6595 }
6596 }
6597
6598 if (budget > 0) {
6599 cause_rx = 0;
Eric Dumazet6ad20162017-01-30 08:22:01 -08006600 napi_complete_done(napi, rx_done);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006601
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006602 mvpp2_qvec_interrupt_enable(qv);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006603 }
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006604 qv->pending_cause_rx = cause_rx;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006605 return rx_done;
6606}
6607
6608/* Set hw internals when starting port */
6609static void mvpp2_start_dev(struct mvpp2_port *port)
6610{
Philippe Reynes8e072692016-06-28 00:08:11 +02006611 struct net_device *ndev = port->dev;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006612 int i;
Philippe Reynes8e072692016-06-28 00:08:11 +02006613
Stefan Chulski76eb1b12017-08-22 19:08:26 +02006614 if (port->gop_id == 0 &&
6615 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
6616 port->phy_interface == PHY_INTERFACE_MODE_10GKR))
6617 mvpp2_xlg_max_rx_size_set(port);
6618 else
6619 mvpp2_gmac_max_rx_size_set(port);
6620
Marcin Wojtas3f518502014-07-10 16:52:13 -03006621 mvpp2_txp_max_tx_size_set(port);
6622
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006623 for (i = 0; i < port->nqvecs; i++)
6624 napi_enable(&port->qvecs[i].napi);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006625
6626 /* Enable interrupts on all CPUs */
6627 mvpp2_interrupts_enable(port);
6628
Antoine Tenart542897d2017-08-30 10:29:15 +02006629 if (port->priv->hw_version == MVPP22) {
6630 mvpp22_comphy_init(port);
Antoine Ténartf84bf382017-08-22 19:08:27 +02006631 mvpp22_gop_init(port);
Antoine Tenart542897d2017-08-30 10:29:15 +02006632 }
Antoine Ténartf84bf382017-08-22 19:08:27 +02006633
Antoine Ténart2055d622017-08-22 19:08:23 +02006634 mvpp2_port_mii_set(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006635 mvpp2_port_enable(port);
Antoine Tenart5997c862017-09-01 11:04:53 +02006636 if (ndev->phydev)
6637 phy_start(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006638 netif_tx_start_all_queues(port->dev);
6639}
6640
6641/* Set hw internals when stopping port */
6642static void mvpp2_stop_dev(struct mvpp2_port *port)
6643{
Philippe Reynes8e072692016-06-28 00:08:11 +02006644 struct net_device *ndev = port->dev;
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006645 int i;
Philippe Reynes8e072692016-06-28 00:08:11 +02006646
Marcin Wojtas3f518502014-07-10 16:52:13 -03006647 /* Stop new packets from arriving to RXQs */
6648 mvpp2_ingress_disable(port);
6649
6650 mdelay(10);
6651
6652 /* Disable interrupts on all CPUs */
6653 mvpp2_interrupts_disable(port);
6654
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006655 for (i = 0; i < port->nqvecs; i++)
6656 napi_disable(&port->qvecs[i].napi);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006657
6658 netif_carrier_off(port->dev);
6659 netif_tx_stop_all_queues(port->dev);
6660
6661 mvpp2_egress_disable(port);
6662 mvpp2_port_disable(port);
Antoine Tenart5997c862017-09-01 11:04:53 +02006663 if (ndev->phydev)
6664 phy_stop(ndev->phydev);
Antoine Tenart542897d2017-08-30 10:29:15 +02006665 phy_power_off(port->comphy);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006666}
6667
Marcin Wojtas3f518502014-07-10 16:52:13 -03006668static int mvpp2_check_ringparam_valid(struct net_device *dev,
6669 struct ethtool_ringparam *ring)
6670{
6671 u16 new_rx_pending = ring->rx_pending;
6672 u16 new_tx_pending = ring->tx_pending;
6673
6674 if (ring->rx_pending == 0 || ring->tx_pending == 0)
6675 return -EINVAL;
6676
6677 if (ring->rx_pending > MVPP2_MAX_RXD)
6678 new_rx_pending = MVPP2_MAX_RXD;
6679 else if (!IS_ALIGNED(ring->rx_pending, 16))
6680 new_rx_pending = ALIGN(ring->rx_pending, 16);
6681
6682 if (ring->tx_pending > MVPP2_MAX_TXD)
6683 new_tx_pending = MVPP2_MAX_TXD;
6684 else if (!IS_ALIGNED(ring->tx_pending, 32))
6685 new_tx_pending = ALIGN(ring->tx_pending, 32);
6686
6687 if (ring->rx_pending != new_rx_pending) {
6688 netdev_info(dev, "illegal Rx ring size value %d, round to %d\n",
6689 ring->rx_pending, new_rx_pending);
6690 ring->rx_pending = new_rx_pending;
6691 }
6692
6693 if (ring->tx_pending != new_tx_pending) {
6694 netdev_info(dev, "illegal Tx ring size value %d, round to %d\n",
6695 ring->tx_pending, new_tx_pending);
6696 ring->tx_pending = new_tx_pending;
6697 }
6698
6699 return 0;
6700}
6701
Thomas Petazzoni26975822017-03-07 16:53:14 +01006702static void mvpp21_get_mac_address(struct mvpp2_port *port, unsigned char *addr)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006703{
6704 u32 mac_addr_l, mac_addr_m, mac_addr_h;
6705
6706 mac_addr_l = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
6707 mac_addr_m = readl(port->priv->lms_base + MVPP2_SRC_ADDR_MIDDLE);
6708 mac_addr_h = readl(port->priv->lms_base + MVPP2_SRC_ADDR_HIGH);
6709 addr[0] = (mac_addr_h >> 24) & 0xFF;
6710 addr[1] = (mac_addr_h >> 16) & 0xFF;
6711 addr[2] = (mac_addr_h >> 8) & 0xFF;
6712 addr[3] = mac_addr_h & 0xFF;
6713 addr[4] = mac_addr_m & 0xFF;
6714 addr[5] = (mac_addr_l >> MVPP2_GMAC_SA_LOW_OFFS) & 0xFF;
6715}
6716
6717static int mvpp2_phy_connect(struct mvpp2_port *port)
6718{
6719 struct phy_device *phy_dev;
6720
Antoine Tenart5997c862017-09-01 11:04:53 +02006721 /* No PHY is attached */
6722 if (!port->phy_node)
6723 return 0;
6724
Marcin Wojtas3f518502014-07-10 16:52:13 -03006725 phy_dev = of_phy_connect(port->dev, port->phy_node, mvpp2_link_event, 0,
6726 port->phy_interface);
6727 if (!phy_dev) {
6728 netdev_err(port->dev, "cannot connect to phy\n");
6729 return -ENODEV;
6730 }
6731 phy_dev->supported &= PHY_GBIT_FEATURES;
6732 phy_dev->advertising = phy_dev->supported;
6733
Marcin Wojtas3f518502014-07-10 16:52:13 -03006734 port->link = 0;
6735 port->duplex = 0;
6736 port->speed = 0;
6737
6738 return 0;
6739}
6740
6741static void mvpp2_phy_disconnect(struct mvpp2_port *port)
6742{
Philippe Reynes8e072692016-06-28 00:08:11 +02006743 struct net_device *ndev = port->dev;
6744
Antoine Tenart5997c862017-09-01 11:04:53 +02006745 if (!ndev->phydev)
6746 return;
6747
Philippe Reynes8e072692016-06-28 00:08:11 +02006748 phy_disconnect(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006749}
6750
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006751static int mvpp2_irqs_init(struct mvpp2_port *port)
6752{
6753 int err, i;
6754
6755 for (i = 0; i < port->nqvecs; i++) {
6756 struct mvpp2_queue_vector *qv = port->qvecs + i;
6757
6758 err = request_irq(qv->irq, mvpp2_isr, 0, port->dev->name, qv);
6759 if (err)
6760 goto err;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006761
6762 if (qv->type == MVPP2_QUEUE_VECTOR_PRIVATE)
6763 irq_set_affinity_hint(qv->irq,
6764 cpumask_of(qv->sw_thread_id));
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006765 }
6766
6767 return 0;
6768err:
6769 for (i = 0; i < port->nqvecs; i++) {
6770 struct mvpp2_queue_vector *qv = port->qvecs + i;
6771
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006772 irq_set_affinity_hint(qv->irq, NULL);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006773 free_irq(qv->irq, qv);
6774 }
6775
6776 return err;
6777}
6778
6779static void mvpp2_irqs_deinit(struct mvpp2_port *port)
6780{
6781 int i;
6782
6783 for (i = 0; i < port->nqvecs; i++) {
6784 struct mvpp2_queue_vector *qv = port->qvecs + i;
6785
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006786 irq_set_affinity_hint(qv->irq, NULL);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006787 free_irq(qv->irq, qv);
6788 }
6789}
6790
Marcin Wojtas3f518502014-07-10 16:52:13 -03006791static int mvpp2_open(struct net_device *dev)
6792{
6793 struct mvpp2_port *port = netdev_priv(dev);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02006794 struct mvpp2 *priv = port->priv;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006795 unsigned char mac_bcast[ETH_ALEN] = {
6796 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
6797 int err;
6798
6799 err = mvpp2_prs_mac_da_accept(port->priv, port->id, mac_bcast, true);
6800 if (err) {
6801 netdev_err(dev, "mvpp2_prs_mac_da_accept BC failed\n");
6802 return err;
6803 }
6804 err = mvpp2_prs_mac_da_accept(port->priv, port->id,
6805 dev->dev_addr, true);
6806 if (err) {
6807 netdev_err(dev, "mvpp2_prs_mac_da_accept MC failed\n");
6808 return err;
6809 }
6810 err = mvpp2_prs_tag_mode_set(port->priv, port->id, MVPP2_TAG_TYPE_MH);
6811 if (err) {
6812 netdev_err(dev, "mvpp2_prs_tag_mode_set failed\n");
6813 return err;
6814 }
6815 err = mvpp2_prs_def_flow(port);
6816 if (err) {
6817 netdev_err(dev, "mvpp2_prs_def_flow failed\n");
6818 return err;
6819 }
6820
6821 /* Allocate the Rx/Tx queues */
6822 err = mvpp2_setup_rxqs(port);
6823 if (err) {
6824 netdev_err(port->dev, "cannot allocate Rx queues\n");
6825 return err;
6826 }
6827
6828 err = mvpp2_setup_txqs(port);
6829 if (err) {
6830 netdev_err(port->dev, "cannot allocate Tx queues\n");
6831 goto err_cleanup_rxqs;
6832 }
6833
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006834 err = mvpp2_irqs_init(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006835 if (err) {
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006836 netdev_err(port->dev, "cannot init IRQs\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03006837 goto err_cleanup_txqs;
6838 }
6839
Antoine Tenartfd3651b2017-09-01 11:04:54 +02006840 if (priv->hw_version == MVPP22 && !port->phy_node && port->link_irq) {
6841 err = request_irq(port->link_irq, mvpp2_link_status_isr, 0,
6842 dev->name, port);
6843 if (err) {
6844 netdev_err(port->dev, "cannot request link IRQ %d\n",
6845 port->link_irq);
6846 goto err_free_irq;
6847 }
6848
6849 mvpp22_gop_setup_irq(port);
6850 }
6851
Marcin Wojtas3f518502014-07-10 16:52:13 -03006852 /* In default link is down */
6853 netif_carrier_off(port->dev);
6854
6855 err = mvpp2_phy_connect(port);
6856 if (err < 0)
Antoine Tenartfd3651b2017-09-01 11:04:54 +02006857 goto err_free_link_irq;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006858
6859 /* Unmask interrupts on all CPUs */
6860 on_each_cpu(mvpp2_interrupts_unmask, port, 1);
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006861 mvpp2_shared_interrupt_mask_unmask(port, false);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006862
6863 mvpp2_start_dev(port);
6864
6865 return 0;
6866
Antoine Tenartfd3651b2017-09-01 11:04:54 +02006867err_free_link_irq:
6868 if (priv->hw_version == MVPP22 && !port->phy_node && port->link_irq)
6869 free_irq(port->link_irq, port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006870err_free_irq:
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006871 mvpp2_irqs_deinit(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006872err_cleanup_txqs:
6873 mvpp2_cleanup_txqs(port);
6874err_cleanup_rxqs:
6875 mvpp2_cleanup_rxqs(port);
6876 return err;
6877}
6878
6879static int mvpp2_stop(struct net_device *dev)
6880{
6881 struct mvpp2_port *port = netdev_priv(dev);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006882 struct mvpp2_port_pcpu *port_pcpu;
Antoine Tenartfd3651b2017-09-01 11:04:54 +02006883 struct mvpp2 *priv = port->priv;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006884 int cpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006885
6886 mvpp2_stop_dev(port);
6887 mvpp2_phy_disconnect(port);
6888
6889 /* Mask interrupts on all CPUs */
6890 on_each_cpu(mvpp2_interrupts_mask, port, 1);
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006891 mvpp2_shared_interrupt_mask_unmask(port, true);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006892
Antoine Tenartfd3651b2017-09-01 11:04:54 +02006893 if (priv->hw_version == MVPP22 && !port->phy_node && port->link_irq)
6894 free_irq(port->link_irq, port);
6895
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02006896 mvpp2_irqs_deinit(port);
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006897 if (!port->has_tx_irqs) {
6898 for_each_present_cpu(cpu) {
6899 port_pcpu = per_cpu_ptr(port->pcpu, cpu);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006900
Thomas Petazzoni213f4282017-08-03 10:42:00 +02006901 hrtimer_cancel(&port_pcpu->tx_done_timer);
6902 port_pcpu->timer_scheduled = false;
6903 tasklet_kill(&port_pcpu->tx_done_tasklet);
6904 }
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006905 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006906 mvpp2_cleanup_rxqs(port);
6907 mvpp2_cleanup_txqs(port);
6908
6909 return 0;
6910}
6911
6912static void mvpp2_set_rx_mode(struct net_device *dev)
6913{
6914 struct mvpp2_port *port = netdev_priv(dev);
6915 struct mvpp2 *priv = port->priv;
6916 struct netdev_hw_addr *ha;
6917 int id = port->id;
6918 bool allmulti = dev->flags & IFF_ALLMULTI;
6919
6920 mvpp2_prs_mac_promisc_set(priv, id, dev->flags & IFF_PROMISC);
6921 mvpp2_prs_mac_multi_set(priv, id, MVPP2_PE_MAC_MC_ALL, allmulti);
6922 mvpp2_prs_mac_multi_set(priv, id, MVPP2_PE_MAC_MC_IP6, allmulti);
6923
6924 /* Remove all port->id's mcast enries */
6925 mvpp2_prs_mcast_del_all(priv, id);
6926
6927 if (allmulti && !netdev_mc_empty(dev)) {
6928 netdev_for_each_mc_addr(ha, dev)
6929 mvpp2_prs_mac_da_accept(priv, id, ha->addr, true);
6930 }
6931}
6932
6933static int mvpp2_set_mac_address(struct net_device *dev, void *p)
6934{
6935 struct mvpp2_port *port = netdev_priv(dev);
6936 const struct sockaddr *addr = p;
6937 int err;
6938
6939 if (!is_valid_ether_addr(addr->sa_data)) {
6940 err = -EADDRNOTAVAIL;
Markus Elfringc1175542017-04-17 11:10:47 +02006941 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006942 }
6943
6944 if (!netif_running(dev)) {
6945 err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
6946 if (!err)
6947 return 0;
6948 /* Reconfigure parser to accept the original MAC address */
6949 err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
6950 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02006951 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006952 }
6953
6954 mvpp2_stop_dev(port);
6955
6956 err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
6957 if (!err)
6958 goto out_start;
6959
6960 /* Reconfigure parser accept the original MAC address */
6961 err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
6962 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02006963 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006964out_start:
6965 mvpp2_start_dev(port);
6966 mvpp2_egress_enable(port);
6967 mvpp2_ingress_enable(port);
6968 return 0;
Markus Elfringc1175542017-04-17 11:10:47 +02006969log_error:
Markus Elfringdfd42402017-04-17 11:20:41 +02006970 netdev_err(dev, "failed to change MAC address\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03006971 return err;
6972}
6973
6974static int mvpp2_change_mtu(struct net_device *dev, int mtu)
6975{
6976 struct mvpp2_port *port = netdev_priv(dev);
6977 int err;
6978
Jarod Wilson57779872016-10-17 15:54:06 -04006979 if (!IS_ALIGNED(MVPP2_RX_PKT_SIZE(mtu), 8)) {
6980 netdev_info(dev, "illegal MTU value %d, round to %d\n", mtu,
6981 ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8));
6982 mtu = ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006983 }
6984
6985 if (!netif_running(dev)) {
6986 err = mvpp2_bm_update_mtu(dev, mtu);
6987 if (!err) {
6988 port->pkt_size = MVPP2_RX_PKT_SIZE(mtu);
6989 return 0;
6990 }
6991
6992 /* Reconfigure BM to the original MTU */
6993 err = mvpp2_bm_update_mtu(dev, dev->mtu);
6994 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02006995 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006996 }
6997
6998 mvpp2_stop_dev(port);
6999
7000 err = mvpp2_bm_update_mtu(dev, mtu);
7001 if (!err) {
7002 port->pkt_size = MVPP2_RX_PKT_SIZE(mtu);
7003 goto out_start;
7004 }
7005
7006 /* Reconfigure BM to the original MTU */
7007 err = mvpp2_bm_update_mtu(dev, dev->mtu);
7008 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02007009 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007010
7011out_start:
7012 mvpp2_start_dev(port);
7013 mvpp2_egress_enable(port);
7014 mvpp2_ingress_enable(port);
7015
7016 return 0;
Markus Elfringc1175542017-04-17 11:10:47 +02007017log_error:
Markus Elfringdfd42402017-04-17 11:20:41 +02007018 netdev_err(dev, "failed to change MTU\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03007019 return err;
7020}
7021
stephen hemmingerbc1f4472017-01-06 19:12:52 -08007022static void
Marcin Wojtas3f518502014-07-10 16:52:13 -03007023mvpp2_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
7024{
7025 struct mvpp2_port *port = netdev_priv(dev);
7026 unsigned int start;
7027 int cpu;
7028
7029 for_each_possible_cpu(cpu) {
7030 struct mvpp2_pcpu_stats *cpu_stats;
7031 u64 rx_packets;
7032 u64 rx_bytes;
7033 u64 tx_packets;
7034 u64 tx_bytes;
7035
7036 cpu_stats = per_cpu_ptr(port->stats, cpu);
7037 do {
7038 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
7039 rx_packets = cpu_stats->rx_packets;
7040 rx_bytes = cpu_stats->rx_bytes;
7041 tx_packets = cpu_stats->tx_packets;
7042 tx_bytes = cpu_stats->tx_bytes;
7043 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
7044
7045 stats->rx_packets += rx_packets;
7046 stats->rx_bytes += rx_bytes;
7047 stats->tx_packets += tx_packets;
7048 stats->tx_bytes += tx_bytes;
7049 }
7050
7051 stats->rx_errors = dev->stats.rx_errors;
7052 stats->rx_dropped = dev->stats.rx_dropped;
7053 stats->tx_dropped = dev->stats.tx_dropped;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007054}
7055
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007056static int mvpp2_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
7057{
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007058 int ret;
7059
Philippe Reynes8e072692016-06-28 00:08:11 +02007060 if (!dev->phydev)
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007061 return -ENOTSUPP;
7062
Philippe Reynes8e072692016-06-28 00:08:11 +02007063 ret = phy_mii_ioctl(dev->phydev, ifr, cmd);
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007064 if (!ret)
7065 mvpp2_link_event(dev);
7066
7067 return ret;
7068}
7069
Marcin Wojtas3f518502014-07-10 16:52:13 -03007070/* Ethtool methods */
7071
Marcin Wojtas3f518502014-07-10 16:52:13 -03007072/* Set interrupt coalescing for ethtools */
7073static int mvpp2_ethtool_set_coalesce(struct net_device *dev,
7074 struct ethtool_coalesce *c)
7075{
7076 struct mvpp2_port *port = netdev_priv(dev);
7077 int queue;
7078
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007079 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007080 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
7081
7082 rxq->time_coal = c->rx_coalesce_usecs;
7083 rxq->pkts_coal = c->rx_max_coalesced_frames;
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01007084 mvpp2_rx_pkts_coal_set(port, rxq);
7085 mvpp2_rx_time_coal_set(port, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007086 }
7087
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007088 if (port->has_tx_irqs) {
7089 port->tx_time_coal = c->tx_coalesce_usecs;
7090 mvpp2_tx_time_coal_set(port);
7091 }
7092
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007093 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007094 struct mvpp2_tx_queue *txq = port->txqs[queue];
7095
7096 txq->done_pkts_coal = c->tx_max_coalesced_frames;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007097
7098 if (port->has_tx_irqs)
7099 mvpp2_tx_pkts_coal_set(port, txq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007100 }
7101
Marcin Wojtas3f518502014-07-10 16:52:13 -03007102 return 0;
7103}
7104
7105/* get coalescing for ethtools */
7106static int mvpp2_ethtool_get_coalesce(struct net_device *dev,
7107 struct ethtool_coalesce *c)
7108{
7109 struct mvpp2_port *port = netdev_priv(dev);
7110
7111 c->rx_coalesce_usecs = port->rxqs[0]->time_coal;
7112 c->rx_max_coalesced_frames = port->rxqs[0]->pkts_coal;
7113 c->tx_max_coalesced_frames = port->txqs[0]->done_pkts_coal;
7114 return 0;
7115}
7116
7117static void mvpp2_ethtool_get_drvinfo(struct net_device *dev,
7118 struct ethtool_drvinfo *drvinfo)
7119{
7120 strlcpy(drvinfo->driver, MVPP2_DRIVER_NAME,
7121 sizeof(drvinfo->driver));
7122 strlcpy(drvinfo->version, MVPP2_DRIVER_VERSION,
7123 sizeof(drvinfo->version));
7124 strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
7125 sizeof(drvinfo->bus_info));
7126}
7127
7128static void mvpp2_ethtool_get_ringparam(struct net_device *dev,
7129 struct ethtool_ringparam *ring)
7130{
7131 struct mvpp2_port *port = netdev_priv(dev);
7132
7133 ring->rx_max_pending = MVPP2_MAX_RXD;
7134 ring->tx_max_pending = MVPP2_MAX_TXD;
7135 ring->rx_pending = port->rx_ring_size;
7136 ring->tx_pending = port->tx_ring_size;
7137}
7138
7139static int mvpp2_ethtool_set_ringparam(struct net_device *dev,
7140 struct ethtool_ringparam *ring)
7141{
7142 struct mvpp2_port *port = netdev_priv(dev);
7143 u16 prev_rx_ring_size = port->rx_ring_size;
7144 u16 prev_tx_ring_size = port->tx_ring_size;
7145 int err;
7146
7147 err = mvpp2_check_ringparam_valid(dev, ring);
7148 if (err)
7149 return err;
7150
7151 if (!netif_running(dev)) {
7152 port->rx_ring_size = ring->rx_pending;
7153 port->tx_ring_size = ring->tx_pending;
7154 return 0;
7155 }
7156
7157 /* The interface is running, so we have to force a
7158 * reallocation of the queues
7159 */
7160 mvpp2_stop_dev(port);
7161 mvpp2_cleanup_rxqs(port);
7162 mvpp2_cleanup_txqs(port);
7163
7164 port->rx_ring_size = ring->rx_pending;
7165 port->tx_ring_size = ring->tx_pending;
7166
7167 err = mvpp2_setup_rxqs(port);
7168 if (err) {
7169 /* Reallocate Rx queues with the original ring size */
7170 port->rx_ring_size = prev_rx_ring_size;
7171 ring->rx_pending = prev_rx_ring_size;
7172 err = mvpp2_setup_rxqs(port);
7173 if (err)
7174 goto err_out;
7175 }
7176 err = mvpp2_setup_txqs(port);
7177 if (err) {
7178 /* Reallocate Tx queues with the original ring size */
7179 port->tx_ring_size = prev_tx_ring_size;
7180 ring->tx_pending = prev_tx_ring_size;
7181 err = mvpp2_setup_txqs(port);
7182 if (err)
7183 goto err_clean_rxqs;
7184 }
7185
7186 mvpp2_start_dev(port);
7187 mvpp2_egress_enable(port);
7188 mvpp2_ingress_enable(port);
7189
7190 return 0;
7191
7192err_clean_rxqs:
7193 mvpp2_cleanup_rxqs(port);
7194err_out:
Markus Elfringdfd42402017-04-17 11:20:41 +02007195 netdev_err(dev, "failed to change ring parameters");
Marcin Wojtas3f518502014-07-10 16:52:13 -03007196 return err;
7197}
7198
7199/* Device ops */
7200
7201static const struct net_device_ops mvpp2_netdev_ops = {
7202 .ndo_open = mvpp2_open,
7203 .ndo_stop = mvpp2_stop,
7204 .ndo_start_xmit = mvpp2_tx,
7205 .ndo_set_rx_mode = mvpp2_set_rx_mode,
7206 .ndo_set_mac_address = mvpp2_set_mac_address,
7207 .ndo_change_mtu = mvpp2_change_mtu,
7208 .ndo_get_stats64 = mvpp2_get_stats64,
Thomas Petazzonibd695a52014-07-27 23:21:36 +02007209 .ndo_do_ioctl = mvpp2_ioctl,
Marcin Wojtas3f518502014-07-10 16:52:13 -03007210};
7211
7212static const struct ethtool_ops mvpp2_eth_tool_ops = {
Florian Fainelli00606c42016-11-15 11:19:48 -08007213 .nway_reset = phy_ethtool_nway_reset,
Marcin Wojtas3f518502014-07-10 16:52:13 -03007214 .get_link = ethtool_op_get_link,
Marcin Wojtas3f518502014-07-10 16:52:13 -03007215 .set_coalesce = mvpp2_ethtool_set_coalesce,
7216 .get_coalesce = mvpp2_ethtool_get_coalesce,
7217 .get_drvinfo = mvpp2_ethtool_get_drvinfo,
7218 .get_ringparam = mvpp2_ethtool_get_ringparam,
7219 .set_ringparam = mvpp2_ethtool_set_ringparam,
Philippe Reynesfb773e92016-06-28 00:08:12 +02007220 .get_link_ksettings = phy_ethtool_get_link_ksettings,
7221 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Marcin Wojtas3f518502014-07-10 16:52:13 -03007222};
7223
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007224/* Used for PPv2.1, or PPv2.2 with the old Device Tree binding that
7225 * had a single IRQ defined per-port.
7226 */
7227static int mvpp2_simple_queue_vectors_init(struct mvpp2_port *port,
7228 struct device_node *port_node)
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007229{
7230 struct mvpp2_queue_vector *v = &port->qvecs[0];
7231
7232 v->first_rxq = 0;
7233 v->nrxqs = port->nrxqs;
7234 v->type = MVPP2_QUEUE_VECTOR_SHARED;
7235 v->sw_thread_id = 0;
7236 v->sw_thread_mask = *cpumask_bits(cpu_online_mask);
7237 v->port = port;
7238 v->irq = irq_of_parse_and_map(port_node, 0);
7239 if (v->irq <= 0)
7240 return -EINVAL;
7241 netif_napi_add(port->dev, &v->napi, mvpp2_poll,
7242 NAPI_POLL_WEIGHT);
7243
7244 port->nqvecs = 1;
7245
7246 return 0;
7247}
7248
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007249static int mvpp2_multi_queue_vectors_init(struct mvpp2_port *port,
7250 struct device_node *port_node)
7251{
7252 struct mvpp2_queue_vector *v;
7253 int i, ret;
7254
7255 port->nqvecs = num_possible_cpus();
7256 if (queue_mode == MVPP2_QDIST_SINGLE_MODE)
7257 port->nqvecs += 1;
7258
7259 for (i = 0; i < port->nqvecs; i++) {
7260 char irqname[16];
7261
7262 v = port->qvecs + i;
7263
7264 v->port = port;
7265 v->type = MVPP2_QUEUE_VECTOR_PRIVATE;
7266 v->sw_thread_id = i;
7267 v->sw_thread_mask = BIT(i);
7268
7269 snprintf(irqname, sizeof(irqname), "tx-cpu%d", i);
7270
7271 if (queue_mode == MVPP2_QDIST_MULTI_MODE) {
7272 v->first_rxq = i * MVPP2_DEFAULT_RXQ;
7273 v->nrxqs = MVPP2_DEFAULT_RXQ;
7274 } else if (queue_mode == MVPP2_QDIST_SINGLE_MODE &&
7275 i == (port->nqvecs - 1)) {
7276 v->first_rxq = 0;
7277 v->nrxqs = port->nrxqs;
7278 v->type = MVPP2_QUEUE_VECTOR_SHARED;
7279 strncpy(irqname, "rx-shared", sizeof(irqname));
7280 }
7281
7282 v->irq = of_irq_get_byname(port_node, irqname);
7283 if (v->irq <= 0) {
7284 ret = -EINVAL;
7285 goto err;
7286 }
7287
7288 netif_napi_add(port->dev, &v->napi, mvpp2_poll,
7289 NAPI_POLL_WEIGHT);
7290 }
7291
7292 return 0;
7293
7294err:
7295 for (i = 0; i < port->nqvecs; i++)
7296 irq_dispose_mapping(port->qvecs[i].irq);
7297 return ret;
7298}
7299
7300static int mvpp2_queue_vectors_init(struct mvpp2_port *port,
7301 struct device_node *port_node)
7302{
7303 if (port->has_tx_irqs)
7304 return mvpp2_multi_queue_vectors_init(port, port_node);
7305 else
7306 return mvpp2_simple_queue_vectors_init(port, port_node);
7307}
7308
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007309static void mvpp2_queue_vectors_deinit(struct mvpp2_port *port)
7310{
7311 int i;
7312
7313 for (i = 0; i < port->nqvecs; i++)
7314 irq_dispose_mapping(port->qvecs[i].irq);
7315}
7316
7317/* Configure Rx queue group interrupt for this port */
7318static void mvpp2_rx_irqs_setup(struct mvpp2_port *port)
7319{
7320 struct mvpp2 *priv = port->priv;
7321 u32 val;
7322 int i;
7323
7324 if (priv->hw_version == MVPP21) {
7325 mvpp2_write(priv, MVPP21_ISR_RXQ_GROUP_REG(port->id),
7326 port->nrxqs);
7327 return;
7328 }
7329
7330 /* Handle the more complicated PPv2.2 case */
7331 for (i = 0; i < port->nqvecs; i++) {
7332 struct mvpp2_queue_vector *qv = port->qvecs + i;
7333
7334 if (!qv->nrxqs)
7335 continue;
7336
7337 val = qv->sw_thread_id;
7338 val |= port->id << MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_OFFSET;
7339 mvpp2_write(priv, MVPP22_ISR_RXQ_GROUP_INDEX_REG, val);
7340
7341 val = qv->first_rxq;
7342 val |= qv->nrxqs << MVPP22_ISR_RXQ_SUB_GROUP_SIZE_OFFSET;
7343 mvpp2_write(priv, MVPP22_ISR_RXQ_SUB_GROUP_CONFIG_REG, val);
7344 }
7345}
7346
Marcin Wojtas3f518502014-07-10 16:52:13 -03007347/* Initialize port HW */
7348static int mvpp2_port_init(struct mvpp2_port *port)
7349{
7350 struct device *dev = port->dev->dev.parent;
7351 struct mvpp2 *priv = port->priv;
7352 struct mvpp2_txq_pcpu *txq_pcpu;
7353 int queue, cpu, err;
7354
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007355 /* Checks for hardware constraints */
7356 if (port->first_rxq + port->nrxqs >
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01007357 MVPP2_MAX_PORTS * priv->max_port_rxqs)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007358 return -EINVAL;
7359
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007360 if (port->nrxqs % 4 || (port->nrxqs > priv->max_port_rxqs) ||
7361 (port->ntxqs > MVPP2_MAX_TXQ))
7362 return -EINVAL;
7363
Marcin Wojtas3f518502014-07-10 16:52:13 -03007364 /* Disable port */
7365 mvpp2_egress_disable(port);
7366 mvpp2_port_disable(port);
7367
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007368 port->tx_time_coal = MVPP2_TXDONE_COAL_USEC;
7369
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007370 port->txqs = devm_kcalloc(dev, port->ntxqs, sizeof(*port->txqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03007371 GFP_KERNEL);
7372 if (!port->txqs)
7373 return -ENOMEM;
7374
7375 /* Associate physical Tx queues to this port and initialize.
7376 * The mapping is predefined.
7377 */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007378 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007379 int queue_phy_id = mvpp2_txq_phys(port->id, queue);
7380 struct mvpp2_tx_queue *txq;
7381
7382 txq = devm_kzalloc(dev, sizeof(*txq), GFP_KERNEL);
Christophe Jaillet177c8d12017-02-19 10:19:57 +01007383 if (!txq) {
7384 err = -ENOMEM;
7385 goto err_free_percpu;
7386 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03007387
7388 txq->pcpu = alloc_percpu(struct mvpp2_txq_pcpu);
7389 if (!txq->pcpu) {
7390 err = -ENOMEM;
7391 goto err_free_percpu;
7392 }
7393
7394 txq->id = queue_phy_id;
7395 txq->log_id = queue;
7396 txq->done_pkts_coal = MVPP2_TXDONE_COAL_PKTS_THRESH;
7397 for_each_present_cpu(cpu) {
7398 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
7399 txq_pcpu->cpu = cpu;
7400 }
7401
7402 port->txqs[queue] = txq;
7403 }
7404
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007405 port->rxqs = devm_kcalloc(dev, port->nrxqs, sizeof(*port->rxqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03007406 GFP_KERNEL);
7407 if (!port->rxqs) {
7408 err = -ENOMEM;
7409 goto err_free_percpu;
7410 }
7411
7412 /* Allocate and initialize Rx queue for this port */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007413 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007414 struct mvpp2_rx_queue *rxq;
7415
7416 /* Map physical Rx queue to port's logical Rx queue */
7417 rxq = devm_kzalloc(dev, sizeof(*rxq), GFP_KERNEL);
Jisheng Zhangd82b0c22016-03-31 17:01:23 +08007418 if (!rxq) {
7419 err = -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007420 goto err_free_percpu;
Jisheng Zhangd82b0c22016-03-31 17:01:23 +08007421 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03007422 /* Map this Rx queue to a physical queue */
7423 rxq->id = port->first_rxq + queue;
7424 rxq->port = port->id;
7425 rxq->logic_rxq = queue;
7426
7427 port->rxqs[queue] = rxq;
7428 }
7429
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007430 mvpp2_rx_irqs_setup(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007431
7432 /* Create Rx descriptor rings */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007433 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007434 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
7435
7436 rxq->size = port->rx_ring_size;
7437 rxq->pkts_coal = MVPP2_RX_COAL_PKTS;
7438 rxq->time_coal = MVPP2_RX_COAL_USEC;
7439 }
7440
7441 mvpp2_ingress_disable(port);
7442
7443 /* Port default configuration */
7444 mvpp2_defaults_set(port);
7445
7446 /* Port's classifier configuration */
7447 mvpp2_cls_oversize_rxq_set(port);
7448 mvpp2_cls_port_config(port);
7449
7450 /* Provide an initial Rx packet size */
7451 port->pkt_size = MVPP2_RX_PKT_SIZE(port->dev->mtu);
7452
7453 /* Initialize pools for swf */
7454 err = mvpp2_swf_bm_pool_init(port);
7455 if (err)
7456 goto err_free_percpu;
7457
7458 return 0;
7459
7460err_free_percpu:
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007461 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03007462 if (!port->txqs[queue])
7463 continue;
7464 free_percpu(port->txqs[queue]->pcpu);
7465 }
7466 return err;
7467}
7468
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007469/* Checks if the port DT description has the TX interrupts
7470 * described. On PPv2.1, there are no such interrupts. On PPv2.2,
7471 * there are available, but we need to keep support for old DTs.
7472 */
7473static bool mvpp2_port_has_tx_irqs(struct mvpp2 *priv,
7474 struct device_node *port_node)
7475{
7476 char *irqs[5] = { "rx-shared", "tx-cpu0", "tx-cpu1",
7477 "tx-cpu2", "tx-cpu3" };
7478 int ret, i;
7479
7480 if (priv->hw_version == MVPP21)
7481 return false;
7482
7483 for (i = 0; i < 5; i++) {
7484 ret = of_property_match_string(port_node, "interrupt-names",
7485 irqs[i]);
7486 if (ret < 0)
7487 return false;
7488 }
7489
7490 return true;
7491}
7492
Antoine Tenart3ba8c812017-09-02 11:06:47 +02007493static void mvpp2_port_copy_mac_addr(struct net_device *dev, struct mvpp2 *priv,
7494 struct device_node *port_node,
7495 char **mac_from)
7496{
7497 struct mvpp2_port *port = netdev_priv(dev);
7498 char hw_mac_addr[ETH_ALEN] = {0};
7499 const char *dt_mac_addr;
7500
7501 dt_mac_addr = of_get_mac_address(port_node);
7502 if (dt_mac_addr && is_valid_ether_addr(dt_mac_addr)) {
7503 *mac_from = "device tree";
7504 ether_addr_copy(dev->dev_addr, dt_mac_addr);
Antoine Tenart688cbaf2017-09-02 11:06:49 +02007505 return;
Antoine Tenart3ba8c812017-09-02 11:06:47 +02007506 }
Antoine Tenart688cbaf2017-09-02 11:06:49 +02007507
7508 if (priv->hw_version == MVPP21) {
7509 mvpp21_get_mac_address(port, hw_mac_addr);
7510 if (is_valid_ether_addr(hw_mac_addr)) {
7511 *mac_from = "hardware";
7512 ether_addr_copy(dev->dev_addr, hw_mac_addr);
7513 return;
7514 }
7515 }
7516
7517 *mac_from = "random";
7518 eth_hw_addr_random(dev);
Antoine Tenart3ba8c812017-09-02 11:06:47 +02007519}
7520
Marcin Wojtas3f518502014-07-10 16:52:13 -03007521/* Ports initialization */
7522static int mvpp2_port_probe(struct platform_device *pdev,
7523 struct device_node *port_node,
Yan Markman6bf69a12017-09-25 14:59:47 +02007524 struct mvpp2 *priv, int index)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007525{
7526 struct device_node *phy_node;
Antoine Tenart542897d2017-08-30 10:29:15 +02007527 struct phy *comphy;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007528 struct mvpp2_port *port;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007529 struct mvpp2_port_pcpu *port_pcpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007530 struct net_device *dev;
7531 struct resource *res;
Antoine Tenart3ba8c812017-09-02 11:06:47 +02007532 char *mac_from = "";
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007533 unsigned int ntxqs, nrxqs;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007534 bool has_tx_irqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007535 u32 id;
7536 int features;
7537 int phy_mode;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007538 int err, i, cpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007539
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007540 has_tx_irqs = mvpp2_port_has_tx_irqs(priv, port_node);
7541
7542 if (!has_tx_irqs)
7543 queue_mode = MVPP2_QDIST_SINGLE_MODE;
7544
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007545 ntxqs = MVPP2_MAX_TXQ;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007546 if (priv->hw_version == MVPP22 && queue_mode == MVPP2_QDIST_MULTI_MODE)
7547 nrxqs = MVPP2_DEFAULT_RXQ * num_possible_cpus();
7548 else
7549 nrxqs = MVPP2_DEFAULT_RXQ;
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007550
7551 dev = alloc_etherdev_mqs(sizeof(*port), ntxqs, nrxqs);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007552 if (!dev)
7553 return -ENOMEM;
7554
7555 phy_node = of_parse_phandle(port_node, "phy", 0);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007556 phy_mode = of_get_phy_mode(port_node);
7557 if (phy_mode < 0) {
7558 dev_err(&pdev->dev, "incorrect phy mode\n");
7559 err = phy_mode;
7560 goto err_free_netdev;
7561 }
7562
Antoine Tenart542897d2017-08-30 10:29:15 +02007563 comphy = devm_of_phy_get(&pdev->dev, port_node, NULL);
7564 if (IS_ERR(comphy)) {
7565 if (PTR_ERR(comphy) == -EPROBE_DEFER) {
7566 err = -EPROBE_DEFER;
7567 goto err_free_netdev;
7568 }
7569 comphy = NULL;
7570 }
7571
Marcin Wojtas3f518502014-07-10 16:52:13 -03007572 if (of_property_read_u32(port_node, "port-id", &id)) {
7573 err = -EINVAL;
7574 dev_err(&pdev->dev, "missing port-id value\n");
7575 goto err_free_netdev;
7576 }
7577
7578 dev->tx_queue_len = MVPP2_MAX_TXD;
7579 dev->watchdog_timeo = 5 * HZ;
7580 dev->netdev_ops = &mvpp2_netdev_ops;
7581 dev->ethtool_ops = &mvpp2_eth_tool_ops;
7582
7583 port = netdev_priv(dev);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007584 port->dev = dev;
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007585 port->ntxqs = ntxqs;
7586 port->nrxqs = nrxqs;
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007587 port->priv = priv;
7588 port->has_tx_irqs = has_tx_irqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007589
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007590 err = mvpp2_queue_vectors_init(port, port_node);
7591 if (err)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007592 goto err_free_netdev;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007593
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007594 port->link_irq = of_irq_get_byname(port_node, "link");
7595 if (port->link_irq == -EPROBE_DEFER) {
7596 err = -EPROBE_DEFER;
7597 goto err_deinit_qvecs;
7598 }
7599 if (port->link_irq <= 0)
7600 /* the link irq is optional */
7601 port->link_irq = 0;
7602
Marcin Wojtas3f518502014-07-10 16:52:13 -03007603 if (of_property_read_bool(port_node, "marvell,loopback"))
7604 port->flags |= MVPP2_F_LOOPBACK;
7605
Marcin Wojtas3f518502014-07-10 16:52:13 -03007606 port->id = id;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01007607 if (priv->hw_version == MVPP21)
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007608 port->first_rxq = port->id * port->nrxqs;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01007609 else
7610 port->first_rxq = port->id * priv->max_port_rxqs;
7611
Marcin Wojtas3f518502014-07-10 16:52:13 -03007612 port->phy_node = phy_node;
7613 port->phy_interface = phy_mode;
Antoine Tenart542897d2017-08-30 10:29:15 +02007614 port->comphy = comphy;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007615
Thomas Petazzonia7868412017-03-07 16:53:13 +01007616 if (priv->hw_version == MVPP21) {
7617 res = platform_get_resource(pdev, IORESOURCE_MEM, 2 + id);
7618 port->base = devm_ioremap_resource(&pdev->dev, res);
7619 if (IS_ERR(port->base)) {
7620 err = PTR_ERR(port->base);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007621 goto err_free_irq;
Thomas Petazzonia7868412017-03-07 16:53:13 +01007622 }
7623 } else {
7624 if (of_property_read_u32(port_node, "gop-port-id",
7625 &port->gop_id)) {
7626 err = -EINVAL;
7627 dev_err(&pdev->dev, "missing gop-port-id value\n");
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007628 goto err_deinit_qvecs;
Thomas Petazzonia7868412017-03-07 16:53:13 +01007629 }
7630
7631 port->base = priv->iface_base + MVPP22_GMAC_BASE(port->gop_id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007632 }
7633
7634 /* Alloc per-cpu stats */
7635 port->stats = netdev_alloc_pcpu_stats(struct mvpp2_pcpu_stats);
7636 if (!port->stats) {
7637 err = -ENOMEM;
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007638 goto err_free_irq;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007639 }
7640
Antoine Tenart3ba8c812017-09-02 11:06:47 +02007641 mvpp2_port_copy_mac_addr(dev, priv, port_node, &mac_from);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007642
7643 port->tx_ring_size = MVPP2_MAX_TXD;
7644 port->rx_ring_size = MVPP2_MAX_RXD;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007645 SET_NETDEV_DEV(dev, &pdev->dev);
7646
7647 err = mvpp2_port_init(port);
7648 if (err < 0) {
7649 dev_err(&pdev->dev, "failed to init port %d\n", id);
7650 goto err_free_stats;
7651 }
Thomas Petazzoni26975822017-03-07 16:53:14 +01007652
Thomas Petazzoni26975822017-03-07 16:53:14 +01007653 mvpp2_port_periodic_xon_disable(port);
7654
7655 if (priv->hw_version == MVPP21)
7656 mvpp2_port_fc_adv_enable(port);
7657
7658 mvpp2_port_reset(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007659
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007660 port->pcpu = alloc_percpu(struct mvpp2_port_pcpu);
7661 if (!port->pcpu) {
7662 err = -ENOMEM;
7663 goto err_free_txq_pcpu;
7664 }
7665
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007666 if (!port->has_tx_irqs) {
7667 for_each_present_cpu(cpu) {
7668 port_pcpu = per_cpu_ptr(port->pcpu, cpu);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007669
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007670 hrtimer_init(&port_pcpu->tx_done_timer, CLOCK_MONOTONIC,
7671 HRTIMER_MODE_REL_PINNED);
7672 port_pcpu->tx_done_timer.function = mvpp2_hr_timer_cb;
7673 port_pcpu->timer_scheduled = false;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007674
Thomas Petazzoni213f4282017-08-03 10:42:00 +02007675 tasklet_init(&port_pcpu->tx_done_tasklet,
7676 mvpp2_tx_proc_cb,
7677 (unsigned long)dev);
7678 }
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007679 }
7680
Antoine Ténart186cd4d2017-08-23 09:46:56 +02007681 features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007682 dev->features = features | NETIF_F_RXCSUM;
7683 dev->hw_features |= features | NETIF_F_RXCSUM | NETIF_F_GRO;
7684 dev->vlan_features |= features;
7685
Jarod Wilson57779872016-10-17 15:54:06 -04007686 /* MTU range: 68 - 9676 */
7687 dev->min_mtu = ETH_MIN_MTU;
7688 /* 9676 == 9700 - 20 and rounding to 8 */
7689 dev->max_mtu = 9676;
7690
Marcin Wojtas3f518502014-07-10 16:52:13 -03007691 err = register_netdev(dev);
7692 if (err < 0) {
7693 dev_err(&pdev->dev, "failed to register netdev\n");
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007694 goto err_free_port_pcpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007695 }
7696 netdev_info(dev, "Using %s mac address %pM\n", mac_from, dev->dev_addr);
7697
Yan Markman6bf69a12017-09-25 14:59:47 +02007698 priv->port_list[index] = port;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007699 return 0;
7700
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007701err_free_port_pcpu:
7702 free_percpu(port->pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007703err_free_txq_pcpu:
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007704 for (i = 0; i < port->ntxqs; i++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007705 free_percpu(port->txqs[i]->pcpu);
7706err_free_stats:
7707 free_percpu(port->stats);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007708err_free_irq:
7709 if (port->link_irq)
7710 irq_dispose_mapping(port->link_irq);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007711err_deinit_qvecs:
7712 mvpp2_queue_vectors_deinit(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007713err_free_netdev:
Peter Chenccb80392016-08-01 15:02:37 +08007714 of_node_put(phy_node);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007715 free_netdev(dev);
7716 return err;
7717}
7718
7719/* Ports removal routine */
7720static void mvpp2_port_remove(struct mvpp2_port *port)
7721{
7722 int i;
7723
7724 unregister_netdev(port->dev);
Peter Chenccb80392016-08-01 15:02:37 +08007725 of_node_put(port->phy_node);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02007726 free_percpu(port->pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007727 free_percpu(port->stats);
Thomas Petazzoni09f83972017-08-03 10:41:57 +02007728 for (i = 0; i < port->ntxqs; i++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03007729 free_percpu(port->txqs[i]->pcpu);
Thomas Petazzoni591f4cf2017-08-03 10:41:59 +02007730 mvpp2_queue_vectors_deinit(port);
Antoine Tenartfd3651b2017-09-01 11:04:54 +02007731 if (port->link_irq)
7732 irq_dispose_mapping(port->link_irq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007733 free_netdev(port->dev);
7734}
7735
7736/* Initialize decoding windows */
7737static void mvpp2_conf_mbus_windows(const struct mbus_dram_target_info *dram,
7738 struct mvpp2 *priv)
7739{
7740 u32 win_enable;
7741 int i;
7742
7743 for (i = 0; i < 6; i++) {
7744 mvpp2_write(priv, MVPP2_WIN_BASE(i), 0);
7745 mvpp2_write(priv, MVPP2_WIN_SIZE(i), 0);
7746
7747 if (i < 4)
7748 mvpp2_write(priv, MVPP2_WIN_REMAP(i), 0);
7749 }
7750
7751 win_enable = 0;
7752
7753 for (i = 0; i < dram->num_cs; i++) {
7754 const struct mbus_dram_window *cs = dram->cs + i;
7755
7756 mvpp2_write(priv, MVPP2_WIN_BASE(i),
7757 (cs->base & 0xffff0000) | (cs->mbus_attr << 8) |
7758 dram->mbus_dram_target_id);
7759
7760 mvpp2_write(priv, MVPP2_WIN_SIZE(i),
7761 (cs->size - 1) & 0xffff0000);
7762
7763 win_enable |= (1 << i);
7764 }
7765
7766 mvpp2_write(priv, MVPP2_BASE_ADDR_ENABLE, win_enable);
7767}
7768
7769/* Initialize Rx FIFO's */
7770static void mvpp2_rx_fifo_init(struct mvpp2 *priv)
7771{
7772 int port;
7773
7774 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
7775 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
Antoine Tenart2d1d7df2017-10-30 11:23:28 +01007776 MVPP2_RX_FIFO_PORT_DATA_SIZE_4KB);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007777 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
Antoine Tenart2d1d7df2017-10-30 11:23:28 +01007778 MVPP2_RX_FIFO_PORT_ATTR_SIZE_4KB);
7779 }
7780
7781 mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
7782 MVPP2_RX_FIFO_PORT_MIN_PKT);
7783 mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
7784}
7785
7786static void mvpp22_rx_fifo_init(struct mvpp2 *priv)
7787{
7788 int port;
7789
7790 /* The FIFO size parameters are set depending on the maximum speed a
7791 * given port can handle:
7792 * - Port 0: 10Gbps
7793 * - Port 1: 2.5Gbps
7794 * - Ports 2 and 3: 1Gbps
7795 */
7796
7797 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(0),
7798 MVPP2_RX_FIFO_PORT_DATA_SIZE_32KB);
7799 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(0),
7800 MVPP2_RX_FIFO_PORT_ATTR_SIZE_32KB);
7801
7802 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(1),
7803 MVPP2_RX_FIFO_PORT_DATA_SIZE_8KB);
7804 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(1),
7805 MVPP2_RX_FIFO_PORT_ATTR_SIZE_8KB);
7806
7807 for (port = 2; port < MVPP2_MAX_PORTS; port++) {
7808 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
7809 MVPP2_RX_FIFO_PORT_DATA_SIZE_4KB);
7810 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
7811 MVPP2_RX_FIFO_PORT_ATTR_SIZE_4KB);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007812 }
7813
7814 mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
7815 MVPP2_RX_FIFO_PORT_MIN_PKT);
7816 mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
7817}
7818
Antoine Tenart7c10f972017-10-30 11:23:29 +01007819/* Initialize Tx FIFO's */
7820static void mvpp22_tx_fifo_init(struct mvpp2 *priv)
7821{
7822 int port;
7823
7824 for (port = 0; port < MVPP2_MAX_PORTS; port++)
7825 mvpp2_write(priv, MVPP22_TX_FIFO_SIZE_REG(port),
7826 MVPP22_TX_FIFO_DATA_SIZE_3KB);
7827}
7828
Thomas Petazzoni6763ce32017-03-07 16:53:15 +01007829static void mvpp2_axi_init(struct mvpp2 *priv)
7830{
7831 u32 val, rdval, wrval;
7832
7833 mvpp2_write(priv, MVPP22_BM_ADDR_HIGH_RLS_REG, 0x0);
7834
7835 /* AXI Bridge Configuration */
7836
7837 rdval = MVPP22_AXI_CODE_CACHE_RD_CACHE
7838 << MVPP22_AXI_ATTR_CACHE_OFFS;
7839 rdval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
7840 << MVPP22_AXI_ATTR_DOMAIN_OFFS;
7841
7842 wrval = MVPP22_AXI_CODE_CACHE_WR_CACHE
7843 << MVPP22_AXI_ATTR_CACHE_OFFS;
7844 wrval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
7845 << MVPP22_AXI_ATTR_DOMAIN_OFFS;
7846
7847 /* BM */
7848 mvpp2_write(priv, MVPP22_AXI_BM_WR_ATTR_REG, wrval);
7849 mvpp2_write(priv, MVPP22_AXI_BM_RD_ATTR_REG, rdval);
7850
7851 /* Descriptors */
7852 mvpp2_write(priv, MVPP22_AXI_AGGRQ_DESCR_RD_ATTR_REG, rdval);
7853 mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_WR_ATTR_REG, wrval);
7854 mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_RD_ATTR_REG, rdval);
7855 mvpp2_write(priv, MVPP22_AXI_RXQ_DESCR_WR_ATTR_REG, wrval);
7856
7857 /* Buffer Data */
7858 mvpp2_write(priv, MVPP22_AXI_TX_DATA_RD_ATTR_REG, rdval);
7859 mvpp2_write(priv, MVPP22_AXI_RX_DATA_WR_ATTR_REG, wrval);
7860
7861 val = MVPP22_AXI_CODE_CACHE_NON_CACHE
7862 << MVPP22_AXI_CODE_CACHE_OFFS;
7863 val |= MVPP22_AXI_CODE_DOMAIN_SYSTEM
7864 << MVPP22_AXI_CODE_DOMAIN_OFFS;
7865 mvpp2_write(priv, MVPP22_AXI_RD_NORMAL_CODE_REG, val);
7866 mvpp2_write(priv, MVPP22_AXI_WR_NORMAL_CODE_REG, val);
7867
7868 val = MVPP22_AXI_CODE_CACHE_RD_CACHE
7869 << MVPP22_AXI_CODE_CACHE_OFFS;
7870 val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
7871 << MVPP22_AXI_CODE_DOMAIN_OFFS;
7872
7873 mvpp2_write(priv, MVPP22_AXI_RD_SNOOP_CODE_REG, val);
7874
7875 val = MVPP22_AXI_CODE_CACHE_WR_CACHE
7876 << MVPP22_AXI_CODE_CACHE_OFFS;
7877 val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
7878 << MVPP22_AXI_CODE_DOMAIN_OFFS;
7879
7880 mvpp2_write(priv, MVPP22_AXI_WR_SNOOP_CODE_REG, val);
7881}
7882
Marcin Wojtas3f518502014-07-10 16:52:13 -03007883/* Initialize network controller common part HW */
7884static int mvpp2_init(struct platform_device *pdev, struct mvpp2 *priv)
7885{
7886 const struct mbus_dram_target_info *dram_target_info;
7887 int err, i;
Marcin Wojtas08a23752014-07-21 13:48:12 -03007888 u32 val;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007889
Marcin Wojtas3f518502014-07-10 16:52:13 -03007890 /* MBUS windows configuration */
7891 dram_target_info = mv_mbus_dram_info();
7892 if (dram_target_info)
7893 mvpp2_conf_mbus_windows(dram_target_info, priv);
7894
Thomas Petazzoni6763ce32017-03-07 16:53:15 +01007895 if (priv->hw_version == MVPP22)
7896 mvpp2_axi_init(priv);
7897
Marcin Wojtas08a23752014-07-21 13:48:12 -03007898 /* Disable HW PHY polling */
Thomas Petazzoni26975822017-03-07 16:53:14 +01007899 if (priv->hw_version == MVPP21) {
7900 val = readl(priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
7901 val |= MVPP2_PHY_AN_STOP_SMI0_MASK;
7902 writel(val, priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
7903 } else {
7904 val = readl(priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
7905 val &= ~MVPP22_SMI_POLLING_EN;
7906 writel(val, priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
7907 }
Marcin Wojtas08a23752014-07-21 13:48:12 -03007908
Marcin Wojtas3f518502014-07-10 16:52:13 -03007909 /* Allocate and initialize aggregated TXQs */
7910 priv->aggr_txqs = devm_kcalloc(&pdev->dev, num_present_cpus(),
Markus Elfringd7ce3ce2017-04-17 08:48:23 +02007911 sizeof(*priv->aggr_txqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03007912 GFP_KERNEL);
7913 if (!priv->aggr_txqs)
7914 return -ENOMEM;
7915
7916 for_each_present_cpu(i) {
7917 priv->aggr_txqs[i].id = i;
7918 priv->aggr_txqs[i].size = MVPP2_AGGR_TXQ_SIZE;
Antoine Ténart85affd72017-08-23 09:46:55 +02007919 err = mvpp2_aggr_txq_init(pdev, &priv->aggr_txqs[i], i, priv);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007920 if (err < 0)
7921 return err;
7922 }
7923
Antoine Tenart7c10f972017-10-30 11:23:29 +01007924 /* Fifo Init */
7925 if (priv->hw_version == MVPP21) {
Antoine Tenart2d1d7df2017-10-30 11:23:28 +01007926 mvpp2_rx_fifo_init(priv);
Antoine Tenart7c10f972017-10-30 11:23:29 +01007927 } else {
Antoine Tenart2d1d7df2017-10-30 11:23:28 +01007928 mvpp22_rx_fifo_init(priv);
Antoine Tenart7c10f972017-10-30 11:23:29 +01007929 mvpp22_tx_fifo_init(priv);
7930 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03007931
Thomas Petazzoni26975822017-03-07 16:53:14 +01007932 if (priv->hw_version == MVPP21)
7933 writel(MVPP2_EXT_GLOBAL_CTRL_DEFAULT,
7934 priv->lms_base + MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007935
7936 /* Allow cache snoop when transmiting packets */
7937 mvpp2_write(priv, MVPP2_TX_SNOOP_REG, 0x1);
7938
7939 /* Buffer Manager initialization */
7940 err = mvpp2_bm_init(pdev, priv);
7941 if (err < 0)
7942 return err;
7943
7944 /* Parser default initialization */
7945 err = mvpp2_prs_default_init(pdev, priv);
7946 if (err < 0)
7947 return err;
7948
7949 /* Classifier default initialization */
7950 mvpp2_cls_init(priv);
7951
7952 return 0;
7953}
7954
7955static int mvpp2_probe(struct platform_device *pdev)
7956{
7957 struct device_node *dn = pdev->dev.of_node;
7958 struct device_node *port_node;
7959 struct mvpp2 *priv;
7960 struct resource *res;
Thomas Petazzonia7868412017-03-07 16:53:13 +01007961 void __iomem *base;
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02007962 int port_count, i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007963 int err;
7964
Markus Elfring0b92e592017-04-17 08:38:32 +02007965 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007966 if (!priv)
7967 return -ENOMEM;
7968
Thomas Petazzonifaca9242017-03-07 16:53:06 +01007969 priv->hw_version =
7970 (unsigned long)of_device_get_match_data(&pdev->dev);
7971
Marcin Wojtas3f518502014-07-10 16:52:13 -03007972 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thomas Petazzonia7868412017-03-07 16:53:13 +01007973 base = devm_ioremap_resource(&pdev->dev, res);
7974 if (IS_ERR(base))
7975 return PTR_ERR(base);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007976
Thomas Petazzonia7868412017-03-07 16:53:13 +01007977 if (priv->hw_version == MVPP21) {
7978 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
7979 priv->lms_base = devm_ioremap_resource(&pdev->dev, res);
7980 if (IS_ERR(priv->lms_base))
7981 return PTR_ERR(priv->lms_base);
7982 } else {
7983 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
7984 priv->iface_base = devm_ioremap_resource(&pdev->dev, res);
7985 if (IS_ERR(priv->iface_base))
7986 return PTR_ERR(priv->iface_base);
Antoine Ténartf84bf382017-08-22 19:08:27 +02007987
7988 priv->sysctrl_base =
7989 syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
7990 "marvell,system-controller");
7991 if (IS_ERR(priv->sysctrl_base))
7992 /* The system controller regmap is optional for dt
7993 * compatibility reasons. When not provided, the
7994 * configuration of the GoP relies on the
7995 * firmware/bootloader.
7996 */
7997 priv->sysctrl_base = NULL;
Thomas Petazzonia7868412017-03-07 16:53:13 +01007998 }
7999
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02008000 for (i = 0; i < MVPP2_MAX_THREADS; i++) {
Thomas Petazzonia7868412017-03-07 16:53:13 +01008001 u32 addr_space_sz;
8002
8003 addr_space_sz = (priv->hw_version == MVPP21 ?
8004 MVPP21_ADDR_SPACE_SZ : MVPP22_ADDR_SPACE_SZ);
Thomas Petazzonidf089aa2017-08-03 10:41:58 +02008005 priv->swth_base[i] = base + i * addr_space_sz;
Thomas Petazzonia7868412017-03-07 16:53:13 +01008006 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03008007
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01008008 if (priv->hw_version == MVPP21)
8009 priv->max_port_rxqs = 8;
8010 else
8011 priv->max_port_rxqs = 32;
8012
Marcin Wojtas3f518502014-07-10 16:52:13 -03008013 priv->pp_clk = devm_clk_get(&pdev->dev, "pp_clk");
8014 if (IS_ERR(priv->pp_clk))
8015 return PTR_ERR(priv->pp_clk);
8016 err = clk_prepare_enable(priv->pp_clk);
8017 if (err < 0)
8018 return err;
8019
8020 priv->gop_clk = devm_clk_get(&pdev->dev, "gop_clk");
8021 if (IS_ERR(priv->gop_clk)) {
8022 err = PTR_ERR(priv->gop_clk);
8023 goto err_pp_clk;
8024 }
8025 err = clk_prepare_enable(priv->gop_clk);
8026 if (err < 0)
8027 goto err_pp_clk;
8028
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008029 if (priv->hw_version == MVPP22) {
8030 priv->mg_clk = devm_clk_get(&pdev->dev, "mg_clk");
8031 if (IS_ERR(priv->mg_clk)) {
8032 err = PTR_ERR(priv->mg_clk);
8033 goto err_gop_clk;
8034 }
8035
8036 err = clk_prepare_enable(priv->mg_clk);
8037 if (err < 0)
8038 goto err_gop_clk;
Gregory CLEMENT4792ea02017-09-29 14:27:39 +02008039
8040 priv->axi_clk = devm_clk_get(&pdev->dev, "axi_clk");
8041 if (IS_ERR(priv->axi_clk)) {
8042 err = PTR_ERR(priv->axi_clk);
8043 if (err == -EPROBE_DEFER)
8044 goto err_gop_clk;
8045 priv->axi_clk = NULL;
8046 } else {
8047 err = clk_prepare_enable(priv->axi_clk);
8048 if (err < 0)
8049 goto err_gop_clk;
8050 }
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008051 }
8052
Marcin Wojtas3f518502014-07-10 16:52:13 -03008053 /* Get system's tclk rate */
8054 priv->tclk = clk_get_rate(priv->pp_clk);
8055
Thomas Petazzoni2067e0a2017-03-07 16:53:19 +01008056 if (priv->hw_version == MVPP22) {
8057 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(40));
8058 if (err)
8059 goto err_mg_clk;
8060 /* Sadly, the BM pools all share the same register to
8061 * store the high 32 bits of their address. So they
8062 * must all have the same high 32 bits, which forces
8063 * us to restrict coherent memory to DMA_BIT_MASK(32).
8064 */
8065 err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
8066 if (err)
8067 goto err_mg_clk;
8068 }
8069
Marcin Wojtas3f518502014-07-10 16:52:13 -03008070 /* Initialize network controller */
8071 err = mvpp2_init(pdev, priv);
8072 if (err < 0) {
8073 dev_err(&pdev->dev, "failed to initialize controller\n");
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008074 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008075 }
8076
8077 port_count = of_get_available_child_count(dn);
8078 if (port_count == 0) {
8079 dev_err(&pdev->dev, "no ports enabled\n");
Wei Yongjun575a1932014-07-20 22:02:43 +08008080 err = -ENODEV;
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008081 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008082 }
8083
8084 priv->port_list = devm_kcalloc(&pdev->dev, port_count,
Markus Elfring0b92e592017-04-17 08:38:32 +02008085 sizeof(*priv->port_list),
8086 GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008087 if (!priv->port_list) {
8088 err = -ENOMEM;
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008089 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008090 }
8091
8092 /* Initialize ports */
Yan Markman6bf69a12017-09-25 14:59:47 +02008093 i = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008094 for_each_available_child_of_node(dn, port_node) {
Yan Markman6bf69a12017-09-25 14:59:47 +02008095 err = mvpp2_port_probe(pdev, port_node, priv, i);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008096 if (err < 0)
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008097 goto err_mg_clk;
Yan Markman6bf69a12017-09-25 14:59:47 +02008098 i++;
Marcin Wojtas3f518502014-07-10 16:52:13 -03008099 }
8100
8101 platform_set_drvdata(pdev, priv);
8102 return 0;
8103
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008104err_mg_clk:
Gregory CLEMENT4792ea02017-09-29 14:27:39 +02008105 clk_disable_unprepare(priv->axi_clk);
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008106 if (priv->hw_version == MVPP22)
8107 clk_disable_unprepare(priv->mg_clk);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008108err_gop_clk:
8109 clk_disable_unprepare(priv->gop_clk);
8110err_pp_clk:
8111 clk_disable_unprepare(priv->pp_clk);
8112 return err;
8113}
8114
8115static int mvpp2_remove(struct platform_device *pdev)
8116{
8117 struct mvpp2 *priv = platform_get_drvdata(pdev);
8118 struct device_node *dn = pdev->dev.of_node;
8119 struct device_node *port_node;
8120 int i = 0;
8121
8122 for_each_available_child_of_node(dn, port_node) {
8123 if (priv->port_list[i])
8124 mvpp2_port_remove(priv->port_list[i]);
8125 i++;
8126 }
8127
8128 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
8129 struct mvpp2_bm_pool *bm_pool = &priv->bm_pools[i];
8130
8131 mvpp2_bm_pool_destroy(pdev, priv, bm_pool);
8132 }
8133
8134 for_each_present_cpu(i) {
8135 struct mvpp2_tx_queue *aggr_txq = &priv->aggr_txqs[i];
8136
8137 dma_free_coherent(&pdev->dev,
8138 MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
8139 aggr_txq->descs,
Thomas Petazzoni20396132017-03-07 16:53:00 +01008140 aggr_txq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008141 }
8142
Gregory CLEMENT4792ea02017-09-29 14:27:39 +02008143 clk_disable_unprepare(priv->axi_clk);
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01008144 clk_disable_unprepare(priv->mg_clk);
Marcin Wojtas3f518502014-07-10 16:52:13 -03008145 clk_disable_unprepare(priv->pp_clk);
8146 clk_disable_unprepare(priv->gop_clk);
8147
8148 return 0;
8149}
8150
8151static const struct of_device_id mvpp2_match[] = {
Thomas Petazzonifaca9242017-03-07 16:53:06 +01008152 {
8153 .compatible = "marvell,armada-375-pp2",
8154 .data = (void *)MVPP21,
8155 },
Thomas Petazzonifc5e1552017-03-07 16:53:20 +01008156 {
8157 .compatible = "marvell,armada-7k-pp22",
8158 .data = (void *)MVPP22,
8159 },
Marcin Wojtas3f518502014-07-10 16:52:13 -03008160 { }
8161};
8162MODULE_DEVICE_TABLE(of, mvpp2_match);
8163
8164static struct platform_driver mvpp2_driver = {
8165 .probe = mvpp2_probe,
8166 .remove = mvpp2_remove,
8167 .driver = {
8168 .name = MVPP2_DRIVER_NAME,
8169 .of_match_table = mvpp2_match,
8170 },
8171};
8172
8173module_platform_driver(mvpp2_driver);
8174
8175MODULE_DESCRIPTION("Marvell PPv2 Ethernet Driver - www.marvell.com");
8176MODULE_AUTHOR("Marcin Wojtas <mw@semihalf.com>");
Ezequiel Garciac6340992014-07-14 10:34:47 -03008177MODULE_LICENSE("GPL v2");