xref: /openbmc/linux/drivers/net/dsa/ocelot/felix.c (revision d19741b0f54487cf3a11307900f8633935cd2849)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright 2019 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/platform_device.h>
17 #include <linux/packing.h>
18 #include <linux/module.h>
19 #include <linux/of_net.h>
20 #include <linux/pci.h>
21 #include <linux/of.h>
22 #include <linux/pcs-lynx.h>
23 #include <net/pkt_sched.h>
24 #include <net/dsa.h>
25 #include "felix.h"
26 
27 static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds,
28 						    int port,
29 						    enum dsa_tag_protocol mp)
30 {
31 	return DSA_TAG_PROTO_OCELOT;
32 }
33 
34 static int felix_set_ageing_time(struct dsa_switch *ds,
35 				 unsigned int ageing_time)
36 {
37 	struct ocelot *ocelot = ds->priv;
38 
39 	ocelot_set_ageing_time(ocelot, ageing_time);
40 
41 	return 0;
42 }
43 
44 static int felix_fdb_dump(struct dsa_switch *ds, int port,
45 			  dsa_fdb_dump_cb_t *cb, void *data)
46 {
47 	struct ocelot *ocelot = ds->priv;
48 
49 	return ocelot_fdb_dump(ocelot, port, cb, data);
50 }
51 
52 static int felix_fdb_add(struct dsa_switch *ds, int port,
53 			 const unsigned char *addr, u16 vid)
54 {
55 	struct ocelot *ocelot = ds->priv;
56 
57 	return ocelot_fdb_add(ocelot, port, addr, vid);
58 }
59 
60 static int felix_fdb_del(struct dsa_switch *ds, int port,
61 			 const unsigned char *addr, u16 vid)
62 {
63 	struct ocelot *ocelot = ds->priv;
64 
65 	return ocelot_fdb_del(ocelot, port, addr, vid);
66 }
67 
68 static int felix_mdb_add(struct dsa_switch *ds, int port,
69 			 const struct switchdev_obj_port_mdb *mdb)
70 {
71 	struct ocelot *ocelot = ds->priv;
72 
73 	return ocelot_port_mdb_add(ocelot, port, mdb);
74 }
75 
76 static int felix_mdb_del(struct dsa_switch *ds, int port,
77 			 const struct switchdev_obj_port_mdb *mdb)
78 {
79 	struct ocelot *ocelot = ds->priv;
80 
81 	return ocelot_port_mdb_del(ocelot, port, mdb);
82 }
83 
84 static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port,
85 				       u8 state)
86 {
87 	struct ocelot *ocelot = ds->priv;
88 
89 	return ocelot_bridge_stp_state_set(ocelot, port, state);
90 }
91 
92 static int felix_bridge_join(struct dsa_switch *ds, int port,
93 			     struct net_device *br)
94 {
95 	struct ocelot *ocelot = ds->priv;
96 
97 	return ocelot_port_bridge_join(ocelot, port, br);
98 }
99 
100 static void felix_bridge_leave(struct dsa_switch *ds, int port,
101 			       struct net_device *br)
102 {
103 	struct ocelot *ocelot = ds->priv;
104 
105 	ocelot_port_bridge_leave(ocelot, port, br);
106 }
107 
108 static int felix_vlan_prepare(struct dsa_switch *ds, int port,
109 			      const struct switchdev_obj_port_vlan *vlan)
110 {
111 	struct ocelot *ocelot = ds->priv;
112 	u16 flags = vlan->flags;
113 
114 	/* Ocelot switches copy frames as-is to the CPU, so the flags:
115 	 * egress-untagged or not, pvid or not, make no difference. This
116 	 * behavior is already better than what DSA just tries to approximate
117 	 * when it installs the VLAN with the same flags on the CPU port.
118 	 * Just accept any configuration, and don't let ocelot deny installing
119 	 * multiple native VLANs on the NPI port, because the switch doesn't
120 	 * look at the port tag settings towards the NPI interface anyway.
121 	 */
122 	if (port == ocelot->npi)
123 		return 0;
124 
125 	return ocelot_vlan_prepare(ocelot, port, vlan->vid,
126 				   flags & BRIDGE_VLAN_INFO_PVID,
127 				   flags & BRIDGE_VLAN_INFO_UNTAGGED);
128 }
129 
130 static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled)
131 {
132 	struct ocelot *ocelot = ds->priv;
133 
134 	return ocelot_port_vlan_filtering(ocelot, port, enabled);
135 }
136 
137 static int felix_vlan_add(struct dsa_switch *ds, int port,
138 			  const struct switchdev_obj_port_vlan *vlan)
139 {
140 	struct ocelot *ocelot = ds->priv;
141 	u16 flags = vlan->flags;
142 	int err;
143 
144 	err = felix_vlan_prepare(ds, port, vlan);
145 	if (err)
146 		return err;
147 
148 	return ocelot_vlan_add(ocelot, port, vlan->vid,
149 			       flags & BRIDGE_VLAN_INFO_PVID,
150 			       flags & BRIDGE_VLAN_INFO_UNTAGGED);
151 }
152 
153 static int felix_vlan_del(struct dsa_switch *ds, int port,
154 			  const struct switchdev_obj_port_vlan *vlan)
155 {
156 	struct ocelot *ocelot = ds->priv;
157 
158 	return ocelot_vlan_del(ocelot, port, vlan->vid);
159 }
160 
161 static int felix_port_enable(struct dsa_switch *ds, int port,
162 			     struct phy_device *phy)
163 {
164 	struct ocelot *ocelot = ds->priv;
165 
166 	ocelot_port_enable(ocelot, port, phy);
167 
168 	return 0;
169 }
170 
171 static void felix_port_disable(struct dsa_switch *ds, int port)
172 {
173 	struct ocelot *ocelot = ds->priv;
174 
175 	return ocelot_port_disable(ocelot, port);
176 }
177 
178 static void felix_phylink_validate(struct dsa_switch *ds, int port,
179 				   unsigned long *supported,
180 				   struct phylink_link_state *state)
181 {
182 	struct ocelot *ocelot = ds->priv;
183 	struct felix *felix = ocelot_to_felix(ocelot);
184 
185 	if (felix->info->phylink_validate)
186 		felix->info->phylink_validate(ocelot, port, supported, state);
187 }
188 
189 static void felix_phylink_mac_config(struct dsa_switch *ds, int port,
190 				     unsigned int link_an_mode,
191 				     const struct phylink_link_state *state)
192 {
193 	struct ocelot *ocelot = ds->priv;
194 	struct felix *felix = ocelot_to_felix(ocelot);
195 	struct dsa_port *dp = dsa_to_port(ds, port);
196 
197 	if (felix->pcs[port])
198 		phylink_set_pcs(dp->pl, &felix->pcs[port]->pcs);
199 }
200 
201 static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port,
202 					unsigned int link_an_mode,
203 					phy_interface_t interface)
204 {
205 	struct ocelot *ocelot = ds->priv;
206 	struct ocelot_port *ocelot_port = ocelot->ports[port];
207 
208 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_ENA_CFG);
209 	ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0);
210 }
211 
212 static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port,
213 				      unsigned int link_an_mode,
214 				      phy_interface_t interface,
215 				      struct phy_device *phydev,
216 				      int speed, int duplex,
217 				      bool tx_pause, bool rx_pause)
218 {
219 	struct ocelot *ocelot = ds->priv;
220 	struct ocelot_port *ocelot_port = ocelot->ports[port];
221 	struct felix *felix = ocelot_to_felix(ocelot);
222 	u32 mac_fc_cfg;
223 
224 	/* Take port out of reset by clearing the MAC_TX_RST, MAC_RX_RST and
225 	 * PORT_RST bits in DEV_CLOCK_CFG. Note that the way this system is
226 	 * integrated is that the MAC speed is fixed and it's the PCS who is
227 	 * performing the rate adaptation, so we have to write "1000Mbps" into
228 	 * the LINK_SPEED field of DEV_CLOCK_CFG (which is also its default
229 	 * value).
230 	 */
231 	ocelot_port_writel(ocelot_port,
232 			   DEV_CLOCK_CFG_LINK_SPEED(OCELOT_SPEED_1000),
233 			   DEV_CLOCK_CFG);
234 
235 	switch (speed) {
236 	case SPEED_10:
237 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(3);
238 		break;
239 	case SPEED_100:
240 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(2);
241 		break;
242 	case SPEED_1000:
243 	case SPEED_2500:
244 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(1);
245 		break;
246 	default:
247 		dev_err(ocelot->dev, "Unsupported speed on port %d: %d\n",
248 			port, speed);
249 		return;
250 	}
251 
252 	/* handle Rx pause in all cases, with 2500base-X this is used for rate
253 	 * adaptation.
254 	 */
255 	mac_fc_cfg |= SYS_MAC_FC_CFG_RX_FC_ENA;
256 
257 	if (tx_pause)
258 		mac_fc_cfg |= SYS_MAC_FC_CFG_TX_FC_ENA |
259 			      SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
260 			      SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
261 			      SYS_MAC_FC_CFG_ZERO_PAUSE_ENA;
262 
263 	/* Flow control. Link speed is only used here to evaluate the time
264 	 * specification in incoming pause frames.
265 	 */
266 	ocelot_write_rix(ocelot, mac_fc_cfg, SYS_MAC_FC_CFG, port);
267 
268 	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
269 
270 	/* Undo the effects of felix_phylink_mac_link_down:
271 	 * enable MAC module
272 	 */
273 	ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
274 			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
275 
276 	/* Enable receiving frames on the port, and activate auto-learning of
277 	 * MAC addresses.
278 	 */
279 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
280 			 ANA_PORT_PORT_CFG_RECV_ENA |
281 			 ANA_PORT_PORT_CFG_PORTID_VAL(port),
282 			 ANA_PORT_PORT_CFG, port);
283 
284 	/* Core: Enable port for frame transfer */
285 	ocelot_fields_write(ocelot, port,
286 			    QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
287 
288 	if (felix->info->port_sched_speed_set)
289 		felix->info->port_sched_speed_set(ocelot, port, speed);
290 }
291 
292 static void felix_port_qos_map_init(struct ocelot *ocelot, int port)
293 {
294 	int i;
295 
296 	ocelot_rmw_gix(ocelot,
297 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
298 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
299 		       ANA_PORT_QOS_CFG,
300 		       port);
301 
302 	for (i = 0; i < FELIX_NUM_TC * 2; i++) {
303 		ocelot_rmw_ix(ocelot,
304 			      (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) |
305 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i),
306 			      ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL |
307 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M,
308 			      ANA_PORT_PCP_DEI_MAP,
309 			      port, i);
310 	}
311 }
312 
313 static void felix_get_strings(struct dsa_switch *ds, int port,
314 			      u32 stringset, u8 *data)
315 {
316 	struct ocelot *ocelot = ds->priv;
317 
318 	return ocelot_get_strings(ocelot, port, stringset, data);
319 }
320 
321 static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data)
322 {
323 	struct ocelot *ocelot = ds->priv;
324 
325 	ocelot_get_ethtool_stats(ocelot, port, data);
326 }
327 
328 static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset)
329 {
330 	struct ocelot *ocelot = ds->priv;
331 
332 	return ocelot_get_sset_count(ocelot, port, sset);
333 }
334 
335 static int felix_get_ts_info(struct dsa_switch *ds, int port,
336 			     struct ethtool_ts_info *info)
337 {
338 	struct ocelot *ocelot = ds->priv;
339 
340 	return ocelot_get_ts_info(ocelot, port, info);
341 }
342 
343 static int felix_parse_ports_node(struct felix *felix,
344 				  struct device_node *ports_node,
345 				  phy_interface_t *port_phy_modes)
346 {
347 	struct ocelot *ocelot = &felix->ocelot;
348 	struct device *dev = felix->ocelot.dev;
349 	struct device_node *child;
350 
351 	for_each_available_child_of_node(ports_node, child) {
352 		phy_interface_t phy_mode;
353 		u32 port;
354 		int err;
355 
356 		/* Get switch port number from DT */
357 		if (of_property_read_u32(child, "reg", &port) < 0) {
358 			dev_err(dev, "Port number not defined in device tree "
359 				"(property \"reg\")\n");
360 			of_node_put(child);
361 			return -ENODEV;
362 		}
363 
364 		/* Get PHY mode from DT */
365 		err = of_get_phy_mode(child, &phy_mode);
366 		if (err) {
367 			dev_err(dev, "Failed to read phy-mode or "
368 				"phy-interface-type property for port %d\n",
369 				port);
370 			of_node_put(child);
371 			return -ENODEV;
372 		}
373 
374 		err = felix->info->prevalidate_phy_mode(ocelot, port, phy_mode);
375 		if (err < 0) {
376 			dev_err(dev, "Unsupported PHY mode %s on port %d\n",
377 				phy_modes(phy_mode), port);
378 			of_node_put(child);
379 			return err;
380 		}
381 
382 		port_phy_modes[port] = phy_mode;
383 	}
384 
385 	return 0;
386 }
387 
388 static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes)
389 {
390 	struct device *dev = felix->ocelot.dev;
391 	struct device_node *switch_node;
392 	struct device_node *ports_node;
393 	int err;
394 
395 	switch_node = dev->of_node;
396 
397 	ports_node = of_get_child_by_name(switch_node, "ports");
398 	if (!ports_node) {
399 		dev_err(dev, "Incorrect bindings: absent \"ports\" node\n");
400 		return -ENODEV;
401 	}
402 
403 	err = felix_parse_ports_node(felix, ports_node, port_phy_modes);
404 	of_node_put(ports_node);
405 
406 	return err;
407 }
408 
409 static int felix_init_structs(struct felix *felix, int num_phys_ports)
410 {
411 	struct ocelot *ocelot = &felix->ocelot;
412 	phy_interface_t *port_phy_modes;
413 	struct resource res;
414 	int port, i, err;
415 
416 	ocelot->num_phys_ports = num_phys_ports;
417 	ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports,
418 				     sizeof(struct ocelot_port *), GFP_KERNEL);
419 	if (!ocelot->ports)
420 		return -ENOMEM;
421 
422 	ocelot->map		= felix->info->map;
423 	ocelot->stats_layout	= felix->info->stats_layout;
424 	ocelot->num_stats	= felix->info->num_stats;
425 	ocelot->num_mact_rows	= felix->info->num_mact_rows;
426 	ocelot->vcap		= felix->info->vcap;
427 	ocelot->ops		= felix->info->ops;
428 	ocelot->inj_prefix	= OCELOT_TAG_PREFIX_SHORT;
429 	ocelot->xtr_prefix	= OCELOT_TAG_PREFIX_SHORT;
430 
431 	port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t),
432 				 GFP_KERNEL);
433 	if (!port_phy_modes)
434 		return -ENOMEM;
435 
436 	err = felix_parse_dt(felix, port_phy_modes);
437 	if (err) {
438 		kfree(port_phy_modes);
439 		return err;
440 	}
441 
442 	for (i = 0; i < TARGET_MAX; i++) {
443 		struct regmap *target;
444 
445 		if (!felix->info->target_io_res[i].name)
446 			continue;
447 
448 		memcpy(&res, &felix->info->target_io_res[i], sizeof(res));
449 		res.flags = IORESOURCE_MEM;
450 		res.start += felix->switch_base;
451 		res.end += felix->switch_base;
452 
453 		target = ocelot_regmap_init(ocelot, &res);
454 		if (IS_ERR(target)) {
455 			dev_err(ocelot->dev,
456 				"Failed to map device memory space\n");
457 			kfree(port_phy_modes);
458 			return PTR_ERR(target);
459 		}
460 
461 		ocelot->targets[i] = target;
462 	}
463 
464 	err = ocelot_regfields_init(ocelot, felix->info->regfields);
465 	if (err) {
466 		dev_err(ocelot->dev, "failed to init reg fields map\n");
467 		kfree(port_phy_modes);
468 		return err;
469 	}
470 
471 	for (port = 0; port < num_phys_ports; port++) {
472 		struct ocelot_port *ocelot_port;
473 		struct regmap *target;
474 		u8 *template;
475 
476 		ocelot_port = devm_kzalloc(ocelot->dev,
477 					   sizeof(struct ocelot_port),
478 					   GFP_KERNEL);
479 		if (!ocelot_port) {
480 			dev_err(ocelot->dev,
481 				"failed to allocate port memory\n");
482 			kfree(port_phy_modes);
483 			return -ENOMEM;
484 		}
485 
486 		memcpy(&res, &felix->info->port_io_res[port], sizeof(res));
487 		res.flags = IORESOURCE_MEM;
488 		res.start += felix->switch_base;
489 		res.end += felix->switch_base;
490 
491 		target = ocelot_regmap_init(ocelot, &res);
492 		if (IS_ERR(target)) {
493 			dev_err(ocelot->dev,
494 				"Failed to map memory space for port %d\n",
495 				port);
496 			kfree(port_phy_modes);
497 			return PTR_ERR(target);
498 		}
499 
500 		template = devm_kzalloc(ocelot->dev, OCELOT_TOTAL_TAG_LEN,
501 					GFP_KERNEL);
502 		if (!template) {
503 			dev_err(ocelot->dev,
504 				"Failed to allocate memory for DSA tag\n");
505 			kfree(port_phy_modes);
506 			return -ENOMEM;
507 		}
508 
509 		ocelot_port->phy_mode = port_phy_modes[port];
510 		ocelot_port->ocelot = ocelot;
511 		ocelot_port->target = target;
512 		ocelot_port->xmit_template = template;
513 		ocelot->ports[port] = ocelot_port;
514 
515 		felix->info->xmit_template_populate(ocelot, port);
516 	}
517 
518 	kfree(port_phy_modes);
519 
520 	if (felix->info->mdio_bus_alloc) {
521 		err = felix->info->mdio_bus_alloc(ocelot);
522 		if (err < 0)
523 			return err;
524 	}
525 
526 	return 0;
527 }
528 
529 /* The CPU port module is connected to the Node Processor Interface (NPI). This
530  * is the mode through which frames can be injected from and extracted to an
531  * external CPU, over Ethernet.
532  */
533 static void felix_npi_port_init(struct ocelot *ocelot, int port)
534 {
535 	ocelot->npi = port;
536 
537 	ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M |
538 		     QSYS_EXT_CPU_CFG_EXT_CPU_PORT(port),
539 		     QSYS_EXT_CPU_CFG);
540 
541 	/* NPI port Injection/Extraction configuration */
542 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
543 			    ocelot->xtr_prefix);
544 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
545 			    ocelot->inj_prefix);
546 
547 	/* Disable transmission of pause frames */
548 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
549 }
550 
551 /* Hardware initialization done here so that we can allocate structures with
552  * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing
553  * us to allocate structures twice (leak memory) and map PCI memory twice
554  * (which will not work).
555  */
556 static int felix_setup(struct dsa_switch *ds)
557 {
558 	struct ocelot *ocelot = ds->priv;
559 	struct felix *felix = ocelot_to_felix(ocelot);
560 	int port, err;
561 
562 	err = felix_init_structs(felix, ds->num_ports);
563 	if (err)
564 		return err;
565 
566 	err = ocelot_init(ocelot);
567 	if (err)
568 		return err;
569 
570 	if (ocelot->ptp) {
571 		err = ocelot_init_timestamp(ocelot, felix->info->ptp_caps);
572 		if (err) {
573 			dev_err(ocelot->dev,
574 				"Timestamp initialization failed\n");
575 			ocelot->ptp = 0;
576 		}
577 	}
578 
579 	for (port = 0; port < ds->num_ports; port++) {
580 		ocelot_init_port(ocelot, port);
581 
582 		if (dsa_is_cpu_port(ds, port))
583 			felix_npi_port_init(ocelot, port);
584 
585 		/* Set the default QoS Classification based on PCP and DEI
586 		 * bits of vlan tag.
587 		 */
588 		felix_port_qos_map_init(ocelot, port);
589 	}
590 
591 	/* Include the CPU port module in the forwarding mask for unknown
592 	 * unicast - the hardware default value for ANA_FLOODING_FLD_UNICAST
593 	 * excludes BIT(ocelot->num_phys_ports), and so does ocelot_init, since
594 	 * Ocelot relies on whitelisting MAC addresses towards PGID_CPU.
595 	 */
596 	ocelot_write_rix(ocelot,
597 			 ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)),
598 			 ANA_PGID_PGID, PGID_UC);
599 
600 	ds->mtu_enforcement_ingress = true;
601 	ds->assisted_learning_on_cpu_port = true;
602 
603 	return 0;
604 }
605 
606 static void felix_teardown(struct dsa_switch *ds)
607 {
608 	struct ocelot *ocelot = ds->priv;
609 	struct felix *felix = ocelot_to_felix(ocelot);
610 	int port;
611 
612 	ocelot_deinit_timestamp(ocelot);
613 	ocelot_deinit(ocelot);
614 
615 	for (port = 0; port < ocelot->num_phys_ports; port++)
616 		ocelot_deinit_port(ocelot, port);
617 
618 	if (felix->info->mdio_bus_free)
619 		felix->info->mdio_bus_free(ocelot);
620 }
621 
622 static int felix_hwtstamp_get(struct dsa_switch *ds, int port,
623 			      struct ifreq *ifr)
624 {
625 	struct ocelot *ocelot = ds->priv;
626 
627 	return ocelot_hwstamp_get(ocelot, port, ifr);
628 }
629 
630 static int felix_hwtstamp_set(struct dsa_switch *ds, int port,
631 			      struct ifreq *ifr)
632 {
633 	struct ocelot *ocelot = ds->priv;
634 
635 	return ocelot_hwstamp_set(ocelot, port, ifr);
636 }
637 
638 static bool felix_rxtstamp(struct dsa_switch *ds, int port,
639 			   struct sk_buff *skb, unsigned int type)
640 {
641 	struct skb_shared_hwtstamps *shhwtstamps;
642 	struct ocelot *ocelot = ds->priv;
643 	u8 *extraction = skb->data - ETH_HLEN - OCELOT_TAG_LEN;
644 	u32 tstamp_lo, tstamp_hi;
645 	struct timespec64 ts;
646 	u64 tstamp, val;
647 
648 	ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
649 	tstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
650 
651 	packing(extraction, &val,  116, 85, OCELOT_TAG_LEN, UNPACK, 0);
652 	tstamp_lo = (u32)val;
653 
654 	tstamp_hi = tstamp >> 32;
655 	if ((tstamp & 0xffffffff) < tstamp_lo)
656 		tstamp_hi--;
657 
658 	tstamp = ((u64)tstamp_hi << 32) | tstamp_lo;
659 
660 	shhwtstamps = skb_hwtstamps(skb);
661 	memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
662 	shhwtstamps->hwtstamp = tstamp;
663 	return false;
664 }
665 
666 static bool felix_txtstamp(struct dsa_switch *ds, int port,
667 			   struct sk_buff *clone, unsigned int type)
668 {
669 	struct ocelot *ocelot = ds->priv;
670 	struct ocelot_port *ocelot_port = ocelot->ports[port];
671 
672 	if (ocelot->ptp && (skb_shinfo(clone)->tx_flags & SKBTX_HW_TSTAMP) &&
673 	    ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
674 		ocelot_port_add_txtstamp_skb(ocelot, port, clone);
675 		return true;
676 	}
677 
678 	return false;
679 }
680 
681 static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
682 {
683 	struct ocelot *ocelot = ds->priv;
684 
685 	ocelot_port_set_maxlen(ocelot, port, new_mtu);
686 
687 	return 0;
688 }
689 
690 static int felix_get_max_mtu(struct dsa_switch *ds, int port)
691 {
692 	struct ocelot *ocelot = ds->priv;
693 
694 	return ocelot_get_max_mtu(ocelot, port);
695 }
696 
697 static int felix_cls_flower_add(struct dsa_switch *ds, int port,
698 				struct flow_cls_offload *cls, bool ingress)
699 {
700 	struct ocelot *ocelot = ds->priv;
701 
702 	return ocelot_cls_flower_replace(ocelot, port, cls, ingress);
703 }
704 
705 static int felix_cls_flower_del(struct dsa_switch *ds, int port,
706 				struct flow_cls_offload *cls, bool ingress)
707 {
708 	struct ocelot *ocelot = ds->priv;
709 
710 	return ocelot_cls_flower_destroy(ocelot, port, cls, ingress);
711 }
712 
713 static int felix_cls_flower_stats(struct dsa_switch *ds, int port,
714 				  struct flow_cls_offload *cls, bool ingress)
715 {
716 	struct ocelot *ocelot = ds->priv;
717 
718 	return ocelot_cls_flower_stats(ocelot, port, cls, ingress);
719 }
720 
721 static int felix_port_policer_add(struct dsa_switch *ds, int port,
722 				  struct dsa_mall_policer_tc_entry *policer)
723 {
724 	struct ocelot *ocelot = ds->priv;
725 	struct ocelot_policer pol = {
726 		.rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8,
727 		.burst = policer->burst,
728 	};
729 
730 	return ocelot_port_policer_add(ocelot, port, &pol);
731 }
732 
733 static void felix_port_policer_del(struct dsa_switch *ds, int port)
734 {
735 	struct ocelot *ocelot = ds->priv;
736 
737 	ocelot_port_policer_del(ocelot, port);
738 }
739 
740 static int felix_port_setup_tc(struct dsa_switch *ds, int port,
741 			       enum tc_setup_type type,
742 			       void *type_data)
743 {
744 	struct ocelot *ocelot = ds->priv;
745 	struct felix *felix = ocelot_to_felix(ocelot);
746 
747 	if (felix->info->port_setup_tc)
748 		return felix->info->port_setup_tc(ds, port, type, type_data);
749 	else
750 		return -EOPNOTSUPP;
751 }
752 
753 const struct dsa_switch_ops felix_switch_ops = {
754 	.get_tag_protocol		= felix_get_tag_protocol,
755 	.setup				= felix_setup,
756 	.teardown			= felix_teardown,
757 	.set_ageing_time		= felix_set_ageing_time,
758 	.get_strings			= felix_get_strings,
759 	.get_ethtool_stats		= felix_get_ethtool_stats,
760 	.get_sset_count			= felix_get_sset_count,
761 	.get_ts_info			= felix_get_ts_info,
762 	.phylink_validate		= felix_phylink_validate,
763 	.phylink_mac_config		= felix_phylink_mac_config,
764 	.phylink_mac_link_down		= felix_phylink_mac_link_down,
765 	.phylink_mac_link_up		= felix_phylink_mac_link_up,
766 	.port_enable			= felix_port_enable,
767 	.port_disable			= felix_port_disable,
768 	.port_fdb_dump			= felix_fdb_dump,
769 	.port_fdb_add			= felix_fdb_add,
770 	.port_fdb_del			= felix_fdb_del,
771 	.port_mdb_add			= felix_mdb_add,
772 	.port_mdb_del			= felix_mdb_del,
773 	.port_bridge_join		= felix_bridge_join,
774 	.port_bridge_leave		= felix_bridge_leave,
775 	.port_stp_state_set		= felix_bridge_stp_state_set,
776 	.port_vlan_filtering		= felix_vlan_filtering,
777 	.port_vlan_add			= felix_vlan_add,
778 	.port_vlan_del			= felix_vlan_del,
779 	.port_hwtstamp_get		= felix_hwtstamp_get,
780 	.port_hwtstamp_set		= felix_hwtstamp_set,
781 	.port_rxtstamp			= felix_rxtstamp,
782 	.port_txtstamp			= felix_txtstamp,
783 	.port_change_mtu		= felix_change_mtu,
784 	.port_max_mtu			= felix_get_max_mtu,
785 	.port_policer_add		= felix_port_policer_add,
786 	.port_policer_del		= felix_port_policer_del,
787 	.cls_flower_add			= felix_cls_flower_add,
788 	.cls_flower_del			= felix_cls_flower_del,
789 	.cls_flower_stats		= felix_cls_flower_stats,
790 	.port_setup_tc			= felix_port_setup_tc,
791 };
792 
793 struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port)
794 {
795 	struct felix *felix = ocelot_to_felix(ocelot);
796 	struct dsa_switch *ds = felix->ds;
797 
798 	if (!dsa_is_user_port(ds, port))
799 		return NULL;
800 
801 	return dsa_to_port(ds, port)->slave;
802 }
803 
804 int felix_netdev_to_port(struct net_device *dev)
805 {
806 	struct dsa_port *dp;
807 
808 	dp = dsa_port_from_netdev(dev);
809 	if (IS_ERR(dp))
810 		return -EINVAL;
811 
812 	return dp->index;
813 }
814