xref: /openbmc/linux/drivers/net/dsa/ocelot/felix.c (revision bae33f2b5afea932176c1b9096851c81dc0983de)
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;
119b7a9e0daSVladimir Oltean 	u16 flags = vlan->flags;
1202f0402feSVladimir Oltean 
1219a720680SVladimir Oltean 	/* Ocelot switches copy frames as-is to the CPU, so the flags:
1229a720680SVladimir Oltean 	 * egress-untagged or not, pvid or not, make no difference. This
1239a720680SVladimir Oltean 	 * behavior is already better than what DSA just tries to approximate
1249a720680SVladimir Oltean 	 * when it installs the VLAN with the same flags on the CPU port.
1259a720680SVladimir Oltean 	 * Just accept any configuration, and don't let ocelot deny installing
1269a720680SVladimir Oltean 	 * multiple native VLANs on the NPI port, because the switch doesn't
1279a720680SVladimir Oltean 	 * look at the port tag settings towards the NPI interface anyway.
1289a720680SVladimir Oltean 	 */
1299a720680SVladimir Oltean 	if (port == ocelot->npi)
1309a720680SVladimir Oltean 		return 0;
1319a720680SVladimir Oltean 
132b7a9e0daSVladimir Oltean 	return ocelot_vlan_prepare(ocelot, port, vlan->vid,
1332f0402feSVladimir Oltean 				   flags & BRIDGE_VLAN_INFO_PVID,
1342f0402feSVladimir Oltean 				   flags & BRIDGE_VLAN_INFO_UNTAGGED);
13556051948SVladimir Oltean }
13656051948SVladimir Oltean 
137*bae33f2bSVladimir Oltean static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled)
13856051948SVladimir Oltean {
13956051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
14056051948SVladimir Oltean 
141*bae33f2bSVladimir Oltean 	return ocelot_port_vlan_filtering(ocelot, port, enabled);
14256051948SVladimir Oltean }
14356051948SVladimir Oltean 
14456051948SVladimir Oltean static void felix_vlan_add(struct dsa_switch *ds, int port,
14556051948SVladimir Oltean 			   const struct switchdev_obj_port_vlan *vlan)
14656051948SVladimir Oltean {
14756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
148183be6f9SVladimir Oltean 	u16 flags = vlan->flags;
14956051948SVladimir Oltean 
150b7a9e0daSVladimir Oltean 	ocelot_vlan_add(ocelot, port, vlan->vid,
151183be6f9SVladimir Oltean 			flags & BRIDGE_VLAN_INFO_PVID,
152183be6f9SVladimir Oltean 			flags & BRIDGE_VLAN_INFO_UNTAGGED);
15356051948SVladimir Oltean }
15456051948SVladimir Oltean 
15556051948SVladimir Oltean static int felix_vlan_del(struct dsa_switch *ds, int port,
15656051948SVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan)
15756051948SVladimir Oltean {
15856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
15956051948SVladimir Oltean 
160b7a9e0daSVladimir Oltean 	return ocelot_vlan_del(ocelot, port, vlan->vid);
16156051948SVladimir Oltean }
16256051948SVladimir Oltean 
16356051948SVladimir Oltean static int felix_port_enable(struct dsa_switch *ds, int port,
16456051948SVladimir Oltean 			     struct phy_device *phy)
16556051948SVladimir Oltean {
16656051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
16756051948SVladimir Oltean 
16856051948SVladimir Oltean 	ocelot_port_enable(ocelot, port, phy);
16956051948SVladimir Oltean 
17056051948SVladimir Oltean 	return 0;
17156051948SVladimir Oltean }
17256051948SVladimir Oltean 
17356051948SVladimir Oltean static void felix_port_disable(struct dsa_switch *ds, int port)
17456051948SVladimir Oltean {
17556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
17656051948SVladimir Oltean 
17756051948SVladimir Oltean 	return ocelot_port_disable(ocelot, port);
17856051948SVladimir Oltean }
17956051948SVladimir Oltean 
180bdeced75SVladimir Oltean static void felix_phylink_validate(struct dsa_switch *ds, int port,
181bdeced75SVladimir Oltean 				   unsigned long *supported,
182bdeced75SVladimir Oltean 				   struct phylink_link_state *state)
183bdeced75SVladimir Oltean {
184bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
185375e1314SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
186bdeced75SVladimir Oltean 
187375e1314SVladimir Oltean 	if (felix->info->phylink_validate)
188375e1314SVladimir Oltean 		felix->info->phylink_validate(ocelot, port, supported, state);
189bdeced75SVladimir Oltean }
190bdeced75SVladimir Oltean 
191bdeced75SVladimir Oltean static void felix_phylink_mac_config(struct dsa_switch *ds, int port,
192bdeced75SVladimir Oltean 				     unsigned int link_an_mode,
193bdeced75SVladimir Oltean 				     const struct phylink_link_state *state)
194bdeced75SVladimir Oltean {
195bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
196bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
197588d0550SIoana Ciornei 	struct dsa_port *dp = dsa_to_port(ds, port);
198bdeced75SVladimir Oltean 
199588d0550SIoana Ciornei 	if (felix->pcs[port])
200588d0550SIoana Ciornei 		phylink_set_pcs(dp->pl, &felix->pcs[port]->pcs);
201bdeced75SVladimir Oltean }
202bdeced75SVladimir Oltean 
203bdeced75SVladimir Oltean static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port,
204bdeced75SVladimir Oltean 					unsigned int link_an_mode,
205bdeced75SVladimir Oltean 					phy_interface_t interface)
206bdeced75SVladimir Oltean {
207bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
208bdeced75SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
209bdeced75SVladimir Oltean 
210bdeced75SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_ENA_CFG);
211886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0);
212bdeced75SVladimir Oltean }
213bdeced75SVladimir Oltean 
214bdeced75SVladimir Oltean static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port,
215bdeced75SVladimir Oltean 				      unsigned int link_an_mode,
216bdeced75SVladimir Oltean 				      phy_interface_t interface,
2175b502a7bSRussell King 				      struct phy_device *phydev,
2185b502a7bSRussell King 				      int speed, int duplex,
2195b502a7bSRussell King 				      bool tx_pause, bool rx_pause)
220bdeced75SVladimir Oltean {
221bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
222bdeced75SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2237e14a2dcSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
2247e14a2dcSVladimir Oltean 	u32 mac_fc_cfg;
225bdeced75SVladimir Oltean 
2267e14a2dcSVladimir Oltean 	/* Take port out of reset by clearing the MAC_TX_RST, MAC_RX_RST and
2277e14a2dcSVladimir Oltean 	 * PORT_RST bits in DEV_CLOCK_CFG. Note that the way this system is
2287e14a2dcSVladimir Oltean 	 * integrated is that the MAC speed is fixed and it's the PCS who is
2297e14a2dcSVladimir Oltean 	 * performing the rate adaptation, so we have to write "1000Mbps" into
2307e14a2dcSVladimir Oltean 	 * the LINK_SPEED field of DEV_CLOCK_CFG (which is also its default
2317e14a2dcSVladimir Oltean 	 * value).
2327e14a2dcSVladimir Oltean 	 */
2337e14a2dcSVladimir Oltean 	ocelot_port_writel(ocelot_port,
2347e14a2dcSVladimir Oltean 			   DEV_CLOCK_CFG_LINK_SPEED(OCELOT_SPEED_1000),
2357e14a2dcSVladimir Oltean 			   DEV_CLOCK_CFG);
2367e14a2dcSVladimir Oltean 
2377e14a2dcSVladimir Oltean 	switch (speed) {
2387e14a2dcSVladimir Oltean 	case SPEED_10:
2397e14a2dcSVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(3);
2407e14a2dcSVladimir Oltean 		break;
2417e14a2dcSVladimir Oltean 	case SPEED_100:
2427e14a2dcSVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(2);
2437e14a2dcSVladimir Oltean 		break;
2447e14a2dcSVladimir Oltean 	case SPEED_1000:
2457e14a2dcSVladimir Oltean 	case SPEED_2500:
2467e14a2dcSVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(1);
2477e14a2dcSVladimir Oltean 		break;
2487e14a2dcSVladimir Oltean 	default:
2497e14a2dcSVladimir Oltean 		dev_err(ocelot->dev, "Unsupported speed on port %d: %d\n",
2507e14a2dcSVladimir Oltean 			port, speed);
2517e14a2dcSVladimir Oltean 		return;
2527e14a2dcSVladimir Oltean 	}
2537e14a2dcSVladimir Oltean 
2547e14a2dcSVladimir Oltean 	/* handle Rx pause in all cases, with 2500base-X this is used for rate
2557e14a2dcSVladimir Oltean 	 * adaptation.
2567e14a2dcSVladimir Oltean 	 */
2577e14a2dcSVladimir Oltean 	mac_fc_cfg |= SYS_MAC_FC_CFG_RX_FC_ENA;
2587e14a2dcSVladimir Oltean 
2597e14a2dcSVladimir Oltean 	if (tx_pause)
2607e14a2dcSVladimir Oltean 		mac_fc_cfg |= SYS_MAC_FC_CFG_TX_FC_ENA |
2617e14a2dcSVladimir Oltean 			      SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
2627e14a2dcSVladimir Oltean 			      SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
2637e14a2dcSVladimir Oltean 			      SYS_MAC_FC_CFG_ZERO_PAUSE_ENA;
2647e14a2dcSVladimir Oltean 
2657e14a2dcSVladimir Oltean 	/* Flow control. Link speed is only used here to evaluate the time
2667e14a2dcSVladimir Oltean 	 * specification in incoming pause frames.
2677e14a2dcSVladimir Oltean 	 */
2687e14a2dcSVladimir Oltean 	ocelot_write_rix(ocelot, mac_fc_cfg, SYS_MAC_FC_CFG, port);
2697e14a2dcSVladimir Oltean 
2707e14a2dcSVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
2717e14a2dcSVladimir Oltean 
2727e14a2dcSVladimir Oltean 	/* Undo the effects of felix_phylink_mac_link_down:
2737e14a2dcSVladimir Oltean 	 * enable MAC module
2747e14a2dcSVladimir Oltean 	 */
275bdeced75SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
276bdeced75SVladimir Oltean 			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
277bdeced75SVladimir Oltean 
278bdeced75SVladimir Oltean 	/* Enable receiving frames on the port, and activate auto-learning of
279bdeced75SVladimir Oltean 	 * MAC addresses.
280bdeced75SVladimir Oltean 	 */
281bdeced75SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
282bdeced75SVladimir Oltean 			 ANA_PORT_PORT_CFG_RECV_ENA |
283bdeced75SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(port),
284bdeced75SVladimir Oltean 			 ANA_PORT_PORT_CFG, port);
285bdeced75SVladimir Oltean 
286bdeced75SVladimir Oltean 	/* Core: Enable port for frame transfer */
287886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, port,
288886e1387SVladimir Oltean 			    QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
2897e14a2dcSVladimir Oltean 
2907e14a2dcSVladimir Oltean 	if (felix->info->port_sched_speed_set)
2917e14a2dcSVladimir Oltean 		felix->info->port_sched_speed_set(ocelot, port, speed);
292bdeced75SVladimir Oltean }
293bdeced75SVladimir Oltean 
294bd2b3161SXiaoliang Yang static void felix_port_qos_map_init(struct ocelot *ocelot, int port)
295bd2b3161SXiaoliang Yang {
296bd2b3161SXiaoliang Yang 	int i;
297bd2b3161SXiaoliang Yang 
298bd2b3161SXiaoliang Yang 	ocelot_rmw_gix(ocelot,
299bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
300bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
301bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG,
302bd2b3161SXiaoliang Yang 		       port);
303bd2b3161SXiaoliang Yang 
304bd2b3161SXiaoliang Yang 	for (i = 0; i < FELIX_NUM_TC * 2; i++) {
305bd2b3161SXiaoliang Yang 		ocelot_rmw_ix(ocelot,
306bd2b3161SXiaoliang Yang 			      (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) |
307bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i),
308bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL |
309bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M,
310bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP,
311bd2b3161SXiaoliang Yang 			      port, i);
312bd2b3161SXiaoliang Yang 	}
313bd2b3161SXiaoliang Yang }
314bd2b3161SXiaoliang Yang 
31556051948SVladimir Oltean static void felix_get_strings(struct dsa_switch *ds, int port,
31656051948SVladimir Oltean 			      u32 stringset, u8 *data)
31756051948SVladimir Oltean {
31856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
31956051948SVladimir Oltean 
32056051948SVladimir Oltean 	return ocelot_get_strings(ocelot, port, stringset, data);
32156051948SVladimir Oltean }
32256051948SVladimir Oltean 
32356051948SVladimir Oltean static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data)
32456051948SVladimir Oltean {
32556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
32656051948SVladimir Oltean 
32756051948SVladimir Oltean 	ocelot_get_ethtool_stats(ocelot, port, data);
32856051948SVladimir Oltean }
32956051948SVladimir Oltean 
33056051948SVladimir Oltean static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset)
33156051948SVladimir Oltean {
33256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
33356051948SVladimir Oltean 
33456051948SVladimir Oltean 	return ocelot_get_sset_count(ocelot, port, sset);
33556051948SVladimir Oltean }
33656051948SVladimir Oltean 
33756051948SVladimir Oltean static int felix_get_ts_info(struct dsa_switch *ds, int port,
33856051948SVladimir Oltean 			     struct ethtool_ts_info *info)
33956051948SVladimir Oltean {
34056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
34156051948SVladimir Oltean 
34256051948SVladimir Oltean 	return ocelot_get_ts_info(ocelot, port, info);
34356051948SVladimir Oltean }
34456051948SVladimir Oltean 
345bdeced75SVladimir Oltean static int felix_parse_ports_node(struct felix *felix,
346bdeced75SVladimir Oltean 				  struct device_node *ports_node,
347bdeced75SVladimir Oltean 				  phy_interface_t *port_phy_modes)
348bdeced75SVladimir Oltean {
349bdeced75SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
350bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
351bdeced75SVladimir Oltean 	struct device_node *child;
352bdeced75SVladimir Oltean 
35337fe45adSVladimir Oltean 	for_each_available_child_of_node(ports_node, child) {
354bdeced75SVladimir Oltean 		phy_interface_t phy_mode;
355bdeced75SVladimir Oltean 		u32 port;
356bdeced75SVladimir Oltean 		int err;
357bdeced75SVladimir Oltean 
358bdeced75SVladimir Oltean 		/* Get switch port number from DT */
359bdeced75SVladimir Oltean 		if (of_property_read_u32(child, "reg", &port) < 0) {
360bdeced75SVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
361bdeced75SVladimir Oltean 				"(property \"reg\")\n");
362bdeced75SVladimir Oltean 			of_node_put(child);
363bdeced75SVladimir Oltean 			return -ENODEV;
364bdeced75SVladimir Oltean 		}
365bdeced75SVladimir Oltean 
366bdeced75SVladimir Oltean 		/* Get PHY mode from DT */
367bdeced75SVladimir Oltean 		err = of_get_phy_mode(child, &phy_mode);
368bdeced75SVladimir Oltean 		if (err) {
369bdeced75SVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
370bdeced75SVladimir Oltean 				"phy-interface-type property for port %d\n",
371bdeced75SVladimir Oltean 				port);
372bdeced75SVladimir Oltean 			of_node_put(child);
373bdeced75SVladimir Oltean 			return -ENODEV;
374bdeced75SVladimir Oltean 		}
375bdeced75SVladimir Oltean 
376bdeced75SVladimir Oltean 		err = felix->info->prevalidate_phy_mode(ocelot, port, phy_mode);
377bdeced75SVladimir Oltean 		if (err < 0) {
378bdeced75SVladimir Oltean 			dev_err(dev, "Unsupported PHY mode %s on port %d\n",
379bdeced75SVladimir Oltean 				phy_modes(phy_mode), port);
38059ebb430SSumera Priyadarsini 			of_node_put(child);
381bdeced75SVladimir Oltean 			return err;
382bdeced75SVladimir Oltean 		}
383bdeced75SVladimir Oltean 
384bdeced75SVladimir Oltean 		port_phy_modes[port] = phy_mode;
385bdeced75SVladimir Oltean 	}
386bdeced75SVladimir Oltean 
387bdeced75SVladimir Oltean 	return 0;
388bdeced75SVladimir Oltean }
389bdeced75SVladimir Oltean 
390bdeced75SVladimir Oltean static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes)
391bdeced75SVladimir Oltean {
392bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
393bdeced75SVladimir Oltean 	struct device_node *switch_node;
394bdeced75SVladimir Oltean 	struct device_node *ports_node;
395bdeced75SVladimir Oltean 	int err;
396bdeced75SVladimir Oltean 
397bdeced75SVladimir Oltean 	switch_node = dev->of_node;
398bdeced75SVladimir Oltean 
399bdeced75SVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
400bdeced75SVladimir Oltean 	if (!ports_node) {
401bdeced75SVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" node\n");
402bdeced75SVladimir Oltean 		return -ENODEV;
403bdeced75SVladimir Oltean 	}
404bdeced75SVladimir Oltean 
405bdeced75SVladimir Oltean 	err = felix_parse_ports_node(felix, ports_node, port_phy_modes);
406bdeced75SVladimir Oltean 	of_node_put(ports_node);
407bdeced75SVladimir Oltean 
408bdeced75SVladimir Oltean 	return err;
409bdeced75SVladimir Oltean }
410bdeced75SVladimir Oltean 
41156051948SVladimir Oltean static int felix_init_structs(struct felix *felix, int num_phys_ports)
41256051948SVladimir Oltean {
41356051948SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
414bdeced75SVladimir Oltean 	phy_interface_t *port_phy_modes;
415b4024c9eSClaudiu Manoil 	struct resource res;
41656051948SVladimir Oltean 	int port, i, err;
41756051948SVladimir Oltean 
41856051948SVladimir Oltean 	ocelot->num_phys_ports = num_phys_ports;
41956051948SVladimir Oltean 	ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports,
42056051948SVladimir Oltean 				     sizeof(struct ocelot_port *), GFP_KERNEL);
42156051948SVladimir Oltean 	if (!ocelot->ports)
42256051948SVladimir Oltean 		return -ENOMEM;
42356051948SVladimir Oltean 
42456051948SVladimir Oltean 	ocelot->map		= felix->info->map;
42556051948SVladimir Oltean 	ocelot->stats_layout	= felix->info->stats_layout;
42656051948SVladimir Oltean 	ocelot->num_stats	= felix->info->num_stats;
42756051948SVladimir Oltean 	ocelot->shared_queue_sz	= felix->info->shared_queue_sz;
42821ce7f3eSVladimir Oltean 	ocelot->num_mact_rows	= felix->info->num_mact_rows;
42907d985eeSVladimir Oltean 	ocelot->vcap		= felix->info->vcap;
43056051948SVladimir Oltean 	ocelot->ops		= felix->info->ops;
4315124197cSVladimir Oltean 	ocelot->inj_prefix	= OCELOT_TAG_PREFIX_SHORT;
4325124197cSVladimir Oltean 	ocelot->xtr_prefix	= OCELOT_TAG_PREFIX_SHORT;
43356051948SVladimir Oltean 
434bdeced75SVladimir Oltean 	port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t),
435bdeced75SVladimir Oltean 				 GFP_KERNEL);
436bdeced75SVladimir Oltean 	if (!port_phy_modes)
437bdeced75SVladimir Oltean 		return -ENOMEM;
438bdeced75SVladimir Oltean 
439bdeced75SVladimir Oltean 	err = felix_parse_dt(felix, port_phy_modes);
440bdeced75SVladimir Oltean 	if (err) {
441bdeced75SVladimir Oltean 		kfree(port_phy_modes);
442bdeced75SVladimir Oltean 		return err;
443bdeced75SVladimir Oltean 	}
444bdeced75SVladimir Oltean 
44556051948SVladimir Oltean 	for (i = 0; i < TARGET_MAX; i++) {
44656051948SVladimir Oltean 		struct regmap *target;
44756051948SVladimir Oltean 
44856051948SVladimir Oltean 		if (!felix->info->target_io_res[i].name)
44956051948SVladimir Oltean 			continue;
45056051948SVladimir Oltean 
451b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->target_io_res[i], sizeof(res));
452b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
453375e1314SVladimir Oltean 		res.start += felix->switch_base;
454375e1314SVladimir Oltean 		res.end += felix->switch_base;
45556051948SVladimir Oltean 
456b4024c9eSClaudiu Manoil 		target = ocelot_regmap_init(ocelot, &res);
45756051948SVladimir Oltean 		if (IS_ERR(target)) {
45856051948SVladimir Oltean 			dev_err(ocelot->dev,
45956051948SVladimir Oltean 				"Failed to map device memory space\n");
460bdeced75SVladimir Oltean 			kfree(port_phy_modes);
46156051948SVladimir Oltean 			return PTR_ERR(target);
46256051948SVladimir Oltean 		}
46356051948SVladimir Oltean 
46456051948SVladimir Oltean 		ocelot->targets[i] = target;
46556051948SVladimir Oltean 	}
46656051948SVladimir Oltean 
46756051948SVladimir Oltean 	err = ocelot_regfields_init(ocelot, felix->info->regfields);
46856051948SVladimir Oltean 	if (err) {
46956051948SVladimir Oltean 		dev_err(ocelot->dev, "failed to init reg fields map\n");
470bdeced75SVladimir Oltean 		kfree(port_phy_modes);
47156051948SVladimir Oltean 		return err;
47256051948SVladimir Oltean 	}
47356051948SVladimir Oltean 
47456051948SVladimir Oltean 	for (port = 0; port < num_phys_ports; port++) {
47556051948SVladimir Oltean 		struct ocelot_port *ocelot_port;
47691c724cfSVladimir Oltean 		struct regmap *target;
47767c24049SVladimir Oltean 		u8 *template;
47856051948SVladimir Oltean 
47956051948SVladimir Oltean 		ocelot_port = devm_kzalloc(ocelot->dev,
48056051948SVladimir Oltean 					   sizeof(struct ocelot_port),
48156051948SVladimir Oltean 					   GFP_KERNEL);
48256051948SVladimir Oltean 		if (!ocelot_port) {
48356051948SVladimir Oltean 			dev_err(ocelot->dev,
48456051948SVladimir Oltean 				"failed to allocate port memory\n");
485bdeced75SVladimir Oltean 			kfree(port_phy_modes);
48656051948SVladimir Oltean 			return -ENOMEM;
48756051948SVladimir Oltean 		}
48856051948SVladimir Oltean 
489b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->port_io_res[port], sizeof(res));
490b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
491375e1314SVladimir Oltean 		res.start += felix->switch_base;
492375e1314SVladimir Oltean 		res.end += felix->switch_base;
49356051948SVladimir Oltean 
49491c724cfSVladimir Oltean 		target = ocelot_regmap_init(ocelot, &res);
49591c724cfSVladimir Oltean 		if (IS_ERR(target)) {
49656051948SVladimir Oltean 			dev_err(ocelot->dev,
49791c724cfSVladimir Oltean 				"Failed to map memory space for port %d\n",
49891c724cfSVladimir Oltean 				port);
499bdeced75SVladimir Oltean 			kfree(port_phy_modes);
50091c724cfSVladimir Oltean 			return PTR_ERR(target);
50156051948SVladimir Oltean 		}
50256051948SVladimir Oltean 
5035124197cSVladimir Oltean 		template = devm_kzalloc(ocelot->dev, OCELOT_TOTAL_TAG_LEN,
50467c24049SVladimir Oltean 					GFP_KERNEL);
50567c24049SVladimir Oltean 		if (!template) {
50667c24049SVladimir Oltean 			dev_err(ocelot->dev,
50767c24049SVladimir Oltean 				"Failed to allocate memory for DSA tag\n");
50867c24049SVladimir Oltean 			kfree(port_phy_modes);
50967c24049SVladimir Oltean 			return -ENOMEM;
51067c24049SVladimir Oltean 		}
51167c24049SVladimir Oltean 
512bdeced75SVladimir Oltean 		ocelot_port->phy_mode = port_phy_modes[port];
51356051948SVladimir Oltean 		ocelot_port->ocelot = ocelot;
51491c724cfSVladimir Oltean 		ocelot_port->target = target;
51567c24049SVladimir Oltean 		ocelot_port->xmit_template = template;
51656051948SVladimir Oltean 		ocelot->ports[port] = ocelot_port;
51767c24049SVladimir Oltean 
51867c24049SVladimir Oltean 		felix->info->xmit_template_populate(ocelot, port);
51956051948SVladimir Oltean 	}
52056051948SVladimir Oltean 
521bdeced75SVladimir Oltean 	kfree(port_phy_modes);
522bdeced75SVladimir Oltean 
523bdeced75SVladimir Oltean 	if (felix->info->mdio_bus_alloc) {
524bdeced75SVladimir Oltean 		err = felix->info->mdio_bus_alloc(ocelot);
525bdeced75SVladimir Oltean 		if (err < 0)
526bdeced75SVladimir Oltean 			return err;
527bdeced75SVladimir Oltean 	}
528bdeced75SVladimir Oltean 
52956051948SVladimir Oltean 	return 0;
53056051948SVladimir Oltean }
53156051948SVladimir Oltean 
5322d44b097SVladimir Oltean /* The CPU port module is connected to the Node Processor Interface (NPI). This
5332d44b097SVladimir Oltean  * is the mode through which frames can be injected from and extracted to an
5342d44b097SVladimir Oltean  * external CPU, over Ethernet.
5352d44b097SVladimir Oltean  */
5362d44b097SVladimir Oltean static void felix_npi_port_init(struct ocelot *ocelot, int port)
5372d44b097SVladimir Oltean {
5382d44b097SVladimir Oltean 	ocelot->npi = port;
5392d44b097SVladimir Oltean 
5402d44b097SVladimir Oltean 	ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M |
5412d44b097SVladimir Oltean 		     QSYS_EXT_CPU_CFG_EXT_CPU_PORT(port),
5422d44b097SVladimir Oltean 		     QSYS_EXT_CPU_CFG);
5432d44b097SVladimir Oltean 
5442d44b097SVladimir Oltean 	/* NPI port Injection/Extraction configuration */
5452d44b097SVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
5462d44b097SVladimir Oltean 			    ocelot->xtr_prefix);
5472d44b097SVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
5482d44b097SVladimir Oltean 			    ocelot->inj_prefix);
5492d44b097SVladimir Oltean 
5502d44b097SVladimir Oltean 	/* Disable transmission of pause frames */
5512d44b097SVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
5522d44b097SVladimir Oltean }
5532d44b097SVladimir Oltean 
55456051948SVladimir Oltean /* Hardware initialization done here so that we can allocate structures with
55556051948SVladimir Oltean  * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing
55656051948SVladimir Oltean  * us to allocate structures twice (leak memory) and map PCI memory twice
55756051948SVladimir Oltean  * (which will not work).
55856051948SVladimir Oltean  */
55956051948SVladimir Oltean static int felix_setup(struct dsa_switch *ds)
56056051948SVladimir Oltean {
56156051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
56256051948SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
56356051948SVladimir Oltean 	int port, err;
56456051948SVladimir Oltean 
56556051948SVladimir Oltean 	err = felix_init_structs(felix, ds->num_ports);
56656051948SVladimir Oltean 	if (err)
56756051948SVladimir Oltean 		return err;
56856051948SVladimir Oltean 
569d1cc0e93SVladimir Oltean 	err = ocelot_init(ocelot);
570d1cc0e93SVladimir Oltean 	if (err)
571d1cc0e93SVladimir Oltean 		return err;
572d1cc0e93SVladimir Oltean 
5732b49d128SYangbo Lu 	if (ocelot->ptp) {
5742ac7c6c5SVladimir Oltean 		err = ocelot_init_timestamp(ocelot, felix->info->ptp_caps);
5752b49d128SYangbo Lu 		if (err) {
5762b49d128SYangbo Lu 			dev_err(ocelot->dev,
5772b49d128SYangbo Lu 				"Timestamp initialization failed\n");
5782b49d128SYangbo Lu 			ocelot->ptp = 0;
5792b49d128SYangbo Lu 		}
5802b49d128SYangbo Lu 	}
58156051948SVladimir Oltean 
58256051948SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
58356051948SVladimir Oltean 		ocelot_init_port(ocelot, port);
58456051948SVladimir Oltean 
585b8fc7177SVladimir Oltean 		if (dsa_is_cpu_port(ds, port))
5862d44b097SVladimir Oltean 			felix_npi_port_init(ocelot, port);
587bd2b3161SXiaoliang Yang 
588bd2b3161SXiaoliang Yang 		/* Set the default QoS Classification based on PCP and DEI
589bd2b3161SXiaoliang Yang 		 * bits of vlan tag.
590bd2b3161SXiaoliang Yang 		 */
591bd2b3161SXiaoliang Yang 		felix_port_qos_map_init(ocelot, port);
59256051948SVladimir Oltean 	}
59356051948SVladimir Oltean 
5941cf3299bSVladimir Oltean 	/* Include the CPU port module in the forwarding mask for unknown
5951cf3299bSVladimir Oltean 	 * unicast - the hardware default value for ANA_FLOODING_FLD_UNICAST
5961cf3299bSVladimir Oltean 	 * excludes BIT(ocelot->num_phys_ports), and so does ocelot_init, since
5971cf3299bSVladimir Oltean 	 * Ocelot relies on whitelisting MAC addresses towards PGID_CPU.
5981cf3299bSVladimir Oltean 	 */
5991cf3299bSVladimir Oltean 	ocelot_write_rix(ocelot,
6001cf3299bSVladimir Oltean 			 ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)),
6011cf3299bSVladimir Oltean 			 ANA_PGID_PGID, PGID_UC);
6021cf3299bSVladimir Oltean 
6030b912fc9SVladimir Oltean 	ds->mtu_enforcement_ingress = true;
604626a8323SVladimir Oltean 	ds->configure_vlan_while_not_filtering = true;
605c54913c1SVladimir Oltean 	ds->assisted_learning_on_cpu_port = true;
606bdeced75SVladimir Oltean 
60756051948SVladimir Oltean 	return 0;
60856051948SVladimir Oltean }
60956051948SVladimir Oltean 
61056051948SVladimir Oltean static void felix_teardown(struct dsa_switch *ds)
61156051948SVladimir Oltean {
61256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
613bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
614e5fb512dSVladimir Oltean 	int port;
615bdeced75SVladimir Oltean 
616bdeced75SVladimir Oltean 	if (felix->info->mdio_bus_free)
617bdeced75SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
61856051948SVladimir Oltean 
619e5fb512dSVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++)
620e5fb512dSVladimir Oltean 		ocelot_deinit_port(ocelot, port);
6212b49d128SYangbo Lu 	ocelot_deinit_timestamp(ocelot);
62256051948SVladimir Oltean 	/* stop workqueue thread */
62356051948SVladimir Oltean 	ocelot_deinit(ocelot);
62456051948SVladimir Oltean }
62556051948SVladimir Oltean 
626c0bcf537SYangbo Lu static int felix_hwtstamp_get(struct dsa_switch *ds, int port,
627c0bcf537SYangbo Lu 			      struct ifreq *ifr)
628c0bcf537SYangbo Lu {
629c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
630c0bcf537SYangbo Lu 
631c0bcf537SYangbo Lu 	return ocelot_hwstamp_get(ocelot, port, ifr);
632c0bcf537SYangbo Lu }
633c0bcf537SYangbo Lu 
634c0bcf537SYangbo Lu static int felix_hwtstamp_set(struct dsa_switch *ds, int port,
635c0bcf537SYangbo Lu 			      struct ifreq *ifr)
636c0bcf537SYangbo Lu {
637c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
638c0bcf537SYangbo Lu 
639c0bcf537SYangbo Lu 	return ocelot_hwstamp_set(ocelot, port, ifr);
640c0bcf537SYangbo Lu }
641c0bcf537SYangbo Lu 
642c0bcf537SYangbo Lu static bool felix_rxtstamp(struct dsa_switch *ds, int port,
643c0bcf537SYangbo Lu 			   struct sk_buff *skb, unsigned int type)
644c0bcf537SYangbo Lu {
645c0bcf537SYangbo Lu 	struct skb_shared_hwtstamps *shhwtstamps;
646c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
647c0bcf537SYangbo Lu 	u8 *extraction = skb->data - ETH_HLEN - OCELOT_TAG_LEN;
648c0bcf537SYangbo Lu 	u32 tstamp_lo, tstamp_hi;
649c0bcf537SYangbo Lu 	struct timespec64 ts;
650c0bcf537SYangbo Lu 	u64 tstamp, val;
651c0bcf537SYangbo Lu 
652c0bcf537SYangbo Lu 	ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
653c0bcf537SYangbo Lu 	tstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
654c0bcf537SYangbo Lu 
655c0bcf537SYangbo Lu 	packing(extraction, &val,  116, 85, OCELOT_TAG_LEN, UNPACK, 0);
656c0bcf537SYangbo Lu 	tstamp_lo = (u32)val;
657c0bcf537SYangbo Lu 
658c0bcf537SYangbo Lu 	tstamp_hi = tstamp >> 32;
659c0bcf537SYangbo Lu 	if ((tstamp & 0xffffffff) < tstamp_lo)
660c0bcf537SYangbo Lu 		tstamp_hi--;
661c0bcf537SYangbo Lu 
662c0bcf537SYangbo Lu 	tstamp = ((u64)tstamp_hi << 32) | tstamp_lo;
663c0bcf537SYangbo Lu 
664c0bcf537SYangbo Lu 	shhwtstamps = skb_hwtstamps(skb);
665c0bcf537SYangbo Lu 	memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
666c0bcf537SYangbo Lu 	shhwtstamps->hwtstamp = tstamp;
667c0bcf537SYangbo Lu 	return false;
668c0bcf537SYangbo Lu }
669c0bcf537SYangbo Lu 
6703243e04aSChen Wandun static bool felix_txtstamp(struct dsa_switch *ds, int port,
671c0bcf537SYangbo Lu 			   struct sk_buff *clone, unsigned int type)
672c0bcf537SYangbo Lu {
673c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
674c0bcf537SYangbo Lu 	struct ocelot_port *ocelot_port = ocelot->ports[port];
675c0bcf537SYangbo Lu 
676e2f9a8feSVladimir Oltean 	if (ocelot->ptp && (skb_shinfo(clone)->tx_flags & SKBTX_HW_TSTAMP) &&
677e2f9a8feSVladimir Oltean 	    ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
678e2f9a8feSVladimir Oltean 		ocelot_port_add_txtstamp_skb(ocelot, port, clone);
679c0bcf537SYangbo Lu 		return true;
680e2f9a8feSVladimir Oltean 	}
681c0bcf537SYangbo Lu 
682c0bcf537SYangbo Lu 	return false;
683c0bcf537SYangbo Lu }
684c0bcf537SYangbo Lu 
6850b912fc9SVladimir Oltean static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
6860b912fc9SVladimir Oltean {
6870b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
6880b912fc9SVladimir Oltean 
6890b912fc9SVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, new_mtu);
6900b912fc9SVladimir Oltean 
6910b912fc9SVladimir Oltean 	return 0;
6920b912fc9SVladimir Oltean }
6930b912fc9SVladimir Oltean 
6940b912fc9SVladimir Oltean static int felix_get_max_mtu(struct dsa_switch *ds, int port)
6950b912fc9SVladimir Oltean {
6960b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
6970b912fc9SVladimir Oltean 
6980b912fc9SVladimir Oltean 	return ocelot_get_max_mtu(ocelot, port);
6990b912fc9SVladimir Oltean }
7000b912fc9SVladimir Oltean 
70107d985eeSVladimir Oltean static int felix_cls_flower_add(struct dsa_switch *ds, int port,
70207d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
70307d985eeSVladimir Oltean {
70407d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
70507d985eeSVladimir Oltean 
70607d985eeSVladimir Oltean 	return ocelot_cls_flower_replace(ocelot, port, cls, ingress);
70707d985eeSVladimir Oltean }
70807d985eeSVladimir Oltean 
70907d985eeSVladimir Oltean static int felix_cls_flower_del(struct dsa_switch *ds, int port,
71007d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
71107d985eeSVladimir Oltean {
71207d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
71307d985eeSVladimir Oltean 
71407d985eeSVladimir Oltean 	return ocelot_cls_flower_destroy(ocelot, port, cls, ingress);
71507d985eeSVladimir Oltean }
71607d985eeSVladimir Oltean 
71707d985eeSVladimir Oltean static int felix_cls_flower_stats(struct dsa_switch *ds, int port,
71807d985eeSVladimir Oltean 				  struct flow_cls_offload *cls, bool ingress)
71907d985eeSVladimir Oltean {
72007d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
72107d985eeSVladimir Oltean 
72207d985eeSVladimir Oltean 	return ocelot_cls_flower_stats(ocelot, port, cls, ingress);
72307d985eeSVladimir Oltean }
72407d985eeSVladimir Oltean 
725fc411eaaSVladimir Oltean static int felix_port_policer_add(struct dsa_switch *ds, int port,
726fc411eaaSVladimir Oltean 				  struct dsa_mall_policer_tc_entry *policer)
727fc411eaaSVladimir Oltean {
728fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
729fc411eaaSVladimir Oltean 	struct ocelot_policer pol = {
730fc411eaaSVladimir Oltean 		.rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8,
7315f035af7SPo Liu 		.burst = policer->burst,
732fc411eaaSVladimir Oltean 	};
733fc411eaaSVladimir Oltean 
734fc411eaaSVladimir Oltean 	return ocelot_port_policer_add(ocelot, port, &pol);
735fc411eaaSVladimir Oltean }
736fc411eaaSVladimir Oltean 
737fc411eaaSVladimir Oltean static void felix_port_policer_del(struct dsa_switch *ds, int port)
738fc411eaaSVladimir Oltean {
739fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
740fc411eaaSVladimir Oltean 
741fc411eaaSVladimir Oltean 	ocelot_port_policer_del(ocelot, port);
742fc411eaaSVladimir Oltean }
743fc411eaaSVladimir Oltean 
744de143c0eSXiaoliang Yang static int felix_port_setup_tc(struct dsa_switch *ds, int port,
745de143c0eSXiaoliang Yang 			       enum tc_setup_type type,
746de143c0eSXiaoliang Yang 			       void *type_data)
747de143c0eSXiaoliang Yang {
748de143c0eSXiaoliang Yang 	struct ocelot *ocelot = ds->priv;
749de143c0eSXiaoliang Yang 	struct felix *felix = ocelot_to_felix(ocelot);
750de143c0eSXiaoliang Yang 
751de143c0eSXiaoliang Yang 	if (felix->info->port_setup_tc)
752de143c0eSXiaoliang Yang 		return felix->info->port_setup_tc(ds, port, type, type_data);
753de143c0eSXiaoliang Yang 	else
754de143c0eSXiaoliang Yang 		return -EOPNOTSUPP;
755de143c0eSXiaoliang Yang }
756de143c0eSXiaoliang Yang 
757375e1314SVladimir Oltean const struct dsa_switch_ops felix_switch_ops = {
75856051948SVladimir Oltean 	.get_tag_protocol	= felix_get_tag_protocol,
75956051948SVladimir Oltean 	.setup			= felix_setup,
76056051948SVladimir Oltean 	.teardown		= felix_teardown,
76156051948SVladimir Oltean 	.set_ageing_time	= felix_set_ageing_time,
76256051948SVladimir Oltean 	.get_strings		= felix_get_strings,
76356051948SVladimir Oltean 	.get_ethtool_stats	= felix_get_ethtool_stats,
76456051948SVladimir Oltean 	.get_sset_count		= felix_get_sset_count,
76556051948SVladimir Oltean 	.get_ts_info		= felix_get_ts_info,
766bdeced75SVladimir Oltean 	.phylink_validate	= felix_phylink_validate,
767bdeced75SVladimir Oltean 	.phylink_mac_config	= felix_phylink_mac_config,
768bdeced75SVladimir Oltean 	.phylink_mac_link_down	= felix_phylink_mac_link_down,
769bdeced75SVladimir Oltean 	.phylink_mac_link_up	= felix_phylink_mac_link_up,
77056051948SVladimir Oltean 	.port_enable		= felix_port_enable,
77156051948SVladimir Oltean 	.port_disable		= felix_port_disable,
77256051948SVladimir Oltean 	.port_fdb_dump		= felix_fdb_dump,
77356051948SVladimir Oltean 	.port_fdb_add		= felix_fdb_add,
77456051948SVladimir Oltean 	.port_fdb_del		= felix_fdb_del,
775209edf95SVladimir Oltean 	.port_mdb_prepare	= felix_mdb_prepare,
776209edf95SVladimir Oltean 	.port_mdb_add		= felix_mdb_add,
777209edf95SVladimir Oltean 	.port_mdb_del		= felix_mdb_del,
77856051948SVladimir Oltean 	.port_bridge_join	= felix_bridge_join,
77956051948SVladimir Oltean 	.port_bridge_leave	= felix_bridge_leave,
78056051948SVladimir Oltean 	.port_stp_state_set	= felix_bridge_stp_state_set,
78156051948SVladimir Oltean 	.port_vlan_prepare	= felix_vlan_prepare,
78256051948SVladimir Oltean 	.port_vlan_filtering	= felix_vlan_filtering,
78356051948SVladimir Oltean 	.port_vlan_add		= felix_vlan_add,
78456051948SVladimir Oltean 	.port_vlan_del		= felix_vlan_del,
785c0bcf537SYangbo Lu 	.port_hwtstamp_get	= felix_hwtstamp_get,
786c0bcf537SYangbo Lu 	.port_hwtstamp_set	= felix_hwtstamp_set,
787c0bcf537SYangbo Lu 	.port_rxtstamp		= felix_rxtstamp,
788c0bcf537SYangbo Lu 	.port_txtstamp		= felix_txtstamp,
7890b912fc9SVladimir Oltean 	.port_change_mtu	= felix_change_mtu,
7900b912fc9SVladimir Oltean 	.port_max_mtu		= felix_get_max_mtu,
791fc411eaaSVladimir Oltean 	.port_policer_add	= felix_port_policer_add,
792fc411eaaSVladimir Oltean 	.port_policer_del	= felix_port_policer_del,
79307d985eeSVladimir Oltean 	.cls_flower_add		= felix_cls_flower_add,
79407d985eeSVladimir Oltean 	.cls_flower_del		= felix_cls_flower_del,
79507d985eeSVladimir Oltean 	.cls_flower_stats	= felix_cls_flower_stats,
796de143c0eSXiaoliang Yang 	.port_setup_tc		= felix_port_setup_tc,
79756051948SVladimir Oltean };
798319e4dd1SVladimir Oltean 
799319e4dd1SVladimir Oltean struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port)
800319e4dd1SVladimir Oltean {
801319e4dd1SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
802319e4dd1SVladimir Oltean 	struct dsa_switch *ds = felix->ds;
803319e4dd1SVladimir Oltean 
804319e4dd1SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
805319e4dd1SVladimir Oltean 		return NULL;
806319e4dd1SVladimir Oltean 
807319e4dd1SVladimir Oltean 	return dsa_to_port(ds, port)->slave;
808319e4dd1SVladimir Oltean }
809319e4dd1SVladimir Oltean 
810319e4dd1SVladimir Oltean int felix_netdev_to_port(struct net_device *dev)
811319e4dd1SVladimir Oltean {
812319e4dd1SVladimir Oltean 	struct dsa_port *dp;
813319e4dd1SVladimir Oltean 
814319e4dd1SVladimir Oltean 	dp = dsa_port_from_netdev(dev);
815319e4dd1SVladimir Oltean 	if (IS_ERR(dp))
816319e4dd1SVladimir Oltean 		return -EINVAL;
817319e4dd1SVladimir Oltean 
818319e4dd1SVladimir Oltean 	return dp->index;
819319e4dd1SVladimir Oltean }
820