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