1313#include <soc/mscc/ocelot_ana.h>
1414#include <soc/mscc/ocelot_ptp.h>
1515#include <soc/mscc/ocelot.h>
16+ #include <linux/dsa/8021q.h>
1617#include <linux/platform_device.h>
1718#include <linux/packing.h>
1819#include <linux/module.h>
2425#include <net/dsa.h>
2526#include "felix.h"
2627
28+ static int felix_tag_8021q_rxvlan_add (struct felix * felix , int port , u16 vid ,
29+ bool pvid , bool untagged )
30+ {
31+ struct ocelot_vcap_filter * outer_tagging_rule ;
32+ struct ocelot * ocelot = & felix -> ocelot ;
33+ struct dsa_switch * ds = felix -> ds ;
34+ int key_length , upstream , err ;
35+
36+ /* We don't need to install the rxvlan into the other ports' filtering
37+ * tables, because we're just pushing the rxvlan when sending towards
38+ * the CPU
39+ */
40+ if (!pvid )
41+ return 0 ;
42+
43+ key_length = ocelot -> vcap [VCAP_ES0 ].keys [VCAP_ES0_IGR_PORT ].length ;
44+ upstream = dsa_upstream_port (ds , port );
45+
46+ outer_tagging_rule = kzalloc (sizeof (struct ocelot_vcap_filter ),
47+ GFP_KERNEL );
48+ if (!outer_tagging_rule )
49+ return - ENOMEM ;
50+
51+ outer_tagging_rule -> key_type = OCELOT_VCAP_KEY_ANY ;
52+ outer_tagging_rule -> prio = 1 ;
53+ outer_tagging_rule -> id .cookie = port ;
54+ outer_tagging_rule -> id .tc_offload = false;
55+ outer_tagging_rule -> block_id = VCAP_ES0 ;
56+ outer_tagging_rule -> type = OCELOT_VCAP_FILTER_OFFLOAD ;
57+ outer_tagging_rule -> lookup = 0 ;
58+ outer_tagging_rule -> ingress_port .value = port ;
59+ outer_tagging_rule -> ingress_port .mask = GENMASK (key_length - 1 , 0 );
60+ outer_tagging_rule -> egress_port .value = upstream ;
61+ outer_tagging_rule -> egress_port .mask = GENMASK (key_length - 1 , 0 );
62+ outer_tagging_rule -> action .push_outer_tag = OCELOT_ES0_TAG ;
63+ outer_tagging_rule -> action .tag_a_tpid_sel = OCELOT_TAG_TPID_SEL_8021AD ;
64+ outer_tagging_rule -> action .tag_a_vid_sel = 1 ;
65+ outer_tagging_rule -> action .vid_a_val = vid ;
66+
67+ err = ocelot_vcap_filter_add (ocelot , outer_tagging_rule , NULL );
68+ if (err )
69+ kfree (outer_tagging_rule );
70+
71+ return err ;
72+ }
73+
74+ static int felix_tag_8021q_txvlan_add (struct felix * felix , int port , u16 vid ,
75+ bool pvid , bool untagged )
76+ {
77+ struct ocelot_vcap_filter * untagging_rule , * redirect_rule ;
78+ struct ocelot * ocelot = & felix -> ocelot ;
79+ struct dsa_switch * ds = felix -> ds ;
80+ int upstream , err ;
81+
82+ /* tag_8021q.c assumes we are implementing this via port VLAN
83+ * membership, which we aren't. So we don't need to add any VCAP filter
84+ * for the CPU port.
85+ */
86+ if (ocelot -> ports [port ]-> is_dsa_8021q_cpu )
87+ return 0 ;
88+
89+ untagging_rule = kzalloc (sizeof (struct ocelot_vcap_filter ), GFP_KERNEL );
90+ if (!untagging_rule )
91+ return - ENOMEM ;
92+
93+ redirect_rule = kzalloc (sizeof (struct ocelot_vcap_filter ), GFP_KERNEL );
94+ if (!redirect_rule ) {
95+ kfree (untagging_rule );
96+ return - ENOMEM ;
97+ }
98+
99+ upstream = dsa_upstream_port (ds , port );
100+
101+ untagging_rule -> key_type = OCELOT_VCAP_KEY_ANY ;
102+ untagging_rule -> ingress_port_mask = BIT (upstream );
103+ untagging_rule -> vlan .vid .value = vid ;
104+ untagging_rule -> vlan .vid .mask = VLAN_VID_MASK ;
105+ untagging_rule -> prio = 1 ;
106+ untagging_rule -> id .cookie = port ;
107+ untagging_rule -> id .tc_offload = false;
108+ untagging_rule -> block_id = VCAP_IS1 ;
109+ untagging_rule -> type = OCELOT_VCAP_FILTER_OFFLOAD ;
110+ untagging_rule -> lookup = 0 ;
111+ untagging_rule -> action .vlan_pop_cnt_ena = true;
112+ untagging_rule -> action .vlan_pop_cnt = 1 ;
113+ untagging_rule -> action .pag_override_mask = 0xff ;
114+ untagging_rule -> action .pag_val = port ;
115+
116+ err = ocelot_vcap_filter_add (ocelot , untagging_rule , NULL );
117+ if (err ) {
118+ kfree (untagging_rule );
119+ kfree (redirect_rule );
120+ return err ;
121+ }
122+
123+ redirect_rule -> key_type = OCELOT_VCAP_KEY_ANY ;
124+ redirect_rule -> ingress_port_mask = BIT (upstream );
125+ redirect_rule -> pag = port ;
126+ redirect_rule -> prio = 1 ;
127+ redirect_rule -> id .cookie = port ;
128+ redirect_rule -> id .tc_offload = false;
129+ redirect_rule -> block_id = VCAP_IS2 ;
130+ redirect_rule -> type = OCELOT_VCAP_FILTER_OFFLOAD ;
131+ redirect_rule -> lookup = 0 ;
132+ redirect_rule -> action .mask_mode = OCELOT_MASK_MODE_REDIRECT ;
133+ redirect_rule -> action .port_mask = BIT (port );
134+
135+ err = ocelot_vcap_filter_add (ocelot , redirect_rule , NULL );
136+ if (err ) {
137+ ocelot_vcap_filter_del (ocelot , untagging_rule );
138+ kfree (redirect_rule );
139+ return err ;
140+ }
141+
142+ return 0 ;
143+ }
144+
145+ static int felix_tag_8021q_vlan_add (struct dsa_switch * ds , int port , u16 vid ,
146+ u16 flags )
147+ {
148+ bool untagged = flags & BRIDGE_VLAN_INFO_UNTAGGED ;
149+ bool pvid = flags & BRIDGE_VLAN_INFO_PVID ;
150+ struct ocelot * ocelot = ds -> priv ;
151+
152+ if (vid_is_dsa_8021q_rxvlan (vid ))
153+ return felix_tag_8021q_rxvlan_add (ocelot_to_felix (ocelot ),
154+ port , vid , pvid , untagged );
155+
156+ if (vid_is_dsa_8021q_txvlan (vid ))
157+ return felix_tag_8021q_txvlan_add (ocelot_to_felix (ocelot ),
158+ port , vid , pvid , untagged );
159+
160+ return 0 ;
161+ }
162+
163+ static int felix_tag_8021q_rxvlan_del (struct felix * felix , int port , u16 vid )
164+ {
165+ struct ocelot_vcap_filter * outer_tagging_rule ;
166+ struct ocelot_vcap_block * block_vcap_es0 ;
167+ struct ocelot * ocelot = & felix -> ocelot ;
168+
169+ block_vcap_es0 = & ocelot -> block [VCAP_ES0 ];
170+
171+ outer_tagging_rule = ocelot_vcap_block_find_filter_by_id (block_vcap_es0 ,
172+ port , false);
173+ /* In rxvlan_add, we had the "if (!pvid) return 0" logic to avoid
174+ * installing outer tagging ES0 rules where they weren't needed.
175+ * But in rxvlan_del, the API doesn't give us the "flags" anymore,
176+ * so that forces us to be slightly sloppy here, and just assume that
177+ * if we didn't find an outer_tagging_rule it means that there was
178+ * none in the first place, i.e. rxvlan_del is called on a non-pvid
179+ * port. This is most probably true though.
180+ */
181+ if (!outer_tagging_rule )
182+ return 0 ;
183+
184+ return ocelot_vcap_filter_del (ocelot , outer_tagging_rule );
185+ }
186+
187+ static int felix_tag_8021q_txvlan_del (struct felix * felix , int port , u16 vid )
188+ {
189+ struct ocelot_vcap_filter * untagging_rule , * redirect_rule ;
190+ struct ocelot_vcap_block * block_vcap_is1 ;
191+ struct ocelot_vcap_block * block_vcap_is2 ;
192+ struct ocelot * ocelot = & felix -> ocelot ;
193+ int err ;
194+
195+ if (ocelot -> ports [port ]-> is_dsa_8021q_cpu )
196+ return 0 ;
197+
198+ block_vcap_is1 = & ocelot -> block [VCAP_IS1 ];
199+ block_vcap_is2 = & ocelot -> block [VCAP_IS2 ];
200+
201+ untagging_rule = ocelot_vcap_block_find_filter_by_id (block_vcap_is1 ,
202+ port , false);
203+ if (!untagging_rule )
204+ return 0 ;
205+
206+ err = ocelot_vcap_filter_del (ocelot , untagging_rule );
207+ if (err )
208+ return err ;
209+
210+ redirect_rule = ocelot_vcap_block_find_filter_by_id (block_vcap_is2 ,
211+ port , false);
212+ if (!redirect_rule )
213+ return 0 ;
214+
215+ return ocelot_vcap_filter_del (ocelot , redirect_rule );
216+ }
217+
218+ static int felix_tag_8021q_vlan_del (struct dsa_switch * ds , int port , u16 vid )
219+ {
220+ struct ocelot * ocelot = ds -> priv ;
221+
222+ if (vid_is_dsa_8021q_rxvlan (vid ))
223+ return felix_tag_8021q_rxvlan_del (ocelot_to_felix (ocelot ),
224+ port , vid );
225+
226+ if (vid_is_dsa_8021q_txvlan (vid ))
227+ return felix_tag_8021q_txvlan_del (ocelot_to_felix (ocelot ),
228+ port , vid );
229+
230+ return 0 ;
231+ }
232+
233+ static const struct dsa_8021q_ops felix_tag_8021q_ops = {
234+ .vlan_add = felix_tag_8021q_vlan_add ,
235+ .vlan_del = felix_tag_8021q_vlan_del ,
236+ };
237+
238+ /* Alternatively to using the NPI functionality, that same hardware MAC
239+ * connected internally to the enetc or fman DSA master can be configured to
240+ * use the software-defined tag_8021q frame format. As far as the hardware is
241+ * concerned, it thinks it is a "dumb switch" - the queues of the CPU port
242+ * module are now disconnected from it, but can still be accessed through
243+ * register-based MMIO.
244+ */
245+ static void felix_8021q_cpu_port_init (struct ocelot * ocelot , int port )
246+ {
247+ ocelot -> ports [port ]-> is_dsa_8021q_cpu = true;
248+ ocelot -> npi = -1 ;
249+
250+ /* Overwrite PGID_CPU with the non-tagging port */
251+ ocelot_write_rix (ocelot , BIT (port ), ANA_PGID_PGID , PGID_CPU );
252+
253+ ocelot_apply_bridge_fwd_mask (ocelot );
254+ }
255+
256+ static void felix_8021q_cpu_port_deinit (struct ocelot * ocelot , int port )
257+ {
258+ ocelot -> ports [port ]-> is_dsa_8021q_cpu = false;
259+
260+ /* Restore PGID_CPU */
261+ ocelot_write_rix (ocelot , BIT (ocelot -> num_phys_ports ), ANA_PGID_PGID ,
262+ PGID_CPU );
263+
264+ ocelot_apply_bridge_fwd_mask (ocelot );
265+ }
266+
267+ static int felix_setup_tag_8021q (struct dsa_switch * ds , int cpu )
268+ {
269+ struct ocelot * ocelot = ds -> priv ;
270+ struct felix * felix = ocelot_to_felix (ocelot );
271+ unsigned long cpu_flood ;
272+ int port , err ;
273+
274+ felix_8021q_cpu_port_init (ocelot , cpu );
275+
276+ for (port = 0 ; port < ds -> num_ports ; port ++ ) {
277+ if (dsa_is_unused_port (ds , port ))
278+ continue ;
279+
280+ /* This overwrites ocelot_init():
281+ * Do not forward BPDU frames to the CPU port module,
282+ * for 2 reasons:
283+ * - When these packets are injected from the tag_8021q
284+ * CPU port, we want them to go out, not loop back
285+ * into the system.
286+ * - STP traffic ingressing on a user port should go to
287+ * the tag_8021q CPU port, not to the hardware CPU
288+ * port module.
289+ */
290+ ocelot_write_gix (ocelot ,
291+ ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA (0 ),
292+ ANA_PORT_CPU_FWD_BPDU_CFG , port );
293+ }
294+
295+ /* In tag_8021q mode, the CPU port module is unused. So we
296+ * want to disable flooding of any kind to the CPU port module,
297+ * since packets going there will end in a black hole.
298+ */
299+ cpu_flood = ANA_PGID_PGID_PGID (BIT (ocelot -> num_phys_ports ));
300+ ocelot_rmw_rix (ocelot , 0 , cpu_flood , ANA_PGID_PGID , PGID_UC );
301+ ocelot_rmw_rix (ocelot , 0 , cpu_flood , ANA_PGID_PGID , PGID_MC );
302+
303+ felix -> dsa_8021q_ctx = kzalloc (sizeof (* felix -> dsa_8021q_ctx ),
304+ GFP_KERNEL );
305+ if (!felix -> dsa_8021q_ctx )
306+ return - ENOMEM ;
307+
308+ felix -> dsa_8021q_ctx -> ops = & felix_tag_8021q_ops ;
309+ felix -> dsa_8021q_ctx -> proto = htons (ETH_P_8021AD );
310+ felix -> dsa_8021q_ctx -> ds = ds ;
311+
312+ err = dsa_8021q_setup (felix -> dsa_8021q_ctx , true);
313+ if (err )
314+ goto out_free_dsa_8021_ctx ;
315+
316+ return 0 ;
317+
318+ out_free_dsa_8021_ctx :
319+ kfree (felix -> dsa_8021q_ctx );
320+ return err ;
321+ }
322+
323+ static void felix_teardown_tag_8021q (struct dsa_switch * ds , int cpu )
324+ {
325+ struct ocelot * ocelot = ds -> priv ;
326+ struct felix * felix = ocelot_to_felix (ocelot );
327+ int err , port ;
328+
329+ err = dsa_8021q_setup (felix -> dsa_8021q_ctx , false);
330+ if (err )
331+ dev_err (ds -> dev , "dsa_8021q_setup returned %d" , err );
332+
333+ kfree (felix -> dsa_8021q_ctx );
334+
335+ for (port = 0 ; port < ds -> num_ports ; port ++ ) {
336+ if (dsa_is_unused_port (ds , port ))
337+ continue ;
338+
339+ /* Restore the logic from ocelot_init:
340+ * do not forward BPDU frames to the front ports.
341+ */
342+ ocelot_write_gix (ocelot ,
343+ ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA (0xffff ),
344+ ANA_PORT_CPU_FWD_BPDU_CFG ,
345+ port );
346+ }
347+
348+ felix_8021q_cpu_port_deinit (ocelot , cpu );
349+ }
350+
27351/* The CPU port module is connected to the Node Processor Interface (NPI). This
28352 * is the mode through which frames can be injected from and extracted to an
29353 * external CPU, over Ethernet. In NXP SoCs, the "external CPU" is the ARM CPU
@@ -107,6 +431,9 @@ static int felix_set_tag_protocol(struct dsa_switch *ds, int cpu,
107431 case DSA_TAG_PROTO_OCELOT :
108432 err = felix_setup_tag_npi (ds , cpu );
109433 break ;
434+ case DSA_TAG_PROTO_OCELOT_8021Q :
435+ err = felix_setup_tag_8021q (ds , cpu );
436+ break ;
110437 default :
111438 err = - EPROTONOSUPPORT ;
112439 }
@@ -121,11 +448,18 @@ static void felix_del_tag_protocol(struct dsa_switch *ds, int cpu,
121448 case DSA_TAG_PROTO_OCELOT :
122449 felix_teardown_tag_npi (ds , cpu );
123450 break ;
451+ case DSA_TAG_PROTO_OCELOT_8021Q :
452+ felix_teardown_tag_8021q (ds , cpu );
453+ break ;
124454 default :
125455 break ;
126456 }
127457}
128458
459+ /* This always leaves the switch in a consistent state, because although the
460+ * tag_8021q setup can fail, the NPI setup can't. So either the change is made,
461+ * or the restoration is guaranteed to work.
462+ */
129463static int felix_change_tag_protocol (struct dsa_switch * ds , int cpu ,
130464 enum dsa_tag_protocol proto )
131465{
@@ -134,7 +468,8 @@ static int felix_change_tag_protocol(struct dsa_switch *ds, int cpu,
134468 enum dsa_tag_protocol old_proto = felix -> tag_proto ;
135469 int err ;
136470
137- if (proto != DSA_TAG_PROTO_OCELOT )
471+ if (proto != DSA_TAG_PROTO_OCELOT &&
472+ proto != DSA_TAG_PROTO_OCELOT_8021Q )
138473 return - EPROTONOSUPPORT ;
139474
140475 felix_del_tag_protocol (ds , cpu , old_proto );
0 commit comments