xref: /openbmc/linux/drivers/net/dsa/ocelot/felix.c (revision b4e18b29)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright 2019-2021 NXP Semiconductors
3  *
4  * This is an umbrella module for all network switches that are
5  * register-compatible with Ocelot and that perform I/O to their host CPU
6  * through an NPI (Node Processor Interface) Ethernet port.
7  */
8 #include <uapi/linux/if_bridge.h>
9 #include <soc/mscc/ocelot_vcap.h>
10 #include <soc/mscc/ocelot_qsys.h>
11 #include <soc/mscc/ocelot_sys.h>
12 #include <soc/mscc/ocelot_dev.h>
13 #include <soc/mscc/ocelot_ana.h>
14 #include <soc/mscc/ocelot_ptp.h>
15 #include <soc/mscc/ocelot.h>
16 #include <linux/dsa/8021q.h>
17 #include <linux/platform_device.h>
18 #include <linux/packing.h>
19 #include <linux/module.h>
20 #include <linux/of_net.h>
21 #include <linux/pci.h>
22 #include <linux/of.h>
23 #include <linux/pcs-lynx.h>
24 #include <net/pkt_sched.h>
25 #include <net/dsa.h>
26 #include "felix.h"
27 
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 
351 /* The CPU port module is connected to the Node Processor Interface (NPI). This
352  * is the mode through which frames can be injected from and extracted to an
353  * external CPU, over Ethernet. In NXP SoCs, the "external CPU" is the ARM CPU
354  * running Linux, and this forms a DSA setup together with the enetc or fman
355  * DSA master.
356  */
357 static void felix_npi_port_init(struct ocelot *ocelot, int port)
358 {
359 	ocelot->npi = port;
360 
361 	ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M |
362 		     QSYS_EXT_CPU_CFG_EXT_CPU_PORT(port),
363 		     QSYS_EXT_CPU_CFG);
364 
365 	/* NPI port Injection/Extraction configuration */
366 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
367 			    ocelot->npi_xtr_prefix);
368 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
369 			    ocelot->npi_inj_prefix);
370 
371 	/* Disable transmission of pause frames */
372 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
373 }
374 
375 static void felix_npi_port_deinit(struct ocelot *ocelot, int port)
376 {
377 	/* Restore hardware defaults */
378 	int unused_port = ocelot->num_phys_ports + 2;
379 
380 	ocelot->npi = -1;
381 
382 	ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPU_PORT(unused_port),
383 		     QSYS_EXT_CPU_CFG);
384 
385 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
386 			    OCELOT_TAG_PREFIX_DISABLED);
387 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
388 			    OCELOT_TAG_PREFIX_DISABLED);
389 
390 	/* Enable transmission of pause frames */
391 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
392 }
393 
394 static int felix_setup_tag_npi(struct dsa_switch *ds, int cpu)
395 {
396 	struct ocelot *ocelot = ds->priv;
397 	unsigned long cpu_flood;
398 
399 	felix_npi_port_init(ocelot, cpu);
400 
401 	/* Include the CPU port module (and indirectly, the NPI port)
402 	 * in the forwarding mask for unknown unicast - the hardware
403 	 * default value for ANA_FLOODING_FLD_UNICAST excludes
404 	 * BIT(ocelot->num_phys_ports), and so does ocelot_init,
405 	 * since Ocelot relies on whitelisting MAC addresses towards
406 	 * PGID_CPU.
407 	 * We do this because DSA does not yet perform RX filtering,
408 	 * and the NPI port does not perform source address learning,
409 	 * so traffic sent to Linux is effectively unknown from the
410 	 * switch's perspective.
411 	 */
412 	cpu_flood = ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports));
413 	ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_UC);
414 
415 	return 0;
416 }
417 
418 static void felix_teardown_tag_npi(struct dsa_switch *ds, int cpu)
419 {
420 	struct ocelot *ocelot = ds->priv;
421 
422 	felix_npi_port_deinit(ocelot, cpu);
423 }
424 
425 static int felix_set_tag_protocol(struct dsa_switch *ds, int cpu,
426 				  enum dsa_tag_protocol proto)
427 {
428 	int err;
429 
430 	switch (proto) {
431 	case DSA_TAG_PROTO_OCELOT:
432 		err = felix_setup_tag_npi(ds, cpu);
433 		break;
434 	case DSA_TAG_PROTO_OCELOT_8021Q:
435 		err = felix_setup_tag_8021q(ds, cpu);
436 		break;
437 	default:
438 		err = -EPROTONOSUPPORT;
439 	}
440 
441 	return err;
442 }
443 
444 static void felix_del_tag_protocol(struct dsa_switch *ds, int cpu,
445 				   enum dsa_tag_protocol proto)
446 {
447 	switch (proto) {
448 	case DSA_TAG_PROTO_OCELOT:
449 		felix_teardown_tag_npi(ds, cpu);
450 		break;
451 	case DSA_TAG_PROTO_OCELOT_8021Q:
452 		felix_teardown_tag_8021q(ds, cpu);
453 		break;
454 	default:
455 		break;
456 	}
457 }
458 
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  */
463 static int felix_change_tag_protocol(struct dsa_switch *ds, int cpu,
464 				     enum dsa_tag_protocol proto)
465 {
466 	struct ocelot *ocelot = ds->priv;
467 	struct felix *felix = ocelot_to_felix(ocelot);
468 	enum dsa_tag_protocol old_proto = felix->tag_proto;
469 	int err;
470 
471 	if (proto != DSA_TAG_PROTO_OCELOT &&
472 	    proto != DSA_TAG_PROTO_OCELOT_8021Q)
473 		return -EPROTONOSUPPORT;
474 
475 	felix_del_tag_protocol(ds, cpu, old_proto);
476 
477 	err = felix_set_tag_protocol(ds, cpu, proto);
478 	if (err) {
479 		felix_set_tag_protocol(ds, cpu, old_proto);
480 		return err;
481 	}
482 
483 	felix->tag_proto = proto;
484 
485 	return 0;
486 }
487 
488 static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds,
489 						    int port,
490 						    enum dsa_tag_protocol mp)
491 {
492 	struct ocelot *ocelot = ds->priv;
493 	struct felix *felix = ocelot_to_felix(ocelot);
494 
495 	return felix->tag_proto;
496 }
497 
498 static int felix_set_ageing_time(struct dsa_switch *ds,
499 				 unsigned int ageing_time)
500 {
501 	struct ocelot *ocelot = ds->priv;
502 
503 	ocelot_set_ageing_time(ocelot, ageing_time);
504 
505 	return 0;
506 }
507 
508 static int felix_fdb_dump(struct dsa_switch *ds, int port,
509 			  dsa_fdb_dump_cb_t *cb, void *data)
510 {
511 	struct ocelot *ocelot = ds->priv;
512 
513 	return ocelot_fdb_dump(ocelot, port, cb, data);
514 }
515 
516 static int felix_fdb_add(struct dsa_switch *ds, int port,
517 			 const unsigned char *addr, u16 vid)
518 {
519 	struct ocelot *ocelot = ds->priv;
520 
521 	return ocelot_fdb_add(ocelot, port, addr, vid);
522 }
523 
524 static int felix_fdb_del(struct dsa_switch *ds, int port,
525 			 const unsigned char *addr, u16 vid)
526 {
527 	struct ocelot *ocelot = ds->priv;
528 
529 	return ocelot_fdb_del(ocelot, port, addr, vid);
530 }
531 
532 static int felix_mdb_add(struct dsa_switch *ds, int port,
533 			 const struct switchdev_obj_port_mdb *mdb)
534 {
535 	struct ocelot *ocelot = ds->priv;
536 
537 	return ocelot_port_mdb_add(ocelot, port, mdb);
538 }
539 
540 static int felix_mdb_del(struct dsa_switch *ds, int port,
541 			 const struct switchdev_obj_port_mdb *mdb)
542 {
543 	struct ocelot *ocelot = ds->priv;
544 
545 	return ocelot_port_mdb_del(ocelot, port, mdb);
546 }
547 
548 static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port,
549 				       u8 state)
550 {
551 	struct ocelot *ocelot = ds->priv;
552 
553 	return ocelot_bridge_stp_state_set(ocelot, port, state);
554 }
555 
556 static int felix_bridge_join(struct dsa_switch *ds, int port,
557 			     struct net_device *br)
558 {
559 	struct ocelot *ocelot = ds->priv;
560 
561 	return ocelot_port_bridge_join(ocelot, port, br);
562 }
563 
564 static void felix_bridge_leave(struct dsa_switch *ds, int port,
565 			       struct net_device *br)
566 {
567 	struct ocelot *ocelot = ds->priv;
568 
569 	ocelot_port_bridge_leave(ocelot, port, br);
570 }
571 
572 static int felix_lag_join(struct dsa_switch *ds, int port,
573 			  struct net_device *bond,
574 			  struct netdev_lag_upper_info *info)
575 {
576 	struct ocelot *ocelot = ds->priv;
577 
578 	return ocelot_port_lag_join(ocelot, port, bond, info);
579 }
580 
581 static int felix_lag_leave(struct dsa_switch *ds, int port,
582 			   struct net_device *bond)
583 {
584 	struct ocelot *ocelot = ds->priv;
585 
586 	ocelot_port_lag_leave(ocelot, port, bond);
587 
588 	return 0;
589 }
590 
591 static int felix_lag_change(struct dsa_switch *ds, int port)
592 {
593 	struct dsa_port *dp = dsa_to_port(ds, port);
594 	struct ocelot *ocelot = ds->priv;
595 
596 	ocelot_port_lag_change(ocelot, port, dp->lag_tx_enabled);
597 
598 	return 0;
599 }
600 
601 static int felix_vlan_prepare(struct dsa_switch *ds, int port,
602 			      const struct switchdev_obj_port_vlan *vlan)
603 {
604 	struct ocelot *ocelot = ds->priv;
605 	u16 flags = vlan->flags;
606 
607 	/* Ocelot switches copy frames as-is to the CPU, so the flags:
608 	 * egress-untagged or not, pvid or not, make no difference. This
609 	 * behavior is already better than what DSA just tries to approximate
610 	 * when it installs the VLAN with the same flags on the CPU port.
611 	 * Just accept any configuration, and don't let ocelot deny installing
612 	 * multiple native VLANs on the NPI port, because the switch doesn't
613 	 * look at the port tag settings towards the NPI interface anyway.
614 	 */
615 	if (port == ocelot->npi)
616 		return 0;
617 
618 	return ocelot_vlan_prepare(ocelot, port, vlan->vid,
619 				   flags & BRIDGE_VLAN_INFO_PVID,
620 				   flags & BRIDGE_VLAN_INFO_UNTAGGED);
621 }
622 
623 static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled)
624 {
625 	struct ocelot *ocelot = ds->priv;
626 
627 	return ocelot_port_vlan_filtering(ocelot, port, enabled);
628 }
629 
630 static int felix_vlan_add(struct dsa_switch *ds, int port,
631 			  const struct switchdev_obj_port_vlan *vlan)
632 {
633 	struct ocelot *ocelot = ds->priv;
634 	u16 flags = vlan->flags;
635 	int err;
636 
637 	err = felix_vlan_prepare(ds, port, vlan);
638 	if (err)
639 		return err;
640 
641 	return ocelot_vlan_add(ocelot, port, vlan->vid,
642 			       flags & BRIDGE_VLAN_INFO_PVID,
643 			       flags & BRIDGE_VLAN_INFO_UNTAGGED);
644 }
645 
646 static int felix_vlan_del(struct dsa_switch *ds, int port,
647 			  const struct switchdev_obj_port_vlan *vlan)
648 {
649 	struct ocelot *ocelot = ds->priv;
650 
651 	return ocelot_vlan_del(ocelot, port, vlan->vid);
652 }
653 
654 static int felix_port_enable(struct dsa_switch *ds, int port,
655 			     struct phy_device *phy)
656 {
657 	struct ocelot *ocelot = ds->priv;
658 
659 	ocelot_port_enable(ocelot, port, phy);
660 
661 	return 0;
662 }
663 
664 static void felix_port_disable(struct dsa_switch *ds, int port)
665 {
666 	struct ocelot *ocelot = ds->priv;
667 
668 	return ocelot_port_disable(ocelot, port);
669 }
670 
671 static void felix_phylink_validate(struct dsa_switch *ds, int port,
672 				   unsigned long *supported,
673 				   struct phylink_link_state *state)
674 {
675 	struct ocelot *ocelot = ds->priv;
676 	struct felix *felix = ocelot_to_felix(ocelot);
677 
678 	if (felix->info->phylink_validate)
679 		felix->info->phylink_validate(ocelot, port, supported, state);
680 }
681 
682 static void felix_phylink_mac_config(struct dsa_switch *ds, int port,
683 				     unsigned int link_an_mode,
684 				     const struct phylink_link_state *state)
685 {
686 	struct ocelot *ocelot = ds->priv;
687 	struct felix *felix = ocelot_to_felix(ocelot);
688 	struct dsa_port *dp = dsa_to_port(ds, port);
689 
690 	if (felix->pcs[port])
691 		phylink_set_pcs(dp->pl, &felix->pcs[port]->pcs);
692 }
693 
694 static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port,
695 					unsigned int link_an_mode,
696 					phy_interface_t interface)
697 {
698 	struct ocelot *ocelot = ds->priv;
699 	struct ocelot_port *ocelot_port = ocelot->ports[port];
700 	int err;
701 
702 	ocelot_port_rmwl(ocelot_port, 0, DEV_MAC_ENA_CFG_RX_ENA,
703 			 DEV_MAC_ENA_CFG);
704 
705 	ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0);
706 
707 	err = ocelot_port_flush(ocelot, port);
708 	if (err)
709 		dev_err(ocelot->dev, "failed to flush port %d: %d\n",
710 			port, err);
711 
712 	/* Put the port in reset. */
713 	ocelot_port_writel(ocelot_port,
714 			   DEV_CLOCK_CFG_MAC_TX_RST |
715 			   DEV_CLOCK_CFG_MAC_RX_RST |
716 			   DEV_CLOCK_CFG_LINK_SPEED(OCELOT_SPEED_1000),
717 			   DEV_CLOCK_CFG);
718 }
719 
720 static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port,
721 				      unsigned int link_an_mode,
722 				      phy_interface_t interface,
723 				      struct phy_device *phydev,
724 				      int speed, int duplex,
725 				      bool tx_pause, bool rx_pause)
726 {
727 	struct ocelot *ocelot = ds->priv;
728 	struct ocelot_port *ocelot_port = ocelot->ports[port];
729 	struct felix *felix = ocelot_to_felix(ocelot);
730 	u32 mac_fc_cfg;
731 
732 	/* Take port out of reset by clearing the MAC_TX_RST, MAC_RX_RST and
733 	 * PORT_RST bits in DEV_CLOCK_CFG. Note that the way this system is
734 	 * integrated is that the MAC speed is fixed and it's the PCS who is
735 	 * performing the rate adaptation, so we have to write "1000Mbps" into
736 	 * the LINK_SPEED field of DEV_CLOCK_CFG (which is also its default
737 	 * value).
738 	 */
739 	ocelot_port_writel(ocelot_port,
740 			   DEV_CLOCK_CFG_LINK_SPEED(OCELOT_SPEED_1000),
741 			   DEV_CLOCK_CFG);
742 
743 	switch (speed) {
744 	case SPEED_10:
745 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(3);
746 		break;
747 	case SPEED_100:
748 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(2);
749 		break;
750 	case SPEED_1000:
751 	case SPEED_2500:
752 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(1);
753 		break;
754 	default:
755 		dev_err(ocelot->dev, "Unsupported speed on port %d: %d\n",
756 			port, speed);
757 		return;
758 	}
759 
760 	/* handle Rx pause in all cases, with 2500base-X this is used for rate
761 	 * adaptation.
762 	 */
763 	mac_fc_cfg |= SYS_MAC_FC_CFG_RX_FC_ENA;
764 
765 	if (tx_pause)
766 		mac_fc_cfg |= SYS_MAC_FC_CFG_TX_FC_ENA |
767 			      SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
768 			      SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
769 			      SYS_MAC_FC_CFG_ZERO_PAUSE_ENA;
770 
771 	/* Flow control. Link speed is only used here to evaluate the time
772 	 * specification in incoming pause frames.
773 	 */
774 	ocelot_write_rix(ocelot, mac_fc_cfg, SYS_MAC_FC_CFG, port);
775 
776 	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
777 
778 	/* Undo the effects of felix_phylink_mac_link_down:
779 	 * enable MAC module
780 	 */
781 	ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
782 			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
783 
784 	/* Enable receiving frames on the port, and activate auto-learning of
785 	 * MAC addresses.
786 	 */
787 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
788 			 ANA_PORT_PORT_CFG_RECV_ENA |
789 			 ANA_PORT_PORT_CFG_PORTID_VAL(port),
790 			 ANA_PORT_PORT_CFG, port);
791 
792 	/* Core: Enable port for frame transfer */
793 	ocelot_fields_write(ocelot, port,
794 			    QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
795 
796 	if (felix->info->port_sched_speed_set)
797 		felix->info->port_sched_speed_set(ocelot, port, speed);
798 }
799 
800 static void felix_port_qos_map_init(struct ocelot *ocelot, int port)
801 {
802 	int i;
803 
804 	ocelot_rmw_gix(ocelot,
805 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
806 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
807 		       ANA_PORT_QOS_CFG,
808 		       port);
809 
810 	for (i = 0; i < OCELOT_NUM_TC * 2; i++) {
811 		ocelot_rmw_ix(ocelot,
812 			      (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) |
813 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i),
814 			      ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL |
815 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M,
816 			      ANA_PORT_PCP_DEI_MAP,
817 			      port, i);
818 	}
819 }
820 
821 static void felix_get_strings(struct dsa_switch *ds, int port,
822 			      u32 stringset, u8 *data)
823 {
824 	struct ocelot *ocelot = ds->priv;
825 
826 	return ocelot_get_strings(ocelot, port, stringset, data);
827 }
828 
829 static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data)
830 {
831 	struct ocelot *ocelot = ds->priv;
832 
833 	ocelot_get_ethtool_stats(ocelot, port, data);
834 }
835 
836 static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset)
837 {
838 	struct ocelot *ocelot = ds->priv;
839 
840 	return ocelot_get_sset_count(ocelot, port, sset);
841 }
842 
843 static int felix_get_ts_info(struct dsa_switch *ds, int port,
844 			     struct ethtool_ts_info *info)
845 {
846 	struct ocelot *ocelot = ds->priv;
847 
848 	return ocelot_get_ts_info(ocelot, port, info);
849 }
850 
851 static int felix_parse_ports_node(struct felix *felix,
852 				  struct device_node *ports_node,
853 				  phy_interface_t *port_phy_modes)
854 {
855 	struct ocelot *ocelot = &felix->ocelot;
856 	struct device *dev = felix->ocelot.dev;
857 	struct device_node *child;
858 
859 	for_each_available_child_of_node(ports_node, child) {
860 		phy_interface_t phy_mode;
861 		u32 port;
862 		int err;
863 
864 		/* Get switch port number from DT */
865 		if (of_property_read_u32(child, "reg", &port) < 0) {
866 			dev_err(dev, "Port number not defined in device tree "
867 				"(property \"reg\")\n");
868 			of_node_put(child);
869 			return -ENODEV;
870 		}
871 
872 		/* Get PHY mode from DT */
873 		err = of_get_phy_mode(child, &phy_mode);
874 		if (err) {
875 			dev_err(dev, "Failed to read phy-mode or "
876 				"phy-interface-type property for port %d\n",
877 				port);
878 			of_node_put(child);
879 			return -ENODEV;
880 		}
881 
882 		err = felix->info->prevalidate_phy_mode(ocelot, port, phy_mode);
883 		if (err < 0) {
884 			dev_err(dev, "Unsupported PHY mode %s on port %d\n",
885 				phy_modes(phy_mode), port);
886 			of_node_put(child);
887 			return err;
888 		}
889 
890 		port_phy_modes[port] = phy_mode;
891 	}
892 
893 	return 0;
894 }
895 
896 static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes)
897 {
898 	struct device *dev = felix->ocelot.dev;
899 	struct device_node *switch_node;
900 	struct device_node *ports_node;
901 	int err;
902 
903 	switch_node = dev->of_node;
904 
905 	ports_node = of_get_child_by_name(switch_node, "ports");
906 	if (!ports_node) {
907 		dev_err(dev, "Incorrect bindings: absent \"ports\" node\n");
908 		return -ENODEV;
909 	}
910 
911 	err = felix_parse_ports_node(felix, ports_node, port_phy_modes);
912 	of_node_put(ports_node);
913 
914 	return err;
915 }
916 
917 static int felix_init_structs(struct felix *felix, int num_phys_ports)
918 {
919 	struct ocelot *ocelot = &felix->ocelot;
920 	phy_interface_t *port_phy_modes;
921 	struct resource res;
922 	int port, i, err;
923 
924 	ocelot->num_phys_ports = num_phys_ports;
925 	ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports,
926 				     sizeof(struct ocelot_port *), GFP_KERNEL);
927 	if (!ocelot->ports)
928 		return -ENOMEM;
929 
930 	ocelot->map		= felix->info->map;
931 	ocelot->stats_layout	= felix->info->stats_layout;
932 	ocelot->num_stats	= felix->info->num_stats;
933 	ocelot->num_mact_rows	= felix->info->num_mact_rows;
934 	ocelot->vcap		= felix->info->vcap;
935 	ocelot->ops		= felix->info->ops;
936 	ocelot->npi_inj_prefix	= OCELOT_TAG_PREFIX_SHORT;
937 	ocelot->npi_xtr_prefix	= OCELOT_TAG_PREFIX_SHORT;
938 	ocelot->devlink		= felix->ds->devlink;
939 
940 	port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t),
941 				 GFP_KERNEL);
942 	if (!port_phy_modes)
943 		return -ENOMEM;
944 
945 	err = felix_parse_dt(felix, port_phy_modes);
946 	if (err) {
947 		kfree(port_phy_modes);
948 		return err;
949 	}
950 
951 	for (i = 0; i < TARGET_MAX; i++) {
952 		struct regmap *target;
953 
954 		if (!felix->info->target_io_res[i].name)
955 			continue;
956 
957 		memcpy(&res, &felix->info->target_io_res[i], sizeof(res));
958 		res.flags = IORESOURCE_MEM;
959 		res.start += felix->switch_base;
960 		res.end += felix->switch_base;
961 
962 		target = ocelot_regmap_init(ocelot, &res);
963 		if (IS_ERR(target)) {
964 			dev_err(ocelot->dev,
965 				"Failed to map device memory space\n");
966 			kfree(port_phy_modes);
967 			return PTR_ERR(target);
968 		}
969 
970 		ocelot->targets[i] = target;
971 	}
972 
973 	err = ocelot_regfields_init(ocelot, felix->info->regfields);
974 	if (err) {
975 		dev_err(ocelot->dev, "failed to init reg fields map\n");
976 		kfree(port_phy_modes);
977 		return err;
978 	}
979 
980 	for (port = 0; port < num_phys_ports; port++) {
981 		struct ocelot_port *ocelot_port;
982 		struct regmap *target;
983 		u8 *template;
984 
985 		ocelot_port = devm_kzalloc(ocelot->dev,
986 					   sizeof(struct ocelot_port),
987 					   GFP_KERNEL);
988 		if (!ocelot_port) {
989 			dev_err(ocelot->dev,
990 				"failed to allocate port memory\n");
991 			kfree(port_phy_modes);
992 			return -ENOMEM;
993 		}
994 
995 		memcpy(&res, &felix->info->port_io_res[port], sizeof(res));
996 		res.flags = IORESOURCE_MEM;
997 		res.start += felix->switch_base;
998 		res.end += felix->switch_base;
999 
1000 		target = ocelot_regmap_init(ocelot, &res);
1001 		if (IS_ERR(target)) {
1002 			dev_err(ocelot->dev,
1003 				"Failed to map memory space for port %d\n",
1004 				port);
1005 			kfree(port_phy_modes);
1006 			return PTR_ERR(target);
1007 		}
1008 
1009 		template = devm_kzalloc(ocelot->dev, OCELOT_TOTAL_TAG_LEN,
1010 					GFP_KERNEL);
1011 		if (!template) {
1012 			dev_err(ocelot->dev,
1013 				"Failed to allocate memory for DSA tag\n");
1014 			kfree(port_phy_modes);
1015 			return -ENOMEM;
1016 		}
1017 
1018 		ocelot_port->phy_mode = port_phy_modes[port];
1019 		ocelot_port->ocelot = ocelot;
1020 		ocelot_port->target = target;
1021 		ocelot_port->xmit_template = template;
1022 		ocelot->ports[port] = ocelot_port;
1023 
1024 		felix->info->xmit_template_populate(ocelot, port);
1025 	}
1026 
1027 	kfree(port_phy_modes);
1028 
1029 	if (felix->info->mdio_bus_alloc) {
1030 		err = felix->info->mdio_bus_alloc(ocelot);
1031 		if (err < 0)
1032 			return err;
1033 	}
1034 
1035 	return 0;
1036 }
1037 
1038 /* Hardware initialization done here so that we can allocate structures with
1039  * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing
1040  * us to allocate structures twice (leak memory) and map PCI memory twice
1041  * (which will not work).
1042  */
1043 static int felix_setup(struct dsa_switch *ds)
1044 {
1045 	struct ocelot *ocelot = ds->priv;
1046 	struct felix *felix = ocelot_to_felix(ocelot);
1047 	int port, err;
1048 
1049 	err = felix_init_structs(felix, ds->num_ports);
1050 	if (err)
1051 		return err;
1052 
1053 	err = ocelot_init(ocelot);
1054 	if (err)
1055 		return err;
1056 
1057 	if (ocelot->ptp) {
1058 		err = ocelot_init_timestamp(ocelot, felix->info->ptp_caps);
1059 		if (err) {
1060 			dev_err(ocelot->dev,
1061 				"Timestamp initialization failed\n");
1062 			ocelot->ptp = 0;
1063 		}
1064 	}
1065 
1066 	for (port = 0; port < ds->num_ports; port++) {
1067 		if (dsa_is_unused_port(ds, port))
1068 			continue;
1069 
1070 		ocelot_init_port(ocelot, port);
1071 
1072 		/* Set the default QoS Classification based on PCP and DEI
1073 		 * bits of vlan tag.
1074 		 */
1075 		felix_port_qos_map_init(ocelot, port);
1076 	}
1077 
1078 	err = ocelot_devlink_sb_register(ocelot);
1079 	if (err)
1080 		return err;
1081 
1082 	for (port = 0; port < ds->num_ports; port++) {
1083 		if (!dsa_is_cpu_port(ds, port))
1084 			continue;
1085 
1086 		/* The initial tag protocol is NPI which always returns 0, so
1087 		 * there's no real point in checking for errors.
1088 		 */
1089 		felix_set_tag_protocol(ds, port, felix->tag_proto);
1090 	}
1091 
1092 	ds->mtu_enforcement_ingress = true;
1093 	ds->assisted_learning_on_cpu_port = true;
1094 
1095 	return 0;
1096 }
1097 
1098 static void felix_teardown(struct dsa_switch *ds)
1099 {
1100 	struct ocelot *ocelot = ds->priv;
1101 	struct felix *felix = ocelot_to_felix(ocelot);
1102 	int port;
1103 
1104 	for (port = 0; port < ds->num_ports; port++) {
1105 		if (!dsa_is_cpu_port(ds, port))
1106 			continue;
1107 
1108 		felix_del_tag_protocol(ds, port, felix->tag_proto);
1109 	}
1110 
1111 	ocelot_devlink_sb_unregister(ocelot);
1112 	ocelot_deinit_timestamp(ocelot);
1113 	ocelot_deinit(ocelot);
1114 
1115 	for (port = 0; port < ocelot->num_phys_ports; port++)
1116 		ocelot_deinit_port(ocelot, port);
1117 
1118 	if (felix->info->mdio_bus_free)
1119 		felix->info->mdio_bus_free(ocelot);
1120 }
1121 
1122 static int felix_hwtstamp_get(struct dsa_switch *ds, int port,
1123 			      struct ifreq *ifr)
1124 {
1125 	struct ocelot *ocelot = ds->priv;
1126 
1127 	return ocelot_hwstamp_get(ocelot, port, ifr);
1128 }
1129 
1130 static int felix_hwtstamp_set(struct dsa_switch *ds, int port,
1131 			      struct ifreq *ifr)
1132 {
1133 	struct ocelot *ocelot = ds->priv;
1134 
1135 	return ocelot_hwstamp_set(ocelot, port, ifr);
1136 }
1137 
1138 static bool felix_rxtstamp(struct dsa_switch *ds, int port,
1139 			   struct sk_buff *skb, unsigned int type)
1140 {
1141 	struct skb_shared_hwtstamps *shhwtstamps;
1142 	struct ocelot *ocelot = ds->priv;
1143 	u8 *extraction = skb->data - ETH_HLEN - OCELOT_TAG_LEN;
1144 	u32 tstamp_lo, tstamp_hi;
1145 	struct timespec64 ts;
1146 	u64 tstamp, val;
1147 
1148 	ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
1149 	tstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
1150 
1151 	packing(extraction, &val,  116, 85, OCELOT_TAG_LEN, UNPACK, 0);
1152 	tstamp_lo = (u32)val;
1153 
1154 	tstamp_hi = tstamp >> 32;
1155 	if ((tstamp & 0xffffffff) < tstamp_lo)
1156 		tstamp_hi--;
1157 
1158 	tstamp = ((u64)tstamp_hi << 32) | tstamp_lo;
1159 
1160 	shhwtstamps = skb_hwtstamps(skb);
1161 	memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
1162 	shhwtstamps->hwtstamp = tstamp;
1163 	return false;
1164 }
1165 
1166 static bool felix_txtstamp(struct dsa_switch *ds, int port,
1167 			   struct sk_buff *clone, unsigned int type)
1168 {
1169 	struct ocelot *ocelot = ds->priv;
1170 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1171 
1172 	if (ocelot->ptp && (skb_shinfo(clone)->tx_flags & SKBTX_HW_TSTAMP) &&
1173 	    ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
1174 		ocelot_port_add_txtstamp_skb(ocelot, port, clone);
1175 		return true;
1176 	}
1177 
1178 	return false;
1179 }
1180 
1181 static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
1182 {
1183 	struct ocelot *ocelot = ds->priv;
1184 
1185 	ocelot_port_set_maxlen(ocelot, port, new_mtu);
1186 
1187 	return 0;
1188 }
1189 
1190 static int felix_get_max_mtu(struct dsa_switch *ds, int port)
1191 {
1192 	struct ocelot *ocelot = ds->priv;
1193 
1194 	return ocelot_get_max_mtu(ocelot, port);
1195 }
1196 
1197 static int felix_cls_flower_add(struct dsa_switch *ds, int port,
1198 				struct flow_cls_offload *cls, bool ingress)
1199 {
1200 	struct ocelot *ocelot = ds->priv;
1201 
1202 	return ocelot_cls_flower_replace(ocelot, port, cls, ingress);
1203 }
1204 
1205 static int felix_cls_flower_del(struct dsa_switch *ds, int port,
1206 				struct flow_cls_offload *cls, bool ingress)
1207 {
1208 	struct ocelot *ocelot = ds->priv;
1209 
1210 	return ocelot_cls_flower_destroy(ocelot, port, cls, ingress);
1211 }
1212 
1213 static int felix_cls_flower_stats(struct dsa_switch *ds, int port,
1214 				  struct flow_cls_offload *cls, bool ingress)
1215 {
1216 	struct ocelot *ocelot = ds->priv;
1217 
1218 	return ocelot_cls_flower_stats(ocelot, port, cls, ingress);
1219 }
1220 
1221 static int felix_port_policer_add(struct dsa_switch *ds, int port,
1222 				  struct dsa_mall_policer_tc_entry *policer)
1223 {
1224 	struct ocelot *ocelot = ds->priv;
1225 	struct ocelot_policer pol = {
1226 		.rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8,
1227 		.burst = policer->burst,
1228 	};
1229 
1230 	return ocelot_port_policer_add(ocelot, port, &pol);
1231 }
1232 
1233 static void felix_port_policer_del(struct dsa_switch *ds, int port)
1234 {
1235 	struct ocelot *ocelot = ds->priv;
1236 
1237 	ocelot_port_policer_del(ocelot, port);
1238 }
1239 
1240 static int felix_port_setup_tc(struct dsa_switch *ds, int port,
1241 			       enum tc_setup_type type,
1242 			       void *type_data)
1243 {
1244 	struct ocelot *ocelot = ds->priv;
1245 	struct felix *felix = ocelot_to_felix(ocelot);
1246 
1247 	if (felix->info->port_setup_tc)
1248 		return felix->info->port_setup_tc(ds, port, type, type_data);
1249 	else
1250 		return -EOPNOTSUPP;
1251 }
1252 
1253 static int felix_sb_pool_get(struct dsa_switch *ds, unsigned int sb_index,
1254 			     u16 pool_index,
1255 			     struct devlink_sb_pool_info *pool_info)
1256 {
1257 	struct ocelot *ocelot = ds->priv;
1258 
1259 	return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info);
1260 }
1261 
1262 static int felix_sb_pool_set(struct dsa_switch *ds, unsigned int sb_index,
1263 			     u16 pool_index, u32 size,
1264 			     enum devlink_sb_threshold_type threshold_type,
1265 			     struct netlink_ext_ack *extack)
1266 {
1267 	struct ocelot *ocelot = ds->priv;
1268 
1269 	return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size,
1270 				  threshold_type, extack);
1271 }
1272 
1273 static int felix_sb_port_pool_get(struct dsa_switch *ds, int port,
1274 				  unsigned int sb_index, u16 pool_index,
1275 				  u32 *p_threshold)
1276 {
1277 	struct ocelot *ocelot = ds->priv;
1278 
1279 	return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index,
1280 				       p_threshold);
1281 }
1282 
1283 static int felix_sb_port_pool_set(struct dsa_switch *ds, int port,
1284 				  unsigned int sb_index, u16 pool_index,
1285 				  u32 threshold, struct netlink_ext_ack *extack)
1286 {
1287 	struct ocelot *ocelot = ds->priv;
1288 
1289 	return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index,
1290 				       threshold, extack);
1291 }
1292 
1293 static int felix_sb_tc_pool_bind_get(struct dsa_switch *ds, int port,
1294 				     unsigned int sb_index, u16 tc_index,
1295 				     enum devlink_sb_pool_type pool_type,
1296 				     u16 *p_pool_index, u32 *p_threshold)
1297 {
1298 	struct ocelot *ocelot = ds->priv;
1299 
1300 	return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index,
1301 					  pool_type, p_pool_index,
1302 					  p_threshold);
1303 }
1304 
1305 static int felix_sb_tc_pool_bind_set(struct dsa_switch *ds, int port,
1306 				     unsigned int sb_index, u16 tc_index,
1307 				     enum devlink_sb_pool_type pool_type,
1308 				     u16 pool_index, u32 threshold,
1309 				     struct netlink_ext_ack *extack)
1310 {
1311 	struct ocelot *ocelot = ds->priv;
1312 
1313 	return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index,
1314 					  pool_type, pool_index, threshold,
1315 					  extack);
1316 }
1317 
1318 static int felix_sb_occ_snapshot(struct dsa_switch *ds,
1319 				 unsigned int sb_index)
1320 {
1321 	struct ocelot *ocelot = ds->priv;
1322 
1323 	return ocelot_sb_occ_snapshot(ocelot, sb_index);
1324 }
1325 
1326 static int felix_sb_occ_max_clear(struct dsa_switch *ds,
1327 				  unsigned int sb_index)
1328 {
1329 	struct ocelot *ocelot = ds->priv;
1330 
1331 	return ocelot_sb_occ_max_clear(ocelot, sb_index);
1332 }
1333 
1334 static int felix_sb_occ_port_pool_get(struct dsa_switch *ds, int port,
1335 				      unsigned int sb_index, u16 pool_index,
1336 				      u32 *p_cur, u32 *p_max)
1337 {
1338 	struct ocelot *ocelot = ds->priv;
1339 
1340 	return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index,
1341 					   p_cur, p_max);
1342 }
1343 
1344 static int felix_sb_occ_tc_port_bind_get(struct dsa_switch *ds, int port,
1345 					 unsigned int sb_index, u16 tc_index,
1346 					 enum devlink_sb_pool_type pool_type,
1347 					 u32 *p_cur, u32 *p_max)
1348 {
1349 	struct ocelot *ocelot = ds->priv;
1350 
1351 	return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index, tc_index,
1352 					      pool_type, p_cur, p_max);
1353 }
1354 
1355 const struct dsa_switch_ops felix_switch_ops = {
1356 	.get_tag_protocol		= felix_get_tag_protocol,
1357 	.change_tag_protocol		= felix_change_tag_protocol,
1358 	.setup				= felix_setup,
1359 	.teardown			= felix_teardown,
1360 	.set_ageing_time		= felix_set_ageing_time,
1361 	.get_strings			= felix_get_strings,
1362 	.get_ethtool_stats		= felix_get_ethtool_stats,
1363 	.get_sset_count			= felix_get_sset_count,
1364 	.get_ts_info			= felix_get_ts_info,
1365 	.phylink_validate		= felix_phylink_validate,
1366 	.phylink_mac_config		= felix_phylink_mac_config,
1367 	.phylink_mac_link_down		= felix_phylink_mac_link_down,
1368 	.phylink_mac_link_up		= felix_phylink_mac_link_up,
1369 	.port_enable			= felix_port_enable,
1370 	.port_disable			= felix_port_disable,
1371 	.port_fdb_dump			= felix_fdb_dump,
1372 	.port_fdb_add			= felix_fdb_add,
1373 	.port_fdb_del			= felix_fdb_del,
1374 	.port_mdb_add			= felix_mdb_add,
1375 	.port_mdb_del			= felix_mdb_del,
1376 	.port_bridge_join		= felix_bridge_join,
1377 	.port_bridge_leave		= felix_bridge_leave,
1378 	.port_lag_join			= felix_lag_join,
1379 	.port_lag_leave			= felix_lag_leave,
1380 	.port_lag_change		= felix_lag_change,
1381 	.port_stp_state_set		= felix_bridge_stp_state_set,
1382 	.port_vlan_filtering		= felix_vlan_filtering,
1383 	.port_vlan_add			= felix_vlan_add,
1384 	.port_vlan_del			= felix_vlan_del,
1385 	.port_hwtstamp_get		= felix_hwtstamp_get,
1386 	.port_hwtstamp_set		= felix_hwtstamp_set,
1387 	.port_rxtstamp			= felix_rxtstamp,
1388 	.port_txtstamp			= felix_txtstamp,
1389 	.port_change_mtu		= felix_change_mtu,
1390 	.port_max_mtu			= felix_get_max_mtu,
1391 	.port_policer_add		= felix_port_policer_add,
1392 	.port_policer_del		= felix_port_policer_del,
1393 	.cls_flower_add			= felix_cls_flower_add,
1394 	.cls_flower_del			= felix_cls_flower_del,
1395 	.cls_flower_stats		= felix_cls_flower_stats,
1396 	.port_setup_tc			= felix_port_setup_tc,
1397 	.devlink_sb_pool_get		= felix_sb_pool_get,
1398 	.devlink_sb_pool_set		= felix_sb_pool_set,
1399 	.devlink_sb_port_pool_get	= felix_sb_port_pool_get,
1400 	.devlink_sb_port_pool_set	= felix_sb_port_pool_set,
1401 	.devlink_sb_tc_pool_bind_get	= felix_sb_tc_pool_bind_get,
1402 	.devlink_sb_tc_pool_bind_set	= felix_sb_tc_pool_bind_set,
1403 	.devlink_sb_occ_snapshot	= felix_sb_occ_snapshot,
1404 	.devlink_sb_occ_max_clear	= felix_sb_occ_max_clear,
1405 	.devlink_sb_occ_port_pool_get	= felix_sb_occ_port_pool_get,
1406 	.devlink_sb_occ_tc_port_bind_get= felix_sb_occ_tc_port_bind_get,
1407 };
1408 
1409 struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port)
1410 {
1411 	struct felix *felix = ocelot_to_felix(ocelot);
1412 	struct dsa_switch *ds = felix->ds;
1413 
1414 	if (!dsa_is_user_port(ds, port))
1415 		return NULL;
1416 
1417 	return dsa_to_port(ds, port)->slave;
1418 }
1419 
1420 int felix_netdev_to_port(struct net_device *dev)
1421 {
1422 	struct dsa_port *dp;
1423 
1424 	dp = dsa_port_from_netdev(dev);
1425 	if (IS_ERR(dp))
1426 		return -EINVAL;
1427 
1428 	return dp->index;
1429 }
1430