xref: /OK3568_Linux_fs/kernel/drivers/net/ethernet/marvell/mvpp2/mvpp2_prs.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Header Parser helpers for Marvell PPv2 Network Controller
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2014 Marvell
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Marcin Wojtas <mw@semihalf.com>
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <linux/kernel.h>
11*4882a593Smuzhiyun #include <linux/netdevice.h>
12*4882a593Smuzhiyun #include <linux/etherdevice.h>
13*4882a593Smuzhiyun #include <linux/platform_device.h>
14*4882a593Smuzhiyun #include <uapi/linux/ppp_defs.h>
15*4882a593Smuzhiyun #include <net/ip.h>
16*4882a593Smuzhiyun #include <net/ipv6.h>
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #include "mvpp2.h"
19*4882a593Smuzhiyun #include "mvpp2_prs.h"
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun /* Update parser tcam and sram hw entries */
mvpp2_prs_hw_write(struct mvpp2 * priv,struct mvpp2_prs_entry * pe)22*4882a593Smuzhiyun static int mvpp2_prs_hw_write(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
23*4882a593Smuzhiyun {
24*4882a593Smuzhiyun 	int i;
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun 	if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
27*4882a593Smuzhiyun 		return -EINVAL;
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun 	/* Clear entry invalidation bit */
30*4882a593Smuzhiyun 	pe->tcam[MVPP2_PRS_TCAM_INV_WORD] &= ~MVPP2_PRS_TCAM_INV_MASK;
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun 	/* Write sram index - indirect access */
33*4882a593Smuzhiyun 	mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
34*4882a593Smuzhiyun 	for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
35*4882a593Smuzhiyun 		mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), pe->sram[i]);
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun 	/* Write tcam index - indirect access */
38*4882a593Smuzhiyun 	mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
39*4882a593Smuzhiyun 	for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
40*4882a593Smuzhiyun 		mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), pe->tcam[i]);
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 	return 0;
43*4882a593Smuzhiyun }
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun /* Initialize tcam entry from hw */
mvpp2_prs_init_from_hw(struct mvpp2 * priv,struct mvpp2_prs_entry * pe,int tid)46*4882a593Smuzhiyun int mvpp2_prs_init_from_hw(struct mvpp2 *priv, struct mvpp2_prs_entry *pe,
47*4882a593Smuzhiyun 			   int tid)
48*4882a593Smuzhiyun {
49*4882a593Smuzhiyun 	int i;
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun 	if (tid > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
52*4882a593Smuzhiyun 		return -EINVAL;
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun 	memset(pe, 0, sizeof(*pe));
55*4882a593Smuzhiyun 	pe->index = tid;
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	/* Write tcam index - indirect access */
58*4882a593Smuzhiyun 	mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	pe->tcam[MVPP2_PRS_TCAM_INV_WORD] = mvpp2_read(priv,
61*4882a593Smuzhiyun 			      MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD));
62*4882a593Smuzhiyun 	if (pe->tcam[MVPP2_PRS_TCAM_INV_WORD] & MVPP2_PRS_TCAM_INV_MASK)
63*4882a593Smuzhiyun 		return MVPP2_PRS_TCAM_ENTRY_INVALID;
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 	for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
66*4882a593Smuzhiyun 		pe->tcam[i] = mvpp2_read(priv, MVPP2_PRS_TCAM_DATA_REG(i));
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 	/* Write sram index - indirect access */
69*4882a593Smuzhiyun 	mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
70*4882a593Smuzhiyun 	for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
71*4882a593Smuzhiyun 		pe->sram[i] = mvpp2_read(priv, MVPP2_PRS_SRAM_DATA_REG(i));
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 	return 0;
74*4882a593Smuzhiyun }
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun /* Invalidate tcam hw entry */
mvpp2_prs_hw_inv(struct mvpp2 * priv,int index)77*4882a593Smuzhiyun static void mvpp2_prs_hw_inv(struct mvpp2 *priv, int index)
78*4882a593Smuzhiyun {
79*4882a593Smuzhiyun 	/* Write index - indirect access */
80*4882a593Smuzhiyun 	mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
81*4882a593Smuzhiyun 	mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD),
82*4882a593Smuzhiyun 		    MVPP2_PRS_TCAM_INV_MASK);
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun /* Enable shadow table entry and set its lookup ID */
mvpp2_prs_shadow_set(struct mvpp2 * priv,int index,int lu)86*4882a593Smuzhiyun static void mvpp2_prs_shadow_set(struct mvpp2 *priv, int index, int lu)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun 	priv->prs_shadow[index].valid = true;
89*4882a593Smuzhiyun 	priv->prs_shadow[index].lu = lu;
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun /* Update ri fields in shadow table entry */
mvpp2_prs_shadow_ri_set(struct mvpp2 * priv,int index,unsigned int ri,unsigned int ri_mask)93*4882a593Smuzhiyun static void mvpp2_prs_shadow_ri_set(struct mvpp2 *priv, int index,
94*4882a593Smuzhiyun 				    unsigned int ri, unsigned int ri_mask)
95*4882a593Smuzhiyun {
96*4882a593Smuzhiyun 	priv->prs_shadow[index].ri_mask = ri_mask;
97*4882a593Smuzhiyun 	priv->prs_shadow[index].ri = ri;
98*4882a593Smuzhiyun }
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun /* Update lookup field in tcam sw entry */
mvpp2_prs_tcam_lu_set(struct mvpp2_prs_entry * pe,unsigned int lu)101*4882a593Smuzhiyun static void mvpp2_prs_tcam_lu_set(struct mvpp2_prs_entry *pe, unsigned int lu)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun 	pe->tcam[MVPP2_PRS_TCAM_LU_WORD] &= ~MVPP2_PRS_TCAM_LU(MVPP2_PRS_LU_MASK);
104*4882a593Smuzhiyun 	pe->tcam[MVPP2_PRS_TCAM_LU_WORD] &= ~MVPP2_PRS_TCAM_LU_EN(MVPP2_PRS_LU_MASK);
105*4882a593Smuzhiyun 	pe->tcam[MVPP2_PRS_TCAM_LU_WORD] |= MVPP2_PRS_TCAM_LU(lu & MVPP2_PRS_LU_MASK);
106*4882a593Smuzhiyun 	pe->tcam[MVPP2_PRS_TCAM_LU_WORD] |= MVPP2_PRS_TCAM_LU_EN(MVPP2_PRS_LU_MASK);
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun /* Update mask for single port in tcam sw entry */
mvpp2_prs_tcam_port_set(struct mvpp2_prs_entry * pe,unsigned int port,bool add)110*4882a593Smuzhiyun static void mvpp2_prs_tcam_port_set(struct mvpp2_prs_entry *pe,
111*4882a593Smuzhiyun 				    unsigned int port, bool add)
112*4882a593Smuzhiyun {
113*4882a593Smuzhiyun 	if (add)
114*4882a593Smuzhiyun 		pe->tcam[MVPP2_PRS_TCAM_PORT_WORD] &= ~MVPP2_PRS_TCAM_PORT_EN(BIT(port));
115*4882a593Smuzhiyun 	else
116*4882a593Smuzhiyun 		pe->tcam[MVPP2_PRS_TCAM_PORT_WORD] |= MVPP2_PRS_TCAM_PORT_EN(BIT(port));
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun /* Update port map in tcam sw entry */
mvpp2_prs_tcam_port_map_set(struct mvpp2_prs_entry * pe,unsigned int ports)120*4882a593Smuzhiyun static void mvpp2_prs_tcam_port_map_set(struct mvpp2_prs_entry *pe,
121*4882a593Smuzhiyun 					unsigned int ports)
122*4882a593Smuzhiyun {
123*4882a593Smuzhiyun 	pe->tcam[MVPP2_PRS_TCAM_PORT_WORD] &= ~MVPP2_PRS_TCAM_PORT(MVPP2_PRS_PORT_MASK);
124*4882a593Smuzhiyun 	pe->tcam[MVPP2_PRS_TCAM_PORT_WORD] &= ~MVPP2_PRS_TCAM_PORT_EN(MVPP2_PRS_PORT_MASK);
125*4882a593Smuzhiyun 	pe->tcam[MVPP2_PRS_TCAM_PORT_WORD] |= MVPP2_PRS_TCAM_PORT_EN(~ports & MVPP2_PRS_PORT_MASK);
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun /* Obtain port map from tcam sw entry */
mvpp2_prs_tcam_port_map_get(struct mvpp2_prs_entry * pe)129*4882a593Smuzhiyun unsigned int mvpp2_prs_tcam_port_map_get(struct mvpp2_prs_entry *pe)
130*4882a593Smuzhiyun {
131*4882a593Smuzhiyun 	return (~pe->tcam[MVPP2_PRS_TCAM_PORT_WORD] >> 24) & MVPP2_PRS_PORT_MASK;
132*4882a593Smuzhiyun }
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun /* 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)135*4882a593Smuzhiyun static void mvpp2_prs_tcam_data_byte_set(struct mvpp2_prs_entry *pe,
136*4882a593Smuzhiyun 					 unsigned int offs, unsigned char byte,
137*4882a593Smuzhiyun 					 unsigned char enable)
138*4882a593Smuzhiyun {
139*4882a593Smuzhiyun 	int pos = MVPP2_PRS_BYTE_IN_WORD(offs) * BITS_PER_BYTE;
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 	pe->tcam[MVPP2_PRS_BYTE_TO_WORD(offs)] &= ~(0xff << pos);
142*4882a593Smuzhiyun 	pe->tcam[MVPP2_PRS_BYTE_TO_WORD(offs)] &= ~(MVPP2_PRS_TCAM_EN(0xff) << pos);
143*4882a593Smuzhiyun 	pe->tcam[MVPP2_PRS_BYTE_TO_WORD(offs)] |= byte << pos;
144*4882a593Smuzhiyun 	pe->tcam[MVPP2_PRS_BYTE_TO_WORD(offs)] |= MVPP2_PRS_TCAM_EN(enable << pos);
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun /* 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)148*4882a593Smuzhiyun void mvpp2_prs_tcam_data_byte_get(struct mvpp2_prs_entry *pe,
149*4882a593Smuzhiyun 				  unsigned int offs, unsigned char *byte,
150*4882a593Smuzhiyun 				  unsigned char *enable)
151*4882a593Smuzhiyun {
152*4882a593Smuzhiyun 	int pos = MVPP2_PRS_BYTE_IN_WORD(offs) * BITS_PER_BYTE;
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	*byte = (pe->tcam[MVPP2_PRS_BYTE_TO_WORD(offs)] >> pos) & 0xff;
155*4882a593Smuzhiyun 	*enable = (pe->tcam[MVPP2_PRS_BYTE_TO_WORD(offs)] >> (pos + 16)) & 0xff;
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun /* Compare tcam data bytes with a pattern */
mvpp2_prs_tcam_data_cmp(struct mvpp2_prs_entry * pe,int offs,u16 data)159*4882a593Smuzhiyun static bool mvpp2_prs_tcam_data_cmp(struct mvpp2_prs_entry *pe, int offs,
160*4882a593Smuzhiyun 				    u16 data)
161*4882a593Smuzhiyun {
162*4882a593Smuzhiyun 	u16 tcam_data;
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun 	tcam_data = pe->tcam[MVPP2_PRS_BYTE_TO_WORD(offs)] & 0xffff;
165*4882a593Smuzhiyun 	return tcam_data == data;
166*4882a593Smuzhiyun }
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun /* Update ai bits in tcam sw entry */
mvpp2_prs_tcam_ai_update(struct mvpp2_prs_entry * pe,unsigned int bits,unsigned int enable)169*4882a593Smuzhiyun static void mvpp2_prs_tcam_ai_update(struct mvpp2_prs_entry *pe,
170*4882a593Smuzhiyun 				     unsigned int bits, unsigned int enable)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun 	int i;
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 	for (i = 0; i < MVPP2_PRS_AI_BITS; i++) {
175*4882a593Smuzhiyun 		if (!(enable & BIT(i)))
176*4882a593Smuzhiyun 			continue;
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 		if (bits & BIT(i))
179*4882a593Smuzhiyun 			pe->tcam[MVPP2_PRS_TCAM_AI_WORD] |= BIT(i);
180*4882a593Smuzhiyun 		else
181*4882a593Smuzhiyun 			pe->tcam[MVPP2_PRS_TCAM_AI_WORD] &= ~BIT(i);
182*4882a593Smuzhiyun 	}
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 	pe->tcam[MVPP2_PRS_TCAM_AI_WORD] |= MVPP2_PRS_TCAM_AI_EN(enable);
185*4882a593Smuzhiyun }
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun /* Get ai bits from tcam sw entry */
mvpp2_prs_tcam_ai_get(struct mvpp2_prs_entry * pe)188*4882a593Smuzhiyun static int mvpp2_prs_tcam_ai_get(struct mvpp2_prs_entry *pe)
189*4882a593Smuzhiyun {
190*4882a593Smuzhiyun 	return pe->tcam[MVPP2_PRS_TCAM_AI_WORD] & MVPP2_PRS_AI_MASK;
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun /* Set ethertype in tcam sw entry */
mvpp2_prs_match_etype(struct mvpp2_prs_entry * pe,int offset,unsigned short ethertype)194*4882a593Smuzhiyun static void mvpp2_prs_match_etype(struct mvpp2_prs_entry *pe, int offset,
195*4882a593Smuzhiyun 				  unsigned short ethertype)
196*4882a593Smuzhiyun {
197*4882a593Smuzhiyun 	mvpp2_prs_tcam_data_byte_set(pe, offset + 0, ethertype >> 8, 0xff);
198*4882a593Smuzhiyun 	mvpp2_prs_tcam_data_byte_set(pe, offset + 1, ethertype & 0xff, 0xff);
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun /* Set vid in tcam sw entry */
mvpp2_prs_match_vid(struct mvpp2_prs_entry * pe,int offset,unsigned short vid)202*4882a593Smuzhiyun static void mvpp2_prs_match_vid(struct mvpp2_prs_entry *pe, int offset,
203*4882a593Smuzhiyun 				unsigned short vid)
204*4882a593Smuzhiyun {
205*4882a593Smuzhiyun 	mvpp2_prs_tcam_data_byte_set(pe, offset + 0, (vid & 0xf00) >> 8, 0xf);
206*4882a593Smuzhiyun 	mvpp2_prs_tcam_data_byte_set(pe, offset + 1, vid & 0xff, 0xff);
207*4882a593Smuzhiyun }
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun /* Set bits in sram sw entry */
mvpp2_prs_sram_bits_set(struct mvpp2_prs_entry * pe,int bit_num,u32 val)210*4882a593Smuzhiyun static void mvpp2_prs_sram_bits_set(struct mvpp2_prs_entry *pe, int bit_num,
211*4882a593Smuzhiyun 				    u32 val)
212*4882a593Smuzhiyun {
213*4882a593Smuzhiyun 	pe->sram[MVPP2_BIT_TO_WORD(bit_num)] |= (val << (MVPP2_BIT_IN_WORD(bit_num)));
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun /* Clear bits in sram sw entry */
mvpp2_prs_sram_bits_clear(struct mvpp2_prs_entry * pe,int bit_num,u32 val)217*4882a593Smuzhiyun static void mvpp2_prs_sram_bits_clear(struct mvpp2_prs_entry *pe, int bit_num,
218*4882a593Smuzhiyun 				      u32 val)
219*4882a593Smuzhiyun {
220*4882a593Smuzhiyun 	pe->sram[MVPP2_BIT_TO_WORD(bit_num)] &= ~(val << (MVPP2_BIT_IN_WORD(bit_num)));
221*4882a593Smuzhiyun }
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun /* Update ri bits in sram sw entry */
mvpp2_prs_sram_ri_update(struct mvpp2_prs_entry * pe,unsigned int bits,unsigned int mask)224*4882a593Smuzhiyun static void mvpp2_prs_sram_ri_update(struct mvpp2_prs_entry *pe,
225*4882a593Smuzhiyun 				     unsigned int bits, unsigned int mask)
226*4882a593Smuzhiyun {
227*4882a593Smuzhiyun 	unsigned int i;
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun 	for (i = 0; i < MVPP2_PRS_SRAM_RI_CTRL_BITS; i++) {
230*4882a593Smuzhiyun 		if (!(mask & BIT(i)))
231*4882a593Smuzhiyun 			continue;
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun 		if (bits & BIT(i))
234*4882a593Smuzhiyun 			mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_RI_OFFS + i,
235*4882a593Smuzhiyun 						1);
236*4882a593Smuzhiyun 		else
237*4882a593Smuzhiyun 			mvpp2_prs_sram_bits_clear(pe,
238*4882a593Smuzhiyun 						  MVPP2_PRS_SRAM_RI_OFFS + i,
239*4882a593Smuzhiyun 						  1);
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun 		mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_RI_CTRL_OFFS + i, 1);
242*4882a593Smuzhiyun 	}
243*4882a593Smuzhiyun }
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun /* Obtain ri bits from sram sw entry */
mvpp2_prs_sram_ri_get(struct mvpp2_prs_entry * pe)246*4882a593Smuzhiyun static int mvpp2_prs_sram_ri_get(struct mvpp2_prs_entry *pe)
247*4882a593Smuzhiyun {
248*4882a593Smuzhiyun 	return pe->sram[MVPP2_PRS_SRAM_RI_WORD];
249*4882a593Smuzhiyun }
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun /* Update ai bits in sram sw entry */
mvpp2_prs_sram_ai_update(struct mvpp2_prs_entry * pe,unsigned int bits,unsigned int mask)252*4882a593Smuzhiyun static void mvpp2_prs_sram_ai_update(struct mvpp2_prs_entry *pe,
253*4882a593Smuzhiyun 				     unsigned int bits, unsigned int mask)
254*4882a593Smuzhiyun {
255*4882a593Smuzhiyun 	unsigned int i;
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	for (i = 0; i < MVPP2_PRS_SRAM_AI_CTRL_BITS; i++) {
258*4882a593Smuzhiyun 		if (!(mask & BIT(i)))
259*4882a593Smuzhiyun 			continue;
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 		if (bits & BIT(i))
262*4882a593Smuzhiyun 			mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_AI_OFFS + i,
263*4882a593Smuzhiyun 						1);
264*4882a593Smuzhiyun 		else
265*4882a593Smuzhiyun 			mvpp2_prs_sram_bits_clear(pe,
266*4882a593Smuzhiyun 						  MVPP2_PRS_SRAM_AI_OFFS + i,
267*4882a593Smuzhiyun 						  1);
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 		mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_AI_CTRL_OFFS + i, 1);
270*4882a593Smuzhiyun 	}
271*4882a593Smuzhiyun }
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun /* Read ai bits from sram sw entry */
mvpp2_prs_sram_ai_get(struct mvpp2_prs_entry * pe)274*4882a593Smuzhiyun static int mvpp2_prs_sram_ai_get(struct mvpp2_prs_entry *pe)
275*4882a593Smuzhiyun {
276*4882a593Smuzhiyun 	u8 bits;
277*4882a593Smuzhiyun 	/* ai is stored on bits 90->97; so it spreads across two u32 */
278*4882a593Smuzhiyun 	int ai_off = MVPP2_BIT_TO_WORD(MVPP2_PRS_SRAM_AI_OFFS);
279*4882a593Smuzhiyun 	int ai_shift = MVPP2_BIT_IN_WORD(MVPP2_PRS_SRAM_AI_OFFS);
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 	bits = (pe->sram[ai_off] >> ai_shift) |
282*4882a593Smuzhiyun 	       (pe->sram[ai_off + 1] << (32 - ai_shift));
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun 	return bits;
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun /* In sram sw entry set lookup ID field of the tcam key to be used in the next
288*4882a593Smuzhiyun  * lookup interation
289*4882a593Smuzhiyun  */
mvpp2_prs_sram_next_lu_set(struct mvpp2_prs_entry * pe,unsigned int lu)290*4882a593Smuzhiyun static void mvpp2_prs_sram_next_lu_set(struct mvpp2_prs_entry *pe,
291*4882a593Smuzhiyun 				       unsigned int lu)
292*4882a593Smuzhiyun {
293*4882a593Smuzhiyun 	int sram_next_off = MVPP2_PRS_SRAM_NEXT_LU_OFFS;
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun 	mvpp2_prs_sram_bits_clear(pe, sram_next_off,
296*4882a593Smuzhiyun 				  MVPP2_PRS_SRAM_NEXT_LU_MASK);
297*4882a593Smuzhiyun 	mvpp2_prs_sram_bits_set(pe, sram_next_off, lu);
298*4882a593Smuzhiyun }
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun /* In the sram sw entry set sign and value of the next lookup offset
301*4882a593Smuzhiyun  * and the offset value generated to the classifier
302*4882a593Smuzhiyun  */
mvpp2_prs_sram_shift_set(struct mvpp2_prs_entry * pe,int shift,unsigned int op)303*4882a593Smuzhiyun static void mvpp2_prs_sram_shift_set(struct mvpp2_prs_entry *pe, int shift,
304*4882a593Smuzhiyun 				     unsigned int op)
305*4882a593Smuzhiyun {
306*4882a593Smuzhiyun 	/* Set sign */
307*4882a593Smuzhiyun 	if (shift < 0) {
308*4882a593Smuzhiyun 		mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
309*4882a593Smuzhiyun 		shift = 0 - shift;
310*4882a593Smuzhiyun 	} else {
311*4882a593Smuzhiyun 		mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
312*4882a593Smuzhiyun 	}
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun 	/* Set value */
315*4882a593Smuzhiyun 	pe->sram[MVPP2_BIT_TO_WORD(MVPP2_PRS_SRAM_SHIFT_OFFS)] |=
316*4882a593Smuzhiyun 		shift & MVPP2_PRS_SRAM_SHIFT_MASK;
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 	/* Reset and set operation */
319*4882a593Smuzhiyun 	mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS,
320*4882a593Smuzhiyun 				  MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK);
321*4882a593Smuzhiyun 	mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS, op);
322*4882a593Smuzhiyun 
323*4882a593Smuzhiyun 	/* Set base offset as current */
324*4882a593Smuzhiyun 	mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
325*4882a593Smuzhiyun }
326*4882a593Smuzhiyun 
327*4882a593Smuzhiyun /* In the sram sw entry set sign and value of the user defined offset
328*4882a593Smuzhiyun  * generated to the classifier
329*4882a593Smuzhiyun  */
mvpp2_prs_sram_offset_set(struct mvpp2_prs_entry * pe,unsigned int type,int offset,unsigned int op)330*4882a593Smuzhiyun static void mvpp2_prs_sram_offset_set(struct mvpp2_prs_entry *pe,
331*4882a593Smuzhiyun 				      unsigned int type, int offset,
332*4882a593Smuzhiyun 				      unsigned int op)
333*4882a593Smuzhiyun {
334*4882a593Smuzhiyun 	/* Set sign */
335*4882a593Smuzhiyun 	if (offset < 0) {
336*4882a593Smuzhiyun 		mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
337*4882a593Smuzhiyun 		offset = 0 - offset;
338*4882a593Smuzhiyun 	} else {
339*4882a593Smuzhiyun 		mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
340*4882a593Smuzhiyun 	}
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun 	/* Set value */
343*4882a593Smuzhiyun 	mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_OFFS,
344*4882a593Smuzhiyun 				  MVPP2_PRS_SRAM_UDF_MASK);
345*4882a593Smuzhiyun 	mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_OFFS,
346*4882a593Smuzhiyun 				offset & MVPP2_PRS_SRAM_UDF_MASK);
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun 	/* Set offset type */
349*4882a593Smuzhiyun 	mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS,
350*4882a593Smuzhiyun 				  MVPP2_PRS_SRAM_UDF_TYPE_MASK);
351*4882a593Smuzhiyun 	mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS, type);
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun 	/* Set offset operation */
354*4882a593Smuzhiyun 	mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS,
355*4882a593Smuzhiyun 				  MVPP2_PRS_SRAM_OP_SEL_UDF_MASK);
356*4882a593Smuzhiyun 	mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS,
357*4882a593Smuzhiyun 				op & MVPP2_PRS_SRAM_OP_SEL_UDF_MASK);
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun 	/* Set base offset as current */
360*4882a593Smuzhiyun 	mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
361*4882a593Smuzhiyun }
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun /* Find parser flow entry */
mvpp2_prs_flow_find(struct mvpp2 * priv,int flow)364*4882a593Smuzhiyun static int mvpp2_prs_flow_find(struct mvpp2 *priv, int flow)
365*4882a593Smuzhiyun {
366*4882a593Smuzhiyun 	struct mvpp2_prs_entry pe;
367*4882a593Smuzhiyun 	int tid;
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun 	/* Go through the all entires with MVPP2_PRS_LU_FLOWS */
370*4882a593Smuzhiyun 	for (tid = MVPP2_PRS_TCAM_SRAM_SIZE - 1; tid >= 0; tid--) {
371*4882a593Smuzhiyun 		u8 bits;
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun 		if (!priv->prs_shadow[tid].valid ||
374*4882a593Smuzhiyun 		    priv->prs_shadow[tid].lu != MVPP2_PRS_LU_FLOWS)
375*4882a593Smuzhiyun 			continue;
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun 		mvpp2_prs_init_from_hw(priv, &pe, tid);
378*4882a593Smuzhiyun 		bits = mvpp2_prs_sram_ai_get(&pe);
379*4882a593Smuzhiyun 
380*4882a593Smuzhiyun 		/* Sram store classification lookup ID in AI bits [5:0] */
381*4882a593Smuzhiyun 		if ((bits & MVPP2_PRS_FLOW_ID_MASK) == flow)
382*4882a593Smuzhiyun 			return tid;
383*4882a593Smuzhiyun 	}
384*4882a593Smuzhiyun 
385*4882a593Smuzhiyun 	return -ENOENT;
386*4882a593Smuzhiyun }
387*4882a593Smuzhiyun 
388*4882a593Smuzhiyun /* Return first free tcam index, seeking from start to end */
mvpp2_prs_tcam_first_free(struct mvpp2 * priv,unsigned char start,unsigned char end)389*4882a593Smuzhiyun static int mvpp2_prs_tcam_first_free(struct mvpp2 *priv, unsigned char start,
390*4882a593Smuzhiyun 				     unsigned char end)
391*4882a593Smuzhiyun {
392*4882a593Smuzhiyun 	int tid;
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun 	if (start > end)
395*4882a593Smuzhiyun 		swap(start, end);
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun 	if (end >= MVPP2_PRS_TCAM_SRAM_SIZE)
398*4882a593Smuzhiyun 		end = MVPP2_PRS_TCAM_SRAM_SIZE - 1;
399*4882a593Smuzhiyun 
400*4882a593Smuzhiyun 	for (tid = start; tid <= end; tid++) {
401*4882a593Smuzhiyun 		if (!priv->prs_shadow[tid].valid)
402*4882a593Smuzhiyun 			return tid;
403*4882a593Smuzhiyun 	}
404*4882a593Smuzhiyun 
405*4882a593Smuzhiyun 	return -EINVAL;
406*4882a593Smuzhiyun }
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun /* Drop flow control pause frames */
mvpp2_prs_drop_fc(struct mvpp2 * priv)409*4882a593Smuzhiyun static void mvpp2_prs_drop_fc(struct mvpp2 *priv)
410*4882a593Smuzhiyun {
411*4882a593Smuzhiyun 	unsigned char da[ETH_ALEN] = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x01 };
412*4882a593Smuzhiyun 	struct mvpp2_prs_entry pe;
413*4882a593Smuzhiyun 	unsigned int len;
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun 	memset(&pe, 0, sizeof(pe));
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun 	/* For all ports - drop flow control frames */
418*4882a593Smuzhiyun 	pe.index = MVPP2_PE_FC_DROP;
419*4882a593Smuzhiyun 	mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
420*4882a593Smuzhiyun 
421*4882a593Smuzhiyun 	/* Set match on DA */
422*4882a593Smuzhiyun 	len = ETH_ALEN;
423*4882a593Smuzhiyun 	while (len--)
424*4882a593Smuzhiyun 		mvpp2_prs_tcam_data_byte_set(&pe, len, da[len], 0xff);
425*4882a593Smuzhiyun 
426*4882a593Smuzhiyun 	mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
427*4882a593Smuzhiyun 				 MVPP2_PRS_RI_DROP_MASK);
428*4882a593Smuzhiyun 
429*4882a593Smuzhiyun 	mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
430*4882a593Smuzhiyun 	mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun 	/* Mask all ports */
433*4882a593Smuzhiyun 	mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
434*4882a593Smuzhiyun 
435*4882a593Smuzhiyun 	/* Update shadow table and hw entry */
436*4882a593Smuzhiyun 	mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
437*4882a593Smuzhiyun 	mvpp2_prs_hw_write(priv, &pe);
438*4882a593Smuzhiyun }
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun /* Enable/disable dropping all mac da's */
mvpp2_prs_mac_drop_all_set(struct mvpp2 * priv,int port,bool add)441*4882a593Smuzhiyun static void mvpp2_prs_mac_drop_all_set(struct mvpp2 *priv, int port, bool add)
442*4882a593Smuzhiyun {
443*4882a593Smuzhiyun 	struct mvpp2_prs_entry pe;
444*4882a593Smuzhiyun 
445*4882a593Smuzhiyun 	if (priv->prs_shadow[MVPP2_PE_DROP_ALL].valid) {
446*4882a593Smuzhiyun 		/* Entry exist - update port only */
447*4882a593Smuzhiyun 		mvpp2_prs_init_from_hw(priv, &pe, MVPP2_PE_DROP_ALL);
448*4882a593Smuzhiyun 	} else {
449*4882a593Smuzhiyun 		/* Entry doesn't exist - create new */
450*4882a593Smuzhiyun 		memset(&pe, 0, sizeof(pe));
451*4882a593Smuzhiyun 		mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
452*4882a593Smuzhiyun 		pe.index = MVPP2_PE_DROP_ALL;
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun 		/* Non-promiscuous mode for all ports - DROP unknown packets */
455*4882a593Smuzhiyun 		mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
456*4882a593Smuzhiyun 					 MVPP2_PRS_RI_DROP_MASK);
457*4882a593Smuzhiyun 
458*4882a593Smuzhiyun 		mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
459*4882a593Smuzhiyun 		mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun 		/* Update shadow table */
462*4882a593Smuzhiyun 		mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun 		/* Mask all ports */
465*4882a593Smuzhiyun 		mvpp2_prs_tcam_port_map_set(&pe, 0);
466*4882a593Smuzhiyun 	}
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun 	/* Update port mask */
469*4882a593Smuzhiyun 	mvpp2_prs_tcam_port_set(&pe, port, add);
470*4882a593Smuzhiyun 
471*4882a593Smuzhiyun 	mvpp2_prs_hw_write(priv, &pe);
472*4882a593Smuzhiyun }
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun /* Set port to unicast or multicast promiscuous mode */
mvpp2_prs_mac_promisc_set(struct mvpp2 * priv,int port,enum mvpp2_prs_l2_cast l2_cast,bool add)475*4882a593Smuzhiyun void mvpp2_prs_mac_promisc_set(struct mvpp2 *priv, int port,
476*4882a593Smuzhiyun 			       enum mvpp2_prs_l2_cast l2_cast, bool add)
477*4882a593Smuzhiyun {
478*4882a593Smuzhiyun 	struct mvpp2_prs_entry pe;
479*4882a593Smuzhiyun 	unsigned char cast_match;
480*4882a593Smuzhiyun 	unsigned int ri;
481*4882a593Smuzhiyun 	int tid;
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun 	if (l2_cast == MVPP2_PRS_L2_UNI_CAST) {
484*4882a593Smuzhiyun 		cast_match = MVPP2_PRS_UCAST_VAL;
485*4882a593Smuzhiyun 		tid = MVPP2_PE_MAC_UC_PROMISCUOUS;
486*4882a593Smuzhiyun 		ri = MVPP2_PRS_RI_L2_UCAST;
487*4882a593Smuzhiyun 	} else {
488*4882a593Smuzhiyun 		cast_match = MVPP2_PRS_MCAST_VAL;
489*4882a593Smuzhiyun 		tid = MVPP2_PE_MAC_MC_PROMISCUOUS;
490*4882a593Smuzhiyun 		ri = MVPP2_PRS_RI_L2_MCAST;
491*4882a593Smuzhiyun 	}
492*4882a593Smuzhiyun 
493*4882a593Smuzhiyun 	/* promiscuous mode - Accept unknown unicast or multicast packets */
494*4882a593Smuzhiyun 	if (priv->prs_shadow[tid].valid) {
495*4882a593Smuzhiyun 		mvpp2_prs_init_from_hw(priv, &pe, tid);
496*4882a593Smuzhiyun 	} else {
497*4882a593Smuzhiyun 		memset(&pe, 0, sizeof(pe));
498*4882a593Smuzhiyun 		mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
499*4882a593Smuzhiyun 		pe.index = tid;
500*4882a593Smuzhiyun 
501*4882a593Smuzhiyun 		/* Continue - set next lookup */
502*4882a593Smuzhiyun 		mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
503*4882a593Smuzhiyun 
504*4882a593Smuzhiyun 		/* Set result info bits */
505*4882a593Smuzhiyun 		mvpp2_prs_sram_ri_update(&pe, ri, MVPP2_PRS_RI_L2_CAST_MASK);
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun 		/* Match UC or MC addresses */
508*4882a593Smuzhiyun 		mvpp2_prs_tcam_data_byte_set(&pe, 0, cast_match,
509*4882a593Smuzhiyun 					     MVPP2_PRS_CAST_MASK);
510*4882a593Smuzhiyun 
511*4882a593Smuzhiyun 		/* Shift to ethertype */
512*4882a593Smuzhiyun 		mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
513*4882a593Smuzhiyun 					 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
514*4882a593Smuzhiyun 
515*4882a593Smuzhiyun 		/* Mask all ports */
516*4882a593Smuzhiyun 		mvpp2_prs_tcam_port_map_set(&pe, 0);
517*4882a593Smuzhiyun 
518*4882a593Smuzhiyun 		/* Update shadow table */
519*4882a593Smuzhiyun 		mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
520*4882a593Smuzhiyun 	}
521*4882a593Smuzhiyun 
522*4882a593Smuzhiyun 	/* Update port mask */
523*4882a593Smuzhiyun 	mvpp2_prs_tcam_port_set(&pe, port, add);
524*4882a593Smuzhiyun 
525*4882a593Smuzhiyun 	mvpp2_prs_hw_write(priv, &pe);
526*4882a593Smuzhiyun }
527*4882a593Smuzhiyun 
528*4882a593Smuzhiyun /* Set entry for dsa packets */
mvpp2_prs_dsa_tag_set(struct mvpp2 * priv,int port,bool add,bool tagged,bool extend)529*4882a593Smuzhiyun static void mvpp2_prs_dsa_tag_set(struct mvpp2 *priv, int port, bool add,
530*4882a593Smuzhiyun 				  bool tagged, bool extend)
531*4882a593Smuzhiyun {
532*4882a593Smuzhiyun 	struct mvpp2_prs_entry pe;
533*4882a593Smuzhiyun 	int tid, shift;
534*4882a593Smuzhiyun 
535*4882a593Smuzhiyun 	if (extend) {
536*4882a593Smuzhiyun 		tid = tagged ? MVPP2_PE_EDSA_TAGGED : MVPP2_PE_EDSA_UNTAGGED;
537*4882a593Smuzhiyun 		shift = 8;
538*4882a593Smuzhiyun 	} else {
539*4882a593Smuzhiyun 		tid = tagged ? MVPP2_PE_DSA_TAGGED : MVPP2_PE_DSA_UNTAGGED;
540*4882a593Smuzhiyun 		shift = 4;
541*4882a593Smuzhiyun 	}
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun 	if (priv->prs_shadow[tid].valid) {
544*4882a593Smuzhiyun 		/* Entry exist - update port only */
545*4882a593Smuzhiyun 		mvpp2_prs_init_from_hw(priv, &pe, tid);
546*4882a593Smuzhiyun 	} else {
547*4882a593Smuzhiyun 		/* Entry doesn't exist - create new */
548*4882a593Smuzhiyun 		memset(&pe, 0, sizeof(pe));
549*4882a593Smuzhiyun 		mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
550*4882a593Smuzhiyun 		pe.index = tid;
551*4882a593Smuzhiyun 
552*4882a593Smuzhiyun 		/* Update shadow table */
553*4882a593Smuzhiyun 		mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
554*4882a593Smuzhiyun 
555*4882a593Smuzhiyun 		if (tagged) {
556*4882a593Smuzhiyun 			/* Set tagged bit in DSA tag */
557*4882a593Smuzhiyun 			mvpp2_prs_tcam_data_byte_set(&pe, 0,
558*4882a593Smuzhiyun 					     MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
559*4882a593Smuzhiyun 					     MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
560*4882a593Smuzhiyun 
561*4882a593Smuzhiyun 			/* Set ai bits for next iteration */
562*4882a593Smuzhiyun 			if (extend)
563*4882a593Smuzhiyun 				mvpp2_prs_sram_ai_update(&pe, 1,
564*4882a593Smuzhiyun 							MVPP2_PRS_SRAM_AI_MASK);
565*4882a593Smuzhiyun 			else
566*4882a593Smuzhiyun 				mvpp2_prs_sram_ai_update(&pe, 0,
567*4882a593Smuzhiyun 							MVPP2_PRS_SRAM_AI_MASK);
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun 			/* Set result info bits to 'single vlan' */
570*4882a593Smuzhiyun 			mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_SINGLE,
571*4882a593Smuzhiyun 						 MVPP2_PRS_RI_VLAN_MASK);
572*4882a593Smuzhiyun 			/* If packet is tagged continue check vid filtering */
573*4882a593Smuzhiyun 			mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VID);
574*4882a593Smuzhiyun 		} else {
575*4882a593Smuzhiyun 			/* Shift 4 bytes for DSA tag or 8 bytes for EDSA tag*/
576*4882a593Smuzhiyun 			mvpp2_prs_sram_shift_set(&pe, shift,
577*4882a593Smuzhiyun 					MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
578*4882a593Smuzhiyun 
579*4882a593Smuzhiyun 			/* Set result info bits to 'no vlans' */
580*4882a593Smuzhiyun 			mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
581*4882a593Smuzhiyun 						 MVPP2_PRS_RI_VLAN_MASK);
582*4882a593Smuzhiyun 			mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
583*4882a593Smuzhiyun 		}
584*4882a593Smuzhiyun 
585*4882a593Smuzhiyun 		/* Mask all ports */
586*4882a593Smuzhiyun 		mvpp2_prs_tcam_port_map_set(&pe, 0);
587*4882a593Smuzhiyun 	}
588*4882a593Smuzhiyun 
589*4882a593Smuzhiyun 	/* Update port mask */
590*4882a593Smuzhiyun 	mvpp2_prs_tcam_port_set(&pe, port, add);
591*4882a593Smuzhiyun 
592*4882a593Smuzhiyun 	mvpp2_prs_hw_write(priv, &pe);
593*4882a593Smuzhiyun }
594*4882a593Smuzhiyun 
595*4882a593Smuzhiyun /* Set entry for dsa ethertype */
mvpp2_prs_dsa_tag_ethertype_set(struct mvpp2 * priv,int port,bool add,bool tagged,bool extend)596*4882a593Smuzhiyun static void mvpp2_prs_dsa_tag_ethertype_set(struct mvpp2 *priv, int port,
597*4882a593Smuzhiyun 					    bool add, bool tagged, bool extend)
598*4882a593Smuzhiyun {
599*4882a593Smuzhiyun 	struct mvpp2_prs_entry pe;
600*4882a593Smuzhiyun 	int tid, shift, port_mask;
601*4882a593Smuzhiyun 
602*4882a593Smuzhiyun 	if (extend) {
603*4882a593Smuzhiyun 		tid = tagged ? MVPP2_PE_ETYPE_EDSA_TAGGED :
604*4882a593Smuzhiyun 		      MVPP2_PE_ETYPE_EDSA_UNTAGGED;
605*4882a593Smuzhiyun 		port_mask = 0;
606*4882a593Smuzhiyun 		shift = 8;
607*4882a593Smuzhiyun 	} else {
608*4882a593Smuzhiyun 		tid = tagged ? MVPP2_PE_ETYPE_DSA_TAGGED :
609*4882a593Smuzhiyun 		      MVPP2_PE_ETYPE_DSA_UNTAGGED;
610*4882a593Smuzhiyun 		port_mask = MVPP2_PRS_PORT_MASK;
611*4882a593Smuzhiyun 		shift = 4;
612*4882a593Smuzhiyun 	}
613*4882a593Smuzhiyun 
614*4882a593Smuzhiyun 	if (priv->prs_shadow[tid].valid) {
615*4882a593Smuzhiyun 		/* Entry exist - update port only */
616*4882a593Smuzhiyun 		mvpp2_prs_init_from_hw(priv, &pe, tid);
617*4882a593Smuzhiyun 	} else {
618*4882a593Smuzhiyun 		/* Entry doesn't exist - create new */
619*4882a593Smuzhiyun 		memset(&pe, 0, sizeof(pe));
620*4882a593Smuzhiyun 		mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
621*4882a593Smuzhiyun 		pe.index = tid;
622*4882a593Smuzhiyun 
623*4882a593Smuzhiyun 		/* Set ethertype */
624*4882a593Smuzhiyun 		mvpp2_prs_match_etype(&pe, 0, ETH_P_EDSA);
625*4882a593Smuzhiyun 		mvpp2_prs_match_etype(&pe, 2, 0);
626*4882a593Smuzhiyun 
627*4882a593Smuzhiyun 		mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DSA_MASK,
628*4882a593Smuzhiyun 					 MVPP2_PRS_RI_DSA_MASK);
629*4882a593Smuzhiyun 		/* Shift ethertype + 2 byte reserved + tag*/
630*4882a593Smuzhiyun 		mvpp2_prs_sram_shift_set(&pe, 2 + MVPP2_ETH_TYPE_LEN + shift,
631*4882a593Smuzhiyun 					 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun 		/* Update shadow table */
634*4882a593Smuzhiyun 		mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
635*4882a593Smuzhiyun 
636*4882a593Smuzhiyun 		if (tagged) {
637*4882a593Smuzhiyun 			/* Set tagged bit in DSA tag */
638*4882a593Smuzhiyun 			mvpp2_prs_tcam_data_byte_set(&pe,
639*4882a593Smuzhiyun 						     MVPP2_ETH_TYPE_LEN + 2 + 3,
640*4882a593Smuzhiyun 						 MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
641*4882a593Smuzhiyun 						 MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
642*4882a593Smuzhiyun 			/* Clear all ai bits for next iteration */
643*4882a593Smuzhiyun 			mvpp2_prs_sram_ai_update(&pe, 0,
644*4882a593Smuzhiyun 						 MVPP2_PRS_SRAM_AI_MASK);
645*4882a593Smuzhiyun 			/* If packet is tagged continue check vlans */
646*4882a593Smuzhiyun 			mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
647*4882a593Smuzhiyun 		} else {
648*4882a593Smuzhiyun 			/* Set result info bits to 'no vlans' */
649*4882a593Smuzhiyun 			mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
650*4882a593Smuzhiyun 						 MVPP2_PRS_RI_VLAN_MASK);
651*4882a593Smuzhiyun 			mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
652*4882a593Smuzhiyun 		}
653*4882a593Smuzhiyun 		/* Mask/unmask all ports, depending on dsa type */
654*4882a593Smuzhiyun 		mvpp2_prs_tcam_port_map_set(&pe, port_mask);
655*4882a593Smuzhiyun 	}
656*4882a593Smuzhiyun 
657*4882a593Smuzhiyun 	/* Update port mask */
658*4882a593Smuzhiyun 	mvpp2_prs_tcam_port_set(&pe, port, add);
659*4882a593Smuzhiyun 
660*4882a593Smuzhiyun 	mvpp2_prs_hw_write(priv, &pe);
661*4882a593Smuzhiyun }
662*4882a593Smuzhiyun 
663*4882a593Smuzhiyun /* Search for existing single/triple vlan entry */
mvpp2_prs_vlan_find(struct mvpp2 * priv,unsigned short tpid,int ai)664*4882a593Smuzhiyun static int mvpp2_prs_vlan_find(struct mvpp2 *priv, unsigned short tpid, int ai)
665*4882a593Smuzhiyun {
666*4882a593Smuzhiyun 	struct mvpp2_prs_entry pe;
667*4882a593Smuzhiyun 	int tid;
668*4882a593Smuzhiyun 
669*4882a593Smuzhiyun 	/* Go through the all entries with MVPP2_PRS_LU_VLAN */
670*4882a593Smuzhiyun 	for (tid = MVPP2_PE_FIRST_FREE_TID;
671*4882a593Smuzhiyun 	     tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
672*4882a593Smuzhiyun 		unsigned int ri_bits, ai_bits;
673*4882a593Smuzhiyun 		bool match;
674*4882a593Smuzhiyun 
675*4882a593Smuzhiyun 		if (!priv->prs_shadow[tid].valid ||
676*4882a593Smuzhiyun 		    priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
677*4882a593Smuzhiyun 			continue;
678*4882a593Smuzhiyun 
679*4882a593Smuzhiyun 		mvpp2_prs_init_from_hw(priv, &pe, tid);
680*4882a593Smuzhiyun 		match = mvpp2_prs_tcam_data_cmp(&pe, 0, tpid);
681*4882a593Smuzhiyun 		if (!match)
682*4882a593Smuzhiyun 			continue;
683*4882a593Smuzhiyun 
684*4882a593Smuzhiyun 		/* Get vlan type */
685*4882a593Smuzhiyun 		ri_bits = mvpp2_prs_sram_ri_get(&pe);
686*4882a593Smuzhiyun 		ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
687*4882a593Smuzhiyun 
688*4882a593Smuzhiyun 		/* Get current ai value from tcam */
689*4882a593Smuzhiyun 		ai_bits = mvpp2_prs_tcam_ai_get(&pe);
690*4882a593Smuzhiyun 		/* Clear double vlan bit */
691*4882a593Smuzhiyun 		ai_bits &= ~MVPP2_PRS_DBL_VLAN_AI_BIT;
692*4882a593Smuzhiyun 
693*4882a593Smuzhiyun 		if (ai != ai_bits)
694*4882a593Smuzhiyun 			continue;
695*4882a593Smuzhiyun 
696*4882a593Smuzhiyun 		if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
697*4882a593Smuzhiyun 		    ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
698*4882a593Smuzhiyun 			return tid;
699*4882a593Smuzhiyun 	}
700*4882a593Smuzhiyun 
701*4882a593Smuzhiyun 	return -ENOENT;
702*4882a593Smuzhiyun }
703*4882a593Smuzhiyun 
704*4882a593Smuzhiyun /* Add/update single/triple vlan entry */
mvpp2_prs_vlan_add(struct mvpp2 * priv,unsigned short tpid,int ai,unsigned int port_map)705*4882a593Smuzhiyun static int mvpp2_prs_vlan_add(struct mvpp2 *priv, unsigned short tpid, int ai,
706*4882a593Smuzhiyun 			      unsigned int port_map)
707*4882a593Smuzhiyun {
708*4882a593Smuzhiyun 	struct mvpp2_prs_entry pe;
709*4882a593Smuzhiyun 	int tid_aux, tid;
710*4882a593Smuzhiyun 	int ret = 0;
711*4882a593Smuzhiyun 
712*4882a593Smuzhiyun 	memset(&pe, 0, sizeof(pe));
713*4882a593Smuzhiyun 
714*4882a593Smuzhiyun 	tid = mvpp2_prs_vlan_find(priv, tpid, ai);
715*4882a593Smuzhiyun 
716*4882a593Smuzhiyun 	if (tid < 0) {
717*4882a593Smuzhiyun 		/* Create new tcam entry */
718*4882a593Smuzhiyun 		tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_LAST_FREE_TID,
719*4882a593Smuzhiyun 						MVPP2_PE_FIRST_FREE_TID);
720*4882a593Smuzhiyun 		if (tid < 0)
721*4882a593Smuzhiyun 			return tid;
722*4882a593Smuzhiyun 
723*4882a593Smuzhiyun 		/* Get last double vlan tid */
724*4882a593Smuzhiyun 		for (tid_aux = MVPP2_PE_LAST_FREE_TID;
725*4882a593Smuzhiyun 		     tid_aux >= MVPP2_PE_FIRST_FREE_TID; tid_aux--) {
726*4882a593Smuzhiyun 			unsigned int ri_bits;
727*4882a593Smuzhiyun 
728*4882a593Smuzhiyun 			if (!priv->prs_shadow[tid_aux].valid ||
729*4882a593Smuzhiyun 			    priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
730*4882a593Smuzhiyun 				continue;
731*4882a593Smuzhiyun 
732*4882a593Smuzhiyun 			mvpp2_prs_init_from_hw(priv, &pe, tid_aux);
733*4882a593Smuzhiyun 			ri_bits = mvpp2_prs_sram_ri_get(&pe);
734*4882a593Smuzhiyun 			if ((ri_bits & MVPP2_PRS_RI_VLAN_MASK) ==
735*4882a593Smuzhiyun 			    MVPP2_PRS_RI_VLAN_DOUBLE)
736*4882a593Smuzhiyun 				break;
737*4882a593Smuzhiyun 		}
738*4882a593Smuzhiyun 
739*4882a593Smuzhiyun 		if (tid <= tid_aux)
740*4882a593Smuzhiyun 			return -EINVAL;
741*4882a593Smuzhiyun 
742*4882a593Smuzhiyun 		memset(&pe, 0, sizeof(pe));
743*4882a593Smuzhiyun 		pe.index = tid;
744*4882a593Smuzhiyun 		mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
745*4882a593Smuzhiyun 
746*4882a593Smuzhiyun 		mvpp2_prs_match_etype(&pe, 0, tpid);
747*4882a593Smuzhiyun 
748*4882a593Smuzhiyun 		/* VLAN tag detected, proceed with VID filtering */
749*4882a593Smuzhiyun 		mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VID);
750*4882a593Smuzhiyun 
751*4882a593Smuzhiyun 		/* Clear all ai bits for next iteration */
752*4882a593Smuzhiyun 		mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
753*4882a593Smuzhiyun 
754*4882a593Smuzhiyun 		if (ai == MVPP2_PRS_SINGLE_VLAN_AI) {
755*4882a593Smuzhiyun 			mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_SINGLE,
756*4882a593Smuzhiyun 						 MVPP2_PRS_RI_VLAN_MASK);
757*4882a593Smuzhiyun 		} else {
758*4882a593Smuzhiyun 			ai |= MVPP2_PRS_DBL_VLAN_AI_BIT;
759*4882a593Smuzhiyun 			mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_TRIPLE,
760*4882a593Smuzhiyun 						 MVPP2_PRS_RI_VLAN_MASK);
761*4882a593Smuzhiyun 		}
762*4882a593Smuzhiyun 		mvpp2_prs_tcam_ai_update(&pe, ai, MVPP2_PRS_SRAM_AI_MASK);
763*4882a593Smuzhiyun 
764*4882a593Smuzhiyun 		mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
765*4882a593Smuzhiyun 	} else {
766*4882a593Smuzhiyun 		mvpp2_prs_init_from_hw(priv, &pe, tid);
767*4882a593Smuzhiyun 	}
768*4882a593Smuzhiyun 	/* Update ports' mask */
769*4882a593Smuzhiyun 	mvpp2_prs_tcam_port_map_set(&pe, port_map);
770*4882a593Smuzhiyun 
771*4882a593Smuzhiyun 	mvpp2_prs_hw_write(priv, &pe);
772*4882a593Smuzhiyun 
773*4882a593Smuzhiyun 	return ret;
774*4882a593Smuzhiyun }
775*4882a593Smuzhiyun 
776*4882a593Smuzhiyun /* Get first free double vlan ai number */
mvpp2_prs_double_vlan_ai_free_get(struct mvpp2 * priv)777*4882a593Smuzhiyun static int mvpp2_prs_double_vlan_ai_free_get(struct mvpp2 *priv)
778*4882a593Smuzhiyun {
779*4882a593Smuzhiyun 	int i;
780*4882a593Smuzhiyun 
781*4882a593Smuzhiyun 	for (i = 1; i < MVPP2_PRS_DBL_VLANS_MAX; i++) {
782*4882a593Smuzhiyun 		if (!priv->prs_double_vlans[i])
783*4882a593Smuzhiyun 			return i;
784*4882a593Smuzhiyun 	}
785*4882a593Smuzhiyun 
786*4882a593Smuzhiyun 	return -EINVAL;
787*4882a593Smuzhiyun }
788*4882a593Smuzhiyun 
789*4882a593Smuzhiyun /* Search for existing double vlan entry */
mvpp2_prs_double_vlan_find(struct mvpp2 * priv,unsigned short tpid1,unsigned short tpid2)790*4882a593Smuzhiyun static int mvpp2_prs_double_vlan_find(struct mvpp2 *priv, unsigned short tpid1,
791*4882a593Smuzhiyun 				      unsigned short tpid2)
792*4882a593Smuzhiyun {
793*4882a593Smuzhiyun 	struct mvpp2_prs_entry pe;
794*4882a593Smuzhiyun 	int tid;
795*4882a593Smuzhiyun 
796*4882a593Smuzhiyun 	/* Go through the all entries with MVPP2_PRS_LU_VLAN */
797*4882a593Smuzhiyun 	for (tid = MVPP2_PE_FIRST_FREE_TID;
798*4882a593Smuzhiyun 	     tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
799*4882a593Smuzhiyun 		unsigned int ri_mask;
800*4882a593Smuzhiyun 		bool match;
801*4882a593Smuzhiyun 
802*4882a593Smuzhiyun 		if (!priv->prs_shadow[tid].valid ||
803*4882a593Smuzhiyun 		    priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
804*4882a593Smuzhiyun 			continue;
805*4882a593Smuzhiyun 
806*4882a593Smuzhiyun 		mvpp2_prs_init_from_hw(priv, &pe, tid);
807*4882a593Smuzhiyun 
808*4882a593Smuzhiyun 		match = mvpp2_prs_tcam_data_cmp(&pe, 0, tpid1) &&
809*4882a593Smuzhiyun 			mvpp2_prs_tcam_data_cmp(&pe, 4, tpid2);
810*4882a593Smuzhiyun 
811*4882a593Smuzhiyun 		if (!match)
812*4882a593Smuzhiyun 			continue;
813*4882a593Smuzhiyun 
814*4882a593Smuzhiyun 		ri_mask = mvpp2_prs_sram_ri_get(&pe) & MVPP2_PRS_RI_VLAN_MASK;
815*4882a593Smuzhiyun 		if (ri_mask == MVPP2_PRS_RI_VLAN_DOUBLE)
816*4882a593Smuzhiyun 			return tid;
817*4882a593Smuzhiyun 	}
818*4882a593Smuzhiyun 
819*4882a593Smuzhiyun 	return -ENOENT;
820*4882a593Smuzhiyun }
821*4882a593Smuzhiyun 
822*4882a593Smuzhiyun /* Add or update double vlan entry */
mvpp2_prs_double_vlan_add(struct mvpp2 * priv,unsigned short tpid1,unsigned short tpid2,unsigned int port_map)823*4882a593Smuzhiyun static int mvpp2_prs_double_vlan_add(struct mvpp2 *priv, unsigned short tpid1,
824*4882a593Smuzhiyun 				     unsigned short tpid2,
825*4882a593Smuzhiyun 				     unsigned int port_map)
826*4882a593Smuzhiyun {
827*4882a593Smuzhiyun 	int tid_aux, tid, ai, ret = 0;
828*4882a593Smuzhiyun 	struct mvpp2_prs_entry pe;
829*4882a593Smuzhiyun 
830*4882a593Smuzhiyun 	memset(&pe, 0, sizeof(pe));
831*4882a593Smuzhiyun 
832*4882a593Smuzhiyun 	tid = mvpp2_prs_double_vlan_find(priv, tpid1, tpid2);
833*4882a593Smuzhiyun 
834*4882a593Smuzhiyun 	if (tid < 0) {
835*4882a593Smuzhiyun 		/* Create new tcam entry */
836*4882a593Smuzhiyun 		tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
837*4882a593Smuzhiyun 				MVPP2_PE_LAST_FREE_TID);
838*4882a593Smuzhiyun 		if (tid < 0)
839*4882a593Smuzhiyun 			return tid;
840*4882a593Smuzhiyun 
841*4882a593Smuzhiyun 		/* Set ai value for new double vlan entry */
842*4882a593Smuzhiyun 		ai = mvpp2_prs_double_vlan_ai_free_get(priv);
843*4882a593Smuzhiyun 		if (ai < 0)
844*4882a593Smuzhiyun 			return ai;
845*4882a593Smuzhiyun 
846*4882a593Smuzhiyun 		/* Get first single/triple vlan tid */
847*4882a593Smuzhiyun 		for (tid_aux = MVPP2_PE_FIRST_FREE_TID;
848*4882a593Smuzhiyun 		     tid_aux <= MVPP2_PE_LAST_FREE_TID; tid_aux++) {
849*4882a593Smuzhiyun 			unsigned int ri_bits;
850*4882a593Smuzhiyun 
851*4882a593Smuzhiyun 			if (!priv->prs_shadow[tid_aux].valid ||
852*4882a593Smuzhiyun 			    priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
853*4882a593Smuzhiyun 				continue;
854*4882a593Smuzhiyun 
855*4882a593Smuzhiyun 			mvpp2_prs_init_from_hw(priv, &pe, tid_aux);
856*4882a593Smuzhiyun 			ri_bits = mvpp2_prs_sram_ri_get(&pe);
857*4882a593Smuzhiyun 			ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
858*4882a593Smuzhiyun 			if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
859*4882a593Smuzhiyun 			    ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
860*4882a593Smuzhiyun 				break;
861*4882a593Smuzhiyun 		}
862*4882a593Smuzhiyun 
863*4882a593Smuzhiyun 		if (tid >= tid_aux)
864*4882a593Smuzhiyun 			return -ERANGE;
865*4882a593Smuzhiyun 
866*4882a593Smuzhiyun 		memset(&pe, 0, sizeof(pe));
867*4882a593Smuzhiyun 		mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
868*4882a593Smuzhiyun 		pe.index = tid;
869*4882a593Smuzhiyun 
870*4882a593Smuzhiyun 		priv->prs_double_vlans[ai] = true;
871*4882a593Smuzhiyun 
872*4882a593Smuzhiyun 		mvpp2_prs_match_etype(&pe, 0, tpid1);
873*4882a593Smuzhiyun 		mvpp2_prs_match_etype(&pe, 4, tpid2);
874*4882a593Smuzhiyun 
875*4882a593Smuzhiyun 		mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
876*4882a593Smuzhiyun 		/* Shift 4 bytes - skip outer vlan tag */
877*4882a593Smuzhiyun 		mvpp2_prs_sram_shift_set(&pe, MVPP2_VLAN_TAG_LEN,
878*4882a593Smuzhiyun 					 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
879*4882a593Smuzhiyun 		mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_DOUBLE,
880*4882a593Smuzhiyun 					 MVPP2_PRS_RI_VLAN_MASK);
881*4882a593Smuzhiyun 		mvpp2_prs_sram_ai_update(&pe, ai | MVPP2_PRS_DBL_VLAN_AI_BIT,
882*4882a593Smuzhiyun 					 MVPP2_PRS_SRAM_AI_MASK);
883*4882a593Smuzhiyun 
884*4882a593Smuzhiyun 		mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
885*4882a593Smuzhiyun 	} else {
886*4882a593Smuzhiyun 		mvpp2_prs_init_from_hw(priv, &pe, tid);
887*4882a593Smuzhiyun 	}
888*4882a593Smuzhiyun 
889*4882a593Smuzhiyun 	/* Update ports' mask */
890*4882a593Smuzhiyun 	mvpp2_prs_tcam_port_map_set(&pe, port_map);
891*4882a593Smuzhiyun 	mvpp2_prs_hw_write(priv, &pe);
892*4882a593Smuzhiyun 
893*4882a593Smuzhiyun 	return ret;
894*4882a593Smuzhiyun }
895*4882a593Smuzhiyun 
896*4882a593Smuzhiyun /* 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)897*4882a593Smuzhiyun static int mvpp2_prs_ip4_proto(struct mvpp2 *priv, unsigned short proto,
898*4882a593Smuzhiyun 			       unsigned int ri, unsigned int ri_mask)
899*4882a593Smuzhiyun {
900*4882a593Smuzhiyun 	struct mvpp2_prs_entry pe;
901*4882a593Smuzhiyun 	int tid;
902*4882a593Smuzhiyun 
903*4882a593Smuzhiyun 	if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
904*4882a593Smuzhiyun 	    (proto != IPPROTO_IGMP))
905*4882a593Smuzhiyun 		return -EINVAL;
906*4882a593Smuzhiyun 
907*4882a593Smuzhiyun 	/* Not fragmented packet */
908*4882a593Smuzhiyun 	tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
909*4882a593Smuzhiyun 					MVPP2_PE_LAST_FREE_TID);
910*4882a593Smuzhiyun 	if (tid < 0)
911*4882a593Smuzhiyun 		return tid;
912*4882a593Smuzhiyun 
913*4882a593Smuzhiyun 	memset(&pe, 0, sizeof(pe));
914*4882a593Smuzhiyun 	mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
915*4882a593Smuzhiyun 	pe.index = tid;
916*4882a593Smuzhiyun 
917*4882a593Smuzhiyun 	/* Set next lu to IPv4 */
918*4882a593Smuzhiyun 	mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
919*4882a593Smuzhiyun 	mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
920*4882a593Smuzhiyun 	/* Set L4 offset */
921*4882a593Smuzhiyun 	mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
922*4882a593Smuzhiyun 				  sizeof(struct iphdr) - 4,
923*4882a593Smuzhiyun 				  MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
924*4882a593Smuzhiyun 	mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
925*4882a593Smuzhiyun 				 MVPP2_PRS_IPV4_DIP_AI_BIT);
926*4882a593Smuzhiyun 	mvpp2_prs_sram_ri_update(&pe, ri, ri_mask | MVPP2_PRS_RI_IP_FRAG_MASK);
927*4882a593Smuzhiyun 
928*4882a593Smuzhiyun 	mvpp2_prs_tcam_data_byte_set(&pe, 2, 0x00,
929*4882a593Smuzhiyun 				     MVPP2_PRS_TCAM_PROTO_MASK_L);
930*4882a593Smuzhiyun 	mvpp2_prs_tcam_data_byte_set(&pe, 3, 0x00,
931*4882a593Smuzhiyun 				     MVPP2_PRS_TCAM_PROTO_MASK);
932*4882a593Smuzhiyun 
933*4882a593Smuzhiyun 	mvpp2_prs_tcam_data_byte_set(&pe, 5, proto, MVPP2_PRS_TCAM_PROTO_MASK);
934*4882a593Smuzhiyun 	mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
935*4882a593Smuzhiyun 	/* Unmask all ports */
936*4882a593Smuzhiyun 	mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
937*4882a593Smuzhiyun 
938*4882a593Smuzhiyun 	/* Update shadow table and hw entry */
939*4882a593Smuzhiyun 	mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
940*4882a593Smuzhiyun 	mvpp2_prs_hw_write(priv, &pe);
941*4882a593Smuzhiyun 
942*4882a593Smuzhiyun 	/* Fragmented packet */
943*4882a593Smuzhiyun 	tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
944*4882a593Smuzhiyun 					MVPP2_PE_LAST_FREE_TID);
945*4882a593Smuzhiyun 	if (tid < 0)
946*4882a593Smuzhiyun 		return tid;
947*4882a593Smuzhiyun 
948*4882a593Smuzhiyun 	pe.index = tid;
949*4882a593Smuzhiyun 	/* Clear ri before updating */
950*4882a593Smuzhiyun 	pe.sram[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
951*4882a593Smuzhiyun 	pe.sram[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
952*4882a593Smuzhiyun 	mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
953*4882a593Smuzhiyun 
954*4882a593Smuzhiyun 	mvpp2_prs_sram_ri_update(&pe, ri | MVPP2_PRS_RI_IP_FRAG_TRUE,
955*4882a593Smuzhiyun 				 ri_mask | MVPP2_PRS_RI_IP_FRAG_MASK);
956*4882a593Smuzhiyun 
957*4882a593Smuzhiyun 	mvpp2_prs_tcam_data_byte_set(&pe, 2, 0x00, 0x0);
958*4882a593Smuzhiyun 	mvpp2_prs_tcam_data_byte_set(&pe, 3, 0x00, 0x0);
959*4882a593Smuzhiyun 
960*4882a593Smuzhiyun 	/* Update shadow table and hw entry */
961*4882a593Smuzhiyun 	mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
962*4882a593Smuzhiyun 	mvpp2_prs_hw_write(priv, &pe);
963*4882a593Smuzhiyun 
964*4882a593Smuzhiyun 	return 0;
965*4882a593Smuzhiyun }
966*4882a593Smuzhiyun 
967*4882a593Smuzhiyun /* IPv4 L3 multicast or broadcast */
mvpp2_prs_ip4_cast(struct mvpp2 * priv,unsigned short l3_cast)968*4882a593Smuzhiyun static int mvpp2_prs_ip4_cast(struct mvpp2 *priv, unsigned short l3_cast)
969*4882a593Smuzhiyun {
970*4882a593Smuzhiyun 	struct mvpp2_prs_entry pe;
971*4882a593Smuzhiyun 	int mask, tid;
972*4882a593Smuzhiyun 
973*4882a593Smuzhiyun 	tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
974*4882a593Smuzhiyun 					MVPP2_PE_LAST_FREE_TID);
975*4882a593Smuzhiyun 	if (tid < 0)
976*4882a593Smuzhiyun 		return tid;
977*4882a593Smuzhiyun 
978*4882a593Smuzhiyun 	memset(&pe, 0, sizeof(pe));
979*4882a593Smuzhiyun 	mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
980*4882a593Smuzhiyun 	pe.index = tid;
981*4882a593Smuzhiyun 
982*4882a593Smuzhiyun 	switch (l3_cast) {
983*4882a593Smuzhiyun 	case MVPP2_PRS_L3_MULTI_CAST:
984*4882a593Smuzhiyun 		mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV4_MC,
985*4882a593Smuzhiyun 					     MVPP2_PRS_IPV4_MC_MASK);
986*4882a593Smuzhiyun 		mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
987*4882a593Smuzhiyun 					 MVPP2_PRS_RI_L3_ADDR_MASK);
988*4882a593Smuzhiyun 		break;
989*4882a593Smuzhiyun 	case  MVPP2_PRS_L3_BROAD_CAST:
990*4882a593Smuzhiyun 		mask = MVPP2_PRS_IPV4_BC_MASK;
991*4882a593Smuzhiyun 		mvpp2_prs_tcam_data_byte_set(&pe, 0, mask, mask);
992*4882a593Smuzhiyun 		mvpp2_prs_tcam_data_byte_set(&pe, 1, mask, mask);
993*4882a593Smuzhiyun 		mvpp2_prs_tcam_data_byte_set(&pe, 2, mask, mask);
994*4882a593Smuzhiyun 		mvpp2_prs_tcam_data_byte_set(&pe, 3, mask, mask);
995*4882a593Smuzhiyun 		mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_BCAST,
996*4882a593Smuzhiyun 					 MVPP2_PRS_RI_L3_ADDR_MASK);
997*4882a593Smuzhiyun 		break;
998*4882a593Smuzhiyun 	default:
999*4882a593Smuzhiyun 		return -EINVAL;
1000*4882a593Smuzhiyun 	}
1001*4882a593Smuzhiyun 
1002*4882a593Smuzhiyun 	/* Finished: go to flowid generation */
1003*4882a593Smuzhiyun 	mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1004*4882a593Smuzhiyun 	mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1005*4882a593Smuzhiyun 
1006*4882a593Smuzhiyun 	mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
1007*4882a593Smuzhiyun 				 MVPP2_PRS_IPV4_DIP_AI_BIT);
1008*4882a593Smuzhiyun 	/* Unmask all ports */
1009*4882a593Smuzhiyun 	mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
1010*4882a593Smuzhiyun 
1011*4882a593Smuzhiyun 	/* Update shadow table and hw entry */
1012*4882a593Smuzhiyun 	mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
1013*4882a593Smuzhiyun 	mvpp2_prs_hw_write(priv, &pe);
1014*4882a593Smuzhiyun 
1015*4882a593Smuzhiyun 	return 0;
1016*4882a593Smuzhiyun }
1017*4882a593Smuzhiyun 
1018*4882a593Smuzhiyun /* Set entries for protocols over IPv6  */
mvpp2_prs_ip6_proto(struct mvpp2 * priv,unsigned short proto,unsigned int ri,unsigned int ri_mask)1019*4882a593Smuzhiyun static int mvpp2_prs_ip6_proto(struct mvpp2 *priv, unsigned short proto,
1020*4882a593Smuzhiyun 			       unsigned int ri, unsigned int ri_mask)
1021*4882a593Smuzhiyun {
1022*4882a593Smuzhiyun 	struct mvpp2_prs_entry pe;
1023*4882a593Smuzhiyun 	int tid;
1024*4882a593Smuzhiyun 
1025*4882a593Smuzhiyun 	if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
1026*4882a593Smuzhiyun 	    (proto != IPPROTO_ICMPV6) && (proto != IPPROTO_IPIP))
1027*4882a593Smuzhiyun 		return -EINVAL;
1028*4882a593Smuzhiyun 
1029*4882a593Smuzhiyun 	tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
1030*4882a593Smuzhiyun 					MVPP2_PE_LAST_FREE_TID);
1031*4882a593Smuzhiyun 	if (tid < 0)
1032*4882a593Smuzhiyun 		return tid;
1033*4882a593Smuzhiyun 
1034*4882a593Smuzhiyun 	memset(&pe, 0, sizeof(pe));
1035*4882a593Smuzhiyun 	mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
1036*4882a593Smuzhiyun 	pe.index = tid;
1037*4882a593Smuzhiyun 
1038*4882a593Smuzhiyun 	/* Finished: go to flowid generation */
1039*4882a593Smuzhiyun 	mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1040*4882a593Smuzhiyun 	mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1041*4882a593Smuzhiyun 	mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
1042*4882a593Smuzhiyun 	mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
1043*4882a593Smuzhiyun 				  sizeof(struct ipv6hdr) - 6,
1044*4882a593Smuzhiyun 				  MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
1045*4882a593Smuzhiyun 
1046*4882a593Smuzhiyun 	mvpp2_prs_tcam_data_byte_set(&pe, 0, proto, MVPP2_PRS_TCAM_PROTO_MASK);
1047*4882a593Smuzhiyun 	mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
1048*4882a593Smuzhiyun 				 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
1049*4882a593Smuzhiyun 	/* Unmask all ports */
1050*4882a593Smuzhiyun 	mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
1051*4882a593Smuzhiyun 
1052*4882a593Smuzhiyun 	/* Write HW */
1053*4882a593Smuzhiyun 	mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
1054*4882a593Smuzhiyun 	mvpp2_prs_hw_write(priv, &pe);
1055*4882a593Smuzhiyun 
1056*4882a593Smuzhiyun 	return 0;
1057*4882a593Smuzhiyun }
1058*4882a593Smuzhiyun 
1059*4882a593Smuzhiyun /* IPv6 L3 multicast entry */
mvpp2_prs_ip6_cast(struct mvpp2 * priv,unsigned short l3_cast)1060*4882a593Smuzhiyun static int mvpp2_prs_ip6_cast(struct mvpp2 *priv, unsigned short l3_cast)
1061*4882a593Smuzhiyun {
1062*4882a593Smuzhiyun 	struct mvpp2_prs_entry pe;
1063*4882a593Smuzhiyun 	int tid;
1064*4882a593Smuzhiyun 
1065*4882a593Smuzhiyun 	if (l3_cast != MVPP2_PRS_L3_MULTI_CAST)
1066*4882a593Smuzhiyun 		return -EINVAL;
1067*4882a593Smuzhiyun 
1068*4882a593Smuzhiyun 	tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
1069*4882a593Smuzhiyun 					MVPP2_PE_LAST_FREE_TID);
1070*4882a593Smuzhiyun 	if (tid < 0)
1071*4882a593Smuzhiyun 		return tid;
1072*4882a593Smuzhiyun 
1073*4882a593Smuzhiyun 	memset(&pe, 0, sizeof(pe));
1074*4882a593Smuzhiyun 	mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
1075*4882a593Smuzhiyun 	pe.index = tid;
1076*4882a593Smuzhiyun 
1077*4882a593Smuzhiyun 	/* Finished: go to flowid generation */
1078*4882a593Smuzhiyun 	mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
1079*4882a593Smuzhiyun 	mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
1080*4882a593Smuzhiyun 				 MVPP2_PRS_RI_L3_ADDR_MASK);
1081*4882a593Smuzhiyun 	mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
1082*4882a593Smuzhiyun 				 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
1083*4882a593Smuzhiyun 	/* Shift back to IPv6 NH */
1084*4882a593Smuzhiyun 	mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1085*4882a593Smuzhiyun 
1086*4882a593Smuzhiyun 	mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV6_MC,
1087*4882a593Smuzhiyun 				     MVPP2_PRS_IPV6_MC_MASK);
1088*4882a593Smuzhiyun 	mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
1089*4882a593Smuzhiyun 	/* Unmask all ports */
1090*4882a593Smuzhiyun 	mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
1091*4882a593Smuzhiyun 
1092*4882a593Smuzhiyun 	/* Update shadow table and hw entry */
1093*4882a593Smuzhiyun 	mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
1094*4882a593Smuzhiyun 	mvpp2_prs_hw_write(priv, &pe);
1095*4882a593Smuzhiyun 
1096*4882a593Smuzhiyun 	return 0;
1097*4882a593Smuzhiyun }
1098*4882a593Smuzhiyun 
1099*4882a593Smuzhiyun /* Parser per-port initialization */
mvpp2_prs_hw_port_init(struct mvpp2 * priv,int port,int lu_first,int lu_max,int offset)1100*4882a593Smuzhiyun static void mvpp2_prs_hw_port_init(struct mvpp2 *priv, int port, int lu_first,
1101*4882a593Smuzhiyun 				   int lu_max, int offset)
1102*4882a593Smuzhiyun {
1103*4882a593Smuzhiyun 	u32 val;
1104*4882a593Smuzhiyun 
1105*4882a593Smuzhiyun 	/* Set lookup ID */
1106*4882a593Smuzhiyun 	val = mvpp2_read(priv, MVPP2_PRS_INIT_LOOKUP_REG);
1107*4882a593Smuzhiyun 	val &= ~MVPP2_PRS_PORT_LU_MASK(port);
1108*4882a593Smuzhiyun 	val |=  MVPP2_PRS_PORT_LU_VAL(port, lu_first);
1109*4882a593Smuzhiyun 	mvpp2_write(priv, MVPP2_PRS_INIT_LOOKUP_REG, val);
1110*4882a593Smuzhiyun 
1111*4882a593Smuzhiyun 	/* Set maximum number of loops for packet received from port */
1112*4882a593Smuzhiyun 	val = mvpp2_read(priv, MVPP2_PRS_MAX_LOOP_REG(port));
1113*4882a593Smuzhiyun 	val &= ~MVPP2_PRS_MAX_LOOP_MASK(port);
1114*4882a593Smuzhiyun 	val |= MVPP2_PRS_MAX_LOOP_VAL(port, lu_max);
1115*4882a593Smuzhiyun 	mvpp2_write(priv, MVPP2_PRS_MAX_LOOP_REG(port), val);
1116*4882a593Smuzhiyun 
1117*4882a593Smuzhiyun 	/* Set initial offset for packet header extraction for the first
1118*4882a593Smuzhiyun 	 * searching loop
1119*4882a593Smuzhiyun 	 */
1120*4882a593Smuzhiyun 	val = mvpp2_read(priv, MVPP2_PRS_INIT_OFFS_REG(port));
1121*4882a593Smuzhiyun 	val &= ~MVPP2_PRS_INIT_OFF_MASK(port);
1122*4882a593Smuzhiyun 	val |= MVPP2_PRS_INIT_OFF_VAL(port, offset);
1123*4882a593Smuzhiyun 	mvpp2_write(priv, MVPP2_PRS_INIT_OFFS_REG(port), val);
1124*4882a593Smuzhiyun }
1125*4882a593Smuzhiyun 
1126*4882a593Smuzhiyun /* Default flow entries initialization for all ports */
mvpp2_prs_def_flow_init(struct mvpp2 * priv)1127*4882a593Smuzhiyun static void mvpp2_prs_def_flow_init(struct mvpp2 *priv)
1128*4882a593Smuzhiyun {
1129*4882a593Smuzhiyun 	struct mvpp2_prs_entry pe;
1130*4882a593Smuzhiyun 	int port;
1131*4882a593Smuzhiyun 
1132*4882a593Smuzhiyun 	for (port = 0; port < MVPP2_MAX_PORTS; port++) {
1133*4882a593Smuzhiyun 		memset(&pe, 0, sizeof(pe));
1134*4882a593Smuzhiyun 		mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1135*4882a593Smuzhiyun 		pe.index = MVPP2_PE_FIRST_DEFAULT_FLOW - port;
1136*4882a593Smuzhiyun 
1137*4882a593Smuzhiyun 		/* Mask all ports */
1138*4882a593Smuzhiyun 		mvpp2_prs_tcam_port_map_set(&pe, 0);
1139*4882a593Smuzhiyun 
1140*4882a593Smuzhiyun 		/* Set flow ID*/
1141*4882a593Smuzhiyun 		mvpp2_prs_sram_ai_update(&pe, port, MVPP2_PRS_FLOW_ID_MASK);
1142*4882a593Smuzhiyun 		mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
1143*4882a593Smuzhiyun 
1144*4882a593Smuzhiyun 		/* Update shadow table and hw entry */
1145*4882a593Smuzhiyun 		mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_FLOWS);
1146*4882a593Smuzhiyun 		mvpp2_prs_hw_write(priv, &pe);
1147*4882a593Smuzhiyun 	}
1148*4882a593Smuzhiyun }
1149*4882a593Smuzhiyun 
1150*4882a593Smuzhiyun /* Set default entry for Marvell Header field */
mvpp2_prs_mh_init(struct mvpp2 * priv)1151*4882a593Smuzhiyun static void mvpp2_prs_mh_init(struct mvpp2 *priv)
1152*4882a593Smuzhiyun {
1153*4882a593Smuzhiyun 	struct mvpp2_prs_entry pe;
1154*4882a593Smuzhiyun 
1155*4882a593Smuzhiyun 	memset(&pe, 0, sizeof(pe));
1156*4882a593Smuzhiyun 
1157*4882a593Smuzhiyun 	pe.index = MVPP2_PE_MH_DEFAULT;
1158*4882a593Smuzhiyun 	mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MH);
1159*4882a593Smuzhiyun 	mvpp2_prs_sram_shift_set(&pe, MVPP2_MH_SIZE,
1160*4882a593Smuzhiyun 				 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1161*4882a593Smuzhiyun 	mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_MAC);
1162*4882a593Smuzhiyun 
1163*4882a593Smuzhiyun 	/* Unmask all ports */
1164*4882a593Smuzhiyun 	mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
1165*4882a593Smuzhiyun 
1166*4882a593Smuzhiyun 	/* Update shadow table and hw entry */
1167*4882a593Smuzhiyun 	mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MH);
1168*4882a593Smuzhiyun 	mvpp2_prs_hw_write(priv, &pe);
1169*4882a593Smuzhiyun }
1170*4882a593Smuzhiyun 
1171*4882a593Smuzhiyun /* Set default entires (place holder) for promiscuous, non-promiscuous and
1172*4882a593Smuzhiyun  * multicast MAC addresses
1173*4882a593Smuzhiyun  */
mvpp2_prs_mac_init(struct mvpp2 * priv)1174*4882a593Smuzhiyun static void mvpp2_prs_mac_init(struct mvpp2 *priv)
1175*4882a593Smuzhiyun {
1176*4882a593Smuzhiyun 	struct mvpp2_prs_entry pe;
1177*4882a593Smuzhiyun 
1178*4882a593Smuzhiyun 	memset(&pe, 0, sizeof(pe));
1179*4882a593Smuzhiyun 
1180*4882a593Smuzhiyun 	/* Non-promiscuous mode for all ports - DROP unknown packets */
1181*4882a593Smuzhiyun 	pe.index = MVPP2_PE_MAC_NON_PROMISCUOUS;
1182*4882a593Smuzhiyun 	mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1183*4882a593Smuzhiyun 
1184*4882a593Smuzhiyun 	mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
1185*4882a593Smuzhiyun 				 MVPP2_PRS_RI_DROP_MASK);
1186*4882a593Smuzhiyun 	mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1187*4882a593Smuzhiyun 	mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1188*4882a593Smuzhiyun 
1189*4882a593Smuzhiyun 	/* Unmask all ports */
1190*4882a593Smuzhiyun 	mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
1191*4882a593Smuzhiyun 
1192*4882a593Smuzhiyun 	/* Update shadow table and hw entry */
1193*4882a593Smuzhiyun 	mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1194*4882a593Smuzhiyun 	mvpp2_prs_hw_write(priv, &pe);
1195*4882a593Smuzhiyun 
1196*4882a593Smuzhiyun 	/* Create dummy entries for drop all and promiscuous modes */
1197*4882a593Smuzhiyun 	mvpp2_prs_drop_fc(priv);
1198*4882a593Smuzhiyun 	mvpp2_prs_mac_drop_all_set(priv, 0, false);
1199*4882a593Smuzhiyun 	mvpp2_prs_mac_promisc_set(priv, 0, MVPP2_PRS_L2_UNI_CAST, false);
1200*4882a593Smuzhiyun 	mvpp2_prs_mac_promisc_set(priv, 0, MVPP2_PRS_L2_MULTI_CAST, false);
1201*4882a593Smuzhiyun }
1202*4882a593Smuzhiyun 
1203*4882a593Smuzhiyun /* Set default entries for various types of dsa packets */
mvpp2_prs_dsa_init(struct mvpp2 * priv)1204*4882a593Smuzhiyun static void mvpp2_prs_dsa_init(struct mvpp2 *priv)
1205*4882a593Smuzhiyun {
1206*4882a593Smuzhiyun 	struct mvpp2_prs_entry pe;
1207*4882a593Smuzhiyun 
1208*4882a593Smuzhiyun 	/* None tagged EDSA entry - place holder */
1209*4882a593Smuzhiyun 	mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
1210*4882a593Smuzhiyun 			      MVPP2_PRS_EDSA);
1211*4882a593Smuzhiyun 
1212*4882a593Smuzhiyun 	/* Tagged EDSA entry - place holder */
1213*4882a593Smuzhiyun 	mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
1214*4882a593Smuzhiyun 
1215*4882a593Smuzhiyun 	/* None tagged DSA entry - place holder */
1216*4882a593Smuzhiyun 	mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
1217*4882a593Smuzhiyun 			      MVPP2_PRS_DSA);
1218*4882a593Smuzhiyun 
1219*4882a593Smuzhiyun 	/* Tagged DSA entry - place holder */
1220*4882a593Smuzhiyun 	mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
1221*4882a593Smuzhiyun 
1222*4882a593Smuzhiyun 	/* None tagged EDSA ethertype entry - place holder*/
1223*4882a593Smuzhiyun 	mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
1224*4882a593Smuzhiyun 					MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
1225*4882a593Smuzhiyun 
1226*4882a593Smuzhiyun 	/* Tagged EDSA ethertype entry - place holder*/
1227*4882a593Smuzhiyun 	mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
1228*4882a593Smuzhiyun 					MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
1229*4882a593Smuzhiyun 
1230*4882a593Smuzhiyun 	/* None tagged DSA ethertype entry */
1231*4882a593Smuzhiyun 	mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
1232*4882a593Smuzhiyun 					MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
1233*4882a593Smuzhiyun 
1234*4882a593Smuzhiyun 	/* Tagged DSA ethertype entry */
1235*4882a593Smuzhiyun 	mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
1236*4882a593Smuzhiyun 					MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
1237*4882a593Smuzhiyun 
1238*4882a593Smuzhiyun 	/* Set default entry, in case DSA or EDSA tag not found */
1239*4882a593Smuzhiyun 	memset(&pe, 0, sizeof(pe));
1240*4882a593Smuzhiyun 	mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
1241*4882a593Smuzhiyun 	pe.index = MVPP2_PE_DSA_DEFAULT;
1242*4882a593Smuzhiyun 	mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
1243*4882a593Smuzhiyun 
1244*4882a593Smuzhiyun 	/* Shift 0 bytes */
1245*4882a593Smuzhiyun 	mvpp2_prs_sram_shift_set(&pe, 0, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1246*4882a593Smuzhiyun 	mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1247*4882a593Smuzhiyun 
1248*4882a593Smuzhiyun 	/* Clear all sram ai bits for next iteration */
1249*4882a593Smuzhiyun 	mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
1250*4882a593Smuzhiyun 
1251*4882a593Smuzhiyun 	/* Unmask all ports */
1252*4882a593Smuzhiyun 	mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
1253*4882a593Smuzhiyun 
1254*4882a593Smuzhiyun 	mvpp2_prs_hw_write(priv, &pe);
1255*4882a593Smuzhiyun }
1256*4882a593Smuzhiyun 
1257*4882a593Smuzhiyun /* Initialize parser entries for VID filtering */
mvpp2_prs_vid_init(struct mvpp2 * priv)1258*4882a593Smuzhiyun static void mvpp2_prs_vid_init(struct mvpp2 *priv)
1259*4882a593Smuzhiyun {
1260*4882a593Smuzhiyun 	struct mvpp2_prs_entry pe;
1261*4882a593Smuzhiyun 
1262*4882a593Smuzhiyun 	memset(&pe, 0, sizeof(pe));
1263*4882a593Smuzhiyun 
1264*4882a593Smuzhiyun 	/* Set default vid entry */
1265*4882a593Smuzhiyun 	pe.index = MVPP2_PE_VID_FLTR_DEFAULT;
1266*4882a593Smuzhiyun 	mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VID);
1267*4882a593Smuzhiyun 
1268*4882a593Smuzhiyun 	mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_EDSA_VID_AI_BIT);
1269*4882a593Smuzhiyun 
1270*4882a593Smuzhiyun 	/* Skip VLAN header - Set offset to 4 bytes */
1271*4882a593Smuzhiyun 	mvpp2_prs_sram_shift_set(&pe, MVPP2_VLAN_TAG_LEN,
1272*4882a593Smuzhiyun 				 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1273*4882a593Smuzhiyun 
1274*4882a593Smuzhiyun 	/* Clear all ai bits for next iteration */
1275*4882a593Smuzhiyun 	mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
1276*4882a593Smuzhiyun 
1277*4882a593Smuzhiyun 	mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
1278*4882a593Smuzhiyun 
1279*4882a593Smuzhiyun 	/* Unmask all ports */
1280*4882a593Smuzhiyun 	mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
1281*4882a593Smuzhiyun 
1282*4882a593Smuzhiyun 	/* Update shadow table and hw entry */
1283*4882a593Smuzhiyun 	mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VID);
1284*4882a593Smuzhiyun 	mvpp2_prs_hw_write(priv, &pe);
1285*4882a593Smuzhiyun 
1286*4882a593Smuzhiyun 	/* Set default vid entry for extended DSA*/
1287*4882a593Smuzhiyun 	memset(&pe, 0, sizeof(pe));
1288*4882a593Smuzhiyun 
1289*4882a593Smuzhiyun 	/* Set default vid entry */
1290*4882a593Smuzhiyun 	pe.index = MVPP2_PE_VID_EDSA_FLTR_DEFAULT;
1291*4882a593Smuzhiyun 	mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VID);
1292*4882a593Smuzhiyun 
1293*4882a593Smuzhiyun 	mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_EDSA_VID_AI_BIT,
1294*4882a593Smuzhiyun 				 MVPP2_PRS_EDSA_VID_AI_BIT);
1295*4882a593Smuzhiyun 
1296*4882a593Smuzhiyun 	/* Skip VLAN header - Set offset to 8 bytes */
1297*4882a593Smuzhiyun 	mvpp2_prs_sram_shift_set(&pe, MVPP2_VLAN_TAG_EDSA_LEN,
1298*4882a593Smuzhiyun 				 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1299*4882a593Smuzhiyun 
1300*4882a593Smuzhiyun 	/* Clear all ai bits for next iteration */
1301*4882a593Smuzhiyun 	mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
1302*4882a593Smuzhiyun 
1303*4882a593Smuzhiyun 	mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
1304*4882a593Smuzhiyun 
1305*4882a593Smuzhiyun 	/* Unmask all ports */
1306*4882a593Smuzhiyun 	mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
1307*4882a593Smuzhiyun 
1308*4882a593Smuzhiyun 	/* Update shadow table and hw entry */
1309*4882a593Smuzhiyun 	mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VID);
1310*4882a593Smuzhiyun 	mvpp2_prs_hw_write(priv, &pe);
1311*4882a593Smuzhiyun }
1312*4882a593Smuzhiyun 
1313*4882a593Smuzhiyun /* Match basic ethertypes */
mvpp2_prs_etype_init(struct mvpp2 * priv)1314*4882a593Smuzhiyun static int mvpp2_prs_etype_init(struct mvpp2 *priv)
1315*4882a593Smuzhiyun {
1316*4882a593Smuzhiyun 	struct mvpp2_prs_entry pe;
1317*4882a593Smuzhiyun 	int tid;
1318*4882a593Smuzhiyun 
1319*4882a593Smuzhiyun 	/* Ethertype: PPPoE */
1320*4882a593Smuzhiyun 	tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
1321*4882a593Smuzhiyun 					MVPP2_PE_LAST_FREE_TID);
1322*4882a593Smuzhiyun 	if (tid < 0)
1323*4882a593Smuzhiyun 		return tid;
1324*4882a593Smuzhiyun 
1325*4882a593Smuzhiyun 	memset(&pe, 0, sizeof(pe));
1326*4882a593Smuzhiyun 	mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
1327*4882a593Smuzhiyun 	pe.index = tid;
1328*4882a593Smuzhiyun 
1329*4882a593Smuzhiyun 	mvpp2_prs_match_etype(&pe, 0, ETH_P_PPP_SES);
1330*4882a593Smuzhiyun 
1331*4882a593Smuzhiyun 	mvpp2_prs_sram_shift_set(&pe, MVPP2_PPPOE_HDR_SIZE,
1332*4882a593Smuzhiyun 				 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1333*4882a593Smuzhiyun 	mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
1334*4882a593Smuzhiyun 	mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_PPPOE_MASK,
1335*4882a593Smuzhiyun 				 MVPP2_PRS_RI_PPPOE_MASK);
1336*4882a593Smuzhiyun 
1337*4882a593Smuzhiyun 	/* Update shadow table and hw entry */
1338*4882a593Smuzhiyun 	mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
1339*4882a593Smuzhiyun 	priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
1340*4882a593Smuzhiyun 	priv->prs_shadow[pe.index].finish = false;
1341*4882a593Smuzhiyun 	mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_PPPOE_MASK,
1342*4882a593Smuzhiyun 				MVPP2_PRS_RI_PPPOE_MASK);
1343*4882a593Smuzhiyun 	mvpp2_prs_hw_write(priv, &pe);
1344*4882a593Smuzhiyun 
1345*4882a593Smuzhiyun 	/* Ethertype: ARP */
1346*4882a593Smuzhiyun 	tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
1347*4882a593Smuzhiyun 					MVPP2_PE_LAST_FREE_TID);
1348*4882a593Smuzhiyun 	if (tid < 0)
1349*4882a593Smuzhiyun 		return tid;
1350*4882a593Smuzhiyun 
1351*4882a593Smuzhiyun 	memset(&pe, 0, sizeof(pe));
1352*4882a593Smuzhiyun 	mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
1353*4882a593Smuzhiyun 	pe.index = tid;
1354*4882a593Smuzhiyun 
1355*4882a593Smuzhiyun 	mvpp2_prs_match_etype(&pe, 0, ETH_P_ARP);
1356*4882a593Smuzhiyun 
1357*4882a593Smuzhiyun 	/* Generate flow in the next iteration*/
1358*4882a593Smuzhiyun 	mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1359*4882a593Smuzhiyun 	mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1360*4882a593Smuzhiyun 	mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_ARP,
1361*4882a593Smuzhiyun 				 MVPP2_PRS_RI_L3_PROTO_MASK);
1362*4882a593Smuzhiyun 	/* Set L3 offset */
1363*4882a593Smuzhiyun 	mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
1364*4882a593Smuzhiyun 				  MVPP2_ETH_TYPE_LEN,
1365*4882a593Smuzhiyun 				  MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
1366*4882a593Smuzhiyun 
1367*4882a593Smuzhiyun 	/* Update shadow table and hw entry */
1368*4882a593Smuzhiyun 	mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
1369*4882a593Smuzhiyun 	priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
1370*4882a593Smuzhiyun 	priv->prs_shadow[pe.index].finish = true;
1371*4882a593Smuzhiyun 	mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_ARP,
1372*4882a593Smuzhiyun 				MVPP2_PRS_RI_L3_PROTO_MASK);
1373*4882a593Smuzhiyun 	mvpp2_prs_hw_write(priv, &pe);
1374*4882a593Smuzhiyun 
1375*4882a593Smuzhiyun 	/* Ethertype: LBTD */
1376*4882a593Smuzhiyun 	tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
1377*4882a593Smuzhiyun 					MVPP2_PE_LAST_FREE_TID);
1378*4882a593Smuzhiyun 	if (tid < 0)
1379*4882a593Smuzhiyun 		return tid;
1380*4882a593Smuzhiyun 
1381*4882a593Smuzhiyun 	memset(&pe, 0, sizeof(pe));
1382*4882a593Smuzhiyun 	mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
1383*4882a593Smuzhiyun 	pe.index = tid;
1384*4882a593Smuzhiyun 
1385*4882a593Smuzhiyun 	mvpp2_prs_match_etype(&pe, 0, MVPP2_IP_LBDT_TYPE);
1386*4882a593Smuzhiyun 
1387*4882a593Smuzhiyun 	/* Generate flow in the next iteration*/
1388*4882a593Smuzhiyun 	mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1389*4882a593Smuzhiyun 	mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1390*4882a593Smuzhiyun 	mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
1391*4882a593Smuzhiyun 				 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
1392*4882a593Smuzhiyun 				 MVPP2_PRS_RI_CPU_CODE_MASK |
1393*4882a593Smuzhiyun 				 MVPP2_PRS_RI_UDF3_MASK);
1394*4882a593Smuzhiyun 	/* Set L3 offset */
1395*4882a593Smuzhiyun 	mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
1396*4882a593Smuzhiyun 				  MVPP2_ETH_TYPE_LEN,
1397*4882a593Smuzhiyun 				  MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
1398*4882a593Smuzhiyun 
1399*4882a593Smuzhiyun 	/* Update shadow table and hw entry */
1400*4882a593Smuzhiyun 	mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
1401*4882a593Smuzhiyun 	priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
1402*4882a593Smuzhiyun 	priv->prs_shadow[pe.index].finish = true;
1403*4882a593Smuzhiyun 	mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
1404*4882a593Smuzhiyun 				MVPP2_PRS_RI_UDF3_RX_SPECIAL,
1405*4882a593Smuzhiyun 				MVPP2_PRS_RI_CPU_CODE_MASK |
1406*4882a593Smuzhiyun 				MVPP2_PRS_RI_UDF3_MASK);
1407*4882a593Smuzhiyun 	mvpp2_prs_hw_write(priv, &pe);
1408*4882a593Smuzhiyun 
1409*4882a593Smuzhiyun 	/* Ethertype: IPv4 without options */
1410*4882a593Smuzhiyun 	tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
1411*4882a593Smuzhiyun 					MVPP2_PE_LAST_FREE_TID);
1412*4882a593Smuzhiyun 	if (tid < 0)
1413*4882a593Smuzhiyun 		return tid;
1414*4882a593Smuzhiyun 
1415*4882a593Smuzhiyun 	memset(&pe, 0, sizeof(pe));
1416*4882a593Smuzhiyun 	mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
1417*4882a593Smuzhiyun 	pe.index = tid;
1418*4882a593Smuzhiyun 
1419*4882a593Smuzhiyun 	mvpp2_prs_match_etype(&pe, 0, ETH_P_IP);
1420*4882a593Smuzhiyun 	mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
1421*4882a593Smuzhiyun 				     MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
1422*4882a593Smuzhiyun 				     MVPP2_PRS_IPV4_HEAD_MASK |
1423*4882a593Smuzhiyun 				     MVPP2_PRS_IPV4_IHL_MASK);
1424*4882a593Smuzhiyun 
1425*4882a593Smuzhiyun 	mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
1426*4882a593Smuzhiyun 	mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
1427*4882a593Smuzhiyun 				 MVPP2_PRS_RI_L3_PROTO_MASK);
1428*4882a593Smuzhiyun 	/* Skip eth_type + 4 bytes of IP header */
1429*4882a593Smuzhiyun 	mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
1430*4882a593Smuzhiyun 				 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1431*4882a593Smuzhiyun 	/* Set L3 offset */
1432*4882a593Smuzhiyun 	mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
1433*4882a593Smuzhiyun 				  MVPP2_ETH_TYPE_LEN,
1434*4882a593Smuzhiyun 				  MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
1435*4882a593Smuzhiyun 
1436*4882a593Smuzhiyun 	/* Update shadow table and hw entry */
1437*4882a593Smuzhiyun 	mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
1438*4882a593Smuzhiyun 	priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
1439*4882a593Smuzhiyun 	priv->prs_shadow[pe.index].finish = false;
1440*4882a593Smuzhiyun 	mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4,
1441*4882a593Smuzhiyun 				MVPP2_PRS_RI_L3_PROTO_MASK);
1442*4882a593Smuzhiyun 	mvpp2_prs_hw_write(priv, &pe);
1443*4882a593Smuzhiyun 
1444*4882a593Smuzhiyun 	/* Ethertype: IPv4 with options */
1445*4882a593Smuzhiyun 	tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
1446*4882a593Smuzhiyun 					MVPP2_PE_LAST_FREE_TID);
1447*4882a593Smuzhiyun 	if (tid < 0)
1448*4882a593Smuzhiyun 		return tid;
1449*4882a593Smuzhiyun 
1450*4882a593Smuzhiyun 	pe.index = tid;
1451*4882a593Smuzhiyun 
1452*4882a593Smuzhiyun 	mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
1453*4882a593Smuzhiyun 				     MVPP2_PRS_IPV4_HEAD,
1454*4882a593Smuzhiyun 				     MVPP2_PRS_IPV4_HEAD_MASK);
1455*4882a593Smuzhiyun 
1456*4882a593Smuzhiyun 	/* Clear ri before updating */
1457*4882a593Smuzhiyun 	pe.sram[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
1458*4882a593Smuzhiyun 	pe.sram[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
1459*4882a593Smuzhiyun 	mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
1460*4882a593Smuzhiyun 				 MVPP2_PRS_RI_L3_PROTO_MASK);
1461*4882a593Smuzhiyun 
1462*4882a593Smuzhiyun 	/* Update shadow table and hw entry */
1463*4882a593Smuzhiyun 	mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
1464*4882a593Smuzhiyun 	priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
1465*4882a593Smuzhiyun 	priv->prs_shadow[pe.index].finish = false;
1466*4882a593Smuzhiyun 	mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4_OPT,
1467*4882a593Smuzhiyun 				MVPP2_PRS_RI_L3_PROTO_MASK);
1468*4882a593Smuzhiyun 	mvpp2_prs_hw_write(priv, &pe);
1469*4882a593Smuzhiyun 
1470*4882a593Smuzhiyun 	/* Ethertype: IPv6 without options */
1471*4882a593Smuzhiyun 	tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
1472*4882a593Smuzhiyun 					MVPP2_PE_LAST_FREE_TID);
1473*4882a593Smuzhiyun 	if (tid < 0)
1474*4882a593Smuzhiyun 		return tid;
1475*4882a593Smuzhiyun 
1476*4882a593Smuzhiyun 	memset(&pe, 0, sizeof(pe));
1477*4882a593Smuzhiyun 	mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
1478*4882a593Smuzhiyun 	pe.index = tid;
1479*4882a593Smuzhiyun 
1480*4882a593Smuzhiyun 	mvpp2_prs_match_etype(&pe, 0, ETH_P_IPV6);
1481*4882a593Smuzhiyun 
1482*4882a593Smuzhiyun 	/* Skip DIP of IPV6 header */
1483*4882a593Smuzhiyun 	mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 8 +
1484*4882a593Smuzhiyun 				 MVPP2_MAX_L3_ADDR_SIZE,
1485*4882a593Smuzhiyun 				 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1486*4882a593Smuzhiyun 	mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
1487*4882a593Smuzhiyun 	mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
1488*4882a593Smuzhiyun 				 MVPP2_PRS_RI_L3_PROTO_MASK);
1489*4882a593Smuzhiyun 	/* Set L3 offset */
1490*4882a593Smuzhiyun 	mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
1491*4882a593Smuzhiyun 				  MVPP2_ETH_TYPE_LEN,
1492*4882a593Smuzhiyun 				  MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
1493*4882a593Smuzhiyun 
1494*4882a593Smuzhiyun 	mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
1495*4882a593Smuzhiyun 	priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
1496*4882a593Smuzhiyun 	priv->prs_shadow[pe.index].finish = false;
1497*4882a593Smuzhiyun 	mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP6,
1498*4882a593Smuzhiyun 				MVPP2_PRS_RI_L3_PROTO_MASK);
1499*4882a593Smuzhiyun 	mvpp2_prs_hw_write(priv, &pe);
1500*4882a593Smuzhiyun 
1501*4882a593Smuzhiyun 	/* Default entry for MVPP2_PRS_LU_L2 - Unknown ethtype */
1502*4882a593Smuzhiyun 	memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1503*4882a593Smuzhiyun 	mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
1504*4882a593Smuzhiyun 	pe.index = MVPP2_PE_ETH_TYPE_UN;
1505*4882a593Smuzhiyun 
1506*4882a593Smuzhiyun 	/* Unmask all ports */
1507*4882a593Smuzhiyun 	mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
1508*4882a593Smuzhiyun 
1509*4882a593Smuzhiyun 	/* Generate flow in the next iteration*/
1510*4882a593Smuzhiyun 	mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1511*4882a593Smuzhiyun 	mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1512*4882a593Smuzhiyun 	mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
1513*4882a593Smuzhiyun 				 MVPP2_PRS_RI_L3_PROTO_MASK);
1514*4882a593Smuzhiyun 	/* Set L3 offset even it's unknown L3 */
1515*4882a593Smuzhiyun 	mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
1516*4882a593Smuzhiyun 				  MVPP2_ETH_TYPE_LEN,
1517*4882a593Smuzhiyun 				  MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
1518*4882a593Smuzhiyun 
1519*4882a593Smuzhiyun 	/* Update shadow table and hw entry */
1520*4882a593Smuzhiyun 	mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
1521*4882a593Smuzhiyun 	priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
1522*4882a593Smuzhiyun 	priv->prs_shadow[pe.index].finish = true;
1523*4882a593Smuzhiyun 	mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_UN,
1524*4882a593Smuzhiyun 				MVPP2_PRS_RI_L3_PROTO_MASK);
1525*4882a593Smuzhiyun 	mvpp2_prs_hw_write(priv, &pe);
1526*4882a593Smuzhiyun 
1527*4882a593Smuzhiyun 	return 0;
1528*4882a593Smuzhiyun }
1529*4882a593Smuzhiyun 
1530*4882a593Smuzhiyun /* Configure vlan entries and detect up to 2 successive VLAN tags.
1531*4882a593Smuzhiyun  * Possible options:
1532*4882a593Smuzhiyun  * 0x8100, 0x88A8
1533*4882a593Smuzhiyun  * 0x8100, 0x8100
1534*4882a593Smuzhiyun  * 0x8100
1535*4882a593Smuzhiyun  * 0x88A8
1536*4882a593Smuzhiyun  */
mvpp2_prs_vlan_init(struct platform_device * pdev,struct mvpp2 * priv)1537*4882a593Smuzhiyun static int mvpp2_prs_vlan_init(struct platform_device *pdev, struct mvpp2 *priv)
1538*4882a593Smuzhiyun {
1539*4882a593Smuzhiyun 	struct mvpp2_prs_entry pe;
1540*4882a593Smuzhiyun 	int err;
1541*4882a593Smuzhiyun 
1542*4882a593Smuzhiyun 	priv->prs_double_vlans = devm_kcalloc(&pdev->dev, sizeof(bool),
1543*4882a593Smuzhiyun 					      MVPP2_PRS_DBL_VLANS_MAX,
1544*4882a593Smuzhiyun 					      GFP_KERNEL);
1545*4882a593Smuzhiyun 	if (!priv->prs_double_vlans)
1546*4882a593Smuzhiyun 		return -ENOMEM;
1547*4882a593Smuzhiyun 
1548*4882a593Smuzhiyun 	/* Double VLAN: 0x8100, 0x88A8 */
1549*4882a593Smuzhiyun 	err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021AD,
1550*4882a593Smuzhiyun 					MVPP2_PRS_PORT_MASK);
1551*4882a593Smuzhiyun 	if (err)
1552*4882a593Smuzhiyun 		return err;
1553*4882a593Smuzhiyun 
1554*4882a593Smuzhiyun 	/* Double VLAN: 0x8100, 0x8100 */
1555*4882a593Smuzhiyun 	err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021Q,
1556*4882a593Smuzhiyun 					MVPP2_PRS_PORT_MASK);
1557*4882a593Smuzhiyun 	if (err)
1558*4882a593Smuzhiyun 		return err;
1559*4882a593Smuzhiyun 
1560*4882a593Smuzhiyun 	/* Single VLAN: 0x88a8 */
1561*4882a593Smuzhiyun 	err = mvpp2_prs_vlan_add(priv, ETH_P_8021AD, MVPP2_PRS_SINGLE_VLAN_AI,
1562*4882a593Smuzhiyun 				 MVPP2_PRS_PORT_MASK);
1563*4882a593Smuzhiyun 	if (err)
1564*4882a593Smuzhiyun 		return err;
1565*4882a593Smuzhiyun 
1566*4882a593Smuzhiyun 	/* Single VLAN: 0x8100 */
1567*4882a593Smuzhiyun 	err = mvpp2_prs_vlan_add(priv, ETH_P_8021Q, MVPP2_PRS_SINGLE_VLAN_AI,
1568*4882a593Smuzhiyun 				 MVPP2_PRS_PORT_MASK);
1569*4882a593Smuzhiyun 	if (err)
1570*4882a593Smuzhiyun 		return err;
1571*4882a593Smuzhiyun 
1572*4882a593Smuzhiyun 	/* Set default double vlan entry */
1573*4882a593Smuzhiyun 	memset(&pe, 0, sizeof(pe));
1574*4882a593Smuzhiyun 	mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
1575*4882a593Smuzhiyun 	pe.index = MVPP2_PE_VLAN_DBL;
1576*4882a593Smuzhiyun 
1577*4882a593Smuzhiyun 	mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VID);
1578*4882a593Smuzhiyun 
1579*4882a593Smuzhiyun 	/* Clear ai for next iterations */
1580*4882a593Smuzhiyun 	mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
1581*4882a593Smuzhiyun 	mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_DOUBLE,
1582*4882a593Smuzhiyun 				 MVPP2_PRS_RI_VLAN_MASK);
1583*4882a593Smuzhiyun 
1584*4882a593Smuzhiyun 	mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_DBL_VLAN_AI_BIT,
1585*4882a593Smuzhiyun 				 MVPP2_PRS_DBL_VLAN_AI_BIT);
1586*4882a593Smuzhiyun 	/* Unmask all ports */
1587*4882a593Smuzhiyun 	mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
1588*4882a593Smuzhiyun 
1589*4882a593Smuzhiyun 	/* Update shadow table and hw entry */
1590*4882a593Smuzhiyun 	mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
1591*4882a593Smuzhiyun 	mvpp2_prs_hw_write(priv, &pe);
1592*4882a593Smuzhiyun 
1593*4882a593Smuzhiyun 	/* Set default vlan none entry */
1594*4882a593Smuzhiyun 	memset(&pe, 0, sizeof(pe));
1595*4882a593Smuzhiyun 	mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
1596*4882a593Smuzhiyun 	pe.index = MVPP2_PE_VLAN_NONE;
1597*4882a593Smuzhiyun 
1598*4882a593Smuzhiyun 	mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
1599*4882a593Smuzhiyun 	mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
1600*4882a593Smuzhiyun 				 MVPP2_PRS_RI_VLAN_MASK);
1601*4882a593Smuzhiyun 
1602*4882a593Smuzhiyun 	/* Unmask all ports */
1603*4882a593Smuzhiyun 	mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
1604*4882a593Smuzhiyun 
1605*4882a593Smuzhiyun 	/* Update shadow table and hw entry */
1606*4882a593Smuzhiyun 	mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
1607*4882a593Smuzhiyun 	mvpp2_prs_hw_write(priv, &pe);
1608*4882a593Smuzhiyun 
1609*4882a593Smuzhiyun 	return 0;
1610*4882a593Smuzhiyun }
1611*4882a593Smuzhiyun 
1612*4882a593Smuzhiyun /* Set entries for PPPoE ethertype */
mvpp2_prs_pppoe_init(struct mvpp2 * priv)1613*4882a593Smuzhiyun static int mvpp2_prs_pppoe_init(struct mvpp2 *priv)
1614*4882a593Smuzhiyun {
1615*4882a593Smuzhiyun 	struct mvpp2_prs_entry pe;
1616*4882a593Smuzhiyun 	int tid;
1617*4882a593Smuzhiyun 
1618*4882a593Smuzhiyun 	/* IPv4 over PPPoE with options */
1619*4882a593Smuzhiyun 	tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
1620*4882a593Smuzhiyun 					MVPP2_PE_LAST_FREE_TID);
1621*4882a593Smuzhiyun 	if (tid < 0)
1622*4882a593Smuzhiyun 		return tid;
1623*4882a593Smuzhiyun 
1624*4882a593Smuzhiyun 	memset(&pe, 0, sizeof(pe));
1625*4882a593Smuzhiyun 	mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
1626*4882a593Smuzhiyun 	pe.index = tid;
1627*4882a593Smuzhiyun 
1628*4882a593Smuzhiyun 	mvpp2_prs_match_etype(&pe, 0, PPP_IP);
1629*4882a593Smuzhiyun 
1630*4882a593Smuzhiyun 	mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
1631*4882a593Smuzhiyun 	mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
1632*4882a593Smuzhiyun 				 MVPP2_PRS_RI_L3_PROTO_MASK);
1633*4882a593Smuzhiyun 	/* Skip eth_type + 4 bytes of IP header */
1634*4882a593Smuzhiyun 	mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
1635*4882a593Smuzhiyun 				 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1636*4882a593Smuzhiyun 	/* Set L3 offset */
1637*4882a593Smuzhiyun 	mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
1638*4882a593Smuzhiyun 				  MVPP2_ETH_TYPE_LEN,
1639*4882a593Smuzhiyun 				  MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
1640*4882a593Smuzhiyun 
1641*4882a593Smuzhiyun 	/* Update shadow table and hw entry */
1642*4882a593Smuzhiyun 	mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
1643*4882a593Smuzhiyun 	mvpp2_prs_hw_write(priv, &pe);
1644*4882a593Smuzhiyun 
1645*4882a593Smuzhiyun 	/* IPv4 over PPPoE without options */
1646*4882a593Smuzhiyun 	tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
1647*4882a593Smuzhiyun 					MVPP2_PE_LAST_FREE_TID);
1648*4882a593Smuzhiyun 	if (tid < 0)
1649*4882a593Smuzhiyun 		return tid;
1650*4882a593Smuzhiyun 
1651*4882a593Smuzhiyun 	pe.index = tid;
1652*4882a593Smuzhiyun 
1653*4882a593Smuzhiyun 	mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
1654*4882a593Smuzhiyun 				     MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
1655*4882a593Smuzhiyun 				     MVPP2_PRS_IPV4_HEAD_MASK |
1656*4882a593Smuzhiyun 				     MVPP2_PRS_IPV4_IHL_MASK);
1657*4882a593Smuzhiyun 
1658*4882a593Smuzhiyun 	/* Clear ri before updating */
1659*4882a593Smuzhiyun 	pe.sram[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
1660*4882a593Smuzhiyun 	pe.sram[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
1661*4882a593Smuzhiyun 	mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
1662*4882a593Smuzhiyun 				 MVPP2_PRS_RI_L3_PROTO_MASK);
1663*4882a593Smuzhiyun 
1664*4882a593Smuzhiyun 	/* Update shadow table and hw entry */
1665*4882a593Smuzhiyun 	mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
1666*4882a593Smuzhiyun 	mvpp2_prs_hw_write(priv, &pe);
1667*4882a593Smuzhiyun 
1668*4882a593Smuzhiyun 	/* IPv6 over PPPoE */
1669*4882a593Smuzhiyun 	tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
1670*4882a593Smuzhiyun 					MVPP2_PE_LAST_FREE_TID);
1671*4882a593Smuzhiyun 	if (tid < 0)
1672*4882a593Smuzhiyun 		return tid;
1673*4882a593Smuzhiyun 
1674*4882a593Smuzhiyun 	memset(&pe, 0, sizeof(pe));
1675*4882a593Smuzhiyun 	mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
1676*4882a593Smuzhiyun 	pe.index = tid;
1677*4882a593Smuzhiyun 
1678*4882a593Smuzhiyun 	mvpp2_prs_match_etype(&pe, 0, PPP_IPV6);
1679*4882a593Smuzhiyun 
1680*4882a593Smuzhiyun 	mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
1681*4882a593Smuzhiyun 	mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
1682*4882a593Smuzhiyun 				 MVPP2_PRS_RI_L3_PROTO_MASK);
1683*4882a593Smuzhiyun 	/* Jump to DIP of IPV6 header */
1684*4882a593Smuzhiyun 	mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 8 +
1685*4882a593Smuzhiyun 				 MVPP2_MAX_L3_ADDR_SIZE,
1686*4882a593Smuzhiyun 				 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1687*4882a593Smuzhiyun 	/* Set L3 offset */
1688*4882a593Smuzhiyun 	mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
1689*4882a593Smuzhiyun 				  MVPP2_ETH_TYPE_LEN,
1690*4882a593Smuzhiyun 				  MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
1691*4882a593Smuzhiyun 
1692*4882a593Smuzhiyun 	/* Update shadow table and hw entry */
1693*4882a593Smuzhiyun 	mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
1694*4882a593Smuzhiyun 	mvpp2_prs_hw_write(priv, &pe);
1695*4882a593Smuzhiyun 
1696*4882a593Smuzhiyun 	/* Non-IP over PPPoE */
1697*4882a593Smuzhiyun 	tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
1698*4882a593Smuzhiyun 					MVPP2_PE_LAST_FREE_TID);
1699*4882a593Smuzhiyun 	if (tid < 0)
1700*4882a593Smuzhiyun 		return tid;
1701*4882a593Smuzhiyun 
1702*4882a593Smuzhiyun 	memset(&pe, 0, sizeof(pe));
1703*4882a593Smuzhiyun 	mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
1704*4882a593Smuzhiyun 	pe.index = tid;
1705*4882a593Smuzhiyun 
1706*4882a593Smuzhiyun 	mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
1707*4882a593Smuzhiyun 				 MVPP2_PRS_RI_L3_PROTO_MASK);
1708*4882a593Smuzhiyun 
1709*4882a593Smuzhiyun 	/* Finished: go to flowid generation */
1710*4882a593Smuzhiyun 	mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1711*4882a593Smuzhiyun 	mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1712*4882a593Smuzhiyun 	/* Set L3 offset even if it's unknown L3 */
1713*4882a593Smuzhiyun 	mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
1714*4882a593Smuzhiyun 				  MVPP2_ETH_TYPE_LEN,
1715*4882a593Smuzhiyun 				  MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
1716*4882a593Smuzhiyun 
1717*4882a593Smuzhiyun 	/* Update shadow table and hw entry */
1718*4882a593Smuzhiyun 	mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
1719*4882a593Smuzhiyun 	mvpp2_prs_hw_write(priv, &pe);
1720*4882a593Smuzhiyun 
1721*4882a593Smuzhiyun 	return 0;
1722*4882a593Smuzhiyun }
1723*4882a593Smuzhiyun 
1724*4882a593Smuzhiyun /* Initialize entries for IPv4 */
mvpp2_prs_ip4_init(struct mvpp2 * priv)1725*4882a593Smuzhiyun static int mvpp2_prs_ip4_init(struct mvpp2 *priv)
1726*4882a593Smuzhiyun {
1727*4882a593Smuzhiyun 	struct mvpp2_prs_entry pe;
1728*4882a593Smuzhiyun 	int err;
1729*4882a593Smuzhiyun 
1730*4882a593Smuzhiyun 	/* Set entries for TCP, UDP and IGMP over IPv4 */
1731*4882a593Smuzhiyun 	err = mvpp2_prs_ip4_proto(priv, IPPROTO_TCP, MVPP2_PRS_RI_L4_TCP,
1732*4882a593Smuzhiyun 				  MVPP2_PRS_RI_L4_PROTO_MASK);
1733*4882a593Smuzhiyun 	if (err)
1734*4882a593Smuzhiyun 		return err;
1735*4882a593Smuzhiyun 
1736*4882a593Smuzhiyun 	err = mvpp2_prs_ip4_proto(priv, IPPROTO_UDP, MVPP2_PRS_RI_L4_UDP,
1737*4882a593Smuzhiyun 				  MVPP2_PRS_RI_L4_PROTO_MASK);
1738*4882a593Smuzhiyun 	if (err)
1739*4882a593Smuzhiyun 		return err;
1740*4882a593Smuzhiyun 
1741*4882a593Smuzhiyun 	err = mvpp2_prs_ip4_proto(priv, IPPROTO_IGMP,
1742*4882a593Smuzhiyun 				  MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
1743*4882a593Smuzhiyun 				  MVPP2_PRS_RI_UDF3_RX_SPECIAL,
1744*4882a593Smuzhiyun 				  MVPP2_PRS_RI_CPU_CODE_MASK |
1745*4882a593Smuzhiyun 				  MVPP2_PRS_RI_UDF3_MASK);
1746*4882a593Smuzhiyun 	if (err)
1747*4882a593Smuzhiyun 		return err;
1748*4882a593Smuzhiyun 
1749*4882a593Smuzhiyun 	/* IPv4 Broadcast */
1750*4882a593Smuzhiyun 	err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_BROAD_CAST);
1751*4882a593Smuzhiyun 	if (err)
1752*4882a593Smuzhiyun 		return err;
1753*4882a593Smuzhiyun 
1754*4882a593Smuzhiyun 	/* IPv4 Multicast */
1755*4882a593Smuzhiyun 	err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
1756*4882a593Smuzhiyun 	if (err)
1757*4882a593Smuzhiyun 		return err;
1758*4882a593Smuzhiyun 
1759*4882a593Smuzhiyun 	/* Default IPv4 entry for unknown protocols */
1760*4882a593Smuzhiyun 	memset(&pe, 0, sizeof(pe));
1761*4882a593Smuzhiyun 	mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
1762*4882a593Smuzhiyun 	pe.index = MVPP2_PE_IP4_PROTO_UN;
1763*4882a593Smuzhiyun 
1764*4882a593Smuzhiyun 	/* Set next lu to IPv4 */
1765*4882a593Smuzhiyun 	mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
1766*4882a593Smuzhiyun 	mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1767*4882a593Smuzhiyun 	/* Set L4 offset */
1768*4882a593Smuzhiyun 	mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
1769*4882a593Smuzhiyun 				  sizeof(struct iphdr) - 4,
1770*4882a593Smuzhiyun 				  MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
1771*4882a593Smuzhiyun 	mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
1772*4882a593Smuzhiyun 				 MVPP2_PRS_IPV4_DIP_AI_BIT);
1773*4882a593Smuzhiyun 	mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
1774*4882a593Smuzhiyun 				 MVPP2_PRS_RI_L4_PROTO_MASK);
1775*4882a593Smuzhiyun 
1776*4882a593Smuzhiyun 	mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
1777*4882a593Smuzhiyun 	/* Unmask all ports */
1778*4882a593Smuzhiyun 	mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
1779*4882a593Smuzhiyun 
1780*4882a593Smuzhiyun 	/* Update shadow table and hw entry */
1781*4882a593Smuzhiyun 	mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
1782*4882a593Smuzhiyun 	mvpp2_prs_hw_write(priv, &pe);
1783*4882a593Smuzhiyun 
1784*4882a593Smuzhiyun 	/* Default IPv4 entry for unicast address */
1785*4882a593Smuzhiyun 	memset(&pe, 0, sizeof(pe));
1786*4882a593Smuzhiyun 	mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
1787*4882a593Smuzhiyun 	pe.index = MVPP2_PE_IP4_ADDR_UN;
1788*4882a593Smuzhiyun 
1789*4882a593Smuzhiyun 	/* Finished: go to flowid generation */
1790*4882a593Smuzhiyun 	mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1791*4882a593Smuzhiyun 	mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1792*4882a593Smuzhiyun 	mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
1793*4882a593Smuzhiyun 				 MVPP2_PRS_RI_L3_ADDR_MASK);
1794*4882a593Smuzhiyun 
1795*4882a593Smuzhiyun 	mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
1796*4882a593Smuzhiyun 				 MVPP2_PRS_IPV4_DIP_AI_BIT);
1797*4882a593Smuzhiyun 	/* Unmask all ports */
1798*4882a593Smuzhiyun 	mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
1799*4882a593Smuzhiyun 
1800*4882a593Smuzhiyun 	/* Update shadow table and hw entry */
1801*4882a593Smuzhiyun 	mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
1802*4882a593Smuzhiyun 	mvpp2_prs_hw_write(priv, &pe);
1803*4882a593Smuzhiyun 
1804*4882a593Smuzhiyun 	return 0;
1805*4882a593Smuzhiyun }
1806*4882a593Smuzhiyun 
1807*4882a593Smuzhiyun /* Initialize entries for IPv6 */
mvpp2_prs_ip6_init(struct mvpp2 * priv)1808*4882a593Smuzhiyun static int mvpp2_prs_ip6_init(struct mvpp2 *priv)
1809*4882a593Smuzhiyun {
1810*4882a593Smuzhiyun 	struct mvpp2_prs_entry pe;
1811*4882a593Smuzhiyun 	int tid, err;
1812*4882a593Smuzhiyun 
1813*4882a593Smuzhiyun 	/* Set entries for TCP, UDP and ICMP over IPv6 */
1814*4882a593Smuzhiyun 	err = mvpp2_prs_ip6_proto(priv, IPPROTO_TCP,
1815*4882a593Smuzhiyun 				  MVPP2_PRS_RI_L4_TCP,
1816*4882a593Smuzhiyun 				  MVPP2_PRS_RI_L4_PROTO_MASK);
1817*4882a593Smuzhiyun 	if (err)
1818*4882a593Smuzhiyun 		return err;
1819*4882a593Smuzhiyun 
1820*4882a593Smuzhiyun 	err = mvpp2_prs_ip6_proto(priv, IPPROTO_UDP,
1821*4882a593Smuzhiyun 				  MVPP2_PRS_RI_L4_UDP,
1822*4882a593Smuzhiyun 				  MVPP2_PRS_RI_L4_PROTO_MASK);
1823*4882a593Smuzhiyun 	if (err)
1824*4882a593Smuzhiyun 		return err;
1825*4882a593Smuzhiyun 
1826*4882a593Smuzhiyun 	err = mvpp2_prs_ip6_proto(priv, IPPROTO_ICMPV6,
1827*4882a593Smuzhiyun 				  MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
1828*4882a593Smuzhiyun 				  MVPP2_PRS_RI_UDF3_RX_SPECIAL,
1829*4882a593Smuzhiyun 				  MVPP2_PRS_RI_CPU_CODE_MASK |
1830*4882a593Smuzhiyun 				  MVPP2_PRS_RI_UDF3_MASK);
1831*4882a593Smuzhiyun 	if (err)
1832*4882a593Smuzhiyun 		return err;
1833*4882a593Smuzhiyun 
1834*4882a593Smuzhiyun 	/* IPv4 is the last header. This is similar case as 6-TCP or 17-UDP */
1835*4882a593Smuzhiyun 	/* Result Info: UDF7=1, DS lite */
1836*4882a593Smuzhiyun 	err = mvpp2_prs_ip6_proto(priv, IPPROTO_IPIP,
1837*4882a593Smuzhiyun 				  MVPP2_PRS_RI_UDF7_IP6_LITE,
1838*4882a593Smuzhiyun 				  MVPP2_PRS_RI_UDF7_MASK);
1839*4882a593Smuzhiyun 	if (err)
1840*4882a593Smuzhiyun 		return err;
1841*4882a593Smuzhiyun 
1842*4882a593Smuzhiyun 	/* IPv6 multicast */
1843*4882a593Smuzhiyun 	err = mvpp2_prs_ip6_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
1844*4882a593Smuzhiyun 	if (err)
1845*4882a593Smuzhiyun 		return err;
1846*4882a593Smuzhiyun 
1847*4882a593Smuzhiyun 	/* Entry for checking hop limit */
1848*4882a593Smuzhiyun 	tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
1849*4882a593Smuzhiyun 					MVPP2_PE_LAST_FREE_TID);
1850*4882a593Smuzhiyun 	if (tid < 0)
1851*4882a593Smuzhiyun 		return tid;
1852*4882a593Smuzhiyun 
1853*4882a593Smuzhiyun 	memset(&pe, 0, sizeof(pe));
1854*4882a593Smuzhiyun 	mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
1855*4882a593Smuzhiyun 	pe.index = tid;
1856*4882a593Smuzhiyun 
1857*4882a593Smuzhiyun 	/* Finished: go to flowid generation */
1858*4882a593Smuzhiyun 	mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1859*4882a593Smuzhiyun 	mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1860*4882a593Smuzhiyun 	mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN |
1861*4882a593Smuzhiyun 				 MVPP2_PRS_RI_DROP_MASK,
1862*4882a593Smuzhiyun 				 MVPP2_PRS_RI_L3_PROTO_MASK |
1863*4882a593Smuzhiyun 				 MVPP2_PRS_RI_DROP_MASK);
1864*4882a593Smuzhiyun 
1865*4882a593Smuzhiyun 	mvpp2_prs_tcam_data_byte_set(&pe, 1, 0x00, MVPP2_PRS_IPV6_HOP_MASK);
1866*4882a593Smuzhiyun 	mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
1867*4882a593Smuzhiyun 				 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
1868*4882a593Smuzhiyun 
1869*4882a593Smuzhiyun 	/* Update shadow table and hw entry */
1870*4882a593Smuzhiyun 	mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
1871*4882a593Smuzhiyun 	mvpp2_prs_hw_write(priv, &pe);
1872*4882a593Smuzhiyun 
1873*4882a593Smuzhiyun 	/* Default IPv6 entry for unknown protocols */
1874*4882a593Smuzhiyun 	memset(&pe, 0, sizeof(pe));
1875*4882a593Smuzhiyun 	mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
1876*4882a593Smuzhiyun 	pe.index = MVPP2_PE_IP6_PROTO_UN;
1877*4882a593Smuzhiyun 
1878*4882a593Smuzhiyun 	/* Finished: go to flowid generation */
1879*4882a593Smuzhiyun 	mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1880*4882a593Smuzhiyun 	mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1881*4882a593Smuzhiyun 	mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
1882*4882a593Smuzhiyun 				 MVPP2_PRS_RI_L4_PROTO_MASK);
1883*4882a593Smuzhiyun 	/* Set L4 offset relatively to our current place */
1884*4882a593Smuzhiyun 	mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
1885*4882a593Smuzhiyun 				  sizeof(struct ipv6hdr) - 4,
1886*4882a593Smuzhiyun 				  MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
1887*4882a593Smuzhiyun 
1888*4882a593Smuzhiyun 	mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
1889*4882a593Smuzhiyun 				 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
1890*4882a593Smuzhiyun 	/* Unmask all ports */
1891*4882a593Smuzhiyun 	mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
1892*4882a593Smuzhiyun 
1893*4882a593Smuzhiyun 	/* Update shadow table and hw entry */
1894*4882a593Smuzhiyun 	mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
1895*4882a593Smuzhiyun 	mvpp2_prs_hw_write(priv, &pe);
1896*4882a593Smuzhiyun 
1897*4882a593Smuzhiyun 	/* Default IPv6 entry for unknown ext protocols */
1898*4882a593Smuzhiyun 	memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1899*4882a593Smuzhiyun 	mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
1900*4882a593Smuzhiyun 	pe.index = MVPP2_PE_IP6_EXT_PROTO_UN;
1901*4882a593Smuzhiyun 
1902*4882a593Smuzhiyun 	/* Finished: go to flowid generation */
1903*4882a593Smuzhiyun 	mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1904*4882a593Smuzhiyun 	mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1905*4882a593Smuzhiyun 	mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
1906*4882a593Smuzhiyun 				 MVPP2_PRS_RI_L4_PROTO_MASK);
1907*4882a593Smuzhiyun 
1908*4882a593Smuzhiyun 	mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_EXT_AI_BIT,
1909*4882a593Smuzhiyun 				 MVPP2_PRS_IPV6_EXT_AI_BIT);
1910*4882a593Smuzhiyun 	/* Unmask all ports */
1911*4882a593Smuzhiyun 	mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
1912*4882a593Smuzhiyun 
1913*4882a593Smuzhiyun 	/* Update shadow table and hw entry */
1914*4882a593Smuzhiyun 	mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
1915*4882a593Smuzhiyun 	mvpp2_prs_hw_write(priv, &pe);
1916*4882a593Smuzhiyun 
1917*4882a593Smuzhiyun 	/* Default IPv6 entry for unicast address */
1918*4882a593Smuzhiyun 	memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1919*4882a593Smuzhiyun 	mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
1920*4882a593Smuzhiyun 	pe.index = MVPP2_PE_IP6_ADDR_UN;
1921*4882a593Smuzhiyun 
1922*4882a593Smuzhiyun 	/* Finished: go to IPv6 again */
1923*4882a593Smuzhiyun 	mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
1924*4882a593Smuzhiyun 	mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
1925*4882a593Smuzhiyun 				 MVPP2_PRS_RI_L3_ADDR_MASK);
1926*4882a593Smuzhiyun 	mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
1927*4882a593Smuzhiyun 				 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
1928*4882a593Smuzhiyun 	/* Shift back to IPV6 NH */
1929*4882a593Smuzhiyun 	mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1930*4882a593Smuzhiyun 
1931*4882a593Smuzhiyun 	mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
1932*4882a593Smuzhiyun 	/* Unmask all ports */
1933*4882a593Smuzhiyun 	mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
1934*4882a593Smuzhiyun 
1935*4882a593Smuzhiyun 	/* Update shadow table and hw entry */
1936*4882a593Smuzhiyun 	mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
1937*4882a593Smuzhiyun 	mvpp2_prs_hw_write(priv, &pe);
1938*4882a593Smuzhiyun 
1939*4882a593Smuzhiyun 	return 0;
1940*4882a593Smuzhiyun }
1941*4882a593Smuzhiyun 
1942*4882a593Smuzhiyun /* Find tcam entry with matched pair <vid,port> */
mvpp2_prs_vid_range_find(struct mvpp2_port * port,u16 vid,u16 mask)1943*4882a593Smuzhiyun static int mvpp2_prs_vid_range_find(struct mvpp2_port *port, u16 vid, u16 mask)
1944*4882a593Smuzhiyun {
1945*4882a593Smuzhiyun 	unsigned char byte[2], enable[2];
1946*4882a593Smuzhiyun 	struct mvpp2_prs_entry pe;
1947*4882a593Smuzhiyun 	u16 rvid, rmask;
1948*4882a593Smuzhiyun 	int tid;
1949*4882a593Smuzhiyun 
1950*4882a593Smuzhiyun 	/* Go through the all entries with MVPP2_PRS_LU_VID */
1951*4882a593Smuzhiyun 	for (tid = MVPP2_PRS_VID_PORT_FIRST(port->id);
1952*4882a593Smuzhiyun 	     tid <= MVPP2_PRS_VID_PORT_LAST(port->id); tid++) {
1953*4882a593Smuzhiyun 		if (!port->priv->prs_shadow[tid].valid ||
1954*4882a593Smuzhiyun 		    port->priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VID)
1955*4882a593Smuzhiyun 			continue;
1956*4882a593Smuzhiyun 
1957*4882a593Smuzhiyun 		mvpp2_prs_init_from_hw(port->priv, &pe, tid);
1958*4882a593Smuzhiyun 
1959*4882a593Smuzhiyun 		mvpp2_prs_tcam_data_byte_get(&pe, 2, &byte[0], &enable[0]);
1960*4882a593Smuzhiyun 		mvpp2_prs_tcam_data_byte_get(&pe, 3, &byte[1], &enable[1]);
1961*4882a593Smuzhiyun 
1962*4882a593Smuzhiyun 		rvid = ((byte[0] & 0xf) << 8) + byte[1];
1963*4882a593Smuzhiyun 		rmask = ((enable[0] & 0xf) << 8) + enable[1];
1964*4882a593Smuzhiyun 
1965*4882a593Smuzhiyun 		if (rvid != vid || rmask != mask)
1966*4882a593Smuzhiyun 			continue;
1967*4882a593Smuzhiyun 
1968*4882a593Smuzhiyun 		return tid;
1969*4882a593Smuzhiyun 	}
1970*4882a593Smuzhiyun 
1971*4882a593Smuzhiyun 	return -ENOENT;
1972*4882a593Smuzhiyun }
1973*4882a593Smuzhiyun 
1974*4882a593Smuzhiyun /* Write parser entry for VID filtering */
mvpp2_prs_vid_entry_add(struct mvpp2_port * port,u16 vid)1975*4882a593Smuzhiyun int mvpp2_prs_vid_entry_add(struct mvpp2_port *port, u16 vid)
1976*4882a593Smuzhiyun {
1977*4882a593Smuzhiyun 	unsigned int vid_start = MVPP2_PE_VID_FILT_RANGE_START +
1978*4882a593Smuzhiyun 				 port->id * MVPP2_PRS_VLAN_FILT_MAX;
1979*4882a593Smuzhiyun 	unsigned int mask = 0xfff, reg_val, shift;
1980*4882a593Smuzhiyun 	struct mvpp2 *priv = port->priv;
1981*4882a593Smuzhiyun 	struct mvpp2_prs_entry pe;
1982*4882a593Smuzhiyun 	int tid;
1983*4882a593Smuzhiyun 
1984*4882a593Smuzhiyun 	memset(&pe, 0, sizeof(pe));
1985*4882a593Smuzhiyun 
1986*4882a593Smuzhiyun 	/* Scan TCAM and see if entry with this <vid,port> already exist */
1987*4882a593Smuzhiyun 	tid = mvpp2_prs_vid_range_find(port, vid, mask);
1988*4882a593Smuzhiyun 
1989*4882a593Smuzhiyun 	reg_val = mvpp2_read(priv, MVPP2_MH_REG(port->id));
1990*4882a593Smuzhiyun 	if (reg_val & MVPP2_DSA_EXTENDED)
1991*4882a593Smuzhiyun 		shift = MVPP2_VLAN_TAG_EDSA_LEN;
1992*4882a593Smuzhiyun 	else
1993*4882a593Smuzhiyun 		shift = MVPP2_VLAN_TAG_LEN;
1994*4882a593Smuzhiyun 
1995*4882a593Smuzhiyun 	/* No such entry */
1996*4882a593Smuzhiyun 	if (tid < 0) {
1997*4882a593Smuzhiyun 
1998*4882a593Smuzhiyun 		/* Go through all entries from first to last in vlan range */
1999*4882a593Smuzhiyun 		tid = mvpp2_prs_tcam_first_free(priv, vid_start,
2000*4882a593Smuzhiyun 						vid_start +
2001*4882a593Smuzhiyun 						MVPP2_PRS_VLAN_FILT_MAX_ENTRY);
2002*4882a593Smuzhiyun 
2003*4882a593Smuzhiyun 		/* There isn't room for a new VID filter */
2004*4882a593Smuzhiyun 		if (tid < 0)
2005*4882a593Smuzhiyun 			return tid;
2006*4882a593Smuzhiyun 
2007*4882a593Smuzhiyun 		mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VID);
2008*4882a593Smuzhiyun 		pe.index = tid;
2009*4882a593Smuzhiyun 
2010*4882a593Smuzhiyun 		/* Mask all ports */
2011*4882a593Smuzhiyun 		mvpp2_prs_tcam_port_map_set(&pe, 0);
2012*4882a593Smuzhiyun 	} else {
2013*4882a593Smuzhiyun 		mvpp2_prs_init_from_hw(priv, &pe, tid);
2014*4882a593Smuzhiyun 	}
2015*4882a593Smuzhiyun 
2016*4882a593Smuzhiyun 	/* Enable the current port */
2017*4882a593Smuzhiyun 	mvpp2_prs_tcam_port_set(&pe, port->id, true);
2018*4882a593Smuzhiyun 
2019*4882a593Smuzhiyun 	/* Continue - set next lookup */
2020*4882a593Smuzhiyun 	mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2021*4882a593Smuzhiyun 
2022*4882a593Smuzhiyun 	/* Skip VLAN header - Set offset to 4 or 8 bytes */
2023*4882a593Smuzhiyun 	mvpp2_prs_sram_shift_set(&pe, shift, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2024*4882a593Smuzhiyun 
2025*4882a593Smuzhiyun 	/* Set match on VID */
2026*4882a593Smuzhiyun 	mvpp2_prs_match_vid(&pe, MVPP2_PRS_VID_TCAM_BYTE, vid);
2027*4882a593Smuzhiyun 
2028*4882a593Smuzhiyun 	/* Clear all ai bits for next iteration */
2029*4882a593Smuzhiyun 	mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2030*4882a593Smuzhiyun 
2031*4882a593Smuzhiyun 	/* Update shadow table */
2032*4882a593Smuzhiyun 	mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VID);
2033*4882a593Smuzhiyun 	mvpp2_prs_hw_write(priv, &pe);
2034*4882a593Smuzhiyun 
2035*4882a593Smuzhiyun 	return 0;
2036*4882a593Smuzhiyun }
2037*4882a593Smuzhiyun 
2038*4882a593Smuzhiyun /* Write parser entry for VID filtering */
mvpp2_prs_vid_entry_remove(struct mvpp2_port * port,u16 vid)2039*4882a593Smuzhiyun void mvpp2_prs_vid_entry_remove(struct mvpp2_port *port, u16 vid)
2040*4882a593Smuzhiyun {
2041*4882a593Smuzhiyun 	struct mvpp2 *priv = port->priv;
2042*4882a593Smuzhiyun 	int tid;
2043*4882a593Smuzhiyun 
2044*4882a593Smuzhiyun 	/* Scan TCAM and see if entry with this <vid,port> already exist */
2045*4882a593Smuzhiyun 	tid = mvpp2_prs_vid_range_find(port, vid, 0xfff);
2046*4882a593Smuzhiyun 
2047*4882a593Smuzhiyun 	/* No such entry */
2048*4882a593Smuzhiyun 	if (tid < 0)
2049*4882a593Smuzhiyun 		return;
2050*4882a593Smuzhiyun 
2051*4882a593Smuzhiyun 	mvpp2_prs_hw_inv(priv, tid);
2052*4882a593Smuzhiyun 	priv->prs_shadow[tid].valid = false;
2053*4882a593Smuzhiyun }
2054*4882a593Smuzhiyun 
2055*4882a593Smuzhiyun /* Remove all existing VID filters on this port */
mvpp2_prs_vid_remove_all(struct mvpp2_port * port)2056*4882a593Smuzhiyun void mvpp2_prs_vid_remove_all(struct mvpp2_port *port)
2057*4882a593Smuzhiyun {
2058*4882a593Smuzhiyun 	struct mvpp2 *priv = port->priv;
2059*4882a593Smuzhiyun 	int tid;
2060*4882a593Smuzhiyun 
2061*4882a593Smuzhiyun 	for (tid = MVPP2_PRS_VID_PORT_FIRST(port->id);
2062*4882a593Smuzhiyun 	     tid <= MVPP2_PRS_VID_PORT_LAST(port->id); tid++) {
2063*4882a593Smuzhiyun 		if (priv->prs_shadow[tid].valid) {
2064*4882a593Smuzhiyun 			mvpp2_prs_hw_inv(priv, tid);
2065*4882a593Smuzhiyun 			priv->prs_shadow[tid].valid = false;
2066*4882a593Smuzhiyun 		}
2067*4882a593Smuzhiyun 	}
2068*4882a593Smuzhiyun }
2069*4882a593Smuzhiyun 
2070*4882a593Smuzhiyun /* Remove VID filering entry for this port */
mvpp2_prs_vid_disable_filtering(struct mvpp2_port * port)2071*4882a593Smuzhiyun void mvpp2_prs_vid_disable_filtering(struct mvpp2_port *port)
2072*4882a593Smuzhiyun {
2073*4882a593Smuzhiyun 	unsigned int tid = MVPP2_PRS_VID_PORT_DFLT(port->id);
2074*4882a593Smuzhiyun 	struct mvpp2 *priv = port->priv;
2075*4882a593Smuzhiyun 
2076*4882a593Smuzhiyun 	/* Invalidate the guard entry */
2077*4882a593Smuzhiyun 	mvpp2_prs_hw_inv(priv, tid);
2078*4882a593Smuzhiyun 
2079*4882a593Smuzhiyun 	priv->prs_shadow[tid].valid = false;
2080*4882a593Smuzhiyun }
2081*4882a593Smuzhiyun 
2082*4882a593Smuzhiyun /* Add guard entry that drops packets when no VID is matched on this port */
mvpp2_prs_vid_enable_filtering(struct mvpp2_port * port)2083*4882a593Smuzhiyun void mvpp2_prs_vid_enable_filtering(struct mvpp2_port *port)
2084*4882a593Smuzhiyun {
2085*4882a593Smuzhiyun 	unsigned int tid = MVPP2_PRS_VID_PORT_DFLT(port->id);
2086*4882a593Smuzhiyun 	struct mvpp2 *priv = port->priv;
2087*4882a593Smuzhiyun 	unsigned int reg_val, shift;
2088*4882a593Smuzhiyun 	struct mvpp2_prs_entry pe;
2089*4882a593Smuzhiyun 
2090*4882a593Smuzhiyun 	if (priv->prs_shadow[tid].valid)
2091*4882a593Smuzhiyun 		return;
2092*4882a593Smuzhiyun 
2093*4882a593Smuzhiyun 	memset(&pe, 0, sizeof(pe));
2094*4882a593Smuzhiyun 
2095*4882a593Smuzhiyun 	pe.index = tid;
2096*4882a593Smuzhiyun 
2097*4882a593Smuzhiyun 	reg_val = mvpp2_read(priv, MVPP2_MH_REG(port->id));
2098*4882a593Smuzhiyun 	if (reg_val & MVPP2_DSA_EXTENDED)
2099*4882a593Smuzhiyun 		shift = MVPP2_VLAN_TAG_EDSA_LEN;
2100*4882a593Smuzhiyun 	else
2101*4882a593Smuzhiyun 		shift = MVPP2_VLAN_TAG_LEN;
2102*4882a593Smuzhiyun 
2103*4882a593Smuzhiyun 	mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VID);
2104*4882a593Smuzhiyun 
2105*4882a593Smuzhiyun 	/* Mask all ports */
2106*4882a593Smuzhiyun 	mvpp2_prs_tcam_port_map_set(&pe, 0);
2107*4882a593Smuzhiyun 
2108*4882a593Smuzhiyun 	/* Update port mask */
2109*4882a593Smuzhiyun 	mvpp2_prs_tcam_port_set(&pe, port->id, true);
2110*4882a593Smuzhiyun 
2111*4882a593Smuzhiyun 	/* Continue - set next lookup */
2112*4882a593Smuzhiyun 	mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2113*4882a593Smuzhiyun 
2114*4882a593Smuzhiyun 	/* Skip VLAN header - Set offset to 4 or 8 bytes */
2115*4882a593Smuzhiyun 	mvpp2_prs_sram_shift_set(&pe, shift, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2116*4882a593Smuzhiyun 
2117*4882a593Smuzhiyun 	/* Drop VLAN packets that don't belong to any VIDs on this port */
2118*4882a593Smuzhiyun 	mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
2119*4882a593Smuzhiyun 				 MVPP2_PRS_RI_DROP_MASK);
2120*4882a593Smuzhiyun 
2121*4882a593Smuzhiyun 	/* Clear all ai bits for next iteration */
2122*4882a593Smuzhiyun 	mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2123*4882a593Smuzhiyun 
2124*4882a593Smuzhiyun 	/* Update shadow table */
2125*4882a593Smuzhiyun 	mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VID);
2126*4882a593Smuzhiyun 	mvpp2_prs_hw_write(priv, &pe);
2127*4882a593Smuzhiyun }
2128*4882a593Smuzhiyun 
2129*4882a593Smuzhiyun /* Parser default initialization */
mvpp2_prs_default_init(struct platform_device * pdev,struct mvpp2 * priv)2130*4882a593Smuzhiyun int mvpp2_prs_default_init(struct platform_device *pdev, struct mvpp2 *priv)
2131*4882a593Smuzhiyun {
2132*4882a593Smuzhiyun 	int err, index, i;
2133*4882a593Smuzhiyun 
2134*4882a593Smuzhiyun 	/* Enable tcam table */
2135*4882a593Smuzhiyun 	mvpp2_write(priv, MVPP2_PRS_TCAM_CTRL_REG, MVPP2_PRS_TCAM_EN_MASK);
2136*4882a593Smuzhiyun 
2137*4882a593Smuzhiyun 	/* Clear all tcam and sram entries */
2138*4882a593Smuzhiyun 	for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++) {
2139*4882a593Smuzhiyun 		mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
2140*4882a593Smuzhiyun 		for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
2141*4882a593Smuzhiyun 			mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), 0);
2142*4882a593Smuzhiyun 
2143*4882a593Smuzhiyun 		mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, index);
2144*4882a593Smuzhiyun 		for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
2145*4882a593Smuzhiyun 			mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), 0);
2146*4882a593Smuzhiyun 	}
2147*4882a593Smuzhiyun 
2148*4882a593Smuzhiyun 	/* Invalidate all tcam entries */
2149*4882a593Smuzhiyun 	for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++)
2150*4882a593Smuzhiyun 		mvpp2_prs_hw_inv(priv, index);
2151*4882a593Smuzhiyun 
2152*4882a593Smuzhiyun 	priv->prs_shadow = devm_kcalloc(&pdev->dev, MVPP2_PRS_TCAM_SRAM_SIZE,
2153*4882a593Smuzhiyun 					sizeof(*priv->prs_shadow),
2154*4882a593Smuzhiyun 					GFP_KERNEL);
2155*4882a593Smuzhiyun 	if (!priv->prs_shadow)
2156*4882a593Smuzhiyun 		return -ENOMEM;
2157*4882a593Smuzhiyun 
2158*4882a593Smuzhiyun 	/* Always start from lookup = 0 */
2159*4882a593Smuzhiyun 	for (index = 0; index < MVPP2_MAX_PORTS; index++)
2160*4882a593Smuzhiyun 		mvpp2_prs_hw_port_init(priv, index, MVPP2_PRS_LU_MH,
2161*4882a593Smuzhiyun 				       MVPP2_PRS_PORT_LU_MAX, 0);
2162*4882a593Smuzhiyun 
2163*4882a593Smuzhiyun 	mvpp2_prs_def_flow_init(priv);
2164*4882a593Smuzhiyun 
2165*4882a593Smuzhiyun 	mvpp2_prs_mh_init(priv);
2166*4882a593Smuzhiyun 
2167*4882a593Smuzhiyun 	mvpp2_prs_mac_init(priv);
2168*4882a593Smuzhiyun 
2169*4882a593Smuzhiyun 	mvpp2_prs_dsa_init(priv);
2170*4882a593Smuzhiyun 
2171*4882a593Smuzhiyun 	mvpp2_prs_vid_init(priv);
2172*4882a593Smuzhiyun 
2173*4882a593Smuzhiyun 	err = mvpp2_prs_etype_init(priv);
2174*4882a593Smuzhiyun 	if (err)
2175*4882a593Smuzhiyun 		return err;
2176*4882a593Smuzhiyun 
2177*4882a593Smuzhiyun 	err = mvpp2_prs_vlan_init(pdev, priv);
2178*4882a593Smuzhiyun 	if (err)
2179*4882a593Smuzhiyun 		return err;
2180*4882a593Smuzhiyun 
2181*4882a593Smuzhiyun 	err = mvpp2_prs_pppoe_init(priv);
2182*4882a593Smuzhiyun 	if (err)
2183*4882a593Smuzhiyun 		return err;
2184*4882a593Smuzhiyun 
2185*4882a593Smuzhiyun 	err = mvpp2_prs_ip6_init(priv);
2186*4882a593Smuzhiyun 	if (err)
2187*4882a593Smuzhiyun 		return err;
2188*4882a593Smuzhiyun 
2189*4882a593Smuzhiyun 	err = mvpp2_prs_ip4_init(priv);
2190*4882a593Smuzhiyun 	if (err)
2191*4882a593Smuzhiyun 		return err;
2192*4882a593Smuzhiyun 
2193*4882a593Smuzhiyun 	return 0;
2194*4882a593Smuzhiyun }
2195*4882a593Smuzhiyun 
2196*4882a593Smuzhiyun /* Compare MAC DA with tcam entry data */
mvpp2_prs_mac_range_equals(struct mvpp2_prs_entry * pe,const u8 * da,unsigned char * mask)2197*4882a593Smuzhiyun static bool mvpp2_prs_mac_range_equals(struct mvpp2_prs_entry *pe,
2198*4882a593Smuzhiyun 				       const u8 *da, unsigned char *mask)
2199*4882a593Smuzhiyun {
2200*4882a593Smuzhiyun 	unsigned char tcam_byte, tcam_mask;
2201*4882a593Smuzhiyun 	int index;
2202*4882a593Smuzhiyun 
2203*4882a593Smuzhiyun 	for (index = 0; index < ETH_ALEN; index++) {
2204*4882a593Smuzhiyun 		mvpp2_prs_tcam_data_byte_get(pe, index, &tcam_byte, &tcam_mask);
2205*4882a593Smuzhiyun 		if (tcam_mask != mask[index])
2206*4882a593Smuzhiyun 			return false;
2207*4882a593Smuzhiyun 
2208*4882a593Smuzhiyun 		if ((tcam_mask & tcam_byte) != (da[index] & mask[index]))
2209*4882a593Smuzhiyun 			return false;
2210*4882a593Smuzhiyun 	}
2211*4882a593Smuzhiyun 
2212*4882a593Smuzhiyun 	return true;
2213*4882a593Smuzhiyun }
2214*4882a593Smuzhiyun 
2215*4882a593Smuzhiyun /* Find tcam entry with matched pair <MAC DA, port> */
2216*4882a593Smuzhiyun static int
mvpp2_prs_mac_da_range_find(struct mvpp2 * priv,int pmap,const u8 * da,unsigned char * mask,int udf_type)2217*4882a593Smuzhiyun mvpp2_prs_mac_da_range_find(struct mvpp2 *priv, int pmap, const u8 *da,
2218*4882a593Smuzhiyun 			    unsigned char *mask, int udf_type)
2219*4882a593Smuzhiyun {
2220*4882a593Smuzhiyun 	struct mvpp2_prs_entry pe;
2221*4882a593Smuzhiyun 	int tid;
2222*4882a593Smuzhiyun 
2223*4882a593Smuzhiyun 	/* Go through the all entires with MVPP2_PRS_LU_MAC */
2224*4882a593Smuzhiyun 	for (tid = MVPP2_PE_MAC_RANGE_START;
2225*4882a593Smuzhiyun 	     tid <= MVPP2_PE_MAC_RANGE_END; tid++) {
2226*4882a593Smuzhiyun 		unsigned int entry_pmap;
2227*4882a593Smuzhiyun 
2228*4882a593Smuzhiyun 		if (!priv->prs_shadow[tid].valid ||
2229*4882a593Smuzhiyun 		    (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
2230*4882a593Smuzhiyun 		    (priv->prs_shadow[tid].udf != udf_type))
2231*4882a593Smuzhiyun 			continue;
2232*4882a593Smuzhiyun 
2233*4882a593Smuzhiyun 		mvpp2_prs_init_from_hw(priv, &pe, tid);
2234*4882a593Smuzhiyun 		entry_pmap = mvpp2_prs_tcam_port_map_get(&pe);
2235*4882a593Smuzhiyun 
2236*4882a593Smuzhiyun 		if (mvpp2_prs_mac_range_equals(&pe, da, mask) &&
2237*4882a593Smuzhiyun 		    entry_pmap == pmap)
2238*4882a593Smuzhiyun 			return tid;
2239*4882a593Smuzhiyun 	}
2240*4882a593Smuzhiyun 
2241*4882a593Smuzhiyun 	return -ENOENT;
2242*4882a593Smuzhiyun }
2243*4882a593Smuzhiyun 
2244*4882a593Smuzhiyun /* Update parser's mac da entry */
mvpp2_prs_mac_da_accept(struct mvpp2_port * port,const u8 * da,bool add)2245*4882a593Smuzhiyun int mvpp2_prs_mac_da_accept(struct mvpp2_port *port, const u8 *da, bool add)
2246*4882a593Smuzhiyun {
2247*4882a593Smuzhiyun 	unsigned char mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
2248*4882a593Smuzhiyun 	struct mvpp2 *priv = port->priv;
2249*4882a593Smuzhiyun 	unsigned int pmap, len, ri;
2250*4882a593Smuzhiyun 	struct mvpp2_prs_entry pe;
2251*4882a593Smuzhiyun 	int tid;
2252*4882a593Smuzhiyun 
2253*4882a593Smuzhiyun 	memset(&pe, 0, sizeof(pe));
2254*4882a593Smuzhiyun 
2255*4882a593Smuzhiyun 	/* Scan TCAM and see if entry with this <MAC DA, port> already exist */
2256*4882a593Smuzhiyun 	tid = mvpp2_prs_mac_da_range_find(priv, BIT(port->id), da, mask,
2257*4882a593Smuzhiyun 					  MVPP2_PRS_UDF_MAC_DEF);
2258*4882a593Smuzhiyun 
2259*4882a593Smuzhiyun 	/* No such entry */
2260*4882a593Smuzhiyun 	if (tid < 0) {
2261*4882a593Smuzhiyun 		if (!add)
2262*4882a593Smuzhiyun 			return 0;
2263*4882a593Smuzhiyun 
2264*4882a593Smuzhiyun 		/* Create new TCAM entry */
2265*4882a593Smuzhiyun 		/* Go through the all entries from first to last */
2266*4882a593Smuzhiyun 		tid = mvpp2_prs_tcam_first_free(priv,
2267*4882a593Smuzhiyun 						MVPP2_PE_MAC_RANGE_START,
2268*4882a593Smuzhiyun 						MVPP2_PE_MAC_RANGE_END);
2269*4882a593Smuzhiyun 		if (tid < 0)
2270*4882a593Smuzhiyun 			return tid;
2271*4882a593Smuzhiyun 
2272*4882a593Smuzhiyun 		pe.index = tid;
2273*4882a593Smuzhiyun 
2274*4882a593Smuzhiyun 		/* Mask all ports */
2275*4882a593Smuzhiyun 		mvpp2_prs_tcam_port_map_set(&pe, 0);
2276*4882a593Smuzhiyun 	} else {
2277*4882a593Smuzhiyun 		mvpp2_prs_init_from_hw(priv, &pe, tid);
2278*4882a593Smuzhiyun 	}
2279*4882a593Smuzhiyun 
2280*4882a593Smuzhiyun 	mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
2281*4882a593Smuzhiyun 
2282*4882a593Smuzhiyun 	/* Update port mask */
2283*4882a593Smuzhiyun 	mvpp2_prs_tcam_port_set(&pe, port->id, add);
2284*4882a593Smuzhiyun 
2285*4882a593Smuzhiyun 	/* Invalidate the entry if no ports are left enabled */
2286*4882a593Smuzhiyun 	pmap = mvpp2_prs_tcam_port_map_get(&pe);
2287*4882a593Smuzhiyun 	if (pmap == 0) {
2288*4882a593Smuzhiyun 		if (add)
2289*4882a593Smuzhiyun 			return -EINVAL;
2290*4882a593Smuzhiyun 
2291*4882a593Smuzhiyun 		mvpp2_prs_hw_inv(priv, pe.index);
2292*4882a593Smuzhiyun 		priv->prs_shadow[pe.index].valid = false;
2293*4882a593Smuzhiyun 		return 0;
2294*4882a593Smuzhiyun 	}
2295*4882a593Smuzhiyun 
2296*4882a593Smuzhiyun 	/* Continue - set next lookup */
2297*4882a593Smuzhiyun 	mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
2298*4882a593Smuzhiyun 
2299*4882a593Smuzhiyun 	/* Set match on DA */
2300*4882a593Smuzhiyun 	len = ETH_ALEN;
2301*4882a593Smuzhiyun 	while (len--)
2302*4882a593Smuzhiyun 		mvpp2_prs_tcam_data_byte_set(&pe, len, da[len], 0xff);
2303*4882a593Smuzhiyun 
2304*4882a593Smuzhiyun 	/* Set result info bits */
2305*4882a593Smuzhiyun 	if (is_broadcast_ether_addr(da)) {
2306*4882a593Smuzhiyun 		ri = MVPP2_PRS_RI_L2_BCAST;
2307*4882a593Smuzhiyun 	} else if (is_multicast_ether_addr(da)) {
2308*4882a593Smuzhiyun 		ri = MVPP2_PRS_RI_L2_MCAST;
2309*4882a593Smuzhiyun 	} else {
2310*4882a593Smuzhiyun 		ri = MVPP2_PRS_RI_L2_UCAST;
2311*4882a593Smuzhiyun 
2312*4882a593Smuzhiyun 		if (ether_addr_equal(da, port->dev->dev_addr))
2313*4882a593Smuzhiyun 			ri |= MVPP2_PRS_RI_MAC_ME_MASK;
2314*4882a593Smuzhiyun 	}
2315*4882a593Smuzhiyun 
2316*4882a593Smuzhiyun 	mvpp2_prs_sram_ri_update(&pe, ri, MVPP2_PRS_RI_L2_CAST_MASK |
2317*4882a593Smuzhiyun 				 MVPP2_PRS_RI_MAC_ME_MASK);
2318*4882a593Smuzhiyun 	mvpp2_prs_shadow_ri_set(priv, pe.index, ri, MVPP2_PRS_RI_L2_CAST_MASK |
2319*4882a593Smuzhiyun 				MVPP2_PRS_RI_MAC_ME_MASK);
2320*4882a593Smuzhiyun 
2321*4882a593Smuzhiyun 	/* Shift to ethertype */
2322*4882a593Smuzhiyun 	mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
2323*4882a593Smuzhiyun 				 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2324*4882a593Smuzhiyun 
2325*4882a593Smuzhiyun 	/* Update shadow table and hw entry */
2326*4882a593Smuzhiyun 	priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_MAC_DEF;
2327*4882a593Smuzhiyun 	mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2328*4882a593Smuzhiyun 	mvpp2_prs_hw_write(priv, &pe);
2329*4882a593Smuzhiyun 
2330*4882a593Smuzhiyun 	return 0;
2331*4882a593Smuzhiyun }
2332*4882a593Smuzhiyun 
mvpp2_prs_update_mac_da(struct net_device * dev,const u8 * da)2333*4882a593Smuzhiyun int mvpp2_prs_update_mac_da(struct net_device *dev, const u8 *da)
2334*4882a593Smuzhiyun {
2335*4882a593Smuzhiyun 	struct mvpp2_port *port = netdev_priv(dev);
2336*4882a593Smuzhiyun 	int err;
2337*4882a593Smuzhiyun 
2338*4882a593Smuzhiyun 	/* Remove old parser entry */
2339*4882a593Smuzhiyun 	err = mvpp2_prs_mac_da_accept(port, dev->dev_addr, false);
2340*4882a593Smuzhiyun 	if (err)
2341*4882a593Smuzhiyun 		return err;
2342*4882a593Smuzhiyun 
2343*4882a593Smuzhiyun 	/* Add new parser entry */
2344*4882a593Smuzhiyun 	err = mvpp2_prs_mac_da_accept(port, da, true);
2345*4882a593Smuzhiyun 	if (err)
2346*4882a593Smuzhiyun 		return err;
2347*4882a593Smuzhiyun 
2348*4882a593Smuzhiyun 	/* Set addr in the device */
2349*4882a593Smuzhiyun 	ether_addr_copy(dev->dev_addr, da);
2350*4882a593Smuzhiyun 
2351*4882a593Smuzhiyun 	return 0;
2352*4882a593Smuzhiyun }
2353*4882a593Smuzhiyun 
mvpp2_prs_mac_del_all(struct mvpp2_port * port)2354*4882a593Smuzhiyun void mvpp2_prs_mac_del_all(struct mvpp2_port *port)
2355*4882a593Smuzhiyun {
2356*4882a593Smuzhiyun 	struct mvpp2 *priv = port->priv;
2357*4882a593Smuzhiyun 	struct mvpp2_prs_entry pe;
2358*4882a593Smuzhiyun 	unsigned long pmap;
2359*4882a593Smuzhiyun 	int index, tid;
2360*4882a593Smuzhiyun 
2361*4882a593Smuzhiyun 	for (tid = MVPP2_PE_MAC_RANGE_START;
2362*4882a593Smuzhiyun 	     tid <= MVPP2_PE_MAC_RANGE_END; tid++) {
2363*4882a593Smuzhiyun 		unsigned char da[ETH_ALEN], da_mask[ETH_ALEN];
2364*4882a593Smuzhiyun 
2365*4882a593Smuzhiyun 		if (!priv->prs_shadow[tid].valid ||
2366*4882a593Smuzhiyun 		    (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
2367*4882a593Smuzhiyun 		    (priv->prs_shadow[tid].udf != MVPP2_PRS_UDF_MAC_DEF))
2368*4882a593Smuzhiyun 			continue;
2369*4882a593Smuzhiyun 
2370*4882a593Smuzhiyun 		mvpp2_prs_init_from_hw(priv, &pe, tid);
2371*4882a593Smuzhiyun 
2372*4882a593Smuzhiyun 		pmap = mvpp2_prs_tcam_port_map_get(&pe);
2373*4882a593Smuzhiyun 
2374*4882a593Smuzhiyun 		/* We only want entries active on this port */
2375*4882a593Smuzhiyun 		if (!test_bit(port->id, &pmap))
2376*4882a593Smuzhiyun 			continue;
2377*4882a593Smuzhiyun 
2378*4882a593Smuzhiyun 		/* Read mac addr from entry */
2379*4882a593Smuzhiyun 		for (index = 0; index < ETH_ALEN; index++)
2380*4882a593Smuzhiyun 			mvpp2_prs_tcam_data_byte_get(&pe, index, &da[index],
2381*4882a593Smuzhiyun 						     &da_mask[index]);
2382*4882a593Smuzhiyun 
2383*4882a593Smuzhiyun 		/* Special cases : Don't remove broadcast and port's own
2384*4882a593Smuzhiyun 		 * address
2385*4882a593Smuzhiyun 		 */
2386*4882a593Smuzhiyun 		if (is_broadcast_ether_addr(da) ||
2387*4882a593Smuzhiyun 		    ether_addr_equal(da, port->dev->dev_addr))
2388*4882a593Smuzhiyun 			continue;
2389*4882a593Smuzhiyun 
2390*4882a593Smuzhiyun 		/* Remove entry from TCAM */
2391*4882a593Smuzhiyun 		mvpp2_prs_mac_da_accept(port, da, false);
2392*4882a593Smuzhiyun 	}
2393*4882a593Smuzhiyun }
2394*4882a593Smuzhiyun 
mvpp2_prs_tag_mode_set(struct mvpp2 * priv,int port,int type)2395*4882a593Smuzhiyun int mvpp2_prs_tag_mode_set(struct mvpp2 *priv, int port, int type)
2396*4882a593Smuzhiyun {
2397*4882a593Smuzhiyun 	switch (type) {
2398*4882a593Smuzhiyun 	case MVPP2_TAG_TYPE_EDSA:
2399*4882a593Smuzhiyun 		/* Add port to EDSA entries */
2400*4882a593Smuzhiyun 		mvpp2_prs_dsa_tag_set(priv, port, true,
2401*4882a593Smuzhiyun 				      MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
2402*4882a593Smuzhiyun 		mvpp2_prs_dsa_tag_set(priv, port, true,
2403*4882a593Smuzhiyun 				      MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
2404*4882a593Smuzhiyun 		/* Remove port from DSA entries */
2405*4882a593Smuzhiyun 		mvpp2_prs_dsa_tag_set(priv, port, false,
2406*4882a593Smuzhiyun 				      MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
2407*4882a593Smuzhiyun 		mvpp2_prs_dsa_tag_set(priv, port, false,
2408*4882a593Smuzhiyun 				      MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
2409*4882a593Smuzhiyun 		break;
2410*4882a593Smuzhiyun 
2411*4882a593Smuzhiyun 	case MVPP2_TAG_TYPE_DSA:
2412*4882a593Smuzhiyun 		/* Add port to DSA entries */
2413*4882a593Smuzhiyun 		mvpp2_prs_dsa_tag_set(priv, port, true,
2414*4882a593Smuzhiyun 				      MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
2415*4882a593Smuzhiyun 		mvpp2_prs_dsa_tag_set(priv, port, true,
2416*4882a593Smuzhiyun 				      MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
2417*4882a593Smuzhiyun 		/* Remove port from EDSA entries */
2418*4882a593Smuzhiyun 		mvpp2_prs_dsa_tag_set(priv, port, false,
2419*4882a593Smuzhiyun 				      MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
2420*4882a593Smuzhiyun 		mvpp2_prs_dsa_tag_set(priv, port, false,
2421*4882a593Smuzhiyun 				      MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
2422*4882a593Smuzhiyun 		break;
2423*4882a593Smuzhiyun 
2424*4882a593Smuzhiyun 	case MVPP2_TAG_TYPE_MH:
2425*4882a593Smuzhiyun 	case MVPP2_TAG_TYPE_NONE:
2426*4882a593Smuzhiyun 		/* Remove port form EDSA and DSA entries */
2427*4882a593Smuzhiyun 		mvpp2_prs_dsa_tag_set(priv, port, false,
2428*4882a593Smuzhiyun 				      MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
2429*4882a593Smuzhiyun 		mvpp2_prs_dsa_tag_set(priv, port, false,
2430*4882a593Smuzhiyun 				      MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
2431*4882a593Smuzhiyun 		mvpp2_prs_dsa_tag_set(priv, port, false,
2432*4882a593Smuzhiyun 				      MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
2433*4882a593Smuzhiyun 		mvpp2_prs_dsa_tag_set(priv, port, false,
2434*4882a593Smuzhiyun 				      MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
2435*4882a593Smuzhiyun 		break;
2436*4882a593Smuzhiyun 
2437*4882a593Smuzhiyun 	default:
2438*4882a593Smuzhiyun 		if ((type < 0) || (type > MVPP2_TAG_TYPE_EDSA))
2439*4882a593Smuzhiyun 			return -EINVAL;
2440*4882a593Smuzhiyun 	}
2441*4882a593Smuzhiyun 
2442*4882a593Smuzhiyun 	return 0;
2443*4882a593Smuzhiyun }
2444*4882a593Smuzhiyun 
mvpp2_prs_add_flow(struct mvpp2 * priv,int flow,u32 ri,u32 ri_mask)2445*4882a593Smuzhiyun int mvpp2_prs_add_flow(struct mvpp2 *priv, int flow, u32 ri, u32 ri_mask)
2446*4882a593Smuzhiyun {
2447*4882a593Smuzhiyun 	struct mvpp2_prs_entry pe;
2448*4882a593Smuzhiyun 	u8 *ri_byte, *ri_byte_mask;
2449*4882a593Smuzhiyun 	int tid, i;
2450*4882a593Smuzhiyun 
2451*4882a593Smuzhiyun 	memset(&pe, 0, sizeof(pe));
2452*4882a593Smuzhiyun 
2453*4882a593Smuzhiyun 	tid = mvpp2_prs_tcam_first_free(priv,
2454*4882a593Smuzhiyun 					MVPP2_PE_LAST_FREE_TID,
2455*4882a593Smuzhiyun 					MVPP2_PE_FIRST_FREE_TID);
2456*4882a593Smuzhiyun 	if (tid < 0)
2457*4882a593Smuzhiyun 		return tid;
2458*4882a593Smuzhiyun 
2459*4882a593Smuzhiyun 	pe.index = tid;
2460*4882a593Smuzhiyun 
2461*4882a593Smuzhiyun 	ri_byte = (u8 *)&ri;
2462*4882a593Smuzhiyun 	ri_byte_mask = (u8 *)&ri_mask;
2463*4882a593Smuzhiyun 
2464*4882a593Smuzhiyun 	mvpp2_prs_sram_ai_update(&pe, flow, MVPP2_PRS_FLOW_ID_MASK);
2465*4882a593Smuzhiyun 	mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
2466*4882a593Smuzhiyun 
2467*4882a593Smuzhiyun 	for (i = 0; i < 4; i++) {
2468*4882a593Smuzhiyun 		mvpp2_prs_tcam_data_byte_set(&pe, i, ri_byte[i],
2469*4882a593Smuzhiyun 					     ri_byte_mask[i]);
2470*4882a593Smuzhiyun 	}
2471*4882a593Smuzhiyun 
2472*4882a593Smuzhiyun 	mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_FLOWS);
2473*4882a593Smuzhiyun 	mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2474*4882a593Smuzhiyun 	mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2475*4882a593Smuzhiyun 	mvpp2_prs_hw_write(priv, &pe);
2476*4882a593Smuzhiyun 
2477*4882a593Smuzhiyun 	return 0;
2478*4882a593Smuzhiyun }
2479*4882a593Smuzhiyun 
2480*4882a593Smuzhiyun /* Set prs flow for the port */
mvpp2_prs_def_flow(struct mvpp2_port * port)2481*4882a593Smuzhiyun int mvpp2_prs_def_flow(struct mvpp2_port *port)
2482*4882a593Smuzhiyun {
2483*4882a593Smuzhiyun 	struct mvpp2_prs_entry pe;
2484*4882a593Smuzhiyun 	int tid;
2485*4882a593Smuzhiyun 
2486*4882a593Smuzhiyun 	memset(&pe, 0, sizeof(pe));
2487*4882a593Smuzhiyun 
2488*4882a593Smuzhiyun 	tid = mvpp2_prs_flow_find(port->priv, port->id);
2489*4882a593Smuzhiyun 
2490*4882a593Smuzhiyun 	/* Such entry not exist */
2491*4882a593Smuzhiyun 	if (tid < 0) {
2492*4882a593Smuzhiyun 		/* Go through the all entires from last to first */
2493*4882a593Smuzhiyun 		tid = mvpp2_prs_tcam_first_free(port->priv,
2494*4882a593Smuzhiyun 						MVPP2_PE_LAST_FREE_TID,
2495*4882a593Smuzhiyun 					       MVPP2_PE_FIRST_FREE_TID);
2496*4882a593Smuzhiyun 		if (tid < 0)
2497*4882a593Smuzhiyun 			return tid;
2498*4882a593Smuzhiyun 
2499*4882a593Smuzhiyun 		pe.index = tid;
2500*4882a593Smuzhiyun 
2501*4882a593Smuzhiyun 		/* Set flow ID*/
2502*4882a593Smuzhiyun 		mvpp2_prs_sram_ai_update(&pe, port->id, MVPP2_PRS_FLOW_ID_MASK);
2503*4882a593Smuzhiyun 		mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
2504*4882a593Smuzhiyun 
2505*4882a593Smuzhiyun 		/* Update shadow table */
2506*4882a593Smuzhiyun 		mvpp2_prs_shadow_set(port->priv, pe.index, MVPP2_PRS_LU_FLOWS);
2507*4882a593Smuzhiyun 	} else {
2508*4882a593Smuzhiyun 		mvpp2_prs_init_from_hw(port->priv, &pe, tid);
2509*4882a593Smuzhiyun 	}
2510*4882a593Smuzhiyun 
2511*4882a593Smuzhiyun 	mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2512*4882a593Smuzhiyun 	mvpp2_prs_tcam_port_map_set(&pe, (1 << port->id));
2513*4882a593Smuzhiyun 	mvpp2_prs_hw_write(port->priv, &pe);
2514*4882a593Smuzhiyun 
2515*4882a593Smuzhiyun 	return 0;
2516*4882a593Smuzhiyun }
2517*4882a593Smuzhiyun 
mvpp2_prs_hits(struct mvpp2 * priv,int index)2518*4882a593Smuzhiyun int mvpp2_prs_hits(struct mvpp2 *priv, int index)
2519*4882a593Smuzhiyun {
2520*4882a593Smuzhiyun 	u32 val;
2521*4882a593Smuzhiyun 
2522*4882a593Smuzhiyun 	if (index > MVPP2_PRS_TCAM_SRAM_SIZE)
2523*4882a593Smuzhiyun 		return -EINVAL;
2524*4882a593Smuzhiyun 
2525*4882a593Smuzhiyun 	mvpp2_write(priv, MVPP2_PRS_TCAM_HIT_IDX_REG, index);
2526*4882a593Smuzhiyun 
2527*4882a593Smuzhiyun 	val = mvpp2_read(priv, MVPP2_PRS_TCAM_HIT_CNT_REG);
2528*4882a593Smuzhiyun 
2529*4882a593Smuzhiyun 	val &= MVPP2_PRS_TCAM_HIT_CNT_MASK;
2530*4882a593Smuzhiyun 
2531*4882a593Smuzhiyun 	return val;
2532*4882a593Smuzhiyun }
2533