xref: /openbmc/linux/drivers/net/ethernet/mscc/ocelot.c (revision ebb4c6a990f786d7e0e4618a0d3766cd660125d8)
1a556c76aSAlexandre Belloni // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2a556c76aSAlexandre Belloni /*
3a556c76aSAlexandre Belloni  * Microsemi Ocelot Switch driver
4a556c76aSAlexandre Belloni  *
5a556c76aSAlexandre Belloni  * Copyright (c) 2017 Microsemi Corporation
6a556c76aSAlexandre Belloni  */
740d3f295SVladimir Oltean #include <linux/dsa/ocelot.h>
8a556c76aSAlexandre Belloni #include <linux/if_bridge.h>
939e5308bSYangbo Lu #include <linux/ptp_classify.h>
1020968054SVladimir Oltean #include <soc/mscc/ocelot_vcap.h>
11a556c76aSAlexandre Belloni #include "ocelot.h"
123c83654fSVladimir Oltean #include "ocelot_vcap.h"
13a556c76aSAlexandre Belloni 
14639c1b26SSteen Hegelund #define TABLE_UPDATE_SLEEP_US 10
15639c1b26SSteen Hegelund #define TABLE_UPDATE_TIMEOUT_US 100000
16639c1b26SSteen Hegelund 
17a556c76aSAlexandre Belloni struct ocelot_mact_entry {
18a556c76aSAlexandre Belloni 	u8 mac[ETH_ALEN];
19a556c76aSAlexandre Belloni 	u16 vid;
20a556c76aSAlexandre Belloni 	enum macaccess_entry_type type;
21a556c76aSAlexandre Belloni };
22a556c76aSAlexandre Belloni 
23639c1b26SSteen Hegelund static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot)
24639c1b26SSteen Hegelund {
25639c1b26SSteen Hegelund 	return ocelot_read(ocelot, ANA_TABLES_MACACCESS);
26639c1b26SSteen Hegelund }
27639c1b26SSteen Hegelund 
28a556c76aSAlexandre Belloni static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot)
29a556c76aSAlexandre Belloni {
30639c1b26SSteen Hegelund 	u32 val;
31a556c76aSAlexandre Belloni 
32639c1b26SSteen Hegelund 	return readx_poll_timeout(ocelot_mact_read_macaccess,
33639c1b26SSteen Hegelund 		ocelot, val,
34639c1b26SSteen Hegelund 		(val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) ==
35639c1b26SSteen Hegelund 		MACACCESS_CMD_IDLE,
36639c1b26SSteen Hegelund 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
37a556c76aSAlexandre Belloni }
38a556c76aSAlexandre Belloni 
39a556c76aSAlexandre Belloni static void ocelot_mact_select(struct ocelot *ocelot,
40a556c76aSAlexandre Belloni 			       const unsigned char mac[ETH_ALEN],
41a556c76aSAlexandre Belloni 			       unsigned int vid)
42a556c76aSAlexandre Belloni {
43a556c76aSAlexandre Belloni 	u32 macl = 0, mach = 0;
44a556c76aSAlexandre Belloni 
45a556c76aSAlexandre Belloni 	/* Set the MAC address to handle and the vlan associated in a format
46a556c76aSAlexandre Belloni 	 * understood by the hardware.
47a556c76aSAlexandre Belloni 	 */
48a556c76aSAlexandre Belloni 	mach |= vid    << 16;
49a556c76aSAlexandre Belloni 	mach |= mac[0] << 8;
50a556c76aSAlexandre Belloni 	mach |= mac[1] << 0;
51a556c76aSAlexandre Belloni 	macl |= mac[2] << 24;
52a556c76aSAlexandre Belloni 	macl |= mac[3] << 16;
53a556c76aSAlexandre Belloni 	macl |= mac[4] << 8;
54a556c76aSAlexandre Belloni 	macl |= mac[5] << 0;
55a556c76aSAlexandre Belloni 
56a556c76aSAlexandre Belloni 	ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA);
57a556c76aSAlexandre Belloni 	ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA);
58a556c76aSAlexandre Belloni 
59a556c76aSAlexandre Belloni }
60a556c76aSAlexandre Belloni 
619c90eea3SVladimir Oltean int ocelot_mact_learn(struct ocelot *ocelot, int port,
62a556c76aSAlexandre Belloni 		      const unsigned char mac[ETH_ALEN],
639c90eea3SVladimir Oltean 		      unsigned int vid, enum macaccess_entry_type type)
64a556c76aSAlexandre Belloni {
65584b7cfcSAlban Bedel 	u32 cmd = ANA_TABLES_MACACCESS_VALID |
66584b7cfcSAlban Bedel 		ANA_TABLES_MACACCESS_DEST_IDX(port) |
67584b7cfcSAlban Bedel 		ANA_TABLES_MACACCESS_ENTRYTYPE(type) |
68584b7cfcSAlban Bedel 		ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN);
69584b7cfcSAlban Bedel 	unsigned int mc_ports;
70584b7cfcSAlban Bedel 
71584b7cfcSAlban Bedel 	/* Set MAC_CPU_COPY if the CPU port is used by a multicast entry */
72584b7cfcSAlban Bedel 	if (type == ENTRYTYPE_MACv4)
73584b7cfcSAlban Bedel 		mc_ports = (mac[1] << 8) | mac[2];
74584b7cfcSAlban Bedel 	else if (type == ENTRYTYPE_MACv6)
75584b7cfcSAlban Bedel 		mc_ports = (mac[0] << 8) | mac[1];
76584b7cfcSAlban Bedel 	else
77584b7cfcSAlban Bedel 		mc_ports = 0;
78584b7cfcSAlban Bedel 
79584b7cfcSAlban Bedel 	if (mc_ports & BIT(ocelot->num_phys_ports))
80584b7cfcSAlban Bedel 		cmd |= ANA_TABLES_MACACCESS_MAC_CPU_COPY;
81584b7cfcSAlban Bedel 
82a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
83a556c76aSAlexandre Belloni 
84a556c76aSAlexandre Belloni 	/* Issue a write command */
85584b7cfcSAlban Bedel 	ocelot_write(ocelot, cmd, ANA_TABLES_MACACCESS);
86a556c76aSAlexandre Belloni 
87a556c76aSAlexandre Belloni 	return ocelot_mact_wait_for_completion(ocelot);
88a556c76aSAlexandre Belloni }
899c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_mact_learn);
90a556c76aSAlexandre Belloni 
919c90eea3SVladimir Oltean int ocelot_mact_forget(struct ocelot *ocelot,
929c90eea3SVladimir Oltean 		       const unsigned char mac[ETH_ALEN], unsigned int vid)
93a556c76aSAlexandre Belloni {
94a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
95a556c76aSAlexandre Belloni 
96a556c76aSAlexandre Belloni 	/* Issue a forget command */
97a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
98a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET),
99a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
100a556c76aSAlexandre Belloni 
101a556c76aSAlexandre Belloni 	return ocelot_mact_wait_for_completion(ocelot);
102a556c76aSAlexandre Belloni }
1039c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_mact_forget);
104a556c76aSAlexandre Belloni 
105a556c76aSAlexandre Belloni static void ocelot_mact_init(struct ocelot *ocelot)
106a556c76aSAlexandre Belloni {
107a556c76aSAlexandre Belloni 	/* Configure the learning mode entries attributes:
108a556c76aSAlexandre Belloni 	 * - Do not copy the frame to the CPU extraction queues.
109a556c76aSAlexandre Belloni 	 * - Use the vlan and mac_cpoy for dmac lookup.
110a556c76aSAlexandre Belloni 	 */
111a556c76aSAlexandre Belloni 	ocelot_rmw(ocelot, 0,
112a556c76aSAlexandre Belloni 		   ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS
113a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_FWD_KILL
114a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_IGNORE_VLAN,
115a556c76aSAlexandre Belloni 		   ANA_AGENCTRL);
116a556c76aSAlexandre Belloni 
117a556c76aSAlexandre Belloni 	/* Clear the MAC table */
118a556c76aSAlexandre Belloni 	ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS);
119a556c76aSAlexandre Belloni }
120a556c76aSAlexandre Belloni 
121f270dbfaSVladimir Oltean static void ocelot_vcap_enable(struct ocelot *ocelot, int port)
122b5962294SHoratiu Vultur {
123b5962294SHoratiu Vultur 	ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA |
124b5962294SHoratiu Vultur 			 ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa),
125f270dbfaSVladimir Oltean 			 ANA_PORT_VCAP_S2_CFG, port);
12675944fdaSXiaoliang Yang 
12775944fdaSXiaoliang Yang 	ocelot_write_gix(ocelot, ANA_PORT_VCAP_CFG_S1_ENA,
12875944fdaSXiaoliang Yang 			 ANA_PORT_VCAP_CFG, port);
1292f17c050SXiaoliang Yang 
1302f17c050SXiaoliang Yang 	ocelot_rmw_gix(ocelot, REW_PORT_CFG_ES0_EN,
1312f17c050SXiaoliang Yang 		       REW_PORT_CFG_ES0_EN,
1322f17c050SXiaoliang Yang 		       REW_PORT_CFG, port);
133b5962294SHoratiu Vultur }
134b5962294SHoratiu Vultur 
135639c1b26SSteen Hegelund static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot)
136639c1b26SSteen Hegelund {
137639c1b26SSteen Hegelund 	return ocelot_read(ocelot, ANA_TABLES_VLANACCESS);
138639c1b26SSteen Hegelund }
139639c1b26SSteen Hegelund 
140a556c76aSAlexandre Belloni static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot)
141a556c76aSAlexandre Belloni {
142639c1b26SSteen Hegelund 	u32 val;
143a556c76aSAlexandre Belloni 
144639c1b26SSteen Hegelund 	return readx_poll_timeout(ocelot_vlant_read_vlanaccess,
145639c1b26SSteen Hegelund 		ocelot,
146639c1b26SSteen Hegelund 		val,
147639c1b26SSteen Hegelund 		(val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) ==
148639c1b26SSteen Hegelund 		ANA_TABLES_VLANACCESS_CMD_IDLE,
149639c1b26SSteen Hegelund 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
150a556c76aSAlexandre Belloni }
151a556c76aSAlexandre Belloni 
1527142529fSAntoine Tenart static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask)
1537142529fSAntoine Tenart {
1547142529fSAntoine Tenart 	/* Select the VID to configure */
1557142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid),
1567142529fSAntoine Tenart 		     ANA_TABLES_VLANTIDX);
1577142529fSAntoine Tenart 	/* Set the vlan port members mask and issue a write command */
1587142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) |
1597142529fSAntoine Tenart 			     ANA_TABLES_VLANACCESS_CMD_WRITE,
1607142529fSAntoine Tenart 		     ANA_TABLES_VLANACCESS);
1617142529fSAntoine Tenart 
1627142529fSAntoine Tenart 	return ocelot_vlant_wait_for_completion(ocelot);
1637142529fSAntoine Tenart }
1647142529fSAntoine Tenart 
1652f0402feSVladimir Oltean static void ocelot_port_set_native_vlan(struct ocelot *ocelot, int port,
166c3e58a75SVladimir Oltean 					struct ocelot_vlan native_vlan)
16797bb69e1SVladimir Oltean {
16897bb69e1SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
16987b0f983SVladimir Oltean 	u32 val = 0;
17097bb69e1SVladimir Oltean 
171c3e58a75SVladimir Oltean 	ocelot_port->native_vlan = native_vlan;
17297bb69e1SVladimir Oltean 
173c3e58a75SVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_VID(native_vlan.vid),
1747142529fSAntoine Tenart 		       REW_PORT_VLAN_CFG_PORT_VID_M,
17597bb69e1SVladimir Oltean 		       REW_PORT_VLAN_CFG, port);
17697bb69e1SVladimir Oltean 
17787b0f983SVladimir Oltean 	if (ocelot_port->vlan_aware) {
178e2b2e83eSVladimir Oltean 		if (native_vlan.valid)
17987b0f983SVladimir Oltean 			/* Tag all frames except when VID == DEFAULT_VLAN */
18087b0f983SVladimir Oltean 			val = REW_TAG_CFG_TAG_CFG(1);
18187b0f983SVladimir Oltean 		else
18287b0f983SVladimir Oltean 			/* Tag all frames */
18387b0f983SVladimir Oltean 			val = REW_TAG_CFG_TAG_CFG(3);
18487b0f983SVladimir Oltean 	} else {
18587b0f983SVladimir Oltean 		/* Port tagging disabled. */
18687b0f983SVladimir Oltean 		val = REW_TAG_CFG_TAG_CFG(0);
18787b0f983SVladimir Oltean 	}
18887b0f983SVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
18987b0f983SVladimir Oltean 		       REW_TAG_CFG_TAG_CFG_M,
19087b0f983SVladimir Oltean 		       REW_TAG_CFG, port);
19197bb69e1SVladimir Oltean }
19297bb69e1SVladimir Oltean 
19375e5a554SVladimir Oltean /* Default vlan to clasify for untagged frames (may be zero) */
194c3e58a75SVladimir Oltean static void ocelot_port_set_pvid(struct ocelot *ocelot, int port,
195c3e58a75SVladimir Oltean 				 struct ocelot_vlan pvid_vlan)
19675e5a554SVladimir Oltean {
19775e5a554SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
198be0576feSVladimir Oltean 	u32 val = 0;
19975e5a554SVladimir Oltean 
200c3e58a75SVladimir Oltean 	ocelot_port->pvid_vlan = pvid_vlan;
20175e5a554SVladimir Oltean 
20275e5a554SVladimir Oltean 	if (!ocelot_port->vlan_aware)
203c3e58a75SVladimir Oltean 		pvid_vlan.vid = 0;
20475e5a554SVladimir Oltean 
20575e5a554SVladimir Oltean 	ocelot_rmw_gix(ocelot,
206c3e58a75SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_VID(pvid_vlan.vid),
20775e5a554SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_VID_M,
20875e5a554SVladimir Oltean 		       ANA_PORT_VLAN_CFG, port);
209be0576feSVladimir Oltean 
210be0576feSVladimir Oltean 	/* If there's no pvid, we should drop not only untagged traffic (which
211be0576feSVladimir Oltean 	 * happens automatically), but also 802.1p traffic which gets
212be0576feSVladimir Oltean 	 * classified to VLAN 0, but that is always in our RX filter, so it
213be0576feSVladimir Oltean 	 * would get accepted were it not for this setting.
214be0576feSVladimir Oltean 	 */
215be0576feSVladimir Oltean 	if (!pvid_vlan.valid && ocelot_port->vlan_aware)
216be0576feSVladimir Oltean 		val = ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
217be0576feSVladimir Oltean 		      ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
218be0576feSVladimir Oltean 
219be0576feSVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
220be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
221be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA,
222be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
22375e5a554SVladimir Oltean }
22475e5a554SVladimir Oltean 
225bbf6a2d9SVladimir Oltean static int ocelot_vlan_member_set(struct ocelot *ocelot, u32 vlan_mask, u16 vid)
226bbf6a2d9SVladimir Oltean {
227bbf6a2d9SVladimir Oltean 	int err;
228bbf6a2d9SVladimir Oltean 
229bbf6a2d9SVladimir Oltean 	err = ocelot_vlant_set_mask(ocelot, vid, vlan_mask);
230bbf6a2d9SVladimir Oltean 	if (err)
231bbf6a2d9SVladimir Oltean 		return err;
232bbf6a2d9SVladimir Oltean 
233bbf6a2d9SVladimir Oltean 	ocelot->vlan_mask[vid] = vlan_mask;
234bbf6a2d9SVladimir Oltean 
235bbf6a2d9SVladimir Oltean 	return 0;
236bbf6a2d9SVladimir Oltean }
237bbf6a2d9SVladimir Oltean 
238bbf6a2d9SVladimir Oltean static int ocelot_vlan_member_add(struct ocelot *ocelot, int port, u16 vid)
239bbf6a2d9SVladimir Oltean {
240bbf6a2d9SVladimir Oltean 	return ocelot_vlan_member_set(ocelot,
241bbf6a2d9SVladimir Oltean 				      ocelot->vlan_mask[vid] | BIT(port),
242bbf6a2d9SVladimir Oltean 				      vid);
243bbf6a2d9SVladimir Oltean }
244bbf6a2d9SVladimir Oltean 
245bbf6a2d9SVladimir Oltean static int ocelot_vlan_member_del(struct ocelot *ocelot, int port, u16 vid)
246bbf6a2d9SVladimir Oltean {
247bbf6a2d9SVladimir Oltean 	return ocelot_vlan_member_set(ocelot,
248bbf6a2d9SVladimir Oltean 				      ocelot->vlan_mask[vid] & ~BIT(port),
249bbf6a2d9SVladimir Oltean 				      vid);
250bbf6a2d9SVladimir Oltean }
251bbf6a2d9SVladimir Oltean 
2522e554a7aSVladimir Oltean int ocelot_port_vlan_filtering(struct ocelot *ocelot, int port,
2533b95d1b2SVladimir Oltean 			       bool vlan_aware, struct netlink_ext_ack *extack)
25487b0f983SVladimir Oltean {
25570edfae1SVladimir Oltean 	struct ocelot_vcap_block *block = &ocelot->block[VCAP_IS1];
256bae33f2bSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
25770edfae1SVladimir Oltean 	struct ocelot_vcap_filter *filter;
258bae33f2bSVladimir Oltean 	u32 val;
25970edfae1SVladimir Oltean 
26070edfae1SVladimir Oltean 	list_for_each_entry(filter, &block->rules, list) {
26170edfae1SVladimir Oltean 		if (filter->ingress_port_mask & BIT(port) &&
26270edfae1SVladimir Oltean 		    filter->action.vid_replace_ena) {
2633b95d1b2SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
2643b95d1b2SVladimir Oltean 					   "Cannot change VLAN state with vlan modify rules active");
26570edfae1SVladimir Oltean 			return -EBUSY;
26670edfae1SVladimir Oltean 		}
26770edfae1SVladimir Oltean 	}
26870edfae1SVladimir Oltean 
26987b0f983SVladimir Oltean 	ocelot_port->vlan_aware = vlan_aware;
27087b0f983SVladimir Oltean 
27187b0f983SVladimir Oltean 	if (vlan_aware)
27287b0f983SVladimir Oltean 		val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
27387b0f983SVladimir Oltean 		      ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
27487b0f983SVladimir Oltean 	else
27587b0f983SVladimir Oltean 		val = 0;
27687b0f983SVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
27787b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
27887b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
27987b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG, port);
28087b0f983SVladimir Oltean 
281c3e58a75SVladimir Oltean 	ocelot_port_set_pvid(ocelot, port, ocelot_port->pvid_vlan);
282c3e58a75SVladimir Oltean 	ocelot_port_set_native_vlan(ocelot, port, ocelot_port->native_vlan);
2832e554a7aSVladimir Oltean 
2842e554a7aSVladimir Oltean 	return 0;
28587b0f983SVladimir Oltean }
28687b0f983SVladimir Oltean EXPORT_SYMBOL(ocelot_port_vlan_filtering);
28787b0f983SVladimir Oltean 
2882f0402feSVladimir Oltean int ocelot_vlan_prepare(struct ocelot *ocelot, int port, u16 vid, bool pvid,
28901af940eSVladimir Oltean 			bool untagged, struct netlink_ext_ack *extack)
2902f0402feSVladimir Oltean {
2912f0402feSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2922f0402feSVladimir Oltean 
2932f0402feSVladimir Oltean 	/* Deny changing the native VLAN, but always permit deleting it */
2942f0402feSVladimir Oltean 	if (untagged && ocelot_port->native_vlan.vid != vid &&
2952f0402feSVladimir Oltean 	    ocelot_port->native_vlan.valid) {
29601af940eSVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
29701af940eSVladimir Oltean 				   "Port already has a native VLAN");
2982f0402feSVladimir Oltean 		return -EBUSY;
2992f0402feSVladimir Oltean 	}
3002f0402feSVladimir Oltean 
3012f0402feSVladimir Oltean 	return 0;
3022f0402feSVladimir Oltean }
3032f0402feSVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_prepare);
3042f0402feSVladimir Oltean 
3055e256365SVladimir Oltean int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid,
3067142529fSAntoine Tenart 		    bool untagged)
3077142529fSAntoine Tenart {
308bbf6a2d9SVladimir Oltean 	int err;
3097142529fSAntoine Tenart 
310bbf6a2d9SVladimir Oltean 	err = ocelot_vlan_member_add(ocelot, port, vid);
311bbf6a2d9SVladimir Oltean 	if (err)
312bbf6a2d9SVladimir Oltean 		return err;
3137142529fSAntoine Tenart 
3147142529fSAntoine Tenart 	/* Default ingress vlan classification */
315c3e58a75SVladimir Oltean 	if (pvid) {
316c3e58a75SVladimir Oltean 		struct ocelot_vlan pvid_vlan;
317c3e58a75SVladimir Oltean 
318c3e58a75SVladimir Oltean 		pvid_vlan.vid = vid;
319e2b2e83eSVladimir Oltean 		pvid_vlan.valid = true;
320c3e58a75SVladimir Oltean 		ocelot_port_set_pvid(ocelot, port, pvid_vlan);
321c3e58a75SVladimir Oltean 	}
3227142529fSAntoine Tenart 
3237142529fSAntoine Tenart 	/* Untagged egress vlan clasification */
32497bb69e1SVladimir Oltean 	if (untagged) {
325c3e58a75SVladimir Oltean 		struct ocelot_vlan native_vlan;
326c3e58a75SVladimir Oltean 
327c3e58a75SVladimir Oltean 		native_vlan.vid = vid;
328e2b2e83eSVladimir Oltean 		native_vlan.valid = true;
3292f0402feSVladimir Oltean 		ocelot_port_set_native_vlan(ocelot, port, native_vlan);
330b9cd75e6SVladimir Oltean 	}
3317142529fSAntoine Tenart 
3327142529fSAntoine Tenart 	return 0;
3337142529fSAntoine Tenart }
3345e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_add);
3357142529fSAntoine Tenart 
3365e256365SVladimir Oltean int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid)
3379855934cSVladimir Oltean {
3389855934cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
339bbf6a2d9SVladimir Oltean 	int err;
3407142529fSAntoine Tenart 
341bbf6a2d9SVladimir Oltean 	err = ocelot_vlan_member_del(ocelot, port, vid);
342bbf6a2d9SVladimir Oltean 	if (err)
343bbf6a2d9SVladimir Oltean 		return err;
3447142529fSAntoine Tenart 
345be0576feSVladimir Oltean 	/* Ingress */
346be0576feSVladimir Oltean 	if (ocelot_port->pvid_vlan.vid == vid) {
347be0576feSVladimir Oltean 		struct ocelot_vlan pvid_vlan = {0};
348be0576feSVladimir Oltean 
349be0576feSVladimir Oltean 		ocelot_port_set_pvid(ocelot, port, pvid_vlan);
350be0576feSVladimir Oltean 	}
351be0576feSVladimir Oltean 
3527142529fSAntoine Tenart 	/* Egress */
353c3e58a75SVladimir Oltean 	if (ocelot_port->native_vlan.vid == vid) {
354e2b2e83eSVladimir Oltean 		struct ocelot_vlan native_vlan = {0};
355c3e58a75SVladimir Oltean 
356c3e58a75SVladimir Oltean 		ocelot_port_set_native_vlan(ocelot, port, native_vlan);
357c3e58a75SVladimir Oltean 	}
3587142529fSAntoine Tenart 
3597142529fSAntoine Tenart 	return 0;
3607142529fSAntoine Tenart }
3615e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_del);
3627142529fSAntoine Tenart 
363a556c76aSAlexandre Belloni static void ocelot_vlan_init(struct ocelot *ocelot)
364a556c76aSAlexandre Belloni {
365bbf6a2d9SVladimir Oltean 	unsigned long all_ports = GENMASK(ocelot->num_phys_ports - 1, 0);
3667142529fSAntoine Tenart 	u16 port, vid;
3677142529fSAntoine Tenart 
368a556c76aSAlexandre Belloni 	/* Clear VLAN table, by default all ports are members of all VLANs */
369a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
370a556c76aSAlexandre Belloni 		     ANA_TABLES_VLANACCESS);
371a556c76aSAlexandre Belloni 	ocelot_vlant_wait_for_completion(ocelot);
3727142529fSAntoine Tenart 
3737142529fSAntoine Tenart 	/* Configure the port VLAN memberships */
374bbf6a2d9SVladimir Oltean 	for (vid = 1; vid < VLAN_N_VID; vid++)
375bbf6a2d9SVladimir Oltean 		ocelot_vlan_member_set(ocelot, 0, vid);
3767142529fSAntoine Tenart 
3777142529fSAntoine Tenart 	/* Because VLAN filtering is enabled, we need VID 0 to get untagged
3787142529fSAntoine Tenart 	 * traffic.  It is added automatically if 8021q module is loaded, but
3797142529fSAntoine Tenart 	 * we can't rely on it since module may be not loaded.
3807142529fSAntoine Tenart 	 */
381bbf6a2d9SVladimir Oltean 	ocelot_vlan_member_set(ocelot, all_ports, 0);
3827142529fSAntoine Tenart 
3837142529fSAntoine Tenart 	/* Set vlan ingress filter mask to all ports but the CPU port by
3847142529fSAntoine Tenart 	 * default.
3857142529fSAntoine Tenart 	 */
386bbf6a2d9SVladimir Oltean 	ocelot_write(ocelot, all_ports, ANA_VLANMASK);
3877142529fSAntoine Tenart 
3887142529fSAntoine Tenart 	for (port = 0; port < ocelot->num_phys_ports; port++) {
3897142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
3907142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
3917142529fSAntoine Tenart 	}
392a556c76aSAlexandre Belloni }
393a556c76aSAlexandre Belloni 
394eb4733d7SVladimir Oltean static u32 ocelot_read_eq_avail(struct ocelot *ocelot, int port)
395eb4733d7SVladimir Oltean {
396eb4733d7SVladimir Oltean 	return ocelot_read_rix(ocelot, QSYS_SW_STATUS, port);
397eb4733d7SVladimir Oltean }
398eb4733d7SVladimir Oltean 
399e6e12df6SVladimir Oltean static int ocelot_port_flush(struct ocelot *ocelot, int port)
400eb4733d7SVladimir Oltean {
4011650bdb1SVladimir Oltean 	unsigned int pause_ena;
402eb4733d7SVladimir Oltean 	int err, val;
403eb4733d7SVladimir Oltean 
404eb4733d7SVladimir Oltean 	/* Disable dequeuing from the egress queues */
405eb4733d7SVladimir Oltean 	ocelot_rmw_rix(ocelot, QSYS_PORT_MODE_DEQUEUE_DIS,
406eb4733d7SVladimir Oltean 		       QSYS_PORT_MODE_DEQUEUE_DIS,
407eb4733d7SVladimir Oltean 		       QSYS_PORT_MODE, port);
408eb4733d7SVladimir Oltean 
409eb4733d7SVladimir Oltean 	/* Disable flow control */
4101650bdb1SVladimir Oltean 	ocelot_fields_read(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, &pause_ena);
411eb4733d7SVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
412eb4733d7SVladimir Oltean 
413eb4733d7SVladimir Oltean 	/* Disable priority flow control */
414eb4733d7SVladimir Oltean 	ocelot_fields_write(ocelot, port,
415eb4733d7SVladimir Oltean 			    QSYS_SWITCH_PORT_MODE_TX_PFC_ENA, 0);
416eb4733d7SVladimir Oltean 
417eb4733d7SVladimir Oltean 	/* Wait at least the time it takes to receive a frame of maximum length
418eb4733d7SVladimir Oltean 	 * at the port.
419eb4733d7SVladimir Oltean 	 * Worst-case delays for 10 kilobyte jumbo frames are:
420eb4733d7SVladimir Oltean 	 * 8 ms on a 10M port
421eb4733d7SVladimir Oltean 	 * 800 μs on a 100M port
422eb4733d7SVladimir Oltean 	 * 80 μs on a 1G port
423eb4733d7SVladimir Oltean 	 * 32 μs on a 2.5G port
424eb4733d7SVladimir Oltean 	 */
425eb4733d7SVladimir Oltean 	usleep_range(8000, 10000);
426eb4733d7SVladimir Oltean 
427eb4733d7SVladimir Oltean 	/* Disable half duplex backpressure. */
428eb4733d7SVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, SYS_FRONT_PORT_MODE_HDX_MODE,
429eb4733d7SVladimir Oltean 		       SYS_FRONT_PORT_MODE, port);
430eb4733d7SVladimir Oltean 
431eb4733d7SVladimir Oltean 	/* Flush the queues associated with the port. */
432eb4733d7SVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG_FLUSH_ENA,
433eb4733d7SVladimir Oltean 		       REW_PORT_CFG, port);
434eb4733d7SVladimir Oltean 
435eb4733d7SVladimir Oltean 	/* Enable dequeuing from the egress queues. */
436eb4733d7SVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, QSYS_PORT_MODE_DEQUEUE_DIS, QSYS_PORT_MODE,
437eb4733d7SVladimir Oltean 		       port);
438eb4733d7SVladimir Oltean 
439eb4733d7SVladimir Oltean 	/* Wait until flushing is complete. */
440eb4733d7SVladimir Oltean 	err = read_poll_timeout(ocelot_read_eq_avail, val, !val,
441eb4733d7SVladimir Oltean 				100, 2000000, false, ocelot, port);
442eb4733d7SVladimir Oltean 
443eb4733d7SVladimir Oltean 	/* Clear flushing again. */
444eb4733d7SVladimir Oltean 	ocelot_rmw_gix(ocelot, 0, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG, port);
445eb4733d7SVladimir Oltean 
4461650bdb1SVladimir Oltean 	/* Re-enable flow control */
4471650bdb1SVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, pause_ena);
4481650bdb1SVladimir Oltean 
449eb4733d7SVladimir Oltean 	return err;
450eb4733d7SVladimir Oltean }
451eb4733d7SVladimir Oltean 
452e6e12df6SVladimir Oltean void ocelot_phylink_mac_link_down(struct ocelot *ocelot, int port,
453e6e12df6SVladimir Oltean 				  unsigned int link_an_mode,
454e6e12df6SVladimir Oltean 				  phy_interface_t interface,
455e6e12df6SVladimir Oltean 				  unsigned long quirks)
456a556c76aSAlexandre Belloni {
45726f4dbabSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
458e6e12df6SVladimir Oltean 	int err;
459a556c76aSAlexandre Belloni 
460e6e12df6SVladimir Oltean 	ocelot_port_rmwl(ocelot_port, 0, DEV_MAC_ENA_CFG_RX_ENA,
461e6e12df6SVladimir Oltean 			 DEV_MAC_ENA_CFG);
462e6e12df6SVladimir Oltean 
463e6e12df6SVladimir Oltean 	ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0);
464e6e12df6SVladimir Oltean 
465e6e12df6SVladimir Oltean 	err = ocelot_port_flush(ocelot, port);
466e6e12df6SVladimir Oltean 	if (err)
467e6e12df6SVladimir Oltean 		dev_err(ocelot->dev, "failed to flush port %d: %d\n",
468e6e12df6SVladimir Oltean 			port, err);
469e6e12df6SVladimir Oltean 
470e6e12df6SVladimir Oltean 	/* Put the port in reset. */
471e6e12df6SVladimir Oltean 	if (interface != PHY_INTERFACE_MODE_QSGMII ||
472e6e12df6SVladimir Oltean 	    !(quirks & OCELOT_QUIRK_QSGMII_PORTS_MUST_BE_UP))
473e6e12df6SVladimir Oltean 		ocelot_port_rmwl(ocelot_port,
474e6e12df6SVladimir Oltean 				 DEV_CLOCK_CFG_MAC_TX_RST |
47574a3bc42SWan Jiabing 				 DEV_CLOCK_CFG_MAC_RX_RST,
476e6e12df6SVladimir Oltean 				 DEV_CLOCK_CFG_MAC_TX_RST |
47774a3bc42SWan Jiabing 				 DEV_CLOCK_CFG_MAC_RX_RST,
478e6e12df6SVladimir Oltean 				 DEV_CLOCK_CFG);
479e6e12df6SVladimir Oltean }
480e6e12df6SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_down);
481e6e12df6SVladimir Oltean 
482e6e12df6SVladimir Oltean void ocelot_phylink_mac_link_up(struct ocelot *ocelot, int port,
483e6e12df6SVladimir Oltean 				struct phy_device *phydev,
484e6e12df6SVladimir Oltean 				unsigned int link_an_mode,
485e6e12df6SVladimir Oltean 				phy_interface_t interface,
486e6e12df6SVladimir Oltean 				int speed, int duplex,
487e6e12df6SVladimir Oltean 				bool tx_pause, bool rx_pause,
488e6e12df6SVladimir Oltean 				unsigned long quirks)
489e6e12df6SVladimir Oltean {
490e6e12df6SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
491e6e12df6SVladimir Oltean 	int mac_speed, mode = 0;
492e6e12df6SVladimir Oltean 	u32 mac_fc_cfg;
493e6e12df6SVladimir Oltean 
494e6e12df6SVladimir Oltean 	/* The MAC might be integrated in systems where the MAC speed is fixed
495e6e12df6SVladimir Oltean 	 * and it's the PCS who is performing the rate adaptation, so we have
496e6e12df6SVladimir Oltean 	 * to write "1000Mbps" into the LINK_SPEED field of DEV_CLOCK_CFG
497e6e12df6SVladimir Oltean 	 * (which is also its default value).
498e6e12df6SVladimir Oltean 	 */
499e6e12df6SVladimir Oltean 	if ((quirks & OCELOT_QUIRK_PCS_PERFORMS_RATE_ADAPTATION) ||
500e6e12df6SVladimir Oltean 	    speed == SPEED_1000) {
501e6e12df6SVladimir Oltean 		mac_speed = OCELOT_SPEED_1000;
502e6e12df6SVladimir Oltean 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
503e6e12df6SVladimir Oltean 	} else if (speed == SPEED_2500) {
504e6e12df6SVladimir Oltean 		mac_speed = OCELOT_SPEED_2500;
505e6e12df6SVladimir Oltean 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
506e6e12df6SVladimir Oltean 	} else if (speed == SPEED_100) {
507e6e12df6SVladimir Oltean 		mac_speed = OCELOT_SPEED_100;
508e6e12df6SVladimir Oltean 	} else {
509e6e12df6SVladimir Oltean 		mac_speed = OCELOT_SPEED_10;
510e6e12df6SVladimir Oltean 	}
511e6e12df6SVladimir Oltean 
512e6e12df6SVladimir Oltean 	if (duplex == DUPLEX_FULL)
513e6e12df6SVladimir Oltean 		mode |= DEV_MAC_MODE_CFG_FDX_ENA;
514e6e12df6SVladimir Oltean 
515e6e12df6SVladimir Oltean 	ocelot_port_writel(ocelot_port, mode, DEV_MAC_MODE_CFG);
516e6e12df6SVladimir Oltean 
517e6e12df6SVladimir Oltean 	/* Take port out of reset by clearing the MAC_TX_RST, MAC_RX_RST and
518e6e12df6SVladimir Oltean 	 * PORT_RST bits in DEV_CLOCK_CFG.
519e6e12df6SVladimir Oltean 	 */
520e6e12df6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(mac_speed),
521e6e12df6SVladimir Oltean 			   DEV_CLOCK_CFG);
522e6e12df6SVladimir Oltean 
523e6e12df6SVladimir Oltean 	switch (speed) {
524a556c76aSAlexandre Belloni 	case SPEED_10:
525e6e12df6SVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_10);
526a556c76aSAlexandre Belloni 		break;
527a556c76aSAlexandre Belloni 	case SPEED_100:
528e6e12df6SVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_100);
529a556c76aSAlexandre Belloni 		break;
530a556c76aSAlexandre Belloni 	case SPEED_1000:
531a556c76aSAlexandre Belloni 	case SPEED_2500:
532e6e12df6SVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_1000);
533a556c76aSAlexandre Belloni 		break;
534a556c76aSAlexandre Belloni 	default:
535e6e12df6SVladimir Oltean 		dev_err(ocelot->dev, "Unsupported speed on port %d: %d\n",
536e6e12df6SVladimir Oltean 			port, speed);
537a556c76aSAlexandre Belloni 		return;
538a556c76aSAlexandre Belloni 	}
539a556c76aSAlexandre Belloni 
540e6e12df6SVladimir Oltean 	/* Handle RX pause in all cases, with 2500base-X this is used for rate
541e6e12df6SVladimir Oltean 	 * adaptation.
542e6e12df6SVladimir Oltean 	 */
543e6e12df6SVladimir Oltean 	mac_fc_cfg |= SYS_MAC_FC_CFG_RX_FC_ENA;
544a556c76aSAlexandre Belloni 
545e6e12df6SVladimir Oltean 	if (tx_pause)
546e6e12df6SVladimir Oltean 		mac_fc_cfg |= SYS_MAC_FC_CFG_TX_FC_ENA |
547e6e12df6SVladimir Oltean 			      SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
548e6e12df6SVladimir Oltean 			      SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
549e6e12df6SVladimir Oltean 			      SYS_MAC_FC_CFG_ZERO_PAUSE_ENA;
550a556c76aSAlexandre Belloni 
551e6e12df6SVladimir Oltean 	/* Flow control. Link speed is only used here to evaluate the time
552e6e12df6SVladimir Oltean 	 * specification in incoming pause frames.
553e6e12df6SVladimir Oltean 	 */
554e6e12df6SVladimir Oltean 	ocelot_write_rix(ocelot, mac_fc_cfg, SYS_MAC_FC_CFG, port);
555a556c76aSAlexandre Belloni 
556e6e12df6SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
5571ba8f656SVladimir Oltean 
558e6e12df6SVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, tx_pause);
5591ba8f656SVladimir Oltean 
560e6e12df6SVladimir Oltean 	/* Undo the effects of ocelot_phylink_mac_link_down:
561e6e12df6SVladimir Oltean 	 * enable MAC module
562e6e12df6SVladimir Oltean 	 */
563004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
564a556c76aSAlexandre Belloni 			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
565a556c76aSAlexandre Belloni 
566a556c76aSAlexandre Belloni 	/* Core: Enable port for frame transfer */
567886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, port,
568886e1387SVladimir Oltean 			    QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
569a556c76aSAlexandre Belloni }
570e6e12df6SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_up);
571889b8950SVladimir Oltean 
57252849bcfSVladimir Oltean static int ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port,
573e2f9a8feSVladimir Oltean 					struct sk_buff *clone)
574400928bfSYangbo Lu {
575e2f9a8feSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
57652849bcfSVladimir Oltean 	unsigned long flags;
577400928bfSYangbo Lu 
57852849bcfSVladimir Oltean 	spin_lock_irqsave(&ocelot->ts_id_lock, flags);
57952849bcfSVladimir Oltean 
58052849bcfSVladimir Oltean 	if (ocelot_port->ptp_skbs_in_flight == OCELOT_MAX_PTP_ID ||
58152849bcfSVladimir Oltean 	    ocelot->ptp_skbs_in_flight == OCELOT_PTP_FIFO_SIZE) {
58252849bcfSVladimir Oltean 		spin_unlock_irqrestore(&ocelot->ts_id_lock, flags);
58352849bcfSVladimir Oltean 		return -EBUSY;
58452849bcfSVladimir Oltean 	}
5856565243cSVladimir Oltean 
586e2f9a8feSVladimir Oltean 	skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS;
587c4b364ceSYangbo Lu 	/* Store timestamp ID in OCELOT_SKB_CB(clone)->ts_id */
588c4b364ceSYangbo Lu 	OCELOT_SKB_CB(clone)->ts_id = ocelot_port->ts_id;
58952849bcfSVladimir Oltean 
590c57fe003SVladimir Oltean 	ocelot_port->ts_id++;
591c57fe003SVladimir Oltean 	if (ocelot_port->ts_id == OCELOT_MAX_PTP_ID)
592c57fe003SVladimir Oltean 		ocelot_port->ts_id = 0;
59352849bcfSVladimir Oltean 
59452849bcfSVladimir Oltean 	ocelot_port->ptp_skbs_in_flight++;
59552849bcfSVladimir Oltean 	ocelot->ptp_skbs_in_flight++;
59652849bcfSVladimir Oltean 
597e2f9a8feSVladimir Oltean 	skb_queue_tail(&ocelot_port->tx_skbs, clone);
5986565243cSVladimir Oltean 
59952849bcfSVladimir Oltean 	spin_unlock_irqrestore(&ocelot->ts_id_lock, flags);
60052849bcfSVladimir Oltean 
60152849bcfSVladimir Oltean 	return 0;
602400928bfSYangbo Lu }
603682eaad9SYangbo Lu 
60439e5308bSYangbo Lu u32 ocelot_ptp_rew_op(struct sk_buff *skb)
60539e5308bSYangbo Lu {
60639e5308bSYangbo Lu 	struct sk_buff *clone = OCELOT_SKB_CB(skb)->clone;
60739e5308bSYangbo Lu 	u8 ptp_cmd = OCELOT_SKB_CB(skb)->ptp_cmd;
60839e5308bSYangbo Lu 	u32 rew_op = 0;
60939e5308bSYangbo Lu 
61039e5308bSYangbo Lu 	if (ptp_cmd == IFH_REW_OP_TWO_STEP_PTP && clone) {
61139e5308bSYangbo Lu 		rew_op = ptp_cmd;
61239e5308bSYangbo Lu 		rew_op |= OCELOT_SKB_CB(clone)->ts_id << 3;
61339e5308bSYangbo Lu 	} else if (ptp_cmd == IFH_REW_OP_ORIGIN_PTP) {
61439e5308bSYangbo Lu 		rew_op = ptp_cmd;
61539e5308bSYangbo Lu 	}
61639e5308bSYangbo Lu 
61739e5308bSYangbo Lu 	return rew_op;
61839e5308bSYangbo Lu }
61939e5308bSYangbo Lu EXPORT_SYMBOL(ocelot_ptp_rew_op);
62039e5308bSYangbo Lu 
621fba01283SVladimir Oltean static bool ocelot_ptp_is_onestep_sync(struct sk_buff *skb,
622fba01283SVladimir Oltean 				       unsigned int ptp_class)
62339e5308bSYangbo Lu {
62439e5308bSYangbo Lu 	struct ptp_header *hdr;
62539e5308bSYangbo Lu 	u8 msgtype, twostep;
62639e5308bSYangbo Lu 
62739e5308bSYangbo Lu 	hdr = ptp_parse_header(skb, ptp_class);
62839e5308bSYangbo Lu 	if (!hdr)
62939e5308bSYangbo Lu 		return false;
63039e5308bSYangbo Lu 
63139e5308bSYangbo Lu 	msgtype = ptp_get_msgtype(hdr, ptp_class);
63239e5308bSYangbo Lu 	twostep = hdr->flag_field[0] & 0x2;
63339e5308bSYangbo Lu 
63439e5308bSYangbo Lu 	if (msgtype == PTP_MSGTYPE_SYNC && twostep == 0)
63539e5308bSYangbo Lu 		return true;
63639e5308bSYangbo Lu 
63739e5308bSYangbo Lu 	return false;
63839e5308bSYangbo Lu }
63939e5308bSYangbo Lu 
640682eaad9SYangbo Lu int ocelot_port_txtstamp_request(struct ocelot *ocelot, int port,
641682eaad9SYangbo Lu 				 struct sk_buff *skb,
642682eaad9SYangbo Lu 				 struct sk_buff **clone)
643682eaad9SYangbo Lu {
644682eaad9SYangbo Lu 	struct ocelot_port *ocelot_port = ocelot->ports[port];
645682eaad9SYangbo Lu 	u8 ptp_cmd = ocelot_port->ptp_cmd;
646fba01283SVladimir Oltean 	unsigned int ptp_class;
64752849bcfSVladimir Oltean 	int err;
648682eaad9SYangbo Lu 
649fba01283SVladimir Oltean 	/* Don't do anything if PTP timestamping not enabled */
650fba01283SVladimir Oltean 	if (!ptp_cmd)
651fba01283SVladimir Oltean 		return 0;
652fba01283SVladimir Oltean 
653fba01283SVladimir Oltean 	ptp_class = ptp_classify_raw(skb);
654fba01283SVladimir Oltean 	if (ptp_class == PTP_CLASS_NONE)
655fba01283SVladimir Oltean 		return -EINVAL;
656fba01283SVladimir Oltean 
65739e5308bSYangbo Lu 	/* Store ptp_cmd in OCELOT_SKB_CB(skb)->ptp_cmd */
65839e5308bSYangbo Lu 	if (ptp_cmd == IFH_REW_OP_ORIGIN_PTP) {
659fba01283SVladimir Oltean 		if (ocelot_ptp_is_onestep_sync(skb, ptp_class)) {
66039e5308bSYangbo Lu 			OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd;
66139e5308bSYangbo Lu 			return 0;
66239e5308bSYangbo Lu 		}
66339e5308bSYangbo Lu 
66439e5308bSYangbo Lu 		/* Fall back to two-step timestamping */
66539e5308bSYangbo Lu 		ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
66639e5308bSYangbo Lu 	}
66739e5308bSYangbo Lu 
668682eaad9SYangbo Lu 	if (ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
669682eaad9SYangbo Lu 		*clone = skb_clone_sk(skb);
670682eaad9SYangbo Lu 		if (!(*clone))
671682eaad9SYangbo Lu 			return -ENOMEM;
672682eaad9SYangbo Lu 
67352849bcfSVladimir Oltean 		err = ocelot_port_add_txtstamp_skb(ocelot, port, *clone);
67452849bcfSVladimir Oltean 		if (err)
67552849bcfSVladimir Oltean 			return err;
67652849bcfSVladimir Oltean 
67739e5308bSYangbo Lu 		OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd;
678*ebb4c6a9SVladimir Oltean 		OCELOT_SKB_CB(*clone)->ptp_class = ptp_class;
679682eaad9SYangbo Lu 	}
680682eaad9SYangbo Lu 
681682eaad9SYangbo Lu 	return 0;
682682eaad9SYangbo Lu }
683682eaad9SYangbo Lu EXPORT_SYMBOL(ocelot_port_txtstamp_request);
684400928bfSYangbo Lu 
685e23a7b3eSYangbo Lu static void ocelot_get_hwtimestamp(struct ocelot *ocelot,
686e23a7b3eSYangbo Lu 				   struct timespec64 *ts)
6874e3b0468SAntoine Tenart {
6884e3b0468SAntoine Tenart 	unsigned long flags;
6894e3b0468SAntoine Tenart 	u32 val;
6904e3b0468SAntoine Tenart 
6914e3b0468SAntoine Tenart 	spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
6924e3b0468SAntoine Tenart 
6934e3b0468SAntoine Tenart 	/* Read current PTP time to get seconds */
6944e3b0468SAntoine Tenart 	val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
6954e3b0468SAntoine Tenart 
6964e3b0468SAntoine Tenart 	val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
6974e3b0468SAntoine Tenart 	val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
6984e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
6994e3b0468SAntoine Tenart 	ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
7004e3b0468SAntoine Tenart 
7014e3b0468SAntoine Tenart 	/* Read packet HW timestamp from FIFO */
7024e3b0468SAntoine Tenart 	val = ocelot_read(ocelot, SYS_PTP_TXSTAMP);
7034e3b0468SAntoine Tenart 	ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val);
7044e3b0468SAntoine Tenart 
7054e3b0468SAntoine Tenart 	/* Sec has incremented since the ts was registered */
7064e3b0468SAntoine Tenart 	if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC))
7074e3b0468SAntoine Tenart 		ts->tv_sec--;
7084e3b0468SAntoine Tenart 
7094e3b0468SAntoine Tenart 	spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
7104e3b0468SAntoine Tenart }
711e23a7b3eSYangbo Lu 
712*ebb4c6a9SVladimir Oltean static bool ocelot_validate_ptp_skb(struct sk_buff *clone, u16 seqid)
713*ebb4c6a9SVladimir Oltean {
714*ebb4c6a9SVladimir Oltean 	struct ptp_header *hdr;
715*ebb4c6a9SVladimir Oltean 
716*ebb4c6a9SVladimir Oltean 	hdr = ptp_parse_header(clone, OCELOT_SKB_CB(clone)->ptp_class);
717*ebb4c6a9SVladimir Oltean 	if (WARN_ON(!hdr))
718*ebb4c6a9SVladimir Oltean 		return false;
719*ebb4c6a9SVladimir Oltean 
720*ebb4c6a9SVladimir Oltean 	return seqid == ntohs(hdr->sequence_id);
721*ebb4c6a9SVladimir Oltean }
722*ebb4c6a9SVladimir Oltean 
723e23a7b3eSYangbo Lu void ocelot_get_txtstamp(struct ocelot *ocelot)
724e23a7b3eSYangbo Lu {
725e23a7b3eSYangbo Lu 	int budget = OCELOT_PTP_QUEUE_SZ;
726e23a7b3eSYangbo Lu 
727e23a7b3eSYangbo Lu 	while (budget--) {
728b049da13SYangbo Lu 		struct sk_buff *skb, *skb_tmp, *skb_match = NULL;
729e23a7b3eSYangbo Lu 		struct skb_shared_hwtstamps shhwtstamps;
730*ebb4c6a9SVladimir Oltean 		u32 val, id, seqid, txport;
731e23a7b3eSYangbo Lu 		struct ocelot_port *port;
732e23a7b3eSYangbo Lu 		struct timespec64 ts;
733b049da13SYangbo Lu 		unsigned long flags;
734e23a7b3eSYangbo Lu 
735e23a7b3eSYangbo Lu 		val = ocelot_read(ocelot, SYS_PTP_STATUS);
736e23a7b3eSYangbo Lu 
737e23a7b3eSYangbo Lu 		/* Check if a timestamp can be retrieved */
738e23a7b3eSYangbo Lu 		if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD))
739e23a7b3eSYangbo Lu 			break;
740e23a7b3eSYangbo Lu 
741e23a7b3eSYangbo Lu 		WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL);
742e23a7b3eSYangbo Lu 
743e23a7b3eSYangbo Lu 		/* Retrieve the ts ID and Tx port */
744e23a7b3eSYangbo Lu 		id = SYS_PTP_STATUS_PTP_MESS_ID_X(val);
745e23a7b3eSYangbo Lu 		txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val);
746*ebb4c6a9SVladimir Oltean 		seqid = SYS_PTP_STATUS_PTP_MESS_SEQ_ID(val);
747e23a7b3eSYangbo Lu 
748e23a7b3eSYangbo Lu 		port = ocelot->ports[txport];
749e23a7b3eSYangbo Lu 
75052849bcfSVladimir Oltean 		spin_lock(&ocelot->ts_id_lock);
75152849bcfSVladimir Oltean 		port->ptp_skbs_in_flight--;
75252849bcfSVladimir Oltean 		ocelot->ptp_skbs_in_flight--;
75352849bcfSVladimir Oltean 		spin_unlock(&ocelot->ts_id_lock);
75452849bcfSVladimir Oltean 
75552849bcfSVladimir Oltean 		/* Retrieve its associated skb */
756*ebb4c6a9SVladimir Oltean try_again:
757b049da13SYangbo Lu 		spin_lock_irqsave(&port->tx_skbs.lock, flags);
758b049da13SYangbo Lu 
759b049da13SYangbo Lu 		skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
760c4b364ceSYangbo Lu 			if (OCELOT_SKB_CB(skb)->ts_id != id)
761e23a7b3eSYangbo Lu 				continue;
762b049da13SYangbo Lu 			__skb_unlink(skb, &port->tx_skbs);
763b049da13SYangbo Lu 			skb_match = skb;
764fc62c094SYangbo Lu 			break;
765e23a7b3eSYangbo Lu 		}
766e23a7b3eSYangbo Lu 
767b049da13SYangbo Lu 		spin_unlock_irqrestore(&port->tx_skbs.lock, flags);
768b049da13SYangbo Lu 
7699fde506eSVladimir Oltean 		if (WARN_ON(!skb_match))
7709fde506eSVladimir Oltean 			continue;
7719fde506eSVladimir Oltean 
772*ebb4c6a9SVladimir Oltean 		if (!ocelot_validate_ptp_skb(skb_match, seqid)) {
773*ebb4c6a9SVladimir Oltean 			dev_err_ratelimited(ocelot->dev,
774*ebb4c6a9SVladimir Oltean 					    "port %d received stale TX timestamp for seqid %d, discarding\n",
775*ebb4c6a9SVladimir Oltean 					    txport, seqid);
776*ebb4c6a9SVladimir Oltean 			dev_kfree_skb_any(skb);
777*ebb4c6a9SVladimir Oltean 			goto try_again;
778*ebb4c6a9SVladimir Oltean 		}
779*ebb4c6a9SVladimir Oltean 
7805fd82200Slaurent brando 		/* Get the h/w timestamp */
7815fd82200Slaurent brando 		ocelot_get_hwtimestamp(ocelot, &ts);
782e23a7b3eSYangbo Lu 
783e23a7b3eSYangbo Lu 		/* Set the timestamp into the skb */
784e23a7b3eSYangbo Lu 		memset(&shhwtstamps, 0, sizeof(shhwtstamps));
785e23a7b3eSYangbo Lu 		shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
786e2f9a8feSVladimir Oltean 		skb_complete_tx_timestamp(skb_match, &shhwtstamps);
7875fd82200Slaurent brando 
7885fd82200Slaurent brando 		/* Next ts */
7895fd82200Slaurent brando 		ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT);
790e23a7b3eSYangbo Lu 	}
791e23a7b3eSYangbo Lu }
792e23a7b3eSYangbo Lu EXPORT_SYMBOL(ocelot_get_txtstamp);
7934e3b0468SAntoine Tenart 
794924ee317SVladimir Oltean static int ocelot_rx_frame_word(struct ocelot *ocelot, u8 grp, bool ifh,
795924ee317SVladimir Oltean 				u32 *rval)
796924ee317SVladimir Oltean {
797924ee317SVladimir Oltean 	u32 bytes_valid, val;
798924ee317SVladimir Oltean 
799924ee317SVladimir Oltean 	val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
800924ee317SVladimir Oltean 	if (val == XTR_NOT_READY) {
801924ee317SVladimir Oltean 		if (ifh)
802924ee317SVladimir Oltean 			return -EIO;
803924ee317SVladimir Oltean 
804924ee317SVladimir Oltean 		do {
805924ee317SVladimir Oltean 			val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
806924ee317SVladimir Oltean 		} while (val == XTR_NOT_READY);
807924ee317SVladimir Oltean 	}
808924ee317SVladimir Oltean 
809924ee317SVladimir Oltean 	switch (val) {
810924ee317SVladimir Oltean 	case XTR_ABORT:
811924ee317SVladimir Oltean 		return -EIO;
812924ee317SVladimir Oltean 	case XTR_EOF_0:
813924ee317SVladimir Oltean 	case XTR_EOF_1:
814924ee317SVladimir Oltean 	case XTR_EOF_2:
815924ee317SVladimir Oltean 	case XTR_EOF_3:
816924ee317SVladimir Oltean 	case XTR_PRUNED:
817924ee317SVladimir Oltean 		bytes_valid = XTR_VALID_BYTES(val);
818924ee317SVladimir Oltean 		val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
819924ee317SVladimir Oltean 		if (val == XTR_ESCAPE)
820924ee317SVladimir Oltean 			*rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
821924ee317SVladimir Oltean 		else
822924ee317SVladimir Oltean 			*rval = val;
823924ee317SVladimir Oltean 
824924ee317SVladimir Oltean 		return bytes_valid;
825924ee317SVladimir Oltean 	case XTR_ESCAPE:
826924ee317SVladimir Oltean 		*rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
827924ee317SVladimir Oltean 
828924ee317SVladimir Oltean 		return 4;
829924ee317SVladimir Oltean 	default:
830924ee317SVladimir Oltean 		*rval = val;
831924ee317SVladimir Oltean 
832924ee317SVladimir Oltean 		return 4;
833924ee317SVladimir Oltean 	}
834924ee317SVladimir Oltean }
835924ee317SVladimir Oltean 
836924ee317SVladimir Oltean static int ocelot_xtr_poll_xfh(struct ocelot *ocelot, int grp, u32 *xfh)
837924ee317SVladimir Oltean {
838924ee317SVladimir Oltean 	int i, err = 0;
839924ee317SVladimir Oltean 
840924ee317SVladimir Oltean 	for (i = 0; i < OCELOT_TAG_LEN / 4; i++) {
841924ee317SVladimir Oltean 		err = ocelot_rx_frame_word(ocelot, grp, true, &xfh[i]);
842924ee317SVladimir Oltean 		if (err != 4)
843924ee317SVladimir Oltean 			return (err < 0) ? err : -EIO;
844924ee317SVladimir Oltean 	}
845924ee317SVladimir Oltean 
846924ee317SVladimir Oltean 	return 0;
847924ee317SVladimir Oltean }
848924ee317SVladimir Oltean 
849924ee317SVladimir Oltean int ocelot_xtr_poll_frame(struct ocelot *ocelot, int grp, struct sk_buff **nskb)
850924ee317SVladimir Oltean {
851924ee317SVladimir Oltean 	struct skb_shared_hwtstamps *shhwtstamps;
8522ed2c5f0SHoratiu Vultur 	u64 tod_in_ns, full_ts_in_ns;
853924ee317SVladimir Oltean 	u64 timestamp, src_port, len;
854924ee317SVladimir Oltean 	u32 xfh[OCELOT_TAG_LEN / 4];
855924ee317SVladimir Oltean 	struct net_device *dev;
856924ee317SVladimir Oltean 	struct timespec64 ts;
857924ee317SVladimir Oltean 	struct sk_buff *skb;
858924ee317SVladimir Oltean 	int sz, buf_len;
859924ee317SVladimir Oltean 	u32 val, *buf;
860924ee317SVladimir Oltean 	int err;
861924ee317SVladimir Oltean 
862924ee317SVladimir Oltean 	err = ocelot_xtr_poll_xfh(ocelot, grp, xfh);
863924ee317SVladimir Oltean 	if (err)
864924ee317SVladimir Oltean 		return err;
865924ee317SVladimir Oltean 
866924ee317SVladimir Oltean 	ocelot_xfh_get_src_port(xfh, &src_port);
867924ee317SVladimir Oltean 	ocelot_xfh_get_len(xfh, &len);
868924ee317SVladimir Oltean 	ocelot_xfh_get_rew_val(xfh, &timestamp);
869924ee317SVladimir Oltean 
870924ee317SVladimir Oltean 	if (WARN_ON(src_port >= ocelot->num_phys_ports))
871924ee317SVladimir Oltean 		return -EINVAL;
872924ee317SVladimir Oltean 
873924ee317SVladimir Oltean 	dev = ocelot->ops->port_to_netdev(ocelot, src_port);
874924ee317SVladimir Oltean 	if (!dev)
875924ee317SVladimir Oltean 		return -EINVAL;
876924ee317SVladimir Oltean 
877924ee317SVladimir Oltean 	skb = netdev_alloc_skb(dev, len);
878924ee317SVladimir Oltean 	if (unlikely(!skb)) {
879924ee317SVladimir Oltean 		netdev_err(dev, "Unable to allocate sk_buff\n");
880924ee317SVladimir Oltean 		return -ENOMEM;
881924ee317SVladimir Oltean 	}
882924ee317SVladimir Oltean 
883924ee317SVladimir Oltean 	buf_len = len - ETH_FCS_LEN;
884924ee317SVladimir Oltean 	buf = (u32 *)skb_put(skb, buf_len);
885924ee317SVladimir Oltean 
886924ee317SVladimir Oltean 	len = 0;
887924ee317SVladimir Oltean 	do {
888924ee317SVladimir Oltean 		sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
889924ee317SVladimir Oltean 		if (sz < 0) {
890924ee317SVladimir Oltean 			err = sz;
891924ee317SVladimir Oltean 			goto out_free_skb;
892924ee317SVladimir Oltean 		}
893924ee317SVladimir Oltean 		*buf++ = val;
894924ee317SVladimir Oltean 		len += sz;
895924ee317SVladimir Oltean 	} while (len < buf_len);
896924ee317SVladimir Oltean 
897924ee317SVladimir Oltean 	/* Read the FCS */
898924ee317SVladimir Oltean 	sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
899924ee317SVladimir Oltean 	if (sz < 0) {
900924ee317SVladimir Oltean 		err = sz;
901924ee317SVladimir Oltean 		goto out_free_skb;
902924ee317SVladimir Oltean 	}
903924ee317SVladimir Oltean 
904924ee317SVladimir Oltean 	/* Update the statistics if part of the FCS was read before */
905924ee317SVladimir Oltean 	len -= ETH_FCS_LEN - sz;
906924ee317SVladimir Oltean 
907924ee317SVladimir Oltean 	if (unlikely(dev->features & NETIF_F_RXFCS)) {
908924ee317SVladimir Oltean 		buf = (u32 *)skb_put(skb, ETH_FCS_LEN);
909924ee317SVladimir Oltean 		*buf = val;
910924ee317SVladimir Oltean 	}
911924ee317SVladimir Oltean 
912924ee317SVladimir Oltean 	if (ocelot->ptp) {
913924ee317SVladimir Oltean 		ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
914924ee317SVladimir Oltean 
915924ee317SVladimir Oltean 		tod_in_ns = ktime_set(ts.tv_sec, ts.tv_nsec);
916924ee317SVladimir Oltean 		if ((tod_in_ns & 0xffffffff) < timestamp)
917924ee317SVladimir Oltean 			full_ts_in_ns = (((tod_in_ns >> 32) - 1) << 32) |
918924ee317SVladimir Oltean 					timestamp;
919924ee317SVladimir Oltean 		else
920924ee317SVladimir Oltean 			full_ts_in_ns = (tod_in_ns & GENMASK_ULL(63, 32)) |
921924ee317SVladimir Oltean 					timestamp;
922924ee317SVladimir Oltean 
923924ee317SVladimir Oltean 		shhwtstamps = skb_hwtstamps(skb);
924924ee317SVladimir Oltean 		memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
925924ee317SVladimir Oltean 		shhwtstamps->hwtstamp = full_ts_in_ns;
926924ee317SVladimir Oltean 	}
927924ee317SVladimir Oltean 
928924ee317SVladimir Oltean 	/* Everything we see on an interface that is in the HW bridge
929924ee317SVladimir Oltean 	 * has already been forwarded.
930924ee317SVladimir Oltean 	 */
931df291e54SVladimir Oltean 	if (ocelot->ports[src_port]->bridge)
932924ee317SVladimir Oltean 		skb->offload_fwd_mark = 1;
933924ee317SVladimir Oltean 
934924ee317SVladimir Oltean 	skb->protocol = eth_type_trans(skb, dev);
935d8ea7ff3SHoratiu Vultur 
936924ee317SVladimir Oltean 	*nskb = skb;
937924ee317SVladimir Oltean 
938924ee317SVladimir Oltean 	return 0;
939924ee317SVladimir Oltean 
940924ee317SVladimir Oltean out_free_skb:
941924ee317SVladimir Oltean 	kfree_skb(skb);
942924ee317SVladimir Oltean 	return err;
943924ee317SVladimir Oltean }
944924ee317SVladimir Oltean EXPORT_SYMBOL(ocelot_xtr_poll_frame);
945924ee317SVladimir Oltean 
946137ffbc4SVladimir Oltean bool ocelot_can_inject(struct ocelot *ocelot, int grp)
947137ffbc4SVladimir Oltean {
948137ffbc4SVladimir Oltean 	u32 val = ocelot_read(ocelot, QS_INJ_STATUS);
949137ffbc4SVladimir Oltean 
950137ffbc4SVladimir Oltean 	if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp))))
951137ffbc4SVladimir Oltean 		return false;
952137ffbc4SVladimir Oltean 	if (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp)))
953137ffbc4SVladimir Oltean 		return false;
954137ffbc4SVladimir Oltean 
955137ffbc4SVladimir Oltean 	return true;
956137ffbc4SVladimir Oltean }
957137ffbc4SVladimir Oltean EXPORT_SYMBOL(ocelot_can_inject);
958137ffbc4SVladimir Oltean 
959137ffbc4SVladimir Oltean void ocelot_port_inject_frame(struct ocelot *ocelot, int port, int grp,
960137ffbc4SVladimir Oltean 			      u32 rew_op, struct sk_buff *skb)
961137ffbc4SVladimir Oltean {
96240d3f295SVladimir Oltean 	u32 ifh[OCELOT_TAG_LEN / 4] = {0};
963137ffbc4SVladimir Oltean 	unsigned int i, count, last;
964137ffbc4SVladimir Oltean 
965137ffbc4SVladimir Oltean 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
966137ffbc4SVladimir Oltean 			 QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp);
967137ffbc4SVladimir Oltean 
96840d3f295SVladimir Oltean 	ocelot_ifh_set_bypass(ifh, 1);
9691f778d50SVladimir Oltean 	ocelot_ifh_set_dest(ifh, BIT_ULL(port));
97040d3f295SVladimir Oltean 	ocelot_ifh_set_tag_type(ifh, IFH_TAG_TYPE_C);
97140d3f295SVladimir Oltean 	ocelot_ifh_set_vid(ifh, skb_vlan_tag_get(skb));
97240d3f295SVladimir Oltean 	ocelot_ifh_set_rew_op(ifh, rew_op);
973137ffbc4SVladimir Oltean 
974137ffbc4SVladimir Oltean 	for (i = 0; i < OCELOT_TAG_LEN / 4; i++)
97540d3f295SVladimir Oltean 		ocelot_write_rix(ocelot, ifh[i], QS_INJ_WR, grp);
976137ffbc4SVladimir Oltean 
977137ffbc4SVladimir Oltean 	count = DIV_ROUND_UP(skb->len, 4);
978137ffbc4SVladimir Oltean 	last = skb->len % 4;
979137ffbc4SVladimir Oltean 	for (i = 0; i < count; i++)
980137ffbc4SVladimir Oltean 		ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp);
981137ffbc4SVladimir Oltean 
982137ffbc4SVladimir Oltean 	/* Add padding */
983137ffbc4SVladimir Oltean 	while (i < (OCELOT_BUFFER_CELL_SZ / 4)) {
984137ffbc4SVladimir Oltean 		ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
985137ffbc4SVladimir Oltean 		i++;
986137ffbc4SVladimir Oltean 	}
987137ffbc4SVladimir Oltean 
988137ffbc4SVladimir Oltean 	/* Indicate EOF and valid bytes in last word */
989137ffbc4SVladimir Oltean 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
990137ffbc4SVladimir Oltean 			 QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) |
991137ffbc4SVladimir Oltean 			 QS_INJ_CTRL_EOF,
992137ffbc4SVladimir Oltean 			 QS_INJ_CTRL, grp);
993137ffbc4SVladimir Oltean 
994137ffbc4SVladimir Oltean 	/* Add dummy CRC */
995137ffbc4SVladimir Oltean 	ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
996137ffbc4SVladimir Oltean 	skb_tx_timestamp(skb);
997137ffbc4SVladimir Oltean 
998137ffbc4SVladimir Oltean 	skb->dev->stats.tx_packets++;
999137ffbc4SVladimir Oltean 	skb->dev->stats.tx_bytes += skb->len;
1000137ffbc4SVladimir Oltean }
1001137ffbc4SVladimir Oltean EXPORT_SYMBOL(ocelot_port_inject_frame);
1002137ffbc4SVladimir Oltean 
10030a6f17c6SVladimir Oltean void ocelot_drain_cpu_queue(struct ocelot *ocelot, int grp)
10040a6f17c6SVladimir Oltean {
10050a6f17c6SVladimir Oltean 	while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp))
10060a6f17c6SVladimir Oltean 		ocelot_read_rix(ocelot, QS_XTR_RD, grp);
10070a6f17c6SVladimir Oltean }
10080a6f17c6SVladimir Oltean EXPORT_SYMBOL(ocelot_drain_cpu_queue);
10090a6f17c6SVladimir Oltean 
10105e256365SVladimir Oltean int ocelot_fdb_add(struct ocelot *ocelot, int port,
101187b0f983SVladimir Oltean 		   const unsigned char *addr, u16 vid)
1012a556c76aSAlexandre Belloni {
1013471beb11SVladimir Oltean 	int pgid = port;
1014471beb11SVladimir Oltean 
1015471beb11SVladimir Oltean 	if (port == ocelot->npi)
1016471beb11SVladimir Oltean 		pgid = PGID_CPU;
1017a556c76aSAlexandre Belloni 
1018471beb11SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid, addr, vid, ENTRYTYPE_LOCKED);
1019a556c76aSAlexandre Belloni }
10205e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_add);
1021a556c76aSAlexandre Belloni 
10225e256365SVladimir Oltean int ocelot_fdb_del(struct ocelot *ocelot, int port,
1023531ee1a6SVladimir Oltean 		   const unsigned char *addr, u16 vid)
1024531ee1a6SVladimir Oltean {
1025531ee1a6SVladimir Oltean 	return ocelot_mact_forget(ocelot, addr, vid);
1026531ee1a6SVladimir Oltean }
10275e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_del);
1028531ee1a6SVladimir Oltean 
10299c90eea3SVladimir Oltean int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid,
1030531ee1a6SVladimir Oltean 			    bool is_static, void *data)
1031a556c76aSAlexandre Belloni {
1032531ee1a6SVladimir Oltean 	struct ocelot_dump_ctx *dump = data;
1033a556c76aSAlexandre Belloni 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
1034a556c76aSAlexandre Belloni 	u32 seq = dump->cb->nlh->nlmsg_seq;
1035a556c76aSAlexandre Belloni 	struct nlmsghdr *nlh;
1036a556c76aSAlexandre Belloni 	struct ndmsg *ndm;
1037a556c76aSAlexandre Belloni 
1038a556c76aSAlexandre Belloni 	if (dump->idx < dump->cb->args[2])
1039a556c76aSAlexandre Belloni 		goto skip;
1040a556c76aSAlexandre Belloni 
1041a556c76aSAlexandre Belloni 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
1042a556c76aSAlexandre Belloni 			sizeof(*ndm), NLM_F_MULTI);
1043a556c76aSAlexandre Belloni 	if (!nlh)
1044a556c76aSAlexandre Belloni 		return -EMSGSIZE;
1045a556c76aSAlexandre Belloni 
1046a556c76aSAlexandre Belloni 	ndm = nlmsg_data(nlh);
1047a556c76aSAlexandre Belloni 	ndm->ndm_family  = AF_BRIDGE;
1048a556c76aSAlexandre Belloni 	ndm->ndm_pad1    = 0;
1049a556c76aSAlexandre Belloni 	ndm->ndm_pad2    = 0;
1050a556c76aSAlexandre Belloni 	ndm->ndm_flags   = NTF_SELF;
1051a556c76aSAlexandre Belloni 	ndm->ndm_type    = 0;
1052a556c76aSAlexandre Belloni 	ndm->ndm_ifindex = dump->dev->ifindex;
1053531ee1a6SVladimir Oltean 	ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
1054a556c76aSAlexandre Belloni 
1055531ee1a6SVladimir Oltean 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
1056a556c76aSAlexandre Belloni 		goto nla_put_failure;
1057a556c76aSAlexandre Belloni 
1058531ee1a6SVladimir Oltean 	if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
1059a556c76aSAlexandre Belloni 		goto nla_put_failure;
1060a556c76aSAlexandre Belloni 
1061a556c76aSAlexandre Belloni 	nlmsg_end(dump->skb, nlh);
1062a556c76aSAlexandre Belloni 
1063a556c76aSAlexandre Belloni skip:
1064a556c76aSAlexandre Belloni 	dump->idx++;
1065a556c76aSAlexandre Belloni 	return 0;
1066a556c76aSAlexandre Belloni 
1067a556c76aSAlexandre Belloni nla_put_failure:
1068a556c76aSAlexandre Belloni 	nlmsg_cancel(dump->skb, nlh);
1069a556c76aSAlexandre Belloni 	return -EMSGSIZE;
1070a556c76aSAlexandre Belloni }
10719c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_fdb_do_dump);
1072a556c76aSAlexandre Belloni 
1073531ee1a6SVladimir Oltean static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col,
1074a556c76aSAlexandre Belloni 			    struct ocelot_mact_entry *entry)
1075a556c76aSAlexandre Belloni {
1076a556c76aSAlexandre Belloni 	u32 val, dst, macl, mach;
1077531ee1a6SVladimir Oltean 	char mac[ETH_ALEN];
1078a556c76aSAlexandre Belloni 
1079a556c76aSAlexandre Belloni 	/* Set row and column to read from */
1080a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
1081a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
1082a556c76aSAlexandre Belloni 
1083a556c76aSAlexandre Belloni 	/* Issue a read command */
1084a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
1085a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
1086a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
1087a556c76aSAlexandre Belloni 
1088a556c76aSAlexandre Belloni 	if (ocelot_mact_wait_for_completion(ocelot))
1089a556c76aSAlexandre Belloni 		return -ETIMEDOUT;
1090a556c76aSAlexandre Belloni 
1091a556c76aSAlexandre Belloni 	/* Read the entry flags */
1092a556c76aSAlexandre Belloni 	val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
1093a556c76aSAlexandre Belloni 	if (!(val & ANA_TABLES_MACACCESS_VALID))
1094a556c76aSAlexandre Belloni 		return -EINVAL;
1095a556c76aSAlexandre Belloni 
1096a556c76aSAlexandre Belloni 	/* If the entry read has another port configured as its destination,
1097a556c76aSAlexandre Belloni 	 * do not report it.
1098a556c76aSAlexandre Belloni 	 */
1099a556c76aSAlexandre Belloni 	dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
1100531ee1a6SVladimir Oltean 	if (dst != port)
1101a556c76aSAlexandre Belloni 		return -EINVAL;
1102a556c76aSAlexandre Belloni 
1103a556c76aSAlexandre Belloni 	/* Get the entry's MAC address and VLAN id */
1104a556c76aSAlexandre Belloni 	macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
1105a556c76aSAlexandre Belloni 	mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
1106a556c76aSAlexandre Belloni 
1107a556c76aSAlexandre Belloni 	mac[0] = (mach >> 8)  & 0xff;
1108a556c76aSAlexandre Belloni 	mac[1] = (mach >> 0)  & 0xff;
1109a556c76aSAlexandre Belloni 	mac[2] = (macl >> 24) & 0xff;
1110a556c76aSAlexandre Belloni 	mac[3] = (macl >> 16) & 0xff;
1111a556c76aSAlexandre Belloni 	mac[4] = (macl >> 8)  & 0xff;
1112a556c76aSAlexandre Belloni 	mac[5] = (macl >> 0)  & 0xff;
1113a556c76aSAlexandre Belloni 
1114a556c76aSAlexandre Belloni 	entry->vid = (mach >> 16) & 0xfff;
1115a556c76aSAlexandre Belloni 	ether_addr_copy(entry->mac, mac);
1116a556c76aSAlexandre Belloni 
1117a556c76aSAlexandre Belloni 	return 0;
1118a556c76aSAlexandre Belloni }
1119a556c76aSAlexandre Belloni 
11205e256365SVladimir Oltean int ocelot_fdb_dump(struct ocelot *ocelot, int port,
1121531ee1a6SVladimir Oltean 		    dsa_fdb_dump_cb_t *cb, void *data)
1122a556c76aSAlexandre Belloni {
1123531ee1a6SVladimir Oltean 	int i, j;
1124a556c76aSAlexandre Belloni 
112521ce7f3eSVladimir Oltean 	/* Loop through all the mac tables entries. */
112621ce7f3eSVladimir Oltean 	for (i = 0; i < ocelot->num_mact_rows; i++) {
1127a556c76aSAlexandre Belloni 		for (j = 0; j < 4; j++) {
1128531ee1a6SVladimir Oltean 			struct ocelot_mact_entry entry;
1129531ee1a6SVladimir Oltean 			bool is_static;
1130531ee1a6SVladimir Oltean 			int ret;
1131531ee1a6SVladimir Oltean 
1132531ee1a6SVladimir Oltean 			ret = ocelot_mact_read(ocelot, port, i, j, &entry);
1133a556c76aSAlexandre Belloni 			/* If the entry is invalid (wrong port, invalid...),
1134a556c76aSAlexandre Belloni 			 * skip it.
1135a556c76aSAlexandre Belloni 			 */
1136a556c76aSAlexandre Belloni 			if (ret == -EINVAL)
1137a556c76aSAlexandre Belloni 				continue;
1138a556c76aSAlexandre Belloni 			else if (ret)
1139531ee1a6SVladimir Oltean 				return ret;
1140a556c76aSAlexandre Belloni 
1141531ee1a6SVladimir Oltean 			is_static = (entry.type == ENTRYTYPE_LOCKED);
1142531ee1a6SVladimir Oltean 
1143531ee1a6SVladimir Oltean 			ret = cb(entry.mac, entry.vid, is_static, data);
1144a556c76aSAlexandre Belloni 			if (ret)
1145531ee1a6SVladimir Oltean 				return ret;
1146a556c76aSAlexandre Belloni 		}
1147a556c76aSAlexandre Belloni 	}
1148a556c76aSAlexandre Belloni 
1149531ee1a6SVladimir Oltean 	return 0;
1150531ee1a6SVladimir Oltean }
11515e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_dump);
1152531ee1a6SVladimir Oltean 
1153f145922dSYangbo Lu int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr)
11544e3b0468SAntoine Tenart {
11554e3b0468SAntoine Tenart 	return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config,
11564e3b0468SAntoine Tenart 			    sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0;
11574e3b0468SAntoine Tenart }
1158f145922dSYangbo Lu EXPORT_SYMBOL(ocelot_hwstamp_get);
11594e3b0468SAntoine Tenart 
1160f145922dSYangbo Lu int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr)
11614e3b0468SAntoine Tenart {
1162306fd44bSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
11634e3b0468SAntoine Tenart 	struct hwtstamp_config cfg;
11644e3b0468SAntoine Tenart 
11654e3b0468SAntoine Tenart 	if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
11664e3b0468SAntoine Tenart 		return -EFAULT;
11674e3b0468SAntoine Tenart 
11684e3b0468SAntoine Tenart 	/* reserved for future extensions */
11694e3b0468SAntoine Tenart 	if (cfg.flags)
11704e3b0468SAntoine Tenart 		return -EINVAL;
11714e3b0468SAntoine Tenart 
11724e3b0468SAntoine Tenart 	/* Tx type sanity check */
11734e3b0468SAntoine Tenart 	switch (cfg.tx_type) {
11744e3b0468SAntoine Tenart 	case HWTSTAMP_TX_ON:
1175306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
11764e3b0468SAntoine Tenart 		break;
11774e3b0468SAntoine Tenart 	case HWTSTAMP_TX_ONESTEP_SYNC:
11784e3b0468SAntoine Tenart 		/* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
11794e3b0468SAntoine Tenart 		 * need to update the origin time.
11804e3b0468SAntoine Tenart 		 */
1181306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP;
11824e3b0468SAntoine Tenart 		break;
11834e3b0468SAntoine Tenart 	case HWTSTAMP_TX_OFF:
1184306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = 0;
11854e3b0468SAntoine Tenart 		break;
11864e3b0468SAntoine Tenart 	default:
11874e3b0468SAntoine Tenart 		return -ERANGE;
11884e3b0468SAntoine Tenart 	}
11894e3b0468SAntoine Tenart 
11904e3b0468SAntoine Tenart 	mutex_lock(&ocelot->ptp_lock);
11914e3b0468SAntoine Tenart 
11924e3b0468SAntoine Tenart 	switch (cfg.rx_filter) {
11934e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_NONE:
11944e3b0468SAntoine Tenart 		break;
11954e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_ALL:
11964e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_SOME:
11974e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
11984e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
11994e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
12004e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_NTP_ALL:
12014e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
12024e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
12034e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
12044e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
12054e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
12064e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
12074e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
12084e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
12094e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
12104e3b0468SAntoine Tenart 		cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
12114e3b0468SAntoine Tenart 		break;
12124e3b0468SAntoine Tenart 	default:
12134e3b0468SAntoine Tenart 		mutex_unlock(&ocelot->ptp_lock);
12144e3b0468SAntoine Tenart 		return -ERANGE;
12154e3b0468SAntoine Tenart 	}
12164e3b0468SAntoine Tenart 
12174e3b0468SAntoine Tenart 	/* Commit back the result & save it */
12184e3b0468SAntoine Tenart 	memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg));
12194e3b0468SAntoine Tenart 	mutex_unlock(&ocelot->ptp_lock);
12204e3b0468SAntoine Tenart 
12214e3b0468SAntoine Tenart 	return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
12224e3b0468SAntoine Tenart }
1223f145922dSYangbo Lu EXPORT_SYMBOL(ocelot_hwstamp_set);
12244e3b0468SAntoine Tenart 
12255e256365SVladimir Oltean void ocelot_get_strings(struct ocelot *ocelot, int port, u32 sset, u8 *data)
1226a556c76aSAlexandre Belloni {
1227a556c76aSAlexandre Belloni 	int i;
1228a556c76aSAlexandre Belloni 
1229a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
1230a556c76aSAlexandre Belloni 		return;
1231a556c76aSAlexandre Belloni 
1232a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
1233a556c76aSAlexandre Belloni 		memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
1234a556c76aSAlexandre Belloni 		       ETH_GSTRING_LEN);
1235a556c76aSAlexandre Belloni }
12365e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_strings);
1237a556c76aSAlexandre Belloni 
12381e1caa97SClaudiu Manoil static void ocelot_update_stats(struct ocelot *ocelot)
1239a556c76aSAlexandre Belloni {
1240a556c76aSAlexandre Belloni 	int i, j;
1241a556c76aSAlexandre Belloni 
1242a556c76aSAlexandre Belloni 	mutex_lock(&ocelot->stats_lock);
1243a556c76aSAlexandre Belloni 
1244a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_phys_ports; i++) {
1245a556c76aSAlexandre Belloni 		/* Configure the port to read the stats from */
1246a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG);
1247a556c76aSAlexandre Belloni 
1248a556c76aSAlexandre Belloni 		for (j = 0; j < ocelot->num_stats; j++) {
1249a556c76aSAlexandre Belloni 			u32 val;
1250a556c76aSAlexandre Belloni 			unsigned int idx = i * ocelot->num_stats + j;
1251a556c76aSAlexandre Belloni 
1252a556c76aSAlexandre Belloni 			val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
1253a556c76aSAlexandre Belloni 					      ocelot->stats_layout[j].offset);
1254a556c76aSAlexandre Belloni 
1255a556c76aSAlexandre Belloni 			if (val < (ocelot->stats[idx] & U32_MAX))
1256a556c76aSAlexandre Belloni 				ocelot->stats[idx] += (u64)1 << 32;
1257a556c76aSAlexandre Belloni 
1258a556c76aSAlexandre Belloni 			ocelot->stats[idx] = (ocelot->stats[idx] &
1259a556c76aSAlexandre Belloni 					      ~(u64)U32_MAX) + val;
1260a556c76aSAlexandre Belloni 		}
1261a556c76aSAlexandre Belloni 	}
1262a556c76aSAlexandre Belloni 
12631e1caa97SClaudiu Manoil 	mutex_unlock(&ocelot->stats_lock);
12641e1caa97SClaudiu Manoil }
12651e1caa97SClaudiu Manoil 
12661e1caa97SClaudiu Manoil static void ocelot_check_stats_work(struct work_struct *work)
12671e1caa97SClaudiu Manoil {
12681e1caa97SClaudiu Manoil 	struct delayed_work *del_work = to_delayed_work(work);
12691e1caa97SClaudiu Manoil 	struct ocelot *ocelot = container_of(del_work, struct ocelot,
12701e1caa97SClaudiu Manoil 					     stats_work);
12711e1caa97SClaudiu Manoil 
12721e1caa97SClaudiu Manoil 	ocelot_update_stats(ocelot);
12731e1caa97SClaudiu Manoil 
1274a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
1275a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
1276a556c76aSAlexandre Belloni }
1277a556c76aSAlexandre Belloni 
12785e256365SVladimir Oltean void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data)
1279a556c76aSAlexandre Belloni {
1280a556c76aSAlexandre Belloni 	int i;
1281a556c76aSAlexandre Belloni 
1282a556c76aSAlexandre Belloni 	/* check and update now */
12831e1caa97SClaudiu Manoil 	ocelot_update_stats(ocelot);
1284a556c76aSAlexandre Belloni 
1285a556c76aSAlexandre Belloni 	/* Copy all counters */
1286a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
1287004d44f6SVladimir Oltean 		*data++ = ocelot->stats[port * ocelot->num_stats + i];
1288a556c76aSAlexandre Belloni }
12895e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_ethtool_stats);
1290a556c76aSAlexandre Belloni 
12915e256365SVladimir Oltean int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset)
1292c7282d38SVladimir Oltean {
1293a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
1294a556c76aSAlexandre Belloni 		return -EOPNOTSUPP;
1295c7282d38SVladimir Oltean 
1296a556c76aSAlexandre Belloni 	return ocelot->num_stats;
1297a556c76aSAlexandre Belloni }
12985e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_sset_count);
1299a556c76aSAlexandre Belloni 
13005e256365SVladimir Oltean int ocelot_get_ts_info(struct ocelot *ocelot, int port,
1301c7282d38SVladimir Oltean 		       struct ethtool_ts_info *info)
1302c7282d38SVladimir Oltean {
13034e3b0468SAntoine Tenart 	info->phc_index = ocelot->ptp_clock ?
13044e3b0468SAntoine Tenart 			  ptp_clock_index(ocelot->ptp_clock) : -1;
1305d2b09a8eSYangbo Lu 	if (info->phc_index == -1) {
1306d2b09a8eSYangbo Lu 		info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
1307d2b09a8eSYangbo Lu 					 SOF_TIMESTAMPING_RX_SOFTWARE |
1308d2b09a8eSYangbo Lu 					 SOF_TIMESTAMPING_SOFTWARE;
1309d2b09a8eSYangbo Lu 		return 0;
1310d2b09a8eSYangbo Lu 	}
13114e3b0468SAntoine Tenart 	info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
13124e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RX_SOFTWARE |
13134e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_SOFTWARE |
13144e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_TX_HARDWARE |
13154e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RX_HARDWARE |
13164e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RAW_HARDWARE;
13174e3b0468SAntoine Tenart 	info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) |
13184e3b0468SAntoine Tenart 			 BIT(HWTSTAMP_TX_ONESTEP_SYNC);
13194e3b0468SAntoine Tenart 	info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL);
13204e3b0468SAntoine Tenart 
13214e3b0468SAntoine Tenart 	return 0;
13224e3b0468SAntoine Tenart }
13235e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_ts_info);
13244e3b0468SAntoine Tenart 
132523ca3b72SVladimir Oltean static u32 ocelot_get_bond_mask(struct ocelot *ocelot, struct net_device *bond,
132623ca3b72SVladimir Oltean 				bool only_active_ports)
1327b80af659SVladimir Oltean {
1328b80af659SVladimir Oltean 	u32 mask = 0;
1329b80af659SVladimir Oltean 	int port;
1330b80af659SVladimir Oltean 
1331b80af659SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1332b80af659SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1333b80af659SVladimir Oltean 
1334b80af659SVladimir Oltean 		if (!ocelot_port)
1335b80af659SVladimir Oltean 			continue;
1336b80af659SVladimir Oltean 
133723ca3b72SVladimir Oltean 		if (ocelot_port->bond == bond) {
133823ca3b72SVladimir Oltean 			if (only_active_ports && !ocelot_port->lag_tx_active)
133923ca3b72SVladimir Oltean 				continue;
134023ca3b72SVladimir Oltean 
1341b80af659SVladimir Oltean 			mask |= BIT(port);
1342b80af659SVladimir Oltean 		}
134323ca3b72SVladimir Oltean 	}
1344b80af659SVladimir Oltean 
1345b80af659SVladimir Oltean 	return mask;
1346b80af659SVladimir Oltean }
1347b80af659SVladimir Oltean 
1348acc64f52SVladimir Oltean static u32 ocelot_get_bridge_fwd_mask(struct ocelot *ocelot, int src_port,
1349df291e54SVladimir Oltean 				      struct net_device *bridge)
1350df291e54SVladimir Oltean {
1351acc64f52SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[src_port];
1352df291e54SVladimir Oltean 	u32 mask = 0;
1353df291e54SVladimir Oltean 	int port;
1354df291e54SVladimir Oltean 
1355acc64f52SVladimir Oltean 	if (!ocelot_port || ocelot_port->bridge != bridge ||
1356acc64f52SVladimir Oltean 	    ocelot_port->stp_state != BR_STATE_FORWARDING)
1357acc64f52SVladimir Oltean 		return 0;
1358acc64f52SVladimir Oltean 
1359df291e54SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1360acc64f52SVladimir Oltean 		ocelot_port = ocelot->ports[port];
1361df291e54SVladimir Oltean 
1362df291e54SVladimir Oltean 		if (!ocelot_port)
1363df291e54SVladimir Oltean 			continue;
1364df291e54SVladimir Oltean 
1365df291e54SVladimir Oltean 		if (ocelot_port->stp_state == BR_STATE_FORWARDING &&
1366df291e54SVladimir Oltean 		    ocelot_port->bridge == bridge)
1367df291e54SVladimir Oltean 			mask |= BIT(port);
1368df291e54SVladimir Oltean 	}
1369df291e54SVladimir Oltean 
1370df291e54SVladimir Oltean 	return mask;
1371df291e54SVladimir Oltean }
1372df291e54SVladimir Oltean 
1373e21268efSVladimir Oltean static u32 ocelot_get_dsa_8021q_cpu_mask(struct ocelot *ocelot)
13749b521250SVladimir Oltean {
1375e21268efSVladimir Oltean 	u32 mask = 0;
13769b521250SVladimir Oltean 	int port;
13779b521250SVladimir Oltean 
1378e21268efSVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1379e21268efSVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1380e21268efSVladimir Oltean 
1381e21268efSVladimir Oltean 		if (!ocelot_port)
1382e21268efSVladimir Oltean 			continue;
1383e21268efSVladimir Oltean 
1384e21268efSVladimir Oltean 		if (ocelot_port->is_dsa_8021q_cpu)
1385e21268efSVladimir Oltean 			mask |= BIT(port);
1386e21268efSVladimir Oltean 	}
1387e21268efSVladimir Oltean 
1388e21268efSVladimir Oltean 	return mask;
1389e21268efSVladimir Oltean }
1390e21268efSVladimir Oltean 
1391e21268efSVladimir Oltean void ocelot_apply_bridge_fwd_mask(struct ocelot *ocelot)
1392e21268efSVladimir Oltean {
1393e21268efSVladimir Oltean 	unsigned long cpu_fwd_mask;
1394e21268efSVladimir Oltean 	int port;
1395e21268efSVladimir Oltean 
1396e21268efSVladimir Oltean 	/* If a DSA tag_8021q CPU exists, it needs to be included in the
1397e21268efSVladimir Oltean 	 * regular forwarding path of the front ports regardless of whether
1398e21268efSVladimir Oltean 	 * those are bridged or standalone.
1399e21268efSVladimir Oltean 	 * If DSA tag_8021q is not used, this returns 0, which is fine because
1400e21268efSVladimir Oltean 	 * the hardware-based CPU port module can be a destination for packets
1401e21268efSVladimir Oltean 	 * even if it isn't part of PGID_SRC.
1402e21268efSVladimir Oltean 	 */
1403e21268efSVladimir Oltean 	cpu_fwd_mask = ocelot_get_dsa_8021q_cpu_mask(ocelot);
1404e21268efSVladimir Oltean 
14059b521250SVladimir Oltean 	/* Apply FWD mask. The loop is needed to add/remove the current port as
14069b521250SVladimir Oltean 	 * a source for the other ports.
14079b521250SVladimir Oltean 	 */
14089b521250SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1409e21268efSVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1410e21268efSVladimir Oltean 		unsigned long mask;
1411e21268efSVladimir Oltean 
1412e21268efSVladimir Oltean 		if (!ocelot_port) {
1413e21268efSVladimir Oltean 			/* Unused ports can't send anywhere */
1414e21268efSVladimir Oltean 			mask = 0;
1415e21268efSVladimir Oltean 		} else if (ocelot_port->is_dsa_8021q_cpu) {
1416e21268efSVladimir Oltean 			/* The DSA tag_8021q CPU ports need to be able to
1417e21268efSVladimir Oltean 			 * forward packets to all other ports except for
1418e21268efSVladimir Oltean 			 * themselves
1419e21268efSVladimir Oltean 			 */
1420e21268efSVladimir Oltean 			mask = GENMASK(ocelot->num_phys_ports - 1, 0);
1421e21268efSVladimir Oltean 			mask &= ~cpu_fwd_mask;
1422df291e54SVladimir Oltean 		} else if (ocelot_port->bridge) {
1423df291e54SVladimir Oltean 			struct net_device *bridge = ocelot_port->bridge;
1424528d3f19SVladimir Oltean 			struct net_device *bond = ocelot_port->bond;
14259b521250SVladimir Oltean 
1426acc64f52SVladimir Oltean 			mask = ocelot_get_bridge_fwd_mask(ocelot, port, bridge);
1427c1930148SVladimir Oltean 			mask |= cpu_fwd_mask;
1428df291e54SVladimir Oltean 			mask &= ~BIT(port);
142923ca3b72SVladimir Oltean 			if (bond) {
143023ca3b72SVladimir Oltean 				mask &= ~ocelot_get_bond_mask(ocelot, bond,
143123ca3b72SVladimir Oltean 							      false);
143223ca3b72SVladimir Oltean 			}
14339b521250SVladimir Oltean 		} else {
1434e21268efSVladimir Oltean 			/* Standalone ports forward only to DSA tag_8021q CPU
1435e21268efSVladimir Oltean 			 * ports (if those exist), or to the hardware CPU port
1436e21268efSVladimir Oltean 			 * module otherwise.
1437e21268efSVladimir Oltean 			 */
1438e21268efSVladimir Oltean 			mask = cpu_fwd_mask;
1439e21268efSVladimir Oltean 		}
1440e21268efSVladimir Oltean 
1441e21268efSVladimir Oltean 		ocelot_write_rix(ocelot, mask, ANA_PGID_PGID, PGID_SRC + port);
14429b521250SVladimir Oltean 	}
14439b521250SVladimir Oltean }
1444e21268efSVladimir Oltean EXPORT_SYMBOL(ocelot_apply_bridge_fwd_mask);
14459b521250SVladimir Oltean 
14465e256365SVladimir Oltean void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state)
1447a556c76aSAlexandre Belloni {
1448421741eaSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1449df291e54SVladimir Oltean 	u32 learn_ena = 0;
1450a556c76aSAlexandre Belloni 
1451df291e54SVladimir Oltean 	ocelot_port->stp_state = state;
1452a556c76aSAlexandre Belloni 
1453df291e54SVladimir Oltean 	if ((state == BR_STATE_LEARNING || state == BR_STATE_FORWARDING) &&
1454df291e54SVladimir Oltean 	    ocelot_port->learn_ena)
1455df291e54SVladimir Oltean 		learn_ena = ANA_PORT_PORT_CFG_LEARN_ENA;
1456a556c76aSAlexandre Belloni 
1457df291e54SVladimir Oltean 	ocelot_rmw_gix(ocelot, learn_ena, ANA_PORT_PORT_CFG_LEARN_ENA,
1458df291e54SVladimir Oltean 		       ANA_PORT_PORT_CFG, port);
1459a556c76aSAlexandre Belloni 
14609b521250SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
1461a556c76aSAlexandre Belloni }
14625e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_bridge_stp_state_set);
1463a556c76aSAlexandre Belloni 
14645e256365SVladimir Oltean void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs)
14654bda1415SVladimir Oltean {
1466c0d7eccbSVladimir Oltean 	unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000);
1467c0d7eccbSVladimir Oltean 
1468c0d7eccbSVladimir Oltean 	/* Setting AGE_PERIOD to zero effectively disables automatic aging,
1469c0d7eccbSVladimir Oltean 	 * which is clearly not what our intention is. So avoid that.
1470c0d7eccbSVladimir Oltean 	 */
1471c0d7eccbSVladimir Oltean 	if (!age_period)
1472c0d7eccbSVladimir Oltean 		age_period = 1;
1473c0d7eccbSVladimir Oltean 
1474c0d7eccbSVladimir Oltean 	ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE);
1475a556c76aSAlexandre Belloni }
14765e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_set_ageing_time);
1477a556c76aSAlexandre Belloni 
1478a556c76aSAlexandre Belloni static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
1479a556c76aSAlexandre Belloni 						     const unsigned char *addr,
1480a556c76aSAlexandre Belloni 						     u16 vid)
1481a556c76aSAlexandre Belloni {
1482a556c76aSAlexandre Belloni 	struct ocelot_multicast *mc;
1483a556c76aSAlexandre Belloni 
1484a556c76aSAlexandre Belloni 	list_for_each_entry(mc, &ocelot->multicast, list) {
1485a556c76aSAlexandre Belloni 		if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
1486a556c76aSAlexandre Belloni 			return mc;
1487a556c76aSAlexandre Belloni 	}
1488a556c76aSAlexandre Belloni 
1489a556c76aSAlexandre Belloni 	return NULL;
1490a556c76aSAlexandre Belloni }
1491a556c76aSAlexandre Belloni 
14929403c158SVladimir Oltean static enum macaccess_entry_type ocelot_classify_mdb(const unsigned char *addr)
14939403c158SVladimir Oltean {
14949403c158SVladimir Oltean 	if (addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e)
14959403c158SVladimir Oltean 		return ENTRYTYPE_MACv4;
14969403c158SVladimir Oltean 	if (addr[0] == 0x33 && addr[1] == 0x33)
14979403c158SVladimir Oltean 		return ENTRYTYPE_MACv6;
14987c313143SVladimir Oltean 	return ENTRYTYPE_LOCKED;
14999403c158SVladimir Oltean }
15009403c158SVladimir Oltean 
1501e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_pgid_alloc(struct ocelot *ocelot, int index,
1502e5d1f896SVladimir Oltean 					     unsigned long ports)
1503e5d1f896SVladimir Oltean {
1504e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1505e5d1f896SVladimir Oltean 
1506e5d1f896SVladimir Oltean 	pgid = kzalloc(sizeof(*pgid), GFP_KERNEL);
1507e5d1f896SVladimir Oltean 	if (!pgid)
1508e5d1f896SVladimir Oltean 		return ERR_PTR(-ENOMEM);
1509e5d1f896SVladimir Oltean 
1510e5d1f896SVladimir Oltean 	pgid->ports = ports;
1511e5d1f896SVladimir Oltean 	pgid->index = index;
1512e5d1f896SVladimir Oltean 	refcount_set(&pgid->refcount, 1);
1513e5d1f896SVladimir Oltean 	list_add_tail(&pgid->list, &ocelot->pgids);
1514e5d1f896SVladimir Oltean 
1515e5d1f896SVladimir Oltean 	return pgid;
1516e5d1f896SVladimir Oltean }
1517e5d1f896SVladimir Oltean 
1518e5d1f896SVladimir Oltean static void ocelot_pgid_free(struct ocelot *ocelot, struct ocelot_pgid *pgid)
1519e5d1f896SVladimir Oltean {
1520e5d1f896SVladimir Oltean 	if (!refcount_dec_and_test(&pgid->refcount))
1521e5d1f896SVladimir Oltean 		return;
1522e5d1f896SVladimir Oltean 
1523e5d1f896SVladimir Oltean 	list_del(&pgid->list);
1524e5d1f896SVladimir Oltean 	kfree(pgid);
1525e5d1f896SVladimir Oltean }
1526e5d1f896SVladimir Oltean 
1527e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_mdb_get_pgid(struct ocelot *ocelot,
1528bb8d53fdSVladimir Oltean 					       const struct ocelot_multicast *mc)
15299403c158SVladimir Oltean {
1530e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1531e5d1f896SVladimir Oltean 	int index;
15329403c158SVladimir Oltean 
15339403c158SVladimir Oltean 	/* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and
15349403c158SVladimir Oltean 	 * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the
15359403c158SVladimir Oltean 	 * destination mask table (PGID), the destination set is programmed as
15369403c158SVladimir Oltean 	 * part of the entry MAC address.", and the DEST_IDX is set to 0.
15379403c158SVladimir Oltean 	 */
1538bb8d53fdSVladimir Oltean 	if (mc->entry_type == ENTRYTYPE_MACv4 ||
1539bb8d53fdSVladimir Oltean 	    mc->entry_type == ENTRYTYPE_MACv6)
1540e5d1f896SVladimir Oltean 		return ocelot_pgid_alloc(ocelot, 0, mc->ports);
15419403c158SVladimir Oltean 
1542e5d1f896SVladimir Oltean 	list_for_each_entry(pgid, &ocelot->pgids, list) {
1543e5d1f896SVladimir Oltean 		/* When searching for a nonreserved multicast PGID, ignore the
1544e5d1f896SVladimir Oltean 		 * dummy PGID of zero that we have for MACv4/MACv6 entries
1545e5d1f896SVladimir Oltean 		 */
1546e5d1f896SVladimir Oltean 		if (pgid->index && pgid->ports == mc->ports) {
1547e5d1f896SVladimir Oltean 			refcount_inc(&pgid->refcount);
1548e5d1f896SVladimir Oltean 			return pgid;
1549e5d1f896SVladimir Oltean 		}
1550e5d1f896SVladimir Oltean 	}
1551e5d1f896SVladimir Oltean 
1552e5d1f896SVladimir Oltean 	/* Search for a free index in the nonreserved multicast PGID area */
1553e5d1f896SVladimir Oltean 	for_each_nonreserved_multicast_dest_pgid(ocelot, index) {
15549403c158SVladimir Oltean 		bool used = false;
15559403c158SVladimir Oltean 
1556e5d1f896SVladimir Oltean 		list_for_each_entry(pgid, &ocelot->pgids, list) {
1557e5d1f896SVladimir Oltean 			if (pgid->index == index) {
15589403c158SVladimir Oltean 				used = true;
15599403c158SVladimir Oltean 				break;
15609403c158SVladimir Oltean 			}
15619403c158SVladimir Oltean 		}
15629403c158SVladimir Oltean 
15639403c158SVladimir Oltean 		if (!used)
1564e5d1f896SVladimir Oltean 			return ocelot_pgid_alloc(ocelot, index, mc->ports);
15659403c158SVladimir Oltean 	}
15669403c158SVladimir Oltean 
1567e5d1f896SVladimir Oltean 	return ERR_PTR(-ENOSPC);
15689403c158SVladimir Oltean }
15699403c158SVladimir Oltean 
15709403c158SVladimir Oltean static void ocelot_encode_ports_to_mdb(unsigned char *addr,
1571bb8d53fdSVladimir Oltean 				       struct ocelot_multicast *mc)
15729403c158SVladimir Oltean {
1573ebbd860eSVladimir Oltean 	ether_addr_copy(addr, mc->addr);
15749403c158SVladimir Oltean 
1575bb8d53fdSVladimir Oltean 	if (mc->entry_type == ENTRYTYPE_MACv4) {
15769403c158SVladimir Oltean 		addr[0] = 0;
15779403c158SVladimir Oltean 		addr[1] = mc->ports >> 8;
15789403c158SVladimir Oltean 		addr[2] = mc->ports & 0xff;
1579bb8d53fdSVladimir Oltean 	} else if (mc->entry_type == ENTRYTYPE_MACv6) {
15809403c158SVladimir Oltean 		addr[0] = mc->ports >> 8;
15819403c158SVladimir Oltean 		addr[1] = mc->ports & 0xff;
15829403c158SVladimir Oltean 	}
15839403c158SVladimir Oltean }
15849403c158SVladimir Oltean 
1585209edf95SVladimir Oltean int ocelot_port_mdb_add(struct ocelot *ocelot, int port,
1586209edf95SVladimir Oltean 			const struct switchdev_obj_port_mdb *mdb)
1587a556c76aSAlexandre Belloni {
1588a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1589004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
1590e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1591a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1592a556c76aSAlexandre Belloni 
1593471beb11SVladimir Oltean 	if (port == ocelot->npi)
1594471beb11SVladimir Oltean 		port = ocelot->num_phys_ports;
1595471beb11SVladimir Oltean 
1596a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1597a556c76aSAlexandre Belloni 	if (!mc) {
1598728e69aeSVladimir Oltean 		/* New entry */
1599bb8d53fdSVladimir Oltean 		mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
1600bb8d53fdSVladimir Oltean 		if (!mc)
1601bb8d53fdSVladimir Oltean 			return -ENOMEM;
1602bb8d53fdSVladimir Oltean 
1603bb8d53fdSVladimir Oltean 		mc->entry_type = ocelot_classify_mdb(mdb->addr);
1604bb8d53fdSVladimir Oltean 		ether_addr_copy(mc->addr, mdb->addr);
1605bb8d53fdSVladimir Oltean 		mc->vid = vid;
1606bb8d53fdSVladimir Oltean 
1607a556c76aSAlexandre Belloni 		list_add_tail(&mc->list, &ocelot->multicast);
1608728e69aeSVladimir Oltean 	} else {
1609e5d1f896SVladimir Oltean 		/* Existing entry. Clean up the current port mask from
1610e5d1f896SVladimir Oltean 		 * hardware now, because we'll be modifying it.
1611e5d1f896SVladimir Oltean 		 */
1612e5d1f896SVladimir Oltean 		ocelot_pgid_free(ocelot, mc->pgid);
1613bb8d53fdSVladimir Oltean 		ocelot_encode_ports_to_mdb(addr, mc);
1614a556c76aSAlexandre Belloni 		ocelot_mact_forget(ocelot, addr, vid);
1615a556c76aSAlexandre Belloni 	}
1616a556c76aSAlexandre Belloni 
1617004d44f6SVladimir Oltean 	mc->ports |= BIT(port);
1618e5d1f896SVladimir Oltean 
1619e5d1f896SVladimir Oltean 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
1620e5d1f896SVladimir Oltean 	if (IS_ERR(pgid)) {
1621e5d1f896SVladimir Oltean 		dev_err(ocelot->dev,
1622e5d1f896SVladimir Oltean 			"Cannot allocate PGID for mdb %pM vid %d\n",
1623e5d1f896SVladimir Oltean 			mc->addr, mc->vid);
1624e5d1f896SVladimir Oltean 		devm_kfree(ocelot->dev, mc);
1625e5d1f896SVladimir Oltean 		return PTR_ERR(pgid);
1626e5d1f896SVladimir Oltean 	}
1627e5d1f896SVladimir Oltean 	mc->pgid = pgid;
1628e5d1f896SVladimir Oltean 
1629bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1630a556c76aSAlexandre Belloni 
1631e5d1f896SVladimir Oltean 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
1632e5d1f896SVladimir Oltean 	    mc->entry_type != ENTRYTYPE_MACv6)
1633e5d1f896SVladimir Oltean 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
1634e5d1f896SVladimir Oltean 				 pgid->index);
1635e5d1f896SVladimir Oltean 
1636e5d1f896SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
1637bb8d53fdSVladimir Oltean 				 mc->entry_type);
1638a556c76aSAlexandre Belloni }
1639209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_add);
1640a556c76aSAlexandre Belloni 
1641209edf95SVladimir Oltean int ocelot_port_mdb_del(struct ocelot *ocelot, int port,
1642a556c76aSAlexandre Belloni 			const struct switchdev_obj_port_mdb *mdb)
1643a556c76aSAlexandre Belloni {
1644a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1645004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
1646e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1647a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1648a556c76aSAlexandre Belloni 
1649471beb11SVladimir Oltean 	if (port == ocelot->npi)
1650471beb11SVladimir Oltean 		port = ocelot->num_phys_ports;
1651471beb11SVladimir Oltean 
1652a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1653a556c76aSAlexandre Belloni 	if (!mc)
1654a556c76aSAlexandre Belloni 		return -ENOENT;
1655a556c76aSAlexandre Belloni 
1656bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1657a556c76aSAlexandre Belloni 	ocelot_mact_forget(ocelot, addr, vid);
1658a556c76aSAlexandre Belloni 
1659e5d1f896SVladimir Oltean 	ocelot_pgid_free(ocelot, mc->pgid);
1660004d44f6SVladimir Oltean 	mc->ports &= ~BIT(port);
1661a556c76aSAlexandre Belloni 	if (!mc->ports) {
1662a556c76aSAlexandre Belloni 		list_del(&mc->list);
1663a556c76aSAlexandre Belloni 		devm_kfree(ocelot->dev, mc);
1664a556c76aSAlexandre Belloni 		return 0;
1665a556c76aSAlexandre Belloni 	}
1666a556c76aSAlexandre Belloni 
1667e5d1f896SVladimir Oltean 	/* We have a PGID with fewer ports now */
1668e5d1f896SVladimir Oltean 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
1669e5d1f896SVladimir Oltean 	if (IS_ERR(pgid))
1670e5d1f896SVladimir Oltean 		return PTR_ERR(pgid);
1671e5d1f896SVladimir Oltean 	mc->pgid = pgid;
1672e5d1f896SVladimir Oltean 
1673bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1674a556c76aSAlexandre Belloni 
1675e5d1f896SVladimir Oltean 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
1676e5d1f896SVladimir Oltean 	    mc->entry_type != ENTRYTYPE_MACv6)
1677e5d1f896SVladimir Oltean 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
1678e5d1f896SVladimir Oltean 				 pgid->index);
1679e5d1f896SVladimir Oltean 
1680e5d1f896SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
1681bb8d53fdSVladimir Oltean 				 mc->entry_type);
1682a556c76aSAlexandre Belloni }
1683209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_del);
1684a556c76aSAlexandre Belloni 
1685e4bd44e8SVladimir Oltean void ocelot_port_bridge_join(struct ocelot *ocelot, int port,
1686a556c76aSAlexandre Belloni 			     struct net_device *bridge)
1687a556c76aSAlexandre Belloni {
1688df291e54SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1689a556c76aSAlexandre Belloni 
1690df291e54SVladimir Oltean 	ocelot_port->bridge = bridge;
1691a556c76aSAlexandre Belloni 
1692e4bd44e8SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
1693a556c76aSAlexandre Belloni }
16945e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_join);
1695a556c76aSAlexandre Belloni 
1696e4bd44e8SVladimir Oltean void ocelot_port_bridge_leave(struct ocelot *ocelot, int port,
1697a556c76aSAlexandre Belloni 			      struct net_device *bridge)
1698a556c76aSAlexandre Belloni {
1699df291e54SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1700c3e58a75SVladimir Oltean 	struct ocelot_vlan pvid = {0}, native_vlan = {0};
17012e554a7aSVladimir Oltean 
1702df291e54SVladimir Oltean 	ocelot_port->bridge = NULL;
17037142529fSAntoine Tenart 
1704c3e58a75SVladimir Oltean 	ocelot_port_set_pvid(ocelot, port, pvid);
17052f0402feSVladimir Oltean 	ocelot_port_set_native_vlan(ocelot, port, native_vlan);
1706e4bd44e8SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
1707a556c76aSAlexandre Belloni }
17085e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_leave);
1709a556c76aSAlexandre Belloni 
1710dc96ee37SAlexandre Belloni static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
1711dc96ee37SAlexandre Belloni {
1712528d3f19SVladimir Oltean 	unsigned long visited = GENMASK(ocelot->num_phys_ports - 1, 0);
1713dc96ee37SAlexandre Belloni 	int i, port, lag;
1714dc96ee37SAlexandre Belloni 
1715dc96ee37SAlexandre Belloni 	/* Reset destination and aggregation PGIDS */
171696b029b0SVladimir Oltean 	for_each_unicast_dest_pgid(ocelot, port)
1717dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1718dc96ee37SAlexandre Belloni 
171996b029b0SVladimir Oltean 	for_each_aggr_pgid(ocelot, i)
1720dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
1721dc96ee37SAlexandre Belloni 				 ANA_PGID_PGID, i);
1722dc96ee37SAlexandre Belloni 
1723528d3f19SVladimir Oltean 	/* The visited ports bitmask holds the list of ports offloading any
1724528d3f19SVladimir Oltean 	 * bonding interface. Initially we mark all these ports as unvisited,
1725528d3f19SVladimir Oltean 	 * then every time we visit a port in this bitmask, we know that it is
1726528d3f19SVladimir Oltean 	 * the lowest numbered port, i.e. the one whose logical ID == physical
1727528d3f19SVladimir Oltean 	 * port ID == LAG ID. So we mark as visited all further ports in the
1728528d3f19SVladimir Oltean 	 * bitmask that are offloading the same bonding interface. This way,
1729528d3f19SVladimir Oltean 	 * we set up the aggregation PGIDs only once per bonding interface.
1730528d3f19SVladimir Oltean 	 */
1731528d3f19SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1732528d3f19SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1733528d3f19SVladimir Oltean 
1734528d3f19SVladimir Oltean 		if (!ocelot_port || !ocelot_port->bond)
1735528d3f19SVladimir Oltean 			continue;
1736528d3f19SVladimir Oltean 
1737528d3f19SVladimir Oltean 		visited &= ~BIT(port);
1738528d3f19SVladimir Oltean 	}
1739528d3f19SVladimir Oltean 
1740528d3f19SVladimir Oltean 	/* Now, set PGIDs for each active LAG */
1741dc96ee37SAlexandre Belloni 	for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
1742528d3f19SVladimir Oltean 		struct net_device *bond = ocelot->ports[lag]->bond;
174323ca3b72SVladimir Oltean 		int num_active_ports = 0;
1744dc96ee37SAlexandre Belloni 		unsigned long bond_mask;
1745dc96ee37SAlexandre Belloni 		u8 aggr_idx[16];
1746dc96ee37SAlexandre Belloni 
1747528d3f19SVladimir Oltean 		if (!bond || (visited & BIT(lag)))
1748dc96ee37SAlexandre Belloni 			continue;
1749dc96ee37SAlexandre Belloni 
175023ca3b72SVladimir Oltean 		bond_mask = ocelot_get_bond_mask(ocelot, bond, true);
1751528d3f19SVladimir Oltean 
1752dc96ee37SAlexandre Belloni 		for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
1753dc96ee37SAlexandre Belloni 			// Destination mask
1754dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, bond_mask,
1755dc96ee37SAlexandre Belloni 					 ANA_PGID_PGID, port);
175623ca3b72SVladimir Oltean 			aggr_idx[num_active_ports++] = port;
1757dc96ee37SAlexandre Belloni 		}
1758dc96ee37SAlexandre Belloni 
175996b029b0SVladimir Oltean 		for_each_aggr_pgid(ocelot, i) {
1760dc96ee37SAlexandre Belloni 			u32 ac;
1761dc96ee37SAlexandre Belloni 
1762dc96ee37SAlexandre Belloni 			ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
1763dc96ee37SAlexandre Belloni 			ac &= ~bond_mask;
176423ca3b72SVladimir Oltean 			/* Don't do division by zero if there was no active
176523ca3b72SVladimir Oltean 			 * port. Just make all aggregation codes zero.
176623ca3b72SVladimir Oltean 			 */
176723ca3b72SVladimir Oltean 			if (num_active_ports)
176823ca3b72SVladimir Oltean 				ac |= BIT(aggr_idx[i % num_active_ports]);
1769dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
1770dc96ee37SAlexandre Belloni 		}
1771528d3f19SVladimir Oltean 
1772528d3f19SVladimir Oltean 		/* Mark all ports in the same LAG as visited to avoid applying
1773528d3f19SVladimir Oltean 		 * the same config again.
1774528d3f19SVladimir Oltean 		 */
1775528d3f19SVladimir Oltean 		for (port = lag; port < ocelot->num_phys_ports; port++) {
1776528d3f19SVladimir Oltean 			struct ocelot_port *ocelot_port = ocelot->ports[port];
1777528d3f19SVladimir Oltean 
1778528d3f19SVladimir Oltean 			if (!ocelot_port)
1779528d3f19SVladimir Oltean 				continue;
1780528d3f19SVladimir Oltean 
1781528d3f19SVladimir Oltean 			if (ocelot_port->bond == bond)
1782528d3f19SVladimir Oltean 				visited |= BIT(port);
1783528d3f19SVladimir Oltean 		}
1784dc96ee37SAlexandre Belloni 	}
1785dc96ee37SAlexandre Belloni }
1786dc96ee37SAlexandre Belloni 
17872527f2e8SVladimir Oltean /* When offloading a bonding interface, the switch ports configured under the
17882527f2e8SVladimir Oltean  * same bond must have the same logical port ID, equal to the physical port ID
17892527f2e8SVladimir Oltean  * of the lowest numbered physical port in that bond. Otherwise, in standalone/
17902527f2e8SVladimir Oltean  * bridged mode, each port has a logical port ID equal to its physical port ID.
17912527f2e8SVladimir Oltean  */
17922527f2e8SVladimir Oltean static void ocelot_setup_logical_port_ids(struct ocelot *ocelot)
1793dc96ee37SAlexandre Belloni {
17942527f2e8SVladimir Oltean 	int port;
1795dc96ee37SAlexandre Belloni 
17962527f2e8SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
17972527f2e8SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
17982527f2e8SVladimir Oltean 		struct net_device *bond;
1799dc96ee37SAlexandre Belloni 
18002527f2e8SVladimir Oltean 		if (!ocelot_port)
18012527f2e8SVladimir Oltean 			continue;
1802dc96ee37SAlexandre Belloni 
18032527f2e8SVladimir Oltean 		bond = ocelot_port->bond;
18042527f2e8SVladimir Oltean 		if (bond) {
180523ca3b72SVladimir Oltean 			int lag = __ffs(ocelot_get_bond_mask(ocelot, bond,
180623ca3b72SVladimir Oltean 							     false));
18072527f2e8SVladimir Oltean 
18082527f2e8SVladimir Oltean 			ocelot_rmw_gix(ocelot,
1809dc96ee37SAlexandre Belloni 				       ANA_PORT_PORT_CFG_PORTID_VAL(lag),
18102527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL_M,
18112527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG, port);
18122527f2e8SVladimir Oltean 		} else {
18132527f2e8SVladimir Oltean 			ocelot_rmw_gix(ocelot,
18142527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL(port),
18152527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL_M,
18162527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG, port);
18172527f2e8SVladimir Oltean 		}
1818dc96ee37SAlexandre Belloni 	}
1819dc96ee37SAlexandre Belloni }
1820dc96ee37SAlexandre Belloni 
18219c90eea3SVladimir Oltean int ocelot_port_lag_join(struct ocelot *ocelot, int port,
1822583cbbe3SVladimir Oltean 			 struct net_device *bond,
1823583cbbe3SVladimir Oltean 			 struct netdev_lag_upper_info *info)
1824dc96ee37SAlexandre Belloni {
1825583cbbe3SVladimir Oltean 	if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH)
1826583cbbe3SVladimir Oltean 		return -EOPNOTSUPP;
1827583cbbe3SVladimir Oltean 
1828b80af659SVladimir Oltean 	ocelot->ports[port]->bond = bond;
1829dc96ee37SAlexandre Belloni 
18302527f2e8SVladimir Oltean 	ocelot_setup_logical_port_ids(ocelot);
18319b521250SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
1832dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
1833dc96ee37SAlexandre Belloni 
1834dc96ee37SAlexandre Belloni 	return 0;
1835dc96ee37SAlexandre Belloni }
18369c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_join);
1837dc96ee37SAlexandre Belloni 
18389c90eea3SVladimir Oltean void ocelot_port_lag_leave(struct ocelot *ocelot, int port,
1839dc96ee37SAlexandre Belloni 			   struct net_device *bond)
1840dc96ee37SAlexandre Belloni {
1841b80af659SVladimir Oltean 	ocelot->ports[port]->bond = NULL;
1842b80af659SVladimir Oltean 
18432527f2e8SVladimir Oltean 	ocelot_setup_logical_port_ids(ocelot);
18449b521250SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
1845dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
1846dc96ee37SAlexandre Belloni }
18479c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_leave);
18480e332c85SPetr Machata 
184923ca3b72SVladimir Oltean void ocelot_port_lag_change(struct ocelot *ocelot, int port, bool lag_tx_active)
185023ca3b72SVladimir Oltean {
185123ca3b72SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
185223ca3b72SVladimir Oltean 
185323ca3b72SVladimir Oltean 	ocelot_port->lag_tx_active = lag_tx_active;
185423ca3b72SVladimir Oltean 
185523ca3b72SVladimir Oltean 	/* Rebalance the LAGs */
185623ca3b72SVladimir Oltean 	ocelot_set_aggr_pgids(ocelot);
185723ca3b72SVladimir Oltean }
185823ca3b72SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_change);
185923ca3b72SVladimir Oltean 
1860a8015dedSVladimir Oltean /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu.
1861a8015dedSVladimir Oltean  * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG.
18620b912fc9SVladimir Oltean  * In the special case that it's the NPI port that we're configuring, the
18630b912fc9SVladimir Oltean  * length of the tag and optional prefix needs to be accounted for privately,
18640b912fc9SVladimir Oltean  * in order to be able to sustain communication at the requested @sdu.
1865a8015dedSVladimir Oltean  */
18660b912fc9SVladimir Oltean void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu)
186731350d7fSVladimir Oltean {
186831350d7fSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1869a8015dedSVladimir Oltean 	int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN;
1870e8e6e73dSVladimir Oltean 	int pause_start, pause_stop;
1871601e984fSVladimir Oltean 	int atop, atop_tot;
187231350d7fSVladimir Oltean 
18730b912fc9SVladimir Oltean 	if (port == ocelot->npi) {
18740b912fc9SVladimir Oltean 		maxlen += OCELOT_TAG_LEN;
18750b912fc9SVladimir Oltean 
1876cacea62fSVladimir Oltean 		if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
18770b912fc9SVladimir Oltean 			maxlen += OCELOT_SHORT_PREFIX_LEN;
1878cacea62fSVladimir Oltean 		else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
18790b912fc9SVladimir Oltean 			maxlen += OCELOT_LONG_PREFIX_LEN;
18800b912fc9SVladimir Oltean 	}
18810b912fc9SVladimir Oltean 
1882a8015dedSVladimir Oltean 	ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG);
1883fa914e9cSVladimir Oltean 
1884e8e6e73dSVladimir Oltean 	/* Set Pause watermark hysteresis */
1885e8e6e73dSVladimir Oltean 	pause_start = 6 * maxlen / OCELOT_BUFFER_CELL_SZ;
1886e8e6e73dSVladimir Oltean 	pause_stop = 4 * maxlen / OCELOT_BUFFER_CELL_SZ;
1887541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_START,
1888541132f0SMaxim Kochetkov 			    pause_start);
1889541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_STOP,
1890541132f0SMaxim Kochetkov 			    pause_stop);
1891fa914e9cSVladimir Oltean 
1892601e984fSVladimir Oltean 	/* Tail dropping watermarks */
1893f6fe01d6SVladimir Oltean 	atop_tot = (ocelot->packet_buffer_size - 9 * maxlen) /
1894a8015dedSVladimir Oltean 		   OCELOT_BUFFER_CELL_SZ;
1895601e984fSVladimir Oltean 	atop = (9 * maxlen) / OCELOT_BUFFER_CELL_SZ;
1896601e984fSVladimir Oltean 	ocelot_write_rix(ocelot, ocelot->ops->wm_enc(atop), SYS_ATOP, port);
1897601e984fSVladimir Oltean 	ocelot_write(ocelot, ocelot->ops->wm_enc(atop_tot), SYS_ATOP_TOT_CFG);
1898fa914e9cSVladimir Oltean }
18990b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_port_set_maxlen);
19000b912fc9SVladimir Oltean 
19010b912fc9SVladimir Oltean int ocelot_get_max_mtu(struct ocelot *ocelot, int port)
19020b912fc9SVladimir Oltean {
19030b912fc9SVladimir Oltean 	int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN;
19040b912fc9SVladimir Oltean 
19050b912fc9SVladimir Oltean 	if (port == ocelot->npi) {
19060b912fc9SVladimir Oltean 		max_mtu -= OCELOT_TAG_LEN;
19070b912fc9SVladimir Oltean 
1908cacea62fSVladimir Oltean 		if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
19090b912fc9SVladimir Oltean 			max_mtu -= OCELOT_SHORT_PREFIX_LEN;
1910cacea62fSVladimir Oltean 		else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
19110b912fc9SVladimir Oltean 			max_mtu -= OCELOT_LONG_PREFIX_LEN;
19120b912fc9SVladimir Oltean 	}
19130b912fc9SVladimir Oltean 
19140b912fc9SVladimir Oltean 	return max_mtu;
19150b912fc9SVladimir Oltean }
19160b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_get_max_mtu);
1917fa914e9cSVladimir Oltean 
1918421741eaSVladimir Oltean static void ocelot_port_set_learning(struct ocelot *ocelot, int port,
1919421741eaSVladimir Oltean 				     bool enabled)
1920421741eaSVladimir Oltean {
1921421741eaSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1922421741eaSVladimir Oltean 	u32 val = 0;
1923421741eaSVladimir Oltean 
1924421741eaSVladimir Oltean 	if (enabled)
1925421741eaSVladimir Oltean 		val = ANA_PORT_PORT_CFG_LEARN_ENA;
1926421741eaSVladimir Oltean 
1927421741eaSVladimir Oltean 	ocelot_rmw_gix(ocelot, val, ANA_PORT_PORT_CFG_LEARN_ENA,
1928421741eaSVladimir Oltean 		       ANA_PORT_PORT_CFG, port);
1929421741eaSVladimir Oltean 
1930421741eaSVladimir Oltean 	ocelot_port->learn_ena = enabled;
1931421741eaSVladimir Oltean }
1932421741eaSVladimir Oltean 
1933421741eaSVladimir Oltean static void ocelot_port_set_ucast_flood(struct ocelot *ocelot, int port,
1934421741eaSVladimir Oltean 					bool enabled)
1935421741eaSVladimir Oltean {
1936421741eaSVladimir Oltean 	u32 val = 0;
1937421741eaSVladimir Oltean 
1938421741eaSVladimir Oltean 	if (enabled)
1939421741eaSVladimir Oltean 		val = BIT(port);
1940421741eaSVladimir Oltean 
1941421741eaSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_UC);
1942421741eaSVladimir Oltean }
1943421741eaSVladimir Oltean 
1944421741eaSVladimir Oltean static void ocelot_port_set_mcast_flood(struct ocelot *ocelot, int port,
1945421741eaSVladimir Oltean 					bool enabled)
1946421741eaSVladimir Oltean {
1947421741eaSVladimir Oltean 	u32 val = 0;
1948421741eaSVladimir Oltean 
1949421741eaSVladimir Oltean 	if (enabled)
1950421741eaSVladimir Oltean 		val = BIT(port);
1951421741eaSVladimir Oltean 
1952421741eaSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MC);
1953421741eaSVladimir Oltean }
1954421741eaSVladimir Oltean 
1955421741eaSVladimir Oltean static void ocelot_port_set_bcast_flood(struct ocelot *ocelot, int port,
1956421741eaSVladimir Oltean 					bool enabled)
1957421741eaSVladimir Oltean {
1958421741eaSVladimir Oltean 	u32 val = 0;
1959421741eaSVladimir Oltean 
1960421741eaSVladimir Oltean 	if (enabled)
1961421741eaSVladimir Oltean 		val = BIT(port);
1962421741eaSVladimir Oltean 
1963421741eaSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_BC);
1964421741eaSVladimir Oltean }
1965421741eaSVladimir Oltean 
1966421741eaSVladimir Oltean int ocelot_port_pre_bridge_flags(struct ocelot *ocelot, int port,
1967421741eaSVladimir Oltean 				 struct switchdev_brport_flags flags)
1968421741eaSVladimir Oltean {
1969421741eaSVladimir Oltean 	if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
1970421741eaSVladimir Oltean 			   BR_BCAST_FLOOD))
1971421741eaSVladimir Oltean 		return -EINVAL;
1972421741eaSVladimir Oltean 
1973421741eaSVladimir Oltean 	return 0;
1974421741eaSVladimir Oltean }
1975421741eaSVladimir Oltean EXPORT_SYMBOL(ocelot_port_pre_bridge_flags);
1976421741eaSVladimir Oltean 
1977421741eaSVladimir Oltean void ocelot_port_bridge_flags(struct ocelot *ocelot, int port,
1978421741eaSVladimir Oltean 			      struct switchdev_brport_flags flags)
1979421741eaSVladimir Oltean {
1980421741eaSVladimir Oltean 	if (flags.mask & BR_LEARNING)
1981421741eaSVladimir Oltean 		ocelot_port_set_learning(ocelot, port,
1982421741eaSVladimir Oltean 					 !!(flags.val & BR_LEARNING));
1983421741eaSVladimir Oltean 
1984421741eaSVladimir Oltean 	if (flags.mask & BR_FLOOD)
1985421741eaSVladimir Oltean 		ocelot_port_set_ucast_flood(ocelot, port,
1986421741eaSVladimir Oltean 					    !!(flags.val & BR_FLOOD));
1987421741eaSVladimir Oltean 
1988421741eaSVladimir Oltean 	if (flags.mask & BR_MCAST_FLOOD)
1989421741eaSVladimir Oltean 		ocelot_port_set_mcast_flood(ocelot, port,
1990421741eaSVladimir Oltean 					    !!(flags.val & BR_MCAST_FLOOD));
1991421741eaSVladimir Oltean 
1992421741eaSVladimir Oltean 	if (flags.mask & BR_BCAST_FLOOD)
1993421741eaSVladimir Oltean 		ocelot_port_set_bcast_flood(ocelot, port,
1994421741eaSVladimir Oltean 					    !!(flags.val & BR_BCAST_FLOOD));
1995421741eaSVladimir Oltean }
1996421741eaSVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_flags);
1997421741eaSVladimir Oltean 
19985e256365SVladimir Oltean void ocelot_init_port(struct ocelot *ocelot, int port)
1999fa914e9cSVladimir Oltean {
2000fa914e9cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2001fa914e9cSVladimir Oltean 
2002b049da13SYangbo Lu 	skb_queue_head_init(&ocelot_port->tx_skbs);
200331350d7fSVladimir Oltean 
200431350d7fSVladimir Oltean 	/* Basic L2 initialization */
200531350d7fSVladimir Oltean 
20065bc9d2e6SVladimir Oltean 	/* Set MAC IFG Gaps
20075bc9d2e6SVladimir Oltean 	 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
20085bc9d2e6SVladimir Oltean 	 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
20095bc9d2e6SVladimir Oltean 	 */
20105bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5),
20115bc9d2e6SVladimir Oltean 			   DEV_MAC_IFG_CFG);
20125bc9d2e6SVladimir Oltean 
20135bc9d2e6SVladimir Oltean 	/* Load seed (0) and set MAC HDX late collision  */
20145bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
20155bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG_SEED_LOAD,
20165bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
20175bc9d2e6SVladimir Oltean 	mdelay(1);
20185bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
20195bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
20205bc9d2e6SVladimir Oltean 
20215bc9d2e6SVladimir Oltean 	/* Set Max Length and maximum tags allowed */
2022a8015dedSVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN);
20235bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
20245bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
2025a8015dedSVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA |
20265bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
20275bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG);
20285bc9d2e6SVladimir Oltean 
20295bc9d2e6SVladimir Oltean 	/* Set SMAC of Pause frame (00:00:00:00:00:00) */
20305bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
20315bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG);
20325bc9d2e6SVladimir Oltean 
2033e8e6e73dSVladimir Oltean 	/* Enable transmission of pause frames */
2034541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
2035e8e6e73dSVladimir Oltean 
203631350d7fSVladimir Oltean 	/* Drop frames with multicast source address */
203731350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
203831350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
203931350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
204031350d7fSVladimir Oltean 
204131350d7fSVladimir Oltean 	/* Set default VLAN and tag type to 8021Q. */
204231350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q),
204331350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG_PORT_TPID_M,
204431350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG, port);
204531350d7fSVladimir Oltean 
2046421741eaSVladimir Oltean 	/* Disable source address learning for standalone mode */
2047421741eaSVladimir Oltean 	ocelot_port_set_learning(ocelot, port, false);
2048421741eaSVladimir Oltean 
204946efe4efSVladimir Oltean 	/* Set the port's initial logical port ID value, enable receiving
205046efe4efSVladimir Oltean 	 * frames on it, and configure the MAC address learning type to
205146efe4efSVladimir Oltean 	 * automatic.
205246efe4efSVladimir Oltean 	 */
205346efe4efSVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
205446efe4efSVladimir Oltean 			 ANA_PORT_PORT_CFG_RECV_ENA |
205546efe4efSVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(port),
205646efe4efSVladimir Oltean 			 ANA_PORT_PORT_CFG, port);
205746efe4efSVladimir Oltean 
205831350d7fSVladimir Oltean 	/* Enable vcap lookups */
205931350d7fSVladimir Oltean 	ocelot_vcap_enable(ocelot, port);
206031350d7fSVladimir Oltean }
20615e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_init_port);
206231350d7fSVladimir Oltean 
20632d44b097SVladimir Oltean /* Configure and enable the CPU port module, which is a set of queues
20642d44b097SVladimir Oltean  * accessible through register MMIO, frame DMA or Ethernet (in case
20652d44b097SVladimir Oltean  * NPI mode is used).
206669df578cSVladimir Oltean  */
20672d44b097SVladimir Oltean static void ocelot_cpu_port_init(struct ocelot *ocelot)
206821468199SVladimir Oltean {
206969df578cSVladimir Oltean 	int cpu = ocelot->num_phys_ports;
207069df578cSVladimir Oltean 
207169df578cSVladimir Oltean 	/* The unicast destination PGID for the CPU port module is unused */
207221468199SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
207369df578cSVladimir Oltean 	/* Instead set up a multicast destination PGID for traffic copied to
207469df578cSVladimir Oltean 	 * the CPU. Whitelisted MAC addresses like the port netdevice MAC
207569df578cSVladimir Oltean 	 * addresses will be copied to the CPU via this PGID.
207669df578cSVladimir Oltean 	 */
207721468199SVladimir Oltean 	ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
207821468199SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
207921468199SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
208021468199SVladimir Oltean 			 ANA_PORT_PORT_CFG, cpu);
208121468199SVladimir Oltean 
208269df578cSVladimir Oltean 	/* Enable CPU port module */
2083886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
208469df578cSVladimir Oltean 	/* CPU port Injection/Extraction configuration */
2085886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_XTR_HDR,
2086cacea62fSVladimir Oltean 			    OCELOT_TAG_PREFIX_NONE);
2087886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_INJ_HDR,
2088cacea62fSVladimir Oltean 			    OCELOT_TAG_PREFIX_NONE);
208921468199SVladimir Oltean 
209021468199SVladimir Oltean 	/* Configure the CPU port to be VLAN aware */
209121468199SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) |
209221468199SVladimir Oltean 				 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
209321468199SVladimir Oltean 				 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
209421468199SVladimir Oltean 			 ANA_PORT_VLAN_CFG, cpu);
209521468199SVladimir Oltean }
209621468199SVladimir Oltean 
2097f6fe01d6SVladimir Oltean static void ocelot_detect_features(struct ocelot *ocelot)
2098f6fe01d6SVladimir Oltean {
2099f6fe01d6SVladimir Oltean 	int mmgt, eq_ctrl;
2100f6fe01d6SVladimir Oltean 
2101f6fe01d6SVladimir Oltean 	/* For Ocelot, Felix, Seville, Serval etc, SYS:MMGT:MMGT:FREECNT holds
2102f6fe01d6SVladimir Oltean 	 * the number of 240-byte free memory words (aka 4-cell chunks) and not
2103f6fe01d6SVladimir Oltean 	 * 192 bytes as the documentation incorrectly says.
2104f6fe01d6SVladimir Oltean 	 */
2105f6fe01d6SVladimir Oltean 	mmgt = ocelot_read(ocelot, SYS_MMGT);
2106f6fe01d6SVladimir Oltean 	ocelot->packet_buffer_size = 240 * SYS_MMGT_FREECNT(mmgt);
2107f6fe01d6SVladimir Oltean 
2108f6fe01d6SVladimir Oltean 	eq_ctrl = ocelot_read(ocelot, QSYS_EQ_CTRL);
2109f6fe01d6SVladimir Oltean 	ocelot->num_frame_refs = QSYS_MMGT_EQ_CTRL_FP_FREE_CNT(eq_ctrl);
2110f6fe01d6SVladimir Oltean }
2111f6fe01d6SVladimir Oltean 
2112a556c76aSAlexandre Belloni int ocelot_init(struct ocelot *ocelot)
2113a556c76aSAlexandre Belloni {
2114a556c76aSAlexandre Belloni 	char queue_name[32];
211521468199SVladimir Oltean 	int i, ret;
211621468199SVladimir Oltean 	u32 port;
2117a556c76aSAlexandre Belloni 
21183a77b593SVladimir Oltean 	if (ocelot->ops->reset) {
21193a77b593SVladimir Oltean 		ret = ocelot->ops->reset(ocelot);
21203a77b593SVladimir Oltean 		if (ret) {
21213a77b593SVladimir Oltean 			dev_err(ocelot->dev, "Switch reset failed\n");
21223a77b593SVladimir Oltean 			return ret;
21233a77b593SVladimir Oltean 		}
21243a77b593SVladimir Oltean 	}
21253a77b593SVladimir Oltean 
2126a556c76aSAlexandre Belloni 	ocelot->stats = devm_kcalloc(ocelot->dev,
2127a556c76aSAlexandre Belloni 				     ocelot->num_phys_ports * ocelot->num_stats,
2128a556c76aSAlexandre Belloni 				     sizeof(u64), GFP_KERNEL);
2129a556c76aSAlexandre Belloni 	if (!ocelot->stats)
2130a556c76aSAlexandre Belloni 		return -ENOMEM;
2131a556c76aSAlexandre Belloni 
2132a556c76aSAlexandre Belloni 	mutex_init(&ocelot->stats_lock);
21334e3b0468SAntoine Tenart 	mutex_init(&ocelot->ptp_lock);
21344e3b0468SAntoine Tenart 	spin_lock_init(&ocelot->ptp_clock_lock);
213552849bcfSVladimir Oltean 	spin_lock_init(&ocelot->ts_id_lock);
2136a556c76aSAlexandre Belloni 	snprintf(queue_name, sizeof(queue_name), "%s-stats",
2137a556c76aSAlexandre Belloni 		 dev_name(ocelot->dev));
2138a556c76aSAlexandre Belloni 	ocelot->stats_queue = create_singlethread_workqueue(queue_name);
2139a556c76aSAlexandre Belloni 	if (!ocelot->stats_queue)
2140a556c76aSAlexandre Belloni 		return -ENOMEM;
2141a556c76aSAlexandre Belloni 
2142ca0b272bSVladimir Oltean 	ocelot->owq = alloc_ordered_workqueue("ocelot-owq", 0);
2143ca0b272bSVladimir Oltean 	if (!ocelot->owq) {
2144ca0b272bSVladimir Oltean 		destroy_workqueue(ocelot->stats_queue);
2145ca0b272bSVladimir Oltean 		return -ENOMEM;
2146ca0b272bSVladimir Oltean 	}
2147ca0b272bSVladimir Oltean 
21482b120ddeSClaudiu Manoil 	INIT_LIST_HEAD(&ocelot->multicast);
2149e5d1f896SVladimir Oltean 	INIT_LIST_HEAD(&ocelot->pgids);
2150f6fe01d6SVladimir Oltean 	ocelot_detect_features(ocelot);
2151a556c76aSAlexandre Belloni 	ocelot_mact_init(ocelot);
2152a556c76aSAlexandre Belloni 	ocelot_vlan_init(ocelot);
2153aae4e500SVladimir Oltean 	ocelot_vcap_init(ocelot);
21542d44b097SVladimir Oltean 	ocelot_cpu_port_init(ocelot);
2155a556c76aSAlexandre Belloni 
2156a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2157a556c76aSAlexandre Belloni 		/* Clear all counters (5 groups) */
2158a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
2159a556c76aSAlexandre Belloni 				     SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
2160a556c76aSAlexandre Belloni 			     SYS_STAT_CFG);
2161a556c76aSAlexandre Belloni 	}
2162a556c76aSAlexandre Belloni 
2163a556c76aSAlexandre Belloni 	/* Only use S-Tag */
2164a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
2165a556c76aSAlexandre Belloni 
2166a556c76aSAlexandre Belloni 	/* Aggregation mode */
2167a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
2168a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_DMAC_ENA |
2169a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
2170f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA |
2171f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP6_FLOW_LBL_ENA |
2172f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP6_TCPUDP_ENA,
2173f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG);
2174a556c76aSAlexandre Belloni 
2175a556c76aSAlexandre Belloni 	/* Set MAC age time to default value. The entry is aged after
2176a556c76aSAlexandre Belloni 	 * 2*AGE_PERIOD
2177a556c76aSAlexandre Belloni 	 */
2178a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
2179a556c76aSAlexandre Belloni 		     ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
2180a556c76aSAlexandre Belloni 		     ANA_AUTOAGE);
2181a556c76aSAlexandre Belloni 
2182a556c76aSAlexandre Belloni 	/* Disable learning for frames discarded by VLAN ingress filtering */
2183a556c76aSAlexandre Belloni 	regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
2184a556c76aSAlexandre Belloni 
2185a556c76aSAlexandre Belloni 	/* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
2186a556c76aSAlexandre Belloni 	ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
2187a556c76aSAlexandre Belloni 		     SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
2188a556c76aSAlexandre Belloni 
2189a556c76aSAlexandre Belloni 	/* Setup flooding PGIDs */
2190edd2410bSVladimir Oltean 	for (i = 0; i < ocelot->num_flooding_pgids; i++)
2191a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
2192b360d94fSVladimir Oltean 				 ANA_FLOODING_FLD_BROADCAST(PGID_BC) |
2193a556c76aSAlexandre Belloni 				 ANA_FLOODING_FLD_UNICAST(PGID_UC),
2194edd2410bSVladimir Oltean 				 ANA_FLOODING, i);
2195a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
2196a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
2197a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
2198a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
2199a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC);
2200a556c76aSAlexandre Belloni 
2201a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2202a556c76aSAlexandre Belloni 		/* Transmit the frame to the local port. */
2203a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
2204a556c76aSAlexandre Belloni 		/* Do not forward BPDU frames to the front ports. */
2205a556c76aSAlexandre Belloni 		ocelot_write_gix(ocelot,
2206a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
2207a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG,
2208a556c76aSAlexandre Belloni 				 port);
2209a556c76aSAlexandre Belloni 		/* Ensure bridging is disabled */
2210a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
2211a556c76aSAlexandre Belloni 	}
2212a556c76aSAlexandre Belloni 
221396b029b0SVladimir Oltean 	for_each_nonreserved_multicast_dest_pgid(ocelot, i) {
2214a556c76aSAlexandre Belloni 		u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
2215a556c76aSAlexandre Belloni 
2216a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
2217a556c76aSAlexandre Belloni 	}
2218ebb1bb40SHoratiu Vultur 
2219ebb1bb40SHoratiu Vultur 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_BLACKHOLE);
2220ebb1bb40SHoratiu Vultur 
2221b360d94fSVladimir Oltean 	/* Allow broadcast and unknown L2 multicast to the CPU. */
2222b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2223b360d94fSVladimir Oltean 		       ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2224a556c76aSAlexandre Belloni 		       ANA_PGID_PGID, PGID_MC);
2225b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2226b360d94fSVladimir Oltean 		       ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2227b360d94fSVladimir Oltean 		       ANA_PGID_PGID, PGID_BC);
2228a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
2229a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
2230a556c76aSAlexandre Belloni 
2231a556c76aSAlexandre Belloni 	/* Allow manual injection via DEVCPU_QS registers, and byte swap these
2232a556c76aSAlexandre Belloni 	 * registers endianness.
2233a556c76aSAlexandre Belloni 	 */
2234a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
2235a556c76aSAlexandre Belloni 			 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
2236a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
2237a556c76aSAlexandre Belloni 			 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
2238a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
2239a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LRN(2) |
2240a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
2241a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
2242a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
2243a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
2244a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
2245a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IGMP(6) |
2246a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
2247a556c76aSAlexandre Belloni 	for (i = 0; i < 16; i++)
2248a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
2249a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
2250a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG, i);
2251a556c76aSAlexandre Belloni 
22521e1caa97SClaudiu Manoil 	INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work);
2253a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
2254a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
22554e3b0468SAntoine Tenart 
2256a556c76aSAlexandre Belloni 	return 0;
2257a556c76aSAlexandre Belloni }
2258a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_init);
2259a556c76aSAlexandre Belloni 
2260a556c76aSAlexandre Belloni void ocelot_deinit(struct ocelot *ocelot)
2261a556c76aSAlexandre Belloni {
2262c5d13969SClaudiu Manoil 	cancel_delayed_work(&ocelot->stats_work);
2263a556c76aSAlexandre Belloni 	destroy_workqueue(ocelot->stats_queue);
2264ca0b272bSVladimir Oltean 	destroy_workqueue(ocelot->owq);
2265a556c76aSAlexandre Belloni 	mutex_destroy(&ocelot->stats_lock);
2266a556c76aSAlexandre Belloni }
2267a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_deinit);
2268a556c76aSAlexandre Belloni 
2269e5fb512dSVladimir Oltean void ocelot_deinit_port(struct ocelot *ocelot, int port)
2270e5fb512dSVladimir Oltean {
2271e5fb512dSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2272e5fb512dSVladimir Oltean 
2273e5fb512dSVladimir Oltean 	skb_queue_purge(&ocelot_port->tx_skbs);
2274e5fb512dSVladimir Oltean }
2275e5fb512dSVladimir Oltean EXPORT_SYMBOL(ocelot_deinit_port);
2276e5fb512dSVladimir Oltean 
2277a556c76aSAlexandre Belloni MODULE_LICENSE("Dual MIT/GPL");
2278