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