xref: /openbmc/linux/drivers/net/dsa/ocelot/felix.c (revision c54913c1d4eeddcd7600a23ed77828c5d7c6e47c)
156051948SVladimir Oltean // SPDX-License-Identifier: GPL-2.0
256051948SVladimir Oltean /* Copyright 2019 NXP Semiconductors
3375e1314SVladimir Oltean  *
4375e1314SVladimir Oltean  * This is an umbrella module for all network switches that are
5375e1314SVladimir Oltean  * register-compatible with Ocelot and that perform I/O to their host CPU
6375e1314SVladimir Oltean  * through an NPI (Node Processor Interface) Ethernet port.
756051948SVladimir Oltean  */
856051948SVladimir Oltean #include <uapi/linux/if_bridge.h>
907d985eeSVladimir Oltean #include <soc/mscc/ocelot_vcap.h>
10bdeced75SVladimir Oltean #include <soc/mscc/ocelot_qsys.h>
11bdeced75SVladimir Oltean #include <soc/mscc/ocelot_sys.h>
12bdeced75SVladimir Oltean #include <soc/mscc/ocelot_dev.h>
13bdeced75SVladimir Oltean #include <soc/mscc/ocelot_ana.h>
142b49d128SYangbo Lu #include <soc/mscc/ocelot_ptp.h>
1556051948SVladimir Oltean #include <soc/mscc/ocelot.h>
1684705fc1SMaxim Kochetkov #include <linux/platform_device.h>
17c0bcf537SYangbo Lu #include <linux/packing.h>
1856051948SVladimir Oltean #include <linux/module.h>
19bdeced75SVladimir Oltean #include <linux/of_net.h>
2056051948SVladimir Oltean #include <linux/pci.h>
2156051948SVladimir Oltean #include <linux/of.h>
22588d0550SIoana Ciornei #include <linux/pcs-lynx.h>
23fc411eaaSVladimir Oltean #include <net/pkt_sched.h>
2456051948SVladimir Oltean #include <net/dsa.h>
2556051948SVladimir Oltean #include "felix.h"
2656051948SVladimir Oltean 
2756051948SVladimir Oltean static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds,
284d776482SFlorian Fainelli 						    int port,
294d776482SFlorian Fainelli 						    enum dsa_tag_protocol mp)
3056051948SVladimir Oltean {
3156051948SVladimir Oltean 	return DSA_TAG_PROTO_OCELOT;
3256051948SVladimir Oltean }
3356051948SVladimir Oltean 
3456051948SVladimir Oltean static int felix_set_ageing_time(struct dsa_switch *ds,
3556051948SVladimir Oltean 				 unsigned int ageing_time)
3656051948SVladimir Oltean {
3756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
3856051948SVladimir Oltean 
3956051948SVladimir Oltean 	ocelot_set_ageing_time(ocelot, ageing_time);
4056051948SVladimir Oltean 
4156051948SVladimir Oltean 	return 0;
4256051948SVladimir Oltean }
4356051948SVladimir Oltean 
4456051948SVladimir Oltean static int felix_fdb_dump(struct dsa_switch *ds, int port,
4556051948SVladimir Oltean 			  dsa_fdb_dump_cb_t *cb, void *data)
4656051948SVladimir Oltean {
4756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
4856051948SVladimir Oltean 
4956051948SVladimir Oltean 	return ocelot_fdb_dump(ocelot, port, cb, data);
5056051948SVladimir Oltean }
5156051948SVladimir Oltean 
5256051948SVladimir Oltean static int felix_fdb_add(struct dsa_switch *ds, int port,
5356051948SVladimir Oltean 			 const unsigned char *addr, u16 vid)
5456051948SVladimir Oltean {
5556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
5656051948SVladimir Oltean 
5787b0f983SVladimir Oltean 	return ocelot_fdb_add(ocelot, port, addr, vid);
5856051948SVladimir Oltean }
5956051948SVladimir Oltean 
6056051948SVladimir Oltean static int felix_fdb_del(struct dsa_switch *ds, int port,
6156051948SVladimir Oltean 			 const unsigned char *addr, u16 vid)
6256051948SVladimir Oltean {
6356051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
6456051948SVladimir Oltean 
6556051948SVladimir Oltean 	return ocelot_fdb_del(ocelot, port, addr, vid);
6656051948SVladimir Oltean }
6756051948SVladimir Oltean 
68209edf95SVladimir Oltean /* This callback needs to be present */
69209edf95SVladimir Oltean static int felix_mdb_prepare(struct dsa_switch *ds, int port,
70209edf95SVladimir Oltean 			     const struct switchdev_obj_port_mdb *mdb)
71209edf95SVladimir Oltean {
72209edf95SVladimir Oltean 	return 0;
73209edf95SVladimir Oltean }
74209edf95SVladimir Oltean 
75209edf95SVladimir Oltean static void felix_mdb_add(struct dsa_switch *ds, int port,
76209edf95SVladimir Oltean 			  const struct switchdev_obj_port_mdb *mdb)
77209edf95SVladimir Oltean {
78209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
79209edf95SVladimir Oltean 
80209edf95SVladimir Oltean 	ocelot_port_mdb_add(ocelot, port, mdb);
81209edf95SVladimir Oltean }
82209edf95SVladimir Oltean 
83209edf95SVladimir Oltean static int felix_mdb_del(struct dsa_switch *ds, int port,
84209edf95SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb)
85209edf95SVladimir Oltean {
86209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
87209edf95SVladimir Oltean 
88209edf95SVladimir Oltean 	return ocelot_port_mdb_del(ocelot, port, mdb);
89209edf95SVladimir Oltean }
90209edf95SVladimir Oltean 
9156051948SVladimir Oltean static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port,
9256051948SVladimir Oltean 				       u8 state)
9356051948SVladimir Oltean {
9456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
9556051948SVladimir Oltean 
9656051948SVladimir Oltean 	return ocelot_bridge_stp_state_set(ocelot, port, state);
9756051948SVladimir Oltean }
9856051948SVladimir Oltean 
9956051948SVladimir Oltean static int felix_bridge_join(struct dsa_switch *ds, int port,
10056051948SVladimir Oltean 			     struct net_device *br)
10156051948SVladimir Oltean {
10256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
10356051948SVladimir Oltean 
10456051948SVladimir Oltean 	return ocelot_port_bridge_join(ocelot, port, br);
10556051948SVladimir Oltean }
10656051948SVladimir Oltean 
10756051948SVladimir Oltean static void felix_bridge_leave(struct dsa_switch *ds, int port,
10856051948SVladimir Oltean 			       struct net_device *br)
10956051948SVladimir Oltean {
11056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
11156051948SVladimir Oltean 
11256051948SVladimir Oltean 	ocelot_port_bridge_leave(ocelot, port, br);
11356051948SVladimir Oltean }
11456051948SVladimir Oltean 
11556051948SVladimir Oltean static int felix_vlan_prepare(struct dsa_switch *ds, int port,
11656051948SVladimir Oltean 			      const struct switchdev_obj_port_vlan *vlan)
11756051948SVladimir Oltean {
1182f0402feSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1192f0402feSVladimir Oltean 	u16 vid, flags = vlan->flags;
1202f0402feSVladimir Oltean 	int err;
1212f0402feSVladimir Oltean 
1229a720680SVladimir Oltean 	/* Ocelot switches copy frames as-is to the CPU, so the flags:
1239a720680SVladimir Oltean 	 * egress-untagged or not, pvid or not, make no difference. This
1249a720680SVladimir Oltean 	 * behavior is already better than what DSA just tries to approximate
1259a720680SVladimir Oltean 	 * when it installs the VLAN with the same flags on the CPU port.
1269a720680SVladimir Oltean 	 * Just accept any configuration, and don't let ocelot deny installing
1279a720680SVladimir Oltean 	 * multiple native VLANs on the NPI port, because the switch doesn't
1289a720680SVladimir Oltean 	 * look at the port tag settings towards the NPI interface anyway.
1299a720680SVladimir Oltean 	 */
1309a720680SVladimir Oltean 	if (port == ocelot->npi)
1319a720680SVladimir Oltean 		return 0;
1329a720680SVladimir Oltean 
1332f0402feSVladimir Oltean 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
1342f0402feSVladimir Oltean 		err = ocelot_vlan_prepare(ocelot, port, vid,
1352f0402feSVladimir Oltean 					  flags & BRIDGE_VLAN_INFO_PVID,
1362f0402feSVladimir Oltean 					  flags & BRIDGE_VLAN_INFO_UNTAGGED);
1372f0402feSVladimir Oltean 		if (err)
1382f0402feSVladimir Oltean 			return err;
1392f0402feSVladimir Oltean 	}
1402f0402feSVladimir Oltean 
14156051948SVladimir Oltean 	return 0;
14256051948SVladimir Oltean }
14356051948SVladimir Oltean 
1442e554a7aSVladimir Oltean static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled,
1452e554a7aSVladimir Oltean 				struct switchdev_trans *trans)
14656051948SVladimir Oltean {
14756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
14856051948SVladimir Oltean 
1492e554a7aSVladimir Oltean 	return ocelot_port_vlan_filtering(ocelot, port, enabled, trans);
15056051948SVladimir Oltean }
15156051948SVladimir Oltean 
15256051948SVladimir Oltean static void felix_vlan_add(struct dsa_switch *ds, int port,
15356051948SVladimir Oltean 			   const struct switchdev_obj_port_vlan *vlan)
15456051948SVladimir Oltean {
15556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
156183be6f9SVladimir Oltean 	u16 flags = vlan->flags;
15756051948SVladimir Oltean 	u16 vid;
15856051948SVladimir Oltean 	int err;
15956051948SVladimir Oltean 
16056051948SVladimir Oltean 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
16156051948SVladimir Oltean 		err = ocelot_vlan_add(ocelot, port, vid,
162183be6f9SVladimir Oltean 				      flags & BRIDGE_VLAN_INFO_PVID,
163183be6f9SVladimir Oltean 				      flags & BRIDGE_VLAN_INFO_UNTAGGED);
16456051948SVladimir Oltean 		if (err) {
16556051948SVladimir Oltean 			dev_err(ds->dev, "Failed to add VLAN %d to port %d: %d\n",
16656051948SVladimir Oltean 				vid, port, err);
16756051948SVladimir Oltean 			return;
16856051948SVladimir Oltean 		}
16956051948SVladimir Oltean 	}
17056051948SVladimir Oltean }
17156051948SVladimir Oltean 
17256051948SVladimir Oltean static int felix_vlan_del(struct dsa_switch *ds, int port,
17356051948SVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan)
17456051948SVladimir Oltean {
17556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
17656051948SVladimir Oltean 	u16 vid;
17756051948SVladimir Oltean 	int err;
17856051948SVladimir Oltean 
17956051948SVladimir Oltean 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
18056051948SVladimir Oltean 		err = ocelot_vlan_del(ocelot, port, vid);
18156051948SVladimir Oltean 		if (err) {
18256051948SVladimir Oltean 			dev_err(ds->dev, "Failed to remove VLAN %d from port %d: %d\n",
18356051948SVladimir Oltean 				vid, port, err);
18456051948SVladimir Oltean 			return err;
18556051948SVladimir Oltean 		}
18656051948SVladimir Oltean 	}
18756051948SVladimir Oltean 	return 0;
18856051948SVladimir Oltean }
18956051948SVladimir Oltean 
19056051948SVladimir Oltean static int felix_port_enable(struct dsa_switch *ds, int port,
19156051948SVladimir Oltean 			     struct phy_device *phy)
19256051948SVladimir Oltean {
19356051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
19456051948SVladimir Oltean 
19556051948SVladimir Oltean 	ocelot_port_enable(ocelot, port, phy);
19656051948SVladimir Oltean 
19756051948SVladimir Oltean 	return 0;
19856051948SVladimir Oltean }
19956051948SVladimir Oltean 
20056051948SVladimir Oltean static void felix_port_disable(struct dsa_switch *ds, int port)
20156051948SVladimir Oltean {
20256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
20356051948SVladimir Oltean 
20456051948SVladimir Oltean 	return ocelot_port_disable(ocelot, port);
20556051948SVladimir Oltean }
20656051948SVladimir Oltean 
207bdeced75SVladimir Oltean static void felix_phylink_validate(struct dsa_switch *ds, int port,
208bdeced75SVladimir Oltean 				   unsigned long *supported,
209bdeced75SVladimir Oltean 				   struct phylink_link_state *state)
210bdeced75SVladimir Oltean {
211bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
212375e1314SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
213bdeced75SVladimir Oltean 
214375e1314SVladimir Oltean 	if (felix->info->phylink_validate)
215375e1314SVladimir Oltean 		felix->info->phylink_validate(ocelot, port, supported, state);
216bdeced75SVladimir Oltean }
217bdeced75SVladimir Oltean 
218bdeced75SVladimir Oltean static void felix_phylink_mac_config(struct dsa_switch *ds, int port,
219bdeced75SVladimir Oltean 				     unsigned int link_an_mode,
220bdeced75SVladimir Oltean 				     const struct phylink_link_state *state)
221bdeced75SVladimir Oltean {
222bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
223bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
224588d0550SIoana Ciornei 	struct dsa_port *dp = dsa_to_port(ds, port);
225bdeced75SVladimir Oltean 
226588d0550SIoana Ciornei 	if (felix->pcs[port])
227588d0550SIoana Ciornei 		phylink_set_pcs(dp->pl, &felix->pcs[port]->pcs);
228bdeced75SVladimir Oltean }
229bdeced75SVladimir Oltean 
230bdeced75SVladimir Oltean static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port,
231bdeced75SVladimir Oltean 					unsigned int link_an_mode,
232bdeced75SVladimir Oltean 					phy_interface_t interface)
233bdeced75SVladimir Oltean {
234bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
235bdeced75SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
236bdeced75SVladimir Oltean 
237bdeced75SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_ENA_CFG);
238886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0);
239bdeced75SVladimir Oltean }
240bdeced75SVladimir Oltean 
241bdeced75SVladimir Oltean static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port,
242bdeced75SVladimir Oltean 				      unsigned int link_an_mode,
243bdeced75SVladimir Oltean 				      phy_interface_t interface,
2445b502a7bSRussell King 				      struct phy_device *phydev,
2455b502a7bSRussell King 				      int speed, int duplex,
2465b502a7bSRussell King 				      bool tx_pause, bool rx_pause)
247bdeced75SVladimir Oltean {
248bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
249bdeced75SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2507e14a2dcSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
2517e14a2dcSVladimir Oltean 	u32 mac_fc_cfg;
252bdeced75SVladimir Oltean 
2537e14a2dcSVladimir Oltean 	/* Take port out of reset by clearing the MAC_TX_RST, MAC_RX_RST and
2547e14a2dcSVladimir Oltean 	 * PORT_RST bits in DEV_CLOCK_CFG. Note that the way this system is
2557e14a2dcSVladimir Oltean 	 * integrated is that the MAC speed is fixed and it's the PCS who is
2567e14a2dcSVladimir Oltean 	 * performing the rate adaptation, so we have to write "1000Mbps" into
2577e14a2dcSVladimir Oltean 	 * the LINK_SPEED field of DEV_CLOCK_CFG (which is also its default
2587e14a2dcSVladimir Oltean 	 * value).
2597e14a2dcSVladimir Oltean 	 */
2607e14a2dcSVladimir Oltean 	ocelot_port_writel(ocelot_port,
2617e14a2dcSVladimir Oltean 			   DEV_CLOCK_CFG_LINK_SPEED(OCELOT_SPEED_1000),
2627e14a2dcSVladimir Oltean 			   DEV_CLOCK_CFG);
2637e14a2dcSVladimir Oltean 
2647e14a2dcSVladimir Oltean 	switch (speed) {
2657e14a2dcSVladimir Oltean 	case SPEED_10:
2667e14a2dcSVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(3);
2677e14a2dcSVladimir Oltean 		break;
2687e14a2dcSVladimir Oltean 	case SPEED_100:
2697e14a2dcSVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(2);
2707e14a2dcSVladimir Oltean 		break;
2717e14a2dcSVladimir Oltean 	case SPEED_1000:
2727e14a2dcSVladimir Oltean 	case SPEED_2500:
2737e14a2dcSVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(1);
2747e14a2dcSVladimir Oltean 		break;
2757e14a2dcSVladimir Oltean 	default:
2767e14a2dcSVladimir Oltean 		dev_err(ocelot->dev, "Unsupported speed on port %d: %d\n",
2777e14a2dcSVladimir Oltean 			port, speed);
2787e14a2dcSVladimir Oltean 		return;
2797e14a2dcSVladimir Oltean 	}
2807e14a2dcSVladimir Oltean 
2817e14a2dcSVladimir Oltean 	/* handle Rx pause in all cases, with 2500base-X this is used for rate
2827e14a2dcSVladimir Oltean 	 * adaptation.
2837e14a2dcSVladimir Oltean 	 */
2847e14a2dcSVladimir Oltean 	mac_fc_cfg |= SYS_MAC_FC_CFG_RX_FC_ENA;
2857e14a2dcSVladimir Oltean 
2867e14a2dcSVladimir Oltean 	if (tx_pause)
2877e14a2dcSVladimir Oltean 		mac_fc_cfg |= SYS_MAC_FC_CFG_TX_FC_ENA |
2887e14a2dcSVladimir Oltean 			      SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
2897e14a2dcSVladimir Oltean 			      SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
2907e14a2dcSVladimir Oltean 			      SYS_MAC_FC_CFG_ZERO_PAUSE_ENA;
2917e14a2dcSVladimir Oltean 
2927e14a2dcSVladimir Oltean 	/* Flow control. Link speed is only used here to evaluate the time
2937e14a2dcSVladimir Oltean 	 * specification in incoming pause frames.
2947e14a2dcSVladimir Oltean 	 */
2957e14a2dcSVladimir Oltean 	ocelot_write_rix(ocelot, mac_fc_cfg, SYS_MAC_FC_CFG, port);
2967e14a2dcSVladimir Oltean 
2977e14a2dcSVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
2987e14a2dcSVladimir Oltean 
2997e14a2dcSVladimir Oltean 	/* Undo the effects of felix_phylink_mac_link_down:
3007e14a2dcSVladimir Oltean 	 * enable MAC module
3017e14a2dcSVladimir Oltean 	 */
302bdeced75SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
303bdeced75SVladimir Oltean 			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
304bdeced75SVladimir Oltean 
305bdeced75SVladimir Oltean 	/* Enable receiving frames on the port, and activate auto-learning of
306bdeced75SVladimir Oltean 	 * MAC addresses.
307bdeced75SVladimir Oltean 	 */
308bdeced75SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
309bdeced75SVladimir Oltean 			 ANA_PORT_PORT_CFG_RECV_ENA |
310bdeced75SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(port),
311bdeced75SVladimir Oltean 			 ANA_PORT_PORT_CFG, port);
312bdeced75SVladimir Oltean 
313bdeced75SVladimir Oltean 	/* Core: Enable port for frame transfer */
314886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, port,
315886e1387SVladimir Oltean 			    QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
3167e14a2dcSVladimir Oltean 
3177e14a2dcSVladimir Oltean 	if (felix->info->port_sched_speed_set)
3187e14a2dcSVladimir Oltean 		felix->info->port_sched_speed_set(ocelot, port, speed);
319bdeced75SVladimir Oltean }
320bdeced75SVladimir Oltean 
321bd2b3161SXiaoliang Yang static void felix_port_qos_map_init(struct ocelot *ocelot, int port)
322bd2b3161SXiaoliang Yang {
323bd2b3161SXiaoliang Yang 	int i;
324bd2b3161SXiaoliang Yang 
325bd2b3161SXiaoliang Yang 	ocelot_rmw_gix(ocelot,
326bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
327bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
328bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG,
329bd2b3161SXiaoliang Yang 		       port);
330bd2b3161SXiaoliang Yang 
331bd2b3161SXiaoliang Yang 	for (i = 0; i < FELIX_NUM_TC * 2; i++) {
332bd2b3161SXiaoliang Yang 		ocelot_rmw_ix(ocelot,
333bd2b3161SXiaoliang Yang 			      (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) |
334bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i),
335bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL |
336bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M,
337bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP,
338bd2b3161SXiaoliang Yang 			      port, i);
339bd2b3161SXiaoliang Yang 	}
340bd2b3161SXiaoliang Yang }
341bd2b3161SXiaoliang Yang 
34256051948SVladimir Oltean static void felix_get_strings(struct dsa_switch *ds, int port,
34356051948SVladimir Oltean 			      u32 stringset, u8 *data)
34456051948SVladimir Oltean {
34556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
34656051948SVladimir Oltean 
34756051948SVladimir Oltean 	return ocelot_get_strings(ocelot, port, stringset, data);
34856051948SVladimir Oltean }
34956051948SVladimir Oltean 
35056051948SVladimir Oltean static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data)
35156051948SVladimir Oltean {
35256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
35356051948SVladimir Oltean 
35456051948SVladimir Oltean 	ocelot_get_ethtool_stats(ocelot, port, data);
35556051948SVladimir Oltean }
35656051948SVladimir Oltean 
35756051948SVladimir Oltean static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset)
35856051948SVladimir Oltean {
35956051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
36056051948SVladimir Oltean 
36156051948SVladimir Oltean 	return ocelot_get_sset_count(ocelot, port, sset);
36256051948SVladimir Oltean }
36356051948SVladimir Oltean 
36456051948SVladimir Oltean static int felix_get_ts_info(struct dsa_switch *ds, int port,
36556051948SVladimir Oltean 			     struct ethtool_ts_info *info)
36656051948SVladimir Oltean {
36756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
36856051948SVladimir Oltean 
36956051948SVladimir Oltean 	return ocelot_get_ts_info(ocelot, port, info);
37056051948SVladimir Oltean }
37156051948SVladimir Oltean 
372bdeced75SVladimir Oltean static int felix_parse_ports_node(struct felix *felix,
373bdeced75SVladimir Oltean 				  struct device_node *ports_node,
374bdeced75SVladimir Oltean 				  phy_interface_t *port_phy_modes)
375bdeced75SVladimir Oltean {
376bdeced75SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
377bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
378bdeced75SVladimir Oltean 	struct device_node *child;
379bdeced75SVladimir Oltean 
38037fe45adSVladimir Oltean 	for_each_available_child_of_node(ports_node, child) {
381bdeced75SVladimir Oltean 		phy_interface_t phy_mode;
382bdeced75SVladimir Oltean 		u32 port;
383bdeced75SVladimir Oltean 		int err;
384bdeced75SVladimir Oltean 
385bdeced75SVladimir Oltean 		/* Get switch port number from DT */
386bdeced75SVladimir Oltean 		if (of_property_read_u32(child, "reg", &port) < 0) {
387bdeced75SVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
388bdeced75SVladimir Oltean 				"(property \"reg\")\n");
389bdeced75SVladimir Oltean 			of_node_put(child);
390bdeced75SVladimir Oltean 			return -ENODEV;
391bdeced75SVladimir Oltean 		}
392bdeced75SVladimir Oltean 
393bdeced75SVladimir Oltean 		/* Get PHY mode from DT */
394bdeced75SVladimir Oltean 		err = of_get_phy_mode(child, &phy_mode);
395bdeced75SVladimir Oltean 		if (err) {
396bdeced75SVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
397bdeced75SVladimir Oltean 				"phy-interface-type property for port %d\n",
398bdeced75SVladimir Oltean 				port);
399bdeced75SVladimir Oltean 			of_node_put(child);
400bdeced75SVladimir Oltean 			return -ENODEV;
401bdeced75SVladimir Oltean 		}
402bdeced75SVladimir Oltean 
403bdeced75SVladimir Oltean 		err = felix->info->prevalidate_phy_mode(ocelot, port, phy_mode);
404bdeced75SVladimir Oltean 		if (err < 0) {
405bdeced75SVladimir Oltean 			dev_err(dev, "Unsupported PHY mode %s on port %d\n",
406bdeced75SVladimir Oltean 				phy_modes(phy_mode), port);
40759ebb430SSumera Priyadarsini 			of_node_put(child);
408bdeced75SVladimir Oltean 			return err;
409bdeced75SVladimir Oltean 		}
410bdeced75SVladimir Oltean 
411bdeced75SVladimir Oltean 		port_phy_modes[port] = phy_mode;
412bdeced75SVladimir Oltean 	}
413bdeced75SVladimir Oltean 
414bdeced75SVladimir Oltean 	return 0;
415bdeced75SVladimir Oltean }
416bdeced75SVladimir Oltean 
417bdeced75SVladimir Oltean static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes)
418bdeced75SVladimir Oltean {
419bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
420bdeced75SVladimir Oltean 	struct device_node *switch_node;
421bdeced75SVladimir Oltean 	struct device_node *ports_node;
422bdeced75SVladimir Oltean 	int err;
423bdeced75SVladimir Oltean 
424bdeced75SVladimir Oltean 	switch_node = dev->of_node;
425bdeced75SVladimir Oltean 
426bdeced75SVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
427bdeced75SVladimir Oltean 	if (!ports_node) {
428bdeced75SVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" node\n");
429bdeced75SVladimir Oltean 		return -ENODEV;
430bdeced75SVladimir Oltean 	}
431bdeced75SVladimir Oltean 
432bdeced75SVladimir Oltean 	err = felix_parse_ports_node(felix, ports_node, port_phy_modes);
433bdeced75SVladimir Oltean 	of_node_put(ports_node);
434bdeced75SVladimir Oltean 
435bdeced75SVladimir Oltean 	return err;
436bdeced75SVladimir Oltean }
437bdeced75SVladimir Oltean 
43856051948SVladimir Oltean static int felix_init_structs(struct felix *felix, int num_phys_ports)
43956051948SVladimir Oltean {
44056051948SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
441bdeced75SVladimir Oltean 	phy_interface_t *port_phy_modes;
442b4024c9eSClaudiu Manoil 	struct resource res;
44356051948SVladimir Oltean 	int port, i, err;
44456051948SVladimir Oltean 
44556051948SVladimir Oltean 	ocelot->num_phys_ports = num_phys_ports;
44656051948SVladimir Oltean 	ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports,
44756051948SVladimir Oltean 				     sizeof(struct ocelot_port *), GFP_KERNEL);
44856051948SVladimir Oltean 	if (!ocelot->ports)
44956051948SVladimir Oltean 		return -ENOMEM;
45056051948SVladimir Oltean 
45156051948SVladimir Oltean 	ocelot->map		= felix->info->map;
45256051948SVladimir Oltean 	ocelot->stats_layout	= felix->info->stats_layout;
45356051948SVladimir Oltean 	ocelot->num_stats	= felix->info->num_stats;
45456051948SVladimir Oltean 	ocelot->shared_queue_sz	= felix->info->shared_queue_sz;
45521ce7f3eSVladimir Oltean 	ocelot->num_mact_rows	= felix->info->num_mact_rows;
45607d985eeSVladimir Oltean 	ocelot->vcap		= felix->info->vcap;
45756051948SVladimir Oltean 	ocelot->ops		= felix->info->ops;
4585124197cSVladimir Oltean 	ocelot->inj_prefix	= OCELOT_TAG_PREFIX_SHORT;
4595124197cSVladimir Oltean 	ocelot->xtr_prefix	= OCELOT_TAG_PREFIX_SHORT;
46056051948SVladimir Oltean 
461bdeced75SVladimir Oltean 	port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t),
462bdeced75SVladimir Oltean 				 GFP_KERNEL);
463bdeced75SVladimir Oltean 	if (!port_phy_modes)
464bdeced75SVladimir Oltean 		return -ENOMEM;
465bdeced75SVladimir Oltean 
466bdeced75SVladimir Oltean 	err = felix_parse_dt(felix, port_phy_modes);
467bdeced75SVladimir Oltean 	if (err) {
468bdeced75SVladimir Oltean 		kfree(port_phy_modes);
469bdeced75SVladimir Oltean 		return err;
470bdeced75SVladimir Oltean 	}
471bdeced75SVladimir Oltean 
47256051948SVladimir Oltean 	for (i = 0; i < TARGET_MAX; i++) {
47356051948SVladimir Oltean 		struct regmap *target;
47456051948SVladimir Oltean 
47556051948SVladimir Oltean 		if (!felix->info->target_io_res[i].name)
47656051948SVladimir Oltean 			continue;
47756051948SVladimir Oltean 
478b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->target_io_res[i], sizeof(res));
479b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
480375e1314SVladimir Oltean 		res.start += felix->switch_base;
481375e1314SVladimir Oltean 		res.end += felix->switch_base;
48256051948SVladimir Oltean 
483b4024c9eSClaudiu Manoil 		target = ocelot_regmap_init(ocelot, &res);
48456051948SVladimir Oltean 		if (IS_ERR(target)) {
48556051948SVladimir Oltean 			dev_err(ocelot->dev,
48656051948SVladimir Oltean 				"Failed to map device memory space\n");
487bdeced75SVladimir Oltean 			kfree(port_phy_modes);
48856051948SVladimir Oltean 			return PTR_ERR(target);
48956051948SVladimir Oltean 		}
49056051948SVladimir Oltean 
49156051948SVladimir Oltean 		ocelot->targets[i] = target;
49256051948SVladimir Oltean 	}
49356051948SVladimir Oltean 
49456051948SVladimir Oltean 	err = ocelot_regfields_init(ocelot, felix->info->regfields);
49556051948SVladimir Oltean 	if (err) {
49656051948SVladimir Oltean 		dev_err(ocelot->dev, "failed to init reg fields map\n");
497bdeced75SVladimir Oltean 		kfree(port_phy_modes);
49856051948SVladimir Oltean 		return err;
49956051948SVladimir Oltean 	}
50056051948SVladimir Oltean 
50156051948SVladimir Oltean 	for (port = 0; port < num_phys_ports; port++) {
50256051948SVladimir Oltean 		struct ocelot_port *ocelot_port;
50391c724cfSVladimir Oltean 		struct regmap *target;
50467c24049SVladimir Oltean 		u8 *template;
50556051948SVladimir Oltean 
50656051948SVladimir Oltean 		ocelot_port = devm_kzalloc(ocelot->dev,
50756051948SVladimir Oltean 					   sizeof(struct ocelot_port),
50856051948SVladimir Oltean 					   GFP_KERNEL);
50956051948SVladimir Oltean 		if (!ocelot_port) {
51056051948SVladimir Oltean 			dev_err(ocelot->dev,
51156051948SVladimir Oltean 				"failed to allocate port memory\n");
512bdeced75SVladimir Oltean 			kfree(port_phy_modes);
51356051948SVladimir Oltean 			return -ENOMEM;
51456051948SVladimir Oltean 		}
51556051948SVladimir Oltean 
516b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->port_io_res[port], sizeof(res));
517b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
518375e1314SVladimir Oltean 		res.start += felix->switch_base;
519375e1314SVladimir Oltean 		res.end += felix->switch_base;
52056051948SVladimir Oltean 
52191c724cfSVladimir Oltean 		target = ocelot_regmap_init(ocelot, &res);
52291c724cfSVladimir Oltean 		if (IS_ERR(target)) {
52356051948SVladimir Oltean 			dev_err(ocelot->dev,
52491c724cfSVladimir Oltean 				"Failed to map memory space for port %d\n",
52591c724cfSVladimir Oltean 				port);
526bdeced75SVladimir Oltean 			kfree(port_phy_modes);
52791c724cfSVladimir Oltean 			return PTR_ERR(target);
52856051948SVladimir Oltean 		}
52956051948SVladimir Oltean 
5305124197cSVladimir Oltean 		template = devm_kzalloc(ocelot->dev, OCELOT_TOTAL_TAG_LEN,
53167c24049SVladimir Oltean 					GFP_KERNEL);
53267c24049SVladimir Oltean 		if (!template) {
53367c24049SVladimir Oltean 			dev_err(ocelot->dev,
53467c24049SVladimir Oltean 				"Failed to allocate memory for DSA tag\n");
53567c24049SVladimir Oltean 			kfree(port_phy_modes);
53667c24049SVladimir Oltean 			return -ENOMEM;
53767c24049SVladimir Oltean 		}
53867c24049SVladimir Oltean 
539bdeced75SVladimir Oltean 		ocelot_port->phy_mode = port_phy_modes[port];
54056051948SVladimir Oltean 		ocelot_port->ocelot = ocelot;
54191c724cfSVladimir Oltean 		ocelot_port->target = target;
54267c24049SVladimir Oltean 		ocelot_port->xmit_template = template;
54356051948SVladimir Oltean 		ocelot->ports[port] = ocelot_port;
54467c24049SVladimir Oltean 
54567c24049SVladimir Oltean 		felix->info->xmit_template_populate(ocelot, port);
54656051948SVladimir Oltean 	}
54756051948SVladimir Oltean 
548bdeced75SVladimir Oltean 	kfree(port_phy_modes);
549bdeced75SVladimir Oltean 
550bdeced75SVladimir Oltean 	if (felix->info->mdio_bus_alloc) {
551bdeced75SVladimir Oltean 		err = felix->info->mdio_bus_alloc(ocelot);
552bdeced75SVladimir Oltean 		if (err < 0)
553bdeced75SVladimir Oltean 			return err;
554bdeced75SVladimir Oltean 	}
555bdeced75SVladimir Oltean 
55656051948SVladimir Oltean 	return 0;
55756051948SVladimir Oltean }
55856051948SVladimir Oltean 
5592d44b097SVladimir Oltean /* The CPU port module is connected to the Node Processor Interface (NPI). This
5602d44b097SVladimir Oltean  * is the mode through which frames can be injected from and extracted to an
5612d44b097SVladimir Oltean  * external CPU, over Ethernet.
5622d44b097SVladimir Oltean  */
5632d44b097SVladimir Oltean static void felix_npi_port_init(struct ocelot *ocelot, int port)
5642d44b097SVladimir Oltean {
5652d44b097SVladimir Oltean 	ocelot->npi = port;
5662d44b097SVladimir Oltean 
5672d44b097SVladimir Oltean 	ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M |
5682d44b097SVladimir Oltean 		     QSYS_EXT_CPU_CFG_EXT_CPU_PORT(port),
5692d44b097SVladimir Oltean 		     QSYS_EXT_CPU_CFG);
5702d44b097SVladimir Oltean 
5712d44b097SVladimir Oltean 	/* NPI port Injection/Extraction configuration */
5722d44b097SVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
5732d44b097SVladimir Oltean 			    ocelot->xtr_prefix);
5742d44b097SVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
5752d44b097SVladimir Oltean 			    ocelot->inj_prefix);
5762d44b097SVladimir Oltean 
5772d44b097SVladimir Oltean 	/* Disable transmission of pause frames */
5782d44b097SVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
5792d44b097SVladimir Oltean }
5802d44b097SVladimir Oltean 
58156051948SVladimir Oltean /* Hardware initialization done here so that we can allocate structures with
58256051948SVladimir Oltean  * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing
58356051948SVladimir Oltean  * us to allocate structures twice (leak memory) and map PCI memory twice
58456051948SVladimir Oltean  * (which will not work).
58556051948SVladimir Oltean  */
58656051948SVladimir Oltean static int felix_setup(struct dsa_switch *ds)
58756051948SVladimir Oltean {
58856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
58956051948SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
59056051948SVladimir Oltean 	int port, err;
59156051948SVladimir Oltean 
59256051948SVladimir Oltean 	err = felix_init_structs(felix, ds->num_ports);
59356051948SVladimir Oltean 	if (err)
59456051948SVladimir Oltean 		return err;
59556051948SVladimir Oltean 
596d1cc0e93SVladimir Oltean 	err = ocelot_init(ocelot);
597d1cc0e93SVladimir Oltean 	if (err)
598d1cc0e93SVladimir Oltean 		return err;
599d1cc0e93SVladimir Oltean 
6002b49d128SYangbo Lu 	if (ocelot->ptp) {
6012ac7c6c5SVladimir Oltean 		err = ocelot_init_timestamp(ocelot, felix->info->ptp_caps);
6022b49d128SYangbo Lu 		if (err) {
6032b49d128SYangbo Lu 			dev_err(ocelot->dev,
6042b49d128SYangbo Lu 				"Timestamp initialization failed\n");
6052b49d128SYangbo Lu 			ocelot->ptp = 0;
6062b49d128SYangbo Lu 		}
6072b49d128SYangbo Lu 	}
60856051948SVladimir Oltean 
60956051948SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
61056051948SVladimir Oltean 		ocelot_init_port(ocelot, port);
61156051948SVladimir Oltean 
612b8fc7177SVladimir Oltean 		if (dsa_is_cpu_port(ds, port))
6132d44b097SVladimir Oltean 			felix_npi_port_init(ocelot, port);
614bd2b3161SXiaoliang Yang 
615bd2b3161SXiaoliang Yang 		/* Set the default QoS Classification based on PCP and DEI
616bd2b3161SXiaoliang Yang 		 * bits of vlan tag.
617bd2b3161SXiaoliang Yang 		 */
618bd2b3161SXiaoliang Yang 		felix_port_qos_map_init(ocelot, port);
61956051948SVladimir Oltean 	}
62056051948SVladimir Oltean 
6211cf3299bSVladimir Oltean 	/* Include the CPU port module in the forwarding mask for unknown
6221cf3299bSVladimir Oltean 	 * unicast - the hardware default value for ANA_FLOODING_FLD_UNICAST
6231cf3299bSVladimir Oltean 	 * excludes BIT(ocelot->num_phys_ports), and so does ocelot_init, since
6241cf3299bSVladimir Oltean 	 * Ocelot relies on whitelisting MAC addresses towards PGID_CPU.
6251cf3299bSVladimir Oltean 	 */
6261cf3299bSVladimir Oltean 	ocelot_write_rix(ocelot,
6271cf3299bSVladimir Oltean 			 ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)),
6281cf3299bSVladimir Oltean 			 ANA_PGID_PGID, PGID_UC);
6291cf3299bSVladimir Oltean 
6300b912fc9SVladimir Oltean 	ds->mtu_enforcement_ingress = true;
631626a8323SVladimir Oltean 	ds->configure_vlan_while_not_filtering = true;
632*c54913c1SVladimir Oltean 	ds->assisted_learning_on_cpu_port = true;
633bdeced75SVladimir Oltean 
63456051948SVladimir Oltean 	return 0;
63556051948SVladimir Oltean }
63656051948SVladimir Oltean 
63756051948SVladimir Oltean static void felix_teardown(struct dsa_switch *ds)
63856051948SVladimir Oltean {
63956051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
640bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
641e5fb512dSVladimir Oltean 	int port;
642bdeced75SVladimir Oltean 
643bdeced75SVladimir Oltean 	if (felix->info->mdio_bus_free)
644bdeced75SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
64556051948SVladimir Oltean 
646e5fb512dSVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++)
647e5fb512dSVladimir Oltean 		ocelot_deinit_port(ocelot, port);
6482b49d128SYangbo Lu 	ocelot_deinit_timestamp(ocelot);
64956051948SVladimir Oltean 	/* stop workqueue thread */
65056051948SVladimir Oltean 	ocelot_deinit(ocelot);
65156051948SVladimir Oltean }
65256051948SVladimir Oltean 
653c0bcf537SYangbo Lu static int felix_hwtstamp_get(struct dsa_switch *ds, int port,
654c0bcf537SYangbo Lu 			      struct ifreq *ifr)
655c0bcf537SYangbo Lu {
656c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
657c0bcf537SYangbo Lu 
658c0bcf537SYangbo Lu 	return ocelot_hwstamp_get(ocelot, port, ifr);
659c0bcf537SYangbo Lu }
660c0bcf537SYangbo Lu 
661c0bcf537SYangbo Lu static int felix_hwtstamp_set(struct dsa_switch *ds, int port,
662c0bcf537SYangbo Lu 			      struct ifreq *ifr)
663c0bcf537SYangbo Lu {
664c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
665c0bcf537SYangbo Lu 
666c0bcf537SYangbo Lu 	return ocelot_hwstamp_set(ocelot, port, ifr);
667c0bcf537SYangbo Lu }
668c0bcf537SYangbo Lu 
669c0bcf537SYangbo Lu static bool felix_rxtstamp(struct dsa_switch *ds, int port,
670c0bcf537SYangbo Lu 			   struct sk_buff *skb, unsigned int type)
671c0bcf537SYangbo Lu {
672c0bcf537SYangbo Lu 	struct skb_shared_hwtstamps *shhwtstamps;
673c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
674c0bcf537SYangbo Lu 	u8 *extraction = skb->data - ETH_HLEN - OCELOT_TAG_LEN;
675c0bcf537SYangbo Lu 	u32 tstamp_lo, tstamp_hi;
676c0bcf537SYangbo Lu 	struct timespec64 ts;
677c0bcf537SYangbo Lu 	u64 tstamp, val;
678c0bcf537SYangbo Lu 
679c0bcf537SYangbo Lu 	ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
680c0bcf537SYangbo Lu 	tstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
681c0bcf537SYangbo Lu 
682c0bcf537SYangbo Lu 	packing(extraction, &val,  116, 85, OCELOT_TAG_LEN, UNPACK, 0);
683c0bcf537SYangbo Lu 	tstamp_lo = (u32)val;
684c0bcf537SYangbo Lu 
685c0bcf537SYangbo Lu 	tstamp_hi = tstamp >> 32;
686c0bcf537SYangbo Lu 	if ((tstamp & 0xffffffff) < tstamp_lo)
687c0bcf537SYangbo Lu 		tstamp_hi--;
688c0bcf537SYangbo Lu 
689c0bcf537SYangbo Lu 	tstamp = ((u64)tstamp_hi << 32) | tstamp_lo;
690c0bcf537SYangbo Lu 
691c0bcf537SYangbo Lu 	shhwtstamps = skb_hwtstamps(skb);
692c0bcf537SYangbo Lu 	memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
693c0bcf537SYangbo Lu 	shhwtstamps->hwtstamp = tstamp;
694c0bcf537SYangbo Lu 	return false;
695c0bcf537SYangbo Lu }
696c0bcf537SYangbo Lu 
6973243e04aSChen Wandun static bool felix_txtstamp(struct dsa_switch *ds, int port,
698c0bcf537SYangbo Lu 			   struct sk_buff *clone, unsigned int type)
699c0bcf537SYangbo Lu {
700c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
701c0bcf537SYangbo Lu 	struct ocelot_port *ocelot_port = ocelot->ports[port];
702c0bcf537SYangbo Lu 
703e2f9a8feSVladimir Oltean 	if (ocelot->ptp && (skb_shinfo(clone)->tx_flags & SKBTX_HW_TSTAMP) &&
704e2f9a8feSVladimir Oltean 	    ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
705e2f9a8feSVladimir Oltean 		ocelot_port_add_txtstamp_skb(ocelot, port, clone);
706c0bcf537SYangbo Lu 		return true;
707e2f9a8feSVladimir Oltean 	}
708c0bcf537SYangbo Lu 
709c0bcf537SYangbo Lu 	return false;
710c0bcf537SYangbo Lu }
711c0bcf537SYangbo Lu 
7120b912fc9SVladimir Oltean static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
7130b912fc9SVladimir Oltean {
7140b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
7150b912fc9SVladimir Oltean 
7160b912fc9SVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, new_mtu);
7170b912fc9SVladimir Oltean 
7180b912fc9SVladimir Oltean 	return 0;
7190b912fc9SVladimir Oltean }
7200b912fc9SVladimir Oltean 
7210b912fc9SVladimir Oltean static int felix_get_max_mtu(struct dsa_switch *ds, int port)
7220b912fc9SVladimir Oltean {
7230b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
7240b912fc9SVladimir Oltean 
7250b912fc9SVladimir Oltean 	return ocelot_get_max_mtu(ocelot, port);
7260b912fc9SVladimir Oltean }
7270b912fc9SVladimir Oltean 
72807d985eeSVladimir Oltean static int felix_cls_flower_add(struct dsa_switch *ds, int port,
72907d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
73007d985eeSVladimir Oltean {
73107d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
73207d985eeSVladimir Oltean 
73307d985eeSVladimir Oltean 	return ocelot_cls_flower_replace(ocelot, port, cls, ingress);
73407d985eeSVladimir Oltean }
73507d985eeSVladimir Oltean 
73607d985eeSVladimir Oltean static int felix_cls_flower_del(struct dsa_switch *ds, int port,
73707d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
73807d985eeSVladimir Oltean {
73907d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
74007d985eeSVladimir Oltean 
74107d985eeSVladimir Oltean 	return ocelot_cls_flower_destroy(ocelot, port, cls, ingress);
74207d985eeSVladimir Oltean }
74307d985eeSVladimir Oltean 
74407d985eeSVladimir Oltean static int felix_cls_flower_stats(struct dsa_switch *ds, int port,
74507d985eeSVladimir Oltean 				  struct flow_cls_offload *cls, bool ingress)
74607d985eeSVladimir Oltean {
74707d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
74807d985eeSVladimir Oltean 
74907d985eeSVladimir Oltean 	return ocelot_cls_flower_stats(ocelot, port, cls, ingress);
75007d985eeSVladimir Oltean }
75107d985eeSVladimir Oltean 
752fc411eaaSVladimir Oltean static int felix_port_policer_add(struct dsa_switch *ds, int port,
753fc411eaaSVladimir Oltean 				  struct dsa_mall_policer_tc_entry *policer)
754fc411eaaSVladimir Oltean {
755fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
756fc411eaaSVladimir Oltean 	struct ocelot_policer pol = {
757fc411eaaSVladimir Oltean 		.rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8,
7585f035af7SPo Liu 		.burst = policer->burst,
759fc411eaaSVladimir Oltean 	};
760fc411eaaSVladimir Oltean 
761fc411eaaSVladimir Oltean 	return ocelot_port_policer_add(ocelot, port, &pol);
762fc411eaaSVladimir Oltean }
763fc411eaaSVladimir Oltean 
764fc411eaaSVladimir Oltean static void felix_port_policer_del(struct dsa_switch *ds, int port)
765fc411eaaSVladimir Oltean {
766fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
767fc411eaaSVladimir Oltean 
768fc411eaaSVladimir Oltean 	ocelot_port_policer_del(ocelot, port);
769fc411eaaSVladimir Oltean }
770fc411eaaSVladimir Oltean 
771de143c0eSXiaoliang Yang static int felix_port_setup_tc(struct dsa_switch *ds, int port,
772de143c0eSXiaoliang Yang 			       enum tc_setup_type type,
773de143c0eSXiaoliang Yang 			       void *type_data)
774de143c0eSXiaoliang Yang {
775de143c0eSXiaoliang Yang 	struct ocelot *ocelot = ds->priv;
776de143c0eSXiaoliang Yang 	struct felix *felix = ocelot_to_felix(ocelot);
777de143c0eSXiaoliang Yang 
778de143c0eSXiaoliang Yang 	if (felix->info->port_setup_tc)
779de143c0eSXiaoliang Yang 		return felix->info->port_setup_tc(ds, port, type, type_data);
780de143c0eSXiaoliang Yang 	else
781de143c0eSXiaoliang Yang 		return -EOPNOTSUPP;
782de143c0eSXiaoliang Yang }
783de143c0eSXiaoliang Yang 
784375e1314SVladimir Oltean const struct dsa_switch_ops felix_switch_ops = {
78556051948SVladimir Oltean 	.get_tag_protocol	= felix_get_tag_protocol,
78656051948SVladimir Oltean 	.setup			= felix_setup,
78756051948SVladimir Oltean 	.teardown		= felix_teardown,
78856051948SVladimir Oltean 	.set_ageing_time	= felix_set_ageing_time,
78956051948SVladimir Oltean 	.get_strings		= felix_get_strings,
79056051948SVladimir Oltean 	.get_ethtool_stats	= felix_get_ethtool_stats,
79156051948SVladimir Oltean 	.get_sset_count		= felix_get_sset_count,
79256051948SVladimir Oltean 	.get_ts_info		= felix_get_ts_info,
793bdeced75SVladimir Oltean 	.phylink_validate	= felix_phylink_validate,
794bdeced75SVladimir Oltean 	.phylink_mac_config	= felix_phylink_mac_config,
795bdeced75SVladimir Oltean 	.phylink_mac_link_down	= felix_phylink_mac_link_down,
796bdeced75SVladimir Oltean 	.phylink_mac_link_up	= felix_phylink_mac_link_up,
79756051948SVladimir Oltean 	.port_enable		= felix_port_enable,
79856051948SVladimir Oltean 	.port_disable		= felix_port_disable,
79956051948SVladimir Oltean 	.port_fdb_dump		= felix_fdb_dump,
80056051948SVladimir Oltean 	.port_fdb_add		= felix_fdb_add,
80156051948SVladimir Oltean 	.port_fdb_del		= felix_fdb_del,
802209edf95SVladimir Oltean 	.port_mdb_prepare	= felix_mdb_prepare,
803209edf95SVladimir Oltean 	.port_mdb_add		= felix_mdb_add,
804209edf95SVladimir Oltean 	.port_mdb_del		= felix_mdb_del,
80556051948SVladimir Oltean 	.port_bridge_join	= felix_bridge_join,
80656051948SVladimir Oltean 	.port_bridge_leave	= felix_bridge_leave,
80756051948SVladimir Oltean 	.port_stp_state_set	= felix_bridge_stp_state_set,
80856051948SVladimir Oltean 	.port_vlan_prepare	= felix_vlan_prepare,
80956051948SVladimir Oltean 	.port_vlan_filtering	= felix_vlan_filtering,
81056051948SVladimir Oltean 	.port_vlan_add		= felix_vlan_add,
81156051948SVladimir Oltean 	.port_vlan_del		= felix_vlan_del,
812c0bcf537SYangbo Lu 	.port_hwtstamp_get	= felix_hwtstamp_get,
813c0bcf537SYangbo Lu 	.port_hwtstamp_set	= felix_hwtstamp_set,
814c0bcf537SYangbo Lu 	.port_rxtstamp		= felix_rxtstamp,
815c0bcf537SYangbo Lu 	.port_txtstamp		= felix_txtstamp,
8160b912fc9SVladimir Oltean 	.port_change_mtu	= felix_change_mtu,
8170b912fc9SVladimir Oltean 	.port_max_mtu		= felix_get_max_mtu,
818fc411eaaSVladimir Oltean 	.port_policer_add	= felix_port_policer_add,
819fc411eaaSVladimir Oltean 	.port_policer_del	= felix_port_policer_del,
82007d985eeSVladimir Oltean 	.cls_flower_add		= felix_cls_flower_add,
82107d985eeSVladimir Oltean 	.cls_flower_del		= felix_cls_flower_del,
82207d985eeSVladimir Oltean 	.cls_flower_stats	= felix_cls_flower_stats,
823de143c0eSXiaoliang Yang 	.port_setup_tc		= felix_port_setup_tc,
82456051948SVladimir Oltean };
825319e4dd1SVladimir Oltean 
826319e4dd1SVladimir Oltean struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port)
827319e4dd1SVladimir Oltean {
828319e4dd1SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
829319e4dd1SVladimir Oltean 	struct dsa_switch *ds = felix->ds;
830319e4dd1SVladimir Oltean 
831319e4dd1SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
832319e4dd1SVladimir Oltean 		return NULL;
833319e4dd1SVladimir Oltean 
834319e4dd1SVladimir Oltean 	return dsa_to_port(ds, port)->slave;
835319e4dd1SVladimir Oltean }
836319e4dd1SVladimir Oltean 
837319e4dd1SVladimir Oltean int felix_netdev_to_port(struct net_device *dev)
838319e4dd1SVladimir Oltean {
839319e4dd1SVladimir Oltean 	struct dsa_port *dp;
840319e4dd1SVladimir Oltean 
841319e4dd1SVladimir Oltean 	dp = dsa_port_from_netdev(dev);
842319e4dd1SVladimir Oltean 	if (IS_ERR(dp))
843319e4dd1SVladimir Oltean 		return -EINVAL;
844319e4dd1SVladimir Oltean 
845319e4dd1SVladimir Oltean 	return dp->index;
846319e4dd1SVladimir Oltean }
847