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