xref: /openbmc/linux/drivers/net/dsa/ocelot/felix.c (revision 375e131429219486e581df8bd11b0dff87f0895a)
156051948SVladimir Oltean // SPDX-License-Identifier: GPL-2.0
256051948SVladimir Oltean /* Copyright 2019 NXP Semiconductors
3*375e1314SVladimir Oltean  *
4*375e1314SVladimir Oltean  * This is an umbrella module for all network switches that are
5*375e1314SVladimir Oltean  * register-compatible with Ocelot and that perform I/O to their host CPU
6*375e1314SVladimir 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>
16c0bcf537SYangbo Lu #include <linux/packing.h>
1756051948SVladimir Oltean #include <linux/module.h>
18bdeced75SVladimir Oltean #include <linux/of_net.h>
1956051948SVladimir Oltean #include <linux/pci.h>
2056051948SVladimir Oltean #include <linux/of.h>
21fc411eaaSVladimir Oltean #include <net/pkt_sched.h>
2256051948SVladimir Oltean #include <net/dsa.h>
2356051948SVladimir Oltean #include "felix.h"
2456051948SVladimir Oltean 
2556051948SVladimir Oltean static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds,
264d776482SFlorian Fainelli 						    int port,
274d776482SFlorian Fainelli 						    enum dsa_tag_protocol mp)
2856051948SVladimir Oltean {
2956051948SVladimir Oltean 	return DSA_TAG_PROTO_OCELOT;
3056051948SVladimir Oltean }
3156051948SVladimir Oltean 
3256051948SVladimir Oltean static int felix_set_ageing_time(struct dsa_switch *ds,
3356051948SVladimir Oltean 				 unsigned int ageing_time)
3456051948SVladimir Oltean {
3556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
3656051948SVladimir Oltean 
3756051948SVladimir Oltean 	ocelot_set_ageing_time(ocelot, ageing_time);
3856051948SVladimir Oltean 
3956051948SVladimir Oltean 	return 0;
4056051948SVladimir Oltean }
4156051948SVladimir Oltean 
4256051948SVladimir Oltean static int felix_fdb_dump(struct dsa_switch *ds, int port,
4356051948SVladimir Oltean 			  dsa_fdb_dump_cb_t *cb, void *data)
4456051948SVladimir Oltean {
4556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
4656051948SVladimir Oltean 
4756051948SVladimir Oltean 	return ocelot_fdb_dump(ocelot, port, cb, data);
4856051948SVladimir Oltean }
4956051948SVladimir Oltean 
5056051948SVladimir Oltean static int felix_fdb_add(struct dsa_switch *ds, int port,
5156051948SVladimir Oltean 			 const unsigned char *addr, u16 vid)
5256051948SVladimir Oltean {
5356051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
5456051948SVladimir Oltean 
5587b0f983SVladimir Oltean 	return ocelot_fdb_add(ocelot, port, addr, vid);
5656051948SVladimir Oltean }
5756051948SVladimir Oltean 
5856051948SVladimir Oltean static int felix_fdb_del(struct dsa_switch *ds, int port,
5956051948SVladimir Oltean 			 const unsigned char *addr, u16 vid)
6056051948SVladimir Oltean {
6156051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
6256051948SVladimir Oltean 
6356051948SVladimir Oltean 	return ocelot_fdb_del(ocelot, port, addr, vid);
6456051948SVladimir Oltean }
6556051948SVladimir Oltean 
66209edf95SVladimir Oltean /* This callback needs to be present */
67209edf95SVladimir Oltean static int felix_mdb_prepare(struct dsa_switch *ds, int port,
68209edf95SVladimir Oltean 			     const struct switchdev_obj_port_mdb *mdb)
69209edf95SVladimir Oltean {
70209edf95SVladimir Oltean 	return 0;
71209edf95SVladimir Oltean }
72209edf95SVladimir Oltean 
73209edf95SVladimir Oltean static void felix_mdb_add(struct dsa_switch *ds, int port,
74209edf95SVladimir Oltean 			  const struct switchdev_obj_port_mdb *mdb)
75209edf95SVladimir Oltean {
76209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
77209edf95SVladimir Oltean 
78209edf95SVladimir Oltean 	ocelot_port_mdb_add(ocelot, port, mdb);
79209edf95SVladimir Oltean }
80209edf95SVladimir Oltean 
81209edf95SVladimir Oltean static int felix_mdb_del(struct dsa_switch *ds, int port,
82209edf95SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb)
83209edf95SVladimir Oltean {
84209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
85209edf95SVladimir Oltean 
86209edf95SVladimir Oltean 	return ocelot_port_mdb_del(ocelot, port, mdb);
87209edf95SVladimir Oltean }
88209edf95SVladimir Oltean 
8956051948SVladimir Oltean static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port,
9056051948SVladimir Oltean 				       u8 state)
9156051948SVladimir Oltean {
9256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
9356051948SVladimir Oltean 
9456051948SVladimir Oltean 	return ocelot_bridge_stp_state_set(ocelot, port, state);
9556051948SVladimir Oltean }
9656051948SVladimir Oltean 
9756051948SVladimir Oltean static int felix_bridge_join(struct dsa_switch *ds, int port,
9856051948SVladimir Oltean 			     struct net_device *br)
9956051948SVladimir Oltean {
10056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
10156051948SVladimir Oltean 
10256051948SVladimir Oltean 	return ocelot_port_bridge_join(ocelot, port, br);
10356051948SVladimir Oltean }
10456051948SVladimir Oltean 
10556051948SVladimir Oltean static void felix_bridge_leave(struct dsa_switch *ds, int port,
10656051948SVladimir Oltean 			       struct net_device *br)
10756051948SVladimir Oltean {
10856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
10956051948SVladimir Oltean 
11056051948SVladimir Oltean 	ocelot_port_bridge_leave(ocelot, port, br);
11156051948SVladimir Oltean }
11256051948SVladimir Oltean 
11356051948SVladimir Oltean /* This callback needs to be present */
11456051948SVladimir Oltean static int felix_vlan_prepare(struct dsa_switch *ds, int port,
11556051948SVladimir Oltean 			      const struct switchdev_obj_port_vlan *vlan)
11656051948SVladimir Oltean {
11756051948SVladimir Oltean 	return 0;
11856051948SVladimir Oltean }
11956051948SVladimir Oltean 
12056051948SVladimir Oltean static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled)
12156051948SVladimir Oltean {
12256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
12356051948SVladimir Oltean 
12456051948SVladimir Oltean 	ocelot_port_vlan_filtering(ocelot, port, enabled);
12556051948SVladimir Oltean 
12656051948SVladimir Oltean 	return 0;
12756051948SVladimir Oltean }
12856051948SVladimir Oltean 
12956051948SVladimir Oltean static void felix_vlan_add(struct dsa_switch *ds, int port,
13056051948SVladimir Oltean 			   const struct switchdev_obj_port_vlan *vlan)
13156051948SVladimir Oltean {
13256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
133183be6f9SVladimir Oltean 	u16 flags = vlan->flags;
13456051948SVladimir Oltean 	u16 vid;
13556051948SVladimir Oltean 	int err;
13656051948SVladimir Oltean 
137183be6f9SVladimir Oltean 	if (dsa_is_cpu_port(ds, port))
138183be6f9SVladimir Oltean 		flags &= ~BRIDGE_VLAN_INFO_UNTAGGED;
139183be6f9SVladimir Oltean 
14056051948SVladimir Oltean 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
14156051948SVladimir Oltean 		err = ocelot_vlan_add(ocelot, port, vid,
142183be6f9SVladimir Oltean 				      flags & BRIDGE_VLAN_INFO_PVID,
143183be6f9SVladimir Oltean 				      flags & BRIDGE_VLAN_INFO_UNTAGGED);
14456051948SVladimir Oltean 		if (err) {
14556051948SVladimir Oltean 			dev_err(ds->dev, "Failed to add VLAN %d to port %d: %d\n",
14656051948SVladimir Oltean 				vid, port, err);
14756051948SVladimir Oltean 			return;
14856051948SVladimir Oltean 		}
14956051948SVladimir Oltean 	}
15056051948SVladimir Oltean }
15156051948SVladimir Oltean 
15256051948SVladimir Oltean static int felix_vlan_del(struct dsa_switch *ds, int port,
15356051948SVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan)
15456051948SVladimir Oltean {
15556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
15656051948SVladimir Oltean 	u16 vid;
15756051948SVladimir Oltean 	int err;
15856051948SVladimir Oltean 
15956051948SVladimir Oltean 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
16056051948SVladimir Oltean 		err = ocelot_vlan_del(ocelot, port, vid);
16156051948SVladimir Oltean 		if (err) {
16256051948SVladimir Oltean 			dev_err(ds->dev, "Failed to remove VLAN %d from port %d: %d\n",
16356051948SVladimir Oltean 				vid, port, err);
16456051948SVladimir Oltean 			return err;
16556051948SVladimir Oltean 		}
16656051948SVladimir Oltean 	}
16756051948SVladimir Oltean 	return 0;
16856051948SVladimir Oltean }
16956051948SVladimir Oltean 
17056051948SVladimir Oltean static int felix_port_enable(struct dsa_switch *ds, int port,
17156051948SVladimir Oltean 			     struct phy_device *phy)
17256051948SVladimir Oltean {
17356051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
17456051948SVladimir Oltean 
17556051948SVladimir Oltean 	ocelot_port_enable(ocelot, port, phy);
17656051948SVladimir Oltean 
17756051948SVladimir Oltean 	return 0;
17856051948SVladimir Oltean }
17956051948SVladimir Oltean 
18056051948SVladimir Oltean static void felix_port_disable(struct dsa_switch *ds, int port)
18156051948SVladimir Oltean {
18256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
18356051948SVladimir Oltean 
18456051948SVladimir Oltean 	return ocelot_port_disable(ocelot, port);
18556051948SVladimir Oltean }
18656051948SVladimir Oltean 
187bdeced75SVladimir Oltean static void felix_phylink_validate(struct dsa_switch *ds, int port,
188bdeced75SVladimir Oltean 				   unsigned long *supported,
189bdeced75SVladimir Oltean 				   struct phylink_link_state *state)
190bdeced75SVladimir Oltean {
191bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
192*375e1314SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
193bdeced75SVladimir Oltean 
194*375e1314SVladimir Oltean 	if (felix->info->phylink_validate)
195*375e1314SVladimir Oltean 		felix->info->phylink_validate(ocelot, port, supported, state);
196bdeced75SVladimir Oltean }
197bdeced75SVladimir Oltean 
198bdeced75SVladimir Oltean static int felix_phylink_mac_pcs_get_state(struct dsa_switch *ds, int port,
199bdeced75SVladimir Oltean 					   struct phylink_link_state *state)
200bdeced75SVladimir Oltean {
201bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
202bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
203bdeced75SVladimir Oltean 
204bdeced75SVladimir Oltean 	if (felix->info->pcs_link_state)
205bdeced75SVladimir Oltean 		felix->info->pcs_link_state(ocelot, port, state);
206bdeced75SVladimir Oltean 
207bdeced75SVladimir Oltean 	return 0;
208bdeced75SVladimir Oltean }
209bdeced75SVladimir Oltean 
210bdeced75SVladimir Oltean static void felix_phylink_mac_config(struct dsa_switch *ds, int port,
211bdeced75SVladimir Oltean 				     unsigned int link_an_mode,
212bdeced75SVladimir Oltean 				     const struct phylink_link_state *state)
213bdeced75SVladimir Oltean {
214bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
215bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
216bdeced75SVladimir Oltean 
2177e14a2dcSVladimir Oltean 	if (felix->info->pcs_config)
2187e14a2dcSVladimir Oltean 		felix->info->pcs_config(ocelot, port, link_an_mode, state);
219bdeced75SVladimir Oltean }
220bdeced75SVladimir Oltean 
221bdeced75SVladimir Oltean static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port,
222bdeced75SVladimir Oltean 					unsigned int link_an_mode,
223bdeced75SVladimir Oltean 					phy_interface_t interface)
224bdeced75SVladimir Oltean {
225bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
226bdeced75SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
227bdeced75SVladimir Oltean 
228bdeced75SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_ENA_CFG);
229886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0);
230bdeced75SVladimir Oltean }
231bdeced75SVladimir Oltean 
232bdeced75SVladimir Oltean static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port,
233bdeced75SVladimir Oltean 				      unsigned int link_an_mode,
234bdeced75SVladimir Oltean 				      phy_interface_t interface,
2355b502a7bSRussell King 				      struct phy_device *phydev,
2365b502a7bSRussell King 				      int speed, int duplex,
2375b502a7bSRussell King 				      bool tx_pause, bool rx_pause)
238bdeced75SVladimir Oltean {
239bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
240bdeced75SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2417e14a2dcSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
2427e14a2dcSVladimir Oltean 	u32 mac_fc_cfg;
243bdeced75SVladimir Oltean 
2447e14a2dcSVladimir Oltean 	/* Take port out of reset by clearing the MAC_TX_RST, MAC_RX_RST and
2457e14a2dcSVladimir Oltean 	 * PORT_RST bits in DEV_CLOCK_CFG. Note that the way this system is
2467e14a2dcSVladimir Oltean 	 * integrated is that the MAC speed is fixed and it's the PCS who is
2477e14a2dcSVladimir Oltean 	 * performing the rate adaptation, so we have to write "1000Mbps" into
2487e14a2dcSVladimir Oltean 	 * the LINK_SPEED field of DEV_CLOCK_CFG (which is also its default
2497e14a2dcSVladimir Oltean 	 * value).
2507e14a2dcSVladimir Oltean 	 */
2517e14a2dcSVladimir Oltean 	ocelot_port_writel(ocelot_port,
2527e14a2dcSVladimir Oltean 			   DEV_CLOCK_CFG_LINK_SPEED(OCELOT_SPEED_1000),
2537e14a2dcSVladimir Oltean 			   DEV_CLOCK_CFG);
2547e14a2dcSVladimir Oltean 
2557e14a2dcSVladimir Oltean 	switch (speed) {
2567e14a2dcSVladimir Oltean 	case SPEED_10:
2577e14a2dcSVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(3);
2587e14a2dcSVladimir Oltean 		break;
2597e14a2dcSVladimir Oltean 	case SPEED_100:
2607e14a2dcSVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(2);
2617e14a2dcSVladimir Oltean 		break;
2627e14a2dcSVladimir Oltean 	case SPEED_1000:
2637e14a2dcSVladimir Oltean 	case SPEED_2500:
2647e14a2dcSVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(1);
2657e14a2dcSVladimir Oltean 		break;
2667e14a2dcSVladimir Oltean 	default:
2677e14a2dcSVladimir Oltean 		dev_err(ocelot->dev, "Unsupported speed on port %d: %d\n",
2687e14a2dcSVladimir Oltean 			port, speed);
2697e14a2dcSVladimir Oltean 		return;
2707e14a2dcSVladimir Oltean 	}
2717e14a2dcSVladimir Oltean 
2727e14a2dcSVladimir Oltean 	/* handle Rx pause in all cases, with 2500base-X this is used for rate
2737e14a2dcSVladimir Oltean 	 * adaptation.
2747e14a2dcSVladimir Oltean 	 */
2757e14a2dcSVladimir Oltean 	mac_fc_cfg |= SYS_MAC_FC_CFG_RX_FC_ENA;
2767e14a2dcSVladimir Oltean 
2777e14a2dcSVladimir Oltean 	if (tx_pause)
2787e14a2dcSVladimir Oltean 		mac_fc_cfg |= SYS_MAC_FC_CFG_TX_FC_ENA |
2797e14a2dcSVladimir Oltean 			      SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
2807e14a2dcSVladimir Oltean 			      SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
2817e14a2dcSVladimir Oltean 			      SYS_MAC_FC_CFG_ZERO_PAUSE_ENA;
2827e14a2dcSVladimir Oltean 
2837e14a2dcSVladimir Oltean 	/* Flow control. Link speed is only used here to evaluate the time
2847e14a2dcSVladimir Oltean 	 * specification in incoming pause frames.
2857e14a2dcSVladimir Oltean 	 */
2867e14a2dcSVladimir Oltean 	ocelot_write_rix(ocelot, mac_fc_cfg, SYS_MAC_FC_CFG, port);
2877e14a2dcSVladimir Oltean 
2887e14a2dcSVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
2897e14a2dcSVladimir Oltean 
2907e14a2dcSVladimir Oltean 	/* Undo the effects of felix_phylink_mac_link_down:
2917e14a2dcSVladimir Oltean 	 * enable MAC module
2927e14a2dcSVladimir Oltean 	 */
293bdeced75SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
294bdeced75SVladimir Oltean 			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
295bdeced75SVladimir Oltean 
296bdeced75SVladimir Oltean 	/* Enable receiving frames on the port, and activate auto-learning of
297bdeced75SVladimir Oltean 	 * MAC addresses.
298bdeced75SVladimir Oltean 	 */
299bdeced75SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
300bdeced75SVladimir Oltean 			 ANA_PORT_PORT_CFG_RECV_ENA |
301bdeced75SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(port),
302bdeced75SVladimir Oltean 			 ANA_PORT_PORT_CFG, port);
303bdeced75SVladimir Oltean 
304bdeced75SVladimir Oltean 	/* Core: Enable port for frame transfer */
305886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, port,
306886e1387SVladimir Oltean 			    QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
3077e14a2dcSVladimir Oltean 
3087e14a2dcSVladimir Oltean 	if (felix->info->pcs_link_up)
3097e14a2dcSVladimir Oltean 		felix->info->pcs_link_up(ocelot, port, link_an_mode, interface,
3107e14a2dcSVladimir Oltean 					 speed, duplex);
3117e14a2dcSVladimir Oltean 
3127e14a2dcSVladimir Oltean 	if (felix->info->port_sched_speed_set)
3137e14a2dcSVladimir Oltean 		felix->info->port_sched_speed_set(ocelot, port, speed);
314bdeced75SVladimir Oltean }
315bdeced75SVladimir Oltean 
316bd2b3161SXiaoliang Yang static void felix_port_qos_map_init(struct ocelot *ocelot, int port)
317bd2b3161SXiaoliang Yang {
318bd2b3161SXiaoliang Yang 	int i;
319bd2b3161SXiaoliang Yang 
320bd2b3161SXiaoliang Yang 	ocelot_rmw_gix(ocelot,
321bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
322bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
323bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG,
324bd2b3161SXiaoliang Yang 		       port);
325bd2b3161SXiaoliang Yang 
326bd2b3161SXiaoliang Yang 	for (i = 0; i < FELIX_NUM_TC * 2; i++) {
327bd2b3161SXiaoliang Yang 		ocelot_rmw_ix(ocelot,
328bd2b3161SXiaoliang Yang 			      (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) |
329bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i),
330bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL |
331bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M,
332bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP,
333bd2b3161SXiaoliang Yang 			      port, i);
334bd2b3161SXiaoliang Yang 	}
335bd2b3161SXiaoliang Yang }
336bd2b3161SXiaoliang Yang 
33756051948SVladimir Oltean static void felix_get_strings(struct dsa_switch *ds, int port,
33856051948SVladimir Oltean 			      u32 stringset, u8 *data)
33956051948SVladimir Oltean {
34056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
34156051948SVladimir Oltean 
34256051948SVladimir Oltean 	return ocelot_get_strings(ocelot, port, stringset, data);
34356051948SVladimir Oltean }
34456051948SVladimir Oltean 
34556051948SVladimir Oltean static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data)
34656051948SVladimir Oltean {
34756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
34856051948SVladimir Oltean 
34956051948SVladimir Oltean 	ocelot_get_ethtool_stats(ocelot, port, data);
35056051948SVladimir Oltean }
35156051948SVladimir Oltean 
35256051948SVladimir Oltean static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset)
35356051948SVladimir Oltean {
35456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
35556051948SVladimir Oltean 
35656051948SVladimir Oltean 	return ocelot_get_sset_count(ocelot, port, sset);
35756051948SVladimir Oltean }
35856051948SVladimir Oltean 
35956051948SVladimir Oltean static int felix_get_ts_info(struct dsa_switch *ds, int port,
36056051948SVladimir Oltean 			     struct ethtool_ts_info *info)
36156051948SVladimir Oltean {
36256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
36356051948SVladimir Oltean 
36456051948SVladimir Oltean 	return ocelot_get_ts_info(ocelot, port, info);
36556051948SVladimir Oltean }
36656051948SVladimir Oltean 
367bdeced75SVladimir Oltean static int felix_parse_ports_node(struct felix *felix,
368bdeced75SVladimir Oltean 				  struct device_node *ports_node,
369bdeced75SVladimir Oltean 				  phy_interface_t *port_phy_modes)
370bdeced75SVladimir Oltean {
371bdeced75SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
372bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
373bdeced75SVladimir Oltean 	struct device_node *child;
374bdeced75SVladimir Oltean 
37537fe45adSVladimir Oltean 	for_each_available_child_of_node(ports_node, child) {
376bdeced75SVladimir Oltean 		phy_interface_t phy_mode;
377bdeced75SVladimir Oltean 		u32 port;
378bdeced75SVladimir Oltean 		int err;
379bdeced75SVladimir Oltean 
380bdeced75SVladimir Oltean 		/* Get switch port number from DT */
381bdeced75SVladimir Oltean 		if (of_property_read_u32(child, "reg", &port) < 0) {
382bdeced75SVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
383bdeced75SVladimir Oltean 				"(property \"reg\")\n");
384bdeced75SVladimir Oltean 			of_node_put(child);
385bdeced75SVladimir Oltean 			return -ENODEV;
386bdeced75SVladimir Oltean 		}
387bdeced75SVladimir Oltean 
388bdeced75SVladimir Oltean 		/* Get PHY mode from DT */
389bdeced75SVladimir Oltean 		err = of_get_phy_mode(child, &phy_mode);
390bdeced75SVladimir Oltean 		if (err) {
391bdeced75SVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
392bdeced75SVladimir Oltean 				"phy-interface-type property for port %d\n",
393bdeced75SVladimir Oltean 				port);
394bdeced75SVladimir Oltean 			of_node_put(child);
395bdeced75SVladimir Oltean 			return -ENODEV;
396bdeced75SVladimir Oltean 		}
397bdeced75SVladimir Oltean 
398bdeced75SVladimir Oltean 		err = felix->info->prevalidate_phy_mode(ocelot, port, phy_mode);
399bdeced75SVladimir Oltean 		if (err < 0) {
400bdeced75SVladimir Oltean 			dev_err(dev, "Unsupported PHY mode %s on port %d\n",
401bdeced75SVladimir Oltean 				phy_modes(phy_mode), port);
402bdeced75SVladimir Oltean 			return err;
403bdeced75SVladimir Oltean 		}
404bdeced75SVladimir Oltean 
405bdeced75SVladimir Oltean 		port_phy_modes[port] = phy_mode;
406bdeced75SVladimir Oltean 	}
407bdeced75SVladimir Oltean 
408bdeced75SVladimir Oltean 	return 0;
409bdeced75SVladimir Oltean }
410bdeced75SVladimir Oltean 
411bdeced75SVladimir Oltean static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes)
412bdeced75SVladimir Oltean {
413bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
414bdeced75SVladimir Oltean 	struct device_node *switch_node;
415bdeced75SVladimir Oltean 	struct device_node *ports_node;
416bdeced75SVladimir Oltean 	int err;
417bdeced75SVladimir Oltean 
418bdeced75SVladimir Oltean 	switch_node = dev->of_node;
419bdeced75SVladimir Oltean 
420bdeced75SVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
421bdeced75SVladimir Oltean 	if (!ports_node) {
422bdeced75SVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" node\n");
423bdeced75SVladimir Oltean 		return -ENODEV;
424bdeced75SVladimir Oltean 	}
425bdeced75SVladimir Oltean 
426bdeced75SVladimir Oltean 	err = felix_parse_ports_node(felix, ports_node, port_phy_modes);
427bdeced75SVladimir Oltean 	of_node_put(ports_node);
428bdeced75SVladimir Oltean 
429bdeced75SVladimir Oltean 	return err;
430bdeced75SVladimir Oltean }
431bdeced75SVladimir Oltean 
43256051948SVladimir Oltean static int felix_init_structs(struct felix *felix, int num_phys_ports)
43356051948SVladimir Oltean {
43456051948SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
435bdeced75SVladimir Oltean 	phy_interface_t *port_phy_modes;
436b4024c9eSClaudiu Manoil 	struct resource res;
43756051948SVladimir Oltean 	int port, i, err;
43856051948SVladimir Oltean 
43956051948SVladimir Oltean 	ocelot->num_phys_ports = num_phys_ports;
44056051948SVladimir Oltean 	ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports,
44156051948SVladimir Oltean 				     sizeof(struct ocelot_port *), GFP_KERNEL);
44256051948SVladimir Oltean 	if (!ocelot->ports)
44356051948SVladimir Oltean 		return -ENOMEM;
44456051948SVladimir Oltean 
44556051948SVladimir Oltean 	ocelot->map		= felix->info->map;
44656051948SVladimir Oltean 	ocelot->stats_layout	= felix->info->stats_layout;
44756051948SVladimir Oltean 	ocelot->num_stats	= felix->info->num_stats;
44856051948SVladimir Oltean 	ocelot->shared_queue_sz	= felix->info->shared_queue_sz;
44921ce7f3eSVladimir Oltean 	ocelot->num_mact_rows	= felix->info->num_mact_rows;
45007d985eeSVladimir Oltean 	ocelot->vcap_is2_keys	= felix->info->vcap_is2_keys;
45107d985eeSVladimir Oltean 	ocelot->vcap_is2_actions= felix->info->vcap_is2_actions;
45207d985eeSVladimir Oltean 	ocelot->vcap		= felix->info->vcap;
45356051948SVladimir Oltean 	ocelot->ops		= felix->info->ops;
45456051948SVladimir Oltean 
455bdeced75SVladimir Oltean 	port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t),
456bdeced75SVladimir Oltean 				 GFP_KERNEL);
457bdeced75SVladimir Oltean 	if (!port_phy_modes)
458bdeced75SVladimir Oltean 		return -ENOMEM;
459bdeced75SVladimir Oltean 
460bdeced75SVladimir Oltean 	err = felix_parse_dt(felix, port_phy_modes);
461bdeced75SVladimir Oltean 	if (err) {
462bdeced75SVladimir Oltean 		kfree(port_phy_modes);
463bdeced75SVladimir Oltean 		return err;
464bdeced75SVladimir Oltean 	}
465bdeced75SVladimir Oltean 
46656051948SVladimir Oltean 	for (i = 0; i < TARGET_MAX; i++) {
46756051948SVladimir Oltean 		struct regmap *target;
46856051948SVladimir Oltean 
46956051948SVladimir Oltean 		if (!felix->info->target_io_res[i].name)
47056051948SVladimir Oltean 			continue;
47156051948SVladimir Oltean 
472b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->target_io_res[i], sizeof(res));
473b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
474*375e1314SVladimir Oltean 		res.start += felix->switch_base;
475*375e1314SVladimir Oltean 		res.end += felix->switch_base;
47656051948SVladimir Oltean 
477b4024c9eSClaudiu Manoil 		target = ocelot_regmap_init(ocelot, &res);
47856051948SVladimir Oltean 		if (IS_ERR(target)) {
47956051948SVladimir Oltean 			dev_err(ocelot->dev,
48056051948SVladimir Oltean 				"Failed to map device memory space\n");
481bdeced75SVladimir Oltean 			kfree(port_phy_modes);
48256051948SVladimir Oltean 			return PTR_ERR(target);
48356051948SVladimir Oltean 		}
48456051948SVladimir Oltean 
48556051948SVladimir Oltean 		ocelot->targets[i] = target;
48656051948SVladimir Oltean 	}
48756051948SVladimir Oltean 
48856051948SVladimir Oltean 	err = ocelot_regfields_init(ocelot, felix->info->regfields);
48956051948SVladimir Oltean 	if (err) {
49056051948SVladimir Oltean 		dev_err(ocelot->dev, "failed to init reg fields map\n");
491bdeced75SVladimir Oltean 		kfree(port_phy_modes);
49256051948SVladimir Oltean 		return err;
49356051948SVladimir Oltean 	}
49456051948SVladimir Oltean 
49556051948SVladimir Oltean 	for (port = 0; port < num_phys_ports; port++) {
49656051948SVladimir Oltean 		struct ocelot_port *ocelot_port;
49791c724cfSVladimir Oltean 		struct regmap *target;
49867c24049SVladimir Oltean 		u8 *template;
49956051948SVladimir Oltean 
50056051948SVladimir Oltean 		ocelot_port = devm_kzalloc(ocelot->dev,
50156051948SVladimir Oltean 					   sizeof(struct ocelot_port),
50256051948SVladimir Oltean 					   GFP_KERNEL);
50356051948SVladimir Oltean 		if (!ocelot_port) {
50456051948SVladimir Oltean 			dev_err(ocelot->dev,
50556051948SVladimir Oltean 				"failed to allocate port memory\n");
506bdeced75SVladimir Oltean 			kfree(port_phy_modes);
50756051948SVladimir Oltean 			return -ENOMEM;
50856051948SVladimir Oltean 		}
50956051948SVladimir Oltean 
510b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->port_io_res[port], sizeof(res));
511b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
512*375e1314SVladimir Oltean 		res.start += felix->switch_base;
513*375e1314SVladimir Oltean 		res.end += felix->switch_base;
51456051948SVladimir Oltean 
51591c724cfSVladimir Oltean 		target = ocelot_regmap_init(ocelot, &res);
51691c724cfSVladimir Oltean 		if (IS_ERR(target)) {
51756051948SVladimir Oltean 			dev_err(ocelot->dev,
51891c724cfSVladimir Oltean 				"Failed to map memory space for port %d\n",
51991c724cfSVladimir Oltean 				port);
520bdeced75SVladimir Oltean 			kfree(port_phy_modes);
52191c724cfSVladimir Oltean 			return PTR_ERR(target);
52256051948SVladimir Oltean 		}
52356051948SVladimir Oltean 
52467c24049SVladimir Oltean 		template = devm_kzalloc(ocelot->dev, OCELOT_TAG_LEN,
52567c24049SVladimir Oltean 					GFP_KERNEL);
52667c24049SVladimir Oltean 		if (!template) {
52767c24049SVladimir Oltean 			dev_err(ocelot->dev,
52867c24049SVladimir Oltean 				"Failed to allocate memory for DSA tag\n");
52967c24049SVladimir Oltean 			kfree(port_phy_modes);
53067c24049SVladimir Oltean 			return -ENOMEM;
53167c24049SVladimir Oltean 		}
53267c24049SVladimir Oltean 
533bdeced75SVladimir Oltean 		ocelot_port->phy_mode = port_phy_modes[port];
53456051948SVladimir Oltean 		ocelot_port->ocelot = ocelot;
53591c724cfSVladimir Oltean 		ocelot_port->target = target;
53667c24049SVladimir Oltean 		ocelot_port->xmit_template = template;
53756051948SVladimir Oltean 		ocelot->ports[port] = ocelot_port;
53867c24049SVladimir Oltean 
53967c24049SVladimir Oltean 		felix->info->xmit_template_populate(ocelot, port);
54056051948SVladimir Oltean 	}
54156051948SVladimir Oltean 
542bdeced75SVladimir Oltean 	kfree(port_phy_modes);
543bdeced75SVladimir Oltean 
544bdeced75SVladimir Oltean 	if (felix->info->mdio_bus_alloc) {
545bdeced75SVladimir Oltean 		err = felix->info->mdio_bus_alloc(ocelot);
546bdeced75SVladimir Oltean 		if (err < 0)
547bdeced75SVladimir Oltean 			return err;
548bdeced75SVladimir Oltean 	}
549bdeced75SVladimir Oltean 
55056051948SVladimir Oltean 	return 0;
55156051948SVladimir Oltean }
55256051948SVladimir Oltean 
5532b49d128SYangbo Lu static struct ptp_clock_info ocelot_ptp_clock_info = {
5542b49d128SYangbo Lu 	.owner		= THIS_MODULE,
5552b49d128SYangbo Lu 	.name		= "felix ptp",
5562b49d128SYangbo Lu 	.max_adj	= 0x7fffffff,
5572b49d128SYangbo Lu 	.n_alarm	= 0,
5582b49d128SYangbo Lu 	.n_ext_ts	= 0,
5595287be40SYangbo Lu 	.n_per_out	= OCELOT_PTP_PINS_NUM,
5605287be40SYangbo Lu 	.n_pins		= OCELOT_PTP_PINS_NUM,
5612b49d128SYangbo Lu 	.pps		= 0,
5622b49d128SYangbo Lu 	.gettime64	= ocelot_ptp_gettime64,
5632b49d128SYangbo Lu 	.settime64	= ocelot_ptp_settime64,
5642b49d128SYangbo Lu 	.adjtime	= ocelot_ptp_adjtime,
5652b49d128SYangbo Lu 	.adjfine	= ocelot_ptp_adjfine,
5665287be40SYangbo Lu 	.verify		= ocelot_ptp_verify,
5675287be40SYangbo Lu 	.enable		= ocelot_ptp_enable,
5682b49d128SYangbo Lu };
5692b49d128SYangbo Lu 
57056051948SVladimir Oltean /* Hardware initialization done here so that we can allocate structures with
57156051948SVladimir Oltean  * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing
57256051948SVladimir Oltean  * us to allocate structures twice (leak memory) and map PCI memory twice
57356051948SVladimir Oltean  * (which will not work).
57456051948SVladimir Oltean  */
57556051948SVladimir Oltean static int felix_setup(struct dsa_switch *ds)
57656051948SVladimir Oltean {
57756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
57856051948SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
57956051948SVladimir Oltean 	int port, err;
5803c7b51bdSXiaoliang Yang 	int tc;
58156051948SVladimir Oltean 
58256051948SVladimir Oltean 	err = felix_init_structs(felix, ds->num_ports);
58356051948SVladimir Oltean 	if (err)
58456051948SVladimir Oltean 		return err;
58556051948SVladimir Oltean 
58656051948SVladimir Oltean 	ocelot_init(ocelot);
5872b49d128SYangbo Lu 	if (ocelot->ptp) {
5882b49d128SYangbo Lu 		err = ocelot_init_timestamp(ocelot, &ocelot_ptp_clock_info);
5892b49d128SYangbo Lu 		if (err) {
5902b49d128SYangbo Lu 			dev_err(ocelot->dev,
5912b49d128SYangbo Lu 				"Timestamp initialization failed\n");
5922b49d128SYangbo Lu 			ocelot->ptp = 0;
5932b49d128SYangbo Lu 		}
5942b49d128SYangbo Lu 	}
59556051948SVladimir Oltean 
59656051948SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
59756051948SVladimir Oltean 		ocelot_init_port(ocelot, port);
59856051948SVladimir Oltean 
59969df578cSVladimir Oltean 		/* Bring up the CPU port module and configure the NPI port */
600b8fc7177SVladimir Oltean 		if (dsa_is_cpu_port(ds, port))
60169df578cSVladimir Oltean 			ocelot_configure_cpu(ocelot, port,
60256051948SVladimir Oltean 					     OCELOT_TAG_PREFIX_NONE,
60356051948SVladimir Oltean 					     OCELOT_TAG_PREFIX_LONG);
604bd2b3161SXiaoliang Yang 
605bd2b3161SXiaoliang Yang 		/* Set the default QoS Classification based on PCP and DEI
606bd2b3161SXiaoliang Yang 		 * bits of vlan tag.
607bd2b3161SXiaoliang Yang 		 */
608bd2b3161SXiaoliang Yang 		felix_port_qos_map_init(ocelot, port);
60956051948SVladimir Oltean 	}
61056051948SVladimir Oltean 
6111cf3299bSVladimir Oltean 	/* Include the CPU port module in the forwarding mask for unknown
6121cf3299bSVladimir Oltean 	 * unicast - the hardware default value for ANA_FLOODING_FLD_UNICAST
6131cf3299bSVladimir Oltean 	 * excludes BIT(ocelot->num_phys_ports), and so does ocelot_init, since
6141cf3299bSVladimir Oltean 	 * Ocelot relies on whitelisting MAC addresses towards PGID_CPU.
6151cf3299bSVladimir Oltean 	 */
6161cf3299bSVladimir Oltean 	ocelot_write_rix(ocelot,
6171cf3299bSVladimir Oltean 			 ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)),
6181cf3299bSVladimir Oltean 			 ANA_PGID_PGID, PGID_UC);
6193c7b51bdSXiaoliang Yang 	/* Setup the per-traffic class flooding PGIDs */
6203c7b51bdSXiaoliang Yang 	for (tc = 0; tc < FELIX_NUM_TC; tc++)
6213c7b51bdSXiaoliang Yang 		ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
6223c7b51bdSXiaoliang Yang 				 ANA_FLOODING_FLD_BROADCAST(PGID_MC) |
6233c7b51bdSXiaoliang Yang 				 ANA_FLOODING_FLD_UNICAST(PGID_UC),
6243c7b51bdSXiaoliang Yang 				 ANA_FLOODING, tc);
6251cf3299bSVladimir Oltean 
6260b912fc9SVladimir Oltean 	ds->mtu_enforcement_ingress = true;
627626a8323SVladimir Oltean 	ds->configure_vlan_while_not_filtering = true;
628bdeced75SVladimir Oltean 	/* It looks like the MAC/PCS interrupt register - PM0_IEVENT (0x8040)
629bdeced75SVladimir Oltean 	 * isn't instantiated for the Felix PF.
630bdeced75SVladimir Oltean 	 * In-band AN may take a few ms to complete, so we need to poll.
631bdeced75SVladimir Oltean 	 */
632bdeced75SVladimir Oltean 	ds->pcs_poll = 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);
641bdeced75SVladimir Oltean 
642bdeced75SVladimir Oltean 	if (felix->info->mdio_bus_free)
643bdeced75SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
64456051948SVladimir Oltean 
6452b49d128SYangbo Lu 	ocelot_deinit_timestamp(ocelot);
64656051948SVladimir Oltean 	/* stop workqueue thread */
64756051948SVladimir Oltean 	ocelot_deinit(ocelot);
64856051948SVladimir Oltean }
64956051948SVladimir Oltean 
650c0bcf537SYangbo Lu static int felix_hwtstamp_get(struct dsa_switch *ds, int port,
651c0bcf537SYangbo Lu 			      struct ifreq *ifr)
652c0bcf537SYangbo Lu {
653c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
654c0bcf537SYangbo Lu 
655c0bcf537SYangbo Lu 	return ocelot_hwstamp_get(ocelot, port, ifr);
656c0bcf537SYangbo Lu }
657c0bcf537SYangbo Lu 
658c0bcf537SYangbo Lu static int felix_hwtstamp_set(struct dsa_switch *ds, int port,
659c0bcf537SYangbo Lu 			      struct ifreq *ifr)
660c0bcf537SYangbo Lu {
661c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
662c0bcf537SYangbo Lu 
663c0bcf537SYangbo Lu 	return ocelot_hwstamp_set(ocelot, port, ifr);
664c0bcf537SYangbo Lu }
665c0bcf537SYangbo Lu 
666c0bcf537SYangbo Lu static bool felix_rxtstamp(struct dsa_switch *ds, int port,
667c0bcf537SYangbo Lu 			   struct sk_buff *skb, unsigned int type)
668c0bcf537SYangbo Lu {
669c0bcf537SYangbo Lu 	struct skb_shared_hwtstamps *shhwtstamps;
670c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
671c0bcf537SYangbo Lu 	u8 *extraction = skb->data - ETH_HLEN - OCELOT_TAG_LEN;
672c0bcf537SYangbo Lu 	u32 tstamp_lo, tstamp_hi;
673c0bcf537SYangbo Lu 	struct timespec64 ts;
674c0bcf537SYangbo Lu 	u64 tstamp, val;
675c0bcf537SYangbo Lu 
676c0bcf537SYangbo Lu 	ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
677c0bcf537SYangbo Lu 	tstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
678c0bcf537SYangbo Lu 
679c0bcf537SYangbo Lu 	packing(extraction, &val,  116, 85, OCELOT_TAG_LEN, UNPACK, 0);
680c0bcf537SYangbo Lu 	tstamp_lo = (u32)val;
681c0bcf537SYangbo Lu 
682c0bcf537SYangbo Lu 	tstamp_hi = tstamp >> 32;
683c0bcf537SYangbo Lu 	if ((tstamp & 0xffffffff) < tstamp_lo)
684c0bcf537SYangbo Lu 		tstamp_hi--;
685c0bcf537SYangbo Lu 
686c0bcf537SYangbo Lu 	tstamp = ((u64)tstamp_hi << 32) | tstamp_lo;
687c0bcf537SYangbo Lu 
688c0bcf537SYangbo Lu 	shhwtstamps = skb_hwtstamps(skb);
689c0bcf537SYangbo Lu 	memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
690c0bcf537SYangbo Lu 	shhwtstamps->hwtstamp = tstamp;
691c0bcf537SYangbo Lu 	return false;
692c0bcf537SYangbo Lu }
693c0bcf537SYangbo Lu 
6943243e04aSChen Wandun static bool felix_txtstamp(struct dsa_switch *ds, int port,
695c0bcf537SYangbo Lu 			   struct sk_buff *clone, unsigned int type)
696c0bcf537SYangbo Lu {
697c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
698c0bcf537SYangbo Lu 	struct ocelot_port *ocelot_port = ocelot->ports[port];
699c0bcf537SYangbo Lu 
700c0bcf537SYangbo Lu 	if (!ocelot_port_add_txtstamp_skb(ocelot_port, clone))
701c0bcf537SYangbo Lu 		return true;
702c0bcf537SYangbo Lu 
703c0bcf537SYangbo Lu 	return false;
704c0bcf537SYangbo Lu }
705c0bcf537SYangbo Lu 
7060b912fc9SVladimir Oltean static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
7070b912fc9SVladimir Oltean {
7080b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
7090b912fc9SVladimir Oltean 
7100b912fc9SVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, new_mtu);
7110b912fc9SVladimir Oltean 
7120b912fc9SVladimir Oltean 	return 0;
7130b912fc9SVladimir Oltean }
7140b912fc9SVladimir Oltean 
7150b912fc9SVladimir Oltean static int felix_get_max_mtu(struct dsa_switch *ds, int port)
7160b912fc9SVladimir Oltean {
7170b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
7180b912fc9SVladimir Oltean 
7190b912fc9SVladimir Oltean 	return ocelot_get_max_mtu(ocelot, port);
7200b912fc9SVladimir Oltean }
7210b912fc9SVladimir Oltean 
72207d985eeSVladimir Oltean static int felix_cls_flower_add(struct dsa_switch *ds, int port,
72307d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
72407d985eeSVladimir Oltean {
72507d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
72607d985eeSVladimir Oltean 
72707d985eeSVladimir Oltean 	return ocelot_cls_flower_replace(ocelot, port, cls, ingress);
72807d985eeSVladimir Oltean }
72907d985eeSVladimir Oltean 
73007d985eeSVladimir Oltean static int felix_cls_flower_del(struct dsa_switch *ds, int port,
73107d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
73207d985eeSVladimir Oltean {
73307d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
73407d985eeSVladimir Oltean 
73507d985eeSVladimir Oltean 	return ocelot_cls_flower_destroy(ocelot, port, cls, ingress);
73607d985eeSVladimir Oltean }
73707d985eeSVladimir Oltean 
73807d985eeSVladimir Oltean static int felix_cls_flower_stats(struct dsa_switch *ds, int port,
73907d985eeSVladimir Oltean 				  struct flow_cls_offload *cls, bool ingress)
74007d985eeSVladimir Oltean {
74107d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
74207d985eeSVladimir Oltean 
74307d985eeSVladimir Oltean 	return ocelot_cls_flower_stats(ocelot, port, cls, ingress);
74407d985eeSVladimir Oltean }
74507d985eeSVladimir Oltean 
746fc411eaaSVladimir Oltean static int felix_port_policer_add(struct dsa_switch *ds, int port,
747fc411eaaSVladimir Oltean 				  struct dsa_mall_policer_tc_entry *policer)
748fc411eaaSVladimir Oltean {
749fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
750fc411eaaSVladimir Oltean 	struct ocelot_policer pol = {
751fc411eaaSVladimir Oltean 		.rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8,
7525f035af7SPo Liu 		.burst = policer->burst,
753fc411eaaSVladimir Oltean 	};
754fc411eaaSVladimir Oltean 
755fc411eaaSVladimir Oltean 	return ocelot_port_policer_add(ocelot, port, &pol);
756fc411eaaSVladimir Oltean }
757fc411eaaSVladimir Oltean 
758fc411eaaSVladimir Oltean static void felix_port_policer_del(struct dsa_switch *ds, int port)
759fc411eaaSVladimir Oltean {
760fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
761fc411eaaSVladimir Oltean 
762fc411eaaSVladimir Oltean 	ocelot_port_policer_del(ocelot, port);
763fc411eaaSVladimir Oltean }
764fc411eaaSVladimir Oltean 
765de143c0eSXiaoliang Yang static int felix_port_setup_tc(struct dsa_switch *ds, int port,
766de143c0eSXiaoliang Yang 			       enum tc_setup_type type,
767de143c0eSXiaoliang Yang 			       void *type_data)
768de143c0eSXiaoliang Yang {
769de143c0eSXiaoliang Yang 	struct ocelot *ocelot = ds->priv;
770de143c0eSXiaoliang Yang 	struct felix *felix = ocelot_to_felix(ocelot);
771de143c0eSXiaoliang Yang 
772de143c0eSXiaoliang Yang 	if (felix->info->port_setup_tc)
773de143c0eSXiaoliang Yang 		return felix->info->port_setup_tc(ds, port, type, type_data);
774de143c0eSXiaoliang Yang 	else
775de143c0eSXiaoliang Yang 		return -EOPNOTSUPP;
776de143c0eSXiaoliang Yang }
777de143c0eSXiaoliang Yang 
778*375e1314SVladimir Oltean const struct dsa_switch_ops felix_switch_ops = {
77956051948SVladimir Oltean 	.get_tag_protocol	= felix_get_tag_protocol,
78056051948SVladimir Oltean 	.setup			= felix_setup,
78156051948SVladimir Oltean 	.teardown		= felix_teardown,
78256051948SVladimir Oltean 	.set_ageing_time	= felix_set_ageing_time,
78356051948SVladimir Oltean 	.get_strings		= felix_get_strings,
78456051948SVladimir Oltean 	.get_ethtool_stats	= felix_get_ethtool_stats,
78556051948SVladimir Oltean 	.get_sset_count		= felix_get_sset_count,
78656051948SVladimir Oltean 	.get_ts_info		= felix_get_ts_info,
787bdeced75SVladimir Oltean 	.phylink_validate	= felix_phylink_validate,
788bdeced75SVladimir Oltean 	.phylink_mac_link_state	= felix_phylink_mac_pcs_get_state,
789bdeced75SVladimir Oltean 	.phylink_mac_config	= felix_phylink_mac_config,
790bdeced75SVladimir Oltean 	.phylink_mac_link_down	= felix_phylink_mac_link_down,
791bdeced75SVladimir Oltean 	.phylink_mac_link_up	= felix_phylink_mac_link_up,
79256051948SVladimir Oltean 	.port_enable		= felix_port_enable,
79356051948SVladimir Oltean 	.port_disable		= felix_port_disable,
79456051948SVladimir Oltean 	.port_fdb_dump		= felix_fdb_dump,
79556051948SVladimir Oltean 	.port_fdb_add		= felix_fdb_add,
79656051948SVladimir Oltean 	.port_fdb_del		= felix_fdb_del,
797209edf95SVladimir Oltean 	.port_mdb_prepare	= felix_mdb_prepare,
798209edf95SVladimir Oltean 	.port_mdb_add		= felix_mdb_add,
799209edf95SVladimir Oltean 	.port_mdb_del		= felix_mdb_del,
80056051948SVladimir Oltean 	.port_bridge_join	= felix_bridge_join,
80156051948SVladimir Oltean 	.port_bridge_leave	= felix_bridge_leave,
80256051948SVladimir Oltean 	.port_stp_state_set	= felix_bridge_stp_state_set,
80356051948SVladimir Oltean 	.port_vlan_prepare	= felix_vlan_prepare,
80456051948SVladimir Oltean 	.port_vlan_filtering	= felix_vlan_filtering,
80556051948SVladimir Oltean 	.port_vlan_add		= felix_vlan_add,
80656051948SVladimir Oltean 	.port_vlan_del		= felix_vlan_del,
807c0bcf537SYangbo Lu 	.port_hwtstamp_get	= felix_hwtstamp_get,
808c0bcf537SYangbo Lu 	.port_hwtstamp_set	= felix_hwtstamp_set,
809c0bcf537SYangbo Lu 	.port_rxtstamp		= felix_rxtstamp,
810c0bcf537SYangbo Lu 	.port_txtstamp		= felix_txtstamp,
8110b912fc9SVladimir Oltean 	.port_change_mtu	= felix_change_mtu,
8120b912fc9SVladimir Oltean 	.port_max_mtu		= felix_get_max_mtu,
813fc411eaaSVladimir Oltean 	.port_policer_add	= felix_port_policer_add,
814fc411eaaSVladimir Oltean 	.port_policer_del	= felix_port_policer_del,
81507d985eeSVladimir Oltean 	.cls_flower_add		= felix_cls_flower_add,
81607d985eeSVladimir Oltean 	.cls_flower_del		= felix_cls_flower_del,
81707d985eeSVladimir Oltean 	.cls_flower_stats	= felix_cls_flower_stats,
818de143c0eSXiaoliang Yang 	.port_setup_tc          = felix_port_setup_tc,
81956051948SVladimir Oltean };
82056051948SVladimir Oltean 
821*375e1314SVladimir Oltean static int __init felix_init(void)
822c0bcf537SYangbo Lu {
823*375e1314SVladimir Oltean 	return pci_register_driver(&felix_vsc9959_pci_driver);
824c0bcf537SYangbo Lu }
825*375e1314SVladimir Oltean module_init(felix_init);
826c0bcf537SYangbo Lu 
827*375e1314SVladimir Oltean static void __exit felix_exit(void)
82856051948SVladimir Oltean {
829*375e1314SVladimir Oltean 	pci_unregister_driver(&felix_vsc9959_pci_driver);
830e90c9fceSMichael Walle }
831*375e1314SVladimir Oltean module_exit(felix_exit);
83256051948SVladimir Oltean 
83356051948SVladimir Oltean MODULE_DESCRIPTION("Felix Switch driver");
83456051948SVladimir Oltean MODULE_LICENSE("GPL v2");
835