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