xref: /openbmc/linux/drivers/net/dsa/ocelot/felix.c (revision 2ac7c6c5b61b3eaeca5da998a16d912bdfb62bae)
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 /* This callback needs to be present */
11656051948SVladimir Oltean static int felix_vlan_prepare(struct dsa_switch *ds, int port,
11756051948SVladimir Oltean 			      const struct switchdev_obj_port_vlan *vlan)
11856051948SVladimir Oltean {
11956051948SVladimir Oltean 	return 0;
12056051948SVladimir Oltean }
12156051948SVladimir Oltean 
12256051948SVladimir Oltean static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled)
12356051948SVladimir Oltean {
12456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
12556051948SVladimir Oltean 
12656051948SVladimir Oltean 	ocelot_port_vlan_filtering(ocelot, port, enabled);
12756051948SVladimir Oltean 
12856051948SVladimir Oltean 	return 0;
12956051948SVladimir Oltean }
13056051948SVladimir Oltean 
13156051948SVladimir Oltean static void felix_vlan_add(struct dsa_switch *ds, int port,
13256051948SVladimir Oltean 			   const struct switchdev_obj_port_vlan *vlan)
13356051948SVladimir Oltean {
13456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
135183be6f9SVladimir Oltean 	u16 flags = vlan->flags;
13656051948SVladimir Oltean 	u16 vid;
13756051948SVladimir Oltean 	int err;
13856051948SVladimir Oltean 
139183be6f9SVladimir Oltean 	if (dsa_is_cpu_port(ds, port))
140183be6f9SVladimir Oltean 		flags &= ~BRIDGE_VLAN_INFO_UNTAGGED;
141183be6f9SVladimir Oltean 
14256051948SVladimir Oltean 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
14356051948SVladimir Oltean 		err = ocelot_vlan_add(ocelot, port, vid,
144183be6f9SVladimir Oltean 				      flags & BRIDGE_VLAN_INFO_PVID,
145183be6f9SVladimir Oltean 				      flags & BRIDGE_VLAN_INFO_UNTAGGED);
14656051948SVladimir Oltean 		if (err) {
14756051948SVladimir Oltean 			dev_err(ds->dev, "Failed to add VLAN %d to port %d: %d\n",
14856051948SVladimir Oltean 				vid, port, err);
14956051948SVladimir Oltean 			return;
15056051948SVladimir Oltean 		}
15156051948SVladimir Oltean 	}
15256051948SVladimir Oltean }
15356051948SVladimir Oltean 
15456051948SVladimir Oltean static int felix_vlan_del(struct dsa_switch *ds, int port,
15556051948SVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan)
15656051948SVladimir Oltean {
15756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
15856051948SVladimir Oltean 	u16 vid;
15956051948SVladimir Oltean 	int err;
16056051948SVladimir Oltean 
16156051948SVladimir Oltean 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
16256051948SVladimir Oltean 		err = ocelot_vlan_del(ocelot, port, vid);
16356051948SVladimir Oltean 		if (err) {
16456051948SVladimir Oltean 			dev_err(ds->dev, "Failed to remove VLAN %d from port %d: %d\n",
16556051948SVladimir Oltean 				vid, port, err);
16656051948SVladimir Oltean 			return err;
16756051948SVladimir Oltean 		}
16856051948SVladimir Oltean 	}
16956051948SVladimir Oltean 	return 0;
17056051948SVladimir Oltean }
17156051948SVladimir Oltean 
17256051948SVladimir Oltean static int felix_port_enable(struct dsa_switch *ds, int port,
17356051948SVladimir Oltean 			     struct phy_device *phy)
17456051948SVladimir Oltean {
17556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
17656051948SVladimir Oltean 
17756051948SVladimir Oltean 	ocelot_port_enable(ocelot, port, phy);
17856051948SVladimir Oltean 
17956051948SVladimir Oltean 	return 0;
18056051948SVladimir Oltean }
18156051948SVladimir Oltean 
18256051948SVladimir Oltean static void felix_port_disable(struct dsa_switch *ds, int port)
18356051948SVladimir Oltean {
18456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
18556051948SVladimir Oltean 
18656051948SVladimir Oltean 	return ocelot_port_disable(ocelot, port);
18756051948SVladimir Oltean }
18856051948SVladimir Oltean 
189bdeced75SVladimir Oltean static void felix_phylink_validate(struct dsa_switch *ds, int port,
190bdeced75SVladimir Oltean 				   unsigned long *supported,
191bdeced75SVladimir Oltean 				   struct phylink_link_state *state)
192bdeced75SVladimir Oltean {
193bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
194375e1314SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
195bdeced75SVladimir Oltean 
196375e1314SVladimir Oltean 	if (felix->info->phylink_validate)
197375e1314SVladimir Oltean 		felix->info->phylink_validate(ocelot, port, supported, state);
198bdeced75SVladimir Oltean }
199bdeced75SVladimir Oltean 
200bdeced75SVladimir Oltean static void felix_phylink_mac_config(struct dsa_switch *ds, int port,
201bdeced75SVladimir Oltean 				     unsigned int link_an_mode,
202bdeced75SVladimir Oltean 				     const struct phylink_link_state *state)
203bdeced75SVladimir Oltean {
204bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
205bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
206588d0550SIoana Ciornei 	struct dsa_port *dp = dsa_to_port(ds, port);
207bdeced75SVladimir Oltean 
208588d0550SIoana Ciornei 	if (felix->pcs[port])
209588d0550SIoana Ciornei 		phylink_set_pcs(dp->pl, &felix->pcs[port]->pcs);
210bdeced75SVladimir Oltean }
211bdeced75SVladimir Oltean 
212bdeced75SVladimir Oltean static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port,
213bdeced75SVladimir Oltean 					unsigned int link_an_mode,
214bdeced75SVladimir Oltean 					phy_interface_t interface)
215bdeced75SVladimir Oltean {
216bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
217bdeced75SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
218bdeced75SVladimir Oltean 
219bdeced75SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_ENA_CFG);
220886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0);
221bdeced75SVladimir Oltean }
222bdeced75SVladimir Oltean 
223bdeced75SVladimir Oltean static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port,
224bdeced75SVladimir Oltean 				      unsigned int link_an_mode,
225bdeced75SVladimir Oltean 				      phy_interface_t interface,
2265b502a7bSRussell King 				      struct phy_device *phydev,
2275b502a7bSRussell King 				      int speed, int duplex,
2285b502a7bSRussell King 				      bool tx_pause, bool rx_pause)
229bdeced75SVladimir Oltean {
230bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
231bdeced75SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2327e14a2dcSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
2337e14a2dcSVladimir Oltean 	u32 mac_fc_cfg;
234bdeced75SVladimir Oltean 
2357e14a2dcSVladimir Oltean 	/* Take port out of reset by clearing the MAC_TX_RST, MAC_RX_RST and
2367e14a2dcSVladimir Oltean 	 * PORT_RST bits in DEV_CLOCK_CFG. Note that the way this system is
2377e14a2dcSVladimir Oltean 	 * integrated is that the MAC speed is fixed and it's the PCS who is
2387e14a2dcSVladimir Oltean 	 * performing the rate adaptation, so we have to write "1000Mbps" into
2397e14a2dcSVladimir Oltean 	 * the LINK_SPEED field of DEV_CLOCK_CFG (which is also its default
2407e14a2dcSVladimir Oltean 	 * value).
2417e14a2dcSVladimir Oltean 	 */
2427e14a2dcSVladimir Oltean 	ocelot_port_writel(ocelot_port,
2437e14a2dcSVladimir Oltean 			   DEV_CLOCK_CFG_LINK_SPEED(OCELOT_SPEED_1000),
2447e14a2dcSVladimir Oltean 			   DEV_CLOCK_CFG);
2457e14a2dcSVladimir Oltean 
2467e14a2dcSVladimir Oltean 	switch (speed) {
2477e14a2dcSVladimir Oltean 	case SPEED_10:
2487e14a2dcSVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(3);
2497e14a2dcSVladimir Oltean 		break;
2507e14a2dcSVladimir Oltean 	case SPEED_100:
2517e14a2dcSVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(2);
2527e14a2dcSVladimir Oltean 		break;
2537e14a2dcSVladimir Oltean 	case SPEED_1000:
2547e14a2dcSVladimir Oltean 	case SPEED_2500:
2557e14a2dcSVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(1);
2567e14a2dcSVladimir Oltean 		break;
2577e14a2dcSVladimir Oltean 	default:
2587e14a2dcSVladimir Oltean 		dev_err(ocelot->dev, "Unsupported speed on port %d: %d\n",
2597e14a2dcSVladimir Oltean 			port, speed);
2607e14a2dcSVladimir Oltean 		return;
2617e14a2dcSVladimir Oltean 	}
2627e14a2dcSVladimir Oltean 
2637e14a2dcSVladimir Oltean 	/* handle Rx pause in all cases, with 2500base-X this is used for rate
2647e14a2dcSVladimir Oltean 	 * adaptation.
2657e14a2dcSVladimir Oltean 	 */
2667e14a2dcSVladimir Oltean 	mac_fc_cfg |= SYS_MAC_FC_CFG_RX_FC_ENA;
2677e14a2dcSVladimir Oltean 
2687e14a2dcSVladimir Oltean 	if (tx_pause)
2697e14a2dcSVladimir Oltean 		mac_fc_cfg |= SYS_MAC_FC_CFG_TX_FC_ENA |
2707e14a2dcSVladimir Oltean 			      SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
2717e14a2dcSVladimir Oltean 			      SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
2727e14a2dcSVladimir Oltean 			      SYS_MAC_FC_CFG_ZERO_PAUSE_ENA;
2737e14a2dcSVladimir Oltean 
2747e14a2dcSVladimir Oltean 	/* Flow control. Link speed is only used here to evaluate the time
2757e14a2dcSVladimir Oltean 	 * specification in incoming pause frames.
2767e14a2dcSVladimir Oltean 	 */
2777e14a2dcSVladimir Oltean 	ocelot_write_rix(ocelot, mac_fc_cfg, SYS_MAC_FC_CFG, port);
2787e14a2dcSVladimir Oltean 
2797e14a2dcSVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
2807e14a2dcSVladimir Oltean 
2817e14a2dcSVladimir Oltean 	/* Undo the effects of felix_phylink_mac_link_down:
2827e14a2dcSVladimir Oltean 	 * enable MAC module
2837e14a2dcSVladimir Oltean 	 */
284bdeced75SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
285bdeced75SVladimir Oltean 			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
286bdeced75SVladimir Oltean 
287bdeced75SVladimir Oltean 	/* Enable receiving frames on the port, and activate auto-learning of
288bdeced75SVladimir Oltean 	 * MAC addresses.
289bdeced75SVladimir Oltean 	 */
290bdeced75SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
291bdeced75SVladimir Oltean 			 ANA_PORT_PORT_CFG_RECV_ENA |
292bdeced75SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(port),
293bdeced75SVladimir Oltean 			 ANA_PORT_PORT_CFG, port);
294bdeced75SVladimir Oltean 
295bdeced75SVladimir Oltean 	/* Core: Enable port for frame transfer */
296886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, port,
297886e1387SVladimir Oltean 			    QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
2987e14a2dcSVladimir Oltean 
2997e14a2dcSVladimir Oltean 	if (felix->info->port_sched_speed_set)
3007e14a2dcSVladimir Oltean 		felix->info->port_sched_speed_set(ocelot, port, speed);
301bdeced75SVladimir Oltean }
302bdeced75SVladimir Oltean 
303bd2b3161SXiaoliang Yang static void felix_port_qos_map_init(struct ocelot *ocelot, int port)
304bd2b3161SXiaoliang Yang {
305bd2b3161SXiaoliang Yang 	int i;
306bd2b3161SXiaoliang Yang 
307bd2b3161SXiaoliang Yang 	ocelot_rmw_gix(ocelot,
308bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
309bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
310bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG,
311bd2b3161SXiaoliang Yang 		       port);
312bd2b3161SXiaoliang Yang 
313bd2b3161SXiaoliang Yang 	for (i = 0; i < FELIX_NUM_TC * 2; i++) {
314bd2b3161SXiaoliang Yang 		ocelot_rmw_ix(ocelot,
315bd2b3161SXiaoliang Yang 			      (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) |
316bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i),
317bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL |
318bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M,
319bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP,
320bd2b3161SXiaoliang Yang 			      port, i);
321bd2b3161SXiaoliang Yang 	}
322bd2b3161SXiaoliang Yang }
323bd2b3161SXiaoliang Yang 
32456051948SVladimir Oltean static void felix_get_strings(struct dsa_switch *ds, int port,
32556051948SVladimir Oltean 			      u32 stringset, u8 *data)
32656051948SVladimir Oltean {
32756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
32856051948SVladimir Oltean 
32956051948SVladimir Oltean 	return ocelot_get_strings(ocelot, port, stringset, data);
33056051948SVladimir Oltean }
33156051948SVladimir Oltean 
33256051948SVladimir Oltean static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data)
33356051948SVladimir Oltean {
33456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
33556051948SVladimir Oltean 
33656051948SVladimir Oltean 	ocelot_get_ethtool_stats(ocelot, port, data);
33756051948SVladimir Oltean }
33856051948SVladimir Oltean 
33956051948SVladimir Oltean static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset)
34056051948SVladimir Oltean {
34156051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
34256051948SVladimir Oltean 
34356051948SVladimir Oltean 	return ocelot_get_sset_count(ocelot, port, sset);
34456051948SVladimir Oltean }
34556051948SVladimir Oltean 
34656051948SVladimir Oltean static int felix_get_ts_info(struct dsa_switch *ds, int port,
34756051948SVladimir Oltean 			     struct ethtool_ts_info *info)
34856051948SVladimir Oltean {
34956051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
35056051948SVladimir Oltean 
35156051948SVladimir Oltean 	return ocelot_get_ts_info(ocelot, port, info);
35256051948SVladimir Oltean }
35356051948SVladimir Oltean 
354bdeced75SVladimir Oltean static int felix_parse_ports_node(struct felix *felix,
355bdeced75SVladimir Oltean 				  struct device_node *ports_node,
356bdeced75SVladimir Oltean 				  phy_interface_t *port_phy_modes)
357bdeced75SVladimir Oltean {
358bdeced75SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
359bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
360bdeced75SVladimir Oltean 	struct device_node *child;
361bdeced75SVladimir Oltean 
36237fe45adSVladimir Oltean 	for_each_available_child_of_node(ports_node, child) {
363bdeced75SVladimir Oltean 		phy_interface_t phy_mode;
364bdeced75SVladimir Oltean 		u32 port;
365bdeced75SVladimir Oltean 		int err;
366bdeced75SVladimir Oltean 
367bdeced75SVladimir Oltean 		/* Get switch port number from DT */
368bdeced75SVladimir Oltean 		if (of_property_read_u32(child, "reg", &port) < 0) {
369bdeced75SVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
370bdeced75SVladimir Oltean 				"(property \"reg\")\n");
371bdeced75SVladimir Oltean 			of_node_put(child);
372bdeced75SVladimir Oltean 			return -ENODEV;
373bdeced75SVladimir Oltean 		}
374bdeced75SVladimir Oltean 
375bdeced75SVladimir Oltean 		/* Get PHY mode from DT */
376bdeced75SVladimir Oltean 		err = of_get_phy_mode(child, &phy_mode);
377bdeced75SVladimir Oltean 		if (err) {
378bdeced75SVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
379bdeced75SVladimir Oltean 				"phy-interface-type property for port %d\n",
380bdeced75SVladimir Oltean 				port);
381bdeced75SVladimir Oltean 			of_node_put(child);
382bdeced75SVladimir Oltean 			return -ENODEV;
383bdeced75SVladimir Oltean 		}
384bdeced75SVladimir Oltean 
385bdeced75SVladimir Oltean 		err = felix->info->prevalidate_phy_mode(ocelot, port, phy_mode);
386bdeced75SVladimir Oltean 		if (err < 0) {
387bdeced75SVladimir Oltean 			dev_err(dev, "Unsupported PHY mode %s on port %d\n",
388bdeced75SVladimir Oltean 				phy_modes(phy_mode), port);
38959ebb430SSumera Priyadarsini 			of_node_put(child);
390bdeced75SVladimir Oltean 			return err;
391bdeced75SVladimir Oltean 		}
392bdeced75SVladimir Oltean 
393bdeced75SVladimir Oltean 		port_phy_modes[port] = phy_mode;
394bdeced75SVladimir Oltean 	}
395bdeced75SVladimir Oltean 
396bdeced75SVladimir Oltean 	return 0;
397bdeced75SVladimir Oltean }
398bdeced75SVladimir Oltean 
399bdeced75SVladimir Oltean static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes)
400bdeced75SVladimir Oltean {
401bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
402bdeced75SVladimir Oltean 	struct device_node *switch_node;
403bdeced75SVladimir Oltean 	struct device_node *ports_node;
404bdeced75SVladimir Oltean 	int err;
405bdeced75SVladimir Oltean 
406bdeced75SVladimir Oltean 	switch_node = dev->of_node;
407bdeced75SVladimir Oltean 
408bdeced75SVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
409bdeced75SVladimir Oltean 	if (!ports_node) {
410bdeced75SVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" node\n");
411bdeced75SVladimir Oltean 		return -ENODEV;
412bdeced75SVladimir Oltean 	}
413bdeced75SVladimir Oltean 
414bdeced75SVladimir Oltean 	err = felix_parse_ports_node(felix, ports_node, port_phy_modes);
415bdeced75SVladimir Oltean 	of_node_put(ports_node);
416bdeced75SVladimir Oltean 
417bdeced75SVladimir Oltean 	return err;
418bdeced75SVladimir Oltean }
419bdeced75SVladimir Oltean 
42056051948SVladimir Oltean static int felix_init_structs(struct felix *felix, int num_phys_ports)
42156051948SVladimir Oltean {
42256051948SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
423bdeced75SVladimir Oltean 	phy_interface_t *port_phy_modes;
424b4024c9eSClaudiu Manoil 	struct resource res;
42556051948SVladimir Oltean 	int port, i, err;
42656051948SVladimir Oltean 
42756051948SVladimir Oltean 	ocelot->num_phys_ports = num_phys_ports;
42856051948SVladimir Oltean 	ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports,
42956051948SVladimir Oltean 				     sizeof(struct ocelot_port *), GFP_KERNEL);
43056051948SVladimir Oltean 	if (!ocelot->ports)
43156051948SVladimir Oltean 		return -ENOMEM;
43256051948SVladimir Oltean 
43356051948SVladimir Oltean 	ocelot->map		= felix->info->map;
43456051948SVladimir Oltean 	ocelot->stats_layout	= felix->info->stats_layout;
43556051948SVladimir Oltean 	ocelot->num_stats	= felix->info->num_stats;
43656051948SVladimir Oltean 	ocelot->shared_queue_sz	= felix->info->shared_queue_sz;
43721ce7f3eSVladimir Oltean 	ocelot->num_mact_rows	= felix->info->num_mact_rows;
43807d985eeSVladimir Oltean 	ocelot->vcap_is2_keys	= felix->info->vcap_is2_keys;
43907d985eeSVladimir Oltean 	ocelot->vcap_is2_actions= felix->info->vcap_is2_actions;
44007d985eeSVladimir Oltean 	ocelot->vcap		= felix->info->vcap;
44156051948SVladimir Oltean 	ocelot->ops		= felix->info->ops;
44256051948SVladimir Oltean 
443bdeced75SVladimir Oltean 	port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t),
444bdeced75SVladimir Oltean 				 GFP_KERNEL);
445bdeced75SVladimir Oltean 	if (!port_phy_modes)
446bdeced75SVladimir Oltean 		return -ENOMEM;
447bdeced75SVladimir Oltean 
448bdeced75SVladimir Oltean 	err = felix_parse_dt(felix, port_phy_modes);
449bdeced75SVladimir Oltean 	if (err) {
450bdeced75SVladimir Oltean 		kfree(port_phy_modes);
451bdeced75SVladimir Oltean 		return err;
452bdeced75SVladimir Oltean 	}
453bdeced75SVladimir Oltean 
45456051948SVladimir Oltean 	for (i = 0; i < TARGET_MAX; i++) {
45556051948SVladimir Oltean 		struct regmap *target;
45656051948SVladimir Oltean 
45756051948SVladimir Oltean 		if (!felix->info->target_io_res[i].name)
45856051948SVladimir Oltean 			continue;
45956051948SVladimir Oltean 
460b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->target_io_res[i], sizeof(res));
461b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
462375e1314SVladimir Oltean 		res.start += felix->switch_base;
463375e1314SVladimir Oltean 		res.end += felix->switch_base;
46456051948SVladimir Oltean 
465b4024c9eSClaudiu Manoil 		target = ocelot_regmap_init(ocelot, &res);
46656051948SVladimir Oltean 		if (IS_ERR(target)) {
46756051948SVladimir Oltean 			dev_err(ocelot->dev,
46856051948SVladimir Oltean 				"Failed to map device memory space\n");
469bdeced75SVladimir Oltean 			kfree(port_phy_modes);
47056051948SVladimir Oltean 			return PTR_ERR(target);
47156051948SVladimir Oltean 		}
47256051948SVladimir Oltean 
47356051948SVladimir Oltean 		ocelot->targets[i] = target;
47456051948SVladimir Oltean 	}
47556051948SVladimir Oltean 
47656051948SVladimir Oltean 	err = ocelot_regfields_init(ocelot, felix->info->regfields);
47756051948SVladimir Oltean 	if (err) {
47856051948SVladimir Oltean 		dev_err(ocelot->dev, "failed to init reg fields map\n");
479bdeced75SVladimir Oltean 		kfree(port_phy_modes);
48056051948SVladimir Oltean 		return err;
48156051948SVladimir Oltean 	}
48256051948SVladimir Oltean 
48356051948SVladimir Oltean 	for (port = 0; port < num_phys_ports; port++) {
48456051948SVladimir Oltean 		struct ocelot_port *ocelot_port;
48591c724cfSVladimir Oltean 		struct regmap *target;
48667c24049SVladimir Oltean 		u8 *template;
48756051948SVladimir Oltean 
48856051948SVladimir Oltean 		ocelot_port = devm_kzalloc(ocelot->dev,
48956051948SVladimir Oltean 					   sizeof(struct ocelot_port),
49056051948SVladimir Oltean 					   GFP_KERNEL);
49156051948SVladimir Oltean 		if (!ocelot_port) {
49256051948SVladimir Oltean 			dev_err(ocelot->dev,
49356051948SVladimir Oltean 				"failed to allocate port memory\n");
494bdeced75SVladimir Oltean 			kfree(port_phy_modes);
49556051948SVladimir Oltean 			return -ENOMEM;
49656051948SVladimir Oltean 		}
49756051948SVladimir Oltean 
498b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->port_io_res[port], sizeof(res));
499b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
500375e1314SVladimir Oltean 		res.start += felix->switch_base;
501375e1314SVladimir Oltean 		res.end += felix->switch_base;
50256051948SVladimir Oltean 
50391c724cfSVladimir Oltean 		target = ocelot_regmap_init(ocelot, &res);
50491c724cfSVladimir Oltean 		if (IS_ERR(target)) {
50556051948SVladimir Oltean 			dev_err(ocelot->dev,
50691c724cfSVladimir Oltean 				"Failed to map memory space for port %d\n",
50791c724cfSVladimir Oltean 				port);
508bdeced75SVladimir Oltean 			kfree(port_phy_modes);
50991c724cfSVladimir Oltean 			return PTR_ERR(target);
51056051948SVladimir Oltean 		}
51156051948SVladimir Oltean 
51267c24049SVladimir Oltean 		template = devm_kzalloc(ocelot->dev, OCELOT_TAG_LEN,
51367c24049SVladimir Oltean 					GFP_KERNEL);
51467c24049SVladimir Oltean 		if (!template) {
51567c24049SVladimir Oltean 			dev_err(ocelot->dev,
51667c24049SVladimir Oltean 				"Failed to allocate memory for DSA tag\n");
51767c24049SVladimir Oltean 			kfree(port_phy_modes);
51867c24049SVladimir Oltean 			return -ENOMEM;
51967c24049SVladimir Oltean 		}
52067c24049SVladimir Oltean 
521bdeced75SVladimir Oltean 		ocelot_port->phy_mode = port_phy_modes[port];
52256051948SVladimir Oltean 		ocelot_port->ocelot = ocelot;
52391c724cfSVladimir Oltean 		ocelot_port->target = target;
52467c24049SVladimir Oltean 		ocelot_port->xmit_template = template;
52556051948SVladimir Oltean 		ocelot->ports[port] = ocelot_port;
52667c24049SVladimir Oltean 
52767c24049SVladimir Oltean 		felix->info->xmit_template_populate(ocelot, port);
52856051948SVladimir Oltean 	}
52956051948SVladimir Oltean 
530bdeced75SVladimir Oltean 	kfree(port_phy_modes);
531bdeced75SVladimir Oltean 
532bdeced75SVladimir Oltean 	if (felix->info->mdio_bus_alloc) {
533bdeced75SVladimir Oltean 		err = felix->info->mdio_bus_alloc(ocelot);
534bdeced75SVladimir Oltean 		if (err < 0)
535bdeced75SVladimir Oltean 			return err;
536bdeced75SVladimir Oltean 	}
537bdeced75SVladimir Oltean 
53856051948SVladimir Oltean 	return 0;
53956051948SVladimir Oltean }
54056051948SVladimir Oltean 
54156051948SVladimir Oltean /* Hardware initialization done here so that we can allocate structures with
54256051948SVladimir Oltean  * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing
54356051948SVladimir Oltean  * us to allocate structures twice (leak memory) and map PCI memory twice
54456051948SVladimir Oltean  * (which will not work).
54556051948SVladimir Oltean  */
54656051948SVladimir Oltean static int felix_setup(struct dsa_switch *ds)
54756051948SVladimir Oltean {
54856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
54956051948SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
55056051948SVladimir Oltean 	int port, err;
5513c7b51bdSXiaoliang Yang 	int tc;
55256051948SVladimir Oltean 
55356051948SVladimir Oltean 	err = felix_init_structs(felix, ds->num_ports);
55456051948SVladimir Oltean 	if (err)
55556051948SVladimir Oltean 		return err;
55656051948SVladimir Oltean 
55756051948SVladimir Oltean 	ocelot_init(ocelot);
5582b49d128SYangbo Lu 	if (ocelot->ptp) {
559*2ac7c6c5SVladimir Oltean 		err = ocelot_init_timestamp(ocelot, felix->info->ptp_caps);
5602b49d128SYangbo Lu 		if (err) {
5612b49d128SYangbo Lu 			dev_err(ocelot->dev,
5622b49d128SYangbo Lu 				"Timestamp initialization failed\n");
5632b49d128SYangbo Lu 			ocelot->ptp = 0;
5642b49d128SYangbo Lu 		}
5652b49d128SYangbo Lu 	}
56656051948SVladimir Oltean 
56756051948SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
56856051948SVladimir Oltean 		ocelot_init_port(ocelot, port);
56956051948SVladimir Oltean 
57069df578cSVladimir Oltean 		/* Bring up the CPU port module and configure the NPI port */
571b8fc7177SVladimir Oltean 		if (dsa_is_cpu_port(ds, port))
57269df578cSVladimir Oltean 			ocelot_configure_cpu(ocelot, port,
57356051948SVladimir Oltean 					     OCELOT_TAG_PREFIX_NONE,
57456051948SVladimir Oltean 					     OCELOT_TAG_PREFIX_LONG);
575bd2b3161SXiaoliang Yang 
576bd2b3161SXiaoliang Yang 		/* Set the default QoS Classification based on PCP and DEI
577bd2b3161SXiaoliang Yang 		 * bits of vlan tag.
578bd2b3161SXiaoliang Yang 		 */
579bd2b3161SXiaoliang Yang 		felix_port_qos_map_init(ocelot, port);
58056051948SVladimir Oltean 	}
58156051948SVladimir Oltean 
5821cf3299bSVladimir Oltean 	/* Include the CPU port module in the forwarding mask for unknown
5831cf3299bSVladimir Oltean 	 * unicast - the hardware default value for ANA_FLOODING_FLD_UNICAST
5841cf3299bSVladimir Oltean 	 * excludes BIT(ocelot->num_phys_ports), and so does ocelot_init, since
5851cf3299bSVladimir Oltean 	 * Ocelot relies on whitelisting MAC addresses towards PGID_CPU.
5861cf3299bSVladimir Oltean 	 */
5871cf3299bSVladimir Oltean 	ocelot_write_rix(ocelot,
5881cf3299bSVladimir Oltean 			 ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)),
5891cf3299bSVladimir Oltean 			 ANA_PGID_PGID, PGID_UC);
5903c7b51bdSXiaoliang Yang 	/* Setup the per-traffic class flooding PGIDs */
5913c7b51bdSXiaoliang Yang 	for (tc = 0; tc < FELIX_NUM_TC; tc++)
5923c7b51bdSXiaoliang Yang 		ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
5933c7b51bdSXiaoliang Yang 				 ANA_FLOODING_FLD_BROADCAST(PGID_MC) |
5943c7b51bdSXiaoliang Yang 				 ANA_FLOODING_FLD_UNICAST(PGID_UC),
5953c7b51bdSXiaoliang Yang 				 ANA_FLOODING, tc);
5961cf3299bSVladimir Oltean 
5970b912fc9SVladimir Oltean 	ds->mtu_enforcement_ingress = true;
598626a8323SVladimir Oltean 	ds->configure_vlan_while_not_filtering = true;
599bdeced75SVladimir Oltean 
60056051948SVladimir Oltean 	return 0;
60156051948SVladimir Oltean }
60256051948SVladimir Oltean 
60356051948SVladimir Oltean static void felix_teardown(struct dsa_switch *ds)
60456051948SVladimir Oltean {
60556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
606bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
607bdeced75SVladimir Oltean 
608bdeced75SVladimir Oltean 	if (felix->info->mdio_bus_free)
609bdeced75SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
61056051948SVladimir Oltean 
6112b49d128SYangbo Lu 	ocelot_deinit_timestamp(ocelot);
61256051948SVladimir Oltean 	/* stop workqueue thread */
61356051948SVladimir Oltean 	ocelot_deinit(ocelot);
61456051948SVladimir Oltean }
61556051948SVladimir Oltean 
616c0bcf537SYangbo Lu static int felix_hwtstamp_get(struct dsa_switch *ds, int port,
617c0bcf537SYangbo Lu 			      struct ifreq *ifr)
618c0bcf537SYangbo Lu {
619c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
620c0bcf537SYangbo Lu 
621c0bcf537SYangbo Lu 	return ocelot_hwstamp_get(ocelot, port, ifr);
622c0bcf537SYangbo Lu }
623c0bcf537SYangbo Lu 
624c0bcf537SYangbo Lu static int felix_hwtstamp_set(struct dsa_switch *ds, int port,
625c0bcf537SYangbo Lu 			      struct ifreq *ifr)
626c0bcf537SYangbo Lu {
627c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
628c0bcf537SYangbo Lu 
629c0bcf537SYangbo Lu 	return ocelot_hwstamp_set(ocelot, port, ifr);
630c0bcf537SYangbo Lu }
631c0bcf537SYangbo Lu 
632c0bcf537SYangbo Lu static bool felix_rxtstamp(struct dsa_switch *ds, int port,
633c0bcf537SYangbo Lu 			   struct sk_buff *skb, unsigned int type)
634c0bcf537SYangbo Lu {
635c0bcf537SYangbo Lu 	struct skb_shared_hwtstamps *shhwtstamps;
636c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
637c0bcf537SYangbo Lu 	u8 *extraction = skb->data - ETH_HLEN - OCELOT_TAG_LEN;
638c0bcf537SYangbo Lu 	u32 tstamp_lo, tstamp_hi;
639c0bcf537SYangbo Lu 	struct timespec64 ts;
640c0bcf537SYangbo Lu 	u64 tstamp, val;
641c0bcf537SYangbo Lu 
642c0bcf537SYangbo Lu 	ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
643c0bcf537SYangbo Lu 	tstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
644c0bcf537SYangbo Lu 
645c0bcf537SYangbo Lu 	packing(extraction, &val,  116, 85, OCELOT_TAG_LEN, UNPACK, 0);
646c0bcf537SYangbo Lu 	tstamp_lo = (u32)val;
647c0bcf537SYangbo Lu 
648c0bcf537SYangbo Lu 	tstamp_hi = tstamp >> 32;
649c0bcf537SYangbo Lu 	if ((tstamp & 0xffffffff) < tstamp_lo)
650c0bcf537SYangbo Lu 		tstamp_hi--;
651c0bcf537SYangbo Lu 
652c0bcf537SYangbo Lu 	tstamp = ((u64)tstamp_hi << 32) | tstamp_lo;
653c0bcf537SYangbo Lu 
654c0bcf537SYangbo Lu 	shhwtstamps = skb_hwtstamps(skb);
655c0bcf537SYangbo Lu 	memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
656c0bcf537SYangbo Lu 	shhwtstamps->hwtstamp = tstamp;
657c0bcf537SYangbo Lu 	return false;
658c0bcf537SYangbo Lu }
659c0bcf537SYangbo Lu 
6603243e04aSChen Wandun static bool felix_txtstamp(struct dsa_switch *ds, int port,
661c0bcf537SYangbo Lu 			   struct sk_buff *clone, unsigned int type)
662c0bcf537SYangbo Lu {
663c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
664c0bcf537SYangbo Lu 	struct ocelot_port *ocelot_port = ocelot->ports[port];
665c0bcf537SYangbo Lu 
666c0bcf537SYangbo Lu 	if (!ocelot_port_add_txtstamp_skb(ocelot_port, clone))
667c0bcf537SYangbo Lu 		return true;
668c0bcf537SYangbo Lu 
669c0bcf537SYangbo Lu 	return false;
670c0bcf537SYangbo Lu }
671c0bcf537SYangbo Lu 
6720b912fc9SVladimir Oltean static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
6730b912fc9SVladimir Oltean {
6740b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
6750b912fc9SVladimir Oltean 
6760b912fc9SVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, new_mtu);
6770b912fc9SVladimir Oltean 
6780b912fc9SVladimir Oltean 	return 0;
6790b912fc9SVladimir Oltean }
6800b912fc9SVladimir Oltean 
6810b912fc9SVladimir Oltean static int felix_get_max_mtu(struct dsa_switch *ds, int port)
6820b912fc9SVladimir Oltean {
6830b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
6840b912fc9SVladimir Oltean 
6850b912fc9SVladimir Oltean 	return ocelot_get_max_mtu(ocelot, port);
6860b912fc9SVladimir Oltean }
6870b912fc9SVladimir Oltean 
68807d985eeSVladimir Oltean static int felix_cls_flower_add(struct dsa_switch *ds, int port,
68907d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
69007d985eeSVladimir Oltean {
69107d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
69207d985eeSVladimir Oltean 
69307d985eeSVladimir Oltean 	return ocelot_cls_flower_replace(ocelot, port, cls, ingress);
69407d985eeSVladimir Oltean }
69507d985eeSVladimir Oltean 
69607d985eeSVladimir Oltean static int felix_cls_flower_del(struct dsa_switch *ds, int port,
69707d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
69807d985eeSVladimir Oltean {
69907d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
70007d985eeSVladimir Oltean 
70107d985eeSVladimir Oltean 	return ocelot_cls_flower_destroy(ocelot, port, cls, ingress);
70207d985eeSVladimir Oltean }
70307d985eeSVladimir Oltean 
70407d985eeSVladimir Oltean static int felix_cls_flower_stats(struct dsa_switch *ds, int port,
70507d985eeSVladimir Oltean 				  struct flow_cls_offload *cls, bool ingress)
70607d985eeSVladimir Oltean {
70707d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
70807d985eeSVladimir Oltean 
70907d985eeSVladimir Oltean 	return ocelot_cls_flower_stats(ocelot, port, cls, ingress);
71007d985eeSVladimir Oltean }
71107d985eeSVladimir Oltean 
712fc411eaaSVladimir Oltean static int felix_port_policer_add(struct dsa_switch *ds, int port,
713fc411eaaSVladimir Oltean 				  struct dsa_mall_policer_tc_entry *policer)
714fc411eaaSVladimir Oltean {
715fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
716fc411eaaSVladimir Oltean 	struct ocelot_policer pol = {
717fc411eaaSVladimir Oltean 		.rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8,
7185f035af7SPo Liu 		.burst = policer->burst,
719fc411eaaSVladimir Oltean 	};
720fc411eaaSVladimir Oltean 
721fc411eaaSVladimir Oltean 	return ocelot_port_policer_add(ocelot, port, &pol);
722fc411eaaSVladimir Oltean }
723fc411eaaSVladimir Oltean 
724fc411eaaSVladimir Oltean static void felix_port_policer_del(struct dsa_switch *ds, int port)
725fc411eaaSVladimir Oltean {
726fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
727fc411eaaSVladimir Oltean 
728fc411eaaSVladimir Oltean 	ocelot_port_policer_del(ocelot, port);
729fc411eaaSVladimir Oltean }
730fc411eaaSVladimir Oltean 
731de143c0eSXiaoliang Yang static int felix_port_setup_tc(struct dsa_switch *ds, int port,
732de143c0eSXiaoliang Yang 			       enum tc_setup_type type,
733de143c0eSXiaoliang Yang 			       void *type_data)
734de143c0eSXiaoliang Yang {
735de143c0eSXiaoliang Yang 	struct ocelot *ocelot = ds->priv;
736de143c0eSXiaoliang Yang 	struct felix *felix = ocelot_to_felix(ocelot);
737de143c0eSXiaoliang Yang 
738de143c0eSXiaoliang Yang 	if (felix->info->port_setup_tc)
739de143c0eSXiaoliang Yang 		return felix->info->port_setup_tc(ds, port, type, type_data);
740de143c0eSXiaoliang Yang 	else
741de143c0eSXiaoliang Yang 		return -EOPNOTSUPP;
742de143c0eSXiaoliang Yang }
743de143c0eSXiaoliang Yang 
744375e1314SVladimir Oltean const struct dsa_switch_ops felix_switch_ops = {
74556051948SVladimir Oltean 	.get_tag_protocol	= felix_get_tag_protocol,
74656051948SVladimir Oltean 	.setup			= felix_setup,
74756051948SVladimir Oltean 	.teardown		= felix_teardown,
74856051948SVladimir Oltean 	.set_ageing_time	= felix_set_ageing_time,
74956051948SVladimir Oltean 	.get_strings		= felix_get_strings,
75056051948SVladimir Oltean 	.get_ethtool_stats	= felix_get_ethtool_stats,
75156051948SVladimir Oltean 	.get_sset_count		= felix_get_sset_count,
75256051948SVladimir Oltean 	.get_ts_info		= felix_get_ts_info,
753bdeced75SVladimir Oltean 	.phylink_validate	= felix_phylink_validate,
754bdeced75SVladimir Oltean 	.phylink_mac_config	= felix_phylink_mac_config,
755bdeced75SVladimir Oltean 	.phylink_mac_link_down	= felix_phylink_mac_link_down,
756bdeced75SVladimir Oltean 	.phylink_mac_link_up	= felix_phylink_mac_link_up,
75756051948SVladimir Oltean 	.port_enable		= felix_port_enable,
75856051948SVladimir Oltean 	.port_disable		= felix_port_disable,
75956051948SVladimir Oltean 	.port_fdb_dump		= felix_fdb_dump,
76056051948SVladimir Oltean 	.port_fdb_add		= felix_fdb_add,
76156051948SVladimir Oltean 	.port_fdb_del		= felix_fdb_del,
762209edf95SVladimir Oltean 	.port_mdb_prepare	= felix_mdb_prepare,
763209edf95SVladimir Oltean 	.port_mdb_add		= felix_mdb_add,
764209edf95SVladimir Oltean 	.port_mdb_del		= felix_mdb_del,
76556051948SVladimir Oltean 	.port_bridge_join	= felix_bridge_join,
76656051948SVladimir Oltean 	.port_bridge_leave	= felix_bridge_leave,
76756051948SVladimir Oltean 	.port_stp_state_set	= felix_bridge_stp_state_set,
76856051948SVladimir Oltean 	.port_vlan_prepare	= felix_vlan_prepare,
76956051948SVladimir Oltean 	.port_vlan_filtering	= felix_vlan_filtering,
77056051948SVladimir Oltean 	.port_vlan_add		= felix_vlan_add,
77156051948SVladimir Oltean 	.port_vlan_del		= felix_vlan_del,
772c0bcf537SYangbo Lu 	.port_hwtstamp_get	= felix_hwtstamp_get,
773c0bcf537SYangbo Lu 	.port_hwtstamp_set	= felix_hwtstamp_set,
774c0bcf537SYangbo Lu 	.port_rxtstamp		= felix_rxtstamp,
775c0bcf537SYangbo Lu 	.port_txtstamp		= felix_txtstamp,
7760b912fc9SVladimir Oltean 	.port_change_mtu	= felix_change_mtu,
7770b912fc9SVladimir Oltean 	.port_max_mtu		= felix_get_max_mtu,
778fc411eaaSVladimir Oltean 	.port_policer_add	= felix_port_policer_add,
779fc411eaaSVladimir Oltean 	.port_policer_del	= felix_port_policer_del,
78007d985eeSVladimir Oltean 	.cls_flower_add		= felix_cls_flower_add,
78107d985eeSVladimir Oltean 	.cls_flower_del		= felix_cls_flower_del,
78207d985eeSVladimir Oltean 	.cls_flower_stats	= felix_cls_flower_stats,
783de143c0eSXiaoliang Yang 	.port_setup_tc		= felix_port_setup_tc,
78456051948SVladimir Oltean };
78556051948SVladimir Oltean 
786375e1314SVladimir Oltean static int __init felix_init(void)
787c0bcf537SYangbo Lu {
78884705fc1SMaxim Kochetkov 	int err;
78984705fc1SMaxim Kochetkov 
79084705fc1SMaxim Kochetkov 	err = pci_register_driver(&felix_vsc9959_pci_driver);
79184705fc1SMaxim Kochetkov 	if (err)
79284705fc1SMaxim Kochetkov 		return err;
79384705fc1SMaxim Kochetkov 
79484705fc1SMaxim Kochetkov 	err = platform_driver_register(&seville_vsc9953_driver);
79584705fc1SMaxim Kochetkov 	if (err)
79684705fc1SMaxim Kochetkov 		return err;
79784705fc1SMaxim Kochetkov 
79884705fc1SMaxim Kochetkov 	return 0;
799c0bcf537SYangbo Lu }
800375e1314SVladimir Oltean module_init(felix_init);
801c0bcf537SYangbo Lu 
802375e1314SVladimir Oltean static void __exit felix_exit(void)
80356051948SVladimir Oltean {
804375e1314SVladimir Oltean 	pci_unregister_driver(&felix_vsc9959_pci_driver);
80584705fc1SMaxim Kochetkov 	platform_driver_unregister(&seville_vsc9953_driver);
806e90c9fceSMichael Walle }
807375e1314SVladimir Oltean module_exit(felix_exit);
80856051948SVladimir Oltean 
80956051948SVladimir Oltean MODULE_DESCRIPTION("Felix Switch driver");
81056051948SVladimir Oltean MODULE_LICENSE("GPL v2");
811