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