blob: da48d6066e7deb3082183bab63fed309a3fdaae2 [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>
21#include <linux/interrupt.h>
22#include <linux/cpumask.h>
23#include <linux/of.h>
24#include <linux/of_irq.h>
25#include <linux/of_mdio.h>
26#include <linux/of_net.h>
27#include <linux/of_address.h>
Thomas Petazzonifaca9242017-03-07 16:53:06 +010028#include <linux/of_device.h>
Marcin Wojtas3f518502014-07-10 16:52:13 -030029#include <linux/phy.h>
30#include <linux/clk.h>
Marcin Wojtasedc660f2015-08-06 19:00:30 +020031#include <linux/hrtimer.h>
32#include <linux/ktime.h>
Marcin Wojtas3f518502014-07-10 16:52:13 -030033#include <uapi/linux/ppp_defs.h>
34#include <net/ip.h>
35#include <net/ipv6.h>
36
37/* RX Fifo Registers */
38#define MVPP2_RX_DATA_FIFO_SIZE_REG(port) (0x00 + 4 * (port))
39#define MVPP2_RX_ATTR_FIFO_SIZE_REG(port) (0x20 + 4 * (port))
40#define MVPP2_RX_MIN_PKT_SIZE_REG 0x60
41#define MVPP2_RX_FIFO_INIT_REG 0x64
42
43/* RX DMA Top Registers */
44#define MVPP2_RX_CTRL_REG(port) (0x140 + 4 * (port))
45#define MVPP2_RX_LOW_LATENCY_PKT_SIZE(s) (((s) & 0xfff) << 16)
46#define MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK BIT(31)
47#define MVPP2_POOL_BUF_SIZE_REG(pool) (0x180 + 4 * (pool))
48#define MVPP2_POOL_BUF_SIZE_OFFSET 5
49#define MVPP2_RXQ_CONFIG_REG(rxq) (0x800 + 4 * (rxq))
50#define MVPP2_SNOOP_PKT_SIZE_MASK 0x1ff
51#define MVPP2_SNOOP_BUF_HDR_MASK BIT(9)
52#define MVPP2_RXQ_POOL_SHORT_OFFS 20
Thomas Petazzoni5eac8922017-03-07 16:53:10 +010053#define MVPP21_RXQ_POOL_SHORT_MASK 0x700000
54#define MVPP22_RXQ_POOL_SHORT_MASK 0xf00000
Marcin Wojtas3f518502014-07-10 16:52:13 -030055#define MVPP2_RXQ_POOL_LONG_OFFS 24
Thomas Petazzoni5eac8922017-03-07 16:53:10 +010056#define MVPP21_RXQ_POOL_LONG_MASK 0x7000000
57#define MVPP22_RXQ_POOL_LONG_MASK 0xf000000
Marcin Wojtas3f518502014-07-10 16:52:13 -030058#define MVPP2_RXQ_PACKET_OFFSET_OFFS 28
59#define MVPP2_RXQ_PACKET_OFFSET_MASK 0x70000000
60#define MVPP2_RXQ_DISABLE_MASK BIT(31)
61
62/* Parser Registers */
63#define MVPP2_PRS_INIT_LOOKUP_REG 0x1000
64#define MVPP2_PRS_PORT_LU_MAX 0xf
65#define MVPP2_PRS_PORT_LU_MASK(port) (0xff << ((port) * 4))
66#define MVPP2_PRS_PORT_LU_VAL(port, val) ((val) << ((port) * 4))
67#define MVPP2_PRS_INIT_OFFS_REG(port) (0x1004 + ((port) & 4))
68#define MVPP2_PRS_INIT_OFF_MASK(port) (0x3f << (((port) % 4) * 8))
69#define MVPP2_PRS_INIT_OFF_VAL(port, val) ((val) << (((port) % 4) * 8))
70#define MVPP2_PRS_MAX_LOOP_REG(port) (0x100c + ((port) & 4))
71#define MVPP2_PRS_MAX_LOOP_MASK(port) (0xff << (((port) % 4) * 8))
72#define MVPP2_PRS_MAX_LOOP_VAL(port, val) ((val) << (((port) % 4) * 8))
73#define MVPP2_PRS_TCAM_IDX_REG 0x1100
74#define MVPP2_PRS_TCAM_DATA_REG(idx) (0x1104 + (idx) * 4)
75#define MVPP2_PRS_TCAM_INV_MASK BIT(31)
76#define MVPP2_PRS_SRAM_IDX_REG 0x1200
77#define MVPP2_PRS_SRAM_DATA_REG(idx) (0x1204 + (idx) * 4)
78#define MVPP2_PRS_TCAM_CTRL_REG 0x1230
79#define MVPP2_PRS_TCAM_EN_MASK BIT(0)
80
81/* Classifier Registers */
82#define MVPP2_CLS_MODE_REG 0x1800
83#define MVPP2_CLS_MODE_ACTIVE_MASK BIT(0)
84#define MVPP2_CLS_PORT_WAY_REG 0x1810
85#define MVPP2_CLS_PORT_WAY_MASK(port) (1 << (port))
86#define MVPP2_CLS_LKP_INDEX_REG 0x1814
87#define MVPP2_CLS_LKP_INDEX_WAY_OFFS 6
88#define MVPP2_CLS_LKP_TBL_REG 0x1818
89#define MVPP2_CLS_LKP_TBL_RXQ_MASK 0xff
90#define MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK BIT(25)
91#define MVPP2_CLS_FLOW_INDEX_REG 0x1820
92#define MVPP2_CLS_FLOW_TBL0_REG 0x1824
93#define MVPP2_CLS_FLOW_TBL1_REG 0x1828
94#define MVPP2_CLS_FLOW_TBL2_REG 0x182c
95#define MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port) (0x1980 + ((port) * 4))
96#define MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS 3
97#define MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK 0x7
98#define MVPP2_CLS_SWFWD_P2HQ_REG(port) (0x19b0 + ((port) * 4))
99#define MVPP2_CLS_SWFWD_PCTRL_REG 0x19d0
100#define MVPP2_CLS_SWFWD_PCTRL_MASK(port) (1 << (port))
101
102/* Descriptor Manager Top Registers */
103#define MVPP2_RXQ_NUM_REG 0x2040
104#define MVPP2_RXQ_DESC_ADDR_REG 0x2044
105#define MVPP2_RXQ_DESC_SIZE_REG 0x2048
106#define MVPP2_RXQ_DESC_SIZE_MASK 0x3ff0
107#define MVPP2_RXQ_STATUS_UPDATE_REG(rxq) (0x3000 + 4 * (rxq))
108#define MVPP2_RXQ_NUM_PROCESSED_OFFSET 0
109#define MVPP2_RXQ_NUM_NEW_OFFSET 16
110#define MVPP2_RXQ_STATUS_REG(rxq) (0x3400 + 4 * (rxq))
111#define MVPP2_RXQ_OCCUPIED_MASK 0x3fff
112#define MVPP2_RXQ_NON_OCCUPIED_OFFSET 16
113#define MVPP2_RXQ_NON_OCCUPIED_MASK 0x3fff0000
114#define MVPP2_RXQ_THRESH_REG 0x204c
115#define MVPP2_OCCUPIED_THRESH_OFFSET 0
116#define MVPP2_OCCUPIED_THRESH_MASK 0x3fff
117#define MVPP2_RXQ_INDEX_REG 0x2050
118#define MVPP2_TXQ_NUM_REG 0x2080
119#define MVPP2_TXQ_DESC_ADDR_REG 0x2084
120#define MVPP2_TXQ_DESC_SIZE_REG 0x2088
121#define MVPP2_TXQ_DESC_SIZE_MASK 0x3ff0
122#define MVPP2_AGGR_TXQ_UPDATE_REG 0x2090
Marcin Wojtas3f518502014-07-10 16:52:13 -0300123#define MVPP2_TXQ_INDEX_REG 0x2098
124#define MVPP2_TXQ_PREF_BUF_REG 0x209c
125#define MVPP2_PREF_BUF_PTR(desc) ((desc) & 0xfff)
126#define MVPP2_PREF_BUF_SIZE_4 (BIT(12) | BIT(13))
127#define MVPP2_PREF_BUF_SIZE_16 (BIT(12) | BIT(14))
128#define MVPP2_PREF_BUF_THRESH(val) ((val) << 17)
129#define MVPP2_TXQ_DRAIN_EN_MASK BIT(31)
130#define MVPP2_TXQ_PENDING_REG 0x20a0
131#define MVPP2_TXQ_PENDING_MASK 0x3fff
132#define MVPP2_TXQ_INT_STATUS_REG 0x20a4
133#define MVPP2_TXQ_SENT_REG(txq) (0x3c00 + 4 * (txq))
134#define MVPP2_TRANSMITTED_COUNT_OFFSET 16
135#define MVPP2_TRANSMITTED_COUNT_MASK 0x3fff0000
136#define MVPP2_TXQ_RSVD_REQ_REG 0x20b0
137#define MVPP2_TXQ_RSVD_REQ_Q_OFFSET 16
138#define MVPP2_TXQ_RSVD_RSLT_REG 0x20b4
139#define MVPP2_TXQ_RSVD_RSLT_MASK 0x3fff
140#define MVPP2_TXQ_RSVD_CLR_REG 0x20b8
141#define MVPP2_TXQ_RSVD_CLR_OFFSET 16
142#define MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu) (0x2100 + 4 * (cpu))
143#define MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu) (0x2140 + 4 * (cpu))
144#define MVPP2_AGGR_TXQ_DESC_SIZE_MASK 0x3ff0
145#define MVPP2_AGGR_TXQ_STATUS_REG(cpu) (0x2180 + 4 * (cpu))
146#define MVPP2_AGGR_TXQ_PENDING_MASK 0x3fff
147#define MVPP2_AGGR_TXQ_INDEX_REG(cpu) (0x21c0 + 4 * (cpu))
148
149/* MBUS bridge registers */
150#define MVPP2_WIN_BASE(w) (0x4000 + ((w) << 2))
151#define MVPP2_WIN_SIZE(w) (0x4020 + ((w) << 2))
152#define MVPP2_WIN_REMAP(w) (0x4040 + ((w) << 2))
153#define MVPP2_BASE_ADDR_ENABLE 0x4060
154
155/* Interrupt Cause and Mask registers */
156#define MVPP2_ISR_RX_THRESHOLD_REG(rxq) (0x5200 + 4 * (rxq))
Thomas Petazzoniab426762017-02-21 11:28:04 +0100157#define MVPP2_MAX_ISR_RX_THRESHOLD 0xfffff0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300158#define MVPP2_ISR_RXQ_GROUP_REG(rxq) (0x5400 + 4 * (rxq))
159#define MVPP2_ISR_ENABLE_REG(port) (0x5420 + 4 * (port))
160#define MVPP2_ISR_ENABLE_INTERRUPT(mask) ((mask) & 0xffff)
161#define MVPP2_ISR_DISABLE_INTERRUPT(mask) (((mask) << 16) & 0xffff0000)
162#define MVPP2_ISR_RX_TX_CAUSE_REG(port) (0x5480 + 4 * (port))
163#define MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK 0xffff
164#define MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK 0xff0000
165#define MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK BIT(24)
166#define MVPP2_CAUSE_FCS_ERR_MASK BIT(25)
167#define MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK BIT(26)
168#define MVPP2_CAUSE_TX_EXCEPTION_SUM_MASK BIT(29)
169#define MVPP2_CAUSE_RX_EXCEPTION_SUM_MASK BIT(30)
170#define MVPP2_CAUSE_MISC_SUM_MASK BIT(31)
171#define MVPP2_ISR_RX_TX_MASK_REG(port) (0x54a0 + 4 * (port))
172#define MVPP2_ISR_PON_RX_TX_MASK_REG 0x54bc
173#define MVPP2_PON_CAUSE_RXQ_OCCUP_DESC_ALL_MASK 0xffff
174#define MVPP2_PON_CAUSE_TXP_OCCUP_DESC_ALL_MASK 0x3fc00000
175#define MVPP2_PON_CAUSE_MISC_SUM_MASK BIT(31)
176#define MVPP2_ISR_MISC_CAUSE_REG 0x55b0
177
178/* Buffer Manager registers */
179#define MVPP2_BM_POOL_BASE_REG(pool) (0x6000 + ((pool) * 4))
180#define MVPP2_BM_POOL_BASE_ADDR_MASK 0xfffff80
181#define MVPP2_BM_POOL_SIZE_REG(pool) (0x6040 + ((pool) * 4))
182#define MVPP2_BM_POOL_SIZE_MASK 0xfff0
183#define MVPP2_BM_POOL_READ_PTR_REG(pool) (0x6080 + ((pool) * 4))
184#define MVPP2_BM_POOL_GET_READ_PTR_MASK 0xfff0
185#define MVPP2_BM_POOL_PTRS_NUM_REG(pool) (0x60c0 + ((pool) * 4))
186#define MVPP2_BM_POOL_PTRS_NUM_MASK 0xfff0
187#define MVPP2_BM_BPPI_READ_PTR_REG(pool) (0x6100 + ((pool) * 4))
188#define MVPP2_BM_BPPI_PTRS_NUM_REG(pool) (0x6140 + ((pool) * 4))
189#define MVPP2_BM_BPPI_PTR_NUM_MASK 0x7ff
190#define MVPP2_BM_BPPI_PREFETCH_FULL_MASK BIT(16)
191#define MVPP2_BM_POOL_CTRL_REG(pool) (0x6200 + ((pool) * 4))
192#define MVPP2_BM_START_MASK BIT(0)
193#define MVPP2_BM_STOP_MASK BIT(1)
194#define MVPP2_BM_STATE_MASK BIT(4)
195#define MVPP2_BM_LOW_THRESH_OFFS 8
196#define MVPP2_BM_LOW_THRESH_MASK 0x7f00
197#define MVPP2_BM_LOW_THRESH_VALUE(val) ((val) << \
198 MVPP2_BM_LOW_THRESH_OFFS)
199#define MVPP2_BM_HIGH_THRESH_OFFS 16
200#define MVPP2_BM_HIGH_THRESH_MASK 0x7f0000
201#define MVPP2_BM_HIGH_THRESH_VALUE(val) ((val) << \
202 MVPP2_BM_HIGH_THRESH_OFFS)
203#define MVPP2_BM_INTR_CAUSE_REG(pool) (0x6240 + ((pool) * 4))
204#define MVPP2_BM_RELEASED_DELAY_MASK BIT(0)
205#define MVPP2_BM_ALLOC_FAILED_MASK BIT(1)
206#define MVPP2_BM_BPPE_EMPTY_MASK BIT(2)
207#define MVPP2_BM_BPPE_FULL_MASK BIT(3)
208#define MVPP2_BM_AVAILABLE_BP_LOW_MASK BIT(4)
209#define MVPP2_BM_INTR_MASK_REG(pool) (0x6280 + ((pool) * 4))
210#define MVPP2_BM_PHY_ALLOC_REG(pool) (0x6400 + ((pool) * 4))
211#define MVPP2_BM_PHY_ALLOC_GRNTD_MASK BIT(0)
212#define MVPP2_BM_VIRT_ALLOC_REG 0x6440
Thomas Petazzonid01524d2017-03-07 16:53:09 +0100213#define MVPP22_BM_ADDR_HIGH_ALLOC 0x6444
214#define MVPP22_BM_ADDR_HIGH_PHYS_MASK 0xff
215#define MVPP22_BM_ADDR_HIGH_VIRT_MASK 0xff00
216#define MVPP22_BM_ADDR_HIGH_VIRT_SHIFT 8
Marcin Wojtas3f518502014-07-10 16:52:13 -0300217#define MVPP2_BM_PHY_RLS_REG(pool) (0x6480 + ((pool) * 4))
218#define MVPP2_BM_PHY_RLS_MC_BUFF_MASK BIT(0)
219#define MVPP2_BM_PHY_RLS_PRIO_EN_MASK BIT(1)
220#define MVPP2_BM_PHY_RLS_GRNTD_MASK BIT(2)
221#define MVPP2_BM_VIRT_RLS_REG 0x64c0
Thomas Petazzonid01524d2017-03-07 16:53:09 +0100222#define MVPP22_BM_ADDR_HIGH_RLS_REG 0x64c4
223#define MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK 0xff
224#define MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK 0xff00
225#define MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT 8
Marcin Wojtas3f518502014-07-10 16:52:13 -0300226
227/* TX Scheduler registers */
228#define MVPP2_TXP_SCHED_PORT_INDEX_REG 0x8000
229#define MVPP2_TXP_SCHED_Q_CMD_REG 0x8004
230#define MVPP2_TXP_SCHED_ENQ_MASK 0xff
231#define MVPP2_TXP_SCHED_DISQ_OFFSET 8
232#define MVPP2_TXP_SCHED_CMD_1_REG 0x8010
233#define MVPP2_TXP_SCHED_PERIOD_REG 0x8018
234#define MVPP2_TXP_SCHED_MTU_REG 0x801c
235#define MVPP2_TXP_MTU_MAX 0x7FFFF
236#define MVPP2_TXP_SCHED_REFILL_REG 0x8020
237#define MVPP2_TXP_REFILL_TOKENS_ALL_MASK 0x7ffff
238#define MVPP2_TXP_REFILL_PERIOD_ALL_MASK 0x3ff00000
239#define MVPP2_TXP_REFILL_PERIOD_MASK(v) ((v) << 20)
240#define MVPP2_TXP_SCHED_TOKEN_SIZE_REG 0x8024
241#define MVPP2_TXP_TOKEN_SIZE_MAX 0xffffffff
242#define MVPP2_TXQ_SCHED_REFILL_REG(q) (0x8040 + ((q) << 2))
243#define MVPP2_TXQ_REFILL_TOKENS_ALL_MASK 0x7ffff
244#define MVPP2_TXQ_REFILL_PERIOD_ALL_MASK 0x3ff00000
245#define MVPP2_TXQ_REFILL_PERIOD_MASK(v) ((v) << 20)
246#define MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(q) (0x8060 + ((q) << 2))
247#define MVPP2_TXQ_TOKEN_SIZE_MAX 0x7fffffff
248#define MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(q) (0x8080 + ((q) << 2))
249#define MVPP2_TXQ_TOKEN_CNTR_MAX 0xffffffff
250
251/* TX general registers */
252#define MVPP2_TX_SNOOP_REG 0x8800
253#define MVPP2_TX_PORT_FLUSH_REG 0x8810
254#define MVPP2_TX_PORT_FLUSH_MASK(port) (1 << (port))
255
256/* LMS registers */
257#define MVPP2_SRC_ADDR_MIDDLE 0x24
258#define MVPP2_SRC_ADDR_HIGH 0x28
Marcin Wojtas08a23752014-07-21 13:48:12 -0300259#define MVPP2_PHY_AN_CFG0_REG 0x34
260#define MVPP2_PHY_AN_STOP_SMI0_MASK BIT(7)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300261#define MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG 0x305c
Thomas Petazzoni31d76772017-02-21 11:28:10 +0100262#define MVPP2_EXT_GLOBAL_CTRL_DEFAULT 0x27
Marcin Wojtas3f518502014-07-10 16:52:13 -0300263
264/* Per-port registers */
265#define MVPP2_GMAC_CTRL_0_REG 0x0
266#define MVPP2_GMAC_PORT_EN_MASK BIT(0)
267#define MVPP2_GMAC_MAX_RX_SIZE_OFFS 2
268#define MVPP2_GMAC_MAX_RX_SIZE_MASK 0x7ffc
269#define MVPP2_GMAC_MIB_CNTR_EN_MASK BIT(15)
270#define MVPP2_GMAC_CTRL_1_REG 0x4
Marcin Wojtasb5c0a802014-07-21 13:48:11 -0300271#define MVPP2_GMAC_PERIODIC_XON_EN_MASK BIT(1)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300272#define MVPP2_GMAC_GMII_LB_EN_MASK BIT(5)
273#define MVPP2_GMAC_PCS_LB_EN_BIT 6
274#define MVPP2_GMAC_PCS_LB_EN_MASK BIT(6)
275#define MVPP2_GMAC_SA_LOW_OFFS 7
276#define MVPP2_GMAC_CTRL_2_REG 0x8
277#define MVPP2_GMAC_INBAND_AN_MASK BIT(0)
278#define MVPP2_GMAC_PCS_ENABLE_MASK BIT(3)
279#define MVPP2_GMAC_PORT_RGMII_MASK BIT(4)
280#define MVPP2_GMAC_PORT_RESET_MASK BIT(6)
281#define MVPP2_GMAC_AUTONEG_CONFIG 0xc
282#define MVPP2_GMAC_FORCE_LINK_DOWN BIT(0)
283#define MVPP2_GMAC_FORCE_LINK_PASS BIT(1)
284#define MVPP2_GMAC_CONFIG_MII_SPEED BIT(5)
285#define MVPP2_GMAC_CONFIG_GMII_SPEED BIT(6)
286#define MVPP2_GMAC_AN_SPEED_EN BIT(7)
Marcin Wojtas08a23752014-07-21 13:48:12 -0300287#define MVPP2_GMAC_FC_ADV_EN BIT(9)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300288#define MVPP2_GMAC_CONFIG_FULL_DUPLEX BIT(12)
289#define MVPP2_GMAC_AN_DUPLEX_EN BIT(13)
290#define MVPP2_GMAC_PORT_FIFO_CFG_1_REG 0x1c
291#define MVPP2_GMAC_TX_FIFO_MIN_TH_OFFS 6
292#define MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK 0x1fc0
293#define MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(v) (((v) << 6) & \
294 MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK)
295
296#define MVPP2_CAUSE_TXQ_SENT_DESC_ALL_MASK 0xff
297
298/* Descriptor ring Macros */
299#define MVPP2_QUEUE_NEXT_DESC(q, index) \
300 (((index) < (q)->last_desc) ? ((index) + 1) : 0)
301
302/* Various constants */
303
304/* Coalescing */
305#define MVPP2_TXDONE_COAL_PKTS_THRESH 15
Marcin Wojtasedc660f2015-08-06 19:00:30 +0200306#define MVPP2_TXDONE_HRTIMER_PERIOD_NS 1000000UL
Marcin Wojtas3f518502014-07-10 16:52:13 -0300307#define MVPP2_RX_COAL_PKTS 32
308#define MVPP2_RX_COAL_USEC 100
309
310/* The two bytes Marvell header. Either contains a special value used
311 * by Marvell switches when a specific hardware mode is enabled (not
312 * supported by this driver) or is filled automatically by zeroes on
313 * the RX side. Those two bytes being at the front of the Ethernet
314 * header, they allow to have the IP header aligned on a 4 bytes
315 * boundary automatically: the hardware skips those two bytes on its
316 * own.
317 */
318#define MVPP2_MH_SIZE 2
319#define MVPP2_ETH_TYPE_LEN 2
320#define MVPP2_PPPOE_HDR_SIZE 8
321#define MVPP2_VLAN_TAG_LEN 4
322
323/* Lbtd 802.3 type */
324#define MVPP2_IP_LBDT_TYPE 0xfffa
325
Marcin Wojtas3f518502014-07-10 16:52:13 -0300326#define MVPP2_TX_CSUM_MAX_SIZE 9800
327
328/* Timeout constants */
329#define MVPP2_TX_DISABLE_TIMEOUT_MSEC 1000
330#define MVPP2_TX_PENDING_TIMEOUT_MSEC 1000
331
332#define MVPP2_TX_MTU_MAX 0x7ffff
333
334/* Maximum number of T-CONTs of PON port */
335#define MVPP2_MAX_TCONT 16
336
337/* Maximum number of supported ports */
338#define MVPP2_MAX_PORTS 4
339
340/* Maximum number of TXQs used by single port */
341#define MVPP2_MAX_TXQ 8
342
343/* Maximum number of RXQs used by single port */
344#define MVPP2_MAX_RXQ 8
345
346/* Dfault number of RXQs in use */
347#define MVPP2_DEFAULT_RXQ 4
348
349/* Total number of RXQs available to all ports */
350#define MVPP2_RXQ_TOTAL_NUM (MVPP2_MAX_PORTS * MVPP2_MAX_RXQ)
351
352/* Max number of Rx descriptors */
353#define MVPP2_MAX_RXD 128
354
355/* Max number of Tx descriptors */
356#define MVPP2_MAX_TXD 1024
357
358/* Amount of Tx descriptors that can be reserved at once by CPU */
359#define MVPP2_CPU_DESC_CHUNK 64
360
361/* Max number of Tx descriptors in each aggregated queue */
362#define MVPP2_AGGR_TXQ_SIZE 256
363
364/* Descriptor aligned size */
365#define MVPP2_DESC_ALIGNED_SIZE 32
366
367/* Descriptor alignment mask */
368#define MVPP2_TX_DESC_ALIGN (MVPP2_DESC_ALIGNED_SIZE - 1)
369
370/* RX FIFO constants */
371#define MVPP2_RX_FIFO_PORT_DATA_SIZE 0x2000
372#define MVPP2_RX_FIFO_PORT_ATTR_SIZE 0x80
373#define MVPP2_RX_FIFO_PORT_MIN_PKT 0x80
374
375/* RX buffer constants */
376#define MVPP2_SKB_SHINFO_SIZE \
377 SKB_DATA_ALIGN(sizeof(struct skb_shared_info))
378
379#define MVPP2_RX_PKT_SIZE(mtu) \
380 ALIGN((mtu) + MVPP2_MH_SIZE + MVPP2_VLAN_TAG_LEN + \
Jisheng Zhang4a0a12d2016-04-01 17:11:05 +0800381 ETH_HLEN + ETH_FCS_LEN, cache_line_size())
Marcin Wojtas3f518502014-07-10 16:52:13 -0300382
383#define MVPP2_RX_BUF_SIZE(pkt_size) ((pkt_size) + NET_SKB_PAD)
384#define MVPP2_RX_TOTAL_SIZE(buf_size) ((buf_size) + MVPP2_SKB_SHINFO_SIZE)
385#define MVPP2_RX_MAX_PKT_SIZE(total_size) \
386 ((total_size) - NET_SKB_PAD - MVPP2_SKB_SHINFO_SIZE)
387
388#define MVPP2_BIT_TO_BYTE(bit) ((bit) / 8)
389
390/* IPv6 max L3 address size */
391#define MVPP2_MAX_L3_ADDR_SIZE 16
392
393/* Port flags */
394#define MVPP2_F_LOOPBACK BIT(0)
395
396/* Marvell tag types */
397enum mvpp2_tag_type {
398 MVPP2_TAG_TYPE_NONE = 0,
399 MVPP2_TAG_TYPE_MH = 1,
400 MVPP2_TAG_TYPE_DSA = 2,
401 MVPP2_TAG_TYPE_EDSA = 3,
402 MVPP2_TAG_TYPE_VLAN = 4,
403 MVPP2_TAG_TYPE_LAST = 5
404};
405
406/* Parser constants */
407#define MVPP2_PRS_TCAM_SRAM_SIZE 256
408#define MVPP2_PRS_TCAM_WORDS 6
409#define MVPP2_PRS_SRAM_WORDS 4
410#define MVPP2_PRS_FLOW_ID_SIZE 64
411#define MVPP2_PRS_FLOW_ID_MASK 0x3f
412#define MVPP2_PRS_TCAM_ENTRY_INVALID 1
413#define MVPP2_PRS_TCAM_DSA_TAGGED_BIT BIT(5)
414#define MVPP2_PRS_IPV4_HEAD 0x40
415#define MVPP2_PRS_IPV4_HEAD_MASK 0xf0
416#define MVPP2_PRS_IPV4_MC 0xe0
417#define MVPP2_PRS_IPV4_MC_MASK 0xf0
418#define MVPP2_PRS_IPV4_BC_MASK 0xff
419#define MVPP2_PRS_IPV4_IHL 0x5
420#define MVPP2_PRS_IPV4_IHL_MASK 0xf
421#define MVPP2_PRS_IPV6_MC 0xff
422#define MVPP2_PRS_IPV6_MC_MASK 0xff
423#define MVPP2_PRS_IPV6_HOP_MASK 0xff
424#define MVPP2_PRS_TCAM_PROTO_MASK 0xff
425#define MVPP2_PRS_TCAM_PROTO_MASK_L 0x3f
426#define MVPP2_PRS_DBL_VLANS_MAX 100
427
428/* Tcam structure:
429 * - lookup ID - 4 bits
430 * - port ID - 1 byte
431 * - additional information - 1 byte
432 * - header data - 8 bytes
433 * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(5)->(0).
434 */
435#define MVPP2_PRS_AI_BITS 8
436#define MVPP2_PRS_PORT_MASK 0xff
437#define MVPP2_PRS_LU_MASK 0xf
438#define MVPP2_PRS_TCAM_DATA_BYTE(offs) \
439 (((offs) - ((offs) % 2)) * 2 + ((offs) % 2))
440#define MVPP2_PRS_TCAM_DATA_BYTE_EN(offs) \
441 (((offs) * 2) - ((offs) % 2) + 2)
442#define MVPP2_PRS_TCAM_AI_BYTE 16
443#define MVPP2_PRS_TCAM_PORT_BYTE 17
444#define MVPP2_PRS_TCAM_LU_BYTE 20
445#define MVPP2_PRS_TCAM_EN_OFFS(offs) ((offs) + 2)
446#define MVPP2_PRS_TCAM_INV_WORD 5
447/* Tcam entries ID */
448#define MVPP2_PE_DROP_ALL 0
449#define MVPP2_PE_FIRST_FREE_TID 1
450#define MVPP2_PE_LAST_FREE_TID (MVPP2_PRS_TCAM_SRAM_SIZE - 31)
451#define MVPP2_PE_IP6_EXT_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 30)
452#define MVPP2_PE_MAC_MC_IP6 (MVPP2_PRS_TCAM_SRAM_SIZE - 29)
453#define MVPP2_PE_IP6_ADDR_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 28)
454#define MVPP2_PE_IP4_ADDR_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 27)
455#define MVPP2_PE_LAST_DEFAULT_FLOW (MVPP2_PRS_TCAM_SRAM_SIZE - 26)
456#define MVPP2_PE_FIRST_DEFAULT_FLOW (MVPP2_PRS_TCAM_SRAM_SIZE - 19)
457#define MVPP2_PE_EDSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 18)
458#define MVPP2_PE_EDSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 17)
459#define MVPP2_PE_DSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 16)
460#define MVPP2_PE_DSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 15)
461#define MVPP2_PE_ETYPE_EDSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 14)
462#define MVPP2_PE_ETYPE_EDSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 13)
463#define MVPP2_PE_ETYPE_DSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 12)
464#define MVPP2_PE_ETYPE_DSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 11)
465#define MVPP2_PE_MH_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 10)
466#define MVPP2_PE_DSA_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 9)
467#define MVPP2_PE_IP6_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 8)
468#define MVPP2_PE_IP4_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 7)
469#define MVPP2_PE_ETH_TYPE_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 6)
470#define MVPP2_PE_VLAN_DBL (MVPP2_PRS_TCAM_SRAM_SIZE - 5)
471#define MVPP2_PE_VLAN_NONE (MVPP2_PRS_TCAM_SRAM_SIZE - 4)
472#define MVPP2_PE_MAC_MC_ALL (MVPP2_PRS_TCAM_SRAM_SIZE - 3)
473#define MVPP2_PE_MAC_PROMISCUOUS (MVPP2_PRS_TCAM_SRAM_SIZE - 2)
474#define MVPP2_PE_MAC_NON_PROMISCUOUS (MVPP2_PRS_TCAM_SRAM_SIZE - 1)
475
476/* Sram structure
477 * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(3)->(0).
478 */
479#define MVPP2_PRS_SRAM_RI_OFFS 0
480#define MVPP2_PRS_SRAM_RI_WORD 0
481#define MVPP2_PRS_SRAM_RI_CTRL_OFFS 32
482#define MVPP2_PRS_SRAM_RI_CTRL_WORD 1
483#define MVPP2_PRS_SRAM_RI_CTRL_BITS 32
484#define MVPP2_PRS_SRAM_SHIFT_OFFS 64
485#define MVPP2_PRS_SRAM_SHIFT_SIGN_BIT 72
486#define MVPP2_PRS_SRAM_UDF_OFFS 73
487#define MVPP2_PRS_SRAM_UDF_BITS 8
488#define MVPP2_PRS_SRAM_UDF_MASK 0xff
489#define MVPP2_PRS_SRAM_UDF_SIGN_BIT 81
490#define MVPP2_PRS_SRAM_UDF_TYPE_OFFS 82
491#define MVPP2_PRS_SRAM_UDF_TYPE_MASK 0x7
492#define MVPP2_PRS_SRAM_UDF_TYPE_L3 1
493#define MVPP2_PRS_SRAM_UDF_TYPE_L4 4
494#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS 85
495#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK 0x3
496#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD 1
497#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP4_ADD 2
498#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP6_ADD 3
499#define MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS 87
500#define MVPP2_PRS_SRAM_OP_SEL_UDF_BITS 2
501#define MVPP2_PRS_SRAM_OP_SEL_UDF_MASK 0x3
502#define MVPP2_PRS_SRAM_OP_SEL_UDF_ADD 0
503#define MVPP2_PRS_SRAM_OP_SEL_UDF_IP4_ADD 2
504#define MVPP2_PRS_SRAM_OP_SEL_UDF_IP6_ADD 3
505#define MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS 89
506#define MVPP2_PRS_SRAM_AI_OFFS 90
507#define MVPP2_PRS_SRAM_AI_CTRL_OFFS 98
508#define MVPP2_PRS_SRAM_AI_CTRL_BITS 8
509#define MVPP2_PRS_SRAM_AI_MASK 0xff
510#define MVPP2_PRS_SRAM_NEXT_LU_OFFS 106
511#define MVPP2_PRS_SRAM_NEXT_LU_MASK 0xf
512#define MVPP2_PRS_SRAM_LU_DONE_BIT 110
513#define MVPP2_PRS_SRAM_LU_GEN_BIT 111
514
515/* Sram result info bits assignment */
516#define MVPP2_PRS_RI_MAC_ME_MASK 0x1
517#define MVPP2_PRS_RI_DSA_MASK 0x2
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100518#define MVPP2_PRS_RI_VLAN_MASK (BIT(2) | BIT(3))
519#define MVPP2_PRS_RI_VLAN_NONE 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300520#define MVPP2_PRS_RI_VLAN_SINGLE BIT(2)
521#define MVPP2_PRS_RI_VLAN_DOUBLE BIT(3)
522#define MVPP2_PRS_RI_VLAN_TRIPLE (BIT(2) | BIT(3))
523#define MVPP2_PRS_RI_CPU_CODE_MASK 0x70
524#define MVPP2_PRS_RI_CPU_CODE_RX_SPEC BIT(4)
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100525#define MVPP2_PRS_RI_L2_CAST_MASK (BIT(9) | BIT(10))
526#define MVPP2_PRS_RI_L2_UCAST 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300527#define MVPP2_PRS_RI_L2_MCAST BIT(9)
528#define MVPP2_PRS_RI_L2_BCAST BIT(10)
529#define MVPP2_PRS_RI_PPPOE_MASK 0x800
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100530#define MVPP2_PRS_RI_L3_PROTO_MASK (BIT(12) | BIT(13) | BIT(14))
531#define MVPP2_PRS_RI_L3_UN 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300532#define MVPP2_PRS_RI_L3_IP4 BIT(12)
533#define MVPP2_PRS_RI_L3_IP4_OPT BIT(13)
534#define MVPP2_PRS_RI_L3_IP4_OTHER (BIT(12) | BIT(13))
535#define MVPP2_PRS_RI_L3_IP6 BIT(14)
536#define MVPP2_PRS_RI_L3_IP6_EXT (BIT(12) | BIT(14))
537#define MVPP2_PRS_RI_L3_ARP (BIT(13) | BIT(14))
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100538#define MVPP2_PRS_RI_L3_ADDR_MASK (BIT(15) | BIT(16))
539#define MVPP2_PRS_RI_L3_UCAST 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300540#define MVPP2_PRS_RI_L3_MCAST BIT(15)
541#define MVPP2_PRS_RI_L3_BCAST (BIT(15) | BIT(16))
542#define MVPP2_PRS_RI_IP_FRAG_MASK 0x20000
543#define MVPP2_PRS_RI_UDF3_MASK 0x300000
544#define MVPP2_PRS_RI_UDF3_RX_SPECIAL BIT(21)
545#define MVPP2_PRS_RI_L4_PROTO_MASK 0x1c00000
546#define MVPP2_PRS_RI_L4_TCP BIT(22)
547#define MVPP2_PRS_RI_L4_UDP BIT(23)
548#define MVPP2_PRS_RI_L4_OTHER (BIT(22) | BIT(23))
549#define MVPP2_PRS_RI_UDF7_MASK 0x60000000
550#define MVPP2_PRS_RI_UDF7_IP6_LITE BIT(29)
551#define MVPP2_PRS_RI_DROP_MASK 0x80000000
552
553/* Sram additional info bits assignment */
554#define MVPP2_PRS_IPV4_DIP_AI_BIT BIT(0)
555#define MVPP2_PRS_IPV6_NO_EXT_AI_BIT BIT(0)
556#define MVPP2_PRS_IPV6_EXT_AI_BIT BIT(1)
557#define MVPP2_PRS_IPV6_EXT_AH_AI_BIT BIT(2)
558#define MVPP2_PRS_IPV6_EXT_AH_LEN_AI_BIT BIT(3)
559#define MVPP2_PRS_IPV6_EXT_AH_L4_AI_BIT BIT(4)
560#define MVPP2_PRS_SINGLE_VLAN_AI 0
561#define MVPP2_PRS_DBL_VLAN_AI_BIT BIT(7)
562
563/* DSA/EDSA type */
564#define MVPP2_PRS_TAGGED true
565#define MVPP2_PRS_UNTAGGED false
566#define MVPP2_PRS_EDSA true
567#define MVPP2_PRS_DSA false
568
569/* MAC entries, shadow udf */
570enum mvpp2_prs_udf {
571 MVPP2_PRS_UDF_MAC_DEF,
572 MVPP2_PRS_UDF_MAC_RANGE,
573 MVPP2_PRS_UDF_L2_DEF,
574 MVPP2_PRS_UDF_L2_DEF_COPY,
575 MVPP2_PRS_UDF_L2_USER,
576};
577
578/* Lookup ID */
579enum mvpp2_prs_lookup {
580 MVPP2_PRS_LU_MH,
581 MVPP2_PRS_LU_MAC,
582 MVPP2_PRS_LU_DSA,
583 MVPP2_PRS_LU_VLAN,
584 MVPP2_PRS_LU_L2,
585 MVPP2_PRS_LU_PPPOE,
586 MVPP2_PRS_LU_IP4,
587 MVPP2_PRS_LU_IP6,
588 MVPP2_PRS_LU_FLOWS,
589 MVPP2_PRS_LU_LAST,
590};
591
592/* L3 cast enum */
593enum mvpp2_prs_l3_cast {
594 MVPP2_PRS_L3_UNI_CAST,
595 MVPP2_PRS_L3_MULTI_CAST,
596 MVPP2_PRS_L3_BROAD_CAST
597};
598
599/* Classifier constants */
600#define MVPP2_CLS_FLOWS_TBL_SIZE 512
601#define MVPP2_CLS_FLOWS_TBL_DATA_WORDS 3
602#define MVPP2_CLS_LKP_TBL_SIZE 64
603
604/* BM constants */
605#define MVPP2_BM_POOLS_NUM 8
606#define MVPP2_BM_LONG_BUF_NUM 1024
607#define MVPP2_BM_SHORT_BUF_NUM 2048
608#define MVPP2_BM_POOL_SIZE_MAX (16*1024 - MVPP2_BM_POOL_PTR_ALIGN/4)
609#define MVPP2_BM_POOL_PTR_ALIGN 128
610#define MVPP2_BM_SWF_LONG_POOL(port) ((port > 2) ? 2 : port)
611#define MVPP2_BM_SWF_SHORT_POOL 3
612
613/* BM cookie (32 bits) definition */
614#define MVPP2_BM_COOKIE_POOL_OFFS 8
615#define MVPP2_BM_COOKIE_CPU_OFFS 24
616
617/* BM short pool packet size
618 * These value assure that for SWF the total number
619 * of bytes allocated for each buffer will be 512
620 */
621#define MVPP2_BM_SHORT_PKT_SIZE MVPP2_RX_MAX_PKT_SIZE(512)
622
623enum mvpp2_bm_type {
624 MVPP2_BM_FREE,
625 MVPP2_BM_SWF_LONG,
626 MVPP2_BM_SWF_SHORT
627};
628
629/* Definitions */
630
631/* Shared Packet Processor resources */
632struct mvpp2 {
633 /* Shared registers' base addresses */
634 void __iomem *base;
635 void __iomem *lms_base;
636
637 /* Common clocks */
638 struct clk *pp_clk;
639 struct clk *gop_clk;
640
641 /* List of pointers to port structures */
642 struct mvpp2_port **port_list;
643
644 /* Aggregated TXQs */
645 struct mvpp2_tx_queue *aggr_txqs;
646
647 /* BM pools */
648 struct mvpp2_bm_pool *bm_pools;
649
650 /* PRS shadow table */
651 struct mvpp2_prs_shadow *prs_shadow;
652 /* PRS auxiliary table for double vlan entries control */
653 bool *prs_double_vlans;
654
655 /* Tclk value */
656 u32 tclk;
Thomas Petazzonifaca9242017-03-07 16:53:06 +0100657
658 /* HW version */
659 enum { MVPP21, MVPP22 } hw_version;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300660};
661
662struct mvpp2_pcpu_stats {
663 struct u64_stats_sync syncp;
664 u64 rx_packets;
665 u64 rx_bytes;
666 u64 tx_packets;
667 u64 tx_bytes;
668};
669
Marcin Wojtasedc660f2015-08-06 19:00:30 +0200670/* Per-CPU port control */
671struct mvpp2_port_pcpu {
672 struct hrtimer tx_done_timer;
673 bool timer_scheduled;
674 /* Tasklet for egress finalization */
675 struct tasklet_struct tx_done_tasklet;
676};
677
Marcin Wojtas3f518502014-07-10 16:52:13 -0300678struct mvpp2_port {
679 u8 id;
680
681 int irq;
682
683 struct mvpp2 *priv;
684
685 /* Per-port registers' base address */
686 void __iomem *base;
687
688 struct mvpp2_rx_queue **rxqs;
689 struct mvpp2_tx_queue **txqs;
690 struct net_device *dev;
691
692 int pkt_size;
693
694 u32 pending_cause_rx;
695 struct napi_struct napi;
696
Marcin Wojtasedc660f2015-08-06 19:00:30 +0200697 /* Per-CPU port control */
698 struct mvpp2_port_pcpu __percpu *pcpu;
699
Marcin Wojtas3f518502014-07-10 16:52:13 -0300700 /* Flags */
701 unsigned long flags;
702
703 u16 tx_ring_size;
704 u16 rx_ring_size;
705 struct mvpp2_pcpu_stats __percpu *stats;
706
Marcin Wojtas3f518502014-07-10 16:52:13 -0300707 phy_interface_t phy_interface;
708 struct device_node *phy_node;
709 unsigned int link;
710 unsigned int duplex;
711 unsigned int speed;
712
713 struct mvpp2_bm_pool *pool_long;
714 struct mvpp2_bm_pool *pool_short;
715
716 /* Index of first port's physical RXQ */
717 u8 first_rxq;
718};
719
720/* The mvpp2_tx_desc and mvpp2_rx_desc structures describe the
721 * layout of the transmit and reception DMA descriptors, and their
722 * layout is therefore defined by the hardware design
723 */
724
725#define MVPP2_TXD_L3_OFF_SHIFT 0
726#define MVPP2_TXD_IP_HLEN_SHIFT 8
727#define MVPP2_TXD_L4_CSUM_FRAG BIT(13)
728#define MVPP2_TXD_L4_CSUM_NOT BIT(14)
729#define MVPP2_TXD_IP_CSUM_DISABLE BIT(15)
730#define MVPP2_TXD_PADDING_DISABLE BIT(23)
731#define MVPP2_TXD_L4_UDP BIT(24)
732#define MVPP2_TXD_L3_IP6 BIT(26)
733#define MVPP2_TXD_L_DESC BIT(28)
734#define MVPP2_TXD_F_DESC BIT(29)
735
736#define MVPP2_RXD_ERR_SUMMARY BIT(15)
737#define MVPP2_RXD_ERR_CODE_MASK (BIT(13) | BIT(14))
738#define MVPP2_RXD_ERR_CRC 0x0
739#define MVPP2_RXD_ERR_OVERRUN BIT(13)
740#define MVPP2_RXD_ERR_RESOURCE (BIT(13) | BIT(14))
741#define MVPP2_RXD_BM_POOL_ID_OFFS 16
742#define MVPP2_RXD_BM_POOL_ID_MASK (BIT(16) | BIT(17) | BIT(18))
743#define MVPP2_RXD_HWF_SYNC BIT(21)
744#define MVPP2_RXD_L4_CSUM_OK BIT(22)
745#define MVPP2_RXD_IP4_HEADER_ERR BIT(24)
746#define MVPP2_RXD_L4_TCP BIT(25)
747#define MVPP2_RXD_L4_UDP BIT(26)
748#define MVPP2_RXD_L3_IP4 BIT(28)
749#define MVPP2_RXD_L3_IP6 BIT(30)
750#define MVPP2_RXD_BUF_HDR BIT(31)
751
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100752/* HW TX descriptor for PPv2.1 */
753struct mvpp21_tx_desc {
Marcin Wojtas3f518502014-07-10 16:52:13 -0300754 u32 command; /* Options used by HW for packet transmitting.*/
755 u8 packet_offset; /* the offset from the buffer beginning */
756 u8 phys_txq; /* destination queue ID */
757 u16 data_size; /* data size of transmitted packet in bytes */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100758 u32 buf_dma_addr; /* physical addr of transmitted buffer */
Marcin Wojtas3f518502014-07-10 16:52:13 -0300759 u32 buf_cookie; /* cookie for access to TX buffer in tx path */
760 u32 reserved1[3]; /* hw_cmd (for future use, BM, PON, PNC) */
761 u32 reserved2; /* reserved (for future use) */
762};
763
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100764/* HW RX descriptor for PPv2.1 */
765struct mvpp21_rx_desc {
Marcin Wojtas3f518502014-07-10 16:52:13 -0300766 u32 status; /* info about received packet */
767 u16 reserved1; /* parser_info (for future use, PnC) */
768 u16 data_size; /* size of received packet in bytes */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100769 u32 buf_dma_addr; /* physical address of the buffer */
Marcin Wojtas3f518502014-07-10 16:52:13 -0300770 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
771 u16 reserved2; /* gem_port_id (for future use, PON) */
772 u16 reserved3; /* csum_l4 (for future use, PnC) */
773 u8 reserved4; /* bm_qset (for future use, BM) */
774 u8 reserved5;
775 u16 reserved6; /* classify_info (for future use, PnC) */
776 u32 reserved7; /* flow_id (for future use, PnC) */
777 u32 reserved8;
778};
779
Thomas Petazzonie7c53592017-03-07 16:53:08 +0100780/* HW TX descriptor for PPv2.2 */
781struct mvpp22_tx_desc {
782 u32 command;
783 u8 packet_offset;
784 u8 phys_txq;
785 u16 data_size;
786 u64 reserved1;
787 u64 buf_dma_addr_ptp;
788 u64 buf_cookie_misc;
789};
790
791/* HW RX descriptor for PPv2.2 */
792struct mvpp22_rx_desc {
793 u32 status;
794 u16 reserved1;
795 u16 data_size;
796 u32 reserved2;
797 u32 reserved3;
798 u64 buf_dma_addr_key_hash;
799 u64 buf_cookie_misc;
800};
801
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100802/* Opaque type used by the driver to manipulate the HW TX and RX
803 * descriptors
804 */
805struct mvpp2_tx_desc {
806 union {
807 struct mvpp21_tx_desc pp21;
Thomas Petazzonie7c53592017-03-07 16:53:08 +0100808 struct mvpp22_tx_desc pp22;
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100809 };
810};
811
812struct mvpp2_rx_desc {
813 union {
814 struct mvpp21_rx_desc pp21;
Thomas Petazzonie7c53592017-03-07 16:53:08 +0100815 struct mvpp22_rx_desc pp22;
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100816 };
817};
818
Thomas Petazzoni83544912016-12-21 11:28:49 +0100819struct mvpp2_txq_pcpu_buf {
820 /* Transmitted SKB */
821 struct sk_buff *skb;
822
823 /* Physical address of transmitted buffer */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100824 dma_addr_t dma;
Thomas Petazzoni83544912016-12-21 11:28:49 +0100825
826 /* Size transmitted */
827 size_t size;
828};
829
Marcin Wojtas3f518502014-07-10 16:52:13 -0300830/* Per-CPU Tx queue control */
831struct mvpp2_txq_pcpu {
832 int cpu;
833
834 /* Number of Tx DMA descriptors in the descriptor ring */
835 int size;
836
837 /* Number of currently used Tx DMA descriptor in the
838 * descriptor ring
839 */
840 int count;
841
842 /* Number of Tx DMA descriptors reserved for each CPU */
843 int reserved_num;
844
Thomas Petazzoni83544912016-12-21 11:28:49 +0100845 /* Infos about transmitted buffers */
846 struct mvpp2_txq_pcpu_buf *buffs;
Marcin Wojtas71ce3912015-08-06 19:00:29 +0200847
Marcin Wojtas3f518502014-07-10 16:52:13 -0300848 /* Index of last TX DMA descriptor that was inserted */
849 int txq_put_index;
850
851 /* Index of the TX DMA descriptor to be cleaned up */
852 int txq_get_index;
853};
854
855struct mvpp2_tx_queue {
856 /* Physical number of this Tx queue */
857 u8 id;
858
859 /* Logical number of this Tx queue */
860 u8 log_id;
861
862 /* Number of Tx DMA descriptors in the descriptor ring */
863 int size;
864
865 /* Number of currently used Tx DMA descriptor in the descriptor ring */
866 int count;
867
868 /* Per-CPU control of physical Tx queues */
869 struct mvpp2_txq_pcpu __percpu *pcpu;
870
Marcin Wojtas3f518502014-07-10 16:52:13 -0300871 u32 done_pkts_coal;
872
873 /* Virtual address of thex Tx DMA descriptors array */
874 struct mvpp2_tx_desc *descs;
875
876 /* DMA address of the Tx DMA descriptors array */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100877 dma_addr_t descs_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300878
879 /* Index of the last Tx DMA descriptor */
880 int last_desc;
881
882 /* Index of the next Tx DMA descriptor to process */
883 int next_desc_to_proc;
884};
885
886struct mvpp2_rx_queue {
887 /* RX queue number, in the range 0-31 for physical RXQs */
888 u8 id;
889
890 /* Num of rx descriptors in the rx descriptor ring */
891 int size;
892
893 u32 pkts_coal;
894 u32 time_coal;
895
896 /* Virtual address of the RX DMA descriptors array */
897 struct mvpp2_rx_desc *descs;
898
899 /* DMA address of the RX DMA descriptors array */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100900 dma_addr_t descs_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300901
902 /* Index of the last RX DMA descriptor */
903 int last_desc;
904
905 /* Index of the next RX DMA descriptor to process */
906 int next_desc_to_proc;
907
908 /* ID of port to which physical RXQ is mapped */
909 int port;
910
911 /* Port's logic RXQ number to which physical RXQ is mapped */
912 int logic_rxq;
913};
914
915union mvpp2_prs_tcam_entry {
916 u32 word[MVPP2_PRS_TCAM_WORDS];
917 u8 byte[MVPP2_PRS_TCAM_WORDS * 4];
918};
919
920union mvpp2_prs_sram_entry {
921 u32 word[MVPP2_PRS_SRAM_WORDS];
922 u8 byte[MVPP2_PRS_SRAM_WORDS * 4];
923};
924
925struct mvpp2_prs_entry {
926 u32 index;
927 union mvpp2_prs_tcam_entry tcam;
928 union mvpp2_prs_sram_entry sram;
929};
930
931struct mvpp2_prs_shadow {
932 bool valid;
933 bool finish;
934
935 /* Lookup ID */
936 int lu;
937
938 /* User defined offset */
939 int udf;
940
941 /* Result info */
942 u32 ri;
943 u32 ri_mask;
944};
945
946struct mvpp2_cls_flow_entry {
947 u32 index;
948 u32 data[MVPP2_CLS_FLOWS_TBL_DATA_WORDS];
949};
950
951struct mvpp2_cls_lookup_entry {
952 u32 lkpid;
953 u32 way;
954 u32 data;
955};
956
957struct mvpp2_bm_pool {
958 /* Pool number in the range 0-7 */
959 int id;
960 enum mvpp2_bm_type type;
961
962 /* Buffer Pointers Pool External (BPPE) size */
963 int size;
Thomas Petazzonid01524d2017-03-07 16:53:09 +0100964 /* BPPE size in bytes */
965 int size_bytes;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300966 /* Number of buffers for this pool */
967 int buf_num;
968 /* Pool buffer size */
969 int buf_size;
970 /* Packet size */
971 int pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +0100972 int frag_size;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300973
974 /* BPPE virtual base address */
975 u32 *virt_addr;
Thomas Petazzoni20396132017-03-07 16:53:00 +0100976 /* BPPE DMA base address */
977 dma_addr_t dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300978
979 /* Ports using BM pool */
980 u32 port_map;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300981};
982
Marcin Wojtas3f518502014-07-10 16:52:13 -0300983/* Static declaractions */
984
985/* Number of RXQs used by single port */
986static int rxq_number = MVPP2_DEFAULT_RXQ;
987/* Number of TXQs used by single port */
988static int txq_number = MVPP2_MAX_TXQ;
989
990#define MVPP2_DRIVER_NAME "mvpp2"
991#define MVPP2_DRIVER_VERSION "1.0"
992
993/* Utility/helper methods */
994
995static void mvpp2_write(struct mvpp2 *priv, u32 offset, u32 data)
996{
997 writel(data, priv->base + offset);
998}
999
1000static u32 mvpp2_read(struct mvpp2 *priv, u32 offset)
1001{
1002 return readl(priv->base + offset);
1003}
1004
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001005static dma_addr_t mvpp2_txdesc_dma_addr_get(struct mvpp2_port *port,
1006 struct mvpp2_tx_desc *tx_desc)
1007{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001008 if (port->priv->hw_version == MVPP21)
1009 return tx_desc->pp21.buf_dma_addr;
1010 else
1011 return tx_desc->pp22.buf_dma_addr_ptp & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001012}
1013
1014static void mvpp2_txdesc_dma_addr_set(struct mvpp2_port *port,
1015 struct mvpp2_tx_desc *tx_desc,
1016 dma_addr_t dma_addr)
1017{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001018 if (port->priv->hw_version == MVPP21) {
1019 tx_desc->pp21.buf_dma_addr = dma_addr;
1020 } else {
1021 u64 val = (u64)dma_addr;
1022
1023 tx_desc->pp22.buf_dma_addr_ptp &= ~GENMASK_ULL(40, 0);
1024 tx_desc->pp22.buf_dma_addr_ptp |= val;
1025 }
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001026}
1027
1028static size_t mvpp2_txdesc_size_get(struct mvpp2_port *port,
1029 struct mvpp2_tx_desc *tx_desc)
1030{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001031 if (port->priv->hw_version == MVPP21)
1032 return tx_desc->pp21.data_size;
1033 else
1034 return tx_desc->pp22.data_size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001035}
1036
1037static void mvpp2_txdesc_size_set(struct mvpp2_port *port,
1038 struct mvpp2_tx_desc *tx_desc,
1039 size_t size)
1040{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001041 if (port->priv->hw_version == MVPP21)
1042 tx_desc->pp21.data_size = size;
1043 else
1044 tx_desc->pp22.data_size = size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001045}
1046
1047static void mvpp2_txdesc_txq_set(struct mvpp2_port *port,
1048 struct mvpp2_tx_desc *tx_desc,
1049 unsigned int txq)
1050{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001051 if (port->priv->hw_version == MVPP21)
1052 tx_desc->pp21.phys_txq = txq;
1053 else
1054 tx_desc->pp22.phys_txq = txq;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001055}
1056
1057static void mvpp2_txdesc_cmd_set(struct mvpp2_port *port,
1058 struct mvpp2_tx_desc *tx_desc,
1059 unsigned int command)
1060{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001061 if (port->priv->hw_version == MVPP21)
1062 tx_desc->pp21.command = command;
1063 else
1064 tx_desc->pp22.command = command;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001065}
1066
1067static void mvpp2_txdesc_offset_set(struct mvpp2_port *port,
1068 struct mvpp2_tx_desc *tx_desc,
1069 unsigned int offset)
1070{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001071 if (port->priv->hw_version == MVPP21)
1072 tx_desc->pp21.packet_offset = offset;
1073 else
1074 tx_desc->pp22.packet_offset = offset;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001075}
1076
1077static unsigned int mvpp2_txdesc_offset_get(struct mvpp2_port *port,
1078 struct mvpp2_tx_desc *tx_desc)
1079{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001080 if (port->priv->hw_version == MVPP21)
1081 return tx_desc->pp21.packet_offset;
1082 else
1083 return tx_desc->pp22.packet_offset;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001084}
1085
1086static dma_addr_t mvpp2_rxdesc_dma_addr_get(struct mvpp2_port *port,
1087 struct mvpp2_rx_desc *rx_desc)
1088{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001089 if (port->priv->hw_version == MVPP21)
1090 return rx_desc->pp21.buf_dma_addr;
1091 else
1092 return rx_desc->pp22.buf_dma_addr_key_hash & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001093}
1094
1095static unsigned long mvpp2_rxdesc_cookie_get(struct mvpp2_port *port,
1096 struct mvpp2_rx_desc *rx_desc)
1097{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001098 if (port->priv->hw_version == MVPP21)
1099 return rx_desc->pp21.buf_cookie;
1100 else
1101 return rx_desc->pp22.buf_cookie_misc & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001102}
1103
1104static size_t mvpp2_rxdesc_size_get(struct mvpp2_port *port,
1105 struct mvpp2_rx_desc *rx_desc)
1106{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001107 if (port->priv->hw_version == MVPP21)
1108 return rx_desc->pp21.data_size;
1109 else
1110 return rx_desc->pp22.data_size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001111}
1112
1113static u32 mvpp2_rxdesc_status_get(struct mvpp2_port *port,
1114 struct mvpp2_rx_desc *rx_desc)
1115{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001116 if (port->priv->hw_version == MVPP21)
1117 return rx_desc->pp21.status;
1118 else
1119 return rx_desc->pp22.status;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001120}
1121
Marcin Wojtas3f518502014-07-10 16:52:13 -03001122static void mvpp2_txq_inc_get(struct mvpp2_txq_pcpu *txq_pcpu)
1123{
1124 txq_pcpu->txq_get_index++;
1125 if (txq_pcpu->txq_get_index == txq_pcpu->size)
1126 txq_pcpu->txq_get_index = 0;
1127}
1128
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001129static void mvpp2_txq_inc_put(struct mvpp2_port *port,
1130 struct mvpp2_txq_pcpu *txq_pcpu,
Marcin Wojtas71ce3912015-08-06 19:00:29 +02001131 struct sk_buff *skb,
1132 struct mvpp2_tx_desc *tx_desc)
Marcin Wojtas3f518502014-07-10 16:52:13 -03001133{
Thomas Petazzoni83544912016-12-21 11:28:49 +01001134 struct mvpp2_txq_pcpu_buf *tx_buf =
1135 txq_pcpu->buffs + txq_pcpu->txq_put_index;
1136 tx_buf->skb = skb;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001137 tx_buf->size = mvpp2_txdesc_size_get(port, tx_desc);
1138 tx_buf->dma = mvpp2_txdesc_dma_addr_get(port, tx_desc) +
1139 mvpp2_txdesc_offset_get(port, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001140 txq_pcpu->txq_put_index++;
1141 if (txq_pcpu->txq_put_index == txq_pcpu->size)
1142 txq_pcpu->txq_put_index = 0;
1143}
1144
1145/* Get number of physical egress port */
1146static inline int mvpp2_egress_port(struct mvpp2_port *port)
1147{
1148 return MVPP2_MAX_TCONT + port->id;
1149}
1150
1151/* Get number of physical TXQ */
1152static inline int mvpp2_txq_phys(int port, int txq)
1153{
1154 return (MVPP2_MAX_TCONT + port) * MVPP2_MAX_TXQ + txq;
1155}
1156
1157/* Parser configuration routines */
1158
1159/* Update parser tcam and sram hw entries */
1160static int mvpp2_prs_hw_write(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1161{
1162 int i;
1163
1164 if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1165 return -EINVAL;
1166
1167 /* Clear entry invalidation bit */
1168 pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] &= ~MVPP2_PRS_TCAM_INV_MASK;
1169
1170 /* Write tcam index - indirect access */
1171 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1172 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1173 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), pe->tcam.word[i]);
1174
1175 /* Write sram index - indirect access */
1176 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1177 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1178 mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), pe->sram.word[i]);
1179
1180 return 0;
1181}
1182
1183/* Read tcam entry from hw */
1184static int mvpp2_prs_hw_read(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1185{
1186 int i;
1187
1188 if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1189 return -EINVAL;
1190
1191 /* Write tcam index - indirect access */
1192 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1193
1194 pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] = mvpp2_read(priv,
1195 MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD));
1196 if (pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] & MVPP2_PRS_TCAM_INV_MASK)
1197 return MVPP2_PRS_TCAM_ENTRY_INVALID;
1198
1199 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1200 pe->tcam.word[i] = mvpp2_read(priv, MVPP2_PRS_TCAM_DATA_REG(i));
1201
1202 /* Write sram index - indirect access */
1203 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1204 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1205 pe->sram.word[i] = mvpp2_read(priv, MVPP2_PRS_SRAM_DATA_REG(i));
1206
1207 return 0;
1208}
1209
1210/* Invalidate tcam hw entry */
1211static void mvpp2_prs_hw_inv(struct mvpp2 *priv, int index)
1212{
1213 /* Write index - indirect access */
1214 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
1215 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD),
1216 MVPP2_PRS_TCAM_INV_MASK);
1217}
1218
1219/* Enable shadow table entry and set its lookup ID */
1220static void mvpp2_prs_shadow_set(struct mvpp2 *priv, int index, int lu)
1221{
1222 priv->prs_shadow[index].valid = true;
1223 priv->prs_shadow[index].lu = lu;
1224}
1225
1226/* Update ri fields in shadow table entry */
1227static void mvpp2_prs_shadow_ri_set(struct mvpp2 *priv, int index,
1228 unsigned int ri, unsigned int ri_mask)
1229{
1230 priv->prs_shadow[index].ri_mask = ri_mask;
1231 priv->prs_shadow[index].ri = ri;
1232}
1233
1234/* Update lookup field in tcam sw entry */
1235static void mvpp2_prs_tcam_lu_set(struct mvpp2_prs_entry *pe, unsigned int lu)
1236{
1237 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_LU_BYTE);
1238
1239 pe->tcam.byte[MVPP2_PRS_TCAM_LU_BYTE] = lu;
1240 pe->tcam.byte[enable_off] = MVPP2_PRS_LU_MASK;
1241}
1242
1243/* Update mask for single port in tcam sw entry */
1244static void mvpp2_prs_tcam_port_set(struct mvpp2_prs_entry *pe,
1245 unsigned int port, bool add)
1246{
1247 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1248
1249 if (add)
1250 pe->tcam.byte[enable_off] &= ~(1 << port);
1251 else
1252 pe->tcam.byte[enable_off] |= 1 << port;
1253}
1254
1255/* Update port map in tcam sw entry */
1256static void mvpp2_prs_tcam_port_map_set(struct mvpp2_prs_entry *pe,
1257 unsigned int ports)
1258{
1259 unsigned char port_mask = MVPP2_PRS_PORT_MASK;
1260 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1261
1262 pe->tcam.byte[MVPP2_PRS_TCAM_PORT_BYTE] = 0;
1263 pe->tcam.byte[enable_off] &= ~port_mask;
1264 pe->tcam.byte[enable_off] |= ~ports & MVPP2_PRS_PORT_MASK;
1265}
1266
1267/* Obtain port map from tcam sw entry */
1268static unsigned int mvpp2_prs_tcam_port_map_get(struct mvpp2_prs_entry *pe)
1269{
1270 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1271
1272 return ~(pe->tcam.byte[enable_off]) & MVPP2_PRS_PORT_MASK;
1273}
1274
1275/* Set byte of data and its enable bits in tcam sw entry */
1276static void mvpp2_prs_tcam_data_byte_set(struct mvpp2_prs_entry *pe,
1277 unsigned int offs, unsigned char byte,
1278 unsigned char enable)
1279{
1280 pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)] = byte;
1281 pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)] = enable;
1282}
1283
1284/* Get byte of data and its enable bits from tcam sw entry */
1285static void mvpp2_prs_tcam_data_byte_get(struct mvpp2_prs_entry *pe,
1286 unsigned int offs, unsigned char *byte,
1287 unsigned char *enable)
1288{
1289 *byte = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)];
1290 *enable = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)];
1291}
1292
1293/* Compare tcam data bytes with a pattern */
1294static bool mvpp2_prs_tcam_data_cmp(struct mvpp2_prs_entry *pe, int offs,
1295 u16 data)
1296{
1297 int off = MVPP2_PRS_TCAM_DATA_BYTE(offs);
1298 u16 tcam_data;
1299
1300 tcam_data = (8 << pe->tcam.byte[off + 1]) | pe->tcam.byte[off];
1301 if (tcam_data != data)
1302 return false;
1303 return true;
1304}
1305
1306/* Update ai bits in tcam sw entry */
1307static void mvpp2_prs_tcam_ai_update(struct mvpp2_prs_entry *pe,
1308 unsigned int bits, unsigned int enable)
1309{
1310 int i, ai_idx = MVPP2_PRS_TCAM_AI_BYTE;
1311
1312 for (i = 0; i < MVPP2_PRS_AI_BITS; i++) {
1313
1314 if (!(enable & BIT(i)))
1315 continue;
1316
1317 if (bits & BIT(i))
1318 pe->tcam.byte[ai_idx] |= 1 << i;
1319 else
1320 pe->tcam.byte[ai_idx] &= ~(1 << i);
1321 }
1322
1323 pe->tcam.byte[MVPP2_PRS_TCAM_EN_OFFS(ai_idx)] |= enable;
1324}
1325
1326/* Get ai bits from tcam sw entry */
1327static int mvpp2_prs_tcam_ai_get(struct mvpp2_prs_entry *pe)
1328{
1329 return pe->tcam.byte[MVPP2_PRS_TCAM_AI_BYTE];
1330}
1331
1332/* Set ethertype in tcam sw entry */
1333static void mvpp2_prs_match_etype(struct mvpp2_prs_entry *pe, int offset,
1334 unsigned short ethertype)
1335{
1336 mvpp2_prs_tcam_data_byte_set(pe, offset + 0, ethertype >> 8, 0xff);
1337 mvpp2_prs_tcam_data_byte_set(pe, offset + 1, ethertype & 0xff, 0xff);
1338}
1339
1340/* Set bits in sram sw entry */
1341static void mvpp2_prs_sram_bits_set(struct mvpp2_prs_entry *pe, int bit_num,
1342 int val)
1343{
1344 pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] |= (val << (bit_num % 8));
1345}
1346
1347/* Clear bits in sram sw entry */
1348static void mvpp2_prs_sram_bits_clear(struct mvpp2_prs_entry *pe, int bit_num,
1349 int val)
1350{
1351 pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] &= ~(val << (bit_num % 8));
1352}
1353
1354/* Update ri bits in sram sw entry */
1355static void mvpp2_prs_sram_ri_update(struct mvpp2_prs_entry *pe,
1356 unsigned int bits, unsigned int mask)
1357{
1358 unsigned int i;
1359
1360 for (i = 0; i < MVPP2_PRS_SRAM_RI_CTRL_BITS; i++) {
1361 int ri_off = MVPP2_PRS_SRAM_RI_OFFS;
1362
1363 if (!(mask & BIT(i)))
1364 continue;
1365
1366 if (bits & BIT(i))
1367 mvpp2_prs_sram_bits_set(pe, ri_off + i, 1);
1368 else
1369 mvpp2_prs_sram_bits_clear(pe, ri_off + i, 1);
1370
1371 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_RI_CTRL_OFFS + i, 1);
1372 }
1373}
1374
1375/* Obtain ri bits from sram sw entry */
1376static int mvpp2_prs_sram_ri_get(struct mvpp2_prs_entry *pe)
1377{
1378 return pe->sram.word[MVPP2_PRS_SRAM_RI_WORD];
1379}
1380
1381/* Update ai bits in sram sw entry */
1382static void mvpp2_prs_sram_ai_update(struct mvpp2_prs_entry *pe,
1383 unsigned int bits, unsigned int mask)
1384{
1385 unsigned int i;
1386 int ai_off = MVPP2_PRS_SRAM_AI_OFFS;
1387
1388 for (i = 0; i < MVPP2_PRS_SRAM_AI_CTRL_BITS; i++) {
1389
1390 if (!(mask & BIT(i)))
1391 continue;
1392
1393 if (bits & BIT(i))
1394 mvpp2_prs_sram_bits_set(pe, ai_off + i, 1);
1395 else
1396 mvpp2_prs_sram_bits_clear(pe, ai_off + i, 1);
1397
1398 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_AI_CTRL_OFFS + i, 1);
1399 }
1400}
1401
1402/* Read ai bits from sram sw entry */
1403static int mvpp2_prs_sram_ai_get(struct mvpp2_prs_entry *pe)
1404{
1405 u8 bits;
1406 int ai_off = MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_AI_OFFS);
1407 int ai_en_off = ai_off + 1;
1408 int ai_shift = MVPP2_PRS_SRAM_AI_OFFS % 8;
1409
1410 bits = (pe->sram.byte[ai_off] >> ai_shift) |
1411 (pe->sram.byte[ai_en_off] << (8 - ai_shift));
1412
1413 return bits;
1414}
1415
1416/* In sram sw entry set lookup ID field of the tcam key to be used in the next
1417 * lookup interation
1418 */
1419static void mvpp2_prs_sram_next_lu_set(struct mvpp2_prs_entry *pe,
1420 unsigned int lu)
1421{
1422 int sram_next_off = MVPP2_PRS_SRAM_NEXT_LU_OFFS;
1423
1424 mvpp2_prs_sram_bits_clear(pe, sram_next_off,
1425 MVPP2_PRS_SRAM_NEXT_LU_MASK);
1426 mvpp2_prs_sram_bits_set(pe, sram_next_off, lu);
1427}
1428
1429/* In the sram sw entry set sign and value of the next lookup offset
1430 * and the offset value generated to the classifier
1431 */
1432static void mvpp2_prs_sram_shift_set(struct mvpp2_prs_entry *pe, int shift,
1433 unsigned int op)
1434{
1435 /* Set sign */
1436 if (shift < 0) {
1437 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1438 shift = 0 - shift;
1439 } else {
1440 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1441 }
1442
1443 /* Set value */
1444 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_SHIFT_OFFS)] =
1445 (unsigned char)shift;
1446
1447 /* Reset and set operation */
1448 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS,
1449 MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK);
1450 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS, op);
1451
1452 /* Set base offset as current */
1453 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1454}
1455
1456/* In the sram sw entry set sign and value of the user defined offset
1457 * generated to the classifier
1458 */
1459static void mvpp2_prs_sram_offset_set(struct mvpp2_prs_entry *pe,
1460 unsigned int type, int offset,
1461 unsigned int op)
1462{
1463 /* Set sign */
1464 if (offset < 0) {
1465 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1466 offset = 0 - offset;
1467 } else {
1468 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1469 }
1470
1471 /* Set value */
1472 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_OFFS,
1473 MVPP2_PRS_SRAM_UDF_MASK);
1474 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_OFFS, offset);
1475 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1476 MVPP2_PRS_SRAM_UDF_BITS)] &=
1477 ~(MVPP2_PRS_SRAM_UDF_MASK >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1478 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1479 MVPP2_PRS_SRAM_UDF_BITS)] |=
1480 (offset >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1481
1482 /* Set offset type */
1483 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS,
1484 MVPP2_PRS_SRAM_UDF_TYPE_MASK);
1485 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS, type);
1486
1487 /* Set offset operation */
1488 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS,
1489 MVPP2_PRS_SRAM_OP_SEL_UDF_MASK);
1490 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS, op);
1491
1492 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1493 MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] &=
1494 ~(MVPP2_PRS_SRAM_OP_SEL_UDF_MASK >>
1495 (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1496
1497 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1498 MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] |=
1499 (op >> (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1500
1501 /* Set base offset as current */
1502 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1503}
1504
1505/* Find parser flow entry */
1506static struct mvpp2_prs_entry *mvpp2_prs_flow_find(struct mvpp2 *priv, int flow)
1507{
1508 struct mvpp2_prs_entry *pe;
1509 int tid;
1510
1511 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
1512 if (!pe)
1513 return NULL;
1514 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
1515
1516 /* Go through the all entires with MVPP2_PRS_LU_FLOWS */
1517 for (tid = MVPP2_PRS_TCAM_SRAM_SIZE - 1; tid >= 0; tid--) {
1518 u8 bits;
1519
1520 if (!priv->prs_shadow[tid].valid ||
1521 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_FLOWS)
1522 continue;
1523
1524 pe->index = tid;
1525 mvpp2_prs_hw_read(priv, pe);
1526 bits = mvpp2_prs_sram_ai_get(pe);
1527
1528 /* Sram store classification lookup ID in AI bits [5:0] */
1529 if ((bits & MVPP2_PRS_FLOW_ID_MASK) == flow)
1530 return pe;
1531 }
1532 kfree(pe);
1533
1534 return NULL;
1535}
1536
1537/* Return first free tcam index, seeking from start to end */
1538static int mvpp2_prs_tcam_first_free(struct mvpp2 *priv, unsigned char start,
1539 unsigned char end)
1540{
1541 int tid;
1542
1543 if (start > end)
1544 swap(start, end);
1545
1546 if (end >= MVPP2_PRS_TCAM_SRAM_SIZE)
1547 end = MVPP2_PRS_TCAM_SRAM_SIZE - 1;
1548
1549 for (tid = start; tid <= end; tid++) {
1550 if (!priv->prs_shadow[tid].valid)
1551 return tid;
1552 }
1553
1554 return -EINVAL;
1555}
1556
1557/* Enable/disable dropping all mac da's */
1558static void mvpp2_prs_mac_drop_all_set(struct mvpp2 *priv, int port, bool add)
1559{
1560 struct mvpp2_prs_entry pe;
1561
1562 if (priv->prs_shadow[MVPP2_PE_DROP_ALL].valid) {
1563 /* Entry exist - update port only */
1564 pe.index = MVPP2_PE_DROP_ALL;
1565 mvpp2_prs_hw_read(priv, &pe);
1566 } else {
1567 /* Entry doesn't exist - create new */
1568 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1569 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1570 pe.index = MVPP2_PE_DROP_ALL;
1571
1572 /* Non-promiscuous mode for all ports - DROP unknown packets */
1573 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
1574 MVPP2_PRS_RI_DROP_MASK);
1575
1576 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1577 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1578
1579 /* Update shadow table */
1580 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1581
1582 /* Mask all ports */
1583 mvpp2_prs_tcam_port_map_set(&pe, 0);
1584 }
1585
1586 /* Update port mask */
1587 mvpp2_prs_tcam_port_set(&pe, port, add);
1588
1589 mvpp2_prs_hw_write(priv, &pe);
1590}
1591
1592/* Set port to promiscuous mode */
1593static void mvpp2_prs_mac_promisc_set(struct mvpp2 *priv, int port, bool add)
1594{
1595 struct mvpp2_prs_entry pe;
1596
Joe Perchesdbedd442015-03-06 20:49:12 -08001597 /* Promiscuous mode - Accept unknown packets */
Marcin Wojtas3f518502014-07-10 16:52:13 -03001598
1599 if (priv->prs_shadow[MVPP2_PE_MAC_PROMISCUOUS].valid) {
1600 /* Entry exist - update port only */
1601 pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1602 mvpp2_prs_hw_read(priv, &pe);
1603 } else {
1604 /* Entry doesn't exist - create new */
1605 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1606 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1607 pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1608
1609 /* Continue - set next lookup */
1610 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
1611
1612 /* Set result info bits */
1613 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_UCAST,
1614 MVPP2_PRS_RI_L2_CAST_MASK);
1615
1616 /* Shift to ethertype */
1617 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
1618 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1619
1620 /* Mask all ports */
1621 mvpp2_prs_tcam_port_map_set(&pe, 0);
1622
1623 /* Update shadow table */
1624 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1625 }
1626
1627 /* Update port mask */
1628 mvpp2_prs_tcam_port_set(&pe, port, add);
1629
1630 mvpp2_prs_hw_write(priv, &pe);
1631}
1632
1633/* Accept multicast */
1634static void mvpp2_prs_mac_multi_set(struct mvpp2 *priv, int port, int index,
1635 bool add)
1636{
1637 struct mvpp2_prs_entry pe;
1638 unsigned char da_mc;
1639
1640 /* Ethernet multicast address first byte is
1641 * 0x01 for IPv4 and 0x33 for IPv6
1642 */
1643 da_mc = (index == MVPP2_PE_MAC_MC_ALL) ? 0x01 : 0x33;
1644
1645 if (priv->prs_shadow[index].valid) {
1646 /* Entry exist - update port only */
1647 pe.index = index;
1648 mvpp2_prs_hw_read(priv, &pe);
1649 } else {
1650 /* Entry doesn't exist - create new */
1651 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1652 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1653 pe.index = index;
1654
1655 /* Continue - set next lookup */
1656 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
1657
1658 /* Set result info bits */
1659 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_MCAST,
1660 MVPP2_PRS_RI_L2_CAST_MASK);
1661
1662 /* Update tcam entry data first byte */
1663 mvpp2_prs_tcam_data_byte_set(&pe, 0, da_mc, 0xff);
1664
1665 /* Shift to ethertype */
1666 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
1667 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1668
1669 /* Mask all ports */
1670 mvpp2_prs_tcam_port_map_set(&pe, 0);
1671
1672 /* Update shadow table */
1673 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1674 }
1675
1676 /* Update port mask */
1677 mvpp2_prs_tcam_port_set(&pe, port, add);
1678
1679 mvpp2_prs_hw_write(priv, &pe);
1680}
1681
1682/* Set entry for dsa packets */
1683static void mvpp2_prs_dsa_tag_set(struct mvpp2 *priv, int port, bool add,
1684 bool tagged, bool extend)
1685{
1686 struct mvpp2_prs_entry pe;
1687 int tid, shift;
1688
1689 if (extend) {
1690 tid = tagged ? MVPP2_PE_EDSA_TAGGED : MVPP2_PE_EDSA_UNTAGGED;
1691 shift = 8;
1692 } else {
1693 tid = tagged ? MVPP2_PE_DSA_TAGGED : MVPP2_PE_DSA_UNTAGGED;
1694 shift = 4;
1695 }
1696
1697 if (priv->prs_shadow[tid].valid) {
1698 /* Entry exist - update port only */
1699 pe.index = tid;
1700 mvpp2_prs_hw_read(priv, &pe);
1701 } else {
1702 /* Entry doesn't exist - create new */
1703 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1704 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
1705 pe.index = tid;
1706
1707 /* Shift 4 bytes if DSA tag or 8 bytes in case of EDSA tag*/
1708 mvpp2_prs_sram_shift_set(&pe, shift,
1709 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1710
1711 /* Update shadow table */
1712 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
1713
1714 if (tagged) {
1715 /* Set tagged bit in DSA tag */
1716 mvpp2_prs_tcam_data_byte_set(&pe, 0,
1717 MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
1718 MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
1719 /* Clear all ai bits for next iteration */
1720 mvpp2_prs_sram_ai_update(&pe, 0,
1721 MVPP2_PRS_SRAM_AI_MASK);
1722 /* If packet is tagged continue check vlans */
1723 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
1724 } else {
1725 /* Set result info bits to 'no vlans' */
1726 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
1727 MVPP2_PRS_RI_VLAN_MASK);
1728 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
1729 }
1730
1731 /* Mask all ports */
1732 mvpp2_prs_tcam_port_map_set(&pe, 0);
1733 }
1734
1735 /* Update port mask */
1736 mvpp2_prs_tcam_port_set(&pe, port, add);
1737
1738 mvpp2_prs_hw_write(priv, &pe);
1739}
1740
1741/* Set entry for dsa ethertype */
1742static void mvpp2_prs_dsa_tag_ethertype_set(struct mvpp2 *priv, int port,
1743 bool add, bool tagged, bool extend)
1744{
1745 struct mvpp2_prs_entry pe;
1746 int tid, shift, port_mask;
1747
1748 if (extend) {
1749 tid = tagged ? MVPP2_PE_ETYPE_EDSA_TAGGED :
1750 MVPP2_PE_ETYPE_EDSA_UNTAGGED;
1751 port_mask = 0;
1752 shift = 8;
1753 } else {
1754 tid = tagged ? MVPP2_PE_ETYPE_DSA_TAGGED :
1755 MVPP2_PE_ETYPE_DSA_UNTAGGED;
1756 port_mask = MVPP2_PRS_PORT_MASK;
1757 shift = 4;
1758 }
1759
1760 if (priv->prs_shadow[tid].valid) {
1761 /* Entry exist - update port only */
1762 pe.index = tid;
1763 mvpp2_prs_hw_read(priv, &pe);
1764 } else {
1765 /* Entry doesn't exist - create new */
1766 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1767 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
1768 pe.index = tid;
1769
1770 /* Set ethertype */
1771 mvpp2_prs_match_etype(&pe, 0, ETH_P_EDSA);
1772 mvpp2_prs_match_etype(&pe, 2, 0);
1773
1774 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DSA_MASK,
1775 MVPP2_PRS_RI_DSA_MASK);
1776 /* Shift ethertype + 2 byte reserved + tag*/
1777 mvpp2_prs_sram_shift_set(&pe, 2 + MVPP2_ETH_TYPE_LEN + shift,
1778 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1779
1780 /* Update shadow table */
1781 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
1782
1783 if (tagged) {
1784 /* Set tagged bit in DSA tag */
1785 mvpp2_prs_tcam_data_byte_set(&pe,
1786 MVPP2_ETH_TYPE_LEN + 2 + 3,
1787 MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
1788 MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
1789 /* Clear all ai bits for next iteration */
1790 mvpp2_prs_sram_ai_update(&pe, 0,
1791 MVPP2_PRS_SRAM_AI_MASK);
1792 /* If packet is tagged continue check vlans */
1793 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
1794 } else {
1795 /* Set result info bits to 'no vlans' */
1796 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
1797 MVPP2_PRS_RI_VLAN_MASK);
1798 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
1799 }
1800 /* Mask/unmask all ports, depending on dsa type */
1801 mvpp2_prs_tcam_port_map_set(&pe, port_mask);
1802 }
1803
1804 /* Update port mask */
1805 mvpp2_prs_tcam_port_set(&pe, port, add);
1806
1807 mvpp2_prs_hw_write(priv, &pe);
1808}
1809
1810/* Search for existing single/triple vlan entry */
1811static struct mvpp2_prs_entry *mvpp2_prs_vlan_find(struct mvpp2 *priv,
1812 unsigned short tpid, int ai)
1813{
1814 struct mvpp2_prs_entry *pe;
1815 int tid;
1816
1817 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
1818 if (!pe)
1819 return NULL;
1820 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
1821
1822 /* Go through the all entries with MVPP2_PRS_LU_VLAN */
1823 for (tid = MVPP2_PE_FIRST_FREE_TID;
1824 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
1825 unsigned int ri_bits, ai_bits;
1826 bool match;
1827
1828 if (!priv->prs_shadow[tid].valid ||
1829 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
1830 continue;
1831
1832 pe->index = tid;
1833
1834 mvpp2_prs_hw_read(priv, pe);
1835 match = mvpp2_prs_tcam_data_cmp(pe, 0, swab16(tpid));
1836 if (!match)
1837 continue;
1838
1839 /* Get vlan type */
1840 ri_bits = mvpp2_prs_sram_ri_get(pe);
1841 ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
1842
1843 /* Get current ai value from tcam */
1844 ai_bits = mvpp2_prs_tcam_ai_get(pe);
1845 /* Clear double vlan bit */
1846 ai_bits &= ~MVPP2_PRS_DBL_VLAN_AI_BIT;
1847
1848 if (ai != ai_bits)
1849 continue;
1850
1851 if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
1852 ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
1853 return pe;
1854 }
1855 kfree(pe);
1856
1857 return NULL;
1858}
1859
1860/* Add/update single/triple vlan entry */
1861static int mvpp2_prs_vlan_add(struct mvpp2 *priv, unsigned short tpid, int ai,
1862 unsigned int port_map)
1863{
1864 struct mvpp2_prs_entry *pe;
1865 int tid_aux, tid;
Sudip Mukherjee43737472014-11-01 16:59:34 +05301866 int ret = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001867
1868 pe = mvpp2_prs_vlan_find(priv, tpid, ai);
1869
1870 if (!pe) {
1871 /* Create new tcam entry */
1872 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_LAST_FREE_TID,
1873 MVPP2_PE_FIRST_FREE_TID);
1874 if (tid < 0)
1875 return tid;
1876
1877 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
1878 if (!pe)
1879 return -ENOMEM;
1880
1881 /* Get last double vlan tid */
1882 for (tid_aux = MVPP2_PE_LAST_FREE_TID;
1883 tid_aux >= MVPP2_PE_FIRST_FREE_TID; tid_aux--) {
1884 unsigned int ri_bits;
1885
1886 if (!priv->prs_shadow[tid_aux].valid ||
1887 priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
1888 continue;
1889
1890 pe->index = tid_aux;
1891 mvpp2_prs_hw_read(priv, pe);
1892 ri_bits = mvpp2_prs_sram_ri_get(pe);
1893 if ((ri_bits & MVPP2_PRS_RI_VLAN_MASK) ==
1894 MVPP2_PRS_RI_VLAN_DOUBLE)
1895 break;
1896 }
1897
Sudip Mukherjee43737472014-11-01 16:59:34 +05301898 if (tid <= tid_aux) {
1899 ret = -EINVAL;
1900 goto error;
1901 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03001902
1903 memset(pe, 0 , sizeof(struct mvpp2_prs_entry));
1904 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
1905 pe->index = tid;
1906
1907 mvpp2_prs_match_etype(pe, 0, tpid);
1908
1909 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_L2);
1910 /* Shift 4 bytes - skip 1 vlan tag */
1911 mvpp2_prs_sram_shift_set(pe, MVPP2_VLAN_TAG_LEN,
1912 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1913 /* Clear all ai bits for next iteration */
1914 mvpp2_prs_sram_ai_update(pe, 0, MVPP2_PRS_SRAM_AI_MASK);
1915
1916 if (ai == MVPP2_PRS_SINGLE_VLAN_AI) {
1917 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_SINGLE,
1918 MVPP2_PRS_RI_VLAN_MASK);
1919 } else {
1920 ai |= MVPP2_PRS_DBL_VLAN_AI_BIT;
1921 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_TRIPLE,
1922 MVPP2_PRS_RI_VLAN_MASK);
1923 }
1924 mvpp2_prs_tcam_ai_update(pe, ai, MVPP2_PRS_SRAM_AI_MASK);
1925
1926 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_VLAN);
1927 }
1928 /* Update ports' mask */
1929 mvpp2_prs_tcam_port_map_set(pe, port_map);
1930
1931 mvpp2_prs_hw_write(priv, pe);
1932
Sudip Mukherjee43737472014-11-01 16:59:34 +05301933error:
Marcin Wojtas3f518502014-07-10 16:52:13 -03001934 kfree(pe);
1935
Sudip Mukherjee43737472014-11-01 16:59:34 +05301936 return ret;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001937}
1938
1939/* Get first free double vlan ai number */
1940static int mvpp2_prs_double_vlan_ai_free_get(struct mvpp2 *priv)
1941{
1942 int i;
1943
1944 for (i = 1; i < MVPP2_PRS_DBL_VLANS_MAX; i++) {
1945 if (!priv->prs_double_vlans[i])
1946 return i;
1947 }
1948
1949 return -EINVAL;
1950}
1951
1952/* Search for existing double vlan entry */
1953static struct mvpp2_prs_entry *mvpp2_prs_double_vlan_find(struct mvpp2 *priv,
1954 unsigned short tpid1,
1955 unsigned short tpid2)
1956{
1957 struct mvpp2_prs_entry *pe;
1958 int tid;
1959
1960 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
1961 if (!pe)
1962 return NULL;
1963 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
1964
1965 /* Go through the all entries with MVPP2_PRS_LU_VLAN */
1966 for (tid = MVPP2_PE_FIRST_FREE_TID;
1967 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
1968 unsigned int ri_mask;
1969 bool match;
1970
1971 if (!priv->prs_shadow[tid].valid ||
1972 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
1973 continue;
1974
1975 pe->index = tid;
1976 mvpp2_prs_hw_read(priv, pe);
1977
1978 match = mvpp2_prs_tcam_data_cmp(pe, 0, swab16(tpid1))
1979 && mvpp2_prs_tcam_data_cmp(pe, 4, swab16(tpid2));
1980
1981 if (!match)
1982 continue;
1983
1984 ri_mask = mvpp2_prs_sram_ri_get(pe) & MVPP2_PRS_RI_VLAN_MASK;
1985 if (ri_mask == MVPP2_PRS_RI_VLAN_DOUBLE)
1986 return pe;
1987 }
1988 kfree(pe);
1989
1990 return NULL;
1991}
1992
1993/* Add or update double vlan entry */
1994static int mvpp2_prs_double_vlan_add(struct mvpp2 *priv, unsigned short tpid1,
1995 unsigned short tpid2,
1996 unsigned int port_map)
1997{
1998 struct mvpp2_prs_entry *pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05301999 int tid_aux, tid, ai, ret = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002000
2001 pe = mvpp2_prs_double_vlan_find(priv, tpid1, tpid2);
2002
2003 if (!pe) {
2004 /* Create new tcam entry */
2005 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2006 MVPP2_PE_LAST_FREE_TID);
2007 if (tid < 0)
2008 return tid;
2009
2010 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2011 if (!pe)
2012 return -ENOMEM;
2013
2014 /* Set ai value for new double vlan entry */
2015 ai = mvpp2_prs_double_vlan_ai_free_get(priv);
Sudip Mukherjee43737472014-11-01 16:59:34 +05302016 if (ai < 0) {
2017 ret = ai;
2018 goto error;
2019 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002020
2021 /* Get first single/triple vlan tid */
2022 for (tid_aux = MVPP2_PE_FIRST_FREE_TID;
2023 tid_aux <= MVPP2_PE_LAST_FREE_TID; tid_aux++) {
2024 unsigned int ri_bits;
2025
2026 if (!priv->prs_shadow[tid_aux].valid ||
2027 priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
2028 continue;
2029
2030 pe->index = tid_aux;
2031 mvpp2_prs_hw_read(priv, pe);
2032 ri_bits = mvpp2_prs_sram_ri_get(pe);
2033 ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
2034 if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
2035 ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
2036 break;
2037 }
2038
Sudip Mukherjee43737472014-11-01 16:59:34 +05302039 if (tid >= tid_aux) {
2040 ret = -ERANGE;
2041 goto error;
2042 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002043
2044 memset(pe, 0, sizeof(struct mvpp2_prs_entry));
2045 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2046 pe->index = tid;
2047
2048 priv->prs_double_vlans[ai] = true;
2049
2050 mvpp2_prs_match_etype(pe, 0, tpid1);
2051 mvpp2_prs_match_etype(pe, 4, tpid2);
2052
2053 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_VLAN);
2054 /* Shift 8 bytes - skip 2 vlan tags */
2055 mvpp2_prs_sram_shift_set(pe, 2 * MVPP2_VLAN_TAG_LEN,
2056 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2057 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_DOUBLE,
2058 MVPP2_PRS_RI_VLAN_MASK);
2059 mvpp2_prs_sram_ai_update(pe, ai | MVPP2_PRS_DBL_VLAN_AI_BIT,
2060 MVPP2_PRS_SRAM_AI_MASK);
2061
2062 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_VLAN);
2063 }
2064
2065 /* Update ports' mask */
2066 mvpp2_prs_tcam_port_map_set(pe, port_map);
2067 mvpp2_prs_hw_write(priv, pe);
2068
Sudip Mukherjee43737472014-11-01 16:59:34 +05302069error:
Marcin Wojtas3f518502014-07-10 16:52:13 -03002070 kfree(pe);
Sudip Mukherjee43737472014-11-01 16:59:34 +05302071 return ret;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002072}
2073
2074/* IPv4 header parsing for fragmentation and L4 offset */
2075static int mvpp2_prs_ip4_proto(struct mvpp2 *priv, unsigned short proto,
2076 unsigned int ri, unsigned int ri_mask)
2077{
2078 struct mvpp2_prs_entry pe;
2079 int tid;
2080
2081 if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
2082 (proto != IPPROTO_IGMP))
2083 return -EINVAL;
2084
2085 /* Fragmented packet */
2086 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2087 MVPP2_PE_LAST_FREE_TID);
2088 if (tid < 0)
2089 return tid;
2090
2091 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2092 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
2093 pe.index = tid;
2094
2095 /* Set next lu to IPv4 */
2096 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2097 mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2098 /* Set L4 offset */
2099 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
2100 sizeof(struct iphdr) - 4,
2101 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2102 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
2103 MVPP2_PRS_IPV4_DIP_AI_BIT);
2104 mvpp2_prs_sram_ri_update(&pe, ri | MVPP2_PRS_RI_IP_FRAG_MASK,
2105 ri_mask | MVPP2_PRS_RI_IP_FRAG_MASK);
2106
2107 mvpp2_prs_tcam_data_byte_set(&pe, 5, proto, MVPP2_PRS_TCAM_PROTO_MASK);
2108 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
2109 /* Unmask all ports */
2110 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2111
2112 /* Update shadow table and hw entry */
2113 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2114 mvpp2_prs_hw_write(priv, &pe);
2115
2116 /* Not fragmented packet */
2117 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2118 MVPP2_PE_LAST_FREE_TID);
2119 if (tid < 0)
2120 return tid;
2121
2122 pe.index = tid;
2123 /* Clear ri before updating */
2124 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2125 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2126 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
2127
2128 mvpp2_prs_tcam_data_byte_set(&pe, 2, 0x00, MVPP2_PRS_TCAM_PROTO_MASK_L);
2129 mvpp2_prs_tcam_data_byte_set(&pe, 3, 0x00, MVPP2_PRS_TCAM_PROTO_MASK);
2130
2131 /* Update shadow table and hw entry */
2132 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2133 mvpp2_prs_hw_write(priv, &pe);
2134
2135 return 0;
2136}
2137
2138/* IPv4 L3 multicast or broadcast */
2139static int mvpp2_prs_ip4_cast(struct mvpp2 *priv, unsigned short l3_cast)
2140{
2141 struct mvpp2_prs_entry pe;
2142 int mask, tid;
2143
2144 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2145 MVPP2_PE_LAST_FREE_TID);
2146 if (tid < 0)
2147 return tid;
2148
2149 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2150 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
2151 pe.index = tid;
2152
2153 switch (l3_cast) {
2154 case MVPP2_PRS_L3_MULTI_CAST:
2155 mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV4_MC,
2156 MVPP2_PRS_IPV4_MC_MASK);
2157 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
2158 MVPP2_PRS_RI_L3_ADDR_MASK);
2159 break;
2160 case MVPP2_PRS_L3_BROAD_CAST:
2161 mask = MVPP2_PRS_IPV4_BC_MASK;
2162 mvpp2_prs_tcam_data_byte_set(&pe, 0, mask, mask);
2163 mvpp2_prs_tcam_data_byte_set(&pe, 1, mask, mask);
2164 mvpp2_prs_tcam_data_byte_set(&pe, 2, mask, mask);
2165 mvpp2_prs_tcam_data_byte_set(&pe, 3, mask, mask);
2166 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_BCAST,
2167 MVPP2_PRS_RI_L3_ADDR_MASK);
2168 break;
2169 default:
2170 return -EINVAL;
2171 }
2172
2173 /* Finished: go to flowid generation */
2174 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2175 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2176
2177 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
2178 MVPP2_PRS_IPV4_DIP_AI_BIT);
2179 /* Unmask all ports */
2180 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2181
2182 /* Update shadow table and hw entry */
2183 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2184 mvpp2_prs_hw_write(priv, &pe);
2185
2186 return 0;
2187}
2188
2189/* Set entries for protocols over IPv6 */
2190static int mvpp2_prs_ip6_proto(struct mvpp2 *priv, unsigned short proto,
2191 unsigned int ri, unsigned int ri_mask)
2192{
2193 struct mvpp2_prs_entry pe;
2194 int tid;
2195
2196 if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
2197 (proto != IPPROTO_ICMPV6) && (proto != IPPROTO_IPIP))
2198 return -EINVAL;
2199
2200 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2201 MVPP2_PE_LAST_FREE_TID);
2202 if (tid < 0)
2203 return tid;
2204
2205 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2206 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2207 pe.index = tid;
2208
2209 /* Finished: go to flowid generation */
2210 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2211 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2212 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
2213 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
2214 sizeof(struct ipv6hdr) - 6,
2215 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2216
2217 mvpp2_prs_tcam_data_byte_set(&pe, 0, proto, MVPP2_PRS_TCAM_PROTO_MASK);
2218 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
2219 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2220 /* Unmask all ports */
2221 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2222
2223 /* Write HW */
2224 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
2225 mvpp2_prs_hw_write(priv, &pe);
2226
2227 return 0;
2228}
2229
2230/* IPv6 L3 multicast entry */
2231static int mvpp2_prs_ip6_cast(struct mvpp2 *priv, unsigned short l3_cast)
2232{
2233 struct mvpp2_prs_entry pe;
2234 int tid;
2235
2236 if (l3_cast != MVPP2_PRS_L3_MULTI_CAST)
2237 return -EINVAL;
2238
2239 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2240 MVPP2_PE_LAST_FREE_TID);
2241 if (tid < 0)
2242 return tid;
2243
2244 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2245 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2246 pe.index = tid;
2247
2248 /* Finished: go to flowid generation */
2249 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2250 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
2251 MVPP2_PRS_RI_L3_ADDR_MASK);
2252 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
2253 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2254 /* Shift back to IPv6 NH */
2255 mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2256
2257 mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV6_MC,
2258 MVPP2_PRS_IPV6_MC_MASK);
2259 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2260 /* Unmask all ports */
2261 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2262
2263 /* Update shadow table and hw entry */
2264 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
2265 mvpp2_prs_hw_write(priv, &pe);
2266
2267 return 0;
2268}
2269
2270/* Parser per-port initialization */
2271static void mvpp2_prs_hw_port_init(struct mvpp2 *priv, int port, int lu_first,
2272 int lu_max, int offset)
2273{
2274 u32 val;
2275
2276 /* Set lookup ID */
2277 val = mvpp2_read(priv, MVPP2_PRS_INIT_LOOKUP_REG);
2278 val &= ~MVPP2_PRS_PORT_LU_MASK(port);
2279 val |= MVPP2_PRS_PORT_LU_VAL(port, lu_first);
2280 mvpp2_write(priv, MVPP2_PRS_INIT_LOOKUP_REG, val);
2281
2282 /* Set maximum number of loops for packet received from port */
2283 val = mvpp2_read(priv, MVPP2_PRS_MAX_LOOP_REG(port));
2284 val &= ~MVPP2_PRS_MAX_LOOP_MASK(port);
2285 val |= MVPP2_PRS_MAX_LOOP_VAL(port, lu_max);
2286 mvpp2_write(priv, MVPP2_PRS_MAX_LOOP_REG(port), val);
2287
2288 /* Set initial offset for packet header extraction for the first
2289 * searching loop
2290 */
2291 val = mvpp2_read(priv, MVPP2_PRS_INIT_OFFS_REG(port));
2292 val &= ~MVPP2_PRS_INIT_OFF_MASK(port);
2293 val |= MVPP2_PRS_INIT_OFF_VAL(port, offset);
2294 mvpp2_write(priv, MVPP2_PRS_INIT_OFFS_REG(port), val);
2295}
2296
2297/* Default flow entries initialization for all ports */
2298static void mvpp2_prs_def_flow_init(struct mvpp2 *priv)
2299{
2300 struct mvpp2_prs_entry pe;
2301 int port;
2302
2303 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
2304 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2305 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2306 pe.index = MVPP2_PE_FIRST_DEFAULT_FLOW - port;
2307
2308 /* Mask all ports */
2309 mvpp2_prs_tcam_port_map_set(&pe, 0);
2310
2311 /* Set flow ID*/
2312 mvpp2_prs_sram_ai_update(&pe, port, MVPP2_PRS_FLOW_ID_MASK);
2313 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
2314
2315 /* Update shadow table and hw entry */
2316 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_FLOWS);
2317 mvpp2_prs_hw_write(priv, &pe);
2318 }
2319}
2320
2321/* Set default entry for Marvell Header field */
2322static void mvpp2_prs_mh_init(struct mvpp2 *priv)
2323{
2324 struct mvpp2_prs_entry pe;
2325
2326 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2327
2328 pe.index = MVPP2_PE_MH_DEFAULT;
2329 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MH);
2330 mvpp2_prs_sram_shift_set(&pe, MVPP2_MH_SIZE,
2331 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2332 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_MAC);
2333
2334 /* Unmask all ports */
2335 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2336
2337 /* Update shadow table and hw entry */
2338 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MH);
2339 mvpp2_prs_hw_write(priv, &pe);
2340}
2341
2342/* Set default entires (place holder) for promiscuous, non-promiscuous and
2343 * multicast MAC addresses
2344 */
2345static void mvpp2_prs_mac_init(struct mvpp2 *priv)
2346{
2347 struct mvpp2_prs_entry pe;
2348
2349 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2350
2351 /* Non-promiscuous mode for all ports - DROP unknown packets */
2352 pe.index = MVPP2_PE_MAC_NON_PROMISCUOUS;
2353 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
2354
2355 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
2356 MVPP2_PRS_RI_DROP_MASK);
2357 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2358 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2359
2360 /* Unmask all ports */
2361 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2362
2363 /* Update shadow table and hw entry */
2364 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2365 mvpp2_prs_hw_write(priv, &pe);
2366
2367 /* place holders only - no ports */
2368 mvpp2_prs_mac_drop_all_set(priv, 0, false);
2369 mvpp2_prs_mac_promisc_set(priv, 0, false);
2370 mvpp2_prs_mac_multi_set(priv, MVPP2_PE_MAC_MC_ALL, 0, false);
2371 mvpp2_prs_mac_multi_set(priv, MVPP2_PE_MAC_MC_IP6, 0, false);
2372}
2373
2374/* Set default entries for various types of dsa packets */
2375static void mvpp2_prs_dsa_init(struct mvpp2 *priv)
2376{
2377 struct mvpp2_prs_entry pe;
2378
2379 /* None tagged EDSA entry - place holder */
2380 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
2381 MVPP2_PRS_EDSA);
2382
2383 /* Tagged EDSA entry - place holder */
2384 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
2385
2386 /* None tagged DSA entry - place holder */
2387 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
2388 MVPP2_PRS_DSA);
2389
2390 /* Tagged DSA entry - place holder */
2391 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
2392
2393 /* None tagged EDSA ethertype entry - place holder*/
2394 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
2395 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
2396
2397 /* Tagged EDSA ethertype entry - place holder*/
2398 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
2399 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
2400
2401 /* None tagged DSA ethertype entry */
2402 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
2403 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
2404
2405 /* Tagged DSA ethertype entry */
2406 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
2407 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
2408
2409 /* Set default entry, in case DSA or EDSA tag not found */
2410 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2411 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
2412 pe.index = MVPP2_PE_DSA_DEFAULT;
2413 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2414
2415 /* Shift 0 bytes */
2416 mvpp2_prs_sram_shift_set(&pe, 0, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2417 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2418
2419 /* Clear all sram ai bits for next iteration */
2420 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2421
2422 /* Unmask all ports */
2423 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2424
2425 mvpp2_prs_hw_write(priv, &pe);
2426}
2427
2428/* Match basic ethertypes */
2429static int mvpp2_prs_etype_init(struct mvpp2 *priv)
2430{
2431 struct mvpp2_prs_entry pe;
2432 int tid;
2433
2434 /* Ethertype: PPPoE */
2435 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2436 MVPP2_PE_LAST_FREE_TID);
2437 if (tid < 0)
2438 return tid;
2439
2440 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2441 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2442 pe.index = tid;
2443
2444 mvpp2_prs_match_etype(&pe, 0, ETH_P_PPP_SES);
2445
2446 mvpp2_prs_sram_shift_set(&pe, MVPP2_PPPOE_HDR_SIZE,
2447 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2448 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2449 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_PPPOE_MASK,
2450 MVPP2_PRS_RI_PPPOE_MASK);
2451
2452 /* Update shadow table and hw entry */
2453 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2454 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2455 priv->prs_shadow[pe.index].finish = false;
2456 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_PPPOE_MASK,
2457 MVPP2_PRS_RI_PPPOE_MASK);
2458 mvpp2_prs_hw_write(priv, &pe);
2459
2460 /* Ethertype: ARP */
2461 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2462 MVPP2_PE_LAST_FREE_TID);
2463 if (tid < 0)
2464 return tid;
2465
2466 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2467 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2468 pe.index = tid;
2469
2470 mvpp2_prs_match_etype(&pe, 0, ETH_P_ARP);
2471
2472 /* Generate flow in the next iteration*/
2473 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2474 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2475 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_ARP,
2476 MVPP2_PRS_RI_L3_PROTO_MASK);
2477 /* Set L3 offset */
2478 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2479 MVPP2_ETH_TYPE_LEN,
2480 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2481
2482 /* Update shadow table and hw entry */
2483 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2484 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2485 priv->prs_shadow[pe.index].finish = true;
2486 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_ARP,
2487 MVPP2_PRS_RI_L3_PROTO_MASK);
2488 mvpp2_prs_hw_write(priv, &pe);
2489
2490 /* Ethertype: LBTD */
2491 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2492 MVPP2_PE_LAST_FREE_TID);
2493 if (tid < 0)
2494 return tid;
2495
2496 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2497 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2498 pe.index = tid;
2499
2500 mvpp2_prs_match_etype(&pe, 0, MVPP2_IP_LBDT_TYPE);
2501
2502 /* Generate flow in the next iteration*/
2503 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2504 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2505 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2506 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2507 MVPP2_PRS_RI_CPU_CODE_MASK |
2508 MVPP2_PRS_RI_UDF3_MASK);
2509 /* Set L3 offset */
2510 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2511 MVPP2_ETH_TYPE_LEN,
2512 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2513
2514 /* Update shadow table and hw entry */
2515 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2516 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2517 priv->prs_shadow[pe.index].finish = true;
2518 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2519 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2520 MVPP2_PRS_RI_CPU_CODE_MASK |
2521 MVPP2_PRS_RI_UDF3_MASK);
2522 mvpp2_prs_hw_write(priv, &pe);
2523
2524 /* Ethertype: IPv4 without options */
2525 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2526 MVPP2_PE_LAST_FREE_TID);
2527 if (tid < 0)
2528 return tid;
2529
2530 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2531 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2532 pe.index = tid;
2533
2534 mvpp2_prs_match_etype(&pe, 0, ETH_P_IP);
2535 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2536 MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
2537 MVPP2_PRS_IPV4_HEAD_MASK |
2538 MVPP2_PRS_IPV4_IHL_MASK);
2539
2540 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2541 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
2542 MVPP2_PRS_RI_L3_PROTO_MASK);
2543 /* Skip eth_type + 4 bytes of IP header */
2544 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
2545 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2546 /* Set L3 offset */
2547 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2548 MVPP2_ETH_TYPE_LEN,
2549 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2550
2551 /* Update shadow table and hw entry */
2552 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2553 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2554 priv->prs_shadow[pe.index].finish = false;
2555 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4,
2556 MVPP2_PRS_RI_L3_PROTO_MASK);
2557 mvpp2_prs_hw_write(priv, &pe);
2558
2559 /* Ethertype: IPv4 with options */
2560 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2561 MVPP2_PE_LAST_FREE_TID);
2562 if (tid < 0)
2563 return tid;
2564
2565 pe.index = tid;
2566
2567 /* Clear tcam data before updating */
2568 pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(MVPP2_ETH_TYPE_LEN)] = 0x0;
2569 pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(MVPP2_ETH_TYPE_LEN)] = 0x0;
2570
2571 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2572 MVPP2_PRS_IPV4_HEAD,
2573 MVPP2_PRS_IPV4_HEAD_MASK);
2574
2575 /* Clear ri before updating */
2576 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2577 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2578 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
2579 MVPP2_PRS_RI_L3_PROTO_MASK);
2580
2581 /* Update shadow table and hw entry */
2582 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2583 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2584 priv->prs_shadow[pe.index].finish = false;
2585 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4_OPT,
2586 MVPP2_PRS_RI_L3_PROTO_MASK);
2587 mvpp2_prs_hw_write(priv, &pe);
2588
2589 /* Ethertype: IPv6 without options */
2590 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2591 MVPP2_PE_LAST_FREE_TID);
2592 if (tid < 0)
2593 return tid;
2594
2595 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2596 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2597 pe.index = tid;
2598
2599 mvpp2_prs_match_etype(&pe, 0, ETH_P_IPV6);
2600
2601 /* Skip DIP of IPV6 header */
2602 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 8 +
2603 MVPP2_MAX_L3_ADDR_SIZE,
2604 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2605 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2606 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
2607 MVPP2_PRS_RI_L3_PROTO_MASK);
2608 /* Set L3 offset */
2609 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2610 MVPP2_ETH_TYPE_LEN,
2611 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2612
2613 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2614 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2615 priv->prs_shadow[pe.index].finish = false;
2616 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP6,
2617 MVPP2_PRS_RI_L3_PROTO_MASK);
2618 mvpp2_prs_hw_write(priv, &pe);
2619
2620 /* Default entry for MVPP2_PRS_LU_L2 - Unknown ethtype */
2621 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2622 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2623 pe.index = MVPP2_PE_ETH_TYPE_UN;
2624
2625 /* Unmask all ports */
2626 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2627
2628 /* Generate flow in the next iteration*/
2629 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2630 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2631 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
2632 MVPP2_PRS_RI_L3_PROTO_MASK);
2633 /* Set L3 offset even it's unknown L3 */
2634 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2635 MVPP2_ETH_TYPE_LEN,
2636 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2637
2638 /* Update shadow table and hw entry */
2639 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2640 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2641 priv->prs_shadow[pe.index].finish = true;
2642 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_UN,
2643 MVPP2_PRS_RI_L3_PROTO_MASK);
2644 mvpp2_prs_hw_write(priv, &pe);
2645
2646 return 0;
2647}
2648
2649/* Configure vlan entries and detect up to 2 successive VLAN tags.
2650 * Possible options:
2651 * 0x8100, 0x88A8
2652 * 0x8100, 0x8100
2653 * 0x8100
2654 * 0x88A8
2655 */
2656static int mvpp2_prs_vlan_init(struct platform_device *pdev, struct mvpp2 *priv)
2657{
2658 struct mvpp2_prs_entry pe;
2659 int err;
2660
2661 priv->prs_double_vlans = devm_kcalloc(&pdev->dev, sizeof(bool),
2662 MVPP2_PRS_DBL_VLANS_MAX,
2663 GFP_KERNEL);
2664 if (!priv->prs_double_vlans)
2665 return -ENOMEM;
2666
2667 /* Double VLAN: 0x8100, 0x88A8 */
2668 err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021AD,
2669 MVPP2_PRS_PORT_MASK);
2670 if (err)
2671 return err;
2672
2673 /* Double VLAN: 0x8100, 0x8100 */
2674 err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021Q,
2675 MVPP2_PRS_PORT_MASK);
2676 if (err)
2677 return err;
2678
2679 /* Single VLAN: 0x88a8 */
2680 err = mvpp2_prs_vlan_add(priv, ETH_P_8021AD, MVPP2_PRS_SINGLE_VLAN_AI,
2681 MVPP2_PRS_PORT_MASK);
2682 if (err)
2683 return err;
2684
2685 /* Single VLAN: 0x8100 */
2686 err = mvpp2_prs_vlan_add(priv, ETH_P_8021Q, MVPP2_PRS_SINGLE_VLAN_AI,
2687 MVPP2_PRS_PORT_MASK);
2688 if (err)
2689 return err;
2690
2691 /* Set default double vlan entry */
2692 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2693 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2694 pe.index = MVPP2_PE_VLAN_DBL;
2695
2696 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2697 /* Clear ai for next iterations */
2698 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2699 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_DOUBLE,
2700 MVPP2_PRS_RI_VLAN_MASK);
2701
2702 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_DBL_VLAN_AI_BIT,
2703 MVPP2_PRS_DBL_VLAN_AI_BIT);
2704 /* Unmask all ports */
2705 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2706
2707 /* Update shadow table and hw entry */
2708 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
2709 mvpp2_prs_hw_write(priv, &pe);
2710
2711 /* Set default vlan none entry */
2712 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2713 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2714 pe.index = MVPP2_PE_VLAN_NONE;
2715
2716 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2717 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
2718 MVPP2_PRS_RI_VLAN_MASK);
2719
2720 /* Unmask all ports */
2721 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2722
2723 /* Update shadow table and hw entry */
2724 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
2725 mvpp2_prs_hw_write(priv, &pe);
2726
2727 return 0;
2728}
2729
2730/* Set entries for PPPoE ethertype */
2731static int mvpp2_prs_pppoe_init(struct mvpp2 *priv)
2732{
2733 struct mvpp2_prs_entry pe;
2734 int tid;
2735
2736 /* IPv4 over PPPoE with options */
2737 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2738 MVPP2_PE_LAST_FREE_TID);
2739 if (tid < 0)
2740 return tid;
2741
2742 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2743 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2744 pe.index = tid;
2745
2746 mvpp2_prs_match_etype(&pe, 0, PPP_IP);
2747
2748 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2749 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
2750 MVPP2_PRS_RI_L3_PROTO_MASK);
2751 /* Skip eth_type + 4 bytes of IP header */
2752 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
2753 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2754 /* Set L3 offset */
2755 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2756 MVPP2_ETH_TYPE_LEN,
2757 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2758
2759 /* Update shadow table and hw entry */
2760 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
2761 mvpp2_prs_hw_write(priv, &pe);
2762
2763 /* IPv4 over PPPoE without options */
2764 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2765 MVPP2_PE_LAST_FREE_TID);
2766 if (tid < 0)
2767 return tid;
2768
2769 pe.index = tid;
2770
2771 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2772 MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
2773 MVPP2_PRS_IPV4_HEAD_MASK |
2774 MVPP2_PRS_IPV4_IHL_MASK);
2775
2776 /* Clear ri before updating */
2777 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2778 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2779 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
2780 MVPP2_PRS_RI_L3_PROTO_MASK);
2781
2782 /* Update shadow table and hw entry */
2783 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
2784 mvpp2_prs_hw_write(priv, &pe);
2785
2786 /* IPv6 over PPPoE */
2787 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2788 MVPP2_PE_LAST_FREE_TID);
2789 if (tid < 0)
2790 return tid;
2791
2792 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2793 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2794 pe.index = tid;
2795
2796 mvpp2_prs_match_etype(&pe, 0, PPP_IPV6);
2797
2798 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2799 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
2800 MVPP2_PRS_RI_L3_PROTO_MASK);
2801 /* Skip eth_type + 4 bytes of IPv6 header */
2802 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
2803 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2804 /* Set L3 offset */
2805 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2806 MVPP2_ETH_TYPE_LEN,
2807 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2808
2809 /* Update shadow table and hw entry */
2810 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
2811 mvpp2_prs_hw_write(priv, &pe);
2812
2813 /* Non-IP over PPPoE */
2814 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2815 MVPP2_PE_LAST_FREE_TID);
2816 if (tid < 0)
2817 return tid;
2818
2819 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2820 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2821 pe.index = tid;
2822
2823 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
2824 MVPP2_PRS_RI_L3_PROTO_MASK);
2825
2826 /* Finished: go to flowid generation */
2827 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2828 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2829 /* Set L3 offset even if it's unknown L3 */
2830 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2831 MVPP2_ETH_TYPE_LEN,
2832 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2833
2834 /* Update shadow table and hw entry */
2835 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
2836 mvpp2_prs_hw_write(priv, &pe);
2837
2838 return 0;
2839}
2840
2841/* Initialize entries for IPv4 */
2842static int mvpp2_prs_ip4_init(struct mvpp2 *priv)
2843{
2844 struct mvpp2_prs_entry pe;
2845 int err;
2846
2847 /* Set entries for TCP, UDP and IGMP over IPv4 */
2848 err = mvpp2_prs_ip4_proto(priv, IPPROTO_TCP, MVPP2_PRS_RI_L4_TCP,
2849 MVPP2_PRS_RI_L4_PROTO_MASK);
2850 if (err)
2851 return err;
2852
2853 err = mvpp2_prs_ip4_proto(priv, IPPROTO_UDP, MVPP2_PRS_RI_L4_UDP,
2854 MVPP2_PRS_RI_L4_PROTO_MASK);
2855 if (err)
2856 return err;
2857
2858 err = mvpp2_prs_ip4_proto(priv, IPPROTO_IGMP,
2859 MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2860 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2861 MVPP2_PRS_RI_CPU_CODE_MASK |
2862 MVPP2_PRS_RI_UDF3_MASK);
2863 if (err)
2864 return err;
2865
2866 /* IPv4 Broadcast */
2867 err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_BROAD_CAST);
2868 if (err)
2869 return err;
2870
2871 /* IPv4 Multicast */
2872 err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
2873 if (err)
2874 return err;
2875
2876 /* Default IPv4 entry for unknown protocols */
2877 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2878 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
2879 pe.index = MVPP2_PE_IP4_PROTO_UN;
2880
2881 /* Set next lu to IPv4 */
2882 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2883 mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2884 /* Set L4 offset */
2885 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
2886 sizeof(struct iphdr) - 4,
2887 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2888 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
2889 MVPP2_PRS_IPV4_DIP_AI_BIT);
2890 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
2891 MVPP2_PRS_RI_L4_PROTO_MASK);
2892
2893 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
2894 /* Unmask all ports */
2895 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2896
2897 /* Update shadow table and hw entry */
2898 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2899 mvpp2_prs_hw_write(priv, &pe);
2900
2901 /* Default IPv4 entry for unicast address */
2902 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2903 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
2904 pe.index = MVPP2_PE_IP4_ADDR_UN;
2905
2906 /* Finished: go to flowid generation */
2907 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2908 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2909 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
2910 MVPP2_PRS_RI_L3_ADDR_MASK);
2911
2912 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
2913 MVPP2_PRS_IPV4_DIP_AI_BIT);
2914 /* Unmask all ports */
2915 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2916
2917 /* Update shadow table and hw entry */
2918 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2919 mvpp2_prs_hw_write(priv, &pe);
2920
2921 return 0;
2922}
2923
2924/* Initialize entries for IPv6 */
2925static int mvpp2_prs_ip6_init(struct mvpp2 *priv)
2926{
2927 struct mvpp2_prs_entry pe;
2928 int tid, err;
2929
2930 /* Set entries for TCP, UDP and ICMP over IPv6 */
2931 err = mvpp2_prs_ip6_proto(priv, IPPROTO_TCP,
2932 MVPP2_PRS_RI_L4_TCP,
2933 MVPP2_PRS_RI_L4_PROTO_MASK);
2934 if (err)
2935 return err;
2936
2937 err = mvpp2_prs_ip6_proto(priv, IPPROTO_UDP,
2938 MVPP2_PRS_RI_L4_UDP,
2939 MVPP2_PRS_RI_L4_PROTO_MASK);
2940 if (err)
2941 return err;
2942
2943 err = mvpp2_prs_ip6_proto(priv, IPPROTO_ICMPV6,
2944 MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2945 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2946 MVPP2_PRS_RI_CPU_CODE_MASK |
2947 MVPP2_PRS_RI_UDF3_MASK);
2948 if (err)
2949 return err;
2950
2951 /* IPv4 is the last header. This is similar case as 6-TCP or 17-UDP */
2952 /* Result Info: UDF7=1, DS lite */
2953 err = mvpp2_prs_ip6_proto(priv, IPPROTO_IPIP,
2954 MVPP2_PRS_RI_UDF7_IP6_LITE,
2955 MVPP2_PRS_RI_UDF7_MASK);
2956 if (err)
2957 return err;
2958
2959 /* IPv6 multicast */
2960 err = mvpp2_prs_ip6_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
2961 if (err)
2962 return err;
2963
2964 /* Entry for checking hop limit */
2965 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2966 MVPP2_PE_LAST_FREE_TID);
2967 if (tid < 0)
2968 return tid;
2969
2970 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2971 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2972 pe.index = tid;
2973
2974 /* Finished: go to flowid generation */
2975 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2976 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2977 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN |
2978 MVPP2_PRS_RI_DROP_MASK,
2979 MVPP2_PRS_RI_L3_PROTO_MASK |
2980 MVPP2_PRS_RI_DROP_MASK);
2981
2982 mvpp2_prs_tcam_data_byte_set(&pe, 1, 0x00, MVPP2_PRS_IPV6_HOP_MASK);
2983 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
2984 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2985
2986 /* Update shadow table and hw entry */
2987 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2988 mvpp2_prs_hw_write(priv, &pe);
2989
2990 /* Default IPv6 entry for unknown protocols */
2991 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2992 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2993 pe.index = MVPP2_PE_IP6_PROTO_UN;
2994
2995 /* Finished: go to flowid generation */
2996 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2997 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2998 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
2999 MVPP2_PRS_RI_L4_PROTO_MASK);
3000 /* Set L4 offset relatively to our current place */
3001 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
3002 sizeof(struct ipv6hdr) - 4,
3003 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3004
3005 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3006 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3007 /* Unmask all ports */
3008 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3009
3010 /* Update shadow table and hw entry */
3011 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3012 mvpp2_prs_hw_write(priv, &pe);
3013
3014 /* Default IPv6 entry for unknown ext protocols */
3015 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3016 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3017 pe.index = MVPP2_PE_IP6_EXT_PROTO_UN;
3018
3019 /* Finished: go to flowid generation */
3020 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3021 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3022 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3023 MVPP2_PRS_RI_L4_PROTO_MASK);
3024
3025 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_EXT_AI_BIT,
3026 MVPP2_PRS_IPV6_EXT_AI_BIT);
3027 /* Unmask all ports */
3028 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3029
3030 /* Update shadow table and hw entry */
3031 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3032 mvpp2_prs_hw_write(priv, &pe);
3033
3034 /* Default IPv6 entry for unicast address */
3035 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3036 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3037 pe.index = MVPP2_PE_IP6_ADDR_UN;
3038
3039 /* Finished: go to IPv6 again */
3040 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
3041 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
3042 MVPP2_PRS_RI_L3_ADDR_MASK);
3043 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3044 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3045 /* Shift back to IPV6 NH */
3046 mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3047
3048 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3049 /* Unmask all ports */
3050 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3051
3052 /* Update shadow table and hw entry */
3053 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
3054 mvpp2_prs_hw_write(priv, &pe);
3055
3056 return 0;
3057}
3058
3059/* Parser default initialization */
3060static int mvpp2_prs_default_init(struct platform_device *pdev,
3061 struct mvpp2 *priv)
3062{
3063 int err, index, i;
3064
3065 /* Enable tcam table */
3066 mvpp2_write(priv, MVPP2_PRS_TCAM_CTRL_REG, MVPP2_PRS_TCAM_EN_MASK);
3067
3068 /* Clear all tcam and sram entries */
3069 for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++) {
3070 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
3071 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
3072 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), 0);
3073
3074 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, index);
3075 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
3076 mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), 0);
3077 }
3078
3079 /* Invalidate all tcam entries */
3080 for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++)
3081 mvpp2_prs_hw_inv(priv, index);
3082
3083 priv->prs_shadow = devm_kcalloc(&pdev->dev, MVPP2_PRS_TCAM_SRAM_SIZE,
3084 sizeof(struct mvpp2_prs_shadow),
3085 GFP_KERNEL);
3086 if (!priv->prs_shadow)
3087 return -ENOMEM;
3088
3089 /* Always start from lookup = 0 */
3090 for (index = 0; index < MVPP2_MAX_PORTS; index++)
3091 mvpp2_prs_hw_port_init(priv, index, MVPP2_PRS_LU_MH,
3092 MVPP2_PRS_PORT_LU_MAX, 0);
3093
3094 mvpp2_prs_def_flow_init(priv);
3095
3096 mvpp2_prs_mh_init(priv);
3097
3098 mvpp2_prs_mac_init(priv);
3099
3100 mvpp2_prs_dsa_init(priv);
3101
3102 err = mvpp2_prs_etype_init(priv);
3103 if (err)
3104 return err;
3105
3106 err = mvpp2_prs_vlan_init(pdev, priv);
3107 if (err)
3108 return err;
3109
3110 err = mvpp2_prs_pppoe_init(priv);
3111 if (err)
3112 return err;
3113
3114 err = mvpp2_prs_ip6_init(priv);
3115 if (err)
3116 return err;
3117
3118 err = mvpp2_prs_ip4_init(priv);
3119 if (err)
3120 return err;
3121
3122 return 0;
3123}
3124
3125/* Compare MAC DA with tcam entry data */
3126static bool mvpp2_prs_mac_range_equals(struct mvpp2_prs_entry *pe,
3127 const u8 *da, unsigned char *mask)
3128{
3129 unsigned char tcam_byte, tcam_mask;
3130 int index;
3131
3132 for (index = 0; index < ETH_ALEN; index++) {
3133 mvpp2_prs_tcam_data_byte_get(pe, index, &tcam_byte, &tcam_mask);
3134 if (tcam_mask != mask[index])
3135 return false;
3136
3137 if ((tcam_mask & tcam_byte) != (da[index] & mask[index]))
3138 return false;
3139 }
3140
3141 return true;
3142}
3143
3144/* Find tcam entry with matched pair <MAC DA, port> */
3145static struct mvpp2_prs_entry *
3146mvpp2_prs_mac_da_range_find(struct mvpp2 *priv, int pmap, const u8 *da,
3147 unsigned char *mask, int udf_type)
3148{
3149 struct mvpp2_prs_entry *pe;
3150 int tid;
3151
3152 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
3153 if (!pe)
3154 return NULL;
3155 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
3156
3157 /* Go through the all entires with MVPP2_PRS_LU_MAC */
3158 for (tid = MVPP2_PE_FIRST_FREE_TID;
3159 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
3160 unsigned int entry_pmap;
3161
3162 if (!priv->prs_shadow[tid].valid ||
3163 (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
3164 (priv->prs_shadow[tid].udf != udf_type))
3165 continue;
3166
3167 pe->index = tid;
3168 mvpp2_prs_hw_read(priv, pe);
3169 entry_pmap = mvpp2_prs_tcam_port_map_get(pe);
3170
3171 if (mvpp2_prs_mac_range_equals(pe, da, mask) &&
3172 entry_pmap == pmap)
3173 return pe;
3174 }
3175 kfree(pe);
3176
3177 return NULL;
3178}
3179
3180/* Update parser's mac da entry */
3181static int mvpp2_prs_mac_da_accept(struct mvpp2 *priv, int port,
3182 const u8 *da, bool add)
3183{
3184 struct mvpp2_prs_entry *pe;
3185 unsigned int pmap, len, ri;
3186 unsigned char mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3187 int tid;
3188
3189 /* Scan TCAM and see if entry with this <MAC DA, port> already exist */
3190 pe = mvpp2_prs_mac_da_range_find(priv, (1 << port), da, mask,
3191 MVPP2_PRS_UDF_MAC_DEF);
3192
3193 /* No such entry */
3194 if (!pe) {
3195 if (!add)
3196 return 0;
3197
3198 /* Create new TCAM entry */
3199 /* Find first range mac entry*/
3200 for (tid = MVPP2_PE_FIRST_FREE_TID;
3201 tid <= MVPP2_PE_LAST_FREE_TID; tid++)
3202 if (priv->prs_shadow[tid].valid &&
3203 (priv->prs_shadow[tid].lu == MVPP2_PRS_LU_MAC) &&
3204 (priv->prs_shadow[tid].udf ==
3205 MVPP2_PRS_UDF_MAC_RANGE))
3206 break;
3207
3208 /* Go through the all entries from first to last */
3209 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3210 tid - 1);
3211 if (tid < 0)
3212 return tid;
3213
3214 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
3215 if (!pe)
Amitoj Kaur Chawlac2bb7bc2016-02-04 19:25:26 +05303216 return -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003217 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
3218 pe->index = tid;
3219
3220 /* Mask all ports */
3221 mvpp2_prs_tcam_port_map_set(pe, 0);
3222 }
3223
3224 /* Update port mask */
3225 mvpp2_prs_tcam_port_set(pe, port, add);
3226
3227 /* Invalidate the entry if no ports are left enabled */
3228 pmap = mvpp2_prs_tcam_port_map_get(pe);
3229 if (pmap == 0) {
3230 if (add) {
3231 kfree(pe);
Amitoj Kaur Chawlac2bb7bc2016-02-04 19:25:26 +05303232 return -EINVAL;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003233 }
3234 mvpp2_prs_hw_inv(priv, pe->index);
3235 priv->prs_shadow[pe->index].valid = false;
3236 kfree(pe);
3237 return 0;
3238 }
3239
3240 /* Continue - set next lookup */
3241 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_DSA);
3242
3243 /* Set match on DA */
3244 len = ETH_ALEN;
3245 while (len--)
3246 mvpp2_prs_tcam_data_byte_set(pe, len, da[len], 0xff);
3247
3248 /* Set result info bits */
3249 if (is_broadcast_ether_addr(da))
3250 ri = MVPP2_PRS_RI_L2_BCAST;
3251 else if (is_multicast_ether_addr(da))
3252 ri = MVPP2_PRS_RI_L2_MCAST;
3253 else
3254 ri = MVPP2_PRS_RI_L2_UCAST | MVPP2_PRS_RI_MAC_ME_MASK;
3255
3256 mvpp2_prs_sram_ri_update(pe, ri, MVPP2_PRS_RI_L2_CAST_MASK |
3257 MVPP2_PRS_RI_MAC_ME_MASK);
3258 mvpp2_prs_shadow_ri_set(priv, pe->index, ri, MVPP2_PRS_RI_L2_CAST_MASK |
3259 MVPP2_PRS_RI_MAC_ME_MASK);
3260
3261 /* Shift to ethertype */
3262 mvpp2_prs_sram_shift_set(pe, 2 * ETH_ALEN,
3263 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3264
3265 /* Update shadow table and hw entry */
3266 priv->prs_shadow[pe->index].udf = MVPP2_PRS_UDF_MAC_DEF;
3267 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_MAC);
3268 mvpp2_prs_hw_write(priv, pe);
3269
3270 kfree(pe);
3271
3272 return 0;
3273}
3274
3275static int mvpp2_prs_update_mac_da(struct net_device *dev, const u8 *da)
3276{
3277 struct mvpp2_port *port = netdev_priv(dev);
3278 int err;
3279
3280 /* Remove old parser entry */
3281 err = mvpp2_prs_mac_da_accept(port->priv, port->id, dev->dev_addr,
3282 false);
3283 if (err)
3284 return err;
3285
3286 /* Add new parser entry */
3287 err = mvpp2_prs_mac_da_accept(port->priv, port->id, da, true);
3288 if (err)
3289 return err;
3290
3291 /* Set addr in the device */
3292 ether_addr_copy(dev->dev_addr, da);
3293
3294 return 0;
3295}
3296
3297/* Delete all port's multicast simple (not range) entries */
3298static void mvpp2_prs_mcast_del_all(struct mvpp2 *priv, int port)
3299{
3300 struct mvpp2_prs_entry pe;
3301 int index, tid;
3302
3303 for (tid = MVPP2_PE_FIRST_FREE_TID;
3304 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
3305 unsigned char da[ETH_ALEN], da_mask[ETH_ALEN];
3306
3307 if (!priv->prs_shadow[tid].valid ||
3308 (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
3309 (priv->prs_shadow[tid].udf != MVPP2_PRS_UDF_MAC_DEF))
3310 continue;
3311
3312 /* Only simple mac entries */
3313 pe.index = tid;
3314 mvpp2_prs_hw_read(priv, &pe);
3315
3316 /* Read mac addr from entry */
3317 for (index = 0; index < ETH_ALEN; index++)
3318 mvpp2_prs_tcam_data_byte_get(&pe, index, &da[index],
3319 &da_mask[index]);
3320
3321 if (is_multicast_ether_addr(da) && !is_broadcast_ether_addr(da))
3322 /* Delete this entry */
3323 mvpp2_prs_mac_da_accept(priv, port, da, false);
3324 }
3325}
3326
3327static int mvpp2_prs_tag_mode_set(struct mvpp2 *priv, int port, int type)
3328{
3329 switch (type) {
3330 case MVPP2_TAG_TYPE_EDSA:
3331 /* Add port to EDSA entries */
3332 mvpp2_prs_dsa_tag_set(priv, port, true,
3333 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3334 mvpp2_prs_dsa_tag_set(priv, port, true,
3335 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3336 /* Remove port from DSA entries */
3337 mvpp2_prs_dsa_tag_set(priv, port, false,
3338 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3339 mvpp2_prs_dsa_tag_set(priv, port, false,
3340 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3341 break;
3342
3343 case MVPP2_TAG_TYPE_DSA:
3344 /* Add port to DSA entries */
3345 mvpp2_prs_dsa_tag_set(priv, port, true,
3346 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3347 mvpp2_prs_dsa_tag_set(priv, port, true,
3348 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3349 /* Remove port from EDSA entries */
3350 mvpp2_prs_dsa_tag_set(priv, port, false,
3351 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3352 mvpp2_prs_dsa_tag_set(priv, port, false,
3353 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3354 break;
3355
3356 case MVPP2_TAG_TYPE_MH:
3357 case MVPP2_TAG_TYPE_NONE:
3358 /* Remove port form EDSA and DSA entries */
3359 mvpp2_prs_dsa_tag_set(priv, port, false,
3360 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3361 mvpp2_prs_dsa_tag_set(priv, port, false,
3362 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3363 mvpp2_prs_dsa_tag_set(priv, port, false,
3364 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3365 mvpp2_prs_dsa_tag_set(priv, port, false,
3366 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3367 break;
3368
3369 default:
3370 if ((type < 0) || (type > MVPP2_TAG_TYPE_EDSA))
3371 return -EINVAL;
3372 }
3373
3374 return 0;
3375}
3376
3377/* Set prs flow for the port */
3378static int mvpp2_prs_def_flow(struct mvpp2_port *port)
3379{
3380 struct mvpp2_prs_entry *pe;
3381 int tid;
3382
3383 pe = mvpp2_prs_flow_find(port->priv, port->id);
3384
3385 /* Such entry not exist */
3386 if (!pe) {
3387 /* Go through the all entires from last to first */
3388 tid = mvpp2_prs_tcam_first_free(port->priv,
3389 MVPP2_PE_LAST_FREE_TID,
3390 MVPP2_PE_FIRST_FREE_TID);
3391 if (tid < 0)
3392 return tid;
3393
3394 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
3395 if (!pe)
3396 return -ENOMEM;
3397
3398 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
3399 pe->index = tid;
3400
3401 /* Set flow ID*/
3402 mvpp2_prs_sram_ai_update(pe, port->id, MVPP2_PRS_FLOW_ID_MASK);
3403 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
3404
3405 /* Update shadow table */
3406 mvpp2_prs_shadow_set(port->priv, pe->index, MVPP2_PRS_LU_FLOWS);
3407 }
3408
3409 mvpp2_prs_tcam_port_map_set(pe, (1 << port->id));
3410 mvpp2_prs_hw_write(port->priv, pe);
3411 kfree(pe);
3412
3413 return 0;
3414}
3415
3416/* Classifier configuration routines */
3417
3418/* Update classification flow table registers */
3419static void mvpp2_cls_flow_write(struct mvpp2 *priv,
3420 struct mvpp2_cls_flow_entry *fe)
3421{
3422 mvpp2_write(priv, MVPP2_CLS_FLOW_INDEX_REG, fe->index);
3423 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL0_REG, fe->data[0]);
3424 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL1_REG, fe->data[1]);
3425 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL2_REG, fe->data[2]);
3426}
3427
3428/* Update classification lookup table register */
3429static void mvpp2_cls_lookup_write(struct mvpp2 *priv,
3430 struct mvpp2_cls_lookup_entry *le)
3431{
3432 u32 val;
3433
3434 val = (le->way << MVPP2_CLS_LKP_INDEX_WAY_OFFS) | le->lkpid;
3435 mvpp2_write(priv, MVPP2_CLS_LKP_INDEX_REG, val);
3436 mvpp2_write(priv, MVPP2_CLS_LKP_TBL_REG, le->data);
3437}
3438
3439/* Classifier default initialization */
3440static void mvpp2_cls_init(struct mvpp2 *priv)
3441{
3442 struct mvpp2_cls_lookup_entry le;
3443 struct mvpp2_cls_flow_entry fe;
3444 int index;
3445
3446 /* Enable classifier */
3447 mvpp2_write(priv, MVPP2_CLS_MODE_REG, MVPP2_CLS_MODE_ACTIVE_MASK);
3448
3449 /* Clear classifier flow table */
Arnd Bergmanne8f967c2016-11-24 17:28:12 +01003450 memset(&fe.data, 0, sizeof(fe.data));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003451 for (index = 0; index < MVPP2_CLS_FLOWS_TBL_SIZE; index++) {
3452 fe.index = index;
3453 mvpp2_cls_flow_write(priv, &fe);
3454 }
3455
3456 /* Clear classifier lookup table */
3457 le.data = 0;
3458 for (index = 0; index < MVPP2_CLS_LKP_TBL_SIZE; index++) {
3459 le.lkpid = index;
3460 le.way = 0;
3461 mvpp2_cls_lookup_write(priv, &le);
3462
3463 le.way = 1;
3464 mvpp2_cls_lookup_write(priv, &le);
3465 }
3466}
3467
3468static void mvpp2_cls_port_config(struct mvpp2_port *port)
3469{
3470 struct mvpp2_cls_lookup_entry le;
3471 u32 val;
3472
3473 /* Set way for the port */
3474 val = mvpp2_read(port->priv, MVPP2_CLS_PORT_WAY_REG);
3475 val &= ~MVPP2_CLS_PORT_WAY_MASK(port->id);
3476 mvpp2_write(port->priv, MVPP2_CLS_PORT_WAY_REG, val);
3477
3478 /* Pick the entry to be accessed in lookup ID decoding table
3479 * according to the way and lkpid.
3480 */
3481 le.lkpid = port->id;
3482 le.way = 0;
3483 le.data = 0;
3484
3485 /* Set initial CPU queue for receiving packets */
3486 le.data &= ~MVPP2_CLS_LKP_TBL_RXQ_MASK;
3487 le.data |= port->first_rxq;
3488
3489 /* Disable classification engines */
3490 le.data &= ~MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK;
3491
3492 /* Update lookup ID table entry */
3493 mvpp2_cls_lookup_write(port->priv, &le);
3494}
3495
3496/* Set CPU queue number for oversize packets */
3497static void mvpp2_cls_oversize_rxq_set(struct mvpp2_port *port)
3498{
3499 u32 val;
3500
3501 mvpp2_write(port->priv, MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port->id),
3502 port->first_rxq & MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK);
3503
3504 mvpp2_write(port->priv, MVPP2_CLS_SWFWD_P2HQ_REG(port->id),
3505 (port->first_rxq >> MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS));
3506
3507 val = mvpp2_read(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG);
3508 val |= MVPP2_CLS_SWFWD_PCTRL_MASK(port->id);
3509 mvpp2_write(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG, val);
3510}
3511
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003512static void *mvpp2_frag_alloc(const struct mvpp2_bm_pool *pool)
3513{
3514 if (likely(pool->frag_size <= PAGE_SIZE))
3515 return netdev_alloc_frag(pool->frag_size);
3516 else
3517 return kmalloc(pool->frag_size, GFP_ATOMIC);
3518}
3519
3520static void mvpp2_frag_free(const struct mvpp2_bm_pool *pool, void *data)
3521{
3522 if (likely(pool->frag_size <= PAGE_SIZE))
3523 skb_free_frag(data);
3524 else
3525 kfree(data);
3526}
3527
Marcin Wojtas3f518502014-07-10 16:52:13 -03003528/* Buffer Manager configuration routines */
3529
3530/* Create pool */
3531static int mvpp2_bm_pool_create(struct platform_device *pdev,
3532 struct mvpp2 *priv,
3533 struct mvpp2_bm_pool *bm_pool, int size)
3534{
Marcin Wojtas3f518502014-07-10 16:52:13 -03003535 u32 val;
3536
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003537 /* Number of buffer pointers must be a multiple of 16, as per
3538 * hardware constraints
3539 */
3540 if (!IS_ALIGNED(size, 16))
3541 return -EINVAL;
3542
3543 /* PPv2.1 needs 8 bytes per buffer pointer, PPv2.2 needs 16
3544 * bytes per buffer pointer
3545 */
3546 if (priv->hw_version == MVPP21)
3547 bm_pool->size_bytes = 2 * sizeof(u32) * size;
3548 else
3549 bm_pool->size_bytes = 2 * sizeof(u64) * size;
3550
3551 bm_pool->virt_addr = dma_alloc_coherent(&pdev->dev, bm_pool->size_bytes,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003552 &bm_pool->dma_addr,
Marcin Wojtas3f518502014-07-10 16:52:13 -03003553 GFP_KERNEL);
3554 if (!bm_pool->virt_addr)
3555 return -ENOMEM;
3556
Thomas Petazzonid3158802017-02-21 11:28:13 +01003557 if (!IS_ALIGNED((unsigned long)bm_pool->virt_addr,
3558 MVPP2_BM_POOL_PTR_ALIGN)) {
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003559 dma_free_coherent(&pdev->dev, bm_pool->size_bytes,
3560 bm_pool->virt_addr, bm_pool->dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003561 dev_err(&pdev->dev, "BM pool %d is not %d bytes aligned\n",
3562 bm_pool->id, MVPP2_BM_POOL_PTR_ALIGN);
3563 return -ENOMEM;
3564 }
3565
3566 mvpp2_write(priv, MVPP2_BM_POOL_BASE_REG(bm_pool->id),
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003567 lower_32_bits(bm_pool->dma_addr));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003568 mvpp2_write(priv, MVPP2_BM_POOL_SIZE_REG(bm_pool->id), size);
3569
3570 val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
3571 val |= MVPP2_BM_START_MASK;
3572 mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
3573
3574 bm_pool->type = MVPP2_BM_FREE;
3575 bm_pool->size = size;
3576 bm_pool->pkt_size = 0;
3577 bm_pool->buf_num = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003578
3579 return 0;
3580}
3581
3582/* Set pool buffer size */
3583static void mvpp2_bm_pool_bufsize_set(struct mvpp2 *priv,
3584 struct mvpp2_bm_pool *bm_pool,
3585 int buf_size)
3586{
3587 u32 val;
3588
3589 bm_pool->buf_size = buf_size;
3590
3591 val = ALIGN(buf_size, 1 << MVPP2_POOL_BUF_SIZE_OFFSET);
3592 mvpp2_write(priv, MVPP2_POOL_BUF_SIZE_REG(bm_pool->id), val);
3593}
3594
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003595static void mvpp2_bm_bufs_get_addrs(struct device *dev, struct mvpp2 *priv,
3596 struct mvpp2_bm_pool *bm_pool,
3597 dma_addr_t *dma_addr,
3598 phys_addr_t *phys_addr)
3599{
3600 *dma_addr = mvpp2_read(priv, MVPP2_BM_PHY_ALLOC_REG(bm_pool->id));
3601 *phys_addr = mvpp2_read(priv, MVPP2_BM_VIRT_ALLOC_REG);
3602
3603 if (priv->hw_version == MVPP22) {
3604 u32 val;
3605 u32 dma_addr_highbits, phys_addr_highbits;
3606
3607 val = mvpp2_read(priv, MVPP22_BM_ADDR_HIGH_ALLOC);
3608 dma_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_PHYS_MASK);
3609 phys_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_VIRT_MASK) >>
3610 MVPP22_BM_ADDR_HIGH_VIRT_SHIFT;
3611
3612 if (sizeof(dma_addr_t) == 8)
3613 *dma_addr |= (u64)dma_addr_highbits << 32;
3614
3615 if (sizeof(phys_addr_t) == 8)
3616 *phys_addr |= (u64)phys_addr_highbits << 32;
3617 }
3618}
3619
Ezequiel Garcia7861f122014-07-21 13:48:14 -03003620/* Free all buffers from the pool */
Marcin Wojtas4229d502015-12-03 15:20:50 +01003621static void mvpp2_bm_bufs_free(struct device *dev, struct mvpp2 *priv,
3622 struct mvpp2_bm_pool *bm_pool)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003623{
3624 int i;
3625
Ezequiel Garcia7861f122014-07-21 13:48:14 -03003626 for (i = 0; i < bm_pool->buf_num; i++) {
Thomas Petazzoni20396132017-03-07 16:53:00 +01003627 dma_addr_t buf_dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003628 phys_addr_t buf_phys_addr;
3629 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003630
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003631 mvpp2_bm_bufs_get_addrs(dev, priv, bm_pool,
3632 &buf_dma_addr, &buf_phys_addr);
Marcin Wojtas4229d502015-12-03 15:20:50 +01003633
Thomas Petazzoni20396132017-03-07 16:53:00 +01003634 dma_unmap_single(dev, buf_dma_addr,
Marcin Wojtas4229d502015-12-03 15:20:50 +01003635 bm_pool->buf_size, DMA_FROM_DEVICE);
3636
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003637 data = (void *)phys_to_virt(buf_phys_addr);
3638 if (!data)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003639 break;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003640
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003641 mvpp2_frag_free(bm_pool, data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003642 }
3643
3644 /* Update BM driver with number of buffers removed from pool */
3645 bm_pool->buf_num -= i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003646}
3647
3648/* Cleanup pool */
3649static int mvpp2_bm_pool_destroy(struct platform_device *pdev,
3650 struct mvpp2 *priv,
3651 struct mvpp2_bm_pool *bm_pool)
3652{
Marcin Wojtas3f518502014-07-10 16:52:13 -03003653 u32 val;
3654
Marcin Wojtas4229d502015-12-03 15:20:50 +01003655 mvpp2_bm_bufs_free(&pdev->dev, priv, bm_pool);
Ezequiel Garciad74c96c2014-07-21 13:48:13 -03003656 if (bm_pool->buf_num) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03003657 WARN(1, "cannot free all buffers in pool %d\n", bm_pool->id);
3658 return 0;
3659 }
3660
3661 val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
3662 val |= MVPP2_BM_STOP_MASK;
3663 mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
3664
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003665 dma_free_coherent(&pdev->dev, bm_pool->size_bytes,
Marcin Wojtas3f518502014-07-10 16:52:13 -03003666 bm_pool->virt_addr,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003667 bm_pool->dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003668 return 0;
3669}
3670
3671static int mvpp2_bm_pools_init(struct platform_device *pdev,
3672 struct mvpp2 *priv)
3673{
3674 int i, err, size;
3675 struct mvpp2_bm_pool *bm_pool;
3676
3677 /* Create all pools with maximum size */
3678 size = MVPP2_BM_POOL_SIZE_MAX;
3679 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
3680 bm_pool = &priv->bm_pools[i];
3681 bm_pool->id = i;
3682 err = mvpp2_bm_pool_create(pdev, priv, bm_pool, size);
3683 if (err)
3684 goto err_unroll_pools;
3685 mvpp2_bm_pool_bufsize_set(priv, bm_pool, 0);
3686 }
3687 return 0;
3688
3689err_unroll_pools:
3690 dev_err(&pdev->dev, "failed to create BM pool %d, size %d\n", i, size);
3691 for (i = i - 1; i >= 0; i--)
3692 mvpp2_bm_pool_destroy(pdev, priv, &priv->bm_pools[i]);
3693 return err;
3694}
3695
3696static int mvpp2_bm_init(struct platform_device *pdev, struct mvpp2 *priv)
3697{
3698 int i, err;
3699
3700 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
3701 /* Mask BM all interrupts */
3702 mvpp2_write(priv, MVPP2_BM_INTR_MASK_REG(i), 0);
3703 /* Clear BM cause register */
3704 mvpp2_write(priv, MVPP2_BM_INTR_CAUSE_REG(i), 0);
3705 }
3706
3707 /* Allocate and initialize BM pools */
3708 priv->bm_pools = devm_kcalloc(&pdev->dev, MVPP2_BM_POOLS_NUM,
3709 sizeof(struct mvpp2_bm_pool), GFP_KERNEL);
3710 if (!priv->bm_pools)
3711 return -ENOMEM;
3712
3713 err = mvpp2_bm_pools_init(pdev, priv);
3714 if (err < 0)
3715 return err;
3716 return 0;
3717}
3718
3719/* Attach long pool to rxq */
3720static void mvpp2_rxq_long_pool_set(struct mvpp2_port *port,
3721 int lrxq, int long_pool)
3722{
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003723 u32 val, mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003724 int prxq;
3725
3726 /* Get queue physical ID */
3727 prxq = port->rxqs[lrxq]->id;
3728
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003729 if (port->priv->hw_version == MVPP21)
3730 mask = MVPP21_RXQ_POOL_LONG_MASK;
3731 else
3732 mask = MVPP22_RXQ_POOL_LONG_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003733
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003734 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
3735 val &= ~mask;
3736 val |= (long_pool << MVPP2_RXQ_POOL_LONG_OFFS) & mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003737 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
3738}
3739
3740/* Attach short pool to rxq */
3741static void mvpp2_rxq_short_pool_set(struct mvpp2_port *port,
3742 int lrxq, int short_pool)
3743{
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003744 u32 val, mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003745 int prxq;
3746
3747 /* Get queue physical ID */
3748 prxq = port->rxqs[lrxq]->id;
3749
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003750 if (port->priv->hw_version == MVPP21)
3751 mask = MVPP21_RXQ_POOL_SHORT_MASK;
3752 else
3753 mask = MVPP22_RXQ_POOL_SHORT_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003754
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003755 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
3756 val &= ~mask;
3757 val |= (short_pool << MVPP2_RXQ_POOL_SHORT_OFFS) & mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003758 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
3759}
3760
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003761static void *mvpp2_buf_alloc(struct mvpp2_port *port,
3762 struct mvpp2_bm_pool *bm_pool,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003763 dma_addr_t *buf_dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003764 phys_addr_t *buf_phys_addr,
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003765 gfp_t gfp_mask)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003766{
Thomas Petazzoni20396132017-03-07 16:53:00 +01003767 dma_addr_t dma_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003768 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003769
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003770 data = mvpp2_frag_alloc(bm_pool);
3771 if (!data)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003772 return NULL;
3773
Thomas Petazzoni20396132017-03-07 16:53:00 +01003774 dma_addr = dma_map_single(port->dev->dev.parent, data,
3775 MVPP2_RX_BUF_SIZE(bm_pool->pkt_size),
3776 DMA_FROM_DEVICE);
3777 if (unlikely(dma_mapping_error(port->dev->dev.parent, dma_addr))) {
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003778 mvpp2_frag_free(bm_pool, data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003779 return NULL;
3780 }
Thomas Petazzoni20396132017-03-07 16:53:00 +01003781 *buf_dma_addr = dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003782 *buf_phys_addr = virt_to_phys(data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003783
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003784 return data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003785}
3786
3787/* Set pool number in a BM cookie */
3788static inline u32 mvpp2_bm_cookie_pool_set(u32 cookie, int pool)
3789{
3790 u32 bm;
3791
3792 bm = cookie & ~(0xFF << MVPP2_BM_COOKIE_POOL_OFFS);
3793 bm |= ((pool & 0xFF) << MVPP2_BM_COOKIE_POOL_OFFS);
3794
3795 return bm;
3796}
3797
3798/* Get pool number from a BM cookie */
Thomas Petazzonid3158802017-02-21 11:28:13 +01003799static inline int mvpp2_bm_cookie_pool_get(unsigned long cookie)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003800{
3801 return (cookie >> MVPP2_BM_COOKIE_POOL_OFFS) & 0xFF;
3802}
3803
3804/* Release buffer to BM */
3805static inline void mvpp2_bm_pool_put(struct mvpp2_port *port, int pool,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003806 dma_addr_t buf_dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003807 phys_addr_t buf_phys_addr)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003808{
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003809 if (port->priv->hw_version == MVPP22) {
3810 u32 val = 0;
3811
3812 if (sizeof(dma_addr_t) == 8)
3813 val |= upper_32_bits(buf_dma_addr) &
3814 MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK;
3815
3816 if (sizeof(phys_addr_t) == 8)
3817 val |= (upper_32_bits(buf_phys_addr)
3818 << MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT) &
3819 MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK;
3820
3821 mvpp2_write(port->priv, MVPP22_BM_ADDR_HIGH_RLS_REG, val);
3822 }
3823
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003824 /* MVPP2_BM_VIRT_RLS_REG is not interpreted by HW, and simply
3825 * returned in the "cookie" field of the RX
3826 * descriptor. Instead of storing the virtual address, we
3827 * store the physical address
3828 */
3829 mvpp2_write(port->priv, MVPP2_BM_VIRT_RLS_REG, buf_phys_addr);
Thomas Petazzoni20396132017-03-07 16:53:00 +01003830 mvpp2_write(port->priv, MVPP2_BM_PHY_RLS_REG(pool), buf_dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003831}
3832
Marcin Wojtas3f518502014-07-10 16:52:13 -03003833/* Refill BM pool */
3834static void mvpp2_pool_refill(struct mvpp2_port *port, u32 bm,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003835 dma_addr_t dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003836 phys_addr_t phys_addr)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003837{
3838 int pool = mvpp2_bm_cookie_pool_get(bm);
3839
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003840 mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003841}
3842
3843/* Allocate buffers for the pool */
3844static int mvpp2_bm_bufs_add(struct mvpp2_port *port,
3845 struct mvpp2_bm_pool *bm_pool, int buf_num)
3846{
Marcin Wojtas3f518502014-07-10 16:52:13 -03003847 int i, buf_size, total_size;
Thomas Petazzoni20396132017-03-07 16:53:00 +01003848 dma_addr_t dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003849 phys_addr_t phys_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003850 void *buf;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003851
3852 buf_size = MVPP2_RX_BUF_SIZE(bm_pool->pkt_size);
3853 total_size = MVPP2_RX_TOTAL_SIZE(buf_size);
3854
3855 if (buf_num < 0 ||
3856 (buf_num + bm_pool->buf_num > bm_pool->size)) {
3857 netdev_err(port->dev,
3858 "cannot allocate %d buffers for pool %d\n",
3859 buf_num, bm_pool->id);
3860 return 0;
3861 }
3862
Marcin Wojtas3f518502014-07-10 16:52:13 -03003863 for (i = 0; i < buf_num; i++) {
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003864 buf = mvpp2_buf_alloc(port, bm_pool, &dma_addr,
3865 &phys_addr, GFP_KERNEL);
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003866 if (!buf)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003867 break;
3868
Thomas Petazzoni20396132017-03-07 16:53:00 +01003869 mvpp2_bm_pool_put(port, bm_pool->id, dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003870 phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003871 }
3872
3873 /* Update BM driver with number of buffers added to pool */
3874 bm_pool->buf_num += i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003875
3876 netdev_dbg(port->dev,
3877 "%s pool %d: pkt_size=%4d, buf_size=%4d, total_size=%4d\n",
3878 bm_pool->type == MVPP2_BM_SWF_SHORT ? "short" : " long",
3879 bm_pool->id, bm_pool->pkt_size, buf_size, total_size);
3880
3881 netdev_dbg(port->dev,
3882 "%s pool %d: %d of %d buffers added\n",
3883 bm_pool->type == MVPP2_BM_SWF_SHORT ? "short" : " long",
3884 bm_pool->id, i, buf_num);
3885 return i;
3886}
3887
3888/* Notify the driver that BM pool is being used as specific type and return the
3889 * pool pointer on success
3890 */
3891static struct mvpp2_bm_pool *
3892mvpp2_bm_pool_use(struct mvpp2_port *port, int pool, enum mvpp2_bm_type type,
3893 int pkt_size)
3894{
Marcin Wojtas3f518502014-07-10 16:52:13 -03003895 struct mvpp2_bm_pool *new_pool = &port->priv->bm_pools[pool];
3896 int num;
3897
3898 if (new_pool->type != MVPP2_BM_FREE && new_pool->type != type) {
3899 netdev_err(port->dev, "mixing pool types is forbidden\n");
3900 return NULL;
3901 }
3902
Marcin Wojtas3f518502014-07-10 16:52:13 -03003903 if (new_pool->type == MVPP2_BM_FREE)
3904 new_pool->type = type;
3905
3906 /* Allocate buffers in case BM pool is used as long pool, but packet
3907 * size doesn't match MTU or BM pool hasn't being used yet
3908 */
3909 if (((type == MVPP2_BM_SWF_LONG) && (pkt_size > new_pool->pkt_size)) ||
3910 (new_pool->pkt_size == 0)) {
3911 int pkts_num;
3912
3913 /* Set default buffer number or free all the buffers in case
3914 * the pool is not empty
3915 */
3916 pkts_num = new_pool->buf_num;
3917 if (pkts_num == 0)
3918 pkts_num = type == MVPP2_BM_SWF_LONG ?
3919 MVPP2_BM_LONG_BUF_NUM :
3920 MVPP2_BM_SHORT_BUF_NUM;
3921 else
Marcin Wojtas4229d502015-12-03 15:20:50 +01003922 mvpp2_bm_bufs_free(port->dev->dev.parent,
3923 port->priv, new_pool);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003924
3925 new_pool->pkt_size = pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003926 new_pool->frag_size =
3927 SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
3928 MVPP2_SKB_SHINFO_SIZE;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003929
3930 /* Allocate buffers for this pool */
3931 num = mvpp2_bm_bufs_add(port, new_pool, pkts_num);
3932 if (num != pkts_num) {
3933 WARN(1, "pool %d: %d of %d allocated\n",
3934 new_pool->id, num, pkts_num);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003935 return NULL;
3936 }
3937 }
3938
3939 mvpp2_bm_pool_bufsize_set(port->priv, new_pool,
3940 MVPP2_RX_BUF_SIZE(new_pool->pkt_size));
3941
Marcin Wojtas3f518502014-07-10 16:52:13 -03003942 return new_pool;
3943}
3944
3945/* Initialize pools for swf */
3946static int mvpp2_swf_bm_pool_init(struct mvpp2_port *port)
3947{
Marcin Wojtas3f518502014-07-10 16:52:13 -03003948 int rxq;
3949
3950 if (!port->pool_long) {
3951 port->pool_long =
3952 mvpp2_bm_pool_use(port, MVPP2_BM_SWF_LONG_POOL(port->id),
3953 MVPP2_BM_SWF_LONG,
3954 port->pkt_size);
3955 if (!port->pool_long)
3956 return -ENOMEM;
3957
Marcin Wojtas3f518502014-07-10 16:52:13 -03003958 port->pool_long->port_map |= (1 << port->id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003959
3960 for (rxq = 0; rxq < rxq_number; rxq++)
3961 mvpp2_rxq_long_pool_set(port, rxq, port->pool_long->id);
3962 }
3963
3964 if (!port->pool_short) {
3965 port->pool_short =
3966 mvpp2_bm_pool_use(port, MVPP2_BM_SWF_SHORT_POOL,
3967 MVPP2_BM_SWF_SHORT,
3968 MVPP2_BM_SHORT_PKT_SIZE);
3969 if (!port->pool_short)
3970 return -ENOMEM;
3971
Marcin Wojtas3f518502014-07-10 16:52:13 -03003972 port->pool_short->port_map |= (1 << port->id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003973
3974 for (rxq = 0; rxq < rxq_number; rxq++)
3975 mvpp2_rxq_short_pool_set(port, rxq,
3976 port->pool_short->id);
3977 }
3978
3979 return 0;
3980}
3981
3982static int mvpp2_bm_update_mtu(struct net_device *dev, int mtu)
3983{
3984 struct mvpp2_port *port = netdev_priv(dev);
3985 struct mvpp2_bm_pool *port_pool = port->pool_long;
3986 int num, pkts_num = port_pool->buf_num;
3987 int pkt_size = MVPP2_RX_PKT_SIZE(mtu);
3988
3989 /* Update BM pool with new buffer size */
Marcin Wojtas4229d502015-12-03 15:20:50 +01003990 mvpp2_bm_bufs_free(dev->dev.parent, port->priv, port_pool);
Ezequiel Garciad74c96c2014-07-21 13:48:13 -03003991 if (port_pool->buf_num) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03003992 WARN(1, "cannot free all buffers in pool %d\n", port_pool->id);
3993 return -EIO;
3994 }
3995
3996 port_pool->pkt_size = pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003997 port_pool->frag_size = SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
3998 MVPP2_SKB_SHINFO_SIZE;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003999 num = mvpp2_bm_bufs_add(port, port_pool, pkts_num);
4000 if (num != pkts_num) {
4001 WARN(1, "pool %d: %d of %d allocated\n",
4002 port_pool->id, num, pkts_num);
4003 return -EIO;
4004 }
4005
4006 mvpp2_bm_pool_bufsize_set(port->priv, port_pool,
4007 MVPP2_RX_BUF_SIZE(port_pool->pkt_size));
4008 dev->mtu = mtu;
4009 netdev_update_features(dev);
4010 return 0;
4011}
4012
4013static inline void mvpp2_interrupts_enable(struct mvpp2_port *port)
4014{
4015 int cpu, cpu_mask = 0;
4016
4017 for_each_present_cpu(cpu)
4018 cpu_mask |= 1 << cpu;
4019 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
4020 MVPP2_ISR_ENABLE_INTERRUPT(cpu_mask));
4021}
4022
4023static inline void mvpp2_interrupts_disable(struct mvpp2_port *port)
4024{
4025 int cpu, cpu_mask = 0;
4026
4027 for_each_present_cpu(cpu)
4028 cpu_mask |= 1 << cpu;
4029 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
4030 MVPP2_ISR_DISABLE_INTERRUPT(cpu_mask));
4031}
4032
4033/* Mask the current CPU's Rx/Tx interrupts */
4034static void mvpp2_interrupts_mask(void *arg)
4035{
4036 struct mvpp2_port *port = arg;
4037
4038 mvpp2_write(port->priv, MVPP2_ISR_RX_TX_MASK_REG(port->id), 0);
4039}
4040
4041/* Unmask the current CPU's Rx/Tx interrupts */
4042static void mvpp2_interrupts_unmask(void *arg)
4043{
4044 struct mvpp2_port *port = arg;
4045
4046 mvpp2_write(port->priv, MVPP2_ISR_RX_TX_MASK_REG(port->id),
4047 (MVPP2_CAUSE_MISC_SUM_MASK |
Marcin Wojtas3f518502014-07-10 16:52:13 -03004048 MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK));
4049}
4050
4051/* Port configuration routines */
4052
4053static void mvpp2_port_mii_set(struct mvpp2_port *port)
4054{
Marcin Wojtas08a23752014-07-21 13:48:12 -03004055 u32 val;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004056
Marcin Wojtas08a23752014-07-21 13:48:12 -03004057 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004058
Marcin Wojtas08a23752014-07-21 13:48:12 -03004059 switch (port->phy_interface) {
4060 case PHY_INTERFACE_MODE_SGMII:
4061 val |= MVPP2_GMAC_INBAND_AN_MASK;
4062 break;
4063 case PHY_INTERFACE_MODE_RGMII:
4064 val |= MVPP2_GMAC_PORT_RGMII_MASK;
4065 default:
4066 val &= ~MVPP2_GMAC_PCS_ENABLE_MASK;
4067 }
4068
4069 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4070}
4071
4072static void mvpp2_port_fc_adv_enable(struct mvpp2_port *port)
4073{
4074 u32 val;
4075
4076 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4077 val |= MVPP2_GMAC_FC_ADV_EN;
4078 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004079}
4080
4081static void mvpp2_port_enable(struct mvpp2_port *port)
4082{
4083 u32 val;
4084
4085 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4086 val |= MVPP2_GMAC_PORT_EN_MASK;
4087 val |= MVPP2_GMAC_MIB_CNTR_EN_MASK;
4088 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4089}
4090
4091static void mvpp2_port_disable(struct mvpp2_port *port)
4092{
4093 u32 val;
4094
4095 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4096 val &= ~(MVPP2_GMAC_PORT_EN_MASK);
4097 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4098}
4099
4100/* Set IEEE 802.3x Flow Control Xon Packet Transmission Mode */
4101static void mvpp2_port_periodic_xon_disable(struct mvpp2_port *port)
4102{
4103 u32 val;
4104
4105 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG) &
4106 ~MVPP2_GMAC_PERIODIC_XON_EN_MASK;
4107 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
4108}
4109
4110/* Configure loopback port */
4111static void mvpp2_port_loopback_set(struct mvpp2_port *port)
4112{
4113 u32 val;
4114
4115 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
4116
4117 if (port->speed == 1000)
4118 val |= MVPP2_GMAC_GMII_LB_EN_MASK;
4119 else
4120 val &= ~MVPP2_GMAC_GMII_LB_EN_MASK;
4121
4122 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
4123 val |= MVPP2_GMAC_PCS_LB_EN_MASK;
4124 else
4125 val &= ~MVPP2_GMAC_PCS_LB_EN_MASK;
4126
4127 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
4128}
4129
4130static void mvpp2_port_reset(struct mvpp2_port *port)
4131{
4132 u32 val;
4133
4134 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
4135 ~MVPP2_GMAC_PORT_RESET_MASK;
4136 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4137
4138 while (readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
4139 MVPP2_GMAC_PORT_RESET_MASK)
4140 continue;
4141}
4142
4143/* Change maximum receive size of the port */
4144static inline void mvpp2_gmac_max_rx_size_set(struct mvpp2_port *port)
4145{
4146 u32 val;
4147
4148 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4149 val &= ~MVPP2_GMAC_MAX_RX_SIZE_MASK;
4150 val |= (((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
4151 MVPP2_GMAC_MAX_RX_SIZE_OFFS);
4152 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4153}
4154
4155/* Set defaults to the MVPP2 port */
4156static void mvpp2_defaults_set(struct mvpp2_port *port)
4157{
4158 int tx_port_num, val, queue, ptxq, lrxq;
4159
Thomas Petazzoni3d9017d2017-03-07 16:53:11 +01004160 if (port->priv->hw_version == MVPP21) {
4161 /* Configure port to loopback if needed */
4162 if (port->flags & MVPP2_F_LOOPBACK)
4163 mvpp2_port_loopback_set(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004164
Thomas Petazzoni3d9017d2017-03-07 16:53:11 +01004165 /* Update TX FIFO MIN Threshold */
4166 val = readl(port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
4167 val &= ~MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK;
4168 /* Min. TX threshold must be less than minimal packet length */
4169 val |= MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(64 - 4 - 2);
4170 writel(val, port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
4171 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004172
4173 /* Disable Legacy WRR, Disable EJP, Release from reset */
4174 tx_port_num = mvpp2_egress_port(port);
4175 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG,
4176 tx_port_num);
4177 mvpp2_write(port->priv, MVPP2_TXP_SCHED_CMD_1_REG, 0);
4178
4179 /* Close bandwidth for all queues */
4180 for (queue = 0; queue < MVPP2_MAX_TXQ; queue++) {
4181 ptxq = mvpp2_txq_phys(port->id, queue);
4182 mvpp2_write(port->priv,
4183 MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(ptxq), 0);
4184 }
4185
4186 /* Set refill period to 1 usec, refill tokens
4187 * and bucket size to maximum
4188 */
4189 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PERIOD_REG,
4190 port->priv->tclk / USEC_PER_SEC);
4191 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_REFILL_REG);
4192 val &= ~MVPP2_TXP_REFILL_PERIOD_ALL_MASK;
4193 val |= MVPP2_TXP_REFILL_PERIOD_MASK(1);
4194 val |= MVPP2_TXP_REFILL_TOKENS_ALL_MASK;
4195 mvpp2_write(port->priv, MVPP2_TXP_SCHED_REFILL_REG, val);
4196 val = MVPP2_TXP_TOKEN_SIZE_MAX;
4197 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
4198
4199 /* Set MaximumLowLatencyPacketSize value to 256 */
4200 mvpp2_write(port->priv, MVPP2_RX_CTRL_REG(port->id),
4201 MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK |
4202 MVPP2_RX_LOW_LATENCY_PKT_SIZE(256));
4203
4204 /* Enable Rx cache snoop */
4205 for (lrxq = 0; lrxq < rxq_number; lrxq++) {
4206 queue = port->rxqs[lrxq]->id;
4207 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
4208 val |= MVPP2_SNOOP_PKT_SIZE_MASK |
4209 MVPP2_SNOOP_BUF_HDR_MASK;
4210 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
4211 }
4212
4213 /* At default, mask all interrupts to all present cpus */
4214 mvpp2_interrupts_disable(port);
4215}
4216
4217/* Enable/disable receiving packets */
4218static void mvpp2_ingress_enable(struct mvpp2_port *port)
4219{
4220 u32 val;
4221 int lrxq, queue;
4222
4223 for (lrxq = 0; lrxq < rxq_number; lrxq++) {
4224 queue = port->rxqs[lrxq]->id;
4225 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
4226 val &= ~MVPP2_RXQ_DISABLE_MASK;
4227 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
4228 }
4229}
4230
4231static void mvpp2_ingress_disable(struct mvpp2_port *port)
4232{
4233 u32 val;
4234 int lrxq, queue;
4235
4236 for (lrxq = 0; lrxq < rxq_number; lrxq++) {
4237 queue = port->rxqs[lrxq]->id;
4238 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
4239 val |= MVPP2_RXQ_DISABLE_MASK;
4240 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
4241 }
4242}
4243
4244/* Enable transmit via physical egress queue
4245 * - HW starts take descriptors from DRAM
4246 */
4247static void mvpp2_egress_enable(struct mvpp2_port *port)
4248{
4249 u32 qmap;
4250 int queue;
4251 int tx_port_num = mvpp2_egress_port(port);
4252
4253 /* Enable all initialized TXs. */
4254 qmap = 0;
4255 for (queue = 0; queue < txq_number; queue++) {
4256 struct mvpp2_tx_queue *txq = port->txqs[queue];
4257
4258 if (txq->descs != NULL)
4259 qmap |= (1 << queue);
4260 }
4261
4262 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
4263 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG, qmap);
4264}
4265
4266/* Disable transmit via physical egress queue
4267 * - HW doesn't take descriptors from DRAM
4268 */
4269static void mvpp2_egress_disable(struct mvpp2_port *port)
4270{
4271 u32 reg_data;
4272 int delay;
4273 int tx_port_num = mvpp2_egress_port(port);
4274
4275 /* Issue stop command for active channels only */
4276 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
4277 reg_data = (mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG)) &
4278 MVPP2_TXP_SCHED_ENQ_MASK;
4279 if (reg_data != 0)
4280 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG,
4281 (reg_data << MVPP2_TXP_SCHED_DISQ_OFFSET));
4282
4283 /* Wait for all Tx activity to terminate. */
4284 delay = 0;
4285 do {
4286 if (delay >= MVPP2_TX_DISABLE_TIMEOUT_MSEC) {
4287 netdev_warn(port->dev,
4288 "Tx stop timed out, status=0x%08x\n",
4289 reg_data);
4290 break;
4291 }
4292 mdelay(1);
4293 delay++;
4294
4295 /* Check port TX Command register that all
4296 * Tx queues are stopped
4297 */
4298 reg_data = mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG);
4299 } while (reg_data & MVPP2_TXP_SCHED_ENQ_MASK);
4300}
4301
4302/* Rx descriptors helper methods */
4303
4304/* Get number of Rx descriptors occupied by received packets */
4305static inline int
4306mvpp2_rxq_received(struct mvpp2_port *port, int rxq_id)
4307{
4308 u32 val = mvpp2_read(port->priv, MVPP2_RXQ_STATUS_REG(rxq_id));
4309
4310 return val & MVPP2_RXQ_OCCUPIED_MASK;
4311}
4312
4313/* Update Rx queue status with the number of occupied and available
4314 * Rx descriptor slots.
4315 */
4316static inline void
4317mvpp2_rxq_status_update(struct mvpp2_port *port, int rxq_id,
4318 int used_count, int free_count)
4319{
4320 /* Decrement the number of used descriptors and increment count
4321 * increment the number of free descriptors.
4322 */
4323 u32 val = used_count | (free_count << MVPP2_RXQ_NUM_NEW_OFFSET);
4324
4325 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_UPDATE_REG(rxq_id), val);
4326}
4327
4328/* Get pointer to next RX descriptor to be processed by SW */
4329static inline struct mvpp2_rx_desc *
4330mvpp2_rxq_next_desc_get(struct mvpp2_rx_queue *rxq)
4331{
4332 int rx_desc = rxq->next_desc_to_proc;
4333
4334 rxq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(rxq, rx_desc);
4335 prefetch(rxq->descs + rxq->next_desc_to_proc);
4336 return rxq->descs + rx_desc;
4337}
4338
4339/* Set rx queue offset */
4340static void mvpp2_rxq_offset_set(struct mvpp2_port *port,
4341 int prxq, int offset)
4342{
4343 u32 val;
4344
4345 /* Convert offset from bytes to units of 32 bytes */
4346 offset = offset >> 5;
4347
4348 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
4349 val &= ~MVPP2_RXQ_PACKET_OFFSET_MASK;
4350
4351 /* Offset is in */
4352 val |= ((offset << MVPP2_RXQ_PACKET_OFFSET_OFFS) &
4353 MVPP2_RXQ_PACKET_OFFSET_MASK);
4354
4355 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
4356}
4357
4358/* Obtain BM cookie information from descriptor */
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01004359static u32 mvpp2_bm_cookie_build(struct mvpp2_port *port,
4360 struct mvpp2_rx_desc *rx_desc)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004361{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004362 int cpu = smp_processor_id();
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01004363 int pool;
4364
4365 pool = (mvpp2_rxdesc_status_get(port, rx_desc) &
4366 MVPP2_RXD_BM_POOL_ID_MASK) >>
4367 MVPP2_RXD_BM_POOL_ID_OFFS;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004368
4369 return ((pool & 0xFF) << MVPP2_BM_COOKIE_POOL_OFFS) |
4370 ((cpu & 0xFF) << MVPP2_BM_COOKIE_CPU_OFFS);
4371}
4372
4373/* Tx descriptors helper methods */
4374
Marcin Wojtas3f518502014-07-10 16:52:13 -03004375/* Get pointer to next Tx descriptor to be processed (send) by HW */
4376static struct mvpp2_tx_desc *
4377mvpp2_txq_next_desc_get(struct mvpp2_tx_queue *txq)
4378{
4379 int tx_desc = txq->next_desc_to_proc;
4380
4381 txq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(txq, tx_desc);
4382 return txq->descs + tx_desc;
4383}
4384
4385/* Update HW with number of aggregated Tx descriptors to be sent */
4386static void mvpp2_aggr_txq_pend_desc_add(struct mvpp2_port *port, int pending)
4387{
4388 /* aggregated access - relevant TXQ number is written in TX desc */
4389 mvpp2_write(port->priv, MVPP2_AGGR_TXQ_UPDATE_REG, pending);
4390}
4391
4392
4393/* Check if there are enough free descriptors in aggregated txq.
4394 * If not, update the number of occupied descriptors and repeat the check.
4395 */
4396static int mvpp2_aggr_desc_num_check(struct mvpp2 *priv,
4397 struct mvpp2_tx_queue *aggr_txq, int num)
4398{
4399 if ((aggr_txq->count + num) > aggr_txq->size) {
4400 /* Update number of occupied aggregated Tx descriptors */
4401 int cpu = smp_processor_id();
4402 u32 val = mvpp2_read(priv, MVPP2_AGGR_TXQ_STATUS_REG(cpu));
4403
4404 aggr_txq->count = val & MVPP2_AGGR_TXQ_PENDING_MASK;
4405 }
4406
4407 if ((aggr_txq->count + num) > aggr_txq->size)
4408 return -ENOMEM;
4409
4410 return 0;
4411}
4412
4413/* Reserved Tx descriptors allocation request */
4414static int mvpp2_txq_alloc_reserved_desc(struct mvpp2 *priv,
4415 struct mvpp2_tx_queue *txq, int num)
4416{
4417 u32 val;
4418
4419 val = (txq->id << MVPP2_TXQ_RSVD_REQ_Q_OFFSET) | num;
4420 mvpp2_write(priv, MVPP2_TXQ_RSVD_REQ_REG, val);
4421
4422 val = mvpp2_read(priv, MVPP2_TXQ_RSVD_RSLT_REG);
4423
4424 return val & MVPP2_TXQ_RSVD_RSLT_MASK;
4425}
4426
4427/* Check if there are enough reserved descriptors for transmission.
4428 * If not, request chunk of reserved descriptors and check again.
4429 */
4430static int mvpp2_txq_reserved_desc_num_proc(struct mvpp2 *priv,
4431 struct mvpp2_tx_queue *txq,
4432 struct mvpp2_txq_pcpu *txq_pcpu,
4433 int num)
4434{
4435 int req, cpu, desc_count;
4436
4437 if (txq_pcpu->reserved_num >= num)
4438 return 0;
4439
4440 /* Not enough descriptors reserved! Update the reserved descriptor
4441 * count and check again.
4442 */
4443
4444 desc_count = 0;
4445 /* Compute total of used descriptors */
4446 for_each_present_cpu(cpu) {
4447 struct mvpp2_txq_pcpu *txq_pcpu_aux;
4448
4449 txq_pcpu_aux = per_cpu_ptr(txq->pcpu, cpu);
4450 desc_count += txq_pcpu_aux->count;
4451 desc_count += txq_pcpu_aux->reserved_num;
4452 }
4453
4454 req = max(MVPP2_CPU_DESC_CHUNK, num - txq_pcpu->reserved_num);
4455 desc_count += req;
4456
4457 if (desc_count >
4458 (txq->size - (num_present_cpus() * MVPP2_CPU_DESC_CHUNK)))
4459 return -ENOMEM;
4460
4461 txq_pcpu->reserved_num += mvpp2_txq_alloc_reserved_desc(priv, txq, req);
4462
4463 /* OK, the descriptor cound has been updated: check again. */
4464 if (txq_pcpu->reserved_num < num)
4465 return -ENOMEM;
4466 return 0;
4467}
4468
4469/* Release the last allocated Tx descriptor. Useful to handle DMA
4470 * mapping failures in the Tx path.
4471 */
4472static void mvpp2_txq_desc_put(struct mvpp2_tx_queue *txq)
4473{
4474 if (txq->next_desc_to_proc == 0)
4475 txq->next_desc_to_proc = txq->last_desc - 1;
4476 else
4477 txq->next_desc_to_proc--;
4478}
4479
4480/* Set Tx descriptors fields relevant for CSUM calculation */
4481static u32 mvpp2_txq_desc_csum(int l3_offs, int l3_proto,
4482 int ip_hdr_len, int l4_proto)
4483{
4484 u32 command;
4485
4486 /* fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
4487 * G_L4_chk, L4_type required only for checksum calculation
4488 */
4489 command = (l3_offs << MVPP2_TXD_L3_OFF_SHIFT);
4490 command |= (ip_hdr_len << MVPP2_TXD_IP_HLEN_SHIFT);
4491 command |= MVPP2_TXD_IP_CSUM_DISABLE;
4492
4493 if (l3_proto == swab16(ETH_P_IP)) {
4494 command &= ~MVPP2_TXD_IP_CSUM_DISABLE; /* enable IPv4 csum */
4495 command &= ~MVPP2_TXD_L3_IP6; /* enable IPv4 */
4496 } else {
4497 command |= MVPP2_TXD_L3_IP6; /* enable IPv6 */
4498 }
4499
4500 if (l4_proto == IPPROTO_TCP) {
4501 command &= ~MVPP2_TXD_L4_UDP; /* enable TCP */
4502 command &= ~MVPP2_TXD_L4_CSUM_FRAG; /* generate L4 csum */
4503 } else if (l4_proto == IPPROTO_UDP) {
4504 command |= MVPP2_TXD_L4_UDP; /* enable UDP */
4505 command &= ~MVPP2_TXD_L4_CSUM_FRAG; /* generate L4 csum */
4506 } else {
4507 command |= MVPP2_TXD_L4_CSUM_NOT;
4508 }
4509
4510 return command;
4511}
4512
4513/* Get number of sent descriptors and decrement counter.
4514 * The number of sent descriptors is returned.
4515 * Per-CPU access
4516 */
4517static inline int mvpp2_txq_sent_desc_proc(struct mvpp2_port *port,
4518 struct mvpp2_tx_queue *txq)
4519{
4520 u32 val;
4521
4522 /* Reading status reg resets transmitted descriptor counter */
4523 val = mvpp2_read(port->priv, MVPP2_TXQ_SENT_REG(txq->id));
4524
4525 return (val & MVPP2_TRANSMITTED_COUNT_MASK) >>
4526 MVPP2_TRANSMITTED_COUNT_OFFSET;
4527}
4528
4529static void mvpp2_txq_sent_counter_clear(void *arg)
4530{
4531 struct mvpp2_port *port = arg;
4532 int queue;
4533
4534 for (queue = 0; queue < txq_number; queue++) {
4535 int id = port->txqs[queue]->id;
4536
4537 mvpp2_read(port->priv, MVPP2_TXQ_SENT_REG(id));
4538 }
4539}
4540
4541/* Set max sizes for Tx queues */
4542static void mvpp2_txp_max_tx_size_set(struct mvpp2_port *port)
4543{
4544 u32 val, size, mtu;
4545 int txq, tx_port_num;
4546
4547 mtu = port->pkt_size * 8;
4548 if (mtu > MVPP2_TXP_MTU_MAX)
4549 mtu = MVPP2_TXP_MTU_MAX;
4550
4551 /* WA for wrong Token bucket update: Set MTU value = 3*real MTU value */
4552 mtu = 3 * mtu;
4553
4554 /* Indirect access to registers */
4555 tx_port_num = mvpp2_egress_port(port);
4556 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
4557
4558 /* Set MTU */
4559 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_MTU_REG);
4560 val &= ~MVPP2_TXP_MTU_MAX;
4561 val |= mtu;
4562 mvpp2_write(port->priv, MVPP2_TXP_SCHED_MTU_REG, val);
4563
4564 /* TXP token size and all TXQs token size must be larger that MTU */
4565 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG);
4566 size = val & MVPP2_TXP_TOKEN_SIZE_MAX;
4567 if (size < mtu) {
4568 size = mtu;
4569 val &= ~MVPP2_TXP_TOKEN_SIZE_MAX;
4570 val |= size;
4571 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
4572 }
4573
4574 for (txq = 0; txq < txq_number; txq++) {
4575 val = mvpp2_read(port->priv,
4576 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq));
4577 size = val & MVPP2_TXQ_TOKEN_SIZE_MAX;
4578
4579 if (size < mtu) {
4580 size = mtu;
4581 val &= ~MVPP2_TXQ_TOKEN_SIZE_MAX;
4582 val |= size;
4583 mvpp2_write(port->priv,
4584 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq),
4585 val);
4586 }
4587 }
4588}
4589
4590/* Set the number of packets that will be received before Rx interrupt
4591 * will be generated by HW.
4592 */
4593static void mvpp2_rx_pkts_coal_set(struct mvpp2_port *port,
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01004594 struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004595{
Thomas Petazzonif8b0d5f2017-02-21 11:28:03 +01004596 if (rxq->pkts_coal > MVPP2_OCCUPIED_THRESH_MASK)
4597 rxq->pkts_coal = MVPP2_OCCUPIED_THRESH_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004598
Marcin Wojtas3f518502014-07-10 16:52:13 -03004599 mvpp2_write(port->priv, MVPP2_RXQ_NUM_REG, rxq->id);
Thomas Petazzonif8b0d5f2017-02-21 11:28:03 +01004600 mvpp2_write(port->priv, MVPP2_RXQ_THRESH_REG,
4601 rxq->pkts_coal);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004602}
4603
Thomas Petazzoniab426762017-02-21 11:28:04 +01004604static u32 mvpp2_usec_to_cycles(u32 usec, unsigned long clk_hz)
4605{
4606 u64 tmp = (u64)clk_hz * usec;
4607
4608 do_div(tmp, USEC_PER_SEC);
4609
4610 return tmp > U32_MAX ? U32_MAX : tmp;
4611}
4612
4613static u32 mvpp2_cycles_to_usec(u32 cycles, unsigned long clk_hz)
4614{
4615 u64 tmp = (u64)cycles * USEC_PER_SEC;
4616
4617 do_div(tmp, clk_hz);
4618
4619 return tmp > U32_MAX ? U32_MAX : tmp;
4620}
4621
Marcin Wojtas3f518502014-07-10 16:52:13 -03004622/* Set the time delay in usec before Rx interrupt */
4623static void mvpp2_rx_time_coal_set(struct mvpp2_port *port,
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01004624 struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004625{
Thomas Petazzoniab426762017-02-21 11:28:04 +01004626 unsigned long freq = port->priv->tclk;
4627 u32 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004628
Thomas Petazzoniab426762017-02-21 11:28:04 +01004629 if (val > MVPP2_MAX_ISR_RX_THRESHOLD) {
4630 rxq->time_coal =
4631 mvpp2_cycles_to_usec(MVPP2_MAX_ISR_RX_THRESHOLD, freq);
4632
4633 /* re-evaluate to get actual register value */
4634 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
4635 }
4636
Marcin Wojtas3f518502014-07-10 16:52:13 -03004637 mvpp2_write(port->priv, MVPP2_ISR_RX_THRESHOLD_REG(rxq->id), val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004638}
4639
Marcin Wojtas3f518502014-07-10 16:52:13 -03004640/* Free Tx queue skbuffs */
4641static void mvpp2_txq_bufs_free(struct mvpp2_port *port,
4642 struct mvpp2_tx_queue *txq,
4643 struct mvpp2_txq_pcpu *txq_pcpu, int num)
4644{
4645 int i;
4646
4647 for (i = 0; i < num; i++) {
Thomas Petazzoni83544912016-12-21 11:28:49 +01004648 struct mvpp2_txq_pcpu_buf *tx_buf =
4649 txq_pcpu->buffs + txq_pcpu->txq_get_index;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004650
Thomas Petazzoni20396132017-03-07 16:53:00 +01004651 dma_unmap_single(port->dev->dev.parent, tx_buf->dma,
Thomas Petazzoni83544912016-12-21 11:28:49 +01004652 tx_buf->size, DMA_TO_DEVICE);
Thomas Petazzoni36fb7432017-02-21 11:28:05 +01004653 if (tx_buf->skb)
4654 dev_kfree_skb_any(tx_buf->skb);
4655
4656 mvpp2_txq_inc_get(txq_pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004657 }
4658}
4659
4660static inline struct mvpp2_rx_queue *mvpp2_get_rx_queue(struct mvpp2_port *port,
4661 u32 cause)
4662{
4663 int queue = fls(cause) - 1;
4664
4665 return port->rxqs[queue];
4666}
4667
4668static inline struct mvpp2_tx_queue *mvpp2_get_tx_queue(struct mvpp2_port *port,
4669 u32 cause)
4670{
Marcin Wojtasedc660f2015-08-06 19:00:30 +02004671 int queue = fls(cause) - 1;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004672
4673 return port->txqs[queue];
4674}
4675
4676/* Handle end of transmission */
4677static void mvpp2_txq_done(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
4678 struct mvpp2_txq_pcpu *txq_pcpu)
4679{
4680 struct netdev_queue *nq = netdev_get_tx_queue(port->dev, txq->log_id);
4681 int tx_done;
4682
4683 if (txq_pcpu->cpu != smp_processor_id())
4684 netdev_err(port->dev, "wrong cpu on the end of Tx processing\n");
4685
4686 tx_done = mvpp2_txq_sent_desc_proc(port, txq);
4687 if (!tx_done)
4688 return;
4689 mvpp2_txq_bufs_free(port, txq, txq_pcpu, tx_done);
4690
4691 txq_pcpu->count -= tx_done;
4692
4693 if (netif_tx_queue_stopped(nq))
4694 if (txq_pcpu->size - txq_pcpu->count >= MAX_SKB_FRAGS + 1)
4695 netif_tx_wake_queue(nq);
4696}
4697
Marcin Wojtasedc660f2015-08-06 19:00:30 +02004698static unsigned int mvpp2_tx_done(struct mvpp2_port *port, u32 cause)
4699{
4700 struct mvpp2_tx_queue *txq;
4701 struct mvpp2_txq_pcpu *txq_pcpu;
4702 unsigned int tx_todo = 0;
4703
4704 while (cause) {
4705 txq = mvpp2_get_tx_queue(port, cause);
4706 if (!txq)
4707 break;
4708
4709 txq_pcpu = this_cpu_ptr(txq->pcpu);
4710
4711 if (txq_pcpu->count) {
4712 mvpp2_txq_done(port, txq, txq_pcpu);
4713 tx_todo += txq_pcpu->count;
4714 }
4715
4716 cause &= ~(1 << txq->log_id);
4717 }
4718 return tx_todo;
4719}
4720
Marcin Wojtas3f518502014-07-10 16:52:13 -03004721/* Rx/Tx queue initialization/cleanup methods */
4722
4723/* Allocate and initialize descriptors for aggr TXQ */
4724static int mvpp2_aggr_txq_init(struct platform_device *pdev,
4725 struct mvpp2_tx_queue *aggr_txq,
4726 int desc_num, int cpu,
4727 struct mvpp2 *priv)
4728{
4729 /* Allocate memory for TX descriptors */
4730 aggr_txq->descs = dma_alloc_coherent(&pdev->dev,
4731 desc_num * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004732 &aggr_txq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004733 if (!aggr_txq->descs)
4734 return -ENOMEM;
4735
Marcin Wojtas3f518502014-07-10 16:52:13 -03004736 aggr_txq->last_desc = aggr_txq->size - 1;
4737
4738 /* Aggr TXQ no reset WA */
4739 aggr_txq->next_desc_to_proc = mvpp2_read(priv,
4740 MVPP2_AGGR_TXQ_INDEX_REG(cpu));
4741
4742 /* Set Tx descriptors queue starting address */
4743 /* indirect access */
4744 mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu),
Thomas Petazzoni20396132017-03-07 16:53:00 +01004745 aggr_txq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004746 mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu), desc_num);
4747
4748 return 0;
4749}
4750
4751/* Create a specified Rx queue */
4752static int mvpp2_rxq_init(struct mvpp2_port *port,
4753 struct mvpp2_rx_queue *rxq)
4754
4755{
4756 rxq->size = port->rx_ring_size;
4757
4758 /* Allocate memory for RX descriptors */
4759 rxq->descs = dma_alloc_coherent(port->dev->dev.parent,
4760 rxq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004761 &rxq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004762 if (!rxq->descs)
4763 return -ENOMEM;
4764
Marcin Wojtas3f518502014-07-10 16:52:13 -03004765 rxq->last_desc = rxq->size - 1;
4766
4767 /* Zero occupied and non-occupied counters - direct access */
4768 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
4769
4770 /* Set Rx descriptors queue starting address - indirect access */
4771 mvpp2_write(port->priv, MVPP2_RXQ_NUM_REG, rxq->id);
Thomas Petazzoni20396132017-03-07 16:53:00 +01004772 mvpp2_write(port->priv, MVPP2_RXQ_DESC_ADDR_REG, rxq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004773 mvpp2_write(port->priv, MVPP2_RXQ_DESC_SIZE_REG, rxq->size);
4774 mvpp2_write(port->priv, MVPP2_RXQ_INDEX_REG, 0);
4775
4776 /* Set Offset */
4777 mvpp2_rxq_offset_set(port, rxq->id, NET_SKB_PAD);
4778
4779 /* Set coalescing pkts and time */
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01004780 mvpp2_rx_pkts_coal_set(port, rxq);
4781 mvpp2_rx_time_coal_set(port, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004782
4783 /* Add number of descriptors ready for receiving packets */
4784 mvpp2_rxq_status_update(port, rxq->id, 0, rxq->size);
4785
4786 return 0;
4787}
4788
4789/* Push packets received by the RXQ to BM pool */
4790static void mvpp2_rxq_drop_pkts(struct mvpp2_port *port,
4791 struct mvpp2_rx_queue *rxq)
4792{
4793 int rx_received, i;
4794
4795 rx_received = mvpp2_rxq_received(port, rxq->id);
4796 if (!rx_received)
4797 return;
4798
4799 for (i = 0; i < rx_received; i++) {
4800 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01004801 u32 bm = mvpp2_bm_cookie_build(port, rx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004802
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01004803 mvpp2_pool_refill(port, bm,
4804 mvpp2_rxdesc_dma_addr_get(port, rx_desc),
4805 mvpp2_rxdesc_cookie_get(port, rx_desc));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004806 }
4807 mvpp2_rxq_status_update(port, rxq->id, rx_received, rx_received);
4808}
4809
4810/* Cleanup Rx queue */
4811static void mvpp2_rxq_deinit(struct mvpp2_port *port,
4812 struct mvpp2_rx_queue *rxq)
4813{
4814 mvpp2_rxq_drop_pkts(port, rxq);
4815
4816 if (rxq->descs)
4817 dma_free_coherent(port->dev->dev.parent,
4818 rxq->size * MVPP2_DESC_ALIGNED_SIZE,
4819 rxq->descs,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004820 rxq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004821
4822 rxq->descs = NULL;
4823 rxq->last_desc = 0;
4824 rxq->next_desc_to_proc = 0;
Thomas Petazzoni20396132017-03-07 16:53:00 +01004825 rxq->descs_dma = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004826
4827 /* Clear Rx descriptors queue starting address and size;
4828 * free descriptor number
4829 */
4830 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
4831 mvpp2_write(port->priv, MVPP2_RXQ_NUM_REG, rxq->id);
4832 mvpp2_write(port->priv, MVPP2_RXQ_DESC_ADDR_REG, 0);
4833 mvpp2_write(port->priv, MVPP2_RXQ_DESC_SIZE_REG, 0);
4834}
4835
4836/* Create and initialize a Tx queue */
4837static int mvpp2_txq_init(struct mvpp2_port *port,
4838 struct mvpp2_tx_queue *txq)
4839{
4840 u32 val;
4841 int cpu, desc, desc_per_txq, tx_port_num;
4842 struct mvpp2_txq_pcpu *txq_pcpu;
4843
4844 txq->size = port->tx_ring_size;
4845
4846 /* Allocate memory for Tx descriptors */
4847 txq->descs = dma_alloc_coherent(port->dev->dev.parent,
4848 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004849 &txq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004850 if (!txq->descs)
4851 return -ENOMEM;
4852
Marcin Wojtas3f518502014-07-10 16:52:13 -03004853 txq->last_desc = txq->size - 1;
4854
4855 /* Set Tx descriptors queue starting address - indirect access */
4856 mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
Thomas Petazzoni20396132017-03-07 16:53:00 +01004857 mvpp2_write(port->priv, MVPP2_TXQ_DESC_ADDR_REG, txq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004858 mvpp2_write(port->priv, MVPP2_TXQ_DESC_SIZE_REG, txq->size &
4859 MVPP2_TXQ_DESC_SIZE_MASK);
4860 mvpp2_write(port->priv, MVPP2_TXQ_INDEX_REG, 0);
4861 mvpp2_write(port->priv, MVPP2_TXQ_RSVD_CLR_REG,
4862 txq->id << MVPP2_TXQ_RSVD_CLR_OFFSET);
4863 val = mvpp2_read(port->priv, MVPP2_TXQ_PENDING_REG);
4864 val &= ~MVPP2_TXQ_PENDING_MASK;
4865 mvpp2_write(port->priv, MVPP2_TXQ_PENDING_REG, val);
4866
4867 /* Calculate base address in prefetch buffer. We reserve 16 descriptors
4868 * for each existing TXQ.
4869 * TCONTS for PON port must be continuous from 0 to MVPP2_MAX_TCONT
4870 * GBE ports assumed to be continious from 0 to MVPP2_MAX_PORTS
4871 */
4872 desc_per_txq = 16;
4873 desc = (port->id * MVPP2_MAX_TXQ * desc_per_txq) +
4874 (txq->log_id * desc_per_txq);
4875
4876 mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG,
4877 MVPP2_PREF_BUF_PTR(desc) | MVPP2_PREF_BUF_SIZE_16 |
4878 MVPP2_PREF_BUF_THRESH(desc_per_txq/2));
4879
4880 /* WRR / EJP configuration - indirect access */
4881 tx_port_num = mvpp2_egress_port(port);
4882 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
4883
4884 val = mvpp2_read(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id));
4885 val &= ~MVPP2_TXQ_REFILL_PERIOD_ALL_MASK;
4886 val |= MVPP2_TXQ_REFILL_PERIOD_MASK(1);
4887 val |= MVPP2_TXQ_REFILL_TOKENS_ALL_MASK;
4888 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id), val);
4889
4890 val = MVPP2_TXQ_TOKEN_SIZE_MAX;
4891 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq->log_id),
4892 val);
4893
4894 for_each_present_cpu(cpu) {
4895 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
4896 txq_pcpu->size = txq->size;
Thomas Petazzoni83544912016-12-21 11:28:49 +01004897 txq_pcpu->buffs = kmalloc(txq_pcpu->size *
4898 sizeof(struct mvpp2_txq_pcpu_buf),
4899 GFP_KERNEL);
4900 if (!txq_pcpu->buffs)
Marcin Wojtas71ce3912015-08-06 19:00:29 +02004901 goto error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004902
4903 txq_pcpu->count = 0;
4904 txq_pcpu->reserved_num = 0;
4905 txq_pcpu->txq_put_index = 0;
4906 txq_pcpu->txq_get_index = 0;
4907 }
4908
4909 return 0;
Marcin Wojtas71ce3912015-08-06 19:00:29 +02004910
4911error:
4912 for_each_present_cpu(cpu) {
4913 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Thomas Petazzoni83544912016-12-21 11:28:49 +01004914 kfree(txq_pcpu->buffs);
Marcin Wojtas71ce3912015-08-06 19:00:29 +02004915 }
4916
4917 dma_free_coherent(port->dev->dev.parent,
4918 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004919 txq->descs, txq->descs_dma);
Marcin Wojtas71ce3912015-08-06 19:00:29 +02004920
4921 return -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004922}
4923
4924/* Free allocated TXQ resources */
4925static void mvpp2_txq_deinit(struct mvpp2_port *port,
4926 struct mvpp2_tx_queue *txq)
4927{
4928 struct mvpp2_txq_pcpu *txq_pcpu;
4929 int cpu;
4930
4931 for_each_present_cpu(cpu) {
4932 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Thomas Petazzoni83544912016-12-21 11:28:49 +01004933 kfree(txq_pcpu->buffs);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004934 }
4935
4936 if (txq->descs)
4937 dma_free_coherent(port->dev->dev.parent,
4938 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004939 txq->descs, txq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004940
4941 txq->descs = NULL;
4942 txq->last_desc = 0;
4943 txq->next_desc_to_proc = 0;
Thomas Petazzoni20396132017-03-07 16:53:00 +01004944 txq->descs_dma = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004945
4946 /* Set minimum bandwidth for disabled TXQs */
4947 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(txq->id), 0);
4948
4949 /* Set Tx descriptors queue starting address and size */
4950 mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
4951 mvpp2_write(port->priv, MVPP2_TXQ_DESC_ADDR_REG, 0);
4952 mvpp2_write(port->priv, MVPP2_TXQ_DESC_SIZE_REG, 0);
4953}
4954
4955/* Cleanup Tx ports */
4956static void mvpp2_txq_clean(struct mvpp2_port *port, struct mvpp2_tx_queue *txq)
4957{
4958 struct mvpp2_txq_pcpu *txq_pcpu;
4959 int delay, pending, cpu;
4960 u32 val;
4961
4962 mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
4963 val = mvpp2_read(port->priv, MVPP2_TXQ_PREF_BUF_REG);
4964 val |= MVPP2_TXQ_DRAIN_EN_MASK;
4965 mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG, val);
4966
4967 /* The napi queue has been stopped so wait for all packets
4968 * to be transmitted.
4969 */
4970 delay = 0;
4971 do {
4972 if (delay >= MVPP2_TX_PENDING_TIMEOUT_MSEC) {
4973 netdev_warn(port->dev,
4974 "port %d: cleaning queue %d timed out\n",
4975 port->id, txq->log_id);
4976 break;
4977 }
4978 mdelay(1);
4979 delay++;
4980
Thomas Petazzoni4d6c2a62017-03-07 16:53:03 +01004981 pending = mvpp2_read(port->priv, MVPP2_TXQ_PENDING_REG) &
4982 MVPP2_TXQ_PENDING_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004983 } while (pending);
4984
4985 val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
4986 mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG, val);
4987
4988 for_each_present_cpu(cpu) {
4989 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
4990
4991 /* Release all packets */
4992 mvpp2_txq_bufs_free(port, txq, txq_pcpu, txq_pcpu->count);
4993
4994 /* Reset queue */
4995 txq_pcpu->count = 0;
4996 txq_pcpu->txq_put_index = 0;
4997 txq_pcpu->txq_get_index = 0;
4998 }
4999}
5000
5001/* Cleanup all Tx queues */
5002static void mvpp2_cleanup_txqs(struct mvpp2_port *port)
5003{
5004 struct mvpp2_tx_queue *txq;
5005 int queue;
5006 u32 val;
5007
5008 val = mvpp2_read(port->priv, MVPP2_TX_PORT_FLUSH_REG);
5009
5010 /* Reset Tx ports and delete Tx queues */
5011 val |= MVPP2_TX_PORT_FLUSH_MASK(port->id);
5012 mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
5013
5014 for (queue = 0; queue < txq_number; queue++) {
5015 txq = port->txqs[queue];
5016 mvpp2_txq_clean(port, txq);
5017 mvpp2_txq_deinit(port, txq);
5018 }
5019
5020 on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
5021
5022 val &= ~MVPP2_TX_PORT_FLUSH_MASK(port->id);
5023 mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
5024}
5025
5026/* Cleanup all Rx queues */
5027static void mvpp2_cleanup_rxqs(struct mvpp2_port *port)
5028{
5029 int queue;
5030
5031 for (queue = 0; queue < rxq_number; queue++)
5032 mvpp2_rxq_deinit(port, port->rxqs[queue]);
5033}
5034
5035/* Init all Rx queues for port */
5036static int mvpp2_setup_rxqs(struct mvpp2_port *port)
5037{
5038 int queue, err;
5039
5040 for (queue = 0; queue < rxq_number; queue++) {
5041 err = mvpp2_rxq_init(port, port->rxqs[queue]);
5042 if (err)
5043 goto err_cleanup;
5044 }
5045 return 0;
5046
5047err_cleanup:
5048 mvpp2_cleanup_rxqs(port);
5049 return err;
5050}
5051
5052/* Init all tx queues for port */
5053static int mvpp2_setup_txqs(struct mvpp2_port *port)
5054{
5055 struct mvpp2_tx_queue *txq;
5056 int queue, err;
5057
5058 for (queue = 0; queue < txq_number; queue++) {
5059 txq = port->txqs[queue];
5060 err = mvpp2_txq_init(port, txq);
5061 if (err)
5062 goto err_cleanup;
5063 }
5064
Marcin Wojtas3f518502014-07-10 16:52:13 -03005065 on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
5066 return 0;
5067
5068err_cleanup:
5069 mvpp2_cleanup_txqs(port);
5070 return err;
5071}
5072
5073/* The callback for per-port interrupt */
5074static irqreturn_t mvpp2_isr(int irq, void *dev_id)
5075{
5076 struct mvpp2_port *port = (struct mvpp2_port *)dev_id;
5077
5078 mvpp2_interrupts_disable(port);
5079
5080 napi_schedule(&port->napi);
5081
5082 return IRQ_HANDLED;
5083}
5084
5085/* Adjust link */
5086static void mvpp2_link_event(struct net_device *dev)
5087{
5088 struct mvpp2_port *port = netdev_priv(dev);
Philippe Reynes8e072692016-06-28 00:08:11 +02005089 struct phy_device *phydev = dev->phydev;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005090 int status_change = 0;
5091 u32 val;
5092
5093 if (phydev->link) {
5094 if ((port->speed != phydev->speed) ||
5095 (port->duplex != phydev->duplex)) {
5096 u32 val;
5097
5098 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5099 val &= ~(MVPP2_GMAC_CONFIG_MII_SPEED |
5100 MVPP2_GMAC_CONFIG_GMII_SPEED |
5101 MVPP2_GMAC_CONFIG_FULL_DUPLEX |
5102 MVPP2_GMAC_AN_SPEED_EN |
5103 MVPP2_GMAC_AN_DUPLEX_EN);
5104
5105 if (phydev->duplex)
5106 val |= MVPP2_GMAC_CONFIG_FULL_DUPLEX;
5107
5108 if (phydev->speed == SPEED_1000)
5109 val |= MVPP2_GMAC_CONFIG_GMII_SPEED;
Thomas Petazzoni2add5112014-07-27 23:21:35 +02005110 else if (phydev->speed == SPEED_100)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005111 val |= MVPP2_GMAC_CONFIG_MII_SPEED;
5112
5113 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5114
5115 port->duplex = phydev->duplex;
5116 port->speed = phydev->speed;
5117 }
5118 }
5119
5120 if (phydev->link != port->link) {
5121 if (!phydev->link) {
5122 port->duplex = -1;
5123 port->speed = 0;
5124 }
5125
5126 port->link = phydev->link;
5127 status_change = 1;
5128 }
5129
5130 if (status_change) {
5131 if (phydev->link) {
5132 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5133 val |= (MVPP2_GMAC_FORCE_LINK_PASS |
5134 MVPP2_GMAC_FORCE_LINK_DOWN);
5135 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5136 mvpp2_egress_enable(port);
5137 mvpp2_ingress_enable(port);
5138 } else {
5139 mvpp2_ingress_disable(port);
5140 mvpp2_egress_disable(port);
5141 }
5142 phy_print_status(phydev);
5143 }
5144}
5145
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005146static void mvpp2_timer_set(struct mvpp2_port_pcpu *port_pcpu)
5147{
5148 ktime_t interval;
5149
5150 if (!port_pcpu->timer_scheduled) {
5151 port_pcpu->timer_scheduled = true;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01005152 interval = MVPP2_TXDONE_HRTIMER_PERIOD_NS;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005153 hrtimer_start(&port_pcpu->tx_done_timer, interval,
5154 HRTIMER_MODE_REL_PINNED);
5155 }
5156}
5157
5158static void mvpp2_tx_proc_cb(unsigned long data)
5159{
5160 struct net_device *dev = (struct net_device *)data;
5161 struct mvpp2_port *port = netdev_priv(dev);
5162 struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
5163 unsigned int tx_todo, cause;
5164
5165 if (!netif_running(dev))
5166 return;
5167 port_pcpu->timer_scheduled = false;
5168
5169 /* Process all the Tx queues */
5170 cause = (1 << txq_number) - 1;
5171 tx_todo = mvpp2_tx_done(port, cause);
5172
5173 /* Set the timer in case not all the packets were processed */
5174 if (tx_todo)
5175 mvpp2_timer_set(port_pcpu);
5176}
5177
5178static enum hrtimer_restart mvpp2_hr_timer_cb(struct hrtimer *timer)
5179{
5180 struct mvpp2_port_pcpu *port_pcpu = container_of(timer,
5181 struct mvpp2_port_pcpu,
5182 tx_done_timer);
5183
5184 tasklet_schedule(&port_pcpu->tx_done_tasklet);
5185
5186 return HRTIMER_NORESTART;
5187}
5188
Marcin Wojtas3f518502014-07-10 16:52:13 -03005189/* Main RX/TX processing routines */
5190
5191/* Display more error info */
5192static void mvpp2_rx_error(struct mvpp2_port *port,
5193 struct mvpp2_rx_desc *rx_desc)
5194{
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005195 u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
5196 size_t sz = mvpp2_rxdesc_size_get(port, rx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005197
5198 switch (status & MVPP2_RXD_ERR_CODE_MASK) {
5199 case MVPP2_RXD_ERR_CRC:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005200 netdev_err(port->dev, "bad rx status %08x (crc error), size=%zu\n",
5201 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005202 break;
5203 case MVPP2_RXD_ERR_OVERRUN:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005204 netdev_err(port->dev, "bad rx status %08x (overrun error), size=%zu\n",
5205 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005206 break;
5207 case MVPP2_RXD_ERR_RESOURCE:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005208 netdev_err(port->dev, "bad rx status %08x (resource error), size=%zu\n",
5209 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005210 break;
5211 }
5212}
5213
5214/* Handle RX checksum offload */
5215static void mvpp2_rx_csum(struct mvpp2_port *port, u32 status,
5216 struct sk_buff *skb)
5217{
5218 if (((status & MVPP2_RXD_L3_IP4) &&
5219 !(status & MVPP2_RXD_IP4_HEADER_ERR)) ||
5220 (status & MVPP2_RXD_L3_IP6))
5221 if (((status & MVPP2_RXD_L4_UDP) ||
5222 (status & MVPP2_RXD_L4_TCP)) &&
5223 (status & MVPP2_RXD_L4_CSUM_OK)) {
5224 skb->csum = 0;
5225 skb->ip_summed = CHECKSUM_UNNECESSARY;
5226 return;
5227 }
5228
5229 skb->ip_summed = CHECKSUM_NONE;
5230}
5231
5232/* Reuse skb if possible, or allocate a new skb and add it to BM pool */
5233static int mvpp2_rx_refill(struct mvpp2_port *port,
Thomas Petazzoni7ef7e1d2017-02-21 11:28:07 +01005234 struct mvpp2_bm_pool *bm_pool, u32 bm)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005235{
Thomas Petazzoni20396132017-03-07 16:53:00 +01005236 dma_addr_t dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01005237 phys_addr_t phys_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005238 void *buf;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005239
Marcin Wojtas3f518502014-07-10 16:52:13 -03005240 /* No recycle or too many buffers are in use, so allocate a new skb */
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01005241 buf = mvpp2_buf_alloc(port, bm_pool, &dma_addr, &phys_addr,
5242 GFP_ATOMIC);
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005243 if (!buf)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005244 return -ENOMEM;
5245
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01005246 mvpp2_pool_refill(port, bm, dma_addr, phys_addr);
Thomas Petazzoni7ef7e1d2017-02-21 11:28:07 +01005247
Marcin Wojtas3f518502014-07-10 16:52:13 -03005248 return 0;
5249}
5250
5251/* Handle tx checksum */
5252static u32 mvpp2_skb_tx_csum(struct mvpp2_port *port, struct sk_buff *skb)
5253{
5254 if (skb->ip_summed == CHECKSUM_PARTIAL) {
5255 int ip_hdr_len = 0;
5256 u8 l4_proto;
5257
5258 if (skb->protocol == htons(ETH_P_IP)) {
5259 struct iphdr *ip4h = ip_hdr(skb);
5260
5261 /* Calculate IPv4 checksum and L4 checksum */
5262 ip_hdr_len = ip4h->ihl;
5263 l4_proto = ip4h->protocol;
5264 } else if (skb->protocol == htons(ETH_P_IPV6)) {
5265 struct ipv6hdr *ip6h = ipv6_hdr(skb);
5266
5267 /* Read l4_protocol from one of IPv6 extra headers */
5268 if (skb_network_header_len(skb) > 0)
5269 ip_hdr_len = (skb_network_header_len(skb) >> 2);
5270 l4_proto = ip6h->nexthdr;
5271 } else {
5272 return MVPP2_TXD_L4_CSUM_NOT;
5273 }
5274
5275 return mvpp2_txq_desc_csum(skb_network_offset(skb),
5276 skb->protocol, ip_hdr_len, l4_proto);
5277 }
5278
5279 return MVPP2_TXD_L4_CSUM_NOT | MVPP2_TXD_IP_CSUM_DISABLE;
5280}
5281
Marcin Wojtas3f518502014-07-10 16:52:13 -03005282/* Main rx processing */
5283static int mvpp2_rx(struct mvpp2_port *port, int rx_todo,
5284 struct mvpp2_rx_queue *rxq)
5285{
5286 struct net_device *dev = port->dev;
Marcin Wojtasb5015852015-12-03 15:20:51 +01005287 int rx_received;
5288 int rx_done = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005289 u32 rcvd_pkts = 0;
5290 u32 rcvd_bytes = 0;
5291
5292 /* Get number of received packets and clamp the to-do */
5293 rx_received = mvpp2_rxq_received(port, rxq->id);
5294 if (rx_todo > rx_received)
5295 rx_todo = rx_received;
5296
Marcin Wojtasb5015852015-12-03 15:20:51 +01005297 while (rx_done < rx_todo) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005298 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
5299 struct mvpp2_bm_pool *bm_pool;
5300 struct sk_buff *skb;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005301 unsigned int frag_size;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005302 dma_addr_t dma_addr;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005303 phys_addr_t phys_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005304 u32 bm, rx_status;
5305 int pool, rx_bytes, err;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005306 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005307
Marcin Wojtasb5015852015-12-03 15:20:51 +01005308 rx_done++;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005309 rx_status = mvpp2_rxdesc_status_get(port, rx_desc);
5310 rx_bytes = mvpp2_rxdesc_size_get(port, rx_desc);
5311 rx_bytes -= MVPP2_MH_SIZE;
5312 dma_addr = mvpp2_rxdesc_dma_addr_get(port, rx_desc);
5313 phys_addr = mvpp2_rxdesc_cookie_get(port, rx_desc);
5314 data = (void *)phys_to_virt(phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005315
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005316 bm = mvpp2_bm_cookie_build(port, rx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005317 pool = mvpp2_bm_cookie_pool_get(bm);
5318 bm_pool = &port->priv->bm_pools[pool];
Marcin Wojtas3f518502014-07-10 16:52:13 -03005319
5320 /* In case of an error, release the requested buffer pointer
5321 * to the Buffer Manager. This request process is controlled
5322 * by the hardware, and the information about the buffer is
5323 * comprised by the RX descriptor.
5324 */
5325 if (rx_status & MVPP2_RXD_ERR_SUMMARY) {
Marcin Wojtasb5015852015-12-03 15:20:51 +01005326 err_drop_frame:
Marcin Wojtas3f518502014-07-10 16:52:13 -03005327 dev->stats.rx_errors++;
5328 mvpp2_rx_error(port, rx_desc);
Marcin Wojtasb5015852015-12-03 15:20:51 +01005329 /* Return the buffer to the pool */
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005330 mvpp2_pool_refill(port, bm, dma_addr, phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005331 continue;
5332 }
5333
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005334 if (bm_pool->frag_size > PAGE_SIZE)
5335 frag_size = 0;
5336 else
5337 frag_size = bm_pool->frag_size;
5338
5339 skb = build_skb(data, frag_size);
5340 if (!skb) {
5341 netdev_warn(port->dev, "skb build failed\n");
5342 goto err_drop_frame;
5343 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03005344
Thomas Petazzoni7ef7e1d2017-02-21 11:28:07 +01005345 err = mvpp2_rx_refill(port, bm_pool, bm);
Marcin Wojtasb5015852015-12-03 15:20:51 +01005346 if (err) {
5347 netdev_err(port->dev, "failed to refill BM pools\n");
5348 goto err_drop_frame;
5349 }
5350
Thomas Petazzoni20396132017-03-07 16:53:00 +01005351 dma_unmap_single(dev->dev.parent, dma_addr,
Marcin Wojtas4229d502015-12-03 15:20:50 +01005352 bm_pool->buf_size, DMA_FROM_DEVICE);
5353
Marcin Wojtas3f518502014-07-10 16:52:13 -03005354 rcvd_pkts++;
5355 rcvd_bytes += rx_bytes;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005356
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005357 skb_reserve(skb, MVPP2_MH_SIZE + NET_SKB_PAD);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005358 skb_put(skb, rx_bytes);
5359 skb->protocol = eth_type_trans(skb, dev);
5360 mvpp2_rx_csum(port, rx_status, skb);
5361
5362 napi_gro_receive(&port->napi, skb);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005363 }
5364
5365 if (rcvd_pkts) {
5366 struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
5367
5368 u64_stats_update_begin(&stats->syncp);
5369 stats->rx_packets += rcvd_pkts;
5370 stats->rx_bytes += rcvd_bytes;
5371 u64_stats_update_end(&stats->syncp);
5372 }
5373
5374 /* Update Rx queue management counters */
5375 wmb();
Marcin Wojtasb5015852015-12-03 15:20:51 +01005376 mvpp2_rxq_status_update(port, rxq->id, rx_done, rx_done);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005377
5378 return rx_todo;
5379}
5380
5381static inline void
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005382tx_desc_unmap_put(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
Marcin Wojtas3f518502014-07-10 16:52:13 -03005383 struct mvpp2_tx_desc *desc)
5384{
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005385 dma_addr_t buf_dma_addr =
5386 mvpp2_txdesc_dma_addr_get(port, desc);
5387 size_t buf_sz =
5388 mvpp2_txdesc_size_get(port, desc);
5389 dma_unmap_single(port->dev->dev.parent, buf_dma_addr,
5390 buf_sz, DMA_TO_DEVICE);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005391 mvpp2_txq_desc_put(txq);
5392}
5393
5394/* Handle tx fragmentation processing */
5395static int mvpp2_tx_frag_process(struct mvpp2_port *port, struct sk_buff *skb,
5396 struct mvpp2_tx_queue *aggr_txq,
5397 struct mvpp2_tx_queue *txq)
5398{
5399 struct mvpp2_txq_pcpu *txq_pcpu = this_cpu_ptr(txq->pcpu);
5400 struct mvpp2_tx_desc *tx_desc;
5401 int i;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005402 dma_addr_t buf_dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005403
5404 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
5405 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5406 void *addr = page_address(frag->page.p) + frag->page_offset;
5407
5408 tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005409 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
5410 mvpp2_txdesc_size_set(port, tx_desc, frag->size);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005411
Thomas Petazzoni20396132017-03-07 16:53:00 +01005412 buf_dma_addr = dma_map_single(port->dev->dev.parent, addr,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005413 frag->size,
5414 DMA_TO_DEVICE);
Thomas Petazzoni20396132017-03-07 16:53:00 +01005415 if (dma_mapping_error(port->dev->dev.parent, buf_dma_addr)) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005416 mvpp2_txq_desc_put(txq);
5417 goto error;
5418 }
5419
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005420 mvpp2_txdesc_offset_set(port, tx_desc,
5421 buf_dma_addr & MVPP2_TX_DESC_ALIGN);
5422 mvpp2_txdesc_dma_addr_set(port, tx_desc,
5423 buf_dma_addr & ~MVPP2_TX_DESC_ALIGN);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005424
5425 if (i == (skb_shinfo(skb)->nr_frags - 1)) {
5426 /* Last descriptor */
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005427 mvpp2_txdesc_cmd_set(port, tx_desc,
5428 MVPP2_TXD_L_DESC);
5429 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005430 } else {
5431 /* Descriptor in the middle: Not First, Not Last */
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005432 mvpp2_txdesc_cmd_set(port, tx_desc, 0);
5433 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005434 }
5435 }
5436
5437 return 0;
5438
5439error:
5440 /* Release all descriptors that were used to map fragments of
5441 * this packet, as well as the corresponding DMA mappings
5442 */
5443 for (i = i - 1; i >= 0; i--) {
5444 tx_desc = txq->descs + i;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005445 tx_desc_unmap_put(port, txq, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005446 }
5447
5448 return -ENOMEM;
5449}
5450
5451/* Main tx processing */
5452static int mvpp2_tx(struct sk_buff *skb, struct net_device *dev)
5453{
5454 struct mvpp2_port *port = netdev_priv(dev);
5455 struct mvpp2_tx_queue *txq, *aggr_txq;
5456 struct mvpp2_txq_pcpu *txq_pcpu;
5457 struct mvpp2_tx_desc *tx_desc;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005458 dma_addr_t buf_dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005459 int frags = 0;
5460 u16 txq_id;
5461 u32 tx_cmd;
5462
5463 txq_id = skb_get_queue_mapping(skb);
5464 txq = port->txqs[txq_id];
5465 txq_pcpu = this_cpu_ptr(txq->pcpu);
5466 aggr_txq = &port->priv->aggr_txqs[smp_processor_id()];
5467
5468 frags = skb_shinfo(skb)->nr_frags + 1;
5469
5470 /* Check number of available descriptors */
5471 if (mvpp2_aggr_desc_num_check(port->priv, aggr_txq, frags) ||
5472 mvpp2_txq_reserved_desc_num_proc(port->priv, txq,
5473 txq_pcpu, frags)) {
5474 frags = 0;
5475 goto out;
5476 }
5477
5478 /* Get a descriptor for the first part of the packet */
5479 tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005480 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
5481 mvpp2_txdesc_size_set(port, tx_desc, skb_headlen(skb));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005482
Thomas Petazzoni20396132017-03-07 16:53:00 +01005483 buf_dma_addr = dma_map_single(dev->dev.parent, skb->data,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005484 skb_headlen(skb), DMA_TO_DEVICE);
Thomas Petazzoni20396132017-03-07 16:53:00 +01005485 if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005486 mvpp2_txq_desc_put(txq);
5487 frags = 0;
5488 goto out;
5489 }
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005490
5491 mvpp2_txdesc_offset_set(port, tx_desc,
5492 buf_dma_addr & MVPP2_TX_DESC_ALIGN);
5493 mvpp2_txdesc_dma_addr_set(port, tx_desc,
5494 buf_dma_addr & ~MVPP2_TX_DESC_ALIGN);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005495
5496 tx_cmd = mvpp2_skb_tx_csum(port, skb);
5497
5498 if (frags == 1) {
5499 /* First and Last descriptor */
5500 tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_L_DESC;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005501 mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
5502 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005503 } else {
5504 /* First but not Last */
5505 tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_PADDING_DISABLE;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005506 mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
5507 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005508
5509 /* Continue with other skb fragments */
5510 if (mvpp2_tx_frag_process(port, skb, aggr_txq, txq)) {
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005511 tx_desc_unmap_put(port, txq, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005512 frags = 0;
5513 goto out;
5514 }
5515 }
5516
5517 txq_pcpu->reserved_num -= frags;
5518 txq_pcpu->count += frags;
5519 aggr_txq->count += frags;
5520
5521 /* Enable transmit */
5522 wmb();
5523 mvpp2_aggr_txq_pend_desc_add(port, frags);
5524
5525 if (txq_pcpu->size - txq_pcpu->count < MAX_SKB_FRAGS + 1) {
5526 struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
5527
5528 netif_tx_stop_queue(nq);
5529 }
5530out:
5531 if (frags > 0) {
5532 struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
5533
5534 u64_stats_update_begin(&stats->syncp);
5535 stats->tx_packets++;
5536 stats->tx_bytes += skb->len;
5537 u64_stats_update_end(&stats->syncp);
5538 } else {
5539 dev->stats.tx_dropped++;
5540 dev_kfree_skb_any(skb);
5541 }
5542
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005543 /* Finalize TX processing */
5544 if (txq_pcpu->count >= txq->done_pkts_coal)
5545 mvpp2_txq_done(port, txq, txq_pcpu);
5546
5547 /* Set the timer in case not all frags were processed */
5548 if (txq_pcpu->count <= frags && txq_pcpu->count > 0) {
5549 struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
5550
5551 mvpp2_timer_set(port_pcpu);
5552 }
5553
Marcin Wojtas3f518502014-07-10 16:52:13 -03005554 return NETDEV_TX_OK;
5555}
5556
5557static inline void mvpp2_cause_error(struct net_device *dev, int cause)
5558{
5559 if (cause & MVPP2_CAUSE_FCS_ERR_MASK)
5560 netdev_err(dev, "FCS error\n");
5561 if (cause & MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK)
5562 netdev_err(dev, "rx fifo overrun error\n");
5563 if (cause & MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK)
5564 netdev_err(dev, "tx fifo underrun error\n");
5565}
5566
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005567static int mvpp2_poll(struct napi_struct *napi, int budget)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005568{
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005569 u32 cause_rx_tx, cause_rx, cause_misc;
5570 int rx_done = 0;
5571 struct mvpp2_port *port = netdev_priv(napi->dev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005572
5573 /* Rx/Tx cause register
5574 *
5575 * Bits 0-15: each bit indicates received packets on the Rx queue
5576 * (bit 0 is for Rx queue 0).
5577 *
5578 * Bits 16-23: each bit indicates transmitted packets on the Tx queue
5579 * (bit 16 is for Tx queue 0).
5580 *
5581 * Each CPU has its own Rx/Tx cause register
5582 */
5583 cause_rx_tx = mvpp2_read(port->priv,
5584 MVPP2_ISR_RX_TX_CAUSE_REG(port->id));
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005585 cause_rx_tx &= ~MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005586 cause_misc = cause_rx_tx & MVPP2_CAUSE_MISC_SUM_MASK;
5587
5588 if (cause_misc) {
5589 mvpp2_cause_error(port->dev, cause_misc);
5590
5591 /* Clear the cause register */
5592 mvpp2_write(port->priv, MVPP2_ISR_MISC_CAUSE_REG, 0);
5593 mvpp2_write(port->priv, MVPP2_ISR_RX_TX_CAUSE_REG(port->id),
5594 cause_rx_tx & ~MVPP2_CAUSE_MISC_SUM_MASK);
5595 }
5596
Marcin Wojtas3f518502014-07-10 16:52:13 -03005597 cause_rx = cause_rx_tx & MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
5598
5599 /* Process RX packets */
5600 cause_rx |= port->pending_cause_rx;
5601 while (cause_rx && budget > 0) {
5602 int count;
5603 struct mvpp2_rx_queue *rxq;
5604
5605 rxq = mvpp2_get_rx_queue(port, cause_rx);
5606 if (!rxq)
5607 break;
5608
5609 count = mvpp2_rx(port, budget, rxq);
5610 rx_done += count;
5611 budget -= count;
5612 if (budget > 0) {
5613 /* Clear the bit associated to this Rx queue
5614 * so that next iteration will continue from
5615 * the next Rx queue.
5616 */
5617 cause_rx &= ~(1 << rxq->logic_rxq);
5618 }
5619 }
5620
5621 if (budget > 0) {
5622 cause_rx = 0;
Eric Dumazet6ad20162017-01-30 08:22:01 -08005623 napi_complete_done(napi, rx_done);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005624
5625 mvpp2_interrupts_enable(port);
5626 }
5627 port->pending_cause_rx = cause_rx;
5628 return rx_done;
5629}
5630
5631/* Set hw internals when starting port */
5632static void mvpp2_start_dev(struct mvpp2_port *port)
5633{
Philippe Reynes8e072692016-06-28 00:08:11 +02005634 struct net_device *ndev = port->dev;
5635
Marcin Wojtas3f518502014-07-10 16:52:13 -03005636 mvpp2_gmac_max_rx_size_set(port);
5637 mvpp2_txp_max_tx_size_set(port);
5638
5639 napi_enable(&port->napi);
5640
5641 /* Enable interrupts on all CPUs */
5642 mvpp2_interrupts_enable(port);
5643
5644 mvpp2_port_enable(port);
Philippe Reynes8e072692016-06-28 00:08:11 +02005645 phy_start(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005646 netif_tx_start_all_queues(port->dev);
5647}
5648
5649/* Set hw internals when stopping port */
5650static void mvpp2_stop_dev(struct mvpp2_port *port)
5651{
Philippe Reynes8e072692016-06-28 00:08:11 +02005652 struct net_device *ndev = port->dev;
5653
Marcin Wojtas3f518502014-07-10 16:52:13 -03005654 /* Stop new packets from arriving to RXQs */
5655 mvpp2_ingress_disable(port);
5656
5657 mdelay(10);
5658
5659 /* Disable interrupts on all CPUs */
5660 mvpp2_interrupts_disable(port);
5661
5662 napi_disable(&port->napi);
5663
5664 netif_carrier_off(port->dev);
5665 netif_tx_stop_all_queues(port->dev);
5666
5667 mvpp2_egress_disable(port);
5668 mvpp2_port_disable(port);
Philippe Reynes8e072692016-06-28 00:08:11 +02005669 phy_stop(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005670}
5671
Marcin Wojtas3f518502014-07-10 16:52:13 -03005672static int mvpp2_check_ringparam_valid(struct net_device *dev,
5673 struct ethtool_ringparam *ring)
5674{
5675 u16 new_rx_pending = ring->rx_pending;
5676 u16 new_tx_pending = ring->tx_pending;
5677
5678 if (ring->rx_pending == 0 || ring->tx_pending == 0)
5679 return -EINVAL;
5680
5681 if (ring->rx_pending > MVPP2_MAX_RXD)
5682 new_rx_pending = MVPP2_MAX_RXD;
5683 else if (!IS_ALIGNED(ring->rx_pending, 16))
5684 new_rx_pending = ALIGN(ring->rx_pending, 16);
5685
5686 if (ring->tx_pending > MVPP2_MAX_TXD)
5687 new_tx_pending = MVPP2_MAX_TXD;
5688 else if (!IS_ALIGNED(ring->tx_pending, 32))
5689 new_tx_pending = ALIGN(ring->tx_pending, 32);
5690
5691 if (ring->rx_pending != new_rx_pending) {
5692 netdev_info(dev, "illegal Rx ring size value %d, round to %d\n",
5693 ring->rx_pending, new_rx_pending);
5694 ring->rx_pending = new_rx_pending;
5695 }
5696
5697 if (ring->tx_pending != new_tx_pending) {
5698 netdev_info(dev, "illegal Tx ring size value %d, round to %d\n",
5699 ring->tx_pending, new_tx_pending);
5700 ring->tx_pending = new_tx_pending;
5701 }
5702
5703 return 0;
5704}
5705
5706static void mvpp2_get_mac_address(struct mvpp2_port *port, unsigned char *addr)
5707{
5708 u32 mac_addr_l, mac_addr_m, mac_addr_h;
5709
5710 mac_addr_l = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
5711 mac_addr_m = readl(port->priv->lms_base + MVPP2_SRC_ADDR_MIDDLE);
5712 mac_addr_h = readl(port->priv->lms_base + MVPP2_SRC_ADDR_HIGH);
5713 addr[0] = (mac_addr_h >> 24) & 0xFF;
5714 addr[1] = (mac_addr_h >> 16) & 0xFF;
5715 addr[2] = (mac_addr_h >> 8) & 0xFF;
5716 addr[3] = mac_addr_h & 0xFF;
5717 addr[4] = mac_addr_m & 0xFF;
5718 addr[5] = (mac_addr_l >> MVPP2_GMAC_SA_LOW_OFFS) & 0xFF;
5719}
5720
5721static int mvpp2_phy_connect(struct mvpp2_port *port)
5722{
5723 struct phy_device *phy_dev;
5724
5725 phy_dev = of_phy_connect(port->dev, port->phy_node, mvpp2_link_event, 0,
5726 port->phy_interface);
5727 if (!phy_dev) {
5728 netdev_err(port->dev, "cannot connect to phy\n");
5729 return -ENODEV;
5730 }
5731 phy_dev->supported &= PHY_GBIT_FEATURES;
5732 phy_dev->advertising = phy_dev->supported;
5733
Marcin Wojtas3f518502014-07-10 16:52:13 -03005734 port->link = 0;
5735 port->duplex = 0;
5736 port->speed = 0;
5737
5738 return 0;
5739}
5740
5741static void mvpp2_phy_disconnect(struct mvpp2_port *port)
5742{
Philippe Reynes8e072692016-06-28 00:08:11 +02005743 struct net_device *ndev = port->dev;
5744
5745 phy_disconnect(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005746}
5747
5748static int mvpp2_open(struct net_device *dev)
5749{
5750 struct mvpp2_port *port = netdev_priv(dev);
5751 unsigned char mac_bcast[ETH_ALEN] = {
5752 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
5753 int err;
5754
5755 err = mvpp2_prs_mac_da_accept(port->priv, port->id, mac_bcast, true);
5756 if (err) {
5757 netdev_err(dev, "mvpp2_prs_mac_da_accept BC failed\n");
5758 return err;
5759 }
5760 err = mvpp2_prs_mac_da_accept(port->priv, port->id,
5761 dev->dev_addr, true);
5762 if (err) {
5763 netdev_err(dev, "mvpp2_prs_mac_da_accept MC failed\n");
5764 return err;
5765 }
5766 err = mvpp2_prs_tag_mode_set(port->priv, port->id, MVPP2_TAG_TYPE_MH);
5767 if (err) {
5768 netdev_err(dev, "mvpp2_prs_tag_mode_set failed\n");
5769 return err;
5770 }
5771 err = mvpp2_prs_def_flow(port);
5772 if (err) {
5773 netdev_err(dev, "mvpp2_prs_def_flow failed\n");
5774 return err;
5775 }
5776
5777 /* Allocate the Rx/Tx queues */
5778 err = mvpp2_setup_rxqs(port);
5779 if (err) {
5780 netdev_err(port->dev, "cannot allocate Rx queues\n");
5781 return err;
5782 }
5783
5784 err = mvpp2_setup_txqs(port);
5785 if (err) {
5786 netdev_err(port->dev, "cannot allocate Tx queues\n");
5787 goto err_cleanup_rxqs;
5788 }
5789
5790 err = request_irq(port->irq, mvpp2_isr, 0, dev->name, port);
5791 if (err) {
5792 netdev_err(port->dev, "cannot request IRQ %d\n", port->irq);
5793 goto err_cleanup_txqs;
5794 }
5795
5796 /* In default link is down */
5797 netif_carrier_off(port->dev);
5798
5799 err = mvpp2_phy_connect(port);
5800 if (err < 0)
5801 goto err_free_irq;
5802
5803 /* Unmask interrupts on all CPUs */
5804 on_each_cpu(mvpp2_interrupts_unmask, port, 1);
5805
5806 mvpp2_start_dev(port);
5807
5808 return 0;
5809
5810err_free_irq:
5811 free_irq(port->irq, port);
5812err_cleanup_txqs:
5813 mvpp2_cleanup_txqs(port);
5814err_cleanup_rxqs:
5815 mvpp2_cleanup_rxqs(port);
5816 return err;
5817}
5818
5819static int mvpp2_stop(struct net_device *dev)
5820{
5821 struct mvpp2_port *port = netdev_priv(dev);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005822 struct mvpp2_port_pcpu *port_pcpu;
5823 int cpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005824
5825 mvpp2_stop_dev(port);
5826 mvpp2_phy_disconnect(port);
5827
5828 /* Mask interrupts on all CPUs */
5829 on_each_cpu(mvpp2_interrupts_mask, port, 1);
5830
5831 free_irq(port->irq, port);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005832 for_each_present_cpu(cpu) {
5833 port_pcpu = per_cpu_ptr(port->pcpu, cpu);
5834
5835 hrtimer_cancel(&port_pcpu->tx_done_timer);
5836 port_pcpu->timer_scheduled = false;
5837 tasklet_kill(&port_pcpu->tx_done_tasklet);
5838 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03005839 mvpp2_cleanup_rxqs(port);
5840 mvpp2_cleanup_txqs(port);
5841
5842 return 0;
5843}
5844
5845static void mvpp2_set_rx_mode(struct net_device *dev)
5846{
5847 struct mvpp2_port *port = netdev_priv(dev);
5848 struct mvpp2 *priv = port->priv;
5849 struct netdev_hw_addr *ha;
5850 int id = port->id;
5851 bool allmulti = dev->flags & IFF_ALLMULTI;
5852
5853 mvpp2_prs_mac_promisc_set(priv, id, dev->flags & IFF_PROMISC);
5854 mvpp2_prs_mac_multi_set(priv, id, MVPP2_PE_MAC_MC_ALL, allmulti);
5855 mvpp2_prs_mac_multi_set(priv, id, MVPP2_PE_MAC_MC_IP6, allmulti);
5856
5857 /* Remove all port->id's mcast enries */
5858 mvpp2_prs_mcast_del_all(priv, id);
5859
5860 if (allmulti && !netdev_mc_empty(dev)) {
5861 netdev_for_each_mc_addr(ha, dev)
5862 mvpp2_prs_mac_da_accept(priv, id, ha->addr, true);
5863 }
5864}
5865
5866static int mvpp2_set_mac_address(struct net_device *dev, void *p)
5867{
5868 struct mvpp2_port *port = netdev_priv(dev);
5869 const struct sockaddr *addr = p;
5870 int err;
5871
5872 if (!is_valid_ether_addr(addr->sa_data)) {
5873 err = -EADDRNOTAVAIL;
5874 goto error;
5875 }
5876
5877 if (!netif_running(dev)) {
5878 err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
5879 if (!err)
5880 return 0;
5881 /* Reconfigure parser to accept the original MAC address */
5882 err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
5883 if (err)
5884 goto error;
5885 }
5886
5887 mvpp2_stop_dev(port);
5888
5889 err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
5890 if (!err)
5891 goto out_start;
5892
5893 /* Reconfigure parser accept the original MAC address */
5894 err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
5895 if (err)
5896 goto error;
5897out_start:
5898 mvpp2_start_dev(port);
5899 mvpp2_egress_enable(port);
5900 mvpp2_ingress_enable(port);
5901 return 0;
5902
5903error:
5904 netdev_err(dev, "fail to change MAC address\n");
5905 return err;
5906}
5907
5908static int mvpp2_change_mtu(struct net_device *dev, int mtu)
5909{
5910 struct mvpp2_port *port = netdev_priv(dev);
5911 int err;
5912
Jarod Wilson57779872016-10-17 15:54:06 -04005913 if (!IS_ALIGNED(MVPP2_RX_PKT_SIZE(mtu), 8)) {
5914 netdev_info(dev, "illegal MTU value %d, round to %d\n", mtu,
5915 ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8));
5916 mtu = ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005917 }
5918
5919 if (!netif_running(dev)) {
5920 err = mvpp2_bm_update_mtu(dev, mtu);
5921 if (!err) {
5922 port->pkt_size = MVPP2_RX_PKT_SIZE(mtu);
5923 return 0;
5924 }
5925
5926 /* Reconfigure BM to the original MTU */
5927 err = mvpp2_bm_update_mtu(dev, dev->mtu);
5928 if (err)
5929 goto error;
5930 }
5931
5932 mvpp2_stop_dev(port);
5933
5934 err = mvpp2_bm_update_mtu(dev, mtu);
5935 if (!err) {
5936 port->pkt_size = MVPP2_RX_PKT_SIZE(mtu);
5937 goto out_start;
5938 }
5939
5940 /* Reconfigure BM to the original MTU */
5941 err = mvpp2_bm_update_mtu(dev, dev->mtu);
5942 if (err)
5943 goto error;
5944
5945out_start:
5946 mvpp2_start_dev(port);
5947 mvpp2_egress_enable(port);
5948 mvpp2_ingress_enable(port);
5949
5950 return 0;
5951
5952error:
5953 netdev_err(dev, "fail to change MTU\n");
5954 return err;
5955}
5956
stephen hemmingerbc1f4472017-01-06 19:12:52 -08005957static void
Marcin Wojtas3f518502014-07-10 16:52:13 -03005958mvpp2_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
5959{
5960 struct mvpp2_port *port = netdev_priv(dev);
5961 unsigned int start;
5962 int cpu;
5963
5964 for_each_possible_cpu(cpu) {
5965 struct mvpp2_pcpu_stats *cpu_stats;
5966 u64 rx_packets;
5967 u64 rx_bytes;
5968 u64 tx_packets;
5969 u64 tx_bytes;
5970
5971 cpu_stats = per_cpu_ptr(port->stats, cpu);
5972 do {
5973 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
5974 rx_packets = cpu_stats->rx_packets;
5975 rx_bytes = cpu_stats->rx_bytes;
5976 tx_packets = cpu_stats->tx_packets;
5977 tx_bytes = cpu_stats->tx_bytes;
5978 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
5979
5980 stats->rx_packets += rx_packets;
5981 stats->rx_bytes += rx_bytes;
5982 stats->tx_packets += tx_packets;
5983 stats->tx_bytes += tx_bytes;
5984 }
5985
5986 stats->rx_errors = dev->stats.rx_errors;
5987 stats->rx_dropped = dev->stats.rx_dropped;
5988 stats->tx_dropped = dev->stats.tx_dropped;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005989}
5990
Thomas Petazzonibd695a52014-07-27 23:21:36 +02005991static int mvpp2_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
5992{
Thomas Petazzonibd695a52014-07-27 23:21:36 +02005993 int ret;
5994
Philippe Reynes8e072692016-06-28 00:08:11 +02005995 if (!dev->phydev)
Thomas Petazzonibd695a52014-07-27 23:21:36 +02005996 return -ENOTSUPP;
5997
Philippe Reynes8e072692016-06-28 00:08:11 +02005998 ret = phy_mii_ioctl(dev->phydev, ifr, cmd);
Thomas Petazzonibd695a52014-07-27 23:21:36 +02005999 if (!ret)
6000 mvpp2_link_event(dev);
6001
6002 return ret;
6003}
6004
Marcin Wojtas3f518502014-07-10 16:52:13 -03006005/* Ethtool methods */
6006
Marcin Wojtas3f518502014-07-10 16:52:13 -03006007/* Set interrupt coalescing for ethtools */
6008static int mvpp2_ethtool_set_coalesce(struct net_device *dev,
6009 struct ethtool_coalesce *c)
6010{
6011 struct mvpp2_port *port = netdev_priv(dev);
6012 int queue;
6013
6014 for (queue = 0; queue < rxq_number; queue++) {
6015 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
6016
6017 rxq->time_coal = c->rx_coalesce_usecs;
6018 rxq->pkts_coal = c->rx_max_coalesced_frames;
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01006019 mvpp2_rx_pkts_coal_set(port, rxq);
6020 mvpp2_rx_time_coal_set(port, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006021 }
6022
6023 for (queue = 0; queue < txq_number; queue++) {
6024 struct mvpp2_tx_queue *txq = port->txqs[queue];
6025
6026 txq->done_pkts_coal = c->tx_max_coalesced_frames;
6027 }
6028
Marcin Wojtas3f518502014-07-10 16:52:13 -03006029 return 0;
6030}
6031
6032/* get coalescing for ethtools */
6033static int mvpp2_ethtool_get_coalesce(struct net_device *dev,
6034 struct ethtool_coalesce *c)
6035{
6036 struct mvpp2_port *port = netdev_priv(dev);
6037
6038 c->rx_coalesce_usecs = port->rxqs[0]->time_coal;
6039 c->rx_max_coalesced_frames = port->rxqs[0]->pkts_coal;
6040 c->tx_max_coalesced_frames = port->txqs[0]->done_pkts_coal;
6041 return 0;
6042}
6043
6044static void mvpp2_ethtool_get_drvinfo(struct net_device *dev,
6045 struct ethtool_drvinfo *drvinfo)
6046{
6047 strlcpy(drvinfo->driver, MVPP2_DRIVER_NAME,
6048 sizeof(drvinfo->driver));
6049 strlcpy(drvinfo->version, MVPP2_DRIVER_VERSION,
6050 sizeof(drvinfo->version));
6051 strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
6052 sizeof(drvinfo->bus_info));
6053}
6054
6055static void mvpp2_ethtool_get_ringparam(struct net_device *dev,
6056 struct ethtool_ringparam *ring)
6057{
6058 struct mvpp2_port *port = netdev_priv(dev);
6059
6060 ring->rx_max_pending = MVPP2_MAX_RXD;
6061 ring->tx_max_pending = MVPP2_MAX_TXD;
6062 ring->rx_pending = port->rx_ring_size;
6063 ring->tx_pending = port->tx_ring_size;
6064}
6065
6066static int mvpp2_ethtool_set_ringparam(struct net_device *dev,
6067 struct ethtool_ringparam *ring)
6068{
6069 struct mvpp2_port *port = netdev_priv(dev);
6070 u16 prev_rx_ring_size = port->rx_ring_size;
6071 u16 prev_tx_ring_size = port->tx_ring_size;
6072 int err;
6073
6074 err = mvpp2_check_ringparam_valid(dev, ring);
6075 if (err)
6076 return err;
6077
6078 if (!netif_running(dev)) {
6079 port->rx_ring_size = ring->rx_pending;
6080 port->tx_ring_size = ring->tx_pending;
6081 return 0;
6082 }
6083
6084 /* The interface is running, so we have to force a
6085 * reallocation of the queues
6086 */
6087 mvpp2_stop_dev(port);
6088 mvpp2_cleanup_rxqs(port);
6089 mvpp2_cleanup_txqs(port);
6090
6091 port->rx_ring_size = ring->rx_pending;
6092 port->tx_ring_size = ring->tx_pending;
6093
6094 err = mvpp2_setup_rxqs(port);
6095 if (err) {
6096 /* Reallocate Rx queues with the original ring size */
6097 port->rx_ring_size = prev_rx_ring_size;
6098 ring->rx_pending = prev_rx_ring_size;
6099 err = mvpp2_setup_rxqs(port);
6100 if (err)
6101 goto err_out;
6102 }
6103 err = mvpp2_setup_txqs(port);
6104 if (err) {
6105 /* Reallocate Tx queues with the original ring size */
6106 port->tx_ring_size = prev_tx_ring_size;
6107 ring->tx_pending = prev_tx_ring_size;
6108 err = mvpp2_setup_txqs(port);
6109 if (err)
6110 goto err_clean_rxqs;
6111 }
6112
6113 mvpp2_start_dev(port);
6114 mvpp2_egress_enable(port);
6115 mvpp2_ingress_enable(port);
6116
6117 return 0;
6118
6119err_clean_rxqs:
6120 mvpp2_cleanup_rxqs(port);
6121err_out:
6122 netdev_err(dev, "fail to change ring parameters");
6123 return err;
6124}
6125
6126/* Device ops */
6127
6128static const struct net_device_ops mvpp2_netdev_ops = {
6129 .ndo_open = mvpp2_open,
6130 .ndo_stop = mvpp2_stop,
6131 .ndo_start_xmit = mvpp2_tx,
6132 .ndo_set_rx_mode = mvpp2_set_rx_mode,
6133 .ndo_set_mac_address = mvpp2_set_mac_address,
6134 .ndo_change_mtu = mvpp2_change_mtu,
6135 .ndo_get_stats64 = mvpp2_get_stats64,
Thomas Petazzonibd695a52014-07-27 23:21:36 +02006136 .ndo_do_ioctl = mvpp2_ioctl,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006137};
6138
6139static const struct ethtool_ops mvpp2_eth_tool_ops = {
Florian Fainelli00606c42016-11-15 11:19:48 -08006140 .nway_reset = phy_ethtool_nway_reset,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006141 .get_link = ethtool_op_get_link,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006142 .set_coalesce = mvpp2_ethtool_set_coalesce,
6143 .get_coalesce = mvpp2_ethtool_get_coalesce,
6144 .get_drvinfo = mvpp2_ethtool_get_drvinfo,
6145 .get_ringparam = mvpp2_ethtool_get_ringparam,
6146 .set_ringparam = mvpp2_ethtool_set_ringparam,
Philippe Reynesfb773e92016-06-28 00:08:12 +02006147 .get_link_ksettings = phy_ethtool_get_link_ksettings,
6148 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006149};
6150
6151/* Driver initialization */
6152
6153static void mvpp2_port_power_up(struct mvpp2_port *port)
6154{
6155 mvpp2_port_mii_set(port);
6156 mvpp2_port_periodic_xon_disable(port);
Marcin Wojtas08a23752014-07-21 13:48:12 -03006157 mvpp2_port_fc_adv_enable(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006158 mvpp2_port_reset(port);
6159}
6160
6161/* Initialize port HW */
6162static int mvpp2_port_init(struct mvpp2_port *port)
6163{
6164 struct device *dev = port->dev->dev.parent;
6165 struct mvpp2 *priv = port->priv;
6166 struct mvpp2_txq_pcpu *txq_pcpu;
6167 int queue, cpu, err;
6168
6169 if (port->first_rxq + rxq_number > MVPP2_RXQ_TOTAL_NUM)
6170 return -EINVAL;
6171
6172 /* Disable port */
6173 mvpp2_egress_disable(port);
6174 mvpp2_port_disable(port);
6175
6176 port->txqs = devm_kcalloc(dev, txq_number, sizeof(*port->txqs),
6177 GFP_KERNEL);
6178 if (!port->txqs)
6179 return -ENOMEM;
6180
6181 /* Associate physical Tx queues to this port and initialize.
6182 * The mapping is predefined.
6183 */
6184 for (queue = 0; queue < txq_number; queue++) {
6185 int queue_phy_id = mvpp2_txq_phys(port->id, queue);
6186 struct mvpp2_tx_queue *txq;
6187
6188 txq = devm_kzalloc(dev, sizeof(*txq), GFP_KERNEL);
Christophe Jaillet177c8d12017-02-19 10:19:57 +01006189 if (!txq) {
6190 err = -ENOMEM;
6191 goto err_free_percpu;
6192 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006193
6194 txq->pcpu = alloc_percpu(struct mvpp2_txq_pcpu);
6195 if (!txq->pcpu) {
6196 err = -ENOMEM;
6197 goto err_free_percpu;
6198 }
6199
6200 txq->id = queue_phy_id;
6201 txq->log_id = queue;
6202 txq->done_pkts_coal = MVPP2_TXDONE_COAL_PKTS_THRESH;
6203 for_each_present_cpu(cpu) {
6204 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
6205 txq_pcpu->cpu = cpu;
6206 }
6207
6208 port->txqs[queue] = txq;
6209 }
6210
6211 port->rxqs = devm_kcalloc(dev, rxq_number, sizeof(*port->rxqs),
6212 GFP_KERNEL);
6213 if (!port->rxqs) {
6214 err = -ENOMEM;
6215 goto err_free_percpu;
6216 }
6217
6218 /* Allocate and initialize Rx queue for this port */
6219 for (queue = 0; queue < rxq_number; queue++) {
6220 struct mvpp2_rx_queue *rxq;
6221
6222 /* Map physical Rx queue to port's logical Rx queue */
6223 rxq = devm_kzalloc(dev, sizeof(*rxq), GFP_KERNEL);
Jisheng Zhangd82b0c22016-03-31 17:01:23 +08006224 if (!rxq) {
6225 err = -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006226 goto err_free_percpu;
Jisheng Zhangd82b0c22016-03-31 17:01:23 +08006227 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006228 /* Map this Rx queue to a physical queue */
6229 rxq->id = port->first_rxq + queue;
6230 rxq->port = port->id;
6231 rxq->logic_rxq = queue;
6232
6233 port->rxqs[queue] = rxq;
6234 }
6235
6236 /* Configure Rx queue group interrupt for this port */
6237 mvpp2_write(priv, MVPP2_ISR_RXQ_GROUP_REG(port->id), rxq_number);
6238
6239 /* Create Rx descriptor rings */
6240 for (queue = 0; queue < rxq_number; queue++) {
6241 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
6242
6243 rxq->size = port->rx_ring_size;
6244 rxq->pkts_coal = MVPP2_RX_COAL_PKTS;
6245 rxq->time_coal = MVPP2_RX_COAL_USEC;
6246 }
6247
6248 mvpp2_ingress_disable(port);
6249
6250 /* Port default configuration */
6251 mvpp2_defaults_set(port);
6252
6253 /* Port's classifier configuration */
6254 mvpp2_cls_oversize_rxq_set(port);
6255 mvpp2_cls_port_config(port);
6256
6257 /* Provide an initial Rx packet size */
6258 port->pkt_size = MVPP2_RX_PKT_SIZE(port->dev->mtu);
6259
6260 /* Initialize pools for swf */
6261 err = mvpp2_swf_bm_pool_init(port);
6262 if (err)
6263 goto err_free_percpu;
6264
6265 return 0;
6266
6267err_free_percpu:
6268 for (queue = 0; queue < txq_number; queue++) {
6269 if (!port->txqs[queue])
6270 continue;
6271 free_percpu(port->txqs[queue]->pcpu);
6272 }
6273 return err;
6274}
6275
6276/* Ports initialization */
6277static int mvpp2_port_probe(struct platform_device *pdev,
6278 struct device_node *port_node,
6279 struct mvpp2 *priv,
6280 int *next_first_rxq)
6281{
6282 struct device_node *phy_node;
6283 struct mvpp2_port *port;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006284 struct mvpp2_port_pcpu *port_pcpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006285 struct net_device *dev;
6286 struct resource *res;
6287 const char *dt_mac_addr;
6288 const char *mac_from;
6289 char hw_mac_addr[ETH_ALEN];
6290 u32 id;
6291 int features;
6292 int phy_mode;
6293 int priv_common_regs_num = 2;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006294 int err, i, cpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006295
6296 dev = alloc_etherdev_mqs(sizeof(struct mvpp2_port), txq_number,
6297 rxq_number);
6298 if (!dev)
6299 return -ENOMEM;
6300
6301 phy_node = of_parse_phandle(port_node, "phy", 0);
6302 if (!phy_node) {
6303 dev_err(&pdev->dev, "missing phy\n");
6304 err = -ENODEV;
6305 goto err_free_netdev;
6306 }
6307
6308 phy_mode = of_get_phy_mode(port_node);
6309 if (phy_mode < 0) {
6310 dev_err(&pdev->dev, "incorrect phy mode\n");
6311 err = phy_mode;
6312 goto err_free_netdev;
6313 }
6314
6315 if (of_property_read_u32(port_node, "port-id", &id)) {
6316 err = -EINVAL;
6317 dev_err(&pdev->dev, "missing port-id value\n");
6318 goto err_free_netdev;
6319 }
6320
6321 dev->tx_queue_len = MVPP2_MAX_TXD;
6322 dev->watchdog_timeo = 5 * HZ;
6323 dev->netdev_ops = &mvpp2_netdev_ops;
6324 dev->ethtool_ops = &mvpp2_eth_tool_ops;
6325
6326 port = netdev_priv(dev);
6327
6328 port->irq = irq_of_parse_and_map(port_node, 0);
6329 if (port->irq <= 0) {
6330 err = -EINVAL;
6331 goto err_free_netdev;
6332 }
6333
6334 if (of_property_read_bool(port_node, "marvell,loopback"))
6335 port->flags |= MVPP2_F_LOOPBACK;
6336
6337 port->priv = priv;
6338 port->id = id;
6339 port->first_rxq = *next_first_rxq;
6340 port->phy_node = phy_node;
6341 port->phy_interface = phy_mode;
6342
6343 res = platform_get_resource(pdev, IORESOURCE_MEM,
6344 priv_common_regs_num + id);
6345 port->base = devm_ioremap_resource(&pdev->dev, res);
6346 if (IS_ERR(port->base)) {
6347 err = PTR_ERR(port->base);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006348 goto err_free_irq;
6349 }
6350
6351 /* Alloc per-cpu stats */
6352 port->stats = netdev_alloc_pcpu_stats(struct mvpp2_pcpu_stats);
6353 if (!port->stats) {
6354 err = -ENOMEM;
6355 goto err_free_irq;
6356 }
6357
6358 dt_mac_addr = of_get_mac_address(port_node);
6359 if (dt_mac_addr && is_valid_ether_addr(dt_mac_addr)) {
6360 mac_from = "device tree";
6361 ether_addr_copy(dev->dev_addr, dt_mac_addr);
6362 } else {
6363 mvpp2_get_mac_address(port, hw_mac_addr);
6364 if (is_valid_ether_addr(hw_mac_addr)) {
6365 mac_from = "hardware";
6366 ether_addr_copy(dev->dev_addr, hw_mac_addr);
6367 } else {
6368 mac_from = "random";
6369 eth_hw_addr_random(dev);
6370 }
6371 }
6372
6373 port->tx_ring_size = MVPP2_MAX_TXD;
6374 port->rx_ring_size = MVPP2_MAX_RXD;
6375 port->dev = dev;
6376 SET_NETDEV_DEV(dev, &pdev->dev);
6377
6378 err = mvpp2_port_init(port);
6379 if (err < 0) {
6380 dev_err(&pdev->dev, "failed to init port %d\n", id);
6381 goto err_free_stats;
6382 }
6383 mvpp2_port_power_up(port);
6384
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006385 port->pcpu = alloc_percpu(struct mvpp2_port_pcpu);
6386 if (!port->pcpu) {
6387 err = -ENOMEM;
6388 goto err_free_txq_pcpu;
6389 }
6390
6391 for_each_present_cpu(cpu) {
6392 port_pcpu = per_cpu_ptr(port->pcpu, cpu);
6393
6394 hrtimer_init(&port_pcpu->tx_done_timer, CLOCK_MONOTONIC,
6395 HRTIMER_MODE_REL_PINNED);
6396 port_pcpu->tx_done_timer.function = mvpp2_hr_timer_cb;
6397 port_pcpu->timer_scheduled = false;
6398
6399 tasklet_init(&port_pcpu->tx_done_tasklet, mvpp2_tx_proc_cb,
6400 (unsigned long)dev);
6401 }
6402
Marcin Wojtas3f518502014-07-10 16:52:13 -03006403 netif_napi_add(dev, &port->napi, mvpp2_poll, NAPI_POLL_WEIGHT);
6404 features = NETIF_F_SG | NETIF_F_IP_CSUM;
6405 dev->features = features | NETIF_F_RXCSUM;
6406 dev->hw_features |= features | NETIF_F_RXCSUM | NETIF_F_GRO;
6407 dev->vlan_features |= features;
6408
Jarod Wilson57779872016-10-17 15:54:06 -04006409 /* MTU range: 68 - 9676 */
6410 dev->min_mtu = ETH_MIN_MTU;
6411 /* 9676 == 9700 - 20 and rounding to 8 */
6412 dev->max_mtu = 9676;
6413
Marcin Wojtas3f518502014-07-10 16:52:13 -03006414 err = register_netdev(dev);
6415 if (err < 0) {
6416 dev_err(&pdev->dev, "failed to register netdev\n");
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006417 goto err_free_port_pcpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006418 }
6419 netdev_info(dev, "Using %s mac address %pM\n", mac_from, dev->dev_addr);
6420
6421 /* Increment the first Rx queue number to be used by the next port */
6422 *next_first_rxq += rxq_number;
6423 priv->port_list[id] = port;
6424 return 0;
6425
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006426err_free_port_pcpu:
6427 free_percpu(port->pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006428err_free_txq_pcpu:
6429 for (i = 0; i < txq_number; i++)
6430 free_percpu(port->txqs[i]->pcpu);
6431err_free_stats:
6432 free_percpu(port->stats);
6433err_free_irq:
6434 irq_dispose_mapping(port->irq);
6435err_free_netdev:
Peter Chenccb80392016-08-01 15:02:37 +08006436 of_node_put(phy_node);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006437 free_netdev(dev);
6438 return err;
6439}
6440
6441/* Ports removal routine */
6442static void mvpp2_port_remove(struct mvpp2_port *port)
6443{
6444 int i;
6445
6446 unregister_netdev(port->dev);
Peter Chenccb80392016-08-01 15:02:37 +08006447 of_node_put(port->phy_node);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006448 free_percpu(port->pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006449 free_percpu(port->stats);
6450 for (i = 0; i < txq_number; i++)
6451 free_percpu(port->txqs[i]->pcpu);
6452 irq_dispose_mapping(port->irq);
6453 free_netdev(port->dev);
6454}
6455
6456/* Initialize decoding windows */
6457static void mvpp2_conf_mbus_windows(const struct mbus_dram_target_info *dram,
6458 struct mvpp2 *priv)
6459{
6460 u32 win_enable;
6461 int i;
6462
6463 for (i = 0; i < 6; i++) {
6464 mvpp2_write(priv, MVPP2_WIN_BASE(i), 0);
6465 mvpp2_write(priv, MVPP2_WIN_SIZE(i), 0);
6466
6467 if (i < 4)
6468 mvpp2_write(priv, MVPP2_WIN_REMAP(i), 0);
6469 }
6470
6471 win_enable = 0;
6472
6473 for (i = 0; i < dram->num_cs; i++) {
6474 const struct mbus_dram_window *cs = dram->cs + i;
6475
6476 mvpp2_write(priv, MVPP2_WIN_BASE(i),
6477 (cs->base & 0xffff0000) | (cs->mbus_attr << 8) |
6478 dram->mbus_dram_target_id);
6479
6480 mvpp2_write(priv, MVPP2_WIN_SIZE(i),
6481 (cs->size - 1) & 0xffff0000);
6482
6483 win_enable |= (1 << i);
6484 }
6485
6486 mvpp2_write(priv, MVPP2_BASE_ADDR_ENABLE, win_enable);
6487}
6488
6489/* Initialize Rx FIFO's */
6490static void mvpp2_rx_fifo_init(struct mvpp2 *priv)
6491{
6492 int port;
6493
6494 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
6495 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
6496 MVPP2_RX_FIFO_PORT_DATA_SIZE);
6497 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
6498 MVPP2_RX_FIFO_PORT_ATTR_SIZE);
6499 }
6500
6501 mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
6502 MVPP2_RX_FIFO_PORT_MIN_PKT);
6503 mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
6504}
6505
6506/* Initialize network controller common part HW */
6507static int mvpp2_init(struct platform_device *pdev, struct mvpp2 *priv)
6508{
6509 const struct mbus_dram_target_info *dram_target_info;
6510 int err, i;
Marcin Wojtas08a23752014-07-21 13:48:12 -03006511 u32 val;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006512
6513 /* Checks for hardware constraints */
6514 if (rxq_number % 4 || (rxq_number > MVPP2_MAX_RXQ) ||
6515 (txq_number > MVPP2_MAX_TXQ)) {
6516 dev_err(&pdev->dev, "invalid queue size parameter\n");
6517 return -EINVAL;
6518 }
6519
6520 /* MBUS windows configuration */
6521 dram_target_info = mv_mbus_dram_info();
6522 if (dram_target_info)
6523 mvpp2_conf_mbus_windows(dram_target_info, priv);
6524
Marcin Wojtas08a23752014-07-21 13:48:12 -03006525 /* Disable HW PHY polling */
6526 val = readl(priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
6527 val |= MVPP2_PHY_AN_STOP_SMI0_MASK;
6528 writel(val, priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
6529
Marcin Wojtas3f518502014-07-10 16:52:13 -03006530 /* Allocate and initialize aggregated TXQs */
6531 priv->aggr_txqs = devm_kcalloc(&pdev->dev, num_present_cpus(),
6532 sizeof(struct mvpp2_tx_queue),
6533 GFP_KERNEL);
6534 if (!priv->aggr_txqs)
6535 return -ENOMEM;
6536
6537 for_each_present_cpu(i) {
6538 priv->aggr_txqs[i].id = i;
6539 priv->aggr_txqs[i].size = MVPP2_AGGR_TXQ_SIZE;
6540 err = mvpp2_aggr_txq_init(pdev, &priv->aggr_txqs[i],
6541 MVPP2_AGGR_TXQ_SIZE, i, priv);
6542 if (err < 0)
6543 return err;
6544 }
6545
6546 /* Rx Fifo Init */
6547 mvpp2_rx_fifo_init(priv);
6548
6549 /* Reset Rx queue group interrupt configuration */
6550 for (i = 0; i < MVPP2_MAX_PORTS; i++)
6551 mvpp2_write(priv, MVPP2_ISR_RXQ_GROUP_REG(i), rxq_number);
6552
6553 writel(MVPP2_EXT_GLOBAL_CTRL_DEFAULT,
6554 priv->lms_base + MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG);
6555
6556 /* Allow cache snoop when transmiting packets */
6557 mvpp2_write(priv, MVPP2_TX_SNOOP_REG, 0x1);
6558
6559 /* Buffer Manager initialization */
6560 err = mvpp2_bm_init(pdev, priv);
6561 if (err < 0)
6562 return err;
6563
6564 /* Parser default initialization */
6565 err = mvpp2_prs_default_init(pdev, priv);
6566 if (err < 0)
6567 return err;
6568
6569 /* Classifier default initialization */
6570 mvpp2_cls_init(priv);
6571
6572 return 0;
6573}
6574
6575static int mvpp2_probe(struct platform_device *pdev)
6576{
6577 struct device_node *dn = pdev->dev.of_node;
6578 struct device_node *port_node;
6579 struct mvpp2 *priv;
6580 struct resource *res;
6581 int port_count, first_rxq;
6582 int err;
6583
6584 priv = devm_kzalloc(&pdev->dev, sizeof(struct mvpp2), GFP_KERNEL);
6585 if (!priv)
6586 return -ENOMEM;
6587
Thomas Petazzonifaca9242017-03-07 16:53:06 +01006588 priv->hw_version =
6589 (unsigned long)of_device_get_match_data(&pdev->dev);
6590
Marcin Wojtas3f518502014-07-10 16:52:13 -03006591 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
6592 priv->base = devm_ioremap_resource(&pdev->dev, res);
6593 if (IS_ERR(priv->base))
6594 return PTR_ERR(priv->base);
6595
6596 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
6597 priv->lms_base = devm_ioremap_resource(&pdev->dev, res);
6598 if (IS_ERR(priv->lms_base))
6599 return PTR_ERR(priv->lms_base);
6600
6601 priv->pp_clk = devm_clk_get(&pdev->dev, "pp_clk");
6602 if (IS_ERR(priv->pp_clk))
6603 return PTR_ERR(priv->pp_clk);
6604 err = clk_prepare_enable(priv->pp_clk);
6605 if (err < 0)
6606 return err;
6607
6608 priv->gop_clk = devm_clk_get(&pdev->dev, "gop_clk");
6609 if (IS_ERR(priv->gop_clk)) {
6610 err = PTR_ERR(priv->gop_clk);
6611 goto err_pp_clk;
6612 }
6613 err = clk_prepare_enable(priv->gop_clk);
6614 if (err < 0)
6615 goto err_pp_clk;
6616
6617 /* Get system's tclk rate */
6618 priv->tclk = clk_get_rate(priv->pp_clk);
6619
6620 /* Initialize network controller */
6621 err = mvpp2_init(pdev, priv);
6622 if (err < 0) {
6623 dev_err(&pdev->dev, "failed to initialize controller\n");
6624 goto err_gop_clk;
6625 }
6626
6627 port_count = of_get_available_child_count(dn);
6628 if (port_count == 0) {
6629 dev_err(&pdev->dev, "no ports enabled\n");
Wei Yongjun575a1932014-07-20 22:02:43 +08006630 err = -ENODEV;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006631 goto err_gop_clk;
6632 }
6633
6634 priv->port_list = devm_kcalloc(&pdev->dev, port_count,
6635 sizeof(struct mvpp2_port *),
6636 GFP_KERNEL);
6637 if (!priv->port_list) {
6638 err = -ENOMEM;
6639 goto err_gop_clk;
6640 }
6641
6642 /* Initialize ports */
6643 first_rxq = 0;
6644 for_each_available_child_of_node(dn, port_node) {
6645 err = mvpp2_port_probe(pdev, port_node, priv, &first_rxq);
6646 if (err < 0)
6647 goto err_gop_clk;
6648 }
6649
6650 platform_set_drvdata(pdev, priv);
6651 return 0;
6652
6653err_gop_clk:
6654 clk_disable_unprepare(priv->gop_clk);
6655err_pp_clk:
6656 clk_disable_unprepare(priv->pp_clk);
6657 return err;
6658}
6659
6660static int mvpp2_remove(struct platform_device *pdev)
6661{
6662 struct mvpp2 *priv = platform_get_drvdata(pdev);
6663 struct device_node *dn = pdev->dev.of_node;
6664 struct device_node *port_node;
6665 int i = 0;
6666
6667 for_each_available_child_of_node(dn, port_node) {
6668 if (priv->port_list[i])
6669 mvpp2_port_remove(priv->port_list[i]);
6670 i++;
6671 }
6672
6673 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
6674 struct mvpp2_bm_pool *bm_pool = &priv->bm_pools[i];
6675
6676 mvpp2_bm_pool_destroy(pdev, priv, bm_pool);
6677 }
6678
6679 for_each_present_cpu(i) {
6680 struct mvpp2_tx_queue *aggr_txq = &priv->aggr_txqs[i];
6681
6682 dma_free_coherent(&pdev->dev,
6683 MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
6684 aggr_txq->descs,
Thomas Petazzoni20396132017-03-07 16:53:00 +01006685 aggr_txq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006686 }
6687
6688 clk_disable_unprepare(priv->pp_clk);
6689 clk_disable_unprepare(priv->gop_clk);
6690
6691 return 0;
6692}
6693
6694static const struct of_device_id mvpp2_match[] = {
Thomas Petazzonifaca9242017-03-07 16:53:06 +01006695 {
6696 .compatible = "marvell,armada-375-pp2",
6697 .data = (void *)MVPP21,
6698 },
Marcin Wojtas3f518502014-07-10 16:52:13 -03006699 { }
6700};
6701MODULE_DEVICE_TABLE(of, mvpp2_match);
6702
6703static struct platform_driver mvpp2_driver = {
6704 .probe = mvpp2_probe,
6705 .remove = mvpp2_remove,
6706 .driver = {
6707 .name = MVPP2_DRIVER_NAME,
6708 .of_match_table = mvpp2_match,
6709 },
6710};
6711
6712module_platform_driver(mvpp2_driver);
6713
6714MODULE_DESCRIPTION("Marvell PPv2 Ethernet Driver - www.marvell.com");
6715MODULE_AUTHOR("Marcin Wojtas <mw@semihalf.com>");
Ezequiel Garciac6340992014-07-14 10:34:47 -03006716MODULE_LICENSE("GPL v2");