55
66#include <net/pkt_cls.h>
77#include <net/tc_act/tc_gact.h>
8-
8+ #include <soc/mscc/ocelot_vcap.h>
99#include "ocelot_vcap.h"
1010
11- static int ocelot_flower_parse_action (struct flow_cls_offload * f ,
11+ /* Arbitrarily chosen constants for encoding the VCAP block and lookup number
12+ * into the chain number. This is UAPI.
13+ */
14+ #define VCAP_BLOCK 10000
15+ #define VCAP_LOOKUP 1000
16+ #define VCAP_IS1_NUM_LOOKUPS 3
17+ #define VCAP_IS2_NUM_LOOKUPS 2
18+ #define VCAP_IS2_NUM_PAG 256
19+ #define VCAP_IS1_CHAIN (lookup ) \
20+ (1 * VCAP_BLOCK + (lookup) * VCAP_LOOKUP)
21+ #define VCAP_IS2_CHAIN (lookup , pag ) \
22+ (2 * VCAP_BLOCK + (lookup) * VCAP_LOOKUP + (pag))
23+
24+ static int ocelot_chain_to_block (int chain , bool ingress )
25+ {
26+ int lookup , pag ;
27+
28+ if (!ingress ) {
29+ if (chain == 0 )
30+ return VCAP_ES0 ;
31+ return - EOPNOTSUPP ;
32+ }
33+
34+ /* Backwards compatibility with older, single-chain tc-flower
35+ * offload support in Ocelot
36+ */
37+ if (chain == 0 )
38+ return VCAP_IS2 ;
39+
40+ for (lookup = 0 ; lookup < VCAP_IS1_NUM_LOOKUPS ; lookup ++ )
41+ if (chain == VCAP_IS1_CHAIN (lookup ))
42+ return VCAP_IS1 ;
43+
44+ for (lookup = 0 ; lookup < VCAP_IS2_NUM_LOOKUPS ; lookup ++ )
45+ for (pag = 0 ; pag < VCAP_IS2_NUM_PAG ; pag ++ )
46+ if (chain == VCAP_IS2_CHAIN (lookup , pag ))
47+ return VCAP_IS2 ;
48+
49+ return - EOPNOTSUPP ;
50+ }
51+
52+ /* Caller must ensure this is a valid IS1 or IS2 chain first,
53+ * by calling ocelot_chain_to_block.
54+ */
55+ static int ocelot_chain_to_lookup (int chain )
56+ {
57+ return (chain / VCAP_LOOKUP ) % 10 ;
58+ }
59+
60+ static bool ocelot_is_goto_target_valid (int goto_target , int chain ,
61+ bool ingress )
62+ {
63+ int pag ;
64+
65+ /* Can't offload GOTO in VCAP ES0 */
66+ if (!ingress )
67+ return (goto_target < 0 );
68+
69+ /* Non-optional GOTOs */
70+ if (chain == 0 )
71+ /* VCAP IS1 can be skipped, either partially or completely */
72+ return (goto_target == VCAP_IS1_CHAIN (0 ) ||
73+ goto_target == VCAP_IS1_CHAIN (1 ) ||
74+ goto_target == VCAP_IS1_CHAIN (2 ) ||
75+ goto_target == VCAP_IS2_CHAIN (0 , 0 ) ||
76+ goto_target == VCAP_IS2_CHAIN (1 , 0 ));
77+
78+ if (chain == VCAP_IS1_CHAIN (0 ))
79+ return (goto_target == VCAP_IS1_CHAIN (1 ));
80+
81+ if (chain == VCAP_IS1_CHAIN (1 ))
82+ return (goto_target == VCAP_IS1_CHAIN (2 ));
83+
84+ /* Lookup 2 of VCAP IS1 can really support non-optional GOTOs,
85+ * using a Policy Association Group (PAG) value, which is an 8-bit
86+ * value encoding a VCAP IS2 target chain.
87+ */
88+ if (chain == VCAP_IS1_CHAIN (2 )) {
89+ for (pag = 0 ; pag < VCAP_IS2_NUM_PAG ; pag ++ )
90+ if (goto_target == VCAP_IS2_CHAIN (0 , pag ))
91+ return true;
92+
93+ return false;
94+ }
95+
96+ /* Non-optional GOTO from VCAP IS2 lookup 0 to lookup 1.
97+ * We cannot change the PAG at this point.
98+ */
99+ for (pag = 0 ; pag < VCAP_IS2_NUM_PAG ; pag ++ )
100+ if (chain == VCAP_IS2_CHAIN (0 , pag ))
101+ return (goto_target == VCAP_IS2_CHAIN (1 , pag ));
102+
103+ /* VCAP IS2 lookup 1 cannot jump anywhere */
104+ return false;
105+ }
106+
107+ static struct ocelot_vcap_filter *
108+ ocelot_find_vcap_filter_that_points_at (struct ocelot * ocelot , int chain )
109+ {
110+ struct ocelot_vcap_filter * filter ;
111+ struct ocelot_vcap_block * block ;
112+ int block_id ;
113+
114+ block_id = ocelot_chain_to_block (chain , true);
115+ if (block_id < 0 )
116+ return NULL ;
117+
118+ if (block_id == VCAP_IS2 ) {
119+ block = & ocelot -> block [VCAP_IS1 ];
120+
121+ list_for_each_entry (filter , & block -> rules , list )
122+ if (filter -> type == OCELOT_VCAP_FILTER_PAG &&
123+ filter -> goto_target == chain )
124+ return filter ;
125+ }
126+
127+ list_for_each_entry (filter , & ocelot -> dummy_rules , list )
128+ if (filter -> goto_target == chain )
129+ return filter ;
130+
131+ return NULL ;
132+ }
133+
134+ static int ocelot_flower_parse_action (struct flow_cls_offload * f , bool ingress ,
12135 struct ocelot_vcap_filter * filter )
13136{
137+ struct netlink_ext_ack * extack = f -> common .extack ;
138+ bool allow_missing_goto_target = false;
14139 const struct flow_action_entry * a ;
140+ int i , chain ;
15141 u64 rate ;
16- int i ;
17142
18143 if (!flow_action_basic_hw_stats_check (& f -> rule -> action ,
19144 f -> common .extack ))
20145 return - EOPNOTSUPP ;
21146
147+ chain = f -> common .chain_index ;
148+ filter -> block_id = ocelot_chain_to_block (chain , ingress );
149+ if (filter -> block_id < 0 ) {
150+ NL_SET_ERR_MSG_MOD (extack , "Cannot offload to this chain" );
151+ return - EOPNOTSUPP ;
152+ }
153+ if (filter -> block_id == VCAP_IS1 || filter -> block_id == VCAP_IS2 )
154+ filter -> lookup = ocelot_chain_to_lookup (chain );
155+
156+ filter -> goto_target = -1 ;
157+ filter -> type = OCELOT_VCAP_FILTER_DUMMY ;
158+
22159 flow_action_for_each (i , a , & f -> rule -> action ) {
23160 switch (a -> id ) {
24161 case FLOW_ACTION_DROP :
162+ if (filter -> block_id != VCAP_IS2 ) {
163+ NL_SET_ERR_MSG_MOD (extack ,
164+ "Drop action can only be offloaded to VCAP IS2" );
165+ return - EOPNOTSUPP ;
166+ }
167+ if (filter -> goto_target != -1 ) {
168+ NL_SET_ERR_MSG_MOD (extack ,
169+ "Last action must be GOTO" );
170+ return - EOPNOTSUPP ;
171+ }
25172 filter -> action .mask_mode = OCELOT_MASK_MODE_PERMIT_DENY ;
26173 filter -> action .port_mask = 0 ;
27174 filter -> action .police_ena = true;
28175 filter -> action .pol_ix = OCELOT_POLICER_DISCARD ;
176+ filter -> type = OCELOT_VCAP_FILTER_OFFLOAD ;
29177 break ;
30178 case FLOW_ACTION_TRAP :
179+ if (filter -> block_id != VCAP_IS2 ) {
180+ NL_SET_ERR_MSG_MOD (extack ,
181+ "Trap action can only be offloaded to VCAP IS2" );
182+ return - EOPNOTSUPP ;
183+ }
184+ if (filter -> goto_target != -1 ) {
185+ NL_SET_ERR_MSG_MOD (extack ,
186+ "Last action must be GOTO" );
187+ return - EOPNOTSUPP ;
188+ }
31189 filter -> action .mask_mode = OCELOT_MASK_MODE_PERMIT_DENY ;
32190 filter -> action .port_mask = 0 ;
33191 filter -> action .cpu_copy_ena = true;
34192 filter -> action .cpu_qu_num = 0 ;
193+ filter -> type = OCELOT_VCAP_FILTER_OFFLOAD ;
35194 break ;
36195 case FLOW_ACTION_POLICE :
196+ if (filter -> block_id != VCAP_IS2 ) {
197+ NL_SET_ERR_MSG_MOD (extack ,
198+ "Police action can only be offloaded to VCAP IS2" );
199+ return - EOPNOTSUPP ;
200+ }
201+ if (filter -> goto_target != -1 ) {
202+ NL_SET_ERR_MSG_MOD (extack ,
203+ "Last action must be GOTO" );
204+ return - EOPNOTSUPP ;
205+ }
37206 filter -> action .police_ena = true;
38207 rate = a -> police .rate_bytes_ps ;
39208 filter -> action .pol .rate = div_u64 (rate , 1000 ) * 8 ;
40209 filter -> action .pol .burst = a -> police .burst ;
210+ filter -> type = OCELOT_VCAP_FILTER_OFFLOAD ;
211+ break ;
212+ case FLOW_ACTION_GOTO :
213+ filter -> goto_target = a -> chain_index ;
41214 break ;
42215 default :
216+ NL_SET_ERR_MSG_MOD (extack , "Cannot offload action" );
43217 return - EOPNOTSUPP ;
44218 }
45219 }
46220
221+ if (filter -> goto_target == -1 ) {
222+ if ((filter -> block_id == VCAP_IS2 && filter -> lookup == 1 ) ||
223+ chain == 0 ) {
224+ allow_missing_goto_target = true;
225+ } else {
226+ NL_SET_ERR_MSG_MOD (extack , "Missing GOTO action" );
227+ return - EOPNOTSUPP ;
228+ }
229+ }
230+
231+ if (!ocelot_is_goto_target_valid (filter -> goto_target , chain , ingress ) &&
232+ !allow_missing_goto_target ) {
233+ NL_SET_ERR_MSG_MOD (extack , "Cannot offload this GOTO target" );
234+ return - EOPNOTSUPP ;
235+ }
236+
47237 return 0 ;
48238}
49239
50- static int ocelot_flower_parse_key (struct flow_cls_offload * f ,
240+ static int ocelot_flower_parse_key (struct flow_cls_offload * f , bool ingress ,
51241 struct ocelot_vcap_filter * filter )
52242{
53243 struct flow_rule * rule = flow_cls_offload_flow_rule (f );
@@ -185,19 +375,19 @@ static int ocelot_flower_parse_key(struct flow_cls_offload *f,
185375 return 0 ;
186376}
187377
188- static int ocelot_flower_parse (struct flow_cls_offload * f ,
378+ static int ocelot_flower_parse (struct flow_cls_offload * f , bool ingress ,
189379 struct ocelot_vcap_filter * filter )
190380{
191381 int ret ;
192382
193383 filter -> prio = f -> common .prio ;
194384 filter -> id = f -> cookie ;
195385
196- ret = ocelot_flower_parse_action (f , filter );
386+ ret = ocelot_flower_parse_action (f , ingress , filter );
197387 if (ret )
198388 return ret ;
199389
200- return ocelot_flower_parse_key (f , filter );
390+ return ocelot_flower_parse_key (f , ingress , filter );
201391}
202392
203393static struct ocelot_vcap_filter
@@ -214,49 +404,95 @@ static struct ocelot_vcap_filter
214404 return filter ;
215405}
216406
407+ static int ocelot_vcap_dummy_filter_add (struct ocelot * ocelot ,
408+ struct ocelot_vcap_filter * filter )
409+ {
410+ list_add (& filter -> list , & ocelot -> dummy_rules );
411+
412+ return 0 ;
413+ }
414+
415+ static int ocelot_vcap_dummy_filter_del (struct ocelot * ocelot ,
416+ struct ocelot_vcap_filter * filter )
417+ {
418+ list_del (& filter -> list );
419+ kfree (filter );
420+
421+ return 0 ;
422+ }
423+
217424int ocelot_cls_flower_replace (struct ocelot * ocelot , int port ,
218425 struct flow_cls_offload * f , bool ingress )
219426{
427+ struct netlink_ext_ack * extack = f -> common .extack ;
220428 struct ocelot_vcap_filter * filter ;
429+ int chain = f -> common .chain_index ;
221430 int ret ;
222431
432+ if (chain && !ocelot_find_vcap_filter_that_points_at (ocelot , chain )) {
433+ NL_SET_ERR_MSG_MOD (extack , "No default GOTO action points to this chain" );
434+ return - EOPNOTSUPP ;
435+ }
436+
223437 filter = ocelot_vcap_filter_create (ocelot , port , f );
224438 if (!filter )
225439 return - ENOMEM ;
226440
227- ret = ocelot_flower_parse (f , filter );
441+ ret = ocelot_flower_parse (f , ingress , filter );
228442 if (ret ) {
229443 kfree (filter );
230444 return ret ;
231445 }
232446
447+ /* The non-optional GOTOs for the TCAM skeleton don't need
448+ * to be actually offloaded.
449+ */
450+ if (filter -> type == OCELOT_VCAP_FILTER_DUMMY )
451+ return ocelot_vcap_dummy_filter_add (ocelot , filter );
452+
233453 return ocelot_vcap_filter_add (ocelot , filter , f -> common .extack );
234454}
235455EXPORT_SYMBOL_GPL (ocelot_cls_flower_replace );
236456
237457int ocelot_cls_flower_destroy (struct ocelot * ocelot , int port ,
238458 struct flow_cls_offload * f , bool ingress )
239459{
240- struct ocelot_vcap_block * block = & ocelot -> block ;
241460 struct ocelot_vcap_filter * filter ;
461+ struct ocelot_vcap_block * block ;
462+ int block_id ;
463+
464+ block_id = ocelot_chain_to_block (f -> common .chain_index , ingress );
465+ if (block_id < 0 )
466+ return 0 ;
467+
468+ block = & ocelot -> block [block_id ];
242469
243470 filter = ocelot_vcap_block_find_filter_by_id (block , f -> cookie );
244471 if (!filter )
245472 return 0 ;
246473
474+ if (filter -> type == OCELOT_VCAP_FILTER_DUMMY )
475+ return ocelot_vcap_dummy_filter_del (ocelot , filter );
476+
247477 return ocelot_vcap_filter_del (ocelot , filter );
248478}
249479EXPORT_SYMBOL_GPL (ocelot_cls_flower_destroy );
250480
251481int ocelot_cls_flower_stats (struct ocelot * ocelot , int port ,
252482 struct flow_cls_offload * f , bool ingress )
253483{
254- struct ocelot_vcap_block * block = & ocelot -> block ;
255484 struct ocelot_vcap_filter * filter ;
256- int ret ;
485+ struct ocelot_vcap_block * block ;
486+ int block_id , ret ;
487+
488+ block_id = ocelot_chain_to_block (f -> common .chain_index , ingress );
489+ if (block_id < 0 )
490+ return 0 ;
491+
492+ block = & ocelot -> block [block_id ];
257493
258494 filter = ocelot_vcap_block_find_filter_by_id (block , f -> cookie );
259- if (!filter )
495+ if (!filter || filter -> type == OCELOT_VCAP_FILTER_DUMMY )
260496 return 0 ;
261497
262498 ret = ocelot_vcap_filter_stats_update (ocelot , filter );
0 commit comments