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