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