18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: (GPL-2.0 OR MIT)
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Microsemi Ocelot Switch driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2017 Microsemi Corporation
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci#include <linux/if_bridge.h>
88c2ecf20Sopenharmony_ci#include <soc/mscc/ocelot_vcap.h>
98c2ecf20Sopenharmony_ci#include "ocelot.h"
108c2ecf20Sopenharmony_ci#include "ocelot_vcap.h"
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#define TABLE_UPDATE_SLEEP_US 10
138c2ecf20Sopenharmony_ci#define TABLE_UPDATE_TIMEOUT_US 100000
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_cistruct ocelot_mact_entry {
168c2ecf20Sopenharmony_ci	u8 mac[ETH_ALEN];
178c2ecf20Sopenharmony_ci	u16 vid;
188c2ecf20Sopenharmony_ci	enum macaccess_entry_type type;
198c2ecf20Sopenharmony_ci};
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_cistatic inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot)
228c2ecf20Sopenharmony_ci{
238c2ecf20Sopenharmony_ci	return ocelot_read(ocelot, ANA_TABLES_MACACCESS);
248c2ecf20Sopenharmony_ci}
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_cistatic inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot)
278c2ecf20Sopenharmony_ci{
288c2ecf20Sopenharmony_ci	u32 val;
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci	return readx_poll_timeout(ocelot_mact_read_macaccess,
318c2ecf20Sopenharmony_ci		ocelot, val,
328c2ecf20Sopenharmony_ci		(val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) ==
338c2ecf20Sopenharmony_ci		MACACCESS_CMD_IDLE,
348c2ecf20Sopenharmony_ci		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
358c2ecf20Sopenharmony_ci}
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cistatic void ocelot_mact_select(struct ocelot *ocelot,
388c2ecf20Sopenharmony_ci			       const unsigned char mac[ETH_ALEN],
398c2ecf20Sopenharmony_ci			       unsigned int vid)
408c2ecf20Sopenharmony_ci{
418c2ecf20Sopenharmony_ci	u32 macl = 0, mach = 0;
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	/* Set the MAC address to handle and the vlan associated in a format
448c2ecf20Sopenharmony_ci	 * understood by the hardware.
458c2ecf20Sopenharmony_ci	 */
468c2ecf20Sopenharmony_ci	mach |= vid    << 16;
478c2ecf20Sopenharmony_ci	mach |= mac[0] << 8;
488c2ecf20Sopenharmony_ci	mach |= mac[1] << 0;
498c2ecf20Sopenharmony_ci	macl |= mac[2] << 24;
508c2ecf20Sopenharmony_ci	macl |= mac[3] << 16;
518c2ecf20Sopenharmony_ci	macl |= mac[4] << 8;
528c2ecf20Sopenharmony_ci	macl |= mac[5] << 0;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA);
558c2ecf20Sopenharmony_ci	ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA);
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci}
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ciint ocelot_mact_learn(struct ocelot *ocelot, int port,
608c2ecf20Sopenharmony_ci		      const unsigned char mac[ETH_ALEN],
618c2ecf20Sopenharmony_ci		      unsigned int vid, enum macaccess_entry_type type)
628c2ecf20Sopenharmony_ci{
638c2ecf20Sopenharmony_ci	u32 cmd = ANA_TABLES_MACACCESS_VALID |
648c2ecf20Sopenharmony_ci		ANA_TABLES_MACACCESS_DEST_IDX(port) |
658c2ecf20Sopenharmony_ci		ANA_TABLES_MACACCESS_ENTRYTYPE(type) |
668c2ecf20Sopenharmony_ci		ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN);
678c2ecf20Sopenharmony_ci	unsigned int mc_ports;
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	/* Set MAC_CPU_COPY if the CPU port is used by a multicast entry */
708c2ecf20Sopenharmony_ci	if (type == ENTRYTYPE_MACv4)
718c2ecf20Sopenharmony_ci		mc_ports = (mac[1] << 8) | mac[2];
728c2ecf20Sopenharmony_ci	else if (type == ENTRYTYPE_MACv6)
738c2ecf20Sopenharmony_ci		mc_ports = (mac[0] << 8) | mac[1];
748c2ecf20Sopenharmony_ci	else
758c2ecf20Sopenharmony_ci		mc_ports = 0;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	if (mc_ports & BIT(ocelot->num_phys_ports))
788c2ecf20Sopenharmony_ci		cmd |= ANA_TABLES_MACACCESS_MAC_CPU_COPY;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	ocelot_mact_select(ocelot, mac, vid);
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	/* Issue a write command */
838c2ecf20Sopenharmony_ci	ocelot_write(ocelot, cmd, ANA_TABLES_MACACCESS);
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	return ocelot_mact_wait_for_completion(ocelot);
868c2ecf20Sopenharmony_ci}
878c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ocelot_mact_learn);
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ciint ocelot_mact_forget(struct ocelot *ocelot,
908c2ecf20Sopenharmony_ci		       const unsigned char mac[ETH_ALEN], unsigned int vid)
918c2ecf20Sopenharmony_ci{
928c2ecf20Sopenharmony_ci	ocelot_mact_select(ocelot, mac, vid);
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	/* Issue a forget command */
958c2ecf20Sopenharmony_ci	ocelot_write(ocelot,
968c2ecf20Sopenharmony_ci		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET),
978c2ecf20Sopenharmony_ci		     ANA_TABLES_MACACCESS);
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	return ocelot_mact_wait_for_completion(ocelot);
1008c2ecf20Sopenharmony_ci}
1018c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ocelot_mact_forget);
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_cistatic void ocelot_mact_init(struct ocelot *ocelot)
1048c2ecf20Sopenharmony_ci{
1058c2ecf20Sopenharmony_ci	/* Configure the learning mode entries attributes:
1068c2ecf20Sopenharmony_ci	 * - Do not copy the frame to the CPU extraction queues.
1078c2ecf20Sopenharmony_ci	 * - Use the vlan and mac_cpoy for dmac lookup.
1088c2ecf20Sopenharmony_ci	 */
1098c2ecf20Sopenharmony_ci	ocelot_rmw(ocelot, 0,
1108c2ecf20Sopenharmony_ci		   ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS
1118c2ecf20Sopenharmony_ci		   | ANA_AGENCTRL_LEARN_FWD_KILL
1128c2ecf20Sopenharmony_ci		   | ANA_AGENCTRL_LEARN_IGNORE_VLAN,
1138c2ecf20Sopenharmony_ci		   ANA_AGENCTRL);
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	/* Clear the MAC table */
1168c2ecf20Sopenharmony_ci	ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS);
1178c2ecf20Sopenharmony_ci}
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_cistatic void ocelot_vcap_enable(struct ocelot *ocelot, int port)
1208c2ecf20Sopenharmony_ci{
1218c2ecf20Sopenharmony_ci	ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA |
1228c2ecf20Sopenharmony_ci			 ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa),
1238c2ecf20Sopenharmony_ci			 ANA_PORT_VCAP_S2_CFG, port);
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	ocelot_write_gix(ocelot, ANA_PORT_VCAP_CFG_S1_ENA,
1268c2ecf20Sopenharmony_ci			 ANA_PORT_VCAP_CFG, port);
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	ocelot_rmw_gix(ocelot, REW_PORT_CFG_ES0_EN,
1298c2ecf20Sopenharmony_ci		       REW_PORT_CFG_ES0_EN,
1308c2ecf20Sopenharmony_ci		       REW_PORT_CFG, port);
1318c2ecf20Sopenharmony_ci}
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_cistatic inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot)
1348c2ecf20Sopenharmony_ci{
1358c2ecf20Sopenharmony_ci	return ocelot_read(ocelot, ANA_TABLES_VLANACCESS);
1368c2ecf20Sopenharmony_ci}
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_cistatic inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot)
1398c2ecf20Sopenharmony_ci{
1408c2ecf20Sopenharmony_ci	u32 val;
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	return readx_poll_timeout(ocelot_vlant_read_vlanaccess,
1438c2ecf20Sopenharmony_ci		ocelot,
1448c2ecf20Sopenharmony_ci		val,
1458c2ecf20Sopenharmony_ci		(val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) ==
1468c2ecf20Sopenharmony_ci		ANA_TABLES_VLANACCESS_CMD_IDLE,
1478c2ecf20Sopenharmony_ci		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
1488c2ecf20Sopenharmony_ci}
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_cistatic int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask)
1518c2ecf20Sopenharmony_ci{
1528c2ecf20Sopenharmony_ci	/* Select the VID to configure */
1538c2ecf20Sopenharmony_ci	ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid),
1548c2ecf20Sopenharmony_ci		     ANA_TABLES_VLANTIDX);
1558c2ecf20Sopenharmony_ci	/* Set the vlan port members mask and issue a write command */
1568c2ecf20Sopenharmony_ci	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) |
1578c2ecf20Sopenharmony_ci			     ANA_TABLES_VLANACCESS_CMD_WRITE,
1588c2ecf20Sopenharmony_ci		     ANA_TABLES_VLANACCESS);
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	return ocelot_vlant_wait_for_completion(ocelot);
1618c2ecf20Sopenharmony_ci}
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_cistatic int ocelot_port_set_native_vlan(struct ocelot *ocelot, int port,
1648c2ecf20Sopenharmony_ci				       u16 vid)
1658c2ecf20Sopenharmony_ci{
1668c2ecf20Sopenharmony_ci	struct ocelot_port *ocelot_port = ocelot->ports[port];
1678c2ecf20Sopenharmony_ci	u32 val = 0;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	if (ocelot_port->vid != vid) {
1708c2ecf20Sopenharmony_ci		/* Always permit deleting the native VLAN (vid = 0) */
1718c2ecf20Sopenharmony_ci		if (ocelot_port->vid && vid) {
1728c2ecf20Sopenharmony_ci			dev_err(ocelot->dev,
1738c2ecf20Sopenharmony_ci				"Port already has a native VLAN: %d\n",
1748c2ecf20Sopenharmony_ci				ocelot_port->vid);
1758c2ecf20Sopenharmony_ci			return -EBUSY;
1768c2ecf20Sopenharmony_ci		}
1778c2ecf20Sopenharmony_ci		ocelot_port->vid = vid;
1788c2ecf20Sopenharmony_ci	}
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_VID(vid),
1818c2ecf20Sopenharmony_ci		       REW_PORT_VLAN_CFG_PORT_VID_M,
1828c2ecf20Sopenharmony_ci		       REW_PORT_VLAN_CFG, port);
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	if (ocelot_port->vlan_aware && !ocelot_port->vid)
1858c2ecf20Sopenharmony_ci		/* If port is vlan-aware and tagged, drop untagged and priority
1868c2ecf20Sopenharmony_ci		 * tagged frames.
1878c2ecf20Sopenharmony_ci		 */
1888c2ecf20Sopenharmony_ci		val = ANA_PORT_DROP_CFG_DROP_UNTAGGED_ENA |
1898c2ecf20Sopenharmony_ci		      ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
1908c2ecf20Sopenharmony_ci		      ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
1918c2ecf20Sopenharmony_ci	ocelot_rmw_gix(ocelot, val,
1928c2ecf20Sopenharmony_ci		       ANA_PORT_DROP_CFG_DROP_UNTAGGED_ENA |
1938c2ecf20Sopenharmony_ci		       ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
1948c2ecf20Sopenharmony_ci		       ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA,
1958c2ecf20Sopenharmony_ci		       ANA_PORT_DROP_CFG, port);
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	if (ocelot_port->vlan_aware) {
1988c2ecf20Sopenharmony_ci		if (ocelot_port->vid)
1998c2ecf20Sopenharmony_ci			/* Tag all frames except when VID == DEFAULT_VLAN */
2008c2ecf20Sopenharmony_ci			val = REW_TAG_CFG_TAG_CFG(1);
2018c2ecf20Sopenharmony_ci		else
2028c2ecf20Sopenharmony_ci			/* Tag all frames */
2038c2ecf20Sopenharmony_ci			val = REW_TAG_CFG_TAG_CFG(3);
2048c2ecf20Sopenharmony_ci	} else {
2058c2ecf20Sopenharmony_ci		/* Port tagging disabled. */
2068c2ecf20Sopenharmony_ci		val = REW_TAG_CFG_TAG_CFG(0);
2078c2ecf20Sopenharmony_ci	}
2088c2ecf20Sopenharmony_ci	ocelot_rmw_gix(ocelot, val,
2098c2ecf20Sopenharmony_ci		       REW_TAG_CFG_TAG_CFG_M,
2108c2ecf20Sopenharmony_ci		       REW_TAG_CFG, port);
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	return 0;
2138c2ecf20Sopenharmony_ci}
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ciint ocelot_port_vlan_filtering(struct ocelot *ocelot, int port,
2168c2ecf20Sopenharmony_ci			       bool vlan_aware, struct switchdev_trans *trans)
2178c2ecf20Sopenharmony_ci{
2188c2ecf20Sopenharmony_ci	struct ocelot_port *ocelot_port = ocelot->ports[port];
2198c2ecf20Sopenharmony_ci	u32 val;
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	if (switchdev_trans_ph_prepare(trans)) {
2228c2ecf20Sopenharmony_ci		struct ocelot_vcap_block *block = &ocelot->block[VCAP_IS1];
2238c2ecf20Sopenharmony_ci		struct ocelot_vcap_filter *filter;
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci		list_for_each_entry(filter, &block->rules, list) {
2268c2ecf20Sopenharmony_ci			if (filter->ingress_port_mask & BIT(port) &&
2278c2ecf20Sopenharmony_ci			    filter->action.vid_replace_ena) {
2288c2ecf20Sopenharmony_ci				dev_err(ocelot->dev,
2298c2ecf20Sopenharmony_ci					"Cannot change VLAN state with vlan modify rules active\n");
2308c2ecf20Sopenharmony_ci				return -EBUSY;
2318c2ecf20Sopenharmony_ci			}
2328c2ecf20Sopenharmony_ci		}
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci		return 0;
2358c2ecf20Sopenharmony_ci	}
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	ocelot_port->vlan_aware = vlan_aware;
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	if (vlan_aware)
2408c2ecf20Sopenharmony_ci		val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
2418c2ecf20Sopenharmony_ci		      ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
2428c2ecf20Sopenharmony_ci	else
2438c2ecf20Sopenharmony_ci		val = 0;
2448c2ecf20Sopenharmony_ci	ocelot_rmw_gix(ocelot, val,
2458c2ecf20Sopenharmony_ci		       ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
2468c2ecf20Sopenharmony_ci		       ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
2478c2ecf20Sopenharmony_ci		       ANA_PORT_VLAN_CFG, port);
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	ocelot_port_set_native_vlan(ocelot, port, ocelot_port->vid);
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	return 0;
2528c2ecf20Sopenharmony_ci}
2538c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ocelot_port_vlan_filtering);
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci/* Default vlan to clasify for untagged frames (may be zero) */
2568c2ecf20Sopenharmony_cistatic void ocelot_port_set_pvid(struct ocelot *ocelot, int port, u16 pvid)
2578c2ecf20Sopenharmony_ci{
2588c2ecf20Sopenharmony_ci	struct ocelot_port *ocelot_port = ocelot->ports[port];
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci	ocelot_rmw_gix(ocelot,
2618c2ecf20Sopenharmony_ci		       ANA_PORT_VLAN_CFG_VLAN_VID(pvid),
2628c2ecf20Sopenharmony_ci		       ANA_PORT_VLAN_CFG_VLAN_VID_M,
2638c2ecf20Sopenharmony_ci		       ANA_PORT_VLAN_CFG, port);
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	ocelot_port->pvid = pvid;
2668c2ecf20Sopenharmony_ci}
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ciint ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid,
2698c2ecf20Sopenharmony_ci		    bool untagged)
2708c2ecf20Sopenharmony_ci{
2718c2ecf20Sopenharmony_ci	int ret;
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	/* Make the port a member of the VLAN */
2748c2ecf20Sopenharmony_ci	ocelot->vlan_mask[vid] |= BIT(port);
2758c2ecf20Sopenharmony_ci	ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
2768c2ecf20Sopenharmony_ci	if (ret)
2778c2ecf20Sopenharmony_ci		return ret;
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	/* Default ingress vlan classification */
2808c2ecf20Sopenharmony_ci	if (pvid)
2818c2ecf20Sopenharmony_ci		ocelot_port_set_pvid(ocelot, port, vid);
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci	/* Untagged egress vlan clasification */
2848c2ecf20Sopenharmony_ci	if (untagged) {
2858c2ecf20Sopenharmony_ci		ret = ocelot_port_set_native_vlan(ocelot, port, vid);
2868c2ecf20Sopenharmony_ci		if (ret)
2878c2ecf20Sopenharmony_ci			return ret;
2888c2ecf20Sopenharmony_ci	}
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	return 0;
2918c2ecf20Sopenharmony_ci}
2928c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ocelot_vlan_add);
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ciint ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid)
2958c2ecf20Sopenharmony_ci{
2968c2ecf20Sopenharmony_ci	struct ocelot_port *ocelot_port = ocelot->ports[port];
2978c2ecf20Sopenharmony_ci	int ret;
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci	/* Stop the port from being a member of the vlan */
3008c2ecf20Sopenharmony_ci	ocelot->vlan_mask[vid] &= ~BIT(port);
3018c2ecf20Sopenharmony_ci	ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
3028c2ecf20Sopenharmony_ci	if (ret)
3038c2ecf20Sopenharmony_ci		return ret;
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	/* Ingress */
3068c2ecf20Sopenharmony_ci	if (ocelot_port->pvid == vid)
3078c2ecf20Sopenharmony_ci		ocelot_port_set_pvid(ocelot, port, 0);
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci	/* Egress */
3108c2ecf20Sopenharmony_ci	if (ocelot_port->vid == vid)
3118c2ecf20Sopenharmony_ci		ocelot_port_set_native_vlan(ocelot, port, 0);
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	return 0;
3148c2ecf20Sopenharmony_ci}
3158c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ocelot_vlan_del);
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_cistatic void ocelot_vlan_init(struct ocelot *ocelot)
3188c2ecf20Sopenharmony_ci{
3198c2ecf20Sopenharmony_ci	u16 port, vid;
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	/* Clear VLAN table, by default all ports are members of all VLANs */
3228c2ecf20Sopenharmony_ci	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
3238c2ecf20Sopenharmony_ci		     ANA_TABLES_VLANACCESS);
3248c2ecf20Sopenharmony_ci	ocelot_vlant_wait_for_completion(ocelot);
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	/* Configure the port VLAN memberships */
3278c2ecf20Sopenharmony_ci	for (vid = 1; vid < VLAN_N_VID; vid++) {
3288c2ecf20Sopenharmony_ci		ocelot->vlan_mask[vid] = 0;
3298c2ecf20Sopenharmony_ci		ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
3308c2ecf20Sopenharmony_ci	}
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	/* Because VLAN filtering is enabled, we need VID 0 to get untagged
3338c2ecf20Sopenharmony_ci	 * traffic.  It is added automatically if 8021q module is loaded, but
3348c2ecf20Sopenharmony_ci	 * we can't rely on it since module may be not loaded.
3358c2ecf20Sopenharmony_ci	 */
3368c2ecf20Sopenharmony_ci	ocelot->vlan_mask[0] = GENMASK(ocelot->num_phys_ports - 1, 0);
3378c2ecf20Sopenharmony_ci	ocelot_vlant_set_mask(ocelot, 0, ocelot->vlan_mask[0]);
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	/* Set vlan ingress filter mask to all ports but the CPU port by
3408c2ecf20Sopenharmony_ci	 * default.
3418c2ecf20Sopenharmony_ci	 */
3428c2ecf20Sopenharmony_ci	ocelot_write(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
3438c2ecf20Sopenharmony_ci		     ANA_VLANMASK);
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci	for (port = 0; port < ocelot->num_phys_ports; port++) {
3468c2ecf20Sopenharmony_ci		ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
3478c2ecf20Sopenharmony_ci		ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
3488c2ecf20Sopenharmony_ci	}
3498c2ecf20Sopenharmony_ci}
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_cistatic u32 ocelot_read_eq_avail(struct ocelot *ocelot, int port)
3528c2ecf20Sopenharmony_ci{
3538c2ecf20Sopenharmony_ci	return ocelot_read_rix(ocelot, QSYS_SW_STATUS, port);
3548c2ecf20Sopenharmony_ci}
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ciint ocelot_port_flush(struct ocelot *ocelot, int port)
3578c2ecf20Sopenharmony_ci{
3588c2ecf20Sopenharmony_ci	unsigned int pause_ena;
3598c2ecf20Sopenharmony_ci	int err, val;
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	/* Disable dequeuing from the egress queues */
3628c2ecf20Sopenharmony_ci	ocelot_rmw_rix(ocelot, QSYS_PORT_MODE_DEQUEUE_DIS,
3638c2ecf20Sopenharmony_ci		       QSYS_PORT_MODE_DEQUEUE_DIS,
3648c2ecf20Sopenharmony_ci		       QSYS_PORT_MODE, port);
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	/* Disable flow control */
3678c2ecf20Sopenharmony_ci	ocelot_fields_read(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, &pause_ena);
3688c2ecf20Sopenharmony_ci	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	/* Disable priority flow control */
3718c2ecf20Sopenharmony_ci	ocelot_fields_write(ocelot, port,
3728c2ecf20Sopenharmony_ci			    QSYS_SWITCH_PORT_MODE_TX_PFC_ENA, 0);
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	/* Wait at least the time it takes to receive a frame of maximum length
3758c2ecf20Sopenharmony_ci	 * at the port.
3768c2ecf20Sopenharmony_ci	 * Worst-case delays for 10 kilobyte jumbo frames are:
3778c2ecf20Sopenharmony_ci	 * 8 ms on a 10M port
3788c2ecf20Sopenharmony_ci	 * 800 μs on a 100M port
3798c2ecf20Sopenharmony_ci	 * 80 μs on a 1G port
3808c2ecf20Sopenharmony_ci	 * 32 μs on a 2.5G port
3818c2ecf20Sopenharmony_ci	 */
3828c2ecf20Sopenharmony_ci	usleep_range(8000, 10000);
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	/* Disable half duplex backpressure. */
3858c2ecf20Sopenharmony_ci	ocelot_rmw_rix(ocelot, 0, SYS_FRONT_PORT_MODE_HDX_MODE,
3868c2ecf20Sopenharmony_ci		       SYS_FRONT_PORT_MODE, port);
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	/* Flush the queues associated with the port. */
3898c2ecf20Sopenharmony_ci	ocelot_rmw_gix(ocelot, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG_FLUSH_ENA,
3908c2ecf20Sopenharmony_ci		       REW_PORT_CFG, port);
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	/* Enable dequeuing from the egress queues. */
3938c2ecf20Sopenharmony_ci	ocelot_rmw_rix(ocelot, 0, QSYS_PORT_MODE_DEQUEUE_DIS, QSYS_PORT_MODE,
3948c2ecf20Sopenharmony_ci		       port);
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	/* Wait until flushing is complete. */
3978c2ecf20Sopenharmony_ci	err = read_poll_timeout(ocelot_read_eq_avail, val, !val,
3988c2ecf20Sopenharmony_ci				100, 2000000, false, ocelot, port);
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	/* Clear flushing again. */
4018c2ecf20Sopenharmony_ci	ocelot_rmw_gix(ocelot, 0, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG, port);
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci	/* Re-enable flow control */
4048c2ecf20Sopenharmony_ci	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, pause_ena);
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci	return err;
4078c2ecf20Sopenharmony_ci}
4088c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ocelot_port_flush);
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_civoid ocelot_adjust_link(struct ocelot *ocelot, int port,
4118c2ecf20Sopenharmony_ci			struct phy_device *phydev)
4128c2ecf20Sopenharmony_ci{
4138c2ecf20Sopenharmony_ci	struct ocelot_port *ocelot_port = ocelot->ports[port];
4148c2ecf20Sopenharmony_ci	int speed, mode = 0;
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci	switch (phydev->speed) {
4178c2ecf20Sopenharmony_ci	case SPEED_10:
4188c2ecf20Sopenharmony_ci		speed = OCELOT_SPEED_10;
4198c2ecf20Sopenharmony_ci		break;
4208c2ecf20Sopenharmony_ci	case SPEED_100:
4218c2ecf20Sopenharmony_ci		speed = OCELOT_SPEED_100;
4228c2ecf20Sopenharmony_ci		break;
4238c2ecf20Sopenharmony_ci	case SPEED_1000:
4248c2ecf20Sopenharmony_ci		speed = OCELOT_SPEED_1000;
4258c2ecf20Sopenharmony_ci		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
4268c2ecf20Sopenharmony_ci		break;
4278c2ecf20Sopenharmony_ci	case SPEED_2500:
4288c2ecf20Sopenharmony_ci		speed = OCELOT_SPEED_2500;
4298c2ecf20Sopenharmony_ci		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
4308c2ecf20Sopenharmony_ci		break;
4318c2ecf20Sopenharmony_ci	default:
4328c2ecf20Sopenharmony_ci		dev_err(ocelot->dev, "Unsupported PHY speed on port %d: %d\n",
4338c2ecf20Sopenharmony_ci			port, phydev->speed);
4348c2ecf20Sopenharmony_ci		return;
4358c2ecf20Sopenharmony_ci	}
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	phy_print_status(phydev);
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci	if (!phydev->link)
4408c2ecf20Sopenharmony_ci		return;
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci	/* Only full duplex supported for now */
4438c2ecf20Sopenharmony_ci	ocelot_port_writel(ocelot_port, DEV_MAC_MODE_CFG_FDX_ENA |
4448c2ecf20Sopenharmony_ci			   mode, DEV_MAC_MODE_CFG);
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci	/* Disable HDX fast control */
4478c2ecf20Sopenharmony_ci	ocelot_port_writel(ocelot_port, DEV_PORT_MISC_HDX_FAST_DIS,
4488c2ecf20Sopenharmony_ci			   DEV_PORT_MISC);
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	/* SGMII only for now */
4518c2ecf20Sopenharmony_ci	ocelot_port_writel(ocelot_port, PCS1G_MODE_CFG_SGMII_MODE_ENA,
4528c2ecf20Sopenharmony_ci			   PCS1G_MODE_CFG);
4538c2ecf20Sopenharmony_ci	ocelot_port_writel(ocelot_port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG);
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci	/* Enable PCS */
4568c2ecf20Sopenharmony_ci	ocelot_port_writel(ocelot_port, PCS1G_CFG_PCS_ENA, PCS1G_CFG);
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	/* No aneg on SGMII */
4598c2ecf20Sopenharmony_ci	ocelot_port_writel(ocelot_port, 0, PCS1G_ANEG_CFG);
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_ci	/* No loopback */
4628c2ecf20Sopenharmony_ci	ocelot_port_writel(ocelot_port, 0, PCS1G_LB_CFG);
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci	/* Enable MAC module */
4658c2ecf20Sopenharmony_ci	ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
4668c2ecf20Sopenharmony_ci			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci	/* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of
4698c2ecf20Sopenharmony_ci	 * reset */
4708c2ecf20Sopenharmony_ci	ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(speed),
4718c2ecf20Sopenharmony_ci			   DEV_CLOCK_CFG);
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	/* No PFC */
4748c2ecf20Sopenharmony_ci	ocelot_write_gix(ocelot, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed),
4758c2ecf20Sopenharmony_ci			 ANA_PFC_PFC_CFG, port);
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci	/* Core: Enable port for frame transfer */
4788c2ecf20Sopenharmony_ci	ocelot_fields_write(ocelot, port,
4798c2ecf20Sopenharmony_ci			    QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci	/* Flow control */
4828c2ecf20Sopenharmony_ci	ocelot_write_rix(ocelot, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
4838c2ecf20Sopenharmony_ci			 SYS_MAC_FC_CFG_RX_FC_ENA | SYS_MAC_FC_CFG_TX_FC_ENA |
4848c2ecf20Sopenharmony_ci			 SYS_MAC_FC_CFG_ZERO_PAUSE_ENA |
4858c2ecf20Sopenharmony_ci			 SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
4868c2ecf20Sopenharmony_ci			 SYS_MAC_FC_CFG_FC_LINK_SPEED(speed),
4878c2ecf20Sopenharmony_ci			 SYS_MAC_FC_CFG, port);
4888c2ecf20Sopenharmony_ci	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
4898c2ecf20Sopenharmony_ci}
4908c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ocelot_adjust_link);
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_civoid ocelot_port_enable(struct ocelot *ocelot, int port,
4938c2ecf20Sopenharmony_ci			struct phy_device *phy)
4948c2ecf20Sopenharmony_ci{
4958c2ecf20Sopenharmony_ci	/* Enable receiving frames on the port, and activate auto-learning of
4968c2ecf20Sopenharmony_ci	 * MAC addresses.
4978c2ecf20Sopenharmony_ci	 */
4988c2ecf20Sopenharmony_ci	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
4998c2ecf20Sopenharmony_ci			 ANA_PORT_PORT_CFG_RECV_ENA |
5008c2ecf20Sopenharmony_ci			 ANA_PORT_PORT_CFG_PORTID_VAL(port),
5018c2ecf20Sopenharmony_ci			 ANA_PORT_PORT_CFG, port);
5028c2ecf20Sopenharmony_ci}
5038c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ocelot_port_enable);
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_civoid ocelot_port_disable(struct ocelot *ocelot, int port)
5068c2ecf20Sopenharmony_ci{
5078c2ecf20Sopenharmony_ci	struct ocelot_port *ocelot_port = ocelot->ports[port];
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ci	ocelot_port_writel(ocelot_port, 0, DEV_MAC_ENA_CFG);
5108c2ecf20Sopenharmony_ci	ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0);
5118c2ecf20Sopenharmony_ci}
5128c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ocelot_port_disable);
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_civoid ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port,
5158c2ecf20Sopenharmony_ci				  struct sk_buff *clone)
5168c2ecf20Sopenharmony_ci{
5178c2ecf20Sopenharmony_ci	struct ocelot_port *ocelot_port = ocelot->ports[port];
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci	spin_lock(&ocelot_port->ts_id_lock);
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci	skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS;
5228c2ecf20Sopenharmony_ci	/* Store timestamp ID in cb[0] of sk_buff */
5238c2ecf20Sopenharmony_ci	clone->cb[0] = ocelot_port->ts_id;
5248c2ecf20Sopenharmony_ci	ocelot_port->ts_id = (ocelot_port->ts_id + 1) % 4;
5258c2ecf20Sopenharmony_ci	skb_queue_tail(&ocelot_port->tx_skbs, clone);
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci	spin_unlock(&ocelot_port->ts_id_lock);
5288c2ecf20Sopenharmony_ci}
5298c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ocelot_port_add_txtstamp_skb);
5308c2ecf20Sopenharmony_ci
5318c2ecf20Sopenharmony_cistatic void ocelot_get_hwtimestamp(struct ocelot *ocelot,
5328c2ecf20Sopenharmony_ci				   struct timespec64 *ts)
5338c2ecf20Sopenharmony_ci{
5348c2ecf20Sopenharmony_ci	unsigned long flags;
5358c2ecf20Sopenharmony_ci	u32 val;
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ci	/* Read current PTP time to get seconds */
5408c2ecf20Sopenharmony_ci	val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_ci	val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
5438c2ecf20Sopenharmony_ci	val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
5448c2ecf20Sopenharmony_ci	ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
5458c2ecf20Sopenharmony_ci	ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci	/* Read packet HW timestamp from FIFO */
5488c2ecf20Sopenharmony_ci	val = ocelot_read(ocelot, SYS_PTP_TXSTAMP);
5498c2ecf20Sopenharmony_ci	ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val);
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_ci	/* Sec has incremented since the ts was registered */
5528c2ecf20Sopenharmony_ci	if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC))
5538c2ecf20Sopenharmony_ci		ts->tv_sec--;
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
5568c2ecf20Sopenharmony_ci}
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_civoid ocelot_get_txtstamp(struct ocelot *ocelot)
5598c2ecf20Sopenharmony_ci{
5608c2ecf20Sopenharmony_ci	int budget = OCELOT_PTP_QUEUE_SZ;
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_ci	while (budget--) {
5638c2ecf20Sopenharmony_ci		struct sk_buff *skb, *skb_tmp, *skb_match = NULL;
5648c2ecf20Sopenharmony_ci		struct skb_shared_hwtstamps shhwtstamps;
5658c2ecf20Sopenharmony_ci		struct ocelot_port *port;
5668c2ecf20Sopenharmony_ci		struct timespec64 ts;
5678c2ecf20Sopenharmony_ci		unsigned long flags;
5688c2ecf20Sopenharmony_ci		u32 val, id, txport;
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci		val = ocelot_read(ocelot, SYS_PTP_STATUS);
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ci		/* Check if a timestamp can be retrieved */
5738c2ecf20Sopenharmony_ci		if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD))
5748c2ecf20Sopenharmony_ci			break;
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_ci		WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL);
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_ci		/* Retrieve the ts ID and Tx port */
5798c2ecf20Sopenharmony_ci		id = SYS_PTP_STATUS_PTP_MESS_ID_X(val);
5808c2ecf20Sopenharmony_ci		txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val);
5818c2ecf20Sopenharmony_ci
5828c2ecf20Sopenharmony_ci		/* Retrieve its associated skb */
5838c2ecf20Sopenharmony_ci		port = ocelot->ports[txport];
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci		spin_lock_irqsave(&port->tx_skbs.lock, flags);
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci		skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
5888c2ecf20Sopenharmony_ci			if (skb->cb[0] != id)
5898c2ecf20Sopenharmony_ci				continue;
5908c2ecf20Sopenharmony_ci			__skb_unlink(skb, &port->tx_skbs);
5918c2ecf20Sopenharmony_ci			skb_match = skb;
5928c2ecf20Sopenharmony_ci			break;
5938c2ecf20Sopenharmony_ci		}
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&port->tx_skbs.lock, flags);
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_ci		if (WARN_ON(!skb_match))
5988c2ecf20Sopenharmony_ci			continue;
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_ci		/* Get the h/w timestamp */
6018c2ecf20Sopenharmony_ci		ocelot_get_hwtimestamp(ocelot, &ts);
6028c2ecf20Sopenharmony_ci
6038c2ecf20Sopenharmony_ci		/* Set the timestamp into the skb */
6048c2ecf20Sopenharmony_ci		memset(&shhwtstamps, 0, sizeof(shhwtstamps));
6058c2ecf20Sopenharmony_ci		shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
6068c2ecf20Sopenharmony_ci		skb_complete_tx_timestamp(skb_match, &shhwtstamps);
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci		/* Next ts */
6098c2ecf20Sopenharmony_ci		ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT);
6108c2ecf20Sopenharmony_ci	}
6118c2ecf20Sopenharmony_ci}
6128c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ocelot_get_txtstamp);
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ciint ocelot_fdb_add(struct ocelot *ocelot, int port,
6158c2ecf20Sopenharmony_ci		   const unsigned char *addr, u16 vid)
6168c2ecf20Sopenharmony_ci{
6178c2ecf20Sopenharmony_ci	struct ocelot_port *ocelot_port = ocelot->ports[port];
6188c2ecf20Sopenharmony_ci	int pgid = port;
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_ci	if (port == ocelot->npi)
6218c2ecf20Sopenharmony_ci		pgid = PGID_CPU;
6228c2ecf20Sopenharmony_ci
6238c2ecf20Sopenharmony_ci	if (!vid) {
6248c2ecf20Sopenharmony_ci		if (!ocelot_port->vlan_aware)
6258c2ecf20Sopenharmony_ci			/* If the bridge is not VLAN aware and no VID was
6268c2ecf20Sopenharmony_ci			 * provided, set it to pvid to ensure the MAC entry
6278c2ecf20Sopenharmony_ci			 * matches incoming untagged packets
6288c2ecf20Sopenharmony_ci			 */
6298c2ecf20Sopenharmony_ci			vid = ocelot_port->pvid;
6308c2ecf20Sopenharmony_ci		else
6318c2ecf20Sopenharmony_ci			/* If the bridge is VLAN aware a VID must be provided as
6328c2ecf20Sopenharmony_ci			 * otherwise the learnt entry wouldn't match any frame.
6338c2ecf20Sopenharmony_ci			 */
6348c2ecf20Sopenharmony_ci			return -EINVAL;
6358c2ecf20Sopenharmony_ci	}
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_ci	return ocelot_mact_learn(ocelot, pgid, addr, vid, ENTRYTYPE_LOCKED);
6388c2ecf20Sopenharmony_ci}
6398c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ocelot_fdb_add);
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_ciint ocelot_fdb_del(struct ocelot *ocelot, int port,
6428c2ecf20Sopenharmony_ci		   const unsigned char *addr, u16 vid)
6438c2ecf20Sopenharmony_ci{
6448c2ecf20Sopenharmony_ci	return ocelot_mact_forget(ocelot, addr, vid);
6458c2ecf20Sopenharmony_ci}
6468c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ocelot_fdb_del);
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_ciint ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid,
6498c2ecf20Sopenharmony_ci			    bool is_static, void *data)
6508c2ecf20Sopenharmony_ci{
6518c2ecf20Sopenharmony_ci	struct ocelot_dump_ctx *dump = data;
6528c2ecf20Sopenharmony_ci	u32 portid = NETLINK_CB(dump->cb->skb).portid;
6538c2ecf20Sopenharmony_ci	u32 seq = dump->cb->nlh->nlmsg_seq;
6548c2ecf20Sopenharmony_ci	struct nlmsghdr *nlh;
6558c2ecf20Sopenharmony_ci	struct ndmsg *ndm;
6568c2ecf20Sopenharmony_ci
6578c2ecf20Sopenharmony_ci	if (dump->idx < dump->cb->args[2])
6588c2ecf20Sopenharmony_ci		goto skip;
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_ci	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
6618c2ecf20Sopenharmony_ci			sizeof(*ndm), NLM_F_MULTI);
6628c2ecf20Sopenharmony_ci	if (!nlh)
6638c2ecf20Sopenharmony_ci		return -EMSGSIZE;
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci	ndm = nlmsg_data(nlh);
6668c2ecf20Sopenharmony_ci	ndm->ndm_family  = AF_BRIDGE;
6678c2ecf20Sopenharmony_ci	ndm->ndm_pad1    = 0;
6688c2ecf20Sopenharmony_ci	ndm->ndm_pad2    = 0;
6698c2ecf20Sopenharmony_ci	ndm->ndm_flags   = NTF_SELF;
6708c2ecf20Sopenharmony_ci	ndm->ndm_type    = 0;
6718c2ecf20Sopenharmony_ci	ndm->ndm_ifindex = dump->dev->ifindex;
6728c2ecf20Sopenharmony_ci	ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_ci	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
6758c2ecf20Sopenharmony_ci		goto nla_put_failure;
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_ci	if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
6788c2ecf20Sopenharmony_ci		goto nla_put_failure;
6798c2ecf20Sopenharmony_ci
6808c2ecf20Sopenharmony_ci	nlmsg_end(dump->skb, nlh);
6818c2ecf20Sopenharmony_ci
6828c2ecf20Sopenharmony_ciskip:
6838c2ecf20Sopenharmony_ci	dump->idx++;
6848c2ecf20Sopenharmony_ci	return 0;
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_cinla_put_failure:
6878c2ecf20Sopenharmony_ci	nlmsg_cancel(dump->skb, nlh);
6888c2ecf20Sopenharmony_ci	return -EMSGSIZE;
6898c2ecf20Sopenharmony_ci}
6908c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ocelot_port_fdb_do_dump);
6918c2ecf20Sopenharmony_ci
6928c2ecf20Sopenharmony_cistatic int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col,
6938c2ecf20Sopenharmony_ci			    struct ocelot_mact_entry *entry)
6948c2ecf20Sopenharmony_ci{
6958c2ecf20Sopenharmony_ci	u32 val, dst, macl, mach;
6968c2ecf20Sopenharmony_ci	char mac[ETH_ALEN];
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_ci	/* Set row and column to read from */
6998c2ecf20Sopenharmony_ci	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
7008c2ecf20Sopenharmony_ci	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_ci	/* Issue a read command */
7038c2ecf20Sopenharmony_ci	ocelot_write(ocelot,
7048c2ecf20Sopenharmony_ci		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
7058c2ecf20Sopenharmony_ci		     ANA_TABLES_MACACCESS);
7068c2ecf20Sopenharmony_ci
7078c2ecf20Sopenharmony_ci	if (ocelot_mact_wait_for_completion(ocelot))
7088c2ecf20Sopenharmony_ci		return -ETIMEDOUT;
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci	/* Read the entry flags */
7118c2ecf20Sopenharmony_ci	val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
7128c2ecf20Sopenharmony_ci	if (!(val & ANA_TABLES_MACACCESS_VALID))
7138c2ecf20Sopenharmony_ci		return -EINVAL;
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_ci	/* If the entry read has another port configured as its destination,
7168c2ecf20Sopenharmony_ci	 * do not report it.
7178c2ecf20Sopenharmony_ci	 */
7188c2ecf20Sopenharmony_ci	dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
7198c2ecf20Sopenharmony_ci	if (dst != port)
7208c2ecf20Sopenharmony_ci		return -EINVAL;
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_ci	/* Get the entry's MAC address and VLAN id */
7238c2ecf20Sopenharmony_ci	macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
7248c2ecf20Sopenharmony_ci	mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
7258c2ecf20Sopenharmony_ci
7268c2ecf20Sopenharmony_ci	mac[0] = (mach >> 8)  & 0xff;
7278c2ecf20Sopenharmony_ci	mac[1] = (mach >> 0)  & 0xff;
7288c2ecf20Sopenharmony_ci	mac[2] = (macl >> 24) & 0xff;
7298c2ecf20Sopenharmony_ci	mac[3] = (macl >> 16) & 0xff;
7308c2ecf20Sopenharmony_ci	mac[4] = (macl >> 8)  & 0xff;
7318c2ecf20Sopenharmony_ci	mac[5] = (macl >> 0)  & 0xff;
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci	entry->vid = (mach >> 16) & 0xfff;
7348c2ecf20Sopenharmony_ci	ether_addr_copy(entry->mac, mac);
7358c2ecf20Sopenharmony_ci
7368c2ecf20Sopenharmony_ci	return 0;
7378c2ecf20Sopenharmony_ci}
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_ciint ocelot_fdb_dump(struct ocelot *ocelot, int port,
7408c2ecf20Sopenharmony_ci		    dsa_fdb_dump_cb_t *cb, void *data)
7418c2ecf20Sopenharmony_ci{
7428c2ecf20Sopenharmony_ci	int i, j;
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_ci	/* Loop through all the mac tables entries. */
7458c2ecf20Sopenharmony_ci	for (i = 0; i < ocelot->num_mact_rows; i++) {
7468c2ecf20Sopenharmony_ci		for (j = 0; j < 4; j++) {
7478c2ecf20Sopenharmony_ci			struct ocelot_mact_entry entry;
7488c2ecf20Sopenharmony_ci			bool is_static;
7498c2ecf20Sopenharmony_ci			int ret;
7508c2ecf20Sopenharmony_ci
7518c2ecf20Sopenharmony_ci			ret = ocelot_mact_read(ocelot, port, i, j, &entry);
7528c2ecf20Sopenharmony_ci			/* If the entry is invalid (wrong port, invalid...),
7538c2ecf20Sopenharmony_ci			 * skip it.
7548c2ecf20Sopenharmony_ci			 */
7558c2ecf20Sopenharmony_ci			if (ret == -EINVAL)
7568c2ecf20Sopenharmony_ci				continue;
7578c2ecf20Sopenharmony_ci			else if (ret)
7588c2ecf20Sopenharmony_ci				return ret;
7598c2ecf20Sopenharmony_ci
7608c2ecf20Sopenharmony_ci			is_static = (entry.type == ENTRYTYPE_LOCKED);
7618c2ecf20Sopenharmony_ci
7628c2ecf20Sopenharmony_ci			ret = cb(entry.mac, entry.vid, is_static, data);
7638c2ecf20Sopenharmony_ci			if (ret)
7648c2ecf20Sopenharmony_ci				return ret;
7658c2ecf20Sopenharmony_ci		}
7668c2ecf20Sopenharmony_ci	}
7678c2ecf20Sopenharmony_ci
7688c2ecf20Sopenharmony_ci	return 0;
7698c2ecf20Sopenharmony_ci}
7708c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ocelot_fdb_dump);
7718c2ecf20Sopenharmony_ci
7728c2ecf20Sopenharmony_ciint ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr)
7738c2ecf20Sopenharmony_ci{
7748c2ecf20Sopenharmony_ci	return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config,
7758c2ecf20Sopenharmony_ci			    sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0;
7768c2ecf20Sopenharmony_ci}
7778c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ocelot_hwstamp_get);
7788c2ecf20Sopenharmony_ci
7798c2ecf20Sopenharmony_ciint ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr)
7808c2ecf20Sopenharmony_ci{
7818c2ecf20Sopenharmony_ci	struct ocelot_port *ocelot_port = ocelot->ports[port];
7828c2ecf20Sopenharmony_ci	struct hwtstamp_config cfg;
7838c2ecf20Sopenharmony_ci
7848c2ecf20Sopenharmony_ci	if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
7858c2ecf20Sopenharmony_ci		return -EFAULT;
7868c2ecf20Sopenharmony_ci
7878c2ecf20Sopenharmony_ci	/* reserved for future extensions */
7888c2ecf20Sopenharmony_ci	if (cfg.flags)
7898c2ecf20Sopenharmony_ci		return -EINVAL;
7908c2ecf20Sopenharmony_ci
7918c2ecf20Sopenharmony_ci	/* Tx type sanity check */
7928c2ecf20Sopenharmony_ci	switch (cfg.tx_type) {
7938c2ecf20Sopenharmony_ci	case HWTSTAMP_TX_ON:
7948c2ecf20Sopenharmony_ci		ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
7958c2ecf20Sopenharmony_ci		break;
7968c2ecf20Sopenharmony_ci	case HWTSTAMP_TX_ONESTEP_SYNC:
7978c2ecf20Sopenharmony_ci		/* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
7988c2ecf20Sopenharmony_ci		 * need to update the origin time.
7998c2ecf20Sopenharmony_ci		 */
8008c2ecf20Sopenharmony_ci		ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP;
8018c2ecf20Sopenharmony_ci		break;
8028c2ecf20Sopenharmony_ci	case HWTSTAMP_TX_OFF:
8038c2ecf20Sopenharmony_ci		ocelot_port->ptp_cmd = 0;
8048c2ecf20Sopenharmony_ci		break;
8058c2ecf20Sopenharmony_ci	default:
8068c2ecf20Sopenharmony_ci		return -ERANGE;
8078c2ecf20Sopenharmony_ci	}
8088c2ecf20Sopenharmony_ci
8098c2ecf20Sopenharmony_ci	mutex_lock(&ocelot->ptp_lock);
8108c2ecf20Sopenharmony_ci
8118c2ecf20Sopenharmony_ci	switch (cfg.rx_filter) {
8128c2ecf20Sopenharmony_ci	case HWTSTAMP_FILTER_NONE:
8138c2ecf20Sopenharmony_ci		break;
8148c2ecf20Sopenharmony_ci	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
8158c2ecf20Sopenharmony_ci	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
8168c2ecf20Sopenharmony_ci	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
8178c2ecf20Sopenharmony_ci	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
8188c2ecf20Sopenharmony_ci	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
8198c2ecf20Sopenharmony_ci	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
8208c2ecf20Sopenharmony_ci	case HWTSTAMP_FILTER_PTP_V2_EVENT:
8218c2ecf20Sopenharmony_ci	case HWTSTAMP_FILTER_PTP_V2_SYNC:
8228c2ecf20Sopenharmony_ci	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
8238c2ecf20Sopenharmony_ci		cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
8248c2ecf20Sopenharmony_ci		break;
8258c2ecf20Sopenharmony_ci	default:
8268c2ecf20Sopenharmony_ci		mutex_unlock(&ocelot->ptp_lock);
8278c2ecf20Sopenharmony_ci		return -ERANGE;
8288c2ecf20Sopenharmony_ci	}
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_ci	/* Commit back the result & save it */
8318c2ecf20Sopenharmony_ci	memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg));
8328c2ecf20Sopenharmony_ci	mutex_unlock(&ocelot->ptp_lock);
8338c2ecf20Sopenharmony_ci
8348c2ecf20Sopenharmony_ci	return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
8358c2ecf20Sopenharmony_ci}
8368c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ocelot_hwstamp_set);
8378c2ecf20Sopenharmony_ci
8388c2ecf20Sopenharmony_civoid ocelot_get_strings(struct ocelot *ocelot, int port, u32 sset, u8 *data)
8398c2ecf20Sopenharmony_ci{
8408c2ecf20Sopenharmony_ci	int i;
8418c2ecf20Sopenharmony_ci
8428c2ecf20Sopenharmony_ci	if (sset != ETH_SS_STATS)
8438c2ecf20Sopenharmony_ci		return;
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_ci	for (i = 0; i < ocelot->num_stats; i++)
8468c2ecf20Sopenharmony_ci		memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
8478c2ecf20Sopenharmony_ci		       ETH_GSTRING_LEN);
8488c2ecf20Sopenharmony_ci}
8498c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ocelot_get_strings);
8508c2ecf20Sopenharmony_ci
8518c2ecf20Sopenharmony_cistatic void ocelot_update_stats(struct ocelot *ocelot)
8528c2ecf20Sopenharmony_ci{
8538c2ecf20Sopenharmony_ci	int i, j;
8548c2ecf20Sopenharmony_ci
8558c2ecf20Sopenharmony_ci	mutex_lock(&ocelot->stats_lock);
8568c2ecf20Sopenharmony_ci
8578c2ecf20Sopenharmony_ci	for (i = 0; i < ocelot->num_phys_ports; i++) {
8588c2ecf20Sopenharmony_ci		/* Configure the port to read the stats from */
8598c2ecf20Sopenharmony_ci		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG);
8608c2ecf20Sopenharmony_ci
8618c2ecf20Sopenharmony_ci		for (j = 0; j < ocelot->num_stats; j++) {
8628c2ecf20Sopenharmony_ci			u32 val;
8638c2ecf20Sopenharmony_ci			unsigned int idx = i * ocelot->num_stats + j;
8648c2ecf20Sopenharmony_ci
8658c2ecf20Sopenharmony_ci			val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
8668c2ecf20Sopenharmony_ci					      ocelot->stats_layout[j].offset);
8678c2ecf20Sopenharmony_ci
8688c2ecf20Sopenharmony_ci			if (val < (ocelot->stats[idx] & U32_MAX))
8698c2ecf20Sopenharmony_ci				ocelot->stats[idx] += (u64)1 << 32;
8708c2ecf20Sopenharmony_ci
8718c2ecf20Sopenharmony_ci			ocelot->stats[idx] = (ocelot->stats[idx] &
8728c2ecf20Sopenharmony_ci					      ~(u64)U32_MAX) + val;
8738c2ecf20Sopenharmony_ci		}
8748c2ecf20Sopenharmony_ci	}
8758c2ecf20Sopenharmony_ci
8768c2ecf20Sopenharmony_ci	mutex_unlock(&ocelot->stats_lock);
8778c2ecf20Sopenharmony_ci}
8788c2ecf20Sopenharmony_ci
8798c2ecf20Sopenharmony_cistatic void ocelot_check_stats_work(struct work_struct *work)
8808c2ecf20Sopenharmony_ci{
8818c2ecf20Sopenharmony_ci	struct delayed_work *del_work = to_delayed_work(work);
8828c2ecf20Sopenharmony_ci	struct ocelot *ocelot = container_of(del_work, struct ocelot,
8838c2ecf20Sopenharmony_ci					     stats_work);
8848c2ecf20Sopenharmony_ci
8858c2ecf20Sopenharmony_ci	ocelot_update_stats(ocelot);
8868c2ecf20Sopenharmony_ci
8878c2ecf20Sopenharmony_ci	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
8888c2ecf20Sopenharmony_ci			   OCELOT_STATS_CHECK_DELAY);
8898c2ecf20Sopenharmony_ci}
8908c2ecf20Sopenharmony_ci
8918c2ecf20Sopenharmony_civoid ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data)
8928c2ecf20Sopenharmony_ci{
8938c2ecf20Sopenharmony_ci	int i;
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_ci	/* check and update now */
8968c2ecf20Sopenharmony_ci	ocelot_update_stats(ocelot);
8978c2ecf20Sopenharmony_ci
8988c2ecf20Sopenharmony_ci	/* Copy all counters */
8998c2ecf20Sopenharmony_ci	for (i = 0; i < ocelot->num_stats; i++)
9008c2ecf20Sopenharmony_ci		*data++ = ocelot->stats[port * ocelot->num_stats + i];
9018c2ecf20Sopenharmony_ci}
9028c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ocelot_get_ethtool_stats);
9038c2ecf20Sopenharmony_ci
9048c2ecf20Sopenharmony_ciint ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset)
9058c2ecf20Sopenharmony_ci{
9068c2ecf20Sopenharmony_ci	if (sset != ETH_SS_STATS)
9078c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
9088c2ecf20Sopenharmony_ci
9098c2ecf20Sopenharmony_ci	return ocelot->num_stats;
9108c2ecf20Sopenharmony_ci}
9118c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ocelot_get_sset_count);
9128c2ecf20Sopenharmony_ci
9138c2ecf20Sopenharmony_ciint ocelot_get_ts_info(struct ocelot *ocelot, int port,
9148c2ecf20Sopenharmony_ci		       struct ethtool_ts_info *info)
9158c2ecf20Sopenharmony_ci{
9168c2ecf20Sopenharmony_ci	info->phc_index = ocelot->ptp_clock ?
9178c2ecf20Sopenharmony_ci			  ptp_clock_index(ocelot->ptp_clock) : -1;
9188c2ecf20Sopenharmony_ci	if (info->phc_index == -1) {
9198c2ecf20Sopenharmony_ci		info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
9208c2ecf20Sopenharmony_ci					 SOF_TIMESTAMPING_RX_SOFTWARE |
9218c2ecf20Sopenharmony_ci					 SOF_TIMESTAMPING_SOFTWARE;
9228c2ecf20Sopenharmony_ci		return 0;
9238c2ecf20Sopenharmony_ci	}
9248c2ecf20Sopenharmony_ci	info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
9258c2ecf20Sopenharmony_ci				 SOF_TIMESTAMPING_RX_SOFTWARE |
9268c2ecf20Sopenharmony_ci				 SOF_TIMESTAMPING_SOFTWARE |
9278c2ecf20Sopenharmony_ci				 SOF_TIMESTAMPING_TX_HARDWARE |
9288c2ecf20Sopenharmony_ci				 SOF_TIMESTAMPING_RX_HARDWARE |
9298c2ecf20Sopenharmony_ci				 SOF_TIMESTAMPING_RAW_HARDWARE;
9308c2ecf20Sopenharmony_ci	info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) |
9318c2ecf20Sopenharmony_ci			 BIT(HWTSTAMP_TX_ONESTEP_SYNC);
9328c2ecf20Sopenharmony_ci	info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) |
9338c2ecf20Sopenharmony_ci			   BIT(HWTSTAMP_FILTER_PTP_V2_EVENT) |
9348c2ecf20Sopenharmony_ci			   BIT(HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
9358c2ecf20Sopenharmony_ci			   BIT(HWTSTAMP_FILTER_PTP_V2_L4_EVENT);
9368c2ecf20Sopenharmony_ci
9378c2ecf20Sopenharmony_ci	return 0;
9388c2ecf20Sopenharmony_ci}
9398c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ocelot_get_ts_info);
9408c2ecf20Sopenharmony_ci
9418c2ecf20Sopenharmony_civoid ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state)
9428c2ecf20Sopenharmony_ci{
9438c2ecf20Sopenharmony_ci	u32 port_cfg;
9448c2ecf20Sopenharmony_ci	int p, i;
9458c2ecf20Sopenharmony_ci
9468c2ecf20Sopenharmony_ci	if (!(BIT(port) & ocelot->bridge_mask))
9478c2ecf20Sopenharmony_ci		return;
9488c2ecf20Sopenharmony_ci
9498c2ecf20Sopenharmony_ci	port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port);
9508c2ecf20Sopenharmony_ci
9518c2ecf20Sopenharmony_ci	switch (state) {
9528c2ecf20Sopenharmony_ci	case BR_STATE_FORWARDING:
9538c2ecf20Sopenharmony_ci		ocelot->bridge_fwd_mask |= BIT(port);
9548c2ecf20Sopenharmony_ci		fallthrough;
9558c2ecf20Sopenharmony_ci	case BR_STATE_LEARNING:
9568c2ecf20Sopenharmony_ci		port_cfg |= ANA_PORT_PORT_CFG_LEARN_ENA;
9578c2ecf20Sopenharmony_ci		break;
9588c2ecf20Sopenharmony_ci
9598c2ecf20Sopenharmony_ci	default:
9608c2ecf20Sopenharmony_ci		port_cfg &= ~ANA_PORT_PORT_CFG_LEARN_ENA;
9618c2ecf20Sopenharmony_ci		ocelot->bridge_fwd_mask &= ~BIT(port);
9628c2ecf20Sopenharmony_ci		break;
9638c2ecf20Sopenharmony_ci	}
9648c2ecf20Sopenharmony_ci
9658c2ecf20Sopenharmony_ci	ocelot_write_gix(ocelot, port_cfg, ANA_PORT_PORT_CFG, port);
9668c2ecf20Sopenharmony_ci
9678c2ecf20Sopenharmony_ci	/* Apply FWD mask. The loop is needed to add/remove the current port as
9688c2ecf20Sopenharmony_ci	 * a source for the other ports.
9698c2ecf20Sopenharmony_ci	 */
9708c2ecf20Sopenharmony_ci	for (p = 0; p < ocelot->num_phys_ports; p++) {
9718c2ecf20Sopenharmony_ci		if (ocelot->bridge_fwd_mask & BIT(p)) {
9728c2ecf20Sopenharmony_ci			unsigned long mask = ocelot->bridge_fwd_mask & ~BIT(p);
9738c2ecf20Sopenharmony_ci
9748c2ecf20Sopenharmony_ci			for (i = 0; i < ocelot->num_phys_ports; i++) {
9758c2ecf20Sopenharmony_ci				unsigned long bond_mask = ocelot->lags[i];
9768c2ecf20Sopenharmony_ci
9778c2ecf20Sopenharmony_ci				if (!bond_mask)
9788c2ecf20Sopenharmony_ci					continue;
9798c2ecf20Sopenharmony_ci
9808c2ecf20Sopenharmony_ci				if (bond_mask & BIT(p)) {
9818c2ecf20Sopenharmony_ci					mask &= ~bond_mask;
9828c2ecf20Sopenharmony_ci					break;
9838c2ecf20Sopenharmony_ci				}
9848c2ecf20Sopenharmony_ci			}
9858c2ecf20Sopenharmony_ci
9868c2ecf20Sopenharmony_ci			ocelot_write_rix(ocelot, mask,
9878c2ecf20Sopenharmony_ci					 ANA_PGID_PGID, PGID_SRC + p);
9888c2ecf20Sopenharmony_ci		} else {
9898c2ecf20Sopenharmony_ci			ocelot_write_rix(ocelot, 0,
9908c2ecf20Sopenharmony_ci					 ANA_PGID_PGID, PGID_SRC + p);
9918c2ecf20Sopenharmony_ci		}
9928c2ecf20Sopenharmony_ci	}
9938c2ecf20Sopenharmony_ci}
9948c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ocelot_bridge_stp_state_set);
9958c2ecf20Sopenharmony_ci
9968c2ecf20Sopenharmony_civoid ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs)
9978c2ecf20Sopenharmony_ci{
9988c2ecf20Sopenharmony_ci	unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000);
9998c2ecf20Sopenharmony_ci
10008c2ecf20Sopenharmony_ci	/* Setting AGE_PERIOD to zero effectively disables automatic aging,
10018c2ecf20Sopenharmony_ci	 * which is clearly not what our intention is. So avoid that.
10028c2ecf20Sopenharmony_ci	 */
10038c2ecf20Sopenharmony_ci	if (!age_period)
10048c2ecf20Sopenharmony_ci		age_period = 1;
10058c2ecf20Sopenharmony_ci
10068c2ecf20Sopenharmony_ci	ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE);
10078c2ecf20Sopenharmony_ci}
10088c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ocelot_set_ageing_time);
10098c2ecf20Sopenharmony_ci
10108c2ecf20Sopenharmony_cistatic struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
10118c2ecf20Sopenharmony_ci						     const unsigned char *addr,
10128c2ecf20Sopenharmony_ci						     u16 vid)
10138c2ecf20Sopenharmony_ci{
10148c2ecf20Sopenharmony_ci	struct ocelot_multicast *mc;
10158c2ecf20Sopenharmony_ci
10168c2ecf20Sopenharmony_ci	list_for_each_entry(mc, &ocelot->multicast, list) {
10178c2ecf20Sopenharmony_ci		if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
10188c2ecf20Sopenharmony_ci			return mc;
10198c2ecf20Sopenharmony_ci	}
10208c2ecf20Sopenharmony_ci
10218c2ecf20Sopenharmony_ci	return NULL;
10228c2ecf20Sopenharmony_ci}
10238c2ecf20Sopenharmony_ci
10248c2ecf20Sopenharmony_cistatic enum macaccess_entry_type ocelot_classify_mdb(const unsigned char *addr)
10258c2ecf20Sopenharmony_ci{
10268c2ecf20Sopenharmony_ci	if (addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e)
10278c2ecf20Sopenharmony_ci		return ENTRYTYPE_MACv4;
10288c2ecf20Sopenharmony_ci	if (addr[0] == 0x33 && addr[1] == 0x33)
10298c2ecf20Sopenharmony_ci		return ENTRYTYPE_MACv6;
10308c2ecf20Sopenharmony_ci	return ENTRYTYPE_NORMAL;
10318c2ecf20Sopenharmony_ci}
10328c2ecf20Sopenharmony_ci
10338c2ecf20Sopenharmony_cistatic int ocelot_mdb_get_pgid(struct ocelot *ocelot,
10348c2ecf20Sopenharmony_ci			       enum macaccess_entry_type entry_type)
10358c2ecf20Sopenharmony_ci{
10368c2ecf20Sopenharmony_ci	int pgid;
10378c2ecf20Sopenharmony_ci
10388c2ecf20Sopenharmony_ci	/* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and
10398c2ecf20Sopenharmony_ci	 * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the
10408c2ecf20Sopenharmony_ci	 * destination mask table (PGID), the destination set is programmed as
10418c2ecf20Sopenharmony_ci	 * part of the entry MAC address.", and the DEST_IDX is set to 0.
10428c2ecf20Sopenharmony_ci	 */
10438c2ecf20Sopenharmony_ci	if (entry_type == ENTRYTYPE_MACv4 ||
10448c2ecf20Sopenharmony_ci	    entry_type == ENTRYTYPE_MACv6)
10458c2ecf20Sopenharmony_ci		return 0;
10468c2ecf20Sopenharmony_ci
10478c2ecf20Sopenharmony_ci	for_each_nonreserved_multicast_dest_pgid(ocelot, pgid) {
10488c2ecf20Sopenharmony_ci		struct ocelot_multicast *mc;
10498c2ecf20Sopenharmony_ci		bool used = false;
10508c2ecf20Sopenharmony_ci
10518c2ecf20Sopenharmony_ci		list_for_each_entry(mc, &ocelot->multicast, list) {
10528c2ecf20Sopenharmony_ci			if (mc->pgid == pgid) {
10538c2ecf20Sopenharmony_ci				used = true;
10548c2ecf20Sopenharmony_ci				break;
10558c2ecf20Sopenharmony_ci			}
10568c2ecf20Sopenharmony_ci		}
10578c2ecf20Sopenharmony_ci
10588c2ecf20Sopenharmony_ci		if (!used)
10598c2ecf20Sopenharmony_ci			return pgid;
10608c2ecf20Sopenharmony_ci	}
10618c2ecf20Sopenharmony_ci
10628c2ecf20Sopenharmony_ci	return -1;
10638c2ecf20Sopenharmony_ci}
10648c2ecf20Sopenharmony_ci
10658c2ecf20Sopenharmony_cistatic void ocelot_encode_ports_to_mdb(unsigned char *addr,
10668c2ecf20Sopenharmony_ci				       struct ocelot_multicast *mc,
10678c2ecf20Sopenharmony_ci				       enum macaccess_entry_type entry_type)
10688c2ecf20Sopenharmony_ci{
10698c2ecf20Sopenharmony_ci	memcpy(addr, mc->addr, ETH_ALEN);
10708c2ecf20Sopenharmony_ci
10718c2ecf20Sopenharmony_ci	if (entry_type == ENTRYTYPE_MACv4) {
10728c2ecf20Sopenharmony_ci		addr[0] = 0;
10738c2ecf20Sopenharmony_ci		addr[1] = mc->ports >> 8;
10748c2ecf20Sopenharmony_ci		addr[2] = mc->ports & 0xff;
10758c2ecf20Sopenharmony_ci	} else if (entry_type == ENTRYTYPE_MACv6) {
10768c2ecf20Sopenharmony_ci		addr[0] = mc->ports >> 8;
10778c2ecf20Sopenharmony_ci		addr[1] = mc->ports & 0xff;
10788c2ecf20Sopenharmony_ci	}
10798c2ecf20Sopenharmony_ci}
10808c2ecf20Sopenharmony_ci
10818c2ecf20Sopenharmony_ciint ocelot_port_mdb_add(struct ocelot *ocelot, int port,
10828c2ecf20Sopenharmony_ci			const struct switchdev_obj_port_mdb *mdb)
10838c2ecf20Sopenharmony_ci{
10848c2ecf20Sopenharmony_ci	struct ocelot_port *ocelot_port = ocelot->ports[port];
10858c2ecf20Sopenharmony_ci	enum macaccess_entry_type entry_type;
10868c2ecf20Sopenharmony_ci	unsigned char addr[ETH_ALEN];
10878c2ecf20Sopenharmony_ci	struct ocelot_multicast *mc;
10888c2ecf20Sopenharmony_ci	u16 vid = mdb->vid;
10898c2ecf20Sopenharmony_ci	bool new = false;
10908c2ecf20Sopenharmony_ci
10918c2ecf20Sopenharmony_ci	if (port == ocelot->npi)
10928c2ecf20Sopenharmony_ci		port = ocelot->num_phys_ports;
10938c2ecf20Sopenharmony_ci
10948c2ecf20Sopenharmony_ci	if (!vid)
10958c2ecf20Sopenharmony_ci		vid = ocelot_port->pvid;
10968c2ecf20Sopenharmony_ci
10978c2ecf20Sopenharmony_ci	entry_type = ocelot_classify_mdb(mdb->addr);
10988c2ecf20Sopenharmony_ci
10998c2ecf20Sopenharmony_ci	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
11008c2ecf20Sopenharmony_ci	if (!mc) {
11018c2ecf20Sopenharmony_ci		int pgid = ocelot_mdb_get_pgid(ocelot, entry_type);
11028c2ecf20Sopenharmony_ci
11038c2ecf20Sopenharmony_ci		if (pgid < 0) {
11048c2ecf20Sopenharmony_ci			dev_err(ocelot->dev,
11058c2ecf20Sopenharmony_ci				"No more PGIDs available for mdb %pM vid %d\n",
11068c2ecf20Sopenharmony_ci				mdb->addr, vid);
11078c2ecf20Sopenharmony_ci			return -ENOSPC;
11088c2ecf20Sopenharmony_ci		}
11098c2ecf20Sopenharmony_ci
11108c2ecf20Sopenharmony_ci		mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
11118c2ecf20Sopenharmony_ci		if (!mc)
11128c2ecf20Sopenharmony_ci			return -ENOMEM;
11138c2ecf20Sopenharmony_ci
11148c2ecf20Sopenharmony_ci		memcpy(mc->addr, mdb->addr, ETH_ALEN);
11158c2ecf20Sopenharmony_ci		mc->vid = vid;
11168c2ecf20Sopenharmony_ci		mc->pgid = pgid;
11178c2ecf20Sopenharmony_ci
11188c2ecf20Sopenharmony_ci		list_add_tail(&mc->list, &ocelot->multicast);
11198c2ecf20Sopenharmony_ci		new = true;
11208c2ecf20Sopenharmony_ci	}
11218c2ecf20Sopenharmony_ci
11228c2ecf20Sopenharmony_ci	if (!new) {
11238c2ecf20Sopenharmony_ci		ocelot_encode_ports_to_mdb(addr, mc, entry_type);
11248c2ecf20Sopenharmony_ci		ocelot_mact_forget(ocelot, addr, vid);
11258c2ecf20Sopenharmony_ci	}
11268c2ecf20Sopenharmony_ci
11278c2ecf20Sopenharmony_ci	mc->ports |= BIT(port);
11288c2ecf20Sopenharmony_ci	ocelot_encode_ports_to_mdb(addr, mc, entry_type);
11298c2ecf20Sopenharmony_ci
11308c2ecf20Sopenharmony_ci	return ocelot_mact_learn(ocelot, mc->pgid, addr, vid, entry_type);
11318c2ecf20Sopenharmony_ci}
11328c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ocelot_port_mdb_add);
11338c2ecf20Sopenharmony_ci
11348c2ecf20Sopenharmony_ciint ocelot_port_mdb_del(struct ocelot *ocelot, int port,
11358c2ecf20Sopenharmony_ci			const struct switchdev_obj_port_mdb *mdb)
11368c2ecf20Sopenharmony_ci{
11378c2ecf20Sopenharmony_ci	struct ocelot_port *ocelot_port = ocelot->ports[port];
11388c2ecf20Sopenharmony_ci	enum macaccess_entry_type entry_type;
11398c2ecf20Sopenharmony_ci	unsigned char addr[ETH_ALEN];
11408c2ecf20Sopenharmony_ci	struct ocelot_multicast *mc;
11418c2ecf20Sopenharmony_ci	u16 vid = mdb->vid;
11428c2ecf20Sopenharmony_ci
11438c2ecf20Sopenharmony_ci	if (port == ocelot->npi)
11448c2ecf20Sopenharmony_ci		port = ocelot->num_phys_ports;
11458c2ecf20Sopenharmony_ci
11468c2ecf20Sopenharmony_ci	if (!vid)
11478c2ecf20Sopenharmony_ci		vid = ocelot_port->pvid;
11488c2ecf20Sopenharmony_ci
11498c2ecf20Sopenharmony_ci	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
11508c2ecf20Sopenharmony_ci	if (!mc)
11518c2ecf20Sopenharmony_ci		return -ENOENT;
11528c2ecf20Sopenharmony_ci
11538c2ecf20Sopenharmony_ci	entry_type = ocelot_classify_mdb(mdb->addr);
11548c2ecf20Sopenharmony_ci
11558c2ecf20Sopenharmony_ci	ocelot_encode_ports_to_mdb(addr, mc, entry_type);
11568c2ecf20Sopenharmony_ci	ocelot_mact_forget(ocelot, addr, vid);
11578c2ecf20Sopenharmony_ci
11588c2ecf20Sopenharmony_ci	mc->ports &= ~BIT(port);
11598c2ecf20Sopenharmony_ci	if (!mc->ports) {
11608c2ecf20Sopenharmony_ci		list_del(&mc->list);
11618c2ecf20Sopenharmony_ci		devm_kfree(ocelot->dev, mc);
11628c2ecf20Sopenharmony_ci		return 0;
11638c2ecf20Sopenharmony_ci	}
11648c2ecf20Sopenharmony_ci
11658c2ecf20Sopenharmony_ci	ocelot_encode_ports_to_mdb(addr, mc, entry_type);
11668c2ecf20Sopenharmony_ci
11678c2ecf20Sopenharmony_ci	return ocelot_mact_learn(ocelot, mc->pgid, addr, vid, entry_type);
11688c2ecf20Sopenharmony_ci}
11698c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ocelot_port_mdb_del);
11708c2ecf20Sopenharmony_ci
11718c2ecf20Sopenharmony_ciint ocelot_port_bridge_join(struct ocelot *ocelot, int port,
11728c2ecf20Sopenharmony_ci			    struct net_device *bridge)
11738c2ecf20Sopenharmony_ci{
11748c2ecf20Sopenharmony_ci	if (!ocelot->bridge_mask) {
11758c2ecf20Sopenharmony_ci		ocelot->hw_bridge_dev = bridge;
11768c2ecf20Sopenharmony_ci	} else {
11778c2ecf20Sopenharmony_ci		if (ocelot->hw_bridge_dev != bridge)
11788c2ecf20Sopenharmony_ci			/* This is adding the port to a second bridge, this is
11798c2ecf20Sopenharmony_ci			 * unsupported */
11808c2ecf20Sopenharmony_ci			return -ENODEV;
11818c2ecf20Sopenharmony_ci	}
11828c2ecf20Sopenharmony_ci
11838c2ecf20Sopenharmony_ci	ocelot->bridge_mask |= BIT(port);
11848c2ecf20Sopenharmony_ci
11858c2ecf20Sopenharmony_ci	return 0;
11868c2ecf20Sopenharmony_ci}
11878c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ocelot_port_bridge_join);
11888c2ecf20Sopenharmony_ci
11898c2ecf20Sopenharmony_ciint ocelot_port_bridge_leave(struct ocelot *ocelot, int port,
11908c2ecf20Sopenharmony_ci			     struct net_device *bridge)
11918c2ecf20Sopenharmony_ci{
11928c2ecf20Sopenharmony_ci	struct switchdev_trans trans;
11938c2ecf20Sopenharmony_ci	int ret;
11948c2ecf20Sopenharmony_ci
11958c2ecf20Sopenharmony_ci	ocelot->bridge_mask &= ~BIT(port);
11968c2ecf20Sopenharmony_ci
11978c2ecf20Sopenharmony_ci	if (!ocelot->bridge_mask)
11988c2ecf20Sopenharmony_ci		ocelot->hw_bridge_dev = NULL;
11998c2ecf20Sopenharmony_ci
12008c2ecf20Sopenharmony_ci	trans.ph_prepare = true;
12018c2ecf20Sopenharmony_ci	ret = ocelot_port_vlan_filtering(ocelot, port, false, &trans);
12028c2ecf20Sopenharmony_ci	if (ret)
12038c2ecf20Sopenharmony_ci		return ret;
12048c2ecf20Sopenharmony_ci
12058c2ecf20Sopenharmony_ci	trans.ph_prepare = false;
12068c2ecf20Sopenharmony_ci	ret = ocelot_port_vlan_filtering(ocelot, port, false, &trans);
12078c2ecf20Sopenharmony_ci	if (ret)
12088c2ecf20Sopenharmony_ci		return ret;
12098c2ecf20Sopenharmony_ci
12108c2ecf20Sopenharmony_ci	ocelot_port_set_pvid(ocelot, port, 0);
12118c2ecf20Sopenharmony_ci	return ocelot_port_set_native_vlan(ocelot, port, 0);
12128c2ecf20Sopenharmony_ci}
12138c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ocelot_port_bridge_leave);
12148c2ecf20Sopenharmony_ci
12158c2ecf20Sopenharmony_cistatic void ocelot_set_aggr_pgids(struct ocelot *ocelot)
12168c2ecf20Sopenharmony_ci{
12178c2ecf20Sopenharmony_ci	int i, port, lag;
12188c2ecf20Sopenharmony_ci
12198c2ecf20Sopenharmony_ci	/* Reset destination and aggregation PGIDS */
12208c2ecf20Sopenharmony_ci	for_each_unicast_dest_pgid(ocelot, port)
12218c2ecf20Sopenharmony_ci		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
12228c2ecf20Sopenharmony_ci
12238c2ecf20Sopenharmony_ci	for_each_aggr_pgid(ocelot, i)
12248c2ecf20Sopenharmony_ci		ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
12258c2ecf20Sopenharmony_ci				 ANA_PGID_PGID, i);
12268c2ecf20Sopenharmony_ci
12278c2ecf20Sopenharmony_ci	/* Now, set PGIDs for each LAG */
12288c2ecf20Sopenharmony_ci	for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
12298c2ecf20Sopenharmony_ci		unsigned long bond_mask;
12308c2ecf20Sopenharmony_ci		int aggr_count = 0;
12318c2ecf20Sopenharmony_ci		u8 aggr_idx[16];
12328c2ecf20Sopenharmony_ci
12338c2ecf20Sopenharmony_ci		bond_mask = ocelot->lags[lag];
12348c2ecf20Sopenharmony_ci		if (!bond_mask)
12358c2ecf20Sopenharmony_ci			continue;
12368c2ecf20Sopenharmony_ci
12378c2ecf20Sopenharmony_ci		for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
12388c2ecf20Sopenharmony_ci			// Destination mask
12398c2ecf20Sopenharmony_ci			ocelot_write_rix(ocelot, bond_mask,
12408c2ecf20Sopenharmony_ci					 ANA_PGID_PGID, port);
12418c2ecf20Sopenharmony_ci			aggr_idx[aggr_count] = port;
12428c2ecf20Sopenharmony_ci			aggr_count++;
12438c2ecf20Sopenharmony_ci		}
12448c2ecf20Sopenharmony_ci
12458c2ecf20Sopenharmony_ci		for_each_aggr_pgid(ocelot, i) {
12468c2ecf20Sopenharmony_ci			u32 ac;
12478c2ecf20Sopenharmony_ci
12488c2ecf20Sopenharmony_ci			ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
12498c2ecf20Sopenharmony_ci			ac &= ~bond_mask;
12508c2ecf20Sopenharmony_ci			ac |= BIT(aggr_idx[i % aggr_count]);
12518c2ecf20Sopenharmony_ci			ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
12528c2ecf20Sopenharmony_ci		}
12538c2ecf20Sopenharmony_ci	}
12548c2ecf20Sopenharmony_ci}
12558c2ecf20Sopenharmony_ci
12568c2ecf20Sopenharmony_cistatic void ocelot_setup_lag(struct ocelot *ocelot, int lag)
12578c2ecf20Sopenharmony_ci{
12588c2ecf20Sopenharmony_ci	unsigned long bond_mask = ocelot->lags[lag];
12598c2ecf20Sopenharmony_ci	unsigned int p;
12608c2ecf20Sopenharmony_ci
12618c2ecf20Sopenharmony_ci	for_each_set_bit(p, &bond_mask, ocelot->num_phys_ports) {
12628c2ecf20Sopenharmony_ci		u32 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, p);
12638c2ecf20Sopenharmony_ci
12648c2ecf20Sopenharmony_ci		port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
12658c2ecf20Sopenharmony_ci
12668c2ecf20Sopenharmony_ci		/* Use lag port as logical port for port i */
12678c2ecf20Sopenharmony_ci		ocelot_write_gix(ocelot, port_cfg |
12688c2ecf20Sopenharmony_ci				 ANA_PORT_PORT_CFG_PORTID_VAL(lag),
12698c2ecf20Sopenharmony_ci				 ANA_PORT_PORT_CFG, p);
12708c2ecf20Sopenharmony_ci	}
12718c2ecf20Sopenharmony_ci}
12728c2ecf20Sopenharmony_ci
12738c2ecf20Sopenharmony_ciint ocelot_port_lag_join(struct ocelot *ocelot, int port,
12748c2ecf20Sopenharmony_ci			 struct net_device *bond)
12758c2ecf20Sopenharmony_ci{
12768c2ecf20Sopenharmony_ci	struct net_device *ndev;
12778c2ecf20Sopenharmony_ci	u32 bond_mask = 0;
12788c2ecf20Sopenharmony_ci	int lag, lp;
12798c2ecf20Sopenharmony_ci
12808c2ecf20Sopenharmony_ci	rcu_read_lock();
12818c2ecf20Sopenharmony_ci	for_each_netdev_in_bond_rcu(bond, ndev) {
12828c2ecf20Sopenharmony_ci		struct ocelot_port_private *priv = netdev_priv(ndev);
12838c2ecf20Sopenharmony_ci
12848c2ecf20Sopenharmony_ci		bond_mask |= BIT(priv->chip_port);
12858c2ecf20Sopenharmony_ci	}
12868c2ecf20Sopenharmony_ci	rcu_read_unlock();
12878c2ecf20Sopenharmony_ci
12888c2ecf20Sopenharmony_ci	lp = __ffs(bond_mask);
12898c2ecf20Sopenharmony_ci
12908c2ecf20Sopenharmony_ci	/* If the new port is the lowest one, use it as the logical port from
12918c2ecf20Sopenharmony_ci	 * now on
12928c2ecf20Sopenharmony_ci	 */
12938c2ecf20Sopenharmony_ci	if (port == lp) {
12948c2ecf20Sopenharmony_ci		lag = port;
12958c2ecf20Sopenharmony_ci		ocelot->lags[port] = bond_mask;
12968c2ecf20Sopenharmony_ci		bond_mask &= ~BIT(port);
12978c2ecf20Sopenharmony_ci		if (bond_mask) {
12988c2ecf20Sopenharmony_ci			lp = __ffs(bond_mask);
12998c2ecf20Sopenharmony_ci			ocelot->lags[lp] = 0;
13008c2ecf20Sopenharmony_ci		}
13018c2ecf20Sopenharmony_ci	} else {
13028c2ecf20Sopenharmony_ci		lag = lp;
13038c2ecf20Sopenharmony_ci		ocelot->lags[lp] |= BIT(port);
13048c2ecf20Sopenharmony_ci	}
13058c2ecf20Sopenharmony_ci
13068c2ecf20Sopenharmony_ci	ocelot_setup_lag(ocelot, lag);
13078c2ecf20Sopenharmony_ci	ocelot_set_aggr_pgids(ocelot);
13088c2ecf20Sopenharmony_ci
13098c2ecf20Sopenharmony_ci	return 0;
13108c2ecf20Sopenharmony_ci}
13118c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ocelot_port_lag_join);
13128c2ecf20Sopenharmony_ci
13138c2ecf20Sopenharmony_civoid ocelot_port_lag_leave(struct ocelot *ocelot, int port,
13148c2ecf20Sopenharmony_ci			   struct net_device *bond)
13158c2ecf20Sopenharmony_ci{
13168c2ecf20Sopenharmony_ci	u32 port_cfg;
13178c2ecf20Sopenharmony_ci	int i;
13188c2ecf20Sopenharmony_ci
13198c2ecf20Sopenharmony_ci	/* Remove port from any lag */
13208c2ecf20Sopenharmony_ci	for (i = 0; i < ocelot->num_phys_ports; i++)
13218c2ecf20Sopenharmony_ci		ocelot->lags[i] &= ~BIT(port);
13228c2ecf20Sopenharmony_ci
13238c2ecf20Sopenharmony_ci	/* if it was the logical port of the lag, move the lag config to the
13248c2ecf20Sopenharmony_ci	 * next port
13258c2ecf20Sopenharmony_ci	 */
13268c2ecf20Sopenharmony_ci	if (ocelot->lags[port]) {
13278c2ecf20Sopenharmony_ci		int n = __ffs(ocelot->lags[port]);
13288c2ecf20Sopenharmony_ci
13298c2ecf20Sopenharmony_ci		ocelot->lags[n] = ocelot->lags[port];
13308c2ecf20Sopenharmony_ci		ocelot->lags[port] = 0;
13318c2ecf20Sopenharmony_ci
13328c2ecf20Sopenharmony_ci		ocelot_setup_lag(ocelot, n);
13338c2ecf20Sopenharmony_ci	}
13348c2ecf20Sopenharmony_ci
13358c2ecf20Sopenharmony_ci	port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port);
13368c2ecf20Sopenharmony_ci	port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
13378c2ecf20Sopenharmony_ci	ocelot_write_gix(ocelot, port_cfg | ANA_PORT_PORT_CFG_PORTID_VAL(port),
13388c2ecf20Sopenharmony_ci			 ANA_PORT_PORT_CFG, port);
13398c2ecf20Sopenharmony_ci
13408c2ecf20Sopenharmony_ci	ocelot_set_aggr_pgids(ocelot);
13418c2ecf20Sopenharmony_ci}
13428c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ocelot_port_lag_leave);
13438c2ecf20Sopenharmony_ci
13448c2ecf20Sopenharmony_ci/* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu.
13458c2ecf20Sopenharmony_ci * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG.
13468c2ecf20Sopenharmony_ci * In the special case that it's the NPI port that we're configuring, the
13478c2ecf20Sopenharmony_ci * length of the tag and optional prefix needs to be accounted for privately,
13488c2ecf20Sopenharmony_ci * in order to be able to sustain communication at the requested @sdu.
13498c2ecf20Sopenharmony_ci */
13508c2ecf20Sopenharmony_civoid ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu)
13518c2ecf20Sopenharmony_ci{
13528c2ecf20Sopenharmony_ci	struct ocelot_port *ocelot_port = ocelot->ports[port];
13538c2ecf20Sopenharmony_ci	int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN;
13548c2ecf20Sopenharmony_ci	int pause_start, pause_stop;
13558c2ecf20Sopenharmony_ci	int atop, atop_tot;
13568c2ecf20Sopenharmony_ci
13578c2ecf20Sopenharmony_ci	if (port == ocelot->npi) {
13588c2ecf20Sopenharmony_ci		maxlen += OCELOT_TAG_LEN;
13598c2ecf20Sopenharmony_ci
13608c2ecf20Sopenharmony_ci		if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_SHORT)
13618c2ecf20Sopenharmony_ci			maxlen += OCELOT_SHORT_PREFIX_LEN;
13628c2ecf20Sopenharmony_ci		else if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_LONG)
13638c2ecf20Sopenharmony_ci			maxlen += OCELOT_LONG_PREFIX_LEN;
13648c2ecf20Sopenharmony_ci	}
13658c2ecf20Sopenharmony_ci
13668c2ecf20Sopenharmony_ci	ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG);
13678c2ecf20Sopenharmony_ci
13688c2ecf20Sopenharmony_ci	/* Set Pause watermark hysteresis */
13698c2ecf20Sopenharmony_ci	pause_start = 6 * maxlen / OCELOT_BUFFER_CELL_SZ;
13708c2ecf20Sopenharmony_ci	pause_stop = 4 * maxlen / OCELOT_BUFFER_CELL_SZ;
13718c2ecf20Sopenharmony_ci	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_START,
13728c2ecf20Sopenharmony_ci			    pause_start);
13738c2ecf20Sopenharmony_ci	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_STOP,
13748c2ecf20Sopenharmony_ci			    pause_stop);
13758c2ecf20Sopenharmony_ci
13768c2ecf20Sopenharmony_ci	/* Tail dropping watermarks */
13778c2ecf20Sopenharmony_ci	atop_tot = (ocelot->shared_queue_sz - 9 * maxlen) /
13788c2ecf20Sopenharmony_ci		   OCELOT_BUFFER_CELL_SZ;
13798c2ecf20Sopenharmony_ci	atop = (9 * maxlen) / OCELOT_BUFFER_CELL_SZ;
13808c2ecf20Sopenharmony_ci	ocelot_write_rix(ocelot, ocelot->ops->wm_enc(atop), SYS_ATOP, port);
13818c2ecf20Sopenharmony_ci	ocelot_write(ocelot, ocelot->ops->wm_enc(atop_tot), SYS_ATOP_TOT_CFG);
13828c2ecf20Sopenharmony_ci}
13838c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ocelot_port_set_maxlen);
13848c2ecf20Sopenharmony_ci
13858c2ecf20Sopenharmony_ciint ocelot_get_max_mtu(struct ocelot *ocelot, int port)
13868c2ecf20Sopenharmony_ci{
13878c2ecf20Sopenharmony_ci	int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN;
13888c2ecf20Sopenharmony_ci
13898c2ecf20Sopenharmony_ci	if (port == ocelot->npi) {
13908c2ecf20Sopenharmony_ci		max_mtu -= OCELOT_TAG_LEN;
13918c2ecf20Sopenharmony_ci
13928c2ecf20Sopenharmony_ci		if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_SHORT)
13938c2ecf20Sopenharmony_ci			max_mtu -= OCELOT_SHORT_PREFIX_LEN;
13948c2ecf20Sopenharmony_ci		else if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_LONG)
13958c2ecf20Sopenharmony_ci			max_mtu -= OCELOT_LONG_PREFIX_LEN;
13968c2ecf20Sopenharmony_ci	}
13978c2ecf20Sopenharmony_ci
13988c2ecf20Sopenharmony_ci	return max_mtu;
13998c2ecf20Sopenharmony_ci}
14008c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ocelot_get_max_mtu);
14018c2ecf20Sopenharmony_ci
14028c2ecf20Sopenharmony_civoid ocelot_init_port(struct ocelot *ocelot, int port)
14038c2ecf20Sopenharmony_ci{
14048c2ecf20Sopenharmony_ci	struct ocelot_port *ocelot_port = ocelot->ports[port];
14058c2ecf20Sopenharmony_ci
14068c2ecf20Sopenharmony_ci	skb_queue_head_init(&ocelot_port->tx_skbs);
14078c2ecf20Sopenharmony_ci	spin_lock_init(&ocelot_port->ts_id_lock);
14088c2ecf20Sopenharmony_ci
14098c2ecf20Sopenharmony_ci	/* Basic L2 initialization */
14108c2ecf20Sopenharmony_ci
14118c2ecf20Sopenharmony_ci	/* Set MAC IFG Gaps
14128c2ecf20Sopenharmony_ci	 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
14138c2ecf20Sopenharmony_ci	 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
14148c2ecf20Sopenharmony_ci	 */
14158c2ecf20Sopenharmony_ci	ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5),
14168c2ecf20Sopenharmony_ci			   DEV_MAC_IFG_CFG);
14178c2ecf20Sopenharmony_ci
14188c2ecf20Sopenharmony_ci	/* Load seed (0) and set MAC HDX late collision  */
14198c2ecf20Sopenharmony_ci	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
14208c2ecf20Sopenharmony_ci			   DEV_MAC_HDX_CFG_SEED_LOAD,
14218c2ecf20Sopenharmony_ci			   DEV_MAC_HDX_CFG);
14228c2ecf20Sopenharmony_ci	mdelay(1);
14238c2ecf20Sopenharmony_ci	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
14248c2ecf20Sopenharmony_ci			   DEV_MAC_HDX_CFG);
14258c2ecf20Sopenharmony_ci
14268c2ecf20Sopenharmony_ci	/* Set Max Length and maximum tags allowed */
14278c2ecf20Sopenharmony_ci	ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN);
14288c2ecf20Sopenharmony_ci	ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
14298c2ecf20Sopenharmony_ci			   DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
14308c2ecf20Sopenharmony_ci			   DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA |
14318c2ecf20Sopenharmony_ci			   DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
14328c2ecf20Sopenharmony_ci			   DEV_MAC_TAGS_CFG);
14338c2ecf20Sopenharmony_ci
14348c2ecf20Sopenharmony_ci	/* Set SMAC of Pause frame (00:00:00:00:00:00) */
14358c2ecf20Sopenharmony_ci	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
14368c2ecf20Sopenharmony_ci	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG);
14378c2ecf20Sopenharmony_ci
14388c2ecf20Sopenharmony_ci	/* Enable transmission of pause frames */
14398c2ecf20Sopenharmony_ci	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
14408c2ecf20Sopenharmony_ci
14418c2ecf20Sopenharmony_ci	/* Drop frames with multicast source address */
14428c2ecf20Sopenharmony_ci	ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
14438c2ecf20Sopenharmony_ci		       ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
14448c2ecf20Sopenharmony_ci		       ANA_PORT_DROP_CFG, port);
14458c2ecf20Sopenharmony_ci
14468c2ecf20Sopenharmony_ci	/* Set default VLAN and tag type to 8021Q. */
14478c2ecf20Sopenharmony_ci	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q),
14488c2ecf20Sopenharmony_ci		       REW_PORT_VLAN_CFG_PORT_TPID_M,
14498c2ecf20Sopenharmony_ci		       REW_PORT_VLAN_CFG, port);
14508c2ecf20Sopenharmony_ci
14518c2ecf20Sopenharmony_ci	/* Enable vcap lookups */
14528c2ecf20Sopenharmony_ci	ocelot_vcap_enable(ocelot, port);
14538c2ecf20Sopenharmony_ci}
14548c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ocelot_init_port);
14558c2ecf20Sopenharmony_ci
14568c2ecf20Sopenharmony_ci/* Configure and enable the CPU port module, which is a set of queues
14578c2ecf20Sopenharmony_ci * accessible through register MMIO, frame DMA or Ethernet (in case
14588c2ecf20Sopenharmony_ci * NPI mode is used).
14598c2ecf20Sopenharmony_ci */
14608c2ecf20Sopenharmony_cistatic void ocelot_cpu_port_init(struct ocelot *ocelot)
14618c2ecf20Sopenharmony_ci{
14628c2ecf20Sopenharmony_ci	int cpu = ocelot->num_phys_ports;
14638c2ecf20Sopenharmony_ci
14648c2ecf20Sopenharmony_ci	/* The unicast destination PGID for the CPU port module is unused */
14658c2ecf20Sopenharmony_ci	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
14668c2ecf20Sopenharmony_ci	/* Instead set up a multicast destination PGID for traffic copied to
14678c2ecf20Sopenharmony_ci	 * the CPU. Whitelisted MAC addresses like the port netdevice MAC
14688c2ecf20Sopenharmony_ci	 * addresses will be copied to the CPU via this PGID.
14698c2ecf20Sopenharmony_ci	 */
14708c2ecf20Sopenharmony_ci	ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
14718c2ecf20Sopenharmony_ci	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
14728c2ecf20Sopenharmony_ci			 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
14738c2ecf20Sopenharmony_ci			 ANA_PORT_PORT_CFG, cpu);
14748c2ecf20Sopenharmony_ci
14758c2ecf20Sopenharmony_ci	/* Enable CPU port module */
14768c2ecf20Sopenharmony_ci	ocelot_fields_write(ocelot, cpu, QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
14778c2ecf20Sopenharmony_ci	/* CPU port Injection/Extraction configuration */
14788c2ecf20Sopenharmony_ci	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_XTR_HDR,
14798c2ecf20Sopenharmony_ci			    ocelot->xtr_prefix);
14808c2ecf20Sopenharmony_ci	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_INJ_HDR,
14818c2ecf20Sopenharmony_ci			    ocelot->inj_prefix);
14828c2ecf20Sopenharmony_ci
14838c2ecf20Sopenharmony_ci	/* Configure the CPU port to be VLAN aware */
14848c2ecf20Sopenharmony_ci	ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) |
14858c2ecf20Sopenharmony_ci				 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
14868c2ecf20Sopenharmony_ci				 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
14878c2ecf20Sopenharmony_ci			 ANA_PORT_VLAN_CFG, cpu);
14888c2ecf20Sopenharmony_ci}
14898c2ecf20Sopenharmony_ci
14908c2ecf20Sopenharmony_ciint ocelot_init(struct ocelot *ocelot)
14918c2ecf20Sopenharmony_ci{
14928c2ecf20Sopenharmony_ci	char queue_name[32];
14938c2ecf20Sopenharmony_ci	int i, ret;
14948c2ecf20Sopenharmony_ci	u32 port;
14958c2ecf20Sopenharmony_ci
14968c2ecf20Sopenharmony_ci	if (ocelot->ops->reset) {
14978c2ecf20Sopenharmony_ci		ret = ocelot->ops->reset(ocelot);
14988c2ecf20Sopenharmony_ci		if (ret) {
14998c2ecf20Sopenharmony_ci			dev_err(ocelot->dev, "Switch reset failed\n");
15008c2ecf20Sopenharmony_ci			return ret;
15018c2ecf20Sopenharmony_ci		}
15028c2ecf20Sopenharmony_ci	}
15038c2ecf20Sopenharmony_ci
15048c2ecf20Sopenharmony_ci	ocelot->lags = devm_kcalloc(ocelot->dev, ocelot->num_phys_ports,
15058c2ecf20Sopenharmony_ci				    sizeof(u32), GFP_KERNEL);
15068c2ecf20Sopenharmony_ci	if (!ocelot->lags)
15078c2ecf20Sopenharmony_ci		return -ENOMEM;
15088c2ecf20Sopenharmony_ci
15098c2ecf20Sopenharmony_ci	ocelot->stats = devm_kcalloc(ocelot->dev,
15108c2ecf20Sopenharmony_ci				     ocelot->num_phys_ports * ocelot->num_stats,
15118c2ecf20Sopenharmony_ci				     sizeof(u64), GFP_KERNEL);
15128c2ecf20Sopenharmony_ci	if (!ocelot->stats)
15138c2ecf20Sopenharmony_ci		return -ENOMEM;
15148c2ecf20Sopenharmony_ci
15158c2ecf20Sopenharmony_ci	mutex_init(&ocelot->stats_lock);
15168c2ecf20Sopenharmony_ci	mutex_init(&ocelot->ptp_lock);
15178c2ecf20Sopenharmony_ci	spin_lock_init(&ocelot->ptp_clock_lock);
15188c2ecf20Sopenharmony_ci	snprintf(queue_name, sizeof(queue_name), "%s-stats",
15198c2ecf20Sopenharmony_ci		 dev_name(ocelot->dev));
15208c2ecf20Sopenharmony_ci	ocelot->stats_queue = create_singlethread_workqueue(queue_name);
15218c2ecf20Sopenharmony_ci	if (!ocelot->stats_queue)
15228c2ecf20Sopenharmony_ci		return -ENOMEM;
15238c2ecf20Sopenharmony_ci
15248c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&ocelot->multicast);
15258c2ecf20Sopenharmony_ci	ocelot_mact_init(ocelot);
15268c2ecf20Sopenharmony_ci	ocelot_vlan_init(ocelot);
15278c2ecf20Sopenharmony_ci	ocelot_vcap_init(ocelot);
15288c2ecf20Sopenharmony_ci	ocelot_cpu_port_init(ocelot);
15298c2ecf20Sopenharmony_ci
15308c2ecf20Sopenharmony_ci	for (port = 0; port < ocelot->num_phys_ports; port++) {
15318c2ecf20Sopenharmony_ci		/* Clear all counters (5 groups) */
15328c2ecf20Sopenharmony_ci		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
15338c2ecf20Sopenharmony_ci				     SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
15348c2ecf20Sopenharmony_ci			     SYS_STAT_CFG);
15358c2ecf20Sopenharmony_ci	}
15368c2ecf20Sopenharmony_ci
15378c2ecf20Sopenharmony_ci	/* Only use S-Tag */
15388c2ecf20Sopenharmony_ci	ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
15398c2ecf20Sopenharmony_ci
15408c2ecf20Sopenharmony_ci	/* Aggregation mode */
15418c2ecf20Sopenharmony_ci	ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
15428c2ecf20Sopenharmony_ci			     ANA_AGGR_CFG_AC_DMAC_ENA |
15438c2ecf20Sopenharmony_ci			     ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
15448c2ecf20Sopenharmony_ci			     ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA, ANA_AGGR_CFG);
15458c2ecf20Sopenharmony_ci
15468c2ecf20Sopenharmony_ci	/* Set MAC age time to default value. The entry is aged after
15478c2ecf20Sopenharmony_ci	 * 2*AGE_PERIOD
15488c2ecf20Sopenharmony_ci	 */
15498c2ecf20Sopenharmony_ci	ocelot_write(ocelot,
15508c2ecf20Sopenharmony_ci		     ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
15518c2ecf20Sopenharmony_ci		     ANA_AUTOAGE);
15528c2ecf20Sopenharmony_ci
15538c2ecf20Sopenharmony_ci	/* Disable learning for frames discarded by VLAN ingress filtering */
15548c2ecf20Sopenharmony_ci	regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
15558c2ecf20Sopenharmony_ci
15568c2ecf20Sopenharmony_ci	/* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
15578c2ecf20Sopenharmony_ci	ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
15588c2ecf20Sopenharmony_ci		     SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
15598c2ecf20Sopenharmony_ci
15608c2ecf20Sopenharmony_ci	/* Setup flooding PGIDs */
15618c2ecf20Sopenharmony_ci	for (i = 0; i < ocelot->num_flooding_pgids; i++)
15628c2ecf20Sopenharmony_ci		ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
15638c2ecf20Sopenharmony_ci				 ANA_FLOODING_FLD_BROADCAST(PGID_MC) |
15648c2ecf20Sopenharmony_ci				 ANA_FLOODING_FLD_UNICAST(PGID_UC),
15658c2ecf20Sopenharmony_ci				 ANA_FLOODING, i);
15668c2ecf20Sopenharmony_ci	ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
15678c2ecf20Sopenharmony_ci		     ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
15688c2ecf20Sopenharmony_ci		     ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
15698c2ecf20Sopenharmony_ci		     ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
15708c2ecf20Sopenharmony_ci		     ANA_FLOODING_IPMC);
15718c2ecf20Sopenharmony_ci
15728c2ecf20Sopenharmony_ci	for (port = 0; port < ocelot->num_phys_ports; port++) {
15738c2ecf20Sopenharmony_ci		/* Transmit the frame to the local port. */
15748c2ecf20Sopenharmony_ci		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
15758c2ecf20Sopenharmony_ci		/* Do not forward BPDU frames to the front ports. */
15768c2ecf20Sopenharmony_ci		ocelot_write_gix(ocelot,
15778c2ecf20Sopenharmony_ci				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
15788c2ecf20Sopenharmony_ci				 ANA_PORT_CPU_FWD_BPDU_CFG,
15798c2ecf20Sopenharmony_ci				 port);
15808c2ecf20Sopenharmony_ci		/* Ensure bridging is disabled */
15818c2ecf20Sopenharmony_ci		ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
15828c2ecf20Sopenharmony_ci	}
15838c2ecf20Sopenharmony_ci
15848c2ecf20Sopenharmony_ci	/* Allow broadcast MAC frames. */
15858c2ecf20Sopenharmony_ci	for_each_nonreserved_multicast_dest_pgid(ocelot, i) {
15868c2ecf20Sopenharmony_ci		u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
15878c2ecf20Sopenharmony_ci
15888c2ecf20Sopenharmony_ci		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
15898c2ecf20Sopenharmony_ci	}
15908c2ecf20Sopenharmony_ci	ocelot_write_rix(ocelot,
15918c2ecf20Sopenharmony_ci			 ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)),
15928c2ecf20Sopenharmony_ci			 ANA_PGID_PGID, PGID_MC);
15938c2ecf20Sopenharmony_ci	ocelot_write_rix(ocelot,
15948c2ecf20Sopenharmony_ci			 ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)),
15958c2ecf20Sopenharmony_ci			 ANA_PGID_PGID, PGID_MCIPV4);
15968c2ecf20Sopenharmony_ci	ocelot_write_rix(ocelot,
15978c2ecf20Sopenharmony_ci			 ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)),
15988c2ecf20Sopenharmony_ci			 ANA_PGID_PGID, PGID_MCIPV6);
15998c2ecf20Sopenharmony_ci
16008c2ecf20Sopenharmony_ci	/* Allow manual injection via DEVCPU_QS registers, and byte swap these
16018c2ecf20Sopenharmony_ci	 * registers endianness.
16028c2ecf20Sopenharmony_ci	 */
16038c2ecf20Sopenharmony_ci	ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
16048c2ecf20Sopenharmony_ci			 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
16058c2ecf20Sopenharmony_ci	ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
16068c2ecf20Sopenharmony_ci			 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
16078c2ecf20Sopenharmony_ci	ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
16088c2ecf20Sopenharmony_ci		     ANA_CPUQ_CFG_CPUQ_LRN(2) |
16098c2ecf20Sopenharmony_ci		     ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
16108c2ecf20Sopenharmony_ci		     ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
16118c2ecf20Sopenharmony_ci		     ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
16128c2ecf20Sopenharmony_ci		     ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
16138c2ecf20Sopenharmony_ci		     ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
16148c2ecf20Sopenharmony_ci		     ANA_CPUQ_CFG_CPUQ_IGMP(6) |
16158c2ecf20Sopenharmony_ci		     ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
16168c2ecf20Sopenharmony_ci	for (i = 0; i < 16; i++)
16178c2ecf20Sopenharmony_ci		ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
16188c2ecf20Sopenharmony_ci				 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
16198c2ecf20Sopenharmony_ci				 ANA_CPUQ_8021_CFG, i);
16208c2ecf20Sopenharmony_ci
16218c2ecf20Sopenharmony_ci	INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work);
16228c2ecf20Sopenharmony_ci	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
16238c2ecf20Sopenharmony_ci			   OCELOT_STATS_CHECK_DELAY);
16248c2ecf20Sopenharmony_ci
16258c2ecf20Sopenharmony_ci	return 0;
16268c2ecf20Sopenharmony_ci}
16278c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ocelot_init);
16288c2ecf20Sopenharmony_ci
16298c2ecf20Sopenharmony_civoid ocelot_deinit(struct ocelot *ocelot)
16308c2ecf20Sopenharmony_ci{
16318c2ecf20Sopenharmony_ci	cancel_delayed_work(&ocelot->stats_work);
16328c2ecf20Sopenharmony_ci	destroy_workqueue(ocelot->stats_queue);
16338c2ecf20Sopenharmony_ci	mutex_destroy(&ocelot->stats_lock);
16348c2ecf20Sopenharmony_ci}
16358c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ocelot_deinit);
16368c2ecf20Sopenharmony_ci
16378c2ecf20Sopenharmony_civoid ocelot_deinit_port(struct ocelot *ocelot, int port)
16388c2ecf20Sopenharmony_ci{
16398c2ecf20Sopenharmony_ci	struct ocelot_port *ocelot_port = ocelot->ports[port];
16408c2ecf20Sopenharmony_ci
16418c2ecf20Sopenharmony_ci	skb_queue_purge(&ocelot_port->tx_skbs);
16428c2ecf20Sopenharmony_ci}
16438c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ocelot_deinit_port);
16448c2ecf20Sopenharmony_ci
16458c2ecf20Sopenharmony_ciMODULE_LICENSE("Dual MIT/GPL");
1646