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