xref: /OK3568_Linux_fs/kernel/drivers/net/ethernet/mscc/ocelot_flower.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2*4882a593Smuzhiyun /* Microsemi Ocelot Switch driver
3*4882a593Smuzhiyun  * Copyright (c) 2019 Microsemi Corporation
4*4882a593Smuzhiyun  */
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun #include <net/pkt_cls.h>
7*4882a593Smuzhiyun #include <net/tc_act/tc_gact.h>
8*4882a593Smuzhiyun #include <soc/mscc/ocelot_vcap.h>
9*4882a593Smuzhiyun #include "ocelot_vcap.h"
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun /* Arbitrarily chosen constants for encoding the VCAP block and lookup number
12*4882a593Smuzhiyun  * into the chain number. This is UAPI.
13*4882a593Smuzhiyun  */
14*4882a593Smuzhiyun #define VCAP_BLOCK			10000
15*4882a593Smuzhiyun #define VCAP_LOOKUP			1000
16*4882a593Smuzhiyun #define VCAP_IS1_NUM_LOOKUPS		3
17*4882a593Smuzhiyun #define VCAP_IS2_NUM_LOOKUPS		2
18*4882a593Smuzhiyun #define VCAP_IS2_NUM_PAG		256
19*4882a593Smuzhiyun #define VCAP_IS1_CHAIN(lookup)		\
20*4882a593Smuzhiyun 	(1 * VCAP_BLOCK + (lookup) * VCAP_LOOKUP)
21*4882a593Smuzhiyun #define VCAP_IS2_CHAIN(lookup, pag)	\
22*4882a593Smuzhiyun 	(2 * VCAP_BLOCK + (lookup) * VCAP_LOOKUP + (pag))
23*4882a593Smuzhiyun 
ocelot_chain_to_block(int chain,bool ingress)24*4882a593Smuzhiyun static int ocelot_chain_to_block(int chain, bool ingress)
25*4882a593Smuzhiyun {
26*4882a593Smuzhiyun 	int lookup, pag;
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun 	if (!ingress) {
29*4882a593Smuzhiyun 		if (chain == 0)
30*4882a593Smuzhiyun 			return VCAP_ES0;
31*4882a593Smuzhiyun 		return -EOPNOTSUPP;
32*4882a593Smuzhiyun 	}
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun 	/* Backwards compatibility with older, single-chain tc-flower
35*4882a593Smuzhiyun 	 * offload support in Ocelot
36*4882a593Smuzhiyun 	 */
37*4882a593Smuzhiyun 	if (chain == 0)
38*4882a593Smuzhiyun 		return VCAP_IS2;
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun 	for (lookup = 0; lookup < VCAP_IS1_NUM_LOOKUPS; lookup++)
41*4882a593Smuzhiyun 		if (chain == VCAP_IS1_CHAIN(lookup))
42*4882a593Smuzhiyun 			return VCAP_IS1;
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun 	for (lookup = 0; lookup < VCAP_IS2_NUM_LOOKUPS; lookup++)
45*4882a593Smuzhiyun 		for (pag = 0; pag < VCAP_IS2_NUM_PAG; pag++)
46*4882a593Smuzhiyun 			if (chain == VCAP_IS2_CHAIN(lookup, pag))
47*4882a593Smuzhiyun 				return VCAP_IS2;
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	return -EOPNOTSUPP;
50*4882a593Smuzhiyun }
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun /* Caller must ensure this is a valid IS1 or IS2 chain first,
53*4882a593Smuzhiyun  * by calling ocelot_chain_to_block.
54*4882a593Smuzhiyun  */
ocelot_chain_to_lookup(int chain)55*4882a593Smuzhiyun static int ocelot_chain_to_lookup(int chain)
56*4882a593Smuzhiyun {
57*4882a593Smuzhiyun 	/* Backwards compatibility with older, single-chain tc-flower
58*4882a593Smuzhiyun 	 * offload support in Ocelot
59*4882a593Smuzhiyun 	 */
60*4882a593Smuzhiyun 	if (chain == 0)
61*4882a593Smuzhiyun 		return 0;
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun 	return (chain / VCAP_LOOKUP) % 10;
64*4882a593Smuzhiyun }
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun /* Caller must ensure this is a valid IS2 chain first,
67*4882a593Smuzhiyun  * by calling ocelot_chain_to_block.
68*4882a593Smuzhiyun  */
ocelot_chain_to_pag(int chain)69*4882a593Smuzhiyun static int ocelot_chain_to_pag(int chain)
70*4882a593Smuzhiyun {
71*4882a593Smuzhiyun 	int lookup;
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 	/* Backwards compatibility with older, single-chain tc-flower
74*4882a593Smuzhiyun 	 * offload support in Ocelot
75*4882a593Smuzhiyun 	 */
76*4882a593Smuzhiyun 	if (chain == 0)
77*4882a593Smuzhiyun 		return 0;
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	lookup = ocelot_chain_to_lookup(chain);
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	/* calculate PAG value as chain index relative to the first PAG */
82*4882a593Smuzhiyun 	return chain - VCAP_IS2_CHAIN(lookup, 0);
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun 
ocelot_is_goto_target_valid(int goto_target,int chain,bool ingress)85*4882a593Smuzhiyun static bool ocelot_is_goto_target_valid(int goto_target, int chain,
86*4882a593Smuzhiyun 					bool ingress)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun 	int pag;
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	/* Can't offload GOTO in VCAP ES0 */
91*4882a593Smuzhiyun 	if (!ingress)
92*4882a593Smuzhiyun 		return (goto_target < 0);
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	/* Non-optional GOTOs */
95*4882a593Smuzhiyun 	if (chain == 0)
96*4882a593Smuzhiyun 		/* VCAP IS1 can be skipped, either partially or completely */
97*4882a593Smuzhiyun 		return (goto_target == VCAP_IS1_CHAIN(0) ||
98*4882a593Smuzhiyun 			goto_target == VCAP_IS1_CHAIN(1) ||
99*4882a593Smuzhiyun 			goto_target == VCAP_IS1_CHAIN(2) ||
100*4882a593Smuzhiyun 			goto_target == VCAP_IS2_CHAIN(0, 0) ||
101*4882a593Smuzhiyun 			goto_target == VCAP_IS2_CHAIN(1, 0));
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	if (chain == VCAP_IS1_CHAIN(0))
104*4882a593Smuzhiyun 		return (goto_target == VCAP_IS1_CHAIN(1));
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 	if (chain == VCAP_IS1_CHAIN(1))
107*4882a593Smuzhiyun 		return (goto_target == VCAP_IS1_CHAIN(2));
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 	/* Lookup 2 of VCAP IS1 can really support non-optional GOTOs,
110*4882a593Smuzhiyun 	 * using a Policy Association Group (PAG) value, which is an 8-bit
111*4882a593Smuzhiyun 	 * value encoding a VCAP IS2 target chain.
112*4882a593Smuzhiyun 	 */
113*4882a593Smuzhiyun 	if (chain == VCAP_IS1_CHAIN(2)) {
114*4882a593Smuzhiyun 		for (pag = 0; pag < VCAP_IS2_NUM_PAG; pag++)
115*4882a593Smuzhiyun 			if (goto_target == VCAP_IS2_CHAIN(0, pag))
116*4882a593Smuzhiyun 				return true;
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun 		return false;
119*4882a593Smuzhiyun 	}
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	/* Non-optional GOTO from VCAP IS2 lookup 0 to lookup 1.
122*4882a593Smuzhiyun 	 * We cannot change the PAG at this point.
123*4882a593Smuzhiyun 	 */
124*4882a593Smuzhiyun 	for (pag = 0; pag < VCAP_IS2_NUM_PAG; pag++)
125*4882a593Smuzhiyun 		if (chain == VCAP_IS2_CHAIN(0, pag))
126*4882a593Smuzhiyun 			return (goto_target == VCAP_IS2_CHAIN(1, pag));
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 	/* VCAP IS2 lookup 1 cannot jump anywhere */
129*4882a593Smuzhiyun 	return false;
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun static struct ocelot_vcap_filter *
ocelot_find_vcap_filter_that_points_at(struct ocelot * ocelot,int chain)133*4882a593Smuzhiyun ocelot_find_vcap_filter_that_points_at(struct ocelot *ocelot, int chain)
134*4882a593Smuzhiyun {
135*4882a593Smuzhiyun 	struct ocelot_vcap_filter *filter;
136*4882a593Smuzhiyun 	struct ocelot_vcap_block *block;
137*4882a593Smuzhiyun 	int block_id;
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	block_id = ocelot_chain_to_block(chain, true);
140*4882a593Smuzhiyun 	if (block_id < 0)
141*4882a593Smuzhiyun 		return NULL;
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	if (block_id == VCAP_IS2) {
144*4882a593Smuzhiyun 		block = &ocelot->block[VCAP_IS1];
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 		list_for_each_entry(filter, &block->rules, list)
147*4882a593Smuzhiyun 			if (filter->type == OCELOT_VCAP_FILTER_PAG &&
148*4882a593Smuzhiyun 			    filter->goto_target == chain)
149*4882a593Smuzhiyun 				return filter;
150*4882a593Smuzhiyun 	}
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 	list_for_each_entry(filter, &ocelot->dummy_rules, list)
153*4882a593Smuzhiyun 		if (filter->goto_target == chain)
154*4882a593Smuzhiyun 			return filter;
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 	return NULL;
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun 
ocelot_flower_parse_action(struct ocelot * ocelot,int port,bool ingress,struct flow_cls_offload * f,struct ocelot_vcap_filter * filter)159*4882a593Smuzhiyun static int ocelot_flower_parse_action(struct ocelot *ocelot, int port,
160*4882a593Smuzhiyun 				      bool ingress, struct flow_cls_offload *f,
161*4882a593Smuzhiyun 				      struct ocelot_vcap_filter *filter)
162*4882a593Smuzhiyun {
163*4882a593Smuzhiyun 	struct ocelot_port *ocelot_port = ocelot->ports[port];
164*4882a593Smuzhiyun 	struct netlink_ext_ack *extack = f->common.extack;
165*4882a593Smuzhiyun 	bool allow_missing_goto_target = false;
166*4882a593Smuzhiyun 	const struct flow_action_entry *a;
167*4882a593Smuzhiyun 	enum ocelot_tag_tpid_sel tpid;
168*4882a593Smuzhiyun 	int i, chain, egress_port;
169*4882a593Smuzhiyun 	u64 rate;
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 	if (!flow_action_basic_hw_stats_check(&f->rule->action,
172*4882a593Smuzhiyun 					      f->common.extack))
173*4882a593Smuzhiyun 		return -EOPNOTSUPP;
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 	chain = f->common.chain_index;
176*4882a593Smuzhiyun 	filter->block_id = ocelot_chain_to_block(chain, ingress);
177*4882a593Smuzhiyun 	if (filter->block_id < 0) {
178*4882a593Smuzhiyun 		NL_SET_ERR_MSG_MOD(extack, "Cannot offload to this chain");
179*4882a593Smuzhiyun 		return -EOPNOTSUPP;
180*4882a593Smuzhiyun 	}
181*4882a593Smuzhiyun 	if (filter->block_id == VCAP_IS1 || filter->block_id == VCAP_IS2)
182*4882a593Smuzhiyun 		filter->lookup = ocelot_chain_to_lookup(chain);
183*4882a593Smuzhiyun 	if (filter->block_id == VCAP_IS2)
184*4882a593Smuzhiyun 		filter->pag = ocelot_chain_to_pag(chain);
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 	filter->goto_target = -1;
187*4882a593Smuzhiyun 	filter->type = OCELOT_VCAP_FILTER_DUMMY;
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	flow_action_for_each(i, a, &f->rule->action) {
190*4882a593Smuzhiyun 		switch (a->id) {
191*4882a593Smuzhiyun 		case FLOW_ACTION_DROP:
192*4882a593Smuzhiyun 			if (filter->block_id != VCAP_IS2) {
193*4882a593Smuzhiyun 				NL_SET_ERR_MSG_MOD(extack,
194*4882a593Smuzhiyun 						   "Drop action can only be offloaded to VCAP IS2");
195*4882a593Smuzhiyun 				return -EOPNOTSUPP;
196*4882a593Smuzhiyun 			}
197*4882a593Smuzhiyun 			if (filter->goto_target != -1) {
198*4882a593Smuzhiyun 				NL_SET_ERR_MSG_MOD(extack,
199*4882a593Smuzhiyun 						   "Last action must be GOTO");
200*4882a593Smuzhiyun 				return -EOPNOTSUPP;
201*4882a593Smuzhiyun 			}
202*4882a593Smuzhiyun 			filter->action.mask_mode = OCELOT_MASK_MODE_PERMIT_DENY;
203*4882a593Smuzhiyun 			filter->action.port_mask = 0;
204*4882a593Smuzhiyun 			filter->action.police_ena = true;
205*4882a593Smuzhiyun 			filter->action.pol_ix = OCELOT_POLICER_DISCARD;
206*4882a593Smuzhiyun 			filter->type = OCELOT_VCAP_FILTER_OFFLOAD;
207*4882a593Smuzhiyun 			break;
208*4882a593Smuzhiyun 		case FLOW_ACTION_TRAP:
209*4882a593Smuzhiyun 			if (filter->block_id != VCAP_IS2 ||
210*4882a593Smuzhiyun 			    filter->lookup != 0) {
211*4882a593Smuzhiyun 				NL_SET_ERR_MSG_MOD(extack,
212*4882a593Smuzhiyun 						   "Trap action can only be offloaded to VCAP IS2 lookup 0");
213*4882a593Smuzhiyun 				return -EOPNOTSUPP;
214*4882a593Smuzhiyun 			}
215*4882a593Smuzhiyun 			if (filter->goto_target != -1) {
216*4882a593Smuzhiyun 				NL_SET_ERR_MSG_MOD(extack,
217*4882a593Smuzhiyun 						   "Last action must be GOTO");
218*4882a593Smuzhiyun 				return -EOPNOTSUPP;
219*4882a593Smuzhiyun 			}
220*4882a593Smuzhiyun 			filter->action.mask_mode = OCELOT_MASK_MODE_PERMIT_DENY;
221*4882a593Smuzhiyun 			filter->action.port_mask = 0;
222*4882a593Smuzhiyun 			filter->action.cpu_copy_ena = true;
223*4882a593Smuzhiyun 			filter->action.cpu_qu_num = 0;
224*4882a593Smuzhiyun 			filter->type = OCELOT_VCAP_FILTER_OFFLOAD;
225*4882a593Smuzhiyun 			break;
226*4882a593Smuzhiyun 		case FLOW_ACTION_POLICE:
227*4882a593Smuzhiyun 			if (filter->block_id != VCAP_IS2 ||
228*4882a593Smuzhiyun 			    filter->lookup != 0) {
229*4882a593Smuzhiyun 				NL_SET_ERR_MSG_MOD(extack,
230*4882a593Smuzhiyun 						   "Police action can only be offloaded to VCAP IS2 lookup 0");
231*4882a593Smuzhiyun 				return -EOPNOTSUPP;
232*4882a593Smuzhiyun 			}
233*4882a593Smuzhiyun 			if (filter->goto_target != -1) {
234*4882a593Smuzhiyun 				NL_SET_ERR_MSG_MOD(extack,
235*4882a593Smuzhiyun 						   "Last action must be GOTO");
236*4882a593Smuzhiyun 				return -EOPNOTSUPP;
237*4882a593Smuzhiyun 			}
238*4882a593Smuzhiyun 			filter->action.police_ena = true;
239*4882a593Smuzhiyun 			rate = a->police.rate_bytes_ps;
240*4882a593Smuzhiyun 			filter->action.pol.rate = div_u64(rate, 1000) * 8;
241*4882a593Smuzhiyun 			filter->action.pol.burst = a->police.burst;
242*4882a593Smuzhiyun 			filter->type = OCELOT_VCAP_FILTER_OFFLOAD;
243*4882a593Smuzhiyun 			break;
244*4882a593Smuzhiyun 		case FLOW_ACTION_REDIRECT:
245*4882a593Smuzhiyun 			if (filter->block_id != VCAP_IS2) {
246*4882a593Smuzhiyun 				NL_SET_ERR_MSG_MOD(extack,
247*4882a593Smuzhiyun 						   "Redirect action can only be offloaded to VCAP IS2");
248*4882a593Smuzhiyun 				return -EOPNOTSUPP;
249*4882a593Smuzhiyun 			}
250*4882a593Smuzhiyun 			if (filter->goto_target != -1) {
251*4882a593Smuzhiyun 				NL_SET_ERR_MSG_MOD(extack,
252*4882a593Smuzhiyun 						   "Last action must be GOTO");
253*4882a593Smuzhiyun 				return -EOPNOTSUPP;
254*4882a593Smuzhiyun 			}
255*4882a593Smuzhiyun 			egress_port = ocelot->ops->netdev_to_port(a->dev);
256*4882a593Smuzhiyun 			if (egress_port < 0) {
257*4882a593Smuzhiyun 				NL_SET_ERR_MSG_MOD(extack,
258*4882a593Smuzhiyun 						   "Destination not an ocelot port");
259*4882a593Smuzhiyun 				return -EOPNOTSUPP;
260*4882a593Smuzhiyun 			}
261*4882a593Smuzhiyun 			filter->action.mask_mode = OCELOT_MASK_MODE_REDIRECT;
262*4882a593Smuzhiyun 			filter->action.port_mask = BIT(egress_port);
263*4882a593Smuzhiyun 			filter->type = OCELOT_VCAP_FILTER_OFFLOAD;
264*4882a593Smuzhiyun 			break;
265*4882a593Smuzhiyun 		case FLOW_ACTION_VLAN_POP:
266*4882a593Smuzhiyun 			if (filter->block_id != VCAP_IS1) {
267*4882a593Smuzhiyun 				NL_SET_ERR_MSG_MOD(extack,
268*4882a593Smuzhiyun 						   "VLAN pop action can only be offloaded to VCAP IS1");
269*4882a593Smuzhiyun 				return -EOPNOTSUPP;
270*4882a593Smuzhiyun 			}
271*4882a593Smuzhiyun 			if (filter->goto_target != -1) {
272*4882a593Smuzhiyun 				NL_SET_ERR_MSG_MOD(extack,
273*4882a593Smuzhiyun 						   "Last action must be GOTO");
274*4882a593Smuzhiyun 				return -EOPNOTSUPP;
275*4882a593Smuzhiyun 			}
276*4882a593Smuzhiyun 			filter->action.vlan_pop_cnt_ena = true;
277*4882a593Smuzhiyun 			filter->action.vlan_pop_cnt++;
278*4882a593Smuzhiyun 			if (filter->action.vlan_pop_cnt > 2) {
279*4882a593Smuzhiyun 				NL_SET_ERR_MSG_MOD(extack,
280*4882a593Smuzhiyun 						   "Cannot pop more than 2 VLAN headers");
281*4882a593Smuzhiyun 				return -EOPNOTSUPP;
282*4882a593Smuzhiyun 			}
283*4882a593Smuzhiyun 			filter->type = OCELOT_VCAP_FILTER_OFFLOAD;
284*4882a593Smuzhiyun 			break;
285*4882a593Smuzhiyun 		case FLOW_ACTION_VLAN_MANGLE:
286*4882a593Smuzhiyun 			if (filter->block_id != VCAP_IS1) {
287*4882a593Smuzhiyun 				NL_SET_ERR_MSG_MOD(extack,
288*4882a593Smuzhiyun 						   "VLAN modify action can only be offloaded to VCAP IS1");
289*4882a593Smuzhiyun 				return -EOPNOTSUPP;
290*4882a593Smuzhiyun 			}
291*4882a593Smuzhiyun 			if (filter->goto_target != -1) {
292*4882a593Smuzhiyun 				NL_SET_ERR_MSG_MOD(extack,
293*4882a593Smuzhiyun 						   "Last action must be GOTO");
294*4882a593Smuzhiyun 				return -EOPNOTSUPP;
295*4882a593Smuzhiyun 			}
296*4882a593Smuzhiyun 			if (!ocelot_port->vlan_aware) {
297*4882a593Smuzhiyun 				NL_SET_ERR_MSG_MOD(extack,
298*4882a593Smuzhiyun 						   "Can only modify VLAN under VLAN aware bridge");
299*4882a593Smuzhiyun 				return -EOPNOTSUPP;
300*4882a593Smuzhiyun 			}
301*4882a593Smuzhiyun 			filter->action.vid_replace_ena = true;
302*4882a593Smuzhiyun 			filter->action.pcp_dei_ena = true;
303*4882a593Smuzhiyun 			filter->action.vid = a->vlan.vid;
304*4882a593Smuzhiyun 			filter->action.pcp = a->vlan.prio;
305*4882a593Smuzhiyun 			filter->type = OCELOT_VCAP_FILTER_OFFLOAD;
306*4882a593Smuzhiyun 			break;
307*4882a593Smuzhiyun 		case FLOW_ACTION_PRIORITY:
308*4882a593Smuzhiyun 			if (filter->block_id != VCAP_IS1) {
309*4882a593Smuzhiyun 				NL_SET_ERR_MSG_MOD(extack,
310*4882a593Smuzhiyun 						   "Priority action can only be offloaded to VCAP IS1");
311*4882a593Smuzhiyun 				return -EOPNOTSUPP;
312*4882a593Smuzhiyun 			}
313*4882a593Smuzhiyun 			if (filter->goto_target != -1) {
314*4882a593Smuzhiyun 				NL_SET_ERR_MSG_MOD(extack,
315*4882a593Smuzhiyun 						   "Last action must be GOTO");
316*4882a593Smuzhiyun 				return -EOPNOTSUPP;
317*4882a593Smuzhiyun 			}
318*4882a593Smuzhiyun 			filter->action.qos_ena = true;
319*4882a593Smuzhiyun 			filter->action.qos_val = a->priority;
320*4882a593Smuzhiyun 			filter->type = OCELOT_VCAP_FILTER_OFFLOAD;
321*4882a593Smuzhiyun 			break;
322*4882a593Smuzhiyun 		case FLOW_ACTION_GOTO:
323*4882a593Smuzhiyun 			filter->goto_target = a->chain_index;
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 			if (filter->block_id == VCAP_IS1 && filter->lookup == 2) {
326*4882a593Smuzhiyun 				int pag = ocelot_chain_to_pag(filter->goto_target);
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 				filter->action.pag_override_mask = 0xff;
329*4882a593Smuzhiyun 				filter->action.pag_val = pag;
330*4882a593Smuzhiyun 				filter->type = OCELOT_VCAP_FILTER_PAG;
331*4882a593Smuzhiyun 			}
332*4882a593Smuzhiyun 			break;
333*4882a593Smuzhiyun 		case FLOW_ACTION_VLAN_PUSH:
334*4882a593Smuzhiyun 			if (filter->block_id != VCAP_ES0) {
335*4882a593Smuzhiyun 				NL_SET_ERR_MSG_MOD(extack,
336*4882a593Smuzhiyun 						   "VLAN push action can only be offloaded to VCAP ES0");
337*4882a593Smuzhiyun 				return -EOPNOTSUPP;
338*4882a593Smuzhiyun 			}
339*4882a593Smuzhiyun 			switch (ntohs(a->vlan.proto)) {
340*4882a593Smuzhiyun 			case ETH_P_8021Q:
341*4882a593Smuzhiyun 				tpid = OCELOT_TAG_TPID_SEL_8021Q;
342*4882a593Smuzhiyun 				break;
343*4882a593Smuzhiyun 			case ETH_P_8021AD:
344*4882a593Smuzhiyun 				tpid = OCELOT_TAG_TPID_SEL_8021AD;
345*4882a593Smuzhiyun 				break;
346*4882a593Smuzhiyun 			default:
347*4882a593Smuzhiyun 				NL_SET_ERR_MSG_MOD(extack,
348*4882a593Smuzhiyun 						   "Cannot push custom TPID");
349*4882a593Smuzhiyun 				return -EOPNOTSUPP;
350*4882a593Smuzhiyun 			}
351*4882a593Smuzhiyun 			filter->action.tag_a_tpid_sel = tpid;
352*4882a593Smuzhiyun 			filter->action.push_outer_tag = OCELOT_ES0_TAG;
353*4882a593Smuzhiyun 			filter->action.tag_a_vid_sel = 1;
354*4882a593Smuzhiyun 			filter->action.vid_a_val = a->vlan.vid;
355*4882a593Smuzhiyun 			filter->action.pcp_a_val = a->vlan.prio;
356*4882a593Smuzhiyun 			filter->type = OCELOT_VCAP_FILTER_OFFLOAD;
357*4882a593Smuzhiyun 			break;
358*4882a593Smuzhiyun 		default:
359*4882a593Smuzhiyun 			NL_SET_ERR_MSG_MOD(extack, "Cannot offload action");
360*4882a593Smuzhiyun 			return -EOPNOTSUPP;
361*4882a593Smuzhiyun 		}
362*4882a593Smuzhiyun 	}
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun 	if (filter->goto_target == -1) {
365*4882a593Smuzhiyun 		if ((filter->block_id == VCAP_IS2 && filter->lookup == 1) ||
366*4882a593Smuzhiyun 		    chain == 0) {
367*4882a593Smuzhiyun 			allow_missing_goto_target = true;
368*4882a593Smuzhiyun 		} else {
369*4882a593Smuzhiyun 			NL_SET_ERR_MSG_MOD(extack, "Missing GOTO action");
370*4882a593Smuzhiyun 			return -EOPNOTSUPP;
371*4882a593Smuzhiyun 		}
372*4882a593Smuzhiyun 	}
373*4882a593Smuzhiyun 
374*4882a593Smuzhiyun 	if (!ocelot_is_goto_target_valid(filter->goto_target, chain, ingress) &&
375*4882a593Smuzhiyun 	    !allow_missing_goto_target) {
376*4882a593Smuzhiyun 		NL_SET_ERR_MSG_MOD(extack, "Cannot offload this GOTO target");
377*4882a593Smuzhiyun 		return -EOPNOTSUPP;
378*4882a593Smuzhiyun 	}
379*4882a593Smuzhiyun 
380*4882a593Smuzhiyun 	return 0;
381*4882a593Smuzhiyun }
382*4882a593Smuzhiyun 
ocelot_flower_parse_indev(struct ocelot * ocelot,int port,struct flow_cls_offload * f,struct ocelot_vcap_filter * filter)383*4882a593Smuzhiyun static int ocelot_flower_parse_indev(struct ocelot *ocelot, int port,
384*4882a593Smuzhiyun 				     struct flow_cls_offload *f,
385*4882a593Smuzhiyun 				     struct ocelot_vcap_filter *filter)
386*4882a593Smuzhiyun {
387*4882a593Smuzhiyun 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
388*4882a593Smuzhiyun 	const struct vcap_props *vcap = &ocelot->vcap[VCAP_ES0];
389*4882a593Smuzhiyun 	int key_length = vcap->keys[VCAP_ES0_IGR_PORT].length;
390*4882a593Smuzhiyun 	struct netlink_ext_ack *extack = f->common.extack;
391*4882a593Smuzhiyun 	struct net_device *dev, *indev;
392*4882a593Smuzhiyun 	struct flow_match_meta match;
393*4882a593Smuzhiyun 	int ingress_port;
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun 	flow_rule_match_meta(rule, &match);
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun 	if (!match.mask->ingress_ifindex)
398*4882a593Smuzhiyun 		return 0;
399*4882a593Smuzhiyun 
400*4882a593Smuzhiyun 	if (match.mask->ingress_ifindex != 0xFFFFFFFF) {
401*4882a593Smuzhiyun 		NL_SET_ERR_MSG_MOD(extack, "Unsupported ingress ifindex mask");
402*4882a593Smuzhiyun 		return -EOPNOTSUPP;
403*4882a593Smuzhiyun 	}
404*4882a593Smuzhiyun 
405*4882a593Smuzhiyun 	dev = ocelot->ops->port_to_netdev(ocelot, port);
406*4882a593Smuzhiyun 	if (!dev)
407*4882a593Smuzhiyun 		return -EINVAL;
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun 	indev = __dev_get_by_index(dev_net(dev), match.key->ingress_ifindex);
410*4882a593Smuzhiyun 	if (!indev) {
411*4882a593Smuzhiyun 		NL_SET_ERR_MSG_MOD(extack,
412*4882a593Smuzhiyun 				   "Can't find the ingress port to match on");
413*4882a593Smuzhiyun 		return -ENOENT;
414*4882a593Smuzhiyun 	}
415*4882a593Smuzhiyun 
416*4882a593Smuzhiyun 	ingress_port = ocelot->ops->netdev_to_port(indev);
417*4882a593Smuzhiyun 	if (ingress_port < 0) {
418*4882a593Smuzhiyun 		NL_SET_ERR_MSG_MOD(extack,
419*4882a593Smuzhiyun 				   "Can only offload an ocelot ingress port");
420*4882a593Smuzhiyun 		return -EOPNOTSUPP;
421*4882a593Smuzhiyun 	}
422*4882a593Smuzhiyun 	if (ingress_port == port) {
423*4882a593Smuzhiyun 		NL_SET_ERR_MSG_MOD(extack,
424*4882a593Smuzhiyun 				   "Ingress port is equal to the egress port");
425*4882a593Smuzhiyun 		return -EINVAL;
426*4882a593Smuzhiyun 	}
427*4882a593Smuzhiyun 
428*4882a593Smuzhiyun 	filter->ingress_port.value = ingress_port;
429*4882a593Smuzhiyun 	filter->ingress_port.mask = GENMASK(key_length - 1, 0);
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun 	return 0;
432*4882a593Smuzhiyun }
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun static int
ocelot_flower_parse_key(struct ocelot * ocelot,int port,bool ingress,struct flow_cls_offload * f,struct ocelot_vcap_filter * filter)435*4882a593Smuzhiyun ocelot_flower_parse_key(struct ocelot *ocelot, int port, bool ingress,
436*4882a593Smuzhiyun 			struct flow_cls_offload *f,
437*4882a593Smuzhiyun 			struct ocelot_vcap_filter *filter)
438*4882a593Smuzhiyun {
439*4882a593Smuzhiyun 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
440*4882a593Smuzhiyun 	struct flow_dissector *dissector = rule->match.dissector;
441*4882a593Smuzhiyun 	struct netlink_ext_ack *extack = f->common.extack;
442*4882a593Smuzhiyun 	u16 proto = ntohs(f->common.protocol);
443*4882a593Smuzhiyun 	bool match_protocol = true;
444*4882a593Smuzhiyun 	int ret;
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun 	if (dissector->used_keys &
447*4882a593Smuzhiyun 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
448*4882a593Smuzhiyun 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
449*4882a593Smuzhiyun 	      BIT(FLOW_DISSECTOR_KEY_META) |
450*4882a593Smuzhiyun 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
451*4882a593Smuzhiyun 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
452*4882a593Smuzhiyun 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
453*4882a593Smuzhiyun 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
454*4882a593Smuzhiyun 	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS))) {
455*4882a593Smuzhiyun 		return -EOPNOTSUPP;
456*4882a593Smuzhiyun 	}
457*4882a593Smuzhiyun 
458*4882a593Smuzhiyun 	/* For VCAP ES0 (egress rewriter) we can match on the ingress port */
459*4882a593Smuzhiyun 	if (!ingress) {
460*4882a593Smuzhiyun 		ret = ocelot_flower_parse_indev(ocelot, port, f, filter);
461*4882a593Smuzhiyun 		if (ret)
462*4882a593Smuzhiyun 			return ret;
463*4882a593Smuzhiyun 	}
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
466*4882a593Smuzhiyun 		struct flow_match_control match;
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun 		flow_rule_match_control(rule, &match);
469*4882a593Smuzhiyun 	}
470*4882a593Smuzhiyun 
471*4882a593Smuzhiyun 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
472*4882a593Smuzhiyun 		struct flow_match_eth_addrs match;
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun 		if (filter->block_id == VCAP_ES0) {
475*4882a593Smuzhiyun 			NL_SET_ERR_MSG_MOD(extack,
476*4882a593Smuzhiyun 					   "VCAP ES0 cannot match on MAC address");
477*4882a593Smuzhiyun 			return -EOPNOTSUPP;
478*4882a593Smuzhiyun 		}
479*4882a593Smuzhiyun 
480*4882a593Smuzhiyun 		/* The hw support mac matches only for MAC_ETYPE key,
481*4882a593Smuzhiyun 		 * therefore if other matches(port, tcp flags, etc) are added
482*4882a593Smuzhiyun 		 * then just bail out
483*4882a593Smuzhiyun 		 */
484*4882a593Smuzhiyun 		if ((dissector->used_keys &
485*4882a593Smuzhiyun 		    (BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
486*4882a593Smuzhiyun 		     BIT(FLOW_DISSECTOR_KEY_BASIC) |
487*4882a593Smuzhiyun 		     BIT(FLOW_DISSECTOR_KEY_CONTROL))) !=
488*4882a593Smuzhiyun 		    (BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
489*4882a593Smuzhiyun 		     BIT(FLOW_DISSECTOR_KEY_BASIC) |
490*4882a593Smuzhiyun 		     BIT(FLOW_DISSECTOR_KEY_CONTROL)))
491*4882a593Smuzhiyun 			return -EOPNOTSUPP;
492*4882a593Smuzhiyun 
493*4882a593Smuzhiyun 		flow_rule_match_eth_addrs(rule, &match);
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun 		if (filter->block_id == VCAP_IS1 &&
496*4882a593Smuzhiyun 		    !is_zero_ether_addr(match.mask->dst)) {
497*4882a593Smuzhiyun 			NL_SET_ERR_MSG_MOD(extack,
498*4882a593Smuzhiyun 					   "Key type S1_NORMAL cannot match on destination MAC");
499*4882a593Smuzhiyun 			return -EOPNOTSUPP;
500*4882a593Smuzhiyun 		}
501*4882a593Smuzhiyun 
502*4882a593Smuzhiyun 		filter->key_type = OCELOT_VCAP_KEY_ETYPE;
503*4882a593Smuzhiyun 		ether_addr_copy(filter->key.etype.dmac.value,
504*4882a593Smuzhiyun 				match.key->dst);
505*4882a593Smuzhiyun 		ether_addr_copy(filter->key.etype.smac.value,
506*4882a593Smuzhiyun 				match.key->src);
507*4882a593Smuzhiyun 		ether_addr_copy(filter->key.etype.dmac.mask,
508*4882a593Smuzhiyun 				match.mask->dst);
509*4882a593Smuzhiyun 		ether_addr_copy(filter->key.etype.smac.mask,
510*4882a593Smuzhiyun 				match.mask->src);
511*4882a593Smuzhiyun 		goto finished_key_parsing;
512*4882a593Smuzhiyun 	}
513*4882a593Smuzhiyun 
514*4882a593Smuzhiyun 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
515*4882a593Smuzhiyun 		struct flow_match_basic match;
516*4882a593Smuzhiyun 
517*4882a593Smuzhiyun 		flow_rule_match_basic(rule, &match);
518*4882a593Smuzhiyun 		if (ntohs(match.key->n_proto) == ETH_P_IP) {
519*4882a593Smuzhiyun 			if (filter->block_id == VCAP_ES0) {
520*4882a593Smuzhiyun 				NL_SET_ERR_MSG_MOD(extack,
521*4882a593Smuzhiyun 						   "VCAP ES0 cannot match on IP protocol");
522*4882a593Smuzhiyun 				return -EOPNOTSUPP;
523*4882a593Smuzhiyun 			}
524*4882a593Smuzhiyun 
525*4882a593Smuzhiyun 			filter->key_type = OCELOT_VCAP_KEY_IPV4;
526*4882a593Smuzhiyun 			filter->key.ipv4.proto.value[0] =
527*4882a593Smuzhiyun 				match.key->ip_proto;
528*4882a593Smuzhiyun 			filter->key.ipv4.proto.mask[0] =
529*4882a593Smuzhiyun 				match.mask->ip_proto;
530*4882a593Smuzhiyun 			match_protocol = false;
531*4882a593Smuzhiyun 		}
532*4882a593Smuzhiyun 		if (ntohs(match.key->n_proto) == ETH_P_IPV6) {
533*4882a593Smuzhiyun 			if (filter->block_id == VCAP_ES0) {
534*4882a593Smuzhiyun 				NL_SET_ERR_MSG_MOD(extack,
535*4882a593Smuzhiyun 						   "VCAP ES0 cannot match on IP protocol");
536*4882a593Smuzhiyun 				return -EOPNOTSUPP;
537*4882a593Smuzhiyun 			}
538*4882a593Smuzhiyun 
539*4882a593Smuzhiyun 			filter->key_type = OCELOT_VCAP_KEY_IPV6;
540*4882a593Smuzhiyun 			filter->key.ipv6.proto.value[0] =
541*4882a593Smuzhiyun 				match.key->ip_proto;
542*4882a593Smuzhiyun 			filter->key.ipv6.proto.mask[0] =
543*4882a593Smuzhiyun 				match.mask->ip_proto;
544*4882a593Smuzhiyun 			match_protocol = false;
545*4882a593Smuzhiyun 		}
546*4882a593Smuzhiyun 	}
547*4882a593Smuzhiyun 
548*4882a593Smuzhiyun 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IPV4_ADDRS) &&
549*4882a593Smuzhiyun 	    proto == ETH_P_IP) {
550*4882a593Smuzhiyun 		struct flow_match_ipv4_addrs match;
551*4882a593Smuzhiyun 		u8 *tmp;
552*4882a593Smuzhiyun 
553*4882a593Smuzhiyun 		if (filter->block_id == VCAP_ES0) {
554*4882a593Smuzhiyun 			NL_SET_ERR_MSG_MOD(extack,
555*4882a593Smuzhiyun 					   "VCAP ES0 cannot match on IP address");
556*4882a593Smuzhiyun 			return -EOPNOTSUPP;
557*4882a593Smuzhiyun 		}
558*4882a593Smuzhiyun 
559*4882a593Smuzhiyun 		flow_rule_match_ipv4_addrs(rule, &match);
560*4882a593Smuzhiyun 
561*4882a593Smuzhiyun 		if (filter->block_id == VCAP_IS1 && *(u32 *)&match.mask->dst) {
562*4882a593Smuzhiyun 			NL_SET_ERR_MSG_MOD(extack,
563*4882a593Smuzhiyun 					   "Key type S1_NORMAL cannot match on destination IP");
564*4882a593Smuzhiyun 			return -EOPNOTSUPP;
565*4882a593Smuzhiyun 		}
566*4882a593Smuzhiyun 
567*4882a593Smuzhiyun 		tmp = &filter->key.ipv4.sip.value.addr[0];
568*4882a593Smuzhiyun 		memcpy(tmp, &match.key->src, 4);
569*4882a593Smuzhiyun 
570*4882a593Smuzhiyun 		tmp = &filter->key.ipv4.sip.mask.addr[0];
571*4882a593Smuzhiyun 		memcpy(tmp, &match.mask->src, 4);
572*4882a593Smuzhiyun 
573*4882a593Smuzhiyun 		tmp = &filter->key.ipv4.dip.value.addr[0];
574*4882a593Smuzhiyun 		memcpy(tmp, &match.key->dst, 4);
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun 		tmp = &filter->key.ipv4.dip.mask.addr[0];
577*4882a593Smuzhiyun 		memcpy(tmp, &match.mask->dst, 4);
578*4882a593Smuzhiyun 		match_protocol = false;
579*4882a593Smuzhiyun 	}
580*4882a593Smuzhiyun 
581*4882a593Smuzhiyun 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IPV6_ADDRS) &&
582*4882a593Smuzhiyun 	    proto == ETH_P_IPV6) {
583*4882a593Smuzhiyun 		return -EOPNOTSUPP;
584*4882a593Smuzhiyun 	}
585*4882a593Smuzhiyun 
586*4882a593Smuzhiyun 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
587*4882a593Smuzhiyun 		struct flow_match_ports match;
588*4882a593Smuzhiyun 
589*4882a593Smuzhiyun 		if (filter->block_id == VCAP_ES0) {
590*4882a593Smuzhiyun 			NL_SET_ERR_MSG_MOD(extack,
591*4882a593Smuzhiyun 					   "VCAP ES0 cannot match on L4 ports");
592*4882a593Smuzhiyun 			return -EOPNOTSUPP;
593*4882a593Smuzhiyun 		}
594*4882a593Smuzhiyun 
595*4882a593Smuzhiyun 		flow_rule_match_ports(rule, &match);
596*4882a593Smuzhiyun 		filter->key.ipv4.sport.value = ntohs(match.key->src);
597*4882a593Smuzhiyun 		filter->key.ipv4.sport.mask = ntohs(match.mask->src);
598*4882a593Smuzhiyun 		filter->key.ipv4.dport.value = ntohs(match.key->dst);
599*4882a593Smuzhiyun 		filter->key.ipv4.dport.mask = ntohs(match.mask->dst);
600*4882a593Smuzhiyun 		match_protocol = false;
601*4882a593Smuzhiyun 	}
602*4882a593Smuzhiyun 
603*4882a593Smuzhiyun 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
604*4882a593Smuzhiyun 		struct flow_match_vlan match;
605*4882a593Smuzhiyun 
606*4882a593Smuzhiyun 		flow_rule_match_vlan(rule, &match);
607*4882a593Smuzhiyun 		filter->key_type = OCELOT_VCAP_KEY_ANY;
608*4882a593Smuzhiyun 		filter->vlan.vid.value = match.key->vlan_id;
609*4882a593Smuzhiyun 		filter->vlan.vid.mask = match.mask->vlan_id;
610*4882a593Smuzhiyun 		filter->vlan.pcp.value[0] = match.key->vlan_priority;
611*4882a593Smuzhiyun 		filter->vlan.pcp.mask[0] = match.mask->vlan_priority;
612*4882a593Smuzhiyun 		match_protocol = false;
613*4882a593Smuzhiyun 	}
614*4882a593Smuzhiyun 
615*4882a593Smuzhiyun finished_key_parsing:
616*4882a593Smuzhiyun 	if (match_protocol && proto != ETH_P_ALL) {
617*4882a593Smuzhiyun 		if (filter->block_id == VCAP_ES0) {
618*4882a593Smuzhiyun 			NL_SET_ERR_MSG_MOD(extack,
619*4882a593Smuzhiyun 					   "VCAP ES0 cannot match on L2 proto");
620*4882a593Smuzhiyun 			return -EOPNOTSUPP;
621*4882a593Smuzhiyun 		}
622*4882a593Smuzhiyun 
623*4882a593Smuzhiyun 		/* TODO: support SNAP, LLC etc */
624*4882a593Smuzhiyun 		if (proto < ETH_P_802_3_MIN)
625*4882a593Smuzhiyun 			return -EOPNOTSUPP;
626*4882a593Smuzhiyun 		filter->key_type = OCELOT_VCAP_KEY_ETYPE;
627*4882a593Smuzhiyun 		*(__be16 *)filter->key.etype.etype.value = htons(proto);
628*4882a593Smuzhiyun 		*(__be16 *)filter->key.etype.etype.mask = htons(0xffff);
629*4882a593Smuzhiyun 	}
630*4882a593Smuzhiyun 	/* else, a filter of type OCELOT_VCAP_KEY_ANY is implicitly added */
631*4882a593Smuzhiyun 
632*4882a593Smuzhiyun 	return 0;
633*4882a593Smuzhiyun }
634*4882a593Smuzhiyun 
ocelot_flower_parse(struct ocelot * ocelot,int port,bool ingress,struct flow_cls_offload * f,struct ocelot_vcap_filter * filter)635*4882a593Smuzhiyun static int ocelot_flower_parse(struct ocelot *ocelot, int port, bool ingress,
636*4882a593Smuzhiyun 			       struct flow_cls_offload *f,
637*4882a593Smuzhiyun 			       struct ocelot_vcap_filter *filter)
638*4882a593Smuzhiyun {
639*4882a593Smuzhiyun 	int ret;
640*4882a593Smuzhiyun 
641*4882a593Smuzhiyun 	filter->prio = f->common.prio;
642*4882a593Smuzhiyun 	filter->id = f->cookie;
643*4882a593Smuzhiyun 
644*4882a593Smuzhiyun 	ret = ocelot_flower_parse_action(ocelot, port, ingress, f, filter);
645*4882a593Smuzhiyun 	if (ret)
646*4882a593Smuzhiyun 		return ret;
647*4882a593Smuzhiyun 
648*4882a593Smuzhiyun 	return ocelot_flower_parse_key(ocelot, port, ingress, f, filter);
649*4882a593Smuzhiyun }
650*4882a593Smuzhiyun 
651*4882a593Smuzhiyun static struct ocelot_vcap_filter
ocelot_vcap_filter_create(struct ocelot * ocelot,int port,bool ingress,struct flow_cls_offload * f)652*4882a593Smuzhiyun *ocelot_vcap_filter_create(struct ocelot *ocelot, int port, bool ingress,
653*4882a593Smuzhiyun 			   struct flow_cls_offload *f)
654*4882a593Smuzhiyun {
655*4882a593Smuzhiyun 	struct ocelot_vcap_filter *filter;
656*4882a593Smuzhiyun 
657*4882a593Smuzhiyun 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
658*4882a593Smuzhiyun 	if (!filter)
659*4882a593Smuzhiyun 		return NULL;
660*4882a593Smuzhiyun 
661*4882a593Smuzhiyun 	if (ingress) {
662*4882a593Smuzhiyun 		filter->ingress_port_mask = BIT(port);
663*4882a593Smuzhiyun 	} else {
664*4882a593Smuzhiyun 		const struct vcap_props *vcap = &ocelot->vcap[VCAP_ES0];
665*4882a593Smuzhiyun 		int key_length = vcap->keys[VCAP_ES0_EGR_PORT].length;
666*4882a593Smuzhiyun 
667*4882a593Smuzhiyun 		filter->egress_port.value = port;
668*4882a593Smuzhiyun 		filter->egress_port.mask = GENMASK(key_length - 1, 0);
669*4882a593Smuzhiyun 	}
670*4882a593Smuzhiyun 
671*4882a593Smuzhiyun 	return filter;
672*4882a593Smuzhiyun }
673*4882a593Smuzhiyun 
ocelot_vcap_dummy_filter_add(struct ocelot * ocelot,struct ocelot_vcap_filter * filter)674*4882a593Smuzhiyun static int ocelot_vcap_dummy_filter_add(struct ocelot *ocelot,
675*4882a593Smuzhiyun 					struct ocelot_vcap_filter *filter)
676*4882a593Smuzhiyun {
677*4882a593Smuzhiyun 	list_add(&filter->list, &ocelot->dummy_rules);
678*4882a593Smuzhiyun 
679*4882a593Smuzhiyun 	return 0;
680*4882a593Smuzhiyun }
681*4882a593Smuzhiyun 
ocelot_vcap_dummy_filter_del(struct ocelot * ocelot,struct ocelot_vcap_filter * filter)682*4882a593Smuzhiyun static int ocelot_vcap_dummy_filter_del(struct ocelot *ocelot,
683*4882a593Smuzhiyun 					struct ocelot_vcap_filter *filter)
684*4882a593Smuzhiyun {
685*4882a593Smuzhiyun 	list_del(&filter->list);
686*4882a593Smuzhiyun 	kfree(filter);
687*4882a593Smuzhiyun 
688*4882a593Smuzhiyun 	return 0;
689*4882a593Smuzhiyun }
690*4882a593Smuzhiyun 
ocelot_cls_flower_replace(struct ocelot * ocelot,int port,struct flow_cls_offload * f,bool ingress)691*4882a593Smuzhiyun int ocelot_cls_flower_replace(struct ocelot *ocelot, int port,
692*4882a593Smuzhiyun 			      struct flow_cls_offload *f, bool ingress)
693*4882a593Smuzhiyun {
694*4882a593Smuzhiyun 	struct netlink_ext_ack *extack = f->common.extack;
695*4882a593Smuzhiyun 	struct ocelot_vcap_filter *filter;
696*4882a593Smuzhiyun 	int chain = f->common.chain_index;
697*4882a593Smuzhiyun 	int ret;
698*4882a593Smuzhiyun 
699*4882a593Smuzhiyun 	if (chain && !ocelot_find_vcap_filter_that_points_at(ocelot, chain)) {
700*4882a593Smuzhiyun 		NL_SET_ERR_MSG_MOD(extack, "No default GOTO action points to this chain");
701*4882a593Smuzhiyun 		return -EOPNOTSUPP;
702*4882a593Smuzhiyun 	}
703*4882a593Smuzhiyun 
704*4882a593Smuzhiyun 	filter = ocelot_vcap_filter_create(ocelot, port, ingress, f);
705*4882a593Smuzhiyun 	if (!filter)
706*4882a593Smuzhiyun 		return -ENOMEM;
707*4882a593Smuzhiyun 
708*4882a593Smuzhiyun 	ret = ocelot_flower_parse(ocelot, port, ingress, f, filter);
709*4882a593Smuzhiyun 	if (ret) {
710*4882a593Smuzhiyun 		kfree(filter);
711*4882a593Smuzhiyun 		return ret;
712*4882a593Smuzhiyun 	}
713*4882a593Smuzhiyun 
714*4882a593Smuzhiyun 	/* The non-optional GOTOs for the TCAM skeleton don't need
715*4882a593Smuzhiyun 	 * to be actually offloaded.
716*4882a593Smuzhiyun 	 */
717*4882a593Smuzhiyun 	if (filter->type == OCELOT_VCAP_FILTER_DUMMY)
718*4882a593Smuzhiyun 		return ocelot_vcap_dummy_filter_add(ocelot, filter);
719*4882a593Smuzhiyun 
720*4882a593Smuzhiyun 	return ocelot_vcap_filter_add(ocelot, filter, f->common.extack);
721*4882a593Smuzhiyun }
722*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(ocelot_cls_flower_replace);
723*4882a593Smuzhiyun 
ocelot_cls_flower_destroy(struct ocelot * ocelot,int port,struct flow_cls_offload * f,bool ingress)724*4882a593Smuzhiyun int ocelot_cls_flower_destroy(struct ocelot *ocelot, int port,
725*4882a593Smuzhiyun 			      struct flow_cls_offload *f, bool ingress)
726*4882a593Smuzhiyun {
727*4882a593Smuzhiyun 	struct ocelot_vcap_filter *filter;
728*4882a593Smuzhiyun 	struct ocelot_vcap_block *block;
729*4882a593Smuzhiyun 	int block_id;
730*4882a593Smuzhiyun 
731*4882a593Smuzhiyun 	block_id = ocelot_chain_to_block(f->common.chain_index, ingress);
732*4882a593Smuzhiyun 	if (block_id < 0)
733*4882a593Smuzhiyun 		return 0;
734*4882a593Smuzhiyun 
735*4882a593Smuzhiyun 	block = &ocelot->block[block_id];
736*4882a593Smuzhiyun 
737*4882a593Smuzhiyun 	filter = ocelot_vcap_block_find_filter_by_id(block, f->cookie);
738*4882a593Smuzhiyun 	if (!filter)
739*4882a593Smuzhiyun 		return 0;
740*4882a593Smuzhiyun 
741*4882a593Smuzhiyun 	if (filter->type == OCELOT_VCAP_FILTER_DUMMY)
742*4882a593Smuzhiyun 		return ocelot_vcap_dummy_filter_del(ocelot, filter);
743*4882a593Smuzhiyun 
744*4882a593Smuzhiyun 	return ocelot_vcap_filter_del(ocelot, filter);
745*4882a593Smuzhiyun }
746*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(ocelot_cls_flower_destroy);
747*4882a593Smuzhiyun 
ocelot_cls_flower_stats(struct ocelot * ocelot,int port,struct flow_cls_offload * f,bool ingress)748*4882a593Smuzhiyun int ocelot_cls_flower_stats(struct ocelot *ocelot, int port,
749*4882a593Smuzhiyun 			    struct flow_cls_offload *f, bool ingress)
750*4882a593Smuzhiyun {
751*4882a593Smuzhiyun 	struct ocelot_vcap_filter *filter;
752*4882a593Smuzhiyun 	struct ocelot_vcap_block *block;
753*4882a593Smuzhiyun 	int block_id, ret;
754*4882a593Smuzhiyun 
755*4882a593Smuzhiyun 	block_id = ocelot_chain_to_block(f->common.chain_index, ingress);
756*4882a593Smuzhiyun 	if (block_id < 0)
757*4882a593Smuzhiyun 		return 0;
758*4882a593Smuzhiyun 
759*4882a593Smuzhiyun 	block = &ocelot->block[block_id];
760*4882a593Smuzhiyun 
761*4882a593Smuzhiyun 	filter = ocelot_vcap_block_find_filter_by_id(block, f->cookie);
762*4882a593Smuzhiyun 	if (!filter || filter->type == OCELOT_VCAP_FILTER_DUMMY)
763*4882a593Smuzhiyun 		return 0;
764*4882a593Smuzhiyun 
765*4882a593Smuzhiyun 	ret = ocelot_vcap_filter_stats_update(ocelot, filter);
766*4882a593Smuzhiyun 	if (ret)
767*4882a593Smuzhiyun 		return ret;
768*4882a593Smuzhiyun 
769*4882a593Smuzhiyun 	flow_stats_update(&f->stats, 0x0, filter->stats.pkts, 0, 0x0,
770*4882a593Smuzhiyun 			  FLOW_ACTION_HW_STATS_IMMEDIATE);
771*4882a593Smuzhiyun 	return 0;
772*4882a593Smuzhiyun }
773*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(ocelot_cls_flower_stats);
774